diff --git a/src/GShark.Test.XUnit/Geometry/PlaneTests.cs b/src/GShark.Test.XUnit/Geometry/PlaneTests.cs index ab9d4911..d924afd8 100644 --- a/src/GShark.Test.XUnit/Geometry/PlaneTests.cs +++ b/src/GShark.Test.XUnit/Geometry/PlaneTests.cs @@ -66,7 +66,24 @@ public void It_Trows_An_Exception_If_The_Three_Point_Are_Collinear() func.Should().Throw() .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() { diff --git a/src/GShark/Geometry/Plane.cs b/src/GShark/Geometry/Plane.cs index c76327d3..acb95959 100644 --- a/src/GShark/Geometry/Plane.cs +++ b/src/GShark/Geometry/Plane.cs @@ -59,9 +59,18 @@ public Plane(Point3 pt1, Point3 pt2, Point3 pt3) /// Y direction. 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(); } ///