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

Plane bug #382

Merged
merged 3 commits into from
Mar 8, 2022
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
17 changes: 17 additions & 0 deletions src/GShark.Test.XUnit/Geometry/PlaneTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,24 @@ public void It_Trows_An_Exception_If_The_Three_Point_Are_Collinear()
func.Should().Throw<Exception>()
.WithMessage("Plane cannot be created, the tree points must not be collinear");
}
[Fact]
public void It_Creates_A_Plane_By_Two_Directions_And_Point()
{
// Arrange (Creating a plane with one direction along world X and the other direction along the
// the vector (1, 1)
Vector3 direction1 = Vector3.XAxis * 5;
Vector3 direction2 = Vector3.XAxis + Vector3.YAxis;
Point3 origin = new Point3();

// Act
Plane plane = new Plane(origin, direction1, direction2);

// Assert
plane.Origin.Equals(origin).Should().BeTrue();
plane.XAxis.EpsilonEquals(Vector3.XAxis, GSharkMath.MaxTolerance).Should().BeTrue();
plane.YAxis.EpsilonEquals(Vector3.YAxis, GSharkMath.MaxTolerance).Should().BeTrue();
plane.ZAxis.EpsilonEquals(Vector3.ZAxis, GSharkMath.MaxTolerance).Should().BeTrue();
}
[Fact]
public void It_Creates_A_Plane_By_Three_Points()
{
Expand Down
13 changes: 11 additions & 2 deletions src/GShark/Geometry/Plane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ public Plane(Point3 pt1, Point3 pt2, Point3 pt3)
/// <param name="yDirection">Y direction.</param>
public Plane(Point3 origin, Vector3 xDirection, Vector3 yDirection)
{
if (Math.Abs(Vector3.VectorAngle(xDirection, yDirection)) < GSharkMath.AngleTolerance)
throw new Exception("Plane cannot be created. The direction vectors are parallel");
Origin = origin;
XAxis = xDirection.IsUnitVector ? xDirection : xDirection.Unitize();
YAxis = yDirection.IsUnitVector ? yDirection : yDirection.Unitize();
// Unitizing the directions
Vector3 unitDir1 = xDirection.IsUnitVector ? xDirection : xDirection.Unitize();
Vector3 unitDir2 = yDirection.IsUnitVector ? yDirection : yDirection.Unitize();
// Xaxis
XAxis = unitDir1;
// Zaxis
Vector3 unitZ = Vector3.CrossProduct(unitDir1, unitDir2);
// Yaxis (Since in a right handed coordinate system, Yvec = Zvec X Xvec
YAxis = (Vector3.CrossProduct(unitZ, XAxis)).Unitize();
}

/// <summary>
Expand Down