Skip to content

Commit 0004d5e

Browse files
committed
added tests for rotation generation + reverse generation; renamed newly added function
1 parent d298e65 commit 0004d5e

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

pkg/MeshTypes/matrix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (a Matrix) Rotate(alpha float64, beta float64, gamma float64) Matrix {
8282
return a.Mul(GenerateRotationMatrix(alpha, beta, gamma))
8383
}
8484

85-
func (a Matrix) ReverseRotation(previousRotationMatrix Matrix) Matrix {
85+
func (a Matrix) ReverseTransformation(previousRotationMatrix Matrix) Matrix {
8686
inv := Matrix{
8787
X00: previousRotationMatrix.X00, X01: previousRotationMatrix.X10, X02: previousRotationMatrix.X20, X03: 0,
8888
X10: previousRotationMatrix.X01, X11: previousRotationMatrix.X11, X12: previousRotationMatrix.X21, X13: 0,

tests/MeshTypes/matrix_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
package MeshTypes_Test
22

33
import (
4+
"math"
5+
"math/rand"
46
"reflect"
57
"testing"
68

79
"github.com/Patch2PDF/GDTF-Mesh-Reader/v2/pkg/MeshTypes"
810
)
911

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+
1028
func TestIdentityMatrix(t *testing.T) {
1129
want := MeshTypes.Matrix{
1230
X00: 1, X01: 0, X02: 0, X03: 0,
@@ -84,3 +102,41 @@ func TestMulPosition(t *testing.T) {
84102
t.Errorf(`Matrix Vector Multiplication Output does not match`)
85103
}
86104
}
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

Comments
 (0)