-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatrix4_test.go
312 lines (271 loc) · 10.9 KB
/
matrix4_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package math
import (
. "launchpad.net/gocheck"
)
type MatrixPerspectiveTestValue struct {
Fov float32
AspectRatio float32
Near float32
Far float32
Expected *Matrix4
}
type MatrixLookAtTestValue struct {
Eye, Center, Up Vector3
Expected *Matrix4
}
type MatrixTranslateTestValue struct {
Translation Vector3
Expected *Matrix4
}
type MatrixRotationTestValue struct {
Axis Vector3
Angle float32
Expected *Matrix4
}
type MatrixOrthoTestValue struct {
Left, Right, Bottom, Top, Near, Far float32
Expected *Matrix4
}
type MatrixMulTestValue struct {
M1 *Matrix4
M2 *Matrix4
Expected *Matrix4
}
type MatrixSetTestValue struct {
M1 *Matrix4
M2 *Matrix4
Expected *Matrix4
}
type MatrixScaleTestValue struct {
Scalar Vector3
Matrix *Matrix4
Expected *Matrix4
}
type MatrixInvertTestValue struct {
Matrix *Matrix4
Expected *Matrix4
}
type MatrixDeterminantTestValue struct {
Matrix *Matrix4
Expected float32
}
type Vector4Matrix4TestValue struct {
Matrix *Matrix4
Value Vector4
Expected Vector4
}
type ProjectMatrix4TestValue struct {
Value Vector3
Model *Matrix4
Proj *Matrix4
Viewport Vector4
Expected Vector3
}
type Matrix4TestSuite struct {
perspectiveTestTable []MatrixPerspectiveTestValue
lookAtTestTable []MatrixLookAtTestValue
translateTestTable []MatrixTranslateTestValue
rotationTestTable []MatrixRotationTestValue
orthoTestTable []MatrixOrthoTestValue
mulTestTable []MatrixMulTestValue
mulVec4TestTable []Vector4Matrix4TestValue
setTestTable []MatrixSetTestValue
scaleTestTable []MatrixScaleTestValue
invertTestTable []MatrixInvertTestValue
determinantTestTable []MatrixDeterminantTestValue
projectTestTable []ProjectMatrix4TestValue
unProjectTestTable []ProjectMatrix4TestValue
}
var matrixTestSuite = Suite(&Matrix4TestSuite{})
func (test *Matrix4TestSuite) SetUpTest(c *C) {
test.perspectiveTestTable = []MatrixPerspectiveTestValue{
MatrixPerspectiveTestValue{45.0, 4.0 / 3.0, 0.1, 100.0, &Matrix4{1.810660, 0.0, 0.0, 0.0, 0.0, 2.4142134, 0.0, 0.0, 0.0, 0.0, -1.002002, -1.0, 0.0, 0.0, -0.2002002, 0.0}},
MatrixPerspectiveTestValue{90.0, 16.0 / 9.0, -1.0, 1.0, &Matrix4{0.562500, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -0.0, -1.0, 0.0, 0.0, 1.0, 0.0}},
}
test.lookAtTestTable = []MatrixLookAtTestValue{
MatrixLookAtTestValue{Vec3(4, 3, 3), Vec3(0, 0, 0), Vec3(0, 1, 0), &Matrix4{0.600000, -0.411597, 0.685994, 0.0, 0.0, 0.857493, 0.514496, 0.0, -0.800000, -0.308697, 0.514496, 0.0, 0.0, 0.0, -5.830953, 1.0}},
}
test.translateTestTable = []MatrixTranslateTestValue{
MatrixTranslateTestValue{Vec3(0, 0, 15), &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 15.0, 1.0}},
MatrixTranslateTestValue{Vec3(-3.0, 2.2, 15), &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.0, 2.200000, 15.0, 1.0}},
}
test.rotationTestTable = []MatrixRotationTestValue{
MatrixRotationTestValue{Vec3(0, 2.5, 0), 25.0, &Matrix4{0.906308, 0.0, -0.422618, 0.0, 0.0, 1.0, 0.0, 0.0, 0.422618, 0.0, 0.906308, 0.0, 0.0, 0.0, 0.0, 1.0}},
MatrixRotationTestValue{Vec3(2.0, 2.5, 0.0), -45.0, &Matrix4{0.821407, 0.142875, 0.552158, 0.0, 0.142875, 0.885700, -0.441726, 0.0, -0.552158, 0.441726, 0.707107, 0.0, 0.0, 0.0, 0.0, 1.0}},
}
test.orthoTestTable = []MatrixOrthoTestValue{
MatrixOrthoTestValue{-10.0, 10.0, -10.0, 10.0, 0.0, 100.0, &Matrix4{.1, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, -0.02, 0.0, -0.0, -0.0, -1.0, 1.0}},
MatrixOrthoTestValue{0.0, 10.0, 0.0, 10.0, 0.0, 100.0, &Matrix4{0.2, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, -0.02, 0.0, -1.0, -1.0, -1.0, 1.0}},
}
test.mulTestTable = []MatrixMulTestValue{
MatrixMulTestValue{
M1: &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0},
M2: &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.0, 2.200000, 15.0, 1.0},
Expected: &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.0, 2.200000, 15.0, 1.0},
},
}
test.mulVec4TestTable = []Vector4Matrix4TestValue{
Vector4Matrix4TestValue{
&Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.0, 2.2, 15.0, 1.0},
Vec4(1.0, 2.0, 3.0, 4.0),
Vec4(-11.0, 10.8, 63.0, 4.0),
},
Vector4Matrix4TestValue{
&Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.3, 2.2, 1.3, 1.0},
Vec4(-1.0, 2.0, 3.0, -4.0),
Vec4(12.2, -6.8, -2.2, -4.0),
},
Vector4Matrix4TestValue{
&Matrix4{0.5625, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -0.0, -1.0, 0.0, 0.0, 1.0, 0.0},
Vec4(1.2, 0.2, -3.3, 4.0),
Vec4(0.675, 0.2, 4.0, 3.3),
},
Vector4Matrix4TestValue{
&Matrix4{0.5625, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -0.0, -1.0, 0.0, 0.0, 1.0, 0.0},
Vec4(-2.2, 0.0, -3.3, 4.0),
Vec4(-1.2375, 0.0, 4.0, 3.3),
},
}
test.setTestTable = []MatrixSetTestValue{
MatrixSetTestValue{
M1: &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0},
M2: &Matrix4{-1.0, 2.2, 0.33, 0.22, 0.2, 1.5, 0.1, 0.3, 4.0, 0.2, 1.1, -2.0, 0.1, 2.2, 32.0, 0.0},
Expected: &Matrix4{-1.0, 2.2, 0.33, 0.22, 0.2, 1.5, 0.1, 0.3, 4.0, 0.2, 1.1, -2.0, 0.1, 2.2, 32.0, 0.0},
},
}
test.scaleTestTable = []MatrixScaleTestValue{
MatrixScaleTestValue{Vec3(2.0, 3.3, -2.2), NewIdentityMatrix4(), &Matrix4{2.0, 0.0, 0.0, 0.0, 0.0, 3.3, 0.0, 0.0, -0.0, -0.0, -2.2, -0.0, 0.0, 0.0, 0.0, 1.0}},
MatrixScaleTestValue{Vec3(2.0, 3.3, -2.2), &Matrix4{0.2, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, -0.02, 0.0, -1.0, -1.0, -1.0, 1.0}, &Matrix4{0.4, 0.0, 0.0, 0.0, 0.0, 0.66, 0.0, 0.0, -0.0, -0.0, 0.044, -0.0, -1.0, -1.0, -1.0, 1.0}},
}
test.invertTestTable = []MatrixInvertTestValue{
MatrixInvertTestValue{NewIdentityMatrix4(), NewIdentityMatrix4()},
MatrixInvertTestValue{&Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.0, 2.2, 15.0, 1.0}, &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 3.0, -2.2, -15.0, 1.0}},
}
test.determinantTestTable = []MatrixDeterminantTestValue{
MatrixDeterminantTestValue{&Matrix4{0.2, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, -0.02, 0.0, -1.0, -1.0, -1.0, 1.0}, -0.0008},
MatrixDeterminantTestValue{NewIdentityMatrix4(), 1.0},
MatrixDeterminantTestValue{&Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.0, 2.2, 15.0, 1.0}, 1.0},
}
test.projectTestTable = []ProjectMatrix4TestValue{
ProjectMatrix4TestValue{
Value: Vec3(-1.0, 2.0, 3.0),
Model: &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, -3.3, 2.2, 1.3, 1.0},
Proj: &Matrix4{0.5625, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -0.0, -1.0, 0.0, 0.0, 1.0, 0.0},
Viewport: Vec4(0.0, 0.0, 1.0, 1.0),
Expected: Vec3(0.78125, 0.011628, 0.383721),
},
}
test.unProjectTestTable = []ProjectMatrix4TestValue{
ProjectMatrix4TestValue{
Value: Vec3(1.0, 2.0, 3.0),
Model: &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 0.965926, 0.258819, 0.0, 0.0, -0.258819, 0.965926, 0.0, 0.0, 0.0, 0.0, 1.0},
Proj: &Matrix4{0.562500, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -0.0, -1.0, 0.0, 0.0, 1.0, 0.0},
Viewport: Vec4(0.0, 0.0, 16.0, 9.0),
Expected: Vec3(-0.311111, -0.159089, -0.164428),
},
ProjectMatrix4TestValue{
Value: Vec3(1.0, 2.0, 3.0),
Model: &Matrix4{1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0},
Proj: &Matrix4{0.5625, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -0.0, -1.0, 0.0, 0.0, 1.0, 0.0},
Viewport: Vec4(0.0, 0.0, 16.0, 9.0),
Expected: Vec3(-0.311111, -0.111111, -0.2),
},
}
}
func (test *Matrix4TestSuite) TestMatrixPerspective(c *C) {
for i := range test.perspectiveTestTable {
value := test.perspectiveTestTable[i]
matrix := NewPerspectiveMatrix4(value.Fov, value.AspectRatio, value.Near, value.Far)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixLookAt(c *C) {
for i := range test.lookAtTestTable {
value := test.lookAtTestTable[i]
matrix := NewLookAtMatrix4(value.Eye, value.Center, value.Up)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixTranslation(c *C) {
for i := range test.translateTestTable {
value := test.translateTestTable[i]
translation := value.Translation
matrix := NewTranslationMatrix4(translation.X, translation.Y, translation.Z)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixRotation(c *C) {
for i := range test.rotationTestTable {
value := test.rotationTestTable[i]
matrix := NewRotationMatrix4(value.Axis, value.Angle)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixOrtho(c *C) {
for i := range test.orthoTestTable {
value := test.orthoTestTable[i]
matrix := NewOrthoMatrix4(value.Left, value.Right, value.Bottom, value.Top, value.Near, value.Far)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixMul(c *C) {
for i := range test.mulTestTable {
value := test.mulTestTable[i]
matrix := value.M1.Mul(value.M2)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixMulVec4(c *C) {
for i := range test.mulVec4TestTable {
value := test.mulVec4TestTable[i]
v := value.Matrix.MulVec4(value.Value)
c.Check(v, Vector4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixSet(c *C) {
for i := range test.setTestTable {
value := test.setTestTable[i]
matrix := value.M1.Set(value.M2)
c.Check(matrix, Matrix4Check, value.Expected)
c.Check(matrix, Matrix4Check, value.M1)
c.Check(matrix, Matrix4Check, value.M2)
c.Check(value.M1, Matrix4Check, value.M2)
}
}
func (test *Matrix4TestSuite) TestMatrixScale(c *C) {
for i := range test.scaleTestTable {
value := test.scaleTestTable[i]
matrix := value.Matrix.Scale(value.Scalar)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixInvert(c *C) {
for i := range test.invertTestTable {
value := test.invertTestTable[i]
matrix, err := value.Matrix.Invert()
c.Check(err, IsNil)
c.Check(matrix, Matrix4Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestMatrixDeterminant(c *C) {
for i := range test.determinantTestTable {
value := test.determinantTestTable[i]
det := value.Matrix.Determinant()
c.Check(det, EqualsFloat32, value.Expected)
}
}
func (test *Matrix4TestSuite) TestProject(c *C) {
for i := range test.projectTestTable {
value := test.projectTestTable[i]
prj := Project(value.Value, value.Model, value.Proj, value.Viewport)
c.Check(prj, Vector3Check, value.Expected)
}
}
func (test *Matrix4TestSuite) TestUnProject(c *C) {
for i := range test.unProjectTestTable {
value := test.unProjectTestTable[i]
unProj, err := UnProject(value.Value, value.Model, value.Proj, value.Viewport)
c.Check(err, IsNil)
c.Check(unProj, Vector3Check, value.Expected)
}
}