Пример #1
0
        public void TestIntersectsWithLine()
        {
            // polygons to intersect with
            Polygon3D t = new Polygon3D(new List <Point3D> {
                t1_1, t1_2, t1_3
            }, "t");
            Polygon3D s = new Polygon3D(new List <Point3D> {
                s1_1, s1_2, s1_3, s1_4
            }, "s");

            // --
            // line in the same plane as polygons
            // --

            // line going through polygons in the same plane
            Point3D start = new Point3D(1, 4, 0);
            Point3D end   = new Point3D(1, -3, 0);

            Assert.AreEqual(t.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(s.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, false), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, false), false);

            // line not going through the polygons in the same plane
            start = new Point3D(4, 4, 0);
            end   = new Point3D(4, -3, 0);
            Assert.AreEqual(t.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(s.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, false), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, false), false);

            // line starting in polygon going out in the same plane
            start = new Point3D(2, 2, 0);
            end   = new Point3D(2, -0.5, 0);
            Assert.AreEqual(t.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(s.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, false), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, false), false);

            // line inside square in the same plane
            start = new Point3D(0.5, 0.5, 0);
            end   = new Point3D(2, 0.5, 0);
            Assert.AreEqual(s.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, false), false);

            // line inside triangle in the same plane
            start = new Point3D(2, 2, 0);
            end   = new Point3D(2, -0.5, 0);
            Assert.AreEqual(t.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, false), false);

            // line = border of square in the same plane
            start = s1_1;
            end   = s1_2;
            Assert.AreEqual(s.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(s.IntersectsWith(start, end, 0, false), false);

            // line = border of triangle in the same plane
            start = t1_1;
            end   = t1_2;
            Assert.AreEqual(t.IntersectionWith(start, end).IsNaN(), true);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, true), false);
            Assert.AreEqual(t.IntersectsWith(start, end, 0, false), false);

            // --
            // now lines which are not in plane of the polygons in fact all lines in z-axis direction
            // --

            // three cases of line intersecting square at s1_4 (0, 3, 0)
            start = new Point3D(0, 3, -2);
            end   = new Point3D(0, 3, 2);
            Assert.AreEqual(s.IntersectionWith(start, end), s1_4);
            Assert.IsTrue(s.IntersectsWith(start, end, 0, true));
            Assert.IsTrue(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(0, 3, -2);
            end   = new Point3D(0, 3, 0);
            Assert.AreEqual(s.IntersectionWith(start, end), s1_4);
            Assert.IsTrue(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(0, 3, -2);
            end   = new Point3D(0, 3, -1);
            Assert.IsTrue(s.IntersectionWith(start, end).IsNaN());
            Assert.IsFalse(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            // three cases of line intersecting square at (1, 4, 0) - no intersection
            start = new Point3D(1, 4, -2);
            end   = new Point3D(1, 4, 2);
            Assert.IsTrue(s.IntersectionWith(start, end).IsNaN());
            Assert.IsFalse(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(1, 4, -2);
            end   = new Point3D(1, 4, 0);
            Assert.IsTrue(s.IntersectionWith(start, end).IsNaN());
            Assert.IsFalse(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(1, 4, -2);
            end   = new Point3D(1, 4, -1);
            Assert.IsTrue(s.IntersectionWith(start, end).IsNaN());
            Assert.IsFalse(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            // three cases of line intersecting square on the edge (1, 3, 0)
            Point3D isec = new Point3D(1, 3, 0);

            start = new Point3D(1, 3, -2);
            end   = new Point3D(1, 3, 2);
            Assert.AreEqual(s.IntersectionWith(start, end), isec);
            Assert.IsTrue(s.IntersectsWith(start, end, 0, true));
            Assert.IsTrue(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(1, 3, -2);
            end   = new Point3D(1, 3, 0);
            Assert.AreEqual(s.IntersectionWith(start, end), isec);
            Assert.IsTrue(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(1, 3, -2);
            end   = new Point3D(1, 3, -1);
            Assert.IsTrue(s.IntersectionWith(start, end).IsNaN());
            Assert.IsFalse(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            // three cases of line intersecting square insede (2, 2, 0)
            isec  = new Point3D(2, 2, 0);
            start = new Point3D(2, 2, -2);
            end   = new Point3D(2, 2, 2);
            Assert.AreEqual(s.IntersectionWith(start, end), isec);
            Assert.IsTrue(s.IntersectsWith(start, end, 0, true));
            Assert.IsTrue(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(2, 2, -2);
            end   = new Point3D(2, 2, 0);
            Assert.AreEqual(s.IntersectionWith(start, end), isec);
            Assert.IsTrue(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            start = new Point3D(2, 2, -2);
            end   = new Point3D(2, 2, -1);
            Assert.IsTrue(s.IntersectionWith(start, end).IsNaN());
            Assert.IsFalse(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));

            // triangle not tested, assuming it will behave the same way

            // --
            // now lines paralel to the plane of the polygon
            // --
            start = new Point3D(1, 4, 1);
            end   = new Point3D(1, -3, 1);
            Assert.IsTrue(s.IntersectionWith(start, end).IsNaN());
            Assert.IsFalse(s.IntersectsWith(start, end, 0, true));
            Assert.IsFalse(s.IntersectsWith(start, end, 0, false));
        }