From de37d4ca16ba44a93c19054f0395bf7862cd957f Mon Sep 17 00:00:00 2001 From: LarsBrubaker Date: Sat, 13 Apr 2019 12:53:09 -0700 Subject: [PATCH] Working to get curve object to do splitting --- .../DesignTools/Operations/CurveObject3D_2.cs | 49 ++++++++++++++----- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs b/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs index e4573e5cc..db4578893 100644 --- a/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/CurveObject3D_2.cs @@ -49,8 +49,8 @@ namespace MatterHackers.MatterControl.DesignTools { public class CurveObject3D_2 : OperationSourceContainerObject3D, IEditorDraw { - // this needs to serialize but not be editable - public Vector3 rotationOffset; + // this needs to serialize but not be editable (so public but not a get set) + public Vector3 RotationOffset; public CurveObject3D_2() { @@ -107,7 +107,7 @@ namespace MatterHackers.MatterControl.DesignTools null, (reporter, cancellationToken) => { - this.Translate(-rotationOffset); + this.Translate(-RotationOffset); SourceContainer.Visible = true; RemoveAllButSource(); @@ -128,12 +128,22 @@ namespace MatterHackers.MatterControl.DesignTools var radius = Diameter / 2; var circumference = MathHelper.Tau * radius; var rotationCenter = new Vector3(aabb.MinXYZ.X + (aabb.MaxXYZ.X - aabb.MinXYZ.X) * (StartPercent / 100), aabb.MaxXYZ.Y + radius, aabb.Center.Z); + double numRotations = aabb.XSize / circumference; + double numberOfCuts = numRotations * MinSidesPerRotation; + double cutSize = aabb.XSize / numberOfCuts; + double cutPosition = aabb.MinXYZ.X; + var cuts = new List(); + for (int i = 0; i < numberOfCuts; i++) + { + cuts.Add(cutPosition); + cutPosition += cutSize; + } - rotationOffset = rotationCenter; + RotationOffset = rotationCenter; if (!BendCcw) { // fix the stored center so we draw correctly - rotationOffset.Y = aabb.MinXYZ.Y - radius; + RotationOffset.Y = aabb.MinXYZ.Y - radius; } foreach (var sourceItem in SourceContainer.VisibleMeshes()) @@ -143,9 +153,7 @@ namespace MatterHackers.MatterControl.DesignTools var itemMatrix = sourceItem.WorldMatrix(SourceContainer); // split the mesh along the x axis - double numRotations = aabb.XSize / circumference; - double numberOfCuts = numRotations * MinSidesPerRotation; - //SplitMeshAlongX(transformedMesh, numberOfCuts); + SplitMeshAlongX(transformedMesh, cuts); if (!BendCcw) { @@ -171,7 +179,7 @@ namespace MatterHackers.MatterControl.DesignTools } // transform back into item local space - transformedMesh.Transform(Matrix4X4.CreateTranslation(-rotationOffset) * itemMatrix.Inverted); + transformedMesh.Transform(Matrix4X4.CreateTranslation(-RotationOffset) * itemMatrix.Inverted); transformedMesh.MarkAsChanged(); transformedMesh.CalculateNormals(); @@ -186,21 +194,22 @@ namespace MatterHackers.MatterControl.DesignTools // set the matrix back Matrix = currentMatrix; - this.Translate(new Vector3(rotationOffset)); + this.Translate(new Vector3(RotationOffset)); SourceContainer.Visible = false; rebuildLocks.Dispose(); } + Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children)); return Task.CompletedTask; }); } - private Mesh SplitMeshAlongX(Mesh mesh, List xCuts) + public static Mesh SplitMeshAlongX(Mesh mesh, List cuts) { var result = new Mesh(); var splitSections = new List(); - for(int i = 0; i< xCuts.Count; i++) + for (int i = 0; i < cuts.Count; i++) { // add a mesh to hold all the polygons that need to be split for each split section splitSections.Add(new Mesh()); @@ -208,19 +217,33 @@ namespace MatterHackers.MatterControl.DesignTools var bounds = mesh.GetAxisAlignedBoundingBox(); + var vertices = mesh.Vertices; + // add the face to every split section it crosses a side of foreach (var face in mesh.Faces) { var faceBounds = face.GetAxisAlignedBoundingBox(mesh); + for (int i = 0; i < cuts.Count; i++) + { + // if the face right >= cuts[0] (left) + // and face left <= cuts[1] (right) + if (faceBounds.MaxXYZ.X <= cuts[i] + && faceBounds.MinXYZ.X >= cuts[i + 1]) + { + var subMesh = splitSections[i]; + subMesh.CreateFace(new Vector3Float[] { vertices[face.v0], vertices[face.v1], vertices[face.v2] }); + } + } } // foreach split section, cut all the polygons that cross the sides of it foreach (var sectionMesh in splitSections) { // find what cut needs to be done to each face - // add the cut faces back to result } + // union all the faces back into results + // clean the result return result;