Browse Source

Added function to transform a plane (#642)

Jorrit Rouwe 2 years ago
parent
commit
8f5df4d52c
2 changed files with 22 additions and 1 deletions
  1. 8 1
      Jolt/Geometry/Plane.h
  2. 14 0
      UnitTests/Geometry/PlaneTests.cpp

+ 8 - 1
Jolt/Geometry/Plane.h

@@ -35,6 +35,13 @@ public:
 	/// Offset the plane (positive value means move it in the direction of the plane normal)
 	Plane			Offset(float inDistance) const											{ return Plane(mNormalAndConstant - Vec4(Vec3::sZero(), inDistance)); }
 
+	/// Transform the plane by a matrix
+	inline Plane	GetTransformed(Mat44Arg inTransform) const
+	{
+		Vec3 transformed_normal = inTransform.Multiply3x3(GetNormal());
+		return Plane(transformed_normal, GetConstant() - inTransform.GetTranslation().Dot(transformed_normal));
+	}
+
 	/// Distance point to plane
 	float			SignedDistance(Vec3Arg inPoint) const									{ return inPoint.Dot(GetNormal()) + GetConstant(); }
 
@@ -63,7 +70,7 @@ public:
 		// [aw*(bz*cy-by*cz)+ay*(bw*cz-bz*cw)+az*(by*cw-bw*cy)]
 		// [aw*(bx*cz-bz*cx)+ax*(bz*cw-bw*cz)+az*(bw*cx-bx*cw)]
 		// [aw*(by*cx-bx*cy)+ax*(bw*cy-by*cw)+ay*(bx*cw-bw*cx)]
-		Vec4 numerator = 
+		Vec4 numerator =
 			a.SplatW() * (b.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X, SWIZZLE_UNUSED>() - b.Swizzle<SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_X, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Z, SWIZZLE_X, SWIZZLE_Y, SWIZZLE_UNUSED>())
 			+ a.Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_X, SWIZZLE_UNUSED>() * (b.Swizzle<SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Y, SWIZZLE_UNUSED>() - b.Swizzle<SWIZZLE_Z, SWIZZLE_W, SWIZZLE_Y, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_W, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_UNUSED>())
 			+ a.Swizzle<SWIZZLE_Z, SWIZZLE_Z, SWIZZLE_Y, SWIZZLE_UNUSED>() * (b.Swizzle<SWIZZLE_Y, SWIZZLE_W, SWIZZLE_X, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_W, SWIZZLE_X, SWIZZLE_W, SWIZZLE_UNUSED>() - b.Swizzle<SWIZZLE_W, SWIZZLE_X, SWIZZLE_W, SWIZZLE_UNUSED>() * c.Swizzle<SWIZZLE_Y, SWIZZLE_W, SWIZZLE_X, SWIZZLE_UNUSED>());

+ 14 - 0
UnitTests/Geometry/PlaneTests.cpp

@@ -14,6 +14,20 @@ TEST_SUITE("PlaneTests")
 		CHECK(p.SignedDistance(Vec3(5, -3, 0)) == -5.0f);
 	}
 
+	TEST_CASE("TestPlaneGetTransformed")
+	{
+		Mat44 transform = Mat44::sRotationTranslation(Quat::sRotation(Vec3(1.0f, 2.0f, 3.0f).Normalized(), 0.1f * JPH_PI), Vec3(5.0f, -7.0f, 9.0f));
+
+		Vec3 point = Vec3(11.0f, 13.0f, 15.0f);
+		Vec3 normal = Vec3(-3.0f, 5.0f, -7.0f).Normalized();
+
+		Plane p1 = Plane::sFromPointAndNormal(point, normal).GetTransformed(transform);
+		Plane p2 = Plane::sFromPointAndNormal(transform * point, transform.Multiply3x3(normal));
+
+		CHECK_APPROX_EQUAL(p1.GetNormal(), p2.GetNormal());
+		CHECK_APPROX_EQUAL(p1.GetConstant(), p2.GetConstant());
+	}
+
 	TEST_CASE("TestPlaneIntersectPlanes")
 	{
 		Plane p1 = Plane::sFromPointAndNormal(Vec3(0, 2, 0), Vec3(0, 1, 0));