-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMatrix.cs
More file actions
581 lines (494 loc) · 16 KB
/
Matrix.cs
File metadata and controls
581 lines (494 loc) · 16 KB
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
574
575
576
577
578
579
580
581
using System;
using UnityEngine;
// Modified from https://msdn.microsoft.com/en-us/magazine/mt736457.aspx
public class Matrix
{
public float[][] mat;
public int rows
{
get { return mat.Length; }
}
public int cols
{
get { return mat[0].Length; }
}
public Matrix(int rows, int cols)
{
mat = MatrixCreate(rows, cols);
}
public Matrix(float[][] values)
{
mat = new float[values.GetLength(0)][];
for (int i = 0; i < values.GetLength(0); i++)
{
mat[i] = new float[values[0].Length];
}
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
mat[i][j] = values[i][j];
}
public Matrix(Matrix4x4 m)
{
Matrix res = new Matrix(new float[] {
m.m00, m.m01, m.m02, m.m03,
m.m10, m.m11, m.m12, m.m13,
m.m20, m.m21, m.m22, m.m23,
m.m30, m.m31, m.m32, m.m33
}, 4, 4);
mat = res.mat;
}
public Matrix(Transform transform, bool useLocalValues)
{
/*
Matrix4x4 m = transform.localToWorldMatrix;
Matrix res = new Matrix(new float[] {
m.m00, m.m01, m.m02, m.m03,
m.m10, m.m11, m.m12, m.m13,
m.m20, m.m21, m.m22, m.m23,
m.m30, m.m31, m.m32, m.m33
}, 4, 4);
mat = res.mat;
*/
if (useLocalValues)
{
Matrix rot = new Matrix(Matrix4x4.TRS(transform.localPosition, transform.localRotation, transform.localScale));
mat = rot.mat;
}
else
{
Matrix rot = new Matrix(Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale));
mat = rot.mat;
}
}
public Matrix T
{
get {
float[] resValues = new float[rows * cols];
int pos = 0;
for (int i = 0; i < cols; ++i)
for (int j = 0; j < rows; ++j)
resValues[pos++] = mat[j][i];
return new Matrix(resValues, cols, rows);
}
}
public Matrix(Vector4 vec)
{
Matrix res = new Matrix(new float[] { vec.x, vec.y, vec.z, vec.w }, 4, 1);
mat = res.mat;
}
public Matrix(Vector3 vec)
{
Matrix res = new Matrix(new float[] { vec.x, vec.y, vec.z }, 3, 1);
mat = res.mat;
}
public Matrix(Vector2 vec)
{
Matrix res = new Matrix(new float[] { vec.x, vec.y }, 2, 1);
mat = res.mat;
}
// Fro http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/
public Matrix(Quaternion quat)
{
mat = MatrixCreate(3, 3);
Vector4 q = new Vector4(quat.x, quat.y, quat.z, quat.w);
q = q.normalized;
float x = q.x;
float y = q.y;
float z = q.z;
float w = q.w;
mat[0][0] = 1 - 2 * y * y - 2 * z * z;
mat[0][1] = 2 * x * y - 2 * z * w;
mat[0][2] = 2 * x * z + 2 * y * w;
mat[1][0] = 2 * x * y + 2 * z * w;
mat[1][1] = 1 - 2 * x * x - 2 * z * z;
mat[1][2] = 2 * y * z - 2 * x * w;
mat[2][0] = 2 * x * z - 2 * y * w;
mat[2][1] = 2 * y * z + 2 * x * w;
mat[2][2] = 1 - 2 * x * x - 2 * y * y;
}
public Matrix(float[] values, int rows, int cols)
{
if (rows*cols != values.Length)
{
throw new Exception("rows x cols: " + rows + "x" + cols + " = " + rows * cols + " does not equal given data of size " + values.Length);
}
mat = MatrixCreate(rows, cols);
int pos = 0;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
mat[i][j] = values[pos];
pos++;
}
}
}
public Matrix(double[] values, int rows, int cols)
{
if (rows * cols != values.Length)
{
throw new Exception("rows x cols: " + rows + "x" + cols + " = " + rows * cols + " does not equal given data of size " + values.Length);
}
mat = MatrixCreate(rows, cols);
int pos = 0;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
mat[i][j] = (float)values[pos];
pos++;
}
}
}
// From http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
// Uses only top left 3x3
public Quaternion ToQuaterion()
{
float[][] a = mat;
Quaternion q = new Quaternion();
float trace = a[0][0] + a[1][1] + a[2][2]; // I removed + 1.0f; see discussion with Ethan
if (trace > 0)
{// I changed M_EPSILON to 0
float s = 0.5f / Mathf.Sqrt(trace + 1.0f);
q.w = 0.25f / s;
q.x = (a[2][1] - a[1][2]) * s;
q.y = (a[0][2] - a[2][0]) * s;
q.z = (a[1][0] - a[0][1]) * s;
}
else
{
if (a[0][0] > a[1][1] && a[0][0] > a[2][2])
{
float s = 2.0f * Mathf.Sqrt(1.0f + a[0][0] - a[1][1] - a[2][2]);
q.w = (a[2][1] - a[1][2]) / s;
q.x = 0.25f * s;
q.y = (a[0][1] + a[1][0]) / s;
q.z = (a[0][2] + a[2][0]) / s;
}
else if (a[1][1] > a[2][2])
{
float s = 2.0f * Mathf.Sqrt(1.0f + a[1][1] - a[0][0] - a[2][2]);
q.w = (a[0][2] - a[2][0]) / s;
q.x = (a[0][1] + a[1][0]) / s;
q.y = 0.25f * s;
q.z = (a[1][2] + a[2][1]) / s;
}
else
{
float s = 2.0f * Mathf.Sqrt(1.0f + a[2][2] - a[0][0] - a[1][1]);
q.w = (a[1][0] - a[0][1]) / s;
q.x = (a[0][2] + a[2][0]) / s;
q.y = (a[1][2] + a[2][1]) / s;
q.z = 0.25f * s;
}
}
return q;
}
public Vector4 ToVec4()
{
float[] arr = ToArray();
if (!((rows == 4 && cols == 1) || (rows == 1 && cols == 4)))
{
throw new ArgumentException("Matrix is of dimension (" + rows + ", " + cols + ") which cannot be converted to a Vector4");
}
return new Vector4(arr[0], arr[1], arr[2], arr[3]);
}
public Vector3 ToVec3()
{
float[] arr = ToArray();
if (!((rows == 3 && cols == 1) || (rows == 1 && cols == 3)))
{
throw new ArgumentException("Matrix is of dimension (" + rows + ", " + cols + ") which cannot be converted to a Vector3");
}
return new Vector3(arr[0], arr[1], arr[2]);
}
public Vector2 ToVec2()
{
float[] arr = ToArray();
if (!((rows == 2 && cols == 1) || (rows == 1 && cols == 2)))
{
throw new ArgumentException("Matrix is of dimension (" + rows + ", " + cols + ") which cannot be converted to a Vector2");
}
return new Vector2(arr[0], arr[1]);
}
public override string ToString()
{
return MatrixAsString(mat);
}
public float[] ToArray()
{
float[] arr = new float[rows * cols];
int pos = 0;
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
arr[pos++] = mat[i][j];
return arr;
}
public float this[int i, int j]
{
get { return mat[i][j]; }
set { mat[i][j] = value; }
}
public static Matrix operator *(Matrix m, float s)
{
Matrix res = new Matrix(m.rows, m.cols);
for (int i = 0; i < m.rows; ++i)
for (int j = 0; j < m.cols; ++j)
res[i, j] = m[i, j] * s;
return res;
}
public static Matrix operator *(float s, Matrix m)
{
return m * s;
}
public static Matrix operator +(Matrix m, float s)
{
Matrix res = new Matrix(m.rows, m.cols);
for (int i = 0; i < m.rows; ++i)
for (int j = 0; j < m.cols; ++j)
res[i, j] = m[i, j] + s;
return res;
}
public static Matrix operator +(float s, Matrix m)
{
return m + s;
}
public static Matrix operator -(Matrix m, float s)
{
return m + (-s);
}
public static Matrix operator -(float s, Matrix m)
{
return (-m) + s;
}
public static Matrix operator *(Matrix c1, Matrix c2)
{
return new Matrix(MatrixProduct(c1.mat, c2.mat));
}
public static Matrix operator *(Matrix c1, Vector3 c2)
{
return new Matrix(MatrixProduct(c1.mat, (new Matrix(c2)).mat));
}
public static Matrix operator *(Vector3 c1, Matrix c2)
{
return new Matrix(MatrixProduct((new Matrix(c1)).T.mat, c2.mat));
}
public static Matrix operator *(Matrix c1, Vector4 c2)
{
return new Matrix(MatrixProduct(c1.mat, (new Matrix(c2)).mat));
}
public static Matrix operator *(Vector4 c1, Matrix c2)
{
return new Matrix(MatrixProduct((new Matrix(c1)).T.mat, c2.mat));
}
public static Matrix operator -(Matrix m)
{
Matrix res = new Matrix(m.rows, m.cols);
for (int i = 0; i < m.rows; ++i)
for (int j = 0; j < m.cols; ++j)
res[i, j] = -m[i, j];
return res;
}
public static Matrix operator +(Matrix c1, Matrix c2)
{
if (c1.rows != c2.rows || c1.cols != c2.cols)
{
throw new ArgumentException("Left hand side size: (" + c1.rows + ", " + c1.cols + ") != Right hand side size: (" + c2.rows + ", " + c2.cols + ")");
}
Matrix res = new Matrix(c1.rows, c1.cols);
for (int i = 0; i < c1.rows; ++i)
for (int j = 0; j < c1.cols; ++j)
res[i, j] = c1[i, j] + c2[i, j];
return res;
}
public static Matrix operator -(Matrix c1, Matrix c2)
{
return c1 + (-c2);
}
public Matrix Inverse()
{
return new Matrix(MatrixInverse(mat));
}
public static float[][] MatrixInverse(float[][] matrix)
{
// assumes determinant is not 0
// that is, the matrix does have an inverse
int n = matrix.Length;
float[][] result = MatrixCreate(n, n); // make a copy of matrix
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
result[i][j] = matrix[i][j];
float[][] lum; // combined lower & upper
int[] perm;
int toggle;
toggle = MatrixDecompose(matrix, out lum, out perm);
float[] b = new float[n];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
if (i == perm[j])
b[j] = 1.0f;
else
b[j] = 0.0f;
float[] x = Helper(lum, b); //
for (int j = 0; j < n; ++j)
result[j][i] = x[j];
}
return result;
} // MatrixInverse
public static int MatrixDecompose(float[][] m, out float[][] lum, out int[] perm)
{
// Crout's LU decomposition for matrix determinant and inverse
// stores combined lower & upper in lum[][]
// stores row permuations into perm[]
// returns +1 or -1 according to even or odd number of row permutations
// lower gets dummy 1.0s on diagonal (0.0s above)
// upper gets lum values on diagonal (0.0s below)
int toggle = +1; // even (+1) or odd (-1) row permutatuions
int n = m.Length;
// make a copy of m[][] into result lu[][]
lum = MatrixCreate(n, n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
lum[i][j] = m[i][j];
// make perm[]
perm = new int[n];
for (int i = 0; i < n; ++i)
perm[i] = i;
for (int j = 0; j < n - 1; ++j) // process by column. note n-1
{
float max = Math.Abs(lum[j][j]);
int piv = j;
for (int i = j + 1; i < n; ++i) // find pivot index
{
float xij = Math.Abs(lum[i][j]);
if (xij > max)
{
max = xij;
piv = i;
}
} // i
if (piv != j)
{
float[] tmp = lum[piv]; // swap rows j, piv
lum[piv] = lum[j];
lum[j] = tmp;
int t = perm[piv]; // swap perm elements
perm[piv] = perm[j];
perm[j] = t;
toggle = -toggle;
}
float xjj = lum[j][j];
if (xjj != 0.0)
{
for (int i = j + 1; i < n; ++i)
{
float xij = lum[i][j] / xjj;
lum[i][j] = xij;
for (int k = j + 1; k < n; ++k)
lum[i][k] -= xij * lum[j][k];
}
}
} // j
return toggle;
} // MatrixDecompose
public static float[] Helper(float[][] luMatrix, float[] b) // helper
{
int n = luMatrix.Length;
float[] x = new float[n];
b.CopyTo(x, 0);
for (int i = 1; i < n; ++i)
{
float sum = x[i];
for (int j = 0; j < i; ++j)
sum -= luMatrix[i][j] * x[j];
x[i] = sum;
}
x[n - 1] /= luMatrix[n - 1][n - 1];
for (int i = n - 2; i >= 0; --i)
{
float sum = x[i];
for (int j = i + 1; j < n; ++j)
sum -= luMatrix[i][j] * x[j];
x[i] = sum / luMatrix[i][i];
}
return x;
} // Helper
public static float MatrixDeterminant(float[][] matrix)
{
float[][] lum;
int[] perm;
int toggle = MatrixDecompose(matrix, out lum, out perm);
float result = toggle;
for (int i = 0; i < lum.Length; ++i)
result *= lum[i][i];
return result;
}
// ----------------------------------------------------------------
public static float[][] MatrixCreate(int rows, int cols)
{
float[][] result = new float[rows][];
for (int i = 0; i < rows; ++i)
result[i] = new float[cols];
return result;
}
public static float[][] MatrixProduct(float[][] matrixA,
float[][] matrixB)
{
int aRows = matrixA.Length;
int aCols = matrixA[0].Length;
int bRows = matrixB.Length;
int bCols = matrixB[0].Length;
if (aCols != bRows)
throw new Exception("Non-conformable matrices");
float[][] result = MatrixCreate(aRows, bCols);
for (int i = 0; i < aRows; ++i) // each row of A
for (int j = 0; j < bCols; ++j) // each col of B
for (int k = 0; k < aCols; ++k) // could use k < bRows
result[i][j] += matrixA[i][k] * matrixB[k][j];
return result;
}
public static string MatrixAsString(float[][] matrix)
{
string s = "";
for (int i = 0; i < matrix.Length; ++i)
{
for (int j = 0; j < matrix[i].Length; ++j)
s += matrix[i][j].ToString("F3").PadLeft(8) + " ";
s += Environment.NewLine;
}
return s;
}
public static float[][] ExtractLower(float[][] lum)
{
// lower part of an LU Doolittle decomposition (dummy 1.0s on diagonal, 0.0s above)
int n = lum.Length;
float[][] result = MatrixCreate(n, n);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i == j)
result[i][j] = 1.0f;
else if (i > j)
result[i][j] = lum[i][j];
}
}
return result;
}
public static float[][] ExtractUpper(float[][] lum)
{
// upper part of an LU (lu values on diagional and above, 0.0s below)
int n = lum.Length;
float[][] result = MatrixCreate(n, n);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i <= j)
result[i][j] = lum[i][j];
}
}
return result;
}
}