Skip to content

Commit

Permalink
Merge pull request #290 from GSharker/dev/mibi/derivativeAt
Browse files Browse the repository at this point in the history
Added derivativeAt.
  • Loading branch information
cesarecaoduro authored Aug 26, 2021
2 parents 7b791c3 + b8456e6 commit dfdb3e7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
Binary file modified src/GShark.Test.XUnit/DebugFiles/GHDebug_Curves.gh
Binary file not shown.
24 changes: 24 additions & 0 deletions src/GShark.Test.XUnit/Geometry/CircleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,30 @@ public void It_Returns_The_Closest_Parameter_On_A_Circle(double[] ptToTest, doub
// Assert
parameter.Should().BeApproximately(expectedParameter, GSharkMath.MaxTolerance);
}

[Fact]
public void It_Returns_The_Derivatives_At_Given_Parameter()
{
// Arrange
Point3 expectedDerv0 = new Point3(69.253451, 22.768104, 1.732159);
Vector3 expectedDerv1 = new Vector3(13.388719, -99.325852, 25.95035);
Vector3 expectedDerv2 = new Vector3(645.009827, 80.060471, -26.349577);
Vector3 expectedDerv3 = new Vector3(-528.565438, 3921.227465, -1024.478763);

// Act
Vector3 derv0 = _circle3D.DerivativeAt(0.15, 0);
Vector3 derv1 = _circle3D.DerivativeAt(0.15, 1);
Vector3 derv2 = _circle3D.DerivativeAt(0.15, 2);
Vector3 derv3 = _circle3D.DerivativeAt(0.15, 3);
Point3 pt = _circle3D.Center + derv0;

// Assert
// The zero derivative is the vector identify the point on the circle.
expectedDerv0.EpsilonEquals(pt, GSharkMath.MaxTolerance).Should().BeTrue();
expectedDerv1.IsParallelTo(derv1).Should().NotBe(0);
expectedDerv2.IsParallelTo(derv2).Should().NotBe(0);
expectedDerv3.IsParallelTo(derv3).Should().NotBe(0);
}
}
}

54 changes: 47 additions & 7 deletions src/GShark/Geometry/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,51 @@ public virtual BoundingBox GetBoundingBox()
return new BoundingBox(min, max);
}

/// <summary>
/// Determines the value of the Nth derivative at a parameter.
/// </summary>
/// <param name="t">Parameter to evaluate derivative. A parameter between 0.0 to 1.0.</param>
/// <param name="derivative">Which order of derivative is wanted. Valid values are 0,1,2,3.</param>
/// <returns>The derivative of the circle at the given parameter.</returns>
public Vector3 DerivativeAt(double t, int derivative = 0)
{
if (t < 0.0)
{
t = 0.0;
}

if (t > 1.0)
{
t = 1.0;
}

double theta = Domain.T0 + (Domain.T1 - Domain.T0) * t;

double r0 = 0;
double r1 = 0;
switch (derivative % 4)
{
case 0:
r0 = Radius * Math.Cos(theta);
r1 = Radius * Math.Sin(theta);
break;
case 1:
r0 = Radius * -Math.Sin(theta);
r1 = Radius * Math.Cos(theta);
break;
case 2:
r0 = Radius * -Math.Cos(theta);
r1 = Radius * -Math.Sin(theta);
break;
case 3:
r0 = Radius * Math.Sin(theta);
r1 = Radius * -Math.Cos(theta);
break;
}

return r0 * Plane.XAxis + r1 * Plane.YAxis;
}

/// <summary>
/// Evaluates the point at the parameter t on the circular curve.
/// </summary>
Expand Down Expand Up @@ -187,13 +232,8 @@ public Vector3 TangentAt(double t)
}

double theta = Domain.T0 + (Domain.T1 - Domain.T0) * t;

double r1 = Radius * (-Math.Sin(theta));
double r2 = Radius * (Math.Cos(theta));

Vector3 vector = Plane.XAxis * r1 + Plane.YAxis * r2;

return vector.Unitize();
Vector3 derivative = DerivativeAt(theta, 1);
return derivative.Unitize();
}

/// <summary>
Expand Down

0 comments on commit dfdb3e7

Please sign in to comment.