forked from go-gl/mathgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquat_test.go
79 lines (62 loc) · 1.64 KB
/
quat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mathgl
import (
"math"
"testing"
)
func TestQuatMulIdentity(t *testing.T) {
i1 := Quatd{1.0, Vec3d{0, 0, 0}}
i2 := QuatIdentd()
i3 := QuatIdentd()
mul := i2.Mul(i3)
if !FloatEqual(mul.W, 1.0) {
t.Errorf("Multiplication of identities does not yield identity")
}
for i := range mul.V {
if mul.V[i] != i1.V[i] {
t.Errorf("Multiplication of identities does not yield identity")
}
}
}
func TestQuatRotateOnAxis(t *testing.T) {
angleDegrees := 30.0
axis := Vec3d{1, 0, 0}
i1 := QuatRotated(angleDegrees, axis)
rotatedAxis := i1.Rotate(axis)
for i := range rotatedAxis {
if rotatedAxis[i] != axis[i] {
t.Errorf("Rotation of axis does not yield identity")
}
}
}
func TestQuatRotateOffAxis(t *testing.T) {
angleDegrees := 30.0
angleRads := angleDegrees * math.Pi / 180.0
axis := Vec3d{1, 0, 0}
i1 := QuatRotated(angleDegrees, axis)
vector := Vec3d{0, 1, 0}
rotatedVector := i1.Rotate(vector)
answer := Vec3d{0, math.Cos(angleRads), math.Sin(angleRads)}
for i := range rotatedVector {
if rotatedVector[i] != answer[i] {
t.Errorf("Rotation of vector does not yield answer")
}
}
}
func TestQuatIdentityToMatrix(t *testing.T) {
quat := QuatIdentd()
matrix := quat.Mat4()
answer := Ident4d()
if !matrix.ApproxEqual(answer) {
t.Errorf("Identity quaternion does not yield identity matrix")
}
}
func TestQuatRotationToMatrix(t *testing.T) {
angle := 45.0
axis := Vec3d{1, 2, 3}.Normalize()
quat := QuatRotated(angle, axis)
matrix := quat.Mat4()
answer := HomogRotate3Dd(angle*math.Pi/180, axis)
if !matrix.ApproxEqual(answer) {
t.Errorf("Rotation quaternion does not yield correct rotation matrix")
}
}