Skip to content

Commit

Permalink
Merge pull request #381 from GSharker/dev/guma/surface-is-planar
Browse files Browse the repository at this point in the history
  • Loading branch information
sonomirco authored Nov 15, 2021
2 parents 0ba3f1d + 31895ea commit 4073ba8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/GShark.Test.XUnit/Geometry/NurbsSurfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,33 @@ public void Returns_True_If_Surface_Is_Close()
surface.IsClosed(SurfaceDirection.V).Should().BeTrue();
}

[Fact]
public void Returns_True_If_Surface_Is_Planar()
{
//Arrange
var planarSurface = NurbsSurface.FromCorners(
new Point3(0, 0, 0),
new Point3(10, 0, 0),
new Point3(10,5,0),
new Point3(0,5,0)
);

var nonPlanarSurface = NurbsSurface.FromCorners(
new Point3(0, 0, 0),
new Point3(10, 0, 5),
new Point3(10, 5, 0),
new Point3(0, 5, 5)
);

//Act
var isPlanarSurfacePlanar = planarSurface.IsPlanar();
var isNonPlanarSurfacePlanar = nonPlanarSurface.IsPlanar();

//Assert
isPlanarSurfacePlanar.Should().BeTrue();
isNonPlanarSurfacePlanar.Should().BeFalse();
}

[Theory]
[InlineData(0.1, 0.1, new double[] { 0.2655, 1, 2.442 })]
[InlineData(0.5, 0.5, new double[] { 4.0625, 5, 4.0625 })]
Expand Down
11 changes: 6 additions & 5 deletions src/GShark/Core/Trigonometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ namespace GShark.Core
public class Trigonometry
{
/// <summary>
/// Determines if the provide points are on the same plane.
/// Determines if the provide points are on the same plane within specified tolerance.
/// </summary>
/// <param name="points">Provided points.</param>
/// <returns>Whether the point are coplanar.</returns>
public static bool ArePointsCoplanar(IList<Point3> points)
/// <param name="points">Points to check.</param>
/// <param name="tolerance">Tolerance.</param>
/// <returns>True if all points are coplanar.</returns>
public static bool ArePointsCoplanar(IList<Point3> points, double tolerance = GSharkMath.Epsilon)
{
// https://en.wikipedia.org/wiki/Triple_product
if (points.Count < 3)
if (points.Count <= 3)
{
return true;
}
Expand Down
11 changes: 11 additions & 0 deletions src/GShark/Geometry/NurbsSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public bool IsClosed(SurfaceDirection direction)
return pts2d.All(pts => pts[0].DistanceTo(pts.Last()) < GSharkMath.Epsilon);
}


/// <summary>
/// Checks is surface is planar within specified tolerance.
/// </summary>
/// <param name="tolerance"></param>
/// <returns></returns>
public bool IsPlanar(double tolerance = GSharkMath.Epsilon)
{
return Trigonometry.ArePointsCoplanar(ControlPointLocations.SelectMany(pt => pt).ToList(), tolerance);
}

/// <summary>
/// Constructs a NURBS surface from four corners.<br/>
/// If the corners are ordered ccw the normal of the surface will point up otherwise, if corners ordered cw the normal will point down.<br/>
Expand Down

0 comments on commit 4073ba8

Please sign in to comment.