|
| 1 | +using System; |
| 2 | +using FluentAssertions; |
| 3 | +using GShark.Core; |
| 4 | +using GShark.Geometry; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Linq; |
| 8 | +using System.Transactions; |
| 9 | +using GShark.Enumerations; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +namespace GShark.Test.XUnit.Core |
| 14 | +{ |
| 15 | + public class TransformMatrixTests |
| 16 | + { |
| 17 | + private readonly ITestOutputHelper _testOutput; |
| 18 | + public TransformMatrixTests(ITestOutputHelper testOutput) |
| 19 | + { |
| 20 | + _testOutput = testOutput; |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void It_Combines_Transformation_Matrices() |
| 25 | + { |
| 26 | + //Arrange |
| 27 | + var t1 = TransformMatrix.Translation(new Vector3(5, 5, 2)); |
| 28 | + var t2 = TransformMatrix.Rotation(Vector3.ZAxis, GSharkMath.ToRadians(30)); |
| 29 | + var t3 = TransformMatrix.Scale(2, 2, 2); |
| 30 | + var expectedMatrix1 = new TransformMatrix() //Compound(t1, t2, t3) checked gh. |
| 31 | + { |
| 32 | + M00 = 1.7320508075688774, |
| 33 | + M01 = 0.9999999999999999, |
| 34 | + M10 = -0.9999999999999999, |
| 35 | + M11 = 1.7320508075688774, |
| 36 | + M22 = 2, |
| 37 | + M30 = 3.660254037844388, |
| 38 | + M31 = 13.660254037844386, |
| 39 | + M32 = 4 |
| 40 | + }; |
| 41 | + |
| 42 | + var expectedMatrix2 = new TransformMatrix() //Compound(t2,t1,t3) checked in gh. |
| 43 | + { |
| 44 | + M00 = 1.7320508075688774, |
| 45 | + M01 = 0.9999999999999999, |
| 46 | + M10 = -0.9999999999999999, |
| 47 | + M11 = 1.7320508075688774, |
| 48 | + M22 = 2, |
| 49 | + M30 = 10, |
| 50 | + M31 = 10, |
| 51 | + M32 = 4 |
| 52 | + }; |
| 53 | + |
| 54 | + //Act |
| 55 | + //order matters, matrices non commutative. |
| 56 | + var result1 = t1.Combine(t2).Combine(t3); |
| 57 | + var result2 = t2.Combine(t1).Combine(t3); |
| 58 | + |
| 59 | + //Assert |
| 60 | +#if DEBUG |
| 61 | + _testOutput.WriteLine(result1.ToString()); |
| 62 | + _testOutput.WriteLine(result2.ToString()); |
| 63 | +#endif |
| 64 | + result1.Equals(expectedMatrix1).Should().BeTrue(); |
| 65 | + result2.Equals(expectedMatrix2).Should().BeTrue(); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void It_Returns_The_Identity_TransformMatrix() |
| 70 | + { |
| 71 | + // Act |
| 72 | + var identityMatrix = new TransformMatrix( |
| 73 | + new List<double> |
| 74 | + { |
| 75 | + 1,0,0, |
| 76 | + 0,1,0, |
| 77 | + 0,0,1, |
| 78 | + 0,0,0 |
| 79 | + }); |
| 80 | + |
| 81 | + //Act |
| 82 | + var transformMatrix = new TransformMatrix(); |
| 83 | + |
| 84 | + // Assert |
| 85 | +#if DEBUG |
| 86 | + _testOutput.WriteLine(transformMatrix.ToString()); |
| 87 | + _testOutput.WriteLine(identityMatrix.ToString()); |
| 88 | +#endif |
| 89 | + transformMatrix.Equals(identityMatrix).Should().BeTrue(); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void It_Creates_A_TransformationMatrix_By_Copying_Another_TransformationMatrix() |
| 94 | + { |
| 95 | + // Arrange |
| 96 | + var translationMatrix = TransformMatrix.Translation(new Vector3(15, 15, 0)); |
| 97 | + |
| 98 | + |
| 99 | + // Act |
| 100 | + var copyMatrix = new TransformMatrix(translationMatrix); |
| 101 | + |
| 102 | + // Assert |
| 103 | + copyMatrix.Equals(translationMatrix).Should().BeTrue(); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void It_Returns_A_Identity_Transform_Matrix() |
| 108 | + { |
| 109 | + // Act |
| 110 | + var transform = new TransformMatrix(); |
| 111 | + |
| 112 | + // Assert |
| 113 | + var matrix = transform.Matrix; |
| 114 | + |
| 115 | + matrix.Count.Should().Be(4); |
| 116 | + matrix[0].Count.Should().Be(4); |
| 117 | + matrix[0][0].Should().Be(1); |
| 118 | + matrix[1][1].Should().Be(1); |
| 119 | + matrix[2][2].Should().Be(1); |
| 120 | + matrix[3][3].Should().Be(1); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void It_Returns_A_Translation_Matrix() |
| 125 | + { |
| 126 | + // Arrange |
| 127 | + var translation = new Vector3(10, 10, 0); |
| 128 | + |
| 129 | + // Act |
| 130 | + var transform = Transform.Translation(translation); |
| 131 | + |
| 132 | + // Assert |
| 133 | + transform.M30.Should().Be(10); |
| 134 | + transform.M31.Should().Be(10); |
| 135 | + transform.M32.Should().Be(0); |
| 136 | + transform.M33.Should().Be(1); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public void It_Returns_A_Rotation_Matrix() |
| 141 | + { |
| 142 | + // Arrange |
| 143 | + var center = new Point3(5, 5, 0); |
| 144 | + double angleInRadians = GSharkMath.ToRadians(30); |
| 145 | + |
| 146 | + // Act |
| 147 | + var transform = Transform.Rotation(angleInRadians, center, RotationAxis.Z); |
| 148 | + |
| 149 | + // Assert |
| 150 | +#if DEBUG |
| 151 | + _testOutput.WriteLine(transform.ToString()); |
| 152 | +#endif |
| 153 | + transform.M00.Equals(0.8660254037844387).Should().BeTrue(); |
| 154 | + transform.M10.Equals(-0.49999999999999994).Should().BeTrue(); |
| 155 | + transform.M20.Equals(0).Should().BeTrue(); |
| 156 | + transform.M30.Equals(3.169872981077806).Should().BeTrue(); |
| 157 | + transform.M01.Equals(0.49999999999999994).Should().BeTrue(); |
| 158 | + transform.M11.Equals(0.8660254037844387).Should().BeTrue(); |
| 159 | + transform.M21.Equals(0).Should().BeTrue(); |
| 160 | + transform.M31.Equals(-1.8301270189221928).Should().BeTrue(); |
| 161 | + transform.M02.Equals(0).Should().BeTrue(); |
| 162 | + transform.M12.Equals(0).Should().BeTrue(); |
| 163 | + transform.M22.Equals(1).Should().BeTrue(); |
| 164 | + transform.M32.Equals(0).Should().BeTrue(); |
| 165 | + transform.M03.Equals(0).Should().BeTrue(); |
| 166 | + transform.M13.Equals(0).Should().BeTrue(); |
| 167 | + transform.M23.Equals(0).Should().BeTrue(); |
| 168 | + transform.M33.Equals(1).Should().BeTrue(); |
| 169 | + } |
| 170 | + |
| 171 | + [Fact] |
| 172 | + public void It_Returns_A_Scale_Matrix() |
| 173 | + { |
| 174 | + // Act |
| 175 | + var scale1 = Transform.Scale(0.5); |
| 176 | + var scale2 = Transform.Scale(new Point3(10, 10, 0), 0.5); |
| 177 | + |
| 178 | + // Assert |
| 179 | +#if DEBUG |
| 180 | + _testOutput.WriteLine(scale2.ToString()); |
| 181 | +#endif |
| 182 | + scale1.M00.Should().Be(0.5); scale2.M00.Should().Be(0.5); |
| 183 | + scale1.M11.Should().Be(0.5); scale2.M11.Should().Be(0.5); |
| 184 | + scale1.M22.Should().Be(0.5); scale2.M22.Should().Be(0.5); |
| 185 | + scale1.M33.Should().Be(1.0); scale2.M33.Should().Be(1.0); |
| 186 | + |
| 187 | + scale2.M30.Should().Be(5.0); |
| 188 | + scale2.M31.Should().Be(5.0); |
| 189 | + } |
| 190 | + |
| 191 | + [Fact] |
| 192 | + public void It_Returns_A_Reflection_Matrix() |
| 193 | + { |
| 194 | + // Arrange |
| 195 | + var pt = new Point3(10, 10, 0); |
| 196 | + Plane plane = new Plane(pt, Vector3.XAxis); |
| 197 | + |
| 198 | + // Act |
| 199 | + var transform = TransformMatrix.Reflection(plane); |
| 200 | + |
| 201 | + // Assert |
| 202 | + transform.M00.Should().Be(-1.0); |
| 203 | + transform.M11.Should().Be(1.0); |
| 204 | + transform.M22.Should().Be(1.0); |
| 205 | + transform.M33.Should().Be(1.0); |
| 206 | + transform.M30.Should().Be(20); |
| 207 | + } |
| 208 | + |
| 209 | + [Fact] |
| 210 | + public void It_Returns_A_Projection_Matrix() |
| 211 | + { |
| 212 | + // Arrange |
| 213 | + var origin = new Point3(5, 0, 0); |
| 214 | + var dir = new Point3(-10, -15, 0); |
| 215 | + Plane plane = new Plane(origin, dir); |
| 216 | + |
| 217 | + // Act |
| 218 | + var transform = TransformMatrix.Projection(plane); |
| 219 | + |
| 220 | + // Assert |
| 221 | +#if DEBUG |
| 222 | + _testOutput.WriteLine(transform.ToString()); |
| 223 | +#endif |
| 224 | + transform.M00.Should().BeApproximately(0.692308, GSharkMath.MaxTolerance); |
| 225 | + transform.M10.Should().BeApproximately(-0.461538, GSharkMath.MaxTolerance); |
| 226 | + transform.M30.Should().BeApproximately(1.538462, GSharkMath.MaxTolerance); |
| 227 | + transform.M01.Should().BeApproximately(-0.461538, GSharkMath.MaxTolerance); |
| 228 | + transform.M11.Should().BeApproximately(0.307692, GSharkMath.MaxTolerance); |
| 229 | + transform.M31.Should().BeApproximately(2.307692, GSharkMath.MaxTolerance); |
| 230 | + transform.M33.Should().BeApproximately(1.0, GSharkMath.MaxTolerance); |
| 231 | + } |
| 232 | + |
| 233 | + [Fact] |
| 234 | + public void It_Multiplies_A_Point3() |
| 235 | + { |
| 236 | + //Arrange |
| 237 | + var p = new Point3(5, -2, 12.5); |
| 238 | + var expectedPt = new Vector3(8,0,13); |
| 239 | + var m = TransformMatrix.Translation(new Vector3(3, 2, 0.5)); |
| 240 | + |
| 241 | + //Act |
| 242 | + var result = p * m; |
| 243 | + |
| 244 | + //Assert |
| 245 | +#if DEBUG |
| 246 | + _testOutput.WriteLine(result.ToString()); |
| 247 | +#endif |
| 248 | + result.Equals(expectedPt).Should().BeTrue(); |
| 249 | + } |
| 250 | + |
| 251 | + |
| 252 | + } |
| 253 | +} |
0 commit comments