Пример #1
0
        public void test_with_holes()
        {
            List <xy>         p1    = ut.MakePoly(new xy(0, 0), new xy(4, 0), new xy(4, 4), new xy(0, 4));
            List <xy>         h1    = ut.MakePoly(new xy(1, 1), new xy(1, 3), new xy(3, 3), new xy(3, 1));
            List <xy>         p2    = ut.MakePoly(new xy(0, 2), new xy(4, 2), new xy(4, 4), new xy(0, 4));
            List <List <xy> > both1 = new List <List <xy> >();

            both1.Add(p1);
            both1.Add(h1);
            ppi2d pi1 = new ppi2d(both1, p2);

            Assert.AreEqual(2 * 4 - 2 * 1, ut.PolygonArea2d(ppi2d.FindTheLoops(pi1.GetIntersection())));

            ppi2d pi2 = new ppi2d(p2, both1);

            Assert.AreEqual(2 * 4 - 2 * 1, ut.PolygonArea2d(ppi2d.FindTheLoops(pi2.GetIntersection())));

            foreach (seginfo2d si in pi2.p1.info)
            {
                Assert.IsNotNull(si);
                Assert.IsNotNull(si.seg);
                string q = si.ToString();
                Assert.IsNotNull(q);
            }
        }
Пример #2
0
        public void triangle_touching_square_at_vertex()
        {
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(4, 0), new xy(4, 4), new xy(0, 4));
            List <xy> p2 = ut.MakePoly(new xy(3, 3), new xy(5, 5), new xy(3, 7));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(4 * 4 + 2 * 4 / 2 - 0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(4 * 4 - 0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(2 * 4 / 2 - 0.5, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }
Пример #3
0
        public void triangle_inside_square()
        {
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(4, 0), new xy(4, 4), new xy(0, 4));
            List <xy> p2 = ut.MakePoly(new xy(1, 1), new xy(3, 1), new xy(2, 3));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(ut.PolygonArea2d(p2), ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(4 * 4, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(4 * 4 - ut.PolygonArea2d(p2), ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(0, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }
Пример #4
0
        public void simple()
        {
            // two 4x4 squares, overlap area is 2x2
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(4, 0), new xy(4, 4), new xy(0, 4));
            List <xy> p2 = ut.MakePoly(new xy(2, 2), new xy(6, 2), new xy(6, 6), new xy(2, 6));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(4 * 4 + 4 * 4 - 2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(4 * 4 - 2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(4 * 4 - 2 * 2, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }
Пример #5
0
        public void nasty_case()
        {
            List <xy> p1 = ut.MakePoly(new xy(0, 0), new xy(10, 0), new xy(10, 1), new xy(0, 1), new xy(0, 2), new xy(5, 2), new xy(5, 3), new xy(0, 3), new xy(0, 4), new xy(10, 4), new xy(10, 5), new xy(0, 5), new xy(0, 6), new xy(5, 6), new xy(5, 7), new xy(0, 7), new xy(0, 8), new xy(10, 8), new xy(10, 9), new xy(0, 9), new xy(0, 10), new xy(10, 10), new xy(10, 11), new xy(-1, 11), new xy(-1, 0));
            List <xy> p2 = ut.MakePoly(new xy(5, 0), new xy(6, 0), new xy(6, 11), new xy(5, 11));

            ppi2d pi = new ppi2d(p1, p2);

            Assert.AreEqual(11, ut.PolygonArea2d(p2));
            Assert.AreEqual(11 + 1 + 6 + 1 + 11 + 1 + 6 + 1 + 11 + 1 + 11, ut.PolygonArea2d(p1));

            Assert.AreEqual(4, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetIntersection())));
            Assert.AreEqual(ut.PolygonArea2d(p1) + 3 + 3 + 1, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetUnion())));
            Assert.AreEqual(ut.PolygonArea2d(p1) - 4, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference1())));
            Assert.AreEqual(7, ut.PolygonArea2d(ppi2d.FindTheLoops(pi.GetDifference2())));
        }
Пример #6
0
        /*
         * This method is only used by the unit tests.  It exists to provide
         * a wrapper for the new ppi2d code to present the API from the old
         * ppi2d code.  Without this wrapper, I would need to rewrite all
         * the ppi2d unit tests.
         * */
        public static List <List <xy> > Polygon2d_Intersection(List <xy> poly1, List <xy> poly2, PolyPolyOp op)
        {
            List <List <xy> > result = new List <List <xy> >();

            Debug.Assert(
                (op == PolyPolyOp.Intersection) ||
                (op == PolyPolyOp.Union) ||
                (op == PolyPolyOp.Difference)
                );
            ppi2d pi = new ppi2d(poly1, poly2);

            switch (op)
            {
            case PolyPolyOp.Intersection:
            {
                ppi2d.FindTheLoops(result, pi.GetIntersection());
                break;
            }

            case PolyPolyOp.Union:
            {
                ppi2d.FindTheLoops(result, pi.GetUnion());
                break;
            }

            case PolyPolyOp.Difference:
            {
                ppi2d.FindTheLoops(result, pi.GetDifference1());
                break;
            }
            }
            if (
                (result == null) ||
                (result.Count == 0)
                )
            {
                return(null);
            }
            else
            {
                return(result);
            }
        }
Пример #7
0
        public static void PartitionFace_HandleOverlaps(List <seg3d> segs, Face f1, Solid s2, bool reverse)
        {
            xyz n1 = f1.UnitNormal();

            List <Face> faces = new List <Face>();

            for (int ndx_f2 = 0; ndx_f2 < s2.Faces.Count; ndx_f2++)
            {
                Face f2 = s2.Faces[ndx_f2];
                xyz  n2 = f2.UnitNormal();
                if (
                    (fp.eq_unitvec(n1, n2) && !reverse) ||
                    (fp.eq_unitvec(n1, -n2) && reverse)
                    )
                {
                    xyz p1 = f1.myPlane.pts[0];
                    xyz p2 = null;
                    foreach (HalfEdge he2 in f2.MainLoop)
                    {
                        if (!fp.eq_inches(he2.to, p1))
                        {
                            p2 = he2.to;
                            break;
                        }
                    }
                    Debug.Assert(p2 != null);
                    double d = xyz.dotsub(n1, p2, p1);
                    if (fp.eq_dot_distancetoplane(d, 0))
                    {
                        // the faces are in the same plane

                        faces.Add(f2);
                    }
                }
            }

            if (faces.Count > 0)
            {
                foreach (Face f2 in faces)
                {
                    List <List <xy> > loops2d = f1.myPlane.Convert3dLoopsTo2d(f2.GetLoopsIn3d());

                    if (reverse)
                    {
                        foreach (List <xy> loop in loops2d)
                        {
                            loop.Reverse();
                        }
                    }

                    ppi2d pi = new ppi2d(f1.myPlane.loops2d, loops2d);

                    List <seg2d> segs_common1_2d = pi.GetIntersection();

                    if (segs_common1_2d.Count > 0)
                    {
                        foreach (seg2d s in segs_common1_2d)
                        {
                            xyz a = f1.myPlane.Convert2dPointTo3d(s.a);
                            xyz b = f1.myPlane.Convert2dPointTo3d(s.b);
                            segs_Remove(segs, new seg3d(a, b));
                        }

                        if (!reverse)
                        {
                            List <seg2d> segs_f2 = new List <seg2d>();
                            pi.p2.GetSegmentsReversed(segs_f2, SegmentInfo.Inside);
                            foreach (seg2d s in segs_f2)
                            {
                                xyz a = f1.myPlane.Convert2dPointTo3d(s.a);
                                xyz b = f1.myPlane.Convert2dPointTo3d(s.b);
                                segs_Add(segs, new seg3d(a, b));
                            }
                        }
                    }
                }
            }
        }