-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
matrix.js
604 lines (478 loc) · 16 KB
/
matrix.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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import { NumArray, toFixed, setMatrix } from './utils/math.js';
import { Events } from './events.js';
// Constants
const cos = Math.cos, sin = Math.sin, tan = Math.tan;
const array = [];
/**
* @name Two.Matrix
* @class
* @param {Number} [a=1] - The value for element at the first column and first row.
* @param {Number} [b=0] - The value for element at the second column and first row.
* @param {Number} [c=0] - The value for element at the third column and first row.
* @param {Number} [d=0] - The value for element at the first column and second row.
* @param {Number} [e=1] - The value for element at the second column and second row.
* @param {Number} [f=0] - The value for element at the third column and second row.
* @param {Number} [g=0] - The value for element at the first column and third row.
* @param {Number} [h=0] - The value for element at the second column and third row.
* @param {Number} [i=1] - The value for element at the third column and third row.
* @description A class to store 3 x 3 transformation matrix information. In addition to storing data `Two.Matrix` has suped up methods for commonplace mathematical operations.
* @nota-bene Order is based on how to construct transformation strings for the browser.
*/
export class Matrix extends Events {
/**
* @name Two.Matrix#elements
* @property {Number[]} - The underlying data stored as an array.
*/
elements = new NumArray(9);
/**
* @name Two.Matrix#manual
* @property {Boolean} - Determines whether Two.js automatically calculates the values for the matrix or if the developer intends to manage the matrix.
* @nota-bene - Setting to `true` nullifies {@link Two.Shape#translation}, {@link Two.Shape#rotation}, and {@link Two.Shape#scale}.
*/
manual = false;
constructor(a, b, c, d, e, f) {
super();
let elements = a;
if (!Array.isArray(elements)) {
elements = Array.prototype.slice.call(arguments);
}
// initialize the elements with default values.
this.identity();
if (elements.length > 0) {
this.set(elements);
}
}
//
/**
* @name Two.Matrix.Identity
* @property {Number[]} - A stored reference to the default value of a 3 x 3 matrix.
*/
static Identity = [
1, 0, 0,
0, 1, 0,
0, 0, 1
];
/**
* @name Two.Matrix.Multiply
* @function
* @param {Number[]} A
* @param {Number[]} B
* @param {Number[]} [C] - An optional matrix to apply the multiplication to.
* @returns {Number[]} - If an optional `C` matrix isn't passed then a new one is created and returned.
* @description Multiply two matrices together and return the result.
*/
static Multiply(A, B, C) {
if (B.length <= 3) { // Multiply Vector
const e = A;
let x, y, z;
const a = B[0] || 0,
b = B[1] || 0,
c = B[2] || 0;
// Go down rows first
// a, d, g, b, e, h, c, f, i
x = e[0] * a + e[1] * b + e[2] * c;
y = e[3] * a + e[4] * b + e[5] * c;
z = e[6] * a + e[7] * b + e[8] * c;
return [x, y, z];
}
const A0 = A[0], A1 = A[1], A2 = A[2];
const A3 = A[3], A4 = A[4], A5 = A[5];
const A6 = A[6], A7 = A[7], A8 = A[8];
const B0 = B[0], B1 = B[1], B2 = B[2];
const B3 = B[3], B4 = B[4], B5 = B[5];
const B6 = B[6], B7 = B[7], B8 = B[8];
C = C || new NumArray(9);
C[0] = A0 * B0 + A1 * B3 + A2 * B6;
C[1] = A0 * B1 + A1 * B4 + A2 * B7;
C[2] = A0 * B2 + A1 * B5 + A2 * B8;
C[3] = A3 * B0 + A4 * B3 + A5 * B6;
C[4] = A3 * B1 + A4 * B4 + A5 * B7;
C[5] = A3 * B2 + A4 * B5 + A5 * B8;
C[6] = A6 * B0 + A7 * B3 + A8 * B6;
C[7] = A6 * B1 + A7 * B4 + A8 * B7;
C[8] = A6 * B2 + A7 * B5 + A8 * B8;
return C;
}
/**
* @name Two.Matrix#set
* @function
* @param {Number} a - The value for element at the first column and first row.
* @param {Number} b - The value for element at the second column and first row.
* @param {Number} c - The value for element at the third column and first row.
* @param {Number} d - The value for element at the first column and second row.
* @param {Number} e - The value for element at the second column and second row.
* @param {Number} f - The value for element at the third column and second row.
* @param {Number} g - The value for element at the first column and third row.
* @param {Number} h - The value for element at the second column and third row.
* @param {Number} i - The value for element at the third column and third row.
* @description Set an array of values onto the matrix. Order described in {@link Two.Matrix}.
*/
/**
* @name Two.Matrix#set
* @function
* @param {Number[]} a - The array of elements to apply.
* @description Set an array of values onto the matrix. Order described in {@link Two.Matrix}.
*/
set(a, b, c, d, e, f, g, h, i) {
if (typeof b === 'undefined') {
const elements = a;
a = elements[0];
b = elements[1];
c = elements[2];
d = elements[3];
e = elements[4];
f = elements[5];
g = elements[6];
h = elements[7];
i = elements[8];
}
this.elements[0] = a;
this.elements[1] = b;
this.elements[2] = c;
this.elements[3] = d;
this.elements[4] = e;
this.elements[5] = f;
this.elements[6] = g;
this.elements[7] = h;
this.elements[8] = i;
return this.trigger(Events.Types.change);
}
/**
* @name Two.Matrix#copy
* @function
* @description Copy the matrix of one to the current instance.
*/
copy(m) {
this.elements[0] = m.elements[0];
this.elements[1] = m.elements[1];
this.elements[2] = m.elements[2];
this.elements[3] = m.elements[3];
this.elements[4] = m.elements[4];
this.elements[5] = m.elements[5];
this.elements[6] = m.elements[6];
this.elements[7] = m.elements[7];
this.elements[8] = m.elements[8];
this.manual = m.manual;
return this.trigger(Events.Types.change);
}
/**
* @name Two.Matrix#identity
* @function
* @description Turn matrix to the identity, like resetting.
*/
identity() {
this.elements[0] = Matrix.Identity[0];
this.elements[1] = Matrix.Identity[1];
this.elements[2] = Matrix.Identity[2];
this.elements[3] = Matrix.Identity[3];
this.elements[4] = Matrix.Identity[4];
this.elements[5] = Matrix.Identity[5];
this.elements[6] = Matrix.Identity[6];
this.elements[7] = Matrix.Identity[7];
this.elements[8] = Matrix.Identity[8];
return this.trigger(Events.Types.change);
}
/**
* @name Two.Matrix#multiply
* @function
* @param {Number} a - The scalar to be multiplied.
* @description Multiply all components of the matrix against a single scalar value.
* @overloaded
*/
/**
* @name Two.Matrix#multiply
* @function
* @param {Number} a - The x component to be multiplied.
* @param {Number} b - The y component to be multiplied.
* @param {Number} c - The z component to be multiplied.
* @description Multiply all components of a matrix against a 3 component vector.
* @overloaded
*/
/**
* @name Two.Matrix#multiply
* @function
* @param {Number} a - The value at the first column and first row of the matrix to be multiplied.
* @param {Number} b - The value at the second column and first row of the matrix to be multiplied.
* @param {Number} c - The value at the third column and first row of the matrix to be multiplied.
* @param {Number} d - The value at the first column and second row of the matrix to be multiplied.
* @param {Number} e - The value at the second column and second row of the matrix to be multiplied.
* @param {Number} f - The value at the third column and second row of the matrix to be multiplied.
* @param {Number} g - The value at the first column and third row of the matrix to be multiplied.
* @param {Number} h - The value at the second column and third row of the matrix to be multiplied.
* @param {Number} i - The value at the third column and third row of the matrix to be multiplied.
* @description Multiply all components of a matrix against another matrix.
* @overloaded
*/
multiply(a, b, c, d, e, f, g, h, i) {
// Multiply scalar
if (typeof b === 'undefined') {
this.elements[0] *= a;
this.elements[1] *= a;
this.elements[2] *= a;
this.elements[3] *= a;
this.elements[4] *= a;
this.elements[5] *= a;
this.elements[6] *= a;
this.elements[7] *= a;
this.elements[8] *= a;
return this.trigger(Events.Types.change);
}
if (typeof c === 'undefined') {
c = 1;
}
if (typeof d === 'undefined') { // Multiply Vector
a = a || 0;
b = b || 0;
c = c || 0;
e = this.elements;
// Go down rows first
// a, d, g, b, e, h, c, f, i
const x = e[0] * a + e[1] * b + e[2] * c;
const y = e[3] * a + e[4] * b + e[5] * c;
const z = e[6] * a + e[7] * b + e[8] * c;
return [x, y, z];
}
// Multiple matrix
const A = this.elements;
const B = [a, b, c, d, e, f, g, h, i];
const A0 = A[0], A1 = A[1], A2 = A[2];
const A3 = A[3], A4 = A[4], A5 = A[5];
const A6 = A[6], A7 = A[7], A8 = A[8];
const B0 = B[0], B1 = B[1], B2 = B[2];
const B3 = B[3], B4 = B[4], B5 = B[5];
const B6 = B[6], B7 = B[7], B8 = B[8];
this.elements[0] = A0 * B0 + A1 * B3 + A2 * B6;
this.elements[1] = A0 * B1 + A1 * B4 + A2 * B7;
this.elements[2] = A0 * B2 + A1 * B5 + A2 * B8;
this.elements[3] = A3 * B0 + A4 * B3 + A5 * B6;
this.elements[4] = A3 * B1 + A4 * B4 + A5 * B7;
this.elements[5] = A3 * B2 + A4 * B5 + A5 * B8;
this.elements[6] = A6 * B0 + A7 * B3 + A8 * B6;
this.elements[7] = A6 * B1 + A7 * B4 + A8 * B7;
this.elements[8] = A6 * B2 + A7 * B5 + A8 * B8;
return this.trigger(Events.Types.change);
}
/**
* @name Two.Matrix#inverse
* @function
* @param {Two.Matrix} [out] - The optional matrix to apply the inversion to.
* @description Return an inverted version of the matrix. If no optional one is passed a new matrix is created and returned.
*/
inverse(out) {
const a = this.elements;
out = out || new Matrix();
const a00 = a[0], a01 = a[1], a02 = a[2];
const a10 = a[3], a11 = a[4], a12 = a[5];
const a20 = a[6], a21 = a[7], a22 = a[8];
const b01 = a22 * a11 - a12 * a21;
const b11 = -a22 * a10 + a12 * a20;
const b21 = a21 * a10 - a11 * a20;
// Calculate the determinant
let det = a00 * b01 + a01 * b11 + a02 * b21;
if (!det) {
return null;
}
det = 1.0 / det;
out.elements[0] = b01 * det;
out.elements[1] = (-a22 * a01 + a02 * a21) * det;
out.elements[2] = (a12 * a01 - a02 * a11) * det;
out.elements[3] = b11 * det;
out.elements[4] = (a22 * a00 - a02 * a20) * det;
out.elements[5] = (-a12 * a00 + a02 * a10) * det;
out.elements[6] = b21 * det;
out.elements[7] = (-a21 * a00 + a01 * a20) * det;
out.elements[8] = (a11 * a00 - a01 * a10) * det;
return out;
}
/**
* @name Two.Matrix#scale
* @function
* @param {Number} scale - The one dimensional scale to apply to the matrix.
* @description Uniformly scale the transformation matrix.
*/
/**
* @name Two.Matrix#scale
* @function
* @param {Number} sx - The horizontal scale factor.
* @param {Number} sy - The vertical scale factor
* @description Scale the transformation matrix in two dimensions.
*/
scale(sx, sy) {
const l = arguments.length;
if (l <= 1) {
sy = sx;
}
return this.multiply(sx, 0, 0, 0, sy, 0, 0, 0, 1);
}
/**
* @name Two.Matrix#rotate
* @function
* @param {Number} Number - The amount to rotate in Number.
* @description Rotate the matrix.
*/
rotate(Number) {
const c = cos(Number);
const s = sin(Number);
return this.multiply(c, -s, 0, s, c, 0, 0, 0, 1);
}
/**
* @name Two.Matrix#translate
* @function
* @param {Number} x - The horizontal translation value to apply.
* @param {Number} y - The vertical translation value to apply.
* @description Translate the matrix.
*/
translate(x, y) {
return this.multiply(1, 0, x, 0, 1, y, 0, 0, 1);
}
/**
* @name Two.Matrix#skewX
* @function
* @param {Number} Number - The amount to skew in Number.
* @description Skew the matrix by an angle in the x axis direction.
*/
skewX(Number) {
const a = tan(Number);
return this.multiply(1, a, 0, 0, 1, 0, 0, 0, 1);
}
/**
* @name Two.Matrix#skewY
* @function
* @param {Number} Number - The amount to skew in Number.
* @description Skew the matrix by an angle in the y axis direction.
*/
skewY(Number) {
const a = tan(Number);
return this.multiply(1, 0, 0, a, 1, 0, 0, 0, 1);
}
/**
* @name Two.Matrix#toString
* @function
* @param {Boolean} [fullMatrix=false] - Return the full 9 elements of the matrix or just 6 for 2D transformations.
* @returns {String} - The transformation matrix as a 6 component string separated by spaces.
* @description Create a transform string. Used for the Two.js rendering APIs.
*/
toString(fullMatrix) {
array.length = 0;
this.toTransformArray(fullMatrix, array);
return array.map(toFixed).join(' ');
}
/**
* @name Two.Matrix#toTransformArray
* @function
* @param {Boolean} [fullMatrix=false] - Return the full 9 elements of the matrix or just 6 in the format for 2D transformations.
* @param {Number[]} [output] - An array empty or otherwise to apply the values to.
* @description Create a transform array. Used for the Two.js rendering APIs.
*/
toTransformArray(fullMatrix, output) {
const elements = this.elements;
const hasOutput = !!output;
const a = elements[0];
const b = elements[1];
const c = elements[2];
const d = elements[3];
const e = elements[4];
const f = elements[5];
if (fullMatrix) {
const g = elements[6];
const h = elements[7];
const i = elements[8];
if (hasOutput) {
output[0] = a;
output[1] = d;
output[2] = g;
output[3] = b;
output[4] = e;
output[5] = h;
output[6] = c;
output[7] = f;
output[8] = i;
return;
}
return [
a, d, g, b, e, h, c, f, i
];
}
if (hasOutput) {
output[0] = a;
output[1] = d;
output[2] = b;
output[3] = e;
output[4] = c;
output[5] = f;
return;
}
return [
a, d, b, e, c, f // Specific format see LN:19
];
}
/**
* @name Two.Matrix#toArray
* @function
* @param {Boolean} [fullMatrix=false] - Return the full 9 elements of the matrix or just 6 for 2D transformations.
* @param {Number[]} [output] - An array empty or otherwise to apply the values to.
* @description Create a transform array. Used for the Two.js rendering APIs.
*/
toArray(fullMatrix, output) {
const elements = this.elements;
const hasOutput = !!output;
const a = elements[0];
const b = elements[1];
const c = elements[2];
const d = elements[3];
const e = elements[4];
const f = elements[5];
if (fullMatrix) {
const g = elements[6];
const h = elements[7];
const i = elements[8];
if (hasOutput) {
output[0] = a;
output[1] = b;
output[2] = c;
output[3] = d;
output[4] = e;
output[5] = f;
output[6] = g;
output[7] = h;
output[8] = i;
return;
}
return [
a, b, c, d, e, f, g, h, i
];
}
if (hasOutput) {
output[0] = a;
output[1] = b;
output[2] = c;
output[3] = d;
output[4] = e;
output[5] = f;
return;
}
return [
a, b, c, d, e, f
];
}
/**
* @name Two.Matrix#toObject
* @function
* @description Create a JSON compatible object that represents information of the matrix.
*/
toObject() {
return {
elements: this.toArray(true),
manual: !!this.manual
};
}
/**
* @name Two.Matrix#clone
* @function
* @description Clone the current matrix.
*/
clone() {
return new Matrix().copy(this);
}
}
setMatrix(Matrix);