-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.go
573 lines (493 loc) · 13.8 KB
/
matrix.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
package matrix
import (
"fmt"
"math"
"github.com/andewx/dieselfluid/math/vector"
)
//-----------------------DSL FLUID Mat LIBRARY ----------------------------
//DSL FLUID Mat Library implementation is non mutating (unless) the function
//had a tag @MUTATE and typically a suffix V. 3D Mat Libraries are typically
//Square so by convention we will only allow mxm square defined Mates. This
//Allows us to wrangle the matrices into one data type slice array of floats
type Mat []float32
const MAT2 = 2
const MAT2_SIZE = 4
const MAT3 = 3
const MAT3_SIZE = 9
const MAT4 = 4
const MAT4_SIZE = 16
//--------------Mat Utilities-----------------------------------------------
//Mat2 Creates new float slice type Mat of length 2 set to identity val
func Mat2(v float32) Mat {
a := make([]float32, MAT2_SIZE)
for i := 0; i < MAT2_SIZE; i += (MAT2 + 1) {
a[i] = v
}
return a
}
//Mat2 Creates new float slice type Mat of length 2 set to identity val
func Mat3(v float32) Mat {
a := make([]float32, MAT3_SIZE)
for i := 0; i < MAT3_SIZE; i += (MAT3 + 1) {
a[i] = v
}
return a
}
//Mat2 Creates new float slice type Mat of length 2 set to identity val
func Mat4(v float32) Mat {
a := make([]float32, MAT4_SIZE)
for i := 0; i < MAT4_SIZE; i += (MAT4 + 1) {
a[i] = v
}
return a
}
//MatN Creates new float slice type Mat of length 2 set to identity val
func MatN(dim int, v float32) Mat {
a := make([]float32, dim*dim)
for i := 0; i < dim*dim; i += (dim + 1) {
a[i] = v
}
return a
}
//Mat2 Constructs Custom 2x2 Mat from construction vectors v0,v1
func Mat2V(v0 vector.Vec, v1 vector.Vec) Mat {
nMat := make([]float32, 4)
vecSwitch := 0
for i := 0; i < MAT2; i++ {
if vecSwitch == 0 {
nMat[i] = v0[i%MAT2]
}
if vecSwitch == 1 {
nMat[i] = v1[i%MAT2]
}
if i%2 == 0 {
vecSwitch++
}
}
return nMat
}
//Mat3 Constructs Custom 3x3 Mat from construction vectors v0,v1, v2
func Mat3V(v0 vector.Vec, v1 vector.Vec, v2 vector.Vec) Mat {
nMat := make([]float32, 9)
vecSwitch := 0
for i := 0; i < MAT3; i++ {
if vecSwitch == 0 {
nMat[i] = v0[i%MAT3]
}
if vecSwitch == 1 {
nMat[i] = v1[i%MAT3]
}
if vecSwitch == 2 {
nMat[i] = v2[i%MAT3]
}
if i%3 == 0 {
vecSwitch++
}
}
return nMat
}
//Mat4 Constructs Custom 4x4 Mat from construction vectors v0,v1, v2
func Mat4V(v0 vector.Vec, v1 vector.Vec, v2 vector.Vec, v3 vector.Vec) Mat {
nMat := make([]float32, 16)
vecSwitch := 0
for i := 0; i < MAT4; i++ {
if vecSwitch == 0 {
if i < 3 {
nMat[i] = v0[i%MAT3]
}
}
if vecSwitch == 1 {
if i < 7 {
nMat[i] = v1[i%MAT3]
}
}
if vecSwitch == 2 {
if i < 11 {
nMat[i] = v2[i%MAT3]
}
}
if vecSwitch == 3 {
if i < 15 {
nMat[i] = v3[i%MAT3]
}
}
if i%4 == 0 {
vecSwitch++
}
}
return nMat
}
//Copy returns a new matrix copy from the addressed Mat object
func (m Mat) Copy() Mat {
newMat := make(Mat, len(m))
for i := 0; i < len(m); i++ {
newMat[i] = m[i]
}
return newMat
}
//MatEqual returns two Matrixes equal by epsilon value comparision
func MatEqual(a Mat, e Mat) bool {
if len(a) != len(e) {
return false
}
for k := 0; k < len(a); k++ {
comp := math.Abs(float64(e[k] - a[k]))
if comp > 0.0000001 {
return false
}
}
return true
}
//Dim Returns Dimensionality of Square Matrix Based on Size...If size not recognized
//Then Dim returns non nil error
func (M Mat) Dim() int {
if len(M) == 4 {
return 2
}
if len(M) == 9 {
return 3
}
if len(M) == 16 {
return 4
}
return 0
}
//Dim Returns Dimensionality of Square Matrix Based on Size...If size not recognized
//Then Dim returns non nil error
func Dim(M Mat) int {
if len(M) == 4 {
return 2
}
if len(M) == 9 {
return 3
}
if len(M) == 16 {
return 4
}
return 0
}
//MAPS any 2D Matrix Position to its linear ROW-Major order layout in memory with
//the returned index being 0 for invalid inputs
func Map(i int, j int, mat_size int) int {
if i < 0 || j < 0 {
return 0
}
if i >= mat_size || j >= mat_size {
return 0
}
return (i * mat_size) + j
}
func (a Mat) Det() float32 {
dim := a.Dim()
if dim == 4 {
return a.Det4()
}
if dim == 3 {
return a.Det3()
}
if dim == 2 {
return a.Det2()
} else {
return 0.0
}
}
//Det2() Handles this matrix as size 2 matrix and returns the determinant
func Det2(a Mat) float32 {
if len(a) != 4 {
fmt.Printf("Invalid Matrix size for Det2()\n")
return 0.0
}
a0 := a[0]
a3 := a[3]
a1 := a[1]
a2 := a[2]
return a0*a3 - a1*a2
}
//Det2() Handles this matrix as size 2 matrix and returns the determinant
func (a Mat) Det2() float32 {
if len(a) != 4 {
fmt.Printf("Invalid Matrix size for Det2()\n")
return 0.0
}
a0 := a[0]
a3 := a[3]
a1 := a[1]
a2 := a[2]
return a0*a3 - a1*a2
}
//Inverse2() Computes the Matrix Inverse of a 2 Sized Matrix for now all inverse
//And determinant functinos will be named since they are inmplemented differently
//Inverse 2 is non mutating and delgates the value of the new matrix
func (m Mat) Inv2() Mat {
inverse := Mat2(0.0)
if m.Dim() != 2 {
fmt.Printf("Invalid Matrix size for Inverse2() returning Mat2 Identity\n")
return Mat2(1.0)
}
det := m.Det2()
if det == 0 {
return inverse
}
inverse[0] = m[3] / det
inverse[1] = -(m[1] / det)
inverse[2] = -(m[2] / det)
inverse[3] = m[0] / det
return inverse
}
//Det calcs the 3x3 Determinant
// return m[0][0] *(m[1][1]M[2][2] - m[1][2]M[2][1]) - m[0][1]*(m[1][0]M[2][2] - m[1][2]M[2][0]) - m[0][2]*(m[1][0]M[2][1]-m[1][1]M[2][0])
func (m Mat) Det3() float32 {
g := Mat([]float32{m[4], m[5], m[7], m[8]})
h := Mat([]float32{m[3], m[5], m[6], m[8]})
j := Mat([]float32{m[3], m[4], m[6], m[7]})
a := m[0]
b := m[1]
c := m[2]
return a*g.Det2() - b*h.Det2() + c*j.Det2()
}
//Set(row int, v vector.Vec) sets a matrix row to the specified vector
//WARNING this is a mutatiing @MUTATE operation that changes the internal
//Values of the referenced matrix M
func (m Mat) Set(row int, v vector.Vec) {
dim := m.Dim()
if dim < len(v) {
vector.LogVecError(v, "Set(row int, v vector.Vec) FAILED vector length less the matrix dim")
return
}
idx := Map(row, 0, dim)
for i := 0; i < 3; i++ {
m[i+idx] = v[i]
}
}
//Get obtains the Row vector.Vector from the parameter row copies vector value into a
//new vector and returns.
func (m Mat) Get(row int) vector.Vec {
idx := Map(row, 0, m.Dim())
v := vector.VecN(m.Dim())
for i := 0; i < m.Dim(); i++ {
v[i] = m[i+idx]
}
return v
}
// Mul performs a scalar multiplcation of the Mat. This is equivalent to iterating
// over every element of the Mat and multiply it by c.
func (m1 Mat) Mul(c float32) Mat {
m2 := MatN(m1.Dim(), 0.0)
for i := 0; i < m1.Dim()*m1.Dim(); i++ {
m2[i] = m1[i] * c
}
return m2
}
// Inv computes the inverse of a square Mat. An inverse is a square Mat such that when multiplied by the
// original, yields the identity.
//
// M_inv * M = M * M_inv = I
//
// In this library, the math is precomputed, and uses no loops, though the multiplications, additions, determinant calculation, and scaling
// are still done. This can still be (relatively) expensive for a 4x4.
//
// This function checks the determinant to see if the Mat is invertible.
// If the determinant is 0.0, this function returns the zero Mat. However, due to floating point errors, it is
// entirely plausible to get a false positive or negative.
// In the future, an alternate function may be written which takes in a pre-computed determinant.
//Tries to simplify main call for matrixes of different dimensions by delgating to other calls if the dimension aren't equal
func (m Mat) Inv() Mat {
//Main attach point for other inverses
if m.Dim() != 4 {
if m.Dim() == 3 {
return m.Inv3()
}
if m.Dim() == 2 {
return m.Inv2()
}
return Mat4(1.0)
}
det := m.Det4()
if det == 0.0 {
return Mat4(1.0)
}
retMat := Mat{
-m[7]*m[10]*m[13] + m[6]*m[11]*m[13] + m[7]*m[9]*m[14] - m[5]*m[11]*m[14] - m[6]*m[9]*m[15] + m[5]*m[10]*m[15],
m[3]*m[10]*m[13] - m[2]*m[11]*m[13] - m[3]*m[9]*m[14] + m[1]*m[11]*m[14] + m[2]*m[9]*m[15] - m[1]*m[10]*m[15],
-m[3]*m[6]*m[13] + m[2]*m[7]*m[13] + m[3]*m[5]*m[14] - m[1]*m[7]*m[14] - m[2]*m[5]*m[15] + m[1]*m[6]*m[15],
m[3]*m[6]*m[9] - m[2]*m[7]*m[9] - m[3]*m[5]*m[10] + m[1]*m[7]*m[10] + m[2]*m[5]*m[11] - m[1]*m[6]*m[11],
m[7]*m[10]*m[12] - m[6]*m[11]*m[12] - m[7]*m[8]*m[14] + m[4]*m[11]*m[14] + m[6]*m[8]*m[15] - m[4]*m[10]*m[15],
-m[3]*m[10]*m[12] + m[2]*m[11]*m[12] + m[3]*m[8]*m[14] - m[0]*m[11]*m[14] - m[2]*m[8]*m[15] + m[0]*m[10]*m[15],
m[3]*m[6]*m[12] - m[2]*m[7]*m[12] - m[3]*m[4]*m[14] + m[0]*m[7]*m[14] + m[2]*m[4]*m[15] - m[0]*m[6]*m[15],
-m[3]*m[6]*m[8] + m[2]*m[7]*m[8] + m[3]*m[4]*m[10] - m[0]*m[7]*m[10] - m[2]*m[4]*m[11] + m[0]*m[6]*m[11],
-m[7]*m[9]*m[12] + m[5]*m[11]*m[12] + m[7]*m[8]*m[13] - m[4]*m[11]*m[13] - m[5]*m[8]*m[15] + m[4]*m[9]*m[15],
m[3]*m[9]*m[12] - m[1]*m[11]*m[12] - m[3]*m[8]*m[13] + m[0]*m[11]*m[13] + m[1]*m[8]*m[15] - m[0]*m[9]*m[15],
-m[3]*m[5]*m[12] + m[1]*m[7]*m[12] + m[3]*m[4]*m[13] - m[0]*m[7]*m[13] - m[1]*m[4]*m[15] + m[0]*m[5]*m[15],
m[3]*m[5]*m[8] - m[1]*m[7]*m[8] - m[3]*m[4]*m[9] + m[0]*m[7]*m[9] + m[1]*m[4]*m[11] - m[0]*m[5]*m[11],
m[6]*m[9]*m[12] - m[5]*m[10]*m[12] - m[6]*m[8]*m[13] + m[4]*m[10]*m[13] + m[5]*m[8]*m[14] - m[4]*m[9]*m[14],
-m[2]*m[9]*m[12] + m[1]*m[10]*m[12] + m[2]*m[8]*m[13] - m[0]*m[10]*m[13] - m[1]*m[8]*m[14] + m[0]*m[9]*m[14],
m[2]*m[5]*m[12] - m[1]*m[6]*m[12] - m[2]*m[4]*m[13] + m[0]*m[6]*m[13] + m[1]*m[4]*m[14] - m[0]*m[5]*m[14],
-m[2]*m[5]*m[8] + m[1]*m[6]*m[8] + m[2]*m[4]*m[9] - m[0]*m[6]*m[9] - m[1]*m[4]*m[10] + m[0]*m[5]*m[10],
}
return retMat.Mul(1 / det)
}
//Inverse solves the 3x3 Mat through determinant
func (m Mat) Inv3() Mat {
if m.Dim() != 3 {
return Mat3(1.0)
}
inv := make([]float32, 9)
det := m.Det3()
minor := make([]Mat, 9)
for k := 0; k < 9; k++ {
minor[k] = Mat2(0.0)
}
for i := 0; i < MAT3; i++ {
for j := 0; j < MAT3; j++ {
indx := Map(i, j, MAT3)
//Get Diagional Indexes and apply modulus
d0 := Map((i+1)%3, (j+1)%3, MAT3)
d1 := Map((i+1)%3, (j+2)%3, MAT3)
d2 := Map((i+2)%3, (j+1)%3, MAT3)
d3 := Map((i+2)%3, (j+2)%3, MAT3)
minor[indx][0] = m[d0]
minor[indx][1] = m[d1]
minor[indx][2] = m[d2]
minor[indx][3] = m[d3]
inv[indx] = minor[indx].Det2() / det
}
}
return inv
}
//Det Solves the 4x4 Mat Determinant
func (m Mat) Det4() float32 {
a := m[0]
b := m[1]
c := m[2]
d := m[3]
i := Mat{m[5], m[6], m[7], m[9], m[10], m[11], m[13], m[14], m[15]}
j := Mat{m[4], m[6], m[7], m[8], m[10], m[11], m[12], m[14], m[15]}
k := Mat{m[4], m[5], m[7], m[8], m[9], m[11], m[12], m[13], m[15]}
l := Mat{m[4], m[5], m[6], m[8], m[9], m[10], m[12], m[13], m[14]}
return a*i.Det() - b*j.Det() + c*k.Det() - d*l.Det()
}
//Multplies two matrices with mutation into left associated A
func MulM(a Mat, b Mat) Mat {
if len(a) != len(b) {
return a
}
c := MatN(a.Dim(), 0.0)
var entry float32
var rv, cv int
dim := a.Dim()
for i := 0; i < dim; i++ {
entry = 0
for j := 0; j < dim; j++ {
idx := Map(i, j, dim)
entry = 0
for k := 0; k < dim; k++ {
rv = Map(i, k, dim)
cv = Map(k, j, dim)
entry += a[rv] * b[cv] //Row Col Dot Product
}
c[idx] = entry
}
}
return c
}
//Multplies two matrices with mutation into left associated A
func (a Mat) MulM(b Mat) Mat {
if len(a) != len(b) {
return a
}
c := MatN(a.Dim(), 0.0)
var entry float32
var rv, cv int
dim := a.Dim()
for i := 0; i < dim; i++ {
entry = 0
for j := 0; j < dim; j++ {
idx := Map(i, j, dim)
entry = 0
for k := 0; k < dim; k++ {
rv = Map(i, k, dim)
cv = Map(k, j, dim)
entry += a[rv] * b[cv] //Row Col Dot Product
}
c[idx] = entry
}
}
return c
}
//Crossvector.Vec crosses a matrix with a vector by summing the Matrix Row (Row Major)
//With the associative column vector. Assuming matrices are all MxM in this LIBRARY
//vector.Vector is returned length M
func (a Mat) CrossVec(b vector.Vec) vector.Vec {
if a.Dim() != len(b) {
vector.LogVecError(b, "Cross vector.Vec Matrix Dim != vector.Vec Dim")
return vector.VecN(a.Dim())
}
r := vector.VecN(a.Dim())
for i := 0; i < a.Dim(); i++ {
for j := 0; j < a.Dim(); j++ {
mIdx := Map(i, j, a.Dim())
r[i] += a[mIdx] * b[j]
}
}
return r
}
//Transpose Matrix reverses matrix index positions for square matrixes only
//Transpose is a non mutating function
func (a Mat) Transpose() Mat {
dim := a.Dim()
retMat := MatN(dim, 1.0)
for i := 0; i < dim; i++ {
for j := 0; j < dim; j++ {
id1 := i*dim + j
id2 := j*dim + i
retMat[id2] = a[id1]
}
}
return retMat
}
//Normalize normalizes the current matrix. This operation is a mutating operation
//WARNING THIS OPERATION IS A MUTATION OF THE CURRENT MATRIX
func (M Mat) Normalize() Mat {
det := M.Det()
if det == 0.0 {
return M
}
for i := 0; i < 16; i++ {
M[i] = M[i] / det
}
return M
}
//Projection Mat creates a projection Mat
func ProjectionMat(l float32, r float32, t float32, b float32, n float32, f float32) Mat {
s := 1 / (float32(math.Tan(float64(45.0 / 2 * 3.141529 / 180))))
proj := Mat{s, 0, 0, 0, 0, s, 0, 0, 0, 0, (-f / (f - n)), (-f * n) / (f - n), 0, 0, -1, 0}
// proj := Mat4{2 * n / (r - l), 0, (r + l) / (r - l), 0, 0, (2 * n) / (t - b), (t + b) / (t - b), 0, 0, 0, (-(f + n) / (f - n)), (-2 * f * n) / (f - n), 0, 0, -1, 0} //scratch a pixel projection Mat
return proj
}
//Projection Mat creates a projection Mat
func ProjectionMatF(fov float32, aspect float32, n float32, f float32) Mat {
s := 1 / (float32(math.Tan(float64((fov / 2) * (3.141529 / 180)))))
proj := Mat{s / aspect, 0, 0, 0, 0, s, 0, 0, 0, 0, (-f / (f - n)), (-f * n) / (f - n), 0, 0, -1, 0}
return proj
}
func Mat3T4(a Mat) Mat {
newMat := Mat4(1.0)
for i := 0; i < 9; i++ {
newMat[i] = a[i]
}
return newMat
}
//Mat Print Operations
func (a Mat) String() string {
s := ""
dim := a.Dim()
for i := 0; i < dim; i++ {
s += fmt.Sprintf("| ")
for j := 0; j < dim; j++ {
id := Map(i, j, dim)
s += fmt.Sprintf(" %f ", a[id])
}
s += fmt.Sprintf(" |\n")
}
return s
}