public void Intersection() { Polygon2f A = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)); Polygon2f B = CreatePolygon2.FromBox(new Vector2f(0), new Vector2f(2)); List <Polygon2f> list; PolygonBoolean2.Intersection(A, B, out list); Assert.AreEqual(1, list.Count); Assert.AreEqual(0, list[0].HoleCount); Assert.AreEqual(1, list[0].Area); }
public void SymmetricDifference() { Polygon2f A = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)); Polygon2f B = CreatePolygon2.FromBox(new Vector2f(0), new Vector2f(2)); List <Polygon2f> list; PolygonBoolean2.SymmetricDifference(A, B, out list); Assert.AreEqual(1, list.Count); Assert.AreEqual(1, list[0].HoleCount); Assert.AreEqual(6, list[0].Area); }
public void CreateInteriorSkeleton() { Polygon2f polygon = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)); var constructor = new HBMeshConstructor <HBVertex2f, HBEdge, HBFace>(); PolygonSkeleton2.CreateInteriorSkeleton(polygon, constructor); var mesh = constructor.PopMesh(); Assert.AreEqual(5, mesh.Vertices.Count); Assert.AreEqual(8, mesh.Edges.Count); Assert.AreEqual(0, mesh.Faces.Count); }
public static void Triangulate <MESH>(Polygon2f polygon, IMeshConstructor <MESH> constructor, ConformingCriteria crit = new ConformingCriteria()) { InsertPolygon(polygon); InsertSeeds(crit.seeds); Box2f bounds = Box2f.CalculateBounds(polygon.Positions); CGAL_InsertSeed(bounds.Min - 0.1f); MeshDescriptor des = Triangulate(crit.iterations, crit.angBounds, crit.lenBounds); CreateMesh(constructor, des); CGAL_Clear(); }
public void DoIntersect() { Polygon2f A = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)); Polygon2f B = CreatePolygon2.FromBox(new Vector2f(0), new Vector2f(2)); Polygon2f C = CreatePolygon2.FromBox(new Vector2f(-1, 1), new Vector2f(1, 3)); Polygon2f D = CreatePolygon2.FromBox(new Vector2f(-3, -1), new Vector2f(-2, 1)); //Intersect Assert.IsTrue(PolygonBoolean2.DoIntersect(A, B)); //Edge case Assert.IsFalse(PolygonBoolean2.DoIntersect(A, C)); //Dont intersect Assert.IsFalse(PolygonBoolean2.DoIntersect(A, D)); }
public static void Triangulate <MESH>(Polygon2f polygon, IMeshConstructor <MESH> constructor) { Insert(polygon); MeshDescriptor descriptor; if (CGAL_Triangulate(out descriptor) != SUCCESS) { throw new Exception("Error triangulating points."); } PolygonIntersection2.PushPolygon(polygon); CreateMesh(constructor, descriptor); PolygonIntersection2.PopPolygon(); CGAL_Clear(); }
protected override void OnPolygonComplete(Polygon2f input) { polygon = input; polygon.MakeCCW(); polygon.BuildIndices(); polygon.BuildHoleIndices(); if (polygon.IsSimple && !polygon.HasHoles) { simplified = PolygonSimplify2.Simplify(polygon, 0.5f, SIMPLIFY_METHOD.SQUARE_DIST); simplified.BuildIndices(); } else { simplified = null; } }
public static Polygon2f FindHull(Vector2f[] points) { CGAL_LoadPoints(points, points.Length); int size = CGAL_FindHull(); Polygon2f hull = new Polygon2f(size); for (int i = 0; i < size; i++) { hull.Positions[i] = CGAL_GetHullVector2f(i); } CGAL_Clear(); hull.CalculatePolygon(); return(hull); }
public void FindHull() { Vector2f[] points = new Vector2f[] { new Vector2f(0, 0), new Vector2f(10, 0), new Vector2f(10, 10), new Vector2f(6, 5), new Vector2f(4, 1) }; Polygon2f convex = ConvexHull2.FindHull(points); Assert.AreEqual(3, convex.VerticesCount); Assert.AreEqual(points[0], convex.Positions[0]); Assert.AreEqual(points[1], convex.Positions[1]); Assert.AreEqual(points[2], convex.Positions[2]); Assert.IsTrue(ConvexHull2.IsStronglyConvex(convex.Positions, convex.IsCCW)); }
public void Triangulate() { Polygon2f polygon = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)); Mesh2f mesh = ConstraintedTriangulation2.Triangulate(polygon); Assert.AreEqual(4, mesh.VerticesCount); Assert.AreEqual(6, mesh.IndicesCount); for (int i = 0; i < mesh.IndicesCount / 3; i++) { Vector2f a = mesh.Positions[mesh.Indices[i * 3 + 0]]; Vector2f b = mesh.Positions[mesh.Indices[i * 3 + 1]]; Vector2f c = mesh.Positions[mesh.Indices[i * 3 + 2]]; Triangle2f tri = new Triangle2f(a, b, c); Assert.IsTrue(tri.SignedArea > 0); } }
protected override void OnPolygonComplete(Polygon2f input) { polygon = input; polygon.MakeCCW(); polygon.BuildIndices(); polygon.BuildHoleIndices(); if (polygon.IsSimple) { var constructor = new HBMeshConstructor <HBVertex2f, HBEdge, HBFace>(); PolygonSkeleton2.CreateInteriorSkeleton(polygon, constructor); var mesh = constructor.PopMesh(); line = HBMeshConversion.ToIndexableMesh2f(mesh); } else { line = null; } }
protected override void OnPolygonComplete(Polygon2f input) { polygon = input; polygon.MakeCCW(); polygon.BuildIndices(); polygon.BuildHoleIndices(); if (polygon.IsSimple && !polygon.HasHoles) { Polygon2f A = shape; Polygon2f B = polygon; sum = MinkowskiSums2.ComputeSum(A, B); sum.BuildIndices(); sum.BuildHoleIndices(); } else { sum = null; } }
protected void DrawPolygon(Camera cam, Polygon2f polygon, Color lineColor, Color vertColor, Matrix4x4f m) { if (polygon == null) { return; } DrawLines.LineMode = LINE_MODE.LINES; DrawVertices.Orientation = DRAW_ORIENTATION.XY; DrawLines.Draw(cam, polygon.Positions, lineColor, m, polygon.Indices); DrawVertices.Draw(cam, 0.02f, polygon.Positions, vertColor, m); if (!polygon.HasHoles) { return; } foreach (var hole in polygon.Holes) { DrawLines.Draw(cam, hole.Positions, lineColor, m, hole.Indices); DrawVertices.Draw(cam, 0.02f, hole.Positions, vertColor, m); } }
protected override void OnPolygonComplete(Polygon2f input) { polygon = input; polygon.MakeCCW(); polygon.BuildIndices(); polygon.BuildHoleIndices(); if (polygon.IsSimple && !polygon.HasHoles) { partition = PolygonPartition2.Partition(polygon, PARTITION_METHOD.OPTIMAL); } else { partition = new List <Polygon2f>(); } foreach (var poly in partition) { poly.MakeCCW(); poly.BuildIndices(); poly.BuildHoleIndices(); } }
protected virtual void Update() { if (Input.GetKeyDown(KeyCode.F1)) { ResetInput(); MadePolygon = true; OnPolygonComplete(CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1))); } else if (Input.GetKeyDown(KeyCode.F2)) { ResetInput(); Polygon2f box = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)); Polygon2f hole = CreatePolygon2.FromBox(new Vector2f(-0.5f), new Vector2f(0.5f)); hole.MakeCW(); box.AddHole(hole); MadePolygon = true; OnPolygonComplete(box); } else if (Input.GetKeyDown(KeyCode.F3)) { ResetInput(); Polygon2f circle = CreatePolygon2.FromCircle(new Vector2f(-1), 1, 16); MadePolygon = true; OnPolygonComplete(circle); } else if (Input.GetKeyDown(KeyCode.F4)) { ResetInput(); Polygon2f circle = CreatePolygon2.FromCircle(new Vector2f(0), 1, 16); Polygon2f hole = CreatePolygon2.FromCircle(new Vector2f(0), 0.5f, 16); hole.MakeCW(); circle.AddHole(hole); MadePolygon = true; OnPolygonComplete(circle); } else if (Input.GetKeyDown(KeyCode.F5)) { ResetInput(); Polygon2f capsule = CreatePolygon2.FromCapsule(new Vector2f(0), 1, 1, 16); MadePolygon = true; OnPolygonComplete(capsule); } else if (Input.GetKeyDown(KeyCode.F6)) { ResetInput(); Polygon2f cathedral = CreatePolygon2.FromCapsule(new Vector2f(0), 1, 1, 16); for (float x = -0.5f; x <= 0.5f; x += 1.0f) { for (float y = -0.75f; y <= 0.75f; y += 0.5f) { Polygon2f pillar = CreatePolygon2.FromCircle(new Vector2f(x, y), 0.1f, 16); pillar.MakeCW(); cathedral.AddHole(pillar); } } MadePolygon = true; OnPolygonComplete(cathedral); } else { bool leftMouseClicked = Input.GetMouseButtonDown(0); if (leftMouseClicked) { Vector2f point = GetMousePosition(); OnLeftClick(point); } if (Input.GetKeyDown(KeyCode.Space)) { ResetInput(); OnPolygonCleared(); } else if (!MadePolygon) { Vector2f point = GetMousePosition(); point = SnapToPolygon(point); if (leftMouseClicked) { if (Points == null) { CreatePoints(); AddPoint(point); AddPoint(point); } else { if (PolygonClosed()) { ClosePolygon(); MadePolygon = true; OnPolygonComplete(new Polygon2f(Points.ToArray())); } else { AddPoint(point); } } } else { MoveLastPoint(point); } } } }
protected virtual void OnPolygonComplete(Polygon2f input) { }
protected void DrawPolygon(Camera cam, Polygon2f polygon, Color lineColor, Color vertColor) { DrawPolygon(cam, polygon, lineColor, vertColor, Matrix4x4f.Identity); }
protected override void OnPolygonComplete(Polygon2f _input) { input = _input; input.MakeCCW(); input.BuildIndices(); input.BuildHoleIndices(); ResetInput(); if (!input.IsSimple) { return; } if (polygons.Count == 0) { polygons.Add(input); } else if (polygons[0].IsSimple) { //Only does the boolean of the first polygon //in scene and the input polygon. //Both polyons must be simple. Polygon2f A = polygons[0]; Polygon2f B = input; polygons.Clear(); List <Polygon2f> output = new List <Polygon2f>(); bool b = false; switch (mode) { case BOOL_MODE.UNION: b = PolygonBoolean2.Union(A, B, output); break; case BOOL_MODE.INTERSECTION: b = PolygonBoolean2.Intersection(A, B, output); break; case BOOL_MODE.DIFFERENCE: b = PolygonBoolean2.Difference(A, B, output); break; case BOOL_MODE.SYM_DIFFERENCE: b = PolygonBoolean2.SymmetricDifference(A, B, output); break; } if (b) { foreach (var polygon in output) { polygons.Add(polygon); polygon.BuildIndices(); polygon.BuildHoleIndices(); } } } }
protected override void OnPolygonCleared() { polygon = null; visibility.Clear(); }