From 352ef8b3ba90aa0d9963d71ab75b338735c7c3d6 Mon Sep 17 00:00:00 2001 From: Arjen Deetman Date: Tue, 11 Apr 2023 17:14:46 +0200 Subject: [PATCH] Code improvements --- ...SeamAtClosestPlaneIntersectionComponent.cs | 4 +- .../SeamAtClosestPointComponent.cs | 4 +- .../Geometry part 2/SeamAtLengthComponent.cs | 4 +- .../Geometry part 2/SeamAtParamComponent.cs | 4 +- SaladSlicer/Geometry/Curves.cs | 36 +++-------- SaladSlicer/Geometry/Seams/Locations.cs | 64 ++++++++----------- SaladSlicer/Nozzles/Nozzles.cs | 3 +- SaladSlicer/Slicers/ClosedPlanar2DSlicer.cs | 6 +- SaladSlicer/Slicers/ClosedPlanarMeshSlicer.cs | 3 +- 9 files changed, 49 insertions(+), 79 deletions(-) diff --git a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPlaneIntersectionComponent.cs b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPlaneIntersectionComponent.cs index 6377287..fb97fe1 100644 --- a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPlaneIntersectionComponent.cs +++ b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPlaneIntersectionComponent.cs @@ -67,12 +67,12 @@ protected override void SolveInstance(IGH_DataAccess DA) if (!DA.GetData(1, ref plane)) return; // Declare the output variable - Curve result = curve; + Curve result = curve.DuplicateCurve(); // Create the new curve try { - result = Locations.SeamAtClosestPlaneIntersection(curve, plane); + result.SeamAtClosestPlaneIntersection(plane); } catch (WarningException w) { diff --git a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPointComponent.cs b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPointComponent.cs index 7c67e1c..c5b1eba 100644 --- a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPointComponent.cs +++ b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtClosestPointComponent.cs @@ -67,12 +67,12 @@ protected override void SolveInstance(IGH_DataAccess DA) if (!DA.GetData(1, ref point)) return; // Delcare the output variable - Curve result = curve; + Curve result = curve.DuplicateCurve(); // Create the new curve try { - result = Locations.SeamAtClosestPoint(curve, point); + result.SeamAtClosestPoint(point); } catch (WarningException w) { diff --git a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtLengthComponent.cs b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtLengthComponent.cs index 17a431f..c626753 100644 --- a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtLengthComponent.cs +++ b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtLengthComponent.cs @@ -70,12 +70,12 @@ protected override void SolveInstance(IGH_DataAccess DA) if (!DA.GetData(2, ref normalize)) return; // Declare the output variables - Curve result = curve; + Curve result = curve.DuplicateCurve(); // Create the new curve try { - result = Locations.SeamAtLength(curve, length, normalize); + result.SeamAtLength(length, normalize); } catch (WarningException w) { diff --git a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtParamComponent.cs b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtParamComponent.cs index f2e4c02..e1d74b1 100644 --- a/SaladSlicer.Gh/Components/Geometry part 2/SeamAtParamComponent.cs +++ b/SaladSlicer.Gh/Components/Geometry part 2/SeamAtParamComponent.cs @@ -67,12 +67,12 @@ protected override void SolveInstance(IGH_DataAccess DA) if (!DA.GetData(1, ref param)) return; // Declare the output variables - Curve result = curve; + Curve result = curve.DuplicateCurve(); // Create the new curve try { - result = Locations.SeamAtParam(curve, param); + result.SeamAtParam(param); } catch (WarningException w) { diff --git a/SaladSlicer/Geometry/Curves.cs b/SaladSlicer/Geometry/Curves.cs index 1462884..26b851f 100644 --- a/SaladSlicer/Geometry/Curves.cs +++ b/SaladSlicer/Geometry/Curves.cs @@ -5,6 +5,7 @@ // System Libs using System; +using System.Linq; using System.Collections.Generic; // Rhino Libs using Rhino.Geometry; @@ -22,13 +23,9 @@ public static class Curves /// /// The curve. /// The curve with a new domain. - public static Curve ResetDomain(Curve curve) + public static void ResetDomain(this Curve curve) { - Curve result = curve.DuplicateCurve(); - Interval domain = new Interval(curve.Domain.Min - curve.Domain.Min, curve.Domain.Max - curve.Domain.Min); - result.Domain = domain; - - return result; + curve.Domain = new Interval(curve.Domain.Min - curve.Domain.Min, curve.Domain.Max - curve.Domain.Min); } /// @@ -59,12 +56,7 @@ public static double NumberClosed(IList curves) /// The alternated curves. public static List AlternateCurves(IList curves) { - List result = new List() { }; - - for (int i = 0; i < curves.Count; i++) - { - result.Add(curves[i].DuplicateCurve()); - } + List result = curves.ToList().ConvertAll(item => item.DuplicateCurve()); for (int i = 1; i < result.Count; i += 2) { @@ -190,12 +182,7 @@ public static Curve[] SplitAtLength(Curve curve, double length) /// List with aligned curves. public static List AlignCurves(IList curves) { - List result = new List() { }; - - for (int i = 0; i < curves.Count; i++) - { - result.Add(curves[i].DuplicateCurve()); - } + List result = curves.ToList().ConvertAll(item => item.DuplicateCurve()); for (int i = 1; i < result.Count; i++) { @@ -219,18 +206,14 @@ public static List AlignCurves(IList curves) /// /// The curve to align. /// The curve to check againts. - /// The aligned curve. - public static Curve AlignCurve(Curve curve1, Curve curve2) + public static void AlignCurve(this Curve curve1, Curve curve2) { - Curve result = curve1.DuplicateCurve(); - if (!Curve.DoDirectionsMatch(curve1, curve2)) { - result.Reverse(); - result.Domain = curve1.Domain; + Interval domain = new Interval(curve1.Domain.Min, curve1.Domain.Max); + curve1.Reverse(); + curve1.Domain = domain; } - - return result; } /// @@ -271,6 +254,7 @@ public static Curve InterpolateCurves(Curve curve1, Curve curve2, double toleran #endregion #region properties + #endregion } } diff --git a/SaladSlicer/Geometry/Seams/Locations.cs b/SaladSlicer/Geometry/Seams/Locations.cs index 9d5b891..4873dee 100644 --- a/SaladSlicer/Geometry/Seams/Locations.cs +++ b/SaladSlicer/Geometry/Seams/Locations.cs @@ -5,6 +5,7 @@ // System Libs using System; +using System.Linq; using System.Collections.Generic; // Rhino Libs using Rhino.Geometry; @@ -25,31 +26,27 @@ public static class Locations /// The parameter of the new point at start. /// Indicates if the curve domain is normalized [0 - 1]. /// The closed curve with a new point at start location. - public static Curve SeamAtParam(Curve curve, double param, bool reparametrized = false) + public static void SeamAtParam(this Curve curve, double param, bool reparametrized = false) { if (curve.IsClosed == false) { throw new Exception("The method Seam at Param requires a closed curve."); } - Curve result = curve.DuplicateCurve(); - if (reparametrized == true) { - result.Domain = new Interval(0, 1); + curve.Domain = new Interval(0, 1); } - if (param >= result.Domain.T0 & param <= result.Domain.T1) + if (param >= curve.Domain.T0 & param <= curve.Domain.T1) { - result.ChangeClosedCurveSeam(param); - result = Curves.ResetDomain(result); + curve.ChangeClosedCurveSeam(param); + curve.ResetDomain(); } else { throw new Exception("Parameter is not inside curve domain."); } - - return result; } /// @@ -59,7 +56,7 @@ public static Curve SeamAtParam(Curve curve, double param, bool reparametrized = /// The position of the new start point defined by the length. /// Indicates if the length factor is normalized [0 - 1] /// The closed curve with a new point at start location. - public static Curve SeamAtLength(Curve curve, double length, bool normalized = false) + public static void SeamAtLength(this Curve curve, double length, bool normalized = false) { if (curve.IsClosed == false) { @@ -82,22 +79,19 @@ public static Curve SeamAtLength(Curve curve, double length, bool normalized = f throw new Exception("Normalized length factor cannot be larger than 1."); } - Curve result = curve.DuplicateCurve(); double param; if (normalized == true) { - result.NormalizedLengthParameter(length, out param); + curve.NormalizedLengthParameter(length, out param); } else { - result.LengthParameter(length, out param); + curve.LengthParameter(length, out param); } - result.ChangeClosedCurveSeam(param); - result = Curves.ResetDomain(result); - - return result; + curve.ChangeClosedCurveSeam(param); + curve.ResetDomain(); } /// @@ -105,21 +99,16 @@ public static Curve SeamAtLength(Curve curve, double length, bool normalized = f /// /// The closed curve to change the point at start from. /// The test point. - /// The closed curve with a new point at start location. - public static Curve SeamAtClosestPoint(Curve curve, Point3d point) + public static void SeamAtClosestPoint(this Curve curve, Point3d point) { if (curve.IsClosed == false) { throw new Exception("The method Seam at Closest Point requires a closed curve."); } - Curve result = curve.DuplicateCurve(); - - result.ClosestPoint(point, out double param); - result.ChangeClosedCurveSeam(param); - result = Curves.ResetDomain(result); - - return result; + curve.ClosestPoint(point, out double param); + curve.ChangeClosedCurveSeam(param); + curve.ResetDomain(); } /// @@ -128,7 +117,7 @@ public static Curve SeamAtClosestPoint(Curve curve, Point3d point) /// The closed curve to change the point at start from. /// Guiding curve. /// The closed curve with a new point at start location. - public static Curve SeamClosestToCurve(Curve curve, Curve guide) + public static void SeamClosestToCurve(this Curve curve, Curve guide) { if (curve.IsClosed == false) { @@ -136,9 +125,7 @@ public static Curve SeamClosestToCurve(Curve curve, Curve guide) } curve.ClosestPoints(guide, out Point3d point, out _); - Curve result = SeamAtClosestPoint(curve, point); - - return result; + curve.SeamAtClosestPoint(point); } /// @@ -148,7 +135,7 @@ public static Curve SeamClosestToCurve(Curve curve, Curve guide) /// The closed curve to change the point at start from. /// The intersection plane. /// The closed curve with a new point at start location. - public static Curve SeamAtClosestPlaneIntersection(Curve curve, Plane plane) + public static void SeamAtClosestPlaneIntersection(this Curve curve, Plane plane) { CurveIntersections intersections = Intersection.CurvePlane(curve, plane, 0.0); @@ -180,7 +167,7 @@ public static Curve SeamAtClosestPlaneIntersection(Curve curve, Plane plane) } } - return SeamAtClosestPoint(curve, closestPoint); + curve.SeamAtClosestPoint(closestPoint); } } #endregion @@ -194,8 +181,7 @@ public static Curve SeamAtClosestPlaneIntersection(Curve curve, Plane plane) /// List with closed curves with a new point at start. public static List AlignSeamsByClosestPoint(IList curves) { - List result = new List() { }; - result.Add(curves[0].DuplicateCurve()); + List result = curves.ToList().ConvertAll(item => item.DuplicateCurve()); Point3d testPoint = curves[0].PointAtStart; curves[0].ClosestPoint(testPoint, out double t); @@ -204,7 +190,7 @@ public static List AlignSeamsByClosestPoint(IList curves) { testPoint = curves[i - 1].PointAt(t); curves[i].ClosestPoint(testPoint, out t); - result.Add(SeamAtParam(curves[i], t)); + result[i].SeamAtParam(t); } return result; @@ -219,11 +205,11 @@ public static List AlignSeamsByClosestPoint(IList curves) /// List with closed curves with a new point at start. public static List AlignSeamsAlongCurve(IList curves, Curve guide) { - List result = new List() { }; + List result = curves.ToList().ConvertAll(item => item.DuplicateCurve()); for (int i = 0; i < curves.Count; i++) { - result.Add(SeamClosestToCurve(curves[i], guide)); + curves[i].SeamClosestToCurve(guide); } return result; @@ -239,11 +225,11 @@ public static List AlignSeamsAlongCurve(IList curves, Curve guide) public static List AlignSeamsAlongClosestPlaneIntersection(IList curves, Plane plane) { - List result = new List() { }; + List result = curves.ToList().ConvertAll(item => item.DuplicateCurve()); for (int i = 0; i < curves.Count; i++) { - result.Add(SeamAtClosestPlaneIntersection(curves[i], plane)); + curves[i].SeamAtClosestPlaneIntersection(plane); } return result; diff --git a/SaladSlicer/Nozzles/Nozzles.cs b/SaladSlicer/Nozzles/Nozzles.cs index 65a267b..8c25768 100644 --- a/SaladSlicer/Nozzles/Nozzles.cs +++ b/SaladSlicer/Nozzles/Nozzles.cs @@ -456,8 +456,7 @@ private static Brep LoftWithLinearCrossSection(Curve curve1, Curve curve2, int c /// The loft betweeon two curves. private static Brep Loft(Curve curve1, Curve curve2) { - curve2 = Curves.AlignCurve(curve2, curve1); - curve1 = Locations.SeamAtClosestPoint(curve1, curve1.PointAtStart); + curve2.AlignCurve(curve1); Brep[] breps = (Brep.CreateFromLoft(new List() { curve1, curve2 }, Point3d.Unset, Point3d.Unset, LoftType.Normal, false)); diff --git a/SaladSlicer/Slicers/ClosedPlanar2DSlicer.cs b/SaladSlicer/Slicers/ClosedPlanar2DSlicer.cs index a55b897..9811453 100644 --- a/SaladSlicer/Slicers/ClosedPlanar2DSlicer.cs +++ b/SaladSlicer/Slicers/ClosedPlanar2DSlicer.cs @@ -180,8 +180,10 @@ public void Slice() private void CreateContours() { _contours.Clear(); - Curve contour = Geometry.Seams.Locations.SeamAtLength(_baseContour, _seamLocation, true); - //contour = Geometry.Seams.Locations.SeamAtLength(contour, contour.GetLength() - 0.5 * _seamLength, false); //TODO: to discuss... + + Curve contour = _baseContour.DuplicateCurve(); + contour.SeamAtLength(_seamLocation, true); + contour.Domain = new Interval(0, contour.GetLength()); for (int i = 0; i < _heights.Count; i++) diff --git a/SaladSlicer/Slicers/ClosedPlanarMeshSlicer.cs b/SaladSlicer/Slicers/ClosedPlanarMeshSlicer.cs index c73a41e..e61ac79 100644 --- a/SaladSlicer/Slicers/ClosedPlanarMeshSlicer.cs +++ b/SaladSlicer/Slicers/ClosedPlanarMeshSlicer.cs @@ -249,8 +249,7 @@ private void CreateContours() // Set seam location _contours = Curves.AlignCurves(_contours); - _contours[0] = Locations.SeamAtLength(_contours[0], _seamLocation, true); - //_contours[0] = Geometry.Seams.Locations.SeamAtLength(_contours[0], _contours[0].GetLength() - 0.5 * _seamLength, false); //TODO: to discuss... + _contours[0].SeamAtLength(_seamLocation, true); _contours = Locations.AlignSeamsByClosestPoint(_contours); // Reverse the contours