-
-
Notifications
You must be signed in to change notification settings - Fork 574
/
Matrix2D.ts
737 lines (675 loc) · 16.9 KB
/
Matrix2D.ts
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import {DEG2RAD} from '../utils';
import {EPSILON, Type, WebGLConvertible} from './Type';
import {PossibleVector2, Vector2} from './Vector';
export type PossibleMatrix2D =
| Matrix2D
| DOMMatrix
| [number, number, number, number, number, number]
| [PossibleVector2, PossibleVector2, PossibleVector2]
| undefined;
/**
* A specialized 2x3 Matrix representing a 2D transformation.
*
* A Matrix2D contains six elements defined as
* [a, b,
* c, d,
* tx, ty]
*
* This is a shortcut for a 3x3 matrix of the form
* [a, b, 0,
* c, d, 0
* tx, ty, 1]
*
* Note that because a Matrix2D ignores the z-values of each component vectors,
* it does not satisfy all properties of a "real" 3x3 matrix.
*
* - A Matrix2D has no transpose
* - A(B + C) = AB + AC does not hold for a Matrix2D
* - (rA)^-1 = r^-1 A^-1, r != 0 does not hold for a Matrix2D
* - r(AB) = (rA)B = A(rB) does not hold for a Matrix2D
*/
export class Matrix2D implements Type, WebGLConvertible {
public static readonly symbol = Symbol.for(
'@motion-canvas/core/types/Matrix2D',
);
public readonly values: Float32Array = new Float32Array(6);
public static readonly identity: Matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0);
public static readonly zero: Matrix2D = new Matrix2D(0, 0, 0, 0, 0, 0);
public static fromRotation(angle: number): Matrix2D {
return Matrix2D.identity.rotate(angle);
}
public static fromTranslation(translation: PossibleVector2): Matrix2D {
return Matrix2D.identity.translate(new Vector2(translation));
}
public static fromScaling(scale: PossibleVector2): Matrix2D {
return Matrix2D.identity.scale(new Vector2(scale));
}
public get x(): Vector2 {
return new Vector2(this.values[0], this.values[1]);
}
public get y(): Vector2 {
return new Vector2(this.values[2], this.values[3]);
}
public get scaleX(): number {
return this.values[0];
}
public set scaleX(value: number) {
this.values[0] = this.x.normalized.scale(value).x;
}
public get skewX(): number {
return this.values[1];
}
public set skewX(value: number) {
this.values[1] = value;
}
public get scaleY(): number {
return this.values[3];
}
public set scaleY(value: number) {
this.values[3] = this.y.normalized.scale(value).y;
}
public get skewY(): number {
return this.values[2];
}
public set skewY(value: number) {
this.values[2] = value;
}
public get translateX(): number {
return this.values[4];
}
public set translateX(value: number) {
this.values[4] = value;
}
public get translateY(): number {
return this.values[5];
}
public set translateY(value: number) {
this.values[5] = value;
}
public get rotation(): number {
return Vector2.degrees(this.values[0], this.values[1]);
}
public set rotation(angle: number) {
const result = this.rotate(angle - this.rotation);
this.values[0] = result.values[0];
this.values[1] = result.values[1];
this.values[2] = result.values[2];
this.values[3] = result.values[3];
}
public get translation(): Vector2 {
return new Vector2(this.values[4], this.values[5]);
}
public set translation(translation: PossibleVector2) {
const vec = new Vector2(translation);
this.values[4] = vec.x;
this.values[5] = vec.y;
}
public get scaling(): Vector2 {
return new Vector2(this.values[0], this.values[3]);
}
public set scaling(value: PossibleVector2) {
const scale = new Vector2(value);
const x = new Vector2(this.values[0], this.values[1]).normalized;
const y = new Vector2(this.values[2], this.values[3]).normalized;
this.values[0] = x.x * scale.x;
this.values[1] = x.y * scale.y;
this.values[2] = y.x * scale.x;
this.values[3] = y.y * scale.y;
}
/**
* Get the inverse of the matrix.
*
* @remarks
* If the matrix is not invertible, i.e. its determinant is `0`, this will
* return `null`, instead.
*
* @example
* ```ts
* const matrix = new Matrix2D(
* [1, 2],
* [3, 4],
* [5, 6],
* );
*
* const inverse = matrix.inverse;
* // => Matrix2D(
* // [-2, 1],
* // [1.5, -0.5],
* // [1, -2],
* // )
* ```
*/
public get inverse(): Matrix2D | null {
const aa = this.values[0],
ab = this.values[1],
ac = this.values[2],
ad = this.values[3];
const atx = this.values[4],
aty = this.values[5];
let det = aa * ad - ab * ac;
if (!det) {
return null;
}
det = 1.0 / det;
return new Matrix2D(
ad * det,
-ab * det,
-ac * det,
aa * det,
(ac * aty - ad * atx) * det,
(ab * atx - aa * aty) * det,
);
}
/**
* Get the determinant of the matrix.
*/
public get determinant(): number {
return this.values[0] * this.values[3] - this.values[1] * this.values[2];
}
public get domMatrix(): DOMMatrix {
return new DOMMatrix([
this.values[0],
this.values[1],
this.values[2],
this.values[3],
this.values[4],
this.values[5],
]);
}
public constructor();
public constructor(matrix: PossibleMatrix2D);
public constructor(
x: PossibleVector2,
y: PossibleVector2,
z: PossibleVector2,
);
public constructor(
a: number,
b: number,
c: number,
d: number,
tx: number,
ty: number,
);
public constructor(
a?: PossibleMatrix2D | PossibleVector2,
b?: PossibleVector2,
c?: PossibleVector2,
d?: number,
tx?: number,
ty?: number,
) {
if (arguments.length === 0) {
this.values = new Float32Array([1, 0, 0, 1, 0, 0]);
return;
}
if (arguments.length === 6) {
this.values[0] = a as number;
this.values[1] = b as number;
this.values[2] = c as number;
this.values[3] = d as number;
this.values[4] = tx as number;
this.values[5] = ty as number;
return;
}
if (a instanceof DOMMatrix) {
this.values[0] = a.m11;
this.values[1] = a.m12;
this.values[2] = a.m21;
this.values[3] = a.m22;
this.values[4] = a.m41;
this.values[5] = a.m42;
return;
}
if (a instanceof Matrix2D) {
this.values = a.values;
return;
}
if (Array.isArray(a)) {
if (a.length === 2) {
this.values[0] = a[0];
this.values[1] = a[1];
this.values[2] = (b as number[])[0];
this.values[3] = (b as number[])[1];
this.values[4] = (c as number[])[0];
this.values[5] = (c as number[])[1];
return;
}
if (a.length === 3) {
const x = new Vector2(a[0]);
const y = new Vector2(a[1]);
const z = new Vector2(a[2]);
this.values[0] = x.x;
this.values[1] = x.y;
this.values[2] = y.x;
this.values[3] = y.y;
this.values[4] = z.x;
this.values[5] = z.y;
return;
}
this.values[0] = a[0];
this.values[1] = a[1];
this.values[2] = a[2];
this.values[3] = a[3];
this.values[4] = a[4];
this.values[5] = a[5];
return;
}
const x = new Vector2(a as PossibleVector2);
const y = new Vector2(b);
const z = new Vector2(c);
this.values[0] = x.x;
this.values[1] = x.y;
this.values[2] = y.x;
this.values[3] = y.y;
this.values[4] = z.x;
this.values[5] = z.y;
}
/**
* Get the nth component vector of the matrix. Only defined for 0, 1, and 2.
*
* @example
* ```ts
* const matrix = new Matrix2D(
* [1, 0],
* [0, 0],
* [1, 0],
* );
*
* const x = matrix.column(0);
* // Vector2(1, 0)
*
* const y = matrix.column(1);
* // Vector2(0, 0)
*
* const z = matrix.column(1);
* // Vector2(1, 0)
* ```
*
* @param index - The index of the component vector to retrieve.
*/
public column(index: number): Vector2 {
return new Vector2(this.values[index * 2], this.values[index * 2 + 1]);
}
/**
* Returns the nth row of the matrix. Only defined for 0 and 1.
*
* @example
* ```ts
* const matrix = new Matrix2D(
* [1, 0],
* [0, 0],
* [1, 0],
* );
*
* const firstRow = matrix.column(0);
* // [1, 0, 1]
*
* const secondRow = matrix.column(1);
* // [0, 0, 0]
* ```
*
* @param index - The index of the row to retrieve.
*/
public row(index: number): [number, number, number] {
return [this.values[index], this.values[index + 2], this.values[index + 4]];
}
/**
* Returns the matrix product of this matrix with the provided matrix.
*
* @remarks
* This method returns a new matrix representing the result of the
* computation. It will not modify the source matrix.
*
* @example
* ```ts
* const a = new Matrix2D(
* [1, 2],
* [0, 1],
* [1, 1],
* );
* const b = new Matrix2D(
* [2, 1],
* [1, 1],
* [1, 1],
* );
*
* const result = a.mul(b);
* // => Matrix2D(
* // [2, 5],
* // [1, 3],
* // [2, 4],
* // )
* ```
*
* @param other - The matrix to multiply with
*/
public mul(other: Matrix2D): Matrix2D {
const a0 = this.values[0],
a1 = this.values[1],
a2 = this.values[2],
a3 = this.values[3],
a4 = this.values[4],
a5 = this.values[5];
const b0 = other.values[0],
b1 = other.values[1],
b2 = other.values[2],
b3 = other.values[3],
b4 = other.values[4],
b5 = other.values[5];
return new Matrix2D(
a0 * b0 + a2 * b1,
a1 * b0 + a3 * b1,
a0 * b2 + a2 * b3,
a1 * b2 + a3 * b3,
a0 * b4 + a2 * b5 + a4,
a1 * b4 + a3 * b5 + a5,
);
}
/**
* Rotate the matrix by the provided angle. By default, the angle is
* provided in degrees.
*
* @remarks
* This method returns a new matrix representing the result of the
* computation. It will not modify the source matrix.
*
* @example
* ```ts
* const a = new Matrix2D(
* [1, 2],
* [3, 4],
* [5, 6],
* );
*
* const result = a.rotate(90);
* // => Matrix2D(
* // [3, 4],
* // [-1, -2],
* // [5, 6],
* // )
*
* // Provide the angle in radians
* const result = a.rotate(Math.PI * 0.5, true);
* // => Matrix2D(
* // [3, 4],
* // [-1, -2],
* // [5, 6],
* // )
* ```
*
* @param angle - The angle by which to rotate the matrix.
* @param degrees - Whether the angle is provided in degrees.
*/
public rotate(angle: number, degrees = true): Matrix2D {
if (degrees) {
angle *= DEG2RAD;
}
const a0 = this.values[0],
a1 = this.values[1],
a2 = this.values[2],
a3 = this.values[3],
a4 = this.values[4],
a5 = this.values[5];
const s = Math.sin(angle);
const c = Math.cos(angle);
return new Matrix2D(
a0 * c + a2 * s,
a1 * c + a3 * s,
a0 * -s + a2 * c,
a1 * -s + a3 * c,
a4,
a5,
);
}
/**
* Scale the x and y component vectors of the matrix.
*
* @remarks
* If `vec` is provided as a vector, the x and y component vectors of the
* matrix will be scaled by the x and y parts of the vector, respectively.
*
* If `vec` is provided as a scalar, the x and y component vectors will be
* scaled uniformly by this factor.
*
* This method returns a new matrix representing the result of the
* computation. It will not modify the source matrix.
*
* @example
* ```ts
* const matrix = new Matrix2D(
* [1, 2],
* [3, 4],
* [5, 6],
* );
*
* const result1 = matrix.scale([2, 3]);
* // => new Matrix2D(
* // [2, 4],
* // [9, 12],
* // [5, 6],
* // )
*
* const result2 = matrix.scale(2);
* // => new Matrix2D(
* // [2, 4],
* // [6, 8],
* // [5, 6],
* // )
* ```
*
* @param vec - The factor by which to scale the matrix
*/
public scale(vec: PossibleVector2): Matrix2D {
const v = new Vector2(vec);
return new Matrix2D(
this.values[0] * v.x,
this.values[1] * v.x,
this.values[2] * v.y,
this.values[3] * v.y,
this.values[4],
this.values[5],
);
}
/**
* Multiply each value of the matrix by a scalar.
*
* * @example
* ```ts
* const matrix = new Matrix2D(
* [1, 2],
* [3, 4],
* [5, 6],
* );
*
* const result1 = matrix.mulScalar(2);
* // => new Matrix2D(
* // [2, 4],
* // [6, 8],
* // [10, 12],
* // )
* ```
*
* @param s - The value by which to scale each term
*/
public mulScalar(s: number): Matrix2D {
return new Matrix2D(
this.values[0] * s,
this.values[1] * s,
this.values[2] * s,
this.values[3] * s,
this.values[4] * s,
this.values[5] * s,
);
}
/**
* Translate the matrix by the dimensions of the provided vector.
*
* @remarks
* If `vec` is provided as a scalar, matrix will be translated uniformly
* by this factor.
*
* This method returns a new matrix representing the result of the
* computation. It will not modify the source matrix.
*
* @example
* ```ts
* const matrix = new Matrix2D(
* [1, 2],
* [3, 4],
* [5, 6],
* );
*
* const result1 = matrix.translate([2, 3]);
* // => new Matrix2D(
* // [1, 2],
* // [3, 4],
* // [16, 22],
* // )
*
* const result2 = matrix.translate(2);
* // => new Matrix2D(
* // [1, 2],
* // [3, 4],
* // [13, 18],
* // )
* ```
*
* @param vec - The vector by which to translate the matrix
*/
public translate(vec: PossibleVector2): Matrix2D {
const v = new Vector2(vec);
return new Matrix2D(
this.values[0],
this.values[1],
this.values[2],
this.values[3],
this.values[0] * v.x + this.values[2] * v.y + this.values[4],
this.values[1] * v.x + this.values[3] * v.y + this.values[5],
);
}
/**
* Add the provided matrix to this matrix.
*
* @remarks
* This method returns a new matrix representing the result of the
* computation. It will not modify the source matrix.
*
* @example
* ```ts
* const a = new Matrix2D(
* [1, 2],
* [3, 4],
* [5, 6],
* );
* const a = new Matrix2D(
* [7, 8],
* [9, 10],
* [11, 12],
* );
*
* const result = a.add(b);
* // => Matrix2D(
* // [8, 10],
* // [12, 14],
* // [16, 18],
* // )
* ```
*
* @param other - The matrix to add
*/
public add(other: Matrix2D): Matrix2D {
return new Matrix2D(
this.values[0] + other.values[0],
this.values[1] + other.values[1],
this.values[2] + other.values[2],
this.values[3] + other.values[3],
this.values[4] + other.values[4],
this.values[5] + other.values[5],
);
}
/**
* Subtract the provided matrix from this matrix.
*
* @remarks
* This method returns a new matrix representing the result of the
* computation. It will not modify the source matrix.
*
* @example
* ```ts
* const a = new Matrix2D(
* [1, 2],
* [3, 4],
* [5, 6],
* );
* const a = new Matrix2D(
* [7, 8],
* [9, 10],
* [11, 12],
* );
*
* const result = a.sub(b);
* // => Matrix2D(
* // [-6, -6],
* // [-6, -6],
* // [-6, -6],
* // )
* ```
*
* @param other - The matrix to subract
*/
public sub(other: Matrix2D): Matrix2D {
return new Matrix2D(
this.values[0] - other.values[0],
this.values[1] - other.values[1],
this.values[2] - other.values[2],
this.values[3] - other.values[3],
this.values[4] - other.values[4],
this.values[5] - other.values[5],
);
}
public toSymbol(): symbol {
return Matrix2D.symbol;
}
public toUniform(
gl: WebGL2RenderingContext,
location: WebGLUniformLocation,
): void {
gl.uniformMatrix3fv(location, false, [
this.values[0],
this.values[1],
0,
this.values[2],
this.values[3],
0,
this.values[4],
this.values[5],
1,
]);
}
public equals(other: Matrix2D, threshold: number = EPSILON): boolean {
return (
Math.abs(this.values[0] - other.values[0]) <=
threshold + Number.EPSILON &&
Math.abs(this.values[1] - other.values[1]) <=
threshold + Number.EPSILON &&
Math.abs(this.values[2] - other.values[2]) <=
threshold + Number.EPSILON &&
Math.abs(this.values[3] - other.values[3]) <=
threshold + Number.EPSILON &&
Math.abs(this.values[4] - other.values[4]) <=
threshold + Number.EPSILON &&
Math.abs(this.values[5] - other.values[5]) <= threshold + Number.EPSILON
);
}
public exactlyEquals(other: Matrix2D): boolean {
return (
this.values[0] === other.values[0] &&
this.values[1] === other.values[1] &&
this.values[2] === other.values[2] &&
this.values[3] === other.values[3] &&
this.values[4] === other.values[4] &&
this.values[5] === other.values[5]
);
}
}