-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathExample7.cs
More file actions
57 lines (44 loc) · 1.77 KB
/
Example7.cs
File metadata and controls
57 lines (44 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace TriangleNet.Examples
{
using System.Linq;
using TriangleNet;
using TriangleNet.Geometry;
using TriangleNet.Meshing.Iterators;
using TriangleNet.Rendering.Text;
using TriangleNet.Tools;
using TriangleNet.Topology;
/// <summary>
/// Boolean operations on mesh regions (intersection, difference, xor).
/// </summary>
public class Example7 : IExample
{
public bool Run(bool print = false)
{
// Generate the input geometry.
var polygon = new Polygon(8, true);
// Two intersecting rectangles.
var A = Generate.Rectangle(0d, 0d, 4d, 4d, label: 1);
var B = Generate.Rectangle(1d, 1d, 4d, 4d, label: 2);
polygon.Add(A);
polygon.Add(B);
// Generate mesh.
var mesh = (Mesh)polygon.Triangulate();
if (print) SvgImage.Save(mesh, "example-7.svg", 500);
// Find a seeding triangle (in this case, the point (2, 2) lies in
// both rectangles).
var seed = (new TriangleQuadTree(mesh)).Query(2.0, 2.0) as Triangle;
var iterator = new RegionIterator();
iterator.Process(seed, t => t.Label ^= 1, 1);
iterator.Process(seed, t => t.Label ^= 2, 2);
// At this point, all triangles will have label 1, 2 or 3 (= 1 xor 2).
// The intersection of A and B.
var intersection = mesh.Triangles.Where(t => t.Label == 3);
// The difference A \ B.
var difference = mesh.Triangles.Where(t => t.Label == 1);
// The xor of A and B.
var xor = mesh.Triangles.Where(t => t.Label == 1 || t.Label == 2);
return intersection.Any() && difference.Any() && xor.Any();
}
}
}