-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat4.go
458 lines (388 loc) · 14 KB
/
mat4.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package dprec
import (
"fmt"
"math"
)
func NewMat4(
m11, m12, m13, m14 float64,
m21, m22, m23, m24 float64,
m31, m32, m33, m34 float64,
m41, m42, m43, m44 float64,
) Mat4 {
return Mat4{
M11: m11, M12: m12, M13: m13, M14: m14,
M21: m21, M22: m22, M23: m23, M24: m24,
M31: m31, M32: m32, M33: m33, M34: m34,
M41: m41, M42: m42, M43: m43, M44: m44,
}
}
func ZeroMat4() Mat4 {
return Mat4{}
}
func IdentityMat4() Mat4 {
var result Mat4
result.M11 = 1.0
result.M22 = 1.0
result.M33 = 1.0
result.M44 = 1.0
return result
}
func TransposedMat4(m Mat4) Mat4 {
return NewMat4(
m.M11, m.M21, m.M31, m.M41,
m.M12, m.M22, m.M32, m.M42,
m.M13, m.M23, m.M33, m.M43,
m.M14, m.M24, m.M34, m.M44,
)
}
func TranslationMat4(x, y, z float64) Mat4 {
result := IdentityMat4()
result.M14 = x
result.M24 = y
result.M34 = z
return result
}
func ScaleMat4(x, y, z float64) Mat4 {
var result Mat4
result.M11 = x
result.M22 = y
result.M33 = z
result.M44 = 1.0
return result
}
func RotationMat4(angle Angle, x, y, z float64) Mat4 {
vector := UnitVec3(NewVec3(x, y, z))
return rotationMat4FromNormalizedData(Cos(angle), Sin(angle), vector)
}
func rotationMat4FromNormalizedData(cs, sn float64, vector Vec3) Mat4 {
x, y, z := vector.X, vector.Y, vector.Z
var result Mat4
result.M11 = x*x*(1.0-cs) + cs
result.M21 = x*y*(1.0-cs) + z*sn
result.M31 = x*z*(1.0-cs) - y*sn
result.M12 = y*x*(1.0-cs) - z*sn
result.M22 = y*y*(1.0-cs) + cs
result.M32 = y*z*(1.0-cs) + x*sn
result.M13 = z*x*(1.0-cs) + y*sn
result.M23 = z*y*(1.0-cs) - x*sn
result.M33 = z*z*(1.0-cs) + cs
result.M44 = 1.0
return result
}
func TRSMat4(translation Vec3, rotation Quat, scale Vec3) Mat4 {
orientX := rotation.OrientationX()
orientY := rotation.OrientationY()
orientZ := rotation.OrientationZ()
var result Mat4
result.M11 = orientX.X * scale.X
result.M12 = orientY.X * scale.Y
result.M13 = orientZ.X * scale.Z
result.M14 = translation.X
result.M21 = orientX.Y * scale.X
result.M22 = orientY.Y * scale.Y
result.M23 = orientZ.Y * scale.Z
result.M24 = translation.Y
result.M31 = orientX.Z * scale.X
result.M32 = orientY.Z * scale.Y
result.M33 = orientZ.Z * scale.Z
result.M34 = translation.Z
result.M44 = 1.0
return result
}
func OrthoMat4(left, right, top, bottom, near, far float64) Mat4 {
var result Mat4
result.M11 = 2.0 / (right - left)
result.M14 = (right + left) / (left - right)
result.M22 = 2.0 / (top - bottom)
result.M24 = (top + bottom) / (bottom - top)
result.M33 = 2.0 / (near - far)
result.M34 = (far + near) / (near - far)
result.M44 = 1.0
return result
}
func PerspectiveMat4(left, right, bottom, top, near, far float64) Mat4 {
var result Mat4
result.M11 = 2.0 * near / (right - left)
result.M13 = (right + left) / (right - left)
result.M22 = 2.0 * near / (top - bottom)
result.M23 = (top + bottom) / (top - bottom)
result.M33 = (far + near) / (near - far)
result.M34 = 2.0 * far * near / (near - far)
result.M43 = -1.0
return result
}
// FastInverseMat4 calculates the inverse of the matrix with a few caveats.
//
// The matrix should be a transformation one that was constructed through the multiplication
// of one or more of the following transformations: identity, translation, rotation.
//
// For all other scenarios (e.g. a scale transformation was used), the InverseMat4 method should be
// used instead, though it will be slower.
func FastInverseMat4(m Mat4) Mat4 {
inverseTranslate := TranslationMat4(
-m.M14, -m.M24, -m.M34,
)
inverseRotate := NewMat4(
m.M11, m.M21, m.M31, 0.0,
m.M12, m.M22, m.M32, 0.0,
m.M13, m.M23, m.M33, 0.0,
0.0, 0.0, 0.0, 1.0,
)
return Mat4Prod(inverseRotate, inverseTranslate)
}
// InverseMat4 calculates the inverse of the matrix.
//
// The behavior is undefined if the matrix is not reversible
// (i.e. has a zero determinant).
func InverseMat4(m Mat4) Mat4 {
minor11 := m.M22*m.M33*m.M44 + m.M23*m.M34*m.M42 + m.M24*m.M32*m.M43 - m.M24*m.M33*m.M42 - m.M23*m.M32*m.M44 - m.M22*m.M34*m.M43
minor12 := m.M21*m.M33*m.M44 + m.M23*m.M34*m.M41 + m.M24*m.M31*m.M43 - m.M24*m.M33*m.M41 - m.M23*m.M31*m.M44 - m.M21*m.M34*m.M43
minor13 := m.M21*m.M32*m.M44 + m.M22*m.M34*m.M41 + m.M24*m.M31*m.M42 - m.M24*m.M32*m.M41 - m.M22*m.M31*m.M44 - m.M21*m.M34*m.M42
minor14 := m.M21*m.M32*m.M43 + m.M22*m.M33*m.M41 + m.M23*m.M31*m.M42 - m.M23*m.M32*m.M41 - m.M22*m.M31*m.M43 - m.M21*m.M33*m.M42
minor21 := m.M12*m.M33*m.M44 + m.M13*m.M34*m.M42 + m.M14*m.M32*m.M43 - m.M14*m.M33*m.M42 - m.M13*m.M32*m.M44 - m.M12*m.M34*m.M43
minor22 := m.M11*m.M33*m.M44 + m.M13*m.M34*m.M41 + m.M14*m.M31*m.M43 - m.M14*m.M33*m.M41 - m.M13*m.M31*m.M44 - m.M11*m.M34*m.M43
minor23 := m.M11*m.M32*m.M44 + m.M12*m.M34*m.M41 + m.M14*m.M31*m.M42 - m.M14*m.M32*m.M41 - m.M12*m.M31*m.M44 - m.M11*m.M34*m.M42
minor24 := m.M11*m.M32*m.M43 + m.M12*m.M33*m.M41 + m.M13*m.M31*m.M42 - m.M13*m.M32*m.M41 - m.M12*m.M31*m.M43 - m.M11*m.M33*m.M42
minor31 := m.M12*m.M23*m.M44 + m.M13*m.M24*m.M42 + m.M14*m.M22*m.M43 - m.M14*m.M23*m.M42 - m.M13*m.M22*m.M44 - m.M12*m.M24*m.M43
minor32 := m.M11*m.M23*m.M44 + m.M13*m.M24*m.M41 + m.M14*m.M21*m.M43 - m.M14*m.M23*m.M41 - m.M13*m.M21*m.M44 - m.M11*m.M24*m.M43
minor33 := m.M11*m.M22*m.M44 + m.M12*m.M24*m.M41 + m.M14*m.M21*m.M42 - m.M14*m.M22*m.M41 - m.M12*m.M21*m.M44 - m.M11*m.M24*m.M42
minor34 := m.M11*m.M22*m.M43 + m.M12*m.M23*m.M41 + m.M13*m.M21*m.M42 - m.M13*m.M22*m.M41 - m.M12*m.M21*m.M43 - m.M11*m.M23*m.M42
minor41 := m.M12*m.M23*m.M34 + m.M13*m.M24*m.M32 + m.M14*m.M22*m.M33 - m.M14*m.M23*m.M32 - m.M13*m.M22*m.M34 - m.M12*m.M24*m.M33
minor42 := m.M11*m.M23*m.M34 + m.M13*m.M24*m.M31 + m.M14*m.M21*m.M33 - m.M14*m.M23*m.M31 - m.M13*m.M21*m.M34 - m.M11*m.M24*m.M33
minor43 := m.M11*m.M22*m.M34 + m.M12*m.M24*m.M31 + m.M14*m.M21*m.M32 - m.M14*m.M22*m.M31 - m.M12*m.M21*m.M34 - m.M11*m.M24*m.M32
minor44 := m.M11*m.M22*m.M33 + m.M12*m.M23*m.M31 + m.M13*m.M21*m.M32 - m.M13*m.M22*m.M31 - m.M12*m.M21*m.M33 - m.M11*m.M23*m.M32
determinant := m.M11*minor11 - m.M12*minor12 + m.M13*minor13 - m.M14*minor14
return NewMat4(
+minor11/determinant, -minor21/determinant, +minor31/determinant, -minor41/determinant,
-minor12/determinant, +minor22/determinant, -minor32/determinant, +minor42/determinant,
+minor13/determinant, -minor23/determinant, +minor33/determinant, -minor43/determinant,
-minor14/determinant, +minor24/determinant, -minor34/determinant, +minor44/determinant,
)
}
func TransformationMat4(orientX, orientY, orientZ, translation Vec3) Mat4 {
var result Mat4
result.M11 = orientX.X
result.M12 = orientY.X
result.M13 = orientZ.X
result.M14 = translation.X
result.M21 = orientX.Y
result.M22 = orientY.Y
result.M23 = orientZ.Y
result.M24 = translation.Y
result.M31 = orientX.Z
result.M32 = orientY.Z
result.M33 = orientZ.Z
result.M34 = translation.Z
result.M44 = 1.0
return result
}
func OrientationMat4(orientX, orientY, orientZ Vec3) Mat4 {
var result Mat4
result.M11 = orientX.X
result.M12 = orientY.X
result.M13 = orientZ.X
result.M21 = orientX.Y
result.M22 = orientY.Y
result.M23 = orientZ.Y
result.M31 = orientX.Z
result.M32 = orientY.Z
result.M33 = orientZ.Z
result.M44 = 1.0
return result
}
func RowMajorArrayToMat4(values [16]float64) Mat4 {
return Mat4{
M11: values[0], M12: values[1], M13: values[2], M14: values[3],
M21: values[4], M22: values[5], M23: values[6], M24: values[7],
M31: values[8], M32: values[9], M33: values[10], M34: values[11],
M41: values[12], M42: values[13], M43: values[14], M44: values[15],
}
}
func ColumnMajorArrayToMat4(values [16]float64) Mat4 {
return Mat4{
M11: values[0], M12: values[4], M13: values[8], M14: values[12],
M21: values[1], M22: values[5], M23: values[9], M24: values[13],
M31: values[2], M32: values[6], M33: values[10], M34: values[14],
M41: values[3], M42: values[7], M43: values[11], M44: values[15],
}
}
func Mat4Prod(left, right Mat4) Mat4 {
return Mat4{
M11: left.M11*right.M11 + left.M12*right.M21 + left.M13*right.M31 + left.M14*right.M41,
M12: left.M11*right.M12 + left.M12*right.M22 + left.M13*right.M32 + left.M14*right.M42,
M13: left.M11*right.M13 + left.M12*right.M23 + left.M13*right.M33 + left.M14*right.M43,
M14: left.M11*right.M14 + left.M12*right.M24 + left.M13*right.M34 + left.M14*right.M44,
M21: left.M21*right.M11 + left.M22*right.M21 + left.M23*right.M31 + left.M24*right.M41,
M22: left.M21*right.M12 + left.M22*right.M22 + left.M23*right.M32 + left.M24*right.M42,
M23: left.M21*right.M13 + left.M22*right.M23 + left.M23*right.M33 + left.M24*right.M43,
M24: left.M21*right.M14 + left.M22*right.M24 + left.M23*right.M34 + left.M24*right.M44,
M31: left.M31*right.M11 + left.M32*right.M21 + left.M33*right.M31 + left.M34*right.M41,
M32: left.M31*right.M12 + left.M32*right.M22 + left.M33*right.M32 + left.M34*right.M42,
M33: left.M31*right.M13 + left.M32*right.M23 + left.M33*right.M33 + left.M34*right.M43,
M34: left.M31*right.M14 + left.M32*right.M24 + left.M33*right.M34 + left.M34*right.M44,
M41: left.M41*right.M11 + left.M42*right.M21 + left.M43*right.M31 + left.M44*right.M41,
M42: left.M41*right.M12 + left.M42*right.M22 + left.M43*right.M32 + left.M44*right.M42,
M43: left.M41*right.M13 + left.M42*right.M23 + left.M43*right.M33 + left.M44*right.M43,
M44: left.M41*right.M14 + left.M42*right.M24 + left.M43*right.M34 + left.M44*right.M44,
}
}
func Mat4MultiProd(first Mat4, others ...Mat4) Mat4 {
result := first
for _, matrix := range others {
result = Mat4Prod(result, matrix)
}
return result
}
func Mat4Vec4Prod(mat Mat4, vec Vec4) Vec4 {
return Vec4{
X: mat.M11*vec.X + mat.M12*vec.Y + mat.M13*vec.Z + mat.M14*vec.W,
Y: mat.M21*vec.X + mat.M22*vec.Y + mat.M23*vec.Z + mat.M24*vec.W,
Z: mat.M31*vec.X + mat.M32*vec.Y + mat.M33*vec.Z + mat.M34*vec.W,
W: mat.M41*vec.X + mat.M42*vec.Y + mat.M43*vec.Z + mat.M44*vec.W,
}
}
func Mat4Vec3Transformation(mat Mat4, vec Vec3) Vec3 {
return Vec3{
X: mat.M11*vec.X + mat.M12*vec.Y + mat.M13*vec.Z + mat.M14,
Y: mat.M21*vec.X + mat.M22*vec.Y + mat.M23*vec.Z + mat.M24,
Z: mat.M31*vec.X + mat.M32*vec.Y + mat.M33*vec.Z + mat.M34,
}
}
type Mat4 struct {
M11, M12, M13, M14 float64
M21, M22, M23, M24 float64
M31, M32, M33, M34 float64
M41, M42, M43, M44 float64
}
func (m Mat4) IsNaN() bool {
return math.IsNaN(m.M11) || math.IsNaN(m.M12) || math.IsNaN(m.M13) || math.IsNaN(m.M14) ||
math.IsNaN(m.M21) || math.IsNaN(m.M22) || math.IsNaN(m.M23) || math.IsNaN(m.M24) ||
math.IsNaN(m.M31) || math.IsNaN(m.M32) || math.IsNaN(m.M33) || math.IsNaN(m.M34) ||
math.IsNaN(m.M41) || math.IsNaN(m.M42) || math.IsNaN(m.M43) || math.IsNaN(m.M44)
}
func (m Mat4) IsInf() bool {
return math.IsInf(m.M11, 0) || math.IsInf(m.M12, 0) || math.IsInf(m.M13, 0) || math.IsInf(m.M14, 0) ||
math.IsInf(m.M21, 0) || math.IsInf(m.M22, 0) || math.IsInf(m.M23, 0) || math.IsInf(m.M24, 0) ||
math.IsInf(m.M31, 0) || math.IsInf(m.M32, 0) || math.IsInf(m.M33, 0) || math.IsInf(m.M34, 0) ||
math.IsInf(m.M41, 0) || math.IsInf(m.M42, 0) || math.IsInf(m.M43, 0) || math.IsInf(m.M44, 0)
}
func (m Mat4) Row1() Vec4 {
return NewVec4(m.M11, m.M12, m.M13, m.M14)
}
func (m Mat4) Row2() Vec4 {
return NewVec4(m.M21, m.M22, m.M23, m.M24)
}
func (m Mat4) Row3() Vec4 {
return NewVec4(m.M31, m.M32, m.M33, m.M34)
}
func (m Mat4) Row4() Vec4 {
return NewVec4(m.M41, m.M42, m.M43, m.M44)
}
func (m Mat4) Column1() Vec4 {
return NewVec4(m.M11, m.M21, m.M31, m.M41)
}
func (m Mat4) Column2() Vec4 {
return NewVec4(m.M12, m.M22, m.M32, m.M42)
}
func (m Mat4) Column3() Vec4 {
return NewVec4(m.M13, m.M23, m.M33, m.M43)
}
func (m Mat4) Column4() Vec4 {
return NewVec4(m.M14, m.M24, m.M34, m.M44)
}
func (m Mat4) OrientationX() Vec3 {
return NewVec3(m.M11, m.M21, m.M31)
}
func (m Mat4) OrientationY() Vec3 {
return NewVec3(m.M12, m.M22, m.M32)
}
func (m Mat4) OrientationZ() Vec3 {
return NewVec3(m.M13, m.M23, m.M33)
}
func (m Mat4) Translation() Vec3 {
return NewVec3(m.M14, m.M24, m.M34)
}
func (m Mat4) Scale() Vec3 {
return NewVec3(
m.OrientationX().Length(),
m.OrientationY().Length(),
m.OrientationZ().Length(),
)
}
// Rotation returns the rotation that is represented by this matrix.
// NOTE: This function assumes that the matrix has identity scale. If you
// want to get the rotation of a matrix that has non-identity scale, consider
// using the TRS method.
func (m Mat4) Rotation() Quat {
// This is calculated by inversing the equations for
// quat.OrientationX, quat.OrientationY and quat.OrientationZ.
sqrX := (1.0 + m.M11 - m.M22 - m.M33) / 4.0
sqrY := (1.0 - m.M11 + m.M22 - m.M33) / 4.0
sqrZ := (1.0 - m.M11 - m.M22 + m.M33) / 4.0
var x, y, z, w float64
if sqrZ > sqrX && sqrZ > sqrY { // Z is largest
if Abs(sqrZ) < Epsilon {
return IdentityQuat()
}
z = Sqrt(sqrZ)
x = (m.M31 + m.M13) / (4 * z)
y = (m.M32 + m.M23) / (4 * z)
w = (m.M21 - m.M12) / (4 * z)
} else if sqrY > sqrX { // Y is largest
if Abs(sqrY) < Epsilon {
return IdentityQuat()
}
y = Sqrt(sqrY)
x = (m.M21 + m.M12) / (4 * y)
z = (m.M32 + m.M23) / (4 * y)
w = (m.M13 - m.M31) / (4 * y)
} else { // X is largest
if Abs(sqrX) < Epsilon {
return IdentityQuat()
}
x = Sqrt(sqrX)
y = (m.M21 + m.M12) / (4 * x)
z = (m.M31 + m.M13) / (4 * x)
w = (m.M32 - m.M23) / (4 * x)
}
return UnitQuat(NewQuat(w, x, y, z))
}
func (m Mat4) TRS() (Vec3, Quat, Vec3) {
translation := m.Translation()
scale := m.Scale()
m.M11 /= scale.X
m.M21 /= scale.X
m.M31 /= scale.X
m.M12 /= scale.Y
m.M22 /= scale.Y
m.M32 /= scale.Y
m.M13 /= scale.Z
m.M23 /= scale.Z
m.M33 /= scale.Z
rotation := m.Rotation()
return translation, rotation, scale
}
func (m Mat4) RowMajorArray() [16]float64 {
return [16]float64{
m.M11, m.M12, m.M13, m.M14,
m.M21, m.M22, m.M23, m.M24,
m.M31, m.M32, m.M33, m.M34,
m.M41, m.M42, m.M43, m.M44,
}
}
func (m Mat4) ColumnMajorArray() [16]float64 {
return [16]float64{
m.M11, m.M21, m.M31, m.M41,
m.M12, m.M22, m.M32, m.M42,
m.M13, m.M23, m.M33, m.M43,
m.M14, m.M24, m.M34, m.M44,
}
}
func (m Mat4) GoString() string {
return fmt.Sprintf("((%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f), (%f, %f, %f, %f))",
m.M11, m.M12, m.M13, m.M14,
m.M21, m.M22, m.M23, m.M24,
m.M31, m.M32, m.M33, m.M34,
m.M41, m.M42, m.M43, m.M44,
)
}