Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
arjendeetman committed Apr 11, 2023
1 parent dbbd58e commit 352ef8b
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
36 changes: 10 additions & 26 deletions SaladSlicer/Geometry/Curves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// System Libs
using System;
using System.Linq;
using System.Collections.Generic;
// Rhino Libs
using Rhino.Geometry;
Expand All @@ -22,13 +23,9 @@ public static class Curves
/// </summary>
/// <param name="curve"> The curve. </param>
/// <returns> The curve with a new domain. </returns>
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);
}

/// <summary>
Expand Down Expand Up @@ -59,12 +56,7 @@ public static double NumberClosed(IList<Curve> curves)
/// <returns> The alternated curves. </returns>
public static List<Curve> AlternateCurves(IList<Curve> curves)
{
List<Curve> result = new List<Curve>() { };

for (int i = 0; i < curves.Count; i++)
{
result.Add(curves[i].DuplicateCurve());
}
List<Curve> result = curves.ToList().ConvertAll(item => item.DuplicateCurve());

for (int i = 1; i < result.Count; i += 2)
{
Expand Down Expand Up @@ -190,12 +182,7 @@ public static Curve[] SplitAtLength(Curve curve, double length)
/// <returns> List with aligned curves. </returns>
public static List<Curve> AlignCurves(IList<Curve> curves)
{
List<Curve> result = new List<Curve>() { };

for (int i = 0; i < curves.Count; i++)
{
result.Add(curves[i].DuplicateCurve());
}
List<Curve> result = curves.ToList().ConvertAll(item => item.DuplicateCurve());

for (int i = 1; i < result.Count; i++)
{
Expand All @@ -219,18 +206,14 @@ public static List<Curve> AlignCurves(IList<Curve> curves)
/// </remarks>
/// <param name="curve1"> The curve to align. </param>
/// <param name="curve2"> The curve to check againts. </param>
/// <returns> The aligned curve. </returns>
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;
}

/// <summary>
Expand Down Expand Up @@ -271,6 +254,7 @@ public static Curve InterpolateCurves(Curve curve1, Curve curve2, double toleran
#endregion

#region properties

#endregion
}
}
64 changes: 25 additions & 39 deletions SaladSlicer/Geometry/Seams/Locations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// System Libs
using System;
using System.Linq;
using System.Collections.Generic;
// Rhino Libs
using Rhino.Geometry;
Expand All @@ -25,31 +26,27 @@ public static class Locations
/// <param name="param">The parameter of the new point at start.</param>
/// <param name="reparametrized"> Indicates if the curve domain is normalized [0 - 1]. </param>
/// <returns> The closed curve with a new point at start location. </returns>
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;
}

/// <summary>
Expand All @@ -59,7 +56,7 @@ public static Curve SeamAtParam(Curve curve, double param, bool reparametrized =
/// <param name="length"> The position of the new start point defined by the length. </param>
/// <param name="normalized"> Indicates if the length factor is normalized [0 - 1] </param>
/// <returns> The closed curve with a new point at start location. </returns>
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)
{
Expand All @@ -82,44 +79,36 @@ 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();
}

/// <summary>
/// Returns the curve with the starting point closest to the given test point. Requires a closed curve.
/// </summary>
/// <param name="curve"> The closed curve to change the point at start from. </param>
/// <param name="point"> The test point. </param>
/// <returns> The closed curve with a new point at start location. </returns>
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();
}

/// <summary>
Expand All @@ -128,17 +117,15 @@ public static Curve SeamAtClosestPoint(Curve curve, Point3d point)
/// <param name="curve"> The closed curve to change the point at start from. </param>
/// <param name="guide"> Guiding curve. </param>
/// <returns> The closed curve with a new point at start location. </returns>
public static Curve SeamClosestToCurve(Curve curve, Curve guide)
public static void SeamClosestToCurve(this Curve curve, Curve guide)
{
if (curve.IsClosed == false)
{
throw new Exception("The method Seam Closest to Curve requires a closed curve.");
}

curve.ClosestPoints(guide, out Point3d point, out _);
Curve result = SeamAtClosestPoint(curve, point);

return result;
curve.SeamAtClosestPoint(point);
}

/// <summary>
Expand All @@ -148,7 +135,7 @@ public static Curve SeamClosestToCurve(Curve curve, Curve guide)
/// <param name="curve"> The closed curve to change the point at start from. </param>
/// <param name="plane"> The intersection plane. </param>
/// <returns> The closed curve with a new point at start location. </returns>
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);

Expand Down Expand Up @@ -180,7 +167,7 @@ public static Curve SeamAtClosestPlaneIntersection(Curve curve, Plane plane)
}
}

return SeamAtClosestPoint(curve, closestPoint);
curve.SeamAtClosestPoint(closestPoint);
}
}
#endregion
Expand All @@ -194,8 +181,7 @@ public static Curve SeamAtClosestPlaneIntersection(Curve curve, Plane plane)
/// <returns> List with closed curves with a new point at start. </returns>
public static List<Curve> AlignSeamsByClosestPoint(IList<Curve> curves)
{
List<Curve> result = new List<Curve>() { };
result.Add(curves[0].DuplicateCurve());
List<Curve> result = curves.ToList().ConvertAll(item => item.DuplicateCurve());

Point3d testPoint = curves[0].PointAtStart;
curves[0].ClosestPoint(testPoint, out double t);
Expand All @@ -204,7 +190,7 @@ public static List<Curve> AlignSeamsByClosestPoint(IList<Curve> 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;
Expand All @@ -219,11 +205,11 @@ public static List<Curve> AlignSeamsByClosestPoint(IList<Curve> curves)
/// <returns> List with closed curves with a new point at start. </returns>
public static List<Curve> AlignSeamsAlongCurve(IList<Curve> curves, Curve guide)
{
List<Curve> result = new List<Curve>() { };
List<Curve> 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;
Expand All @@ -239,11 +225,11 @@ public static List<Curve> AlignSeamsAlongCurve(IList<Curve> curves, Curve guide)

public static List<Curve> AlignSeamsAlongClosestPlaneIntersection(IList<Curve> curves, Plane plane)
{
List<Curve> result = new List<Curve>() { };
List<Curve> 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;
Expand Down
3 changes: 1 addition & 2 deletions SaladSlicer/Nozzles/Nozzles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@ private static Brep LoftWithLinearCrossSection(Curve curve1, Curve curve2, int c
/// <returns> The loft betweeon two curves. </returns>
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<Curve>() { curve1, curve2 }, Point3d.Unset, Point3d.Unset, LoftType.Normal, false));

Expand Down
6 changes: 4 additions & 2 deletions SaladSlicer/Slicers/ClosedPlanar2DSlicer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
3 changes: 1 addition & 2 deletions SaladSlicer/Slicers/ClosedPlanarMeshSlicer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 352ef8b

Please sign in to comment.