Esempio n. 1
0
        public void ContainsPoint()
        {
            Polygon2f polygon = CreatePolygon2.FromBox(new Vector2f(-2), new Vector2f(2));
            Polygon2f hole    = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1));

            hole.MakeCW();

            polygon.AddHole(hole);

            PolygonIntersection2.PushPolygon(polygon);

            //in polygon
            Assert.IsTrue(PolygonIntersection2.ContainsPoint(new Vector2f(1.5f, 1.5f)));
            Assert.IsTrue(PolygonIntersection2.ContainsPoint(new Vector2f(1.5f, -1.5f)));
            Assert.IsTrue(PolygonIntersection2.ContainsPoint(new Vector2f(-1.5f, 1.5f)));
            Assert.IsTrue(PolygonIntersection2.ContainsPoint(new Vector2f(-1.5f, -1.5f)));

            //in hole
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(0, 0)));

            //on polygon boundary
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(2, 2)));
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(2, -2)));
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(-2, 2)));
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(-2, -2)));

            //on hole boundary
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(1, 1)));
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(1, -1)));
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(-1, 1)));
            Assert.IsFalse(PolygonIntersection2.ContainsPoint(new Vector2f(-1, -1)));

            PolygonIntersection2.PopPolygon();
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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));
        }
Esempio n. 6
0
        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);
            }
        }
Esempio n. 7
0
        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);
                    }
                }
            }
        }