-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtransform_test.go
183 lines (168 loc) · 3.87 KB
/
transform_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// This file is generated from mgl32/transform_test.go; DO NOT EDIT
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mgl64
import (
"math"
"testing"
)
func TestHomogRotate3D(t *testing.T) {
tests := []struct {
Description string
Angle float64
Axis Vec3
Expected Mat4
}{
{
"forward",
0, Vec3{0, 0, 0},
Ident4(),
},
{
"heading 90 degree",
DegToRad(90), Vec3{0, 1, 0},
Mat4{
0, 0, -1, 0,
0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 1,
},
},
{
"heading 180 degree",
DegToRad(180), Vec3{0, 1, 0},
Mat4{
-1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1,
},
},
{
"attitude 90 degree",
DegToRad(90), Vec3{0, 0, 1},
Mat4{
0, 1, 0, 0,
-1, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
},
},
{
"bank 90 degree",
DegToRad(90), Vec3{1, 0, 0},
Mat4{
1, 0, 0, 0,
0, 0, 1, 0,
0, -1, 0, 0,
0, 0, 0, 1,
},
},
{
"heading and attitude 90 degree",
DegToRad(90), Vec3{0, 1, 1}.Normalize(),
Mat4{
0, 0.707107, -0.707107, 0,
-0.707107, 0.5, 0.5, 0,
0.707107, 0.5, 0.5, 0,
0, 0, 0, 1,
},
},
{
"bank, heading and attitude 180 degree",
DegToRad(180), Vec3{1, 1, 1}.Normalize(),
Mat4{
-1 / 3.0, 2 / 3.0, 2 / 3.0, 0,
2 / 3.0, -1 / 3.0, 2 / 3.0, 0,
2 / 3.0, 2 / 3.0, -1 / 3.0, 0,
0, 0, 0, 1,
},
},
}
threshold := float64(math.Pow(10, -2))
for _, c := range tests {
if r := HomogRotate3D(c.Angle, c.Axis); !r.ApproxEqualThreshold(c.Expected, threshold) {
t.Errorf("%v failed: HomogRotate3D(%v, %v) != %v (got %v)", c.Description, c.Angle, c.Axis, c.Expected, r)
}
}
}
func TestExtract3DScale(t *testing.T) {
tests := []struct {
M Mat4
X, Y, Z float64
}{
{
Ident4(),
1, 1, 1,
}, {
Scale3D(1, 2, 3),
1, 2, 3,
}, {
Translate3D(10, 12, -5).Mul4(HomogRotate3D(math.Pi/2, Vec3{1, 0, 0})).Mul4(Scale3D(2, 3, 4)),
2, 3, 4,
},
}
eq := FloatEqualFunc(1e-6)
for _, c := range tests {
if x, y, z := Extract3DScale(c.M); !eq(x, c.X) || !eq(y, c.Y) || !eq(z, c.Z) {
t.Errorf("ExtractScale(%v) != %v, %v, %v (got %v, %v, %v)", c.M, c.X, c.Y, c.Z, x, y, z)
}
}
}
func TestExtractMaxScale(t *testing.T) {
tests := []struct {
M Mat4
V float64
}{
{
Ident4(),
1,
}, {
Scale3D(1, 2, 3),
3,
}, {
Translate3D(10, 12, -5).Mul4(HomogRotate3D(math.Pi/2, Vec3{1, 0, 0})).Mul4(Scale3D(2, 3, 4)),
4,
},
}
eq := FloatEqualFunc(1e-6)
for _, c := range tests {
if r := ExtractMaxScale(c.M); !eq(r, c.V) {
t.Errorf("ExtractMaxScale(%v) != %v (got %v)", c.M, c.V, r)
}
}
}
func TestTransformCoordinate(t *testing.T) {
tests := [...]struct {
v Vec3
m Mat4
out Vec3
}{
{Vec3{1, 1, 1}, Ident4(), Vec3{1, 1, 1}},
{Vec3{1, 1, 1}, Translate3D(0, 1, 1).Mul4(Scale3D(2, 2, 2)), Vec3{2, 3, 3}},
{Vec3{1, 1, -1}, Perspective(DegToRad(90), 4/3, 0, 100), Vec3{1, 1, 1}},
{Vec3{0, 0, -100}, Perspective(DegToRad(45), 4/3, 0, 100), Vec3{0, 0, 1}},
{Vec3{2, 2, -2}, Perspective(DegToRad(45), 4/3, 0, 100), Vec3{2.4142, 2.4142, 1}}, // sqrt(1^2+1^2)+1
}
for _, test := range tests {
if v := TransformCoordinate(test.v, test.m); !test.out.ApproxEqualThreshold(v, 1e-4) {
t.Errorf("TransformCoordinate on vector %v and matrix %v fails to give result %v (got %v)", test.v, test.m, test.out, v)
}
}
}
func TestTransformNormal(t *testing.T) {
tests := [...]struct {
v Vec3
m Mat4
out Vec3
}{
{Vec3{1, 1, 1}, Ident4(), Vec3{1, 1, 1}},
{Vec3{1, 1, 1}, Translate3D(0, 1, 1).Mul4(Scale3D(2, 2, 2)), Vec3{2, 2, 2}},
}
for _, test := range tests {
if v := TransformNormal(test.v, test.m); !test.out.ApproxEqualThreshold(v, 1e-4) {
t.Errorf("TransformNormal on vector %v and matrix %v fails to give result %v (got %v)", test.v, test.m, test.out, v)
}
}
}