Skip to content

Commit

Permalink
Merge pull request #359 from GSharker/dev/mibi/line-inheritance-nurbs…
Browse files Browse the repository at this point in the history
…base

Line inheritance NurbsBase.
  • Loading branch information
sonomirco authored Sep 11, 2021
2 parents 874862e + 5de1586 commit 5f74da4
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/GShark.Test.XUnit/Data/NurbsSurfaceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static NurbsSurface Loft()
PolyLine poly = new PolyLine(pts1);
NurbsCurve crv1 = new NurbsCurve(pts2, 2);
NurbsCurve crv2 = new NurbsCurve(pts3, 3);
List<NurbsBase> crvs = new List<NurbsBase> { ln.ToNurbs(), crv0, poly.ToNurbs(), crv1, crv2 };
List<NurbsBase> crvs = new List<NurbsBase> { ln, crv0, poly.ToNurbs(), crv1, crv2 };

return NurbsSurface.CreateLoftedSurface(crvs);
}
Expand Down
17 changes: 7 additions & 10 deletions src/GShark.Test.XUnit/Geometry/LineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void It_Returns_The_Parameter_On_The_Line_Closest_To_The_Point(double exp
public void It_Returns_A_Flipped_Line()
{
// Act
Line flippedLine = _exampleLine.Flip();
Line flippedLine = _exampleLine.Reverse();

// Assert
flippedLine.StartPoint.Equals(_exampleLine.EndPoint).Should().BeTrue();
Expand Down Expand Up @@ -263,8 +263,8 @@ public void Returns_The_Offset_Of_A_Line()
public void It_Checks_If_Two_Lines_Are_Equals()
{
// Act
Line lineFlip = _exampleLine.Flip();
Line lineFlippedBack = lineFlip.Flip();
Line lineFlip = _exampleLine.Reverse();
Line lineFlippedBack = lineFlip.Reverse();

// Assert
lineFlip.Equals(lineFlippedBack).Should().BeFalse();
Expand All @@ -286,18 +286,15 @@ public void It_Translates_A_Line()
}

[Fact]
public void It_Returns_A_NurbsBase_Form_Of_A_Line()
public void It_Returns_True_If_The_NurbsBase_Form_Of_A_Line_Is_Correct()
{
// Arrange
var line = _exampleLine;

//Act
var nurbsLine = line.ToNurbs();
NurbsBase nurbsLine = _exampleLine;

// Assert
nurbsLine.ControlPointLocations.Count.Should().Be(2);
nurbsLine.ControlPointLocations[0].Equals(line.StartPoint).Should().BeTrue();
nurbsLine.ControlPointLocations[1].Equals(line.EndPoint).Should().BeTrue();
nurbsLine.ControlPointLocations[0].Equals(_exampleLine.StartPoint).Should().BeTrue();
nurbsLine.ControlPointLocations[1].Equals(_exampleLine.EndPoint).Should().BeTrue();
nurbsLine.Degree.Should().Be(1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/GShark.Test.XUnit/Geometry/NurbsCurveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void Returns_A_Curve_Joining_Different_Types_Of_Curves()
NurbsCurve curve = new NurbsCurve(pts, degree);
Line ln = new Line(new Point3(5, 5, 0), new Point3(5, 5, -2.5));
Arc arc = Arc.ByStartEndDirection(new Point3(5, 5, -2.5), new Point3(10, 5, -5), new Vector3(0, 0, -1));
NurbsBase[] curves = { ln.ToNurbs(), arc.ToNurbs(), curve };
NurbsBase[] curves = { ln, arc.ToNurbs(), curve };

Point3 expectedPt1 = new Point3(5, 3.042501, 4.519036);
Point3 expectedPt2 = new Point3(5, 5, -1.230175);
Expand Down Expand Up @@ -377,7 +377,7 @@ public void Returns_A_Curve_Joining_Polylines_And_Lines()
double expectedLength = 43.932474;

// Act
NurbsBase joinedCurve = NurbsBase.Join(new List<NurbsBase> { poly.ToNurbs(), ln0.ToNurbs(), ln1.ToNurbs() });
NurbsBase joinedCurve = NurbsBase.Join(new List<NurbsBase> { poly.ToNurbs(), ln0, ln1 });
Point3 pt1 = joinedCurve.PointAtLength(15);
Point3 pt2 = joinedCurve.PointAtLength(21.5);
Point3 pt3 = joinedCurve.PointAtLength(27.5);
Expand Down
2 changes: 1 addition & 1 deletion src/GShark.Test.XUnit/Sampling/CurveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public void Return_Adaptive_Sample_Subdivision_Of_A_Line()


// Act
var (tValues, pts) = Curve.AdaptiveSample(ln.ToNurbs());
var (tValues, pts) = Curve.AdaptiveSample(ln);

// Arrange
pts.Count.Should().Be(tValues.Count).And.Be(2);
Expand Down
48 changes: 27 additions & 21 deletions src/GShark/Geometry/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace GShark.Geometry
/// <example>
/// [!code-csharp[Example](../../src/GShark.Test.XUnit/Geometry/LineTests.cs?name=example)]
/// </example>
public class Line : ICurve, IEquatable<Line>, ITransformable<Line>
public class Line : NurbsBase, IEquatable<Line>, ITransformable<Line>
{
/// <summary>
/// Initializes a line by start point and end point.
Expand All @@ -29,6 +29,7 @@ public Line(Point3 startPoint, Point3 endPoint)
EndPoint = endPoint;
Length = StartPoint.DistanceTo(EndPoint);
Direction = (EndPoint - StartPoint).Unitize();
ToNurbs();
}

/// <summary>
Expand All @@ -48,27 +49,28 @@ public Line(Point3 startPoint, Vector3 direction, double length)
EndPoint = startPoint + direction.Amplify(length);
Length = Math.Abs(length);
Direction = (EndPoint - StartPoint).Unitize();
ToNurbs();
}

/// <summary>
/// Gets the start point of the line.
/// </summary>
public Point3 StartPoint { get; }
public override Point3 StartPoint { get; }

/// <summary>
/// Gets the middle point of the line.
/// </summary>
public Point3 MidPoint => StartPoint + (EndPoint - StartPoint) / 2;
public override Point3 MidPoint => StartPoint + (EndPoint - StartPoint) / 2;

/// <summary>
/// Gets the end point of the line.
/// </summary>
public Point3 EndPoint { get; }
public override Point3 EndPoint { get; }

/// <summary>
/// Length of the line.
/// </summary>
public double Length { get; }
public override double Length { get; }

/// <summary>
/// Unit vector representing direction of the line.
Expand All @@ -78,7 +80,7 @@ public Line(Point3 startPoint, Vector3 direction, double length)
/// <summary>
/// Gets the bounding box in ascending fashion.
/// </summary>
public BoundingBox GetBoundingBox()
public override BoundingBox GetBoundingBox()
{
BoundingBox bBox = new BoundingBox(StartPoint, EndPoint);
BoundingBox validBBox = bBox.MakeItValid();
Expand All @@ -88,12 +90,12 @@ public BoundingBox GetBoundingBox()
/// <summary>
/// Gets the closest point on the line to the test point.
/// </summary>
/// <param name="pt">The closest point to find.</param>
/// <param name="point">The closest point to find.</param>
/// <returns>The point on the line closest to the test point.</returns>
public Point3 ClosestPoint(Point3 pt)
public override Point3 ClosestPoint(Point3 point)
{
Vector3 dir = Direction;
Vector3 v = pt - StartPoint;
Vector3 v = point - StartPoint;
double d = Vector3.DotProduct(v, dir);

d = Math.Min(Length, d);
Expand All @@ -105,16 +107,16 @@ public Point3 ClosestPoint(Point3 pt)
/// <summary>
/// Computes the parameter on the line that is closest to a test point.
/// </summary>
/// <param name="pt">The test point.</param>
/// <param name="point">The test point.</param>
/// <returns>The parameter on the line closest to the test point.</returns>
public double ClosestParameter(Point3 pt)
public override double ClosestParameter(Point3 point)
{
Vector3 dir = EndPoint - StartPoint;
double dirLength = dir.SquareLength;

if (!(dirLength > 0.0)) return 0.0;
Vector3 ptToStart = pt - StartPoint;
Vector3 ptToEnd = pt - EndPoint;
Vector3 ptToStart = point - StartPoint;
Vector3 ptToEnd = point - EndPoint;

if (ptToStart.SquareLength <= ptToEnd.SquareLength)
{
Expand All @@ -129,7 +131,7 @@ public double ClosestParameter(Point3 pt)
/// </summary>
/// <param name="t">Parameter to evaluate the line. Parameter should be between 0.0 and 1.0.</param>
/// <returns>The point at the specific parameter.</returns>
public Point3 PointAt(double t)
public override Point3 PointAt(double t)
{
if (t <= 0.0)
{
Expand All @@ -149,7 +151,7 @@ public Point3 PointAt(double t)
/// </summary>
/// <param name="length">Length, between 0.0 and the length of the curve.</param>
/// <returns>The point at the given length.</returns>
public Point3 PointAtLength(double length)
public override Point3 PointAtLength(double length)
{
if (length <= 0)
{
Expand All @@ -169,7 +171,7 @@ public Point3 PointAtLength(double length)
/// </summary>
/// <param name="t">Parameter, between 0.0 and 1.0.</param>
/// <returns>The curve length at parameter.</returns>
public double LengthAt(double t)
public override double LengthAt(double t)
{
if (t <= 0)
{
Expand All @@ -188,7 +190,7 @@ public double LengthAt(double t)
/// Flip the endpoint of the line.
/// </summary>
/// <returns>The line flipped.</returns>
public Line Flip()
public new Line Reverse()
{
return new Line(EndPoint, StartPoint);
}
Expand All @@ -199,7 +201,7 @@ public Line Flip()
/// <param name="distance">The distance of the offset. If negative the offset will be in the opposite side.</param>
/// <param name="pln">The plane for the offset operation.</param>
/// <returns>The offset line.</returns>
public Line Offset(double distance, Plane pln)
public new Line Offset(double distance, Plane pln)
{
if (distance == 0.0)
{
Expand Down Expand Up @@ -238,17 +240,21 @@ public Line Extend(double startLength, double endLength)
/// Gets the NURBS form of the curve.
/// </summary>
/// <returns>A NURBS curve.</returns>
public NurbsBase ToNurbs()
private void ToNurbs()
{
return new NurbsCurve(new List<Point3> { StartPoint, EndPoint }, 1);
Weights = new List<double> {1.0, 1.0};
Degree = 1;
Knots = new KnotVector{0.0, 0.0, 1.0, 1.0};
ControlPointLocations = new List<Point3> { StartPoint, EndPoint };
ControlPoints = new List<Point4> { StartPoint, EndPoint };
}

/// <summary>
/// Transforms the line using the transformation matrix.
/// </summary>
/// <param name="transform">Transformation matrix to apply.</param>
/// <returns>A line transformed.</returns>
public Line Transform(Transform transform)
public new Line Transform(Transform transform)
{
Point3 pt1 = StartPoint.Transform(transform);
Point3 pt2 = EndPoint.Transform(transform);
Expand Down
37 changes: 24 additions & 13 deletions src/GShark/Geometry/NurbsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ protected NurbsBase(int degree, KnotVector knots, List<Point4> controlPoints)
/// </summary>
public KnotVector Knots { get; protected set; }

public double Length => Analyze.Curve.Length(this);
public virtual double Length => Analyze.Curve.Length(this);

public Point3 StartPoint => PointAt(0.0);
public virtual Point3 StartPoint => PointAt(0.0);

public Point3 MidPoint => PointAt(0.5);
public virtual Point3 MidPoint => PointAt(0.5);

public Point3 EndPoint => PointAt(1.0);
public virtual Point3 EndPoint => PointAt(1.0);

public BoundingBox GetBoundingBox()
public virtual BoundingBox GetBoundingBox()
{
NurbsBase curve = this;

Expand Down Expand Up @@ -167,7 +167,7 @@ public NurbsBase Close()
/// <summary>
/// <inheritdoc cref="ICurve.PointAt"/>
/// </summary>
public Point3 PointAt(double t)
public virtual Point3 PointAt(double t)
{
if (t <= 0.0)
{
Expand All @@ -184,7 +184,7 @@ public Point3 PointAt(double t)
/// <summary>
/// <inheritdoc cref="ICurve.PointAtLength"/>
/// </summary>
public Point3 PointAtLength(double length)
public virtual Point3 PointAtLength(double length)
{
double parameter = Analyze.Curve.ParameterAtLength(this, length);
return Evaluate.Curve.PointAt(this, parameter);
Expand All @@ -195,7 +195,7 @@ public Point3 PointAtLength(double length)
/// </summary>
/// <param name="normalizedLength">The length factor is normalized between 0.0 and 1.0.</param>
/// <returns>The point at the length.</returns>
public Point3 PointAtNormalizedLength(double normalizedLength)
public virtual Point3 PointAtNormalizedLength(double normalizedLength)
{
double length = GSharkMath.RemapValue(normalizedLength, new Interval(0.0, 1.0), new Interval(0.0, Length));
return PointAtLength(length);
Expand Down Expand Up @@ -307,7 +307,7 @@ public Plane PerpendicularFrameAt(double t)
/// Reverses the parametrization of the curve.
/// </summary>
/// <returns>A reversed curve.</returns>
public NurbsBase Reverse()
public virtual NurbsBase Reverse()
{
List<Point4> controlPts = new List<Point4>(ControlPoints);
controlPts.Reverse();
Expand All @@ -320,7 +320,7 @@ public NurbsBase Reverse()
/// <summary>
/// <inheritdoc cref="ICurve.ClosestPoint"/>
/// </summary>
public Point3 ClosestPoint(Point3 point)
public virtual Point3 ClosestPoint(Point3 point)
{
double t = Analyze.Curve.ClosestParameter(this, point);
Point3 pointAt = Evaluate.Curve.PointAt(this, t);
Expand All @@ -330,7 +330,7 @@ public Point3 ClosestPoint(Point3 point)
/// <summary>
/// <inheritdoc cref="ICurve.ClosestParameter"/>
/// </summary>
public double ClosestParameter(Point3 pt)
public virtual double ClosestParameter(Point3 pt)
{
return Analyze.Curve.ClosestParameter(this, pt);
}
Expand Down Expand Up @@ -358,7 +358,7 @@ public double ParameterAtLength(double segmentLength)
/// <summary>
/// <inheritdoc cref="ICurve.LengthAt"/>
/// </summary>
public double LengthAt(double t)
public virtual double LengthAt(double t)
{
if (t <= 0.0)
{
Expand Down Expand Up @@ -403,7 +403,7 @@ public NurbsBase ClampEnds()
/// <param name="distance">The distance of the offset. If negative the offset will be in the opposite side.</param>
/// <param name="pln">The plane for the offset operation.</param>
/// <returns>The offset curve.</returns>
public NurbsBase Offset(double distance, Plane pln)
public virtual NurbsBase Offset(double distance, Plane pln)
{
if (distance == 0.0)
{
Expand Down Expand Up @@ -493,6 +493,17 @@ public NurbsBase ReduceDegree(double tolerance = 10e-4)
return Modify.Curve.ReduceDegree(this, tolerance);
}

/// <summary>
/// Transforms a curve with the given transformation matrix.
/// </summary>
/// <param name="transformation">The transformation matrix.</param>
/// <returns>A new NURBS curve transformed.</returns>
public virtual NurbsBase Transform(Transform transformation)
{
List<Point4> pts = ControlPoints.Select(pt => pt.Transform(transformation)).ToList();
return new NurbsCurve(Degree, Knots, pts);
}

/// <summary>
/// Divides a curve for a given number of time, including the end points.<br/>
/// The result is not split curves but a collection of t values and lengths that can be used for splitting.<br/>
Expand Down
13 changes: 1 addition & 12 deletions src/GShark/Geometry/NurbsCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace GShark.Geometry
/// <example>
/// [!code-csharp[Example](../../src/GShark.Test.XUnit/Data/NurbsBaseCollection.cs?name=example)]
/// </example>
public class NurbsCurve : NurbsBase, ITransformable<NurbsCurve>
public class NurbsCurve : NurbsBase
{
/// <summary>
/// Internal constructor, creates a NURBS curve.
Expand Down Expand Up @@ -42,16 +42,5 @@ public NurbsCurve(List<Point3>? points, List<double> weights, int degree)
: this(degree, new KnotVector(degree, points!.Count), points.Select((p, i) => new Point4(p, weights[i])).ToList())
{
}

/// <summary>
/// Transforms a curve with the given transformation matrix.
/// </summary>
/// <param name="transformation">The transformation matrix.</param>
/// <returns>A new NURBS curve transformed.</returns>
public NurbsCurve Transform(Transform transformation)
{
List<Point4> pts = ControlPoints.Select(pt => pt.Transform(transformation)).ToList();
return new NurbsCurve(Degree, Knots, pts);
}
}
}
2 changes: 1 addition & 1 deletion src/GShark/Geometry/PolyCurve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Append(Line line)
{
HealthChecks(line);
_segments.Add(line);
_segmentsNurbs.Add(line.ToNurbs());
_segmentsNurbs.Add(line);
ToNurbsForm();
}

Expand Down
2 changes: 1 addition & 1 deletion src/GShark/Intersection/Intersect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public static bool PlaneCircle(Plane pl, Circle cl, out Point3[] pts)
/// <returns>A collection of <see cref="CurvesIntersectionResult"/>.</returns>
public static List<CurvesIntersectionResult> CurveLine(NurbsBase crv, Line ln)
{
return CurveCurve(crv, ln.ToNurbs());
return CurveCurve(crv, ln);
}

/// <summary>
Expand Down

0 comments on commit 5f74da4

Please sign in to comment.