Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NurbsBase.PerpendicularFrames #412

Merged
merged 4 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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