@ac-kit/math-linear 0.2.1 → 0.4.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 +337 -0
  2. package/README.md +232 -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 +214 -0
  8. package/dist/_invert.d.ts.map +1 -0
  9. package/dist/_invert.js +884 -0
  10. package/dist/_invert.js.map +1 -0
  11. package/dist/_slerp.d.ts +29 -0
  12. package/dist/_slerp.d.ts.map +1 -1
  13. package/dist/_slerp.js +103 -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 +156 -0
  32. package/dist/affine2.d.ts.map +1 -0
  33. package/dist/affine2.js +437 -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 +205 -0
  44. package/dist/affine3.d.ts.map +1 -0
  45. package/dist/affine3.js +792 -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 +79 -25
  71. package/dist/mat2x2.d.ts.map +1 -1
  72. package/dist/mat2x2.js +192 -93
  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 +153 -48
  83. package/dist/mat3x3.d.ts.map +1 -1
  84. package/dist/mat3x3.js +470 -192
  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 +221 -67
  95. package/dist/mat4x4.d.ts.map +1 -1
  96. package/dist/mat4x4.js +972 -334
  97. package/dist/mat4x4.js.map +1 -1
  98. package/dist/projection.d.ts +113 -0
  99. package/dist/projection.d.ts.map +1 -0
  100. package/dist/projection.js +418 -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 +106 -22
  107. package/dist/quaternion.d.ts.map +1 -1
  108. package/dist/quaternion.js +253 -182
  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 +207 -0
  123. package/dist/vec2-array-componentwise.d.ts.map +1 -0
  124. package/dist/vec2-array-componentwise.js +472 -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 +207 -0
  139. package/dist/vec3-array-componentwise.d.ts.map +1 -0
  140. package/dist/vec3-array-componentwise.js +510 -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 +391 -272
  153. package/dist/vec3.js.map +1 -1
  154. package/dist/vec4-array-componentwise.d.ts +198 -0
  155. package/dist/vec4-array-componentwise.d.ts.map +1 -0
  156. package/dist/vec4-array-componentwise.js +521 -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 +59 -53
  167. package/dist/vec4.d.ts.map +1 -1
  168. package/dist/vec4.js +74 -249
  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 +19 -9
package/dist/mat4x4.js CHANGED
@@ -1,371 +1,1009 @@
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 { INVERSE_LIMIT_, invertRescaled_, isCofactorExpansionExact_, ROUNDING_TOLERANCE_, UNDERFLOW_TOLERANCE_, UNDERFLOW_UNIT_, UNIT_ROUNDOFF_, } 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;
42
63
  }
43
64
  /**
44
- * Composes a 4×4 transformation matrix from translation, rotation (quaternion),
45
- * and scale. Equivalent to T × R × S.
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`.
46
68
  */
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
- ];
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;
56
119
  }
57
- 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]);
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;
77
146
  }
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
- }
86
- }
87
- return true;
147
+ /**
148
+ * Writes `diag(v) · m` into `out`: row `r` of `m` multiplied by `v[r]`. `out`
149
+ * may be `m`.
150
+ */
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;
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
+ */
211
+ export function mat4x4Determinant(m) {
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;
88
241
  }
89
242
  /**
90
- * Element-wise approximate equality.
243
+ * Writes the inverse of `m` into `out` and returns `true`. When `m` has no
244
+ * inverse a binary64 number can hold (it is singular, an entry is not finite,
245
+ * or an entry of the inverse overflows) `out` is filled with `NaN` and the
246
+ * result is `false`: `true` always comes with a finite `out`, never a stale or
247
+ * partial matrix, and nothing throws. Whatever its determinant, a matrix whose
248
+ * inverse fits is inverted (`diag(1e-80)` gives `diag(1e80)`, `diag(1e200,
249
+ * 1e200, 1e200, 1e-200)` gives `diag(1e-200, 1e-200, 1e-200, 1e200)`, and `[[1,
250
+ * 1, 0, 0], [1, 1, 2^-600, 0], [0, 2^-600, 1, 1], [0, 0, 1, 1]]`, of
251
+ * determinant `-2^-1200`, has an inverse of entries ±2^600).
91
252
  *
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.
253
+ * Plain arithmetic first: the sixteen cofactors share eighteen 2 × 2 minors,
254
+ * and the determinant is the first row of the matrix against its cofactors.
255
+ * When that determinant or an entry of the inverse leaves the range, or a
256
+ * forward error bound of the expansion (Higham, "Accuracy and Stability of
257
+ * Numerical Algorithms", §3.1) does not keep the determinant within `2^-41` of
258
+ * itself and the cofactors within `2^-43` of the largest of them (a matrix
259
+ * nearly singular, or whose cofactors cancel to a rounding residue), unless the
260
+ * expansion rounded nowhere, the rows and columns are scaled by powers of two
261
+ * so the largest entry of each is in `[1, 2)`, exactly, and the scaled matrix
262
+ * is inverted by its cofactors under the same bound or else by LU factorisation
263
+ * with partial pivoting (Golub and Van Loan, §3.4), backward stable; the
264
+ * scaling is undone on the inverse. An elimination without rounding proves the
265
+ * matrix singular or invertible; one that rounds is settled in exact
266
+ * arithmetic, as is an inverse whose scaled-back entries overflow or come
267
+ * within their error bound of the overflow threshold, and a matrix whose
268
+ * scaling rounds an entry (one more than about `2^1074` below the largest of
269
+ * its column once its row is scaled, `2^-200` in `[[2^900, 2^900, 0, 0],
270
+ * [2^900, 2^900, 2^-200, 0], [0, 2^900, 2^900, 0], [0, 0, 0, 1]]`). So `false`
271
+ * is never a rounding artefact: `m` is singular, an entry is not finite, or an
272
+ * entry of its inverse, rounded once, is beyond `Number.MAX_VALUE`. `true`
273
+ * comes with an inverse backward stable for `m` with its rows and columns so
274
+ * scaled (a normwise backward error of about `2^-40`), whose entries far below
275
+ * the largest of that scaled inverse may carry no correct digit, as in any
276
+ * elimination. Allocates nothing unless the exact settlement runs: for a matrix
277
+ * of condition number beyond about `2^52`, an inverse that overflows, or an
278
+ * entry the scaling rounds. `out` may be `m`.
94
279
  */
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
- }
280
+ export function mat4x4Invert(out, m) {
281
+ return (invert_(out, m, false) || invertRescaled_(out, m, 4, invertDividing_, false));
282
+ }
283
+ /** {@link invert_} that divides by a subnormal determinant, for any layout. */
284
+ function invertDividing_(out, m) {
285
+ return invert_(out, m, true);
286
+ }
287
+ /**
288
+ * The inverse by the cofactors over the determinant, written to `out` only when
289
+ * every entry is finite. The reciprocal of the determinant multiplies the
290
+ * cofactors; when it overflows (a subnormal determinant) they are divided by
291
+ * the determinant instead, if `divide`, and not otherwise.
292
+ */
293
+ function invert_(out, m, divide) {
294
+ // nRC is the entry of row R, column C, counted from 1.
295
+ const n11 = m[0];
296
+ const n21 = m[1];
297
+ const n31 = m[2];
298
+ const n41 = m[3];
299
+ const n12 = m[4];
300
+ const n22 = m[5];
301
+ const n32 = m[6];
302
+ const n42 = m[7];
303
+ const n13 = m[8];
304
+ const n23 = m[9];
305
+ const n33 = m[10];
306
+ const n43 = m[11];
307
+ const n14 = m[12];
308
+ const n24 = m[13];
309
+ const n34 = m[14];
310
+ const n44 = m[15];
311
+ // `rIJcKL` is the minor of rows I and J, columns K and L.
312
+ const r34c34 = n33 * n44 - n34 * n43;
313
+ const r34c24 = n32 * n44 - n34 * n42;
314
+ const r34c23 = n32 * n43 - n33 * n42;
315
+ const r34c14 = n31 * n44 - n34 * n41;
316
+ const r34c13 = n31 * n43 - n33 * n41;
317
+ const r34c12 = n31 * n42 - n32 * n41;
318
+ const r24c34 = n23 * n44 - n24 * n43;
319
+ const r24c24 = n22 * n44 - n24 * n42;
320
+ const r24c23 = n22 * n43 - n23 * n42;
321
+ const r24c14 = n21 * n44 - n24 * n41;
322
+ const r24c13 = n21 * n43 - n23 * n41;
323
+ const r24c12 = n21 * n42 - n22 * n41;
324
+ const r23c34 = n23 * n34 - n24 * n33;
325
+ const r23c24 = n22 * n34 - n24 * n32;
326
+ const r23c23 = n22 * n33 - n23 * n32;
327
+ const r23c14 = n21 * n34 - n24 * n31;
328
+ const r23c13 = n21 * n33 - n23 * n31;
329
+ const r23c12 = n21 * n32 - n22 * n31;
330
+ const c11 = n22 * r34c34 - n23 * r34c24 + n24 * r34c23;
331
+ const c12 = -(n21 * r34c34 - n23 * r34c14 + n24 * r34c13);
332
+ const c13 = n21 * r34c24 - n22 * r34c14 + n24 * r34c12;
333
+ const c14 = -(n21 * r34c23 - n22 * r34c13 + n23 * r34c12);
334
+ const determinant = n11 * c11 + n12 * c12 + n13 * c13 + n14 * c14;
335
+ if (determinant === 0 || !Number.isFinite(determinant)) {
336
+ return false;
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
+ // A forward error bound of the expansion (Higham, "Accuracy and Stability
351
+ // of Numerical Algorithms", §3.1), from the same expansion of the absolute
352
+ // values: a minor `x·y − z·w` is within 3u·R + η of its value, R = |x·y| +
353
+ // |z·w|; a cofactor within 7u·Q + (3M + 2)·2η, Q = Σ|n|·R and M the largest
354
+ // entry; the determinant within 12u·P + (4M·(3M + 2)·2 + 3)·η, P = Σ|n|·Q
355
+ // along the first row. The inverse is accepted when the determinant is
356
+ // within 2^-41 of itself and the cofactors within 2^-41 / 4 of the largest
357
+ // of them, a normwise backward error of at most about 2^-40; otherwise an
358
+ // expansion that rounded nowhere is accepted too. P and every Q are
359
+ // permanents of |m| or of its blocks: expanded along the last row, each is
360
+ // at most the sum over its entries `|n4k|` of the products of the other
361
+ // columns' sums over the rows 1 to 3 (each term of a permanent is one of
362
+ // the terms those products expand to), which holds no translation of an
363
+ // affine matrix. The largest cofactor is first bounded below by the
364
+ // cofactors of the last row, which hold the translation of the inverse and,
365
+ // expanded along that row, its determinant; when these bounds are not
366
+ // enough, every cofactor is taken, then P and the Q exactly
367
+ // ({@link isWithinRoundingTolerance_}).
368
+ const a41 = Math.abs(n41);
369
+ const a42 = Math.abs(n42);
370
+ const a43 = Math.abs(n43);
371
+ const a44 = Math.abs(n44);
372
+ const s1 = Math.abs(n11) + Math.abs(n21) + Math.abs(n31);
373
+ const s2 = Math.abs(n12) + Math.abs(n22) + Math.abs(n32);
374
+ const s3 = Math.abs(n13) + Math.abs(n23) + Math.abs(n33);
375
+ const s4 = Math.abs(n14) + Math.abs(n24) + Math.abs(n34);
376
+ const s12 = s1 * s2;
377
+ const s13 = s1 * s3;
378
+ const s14 = s1 * s4;
379
+ const s23 = s2 * s3;
380
+ const s24 = s2 * s4;
381
+ const s34 = s3 * s4;
382
+ // `e_j`: the product of the sums but the j-th.
383
+ const e1 = s2 * s34;
384
+ const e2 = s1 * s34;
385
+ const e3 = s12 * s4;
386
+ const e4 = s12 * s3;
387
+ const permanentBound = a41 * e1 + a42 * e2 + a43 * e3 + a44 * e4;
388
+ // Sums for maxima, an upper bound of the largest Q and of M, and a quarter
389
+ // of the sum for the largest cofactor of the last row, a lower bound: a
390
+ // sum costs less than a maximum on a double.
391
+ const row4 = a41 + a42 + a43 + a44;
392
+ const cofactorBound = e1 + e2 + e3 + e4 + row4 * (s12 + s13 + s14 + s23 + s24 + s34);
393
+ const largestEntry = s1 + s2 + s3 + s4 + row4;
394
+ const magnitude = Math.abs(determinant);
395
+ const outerCofactor = 0.25 * (Math.abs(c41) + Math.abs(c42) + Math.abs(c43) + Math.abs(c44));
396
+ // Below 2^200 for M, and above 2^-400 for the determinant and the largest
397
+ // cofactor, the underflow terms are far below their share.
398
+ if (!(12 * UNIT_ROUNDOFF_ * permanentBound <= ROUNDING_TOLERANCE_ * magnitude &&
399
+ 28 * UNIT_ROUNDOFF_ * cofactorBound <=
400
+ ROUNDING_TOLERANCE_ * outerCofactor &&
401
+ largestEntry <= 2 ** 200 &&
402
+ magnitude >= 2 ** -400 &&
403
+ outerCofactor >= 2 ** -400)) {
404
+ const largestCofactor = Math.max(Math.abs(c11), Math.abs(c12), Math.abs(c13), Math.abs(c14), Math.abs(c21), Math.abs(c22), Math.abs(c23), Math.abs(c24), Math.abs(c31), Math.abs(c32), Math.abs(c33), Math.abs(c34), Math.abs(c41), Math.abs(c42), Math.abs(c43), Math.abs(c44));
405
+ const cofactorUnderflow = (3 * largestEntry + 2) * 2;
406
+ let accepted = (4 * largestEntry * cofactorUnderflow + 3) * UNDERFLOW_UNIT_ <=
407
+ UNDERFLOW_TOLERANCE_ * magnitude &&
408
+ 4 * cofactorUnderflow * UNDERFLOW_UNIT_ <=
409
+ UNDERFLOW_TOLERANCE_ * largestCofactor;
410
+ if (accepted &&
411
+ !(12 * UNIT_ROUNDOFF_ * permanentBound <=
412
+ ROUNDING_TOLERANCE_ * magnitude &&
413
+ 28 * UNIT_ROUNDOFF_ * cofactorBound <=
414
+ ROUNDING_TOLERANCE_ * largestCofactor)) {
415
+ TOLERANCES_[0] = ROUNDING_TOLERANCE_ * magnitude;
416
+ TOLERANCES_[1] = ROUNDING_TOLERANCE_ * largestCofactor;
417
+ accepted = isWithinRoundingTolerance_(m);
418
+ }
419
+ if (!accepted && !isCofactorExpansionExact_(m, 4)) {
420
+ return false;
101
421
  }
102
422
  }
423
+ const scale = 1 / determinant;
424
+ const reciprocal = Number.isFinite(scale);
425
+ if (!reciprocal && !divide) {
426
+ return false;
427
+ }
428
+ // The inverse is the transpose of the cofactor matrix over the determinant.
429
+ const o0 = reciprocal ? c11 * scale : c11 / determinant;
430
+ const o1 = reciprocal ? c12 * scale : c12 / determinant;
431
+ const o2 = reciprocal ? c13 * scale : c13 / determinant;
432
+ const o3 = reciprocal ? c14 * scale : c14 / determinant;
433
+ const o4 = reciprocal ? c21 * scale : c21 / determinant;
434
+ const o5 = reciprocal ? c22 * scale : c22 / determinant;
435
+ const o6 = reciprocal ? c23 * scale : c23 / determinant;
436
+ const o7 = reciprocal ? c24 * scale : c24 / determinant;
437
+ const o8 = reciprocal ? c31 * scale : c31 / determinant;
438
+ const o9 = reciprocal ? c32 * scale : c32 / determinant;
439
+ const o10 = reciprocal ? c33 * scale : c33 / determinant;
440
+ const o11 = reciprocal ? c34 * scale : c34 / determinant;
441
+ const o12 = reciprocal ? c41 * scale : c41 / determinant;
442
+ const o13 = reciprocal ? c42 * scale : c42 / determinant;
443
+ const o14 = reciprocal ? c43 * scale : c43 / determinant;
444
+ const o15 = reciprocal ? c44 * scale : c44 / determinant;
445
+ // Within INVERSE_LIMIT_, the exact entries, within about 2^-40 of the
446
+ // largest of these, round to finite numbers; past it, or not finite, the
447
+ // rescaled path decides.
448
+ if (!(Math.max(Math.abs(o0), Math.abs(o1), Math.abs(o2), Math.abs(o3), Math.abs(o4), Math.abs(o5), Math.abs(o6), Math.abs(o7), Math.abs(o8), Math.abs(o9), Math.abs(o10), Math.abs(o11), Math.abs(o12), Math.abs(o13), Math.abs(o14), Math.abs(o15)) <= INVERSE_LIMIT_)) {
449
+ return false;
450
+ }
451
+ out[0] = o0;
452
+ out[1] = o1;
453
+ out[2] = o2;
454
+ out[3] = o3;
455
+ out[4] = o4;
456
+ out[5] = o5;
457
+ out[6] = o6;
458
+ out[7] = o7;
459
+ out[8] = o8;
460
+ out[9] = o9;
461
+ out[10] = o10;
462
+ out[11] = o11;
463
+ out[12] = o12;
464
+ out[13] = o13;
465
+ out[14] = o14;
466
+ out[15] = o15;
103
467
  return true;
104
468
  }
105
469
  /**
106
- * Creates a 4×4 matrix from a quaternion (pure rotation, no translation or
107
- * scale).
470
+ * What {@link isWithinRoundingTolerance_} compares its bounds with: the
471
+ * determinant's, then the largest cofactor's, each times `ROUNDING_TOLERANCE_`.
472
+ * Passed through typed storage, as a double passed to a call the engine does
473
+ * not inline is boxed.
474
+ */
475
+ const TOLERANCES_ = new Float64Array(2);
476
+ /**
477
+ * Whether the rounding terms of `invert_`'s forward error bound, `12u·P` of the
478
+ * determinant and `28u` times the largest `Q` of the cofactors, computed
479
+ * exactly as the same expansion of `|m|`, are within {@link TOLERANCES_}: the
480
+ * second chance of a matrix whose rows' and columns' sums bounded them too
481
+ * loosely. Time complexity: O(1), with no allocation.
108
482
  */
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
- ];
483
+ function isWithinRoundingTolerance_(m) {
484
+ const a11 = Math.abs(m[0]);
485
+ const a21 = Math.abs(m[1]);
486
+ const a31 = Math.abs(m[2]);
487
+ const a41 = Math.abs(m[3]);
488
+ const a12 = Math.abs(m[4]);
489
+ const a22 = Math.abs(m[5]);
490
+ const a32 = Math.abs(m[6]);
491
+ const a42 = Math.abs(m[7]);
492
+ const a13 = Math.abs(m[8]);
493
+ const a23 = Math.abs(m[9]);
494
+ const a33 = Math.abs(m[10]);
495
+ const a43 = Math.abs(m[11]);
496
+ const a14 = Math.abs(m[12]);
497
+ const a24 = Math.abs(m[13]);
498
+ const a34 = Math.abs(m[14]);
499
+ const a44 = Math.abs(m[15]);
500
+ const b34c34 = a33 * a44 + a34 * a43;
501
+ const b34c24 = a32 * a44 + a34 * a42;
502
+ const b34c23 = a32 * a43 + a33 * a42;
503
+ const b34c14 = a31 * a44 + a34 * a41;
504
+ const b34c13 = a31 * a43 + a33 * a41;
505
+ const b34c12 = a31 * a42 + a32 * a41;
506
+ const b24c34 = a23 * a44 + a24 * a43;
507
+ const b24c24 = a22 * a44 + a24 * a42;
508
+ const b24c23 = a22 * a43 + a23 * a42;
509
+ const b24c14 = a21 * a44 + a24 * a41;
510
+ const b24c13 = a21 * a43 + a23 * a41;
511
+ const b24c12 = a21 * a42 + a22 * a41;
512
+ const b23c34 = a23 * a34 + a24 * a33;
513
+ const b23c24 = a22 * a34 + a24 * a32;
514
+ const b23c23 = a22 * a33 + a23 * a32;
515
+ const b23c14 = a21 * a34 + a24 * a31;
516
+ const b23c13 = a21 * a33 + a23 * a31;
517
+ const b23c12 = a21 * a32 + a22 * a31;
518
+ const q11 = a22 * b34c34 + a23 * b34c24 + a24 * b34c23;
519
+ const q12 = a21 * b34c34 + a23 * b34c14 + a24 * b34c13;
520
+ const q13 = a21 * b34c24 + a22 * b34c14 + a24 * b34c12;
521
+ const q14 = a21 * b34c23 + a22 * b34c13 + a23 * b34c12;
522
+ const permanent = a11 * q11 + a12 * q12 + a13 * q13 + a14 * q14;
523
+ const largestBound = Math.max(q11, q12, q13, q14, a12 * b34c34 + a13 * b34c24 + a14 * b34c23, a11 * b34c34 + a13 * b34c14 + a14 * b34c13, a11 * b34c24 + a12 * b34c14 + a14 * b34c12, a11 * b34c23 + a12 * b34c13 + a13 * b34c12, a12 * b24c34 + a13 * b24c24 + a14 * b24c23, a11 * b24c34 + a13 * b24c14 + a14 * b24c13, a11 * b24c24 + a12 * b24c14 + a14 * b24c12, a11 * b24c23 + a12 * b24c13 + a13 * b24c12, a12 * b23c34 + a13 * b23c24 + a14 * b23c23, a11 * b23c34 + a13 * b23c14 + a14 * b23c13, a11 * b23c24 + a12 * b23c14 + a14 * b23c12, a11 * b23c23 + a12 * b23c13 + a13 * b23c12);
524
+ return (12 * UNIT_ROUNDOFF_ * permanent <= TOLERANCES_[0] &&
525
+ 28 * UNIT_ROUNDOFF_ * largestBound <= TOLERANCES_[1]);
120
526
  }
121
- /** Creates a 4×4 rotation matrix around the X axis (right-hand rule). */
122
- export function mat4x4FromRotationX(angle) {
527
+ /** Writes the translation by `translation` into `out`. */
528
+ export function mat4x4FromTranslation(out, translation) {
529
+ out[0] = 1;
530
+ out[1] = 0;
531
+ out[2] = 0;
532
+ out[3] = 0;
533
+ out[4] = 0;
534
+ out[5] = 1;
535
+ out[6] = 0;
536
+ out[7] = 0;
537
+ out[8] = 0;
538
+ out[9] = 0;
539
+ out[10] = 1;
540
+ out[11] = 0;
541
+ out[12] = translation[0];
542
+ out[13] = translation[1];
543
+ out[14] = translation[2];
544
+ out[15] = 1;
545
+ return out;
546
+ }
547
+ /** Writes the scale `diag(scale, 1)` into `out`. */
548
+ export function mat4x4FromScale(out, scale) {
549
+ out[0] = scale[0];
550
+ out[1] = 0;
551
+ out[2] = 0;
552
+ out[3] = 0;
553
+ out[4] = 0;
554
+ out[5] = scale[1];
555
+ out[6] = 0;
556
+ out[7] = 0;
557
+ out[8] = 0;
558
+ out[9] = 0;
559
+ out[10] = scale[2];
560
+ out[11] = 0;
561
+ out[12] = 0;
562
+ out[13] = 0;
563
+ out[14] = 0;
564
+ out[15] = 1;
565
+ return out;
566
+ }
567
+ /**
568
+ * Writes the rotation by `angle` radians about the x axis (right-hand rule)
569
+ * into `out`.
570
+ */
571
+ export function mat4x4FromRotationX(out, angle) {
123
572
  const cos = Math.cos(angle);
124
573
  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
- ];
574
+ out[0] = 1;
575
+ out[1] = 0;
576
+ out[2] = 0;
577
+ out[3] = 0;
578
+ out[4] = 0;
579
+ out[5] = cos;
580
+ out[6] = sin;
581
+ out[7] = 0;
582
+ out[8] = 0;
583
+ out[9] = -sin;
584
+ out[10] = cos;
585
+ out[11] = 0;
586
+ out[12] = 0;
587
+ out[13] = 0;
588
+ out[14] = 0;
589
+ out[15] = 1;
590
+ return out;
131
591
  }
132
- /** Creates a 4×4 rotation matrix around the Y axis (right-hand rule). */
133
- export function mat4x4FromRotationY(angle) {
592
+ /**
593
+ * Writes the rotation by `angle` radians about the y axis (right-hand rule)
594
+ * into `out`.
595
+ */
596
+ export function mat4x4FromRotationY(out, angle) {
134
597
  const cos = Math.cos(angle);
135
598
  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
- ];
599
+ out[0] = cos;
600
+ out[1] = 0;
601
+ out[2] = -sin;
602
+ out[3] = 0;
603
+ out[4] = 0;
604
+ out[5] = 1;
605
+ out[6] = 0;
606
+ out[7] = 0;
607
+ out[8] = sin;
608
+ out[9] = 0;
609
+ out[10] = cos;
610
+ out[11] = 0;
611
+ out[12] = 0;
612
+ out[13] = 0;
613
+ out[14] = 0;
614
+ out[15] = 1;
615
+ return out;
142
616
  }
143
- /** Creates a 4×4 rotation matrix around the Z axis (right-hand rule). */
144
- export function mat4x4FromRotationZ(angle) {
617
+ /**
618
+ * Writes the rotation by `angle` radians about the z axis (right-hand rule)
619
+ * into `out`.
620
+ */
621
+ export function mat4x4FromRotationZ(out, angle) {
145
622
  const cos = Math.cos(angle);
146
623
  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
- ];
624
+ out[0] = cos;
625
+ out[1] = sin;
626
+ out[2] = 0;
627
+ out[3] = 0;
628
+ out[4] = -sin;
629
+ out[5] = cos;
630
+ out[6] = 0;
631
+ out[7] = 0;
632
+ out[8] = 0;
633
+ out[9] = 0;
634
+ out[10] = 1;
635
+ out[11] = 0;
636
+ out[12] = 0;
637
+ out[13] = 0;
638
+ out[14] = 0;
639
+ out[15] = 1;
640
+ return out;
153
641
  }
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
- ];
642
+ /**
643
+ * Writes the rotation of the unit quaternion `q` into `out`, no translation and
644
+ * no scale. `q` must be unit length; it is not normalised here.
645
+ */
646
+ export function mat4x4FromQuaternion(out, q) {
647
+ const x = q[0];
648
+ const y = q[1];
649
+ const z = q[2];
650
+ const w = q[3];
651
+ const xx = x * x;
652
+ const yy = y * y;
653
+ const zz = z * z;
654
+ const xy = x * y;
655
+ const xz = x * z;
656
+ const yz = y * z;
657
+ const wx = w * x;
658
+ const wy = w * y;
659
+ const wz = w * z;
660
+ out[0] = 1 - 2 * (yy + zz);
661
+ out[1] = 2 * (xy + wz);
662
+ out[2] = 2 * (xz - wy);
663
+ out[3] = 0;
664
+ out[4] = 2 * (xy - wz);
665
+ out[5] = 1 - 2 * (xx + zz);
666
+ out[6] = 2 * (yz + wx);
667
+ out[7] = 0;
668
+ out[8] = 2 * (xz + wy);
669
+ out[9] = 2 * (yz - wx);
670
+ out[10] = 1 - 2 * (xx + yy);
671
+ out[11] = 0;
672
+ out[12] = 0;
673
+ out[13] = 0;
674
+ out[14] = 0;
675
+ out[15] = 1;
676
+ return out;
161
677
  }
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
- ];
678
+ /**
679
+ * Writes the transform that scales, then rotates, then translates into `out`:
680
+ * `T · R · S`, with `rotation` a unit quaternion (not normalised here).
681
+ */
682
+ export function mat4x4Compose(out, translation, rotation, scale) {
683
+ const x = rotation[0];
684
+ const y = rotation[1];
685
+ const z = rotation[2];
686
+ const w = rotation[3];
687
+ const sx = scale[0];
688
+ const sy = scale[1];
689
+ const sz = scale[2];
690
+ const xx = x * x;
691
+ const yy = y * y;
692
+ const zz = z * z;
693
+ const xy = x * y;
694
+ const xz = x * z;
695
+ const yz = y * z;
696
+ const wx = w * x;
697
+ const wy = w * y;
698
+ const wz = w * z;
699
+ out[0] = (1 - 2 * (yy + zz)) * sx;
700
+ out[1] = 2 * (xy + wz) * sx;
701
+ out[2] = 2 * (xz - wy) * sx;
702
+ out[3] = 0;
703
+ out[4] = 2 * (xy - wz) * sy;
704
+ out[5] = (1 - 2 * (xx + zz)) * sy;
705
+ out[6] = 2 * (yz + wx) * sy;
706
+ out[7] = 0;
707
+ out[8] = 2 * (xz + wy) * sz;
708
+ out[9] = 2 * (yz - wx) * sz;
709
+ out[10] = (1 - 2 * (xx + yy)) * sz;
710
+ out[11] = 0;
711
+ out[12] = translation[0];
712
+ out[13] = translation[1];
713
+ out[14] = translation[2];
714
+ out[15] = 1;
715
+ return out;
169
716
  }
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
- ];
717
+ /**
718
+ * Writes the rotation by `angle` radians about the unit vector `axis`
719
+ * (right-hand rule, Rodrigues' formula) into `out`, no translation. `axis` is
720
+ * not normalised here: for any other length the matrix is not a rotation.
721
+ */
722
+ export function mat4x4FromAxisAngle(out, axis, angle) {
723
+ const x = axis[0];
724
+ const y = axis[1];
725
+ const z = axis[2];
726
+ const sin = Math.sin(angle);
727
+ // 1 − cos as twice the squared half-angle sine, and the diagonal as one
728
+ // minus its share: exact in the first digits for the small angles where
729
+ // cos − 1 would lose them, and exactly 1 on the axis itself.
730
+ const half = Math.sin(angle / 2);
731
+ const rest = 2 * half * half;
732
+ out[0] = 1 - rest * (y * y + z * z);
733
+ out[1] = rest * x * y + sin * z;
734
+ out[2] = rest * x * z - sin * y;
735
+ out[3] = 0;
736
+ out[4] = rest * x * y - sin * z;
737
+ out[5] = 1 - rest * (x * x + z * z);
738
+ out[6] = rest * y * z + sin * x;
739
+ out[7] = 0;
740
+ out[8] = rest * x * z + sin * y;
741
+ out[9] = rest * y * z - sin * x;
742
+ out[10] = 1 - rest * (x * x + y * y);
743
+ out[11] = 0;
744
+ out[12] = 0;
745
+ out[13] = 0;
746
+ out[14] = 0;
747
+ out[15] = 1;
748
+ return out;
223
749
  }
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]);
750
+ /**
751
+ * Writes the 3 × 3 matrix `m` as the upper-left block of a 4 × 4 matrix whose
752
+ * last row and column are those of the identity into `out`.
753
+ */
754
+ export function mat4x4FromMat3x3(out, m) {
755
+ out[0] = m[0];
756
+ out[1] = m[1];
757
+ out[2] = m[2];
758
+ out[3] = 0;
759
+ out[4] = m[3];
760
+ out[5] = m[4];
761
+ out[6] = m[5];
762
+ out[7] = 0;
763
+ out[8] = m[6];
764
+ out[9] = m[7];
765
+ out[10] = m[8];
766
+ out[11] = 0;
767
+ out[12] = 0;
768
+ out[13] = 0;
769
+ out[14] = 0;
770
+ out[15] = 1;
771
+ return out;
227
772
  }
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;
773
+ /**
774
+ * Writes the 3D affine `transform` as a 4 × 4 matrix into `out`: the bottom
775
+ * row, which an {@link Affine3} does not store, is `0, 0, 0, 1`.
776
+ */
777
+ export function mat4x4FromAffine3(out, transform) {
778
+ out[0] = transform[0];
779
+ out[1] = transform[1];
780
+ out[2] = transform[2];
781
+ out[3] = 0;
782
+ out[4] = transform[3];
783
+ out[5] = transform[4];
784
+ out[6] = transform[5];
785
+ out[7] = 0;
786
+ out[8] = transform[6];
787
+ out[9] = transform[7];
788
+ out[10] = transform[8];
789
+ out[11] = 0;
790
+ out[12] = transform[9];
791
+ out[13] = transform[10];
792
+ out[14] = transform[11];
793
+ out[15] = 1;
794
+ return out;
242
795
  }
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
- ];
796
+ /**
797
+ * Writes the 2D affine `transform` as a 4 × 4 matrix that acts on `x` and `y`
798
+ * and passes `z` and `w` through into `out`: a 2D view as the `viewProjection`
799
+ * of a 3D pipeline, the depth untouched.
800
+ */
801
+ export function mat4x4FromAffine2(out, transform) {
802
+ out[0] = transform[0];
803
+ out[1] = transform[1];
804
+ out[2] = 0;
805
+ out[3] = 0;
806
+ out[4] = transform[2];
807
+ out[5] = transform[3];
808
+ out[6] = 0;
809
+ out[7] = 0;
810
+ out[8] = 0;
811
+ out[9] = 0;
812
+ out[10] = 1;
813
+ out[11] = 0;
814
+ out[12] = transform[4];
815
+ out[13] = transform[5];
816
+ out[14] = 0;
817
+ out[15] = 1;
818
+ return out;
270
819
  }
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
- ];
820
+ /**
821
+ * Splits `m` into the translation, rotation and scale that {@link mat4x4Compose}
822
+ * builds it from: the translation is the last column, the scale is the length
823
+ * of each of the first three, and the rotation is the unit quaternion of those
824
+ * columns divided by it. A reflection (negative determinant) puts its sign in
825
+ * the x scale, so the rotation is proper.
826
+ *
827
+ * Returns `false` when there is no such split: a column of zero length or not
828
+ * finite, two columns further from perpendicular than about `1e-6` in cosine (a
829
+ * shear), a bottom row other than `0, 0, 0, 1` (a projection), or a translation
830
+ * that is not finite. The three outputs then hold `NaN`, never a stale or
831
+ * partial result, and nothing throws.
832
+ */
833
+ export function mat4x4Decompose(outTranslation, outRotation, outScale, m) {
834
+ const x = m[12];
835
+ const y = m[13];
836
+ const z = m[14];
837
+ if (m[3] === 0 &&
838
+ m[7] === 0 &&
839
+ m[11] === 0 &&
840
+ Math.abs(m[15] - 1) <= AFFINE_CORNER_TOLERANCE_ &&
841
+ Number.isFinite(x) &&
842
+ Number.isFinite(y) &&
843
+ Number.isFinite(z) &&
844
+ decomposeLinear_(outRotation, outScale, m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10])) {
845
+ outTranslation[0] = x;
846
+ outTranslation[1] = y;
847
+ outTranslation[2] = z;
848
+ return true;
849
+ }
850
+ outTranslation[0] = Number.NaN;
851
+ outTranslation[1] = Number.NaN;
852
+ outTranslation[2] = Number.NaN;
853
+ outRotation[0] = Number.NaN;
854
+ outRotation[1] = Number.NaN;
855
+ outRotation[2] = Number.NaN;
856
+ outRotation[3] = Number.NaN;
857
+ outScale[0] = Number.NaN;
858
+ outScale[1] = Number.NaN;
859
+ outScale[2] = Number.NaN;
860
+ return false;
861
+ }
862
+ /**
863
+ * Writes the product `m · transform` into `out`, the 3D affine `transform`
864
+ * taken as the 4 × 4 matrix with a bottom row `0, 0, 0, 1`: a projection times
865
+ * a view times an affine model, in plain arithmetic and straight-line. `out`
866
+ * may be `m`.
867
+ */
868
+ export function mat4x4MultiplyAffine3(out, m, transform) {
869
+ const m0 = m[0];
870
+ const m1 = m[1];
871
+ const m2 = m[2];
872
+ const m3 = m[3];
873
+ const m4 = m[4];
874
+ const m5 = m[5];
875
+ const m6 = m[6];
876
+ const m7 = m[7];
877
+ const m8 = m[8];
878
+ const m9 = m[9];
879
+ const m10 = m[10];
880
+ const m11 = m[11];
881
+ const m12 = m[12];
882
+ const m13 = m[13];
883
+ const m14 = m[14];
884
+ const m15 = m[15];
885
+ const t0 = transform[0];
886
+ const t1 = transform[1];
887
+ const t2 = transform[2];
888
+ out[0] = m0 * t0 + m4 * t1 + m8 * t2;
889
+ out[1] = m1 * t0 + m5 * t1 + m9 * t2;
890
+ out[2] = m2 * t0 + m6 * t1 + m10 * t2;
891
+ out[3] = m3 * t0 + m7 * t1 + m11 * t2;
892
+ const t3 = transform[3];
893
+ const t4 = transform[4];
894
+ const t5 = transform[5];
895
+ out[4] = m0 * t3 + m4 * t4 + m8 * t5;
896
+ out[5] = m1 * t3 + m5 * t4 + m9 * t5;
897
+ out[6] = m2 * t3 + m6 * t4 + m10 * t5;
898
+ out[7] = m3 * t3 + m7 * t4 + m11 * t5;
899
+ const t6 = transform[6];
900
+ const t7 = transform[7];
901
+ const t8 = transform[8];
902
+ out[8] = m0 * t6 + m4 * t7 + m8 * t8;
903
+ out[9] = m1 * t6 + m5 * t7 + m9 * t8;
904
+ out[10] = m2 * t6 + m6 * t7 + m10 * t8;
905
+ out[11] = m3 * t6 + m7 * t7 + m11 * t8;
906
+ const t9 = transform[9];
907
+ const t10 = transform[10];
908
+ const t11 = transform[11];
909
+ out[12] = m0 * t9 + m4 * t10 + m8 * t11 + m12;
910
+ out[13] = m1 * t9 + m5 * t10 + m9 * t11 + m13;
911
+ out[14] = m2 * t9 + m6 * t10 + m10 * t11 + m14;
912
+ out[15] = m3 * t9 + m7 * t10 + m11 * t11 + m15;
913
+ return out;
298
914
  }
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
- ];
915
+ /**
916
+ * Writes the product `m · T(translation)` into `out`: `m` with a translation
917
+ * applied first, in its own local space, which is how a renderer folds its
918
+ * origin into a matrix. Only the last column changes. `out` may be `m`.
919
+ */
920
+ export function mat4x4Translate(out, m, translation) {
921
+ const x = translation[0];
922
+ const y = translation[1];
923
+ const z = translation[2];
924
+ const m0 = m[0];
925
+ const m1 = m[1];
926
+ const m2 = m[2];
927
+ const m3 = m[3];
928
+ const m4 = m[4];
929
+ const m5 = m[5];
930
+ const m6 = m[6];
931
+ const m7 = m[7];
932
+ const m8 = m[8];
933
+ const m9 = m[9];
934
+ const m10 = m[10];
935
+ const m11 = m[11];
936
+ const m12 = m[12];
937
+ const m13 = m[13];
938
+ const m14 = m[14];
939
+ const m15 = m[15];
940
+ out[0] = m0;
941
+ out[1] = m1;
942
+ out[2] = m2;
943
+ out[3] = m3;
944
+ out[4] = m4;
945
+ out[5] = m5;
946
+ out[6] = m6;
947
+ out[7] = m7;
948
+ out[8] = m8;
949
+ out[9] = m9;
950
+ out[10] = m10;
951
+ out[11] = m11;
952
+ out[12] = m0 * x + m4 * y + m8 * z + m12;
953
+ out[13] = m1 * x + m5 * y + m9 * z + m13;
954
+ out[14] = m2 * x + m6 * y + m10 * z + m14;
955
+ out[15] = m3 * x + m7 * y + m11 * z + m15;
956
+ return out;
326
957
  }
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
- ];
958
+ /**
959
+ * Writes the last column of `m` without its bottom entry, the translation of an
960
+ * affine matrix, into `out`.
961
+ */
962
+ export function mat4x4GetTranslation(out, m) {
963
+ out[0] = m[12];
964
+ out[1] = m[13];
965
+ out[2] = m[14];
966
+ return out;
334
967
  }
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
- ];
968
+ /**
969
+ * Writes the length of each of the first three columns of `m`, the scale of a
970
+ * matrix with no shear (the sign of a reflection is not recovered: see
971
+ * {@link mat4x4Decompose}), into `out`. Plain arithmetic: right between about
972
+ * `1e-150` and `1e150`.
973
+ */
974
+ export function mat4x4GetScale(out, m) {
975
+ const m0 = m[0];
976
+ const m1 = m[1];
977
+ const m2 = m[2];
978
+ const m4 = m[4];
979
+ const m5 = m[5];
980
+ const m6 = m[6];
981
+ const m8 = m[8];
982
+ const m9 = m[9];
983
+ const m10 = m[10];
984
+ out[0] = Math.sqrt(m0 * m0 + m1 * m1 + m2 * m2);
985
+ out[1] = Math.sqrt(m4 * m4 + m5 * m5 + m6 * m6);
986
+ out[2] = Math.sqrt(m8 * m8 + m9 * m9 + m10 * m10);
987
+ return out;
362
988
  }
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
- ];
989
+ /**
990
+ * Writes column `column` of `m`, 0 to 3, into `out`; any other `column` gives
991
+ * `NaN` throughout, never `undefined`.
992
+ */
993
+ export function mat4x4GetColumn(out, m, column) {
994
+ if (column === 0 || column === 1 || column === 2 || column === 3) {
995
+ const start = column * 4;
996
+ out[0] = m[start];
997
+ out[1] = m[start + 1];
998
+ out[2] = m[start + 2];
999
+ out[3] = m[start + 3];
1000
+ }
1001
+ else {
1002
+ out[0] = Number.NaN;
1003
+ out[1] = Number.NaN;
1004
+ out[2] = Number.NaN;
1005
+ out[3] = Number.NaN;
1006
+ }
1007
+ return out;
370
1008
  }
371
1009
  //# sourceMappingURL=mat4x4.js.map