Ejemplo n.º 1
0
        /// <summary>
        /// construct a DistPoint3Triangle3 object for a mesh triangle
        /// </summary>
        public static DistPoint3Triangle3 TriangleDistance(DMesh3 mesh, int ti, Vector3d point)
        {
            if (!mesh.IsTriangle(ti))
            {
                return(null);
            }
            Triangle3d tri = new Triangle3d();

            mesh.GetTriVertices(ti, ref tri.V0, ref tri.V1, ref tri.V2);
            DistPoint3Triangle3 q = new DistPoint3Triangle3(point, tri);

            q.GetSquared();
            return(q);
        }
        public double GetSquared()
        {
            if (DistanceSquared >= 0)
            {
                return(DistanceSquared);
            }

            var    line    = new Line3d(segment.Center, segment.Direction);
            var    queryLT = new DistLine3Triangle3(line, triangle);
            double sqrDist = queryLT.GetSquared();

            SegmentParam = queryLT.LineParam;

            if (SegmentParam >= -segment.Extent)
            {
                if (SegmentParam <= segment.Extent)
                {
                    SegmentClosest     = queryLT.LineClosest;
                    TriangleClosest    = queryLT.TriangleClosest;
                    TriangleBaryCoords = queryLT.TriangleBaryCoords;
                }
                else
                {
                    SegmentClosest = segment.P1;
                    var queryPT = new DistPoint3Triangle3(SegmentClosest, triangle);
                    sqrDist            = queryPT.GetSquared();
                    TriangleClosest    = queryPT.TriangleClosest;
                    SegmentParam       = segment.Extent;
                    TriangleBaryCoords = queryPT.TriangleBaryCoords;
                }
            }
            else
            {
                SegmentClosest = segment.P0;
                var queryPT = new DistPoint3Triangle3(SegmentClosest, triangle);
                sqrDist            = queryPT.GetSquared();
                TriangleClosest    = queryPT.TriangleClosest;
                SegmentParam       = -segment.Extent;
                TriangleBaryCoords = queryPT.TriangleBaryCoords;
            }

            DistanceSquared = sqrDist;
            return(DistanceSquared);
        }