From bef67e152b620d9dee4919557f1eb854b442975c Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 26 Nov 2024 05:14:00 +0900 Subject: [PATCH] Fix Matrix4x4.CreateReflection when D is not zero (#110057) * Fix Matrix4x4.CreateReflection * Update tests * Use AssertExtensions instead * Update Matrix4x4Tests.cs * Update Matrix4x4Tests.cs * Use AssertExtensions for new test only --------- Co-authored-by: Tanner Gooding --- .../tests/Matrix4x4Tests.cs | 29 ++++++++++++++++++- .../src/System/Numerics/Matrix4x4.Impl.cs | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs index 80877852d6378..2107bcf184f8d 100644 --- a/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs +++ b/src/libraries/System.Numerics.Vectors/tests/Matrix4x4Tests.cs @@ -819,11 +819,38 @@ public void Matrix4x4CreateReflectionTest01() Vector3 v = point - pp; float d = Vector3.Dot(v, plane.Normal); Vector3 vp = point - 2.0f * d * plane.Normal; - Assert.True(MathHelper.Equal(rp, vp), "Matrix4x4.Reflection did not provide expected value."); + Assert.True(MathHelper.Equal(rp, vp), "Matrix4x4.CreateReflection did not provide expected value."); } } } + [Fact] + public void Matrix4x4CreateReflectionTest02() + { + Plane plane = new Plane(0, 1, 0, 60); + Matrix4x4 actual = Matrix4x4.CreateReflection(plane); + + AssertExtensions.Equal(1.0f, actual.M11, 0.0f); + AssertExtensions.Equal(0.0f, actual.M12, 0.0f); + AssertExtensions.Equal(0.0f, actual.M13, 0.0f); + AssertExtensions.Equal(0.0f, actual.M14, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M21, 0.0f); + AssertExtensions.Equal(-1.0f, actual.M22, 0.0f); + AssertExtensions.Equal(0.0f, actual.M23, 0.0f); + AssertExtensions.Equal(0.0f, actual.M24, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M31, 0.0f); + AssertExtensions.Equal(0.0f, actual.M32, 0.0f); + AssertExtensions.Equal(1.0f, actual.M33, 0.0f); + AssertExtensions.Equal(0.0f, actual.M34, 0.0f); + + AssertExtensions.Equal(0.0f, actual.M41, 0.0f); + AssertExtensions.Equal(-120.0f, actual.M42, 0.0f); + AssertExtensions.Equal(0.0f, actual.M43, 0.0f); + AssertExtensions.Equal(1.0f, actual.M44, 0.0f); + } + // A test for CreateRotationZ (float) [Fact] public void Matrix4x4CreateRotationZTest() diff --git a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs index 3a489f0fc1af0..889311ca07302 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs @@ -620,7 +620,7 @@ public static Impl CreateReflection(in Plane value) // https://github.com/microsoft/DirectXMath/blob/master/Inc/DirectXMathMatrix.inl Vector4 p = Plane.Normalize(value).AsVector4(); - Vector4 s = p * -2.0f; + Vector4 s = p * Vector4.Create(-2.0f, -2.0f, -2.0f, 0.0f); Impl result;