Exemple #1
0
        public void CircleIntersectionWithCircle3DTest()
        {
            // Touching circles
            Circle3d c1 = new Circle3d(new Point3d(0, 0, 0), 5, new Vector3d(0, 0, 1));
            Circle3d c2 = new Circle3d(new Point3d(5, 0, 0), 5, new Vector3d(1, 0, 0));

            Assert.AreEqual(c1.IntersectionWith(c2), new Point3d(5, 0, 0));

            // Touching circles
            c2 = new Circle3d(new Point3d(10, 0, 0), 5, new Vector3d(0, 1, 0));
            Assert.AreEqual(c1.IntersectionWith(c2), new Point3d(5, 0, 0));

            // Intersecting circles
            c2 = new Circle3d(new Point3d(0, 0, 0), 5, new Vector3d(0, 1, 0));
            Segment3d s = new Segment3d(new Point3d(-5, 0, 0), new Point3d(5, 0, 0));

            Assert.AreEqual(c1.IntersectionWith(c2), s);

            // Intersecting circles
            c2 = new Circle3d(new Point3d(5, 0, 0), 5, new Vector3d(0, 1, 0));
            s  = new Segment3d(new Point3d(0, 0, 0), new Point3d(5, 0, 0));
            Assert.AreEqual(c1.IntersectionWith(c2), s);

            // Intersecting circles
            c2 = new Circle3d(new Point3d(0, 0, 4), 5, new Vector3d(0, 1, 0));
            s  = new Segment3d(new Point3d(-3, 0, 0), new Point3d(3, 0, 0));
            Assert.AreEqual(c1.IntersectionWith(c2), s);
        }
Exemple #2
0
        public void CircleIntersectionWithCircle2DTest()
        {
            // parallel obecjts
            Circle3d c1 = new Circle3d(new Point3d(0, 0, 0), 5, new Vector3d(0, 0, 1));
            Circle3d c2 = new Circle3d(new Point3d(5, 0, 1), 5, new Vector3d(0, 0, 1));

            Assert.AreEqual(c1.IntersectionWith(c2), null);

            // Coincided circles
            c2 = new Circle3d(new Point3d(0, 0, 0), 5, new Vector3d(0, 0, 1));
            Assert.AreEqual(c1.IntersectionWith(c2), c1);

            // Separated circles
            c2 = new Circle3d(new Point3d(10, 0, 0), 2, new Vector3d(0, 0, 1));
            Assert.AreEqual(c1.IntersectionWith(c2), null);

            // Outer tangency
            c2 = new Circle3d(new Point3d(10, 0, 0), 5, new Vector3d(0, 0, 1));
            Assert.AreEqual(c1.IntersectionWith(c2), new Point3d(5, 0, 0));

            // Inner tangency 1
            c2 = new Circle3d(new Point3d(3, 0, 0), 2, new Vector3d(0, 0, 1));
            Assert.AreEqual(c1.IntersectionWith(c2), new Point3d(5, 0, 0));

            // Inner tangency 2
            c2 = new Circle3d(new Point3d(-2, 0, 0), 7, new Vector3d(0, 0, 1));
            Assert.AreEqual(c1.IntersectionWith(c2), new Point3d(5, 0, 0));

            // Intersection
            c2 = new Circle3d(new Point3d(6, 0, 0), 5, new Vector3d(0, 0, 1));
            Segment3d s = new Segment3d(new Point3d(3, 4, 0), new Point3d(3, -4, 0));

            Assert.AreEqual(c1.IntersectionWith(c2), s);
        }
        public void SphereIntersectionWithSegmentRelativeTest()
        {
            double tol  = GeometRi3D.Tolerance;
            bool   mode = GeometRi3D.UseAbsoluteTolerance;

            GeometRi3D.Tolerance            = 0.01;
            GeometRi3D.UseAbsoluteTolerance = false;

            Sphere    s = new Sphere(new Point3d(0, 0, 0), 5);
            Segment3d l = new Segment3d(new Point3d(10, 12, 13), new Point3d(22, 23, 24));

            Assert.IsTrue(s.IntersectionWith(l) == null);

            l = new Segment3d(new Point3d(-2, -2, -1), new Point3d(1, 2, 2));
            Assert.IsTrue((Segment3d)s.IntersectionWith(l) == l);

            l = new Segment3d(new Point3d(5.01, 0, 0), new Point3d(25, 0, 0));
            Assert.IsTrue((Point3d)s.IntersectionWith(l) == new Point3d(5, 0, 0));

            l = new Segment3d(new Point3d(0, 0, 0), new Point3d(25, 0, 0));
            Assert.IsTrue((Segment3d)s.IntersectionWith(l) == new Segment3d(new Point3d(0, 0, 0), new Point3d(5, 0, 0)));

            // Resore initial state
            GeometRi3D.UseAbsoluteTolerance = mode;
            GeometRi3D.Tolerance            = tol;
        }
        public void RayIntersectionWithSegmentTest()
        {
            Segment3d s = new Segment3d(new Point3d(0, 0, 0), new Point3d(10, 0, 0));
            Ray3d     r = new Ray3d(new Point3d(-1, 0, 0), new Vector3d(1, 0, 0));

            Assert.AreEqual(r.IntersectionWith(s), s);

            r = new Ray3d(new Point3d(0, 0, 0), new Vector3d(1, 0, 0));
            Assert.AreEqual(r.IntersectionWith(s), s);

            r = new Ray3d(new Point3d(1, 0, 0), new Vector3d(1, 0, 0));
            Assert.AreEqual(r.IntersectionWith(s), new Segment3d(new Point3d(1, 0, 0), new Point3d(10, 0, 0)));

            r = new Ray3d(new Point3d(5, 0, 0), new Vector3d(-1, 0, 0));
            Assert.AreEqual(r.IntersectionWith(s), new Segment3d(new Point3d(0, 0, 0), new Point3d(5, 0, 0)));

            r = new Ray3d(new Point3d(10, 0, 0), new Vector3d(1, 0, 0));
            Assert.AreEqual(r.IntersectionWith(s), new Point3d(10, 0, 0));

            r = new Ray3d(new Point3d(0, 0, 0), new Vector3d(-1, 0, 0));
            Assert.AreEqual(r.IntersectionWith(s), new Point3d(0, 0, 0));

            r = new Ray3d(new Point3d(1, 1, 0), new Vector3d(1, -1, 0));
            Assert.AreEqual(r.IntersectionWith(s), new Point3d(2, 0, 0));

            r = new Ray3d(new Point3d(1, 1, 0), new Vector3d(1, -1, 1));
            Assert.IsNull(r.IntersectionWith(s));
        }
        public void SegmentProjectionToLineTest2()
        {
            Line3d    l = new Line3d(new Point3d(1, 1, 1), new Vector3d(0, 0, 1));
            Segment3d r = new Segment3d(new Point3d(-1, -3, -2), new Point3d(-5, 1, -2));

            Assert.IsTrue((Point3d)r.ProjectionTo(l) == new Point3d(1, 1, -2));
        }
        private ToolpathPreviewJoint GenerateRightBevel(Segment3d segBefore, Segment3d segAfter, TPrintVertex printVertex, ToolpathPreviewMesh mesh)
        {
            CreateFrames(segBefore, segAfter, out var frameMiter, out var frameSegBefore, out var frameSegAfter);

            var joint = new ToolpathPreviewJoint();

            joint.InTop = joint.OutTop = mesh.AddVertex(vertexFactory(printVertex,
                                                                      frameMiter.FromFrameP(DiamondCrossSection.Top(printVertex.Dimensions)), brightnessMax));

            if (CornerIsInsideTube(segBefore, segAfter, printVertex.Dimensions.x))
            {
                AddLeftSquare(mesh, printVertex, ref frameSegBefore, ref frameSegAfter, joint);
            }
            else
            {
                AddLeftMiter(mesh, printVertex, ref frameMiter, ref frameSegBefore, joint);
            }

            joint.InBottom = joint.OutBottom = mesh.AddVertex(vertexFactory(printVertex,
                                                                            frameMiter.FromFrameP(DiamondCrossSection.Bottom(printVertex.Dimensions)), brightnessMax));

            double bevelDistance = GetBevelDistance(ref segBefore, ref segAfter, printVertex.Dimensions);

            joint.InRight = mesh.AddVertex(vertexFactory(printVertex,
                                                         frameSegBefore.FromFrameP(DiamondCrossSection.Right(printVertex.Dimensions, 1, bevelDistance)), brightnessMin));

            joint.OutRight = mesh.AddVertex(vertexFactory(printVertex,
                                                          frameSegAfter.FromFrameP(DiamondCrossSection.Right(printVertex.Dimensions, 1, -bevelDistance)), brightnessMin));

            mesh.AddTriangle(joint.InRight, joint.OutRight, joint.InTop);
            mesh.AddTriangle(joint.InRight, joint.InBottom, joint.OutRight);

            return(joint);
        }
        public void EllipsoidProjectionToLineTest_2()
        {
            Point3d   p  = new Point3d(0, 0, 0);
            Vector3d  v1 = new Vector3d(4, 0, 0);
            Vector3d  v2 = new Vector3d(0, 6, 0);
            Vector3d  v3 = new Vector3d(0, 0, 9);
            Ellipsoid e  = new Ellipsoid(p, v1, v2, v3);

            p  = new Point3d(1, 1, 1);
            v1 = new Vector3d(1, 1, 3);
            Line3d    l = new Line3d(p, v1);
            Segment3d s = e.ProjectionTo(l);

            // Construct plane orthogonal to line and passing through segment end point
            // And check if it is touching ellipsoid
            Plane3d pl1 = new Plane3d(s.P1, v1);
            object  obj = e.IntersectionWith(pl1);

            if (obj.GetType() == typeof(Point3d))
            {
                p = (Point3d)obj;
                if (p.BelongsTo(e))
                {
                    Assert.IsTrue(true);
                }
                else
                {
                    Assert.Fail();
                }
            }
            else
            {
                Assert.Fail();
            }
        }
Exemple #8
0
        public void TriangleIntersectionWithSegmentRelativeTest()
        {
            double tol  = GeometRi3D.Tolerance;
            bool   mode = GeometRi3D.UseAbsoluteTolerance;

            GeometRi3D.Tolerance            = 0.01;
            GeometRi3D.UseAbsoluteTolerance = false;

            Point3d p1 = new Point3d(0, 0, 0);
            Point3d p2 = new Point3d(6, 0, 0);
            Point3d p3 = new Point3d(0, 6, 0);

            Triangle t = new Triangle(p1, p2, p3);

            Segment3d s = new Segment3d(new Point3d(0, 0, 0.01), new Point3d(3.01, 3, 0));

            Assert.IsTrue((Segment3d)t.IntersectionWith(s) == s);

            s = new Segment3d(new Point3d(2, 2, 0.01), new Point3d(5.01, 5, 0));
            Assert.IsTrue((Segment3d)t.IntersectionWith(s) == new Segment3d(new Point3d(2, 2, 0), new Point3d(3, 3, 0)));

            // Resore initial state
            GeometRi3D.UseAbsoluteTolerance = mode;
            GeometRi3D.Tolerance            = tol;
        }
        public void SegmentIntersectionWithSegmentSymmetryTest()
        {
            // Test symmetry in segment-segment intersection
            // s1.IntersectionWith(s2) == s2.IntersectionWith(s1)

            Segment3d s1 = new Segment3d(new Point3d(0, 0, 0), new Point3d(5, 5, 0));

            // Coincided segments
            Segment3d s2   = new Segment3d(new Point3d(5, 5, 0), new Point3d(0, 0, 0));
            object    obj1 = s1.IntersectionWith(s2);
            object    obj2 = s2.IntersectionWith(s1);

            Assert.IsTrue((obj1 == null && obj2 == null) || (obj1 != null && obj2 != null));

            // Non-parallel segments
            s2   = new Segment3d(new Point3d(5, 5.000000001, 0), new Point3d(-0.0000000001, 0, 0));
            obj1 = s1.IntersectionWith(s2);
            obj2 = s2.IntersectionWith(s1);
            Assert.IsTrue((obj1 == null && obj2 == null) || (obj1 != null && obj2 != null));

            // Nearly parallel segments
            s2   = new Segment3d(new Point3d(3, 3.000000000001, 0), new Point3d(6, 6, 0));
            obj1 = s1.IntersectionWith(s2);
            obj2 = s2.IntersectionWith(s1);
            Assert.IsTrue((obj1 == null && obj2 == null) || (obj1 != null && obj2 != null));
        }
        protected virtual ToolpathPreviewJoint GenerateMiterJoint(Segment3d segmentBefore, Segment3d segmentAfter, TPrintVertex printVertex, ToolpathPreviewMesh mesh)
        {
            var averageDirection = (segmentBefore.Direction + segmentAfter.Direction).Normalized;
            var scaleFactor      = 1 / segmentBefore.Direction.Dot(averageDirection);

            var frame = new Frame3f(printVertex.Position);

            frame.AlignAxis(1, ToVector3f(averageDirection));

            var joint = new ToolpathPreviewJoint();

            joint.InTop = joint.OutTop = mesh.AddVertex(vertexFactory(printVertex,
                                                                      frame.FromFrameP(DiamondCrossSection.Top(printVertex.Dimensions)), brightnessMax));

            joint.InRight = joint.OutRight = mesh.AddVertex(vertexFactory(printVertex,
                                                                          frame.FromFrameP(DiamondCrossSection.Right(printVertex.Dimensions)), brightnessMin));

            joint.InBottom = joint.OutBottom = mesh.AddVertex(vertexFactory(printVertex,
                                                                            frame.FromFrameP(DiamondCrossSection.Bottom(printVertex.Dimensions)), brightnessMax));

            joint.InLeft = joint.OutLeft = mesh.AddVertex(vertexFactory(printVertex,
                                                                        frame.FromFrameP(DiamondCrossSection.Left(printVertex.Dimensions)), brightnessMin));

            return(joint);
        }
Exemple #11
0
        void draw_lines(LineSet lines, Vector3f cameraPos)
        {
            GL.Begin(GL.LINES);
            GL.Color(lines.Color);
            int NS = lines.Segments.Count;

            for (int k = 0; k < NS; ++k)
            {
                Segment3d seg = lines.Segments[k];
                GL.Vertex((Vector3)seg.P0);
                GL.Vertex((Vector3)seg.P1);
            }
            GL.End();

            int NC = lines.Curves.Count;

            for (int k = 0; k < NC; ++k)
            {
                DCurve3 c = lines.Curves[k];
                GL.Begin(GL.LINE_STRIP);
                GL.Color(lines.Color);
                int NV = c.VertexCount;
                for (int i = 0; i < NV; ++i)
                {
                    GL.Vertex((Vector3)c[i]);
                }
                if (c.Closed)
                {
                    GL.Vertex((Vector3)c[0]);
                }
                GL.End();
            }
        }
        public void SegmentIntersectionWithBoxRelativeTest()
        {
            double tol  = GeometRi3D.Tolerance;
            bool   mode = GeometRi3D.UseAbsoluteTolerance;

            GeometRi3D.Tolerance            = 0.01;
            GeometRi3D.UseAbsoluteTolerance = false;

            Rotation rot = new Rotation();
            Point3d  p   = new Point3d(0, 0, 0);
            Box3d    b   = new Box3d(p, 2, 2, 2, rot);

            // Segment aligned with X-axis
            Segment3d s = new Segment3d(new Point3d(-2, 0, 0), new Point3d(-1.01, 0, 0));

            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(-1, 0, 0));

            s = new Segment3d(new Point3d(1.01, 0, 0), new Point3d(2, 0, 0));
            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(1, 0, 0));

            s = new Segment3d(new Point3d(-0.5, 0, 0), new Point3d(0.5, 0, 0));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), s);

            s = new Segment3d(new Point3d(-1.5, 0, 0), new Point3d(1.5, 0, 0));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(-1, 0, 0), new Point3d(1, 0, 0)));

            // Segment aligned with Y-axis
            s = new Segment3d(new Point3d(0, -2, 0), new Point3d(0, -1.01, 0));
            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(0, -1, 0));

            s = new Segment3d(new Point3d(0, 1.01, 0), new Point3d(0, 2, 0));
            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(0, 1, 0));

            s = new Segment3d(new Point3d(0, -0.5, 0), new Point3d(0, 0.5, 0));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), s);

            s = new Segment3d(new Point3d(0, -1.5, 0), new Point3d(0, 1.5, 0));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(0, -1, 0), new Point3d(0, 1, 0)));

            // Segment aligned with Z-axis
            s = new Segment3d(new Point3d(0, 0, -2), new Point3d(0, 0, -1.01));
            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(0, 0, -1));

            s = new Segment3d(new Point3d(0, 0, 1.01), new Point3d(0, 0, 2));
            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(0, 0, 1));

            s = new Segment3d(new Point3d(0, 0, -0.5), new Point3d(0, 0, 0.5));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), s);

            s = new Segment3d(new Point3d(0, 0, -1.5), new Point3d(0, 0, 1.5));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(0, 0, -1), new Point3d(0, 0, 1)));

            // Segment crossing corner
            s = new Segment3d(new Point3d(2.01, 0, 1), new Point3d(0.01, 2, 1));
            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(1, 1, 1));

            // Resore initial state
            GeometRi3D.UseAbsoluteTolerance = mode;
            GeometRi3D.Tolerance            = tol;
        }
        public void SegmentIntersectionWithBoxTest()
        {
            Rotation rot = new Rotation();
            Point3d  p   = new Point3d(0, 0, 0);
            Box3d    b   = new Box3d(p, 2, 2, 2, rot);

            Segment3d s = new Segment3d(new Point3d(-1, -1, -1), new Point3d(1, 1, 1));

            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(-1, -1, -1), new Point3d(1, 1, 1)));

            s = new Segment3d(new Point3d(0, 0, 0), new Point3d(1, 1, 1));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(0, 0, 0), new Point3d(1, 1, 1)));

            s = new Segment3d(new Point3d(1, -1, -1), new Point3d(-1, 1, 1));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(1, -1, -1), new Point3d(-1, 1, 1)));

            s = new Segment3d(new Point3d(0, 0, 0), new Point3d(1, 0, 0));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(0, 0, 0), new Point3d(1, 0, 0)));

            s = new Segment3d(new Point3d(0, 0, 0), new Point3d(0, -1, 0));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(0, 0, 0), new Point3d(0, -1, 0)));

            s = new Segment3d(new Point3d(0, 0, 0), new Point3d(0, 0, 1));
            Assert.AreEqual((Segment3d)b.IntersectionWith(s), new Segment3d(new Point3d(0, 0, 0), new Point3d(0, 0, 1)));

            // Intersection is point
            s = new Segment3d(new Point3d(-1, -1, 1), new Point3d(0, 0, 2));
            Assert.AreEqual((Point3d)b.IntersectionWith(s), new Point3d(-1, -1, 1));
        }
        public void RayIntersectionWithBoxTest()
        {
            Rotation rot = new Rotation();
            Point3d  p   = new Point3d(0, 0, 0);
            Box3d    b   = new Box3d(p, 2, 2, 2, rot);

            Ray3d     r = new Ray3d(new Point3d(-1, -1, -1), new Vector3d(1, 1, 1));
            Segment3d s = (Segment3d)b.IntersectionWith(r);

            Assert.AreEqual(s, new Segment3d(new Point3d(-1, -1, -1), new Point3d(1, 1, 1)));

            r = new Ray3d(new Point3d(0, 0, 0), new Vector3d(1, 1, 1));
            s = (Segment3d)b.IntersectionWith(r);
            Assert.AreEqual(s, new Segment3d(new Point3d(0, 0, 0), new Point3d(1, 1, 1)));

            r = new Ray3d(new Point3d(1, -1, -1), new Vector3d(-1, 1, 1));
            s = (Segment3d)b.IntersectionWith(r);
            Assert.AreEqual(s, new Segment3d(new Point3d(1, -1, -1), new Point3d(-1, 1, 1)));

            r = new Ray3d(new Point3d(0, 0, 0), new Vector3d(1, 0, 0));
            s = (Segment3d)b.IntersectionWith(r);
            Assert.AreEqual(s, new Segment3d(new Point3d(0, 0, 0), new Point3d(1, 0, 0)));

            r = new Ray3d(new Point3d(0, 0, 0), new Vector3d(0, -1, 0));
            s = (Segment3d)b.IntersectionWith(r);
            Assert.AreEqual(s, new Segment3d(new Point3d(0, 0, 0), new Point3d(0, -1, 0)));

            r = new Ray3d(new Point3d(0, 0, 0), new Vector3d(0, 0, 1));
            s = (Segment3d)b.IntersectionWith(r);
            Assert.AreEqual(s, new Segment3d(new Point3d(0, 0, 0), new Point3d(0, 0, 1)));

            // Intersection is point
            r = new Ray3d(new Point3d(-1, -1, 1), new Vector3d(1, 1, 1));
            Assert.AreEqual((Point3d)b.IntersectionWith(r), new Point3d(-1, -1, 1));
        }
        public void SegmentProjectionToPlaneTest()
        {
            Plane3d   s = new Plane3d(0, 0, 1, -1);
            Segment3d r = new Segment3d(new Point3d(-1, -3, -2), new Point3d(-5, 1, -3));

            Assert.IsTrue((Segment3d)r.ProjectionTo(s) == new Segment3d(new Point3d(-1, -3, 1), new Point3d(-5, 1, 1)));
        }
        public void EllipsoidIntersectionWithSegmentRelativeTest()
        {
            double tol  = GeometRi3D.Tolerance;
            bool   mode = GeometRi3D.UseAbsoluteTolerance;

            GeometRi3D.Tolerance            = 0.01;
            GeometRi3D.UseAbsoluteTolerance = false;

            Point3d   p  = new Point3d(0, 0, 0);
            Vector3d  v1 = new Vector3d(4, 0, 0);
            Vector3d  v2 = new Vector3d(0, 6, 0);
            Vector3d  v3 = new Vector3d(0, 0, 9);
            Ellipsoid e  = new Ellipsoid(p, v1, v2, v3);

            Segment3d s = new Segment3d(new Point3d(-5, 0.01, 0), new Point3d(5, 0, 0));

            Assert.IsTrue((Segment3d)e.IntersectionWith(s) == new Segment3d(new Point3d(-4, 0, 0), new Point3d(4, 0, 0)));

            s = new Segment3d(new Point3d(0, -7, 0), new Point3d(0.01, 7, 0));
            Assert.IsTrue((Segment3d)e.IntersectionWith(s) == new Segment3d(new Point3d(0, -6, 0), new Point3d(0, 6, 0)));

            s = new Segment3d(new Point3d(0, 0, 0), new Point3d(0.01, 0, 10));
            Assert.IsTrue((Segment3d)e.IntersectionWith(s) == new Segment3d(new Point3d(0, 0, 0), new Point3d(0, 0, 9)));

            // Resore initial state
            GeometRi3D.UseAbsoluteTolerance = mode;
            GeometRi3D.Tolerance            = tol;
        }
        private static bool CornerIsInsideTube(Segment3d segmentBeforeJoint, Segment3d segmentAfterJoint, double tubeWidth)
        {
            var angle         = Math.PI - segmentBeforeJoint.Direction.AngleR(segmentAfterJoint.Direction, true);
            var minimumLength = (tubeWidth / 2d) / Math.Tan(angle / 2d);

            return(segmentAfterJoint.Length < minimumLength);
        }
        public void EllipsoidIntersectionWithLineTest()
        {
            Point3d   p  = new Point3d(0, 0, 0);
            Vector3d  v1 = new Vector3d(4, 0, 0);
            Vector3d  v2 = new Vector3d(0, 6, 0);
            Vector3d  v3 = new Vector3d(0, 0, 9);
            Ellipsoid e  = new Ellipsoid(p, v1, v2, v3);


            Line3d l = new Line3d(new Point3d(0, 0, 0), v1 = new Vector3d(1, 0, 0));

            Assert.IsTrue((Segment3d)e.IntersectionWith(l) == new Segment3d(new Point3d(-4, 0, 0), new Point3d(4, 0, 0)));

            l = new Line3d(new Point3d(0, 0, 0), v1 = new Vector3d(0, 1, 0));
            Assert.IsTrue((Segment3d)e.IntersectionWith(l) == new Segment3d(new Point3d(0, -6, 0), new Point3d(0, 6, 0)));

            l = new Line3d(new Point3d(0, 0, 0), v1 = new Vector3d(0, 0, 1));
            Assert.IsTrue((Segment3d)e.IntersectionWith(l) == new Segment3d(new Point3d(0, 0, -9), new Point3d(0, 0, 9)));


            p  = new Point3d(0, 2, 1);
            v1 = new Vector3d(-1, 1, 3);
            l  = new Line3d(p, v1);

            Segment3d s = (Segment3d)e.IntersectionWith(l);

            Assert.IsTrue(s.P1.IsOnBoundary(e));
            Assert.IsTrue(s.P2.IsOnBoundary(e));
        }
Exemple #19
0
        private static IEnumerable <Line3d> ExpandSegments(IEnumerable <Segment3d> segments, double distance)
        {
            var lines = new List <Line3d>();

            foreach (Segment3d segment in segments)
            {
                // TODO:
                //var p1 = new Vector3d(segment.P1.X, segment.P1.Y, 0);
                //var line = segment.ToLine;

                var directionVec1 = new Vector3d(segment.P1.Y, -segment.P1.X, 0);
                directionVec1.Normalize();
                var v1 = directionVec1 * distance;
                var p1 = new Point3d(segment.P1.X + v1.X, segment.P1.Y + v1.Y, 0);

                var directionVec2 = new Vector3d(segment.P2.Y, -segment.P2.X, 0);
                directionVec2.Normalize();
                var v2 = directionVec2 * distance;
                var p2 = new Point3d(segment.P2.X + v2.X, segment.P2.Y + v2.Y, 0);

                var s = new Segment3d(p2, p2);
                lines.Add(s.ToLine);

                //bool InLeftHemisphere(double theta) => ((0.5 * Math.PI) < theta) && (theta < (1.5 * Math.PI));

                //lines.Add(InLeftHemisphere(segment.P1.AngleTo(segment.P2))
                //? line.Parallel(distance)
                //: line.Parallel(-distance));
            }

            return(lines);
        }
        protected DMesh3 MakeDebugGraphMesh()
        {
            DMesh3 graphMesh = new DMesh3();

            graphMesh.EnableVertexColors(Vector3f.One);
            foreach (int vid in Graph.VertexIndices())
            {
                if (TipVertices.Contains(vid))
                {
                    MeshEditor.AppendBox(graphMesh, Graph.GetVertex(vid), 0.3f, Colorf.Green);
                }
                else if (TipBaseVertices.Contains(vid))
                {
                    MeshEditor.AppendBox(graphMesh, Graph.GetVertex(vid), 0.225f, Colorf.Magenta);
                }
                else if (GroundVertices.Contains(vid))
                {
                    MeshEditor.AppendBox(graphMesh, Graph.GetVertex(vid), 0.35f, Colorf.Blue);
                }
                else
                {
                    MeshEditor.AppendBox(graphMesh, Graph.GetVertex(vid), 0.15f, Colorf.White);
                }
            }
            foreach (int eid in Graph.EdgeIndices())
            {
                Segment3d seg = Graph.GetEdgeSegment(eid);
                MeshEditor.AppendLine(graphMesh, seg, 0.1f);
            }
            return(graphMesh);
        }
        public void PointInSegmentTest()
        {
            Point3d   p = new Point3d(0, 0, 0);
            Segment3d s = new Segment3d(p, new Point3d(10, 0, 0));

            p = new Point3d(5, 0, 0);  // Point inside
            Assert.IsTrue(p.BelongsTo(s));
            Assert.IsTrue(p.IsInside(s));
            Assert.IsFalse(p.IsOutside(s));
            Assert.IsFalse(p.IsOnBoundary(s));

            p = new Point3d(10, 0, 0);  // Point on boundary
            Assert.IsTrue(p.BelongsTo(s));
            Assert.IsFalse(p.IsInside(s));
            Assert.IsFalse(p.IsOutside(s));
            Assert.IsTrue(p.IsOnBoundary(s));

            p = new Point3d(1, 3, 0);  // Point outside
            Assert.IsFalse(p.BelongsTo(s));
            Assert.IsFalse(p.IsInside(s));
            Assert.IsTrue(p.IsOutside(s));
            Assert.IsFalse(p.IsOnBoundary(s));

            p = new Point3d(11, 0, 0);  // Point outside
            Assert.IsFalse(p.BelongsTo(s));
            Assert.IsFalse(p.IsInside(s));
            Assert.IsTrue(p.IsOutside(s));
            Assert.IsFalse(p.IsOnBoundary(s));
        }
        public void LineIntersectionWithBoxTest()
        {
            Rotation rot = new Rotation();
            Point3d  p   = new Point3d(0, 0, 0);
            Box3d    b   = new Box3d(p, 2, 2, 2, rot);

            Line3d    l = new Line3d(new Point3d(1, 1, 1), new Vector3d(1, 1, 1));
            Segment3d s = (Segment3d)b.IntersectionWith(l);

            Assert.AreEqual(s, new Segment3d(new Point3d(-1, -1, -1), new Point3d(1, 1, 1)));

            l = new Line3d(new Point3d(1, -1, -1), new Vector3d(-1, 1, 1));
            s = (Segment3d)b.IntersectionWith(l);
            Assert.AreEqual(s, new Segment3d(new Point3d(1, -1, -1), new Point3d(-1, 1, 1)));

            l = new Line3d(new Point3d(0, 0, 0), new Vector3d(1, 0, 0));
            s = (Segment3d)b.IntersectionWith(l);
            Assert.AreEqual(s, new Segment3d(new Point3d(-1, 0, 0), new Point3d(1, 0, 0)));

            l = new Line3d(new Point3d(0, 0, 0), new Vector3d(0, 1, 0));
            s = (Segment3d)b.IntersectionWith(l);
            Assert.AreEqual(s, new Segment3d(new Point3d(0, -1, 0), new Point3d(0, 1, 0)));

            l = new Line3d(new Point3d(0, 0, 0), new Vector3d(0, 0, 1));
            s = (Segment3d)b.IntersectionWith(l);
            Assert.AreEqual(s, new Segment3d(new Point3d(0, 0, -1), new Point3d(0, 0, 1)));

            // Intersection is point
            l = new Line3d(new Point3d(-1, -1, 1), new Vector3d(1, 1, 1));
            Assert.AreEqual((Point3d)b.IntersectionWith(l), new Point3d(-1, -1, 1));
        }
        public static Segment2d IntoFrame(this Segment3d _seg3d, Frame3f _frame)
        {
            var seg2f = new Segment2f
                            (_frame.ToPlaneUV((Vector3f)_seg3d.P0, 2),
                            _frame.ToPlaneUV((Vector3f)_seg3d.P1, 2));

            return(new Segment2d(seg2f.P0, seg2f.P1));
        }
        private static double GetBevelDistance(ref Segment3d segmentBefore, ref Segment3d segmentAfter, Vector2d dimensions)
        {
            double angle2 = Math.Abs(segmentBefore.Direction.AngleR(segmentAfter.Direction));

            double miterExtensions = Math.Tan(angle2 / 4) * dimensions.x / 2;

            return(miterExtensions);
        }
        public void SegmentAngleToPlaneTest()
        {
            Plane3d   s  = new Plane3d(0, 0, 1, -1);
            Segment3d r1 = new Segment3d(new Point3d(0, 0, 3), new Point3d(1, 0, 4));
            Segment3d r2 = new Segment3d(new Point3d(0, 0, -3), new Point3d(1, 0, -4));

            Assert.IsTrue(Abs(r1.AngleToDeg(s) - 45) < GeometRi3D.Tolerance && Abs(r1.AngleTo(s) - r2.AngleTo(s)) < GeometRi3D.Tolerance);
        }
        public static Segment3d OntoFrame(this Segment3d _seg3d, Frame3f _frame)
        {
            var seg3f = new Segment3f
                            (_frame.ToFrameP((Vector3f)_seg3d.P0),
                            _frame.ToFrameP((Vector3f)_seg3d.P1));

            return(new Segment3d(seg3f.P0, seg3f.P1));
        }
Exemple #27
0
        public void SphereProjectionToLineTest()
        {
            Sphere    s   = new Sphere(new Point3d(-4, -3, -2), 5);
            Line3d    l   = new Line3d(new Point3d(0, 0, 0), new Vector3d(4, 3, 0));
            Segment3d c   = s.ProjectionTo(l);
            Segment3d res = new Segment3d(new Point3d(0, 0, 0), new Point3d(-8, -6, 0));

            Assert.AreEqual(c, res);
        }
Exemple #28
0
        void draw_quads(LineSet lines, Vector3f cameraPos)
        {
            GL.Begin(GL.QUADS);
            GL.Color(lines.Color);

            int NS = lines.Segments.Count;

            for (int k = 0; k < NS; ++k)
            {
                Segment3d seg = lines.Segments[k];
                Vector3f  start = (Vector3f)seg.P0, end = (Vector3f)seg.P1;
                if (lines.UseFixedNormal)
                {
                    draw_quad_fixednormal(ref start, ref end, ref lines.FixedNormal, lines.Width);
                }
                else
                {
                    draw_quad_viewalign(ref start, ref end, ref cameraPos, lines.Width);
                }
            }

            int NC = lines.Curves.Count;

            for (int k = 0; k < NC; ++k)
            {
                DCurve3  c    = lines.Curves[k];
                int      NV   = c.VertexCount;
                Vector3f prev = (Vector3f)c[0];
                for (int i = 1; i < NV; ++i)
                {
                    Vector3f cur = (Vector3f)c[i];
                    if (lines.UseFixedNormal)
                    {
                        draw_quad_fixednormal(ref prev, ref cur, ref lines.FixedNormal, lines.Width);
                    }
                    else
                    {
                        draw_quad_viewalign(ref prev, ref cur, ref cameraPos, lines.Width);
                    }
                    prev = cur;
                }
                if (c.Closed)
                {
                    Vector3f cur = (Vector3f)c[0];
                    if (lines.UseFixedNormal)
                    {
                        draw_quad_fixednormal(ref prev, ref cur, ref lines.FixedNormal, lines.Width);
                    }
                    else
                    {
                        draw_quad_viewalign(ref prev, ref cur, ref cameraPos, lines.Width);
                    }
                }
            }

            GL.End();
        }
        public void SegmentEqualsTest()
        {
            Point3d   p1 = new Point3d(1, 4, 6);
            Point3d   p2 = new Point3d(8, -4, 0);
            Segment3d r1 = new Segment3d(p1, p2);
            Segment3d r2 = new Segment3d(p2, p1);

            Assert.IsTrue(r1 == r2);
        }
Exemple #30
0
    /// <summary>
    /// 3d空间中点到直线距离
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <param name="s"></param>
    /// <returns></returns>
//     public static FloatL DistanceOfPoint3dWithLine3d(Line3d line3d, Vector3L s)
//     {
//         FloatL ab = FixPointMath.Sqrt(FixPointMath.Pow((line3d.m_point1.x - line3d.m_point2.x), 2.0f) + FixPointMath.Pow((line3d.m_point1.y - line3d.m_point2.y), 2.0f) + FixPointMath.Pow((line3d.m_point1.z - line3d.m_point2.z), 2.0f));
//         FloatL as2 = FixPointMath.Sqrt(FixPointMath.Pow((line3d.m_point1.x - s.x), 2.0f) + FixPointMath.Pow((line3d.m_point1.y - s.y), 2.0f) + FixPointMath.Pow((line3d.m_point1.z - s.z), 2.0f));
//         FloatL bs = FixPointMath.Sqrt(FixPointMath.Pow((s.x - line3d.m_point2.x), 2.0f) + FixPointMath.Pow((s.y - line3d.m_point2.y), 2.0f) + FixPointMath.Pow((s.z - line3d.m_point2.z), 2.0f));
//         FloatL cos_A = (FixPointMath.Pow(as2, 2.0f) + FixPointMath.Pow(ab, 2.0f) - FixPointMath.Pow(bs, 2.0f)) / (2 * ab * as2);
//         FloatL sin_A = FixPointMath.Sqrt(1 - FixPointMath.Pow(cos_A, 2.0f));
//         return as2 * sin_A;
//     }
    public static Vector3L ClosestPointOfPoint3dWithSegment3d(Vector3L point, Segment3d line)
    {
        Vector3L lVec = line.m_point2 - line.m_point1; // Line Vector
        FloatL   t    = Vector3L.Dot(point - line.m_point1, lVec) / Vector3L.Dot(lVec, lVec);

        t = FixPointMath.Max(t, 0.0f); // Clamp to 0
        t = FixPointMath.Min(t, 1.0f); // Clamp to 1
        return(line.m_point1 + lVec * t);
    }