Skip to content

Commit

Permalink
Merge pull request #412 from SearchAThing-forks/dev/devel0/fix-perp-f…
Browse files Browse the repository at this point in the history
…rames

fix NurbsBase.PerpendicularFrames
  • Loading branch information
d3ssy committed May 17, 2023
2 parents 14848cf + 2832682 commit d526a36
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
41 changes: 41 additions & 0 deletions src/GShark.Test.XUnit/Sampling/CurveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,47 @@ public void It_Calculates_Rotation_Minimized_Frames_Along_Curve()
}
}

[Fact]
public void It_Calculates_Rotation_Minimized_Frames_Along_Curve2()
{
//Arrange
List<Point3> railPts = new List<Point3>()
{
new Point3(-0.9214326, -1.0785674, -7.991379E-08),
new Point3(-0.4986757, -0.79609025, -7.991379E-08),
new Point3(0, -0.6968975, -7.991379E-08),
new Point3(0.4986757, -0.79609036, -7.991379E-08),
new Point3(0.9214326, -1.0785673, -7.991379E-08)
};
List<Point3> profilePts = new List<Point3>()
{
new Point3(-0.29289323, -1.7071068, 0),
new Point3(-0.50000006, -1.5, 0.7071067),
new Point3(-1, -1, 0.99999994),
new Point3(-1.5, -0.50000006, 0.7071067),
new Point3(-1.7071067, -0.2928933, -8.742278E-08),
new Point3(-1.4999999, -0.50000006, -0.7071068),
new Point3(-1, -1, -0.99999994),
new Point3(-0.5000001, -1.4999998, -0.7071068),
new Point3(-0.29289323, -1.7071068, -3.0199158E-07)
};

var rail = new NurbsCurve(railPts, 1);
var profile = new NurbsCurve(profilePts, 1);

var sweepNurb = NurbsSurface.FromSweep(rail, profile);

var tValues = new [] { 0d, .25d, .5d, .75d, 1d }.ToList();
List<Plane> frames = rail.PerpendicularFrames(tValues);

foreach (var frame in frames)
{
frame.XAxis.Length.Should().BeApproximately(1, GSharkMath.MinTolerance);
frame.YAxis.Length.Should().BeApproximately(1, GSharkMath.MinTolerance);
frame.ZAxis.Length.Should().BeApproximately(1, GSharkMath.MinTolerance);
}
}

[Fact]
public void Return_Adaptive_Sample_Subdivision_Of_A_Nurbs()
{
Expand Down
2 changes: 1 addition & 1 deletion src/GShark/GShark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AssemblyName>GShark</AssemblyName>
<RootNamespace>GShark</RootNamespace>
<LangVersion>8.0</LangVersion>
<LangVersion>11.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Sharker/G-Shark</RepositoryUrl>
Expand Down
16 changes: 12 additions & 4 deletions src/GShark/Geometry/NurbsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,17 +615,21 @@ public NurbsBase ReduceDegree(double tolerance = 10e-4)
/// Double reflection method taken from Wang, W., J¨uttler, B., Zheng, D., and Liu, Y. 2008. "Computation of rotation minimizing frame."<br/>
/// https://www.microsoft.com/en-us/research/wp-content/uploads/2016/12/Computation-of-rotation-minimizing-frames.pdf
/// </summary>
///<param name="uValues">The curve parameter values to locate perpendicular curve frames</param>
/// <param name="uValues">The curve parameter values to locate perpendicular curve frames</param>
/// <param name="startTangent">If not null override start tangent vector.</param>
/// <param name="endTangent">If not null override end tangent vector.</param>
/// <returns>A collection of planes.</returns>
public List<Plane> PerpendicularFrames(List<double> uValues)
public List<Plane> PerpendicularFrames(List<double> uValues,
Vector3? startTangent = null,
Vector3? endTangent = null)
{
var pointsOnCurve = uValues.Select(PointAt).ToList(); //get points at t values
var pointsOnCurveTan = uValues.Select(t => Evaluate.Curve.RationalDerivatives(this, t, 1)[1]).ToList(); //get tangents at t values
var firstParameter = uValues[0]; //get first t value

//Create initial frame at first parameter
var origin = PointAt(firstParameter);
var crvTan = Evaluate.Curve.RationalDerivatives(this, firstParameter, 1)[1];
var crvTan = startTangent is not null ? startTangent.Value : Evaluate.Curve.RationalDerivatives(this, firstParameter, 1)[1];
var crvNormal = Vector3.PerpendicularTo(crvTan);
var yAxis = Vector3.CrossProduct(crvTan, crvNormal);
var xAxis = Vector3.CrossProduct(yAxis, crvTan);
Expand Down Expand Up @@ -659,7 +663,11 @@ public List<Plane> PerpendicularFrames(List<double> uValues)
var sNext = Vector3.CrossProduct(pointsOnCurveTan[i + 1], rNext); //compute vector s[i+1] of next frame

//create output frame
var frameNext = new Plane { Origin = pointsOnCurve[i + 1], XAxis = rNext, YAxis = sNext };
if (i == pointsOnCurve.Count - 2 && endTangent is not null)
{
rNext = Vector3.CrossProduct(sNext, endTangent.Value);
}
var frameNext = new Plane(pointsOnCurve[i + 1], rNext, sNext);
perpFrames[i + 1] = frameNext; //output frame
}

Expand Down
8 changes: 6 additions & 2 deletions src/GShark/Geometry/NurbsSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,15 @@ public static NurbsSurface FromExtrusion(Vector3 direction, NurbsBase profile)
/// </summary>
/// <param name="rail">The rail curve.</param>
/// <param name="profile">The section curve.</param>
/// <param name="startTangent">If not null override start tangent vector.</param>
/// <param name="endTangent">If not null override end tangent vector.</param>
/// <returns>The sweep surface.</returns>
public static NurbsSurface FromSweep(NurbsBase rail, NurbsBase profile)
public static NurbsSurface FromSweep(NurbsBase rail, NurbsBase profile,
Vector3? startTangent = null,
Vector3? endTangent = null)
{
var (tValues, _) = Sampling.Curve.AdaptiveSample(rail, GSharkMath.MaxTolerance);
List<Plane> frames = rail.PerpendicularFrames(tValues);
List<Plane> frames = rail.PerpendicularFrames(tValues, startTangent, endTangent);
List<NurbsBase> curves = new List<NurbsBase> {profile};

for (int i = 1; i < frames.Count; i++)
Expand Down

0 comments on commit d526a36

Please sign in to comment.