public static List <List <IntPoint> > CreateSlice(Mesh mesh, Plane plane, Matrix4X4 transformTo0Plane, IBvhItem acccelerator = null, bool includeBehindThePlane = true) { var unorderedSegments = GetUnorderdSegments(mesh, plane, transformTo0Plane, acccelerator, includeBehindThePlane); // connect all the segments together into polygons return(FindClosedPolygons(unorderedSegments)); }
public BvhIterator(IBvhItem referenceItem, Matrix4X4 initialTransform = default(Matrix4X4), int initialDepth = 0, Func <BvhIterator, bool> decentFilter = null) { TransformToWorld = initialTransform; if (TransformToWorld == default(Matrix4X4)) { TransformToWorld = Matrix4X4.Identity; } Depth = initialDepth; Bvh = referenceItem; this.DecentFilter = decentFilter; }
public static void RenderBounds(DrawEventArgs e, WorldView world, Matrix4X4 transformToWorld, IBvhItem bvh, int depth = int.MinValue) { for (int i = 0; i < 4; i++) { Vector3 bottomStartPosition = Vector3Ex.Transform(bvh.GetAxisAlignedBoundingBox().GetBottomCorner(i), transformToWorld); var bottomStartScreenPos = world.GetScreenPosition(bottomStartPosition); Vector3 bottomEndPosition = Vector3Ex.Transform(bvh.GetAxisAlignedBoundingBox().GetBottomCorner((i + 1) % 4), transformToWorld); var bottomEndScreenPos = world.GetScreenPosition(bottomEndPosition); Vector3 topStartPosition = Vector3Ex.Transform(bvh.GetAxisAlignedBoundingBox().GetTopCorner(i), transformToWorld); var topStartScreenPos = world.GetScreenPosition(topStartPosition); Vector3 topEndPosition = Vector3Ex.Transform(bvh.GetAxisAlignedBoundingBox().GetTopCorner((i + 1) % 4), transformToWorld); var topEndScreenPos = world.GetScreenPosition(topEndPosition); e.Graphics2D.Line(bottomStartScreenPos, bottomEndScreenPos, Color.Black); e.Graphics2D.Line(topStartScreenPos, topEndScreenPos, Color.Black); e.Graphics2D.Line(topStartScreenPos, bottomStartScreenPos, Color.Black); } if (bvh is ITriangle tri) { for (int i = 0; i < 3; i++) { var vertexPos = tri.GetVertex(i); var screenCenter = Vector3Ex.Transform(vertexPos, transformToWorld); var screenPos = world.GetScreenPosition(screenCenter); e.Graphics2D.Circle(screenPos, 3, Color.Red); } } else { var center = bvh.GetCenter(); var worldCenter = Vector3Ex.Transform(center, transformToWorld); var screenPos2 = world.GetScreenPosition(worldCenter); if (depth != int.MinValue) { e.Graphics2D.Circle(screenPos2, 3, Color.Yellow); e.Graphics2D.DrawString($"{depth},", screenPos2.X + 12 * depth, screenPos2.Y); } } }
public BvhItemView(IBvhItem item, Matrix4X4 matrix) { this.BvhItem = item; this.WorldMatrix = matrix; }
public static List <Segment> GetUnorderdSegments(Mesh mesh, Plane plane, Matrix4X4 meshTo0Plane, IBvhItem acccelerator = null, bool includeBehindThePlane = true) { // collect all the segments this plane intersects and record them in unordered segments in z 0 space var unorderedSegments = new List <Segment>(); if (acccelerator != null) { foreach (var bvhItem in acccelerator.GetCrossing(plane)) { var faceIndex = -1; if (bvhItem is MinimalTriangle minimalTriangle) { faceIndex = minimalTriangle.FaceIndex; } else if (bvhItem is MinimalTriangle triangleShape) { faceIndex = triangleShape.FaceIndex; } var face = mesh.Faces[faceIndex]; if (!includeBehindThePlane) { // make sure some part of this face is in front of the plane if (plane.GetDistanceFromPlane(mesh.Vertices[face.v0]) < .001 && plane.GetDistanceFromPlane(mesh.Vertices[face.v1]) < .001 && plane.GetDistanceFromPlane(mesh.Vertices[face.v2]) < .001) { // skip this face as it is behind the plane continue; } } if (face.GetCutLine(mesh.Vertices, plane, out Vector3 start, out Vector3 end)) { var startAtZ0 = Vector3Ex.Transform(start, meshTo0Plane); var endAtZ0 = Vector3Ex.Transform(end, meshTo0Plane); unorderedSegments.Add( new Segment( new IntPoint(startAtZ0.X, startAtZ0.Y), new IntPoint(endAtZ0.X, endAtZ0.Y))); } } } else { for (var faceIndex = 0; faceIndex < mesh.Faces.Count; faceIndex++) { var face = mesh.Faces[faceIndex]; if (face.GetCutLine(mesh.Vertices, plane, out Vector3 start, out Vector3 end)) { var startAtZ0 = Vector3Ex.Transform(start, meshTo0Plane); var endAtZ0 = Vector3Ex.Transform(end, meshTo0Plane); unorderedSegments.Add( new Segment( new IntPoint(startAtZ0.X, startAtZ0.Y), new IntPoint(endAtZ0.X, endAtZ0.Y))); } } } return(unorderedSegments); }
public static List <Segment> GetUnorderdSegments(Mesh mesh, Plane plane, IBvhItem acccelerator = null, bool includeBehindThePlane = true) { return(GetUnorderdSegments(mesh, plane, GetTransformTo0Plane(plane), acccelerator, includeBehindThePlane)); }
public static BvhIterator Filter(this IBvhItem item, Func <BvhIterator, bool> decentFilter = null) { return(new BvhIterator(item, Matrix4X4.Identity, 0, decentFilter)); }