@lakuna/umath 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/README.md +41 -1
  2. package/dist/algorithms/combinations.d.ts +2 -0
  3. package/dist/algorithms/combinations.d.ts.map +1 -0
  4. package/dist/algorithms/combinations.js +5 -0
  5. package/dist/algorithms/combinations.js.map +1 -0
  6. package/dist/algorithms/degreesToRadians.d.ts +2 -0
  7. package/dist/algorithms/degreesToRadians.d.ts.map +1 -0
  8. package/dist/algorithms/degreesToRadians.js +4 -0
  9. package/dist/algorithms/degreesToRadians.js.map +1 -0
  10. package/dist/algorithms/factorial.d.ts +2 -0
  11. package/dist/algorithms/factorial.d.ts.map +1 -0
  12. package/dist/algorithms/factorial.js +4 -0
  13. package/dist/algorithms/factorial.js.map +1 -0
  14. package/dist/algorithms/fibonacci.d.ts +2 -0
  15. package/dist/algorithms/fibonacci.d.ts.map +1 -0
  16. package/dist/algorithms/fibonacci.js +9 -0
  17. package/dist/algorithms/fibonacci.js.map +1 -0
  18. package/dist/algorithms/greatestCommonDivisor.d.ts +3 -0
  19. package/dist/algorithms/greatestCommonDivisor.d.ts.map +1 -0
  20. package/dist/algorithms/greatestCommonDivisor.js +13 -0
  21. package/dist/algorithms/greatestCommonDivisor.js.map +1 -0
  22. package/dist/algorithms/isPrime.d.ts +2 -0
  23. package/dist/algorithms/isPrime.d.ts.map +1 -0
  24. package/dist/algorithms/isPrime.js +12 -0
  25. package/dist/algorithms/isPrime.js.map +1 -0
  26. package/dist/algorithms/permutations.d.ts +2 -0
  27. package/dist/algorithms/permutations.d.ts.map +1 -0
  28. package/dist/algorithms/permutations.js +5 -0
  29. package/dist/algorithms/permutations.js.map +1 -0
  30. package/dist/algorithms/primeFactorization.d.ts +2 -0
  31. package/dist/algorithms/primeFactorization.d.ts.map +1 -0
  32. package/dist/algorithms/primeFactorization.js +15 -0
  33. package/dist/algorithms/primeFactorization.js.map +1 -0
  34. package/dist/algorithms/radiansToDegrees.d.ts +2 -0
  35. package/dist/algorithms/radiansToDegrees.d.ts.map +1 -0
  36. package/dist/algorithms/radiansToDegrees.js +4 -0
  37. package/dist/algorithms/radiansToDegrees.js.map +1 -0
  38. package/dist/algorithms/summation.d.ts +2 -0
  39. package/dist/algorithms/summation.d.ts.map +1 -0
  40. package/dist/algorithms/summation.js +7 -0
  41. package/dist/algorithms/summation.js.map +1 -0
  42. package/dist/index.d.ts +28 -1
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +22 -1
  45. package/dist/index.js.map +1 -1
  46. package/dist/linear/DualQuaternion.d.ts +89 -0
  47. package/dist/linear/DualQuaternion.d.ts.map +1 -0
  48. package/dist/linear/DualQuaternion.js +530 -0
  49. package/dist/linear/DualQuaternion.js.map +1 -0
  50. package/dist/linear/Matrix.d.ts +18 -0
  51. package/dist/linear/Matrix.d.ts.map +1 -0
  52. package/dist/linear/Matrix.js +2 -0
  53. package/dist/linear/Matrix.js.map +1 -0
  54. package/dist/linear/Matrix2.d.ts +66 -0
  55. package/dist/linear/Matrix2.d.ts.map +1 -0
  56. package/dist/linear/Matrix2.js +240 -0
  57. package/dist/linear/Matrix2.js.map +1 -0
  58. package/dist/linear/Matrix3.d.ts +91 -0
  59. package/dist/linear/Matrix3.d.ts.map +1 -0
  60. package/dist/linear/Matrix3.js +533 -0
  61. package/dist/linear/Matrix3.js.map +1 -0
  62. package/dist/linear/Matrix4.d.ts +140 -0
  63. package/dist/linear/Matrix4.d.ts.map +1 -0
  64. package/dist/linear/Matrix4.js +1363 -0
  65. package/dist/linear/Matrix4.js.map +1 -0
  66. package/dist/linear/Quaternion.d.ts +80 -0
  67. package/dist/linear/Quaternion.d.ts.map +1 -0
  68. package/dist/linear/Quaternion.js +371 -0
  69. package/dist/linear/Quaternion.js.map +1 -0
  70. package/dist/linear/SquareMatrix.d.ts +7 -0
  71. package/dist/linear/SquareMatrix.d.ts.map +1 -0
  72. package/dist/linear/SquareMatrix.js +2 -0
  73. package/dist/linear/SquareMatrix.js.map +1 -0
  74. package/dist/linear/Vector.d.ts +30 -0
  75. package/dist/linear/Vector.d.ts.map +1 -0
  76. package/dist/linear/Vector.js +2 -0
  77. package/dist/linear/Vector.js.map +1 -0
  78. package/dist/linear/Vector2.d.ts +96 -0
  79. package/dist/linear/Vector2.d.ts.map +1 -0
  80. package/dist/linear/Vector2.js +293 -0
  81. package/dist/linear/Vector2.js.map +1 -0
  82. package/dist/linear/Vector3.d.ts +111 -0
  83. package/dist/linear/Vector3.d.ts.map +1 -0
  84. package/dist/linear/Vector3.js +466 -0
  85. package/dist/linear/Vector3.js.map +1 -0
  86. package/dist/linear/Vector4.d.ts +86 -0
  87. package/dist/linear/Vector4.d.ts.map +1 -0
  88. package/dist/linear/Vector4.js +348 -0
  89. package/dist/linear/Vector4.js.map +1 -0
  90. package/dist/types/AxisAngle.d.ts +6 -0
  91. package/dist/types/AxisAngle.d.ts.map +1 -0
  92. package/dist/types/AxisAngle.js +2 -0
  93. package/dist/types/AxisAngle.js.map +1 -0
  94. package/dist/types/FieldOfView.d.ts +7 -0
  95. package/dist/types/FieldOfView.d.ts.map +1 -0
  96. package/dist/types/FieldOfView.js +2 -0
  97. package/dist/types/FieldOfView.js.map +1 -0
  98. package/dist/types/IntegerRepresentation.d.ts +2 -0
  99. package/dist/types/IntegerRepresentation.d.ts.map +1 -0
  100. package/dist/types/IntegerRepresentation.js +2 -0
  101. package/dist/types/IntegerRepresentation.js.map +1 -0
  102. package/dist/utility/BigNumber.d.ts +15 -0
  103. package/dist/utility/BigNumber.d.ts.map +1 -0
  104. package/dist/utility/BigNumber.js +75 -0
  105. package/dist/utility/BigNumber.js.map +1 -0
  106. package/dist/utility/MagnitudeError.d.ts +4 -0
  107. package/dist/utility/MagnitudeError.d.ts.map +1 -0
  108. package/dist/utility/MagnitudeError.js +6 -0
  109. package/dist/utility/MagnitudeError.js.map +1 -0
  110. package/dist/utility/SingularMatrixError.d.ts +4 -0
  111. package/dist/utility/SingularMatrixError.d.ts.map +1 -0
  112. package/dist/utility/SingularMatrixError.js +6 -0
  113. package/dist/utility/SingularMatrixError.js.map +1 -0
  114. package/dist/utility/epsilon.d.ts +3 -0
  115. package/dist/utility/epsilon.d.ts.map +1 -0
  116. package/dist/utility/epsilon.js +2 -0
  117. package/dist/utility/epsilon.js.map +1 -0
  118. package/package.json +17 -9
  119. package/src/algorithms/combinations.ts +12 -0
  120. package/src/algorithms/degreesToRadians.ts +10 -0
  121. package/src/algorithms/factorial.ts +9 -0
  122. package/src/algorithms/fibonacci.ts +14 -0
  123. package/src/algorithms/greatestCommonDivisor.ts +30 -0
  124. package/src/algorithms/isPrime.ts +20 -0
  125. package/src/algorithms/permutations.ts +12 -0
  126. package/src/algorithms/primeFactorization.ts +21 -0
  127. package/src/algorithms/radiansToDegrees.ts +10 -0
  128. package/src/algorithms/summation.ts +23 -0
  129. package/src/index.ts +28 -1
  130. package/src/linear/DualQuaternion.ts +1213 -0
  131. package/src/linear/Matrix.ts +106 -0
  132. package/src/linear/Matrix2.ts +709 -0
  133. package/src/linear/Matrix3.ts +1194 -0
  134. package/src/linear/Matrix4.ts +2569 -0
  135. package/src/linear/Quaternion.ts +967 -0
  136. package/src/linear/SquareMatrix.ts +27 -0
  137. package/src/linear/Vector.ts +178 -0
  138. package/src/linear/Vector2.ts +979 -0
  139. package/src/linear/Vector3.ts +1320 -0
  140. package/src/linear/Vector4.ts +975 -0
  141. package/src/types/AxisAngle.ts +10 -0
  142. package/src/types/FieldOfView.ts +14 -0
  143. package/src/types/IntegerRepresentation.ts +2 -0
  144. package/src/utility/BigNumber.ts +130 -0
  145. package/src/utility/MagnitudeError.ts +10 -0
  146. package/src/utility/SingularMatrixError.ts +13 -0
  147. package/src/utility/epsilon.ts +2 -0
@@ -0,0 +1,1194 @@
1
+ import { SingularMatrixError, epsilon, type SquareMatrix } from "@lakuna/umath";
2
+ import type { Matrix4Like } from "@lakuna/umath/Matrix4";
3
+ import type { QuaternionLike } from "@lakuna/umath/Quaternion";
4
+ import type { Vector2Like } from "@lakuna/umath/Vector2";
5
+
6
+ /**
7
+ * Numbers arranged into three columns and three rows.
8
+ * @see [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics))
9
+ */
10
+ export type Matrix3Like = Matrix3 | [
11
+ number, number, number,
12
+ number, number, number,
13
+ number, number, number
14
+ ];
15
+
16
+ /**
17
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
18
+ * @param radians The angle in radians.
19
+ * @param out The matrix to store the result in.
20
+ * @returns The transformation matrix.
21
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
22
+ */
23
+ export function fromRotation<T extends Matrix3Like>(radians: number, out: T): T {
24
+ const s: number = Math.sin(radians);
25
+ const c: number = Math.cos(radians);
26
+
27
+ out[0] = c;
28
+ out[1] = s;
29
+ out[2] = 0;
30
+ out[3] = -s;
31
+ out[4] = c;
32
+ out[5] = 0;
33
+ out[6] = 0;
34
+ out[7] = 0;
35
+ out[8] = 1;
36
+ return out;
37
+ }
38
+
39
+ /**
40
+ * Creates a transformation matrix that represents a scaling by the given vector.
41
+ * @param vector The scaling vector.
42
+ * @param out The matrix to store the result in.
43
+ * @returns The transformation matrix.
44
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
45
+ */
46
+ export function fromScaling<T extends Matrix3Like>(vector: Vector2Like, out: T): T {
47
+ out[0] = vector[0];
48
+ out[1] = 0;
49
+ out[2] = 0;
50
+ out[3] = 0;
51
+ out[4] = vector[1];
52
+ out[5] = 0;
53
+ out[6] = 0;
54
+ out[7] = 0;
55
+ out[8] = 1;
56
+ return out;
57
+ }
58
+
59
+ /**
60
+ * Creates a transformation matrix that represents a translation by the given vector.
61
+ * @param vector The translation vector.
62
+ * @param out The matrix to store the result in.
63
+ * @returns The transformation matrix.
64
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
65
+ */
66
+ export function fromTranslation<T extends Matrix3Like>(vector: Vector2Like, out: T): T {
67
+ out[0] = 1;
68
+ out[1] = 0;
69
+ out[2] = 0;
70
+ out[3] = 0;
71
+ out[4] = 1;
72
+ out[5] = 0;
73
+ out[6] = vector[0];
74
+ out[7] = vector[1];
75
+ out[8] = 1;
76
+ return out;
77
+ }
78
+
79
+ /**
80
+ * Creates a transformation matrix that represents a rotation by the given quaternion.
81
+ * @param quaternion The quaternion.
82
+ * @param out The matrix to store the result in.
83
+ * @returns The transformation matrix.
84
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
85
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
86
+ */
87
+ export function fromQuaternion<T extends Matrix3Like>(quaternion: QuaternionLike, out: T): T {
88
+ const x: number = quaternion[0];
89
+ const y: number = quaternion[1];
90
+ const z: number = quaternion[2];
91
+ const w: number = quaternion[3];
92
+
93
+ const x2: number = x + x;
94
+ const y2: number = y + y;
95
+ const z2: number = z + z;
96
+ const xx: number = x * x2;
97
+ const yx: number = y * x2;
98
+ const yy: number = y * y2;
99
+ const zx: number = z * x2;
100
+ const zy: number = z * y2;
101
+ const zz: number = z * z2;
102
+ const wx: number = w * x2;
103
+ const wy: number = w * y2;
104
+ const wz: number = w * z2;
105
+
106
+ out[0] = 1 - yy - zz;
107
+ out[3] = yx - wz;
108
+ out[6] = zx + wy;
109
+ out[1] = yx + wz;
110
+ out[4] = 1 - xx - zz;
111
+ out[7] = zy - wx;
112
+ out[2] = zx - wy;
113
+ out[5] = zy + wx;
114
+ out[8] = 1 - xx - yy;
115
+ return out;
116
+ }
117
+
118
+ /**
119
+ * Calculates a three-by-three normal (inverse transpose) matrix from a four-by-four matrix.
120
+ * @param matrix The four-by-four matrix.
121
+ * @param out The matrix to store the result in.
122
+ * @returns The normal matrix.
123
+ * @see [Normal matrix](https://en.wikipedia.org/wiki/Normal_matrix)
124
+ */
125
+ export function normalFromMatrix4<T extends Matrix3Like>(matrix: Matrix4Like, out: T): T {
126
+ const a00: number = matrix[0];
127
+ const a01: number = matrix[1];
128
+ const a02: number = matrix[2];
129
+ const a03: number = matrix[3];
130
+ const a10: number = matrix[4];
131
+ const a11: number = matrix[5];
132
+ const a12: number = matrix[6];
133
+ const a13: number = matrix[7];
134
+ const a20: number = matrix[8];
135
+ const a21: number = matrix[9];
136
+ const a22: number = matrix[10];
137
+ const a23: number = matrix[11];
138
+ const a30: number = matrix[12];
139
+ const a31: number = matrix[13];
140
+ const a32: number = matrix[14];
141
+ const a33: number = matrix[15];
142
+
143
+ const b00: number = a00 * a11 - a01 * a10;
144
+ const b01: number = a00 * a12 - a02 * a10;
145
+ const b02: number = a00 * a13 - a03 * a10;
146
+ const b03: number = a01 * a12 - a02 * a11;
147
+ const b04: number = a01 * a13 - a03 * a11;
148
+ const b05: number = a02 * a13 - a03 * a12;
149
+ const b06: number = a20 * a31 - a21 * a30;
150
+ const b07: number = a20 * a32 - a22 * a30;
151
+ const b08: number = a20 * a33 - a23 * a30;
152
+ const b09: number = a21 * a32 - a22 * a31;
153
+ const b10: number = a21 * a33 - a23 * a31;
154
+ const b11: number = a22 * a33 - a23 * a32;
155
+
156
+ let determinant: number = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
157
+ if (!determinant) {
158
+ throw new SingularMatrixError();
159
+ }
160
+ determinant = 1 / determinant;
161
+
162
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * determinant;
163
+ out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * determinant;
164
+ out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * determinant;
165
+ out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * determinant;
166
+ out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * determinant;
167
+ out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * determinant;
168
+ out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * determinant;
169
+ out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * determinant;
170
+ out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * determinant;
171
+ return out;
172
+ }
173
+
174
+ /**
175
+ * Generates a two-dimensional projection matrix with the given bounds.
176
+ * @param width The width of the projection.
177
+ * @param height The height of the projection.
178
+ * @param out The matrix to store the result in.
179
+ * @returns The projection matrix.
180
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
181
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
182
+ */
183
+ export function projection<T extends Matrix3Like>(width: number, height: number, out: T): T {
184
+ out[0] = 2 / width;
185
+ out[1] = 0;
186
+ out[2] = 0;
187
+ out[3] = 0;
188
+ out[4] = -2 / height;
189
+ out[5] = 0;
190
+ out[6] = -1;
191
+ out[7] = 1;
192
+ out[8] = 1;
193
+ return out;
194
+ }
195
+
196
+ /**
197
+ * Creates a three-by-three matrix from the upper-left corner of a four-by-four matrix.
198
+ * @param matrix The four-by-four matrix.
199
+ * @param out The matrix to store the result in.
200
+ * @returns The three-by-three matrix.
201
+ */
202
+ export function fromMatrix4<T extends Matrix3Like>(matrix: Matrix4Like, out: T): T {
203
+ out[0] = matrix[0];
204
+ out[1] = matrix[1];
205
+ out[2] = matrix[2];
206
+ out[3] = matrix[4];
207
+ out[4] = matrix[5];
208
+ out[5] = matrix[6];
209
+ out[6] = matrix[8];
210
+ out[7] = matrix[9];
211
+ out[8] = matrix[10];
212
+ return out;
213
+ }
214
+
215
+ /**
216
+ * Creates a two-by-two matrix with the given values.
217
+ * @param c0r0 The value in the first column and first row.
218
+ * @param c0r1 The value in the first column and second row.
219
+ * @param c0r2 The value in the first column and third row.
220
+ * @param c1r0 The value in the second column and first row.
221
+ * @param c1r1 The value in the second column and second row.
222
+ * @param c1r2 The value in the second column and third row.
223
+ * @param c2r0 The value in the third column and first row.
224
+ * @param c2r1 The value in the third column and second row.
225
+ * @param c2r2 The value in the third column and third row.
226
+ * @param out The matrix to store the result in.
227
+ * @returns The matrix.
228
+ */
229
+ export function fromValues<T extends Matrix3Like>(c0r0: number, c0r1: number, c0r2: number, c1r0: number, c1r1: number, c1r2: number, c2r0: number, c2r1: number, c2r2: number, out: T): T {
230
+ out[0] = c0r0;
231
+ out[1] = c0r1;
232
+ out[2] = c0r2;
233
+ out[3] = c1r0;
234
+ out[4] = c1r1;
235
+ out[5] = c1r2;
236
+ out[6] = c2r0;
237
+ out[7] = c2r1;
238
+ out[8] = c2r2;
239
+ return out;
240
+ }
241
+
242
+ /**
243
+ * Determines whether two matrices are roughly equivalent.
244
+ * @param a The first matrix.
245
+ * @param b The second matrix.
246
+ * @returns Whether the matrices are equivalent.
247
+ */
248
+ export function equals(a: Matrix3Like, b: Matrix3Like): boolean {
249
+ const a0: number = a[0];
250
+ const a1: number = a[1];
251
+ const a2: number = a[2];
252
+ const a3: number = a[3];
253
+ const a4: number = a[4];
254
+ const a5: number = a[5];
255
+ const a6: number = a[6];
256
+ const a7: number = a[7];
257
+ const a8: number = a[8];
258
+
259
+ const b0: number = b[0];
260
+ const b1: number = b[1];
261
+ const b2: number = b[2];
262
+ const b3: number = b[3];
263
+ const b4: number = b[4];
264
+ const b5: number = b[5];
265
+ const b6: number = b[6];
266
+ const b7: number = b[7];
267
+ const b8: number = b[8];
268
+
269
+ return (Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0))
270
+ && Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1))
271
+ && Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2))
272
+ && Math.abs(a3 - b3) <= epsilon * Math.max(1, Math.abs(a3), Math.abs(b3))
273
+ && Math.abs(a4 - b4) <= epsilon * Math.max(1, Math.abs(a4), Math.abs(b4))
274
+ && Math.abs(a5 - b5) <= epsilon * Math.max(1, Math.abs(a5), Math.abs(b5))
275
+ && Math.abs(a6 - b6) <= epsilon * Math.max(1, Math.abs(a6), Math.abs(b6))
276
+ && Math.abs(a7 - b7) <= epsilon * Math.max(1, Math.abs(a7), Math.abs(b7))
277
+ && Math.abs(a8 - b8) <= epsilon * Math.max(1, Math.abs(a8), Math.abs(b8)));
278
+ }
279
+
280
+ /**
281
+ * Determines whether two matrices are exactly equivalent.
282
+ * @param a The first matrix.
283
+ * @param b The second matrix.
284
+ * @returns Whether the matrices are equivalent.
285
+ */
286
+ export function exactEquals(a: Matrix3Like, b: Matrix3Like): boolean {
287
+ return a[0] == b[0]
288
+ && a[1] == b[1]
289
+ && a[2] == b[2]
290
+ && a[3] == b[3]
291
+ && a[4] == b[4]
292
+ && a[5] == b[5]
293
+ && a[6] == b[6]
294
+ && a[7] == b[7]
295
+ && a[8] == b[8];
296
+ }
297
+
298
+ /**
299
+ * Adds two matrices.
300
+ * @param a The augend.
301
+ * @param b The addend.
302
+ * @param out The matrix to store the result in.
303
+ * @returns The sum.
304
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
305
+ */
306
+ export function add<T extends Matrix3Like>(a: Matrix3Like, b: Matrix3Like, out: T): T {
307
+ out[0] = a[0] + b[0];
308
+ out[1] = a[1] + b[1];
309
+ out[2] = a[2] + b[2];
310
+ out[3] = a[3] + b[3];
311
+ out[4] = a[4] + b[4];
312
+ out[5] = a[5] + b[5];
313
+ out[6] = a[6] + b[6];
314
+ out[7] = a[7] + b[7];
315
+ out[8] = a[8] + b[8];
316
+ return out;
317
+ }
318
+
319
+ /**
320
+ * Calculates the adjugate of a matrix.
321
+ * @param matrix The matrix.
322
+ * @param out The matrix to store the result in.
323
+ * @returns The adjugate of the matrix.
324
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
325
+ */
326
+ export function adjoint<T extends Matrix3Like>(matrix: Matrix3Like, out: T): T {
327
+ const a00: number = matrix[0];
328
+ const a01: number = matrix[1];
329
+ const a02: number = matrix[2];
330
+ const a10: number = matrix[3];
331
+ const a11: number = matrix[4];
332
+ const a12: number = matrix[5];
333
+ const a20: number = matrix[6];
334
+ const a21: number = matrix[7];
335
+ const a22: number = matrix[8];
336
+
337
+ out[0] = a11 * a22 - a12 * a21;
338
+ out[1] = a02 * a21 - a01 * a22;
339
+ out[2] = a01 * a12 - a02 * a11;
340
+ out[3] = a12 * a20 - a10 * a22;
341
+ out[4] = a00 * a22 - a02 * a20;
342
+ out[5] = a02 * a10 - a00 * a12;
343
+ out[6] = a10 * a21 - a11 * a20;
344
+ out[7] = a01 * a20 - a00 * a21;
345
+ out[8] = a00 * a11 - a01 * a10;
346
+ return out;
347
+ }
348
+
349
+ /**
350
+ * Copies the values of one matrix into another.
351
+ * @param matrix The matrix to copy.
352
+ * @param out The matrix to store the result in.
353
+ * @returns This matrix.
354
+ */
355
+ export function copy<T extends Matrix3Like>(matrix: Matrix3Like, out: T): T {
356
+ out[0] = matrix[0];
357
+ out[1] = matrix[1];
358
+ out[2] = matrix[2];
359
+ out[3] = matrix[3];
360
+ out[4] = matrix[4];
361
+ out[5] = matrix[5];
362
+ out[6] = matrix[6];
363
+ out[7] = matrix[7];
364
+ out[8] = matrix[8];
365
+ return out;
366
+ }
367
+
368
+ /**
369
+ * Calculates the Frobenius norm of a matrix.
370
+ * @param matrix The matrix.
371
+ * @returns The Frobenius norm.
372
+ * @see [Matrix norm](https://en.wikipedia.org/wiki/Matrix_norm)
373
+ */
374
+ export function frob(matrix: Matrix3Like): number {
375
+ return Math.hypot(
376
+ matrix[0] as number, matrix[1] as number, matrix[2] as number,
377
+ matrix[3] as number, matrix[4] as number, matrix[5] as number,
378
+ matrix[6] as number, matrix[7] as number, matrix[8] as number
379
+ );
380
+ }
381
+
382
+ /**
383
+ * Multiplies two matrices.
384
+ * @param a The multiplicand.
385
+ * @param b The multiplier.
386
+ * @param out The matrix to store the result in.
387
+ * @returns The product of the matrices.
388
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
389
+ */
390
+ export function multiply<T extends Matrix3Like>(a: Matrix3Like, b: Matrix3Like, out: T): T {
391
+ const a00: number = a[0];
392
+ const a01: number = a[1];
393
+ const a02: number = a[2];
394
+ const a10: number = a[3];
395
+ const a11: number = a[4];
396
+ const a12: number = a[5];
397
+ const a20: number = a[6];
398
+ const a21: number = a[7];
399
+ const a22: number = a[8];
400
+
401
+ const b00: number = b[0];
402
+ const b01: number = b[1];
403
+ const b02: number = b[2];
404
+ const b10: number = b[3];
405
+ const b11: number = b[4];
406
+ const b12: number = b[5];
407
+ const b20: number = b[6];
408
+ const b21: number = b[7];
409
+ const b22: number = b[8];
410
+
411
+ out[0] = b00 * a00 + b01 * a10 + b02 * a20;
412
+ out[1] = b00 * a01 + b01 * a11 + b02 * a21;
413
+ out[2] = b00 * a02 + b01 * a12 + b02 * a22;
414
+ out[3] = b10 * a00 + b11 * a10 + b12 * a20;
415
+ out[4] = b10 * a01 + b11 * a11 + b12 * a21;
416
+ out[5] = b10 * a02 + b11 * a12 + b12 * a22;
417
+ out[6] = b20 * a00 + b21 * a10 + b22 * a20;
418
+ out[7] = b20 * a01 + b21 * a11 + b22 * a21;
419
+ out[8] = b20 * a02 + b21 * a12 + b22 * a22;
420
+ return out;
421
+ }
422
+
423
+ /**
424
+ * Multiplies a matrix by a scalar value.
425
+ * @param matrix The multiplicand.
426
+ * @param scalar The multiplier.
427
+ * @param out The matrix to store the result in.
428
+ * @returns The product.
429
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
430
+ */
431
+ export function multiplyScalar<T extends Matrix3Like>(matrix: Matrix3Like, scalar: number, out: T): T {
432
+ out[0] = matrix[0] * scalar;
433
+ out[1] = matrix[1] * scalar;
434
+ out[2] = matrix[2] * scalar;
435
+ out[3] = matrix[3] * scalar;
436
+ out[4] = matrix[4] * scalar;
437
+ out[5] = matrix[5] * scalar;
438
+ out[6] = matrix[6] * scalar;
439
+ out[7] = matrix[7] * scalar;
440
+ out[8] = matrix[8] * scalar;
441
+ return out;
442
+ }
443
+
444
+ /**
445
+ * Adds a matrix to another after multiplying the other by a scalar.
446
+ * @param a The augend.
447
+ * @param b The addend.
448
+ * @param scalar The multiplier.
449
+ * @param out The matrix to store the result in.
450
+ * @returns The sum.
451
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
452
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
453
+ */
454
+ export function multiplyScalarAndAdd<T extends Matrix3Like>(a: Matrix3Like, b: Matrix3Like, scalar: number, out: T): T {
455
+ out[0] = a[0] + b[0] * scalar;
456
+ out[1] = a[1] + b[1] * scalar;
457
+ out[2] = a[2] + b[2] * scalar;
458
+ out[3] = a[3] + b[3] * scalar;
459
+ out[4] = a[4] + b[4] * scalar;
460
+ out[5] = a[5] + b[5] * scalar;
461
+ out[6] = a[6] + b[6] * scalar;
462
+ out[7] = a[7] + b[7] * scalar;
463
+ out[8] = a[8] + b[8] * scalar;
464
+ return out;
465
+ }
466
+
467
+ /**
468
+ * Subtracts two matrices.
469
+ * @param a The minuend.
470
+ * @param b The subtrahend.
471
+ * @param out The matrix to store the result in.
472
+ * @returns The difference.
473
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
474
+ */
475
+ export function subtract<T extends Matrix3Like>(a: Matrix3Like, b: Matrix3Like, out: T): T {
476
+ out[0] = a[0] - b[0];
477
+ out[1] = a[1] - b[1];
478
+ out[2] = a[2] - b[2];
479
+ out[3] = a[3] - b[3];
480
+ out[4] = a[4] - b[4];
481
+ out[5] = a[5] - b[5];
482
+ out[6] = a[6] - b[6];
483
+ out[7] = a[7] - b[7];
484
+ out[8] = a[8] - b[8];
485
+ return out;
486
+ }
487
+
488
+ /**
489
+ * Transposes a matrix.
490
+ * @param matrix The matrix.
491
+ * @param out The matrix to store the result in.
492
+ * @returns The transpose of the matrix.
493
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
494
+ */
495
+ export function transpose<T extends Matrix3Like>(matrix: Matrix3Like, out: T): T {
496
+ if (out == matrix as unknown as T) {
497
+ const a01: number = matrix[1];
498
+ const a02: number = matrix[2];
499
+ const a12: number = matrix[5];
500
+ out[1] = matrix[3];
501
+ out[2] = matrix[6];
502
+ out[3] = a01;
503
+ out[5] = matrix[7];
504
+ out[6] = a02;
505
+ out[7] = a12;
506
+ } else {
507
+ out[0] = matrix[0];
508
+ out[1] = matrix[3];
509
+ out[2] = matrix[6];
510
+ out[3] = matrix[1];
511
+ out[4] = matrix[4];
512
+ out[5] = matrix[7];
513
+ out[6] = matrix[2];
514
+ out[7] = matrix[5];
515
+ out[8] = matrix[8];
516
+ }
517
+ return out;
518
+ }
519
+
520
+ /**
521
+ * Calculates the determinant of a matrix.
522
+ * @param matrix The matrix.
523
+ * @returns The determinant.
524
+ * @see [Determinant](https://en.wikipedia.org/wiki/Determinant)
525
+ */
526
+ export function determinant(matrix: Matrix3Like): number {
527
+ const a00: number = matrix[0];
528
+ const a01: number = matrix[1];
529
+ const a02: number = matrix[2];
530
+ const a10: number = matrix[3];
531
+ const a11: number = matrix[4];
532
+ const a12: number = matrix[5];
533
+ const a20: number = matrix[6];
534
+ const a21: number = matrix[7];
535
+ const a22: number = matrix[8];
536
+
537
+ return (a00 * (a22 * a11 - a12 * a21)
538
+ + a01 * (-a22 * a10 + a12 * a20)
539
+ + a02 * (a21 * a10 - a11 * a20));
540
+ }
541
+
542
+ /**
543
+ * Resets a matrix to identity.
544
+ * @param out The matrix to store the result in.
545
+ * @returns The matrix.
546
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
547
+ */
548
+ export function identity<T extends Matrix3Like>(out: T): T {
549
+ out[0] = 1;
550
+ out[1] = 0;
551
+ out[2] = 0;
552
+ out[3] = 0;
553
+ out[4] = 1;
554
+ out[5] = 0;
555
+ out[6] = 0;
556
+ out[7] = 0;
557
+ out[8] = 1;
558
+ return out;
559
+ }
560
+
561
+ /**
562
+ * Inverts a matrix.
563
+ * @param matrix The matrix.
564
+ * @param out The matrix to store the result in.
565
+ * @returns The inverted matrix.
566
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
567
+ */
568
+ export function invert<T extends Matrix3Like>(matrix: Matrix3Like, out: T): T {
569
+ const a00: number = matrix[0];
570
+ const a01: number = matrix[1];
571
+ const a02: number = matrix[2];
572
+ const a10: number = matrix[3];
573
+ const a11: number = matrix[4];
574
+ const a12: number = matrix[5];
575
+ const a20: number = matrix[6];
576
+ const a21: number = matrix[7];
577
+ const a22: number = matrix[8];
578
+
579
+ const b01: number = a22 * a11 - a12 * a21;
580
+ const b11: number = -a22 * a10 + a12 * a20;
581
+ const b21: number = a21 * a10 - a11 * a20;
582
+
583
+ let determinant: number = a00 * b01 + a01 * b11 + a02 * b21;
584
+ if (!determinant) {
585
+ throw new SingularMatrixError();
586
+ }
587
+ determinant = 1 / determinant;
588
+
589
+ out[0] = b01 * determinant;
590
+ out[1] = (-a22 * a01 + a02 * a21) * determinant;
591
+ out[2] = (a12 * a01 - a02 * a11) * determinant;
592
+ out[3] = b11 * determinant;
593
+ out[4] = (a22 * a00 - a02 * a20) * determinant;
594
+ out[5] = (-a12 * a00 + a02 * a10) * determinant;
595
+ out[6] = b21 * determinant;
596
+ out[7] = (-a21 * a00 + a01 * a20) * determinant;
597
+ out[8] = (a11 * a00 - a01 * a10) * determinant;
598
+ return out;
599
+ }
600
+
601
+ /**
602
+ * Rotates a matrix by the given angle around the Z-axis.
603
+ * @param matrix The matrix.
604
+ * @param radians The angle in radians.
605
+ * @param out The matrix to store the result in.
606
+ * @returns The rotated matrix.
607
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
608
+ */
609
+ export function rotate<T extends Matrix3Like>(matrix: Matrix3Like, radians: number, out: T): T {
610
+ const a00: number = matrix[0];
611
+ const a01: number = matrix[1];
612
+ const a02: number = matrix[2];
613
+ const a10: number = matrix[3];
614
+ const a11: number = matrix[4];
615
+ const a12: number = matrix[5];
616
+ const a20: number = matrix[6];
617
+ const a21: number = matrix[7];
618
+ const a22: number = matrix[8];
619
+
620
+ const s: number = Math.sin(radians);
621
+ const c: number = Math.cos(radians);
622
+
623
+ out[0] = c * a00 + s * a10;
624
+ out[1] = c * a01 + s * a11;
625
+ out[2] = c * a02 + s * a12;
626
+ out[3] = c * a10 - s * a00;
627
+ out[4] = c * a11 - s * a01;
628
+ out[5] = c * a12 - s * a02;
629
+ out[6] = a20;
630
+ out[7] = a21;
631
+ out[8] = a22;
632
+ return out;
633
+ }
634
+
635
+ /**
636
+ * Scales a matrix by the given vector.
637
+ * @param matrix The matrix.
638
+ * @param vector The scaling vector.
639
+ * @param out The matrix to store the result in.
640
+ * @returns The scaled matrix.
641
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
642
+ */
643
+ export function scale<T extends Matrix3Like>(matrix: Matrix3Like, vector: Vector2Like, out: T): T {
644
+ const x: number = vector[0];
645
+ const y: number = vector[1];
646
+
647
+ out[0] = matrix[0] * x;
648
+ out[1] = matrix[1] * x;
649
+ out[2] = matrix[2] * x;
650
+ out[3] = matrix[3] * y;
651
+ out[4] = matrix[4] * y;
652
+ out[5] = matrix[5] * y;
653
+ out[6] = matrix[6];
654
+ out[7] = matrix[7];
655
+ out[8] = matrix[8];
656
+ return out;
657
+ }
658
+
659
+ /**
660
+ * Translates a matrix by the given vector.
661
+ * @param matrix The matrix.
662
+ * @param vector The translation vector.
663
+ * @param out The matrix to store the result in.
664
+ * @returns The translated matrix.
665
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
666
+ */
667
+ export function translate<T extends Matrix3Like>(matrix: Matrix3Like, vector: Vector2Like, out: T): T {
668
+ const a00: number = matrix[0];
669
+ const a01: number = matrix[1];
670
+ const a02: number = matrix[2];
671
+ const a10: number = matrix[3];
672
+ const a11: number = matrix[4];
673
+ const a12: number = matrix[5];
674
+ const a20: number = matrix[6];
675
+ const a21: number = matrix[7];
676
+ const a22: number = matrix[8];
677
+
678
+ const x: number = vector[0];
679
+ const y: number = vector[1];
680
+
681
+ out[0] = a00;
682
+ out[1] = a01;
683
+ out[2] = a02;
684
+ out[3] = a10;
685
+ out[4] = a11;
686
+ out[5] = a12;
687
+ out[6] = x * a00 + y * a10 + a20;
688
+ out[7] = x * a01 + y * a11 + a21;
689
+ out[8] = x * a02 + y * a12 + a22;
690
+ return out;
691
+ }
692
+
693
+ /**
694
+ * A three-by-three matrix.
695
+ * @see [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics))
696
+ */
697
+ export default class Matrix3 extends Float32Array implements SquareMatrix {
698
+ /**
699
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
700
+ * @param radians The angle in radians.
701
+ * @returns The transformation matrix.
702
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
703
+ */
704
+ public static fromRotation(radians: number): Matrix3;
705
+
706
+ /**
707
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
708
+ * @param radians The angle in radians.
709
+ * @param out The matrix to store the result in.
710
+ * @returns The transformation matrix.
711
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
712
+ */
713
+ public static fromRotation<T extends Matrix3Like>(radians: number, out: T): T;
714
+
715
+ public static fromRotation<T extends Matrix3Like>(radians: number, out: T = new Matrix3() as T): T {
716
+ return fromRotation(radians, out);
717
+ }
718
+
719
+ /**
720
+ * Creates a transformation matrix that represents a scaling by the given vector.
721
+ * @param vector The scaling vector.
722
+ * @returns The transformation matrix.
723
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
724
+ */
725
+ public static fromScaling(vector: Vector2Like): Matrix3;
726
+
727
+ /**
728
+ * Creates a transformation matrix that represents a scaling by the given vector.
729
+ * @param vector The scaling vector.
730
+ * @param out The matrix to store the result in.
731
+ * @returns The transformation matrix.
732
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
733
+ */
734
+ public static fromScaling<T extends Matrix3Like>(vector: Vector2Like, out: T): T;
735
+
736
+ public static fromScaling<T extends Matrix3Like>(vector: Vector2Like, out: T = new Matrix3() as T): T {
737
+ return fromScaling(vector, out);
738
+ }
739
+
740
+ /**
741
+ * Creates a transformation matrix that represents a translation by the given vector.
742
+ * @param vector The translation vector.
743
+ * @returns The transformation matrix.
744
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
745
+ */
746
+ public static fromTranslation(vector: Vector2Like): Matrix3;
747
+
748
+ /**
749
+ * Creates a transformation matrix that represents a translation by the given vector.
750
+ * @param vector The translation vector.
751
+ * @param out The matrix to store the result in.
752
+ * @returns The transformation matrix.
753
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
754
+ */
755
+ public static fromTranslation<T extends Matrix3Like>(vector: Vector2Like, out: T): T;
756
+
757
+ public static fromTranslation<T extends Matrix3Like>(vector: Vector2Like, out: T = new Matrix3() as T): T {
758
+ return fromTranslation(vector, out);
759
+ }
760
+
761
+ /**
762
+ * Creates a transformation matrix that represents a rotation by the given quaternion.
763
+ * @param quaternion The quaternion.
764
+ * @returns The transformation matrix.
765
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
766
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
767
+ */
768
+ public static fromQuaternion(quaternion: QuaternionLike): Matrix3;
769
+
770
+ /**
771
+ * Creates a transformation matrix that represents a rotation by the given quaternion.
772
+ * @param quaternion The quaternion.
773
+ * @param out The matrix to store the result in.
774
+ * @returns The transformation matrix.
775
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
776
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
777
+ */
778
+ public static fromQuaternion<T extends Matrix3Like>(quaternion: QuaternionLike, out: T): T;
779
+
780
+ public static fromQuaternion<T extends Matrix3Like>(quaternion: QuaternionLike, out: T = new Matrix3() as T): T {
781
+ return fromQuaternion(quaternion, out);
782
+ }
783
+
784
+ /**
785
+ * Calculates a three-by-three normal (inverse transpose) matrix from a four-by-four matrix.
786
+ * @param matrix The four-by-four matrix.
787
+ * @returns The normal matrix.
788
+ * @see [Normal matrix](https://en.wikipedia.org/wiki/Normal_matrix)
789
+ */
790
+ public static normalFromMatrix4(matrix: Matrix4Like): Matrix3;
791
+
792
+ /**
793
+ * Calculates a three-by-three normal (inverse transpose) matrix from a four-by-four matrix.
794
+ * @param matrix The four-by-four matrix.
795
+ * @param out The matrix to store the result in.
796
+ * @returns The normal matrix.
797
+ * @see [Normal matrix](https://en.wikipedia.org/wiki/Normal_matrix)
798
+ */
799
+ public static normalFromMatrix4<T extends Matrix3Like>(matrix: Matrix4Like, out: T): T;
800
+
801
+ public static normalFromMatrix4<T extends Matrix3Like>(matrix: Matrix4Like, out: T = new Matrix3() as T): T {
802
+ return normalFromMatrix4(matrix, out);
803
+ }
804
+
805
+ /**
806
+ * Generates a two-dimensional projection matrix with the given bounds.
807
+ * @param width The width of the projection.
808
+ * @param height The height of the projection.
809
+ * @returns The projection matrix.
810
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
811
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
812
+ */
813
+ public static projection(width: number, height: number): Matrix3;
814
+
815
+ /**
816
+ * Generates a two-dimensional projection matrix with the given bounds.
817
+ * @param width The width of the projection.
818
+ * @param height The height of the projection.
819
+ * @param out The matrix to store the result in.
820
+ * @returns The projection matrix.
821
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
822
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
823
+ */
824
+ public static projection<T extends Matrix3Like>(width: number, height: number, out: T): T;
825
+
826
+ public static projection<T extends Matrix3Like>(width: number, height: number, out: T = new Matrix3() as T): T {
827
+ return projection(width, height, out);
828
+ }
829
+
830
+ /**
831
+ * Creates a three-by-three matrix from the upper-left corner of a four-by-four matrix.
832
+ * @param matrix The four-by-four matrix.
833
+ * @returns The three-by-three matrix.
834
+ */
835
+ public static fromMatrix4(matrix: Matrix4Like): Matrix3;
836
+
837
+ /**
838
+ * Creates a three-by-three matrix from the upper-left corner of a four-by-four matrix.
839
+ * @param matrix The four-by-four matrix.
840
+ * @param out The matrix to store the result in.
841
+ * @returns The three-by-three matrix.
842
+ */
843
+ public static fromMatrix4<T extends Matrix3Like>(matrix: Matrix4Like, out: T): T;
844
+
845
+ public static fromMatrix4<T extends Matrix3Like>(matrix: Matrix4Like, out: T = new Matrix3() as T): T {
846
+ return fromMatrix4(matrix, out);
847
+ }
848
+
849
+ /**
850
+ * Creates a two-by-two matrix with the given values.
851
+ * @param c0r0 The value in the first column and first row.
852
+ * @param c0r1 The value in the first column and second row.
853
+ * @param c0r2 The value in the first column and third row.
854
+ * @param c1r0 The value in the second column and first row.
855
+ * @param c1r1 The value in the second column and second row.
856
+ * @param c1r2 The value in the second column and third row.
857
+ * @param c2r0 The value in the third column and first row.
858
+ * @param c2r1 The value in the third column and second row.
859
+ * @param c2r2 The value in the third column and third row.
860
+ * @returns The matrix.
861
+ */
862
+ public static fromValues(c0r0: number, c0r1: number, c0r2: number, c1r0: number, c1r1: number, c1r2: number, c2r0: number, c2r1: number, c2r2: number): Matrix3;
863
+
864
+ /**
865
+ * Creates a two-by-two matrix with the given values.
866
+ * @param c0r0 The value in the first column and first row.
867
+ * @param c0r1 The value in the first column and second row.
868
+ * @param c0r2 The value in the first column and third row.
869
+ * @param c1r0 The value in the second column and first row.
870
+ * @param c1r1 The value in the second column and second row.
871
+ * @param c1r2 The value in the second column and third row.
872
+ * @param c2r0 The value in the third column and first row.
873
+ * @param c2r1 The value in the third column and second row.
874
+ * @param c2r2 The value in the third column and third row.
875
+ * @param out The matrix to store the result in.
876
+ * @returns The matrix.
877
+ */
878
+ public static fromValues<T extends Matrix3Like>(c0r0: number, c0r1: number, c0r2: number, c1r0: number, c1r1: number, c1r2: number, c2r0: number, c2r1: number, c2r2: number, out: T): T;
879
+
880
+ public static fromValues<T extends Matrix3Like>(c0r0: number, c0r1: number, c0r2: number, c1r0: number, c1r1: number, c1r2: number, c2r0: number, c2r1: number, c2r2: number, out: T = new Matrix3() as T): T {
881
+ return fromValues(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2, out);
882
+ }
883
+
884
+ /**
885
+ * Creates a three-by-three identity matrix.
886
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
887
+ */
888
+ public constructor() {
889
+ super(9);
890
+
891
+ this[0] = 1;
892
+ this[4] = 1;
893
+ this[8] = 1;
894
+
895
+ this.width = 3;
896
+ this.height = 3;
897
+ }
898
+
899
+ /** The number of columns in this matrix. */
900
+ public readonly width: 3;
901
+
902
+ /** The number of rows in this matrix. */
903
+ public readonly height: 3;
904
+
905
+ /**
906
+ * Determines whether this matrix is roughly equivalent to another.
907
+ * @param matrix The other matrix.
908
+ * @returns Whether the matrices are equivalent.
909
+ */
910
+ public equals(matrix: Matrix3Like): boolean {
911
+ return equals(this, matrix);
912
+ }
913
+
914
+ /**
915
+ * Determines whether this matrix is exactly equivalent to another.
916
+ * @param matrix The other matrix.
917
+ * @returns Whether the matrices are equivalent.
918
+ */
919
+ public exactEquals(matrix: Matrix3Like): boolean {
920
+ return exactEquals(this, matrix);
921
+ }
922
+
923
+ /**
924
+ * Adds two matrices of the same size.
925
+ * @param matrix The other matrix.
926
+ * @returns The sum of the matrices.
927
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
928
+ */
929
+ public add(matrix: Matrix3Like): Matrix3;
930
+
931
+ /**
932
+ * Adds two matrices of the same size.
933
+ * @param matrix The other matrix.
934
+ * @param out The matrix to store the result in.
935
+ * @returns The sum of the matrices.
936
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
937
+ */
938
+ public add<T extends Matrix3Like>(matrix: Matrix3Like, out: T): T;
939
+
940
+ public add<T extends Matrix3Like>(matrix: Matrix3Like, out: T = new Matrix3() as T): T {
941
+ return add(this, matrix, out);
942
+ }
943
+
944
+ /**
945
+ * Calculates the adjugate of this matrix.
946
+ * @returns The adjugate of this matrix.
947
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
948
+ */
949
+ public adjoint(): Matrix3;
950
+
951
+ /**
952
+ * Calculates the adjugate of this matrix.
953
+ * @param out The matrix to store the result in.
954
+ * @returns The adjugate of this matrix.
955
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
956
+ */
957
+ public adjoint<T extends Matrix3Like>(out: T): T;
958
+
959
+ public adjoint<T extends Matrix3Like>(out: T = new Matrix3() as T): T {
960
+ return adjoint(this, out);
961
+ }
962
+
963
+ /**
964
+ * Creates a copy of this matrix.
965
+ * @returns A copy of this matrix.
966
+ */
967
+ public clone(): Matrix3 {
968
+ // TODO: `out` parameter.
969
+ return copy(this, new Matrix3());
970
+ }
971
+
972
+ /**
973
+ * Copies the values of another matrix into this one.
974
+ * @param matrix The matrix to copy.
975
+ * @returns This matrix.
976
+ */
977
+ public copy(matrix: Matrix3Like): this {
978
+ return copy(matrix, this);
979
+ }
980
+
981
+ /**
982
+ * The Frobenius norm of this matrix.
983
+ * @see [Matrix norm](https://en.wikipedia.org/wiki/Matrix_norm)
984
+ */
985
+ public get frob(): number {
986
+ return frob(this);
987
+ }
988
+
989
+ /**
990
+ * Multiplies this matrix by another.
991
+ * @param matrix The other matrix.
992
+ * @returns The product of the matrices.
993
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
994
+ */
995
+ public multiply(matrix: Matrix3Like): Matrix3;
996
+
997
+ /**
998
+ * Multiplies this matrix by another.
999
+ * @param matrix The other matrix.
1000
+ * @param out The matrix to store the result in.
1001
+ * @returns The product of the matrices.
1002
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1003
+ */
1004
+ public multiply<T extends Matrix3Like>(matrix: Matrix3Like, out: T): T;
1005
+
1006
+ public multiply<T extends Matrix3Like>(matrix: Matrix3Like, out: T = new Matrix3() as T): T {
1007
+ return multiply(this, matrix, out);
1008
+ }
1009
+
1010
+ /**
1011
+ * Multiplies this matrix by a scalar value.
1012
+ * @param scalar The scalar value.
1013
+ * @returns The product of the matrix and the scalar value.
1014
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1015
+ */
1016
+ public multiplyScalar(scalar: number): Matrix3;
1017
+
1018
+ /**
1019
+ * Multiplies this matrix by a scalar value.
1020
+ * @param scalar The scalar value.
1021
+ * @param out The matrix to store the result in.
1022
+ * @returns The product of the matrix and the scalar value.
1023
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1024
+ */
1025
+ public multiplyScalar<T extends Matrix3Like>(scalar: number, out: T): T;
1026
+
1027
+ public multiplyScalar<T extends Matrix3Like>(scalar: number, out: T = new Matrix3() as T): T {
1028
+ return multiplyScalar(this, scalar, out);
1029
+ }
1030
+
1031
+ /**
1032
+ * Adds this matrix to another after multiplying the other by a scalar.
1033
+ * @param matrix The other matrix.
1034
+ * @param scalar The scalar.
1035
+ * @returns The sum.
1036
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
1037
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1038
+ */
1039
+ public multiplyScalarAndAdd(matrix: Matrix3Like, scalar: number): Matrix3;
1040
+
1041
+ /**
1042
+ * Adds this matrix to another after multiplying the other by a scalar.
1043
+ * @param matrix The other matrix.
1044
+ * @param scalar The scalar.
1045
+ * @param out The matrix to store the result in.
1046
+ * @returns The sum.
1047
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
1048
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1049
+ */
1050
+ public multiplyScalarAndAdd<T extends Matrix3Like>(matrix: Matrix3Like, scalar: number, out: T): T;
1051
+
1052
+ public multiplyScalarAndAdd<T extends Matrix3Like>(matrix: Matrix3Like, scalar: number, out: T = new Matrix3() as T): T {
1053
+ return multiplyScalarAndAdd(this, matrix, scalar, out);
1054
+ }
1055
+
1056
+ /**
1057
+ * Subtracts another matrix from this one.
1058
+ * @param matrix The other matrix.
1059
+ * @returns The difference between the matrices.
1060
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
1061
+ */
1062
+ public subtract(matrix: Matrix3Like): Matrix3;
1063
+
1064
+ /**
1065
+ * Subtracts another matrix from this one.
1066
+ * @param matrix The other matrix.
1067
+ * @param out The matrix to store the result in.
1068
+ * @returns The difference between the matrices.
1069
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
1070
+ */
1071
+ public subtract<T extends Matrix3Like>(matrix: Matrix3Like, out: T): T;
1072
+
1073
+ public subtract<T extends Matrix3Like>(matrix: Matrix3Like, out: T = new Matrix3() as T): T {
1074
+ return subtract(this, matrix, out);
1075
+ }
1076
+
1077
+ /**
1078
+ * Transposes this matrix.
1079
+ * @returns The transpose of this matrix.
1080
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
1081
+ */
1082
+ public transpose(): Matrix3;
1083
+
1084
+ /**
1085
+ * Transposes this matrix.
1086
+ * @param out The matrix to store the result in.
1087
+ * @returns The transpose of this matrix.
1088
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
1089
+ */
1090
+ public transpose<T extends Matrix3Like>(out: T): T;
1091
+
1092
+ public transpose<T extends Matrix3Like>(out: T = new Matrix3() as T): T {
1093
+ return transpose(this, out);
1094
+ }
1095
+
1096
+ /**
1097
+ * The determinant of this matrix.
1098
+ * @see [Determinant](https://en.wikipedia.org/wiki/Determinant)
1099
+ */
1100
+ public get determinant(): number {
1101
+ return determinant(this);
1102
+ }
1103
+
1104
+ /**
1105
+ * Resets this matrix to identity.
1106
+ * @returns This matrix.
1107
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
1108
+ */
1109
+ public identity(): this {
1110
+ return identity(this);
1111
+ }
1112
+
1113
+ /**
1114
+ * Inverts this matrix.
1115
+ * @returns The inverted matrix.
1116
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
1117
+ */
1118
+ public invert(): Matrix3;
1119
+
1120
+ /**
1121
+ * Inverts this matrix.
1122
+ * @param out The matrix to store the result in.
1123
+ * @returns The inverted matrix.
1124
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
1125
+ */
1126
+ public invert<T extends Matrix3Like>(out: T): T;
1127
+
1128
+ public invert<T extends Matrix3Like>(out: T = new Matrix3() as T): T {
1129
+ return invert(this, out);
1130
+ }
1131
+
1132
+ /**
1133
+ * Rotates this matrix by the given angle around the Z-axis.
1134
+ * @param radians The angle in radians.
1135
+ * @returns The rotated matrix.
1136
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1137
+ */
1138
+ public rotate(radians: number): Matrix3;
1139
+
1140
+ /**
1141
+ * Rotates this matrix by the given angle around the Z-axis.
1142
+ * @param radians The angle in radians.
1143
+ * @param out The matrix to store the result in.
1144
+ * @returns The rotated matrix.
1145
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1146
+ */
1147
+ public rotate<T extends Matrix3Like>(radians: number, out: T): T;
1148
+
1149
+ public rotate<T extends Matrix3Like>(radians: number, out: T = new Matrix3() as T): T {
1150
+ return rotate(this, radians, out);
1151
+ }
1152
+
1153
+ /**
1154
+ * Scales this matrix by the given vector.
1155
+ * @param vector The scaling vector.
1156
+ * @returns The scaled matrix.
1157
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1158
+ */
1159
+ public scale(vector: Vector2Like): Matrix3;
1160
+
1161
+ /**
1162
+ * Scales this matrix by the given vector.
1163
+ * @param vector The scaling vector.
1164
+ * @param out The matrix to store the result in.
1165
+ * @returns The scaled matrix.
1166
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1167
+ */
1168
+ public scale<T extends Matrix3Like>(vector: Vector2Like, out: T): T;
1169
+
1170
+ public scale<T extends Matrix3Like>(vector: Vector2Like, out: T = new Matrix3() as T): T {
1171
+ return scale(this, vector, out);
1172
+ }
1173
+
1174
+ /**
1175
+ * Translates this matrix by the given vector.
1176
+ * @param vector The translation vector.
1177
+ * @returns The translated matrix.
1178
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1179
+ */
1180
+ public translate(vector: Vector2Like): Matrix3;
1181
+
1182
+ /**
1183
+ * Translates this matrix by the given vector.
1184
+ * @param vector The translation vector.
1185
+ * @param out The matrix to store the result in.
1186
+ * @returns The translated matrix.
1187
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1188
+ */
1189
+ public translate<T extends Matrix3Like>(vector: Vector2Like, out: T): T;
1190
+
1191
+ public translate<T extends Matrix3Like>(vector: Vector2Like, out: T = new Matrix3() as T): T {
1192
+ return translate(this, vector, out);
1193
+ }
1194
+ }