Skip to content

Commit 4073ba8

Browse files
authored
Merge pull request #381 from GSharker/dev/guma/surface-is-planar
2 parents 0ba3f1d + 31895ea commit 4073ba8

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/GShark.Test.XUnit/Geometry/NurbsSurfaceTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,33 @@ public void Returns_True_If_Surface_Is_Close()
269269
surface.IsClosed(SurfaceDirection.V).Should().BeTrue();
270270
}
271271

272+
[Fact]
273+
public void Returns_True_If_Surface_Is_Planar()
274+
{
275+
//Arrange
276+
var planarSurface = NurbsSurface.FromCorners(
277+
new Point3(0, 0, 0),
278+
new Point3(10, 0, 0),
279+
new Point3(10,5,0),
280+
new Point3(0,5,0)
281+
);
282+
283+
var nonPlanarSurface = NurbsSurface.FromCorners(
284+
new Point3(0, 0, 0),
285+
new Point3(10, 0, 5),
286+
new Point3(10, 5, 0),
287+
new Point3(0, 5, 5)
288+
);
289+
290+
//Act
291+
var isPlanarSurfacePlanar = planarSurface.IsPlanar();
292+
var isNonPlanarSurfacePlanar = nonPlanarSurface.IsPlanar();
293+
294+
//Assert
295+
isPlanarSurfacePlanar.Should().BeTrue();
296+
isNonPlanarSurfacePlanar.Should().BeFalse();
297+
}
298+
272299
[Theory]
273300
[InlineData(0.1, 0.1, new double[] { 0.2655, 1, 2.442 })]
274301
[InlineData(0.5, 0.5, new double[] { 4.0625, 5, 4.0625 })]

src/GShark/Core/Trigonometry.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ namespace GShark.Core
1111
public class Trigonometry
1212
{
1313
/// <summary>
14-
/// Determines if the provide points are on the same plane.
14+
/// Determines if the provide points are on the same plane within specified tolerance.
1515
/// </summary>
16-
/// <param name="points">Provided points.</param>
17-
/// <returns>Whether the point are coplanar.</returns>
18-
public static bool ArePointsCoplanar(IList<Point3> points)
16+
/// <param name="points">Points to check.</param>
17+
/// <param name="tolerance">Tolerance.</param>
18+
/// <returns>True if all points are coplanar.</returns>
19+
public static bool ArePointsCoplanar(IList<Point3> points, double tolerance = GSharkMath.Epsilon)
1920
{
2021
// https://en.wikipedia.org/wiki/Triple_product
21-
if (points.Count < 3)
22+
if (points.Count <= 3)
2223
{
2324
return true;
2425
}

src/GShark/Geometry/NurbsSurface.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ public bool IsClosed(SurfaceDirection direction)
108108
return pts2d.All(pts => pts[0].DistanceTo(pts.Last()) < GSharkMath.Epsilon);
109109
}
110110

111+
112+
/// <summary>
113+
/// Checks is surface is planar within specified tolerance.
114+
/// </summary>
115+
/// <param name="tolerance"></param>
116+
/// <returns></returns>
117+
public bool IsPlanar(double tolerance = GSharkMath.Epsilon)
118+
{
119+
return Trigonometry.ArePointsCoplanar(ControlPointLocations.SelectMany(pt => pt).ToList(), tolerance);
120+
}
121+
111122
/// <summary>
112123
/// Constructs a NURBS surface from four corners.<br/>
113124
/// 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/>

0 commit comments

Comments
 (0)