@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,2569 @@
1
+ import { type FieldOfView, MagnitudeError, SingularMatrixError, epsilon, type SquareMatrix } from "@lakuna/umath";
2
+ import type { DualQuaternionLike } from "@lakuna/umath/DualQuaternion";
3
+ import type { QuaternionLike } from "@lakuna/umath/Quaternion";
4
+ import type { Vector3Like } from "@lakuna/umath/Vector3";
5
+
6
+ /**
7
+ * Numbers arranged into four columns and four rows.
8
+ * @see [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics))
9
+ */
10
+ export type Matrix4Like = Matrix4 | [
11
+ number, number, number, number,
12
+ number, number, number, number,
13
+ number, number, number, number,
14
+ number, number, number, number
15
+ ];
16
+
17
+ /**
18
+ * Creates a transformation matrix that represents a translation by the given vector.
19
+ * @param vector The translation vector.
20
+ * @param out The matrix to store the result in.
21
+ * @returns The transformation matrix.
22
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
23
+ */
24
+ export function fromTranslation<T extends Matrix4Like>(vector: Vector3Like, out: T): T {
25
+ out[0] = 1;
26
+ out[1] = 0;
27
+ out[2] = 0;
28
+ out[3] = 0;
29
+ out[4] = 0;
30
+ out[5] = 1;
31
+ out[6] = 0;
32
+ out[7] = 0;
33
+ out[8] = 0;
34
+ out[9] = 0;
35
+ out[10] = 1;
36
+ out[11] = 0;
37
+ out[12] = vector[0];
38
+ out[13] = vector[1];
39
+ out[14] = vector[2];
40
+ out[15] = 1;
41
+ return out;
42
+ }
43
+
44
+ /**
45
+ * Creates a transformation matrix that represents a scaling by the given vector.
46
+ * @param vector The scaling vector.
47
+ * @param out The matrix to store the result in.
48
+ * @returns The transformation matrix.
49
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
50
+ */
51
+ export function fromScaling<T extends Matrix4Like>(vector: Vector3Like, out: T): T {
52
+ out[0] = vector[0];
53
+ out[1] = 0;
54
+ out[2] = 0;
55
+ out[3] = 0;
56
+ out[4] = 0;
57
+ out[5] = vector[1];
58
+ out[6] = 0;
59
+ out[7] = 0;
60
+ out[8] = 0;
61
+ out[9] = 0;
62
+ out[10] = vector[2];
63
+ out[11] = 0;
64
+ out[12] = 0;
65
+ out[13] = 0;
66
+ out[14] = 0;
67
+ out[15] = 1;
68
+ return out;
69
+ }
70
+
71
+ /**
72
+ * Resets a matrix to identity.
73
+ * @param out The matrix to store the result in.
74
+ * @returns The matrix.
75
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
76
+ */
77
+ export function identity<T extends Matrix4Like>(out: T): T {
78
+ out[0] = 1;
79
+ out[1] = 0;
80
+ out[2] = 0;
81
+ out[3] = 0;
82
+ out[4] = 0;
83
+ out[5] = 1;
84
+ out[6] = 0;
85
+ out[7] = 0;
86
+ out[8] = 0;
87
+ out[9] = 0;
88
+ out[10] = 1;
89
+ out[11] = 0;
90
+ out[12] = 0;
91
+ out[13] = 0;
92
+ out[14] = 0;
93
+ out[15] = 1;
94
+ return out;
95
+ }
96
+
97
+ /**
98
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
99
+ * @param radians The angle in radians.
100
+ * @param axis The axis to rotate around.
101
+ * @param out The matrix to store the result in.
102
+ * @returns The transformation matrix.
103
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
104
+ */
105
+ export function fromRotation<T extends Matrix4Like>(radians: number, axis: Vector3Like, out: T): T {
106
+ let x: number = axis[0];
107
+ let y: number = axis[1];
108
+ let z: number = axis[2];
109
+
110
+ let len: number = Math.hypot(x, y, z);
111
+ if (len == 0) {
112
+ throw new MagnitudeError();
113
+ }
114
+ len = 1 / len;
115
+
116
+ x *= len;
117
+ y *= len;
118
+ z *= len;
119
+
120
+ const s: number = Math.sin(radians);
121
+ const c: number = Math.cos(radians);
122
+ const t: number = 1 - c;
123
+
124
+ out[0] = x * x * t + c;
125
+ out[1] = y * x * t + z * s;
126
+ out[2] = z * x * t - y * s;
127
+ out[3] = 0;
128
+ out[4] = x * y * t - z * s;
129
+ out[5] = y * y * t + c;
130
+ out[6] = z * y * t + x * s;
131
+ out[7] = 0;
132
+ out[8] = x * z * t + y * s;
133
+ out[9] = y * z * t - x * s;
134
+ out[10] = z * z * t + c;
135
+ out[11] = 0;
136
+ out[12] = 0;
137
+ out[13] = 0;
138
+ out[14] = 0;
139
+ out[15] = 1;
140
+ return out;
141
+ }
142
+
143
+ /**
144
+ * Creates a transformation matrix that represents a rotation by the given angle around the X-axis.
145
+ * @param radians The angle in radians.
146
+ * @param out The matrix to store the result in.
147
+ * @returns The transformation matrix.
148
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
149
+ */
150
+ export function fromXRotation<T extends Matrix4Like>(radians: number, out: T): T {
151
+ const s: number = Math.sin(radians);
152
+ const c: number = Math.cos(radians);
153
+
154
+ out[0] = 1;
155
+ out[1] = 0;
156
+ out[2] = 0;
157
+ out[3] = 0;
158
+ out[4] = 0;
159
+ out[5] = c;
160
+ out[6] = s;
161
+ out[7] = 0;
162
+ out[8] = 0;
163
+ out[9] = -s;
164
+ out[10] = c;
165
+ out[11] = 0;
166
+ out[12] = 0;
167
+ out[13] = 0;
168
+ out[14] = 0;
169
+ out[15] = 1;
170
+ return out;
171
+ }
172
+
173
+ /**
174
+ * Creates a transformation matrix that represents a rotation by the given angle around the Y-axis.
175
+ * @param radians The angle in radians.
176
+ * @param out The matrix to store the result in.
177
+ * @returns The transformation matrix.
178
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
179
+ */
180
+ export function fromYRotation<T extends Matrix4Like>(radians: number, out: T): T {
181
+ const s: number = Math.sin(radians);
182
+ const c: number = Math.cos(radians);
183
+
184
+ out[0] = c;
185
+ out[1] = 0;
186
+ out[2] = -s;
187
+ out[3] = 0;
188
+ out[4] = 0;
189
+ out[5] = 1;
190
+ out[6] = 0;
191
+ out[7] = 0;
192
+ out[8] = s;
193
+ out[9] = 0;
194
+ out[10] = c;
195
+ out[11] = 0;
196
+ out[12] = 0;
197
+ out[13] = 0;
198
+ out[14] = 0;
199
+ out[15] = 1;
200
+ return out;
201
+ }
202
+
203
+ /**
204
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
205
+ * @param radians The angle in radians.
206
+ * @param out The matrix to store the result in.
207
+ * @returns The transformation matrix.
208
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
209
+ */
210
+ export function fromZRotation<T extends Matrix4Like>(radians: number, out: T): T {
211
+ const s: number = Math.sin(radians);
212
+ const c: number = Math.cos(radians);
213
+
214
+ out[0] = c;
215
+ out[1] = s;
216
+ out[2] = 0;
217
+ out[3] = 0;
218
+ out[4] = -s;
219
+ out[5] = c;
220
+ out[6] = 0;
221
+ out[7] = 0;
222
+ out[8] = 0;
223
+ out[9] = 0;
224
+ out[10] = 1;
225
+ out[11] = 0;
226
+ out[12] = 0;
227
+ out[13] = 0;
228
+ out[14] = 0;
229
+ out[15] = 1;
230
+ return out;
231
+ }
232
+
233
+ /**
234
+ * Creates a transformation matrix from the given rotation and translation.
235
+ * @param rotation The rotation quaternion.
236
+ * @param translation The translation vector.
237
+ * @param out The matrix to store the result in.
238
+ * @returns The transformation matrix.
239
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
240
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
241
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
242
+ */
243
+ export function fromRotationTranslation<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, out: T): T {
244
+ const x: number = rotation[0];
245
+ const y: number = rotation[1];
246
+ const z: number = rotation[2];
247
+ const w: number = rotation[3];
248
+
249
+ const x2: number = x + x;
250
+ const y2: number = y + y;
251
+ const z2: number = z + z;
252
+ const xx: number = x * x2;
253
+ const xy: number = x * y2;
254
+ const xz: number = x * z2;
255
+ const yy: number = y * y2;
256
+ const yz: number = y * z2;
257
+ const zz: number = z * z2;
258
+ const wx: number = w * x2;
259
+ const wy: number = w * y2;
260
+ const wz: number = w * z2;
261
+
262
+ out[0] = 1 - (yy + zz);
263
+ out[1] = xy + wz;
264
+ out[2] = xz - wy;
265
+ out[3] = 0;
266
+ out[4] = xy - wz;
267
+ out[5] = 1 - (xx + zz);
268
+ out[6] = yz + wx;
269
+ out[7] = 0;
270
+ out[8] = xz + wy;
271
+ out[9] = yz - wx;
272
+ out[10] = 1 - (xx + yy);
273
+ out[11] = 0;
274
+ out[12] = translation[0];
275
+ out[13] = translation[1];
276
+ out[14] = translation[2];
277
+ out[15] = 1;
278
+ return out;
279
+ }
280
+
281
+ /**
282
+ * Creates a transformation matrix from the given rotation, translation, and scale.
283
+ * @param rotation The rotation quaternion.
284
+ * @param translation The translation vector.
285
+ * @param scaling The scaling vector.
286
+ * @param out The matrix to store the result in.
287
+ * @returns The transformation matrix.
288
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
289
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
290
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
291
+ */
292
+ export function fromRotationTranslationScale<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like, out: T): T {
293
+ const x: number = rotation[0];
294
+ const y: number = rotation[1];
295
+ const z: number = rotation[2];
296
+ const w: number = rotation[3];
297
+
298
+ const x2: number = x + x;
299
+ const y2: number = y + y;
300
+ const z2: number = z + z;
301
+ const xx: number = x * x2;
302
+ const xy: number = x * y2;
303
+ const xz: number = x * z2;
304
+ const yy: number = y * y2;
305
+ const yz: number = y * z2;
306
+ const zz: number = z * z2;
307
+ const wx: number = w * x2;
308
+ const wy: number = w * y2;
309
+ const wz: number = w * z2;
310
+
311
+ const sx: number = scaling[0];
312
+ const sy: number = scaling[1];
313
+ const sz: number = scaling[2];
314
+
315
+ out[0] = (1 - (yy + zz)) * sx;
316
+ out[1] = (xy + wz) * sx;
317
+ out[2] = (xz - wy) * sx;
318
+ out[3] = 0;
319
+ out[4] = (xy - wz) * sy;
320
+ out[5] = (1 - (xx + zz)) * sy;
321
+ out[6] = (yz + wx) * sy;
322
+ out[7] = 0;
323
+ out[8] = (xz + wy) * sz;
324
+ out[9] = (yz - wx) * sz;
325
+ out[10] = (1 - (xx + yy)) * sz;
326
+ out[11] = 0;
327
+ out[12] = translation[0];
328
+ out[13] = translation[1];
329
+ out[14] = translation[2];
330
+ out[15] = 1;
331
+ return out;
332
+ }
333
+
334
+ /**
335
+ * Creates a transformation matrix from the given rotation, translation, and scale around the given origin.
336
+ * @param rotation The rotation quaternion.
337
+ * @param translation The translation vector.
338
+ * @param scaling The scaling vector.
339
+ * @param origin The origin vector.
340
+ * @param out The matrix to store the result in.
341
+ * @returns The transformation matrix.
342
+ * @see [Quaternion](https://en.wikipedia.org/wiki/Quaternion)
343
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
344
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
345
+ */
346
+ export function fromRotationTranslationScaleOrigin<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like, origin: Vector3Like, out: T): T {
347
+ const x: number = rotation[0];
348
+ const y: number = rotation[1];
349
+ const z: number = rotation[2];
350
+ const w: number = rotation[3];
351
+
352
+ const x2: number = x + x;
353
+ const y2: number = y + y;
354
+ const z2: number = z + z;
355
+ const xx: number = x * x2;
356
+ const xy: number = x * y2;
357
+ const xz: number = x * z2;
358
+ const yy: number = y * y2;
359
+ const yz: number = y * z2;
360
+ const zz: number = z * z2;
361
+ const wx: number = w * x2;
362
+ const wy: number = w * y2;
363
+ const wz: number = w * z2;
364
+
365
+ const sx: number = scaling[0];
366
+ const sy: number = scaling[1];
367
+ const sz: number = scaling[2];
368
+ const ox: number = origin[0];
369
+ const oy: number = origin[1];
370
+ const oz: number = origin[2];
371
+
372
+ const out0: number = (1 - (yy + zz)) * sx;
373
+ const out1: number = (xy + wz) * sx;
374
+ const out2: number = (xz - wy) * sx;
375
+ const out4: number = (xy - wz) * sy;
376
+ const out5: number = (1 - (xx + zz)) * sy;
377
+ const out6: number = (yz + wx) * sy;
378
+ const out8: number = (xz + wy) * sz;
379
+ const out9: number = (yz - wx) * sz;
380
+ const out10: number = (1 - (xx + yy)) * sz;
381
+
382
+ out[0] = out0;
383
+ out[1] = out1;
384
+ out[2] = out2;
385
+ out[3] = 0;
386
+ out[4] = out4;
387
+ out[5] = out5;
388
+ out[6] = out6;
389
+ out[7] = 0;
390
+ out[8] = out8;
391
+ out[9] = out9;
392
+ out[10] = out10;
393
+ out[11] = 0;
394
+ out[12] = translation[0] + ox - (out0 * ox + out4 * oy + out8 * oz);
395
+ out[13] = translation[1] + oy - (out1 * ox + out5 * oy + out9 * oz);
396
+ out[14] = translation[2] + oz - (out2 * ox + out6 * oy + out10 * oz);
397
+ out[15] = 1;
398
+ return out;
399
+ }
400
+
401
+ /** A vector that is used to store intermediary values for some functions. */
402
+ const intermediary: Vector3Like = new Float32Array(3) as Vector3Like;
403
+
404
+ /**
405
+ * Creates a transformation matrix from a dual quaternion.
406
+ * @param quaternion The dual quaternion.
407
+ * @param out The matrix to store the result in.
408
+ * @returns The transformation matrix.
409
+ * @see [Dual quaternion](https://en.wikipedia.org/wiki/Dual_quaternion)
410
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
411
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
412
+ */
413
+ export function fromDualQuaternion<T extends Matrix4Like>(quaternion: DualQuaternionLike, out: T): T {
414
+ const bx: number = -quaternion[0];
415
+ const by: number = -quaternion[1];
416
+ const bz: number = -quaternion[2];
417
+ const bw: number = quaternion[3];
418
+ const ax: number = quaternion[4];
419
+ const ay: number = quaternion[5];
420
+ const az: number = quaternion[6];
421
+ const aw: number = quaternion[7];
422
+
423
+ const magnitude: number = bx * bx + by * by + bz * bz + bw * bw;
424
+ if (magnitude > 0) {
425
+ intermediary[0] = ((ax * bw + aw * bx + ay * bz - az * by) * 2) / magnitude;
426
+ intermediary[1] = ((ay * bw + aw * by + az * bx - ax * bz) * 2) / magnitude;
427
+ intermediary[2] = ((az * bw + aw * bz + ax * by - ay * bx) * 2) / magnitude;
428
+ } else {
429
+ intermediary[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
430
+ intermediary[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
431
+ intermediary[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
432
+ }
433
+
434
+ return fromRotationTranslation(quaternion as unknown as QuaternionLike, intermediary, out);
435
+ }
436
+
437
+ /**
438
+ * Generates a frustum matrix with the given bounds.
439
+ * @param left The left bound of the frustum.
440
+ * @param right The right bound of the frustum.
441
+ * @param bottom The bottom bound of the frustum.
442
+ * @param top The top bound of the frustum.
443
+ * @param near The near bound of the frustum.
444
+ * @param far The far bound of the frustum.
445
+ * @param out The matrix to store the result in.
446
+ * @returns The frustum matrix.
447
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
448
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
449
+ */
450
+ export function frustum<T extends Matrix4Like>(left: number, right: number, bottom: number, top: number, near: number, far: number, out: T): T {
451
+ const rl: number = 1 / (right - left);
452
+ const tb: number = 1 / (top - bottom);
453
+ const nf: number = 1 / (near - far);
454
+
455
+ out[0] = near * 2 * rl;
456
+ out[1] = 0;
457
+ out[2] = 0;
458
+ out[3] = 0;
459
+ out[4] = 0;
460
+ out[5] = near * 2 * tb;
461
+ out[6] = 0;
462
+ out[7] = 0;
463
+ out[8] = (right + left) * rl;
464
+ out[9] = (top + bottom) * tb;
465
+ out[10] = (far + near) * nf;
466
+ out[11] = -1;
467
+ out[12] = 0;
468
+ out[13] = 0;
469
+ out[14] = far * near * 2 * nf;
470
+ out[15] = 0;
471
+ return out;
472
+ }
473
+
474
+ /**
475
+ * Creates a perspective projection matrix with the given bounds.
476
+ * @param fov The vertical field of view in radians.
477
+ * @param aspect The aspect ratio (typically the width of the viewport divided by its height).
478
+ * @param near The near bound of the frustum.
479
+ * @param far The far bound of the frustum.
480
+ * @param out The matrix to store the result in.
481
+ * @returns The perspective projection matrix.
482
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
483
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
484
+ */
485
+ export function perspective<T extends Matrix4Like>(fov: number, aspect: number, near: number, far: number, out: T): T {
486
+ const f: number = 1 / Math.tan(fov / 2);
487
+
488
+ out[0] = f / aspect;
489
+ out[1] = 0;
490
+ out[2] = 0;
491
+ out[3] = 0;
492
+ out[4] = 0;
493
+ out[5] = f;
494
+ out[6] = 0;
495
+ out[7] = 0;
496
+ out[8] = 0;
497
+ out[9] = 0;
498
+ out[11] = -1;
499
+ out[12] = 0;
500
+ out[13] = 0;
501
+ out[15] = 0;
502
+
503
+ if (far != null && far != Infinity) {
504
+ const nf: number = 1 / (near - far);
505
+ out[10] = (far + near) * nf;
506
+ out[14] = 2 * far * near * nf;
507
+ } else {
508
+ out[10] = -1;
509
+ out[14] = -2 * near;
510
+ }
511
+
512
+ return out;
513
+ }
514
+
515
+ /**
516
+ * Creates a perspective projection matrix from a field of view. Useful for generating projection matrices to be used with the WebXR API.
517
+ * @param fov The field of view.
518
+ * @param near The near bound of the frustum.
519
+ * @param far The far bound of the frustum.
520
+ * @param out The matrix to store the result in.
521
+ * @returns The perspective projection matrix.
522
+ * @see [WebXR API](https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API)
523
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
524
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
525
+ */
526
+ export function perspectiveFromFieldOfView<T extends Matrix4Like>(fov: FieldOfView, near: number, far: number, out: T): T {
527
+ const upTan: number = Math.tan((fov.upDegrees * Math.PI) / 180);
528
+ const downTan: number = Math.tan((fov.downDegrees * Math.PI) / 180);
529
+ const leftTan: number = Math.tan((fov.leftDegrees * Math.PI) / 180);
530
+ const rightTan: number = Math.tan((fov.rightDegrees * Math.PI) / 180);
531
+ const xScale: number = 2 / (leftTan + rightTan);
532
+ const yScale: number = 2 / (upTan + downTan);
533
+
534
+ out[0] = xScale;
535
+ out[1] = 0;
536
+ out[2] = 0;
537
+ out[3] = 0;
538
+ out[4] = 0;
539
+ out[5] = yScale;
540
+ out[6] = 0;
541
+ out[7] = 0;
542
+ out[8] = -((leftTan - rightTan) * xScale * 0.5);
543
+ out[9] = (upTan - downTan) * yScale * 0.5;
544
+ out[10] = far / (near - far);
545
+ out[11] = -1;
546
+ out[12] = 0;
547
+ out[13] = 0;
548
+ out[14] = (far * near) / (near - far);
549
+ out[15] = 0;
550
+ return out;
551
+ }
552
+
553
+ /**
554
+ * Generates an orthogonal projection matrix with the given bounds.
555
+ * @param left The left bound of the frustum.
556
+ * @param right The right bound of the frustum.
557
+ * @param bottom The bottom bound of the frustum.
558
+ * @param top The top bound of the frustum.
559
+ * @param near The near bound of the frustum.
560
+ * @param far The far bound of the frustum.
561
+ * @param out The matrix to store the result in.
562
+ * @returns The frustum matrix.
563
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
564
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
565
+ */
566
+ export function ortho<T extends Matrix4Like>(left: number, right: number, bottom: number, top: number, near: number, far: number, out: T): T {
567
+ const lr: number = 1 / (left - right);
568
+ const bt: number = 1 / (bottom - top);
569
+ const nf: number = 1 / (near - far);
570
+
571
+ out[0] = -2 * lr;
572
+ out[1] = 0;
573
+ out[2] = 0;
574
+ out[3] = 0;
575
+ out[4] = 0;
576
+ out[5] = -2 * bt;
577
+ out[6] = 0;
578
+ out[7] = 0;
579
+ out[8] = 0;
580
+ out[9] = 0;
581
+ out[10] = 2 * nf;
582
+ out[11] = 0;
583
+ out[12] = (left + right) * lr;
584
+ out[13] = (top + bottom) * bt;
585
+ out[14] = (far + near) * nf;
586
+ out[15] = 1;
587
+ return out;
588
+ }
589
+
590
+ /**
591
+ * Generates a look-at matrix. If you want a matrix that actually makes an object look at another object, use `targetTo` instead.
592
+ * @param eye The position of the viewer.
593
+ * @param center The point that the viewer is looking at.
594
+ * @param up The local up direction.
595
+ * @param out The matrix to store the result in.
596
+ * @returns The look-at matrix.
597
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
598
+ */
599
+ export function lookAt<T extends Matrix4Like>(eye: Vector3Like, center: Vector3Like, up: Vector3Like, out: T): T {
600
+ const eyex: number = eye[0];
601
+ const eyey: number = eye[1];
602
+ const eyez: number = eye[2];
603
+
604
+ const upx: number = up[0];
605
+ const upy: number = up[1];
606
+ const upz: number = up[2];
607
+
608
+ const centerx: number = center[0];
609
+ const centery: number = center[1];
610
+ const centerz: number = center[2];
611
+
612
+ if (Math.abs(eyex - centerx) < epsilon
613
+ && Math.abs(eyey - centery) < epsilon
614
+ && Math.abs(eyez - centerz) < epsilon) {
615
+ return identity(out);
616
+ }
617
+
618
+ let z0: number = eyex - centerx;
619
+ let z1: number = eyey - centery;
620
+ let z2: number = eyez - centerz;
621
+ let len: number = 1 / Math.hypot(z0, z1, z2);
622
+ z0 *= len;
623
+ z1 *= len;
624
+ z2 *= len;
625
+
626
+ let x0: number = upy * z2 - upz * z1;
627
+ let x1: number = upz * z0 - upx * z2;
628
+ let x2: number = upx * z1 - upy * z0;
629
+ len = Math.hypot(x0, x1, x2);
630
+ if (!len) {
631
+ x0 = 0;
632
+ x1 = 0;
633
+ x2 = 0;
634
+ } else {
635
+ len = 1 / len;
636
+ x0 *= len;
637
+ x1 *= len;
638
+ x2 *= len;
639
+ }
640
+
641
+ let y0: number = z1 * x2 - z2 * x1;
642
+ let y1: number = z2 * x0 - z0 * x2;
643
+ let y2: number = z0 * x1 - z1 * x0;
644
+ len = Math.hypot(y0, y1, y2);
645
+ if (!len) {
646
+ y0 = 0;
647
+ y1 = 0;
648
+ y2 = 0;
649
+ } else {
650
+ len = 1 / len;
651
+ y0 *= len;
652
+ y1 *= len;
653
+ y2 *= len;
654
+ }
655
+
656
+ out[0] = x0;
657
+ out[1] = y0;
658
+ out[2] = z0;
659
+ out[3] = 0;
660
+ out[4] = x1;
661
+ out[5] = y1;
662
+ out[6] = z1;
663
+ out[7] = 0;
664
+ out[8] = x2;
665
+ out[9] = y2;
666
+ out[10] = z2;
667
+ out[11] = 0;
668
+ out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
669
+ out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
670
+ out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
671
+ out[15] = 1;
672
+ return out;
673
+ }
674
+
675
+ /**
676
+ * Creates a matrix that makes something look at something else.
677
+ * @param eye The position of the viewer.
678
+ * @param center The point that the viewer is looking at.
679
+ * @param up The local up direction.
680
+ * @param out The matrix to store the result in.
681
+ * @returns The transformation matrix.
682
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
683
+ */
684
+ export function targetTo<T extends Matrix4>(eye: Vector3Like, target: Vector3Like, up: Vector3Like, out: T): T {
685
+ const eyex: number = eye[0];
686
+ const eyey: number = eye[1];
687
+ const eyez: number = eye[2];
688
+
689
+ const upx: number = up[0];
690
+ const upy: number = up[1];
691
+ const upz: number = up[2];
692
+
693
+ let z0: number = eyex - target[0];
694
+ let z1: number = eyey - target[1];
695
+ let z2: number = eyez - target[2];
696
+ let len: number = z0 * z0 + z1 * z1 + z2 * z2;
697
+ if (len > 0) {
698
+ len = 1 / Math.sqrt(len);
699
+ z0 *= len;
700
+ z1 *= len;
701
+ z2 *= len;
702
+ }
703
+
704
+ let x0: number = upy * z2 - upz * z1;
705
+ let x1: number = upz * z0 - upx * z2;
706
+ let x2: number = upx * z1 - upy * z0;
707
+ len = x0 * x0 + x1 * x1 + x2 * x2;
708
+ if (len > 0) {
709
+ len = 1 / Math.sqrt(len);
710
+ x0 *= len;
711
+ x1 *= len;
712
+ x2 *= len;
713
+ }
714
+
715
+ out[0] = x0;
716
+ out[1] = x1;
717
+ out[2] = x2;
718
+ out[3] = 0;
719
+ out[4] = z1 * x2 - z2 * x1;
720
+ out[5] = z2 * x0 - z0 * x2;
721
+ out[6] = z0 * x1 - z1 * x0;
722
+ out[7] = 0;
723
+ out[8] = z0;
724
+ out[9] = z1;
725
+ out[10] = z2;
726
+ out[11] = 0;
727
+ out[12] = eyex;
728
+ out[13] = eyey;
729
+ out[14] = eyez;
730
+ out[15] = 1;
731
+ return out;
732
+ }
733
+
734
+ /**
735
+ * Adds two matrices.
736
+ * @param a The augend.
737
+ * @param b The addend.
738
+ * @param out The matrix to store the result in.
739
+ * @returns The sum.
740
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
741
+ */
742
+ export function add<T extends Matrix4Like>(a: Matrix4Like, b: Matrix4Like, out: T): T {
743
+ out[0] = a[0] + b[0];
744
+ out[1] = a[1] + b[1];
745
+ out[2] = a[2] + b[2];
746
+ out[3] = a[3] + b[3];
747
+ out[4] = a[4] + b[4];
748
+ out[5] = a[5] + b[5];
749
+ out[6] = a[6] + b[6];
750
+ out[7] = a[7] + b[7];
751
+ out[8] = a[8] + b[8];
752
+ out[9] = a[9] + b[8];
753
+ out[10] = a[10] + b[10];
754
+ out[11] = a[11] + b[11];
755
+ out[12] = a[12] + b[12];
756
+ out[13] = a[13] + b[13];
757
+ out[14] = a[14] + b[14];
758
+ out[15] = a[15] + b[15];
759
+ return out;
760
+ }
761
+
762
+ /**
763
+ * Creates a two-by-two matrix with the given values.
764
+ * @param c0r0 The value in the first column and first row.
765
+ * @param c0r1 The value in the first column and second row.
766
+ * @param c0r2 The value in the first column and third row.
767
+ * @param c0r3 The value in the first column and fourth row.
768
+ * @param c1r0 The value in the second column and first row.
769
+ * @param c1r1 The value in the second column and second row.
770
+ * @param c1r2 The value in the second column and third row.
771
+ * @param c1r3 The value in the second column and fourth row.
772
+ * @param c2r0 The value in the third column and first row.
773
+ * @param c2r1 The value in the third column and second row.
774
+ * @param c2r2 The value in the third column and third row.
775
+ * @param c2r3 The value in the third column and fourth row.
776
+ * @param c3r0 The value in the fourth column and first row.
777
+ * @param c3r1 The value in the fourth column and second row.
778
+ * @param c3r2 The value in the fourth column and third row.
779
+ * @param c3r3 The value in the fourth column and fourth row.
780
+ * @param out The matrix to store the result in.
781
+ * @returns The matrix.
782
+ */
783
+ export function fromValues<T extends Matrix4Like>(c0r0: number, c0r1: number, c0r2: number, c0r3: number, c1r0: number, c1r1: number, c1r2: number, c1r3: number, c2r0: number, c2r1: number, c2r2: number, c2r3: number, c3r0: number, c3r1: number, c3r2: number, c3r3: number, out: T): T {
784
+ out[0] = c0r0;
785
+ out[1] = c0r1;
786
+ out[2] = c0r2;
787
+ out[3] = c0r3;
788
+ out[4] = c1r0;
789
+ out[5] = c1r1;
790
+ out[6] = c1r2;
791
+ out[7] = c1r3;
792
+ out[8] = c2r0;
793
+ out[9] = c2r1;
794
+ out[10] = c2r2;
795
+ out[11] = c2r3;
796
+ out[12] = c3r0;
797
+ out[13] = c3r1;
798
+ out[14] = c3r2;
799
+ out[15] = c3r3;
800
+ return out;
801
+ }
802
+
803
+ /**
804
+ * Determines whether two matrices are roughly equivalent.
805
+ * @param a The first matrix.
806
+ * @param b The second matrix.
807
+ * @returns Whether the matrices are equivalent.
808
+ */
809
+ export function equals(a: Matrix4Like, b: Matrix4Like): boolean {
810
+ const a0: number = a[0];
811
+ const a1: number = a[1];
812
+ const a2: number = a[2];
813
+ const a3: number = a[3];
814
+ const a4: number = a[4];
815
+ const a5: number = a[5];
816
+ const a6: number = a[6];
817
+ const a7: number = a[7];
818
+ const a8: number = a[8];
819
+ const a9: number = a[9];
820
+ const a10: number = a[10];
821
+ const a11: number = a[11];
822
+ const a12: number = a[12];
823
+ const a13: number = a[13];
824
+ const a14: number = a[14];
825
+ const a15: number = a[15];
826
+
827
+ const b0: number = b[0];
828
+ const b1: number = b[1];
829
+ const b2: number = b[2];
830
+ const b3: number = b[3];
831
+ const b4: number = b[4];
832
+ const b5: number = b[5];
833
+ const b6: number = b[6];
834
+ const b7: number = b[7];
835
+ const b8: number = b[8];
836
+ const b9: number = b[9];
837
+ const b10: number = b[10];
838
+ const b11: number = b[11];
839
+ const b12: number = b[12];
840
+ const b13: number = b[13];
841
+ const b14: number = b[14];
842
+ const b15: number = b[15];
843
+
844
+ return (Math.abs(a0 - b0) <= epsilon * Math.max(1, Math.abs(a0), Math.abs(b0))
845
+ && Math.abs(a1 - b1) <= epsilon * Math.max(1, Math.abs(a1), Math.abs(b1))
846
+ && Math.abs(a2 - b2) <= epsilon * Math.max(1, Math.abs(a2), Math.abs(b2))
847
+ && Math.abs(a3 - b3) <= epsilon * Math.max(1, Math.abs(a3), Math.abs(b3))
848
+ && Math.abs(a4 - b4) <= epsilon * Math.max(1, Math.abs(a4), Math.abs(b4))
849
+ && Math.abs(a5 - b5) <= epsilon * Math.max(1, Math.abs(a5), Math.abs(b5))
850
+ && Math.abs(a6 - b6) <= epsilon * Math.max(1, Math.abs(a6), Math.abs(b6))
851
+ && Math.abs(a7 - b7) <= epsilon * Math.max(1, Math.abs(a7), Math.abs(b7))
852
+ && Math.abs(a8 - b8) <= epsilon * Math.max(1, Math.abs(a8), Math.abs(b8))
853
+ && Math.abs(a9 - b9) <= epsilon * Math.max(1, Math.abs(a9), Math.abs(b9))
854
+ && Math.abs(a10 - b10) <= epsilon * Math.max(1, Math.abs(a10), Math.abs(b10))
855
+ && Math.abs(a11 - b11) <= epsilon * Math.max(1, Math.abs(a11), Math.abs(b11))
856
+ && Math.abs(a12 - b12) <= epsilon * Math.max(1, Math.abs(a12), Math.abs(b12))
857
+ && Math.abs(a13 - b13) <= epsilon * Math.max(1, Math.abs(a13), Math.abs(b13))
858
+ && Math.abs(a14 - b14) <= epsilon * Math.max(1, Math.abs(a14), Math.abs(b14))
859
+ && Math.abs(a15 - b15) <= epsilon * Math.max(1, Math.abs(a15), Math.abs(b15)));
860
+ }
861
+
862
+ /**
863
+ * Determines whether two matrices are exactly equivalent.
864
+ * @param a The first matrix.
865
+ * @param b The second matrix.
866
+ * @returns Whether the matrices are equivalent.
867
+ */
868
+ export function exactEquals(a: Matrix4Like, b: Matrix4Like): boolean {
869
+ return a[0] == b[0]
870
+ && a[1] == b[1]
871
+ && a[2] == b[2]
872
+ && a[3] == b[3]
873
+ && a[4] == b[4]
874
+ && a[5] == b[5]
875
+ && a[6] == b[6]
876
+ && a[7] == b[7]
877
+ && a[8] == b[8]
878
+ && a[9] == b[9]
879
+ && a[10] == b[10]
880
+ && a[11] == b[11]
881
+ && a[12] == b[12]
882
+ && a[13] == b[13]
883
+ && a[14] == b[14]
884
+ && a[15] == b[15];
885
+ }
886
+
887
+ /**
888
+ * Calculates the adjugate of a matrix.
889
+ * @param matrix The matrix.
890
+ * @param out The matrix to store the result in.
891
+ * @returns The adjugate of the matrix.
892
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
893
+ */
894
+ export function adjoint<T extends Matrix4Like>(matrix: Matrix4Like, out: T): T {
895
+ const a00: number = matrix[0];
896
+ const a01: number = matrix[1];
897
+ const a02: number = matrix[2];
898
+ const a03: number = matrix[3];
899
+ const a10: number = matrix[4];
900
+ const a11: number = matrix[5];
901
+ const a12: number = matrix[6];
902
+ const a13: number = matrix[7];
903
+ const a20: number = matrix[8];
904
+ const a21: number = matrix[9];
905
+ const a22: number = matrix[10];
906
+ const a23: number = matrix[11];
907
+ const a30: number = matrix[12];
908
+ const a31: number = matrix[13];
909
+ const a32: number = matrix[14];
910
+ const a33: number = matrix[15];
911
+
912
+ out[0] =
913
+ a11 * (a22 * a33 - a23 * a32) -
914
+ a21 * (a12 * a33 - a13 * a32) +
915
+ a31 * (a12 * a23 - a13 * a22);
916
+ out[1] = -(
917
+ a01 * (a22 * a33 - a23 * a32) -
918
+ a21 * (a02 * a33 - a03 * a32) +
919
+ a31 * (a02 * a23 - a03 * a22));
920
+ out[2] =
921
+ a01 * (a12 * a33 - a13 * a32) -
922
+ a11 * (a02 * a33 - a03 * a32) +
923
+ a31 * (a02 * a13 - a03 * a12);
924
+ out[3] = -(
925
+ a01 * (a12 * a23 - a13 * a22) -
926
+ a11 * (a02 * a23 - a03 * a22) +
927
+ a21 * (a02 * a13 - a03 * a12));
928
+ out[4] = -(
929
+ a10 * (a22 * a33 - a23 * a32) -
930
+ a20 * (a12 * a33 - a13 * a32) +
931
+ a30 * (a12 * a23 - a13 * a22));
932
+ out[5] =
933
+ a00 * (a22 * a33 - a23 * a32) -
934
+ a20 * (a02 * a33 - a03 * a32) +
935
+ a30 * (a02 * a23 - a03 * a22);
936
+ out[6] = -(
937
+ a00 * (a12 * a33 - a13 * a32) -
938
+ a10 * (a02 * a33 - a03 * a32) +
939
+ a30 * (a02 * a13 - a03 * a12));
940
+ out[7] =
941
+ a00 * (a12 * a23 - a13 * a22) -
942
+ a10 * (a02 * a23 - a03 * a22) +
943
+ a20 * (a02 * a13 - a03 * a12);
944
+ out[8] =
945
+ a10 * (a21 * a33 - a23 * a31) -
946
+ a20 * (a11 * a33 - a13 * a31) +
947
+ a30 * (a11 * a23 - a13 * a21);
948
+ out[9] = -(
949
+ a00 * (a21 * a33 - a23 * a31) -
950
+ a20 * (a01 * a33 - a03 * a31) +
951
+ a30 * (a01 * a23 - a03 * a21));
952
+ out[10] =
953
+ a00 * (a11 * a33 - a13 * a31) -
954
+ a10 * (a01 * a33 - a03 * a31) +
955
+ a30 * (a01 * a13 - a03 * a11);
956
+ out[11] = -(
957
+ a00 * (a11 * a23 - a13 * a21) -
958
+ a10 * (a01 * a23 - a03 * a21) +
959
+ a20 * (a01 * a13 - a03 * a11));
960
+ out[12] = -(
961
+ a10 * (a21 * a32 - a22 * a31) -
962
+ a20 * (a11 * a32 - a12 * a31) +
963
+ a30 * (a11 * a22 - a12 * a21));
964
+ out[13] =
965
+ a00 * (a21 * a32 - a22 * a31) -
966
+ a20 * (a01 * a32 - a02 * a31) +
967
+ a30 * (a01 * a22 - a02 * a21);
968
+ out[14] = -(
969
+ a00 * (a11 * a32 - a12 * a31) -
970
+ a10 * (a01 * a32 - a02 * a31) +
971
+ a30 * (a01 * a12 - a02 * a11));
972
+ out[15] =
973
+ a00 * (a11 * a22 - a12 * a21) -
974
+ a10 * (a01 * a22 - a02 * a21) +
975
+ a20 * (a01 * a12 - a02 * a11);
976
+ return out;
977
+ }
978
+
979
+ /**
980
+ * Copies the values of one matrix into another.
981
+ * @param matrix The matrix to copy.
982
+ * @param out The matrix to store the result in.
983
+ * @returns This matrix.
984
+ */
985
+ export function copy<T extends Matrix4Like>(matrix: Matrix4Like, out: T): T {
986
+ out[0] = matrix[0];
987
+ out[1] = matrix[1];
988
+ out[2] = matrix[2];
989
+ out[3] = matrix[3];
990
+ out[4] = matrix[4];
991
+ out[5] = matrix[5];
992
+ out[6] = matrix[6];
993
+ out[7] = matrix[7];
994
+ out[8] = matrix[8];
995
+ out[9] = matrix[9];
996
+ out[10] = matrix[10];
997
+ out[11] = matrix[11];
998
+ out[12] = matrix[12];
999
+ out[13] = matrix[13];
1000
+ out[14] = matrix[14];
1001
+ out[15] = matrix[15];
1002
+ return out;
1003
+ }
1004
+
1005
+ /**
1006
+ * Calculates the Frobenius norm of a matrix.
1007
+ * @param matrix The matrix.
1008
+ * @returns The Frobenius norm.
1009
+ * @see [Matrix norm](https://en.wikipedia.org/wiki/Matrix_norm)
1010
+ */
1011
+ export function frob(matrix: Matrix4Like): number {
1012
+ return Math.hypot(
1013
+ matrix[0] as number, matrix[1] as number, matrix[2] as number, matrix[3] as number,
1014
+ matrix[4] as number, matrix[5] as number, matrix[6] as number, matrix[7] as number,
1015
+ matrix[8] as number, matrix[9] as number, matrix[10] as number, matrix[11] as number,
1016
+ matrix[12] as number, matrix[13] as number, matrix[14] as number, matrix[15] as number
1017
+ );
1018
+ }
1019
+
1020
+ /**
1021
+ * Multiplies two matrices.
1022
+ * @param a The multiplicand.
1023
+ * @param b The multiplier.
1024
+ * @param out The matrix to store the result in.
1025
+ * @returns The product.
1026
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1027
+ */
1028
+ export function multiply<T extends Matrix4Like>(a: Matrix4Like, b: Matrix4Like, out: T): T {
1029
+ const a00: number = a[0];
1030
+ const a01: number = a[1];
1031
+ const a02: number = a[2];
1032
+ const a03: number = a[3];
1033
+ const a10: number = a[4];
1034
+ const a11: number = a[5];
1035
+ const a12: number = a[6];
1036
+ const a13: number = a[7];
1037
+ const a20: number = a[8];
1038
+ const a21: number = a[9];
1039
+ const a22: number = a[10];
1040
+ const a23: number = a[11];
1041
+ const a30: number = a[12];
1042
+ const a31: number = a[13];
1043
+ const a32: number = a[14];
1044
+ const a33: number = a[15];
1045
+
1046
+ let b0: number = b[0];
1047
+ let b1: number = b[1];
1048
+ let b2: number = b[2];
1049
+ let b3: number = b[3];
1050
+ out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1051
+ out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1052
+ out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1053
+ out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1054
+ b0 = b[4];
1055
+ b1 = b[5];
1056
+ b2 = b[6];
1057
+ b3 = b[7];
1058
+ out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1059
+ out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1060
+ out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1061
+ out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1062
+ b0 = b[8];
1063
+ b1 = b[9];
1064
+ b2 = b[10];
1065
+ b3 = b[11];
1066
+ out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1067
+ out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1068
+ out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1069
+ out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1070
+ b0 = b[12];
1071
+ b1 = b[13];
1072
+ b2 = b[14];
1073
+ b3 = b[15];
1074
+ out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1075
+ out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1076
+ out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1077
+ out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1078
+ return out;
1079
+ }
1080
+
1081
+ /**
1082
+ * Multiplies a matrix by a scalar value.
1083
+ * @param matrix The multiplicand.
1084
+ * @param scalar The multiplier.
1085
+ * @param out The matrix to store the result in.
1086
+ * @returns The product.
1087
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1088
+ */
1089
+ export function multiplyScalar<T extends Matrix4Like>(matrix: Matrix4Like, scalar: number, out: T): T {
1090
+ out[0] = matrix[0] * scalar;
1091
+ out[1] = matrix[1] * scalar;
1092
+ out[2] = matrix[2] * scalar;
1093
+ out[3] = matrix[3] * scalar;
1094
+ out[4] = matrix[4] * scalar;
1095
+ out[5] = matrix[5] * scalar;
1096
+ out[6] = matrix[6] * scalar;
1097
+ out[7] = matrix[7] * scalar;
1098
+ out[8] = matrix[8] * scalar;
1099
+ out[9] = matrix[9] * scalar;
1100
+ out[10] = matrix[10] * scalar;
1101
+ out[11] = matrix[11] * scalar;
1102
+ out[12] = matrix[12] * scalar;
1103
+ out[13] = matrix[13] * scalar;
1104
+ out[14] = matrix[14] * scalar;
1105
+ out[15] = matrix[15] * scalar;
1106
+ return out;
1107
+ }
1108
+
1109
+ /**
1110
+ * Adds a matrix to another after multiplying the other by a scalar.
1111
+ * @param a The augend.
1112
+ * @param b The addend.
1113
+ * @param scalar The multiplier.
1114
+ * @param out The matrix to store the result in.
1115
+ * @returns The sum.
1116
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
1117
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
1118
+ */
1119
+ export function multiplyScalarAndAdd<T extends Matrix4Like>(a: Matrix4Like, b: Matrix4Like, scalar: number, out: T): T {
1120
+ out[0] = a[0] + b[0] * scalar;
1121
+ out[1] = a[1] + b[1] * scalar;
1122
+ out[2] = a[2] + b[2] * scalar;
1123
+ out[3] = a[3] + b[3] * scalar;
1124
+ out[4] = a[4] + b[4] * scalar;
1125
+ out[5] = a[5] + b[5] * scalar;
1126
+ out[6] = a[6] + b[6] * scalar;
1127
+ out[7] = a[7] + b[7] * scalar;
1128
+ out[8] = a[8] + b[8] * scalar;
1129
+ out[9] = a[9] + b[9] * scalar;
1130
+ out[10] = a[10] + b[10] * scalar;
1131
+ out[11] = a[11] + b[11] * scalar;
1132
+ out[12] = a[12] + b[12] * scalar;
1133
+ out[13] = a[13] + b[13] * scalar;
1134
+ out[14] = a[14] + b[14] * scalar;
1135
+ out[15] = a[15] + b[15] * scalar;
1136
+ return out;
1137
+ }
1138
+
1139
+ /**
1140
+ * Subtracts two matrices.
1141
+ * @param a The minuend.
1142
+ * @param b The subtrahend.
1143
+ * @param out The matrix to store the result in.
1144
+ * @returns The difference.
1145
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
1146
+ */
1147
+ export function subtract<T extends Matrix4Like>(a: Matrix4Like, b: Matrix4Like, out: T): T {
1148
+ out[0] = a[0] - b[0];
1149
+ out[1] = a[1] - b[1];
1150
+ out[2] = a[2] - b[2];
1151
+ out[3] = a[3] - b[3];
1152
+ out[4] = a[4] - b[4];
1153
+ out[5] = a[5] - b[5];
1154
+ out[6] = a[6] - b[6];
1155
+ out[7] = a[7] - b[7];
1156
+ out[8] = a[8] - b[8];
1157
+ out[9] = a[9] - b[8];
1158
+ out[10] = a[10] - b[10];
1159
+ out[11] = a[11] - b[11];
1160
+ out[12] = a[12] - b[12];
1161
+ out[13] = a[13] - b[13];
1162
+ out[14] = a[14] - b[14];
1163
+ out[15] = a[15] - b[15];
1164
+ return out;
1165
+ }
1166
+
1167
+ /**
1168
+ * Transposes a matrix.
1169
+ * @param matrix The matrix.
1170
+ * @param out The matrix to store the result in.
1171
+ * @returns The transpose.
1172
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
1173
+ */
1174
+ export function transpose<T extends Matrix4Like>(matrix: Matrix4Like, out: T): T {
1175
+ if (out == matrix as unknown as T) {
1176
+ const a01: number = matrix[1];
1177
+ const a02: number = matrix[2];
1178
+ const a03: number = matrix[3];
1179
+ const a12: number = matrix[6];
1180
+ const a13: number = matrix[7];
1181
+ const a23: number = matrix[11];
1182
+ out[1] = matrix[4];
1183
+ out[2] = matrix[8];
1184
+ out[3] = matrix[12];
1185
+ out[4] = a01;
1186
+ out[6] = matrix[9];
1187
+ out[7] = matrix[13];
1188
+ out[8] = a02;
1189
+ out[9] = a12;
1190
+ out[11] = matrix[14];
1191
+ out[12] = a03;
1192
+ out[13] = a13;
1193
+ out[14] = a23;
1194
+ } else {
1195
+ out[0] = matrix[0];
1196
+ out[1] = matrix[4];
1197
+ out[2] = matrix[8];
1198
+ out[3] = matrix[12];
1199
+ out[4] = matrix[1];
1200
+ out[5] = matrix[5];
1201
+ out[6] = matrix[9];
1202
+ out[7] = matrix[13];
1203
+ out[8] = matrix[2];
1204
+ out[9] = matrix[6];
1205
+ out[10] = matrix[10];
1206
+ out[11] = matrix[14];
1207
+ out[12] = matrix[3];
1208
+ out[13] = matrix[7];
1209
+ out[14] = matrix[11];
1210
+ out[15] = matrix[15];
1211
+ }
1212
+
1213
+ return out;
1214
+ }
1215
+
1216
+ /**
1217
+ * Calculates the determinant of a matrix.
1218
+ * @param matrix The matrix.
1219
+ * @returns The determinant.
1220
+ * @see [Determinant](https://en.wikipedia.org/wiki/Determinant)
1221
+ */
1222
+ export function determinant(matrix: Matrix4Like): number {
1223
+ const a00: number = matrix[0];
1224
+ const a01: number = matrix[1];
1225
+ const a02: number = matrix[2];
1226
+ const a03: number = matrix[3];
1227
+ const a10: number = matrix[4];
1228
+ const a11: number = matrix[5];
1229
+ const a12: number = matrix[6];
1230
+ const a13: number = matrix[7];
1231
+ const a20: number = matrix[8];
1232
+ const a21: number = matrix[9];
1233
+ const a22: number = matrix[10];
1234
+ const a23: number = matrix[11];
1235
+ const a30: number = matrix[12];
1236
+ const a31: number = matrix[13];
1237
+ const a32: number = matrix[14];
1238
+ const a33: number = matrix[15];
1239
+
1240
+ const b00: number = a00 * a11 - a01 * a10;
1241
+ const b01: number = a00 * a12 - a02 * a10;
1242
+ const b02: number = a00 * a13 - a03 * a10;
1243
+ const b03: number = a01 * a12 - a02 * a11;
1244
+ const b04: number = a01 * a13 - a03 * a11;
1245
+ const b05: number = a02 * a13 - a03 * a12;
1246
+ const b06: number = a20 * a31 - a21 * a30;
1247
+ const b07: number = a20 * a32 - a22 * a30;
1248
+ const b08: number = a20 * a33 - a23 * a30;
1249
+ const b09: number = a21 * a32 - a22 * a31;
1250
+ const b10: number = a21 * a33 - a23 * a31;
1251
+ const b11: number = a22 * a33 - a23 * a32;
1252
+
1253
+ return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
1254
+ }
1255
+
1256
+ /**
1257
+ * Inverts a matrix.
1258
+ * @param matrix The matrix.
1259
+ * @param out The matrix to store the result in.
1260
+ * @returns The inverted matrix.
1261
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
1262
+ */
1263
+ export function invert<T extends Matrix4Like>(matrix: Matrix4Like, out: T): T {
1264
+ const a00: number = matrix[0];
1265
+ const a01: number = matrix[1];
1266
+ const a02: number = matrix[2];
1267
+ const a03: number = matrix[3];
1268
+ const a10: number = matrix[4];
1269
+ const a11: number = matrix[5];
1270
+ const a12: number = matrix[6];
1271
+ const a13: number = matrix[7];
1272
+ const a20: number = matrix[8];
1273
+ const a21: number = matrix[9];
1274
+ const a22: number = matrix[10];
1275
+ const a23: number = matrix[11];
1276
+ const a30: number = matrix[12];
1277
+ const a31: number = matrix[13];
1278
+ const a32: number = matrix[14];
1279
+ const a33: number = matrix[15];
1280
+
1281
+ const b00: number = a00 * a11 - a01 * a10;
1282
+ const b01: number = a00 * a12 - a02 * a10;
1283
+ const b02: number = a00 * a13 - a03 * a10;
1284
+ const b03: number = a01 * a12 - a02 * a11;
1285
+ const b04: number = a01 * a13 - a03 * a11;
1286
+ const b05: number = a02 * a13 - a03 * a12;
1287
+ const b06: number = a20 * a31 - a21 * a30;
1288
+ const b07: number = a20 * a32 - a22 * a30;
1289
+ const b08: number = a20 * a33 - a23 * a30;
1290
+ const b09: number = a21 * a32 - a22 * a31;
1291
+ const b10: number = a21 * a33 - a23 * a31;
1292
+ const b11: number = a22 * a33 - a23 * a32;
1293
+
1294
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
1295
+ if (!det) {
1296
+ throw new SingularMatrixError();
1297
+ }
1298
+ det = 1 / det;
1299
+
1300
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
1301
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
1302
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
1303
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
1304
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
1305
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
1306
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
1307
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
1308
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
1309
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
1310
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
1311
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
1312
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
1313
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
1314
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
1315
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
1316
+ return out;
1317
+ }
1318
+
1319
+ /**
1320
+ * Scales a matrix by the given vector.
1321
+ * @param matrix The matrix.
1322
+ * @param vector The scaling vector.
1323
+ * @param out The matrix to store the result in.
1324
+ * @returns The scaled matrix.
1325
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1326
+ */
1327
+ export function scale<T extends Matrix4Like>(matrix: Matrix4Like, vector: Vector3Like, out: T): T {
1328
+ const x: number = vector[0];
1329
+ const y: number = vector[1];
1330
+ const z: number = vector[2];
1331
+
1332
+ out[0] = matrix[0] * x;
1333
+ out[1] = matrix[1] * x;
1334
+ out[2] = matrix[2] * x;
1335
+ out[3] = matrix[3] * x;
1336
+ out[4] = matrix[4] * y;
1337
+ out[5] = matrix[5] * y;
1338
+ out[6] = matrix[6] * y;
1339
+ out[7] = matrix[7] * y;
1340
+ out[8] = matrix[8] * z;
1341
+ out[9] = matrix[9] * z;
1342
+ out[10] = matrix[10] * z;
1343
+ out[11] = matrix[11] * z;
1344
+ out[12] = matrix[12];
1345
+ out[13] = matrix[13];
1346
+ out[14] = matrix[14];
1347
+ out[15] = matrix[15];
1348
+ return out;
1349
+ }
1350
+
1351
+ /**
1352
+ * Translates a matrix by the given vector.
1353
+ * @param matrix The matrix.
1354
+ * @param vector The translation vector.
1355
+ * @param out The matrix to store the result in.
1356
+ * @returns The translated matrix.
1357
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1358
+ */
1359
+ export function translate<T extends Matrix4Like>(matrix: Matrix4Like, vector: Vector3Like, out: T): T {
1360
+ const x: number = vector[0];
1361
+ const y: number = vector[1];
1362
+ const z: number = vector[2];
1363
+
1364
+ if (out == matrix) {
1365
+ out[12] = matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12];
1366
+ out[13] = matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13];
1367
+ out[14] = matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14];
1368
+ out[15] = matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15];
1369
+ } else {
1370
+ const a00: number = matrix[0];
1371
+ const a01: number = matrix[1];
1372
+ const a02: number = matrix[2];
1373
+ const a03: number = matrix[3];
1374
+ const a10: number = matrix[4];
1375
+ const a11: number = matrix[5];
1376
+ const a12: number = matrix[6];
1377
+ const a13: number = matrix[7];
1378
+ const a20: number = matrix[8];
1379
+ const a21: number = matrix[9];
1380
+ const a22: number = matrix[10];
1381
+ const a23: number = matrix[11];
1382
+
1383
+ out[0] = a00;
1384
+ out[1] = a01;
1385
+ out[2] = a02;
1386
+ out[3] = a03;
1387
+ out[4] = a10;
1388
+ out[5] = a11;
1389
+ out[6] = a12;
1390
+ out[7] = a13;
1391
+ out[8] = a20;
1392
+ out[9] = a21;
1393
+ out[10] = a22;
1394
+ out[11] = a23;
1395
+ out[12] = a00 * x + a10 * y + a20 * z + matrix[12];
1396
+ out[13] = a01 * x + a11 * y + a21 * z + matrix[13];
1397
+ out[14] = a02 * x + a12 * y + a22 * z + matrix[14];
1398
+ out[15] = a03 * x + a13 * y + a23 * z + matrix[15];
1399
+ }
1400
+
1401
+ return out;
1402
+ }
1403
+
1404
+ /**
1405
+ * Rotates a matrix by the given angle around the given axis.
1406
+ * @param matrix The matrix.
1407
+ * @param radians The angle in radians.
1408
+ * @param axis The axis.
1409
+ * @param out The matrix to store the result in.
1410
+ * @returns The rotated matrix.
1411
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1412
+ */
1413
+ export function rotate<T extends Matrix4Like>(matrix: Matrix4Like, radians: number, axis: Vector3Like, out: T): T {
1414
+ let x: number = axis[0];
1415
+ let y: number = axis[1];
1416
+ let z: number = axis[2];
1417
+
1418
+ let len: number = Math.hypot(x, y, z);
1419
+ if (len < epsilon) {
1420
+ return out;
1421
+ }
1422
+
1423
+ len = 1 / len;
1424
+ x *= len;
1425
+ y *= len;
1426
+ z *= len;
1427
+
1428
+ const s: number = Math.sin(radians);
1429
+ const c: number = Math.cos(radians);
1430
+ const t: number = 1 - c;
1431
+
1432
+ const a00: number = matrix[0];
1433
+ const a01: number = matrix[1];
1434
+ const a02: number = matrix[2];
1435
+ const a03: number = matrix[3];
1436
+ const a10: number = matrix[4];
1437
+ const a11: number = matrix[5];
1438
+ const a12: number = matrix[6];
1439
+ const a13: number = matrix[7];
1440
+ const a20: number = matrix[8];
1441
+ const a21: number = matrix[9];
1442
+ const a22: number = matrix[10];
1443
+ const a23: number = matrix[11];
1444
+
1445
+ const b00: number = x * x * t + c;
1446
+ const b01: number = y * x * t + z * s;
1447
+ const b02: number = z * x * t - y * s;
1448
+ const b10: number = x * y * t - z * s;
1449
+ const b11: number = y * y * t + c;
1450
+ const b12: number = z * y * t + x * s;
1451
+ const b20: number = x * z * t + y * s;
1452
+ const b21: number = y * z * t - x * s;
1453
+ const b22: number = z * z * t + c;
1454
+
1455
+ out[0] = a00 * b00 + a10 * b01 + a20 * b02;
1456
+ out[1] = a01 * b00 + a11 * b01 + a21 * b02;
1457
+ out[2] = a02 * b00 + a12 * b01 + a22 * b02;
1458
+ out[3] = a03 * b00 + a13 * b01 + a23 * b02;
1459
+ out[4] = a00 * b10 + a10 * b11 + a20 * b12;
1460
+ out[5] = a01 * b10 + a11 * b11 + a21 * b12;
1461
+ out[6] = a02 * b10 + a12 * b11 + a22 * b12;
1462
+ out[7] = a03 * b10 + a13 * b11 + a23 * b12;
1463
+ out[8] = a00 * b20 + a10 * b21 + a20 * b22;
1464
+ out[9] = a01 * b20 + a11 * b21 + a21 * b22;
1465
+ out[10] = a02 * b20 + a12 * b21 + a22 * b22;
1466
+ out[11] = a03 * b20 + a13 * b21 + a23 * b22;
1467
+ if (out != matrix) {
1468
+ out[12] = matrix[12];
1469
+ out[13] = matrix[13];
1470
+ out[14] = matrix[14];
1471
+ out[15] = matrix[15];
1472
+ }
1473
+ return out;
1474
+ }
1475
+
1476
+ /**
1477
+ * Rotates a matrix by the given angle around the X-axis.
1478
+ * @param matrix The matrix.
1479
+ * @param radians The angle in radians.
1480
+ * @param out The matrix to store the result in.
1481
+ * @returns The rotated matrix.
1482
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1483
+ */
1484
+ export function rotateX<T extends Matrix4Like>(matrix: Matrix4Like, radians: number, out: T): T {
1485
+ const s: number = Math.sin(radians);
1486
+ const c: number = Math.cos(radians);
1487
+
1488
+ const a10: number = matrix[4];
1489
+ const a11: number = matrix[5];
1490
+ const a12: number = matrix[6];
1491
+ const a13: number = matrix[7];
1492
+ const a20: number = matrix[8];
1493
+ const a21: number = matrix[9];
1494
+ const a22: number = matrix[10];
1495
+ const a23: number = matrix[11];
1496
+
1497
+ if (out != matrix) {
1498
+ out[0] = matrix[0];
1499
+ out[1] = matrix[1];
1500
+ out[2] = matrix[2];
1501
+ out[3] = matrix[3];
1502
+ out[12] = matrix[12];
1503
+ out[13] = matrix[13];
1504
+ out[14] = matrix[14];
1505
+ out[15] = matrix[15];
1506
+ }
1507
+
1508
+ out[4] = a10 * c + a20 * s;
1509
+ out[5] = a11 * c + a21 * s;
1510
+ out[6] = a12 * c + a22 * s;
1511
+ out[7] = a13 * c + a23 * s;
1512
+ out[8] = a20 * c - a10 * s;
1513
+ out[9] = a21 * c - a11 * s;
1514
+ out[10] = a22 * c - a12 * s;
1515
+ out[11] = a23 * c - a13 * s;
1516
+ return out;
1517
+ }
1518
+
1519
+ /**
1520
+ * Rotates a matrix by the given angle around the Y-axis.
1521
+ * @param matrix The matrix.
1522
+ * @param radians The angle in radians.
1523
+ * @param out The matrix to store the result in.
1524
+ * @returns The rotated matrix.
1525
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1526
+ */
1527
+ export function rotateY<T extends Matrix4Like>(matrix: Matrix4Like, radians: number, out: T): T {
1528
+ const s: number = Math.sin(radians);
1529
+ const c: number = Math.cos(radians);
1530
+
1531
+ const a00: number = matrix[0];
1532
+ const a01: number = matrix[1];
1533
+ const a02: number = matrix[2];
1534
+ const a03: number = matrix[3];
1535
+ const a20: number = matrix[8];
1536
+ const a21: number = matrix[9];
1537
+ const a22: number = matrix[10];
1538
+ const a23: number = matrix[11];
1539
+
1540
+ if (out != matrix) {
1541
+ out[4] = matrix[4];
1542
+ out[5] = matrix[5];
1543
+ out[6] = matrix[6];
1544
+ out[7] = matrix[7];
1545
+ out[12] = matrix[12];
1546
+ out[13] = matrix[13];
1547
+ out[14] = matrix[14];
1548
+ out[15] = matrix[15];
1549
+ }
1550
+
1551
+ out[0] = a00 * c - a20 * s;
1552
+ out[1] = a01 * c - a21 * s;
1553
+ out[2] = a02 * c - a22 * s;
1554
+ out[3] = a03 * c - a23 * s;
1555
+ out[8] = a00 * s + a20 * c;
1556
+ out[9] = a01 * s + a21 * c;
1557
+ out[10] = a02 * s + a22 * c;
1558
+ out[11] = a03 * s + a23 * c;
1559
+ return out;
1560
+ }
1561
+
1562
+ /**
1563
+ * Rotates a matrix by the given angle around the Z-axis.
1564
+ * @param matrix The matrix.
1565
+ * @param radians The angle in radians.
1566
+ * @param out The matrix to store the result in.
1567
+ * @returns The rotated matrix.
1568
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1569
+ */
1570
+ export function rotateZ<T extends Matrix4Like>(matrix: Matrix4Like, radians: number, out: T): T {
1571
+ const s: number = Math.sin(radians);
1572
+ const c: number = Math.cos(radians);
1573
+
1574
+ const a00: number = matrix[0];
1575
+ const a01: number = matrix[1];
1576
+ const a02: number = matrix[2];
1577
+ const a03: number = matrix[3];
1578
+ const a10: number = matrix[4];
1579
+ const a11: number = matrix[5];
1580
+ const a12: number = matrix[6];
1581
+ const a13: number = matrix[7];
1582
+
1583
+ if (out != matrix) {
1584
+ out[8] = matrix[8];
1585
+ out[9] = matrix[9];
1586
+ out[10] = matrix[10];
1587
+ out[11] = matrix[11];
1588
+ out[12] = matrix[12];
1589
+ out[13] = matrix[13];
1590
+ out[14] = matrix[14];
1591
+ out[15] = matrix[15];
1592
+ }
1593
+
1594
+ out[0] = a00 * c + a10 * s;
1595
+ out[1] = a01 * c + a11 * s;
1596
+ out[2] = a02 * c + a12 * s;
1597
+ out[3] = a03 * c + a13 * s;
1598
+ out[4] = a10 * c - a00 * s;
1599
+ out[5] = a11 * c - a01 * s;
1600
+ out[6] = a12 * c - a02 * s;
1601
+ out[7] = a13 * c - a03 * s;
1602
+ return out;
1603
+ }
1604
+
1605
+ /**
1606
+ * Gets the translation vector component of a transformation matrix.
1607
+ * @param matrix The matrix.
1608
+ * @param out The vector to store the result in.
1609
+ * @returns The translation vector.
1610
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1611
+ */
1612
+ export function getTranslation<T extends Vector3Like>(matrix: Matrix4Like, out: T): T {
1613
+ out[0] = matrix[12];
1614
+ out[1] = matrix[13];
1615
+ out[2] = matrix[14];
1616
+ return out;
1617
+ }
1618
+
1619
+ /**
1620
+ * Gets the scaling factor of a transformation matrix.
1621
+ * @param matrix The matrix.
1622
+ * @param out The vector to store the result in.
1623
+ * @returns The scaling vector.
1624
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1625
+ */
1626
+ export function getScaling<T extends Vector3Like>(matrix: Matrix4Like, out: T): T {
1627
+ const m11: number = matrix[0];
1628
+ const m12: number = matrix[1];
1629
+ const m13: number = matrix[2];
1630
+ const m21: number = matrix[4];
1631
+ const m22: number = matrix[5];
1632
+ const m23: number = matrix[6];
1633
+ const m31: number = matrix[8];
1634
+ const m32: number = matrix[9];
1635
+ const m33: number = matrix[10];
1636
+
1637
+ out[0] = Math.hypot(m11, m12, m13);
1638
+ out[1] = Math.hypot(m21, m22, m23);
1639
+ out[2] = Math.hypot(m31, m32, m33);
1640
+ return out;
1641
+ }
1642
+
1643
+ /**
1644
+ * Gets the rotational component of a transformation matrix.
1645
+ * @param matrix The matrix.
1646
+ * @param out The quaternion to store the result in.
1647
+ * @returns The rotation.
1648
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1649
+ */
1650
+ export function getRotation<T extends QuaternionLike>(matrix: Matrix4Like, out: T): T {
1651
+ const scaling: Vector3Like = getScaling(matrix, new Float32Array(3) as Vector3Like);
1652
+
1653
+ const is1: number = 1 / scaling[0];
1654
+ const is2: number = 1 / scaling[1];
1655
+ const is3: number = 1 / scaling[2];
1656
+
1657
+ const sm11: number = matrix[0] * is1;
1658
+ const sm12: number = matrix[1] * is2;
1659
+ const sm13: number = matrix[2] * is3;
1660
+ const sm21: number = matrix[4] * is1;
1661
+ const sm22: number = matrix[5] * is2;
1662
+ const sm23: number = matrix[6] * is3;
1663
+ const sm31: number = matrix[8] * is1;
1664
+ const sm32: number = matrix[9] * is2;
1665
+ const sm33: number = matrix[10] * is3;
1666
+
1667
+ const trace: number = sm11 + sm22 + sm33;
1668
+
1669
+ if (trace > 0) {
1670
+ const s: number = Math.sqrt(trace + 1) * 2;
1671
+ out[0] = (sm23 - sm32) / s;
1672
+ out[1] = (sm31 - sm13) / s;
1673
+ out[2] = (sm12 - sm21) / s;
1674
+ out[3] = 0.25 * s;
1675
+ return out;
1676
+ }
1677
+
1678
+ if (sm11 > sm22 && sm11 > sm33) {
1679
+ const s: number = Math.sqrt(1 + sm11 - sm22 - sm33) * 2;
1680
+ out[0] = 0.25 * s;
1681
+ out[1] = (sm12 + sm21) / s;
1682
+ out[2] = (sm31 + sm13) / s;
1683
+ out[3] = (sm23 - sm32) / s;
1684
+ return out;
1685
+ }
1686
+
1687
+ if (sm22 > sm33) {
1688
+ const s: number = Math.sqrt(1 + sm22 - sm11 - sm33) * 2;
1689
+ out[0] = (sm12 + sm21) / s;
1690
+ out[1] = 0.25 * s;
1691
+ out[2] = (sm23 + sm32) / s;
1692
+ out[3] = (sm31 - sm13) / s;
1693
+ return out;
1694
+ }
1695
+
1696
+ const s: number = Math.sqrt(1 + sm33 - sm11 - sm22) * 2;
1697
+ out[0] = (sm31 + sm13) / s;
1698
+ out[1] = (sm23 + sm32) / s;
1699
+ out[2] = 0.25 * s;
1700
+ out[3] = (sm12 - sm21) / s;
1701
+ return out;
1702
+ }
1703
+
1704
+ /**
1705
+ * A four-by-four matrix.
1706
+ * @see [Matrix](https://en.wikipedia.org/wiki/Matrix_(mathematics))
1707
+ */
1708
+ export default class Matrix4 extends Float32Array implements SquareMatrix {
1709
+ /**
1710
+ * Creates a transformation matrix that represents a translation by the given vector.
1711
+ * @param vector The translation vector.
1712
+ * @returns The transformation matrix.
1713
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1714
+ */
1715
+ public static fromTranslation(vector: Vector3Like): Matrix4;
1716
+
1717
+ /**
1718
+ * Creates a transformation matrix that represents a translation by the given vector.
1719
+ * @param vector The translation vector.
1720
+ * @param out The matrix to store the result in.
1721
+ * @returns The transformation matrix.
1722
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1723
+ */
1724
+ public static fromTranslation<T extends Matrix4Like>(vector: Vector3Like, out: T): T;
1725
+
1726
+ public static fromTranslation<T extends Matrix4Like>(vector: Vector3Like, out: T = new Matrix4() as T): T {
1727
+ return fromTranslation(vector, out);
1728
+ }
1729
+
1730
+ /**
1731
+ * Creates a transformation matrix that represents a scaling by the given vector.
1732
+ * @param vector The scaling vector.
1733
+ * @returns The transformation matrix.
1734
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1735
+ */
1736
+ public static fromScaling(vector: Vector3Like): Matrix4;
1737
+
1738
+ /**
1739
+ * Creates a transformation matrix that represents a scaling by the given vector.
1740
+ * @param vector The scaling vector.
1741
+ * @param out The matrix to store the result in.
1742
+ * @returns The transformation matrix.
1743
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1744
+ */
1745
+ public static fromScaling<T extends Matrix4Like>(vector: Vector3Like, out: T): T;
1746
+
1747
+ public static fromScaling<T extends Matrix4Like>(vector: Vector3Like, out: T = new Matrix4() as T): T {
1748
+ return fromScaling(vector, out);
1749
+ }
1750
+
1751
+ /**
1752
+ * Creates a transformation matrix that represents a rotation by the given angle around the given axis.
1753
+ * @param radians The angle in radians.
1754
+ * @param axis The axis to rotate around.
1755
+ * @returns The transformation matrix.
1756
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1757
+ */
1758
+ public static fromRotation(radians: number, axis: Vector3Like): Matrix4;
1759
+
1760
+ /**
1761
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
1762
+ * @param radians The angle in radians.
1763
+ * @param axis The axis to rotate around.
1764
+ * @param out The matrix to store the result in.
1765
+ * @returns The transformation matrix.
1766
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1767
+ */
1768
+ public static fromRotation<T extends Matrix4Like>(radians: number, axis: Vector3Like, out: T): T;
1769
+
1770
+ public static fromRotation<T extends Matrix4Like>(radians: number, axis: Vector3Like, out: T = new Matrix4() as T): T {
1771
+ return fromRotation(radians, axis, out);
1772
+ }
1773
+
1774
+ /**
1775
+ * Creates a transformation matrix that represents a rotation by the given angle around the X-axis.
1776
+ * @param radians The angle in radians.
1777
+ * @returns The transformation matrix.
1778
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1779
+ */
1780
+ public static fromXRotation(radians: number): Matrix4;
1781
+
1782
+ /**
1783
+ * Creates a transformation matrix that represents a rotation by the given angle around the X-axis.
1784
+ * @param radians The angle in radians.
1785
+ * @param out The matrix to store the result in.
1786
+ * @returns The transformation matrix.
1787
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1788
+ */
1789
+ public static fromXRotation<T extends Matrix4Like>(radians: number, out: T): T;
1790
+
1791
+ public static fromXRotation<T extends Matrix4Like>(radians: number, out: T = new Matrix4() as T): T {
1792
+ return fromXRotation(radians, out);
1793
+ }
1794
+
1795
+ /**
1796
+ * Creates a transformation matrix that represents a rotation by the given angle around the Y-axis.
1797
+ * @param radians The angle in radians.
1798
+ * @returns The transformation matrix.
1799
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1800
+ */
1801
+ public static fromYRotation(radians: number): Matrix4;
1802
+
1803
+ /**
1804
+ * Creates a transformation matrix that represents a rotation by the given angle around the Y-axis.
1805
+ * @param radians The angle in radians.
1806
+ * @param out The matrix to store the result in.
1807
+ * @returns The transformation matrix.
1808
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1809
+ */
1810
+ public static fromYRotation<T extends Matrix4Like>(radians: number, out: T): T;
1811
+
1812
+ public static fromYRotation<T extends Matrix4Like>(radians: number, out: T = new Matrix4() as T): T {
1813
+ return fromYRotation(radians, out);
1814
+ }
1815
+
1816
+ /**
1817
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
1818
+ * @param radians The angle in radians.
1819
+ * @returns The transformation matrix.
1820
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1821
+ */
1822
+ public static fromZRotation(radians: number): Matrix4;
1823
+
1824
+ /**
1825
+ * Creates a transformation matrix that represents a rotation by the given angle around the Z-axis.
1826
+ * @param radians The angle in radians.
1827
+ * @param out The matrix to store the result in.
1828
+ * @returns The transformation matrix.
1829
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1830
+ */
1831
+ public static fromZRotation<T extends Matrix4Like>(radians: number, out: T): T;
1832
+
1833
+ public static fromZRotation<T extends Matrix4Like>(radians: number, out: T = new Matrix4() as T): T {
1834
+ return fromZRotation(radians, out);
1835
+ }
1836
+
1837
+ /**
1838
+ * Creates a transformation matrix from the given rotation and translation.
1839
+ * @param rotation The rotation quaternion.
1840
+ * @param translation The translation vector.
1841
+ * @returns The transformation matrix.
1842
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1843
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1844
+ */
1845
+ public static fromRotationTranslation(rotation: QuaternionLike, translation: Vector3Like): Matrix4;
1846
+
1847
+ /**
1848
+ * Creates a transformation matrix from the given rotation and translation.
1849
+ * @param rotation The rotation quaternion.
1850
+ * @param translation The translation vector.
1851
+ * @param out The matrix to store the result in.
1852
+ * @returns The transformation matrix.
1853
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1854
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1855
+ */
1856
+ public static fromRotationTranslation<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, out: T): T;
1857
+
1858
+ public static fromRotationTranslation<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, out: T = new Matrix4() as T): T {
1859
+ return fromRotationTranslation(rotation, translation, out);
1860
+ }
1861
+
1862
+ /**
1863
+ * Creates a transformation matrix from the given rotation, translation, and scale.
1864
+ * @param rotation The rotation quaternion.
1865
+ * @param translation The translation vector.
1866
+ * @param scaling The scaling vector.
1867
+ * @returns The transformation matrix.
1868
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1869
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1870
+ */
1871
+ public static fromRotationTranslationScale(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like): Matrix4;
1872
+
1873
+ /**
1874
+ * Creates a transformation matrix from the given rotation, translation, and scale.
1875
+ * @param rotation The rotation quaternion.
1876
+ * @param translation The translation vector.
1877
+ * @param scaling The scaling vector.
1878
+ * @param out The matrix to store the result in.
1879
+ * @returns The transformation matrix.
1880
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1881
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1882
+ */
1883
+ public static fromRotationTranslationScale<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like, out: T): T;
1884
+
1885
+ public static fromRotationTranslationScale<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like, out: T = new Matrix4() as T): T {
1886
+ return fromRotationTranslationScale(rotation, translation, scaling, out);
1887
+ }
1888
+
1889
+ /**
1890
+ * Creates a transformation matrix from the given rotation, translation, and scale around the given origin.
1891
+ * @param rotation The rotation quaternion.
1892
+ * @param translation The translation vector.
1893
+ * @param scaling The scaling vector.
1894
+ * @param origin The origin vector.
1895
+ * @returns The transformation matrix.
1896
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1897
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1898
+ */
1899
+ public static fromRotationTranslationScaleOrigin(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like, origin: Vector3Like): Matrix4;
1900
+
1901
+ /**
1902
+ * Creates a transformation matrix from the given rotation, translation, and scale around the given origin.
1903
+ * @param rotation The rotation quaternion.
1904
+ * @param translation The translation vector.
1905
+ * @param scaling The scaling vector.
1906
+ * @param origin The origin vector.
1907
+ * @param out The matrix to store the result in.
1908
+ * @returns The transformation matrix.
1909
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1910
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1911
+ */
1912
+ public static fromRotationTranslationScaleOrigin<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like, origin: Vector3Like, out: T): T;
1913
+
1914
+ public static fromRotationTranslationScaleOrigin<T extends Matrix4Like>(rotation: QuaternionLike, translation: Vector3Like, scaling: Vector3Like, origin: Vector3Like, out: T = new Matrix4() as T): T {
1915
+ return fromRotationTranslationScaleOrigin(rotation, translation, scaling, origin, out);
1916
+ }
1917
+
1918
+ /**
1919
+ * Creates a transformation matrix from a dual quaternion.
1920
+ * @param quaternion The dual quaternion.
1921
+ * @returns The transformation matrix.
1922
+ * @see [Dual quaternion](https://en.wikipedia.org/wiki/Dual_quaternion)
1923
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1924
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1925
+ */
1926
+ public static fromDualQuaternion(quaternion: DualQuaternionLike): Matrix4;
1927
+
1928
+ /**
1929
+ * Creates a transformation matrix from a dual quaternion.
1930
+ * @param quaternion The dual quaternion.
1931
+ * @param out The matrix to store the result in.
1932
+ * @returns The transformation matrix.
1933
+ * @see [Dual quaternion](https://en.wikipedia.org/wiki/Dual_quaternion)
1934
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
1935
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
1936
+ */
1937
+ public static fromDualQuaternion<T extends Matrix4Like>(quaternion: DualQuaternionLike, out: T): T;
1938
+
1939
+ public static fromDualQuaternion<T extends Matrix4Like>(quaternion: DualQuaternionLike, out: T = new Matrix4() as T): T {
1940
+ return fromDualQuaternion(quaternion, out);
1941
+ }
1942
+
1943
+ /**
1944
+ * Generates a frustum matrix with the given bounds.
1945
+ * @param left The left bound of the frustum.
1946
+ * @param right The right bound of the frustum.
1947
+ * @param bottom The bottom bound of the frustum.
1948
+ * @param top The top bound of the frustum.
1949
+ * @param near The near bound of the frustum.
1950
+ * @param far The far bound of the frustum.
1951
+ * @returns The frustum matrix.
1952
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
1953
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
1954
+ */
1955
+ public static frustum(left: number, right: number, bottom: number, top: number, near: number, far: number): Matrix4;
1956
+
1957
+ /**
1958
+ * Generates a frustum matrix with the given bounds.
1959
+ * @param left The left bound of the frustum.
1960
+ * @param right The right bound of the frustum.
1961
+ * @param bottom The bottom bound of the frustum.
1962
+ * @param top The top bound of the frustum.
1963
+ * @param near The near bound of the frustum.
1964
+ * @param far The far bound of the frustum.
1965
+ * @param out The matrix to store the result in.
1966
+ * @returns The frustum matrix.
1967
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
1968
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
1969
+ */
1970
+ public static frustum<T extends Matrix4Like>(left: number, right: number, bottom: number, top: number, near: number, far: number, out: T): T;
1971
+
1972
+ public static frustum<T extends Matrix4Like>(left: number, right: number, bottom: number, top: number, near: number, far: number, out: T = new Matrix4() as T): T {
1973
+ return frustum(left, right, bottom, top, near, far, out);
1974
+ }
1975
+
1976
+ /**
1977
+ * Creates a perspective projection matrix with the given bounds.
1978
+ * @param fov The vertical field of view in radians.
1979
+ * @param aspect The aspect ratio (typically the width of the viewport divided by its height).
1980
+ * @param near The near bound of the frustum.
1981
+ * @param far The far bound of the frustum.
1982
+ * @returns The perspective projection matrix.
1983
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
1984
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
1985
+ */
1986
+ public static perspective(fov: number, aspect: number, near: number, far: number): Matrix4;
1987
+
1988
+ /**
1989
+ * Creates a perspective projection matrix with the given bounds.
1990
+ * @param fov The vertical field of view in radians.
1991
+ * @param aspect The aspect ratio (typically the width of the viewport divided by its height).
1992
+ * @param near The near bound of the frustum.
1993
+ * @param far The far bound of the frustum.
1994
+ * @param out The matrix to store the result in.
1995
+ * @returns The perspective projection matrix.
1996
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
1997
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
1998
+ */
1999
+ public static perspective<T extends Matrix4Like>(fov: number, aspect: number, near: number, far: number, out: T): T;
2000
+
2001
+ public static perspective<T extends Matrix4Like>(fov: number, aspect: number, near: number, far: number, out: T = new Matrix4() as T): T {
2002
+ return perspective(fov, aspect, near, far, out);
2003
+ }
2004
+
2005
+ /**
2006
+ * Creates a perspective projection matrix from a field of view. Useful for generating projection matrices to be used with the WebXR API.
2007
+ * @param fov The field of view.
2008
+ * @param near The near bound of the frustum.
2009
+ * @param far The far bound of the frustum.
2010
+ * @returns The perspective projection matrix.
2011
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
2012
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
2013
+ * @see [WebXR API](https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API)
2014
+ */
2015
+ public static perspectiveFromFieldOfView(fov: FieldOfView, near: number, far: number): Matrix4;
2016
+
2017
+ /**
2018
+ * Creates a perspective projection matrix from a field of view. Useful for generating projection matrices to be used with the WebXR API.
2019
+ * @param fov The field of view.
2020
+ * @param near The near bound of the frustum.
2021
+ * @param far The far bound of the frustum.
2022
+ * @param out The matrix to store the result in.
2023
+ * @returns The perspective projection matrix.
2024
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
2025
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
2026
+ * @see [WebXR API](https://developer.mozilla.org/en-US/docs/Web/API/WebXR_Device_API)
2027
+ */
2028
+ public static perspectiveFromFieldOfView<T extends Matrix4Like>(fov: FieldOfView, near: number, far: number, out: T): T;
2029
+
2030
+ public static perspectiveFromFieldOfView<T extends Matrix4Like>(fov: FieldOfView, near: number, far: number, out: T = new Matrix4() as T): T {
2031
+ return perspectiveFromFieldOfView(fov, near, far, out);
2032
+ }
2033
+
2034
+ /**
2035
+ * Generates an orthogonal projection matrix with the given bounds.
2036
+ * @param left The left bound of the frustum.
2037
+ * @param right The right bound of the frustum.
2038
+ * @param bottom The bottom bound of the frustum.
2039
+ * @param top The top bound of the frustum.
2040
+ * @param near The near bound of the frustum.
2041
+ * @param far The far bound of the frustum.
2042
+ * @returns The frustum matrix.
2043
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
2044
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
2045
+ */
2046
+ public static ortho(left: number, right: number, bottom: number, top: number, near: number, far: number): Matrix4;
2047
+
2048
+ /**
2049
+ * Generates an orthogonal projection matrix with the given bounds.
2050
+ * @param left The left bound of the frustum.
2051
+ * @param right The right bound of the frustum.
2052
+ * @param bottom The bottom bound of the frustum.
2053
+ * @param top The top bound of the frustum.
2054
+ * @param near The near bound of the frustum.
2055
+ * @param far The far bound of the frustum.
2056
+ * @param out The matrix to store the result in.
2057
+ * @returns The frustum matrix.
2058
+ * @see [Camera matrix](https://en.wikipedia.org/wiki/Camera_matrix)
2059
+ * @see [3D projection](https://en.wikipedia.org/wiki/3D_projection)
2060
+ */
2061
+ public static ortho<T extends Matrix4Like>(left: number, right: number, bottom: number, top: number, near: number, far: number, out: T): T;
2062
+
2063
+ public static ortho<T extends Matrix4Like>(left: number, right: number, bottom: number, top: number, near: number, far: number, out: T = new Matrix4() as T): T {
2064
+ return ortho(left, right, bottom, top, near, far, out);
2065
+ }
2066
+
2067
+ /**
2068
+ * Generates a look-at matrix. If you want a matrix that actually makes an object look at another object, use `targetTo` instead.
2069
+ * @param eye The position of the viewer.
2070
+ * @param center The point that the viewer is looking at.
2071
+ * @param up The local up direction.
2072
+ * @returns The look-at matrix.
2073
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2074
+ */
2075
+ public static lookAt(eye: Vector3Like, center: Vector3Like, up?: Vector3Like): Matrix4;
2076
+
2077
+ /**
2078
+ * Generates a look-at matrix. If you want a matrix that actually makes an object look at another object, use `targetTo` instead.
2079
+ * @param eye The position of the viewer.
2080
+ * @param center The point that the viewer is looking at.
2081
+ * @param up The local up direction.
2082
+ * @param out The matrix to store the result in.
2083
+ * @returns The look-at matrix.
2084
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2085
+ */
2086
+ public static lookAt<T extends Matrix4Like>(eye: Vector3Like, center: Vector3Like, up?: Vector3Like, out?: T): T;
2087
+
2088
+ public static lookAt<T extends Matrix4Like>(eye: Vector3Like, center: Vector3Like, up: Vector3Like = new Float32Array([0, 1, 0]) as Vector3Like, out: T = new Matrix4() as T): T {
2089
+ return lookAt(eye, center, up, out);
2090
+ }
2091
+
2092
+ /**
2093
+ * Creates a matrix that makes something look at something else.
2094
+ * @param eye The position of the viewer.
2095
+ * @param center The point that the viewer is looking at.
2096
+ * @param up The local up direction.
2097
+ * @returns The transformation matrix.
2098
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2099
+ */
2100
+ public static targetTo(eye: Vector3Like, target: Vector3Like, up?: Vector3Like): Matrix4;
2101
+
2102
+ /**
2103
+ * Creates a matrix that makes something look at something else.
2104
+ * @param eye The position of the viewer.
2105
+ * @param center The point that the viewer is looking at.
2106
+ * @param up The local up direction.
2107
+ * @param out The matrix to store the result in.
2108
+ * @returns The transformation matrix.
2109
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2110
+ */
2111
+ public static targetTo<T extends Matrix4>(eye: Vector3Like, target: Vector3Like, up?: Vector3Like, out?: T): T;
2112
+
2113
+ public static targetTo<T extends Matrix4>(eye: Vector3Like, target: Vector3Like, up: Vector3Like = new Float32Array([0, 1, 0]) as Vector3Like, out: T = new Matrix4() as T): T {
2114
+ return targetTo(eye, target, up, out);
2115
+ }
2116
+
2117
+ /**
2118
+ * Creates a two-by-two matrix with the given values.
2119
+ * @param c0r0 The value in the first column and first row.
2120
+ * @param c0r1 The value in the first column and second row.
2121
+ * @param c0r2 The value in the first column and third row.
2122
+ * @param c0r3 The value in the first column and fourth row.
2123
+ * @param c1r0 The value in the second column and first row.
2124
+ * @param c1r1 The value in the second column and second row.
2125
+ * @param c1r2 The value in the second column and third row.
2126
+ * @param c1r3 The value in the second column and fourth row.
2127
+ * @param c2r0 The value in the third column and first row.
2128
+ * @param c2r1 The value in the third column and second row.
2129
+ * @param c2r2 The value in the third column and third row.
2130
+ * @param c2r3 The value in the third column and fourth row.
2131
+ * @param c3r0 The value in the fourth column and first row.
2132
+ * @param c3r1 The value in the fourth column and second row.
2133
+ * @param c3r2 The value in the fourth column and third row.
2134
+ * @param c3r3 The value in the fourth column and fourth row.
2135
+ * @returns The matrix.
2136
+ */
2137
+ public static fromValues(c0r0: number, c0r1: number, c0r2: number, c0r3: number, c1r0: number, c1r1: number, c1r2: number, c1r3: number, c2r0: number, c2r1: number, c2r2: number, c2r3: number, c3r0: number, c3r1: number, c3r2: number, c3r3: number): Matrix4;
2138
+
2139
+ /**
2140
+ * Creates a two-by-two matrix with the given values.
2141
+ * @param c0r0 The value in the first column and first row.
2142
+ * @param c0r1 The value in the first column and second row.
2143
+ * @param c0r2 The value in the first column and third row.
2144
+ * @param c0r3 The value in the first column and fourth row.
2145
+ * @param c1r0 The value in the second column and first row.
2146
+ * @param c1r1 The value in the second column and second row.
2147
+ * @param c1r2 The value in the second column and third row.
2148
+ * @param c1r3 The value in the second column and fourth row.
2149
+ * @param c2r0 The value in the third column and first row.
2150
+ * @param c2r1 The value in the third column and second row.
2151
+ * @param c2r2 The value in the third column and third row.
2152
+ * @param c2r3 The value in the third column and fourth row.
2153
+ * @param c3r0 The value in the fourth column and first row.
2154
+ * @param c3r1 The value in the fourth column and second row.
2155
+ * @param c3r2 The value in the fourth column and third row.
2156
+ * @param c3r3 The value in the fourth column and fourth row.
2157
+ * @param out The matrix to store the result in.
2158
+ * @returns The matrix.
2159
+ */
2160
+ public static fromValues<T extends Matrix4Like>(c0r0: number, c0r1: number, c0r2: number, c0r3: number, c1r0: number, c1r1: number, c1r2: number, c1r3: number, c2r0: number, c2r1: number, c2r2: number, c2r3: number, c3r0: number, c3r1: number, c3r2: number, c3r3: number, out: T): T;
2161
+
2162
+ public static fromValues<T extends Matrix4Like>(c0r0: number, c0r1: number, c0r2: number, c0r3: number, c1r0: number, c1r1: number, c1r2: number, c1r3: number, c2r0: number, c2r1: number, c2r2: number, c2r3: number, c3r0: number, c3r1: number, c3r2: number, c3r3: number, out: T = new Matrix4() as T): T {
2163
+ return fromValues(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3, out);
2164
+ }
2165
+
2166
+ /**
2167
+ * Creates a four-by-four identity matrix.
2168
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
2169
+ */
2170
+ public constructor() {
2171
+ super(16);
2172
+
2173
+ this[0] = 1;
2174
+ this[5] = 1;
2175
+ this[10] = 1;
2176
+ this[15] = 1;
2177
+
2178
+ this.width = 4;
2179
+ this.height = 4;
2180
+ }
2181
+
2182
+ /** The number of columns in this matrix. */
2183
+ public readonly width: 4;
2184
+
2185
+ /** The number of rows in this matrix. */
2186
+ public readonly height: 4;
2187
+
2188
+ /**
2189
+ * Determines whether this matrix is roughly equivalent to another.
2190
+ * @param matrix The other matrix.
2191
+ * @returns Whether the matrices are equivalent.
2192
+ */
2193
+ public equals(matrix: Matrix4Like): boolean {
2194
+ return equals(this, matrix);
2195
+ }
2196
+
2197
+ /**
2198
+ * Determines whether this matrix is exactly equivalent to another.
2199
+ * @param matrix The other matrix.
2200
+ * @returns Whether the matrices are equivalent.
2201
+ */
2202
+ public exactEquals(matrix: Matrix4Like): boolean {
2203
+ return exactEquals(this, matrix);
2204
+ }
2205
+
2206
+ /**
2207
+ * Adds two matrices of the same size.
2208
+ * @param matrix The other matrix.
2209
+ * @returns The sum of the matrices.
2210
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
2211
+ */
2212
+ public add(matrix: Matrix4Like): Matrix4;
2213
+
2214
+ /**
2215
+ * Adds two matrices of the same size.
2216
+ * @param matrix The other matrix.
2217
+ * @param out The matrix to store the result in.
2218
+ * @returns The sum of the matrices.
2219
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
2220
+ */
2221
+ public add<T extends Matrix4Like>(matrix: Matrix4Like, out: T): T;
2222
+
2223
+ public add<T extends Matrix4Like>(matrix: Matrix4Like, out: T = new Matrix4() as T): T {
2224
+ return add(this, matrix, out);
2225
+ }
2226
+
2227
+ /**
2228
+ * Calculates the adjugate of this matrix.
2229
+ * @returns The adjugate of this matrix.
2230
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
2231
+ */
2232
+ public adjoint(): Matrix4;
2233
+
2234
+ /**
2235
+ * Calculates the adjugate of this matrix.
2236
+ * @param out The matrix to store the result in.
2237
+ * @returns The adjugate of this matrix.
2238
+ * @see [Adjugate matrix](https://en.wikipedia.org/wiki/Adjugate_matrix)
2239
+ */
2240
+ public adjoint<T extends Matrix4Like>(out: T): T;
2241
+
2242
+ public adjoint<T extends Matrix4Like>(out: T = new Matrix4() as T): T {
2243
+ return adjoint(this, out);
2244
+ }
2245
+
2246
+ /**
2247
+ * Creates a copy of this matrix.
2248
+ * @returns A copy of this matrix.
2249
+ */
2250
+ public clone(): Matrix4 {
2251
+ // TODO: `out` parameter.
2252
+ return copy(this, new Matrix4());
2253
+ }
2254
+
2255
+ /**
2256
+ * Copies the values of another matrix into this one.
2257
+ * @param matrix The matrix to copy.
2258
+ * @returns This matrix.
2259
+ */
2260
+ public copy(matrix: Matrix4Like): this {
2261
+ return copy(matrix, this);
2262
+ }
2263
+
2264
+ /**
2265
+ * The Frobenius norm of this matrix.
2266
+ * @see [Matrix norm](https://en.wikipedia.org/wiki/Matrix_norm)
2267
+ */
2268
+ public get frob(): number {
2269
+ return frob(this);
2270
+ }
2271
+
2272
+ /**
2273
+ * Multiplies this matrix by another.
2274
+ * @param matrix The other matrix.
2275
+ * @returns The product of the matrices.
2276
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
2277
+ */
2278
+ public multiply(matrix: Matrix4Like): Matrix4;
2279
+
2280
+ /**
2281
+ * Multiplies this matrix by another.
2282
+ * @param matrix The other matrix.
2283
+ * @param out The matrix to store the result in.
2284
+ * @returns The product of the matrices.
2285
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
2286
+ */
2287
+ public multiply<T extends Matrix4Like>(matrix: Matrix4Like, out: T): T;
2288
+
2289
+ public multiply<T extends Matrix4Like>(matrix: Matrix4Like, out: T = new Matrix4() as T): T {
2290
+ return multiply(this, matrix, out);
2291
+ }
2292
+
2293
+ /**
2294
+ * Multiplies this matrix by a scalar value.
2295
+ * @param scalar The scalar value.
2296
+ * @returns The product of the matrix and the scalar value.
2297
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
2298
+ */
2299
+ public multiplyScalar(scalar: number): Matrix4;
2300
+
2301
+ /**
2302
+ * Multiplies this matrix by a scalar value.
2303
+ * @param scalar The scalar value.
2304
+ * @param out The matrix to store the result in.
2305
+ * @returns The product of the matrix and the scalar value.
2306
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
2307
+ */
2308
+ public multiplyScalar<T extends Matrix4Like>(scalar: number, out: T): T;
2309
+
2310
+ public multiplyScalar<T extends Matrix4Like>(scalar: number, out: T = new Matrix4() as T): T {
2311
+ return multiplyScalar(this, scalar, out);
2312
+ }
2313
+
2314
+ /**
2315
+ * Adds this matrix to another after multiplying the other by a scalar.
2316
+ * @param matrix The other matrix.
2317
+ * @param scalar The scalar.
2318
+ * @returns The sum.
2319
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
2320
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
2321
+ */
2322
+ public multiplyScalarAndAdd(matrix: Matrix4Like, scalar: number): Matrix4;
2323
+
2324
+ /**
2325
+ * Adds this matrix to another after multiplying the other by a scalar.
2326
+ * @param matrix The other matrix.
2327
+ * @param scalar The scalar.
2328
+ * @param out The matrix to store the result in.
2329
+ * @returns The sum.
2330
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
2331
+ * @see [Matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication)
2332
+ */
2333
+ public multiplyScalarAndAdd<T extends Matrix4Like>(matrix: Matrix4Like, scalar: number, out: T): T;
2334
+
2335
+ public multiplyScalarAndAdd<T extends Matrix4Like>(matrix: Matrix4Like, scalar: number, out: T = new Matrix4() as T): T {
2336
+ return multiplyScalarAndAdd(this, matrix, scalar, out);
2337
+ }
2338
+
2339
+ /**
2340
+ * Subtracts another matrix from this one.
2341
+ * @param matrix The other matrix.
2342
+ * @returns The difference between the matrices.
2343
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
2344
+ */
2345
+ public subtract(matrix: Matrix4Like): Matrix4;
2346
+
2347
+ /**
2348
+ * Subtracts another matrix from this one.
2349
+ * @param matrix The other matrix.
2350
+ * @param out The matrix to store the result in.
2351
+ * @returns The difference between the matrices.
2352
+ * @see [Matrix addition](https://en.wikipedia.org/wiki/Matrix_addition)
2353
+ */
2354
+ public subtract<T extends Matrix4Like>(matrix: Matrix4Like, out: T): T;
2355
+
2356
+ public subtract<T extends Matrix4Like>(matrix: Matrix4Like, out: T = new Matrix4() as T): T {
2357
+ return subtract(this, matrix, out);
2358
+ }
2359
+
2360
+ /**
2361
+ * Transposes this matrix.
2362
+ * @returns The transpose of this matrix.
2363
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
2364
+ */
2365
+ public transpose(): Matrix4;
2366
+
2367
+ /**
2368
+ * Transposes this matrix.
2369
+ * @param out The matrix to store the result in.
2370
+ * @returns The transpose of this matrix.
2371
+ * @see [Transpose](https://en.wikipedia.org/wiki/Transpose)
2372
+ */
2373
+ public transpose<T extends Matrix4Like>(out: T): T;
2374
+
2375
+ public transpose<T extends Matrix4Like>(out: T = new Matrix4() as T): T {
2376
+ return transpose(this, out);
2377
+ }
2378
+
2379
+ /**
2380
+ * The determinant of this matrix.
2381
+ * @see [Determinant](https://en.wikipedia.org/wiki/Determinant)
2382
+ */
2383
+ public get determinant(): number {
2384
+ return determinant(this);
2385
+ }
2386
+
2387
+ /**
2388
+ * Resets this matrix to identity.
2389
+ * @returns This matrix.
2390
+ * @see [Identity matrix](https://en.wikipedia.org/wiki/Identity_matrix)
2391
+ */
2392
+ public identity(): this {
2393
+ return identity(this);
2394
+ }
2395
+
2396
+ /**
2397
+ * Inverts this matrix.
2398
+ * @returns The inverted matrix.
2399
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
2400
+ */
2401
+ public invert(): Matrix4;
2402
+
2403
+ /**
2404
+ * Inverts this matrix.
2405
+ * @param out The matrix to store the result in.
2406
+ * @returns The inverted matrix.
2407
+ * @see [Invertible matrix](https://en.wikipedia.org/wiki/Invertible_matrix)
2408
+ */
2409
+ public invert<T extends Matrix4Like>(out: T): T;
2410
+
2411
+ public invert<T extends Matrix4Like>(out: T = new Matrix4() as T): T {
2412
+ return invert(this, out);
2413
+ }
2414
+
2415
+ /**
2416
+ * Scales this matrix by the given vector.
2417
+ * @param vector The scaling vector.
2418
+ * @returns The scaled matrix.
2419
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2420
+ */
2421
+ public scale(vector: Vector3Like): Matrix4;
2422
+
2423
+ /**
2424
+ * Scales this matrix by the given vector.
2425
+ * @param vector The scaling vector.
2426
+ * @param out The matrix to store the result in.
2427
+ * @returns The scaled matrix.
2428
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2429
+ */
2430
+ public scale<T extends Matrix4Like>(vector: Vector3Like, out: T): T;
2431
+
2432
+ public scale<T extends Matrix4Like>(vector: Vector3Like, out: T = new Matrix4() as T): T {
2433
+ return scale(this, vector, out);
2434
+ }
2435
+
2436
+ /**
2437
+ * Translates this matrix by the given vector.
2438
+ * @param vector The translation vector.
2439
+ * @returns The translated matrix.
2440
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2441
+ */
2442
+ public translate(vector: Vector3Like): Matrix4;
2443
+
2444
+ /**
2445
+ * Translates this matrix by the given vector.
2446
+ * @param vector The translation vector.
2447
+ * @param out The matrix to store the result in.
2448
+ * @returns The translated matrix.
2449
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2450
+ */
2451
+ public translate<T extends Matrix4Like>(vector: Vector3Like, out: T): T;
2452
+
2453
+ public translate<T extends Matrix4Like>(vector: Vector3Like, out: T = new Matrix4() as T): T {
2454
+ return translate(this, vector, out);
2455
+ }
2456
+
2457
+ /**
2458
+ * Rotates this matrix by the given angle around the given axis.
2459
+ * @param radians The angle in radians.
2460
+ * @param axis The axis.
2461
+ * @returns The rotated matrix.
2462
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2463
+ */
2464
+ public rotate(radians: number, axis: Vector3Like): Matrix4;
2465
+
2466
+ /**
2467
+ * Rotates this matrix by the given angle around the given axis.
2468
+ * @param radians The angle in radians.
2469
+ * @param axis The axis.
2470
+ * @param out The matrix to store the result in.
2471
+ * @returns The rotated matrix.
2472
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2473
+ */
2474
+ public rotate<T extends Matrix4Like>(radians: number, axis: Vector3Like, out: T): T;
2475
+
2476
+ public rotate<T extends Matrix4Like>(radians: number, axis: Vector3Like, out: T = new Matrix4() as T): T {
2477
+ return rotate(this, radians, axis, out);
2478
+ }
2479
+
2480
+ /**
2481
+ * Rotates this matrix by the given angle around the X-axis.
2482
+ * @param radians The angle in radians.
2483
+ * @returns The rotated matrix.
2484
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2485
+ */
2486
+ public rotateX(radians: number): Matrix4;
2487
+
2488
+ /**
2489
+ * Rotates this matrix by the given angle around the X-axis.
2490
+ * @param radians The angle in radians.
2491
+ * @param out The matrix to store the result in.
2492
+ * @returns The rotated matrix.
2493
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2494
+ */
2495
+ public rotateX<T extends Matrix4Like>(radians: number, out: T): T;
2496
+
2497
+ public rotateX<T extends Matrix4Like>(radians: number, out: T = new Matrix4() as T): T {
2498
+ return rotateX(this, radians, out);
2499
+ }
2500
+
2501
+ /**
2502
+ * Rotates this matrix by the given angle around the Y-axis.
2503
+ * @param radians The angle in radians.
2504
+ * @returns The rotated matrix.
2505
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2506
+ */
2507
+ public rotateY(radians: number): Matrix4;
2508
+
2509
+ /**
2510
+ * Rotates this matrix by the given angle around the Y-axis.
2511
+ * @param radians The angle in radians.
2512
+ * @param out The matrix to store the result in.
2513
+ * @returns The rotated matrix.
2514
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2515
+ */
2516
+ public rotateY<T extends Matrix4Like>(radians: number, out: T): T;
2517
+
2518
+ public rotateY<T extends Matrix4Like>(radians: number, out: T = new Matrix4() as T): T {
2519
+ return rotateY(this, radians, out);
2520
+ }
2521
+
2522
+ /**
2523
+ * Rotates this matrix by the given angle around the Z-axis.
2524
+ * @param radians The angle in radians.
2525
+ * @returns The rotated matrix.
2526
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2527
+ */
2528
+ public rotateZ(radians: number): Matrix4;
2529
+
2530
+ /**
2531
+ * Rotates this matrix by the given angle around the Z-axis.
2532
+ * @param radians The angle in radians.
2533
+ * @param out The matrix to store the result in.
2534
+ * @returns The rotated matrix.
2535
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2536
+ */
2537
+ public rotateZ<T extends Matrix4Like>(radians: number, out: T): T;
2538
+
2539
+ public rotateZ<T extends Matrix4Like>(radians: number, out: T = new Matrix4() as T): T {
2540
+ return rotateZ(this, radians, out);
2541
+ }
2542
+
2543
+ /**
2544
+ * The translation vector component of this transformation matrix.
2545
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2546
+ */
2547
+ public get translation(): Vector3Like {
2548
+ // TODO: `out` parameter.
2549
+ return getTranslation(this, new Float32Array(3) as Vector3Like);
2550
+ }
2551
+
2552
+ /**
2553
+ * The scaling factor of this transformation matrix.
2554
+ * @see [Transformation matrix](https://en.wikipedia.org/wiki/Transformation_matrix)
2555
+ */
2556
+ public get scaling(): Vector3Like {
2557
+ // TODO: `out` parameter.
2558
+ return getScaling(this, new Float32Array(3) as Vector3Like);
2559
+ }
2560
+
2561
+ /**
2562
+ * The rotational component of this transformation matrix.
2563
+ * @see [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
2564
+ */
2565
+ public get rotation(): QuaternionLike {
2566
+ // TODO: `out` parameter.
2567
+ return getRotation(this, new Float32Array(4) as QuaternionLike);
2568
+ }
2569
+ }