From 46f71cca7ba265ea465f0c0791af48435671262b Mon Sep 17 00:00:00 2001 From: Chuong Ho Date: Thu, 12 Jan 2023 11:22:26 +0800 Subject: [PATCH 1/4] add project point to plane --- src/GShark.Test.XUnit/Geometry/Point3Tests.cs | 19 +++++++++++++++++++ src/GShark/Geometry/Point3.cs | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/GShark.Test.XUnit/Geometry/Point3Tests.cs b/src/GShark.Test.XUnit/Geometry/Point3Tests.cs index 1c69d92f..53b519a6 100644 --- a/src/GShark.Test.XUnit/Geometry/Point3Tests.cs +++ b/src/GShark.Test.XUnit/Geometry/Point3Tests.cs @@ -57,6 +57,24 @@ public void It_Checks_If_A_Point_Lies_On_A_Plane() // Assert pt.IsOnPlane(plane, 0.001).Should().BeTrue(); } + + [Fact] + public void If_Check_Project_A_Point_On_A_Plane() + { + // Arrange + Point3 point = new Point3(10, 20, -5); + Point3 planeOrigin = new Point3(0, 10, 0); + Vector3 direction = new Vector3(0, 1, 0); + Plane plane = new Plane(planeOrigin, direction); + var expectedPt = new Point3(10, 10, -5); + + // Act + var result = Point3.ProjectTo(point,plane); + + // Assert + result.Should().BeEquivalentTo(expectedPt); + result.Equals(expectedPt).Should().Be(true); + } [Fact] public void It_Returns_The_Linear_Interpolation_Between_Two_Points() @@ -71,6 +89,7 @@ public void It_Returns_The_Linear_Interpolation_Between_Two_Points() var result = Point3.Interpolate(p1, p2, amount); // Assert + result.Equals(expectedPoint).Should().Be(true); } diff --git a/src/GShark/Geometry/Point3.cs b/src/GShark/Geometry/Point3.cs index 9e0ad89a..f9b51d45 100644 --- a/src/GShark/Geometry/Point3.cs +++ b/src/GShark/Geometry/Point3.cs @@ -527,6 +527,20 @@ public double DistanceTo(Point3 other) } return d; } + + /// + /// Project a point onto a plane. + /// + /// point + /// plane to project onto + /// + public static Point3 ProjectTo(Point3 point, Plane plane) + { + Vector3 v = plane.Origin - point; + Vector3 normal = plane.ZAxis; + double d = Vector3.DotProduct(v, normal); + return point + d * normal; + } /// /// Calculates the distance of a point to a line. @@ -665,6 +679,7 @@ public int InPolygon(Polygon polygon) } return inside ? 1 : -1; } + } } From 4bdbee59e3f79c6814478fda7b0706b6eb346192 Mon Sep 17 00:00:00 2001 From: chuongmep Date: Tue, 17 Jan 2023 10:36:10 +0700 Subject: [PATCH 2/4] fix spell --- src/GShark/Geometry/Point3.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GShark/Geometry/Point3.cs b/src/GShark/Geometry/Point3.cs index f9b51d45..660df5d4 100644 --- a/src/GShark/Geometry/Point3.cs +++ b/src/GShark/Geometry/Point3.cs @@ -529,7 +529,7 @@ public double DistanceTo(Point3 other) } /// - /// Project a point onto a plane. + /// Projects a point onto a plane. /// /// point /// plane to project onto From 582fb396a2f986cde3bc6758fb979e5300c450ef Mon Sep 17 00:00:00 2001 From: chuongmep Date: Tue, 17 Jan 2023 10:38:26 +0700 Subject: [PATCH 3/4] pass only a plane as input --- src/GShark.Test.XUnit/Geometry/Point3Tests.cs | 2 +- src/GShark/Geometry/Point3.cs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/GShark.Test.XUnit/Geometry/Point3Tests.cs b/src/GShark.Test.XUnit/Geometry/Point3Tests.cs index 53b519a6..e15293a5 100644 --- a/src/GShark.Test.XUnit/Geometry/Point3Tests.cs +++ b/src/GShark.Test.XUnit/Geometry/Point3Tests.cs @@ -69,7 +69,7 @@ public void If_Check_Project_A_Point_On_A_Plane() var expectedPt = new Point3(10, 10, -5); // Act - var result = Point3.ProjectTo(point,plane); + var result = point.ProjectToPlan(plane); // Assert result.Should().BeEquivalentTo(expectedPt); diff --git a/src/GShark/Geometry/Point3.cs b/src/GShark/Geometry/Point3.cs index 660df5d4..276491dd 100644 --- a/src/GShark/Geometry/Point3.cs +++ b/src/GShark/Geometry/Point3.cs @@ -531,15 +531,14 @@ public double DistanceTo(Point3 other) /// /// Projects a point onto a plane. /// - /// point /// plane to project onto /// - public static Point3 ProjectTo(Point3 point, Plane plane) + public Point3 ProjectToPlan(Plane plane) { - Vector3 v = plane.Origin - point; + Vector3 v = plane.Origin - this; Vector3 normal = plane.ZAxis; double d = Vector3.DotProduct(v, normal); - return point + d * normal; + return this + d * normal; } /// From 314e5eb27316881937f704831e6e6d84b3573ef1 Mon Sep 17 00:00:00 2001 From: Chuong Ho Date: Mon, 6 Feb 2023 08:01:13 +0800 Subject: [PATCH 4/4] update text description --- src/GShark/Geometry/Point3.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/GShark/Geometry/Point3.cs b/src/GShark/Geometry/Point3.cs index 276491dd..3285e65c 100644 --- a/src/GShark/Geometry/Point3.cs +++ b/src/GShark/Geometry/Point3.cs @@ -531,8 +531,8 @@ public double DistanceTo(Point3 other) /// /// Projects a point onto a plane. /// - /// plane to project onto - /// + /// Plane to project onto + /// The point projected to plane public Point3 ProjectToPlan(Plane plane) { Vector3 v = plane.Origin - this;