Beispiel #1
0
        private static bool CanIntersect(InfPlane plane, Vector3D lineStart, Vector3D lineEnd)
        {
            var startAbove = IsPointAbovePlane(lineStart, plane);
            var endAbove   = IsPointAbovePlane(lineEnd, plane);

            return((startAbove && !endAbove) || (!startAbove && endAbove));
        }
Beispiel #2
0
        private static bool TryIntersect(Mesh mesh, Face face, InfPlane plane, out LineSegment intersection)
        {
            intersection = null;

            if (face.IndexCount != 3)
            {
                throw new Exception("Face not triangular!");
            }

            var ia = face.Indices[0];
            var ib = face.Indices[1];
            var ic = face.Indices[2];

            var a = mesh.Vertices[ia];
            var b = mesh.Vertices[ib];
            var c = mesh.Vertices[ic];

            var ab = CanIntersect(plane, a, b);
            var bc = CanIntersect(plane, b, c);
            var ca = CanIntersect(plane, c, a);

            var planeNormal = plane.Normal;
            var planePoint  = plane.Point;

            if (ab && bc)
            {
                var start = IntersectPoint(a, b, planeNormal, planePoint);
                var end   = IntersectPoint(b, c, planeNormal, planePoint);
                intersection = new LineSegment(start, end, face);
            }
            else if (bc && ca)
            {
                var start = IntersectPoint(b, c, planeNormal, planePoint);
                var end   = IntersectPoint(c, a, planeNormal, planePoint);
                intersection = new LineSegment(start, end, face);
            }
            else if (ca && ab)
            {
                var start = IntersectPoint(c, a, planeNormal, planePoint);
                var end   = IntersectPoint(a, b, planeNormal, planePoint);
                intersection = new LineSegment(start, end, face);
            }
            else
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        public static List <LineSegment> Slice(Mesh mesh, InfPlane plane)
        {
            var segments = new List <LineSegment>();

            foreach (var face in mesh.Faces)
            {
                if (!TryIntersect(mesh, face, plane, out var intersection))
                {
                    continue;
                }
                segments.Add(intersection);
            }

            return(segments);
        }
Beispiel #4
0
 private static bool IsPointAbovePlane(Vector3D point, InfPlane plane)
 {
     return(plane.Normal.Dot(point - plane.Point) > 0);
 }