@ac-kit/math-linear 0.2.1 → 0.3.0

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 (178) hide show
  1. package/CHANGELOG.md +257 -0
  2. package/README.md +229 -16
  3. package/dist/_brand.d.ts +14 -0
  4. package/dist/_brand.d.ts.map +1 -0
  5. package/dist/_brand.js +2 -0
  6. package/dist/_brand.js.map +1 -0
  7. package/dist/_invert.d.ts +19 -0
  8. package/dist/_invert.d.ts.map +1 -0
  9. package/dist/_invert.js +48 -0
  10. package/dist/_invert.js.map +1 -0
  11. package/dist/_slerp.d.ts +15 -0
  12. package/dist/_slerp.d.ts.map +1 -1
  13. package/dist/_slerp.js +47 -0
  14. package/dist/_slerp.js.map +1 -1
  15. package/dist/_strided-range.d.ts +23 -0
  16. package/dist/_strided-range.d.ts.map +1 -0
  17. package/dist/_strided-range.js +75 -0
  18. package/dist/_strided-range.js.map +1 -0
  19. package/dist/_trs.d.ts +38 -0
  20. package/dist/_trs.d.ts.map +1 -0
  21. package/dist/_trs.js +132 -0
  22. package/dist/_trs.js.map +1 -0
  23. package/dist/affine2-array.d.ts +53 -0
  24. package/dist/affine2-array.d.ts.map +1 -0
  25. package/dist/affine2-array.js +107 -0
  26. package/dist/affine2-array.js.map +1 -0
  27. package/dist/affine2-componentwise.d.ts +28 -0
  28. package/dist/affine2-componentwise.d.ts.map +1 -0
  29. package/dist/affine2-componentwise.js +66 -0
  30. package/dist/affine2-componentwise.js.map +1 -0
  31. package/dist/affine2.d.ts +144 -0
  32. package/dist/affine2.d.ts.map +1 -0
  33. package/dist/affine2.js +375 -0
  34. package/dist/affine2.js.map +1 -0
  35. package/dist/affine3-array.d.ts +53 -0
  36. package/dist/affine3-array.d.ts.map +1 -0
  37. package/dist/affine3-array.js +137 -0
  38. package/dist/affine3-array.js.map +1 -0
  39. package/dist/affine3-componentwise.d.ts +32 -0
  40. package/dist/affine3-componentwise.d.ts.map +1 -0
  41. package/dist/affine3-componentwise.js +100 -0
  42. package/dist/affine3-componentwise.js.map +1 -0
  43. package/dist/affine3.d.ts +192 -0
  44. package/dist/affine3.d.ts.map +1 -0
  45. package/dist/affine3.js +718 -0
  46. package/dist/affine3.js.map +1 -0
  47. package/dist/checked/index.d.ts +35 -0
  48. package/dist/checked/index.d.ts.map +1 -0
  49. package/dist/checked/index.js +58 -0
  50. package/dist/checked/index.js.map +1 -0
  51. package/dist/euler.d.ts +65 -0
  52. package/dist/euler.d.ts.map +1 -0
  53. package/dist/euler.js +217 -0
  54. package/dist/euler.js.map +1 -0
  55. package/dist/index.d.ts +29 -0
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +29 -0
  58. package/dist/index.js.map +1 -1
  59. package/dist/kl-activations.d.ts.map +1 -1
  60. package/dist/kl-activations.js +4 -3
  61. package/dist/kl-activations.js.map +1 -1
  62. package/dist/mat2x2-array.d.ts +36 -0
  63. package/dist/mat2x2-array.d.ts.map +1 -0
  64. package/dist/mat2x2-array.js +71 -0
  65. package/dist/mat2x2-array.js.map +1 -0
  66. package/dist/mat2x2-componentwise.d.ts +86 -0
  67. package/dist/mat2x2-componentwise.d.ts.map +1 -0
  68. package/dist/mat2x2-componentwise.js +199 -0
  69. package/dist/mat2x2-componentwise.js.map +1 -0
  70. package/dist/mat2x2.d.ts +67 -25
  71. package/dist/mat2x2.d.ts.map +1 -1
  72. package/dist/mat2x2.js +150 -94
  73. package/dist/mat2x2.js.map +1 -1
  74. package/dist/mat3x3-array.d.ts +36 -0
  75. package/dist/mat3x3-array.d.ts.map +1 -0
  76. package/dist/mat3x3-array.js +91 -0
  77. package/dist/mat3x3-array.js.map +1 -0
  78. package/dist/mat3x3-componentwise.d.ts +86 -0
  79. package/dist/mat3x3-componentwise.d.ts.map +1 -0
  80. package/dist/mat3x3-componentwise.js +299 -0
  81. package/dist/mat3x3-componentwise.js.map +1 -0
  82. package/dist/mat3x3.d.ts +131 -48
  83. package/dist/mat3x3.d.ts.map +1 -1
  84. package/dist/mat3x3.js +434 -193
  85. package/dist/mat3x3.js.map +1 -1
  86. package/dist/mat4x4-array.d.ts +46 -0
  87. package/dist/mat4x4-array.d.ts.map +1 -0
  88. package/dist/mat4x4-array.js +133 -0
  89. package/dist/mat4x4-array.js.map +1 -0
  90. package/dist/mat4x4-componentwise.d.ts +87 -0
  91. package/dist/mat4x4-componentwise.d.ts.map +1 -0
  92. package/dist/mat4x4-componentwise.js +434 -0
  93. package/dist/mat4x4-componentwise.js.map +1 -0
  94. package/dist/mat4x4.d.ts +195 -67
  95. package/dist/mat4x4.d.ts.map +1 -1
  96. package/dist/mat4x4.js +849 -335
  97. package/dist/mat4x4.js.map +1 -1
  98. package/dist/projection.d.ts +98 -0
  99. package/dist/projection.d.ts.map +1 -0
  100. package/dist/projection.js +369 -0
  101. package/dist/projection.js.map +1 -0
  102. package/dist/quaternion-array.d.ts +47 -0
  103. package/dist/quaternion-array.d.ts.map +1 -0
  104. package/dist/quaternion-array.js +86 -0
  105. package/dist/quaternion-array.js.map +1 -0
  106. package/dist/quaternion.d.ts +104 -22
  107. package/dist/quaternion.d.ts.map +1 -1
  108. package/dist/quaternion.js +294 -181
  109. package/dist/quaternion.js.map +1 -1
  110. package/dist/transform.d.ts +220 -0
  111. package/dist/transform.d.ts.map +1 -0
  112. package/dist/transform.js +412 -0
  113. package/dist/transform.js.map +1 -0
  114. package/dist/tridiagonal.d.ts +52 -0
  115. package/dist/tridiagonal.d.ts.map +1 -0
  116. package/dist/tridiagonal.js +90 -0
  117. package/dist/tridiagonal.js.map +1 -0
  118. package/dist/upload.d.ts +55 -0
  119. package/dist/upload.d.ts.map +1 -0
  120. package/dist/upload.js +129 -0
  121. package/dist/upload.js.map +1 -0
  122. package/dist/vec2-array-componentwise.d.ts +189 -0
  123. package/dist/vec2-array-componentwise.d.ts.map +1 -0
  124. package/dist/vec2-array-componentwise.js +424 -0
  125. package/dist/vec2-array-componentwise.js.map +1 -0
  126. package/dist/vec2-array.d.ts +53 -0
  127. package/dist/vec2-array.d.ts.map +1 -0
  128. package/dist/vec2-array.js +123 -0
  129. package/dist/vec2-array.js.map +1 -0
  130. package/dist/vec2-componentwise.d.ts +311 -0
  131. package/dist/vec2-componentwise.d.ts.map +1 -0
  132. package/dist/vec2-componentwise.js +542 -0
  133. package/dist/vec2-componentwise.js.map +1 -0
  134. package/dist/vec2.d.ts +97 -82
  135. package/dist/vec2.d.ts.map +1 -1
  136. package/dist/vec2.js +121 -216
  137. package/dist/vec2.js.map +1 -1
  138. package/dist/vec3-array-componentwise.d.ts +189 -0
  139. package/dist/vec3-array-componentwise.d.ts.map +1 -0
  140. package/dist/vec3-array-componentwise.js +458 -0
  141. package/dist/vec3-array-componentwise.js.map +1 -0
  142. package/dist/vec3-array.d.ts +102 -0
  143. package/dist/vec3-array.d.ts.map +1 -0
  144. package/dist/vec3-array.js +299 -0
  145. package/dist/vec3-array.js.map +1 -0
  146. package/dist/vec3-componentwise.d.ts +310 -0
  147. package/dist/vec3-componentwise.d.ts.map +1 -0
  148. package/dist/vec3-componentwise.js +594 -0
  149. package/dist/vec3-componentwise.js.map +1 -0
  150. package/dist/vec3.d.ts +177 -87
  151. package/dist/vec3.d.ts.map +1 -1
  152. package/dist/vec3.js +366 -273
  153. package/dist/vec3.js.map +1 -1
  154. package/dist/vec4-array-componentwise.d.ts +155 -0
  155. package/dist/vec4-array-componentwise.d.ts.map +1 -0
  156. package/dist/vec4-array-componentwise.js +401 -0
  157. package/dist/vec4-array-componentwise.js.map +1 -0
  158. package/dist/vec4-array.d.ts +12 -0
  159. package/dist/vec4-array.d.ts.map +1 -0
  160. package/dist/vec4-array.js +43 -0
  161. package/dist/vec4-array.js.map +1 -0
  162. package/dist/vec4-componentwise.d.ts +299 -0
  163. package/dist/vec4-componentwise.d.ts.map +1 -0
  164. package/dist/vec4-componentwise.js +637 -0
  165. package/dist/vec4-componentwise.js.map +1 -0
  166. package/dist/vec4.d.ts +57 -53
  167. package/dist/vec4.d.ts.map +1 -1
  168. package/dist/vec4.js +94 -246
  169. package/dist/vec4.js.map +1 -1
  170. package/dist/view-basis.d.ts +15 -0
  171. package/dist/view-basis.d.ts.map +1 -0
  172. package/dist/view-basis.js +29 -0
  173. package/dist/view-basis.js.map +1 -0
  174. package/dist/viewport.d.ts +114 -0
  175. package/dist/viewport.d.ts.map +1 -0
  176. package/dist/viewport.js +250 -0
  177. package/dist/viewport.js.map +1 -0
  178. package/package.json +18 -9
package/dist/mat4x4.js CHANGED
@@ -1,371 +1,885 @@
1
- import { isCloseTo } from "@ac-kit/core";
2
- import { diffOfProducts, dotPrecise } from "@ac-kit/math-scalar";
3
- export const MAT4X4_ZERO = [
4
- [0, 0, 0, 0],
5
- [0, 0, 0, 0],
6
- [0, 0, 0, 0],
7
- [0, 0, 0, 0],
8
- ];
9
- export const MAT4X4_IDENTITY = [
10
- [1, 0, 0, 0],
11
- [0, 1, 0, 0],
12
- [0, 0, 1, 0],
13
- [0, 0, 0, 1],
14
- ];
15
- export function mat4x4Add(a, b) {
16
- return [
17
- [
18
- a[0][0] + b[0][0],
19
- a[0][1] + b[0][1],
20
- a[0][2] + b[0][2],
21
- a[0][3] + b[0][3],
22
- ],
23
- [
24
- a[1][0] + b[1][0],
25
- a[1][1] + b[1][1],
26
- a[1][2] + b[1][2],
27
- a[1][3] + b[1][3],
28
- ],
29
- [
30
- a[2][0] + b[2][0],
31
- a[2][1] + b[2][1],
32
- a[2][2] + b[2][2],
33
- a[2][3] + b[2][3],
34
- ],
35
- [
36
- a[3][0] + b[3][0],
37
- a[3][1] + b[3][1],
38
- a[3][2] + b[3][2],
39
- a[3][3] + b[3][3],
40
- ],
41
- ];
1
+ import { dotPrecise4 } from "@ac-kit/math-scalar";
2
+ import { scaleForInversion_, unscaleInverse_ } from "./_invert.js";
3
+ import { AFFINE_CORNER_TOLERANCE_, decomposeLinear_ } from "./_trs.js";
4
+ /**
5
+ * Writes the product `a · b` into `out`: `b` is applied first. Not commutative.
6
+ * Plain arithmetic, straight-line: each entry is within four roundings of the
7
+ * exact value relative to the sum of the absolute values of its terms, so it
8
+ * can lose every digit where the terms cancel (see
9
+ * {@link mat4x4MultiplyPrecise}). `out` may be `a` or `b`.
10
+ */
11
+ export function mat4x4Multiply(out, a, b) {
12
+ const a0 = a[0];
13
+ const a1 = a[1];
14
+ const a2 = a[2];
15
+ const a3 = a[3];
16
+ const a4 = a[4];
17
+ const a5 = a[5];
18
+ const a6 = a[6];
19
+ const a7 = a[7];
20
+ const a8 = a[8];
21
+ const a9 = a[9];
22
+ const a10 = a[10];
23
+ const a11 = a[11];
24
+ const a12 = a[12];
25
+ const a13 = a[13];
26
+ const a14 = a[14];
27
+ const a15 = a[15];
28
+ // Each column of `b` is read before the same column of `out` is written, so
29
+ // `out` may be `b`.
30
+ let b0 = b[0];
31
+ let b1 = b[1];
32
+ let b2 = b[2];
33
+ let b3 = b[3];
34
+ out[0] = a0 * b0 + a4 * b1 + a8 * b2 + a12 * b3;
35
+ out[1] = a1 * b0 + a5 * b1 + a9 * b2 + a13 * b3;
36
+ out[2] = a2 * b0 + a6 * b1 + a10 * b2 + a14 * b3;
37
+ out[3] = a3 * b0 + a7 * b1 + a11 * b2 + a15 * b3;
38
+ b0 = b[4];
39
+ b1 = b[5];
40
+ b2 = b[6];
41
+ b3 = b[7];
42
+ out[4] = a0 * b0 + a4 * b1 + a8 * b2 + a12 * b3;
43
+ out[5] = a1 * b0 + a5 * b1 + a9 * b2 + a13 * b3;
44
+ out[6] = a2 * b0 + a6 * b1 + a10 * b2 + a14 * b3;
45
+ out[7] = a3 * b0 + a7 * b1 + a11 * b2 + a15 * b3;
46
+ b0 = b[8];
47
+ b1 = b[9];
48
+ b2 = b[10];
49
+ b3 = b[11];
50
+ out[8] = a0 * b0 + a4 * b1 + a8 * b2 + a12 * b3;
51
+ out[9] = a1 * b0 + a5 * b1 + a9 * b2 + a13 * b3;
52
+ out[10] = a2 * b0 + a6 * b1 + a10 * b2 + a14 * b3;
53
+ out[11] = a3 * b0 + a7 * b1 + a11 * b2 + a15 * b3;
54
+ b0 = b[12];
55
+ b1 = b[13];
56
+ b2 = b[14];
57
+ b3 = b[15];
58
+ out[12] = a0 * b0 + a4 * b1 + a8 * b2 + a12 * b3;
59
+ out[13] = a1 * b0 + a5 * b1 + a9 * b2 + a13 * b3;
60
+ out[14] = a2 * b0 + a6 * b1 + a10 * b2 + a14 * b3;
61
+ out[15] = a3 * b0 + a7 * b1 + a11 * b2 + a15 * b3;
62
+ return out;
63
+ }
64
+ /**
65
+ * {@link mat4x4Multiply} with each entry a compensated dot product, as accurate
66
+ * as one in doubled precision rounded once. About ten times the cost. `out` may
67
+ * be `a` or `b`.
68
+ */
69
+ export function mat4x4MultiplyPrecise(out, a, b) {
70
+ const a0 = a[0];
71
+ const a1 = a[1];
72
+ const a2 = a[2];
73
+ const a3 = a[3];
74
+ const a4 = a[4];
75
+ const a5 = a[5];
76
+ const a6 = a[6];
77
+ const a7 = a[7];
78
+ const a8 = a[8];
79
+ const a9 = a[9];
80
+ const a10 = a[10];
81
+ const a11 = a[11];
82
+ const a12 = a[12];
83
+ const a13 = a[13];
84
+ const a14 = a[14];
85
+ const a15 = a[15];
86
+ let b0 = b[0];
87
+ let b1 = b[1];
88
+ let b2 = b[2];
89
+ let b3 = b[3];
90
+ out[0] = dotPrecise4(a0, b0, a4, b1, a8, b2, a12, b3);
91
+ out[1] = dotPrecise4(a1, b0, a5, b1, a9, b2, a13, b3);
92
+ out[2] = dotPrecise4(a2, b0, a6, b1, a10, b2, a14, b3);
93
+ out[3] = dotPrecise4(a3, b0, a7, b1, a11, b2, a15, b3);
94
+ b0 = b[4];
95
+ b1 = b[5];
96
+ b2 = b[6];
97
+ b3 = b[7];
98
+ out[4] = dotPrecise4(a0, b0, a4, b1, a8, b2, a12, b3);
99
+ out[5] = dotPrecise4(a1, b0, a5, b1, a9, b2, a13, b3);
100
+ out[6] = dotPrecise4(a2, b0, a6, b1, a10, b2, a14, b3);
101
+ out[7] = dotPrecise4(a3, b0, a7, b1, a11, b2, a15, b3);
102
+ b0 = b[8];
103
+ b1 = b[9];
104
+ b2 = b[10];
105
+ b3 = b[11];
106
+ out[8] = dotPrecise4(a0, b0, a4, b1, a8, b2, a12, b3);
107
+ out[9] = dotPrecise4(a1, b0, a5, b1, a9, b2, a13, b3);
108
+ out[10] = dotPrecise4(a2, b0, a6, b1, a10, b2, a14, b3);
109
+ out[11] = dotPrecise4(a3, b0, a7, b1, a11, b2, a15, b3);
110
+ b0 = b[12];
111
+ b1 = b[13];
112
+ b2 = b[14];
113
+ b3 = b[15];
114
+ out[12] = dotPrecise4(a0, b0, a4, b1, a8, b2, a12, b3);
115
+ out[13] = dotPrecise4(a1, b0, a5, b1, a9, b2, a13, b3);
116
+ out[14] = dotPrecise4(a2, b0, a6, b1, a10, b2, a14, b3);
117
+ out[15] = dotPrecise4(a3, b0, a7, b1, a11, b2, a15, b3);
118
+ return out;
119
+ }
120
+ /**
121
+ * Writes `m · diag(v)` into `out`: column `c` of `m` multiplied by `v[c]`.
122
+ * `out` may be `m`.
123
+ */
124
+ export function mat4x4ScaleColumns(out, m, v) {
125
+ const v0 = v[0];
126
+ const v1 = v[1];
127
+ const v2 = v[2];
128
+ const v3 = v[3];
129
+ out[0] = m[0] * v0;
130
+ out[1] = m[1] * v0;
131
+ out[2] = m[2] * v0;
132
+ out[3] = m[3] * v0;
133
+ out[4] = m[4] * v1;
134
+ out[5] = m[5] * v1;
135
+ out[6] = m[6] * v1;
136
+ out[7] = m[7] * v1;
137
+ out[8] = m[8] * v2;
138
+ out[9] = m[9] * v2;
139
+ out[10] = m[10] * v2;
140
+ out[11] = m[11] * v2;
141
+ out[12] = m[12] * v3;
142
+ out[13] = m[13] * v3;
143
+ out[14] = m[14] * v3;
144
+ out[15] = m[15] * v3;
145
+ return out;
42
146
  }
43
147
  /**
44
- * Composes a 4×4 transformation matrix from translation, rotation (quaternion),
45
- * and scale. Equivalent to T × R × S.
148
+ * Writes `diag(v) · m` into `out`: row `r` of `m` multiplied by `v[r]`. `out`
149
+ * may be `m`.
46
150
  */
47
- export function mat4x4Compose(translation, rotation, scale) {
48
- const r = mat4x4FromQuaternion(rotation);
49
- const [sx, sy, sz] = scale;
50
- return [
51
- [r[0][0] * sx, r[0][1] * sy, r[0][2] * sz, translation[0]],
52
- [r[1][0] * sx, r[1][1] * sy, r[1][2] * sz, translation[1]],
53
- [r[2][0] * sx, r[2][1] * sy, r[2][2] * sz, translation[2]],
54
- [0, 0, 0, 1],
55
- ];
151
+ export function mat4x4ScaleRows(out, m, v) {
152
+ const v0 = v[0];
153
+ const v1 = v[1];
154
+ const v2 = v[2];
155
+ const v3 = v[3];
156
+ out[0] = m[0] * v0;
157
+ out[1] = m[1] * v1;
158
+ out[2] = m[2] * v2;
159
+ out[3] = m[3] * v3;
160
+ out[4] = m[4] * v0;
161
+ out[5] = m[5] * v1;
162
+ out[6] = m[6] * v2;
163
+ out[7] = m[7] * v3;
164
+ out[8] = m[8] * v0;
165
+ out[9] = m[9] * v1;
166
+ out[10] = m[10] * v2;
167
+ out[11] = m[11] * v3;
168
+ out[12] = m[12] * v0;
169
+ out[13] = m[13] * v1;
170
+ out[14] = m[14] * v2;
171
+ out[15] = m[15] * v3;
172
+ return out;
56
173
  }
174
+ /** Writes the transpose of `m` into `out`. `out` may be `m`. */
175
+ export function mat4x4Transpose(out, m) {
176
+ const m1 = m[1];
177
+ const m2 = m[2];
178
+ const m3 = m[3];
179
+ const m4 = m[4];
180
+ const m6 = m[6];
181
+ const m7 = m[7];
182
+ const m8 = m[8];
183
+ const m9 = m[9];
184
+ const m11 = m[11];
185
+ const m12 = m[12];
186
+ const m13 = m[13];
187
+ const m14 = m[14];
188
+ out[0] = m[0];
189
+ out[1] = m4;
190
+ out[2] = m8;
191
+ out[3] = m12;
192
+ out[4] = m1;
193
+ out[5] = m[5];
194
+ out[6] = m9;
195
+ out[7] = m13;
196
+ out[8] = m2;
197
+ out[9] = m6;
198
+ out[10] = m[10];
199
+ out[11] = m14;
200
+ out[12] = m3;
201
+ out[13] = m7;
202
+ out[14] = m11;
203
+ out[15] = m[15];
204
+ return out;
205
+ }
206
+ /**
207
+ * The determinant, from the six 2 × 2 minors of the first two columns and of
208
+ * the last two, in plain arithmetic: the signed volume scale of the
209
+ * transformation.
210
+ */
57
211
  export function mat4x4Determinant(m) {
58
- const [a00, a01, a02, a03] = m[0];
59
- const [a10, a11, a12, a13] = m[1];
60
- const [a20, a21, a22, a23] = m[2];
61
- const [a30, a31, a32, a33] = m[3];
62
- const b00 = diffOfProducts(a00, a11, a01, a10);
63
- const b01 = diffOfProducts(a00, a12, a02, a10);
64
- const b02 = diffOfProducts(a00, a13, a03, a10);
65
- const b03 = diffOfProducts(a01, a12, a02, a11);
66
- const b04 = diffOfProducts(a01, a13, a03, a11);
67
- const b05 = diffOfProducts(a02, a13, a03, a12);
68
- const b06 = diffOfProducts(a20, a31, a21, a30);
69
- const b07 = diffOfProducts(a20, a32, a22, a30);
70
- const b08 = diffOfProducts(a20, a33, a23, a30);
71
- const b09 = diffOfProducts(a21, a32, a22, a31);
72
- const b10 = diffOfProducts(a21, a33, a23, a31);
73
- const b11 = diffOfProducts(a22, a33, a23, a32);
74
- // Each minor is now exactly rounded; the six outer products still cancel, so
75
- // the whole expansion goes through one compensated dot product.
76
- return dotPrecise([b00, -b01, b02, b03, -b04, b05], [b11, b10, b09, b08, b07, b06]);
212
+ const m0 = m[0];
213
+ const m1 = m[1];
214
+ const m2 = m[2];
215
+ const m3 = m[3];
216
+ const m4 = m[4];
217
+ const m5 = m[5];
218
+ const m6 = m[6];
219
+ const m7 = m[7];
220
+ const m8 = m[8];
221
+ const m9 = m[9];
222
+ const m10 = m[10];
223
+ const m11 = m[11];
224
+ const m12 = m[12];
225
+ const m13 = m[13];
226
+ const m14 = m[14];
227
+ const m15 = m[15];
228
+ const s0 = m0 * m5 - m1 * m4;
229
+ const s1 = m0 * m6 - m2 * m4;
230
+ const s2 = m0 * m7 - m3 * m4;
231
+ const s3 = m1 * m6 - m2 * m5;
232
+ const s4 = m1 * m7 - m3 * m5;
233
+ const s5 = m2 * m7 - m3 * m6;
234
+ const c0 = m8 * m13 - m9 * m12;
235
+ const c1 = m8 * m14 - m10 * m12;
236
+ const c2 = m8 * m15 - m11 * m12;
237
+ const c3 = m9 * m14 - m10 * m13;
238
+ const c4 = m9 * m15 - m11 * m13;
239
+ const c5 = m10 * m15 - m11 * m14;
240
+ return s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0;
77
241
  }
78
- /** Exact element-wise equality. See {@link mat4x4IsClose} for a tolerant test. */
79
- export function mat4x4Equals(a, b) {
80
- for (let i = 0; i < 4; i++) {
81
- for (let j = 0; j < 4; j++) {
82
- if (a[i][j] !== b[i][j]) {
83
- return false;
84
- }
85
- }
242
+ /**
243
+ * Writes the inverse of `m` into `out` and returns `true`. When `m` has no
244
+ * inverse a binary64 number can hold (the determinant is zero, an entry is not
245
+ * finite, or an entry of the inverse overflows) `out` is filled with `NaN` and
246
+ * the result is `false`: `true` always comes with a finite `out`, never a stale
247
+ * or partial matrix, and nothing throws. A matrix of one very large or very
248
+ * small magnitude, whose determinant overflows or underflows, is inverted all
249
+ * the same (`diag(1e-80)` gives `diag(1e80)`), by scaling it by a power of two
250
+ * first. Plain arithmetic: the sixteen cofactors share eighteen 2 × 2 minors,
251
+ * and the determinant is the first row of the matrix against its cofactors.
252
+ * `out` may be `m`.
253
+ */
254
+ export function mat4x4Invert(out, m) {
255
+ if (invert_(out, m, false)) {
256
+ return true;
86
257
  }
87
- return true;
258
+ const exponent = scaleForInversion_(out, m);
259
+ if (!Number.isNaN(exponent) &&
260
+ invert_(out, out, true) &&
261
+ unscaleInverse_(out, exponent)) {
262
+ return true;
263
+ }
264
+ out[0] = Number.NaN;
265
+ out[1] = Number.NaN;
266
+ out[2] = Number.NaN;
267
+ out[3] = Number.NaN;
268
+ out[4] = Number.NaN;
269
+ out[5] = Number.NaN;
270
+ out[6] = Number.NaN;
271
+ out[7] = Number.NaN;
272
+ out[8] = Number.NaN;
273
+ out[9] = Number.NaN;
274
+ out[10] = Number.NaN;
275
+ out[11] = Number.NaN;
276
+ out[12] = Number.NaN;
277
+ out[13] = Number.NaN;
278
+ out[14] = Number.NaN;
279
+ out[15] = Number.NaN;
280
+ return false;
88
281
  }
89
282
  /**
90
- * Element-wise approximate equality.
91
- *
92
- * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
93
- * and never a basis for ordering — use {@link mat4x4Equals} there.
283
+ * The inverse by the cofactors over the determinant, written to `out` only when
284
+ * every entry is finite. The reciprocal of the determinant multiplies the
285
+ * cofactors; when it overflows (a subnormal determinant) they are divided by
286
+ * the determinant instead, if `divide`, and not otherwise.
94
287
  */
95
- export function mat4x4IsClose(a, b, tolerance) {
96
- for (let i = 0; i < 4; i++) {
97
- for (let j = 0; j < 4; j++) {
98
- if (!isCloseTo(a[i][j], b[i][j], tolerance)) {
99
- return false;
100
- }
101
- }
288
+ function invert_(out, m, divide) {
289
+ // nRC is the entry of row R, column C, counted from 1.
290
+ const n11 = m[0];
291
+ const n21 = m[1];
292
+ const n31 = m[2];
293
+ const n41 = m[3];
294
+ const n12 = m[4];
295
+ const n22 = m[5];
296
+ const n32 = m[6];
297
+ const n42 = m[7];
298
+ const n13 = m[8];
299
+ const n23 = m[9];
300
+ const n33 = m[10];
301
+ const n43 = m[11];
302
+ const n14 = m[12];
303
+ const n24 = m[13];
304
+ const n34 = m[14];
305
+ const n44 = m[15];
306
+ // `rIJcKL` is the minor of rows I and J, columns K and L.
307
+ const r34c34 = n33 * n44 - n34 * n43;
308
+ const r34c24 = n32 * n44 - n34 * n42;
309
+ const r34c23 = n32 * n43 - n33 * n42;
310
+ const r34c14 = n31 * n44 - n34 * n41;
311
+ const r34c13 = n31 * n43 - n33 * n41;
312
+ const r34c12 = n31 * n42 - n32 * n41;
313
+ const r24c34 = n23 * n44 - n24 * n43;
314
+ const r24c24 = n22 * n44 - n24 * n42;
315
+ const r24c23 = n22 * n43 - n23 * n42;
316
+ const r24c14 = n21 * n44 - n24 * n41;
317
+ const r24c13 = n21 * n43 - n23 * n41;
318
+ const r24c12 = n21 * n42 - n22 * n41;
319
+ const r23c34 = n23 * n34 - n24 * n33;
320
+ const r23c24 = n22 * n34 - n24 * n32;
321
+ const r23c23 = n22 * n33 - n23 * n32;
322
+ const r23c14 = n21 * n34 - n24 * n31;
323
+ const r23c13 = n21 * n33 - n23 * n31;
324
+ const r23c12 = n21 * n32 - n22 * n31;
325
+ const c11 = n22 * r34c34 - n23 * r34c24 + n24 * r34c23;
326
+ const c12 = -(n21 * r34c34 - n23 * r34c14 + n24 * r34c13);
327
+ const c13 = n21 * r34c24 - n22 * r34c14 + n24 * r34c12;
328
+ const c14 = -(n21 * r34c23 - n22 * r34c13 + n23 * r34c12);
329
+ const determinant = n11 * c11 + n12 * c12 + n13 * c13 + n14 * c14;
330
+ if (determinant === 0 || !Number.isFinite(determinant)) {
331
+ return false;
332
+ }
333
+ const scale = 1 / determinant;
334
+ const reciprocal = Number.isFinite(scale);
335
+ if (!reciprocal && !divide) {
336
+ return false;
102
337
  }
338
+ const c21 = -(n12 * r34c34 - n13 * r34c24 + n14 * r34c23);
339
+ const c22 = n11 * r34c34 - n13 * r34c14 + n14 * r34c13;
340
+ const c23 = -(n11 * r34c24 - n12 * r34c14 + n14 * r34c12);
341
+ const c24 = n11 * r34c23 - n12 * r34c13 + n13 * r34c12;
342
+ const c31 = n12 * r24c34 - n13 * r24c24 + n14 * r24c23;
343
+ const c32 = -(n11 * r24c34 - n13 * r24c14 + n14 * r24c13);
344
+ const c33 = n11 * r24c24 - n12 * r24c14 + n14 * r24c12;
345
+ const c34 = -(n11 * r24c23 - n12 * r24c13 + n13 * r24c12);
346
+ const c41 = -(n12 * r23c34 - n13 * r23c24 + n14 * r23c23);
347
+ const c42 = n11 * r23c34 - n13 * r23c14 + n14 * r23c13;
348
+ const c43 = -(n11 * r23c24 - n12 * r23c14 + n14 * r23c12);
349
+ const c44 = n11 * r23c23 - n12 * r23c13 + n13 * r23c12;
350
+ // The inverse is the transpose of the cofactor matrix over the determinant.
351
+ const o0 = reciprocal ? c11 * scale : c11 / determinant;
352
+ const o1 = reciprocal ? c12 * scale : c12 / determinant;
353
+ const o2 = reciprocal ? c13 * scale : c13 / determinant;
354
+ const o3 = reciprocal ? c14 * scale : c14 / determinant;
355
+ const o4 = reciprocal ? c21 * scale : c21 / determinant;
356
+ const o5 = reciprocal ? c22 * scale : c22 / determinant;
357
+ const o6 = reciprocal ? c23 * scale : c23 / determinant;
358
+ const o7 = reciprocal ? c24 * scale : c24 / determinant;
359
+ const o8 = reciprocal ? c31 * scale : c31 / determinant;
360
+ const o9 = reciprocal ? c32 * scale : c32 / determinant;
361
+ const o10 = reciprocal ? c33 * scale : c33 / determinant;
362
+ const o11 = reciprocal ? c34 * scale : c34 / determinant;
363
+ const o12 = reciprocal ? c41 * scale : c41 / determinant;
364
+ const o13 = reciprocal ? c42 * scale : c42 / determinant;
365
+ const o14 = reciprocal ? c43 * scale : c43 / determinant;
366
+ const o15 = reciprocal ? c44 * scale : c44 / determinant;
367
+ if (!(Number.isFinite(o0) &&
368
+ Number.isFinite(o1) &&
369
+ Number.isFinite(o2) &&
370
+ Number.isFinite(o3) &&
371
+ Number.isFinite(o4) &&
372
+ Number.isFinite(o5) &&
373
+ Number.isFinite(o6) &&
374
+ Number.isFinite(o7) &&
375
+ Number.isFinite(o8) &&
376
+ Number.isFinite(o9) &&
377
+ Number.isFinite(o10) &&
378
+ Number.isFinite(o11) &&
379
+ Number.isFinite(o12) &&
380
+ Number.isFinite(o13) &&
381
+ Number.isFinite(o14) &&
382
+ Number.isFinite(o15))) {
383
+ return false;
384
+ }
385
+ out[0] = o0;
386
+ out[1] = o1;
387
+ out[2] = o2;
388
+ out[3] = o3;
389
+ out[4] = o4;
390
+ out[5] = o5;
391
+ out[6] = o6;
392
+ out[7] = o7;
393
+ out[8] = o8;
394
+ out[9] = o9;
395
+ out[10] = o10;
396
+ out[11] = o11;
397
+ out[12] = o12;
398
+ out[13] = o13;
399
+ out[14] = o14;
400
+ out[15] = o15;
103
401
  return true;
104
402
  }
403
+ /** Writes the translation by `translation` into `out`. */
404
+ export function mat4x4FromTranslation(out, translation) {
405
+ out[0] = 1;
406
+ out[1] = 0;
407
+ out[2] = 0;
408
+ out[3] = 0;
409
+ out[4] = 0;
410
+ out[5] = 1;
411
+ out[6] = 0;
412
+ out[7] = 0;
413
+ out[8] = 0;
414
+ out[9] = 0;
415
+ out[10] = 1;
416
+ out[11] = 0;
417
+ out[12] = translation[0];
418
+ out[13] = translation[1];
419
+ out[14] = translation[2];
420
+ out[15] = 1;
421
+ return out;
422
+ }
423
+ /** Writes the scale `diag(scale, 1)` into `out`. */
424
+ export function mat4x4FromScale(out, scale) {
425
+ out[0] = scale[0];
426
+ out[1] = 0;
427
+ out[2] = 0;
428
+ out[3] = 0;
429
+ out[4] = 0;
430
+ out[5] = scale[1];
431
+ out[6] = 0;
432
+ out[7] = 0;
433
+ out[8] = 0;
434
+ out[9] = 0;
435
+ out[10] = scale[2];
436
+ out[11] = 0;
437
+ out[12] = 0;
438
+ out[13] = 0;
439
+ out[14] = 0;
440
+ out[15] = 1;
441
+ return out;
442
+ }
105
443
  /**
106
- * Creates a 4×4 matrix from a quaternion (pure rotation, no translation or
107
- * scale).
444
+ * Writes the rotation by `angle` radians about the x axis (right-hand rule)
445
+ * into `out`.
108
446
  */
109
- export function mat4x4FromQuaternion(q) {
110
- const x = q[0], y = q[1], z = q[2], w = q[3];
111
- const xx = x * x, yy = y * y, zz = z * z;
112
- const xy = x * y, xz = x * z, yz = y * z;
113
- const wx = w * x, wy = w * y, wz = w * z;
114
- return [
115
- [1 - 2 * (yy + zz), 2 * (xy - wz), 2 * (xz + wy), 0],
116
- [2 * (xy + wz), 1 - 2 * (xx + zz), 2 * (yz - wx), 0],
117
- [2 * (xz - wy), 2 * (yz + wx), 1 - 2 * (xx + yy), 0],
118
- [0, 0, 0, 1],
119
- ];
120
- }
121
- /** Creates a 4×4 rotation matrix around the X axis (right-hand rule). */
122
- export function mat4x4FromRotationX(angle) {
447
+ export function mat4x4FromRotationX(out, angle) {
123
448
  const cos = Math.cos(angle);
124
449
  const sin = Math.sin(angle);
125
- return [
126
- [1, 0, 0, 0],
127
- [0, cos, -sin, 0],
128
- [0, sin, cos, 0],
129
- [0, 0, 0, 1],
130
- ];
450
+ out[0] = 1;
451
+ out[1] = 0;
452
+ out[2] = 0;
453
+ out[3] = 0;
454
+ out[4] = 0;
455
+ out[5] = cos;
456
+ out[6] = sin;
457
+ out[7] = 0;
458
+ out[8] = 0;
459
+ out[9] = -sin;
460
+ out[10] = cos;
461
+ out[11] = 0;
462
+ out[12] = 0;
463
+ out[13] = 0;
464
+ out[14] = 0;
465
+ out[15] = 1;
466
+ return out;
131
467
  }
132
- /** Creates a 4×4 rotation matrix around the Y axis (right-hand rule). */
133
- export function mat4x4FromRotationY(angle) {
468
+ /**
469
+ * Writes the rotation by `angle` radians about the y axis (right-hand rule)
470
+ * into `out`.
471
+ */
472
+ export function mat4x4FromRotationY(out, angle) {
134
473
  const cos = Math.cos(angle);
135
474
  const sin = Math.sin(angle);
136
- return [
137
- [cos, 0, sin, 0],
138
- [0, 1, 0, 0],
139
- [-sin, 0, cos, 0],
140
- [0, 0, 0, 1],
141
- ];
475
+ out[0] = cos;
476
+ out[1] = 0;
477
+ out[2] = -sin;
478
+ out[3] = 0;
479
+ out[4] = 0;
480
+ out[5] = 1;
481
+ out[6] = 0;
482
+ out[7] = 0;
483
+ out[8] = sin;
484
+ out[9] = 0;
485
+ out[10] = cos;
486
+ out[11] = 0;
487
+ out[12] = 0;
488
+ out[13] = 0;
489
+ out[14] = 0;
490
+ out[15] = 1;
491
+ return out;
142
492
  }
143
- /** Creates a 4×4 rotation matrix around the Z axis (right-hand rule). */
144
- export function mat4x4FromRotationZ(angle) {
493
+ /**
494
+ * Writes the rotation by `angle` radians about the z axis (right-hand rule)
495
+ * into `out`.
496
+ */
497
+ export function mat4x4FromRotationZ(out, angle) {
145
498
  const cos = Math.cos(angle);
146
499
  const sin = Math.sin(angle);
147
- return [
148
- [cos, -sin, 0, 0],
149
- [sin, cos, 0, 0],
150
- [0, 0, 1, 0],
151
- [0, 0, 0, 1],
152
- ];
500
+ out[0] = cos;
501
+ out[1] = sin;
502
+ out[2] = 0;
503
+ out[3] = 0;
504
+ out[4] = -sin;
505
+ out[5] = cos;
506
+ out[6] = 0;
507
+ out[7] = 0;
508
+ out[8] = 0;
509
+ out[9] = 0;
510
+ out[10] = 1;
511
+ out[11] = 0;
512
+ out[12] = 0;
513
+ out[13] = 0;
514
+ out[14] = 0;
515
+ out[15] = 1;
516
+ return out;
153
517
  }
154
- export function mat4x4FromScale(v) {
155
- return [
156
- [v[0], 0, 0, 0],
157
- [0, v[1], 0, 0],
158
- [0, 0, v[2], 0],
159
- [0, 0, 0, 1],
160
- ];
518
+ /**
519
+ * Writes the rotation of the unit quaternion `q` into `out`, no translation and
520
+ * no scale. `q` must be unit length; it is not normalised here.
521
+ */
522
+ export function mat4x4FromQuaternion(out, q) {
523
+ const x = q[0];
524
+ const y = q[1];
525
+ const z = q[2];
526
+ const w = q[3];
527
+ const xx = x * x;
528
+ const yy = y * y;
529
+ const zz = z * z;
530
+ const xy = x * y;
531
+ const xz = x * z;
532
+ const yz = y * z;
533
+ const wx = w * x;
534
+ const wy = w * y;
535
+ const wz = w * z;
536
+ out[0] = 1 - 2 * (yy + zz);
537
+ out[1] = 2 * (xy + wz);
538
+ out[2] = 2 * (xz - wy);
539
+ out[3] = 0;
540
+ out[4] = 2 * (xy - wz);
541
+ out[5] = 1 - 2 * (xx + zz);
542
+ out[6] = 2 * (yz + wx);
543
+ out[7] = 0;
544
+ out[8] = 2 * (xz + wy);
545
+ out[9] = 2 * (yz - wx);
546
+ out[10] = 1 - 2 * (xx + yy);
547
+ out[11] = 0;
548
+ out[12] = 0;
549
+ out[13] = 0;
550
+ out[14] = 0;
551
+ out[15] = 1;
552
+ return out;
161
553
  }
162
- export function mat4x4FromTranslation(v) {
163
- return [
164
- [1, 0, 0, v[0]],
165
- [0, 1, 0, v[1]],
166
- [0, 0, 1, v[2]],
167
- [0, 0, 0, 1],
168
- ];
554
+ /**
555
+ * Writes the transform that scales, then rotates, then translates into `out`:
556
+ * `T · R · S`, with `rotation` a unit quaternion (not normalised here).
557
+ */
558
+ export function mat4x4Compose(out, translation, rotation, scale) {
559
+ const x = rotation[0];
560
+ const y = rotation[1];
561
+ const z = rotation[2];
562
+ const w = rotation[3];
563
+ const sx = scale[0];
564
+ const sy = scale[1];
565
+ const sz = scale[2];
566
+ const xx = x * x;
567
+ const yy = y * y;
568
+ const zz = z * z;
569
+ const xy = x * y;
570
+ const xz = x * z;
571
+ const yz = y * z;
572
+ const wx = w * x;
573
+ const wy = w * y;
574
+ const wz = w * z;
575
+ out[0] = (1 - 2 * (yy + zz)) * sx;
576
+ out[1] = 2 * (xy + wz) * sx;
577
+ out[2] = 2 * (xz - wy) * sx;
578
+ out[3] = 0;
579
+ out[4] = 2 * (xy - wz) * sy;
580
+ out[5] = (1 - 2 * (xx + zz)) * sy;
581
+ out[6] = 2 * (yz + wx) * sy;
582
+ out[7] = 0;
583
+ out[8] = 2 * (xz + wy) * sz;
584
+ out[9] = 2 * (yz - wx) * sz;
585
+ out[10] = (1 - 2 * (xx + yy)) * sz;
586
+ out[11] = 0;
587
+ out[12] = translation[0];
588
+ out[13] = translation[1];
589
+ out[14] = translation[2];
590
+ out[15] = 1;
591
+ return out;
169
592
  }
170
- export function mat4x4Invert(m) {
171
- const n11 = m[0][0], n12 = m[0][1], n13 = m[0][2], n14 = m[0][3];
172
- const n21 = m[1][0], n22 = m[1][1], n23 = m[1][2], n24 = m[1][3];
173
- const n31 = m[2][0], n32 = m[2][1], n33 = m[2][2], n34 = m[2][3];
174
- const n41 = m[3][0], n42 = m[3][1], n43 = m[3][2], n44 = m[3][3];
175
- // The sixteen cofactors share eighteen 2x2 minors between them; naming each
176
- // one `rIJcKL` (rows I,J and columns K,L) computes it once and exactly.
177
- const r34c34 = diffOfProducts(n33, n44, n34, n43);
178
- const r34c24 = diffOfProducts(n32, n44, n34, n42);
179
- const r34c23 = diffOfProducts(n32, n43, n33, n42);
180
- const r34c14 = diffOfProducts(n31, n44, n34, n41);
181
- const r34c13 = diffOfProducts(n31, n43, n33, n41);
182
- const r34c12 = diffOfProducts(n31, n42, n32, n41);
183
- const r24c34 = diffOfProducts(n23, n44, n24, n43);
184
- const r24c24 = diffOfProducts(n22, n44, n24, n42);
185
- const r24c23 = diffOfProducts(n22, n43, n23, n42);
186
- const r24c14 = diffOfProducts(n21, n44, n24, n41);
187
- const r24c13 = diffOfProducts(n21, n43, n23, n41);
188
- const r24c12 = diffOfProducts(n21, n42, n22, n41);
189
- const r23c34 = diffOfProducts(n23, n34, n24, n33);
190
- const r23c24 = diffOfProducts(n22, n34, n24, n32);
191
- const r23c23 = diffOfProducts(n22, n33, n23, n32);
192
- const r23c14 = diffOfProducts(n21, n34, n24, n31);
193
- const r23c13 = diffOfProducts(n21, n33, n23, n31);
194
- const r23c12 = diffOfProducts(n21, n32, n22, n31);
195
- const c11 = cofactor_(n22, r34c34, n23, r34c24, n24, r34c23);
196
- const c12 = -cofactor_(n21, r34c34, n23, r34c14, n24, r34c13);
197
- const c13 = cofactor_(n21, r34c24, n22, r34c14, n24, r34c12);
198
- const c14 = -cofactor_(n21, r34c23, n22, r34c13, n23, r34c12);
199
- const det = dotPrecise([n11, n12, n13, n14], [c11, c12, c13, c14]);
200
- if (det === 0) {
201
- throw new Error("Matrix is not invertible.");
202
- }
203
- const inv = 1 / det;
204
- const c21 = -cofactor_(n12, r34c34, n13, r34c24, n14, r34c23);
205
- const c22 = cofactor_(n11, r34c34, n13, r34c14, n14, r34c13);
206
- const c23 = -cofactor_(n11, r34c24, n12, r34c14, n14, r34c12);
207
- const c24 = cofactor_(n11, r34c23, n12, r34c13, n13, r34c12);
208
- const c31 = cofactor_(n12, r24c34, n13, r24c24, n14, r24c23);
209
- const c32 = -cofactor_(n11, r24c34, n13, r24c14, n14, r24c13);
210
- const c33 = cofactor_(n11, r24c24, n12, r24c14, n14, r24c12);
211
- const c34 = -cofactor_(n11, r24c23, n12, r24c13, n13, r24c12);
212
- const c41 = -cofactor_(n12, r23c34, n13, r23c24, n14, r23c23);
213
- const c42 = cofactor_(n11, r23c34, n13, r23c14, n14, r23c13);
214
- const c43 = -cofactor_(n11, r23c24, n12, r23c14, n14, r23c12);
215
- const c44 = cofactor_(n11, r23c23, n12, r23c13, n13, r23c12);
216
- // Inverse = transpose(cofactor matrix) / det
217
- return [
218
- [c11 * inv, c21 * inv, c31 * inv, c41 * inv],
219
- [c12 * inv, c22 * inv, c32 * inv, c42 * inv],
220
- [c13 * inv, c23 * inv, c33 * inv, c43 * inv],
221
- [c14 * inv, c24 * inv, c34 * inv, c44 * inv],
222
- ];
593
+ /**
594
+ * Writes the rotation by `angle` radians about the unit vector `axis`
595
+ * (right-hand rule, Rodrigues' formula) into `out`, no translation. `axis` is
596
+ * not normalised here: for any other length the matrix is not a rotation.
597
+ */
598
+ export function mat4x4FromAxisAngle(out, axis, angle) {
599
+ const x = axis[0];
600
+ const y = axis[1];
601
+ const z = axis[2];
602
+ const sin = Math.sin(angle);
603
+ // 1 − cos as twice the squared half-angle sine, and the diagonal as one
604
+ // minus its share: exact in the first digits for the small angles where
605
+ // cos − 1 would lose them, and exactly 1 on the axis itself.
606
+ const half = Math.sin(angle / 2);
607
+ const rest = 2 * half * half;
608
+ out[0] = 1 - rest * (y * y + z * z);
609
+ out[1] = rest * x * y + sin * z;
610
+ out[2] = rest * x * z - sin * y;
611
+ out[3] = 0;
612
+ out[4] = rest * x * y - sin * z;
613
+ out[5] = 1 - rest * (x * x + z * z);
614
+ out[6] = rest * y * z + sin * x;
615
+ out[7] = 0;
616
+ out[8] = rest * x * z + sin * y;
617
+ out[9] = rest * y * z - sin * x;
618
+ out[10] = 1 - rest * (x * x + y * y);
619
+ out[11] = 0;
620
+ out[12] = 0;
621
+ out[13] = 0;
622
+ out[14] = 0;
623
+ out[15] = 1;
624
+ return out;
223
625
  }
224
- /** `a*x - b*y + c*z`, the alternating expansion every 3x3 cofactor takes. */
225
- function cofactor_(a, x, b, y, c, z) {
226
- return dotPrecise([a, -b, c], [x, y, z]);
626
+ /**
627
+ * Writes the 3 × 3 matrix `m` as the upper-left block of a 4 × 4 matrix whose
628
+ * last row and column are those of the identity into `out`.
629
+ */
630
+ export function mat4x4FromMat3x3(out, m) {
631
+ out[0] = m[0];
632
+ out[1] = m[1];
633
+ out[2] = m[2];
634
+ out[3] = 0;
635
+ out[4] = m[3];
636
+ out[5] = m[4];
637
+ out[6] = m[5];
638
+ out[7] = 0;
639
+ out[8] = m[6];
640
+ out[9] = m[7];
641
+ out[10] = m[8];
642
+ out[11] = 0;
643
+ out[12] = 0;
644
+ out[13] = 0;
645
+ out[14] = 0;
646
+ out[15] = 1;
647
+ return out;
227
648
  }
228
- export function mat4x4Multiply(a, b) {
229
- const columns = mat4x4Transpose(b);
230
- const result = [
231
- [0, 0, 0, 0],
232
- [0, 0, 0, 0],
233
- [0, 0, 0, 0],
234
- [0, 0, 0, 0],
235
- ];
236
- for (let i = 0; i < 4; i++) {
237
- for (let j = 0; j < 4; j++) {
238
- result[i][j] = dotPrecise(a[i], columns[j]);
239
- }
240
- }
241
- return result;
649
+ /**
650
+ * Writes the 3D affine `transform` as a 4 × 4 matrix into `out`: the bottom
651
+ * row, which an {@link Affine3} does not store, is `0, 0, 0, 1`.
652
+ */
653
+ export function mat4x4FromAffine3(out, transform) {
654
+ out[0] = transform[0];
655
+ out[1] = transform[1];
656
+ out[2] = transform[2];
657
+ out[3] = 0;
658
+ out[4] = transform[3];
659
+ out[5] = transform[4];
660
+ out[6] = transform[5];
661
+ out[7] = 0;
662
+ out[8] = transform[6];
663
+ out[9] = transform[7];
664
+ out[10] = transform[8];
665
+ out[11] = 0;
666
+ out[12] = transform[9];
667
+ out[13] = transform[10];
668
+ out[14] = transform[11];
669
+ out[15] = 1;
670
+ return out;
242
671
  }
243
- export function mat4x4MultiplyCols(matrix, v) {
244
- return [
245
- [
246
- matrix[0][0] * v[0],
247
- matrix[0][1] * v[1],
248
- matrix[0][2] * v[2],
249
- matrix[0][3] * v[3],
250
- ],
251
- [
252
- matrix[1][0] * v[0],
253
- matrix[1][1] * v[1],
254
- matrix[1][2] * v[2],
255
- matrix[1][3] * v[3],
256
- ],
257
- [
258
- matrix[2][0] * v[0],
259
- matrix[2][1] * v[1],
260
- matrix[2][2] * v[2],
261
- matrix[2][3] * v[3],
262
- ],
263
- [
264
- matrix[3][0] * v[0],
265
- matrix[3][1] * v[1],
266
- matrix[3][2] * v[2],
267
- matrix[3][3] * v[3],
268
- ],
269
- ];
672
+ /**
673
+ * Writes the 2D affine `transform` as a 4 × 4 matrix that acts on `x` and `y`
674
+ * and passes `z` and `w` through into `out`: a 2D view as the `viewProjection`
675
+ * of a 3D pipeline, the depth untouched.
676
+ */
677
+ export function mat4x4FromAffine2(out, transform) {
678
+ out[0] = transform[0];
679
+ out[1] = transform[1];
680
+ out[2] = 0;
681
+ out[3] = 0;
682
+ out[4] = transform[2];
683
+ out[5] = transform[3];
684
+ out[6] = 0;
685
+ out[7] = 0;
686
+ out[8] = 0;
687
+ out[9] = 0;
688
+ out[10] = 1;
689
+ out[11] = 0;
690
+ out[12] = transform[4];
691
+ out[13] = transform[5];
692
+ out[14] = 0;
693
+ out[15] = 1;
694
+ return out;
695
+ }
696
+ /**
697
+ * Splits `m` into the translation, rotation and scale that {@link mat4x4Compose}
698
+ * builds it from: the translation is the last column, the scale is the length
699
+ * of each of the first three, and the rotation is the unit quaternion of those
700
+ * columns divided by it. A reflection (negative determinant) puts its sign in
701
+ * the x scale, so the rotation is proper.
702
+ *
703
+ * Returns `false` when there is no such split: a column of zero length or not
704
+ * finite, two columns further from perpendicular than about `1e-6` in cosine (a
705
+ * shear), a bottom row other than `0, 0, 0, 1` (a projection), or a translation
706
+ * that is not finite. The three outputs then hold `NaN`, never a stale or
707
+ * partial result, and nothing throws.
708
+ */
709
+ export function mat4x4Decompose(outTranslation, outRotation, outScale, m) {
710
+ const x = m[12];
711
+ const y = m[13];
712
+ const z = m[14];
713
+ if (m[3] === 0 &&
714
+ m[7] === 0 &&
715
+ m[11] === 0 &&
716
+ Math.abs(m[15] - 1) <= AFFINE_CORNER_TOLERANCE_ &&
717
+ Number.isFinite(x) &&
718
+ Number.isFinite(y) &&
719
+ Number.isFinite(z) &&
720
+ decomposeLinear_(outRotation, outScale, m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10])) {
721
+ outTranslation[0] = x;
722
+ outTranslation[1] = y;
723
+ outTranslation[2] = z;
724
+ return true;
725
+ }
726
+ outTranslation[0] = Number.NaN;
727
+ outTranslation[1] = Number.NaN;
728
+ outTranslation[2] = Number.NaN;
729
+ outRotation[0] = Number.NaN;
730
+ outRotation[1] = Number.NaN;
731
+ outRotation[2] = Number.NaN;
732
+ outRotation[3] = Number.NaN;
733
+ outScale[0] = Number.NaN;
734
+ outScale[1] = Number.NaN;
735
+ outScale[2] = Number.NaN;
736
+ return false;
270
737
  }
271
- export function mat4x4MultiplyRows(matrix, v) {
272
- return [
273
- [
274
- matrix[0][0] * v[0],
275
- matrix[0][1] * v[0],
276
- matrix[0][2] * v[0],
277
- matrix[0][3] * v[0],
278
- ],
279
- [
280
- matrix[1][0] * v[1],
281
- matrix[1][1] * v[1],
282
- matrix[1][2] * v[1],
283
- matrix[1][3] * v[1],
284
- ],
285
- [
286
- matrix[2][0] * v[2],
287
- matrix[2][1] * v[2],
288
- matrix[2][2] * v[2],
289
- matrix[2][3] * v[2],
290
- ],
291
- [
292
- matrix[3][0] * v[3],
293
- matrix[3][1] * v[3],
294
- matrix[3][2] * v[3],
295
- matrix[3][3] * v[3],
296
- ],
297
- ];
738
+ /**
739
+ * Writes the product `m · transform` into `out`, the 3D affine `transform`
740
+ * taken as the 4 × 4 matrix with a bottom row `0, 0, 0, 1`: a projection times
741
+ * a view times an affine model, in plain arithmetic and straight-line. `out`
742
+ * may be `m`.
743
+ */
744
+ export function mat4x4MultiplyAffine3(out, m, transform) {
745
+ const m0 = m[0];
746
+ const m1 = m[1];
747
+ const m2 = m[2];
748
+ const m3 = m[3];
749
+ const m4 = m[4];
750
+ const m5 = m[5];
751
+ const m6 = m[6];
752
+ const m7 = m[7];
753
+ const m8 = m[8];
754
+ const m9 = m[9];
755
+ const m10 = m[10];
756
+ const m11 = m[11];
757
+ const m12 = m[12];
758
+ const m13 = m[13];
759
+ const m14 = m[14];
760
+ const m15 = m[15];
761
+ const t0 = transform[0];
762
+ const t1 = transform[1];
763
+ const t2 = transform[2];
764
+ out[0] = m0 * t0 + m4 * t1 + m8 * t2;
765
+ out[1] = m1 * t0 + m5 * t1 + m9 * t2;
766
+ out[2] = m2 * t0 + m6 * t1 + m10 * t2;
767
+ out[3] = m3 * t0 + m7 * t1 + m11 * t2;
768
+ const t3 = transform[3];
769
+ const t4 = transform[4];
770
+ const t5 = transform[5];
771
+ out[4] = m0 * t3 + m4 * t4 + m8 * t5;
772
+ out[5] = m1 * t3 + m5 * t4 + m9 * t5;
773
+ out[6] = m2 * t3 + m6 * t4 + m10 * t5;
774
+ out[7] = m3 * t3 + m7 * t4 + m11 * t5;
775
+ const t6 = transform[6];
776
+ const t7 = transform[7];
777
+ const t8 = transform[8];
778
+ out[8] = m0 * t6 + m4 * t7 + m8 * t8;
779
+ out[9] = m1 * t6 + m5 * t7 + m9 * t8;
780
+ out[10] = m2 * t6 + m6 * t7 + m10 * t8;
781
+ out[11] = m3 * t6 + m7 * t7 + m11 * t8;
782
+ const t9 = transform[9];
783
+ const t10 = transform[10];
784
+ const t11 = transform[11];
785
+ out[12] = m0 * t9 + m4 * t10 + m8 * t11 + m12;
786
+ out[13] = m1 * t9 + m5 * t10 + m9 * t11 + m13;
787
+ out[14] = m2 * t9 + m6 * t10 + m10 * t11 + m14;
788
+ out[15] = m3 * t9 + m7 * t10 + m11 * t11 + m15;
789
+ return out;
298
790
  }
299
- export function mat4x4MultiplyScalar(matrix, scalar) {
300
- return [
301
- [
302
- matrix[0][0] * scalar,
303
- matrix[0][1] * scalar,
304
- matrix[0][2] * scalar,
305
- matrix[0][3] * scalar,
306
- ],
307
- [
308
- matrix[1][0] * scalar,
309
- matrix[1][1] * scalar,
310
- matrix[1][2] * scalar,
311
- matrix[1][3] * scalar,
312
- ],
313
- [
314
- matrix[2][0] * scalar,
315
- matrix[2][1] * scalar,
316
- matrix[2][2] * scalar,
317
- matrix[2][3] * scalar,
318
- ],
319
- [
320
- matrix[3][0] * scalar,
321
- matrix[3][1] * scalar,
322
- matrix[3][2] * scalar,
323
- matrix[3][3] * scalar,
324
- ],
325
- ];
791
+ /**
792
+ * Writes the product `m · T(translation)` into `out`: `m` with a translation
793
+ * applied first, in its own local space, which is how a renderer folds its
794
+ * origin into a matrix. Only the last column changes. `out` may be `m`.
795
+ */
796
+ export function mat4x4Translate(out, m, translation) {
797
+ const x = translation[0];
798
+ const y = translation[1];
799
+ const z = translation[2];
800
+ const m0 = m[0];
801
+ const m1 = m[1];
802
+ const m2 = m[2];
803
+ const m3 = m[3];
804
+ const m4 = m[4];
805
+ const m5 = m[5];
806
+ const m6 = m[6];
807
+ const m7 = m[7];
808
+ const m8 = m[8];
809
+ const m9 = m[9];
810
+ const m10 = m[10];
811
+ const m11 = m[11];
812
+ const m12 = m[12];
813
+ const m13 = m[13];
814
+ const m14 = m[14];
815
+ const m15 = m[15];
816
+ out[0] = m0;
817
+ out[1] = m1;
818
+ out[2] = m2;
819
+ out[3] = m3;
820
+ out[4] = m4;
821
+ out[5] = m5;
822
+ out[6] = m6;
823
+ out[7] = m7;
824
+ out[8] = m8;
825
+ out[9] = m9;
826
+ out[10] = m10;
827
+ out[11] = m11;
828
+ out[12] = m0 * x + m4 * y + m8 * z + m12;
829
+ out[13] = m1 * x + m5 * y + m9 * z + m13;
830
+ out[14] = m2 * x + m6 * y + m10 * z + m14;
831
+ out[15] = m3 * x + m7 * y + m11 * z + m15;
832
+ return out;
326
833
  }
327
- export function mat4x4Negate(matrix) {
328
- return [
329
- [-matrix[0][0], -matrix[0][1], -matrix[0][2], -matrix[0][3]],
330
- [-matrix[1][0], -matrix[1][1], -matrix[1][2], -matrix[1][3]],
331
- [-matrix[2][0], -matrix[2][1], -matrix[2][2], -matrix[2][3]],
332
- [-matrix[3][0], -matrix[3][1], -matrix[3][2], -matrix[3][3]],
333
- ];
834
+ /**
835
+ * Writes the last column of `m` without its bottom entry, the translation of an
836
+ * affine matrix, into `out`.
837
+ */
838
+ export function mat4x4GetTranslation(out, m) {
839
+ out[0] = m[12];
840
+ out[1] = m[13];
841
+ out[2] = m[14];
842
+ return out;
334
843
  }
335
- export function mat4x4Sub(a, b) {
336
- return [
337
- [
338
- a[0][0] - b[0][0],
339
- a[0][1] - b[0][1],
340
- a[0][2] - b[0][2],
341
- a[0][3] - b[0][3],
342
- ],
343
- [
344
- a[1][0] - b[1][0],
345
- a[1][1] - b[1][1],
346
- a[1][2] - b[1][2],
347
- a[1][3] - b[1][3],
348
- ],
349
- [
350
- a[2][0] - b[2][0],
351
- a[2][1] - b[2][1],
352
- a[2][2] - b[2][2],
353
- a[2][3] - b[2][3],
354
- ],
355
- [
356
- a[3][0] - b[3][0],
357
- a[3][1] - b[3][1],
358
- a[3][2] - b[3][2],
359
- a[3][3] - b[3][3],
360
- ],
361
- ];
844
+ /**
845
+ * Writes the length of each of the first three columns of `m`, the scale of a
846
+ * matrix with no shear (the sign of a reflection is not recovered: see
847
+ * {@link mat4x4Decompose}), into `out`. Plain arithmetic: right between about
848
+ * `1e-150` and `1e150`.
849
+ */
850
+ export function mat4x4GetScale(out, m) {
851
+ const m0 = m[0];
852
+ const m1 = m[1];
853
+ const m2 = m[2];
854
+ const m4 = m[4];
855
+ const m5 = m[5];
856
+ const m6 = m[6];
857
+ const m8 = m[8];
858
+ const m9 = m[9];
859
+ const m10 = m[10];
860
+ out[0] = Math.sqrt(m0 * m0 + m1 * m1 + m2 * m2);
861
+ out[1] = Math.sqrt(m4 * m4 + m5 * m5 + m6 * m6);
862
+ out[2] = Math.sqrt(m8 * m8 + m9 * m9 + m10 * m10);
863
+ return out;
362
864
  }
363
- export function mat4x4Transpose(matrix) {
364
- return [
365
- [matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0]],
366
- [matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1]],
367
- [matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2]],
368
- [matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]],
369
- ];
865
+ /**
866
+ * Writes column `column` of `m`, 0 to 3, into `out`; any other `column` gives
867
+ * `NaN` throughout, never `undefined`.
868
+ */
869
+ export function mat4x4GetColumn(out, m, column) {
870
+ if (column === 0 || column === 1 || column === 2 || column === 3) {
871
+ const start = column * 4;
872
+ out[0] = m[start];
873
+ out[1] = m[start + 1];
874
+ out[2] = m[start + 2];
875
+ out[3] = m[start + 3];
876
+ }
877
+ else {
878
+ out[0] = Number.NaN;
879
+ out[1] = Number.NaN;
880
+ out[2] = Number.NaN;
881
+ out[3] = Number.NaN;
882
+ }
883
+ return out;
370
884
  }
371
885
  //# sourceMappingURL=mat4x4.js.map