-
Notifications
You must be signed in to change notification settings - Fork 0
/
linalg.js
583 lines (545 loc) · 28.2 KB
/
linalg.js
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
582
583
var TMATH = {
errors: {
invalidArgument: 'Invalid function argument.',
scalarDivisionByZero: 'Scalar division by zero not allowed.',
pointwiseDivisionByZero: 'Pointwise division by zero not allowed.',
},
// Checks whether two line segments (a1->a2 and b1->b2) in the 2D plane intersect.
// If the line segments are parallel and intersect, the result is not necessarily defined.
// a1, a2, b1, b2 are of type Vec2.
lineSegmentsIntersect: function(a1, a2, b1, b2) {
// Defining bounding boxes:
let bba_max = new Vec2(Math.max(a1.x, a2.x), Math.max(a1.y, a2.y));
let bba_min = new Vec2(Math.min(a1.x, a2.x), Math.min(a1.y, a2.y));
let bbb_max = new Vec2(Math.max(b1.x, b2.x), Math.max(b1.y, b2.y));
let bbb_min = new Vec2(Math.min(b1.x, b2.x), Math.min(b1.y, b2.y));
// If bounding boxes do not intersect, neither can the line segments a and b:
if (bba_min.x >= bbb_max.x ||
bba_min.y >= bbb_max.y ||
bbb_min.x >= bba_max.x ||
bbb_min.y >= bba_max.y) return false;
// Defining the direction vectors of the line segments a and b:
let a = a2.sub(a1);
let b = b2.sub(b1);
if (a.lengthSqr === 0 || b.lengthSqr === 0) return false;
// Find the intersection point between the lines:
let t = new Mat2(a, b.neg).inverse.mul(b1.sub(a1)); // The parameter t of the line intersection equation a1 + (a2 - a1)*t_0 = b1 + (b2 - b1)*t_1
// The line segments intersect iff the intersection point of the full lines
// lies on both line segments:
if (0 < t.x && t.x < 1 &&
0 < t.y && t.y < 1) return true;
else return false;
}
}
class Vec2 {
constructor(x, y) {
this._x = x;
this._y = y;
this._type = this.constructor.name;
}
get type() { return this._type; }
get x() { return this._x; }
get y() { return this._y; }
get length() { return Math.sqrt(this._x * this._x + this._y * this._y); }
get lengthSqr() { return this._x * this._x + this._y * this._y; }
get neg() { return new this.constructor(-this._x, -this._y); }
get normalized() { return this.div(this.length); }
get isZero() { return this._x === 0 && this._y === 0; }
get toVec3() { return new Vec3(this._x, this._y, 0); }
get toVec4() { return new Vec4(this._x, this._y, 0, 0); }
get toVec4Homogeneous() { return new Vec4(this._x, this._y, 0, 1); }
get toString() { return `${ this._type }(${ this._x }, ${ this._y })`; }
equals(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this._x === other.x && this._y === other.y;
} else throw TMATH.errors.invalidArgument;
}
mul(other) {
switch(typeof(other)) {
case 'number':
return new this.constructor(this._x * other, this._y * other);
case 'object':
if (other.type === this._type) {
return new this.constructor(this._x * other.x, this._y * other.y);
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
div(other) {
switch(typeof(other)) {
case 'number':
//if (other === 0) throw TMATH.errors.scalarDivisionByZero;
return new this.constructor(this._x / other, this._y / other);
case 'object':
if (other.type === this._type) {
//if (other.x === 0 || other.y === 0) throw TMATH.errors.pointwiseDivisionByZero;
return new this.constructor(this._x / other.x, this._y / other.y);
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
add(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._x + other.x, this._y + other.y);
} else throw TMATH.errors.invalidArgument;
}
sub(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._x - other.x, this._y - other.y);
} else throw TMATH.errors.invalidArgument;
}
dot(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this._x * other.x + this._y * other.y;
} else throw TMATH.errors.invalidArgument;
}
};
class Vec3 {
constructor(x, y, z) {
this._x = x;
this._y = y;
this._z = z;
this._type = this.constructor.name;
}
get type() { return this._type; }
get x() { return this._x; }
get y() { return this._y; }
get z() { return this._z; }
get length() { return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z); }
get lengthSqr() { return this._x * this._x + this._y * this._y + this._z * this._z; }
get neg() { return new this.constructor(-this._x, -this._y, -this._z); }
get normalized() { return this.div(this.length); }
get isZero() { return this._x === 0 && this._y === 0 && this._z === 0; }
get toVec4() { return new Vec4(this._x, this._y, this._z, 0); }
get toVec4Homogeneous() { return new Vec4(this._x, this._y, this._z, 1); }
get toString() { return `${ this._type }(${ this._x }, ${ this._y }, ${ this._z })`; }
equals(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this._x === other.x && this._y === other.y && this._z === other.z;
} else throw TMATH.errors.invalidArgument;
}
mul(other) {
switch(typeof(other)) {
case 'number':
return new this.constructor(this._x * other, this._y * other, this._z * other);
case 'object':
if (other.type === this._type) {
return new this.constructor(this._x * other.x, this._y * other.y, this._z * other.z);
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
div(other) {
switch(typeof(other)) {
case 'number':
//if (other === 0) throw TMATH.errors.scalarDivisionByZero;
return new this.constructor(this._x / other, this._y / other, this._z / other);
case 'object':
if (other.type === this._type) {
//if (other.x === 0 || other.y === 0 || other.z === 0) throw TMATH.errors.pointwiseDivisionByZero;
return new this.constructor(this._x / other.x, this._y / other.y, this._z / other.z);
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
add(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._x + other.x, this._y + other.y, this._z + other.z);
} else throw TMATH.errors.invalidArgument;
}
sub(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._x - other.x, this._y - other.y, this._z - other.z);
} else throw TMATH.errors.invalidArgument;
}
dot(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this._x * other.x + this._y * other.y + this._z * other.z;
} else throw TMATH.errors.invalidArgument;
}
cross(other) {
if (typeof(other) === 'object' && other.type === this._type) {
let x = this._y * other.z - this._z * other.y;
let y = this._z * other.x - this._x * other.z;
let z = this._x * other.y - this._y * other.x;
return new Vec3(x, y, z);
} else throw TMATH.errors.invalidArgument;
}
};
class Vec4 {
constructor(x, y, z, w) {
this._x = x;
this._y = y;
this._z = z;
this._w = w;
this._type = this.constructor.name;
}
get type() { return this._type; }
get x() { return this._x; }
get y() { return this._y; }
get z() { return this._z; }
get w() { return this._w; }
get xyz() { return new Vec3(this._x, this._y, this._z) }
get length() { return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w); }
get lengthSqr() { return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w; }
get neg() { return new this.constructor(-this._x, -this._y, -this._z, -this._w); }
get normalized() { return this.div(this.length); }
get isZero() { return this._x === 0 && this._y === 0 && this._z === 0 && this._w === 0; }
get toString() { return `${ this._type }(${ this._x }, ${ this._y }, ${ this._z }, ${ this._w })`; }
equals(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this._x === other.x && this._y === other.y && this._z === other.z && this._w === other.w;
} else throw TMATH.errors.invalidArgument;
}
mul(other) {
switch(typeof(other)) {
case 'number':
return new this.constructor(this._x * other, this._y * other, this._z * other, this._w * other);
case 'object':
if (other.type === this._type) {
return new this.constructor(this._x * other.x, this._y * other.y, this._z * other.z, this._w * other.w);
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
div(other) {
switch(typeof(other)) {
case 'number':
//if (other === 0) throw TMATH.errors.scalarDivisionByZero;
return new this.constructor(this._x / other, this._y / other, this._z / other, this._w / other);
case 'object':
if (other.type === this._type) {
//if (other.x === 0 || other.y === 0 || other.z === 0 || other.w === 0) throw TMATH.errors.pointwiseDivisionByZero;
return new this.constructor(this._x / other.x, this._y / other.y, this._z / other.z, this._w / other.w);
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
add(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._x + other.x, this._y + other.y, this._z + other.z, this._w + other.w);
} else throw TMATH.errors.invalidArgument;
}
sub(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._x - other.x, this._y - other.y, this._z - other.z, this._w - other.w);
} else throw TMATH.errors.invalidArgument;
}
dot(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this._x * other.x + this._y * other.y + this._z * other.z + this._w * other.w;
} else throw TMATH.errors.invalidArgument;
}
};
class Mat2 {
constructor() {
this._type = this.constructor.name;
if (arguments.length === 0) {
this._col0 = new Vec2(1,0);
this._col1 = new Vec2(0,1);
} else if (typeof(arguments[0]) === 'number') {
if (arguments.length === 4 && Array.prototype.every(function(arg) { return typeof(arg) === 'number'; })) {
this._col0 = new Vec2(arguments[0], arguments[1]);
this._col1 = new Vec2(arguments[2], arguments[3]);
} else if (arguments.length === 1) {
this._col0 = new Vec2(arguments[0], arguments[0]);
this._col1 = new Vec2(arguments[0], arguments[0]);
} else {
throw TMATH.errors.invalidArgument;
}
} else if (typeof(arguments[0]) === 'object' && typeof(arguments[1]) === 'object' && arguments[0].type === 'Vec2' && arguments[1].type === 'Vec2') {
this._col0 = arguments[0];
this._col1 = arguments[1];
} else {
throw TMATH.errors.invalidArgument;
}
}
get type() { return this._type; }
get col0() { return this._col0; }
get col1() { return this._col1; }
get row0() { return new Vec2(this._col0.x, this._col1.x); }
get row1() { return new Vec2(this._col0.y, this._col1.y); }
get neg() { return new this.constructor(this.col0.neg, this.col1.neg); }
get isIdentity() {
return this._col0.x === 1 && this._col0.y === 0 &&
this._col1.x === 0 && this._col1.y === 1;
}
get toString() { return `(${ this._col0.x }\t\t${ this._col1.x })\n(${ this._col0.y }\t\t${ this._col1.y })`; }
get transpose() { return new this.constructor(new Vec2(this._col0.x, this._col1.x), new Vec2(this._col0.y, this._col1.y)); }
get inverse() {
return new Mat2(this._col1.y, -this._col0.y, -this._col1.x, this._col0.x).div(this.determinant);
}
get determinant() { return this._col0.x * this._col1.y - this._col0.y * this._col1.x; }
equals(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this.col0.equals(other.col0) && this.col1.equals(other.col1);
} else throw TMATH.errors.invalidArgument;
}
add(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._col0.add(other.col0), this._col1.add(other.col1));
} else throw TMATH.errors.invalidArgument;
}
sub(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._col0.sub(other.col0), this._col1.sub(other.col1));
} else throw TMATH.errors.invalidArgument;
}
mul(other) {
switch(typeof(other)) {
case 'number':
return new this.constructor(this._col0.mul(other), this._col1.mul(other));
case 'object':
if (other.type === this._col0.type) {
let t = this.transpose;
return new this._col0.constructor(t.col0.dot(other), t.col1.dot(other));
}
if (other.type === this._type) {
return new this.constructor(this.mul(other.col0), this.mul(other.col1));
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
div(other) {
switch(typeof(other)) {
case 'number':
//if (other === 0) throw TMATH.errors.scalarDivisionByZero;
return new this.constructor(this._col0.div(other), this._col1.div(other));
default: throw TMATH.errors.invalidArgument;
}
}
};
class Mat3 {
constructor() {
this._type = this.constructor.name;
if (arguments.length === 0) {
this._col0 = new Vec3(1,0,0);
this._col1 = new Vec3(0,1,0);
this._col2 = new Vec3(0,0,1);
} else if (typeof(arguments[0]) === 'number') {
if (arguments.length === 9 && Array.prototype.every(function(arg) { return typeof(arg) === 'number'; })) {
this._col0 = new Vec3(arguments[0], arguments[1], arguments[2]);
this._col1 = new Vec3(arguments[3], arguments[4], arguments[5]);
this._col2 = new Vec3(arguments[6], arguments[7], arguments[8]);
} else if (arguments.length === 1) {
this._col0 = new Vec3(arguments[0], arguments[0], arguments[0]);
this._col1 = new Vec3(arguments[0], arguments[0], arguments[0]);
this._col2 = new Vec3(arguments[0], arguments[0], arguments[0]);
} else {
throw TMATH.errors.invalidArgument;
}
} else if ( typeof(arguments[0]) === 'object' && typeof(arguments[1]) === 'object' && typeof(arguments[2]) === 'object' &&
arguments[0].type === 'Vec3' && arguments[1].type === 'Vec3' && arguments[2].type === 'Vec3') {
this._col0 = arguments[0];
this._col1 = arguments[1];
this._col2 = arguments[2];
} else {
throw TMATH.errors.invalidArgument;
}
}
get type() { return this._type; }
get col0() { return this._col0; }
get col1() { return this._col1; }
get col2() { return this._col2; }
get row0() { return new Vec3(this._col0.x, this._col1.x, this._col2.x); }
get row1() { return new Vec3(this._col0.y, this._col1.y, this._col2.y); }
get row2() { return new Vec3(this._col0.z, this._col1.z, this._col2.z); }
get neg() { return new this.constructor(this.col0.neg, this.col1.neg, this._col2.neg); }
get isIdentity() {
return this._col0.x === 1 && this._col0.y === 0 && this._col0.z === 0 &&
this._col1.x === 0 && this._col1.y === 1 && this._col1.z === 0 &&
this._col2.x === 0 && this._col2.y === 0 && this._col2.z === 1;
}
get toString() { return `(${ this._col0.x }\t\t${ this._col1.x }\t\t${ this._col2.x })\n(${ this._col0.y }\t\t${ this._col1.y }\t\t${ this._col2.y})\n(${ this._col0.z }\t\t${ this._col1.z }\t\t${ this._col2.z})`; }
get transpose() {
return new this.constructor(this._col0.x, this._col1.x, this._col2.x,
this._col0.y, this._col1.y, this._col2.y,
this._col0.z, this._col1.z, this._col2.z);
}
get inverse() {
function det2(a,b,c,d) { return a*d - b*c; }
return new Mat3(
det2( this._col1.y, this._col2.y, this._col1.z, this._col2.z ),
-det2( this._col1.x, this._col2.x, this._col1.z, this._col2.z ),
det2( this._col1.x, this._col2.x, this._col1.y, this._col2.y ),
-det2( this._col0.y, this._col2.y, this._col0.z, this._col2.z ),
det2( this._col0.x, this._col2.x, this._col0.z, this._col2.z ),
-det2( this._col0.x, this._col2.x, this._col0.y, this._col2.y ),
det2( this._col0.y, this._col1.y, this._col0.z, this._col1.z ),
-det2( this._col0.x, this._col1.x, this._col0.z, this._col1.z ),
det2( this._col0.x, this._col1.x, this._col0.y, this._col1.y ),
).transpose.div(this.determinant);
}
get determinant() {
return this._col0.x * this._col1.y * this._col2.z
+ this._col1.x * this._col2.y * this._col0.z
+ this._col2.x * this._col0.y * this._col1.z
- this._col2.x * this._col1.y * this._col0.z
- this._col1.x * this._col0.y * this._col2.z
- this._col0.x * this._col2.y * this._col1.z;
}
equals(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this.col0.equals(other.col0) && this.col1.equals(other.col1) && this._col2.equals(other.col2);
} else throw TMATH.errors.invalidArgument;
}
add(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._col0.add(other.col0), this._col1.add(other.col1), this._col2.add(other.col2));
} else throw TMATH.errors.invalidArgument;
}
sub(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._col0.sub(other.col0), this._col1.sub(other.col1), this._col2.sub(other.col2));
} else throw TMATH.errors.invalidArgument;
}
mul(other) {
switch(typeof(other)) {
case 'number':
return new this.constructor(this._col0.mul(other), this._col1.mul(other), this._col2.mul(other));
case 'object':
if (other.type === this._col0.type) {
let t = this.transpose;
return new this._col0.constructor(t.col0.dot(other), t.col1.dot(other), t.col2.dot(other));
}
if (other.type === this._type) {
return new this.constructor(this.mul(other.col0), this.mul(other.col1), this.mul(other.col2));
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
div(other) {
switch(typeof(other)) {
case 'number':
//if (other === 0) throw TMATH.errors.scalarDivisionByZero;
return new this.constructor(this._col0.div(other), this._col1.div(other), this._col2.div(other));
default: throw TMATH.errors.invalidArgument;
}
}
};
class Mat4 {
constructor() {
this._type = this.constructor.name;
if (arguments.length === 0) {
this._col0 = new Vec4(1,0,0,0);
this._col1 = new Vec4(0,1,0,0);
this._col2 = new Vec4(0,0,1,0);
this._col3 = new Vec4(0,0,0,1);
} else if (typeof(arguments[0]) === 'number') {
if (arguments.length === 16 && Array.prototype.every(function(arg) { return typeof(arg) === 'number'; })) {
this._col0 = new Vec4(arguments[0], arguments[1], arguments[2], arguments[3]);
this._col1 = new Vec4(arguments[4], arguments[5], arguments[6], arguments[7]);
this._col2 = new Vec4(arguments[8], arguments[9], arguments[10], arguments[11]);
this._col3 = new Vec4(arguments[12], arguments[13], arguments[14], arguments[15]);
} else if (arguments.length === 1) {
this._col0 = new Vec4(arguments[0], arguments[0], arguments[0], arguments[0]);
this._col1 = new Vec4(arguments[0], arguments[0], arguments[0], arguments[0]);
this._col2 = new Vec4(arguments[0], arguments[0], arguments[0], arguments[0]);
this._col3 = new Vec4(arguments[0], arguments[0], arguments[0], arguments[0]);
} else {
throw TMATH.errors.invalidArgument;
}
} else if ( typeof(arguments[0]) === 'object' && typeof(arguments[1]) === 'object' && typeof(arguments[2]) === 'object' && typeof(arguments[3] === 'object') &&
arguments[0].type === 'Vec4' && arguments[1].type === 'Vec4' && arguments[2].type === 'Vec4' && arguments[3].type === 'Vec4') {
this._col0 = arguments[0];
this._col1 = arguments[1];
this._col2 = arguments[2];
this._col3 = arguments[3];
} else {
throw TMATH.errors.invalidArgument;
}
}
get type() { return this._type; }
get col0() { return this._col0; }
get col1() { return this._col1; }
get col2() { return this._col2; }
get col3() { return this._col3; }
get row0() { return new Vec4(this._col0.x, this._col1.x, this._col2.x, this._col3.x); }
get row1() { return new Vec4(this._col0.y, this._col1.y, this._col2.y, this._col3.y); }
get row2() { return new Vec4(this._col0.z, this._col1.z, this._col2.z, this._col3.z); }
get row3() { return new Vec4(this._col0.w, this._col1.w, this._col2.w, this._col3.w); }
get toMat3() { return new Mat3(this._col0.xyz, this._col1.xyz, this._col2.xyz); }
get neg() { return new this.constructor(this.col0.neg, this.col1.neg, this._col2.neg, this._col3.neg); }
get isIdentity() {
return this._col0.x === 1 && this._col0.y === 0 && this._col0.z === 0 && this._col0.w === 0 &&
this._col1.x === 0 && this._col1.y === 1 && this._col1.z === 0 && this._col1.w === 0 &&
this._col2.x === 0 && this._col2.y === 0 && this._col2.z === 1 && this._col2.w === 0 &&
this._col3.x === 0 && this._col3.y === 0 && this._col3.z === 0 && this._col3.w === 1;
}
get toString() { return `(${ this._col0.x }\t\t${ this._col1.x }\t\t${ this._col2.x }\t\t${ this._col3.x })\n(${ this._col0.y }\t\t${ this._col1.y }\t\t${ this._col2.y }\t\t${ this._col3.y })\n(${ this._col0.z }\t\t${ this._col1.z }\t\t${ this._col2.z }\t\t${ this._col3.z })\n(${ this._col0.w }\t\t${ this._col1.w }\t\t${ this._col2.w }\t\t${ this._col3.w })`; }
get transpose() {
return new this.constructor(this._col0.x,this._col1.x,this._col2.x,this._col3.x,
this._col0.y,this._col1.y,this._col2.y,this._col3.y,
this._col0.z,this._col1.z,this._col2.z,this._col3.z,
this._col0.w,this._col1.w,this._col2.w,this._col3.w);
}
get inverse() {
function det3(a,b,c, d,e,f, g,h,i) { return new Mat3(a,d,g, b,e,h, c,f,i).determinant; }
return new Mat4(
det3( this._col1.y, this._col2.y, this._col3.y, this._col1.z, this._col2.z, this._col3.z, this._col1.w, this._col2.w, this._col3.w ),
-det3( this._col1.x, this._col2.x, this._col3.x, this._col1.z, this._col2.z, this._col3.z, this._col1.w, this._col2.w, this._col3.w ),
det3( this._col1.x, this._col2.x, this._col3.x, this._col1.y, this._col2.y, this._col3.y, this._col1.w, this._col2.w, this._col3.w ),
-det3( this._col1.x, this._col2.x, this._col3.x, this._col1.y, this._col2.y, this._col3.y, this._col1.z, this._col2.z, this._col3.z ),
-det3( this._col0.y, this._col2.y, this._col3.y, this._col0.z, this._col2.z, this._col3.z, this._col0.w, this._col2.w, this._col3.w ),
det3( this._col0.x, this._col2.x, this._col3.x, this._col0.z, this._col2.z, this._col3.z, this._col0.w, this._col2.w, this._col3.w ),
-det3( this._col0.x, this._col2.x, this._col3.x, this._col0.y, this._col2.y, this._col3.y, this._col0.w, this._col2.w, this._col3.w ),
det3( this._col0.x, this._col2.x, this._col3.x, this._col0.y, this._col2.y, this._col3.y, this._col0.z, this._col2.z, this._col3.z ),
det3( this._col0.y, this._col1.y, this._col3.y, this._col0.z, this._col1.z, this._col3.z, this._col0.w, this._col1.w, this._col3.w ),
-det3( this._col0.x, this._col1.x, this._col3.x, this._col0.z, this._col1.z, this._col3.z, this._col0.w, this._col1.w, this._col3.w ),
det3( this._col0.x, this._col1.x, this._col3.x, this._col0.y, this._col1.y, this._col3.y, this._col0.w, this._col1.w, this._col3.w ),
-det3( this._col0.x, this._col1.x, this._col3.x, this._col0.y, this._col1.y, this._col3.y, this._col0.z, this._col1.z, this._col3.z ),
-det3( this._col0.y, this._col1.y, this._col2.y, this._col0.z, this._col1.z, this._col2.z, this._col0.w, this._col1.w, this._col2.w ),
det3( this._col0.x, this._col1.x, this._col2.x, this._col0.z, this._col1.z, this._col2.z, this._col0.w, this._col1.w, this._col2.w ),
-det3( this._col0.x, this._col1.x, this._col2.x, this._col0.y, this._col1.y, this._col2.y, this._col0.w, this._col1.w, this._col2.w ),
det3( this._col0.x, this._col1.x, this._col2.x, this._col0.y, this._col1.y, this._col2.y, this._col0.z, this._col1.z, this._col2.z ),
).transpose.div(this.determinant);
}
get determinant() {
let v0 = new Vec3(this._col0.y, this._col0.z, this._col0.w);
let v1 = new Vec3(this._col1.y, this._col1.z, this._col1.w);
let v2 = new Vec3(this._col2.y, this._col2.z, this._col2.w);
let v3 = new Vec3(this._col3.y, this._col3.z, this._col3.w);
return this._col0.x * new Mat3(v1,v2,v3).determinant
- this._col1.x * new Mat3(v0,v2,v3).determinant
+ this._col2.x * new Mat3(v0,v1,v3).determinant
- this._col3.x * new Mat3(v0,v1,v2).determinant;
}
equals(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return this.col0.equals(other.col0) && this.col1.equals(other.col1) && this._col2.equals(other.col2) && this._col3.equals(other.col3);
} else throw TMATH.errors.invalidArgument;
}
add(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._col0.add(other.col0), this._col1.add(other.col1), this._col2.add(other.col2), this._col3.add(other.col3));
} else throw TMATH.errors.invalidArgument;
}
sub(other) {
if (typeof(other) === 'object' && other.type === this._type) {
return new this.constructor(this._col0.sub(other.col0), this._col1.sub(other.col1), this._col2.sub(other.col2), this._col3.sub(other.col3));
} else throw TMATH.errors.invalidArgument;
}
mul(other) {
switch(typeof(other)) {
case 'number':
return new this.constructor(this._col0.mul(other), this._col1.mul(other), this._col2.mul(other), this._col3.mul(other));
case 'object':
if (other.type === this._col0.type) {
let t = this.transpose;
return new this._col0.constructor(t.col0.dot(other), t.col1.dot(other), t.col2.dot(other), t.col3.dot(other));
}
if (other.type === this._type) {
return new this.constructor(this.mul(other.col0), this.mul(other.col1), this.mul(other.col2), this.mul(other.col3));
} else throw TMATH.errors.invalidArgument;
default: throw TMATH.errors.invalidArgument;
}
}
div(other) {
switch(typeof(other)) {
case 'number':
//if (other === 0) throw TMATH.errors.scalarDivisionByZero;
return new this.constructor(this._col0.div(other), this._col1.div(other), this._col2.div(other), this._col3.div(other));
default: throw TMATH.errors.invalidArgument;
}
}
};