Skip to content

Commit

Permalink
Merge pull request #370 from GSharker/dev/guma/point-at-chord-length
Browse files Browse the repository at this point in the history
Dev/guma/point at chord length
  • Loading branch information
d3ssy authored Sep 20, 2021
2 parents 00b81f0 + 53a435b commit 0a6f0d3
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/GShark.Test.XUnit/Analyze/CurveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void It_Returns_The_Approximated_Length_Of_A_Bezier()
public void It_Returns_Parameter_At_The_Given_Length_Of_A_Bezier()
{
// Arrange
NurbsBase curve = NurbsBaseCollection.NurbsPlanarExample();
NurbsBase curve = NurbsCurveCollection.PlanarCurveDegreeThree();
double[] tValuesExpected = new[] { 0, 0.122941, 0.265156, 0.420293, 0.579707, 0.734844, 0.877059, 1 };

int steps = 7;
Expand All @@ -69,7 +69,7 @@ public void It_Returns_Parameter_At_The_Given_Length_Of_A_Bezier()
public void It_Returns_The_Length_Of_The_Curve()
{
// Arrange
NurbsBase curve = NurbsBaseCollection.NurbsPlanarExample();
NurbsBase curve = NurbsCurveCollection.PlanarCurveDegreeThree();
double expectedLength = 50.334675;

// Act
Expand All @@ -89,7 +89,7 @@ public void It_Returns_The_Length_Of_The_Curve()
public void It_Returns_The_Closest_Point_And_Parameter(double[] ptToCheck, double[] ptExpected, double tValExpected)
{
// Arrange
NurbsBase curve = NurbsBaseCollection.NurbsPlanarExample();
NurbsBase curve = NurbsCurveCollection.PlanarCurveDegreeThree();
Point3 testPt = new Point3(ptToCheck[0], ptToCheck[1], ptToCheck[2]);
Point3 expectedPt = new Point3(ptExpected[0], ptExpected[1], ptExpected[2]);

Expand All @@ -111,7 +111,7 @@ public void It_Returns_The_Closest_Point_And_Parameter(double[] ptToCheck, doubl
public void It_Returns_Parameter_At_The_Given_Length(double segmentLength, double tValueExpected)
{
// Arrange
NurbsBase curve = NurbsBaseCollection.NurbsPlanarExample();
NurbsBase curve = NurbsCurveCollection.PlanarCurveDegreeThree();

// Act
double parameter = curve.ParameterAtLength(segmentLength);
Expand Down
24 changes: 12 additions & 12 deletions src/GShark.Test.XUnit/Data/NurbsCurveCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GShark.Test.XUnit.Data
{
public class NurbsBaseCollection
public class NurbsCurveCollection
{
public static NurbsCurve NurbsBaseExample()
{
Expand All @@ -18,7 +18,7 @@ public static NurbsCurve NurbsBaseExample()
return new NurbsCurve(pts, degree);
}

public static NurbsCurve NurbsPtsAndWeightsExample()
public static NurbsCurve RationalCurveDegreeTwo()
{
int degree = 2;
List<Point3> pts = new List<Point3>
Expand All @@ -32,7 +32,7 @@ public static NurbsCurve NurbsPtsAndWeightsExample()
return new NurbsCurve(pts, weights, degree);
}

public static NurbsCurve NurbsPlanarExample()
public static NurbsCurve PlanarCurveDegreeThree()
{
int degree = 3;
List<Point3> pts = new List<Point3>
Expand All @@ -47,7 +47,7 @@ public static NurbsCurve NurbsPlanarExample()
return new NurbsCurve(pts, degree);
}

public static NurbsCurve Nurbs3DExample()
public static NurbsCurve DegreeThreeCurve3D()
{
#region example
int degree = 3;
Expand All @@ -65,7 +65,7 @@ public static NurbsCurve Nurbs3DExample()
return curve;
}

public static NurbsCurve NurbsCubicBezierPlanar()
public static NurbsCurve CubicBezierPlanar()
{
int degree = 3;
List<Point3> pts = new List<Point3>
Expand All @@ -78,7 +78,7 @@ public static NurbsCurve NurbsCubicBezierPlanar()
return new NurbsCurve(pts, degree);
}

public static NurbsCurve NurbsQuadraticBezierPlanar()
public static NurbsCurve QuadraticBezierPlanar()
{
int degree = 2;
List<Point3> pts = new List<Point3>
Expand All @@ -90,7 +90,7 @@ public static NurbsCurve NurbsQuadraticBezierPlanar()
return new NurbsCurve(pts, degree);
}

public static NurbsCurve NurbsBaseQuadratic3DBezier()
public static NurbsCurve QuadraticBezier3D()
{
int degree = 2;
List<Point3> pts = new List<Point3>
Expand All @@ -102,7 +102,7 @@ public static NurbsCurve NurbsBaseQuadratic3DBezier()
return new NurbsCurve(pts, degree);
}

public static NurbsBase PeriodicClosedNurbs()
public static NurbsBase PeriodicClosedCurves()
{
int degree = 3;
List<Point3> pts = new List<Point3>
Expand All @@ -116,7 +116,7 @@ public static NurbsBase PeriodicClosedNurbs()
return new NurbsCurve(pts, degree).Close();
}

public static NurbsCurve NurbsWithStartingAndEndPointOverlapping()
public static NurbsCurve WithStartingAndEndPointOverlapping()
{
int degree = 2;
List<Point3> pts = new List<Point3>
Expand All @@ -129,7 +129,7 @@ public static NurbsCurve NurbsWithStartingAndEndPointOverlapping()
return new NurbsCurve(pts, degree);
}

public static List<NurbsBase> OpenNurbs()
public static List<NurbsBase> OpenCurves()
{
int degree = 2;
List<Point3> points1 = new List<Point3> { new Point3(-20.0, 0.0, 0.0),
Expand All @@ -156,9 +156,9 @@ public static List<NurbsBase> OpenNurbs()
return new List<NurbsBase> { c1, c2, c3, c4 };
}

public static List<NurbsBase> ClosedNurbs()
public static List<NurbsBase> ClosedCurves()
{
List<NurbsBase> crvs = NurbsBaseCollection.OpenNurbs();
List<NurbsBase> crvs = NurbsCurveCollection.OpenCurves();
for (int i = 0; i < crvs.Count; i++)
crvs[i] = crvs[i].Close();

Expand Down
Binary file modified src/GShark.Test.XUnit/DebugFiles/GHDebug_Curves.gh
Binary file not shown.
2 changes: 1 addition & 1 deletion src/GShark.Test.XUnit/Evaluate/CurveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void It_Returns_The_Tangent_At_Given_Parameter(double t, double[] tangent
// Act
// Act on a linear nurbs curve.
Vector3 tangentLinearCurve = curve.TangentAt(t);
Vector3 tangentPlanarCurve = NurbsBaseCollection.NurbsPlanarExample().TangentAt(t);
Vector3 tangentPlanarCurve = NurbsCurveCollection.PlanarCurveDegreeThree().TangentAt(t);

// Assert
(tangentLinearCurve == tangentExpectedLinearCurve).Should().BeTrue();
Expand Down
89 changes: 71 additions & 18 deletions src/GShark.Test.XUnit/Geometry/NurbsCurveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static (int degree, List<Point3> pts, KnotVector knots, List<double> weig
public void It_Returns_A_NurbsBase()
{
// Act
NurbsBase NurbsBase = NurbsBaseCollection.NurbsPlanarExample();
NurbsBase NurbsBase = NurbsCurveCollection.PlanarCurveDegreeThree();

// Assert
NurbsBase.Should().NotBeNull();
Expand Down Expand Up @@ -76,22 +76,22 @@ public void It_Returns_A_Closed_NurbsBase()
public void It_Returns_True_If_A_NurbsBase_Is_Closed()
{
// Assert
NurbsBaseCollection.NurbsWithStartingAndEndPointOverlapping().IsClosed.Should().BeTrue();
NurbsBaseCollection.PeriodicClosedNurbs().IsClosed.Should().BeTrue();
NurbsCurveCollection.WithStartingAndEndPointOverlapping().IsClosed.Should().BeTrue();
NurbsCurveCollection.PeriodicClosedCurves().IsClosed.Should().BeTrue();
}

[Fact]
public void It_Returns_True_If_A_NurbsBase_Is_Periodic()
{
// Assert
NurbsBaseCollection.PeriodicClosedNurbs().IsPeriodic.Should().BeTrue();
NurbsCurveCollection.PeriodicClosedCurves().IsPeriodic.Should().BeTrue();
}

[Fact]
public void It_Returns_A_NurbsBase_Evaluated_With_A_List_Of_Weights()
{
// Act
NurbsBase NurbsBase = NurbsBaseCollection.NurbsPtsAndWeightsExample();
NurbsBase NurbsBase = NurbsCurveCollection.RationalCurveDegreeTwo();

// Assert
NurbsBase.Should().NotBeNull();
Expand All @@ -116,8 +116,8 @@ public void It_Creates_A_NurbsBase_From_ControlPoints_And_Degree()
public void It_Returns_The_Bounding_Box_Of_A_Planar_Curve()
{
// Arrange
NurbsBase crv0 = NurbsBaseCollection.NurbsCubicBezierPlanar();
NurbsBase crv1 = NurbsBaseCollection.NurbsQuadraticBezierPlanar();
NurbsBase crv0 = NurbsCurveCollection.CubicBezierPlanar();
NurbsBase crv1 = NurbsCurveCollection.QuadraticBezierPlanar();

var expectedPtMin0 = new Point3(0, 0, 0);
var expectedPtMax0 = new Point3(2, 0.444444, 0);
Expand All @@ -141,8 +141,8 @@ public void It_Returns_The_Bounding_Box_Of_A_Planar_Curve()
public void It_Returns_The_Bounding_Box_Of_A_3D_Nurbs_Curve()
{
// Arrange
NurbsBase crv0 = NurbsBaseCollection.Nurbs3DExample();
NurbsBase crv1 = NurbsBaseCollection.NurbsBaseQuadratic3DBezier();
NurbsBase crv0 = NurbsCurveCollection.DegreeThreeCurve3D();
NurbsBase crv1 = NurbsCurveCollection.QuadraticBezier3D();

var expectedPtMin0 = new Point3(0, 0.5555556, 0);
var expectedPtMax0 = new Point3(4.089468, 5, 5);
Expand Down Expand Up @@ -170,7 +170,7 @@ public void It_Returns_The_Bounding_Box_Of_A_Periodic_Curve()
Point3 expectedPtMax = new Point3(4.354648, 5, 3.333333);

// Act
BoundingBox bBox = NurbsBaseCollection.PeriodicClosedNurbs().GetBoundingBox();
BoundingBox bBox = NurbsCurveCollection.PeriodicClosedCurves().GetBoundingBox();

// Assert
bBox.Max.DistanceTo(expectedPtMax).Should().BeLessThan(GSharkMath.MaxTolerance);
Expand All @@ -181,7 +181,7 @@ public void It_Returns_The_Bounding_Box_Of_A_Periodic_Curve()
public void It_Transforms_A_NurbsBase_By_A_Given_Matrix()
{
// Arrange
var curve = NurbsBaseCollection.NurbsPlanarExample();
var curve = NurbsCurveCollection.PlanarCurveDegreeThree();
var transform = Transform.Translation(new Vector3(-10, 20, 0));

// Act
Expand All @@ -198,7 +198,7 @@ public void It_Transforms_A_NurbsBase_By_A_Given_Matrix()
public void It_Returns_A_NurbsBase_With_Clamped_End()
{
// Arrange
NurbsBase curve = NurbsBaseCollection.PeriodicClosedNurbs();
NurbsBase curve = NurbsCurveCollection.PeriodicClosedCurves();

// Act
NurbsBase curveClamped = curve.ClampEnds();
Expand Down Expand Up @@ -226,8 +226,8 @@ public void It_Returns_A_Perpendicular_Frame_At_Given_Parameter()
Vector3 expectedXDir1 = new Vector3(-0.690371, -0.162782, -0.704905);

// Act
Plane frame0 = NurbsBaseCollection.Nurbs3DExample().PerpendicularFrameAt(t0);
Plane frame1 = NurbsBaseCollection.Nurbs3DExample().PerpendicularFrameAt(t1);
Plane frame0 = NurbsCurveCollection.DegreeThreeCurve3D().PerpendicularFrameAt(t0);
Plane frame1 = NurbsCurveCollection.DegreeThreeCurve3D().PerpendicularFrameAt(t1);

// Assert
frame0.Origin.EpsilonEquals(expectedPlaneOrigin0, GSharkMath.MinTolerance).Should().BeTrue();
Expand All @@ -245,7 +245,7 @@ public void It_Returns_The_Curvature_Vector_At_The_Given_Parameter()
Vector3 expectedCurvature = new Vector3(1.044141, 0.730898, 0.730898);

// Act
Vector3 curvature = NurbsBaseCollection.Nurbs3DExample().CurvatureAt(0.25);
Vector3 curvature = NurbsCurveCollection.DegreeThreeCurve3D().CurvatureAt(0.25);

// Assert
(curvature.Length - expectedRadiusLength).Should().BeLessThan(GSharkMath.MinTolerance);
Expand All @@ -256,7 +256,7 @@ public void It_Returns_The_Curvature_Vector_At_The_Given_Parameter()
public void It_Reverses_The_Curve()
{
// Arrange
NurbsBase curve = NurbsBaseCollection.NurbsCubicBezierPlanar();
NurbsBase curve = NurbsCurveCollection.CubicBezierPlanar();

// Act
NurbsBase crvRev1 = curve.Reverse();
Expand Down Expand Up @@ -288,11 +288,64 @@ public void Returns_The_Offset_Of_A_Curve()
}
}

[Fact]
public void It_Returns_A_Curve_Parameter_At_A_Given_ChordLength_From_A_Starting_Curve_Parameter()
{
//Arrange
var chordLength = 5.0;
var expectedParamOnCrv = 0.169828641346571;
var crv = NurbsCurveCollection.DegreeThreeCurve3D();
var startParam = 0;

//Act
double param = crv.ParameterAtChordLength(0.0, chordLength);

//Assert
var resultingChordLength = crv.PointAt(param).DistanceTo(crv.PointAt(startParam));
resultingChordLength.Should().BeApproximately(chordLength, GSharkMath.MinTolerance);
param.Should().BeApproximately(expectedParamOnCrv, GSharkMath.MinTolerance);
}

[Fact]
public void It_Divides_A_Curve_By_Chord_Length()
{
//Arrange
var chordLength = 1.5;
var crv = NurbsCurveCollection.DegreeThreeCurve3D();
var expectedParams = new List<double>() //excludes start point
{
0.038210676523551,
0.0841487542118296,
0.143655776583172,
0.235393592061853,
0.411166407250385,
0.535196596976369,
0.647125285838886,
0.790170373873905,
0.88786351007188 ,
0.945234051105388,
0.987630897234936
};

//Act
var divisionParams = crv.DivideByChordLength(chordLength);

//Assert
var resultingChordLengths = new List<double>();
for (int i = 0; i < divisionParams.Count - 1; i++)
{
divisionParams[i].Should().BeApproximately(expectedParams[i], GSharkMath.MinTolerance);
var tempChordLength = crv.PointAt(divisionParams[i]).DistanceTo(crv.PointAt(divisionParams[i + 1]));
resultingChordLengths.Add(tempChordLength);
}
resultingChordLengths.All(x => (chordLength - x) <= GSharkMath.MinTolerance).Should().BeTrue();
}

[Fact]
public void JoinCurve_Throw_An_Exception_If_The_Number_Of_Curves_Is_Insufficient()
{
// Arrange
NurbsBase[] curves = { NurbsBaseCollection.NurbsPlanarExample() };
NurbsBase[] curves = { NurbsCurveCollection.PlanarCurveDegreeThree() };

// Act
Func<object> func = () => NurbsBase.Join(curves);
Expand All @@ -305,7 +358,7 @@ public void JoinCurve_Throw_An_Exception_If_The_Number_Of_Curves_Is_Insufficient
public void JoinCurve_Throw_An_Exception_If_Curves_Are_Close_Enough_To_Be_Joined()
{
// Arrange
NurbsBase[] curves = { NurbsBaseCollection.NurbsPlanarExample(), NurbsBaseCollection.NurbsBaseQuadratic3DBezier() };
NurbsBase[] curves = { NurbsCurveCollection.PlanarCurveDegreeThree(), NurbsCurveCollection.QuadraticBezier3D() };

// Act
Func<object> func = () => NurbsBase.Join(curves);
Expand Down
14 changes: 7 additions & 7 deletions src/GShark.Test.XUnit/Geometry/NurbsSurfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void It_Returns_A_Normal_Lofted_Surface_By_Opened_Curves(double u, double
Point3 expectedPt = new Point3(pt[0], pt[1], pt[2]);

// Act
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsBaseCollection.OpenNurbs());
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsCurveCollection.OpenCurves());
Point3 evalPt = surface.PointAt(u, v);

// Assert
Expand All @@ -107,7 +107,7 @@ public void It_Returns_A_Loose_Lofted_Surface_By_Opened_Curves(double u, double
Point3 expectedPt = new Point3(pt[0], pt[1], pt[2]);

// Act
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsBaseCollection.OpenNurbs(), LoftType.Loose);
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsCurveCollection.OpenCurves(), LoftType.Loose);
Point3 evalPt = surface.PointAt(u, v);

// Assert
Expand All @@ -125,7 +125,7 @@ public void It_Returns_A_Loose_Lofted_Surface_By_Closed_Curves(double u, double
Point3 expectedPt = new Point3(pt[0], pt[1], pt[2]);

// Act
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsBaseCollection.ClosedNurbs(), LoftType.Loose);
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsCurveCollection.ClosedCurves(), LoftType.Loose);
Point3 evalPt = surface.PointAt(u, v);

// Assert
Expand All @@ -148,7 +148,7 @@ public void Lofted_Surface_Throws_An_Exception_If_The_Curves_Are_Null()
public void Lofted_Surface_Throws_An_Exception_If_There_Are_Null_Curves()
{
// Arrange
List<NurbsBase> crvs = NurbsBaseCollection.OpenNurbs();
List<NurbsBase> crvs = NurbsCurveCollection.OpenCurves();
crvs.Add(null);

// Act
Expand All @@ -163,7 +163,7 @@ public void Lofted_Surface_Throws_An_Exception_If_There_Are_Null_Curves()
public void Lofted_Surface_Throws_An_Exception_If_Curves_Count_Are_Less_Than_Two()
{
// Arrange
NurbsBase[] crvs = { NurbsBaseCollection.OpenNurbs()[0] };
NurbsBase[] crvs = { NurbsCurveCollection.OpenCurves()[0] };

// Act
Func<NurbsSurface> func = () => NurbsSurface.CreateLoftedSurface(crvs);
Expand All @@ -177,7 +177,7 @@ public void Lofted_Surface_Throws_An_Exception_If_Curves_Count_Are_Less_Than_Two
public void Lofted_Surface_Throws_An_Exception_If_The_All_Curves_Are_Not_Closed_Or_Open()
{
// Arrange
List<NurbsBase> crvs = NurbsBaseCollection.OpenNurbs();
List<NurbsBase> crvs = NurbsCurveCollection.OpenCurves();
crvs[1] = crvs[1].Close();

// Act
Expand Down Expand Up @@ -222,7 +222,7 @@ public void Returns_True_If_Two_Surfaces_Are_Equals()
public void Returns_True_If_Surface_Is_Close()
{
// Act
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsBaseCollection.ClosedNurbs(), LoftType.Loose);
NurbsSurface surface = NurbsSurface.CreateLoftedSurface(NurbsCurveCollection.ClosedCurves(), LoftType.Loose);

// Assert
surface.IsClosed(SurfaceDirection.V).Should().BeTrue();
Expand Down
Loading

0 comments on commit 0a6f0d3

Please sign in to comment.