|
2 | 2 | // Distributed under the MIT license. See the LICENSE.md file in the project root for more information. |
3 | 3 | using Xunit; |
4 | 4 |
|
5 | | -namespace Stride.Core.Mathematics.Tests |
| 5 | +namespace Stride.Core.Mathematics.Tests; |
| 6 | + |
| 7 | +public class TestMatrix |
6 | 8 | { |
7 | | - public class TestMatrix |
| 9 | + /* Note: As seen in the TestCompose* tests, we check both expectedQuat == decompedQuat and expectedQuat == -decompedQuat |
| 10 | + * This is because different combinations of yaw/pitch/roll can result in the same *orientation*, which is what we're actually testing. |
| 11 | + * This means that decomposing a rotation matrix or quaternion can actually have multiple answers, but we arbitrarily pick |
| 12 | + * one result, and this may not have actually been the original yaw/pitch/roll the user chose. |
| 13 | + */ |
| 14 | + |
| 15 | + [Theory, ClassData(typeof(TestRotationsData.YRPTestData))] |
| 16 | + public void TestDecomposeYawPitchRollFromQuaternionYPR(float yawDegrees, float pitchDegrees, float rollDegrees) |
| 17 | + { |
| 18 | + var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
| 19 | + var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
| 20 | + var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
| 21 | + |
| 22 | + var rotQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
| 23 | + var rotMatrix = Matrix.RotationQuaternion(rotQuat); |
| 24 | + rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll); |
| 25 | + |
| 26 | + var expectedQuat = rotQuat; |
| 27 | + var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll); |
| 28 | + Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
| 29 | + } |
| 30 | + |
| 31 | + [Theory, ClassData(typeof(TestRotationsData.YRPTestData))] |
| 32 | + public void TestDecomposeYawPitchRollFromMatrixYPR(float yawDegrees, float pitchDegrees, float rollDegrees) |
| 33 | + { |
| 34 | + var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
| 35 | + var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
| 36 | + var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
| 37 | + |
| 38 | + var rotMatrix = Matrix.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
| 39 | + rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll); |
| 40 | + |
| 41 | + var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
| 42 | + var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll); |
| 43 | + Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory, ClassData(typeof(TestRotationsData.YRPTestData))] |
| 47 | + public void TestDecomposeYawPitchRollFromMatricesZXY(float yawDegrees, float pitchDegrees, float rollDegrees) |
| 48 | + { |
| 49 | + var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
| 50 | + var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
| 51 | + var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
| 52 | + |
| 53 | + // Yaw-Pitch-Roll is the intrinsic rotation order, so extrinsic is the reverse (ie. Z-X-Y) |
| 54 | + var rotMatrix = Matrix.RotationZ(rollRadians) * Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians); |
| 55 | + rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll); |
| 56 | + |
| 57 | + var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
| 58 | + var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll); |
| 59 | + Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
| 60 | + } |
| 61 | + |
| 62 | + [Theory, ClassData(typeof(TestRotationsData.XYZTestData))] |
| 63 | + public void TestDecomposeXYZFromMatricesXYZ(float yawDegrees, float pitchDegrees, float rollDegrees) |
| 64 | + { |
| 65 | + var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
| 66 | + var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
| 67 | + var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
| 68 | + |
| 69 | + var rotMatrix = Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians) * Matrix.RotationZ(rollRadians); |
| 70 | + rotMatrix.DecomposeXYZ(out Vector3 eulerAngles); |
| 71 | + |
| 72 | + var decompedRotMatrix = Matrix.RotationX(eulerAngles.X) * Matrix.RotationY(eulerAngles.Y) * Matrix.RotationZ(eulerAngles.Z); |
| 73 | + var decompedQuat = Quaternion.RotationMatrix(decompedRotMatrix); |
| 74 | + |
| 75 | + var expectedQuat = Quaternion.RotationX(pitchRadians) * Quaternion.RotationY(yawRadians) * Quaternion.RotationZ(rollRadians); |
| 76 | + Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void TestNumericConversion() |
| 81 | + { |
| 82 | + System.Numerics.Matrix4x4 matrix = new System.Numerics.Matrix4x4( |
| 83 | + 1, 2, 3, 4, |
| 84 | + 5, 6, 7, 8, |
| 85 | + 9, 10, 11, 12, |
| 86 | + 13, 14, 15, 16); |
| 87 | + |
| 88 | + Matrix baseStrideMatrix = new Matrix( |
| 89 | + 1, 2, 3, 4, |
| 90 | + 5, 6, 7, 8, |
| 91 | + 9, 10, 11, 12, |
| 92 | + 13, 14, 15, 16); |
| 93 | + |
| 94 | + Matrix strideMatrix = matrix; |
| 95 | + Assert.Equal(baseStrideMatrix, strideMatrix); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void TestStrideConversion() |
8 | 100 | { |
9 | | - /* Note: As seen in the TestCompose* tests, we check both expectedQuat == decompedQuat and expectedQuat == -decompedQuat |
10 | | - * This is because different combinations of yaw/pitch/roll can result in the same *orientation*, which is what we're actually testing. |
11 | | - * This means that decomposing a rotation matrix or quaternion can actually have multiple answers, but we arbitrarily pick |
12 | | - * one result, and this may not have actually been the original yaw/pitch/roll the user chose. |
13 | | - */ |
14 | | - |
15 | | - [Theory, ClassData(typeof(TestRotationsData.YRPTestData))] |
16 | | - public void TestDecomposeYawPitchRollFromQuaternionYPR(float yawDegrees, float pitchDegrees, float rollDegrees) |
17 | | - { |
18 | | - var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
19 | | - var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
20 | | - var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
21 | | - |
22 | | - var rotQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
23 | | - var rotMatrix = Matrix.RotationQuaternion(rotQuat); |
24 | | - rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll); |
25 | | - |
26 | | - var expectedQuat = rotQuat; |
27 | | - var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll); |
28 | | - Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
29 | | - } |
30 | | - |
31 | | - [Theory, ClassData(typeof(TestRotationsData.YRPTestData))] |
32 | | - public void TestDecomposeYawPitchRollFromMatrixYPR(float yawDegrees, float pitchDegrees, float rollDegrees) |
33 | | - { |
34 | | - var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
35 | | - var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
36 | | - var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
37 | | - |
38 | | - var rotMatrix = Matrix.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
39 | | - rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll); |
40 | | - |
41 | | - var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
42 | | - var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll); |
43 | | - Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
44 | | - } |
45 | | - |
46 | | - [Theory, ClassData(typeof(TestRotationsData.YRPTestData))] |
47 | | - public void TestDecomposeYawPitchRollFromMatricesZXY(float yawDegrees, float pitchDegrees, float rollDegrees) |
48 | | - { |
49 | | - var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
50 | | - var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
51 | | - var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
52 | | - |
53 | | - // Yaw-Pitch-Roll is the intrinsic rotation order, so extrinsic is the reverse (ie. Z-X-Y) |
54 | | - var rotMatrix = Matrix.RotationZ(rollRadians) * Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians); |
55 | | - rotMatrix.Decompose(out float decomposedYaw, out float decomposedPitch, out float decomposedRoll); |
56 | | - |
57 | | - var expectedQuat = Quaternion.RotationYawPitchRoll(yawRadians, pitchRadians, rollRadians); |
58 | | - var decompedQuat = Quaternion.RotationYawPitchRoll(decomposedYaw, decomposedPitch, decomposedRoll); |
59 | | - Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
60 | | - } |
61 | | - |
62 | | - [Theory, ClassData(typeof(TestRotationsData.XYZTestData))] |
63 | | - public void TestDecomposeXYZFromMatricesXYZ(float yawDegrees, float pitchDegrees, float rollDegrees) |
64 | | - { |
65 | | - var yawRadians = MathUtil.DegreesToRadians(yawDegrees); |
66 | | - var pitchRadians = MathUtil.DegreesToRadians(pitchDegrees); |
67 | | - var rollRadians = MathUtil.DegreesToRadians(rollDegrees); |
68 | | - |
69 | | - var rotMatrix = Matrix.RotationX(pitchRadians) * Matrix.RotationY(yawRadians) * Matrix.RotationZ(rollRadians); |
70 | | - rotMatrix.DecomposeXYZ(out Vector3 eulerAngles); |
71 | | - |
72 | | - var decompedRotMatrix = Matrix.RotationX(eulerAngles.X) * Matrix.RotationY(eulerAngles.Y) * Matrix.RotationZ(eulerAngles.Z); |
73 | | - var decompedQuat = Quaternion.RotationMatrix(decompedRotMatrix); |
74 | | - |
75 | | - var expectedQuat = Quaternion.RotationX(pitchRadians) * Quaternion.RotationY(yawRadians) * Quaternion.RotationZ(rollRadians); |
76 | | - Assert.True(expectedQuat == decompedQuat || expectedQuat == -decompedQuat, $"Quat not equals: Expected: {expectedQuat} - Actual: {decompedQuat}"); |
77 | | - } |
78 | | - |
79 | | - [Fact] |
80 | | - public void TestNumericConversion() |
81 | | - { |
82 | | - System.Numerics.Matrix4x4 matrix = new System.Numerics.Matrix4x4( |
83 | | - 1, 2, 3, 4, |
84 | | - 5, 6, 7, 8, |
85 | | - 9, 10, 11, 12, |
86 | | - 13, 14, 15, 16); |
87 | | - |
88 | | - Matrix baseStrideMatrix = new Matrix( |
89 | | - 1, 2, 3, 4, |
90 | | - 5, 6, 7, 8, |
91 | | - 9, 10, 11, 12, |
92 | | - 13, 14, 15, 16); |
93 | | - |
94 | | - Matrix strideMatrix = matrix; |
95 | | - Assert.Equal(baseStrideMatrix, strideMatrix); |
96 | | - } |
97 | | - |
98 | | - [Fact] |
99 | | - public void TestStrideConversion() |
100 | | - { |
101 | | - Matrix matrix = new( |
102 | | - 1, 2, 3, 4, |
103 | | - 5, 6, 7, 8, |
104 | | - 9, 10, 11, 12, |
105 | | - 13, 14, 15, 16); |
106 | | - |
107 | | - System.Numerics.Matrix4x4 baseNumericseMatrix = new( |
108 | | - 1, 2, 3, 4, |
109 | | - 5, 6, 7, 8, |
110 | | - 9, 10, 11, 12, |
111 | | - 13, 14, 15, 16); |
112 | | - |
113 | | - System.Numerics.Matrix4x4 numericsMatrix = matrix; |
114 | | - Assert.Equal(baseNumericseMatrix, numericsMatrix); |
115 | | - } |
| 101 | + Matrix matrix = new( |
| 102 | + 1, 2, 3, 4, |
| 103 | + 5, 6, 7, 8, |
| 104 | + 9, 10, 11, 12, |
| 105 | + 13, 14, 15, 16); |
| 106 | + |
| 107 | + System.Numerics.Matrix4x4 baseNumericseMatrix = new( |
| 108 | + 1, 2, 3, 4, |
| 109 | + 5, 6, 7, 8, |
| 110 | + 9, 10, 11, 12, |
| 111 | + 13, 14, 15, 16); |
| 112 | + |
| 113 | + System.Numerics.Matrix4x4 numericsMatrix = matrix; |
| 114 | + Assert.Equal(baseNumericseMatrix, numericsMatrix); |
116 | 115 | } |
117 | 116 | } |
0 commit comments