|
1 | 1 | package MeshTypes_Test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "math" |
| 5 | + "math/rand" |
4 | 6 | "reflect" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | "github.com/Patch2PDF/GDTF-Mesh-Reader/v2/pkg/MeshTypes" |
8 | 10 | ) |
9 | 11 |
|
| 12 | +func MatrixEquals(a MeshTypes.Matrix, b MeshTypes.Matrix) bool { |
| 13 | + // Helper to check individual floats |
| 14 | + isClose := func(a, b float64) bool { |
| 15 | + return math.Abs(a-b) < 0.000000000000001 |
| 16 | + } |
| 17 | + |
| 18 | + return isClose(a.X00, b.X00) && isClose(a.X01, b.X01) && |
| 19 | + isClose(a.X02, b.X02) && isClose(a.X03, b.X03) && |
| 20 | + isClose(a.X10, b.X10) && isClose(a.X11, b.X11) && |
| 21 | + isClose(a.X12, b.X12) && isClose(a.X13, b.X13) && |
| 22 | + isClose(a.X20, b.X20) && isClose(a.X21, b.X21) && |
| 23 | + isClose(a.X22, b.X22) && isClose(a.X23, b.X23) && |
| 24 | + isClose(a.X30, b.X30) && isClose(a.X31, b.X31) && |
| 25 | + isClose(a.X32, b.X32) && isClose(a.X33, b.X33) |
| 26 | +} |
| 27 | + |
10 | 28 | func TestIdentityMatrix(t *testing.T) { |
11 | 29 | want := MeshTypes.Matrix{ |
12 | 30 | X00: 1, X01: 0, X02: 0, X03: 0, |
@@ -84,3 +102,41 @@ func TestMulPosition(t *testing.T) { |
84 | 102 | t.Errorf(`Matrix Vector Multiplication Output does not match`) |
85 | 103 | } |
86 | 104 | } |
| 105 | + |
| 106 | +func TestRotation(t *testing.T) { |
| 107 | + a := MeshTypes.Matrix{ |
| 108 | + X00: rand.Float64(), X01: rand.Float64(), X02: rand.Float64(), X03: rand.Float64(), |
| 109 | + X10: rand.Float64(), X11: rand.Float64(), X12: rand.Float64(), X13: rand.Float64(), |
| 110 | + X20: rand.Float64(), X21: rand.Float64(), X22: rand.Float64(), X23: rand.Float64(), |
| 111 | + X30: 0, X31: 0, X32: 0, X33: 1, |
| 112 | + } |
| 113 | + |
| 114 | + alpha := rand.Float64() |
| 115 | + beta := rand.Float64() |
| 116 | + gamma := rand.Float64() |
| 117 | + |
| 118 | + rotation := MeshTypes.GenerateRotationMatrix(alpha, beta, gamma) |
| 119 | + |
| 120 | + if !reflect.DeepEqual(a.Mul(rotation), a.Rotate(alpha, beta, gamma)) { |
| 121 | + t.Errorf(`Matrix Vector Rotation Output does not match`) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestMatrixRotationReversal(t *testing.T) { |
| 126 | + a := MeshTypes.Matrix{ |
| 127 | + X00: rand.Float64(), X01: rand.Float64(), X02: rand.Float64(), X03: rand.Float64(), |
| 128 | + X10: rand.Float64(), X11: rand.Float64(), X12: rand.Float64(), X13: rand.Float64(), |
| 129 | + X20: rand.Float64(), X21: rand.Float64(), X22: rand.Float64(), X23: rand.Float64(), |
| 130 | + X30: 0, X31: 0, X32: 0, X33: 1, |
| 131 | + } |
| 132 | + |
| 133 | + rotation := MeshTypes.GenerateRotationMatrix(rand.Float64(), rand.Float64(), rand.Float64()) |
| 134 | + |
| 135 | + rotated := a.Mul(rotation) |
| 136 | + |
| 137 | + back_rotated := rotated.ReverseTransformation(rotation) |
| 138 | + |
| 139 | + if !MatrixEquals(a, back_rotated) { |
| 140 | + t.Errorf(`Matrix Vector Rotation Reversal Output does not match`) |
| 141 | + } |
| 142 | +} |
0 commit comments