@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/vec3.js CHANGED
@@ -1,314 +1,433 @@
1
- import { clamp, isCloseTo, PreciseSum, round, } from "@ac-kit/core";
2
- import { diffOfProducts, dotPrecise, lerp, TWO_PI } from "@ac-kit/math-scalar";
1
+ import { binaryExponent, scaleByPowerOfTwo } from "@ac-kit/core";
2
+ import { angleNormalizeRad, diffOfProducts, dotPrecise3, lerp, TWO_PI, } from "@ac-kit/math-scalar";
3
3
  import { SLERP_LINEAR_FALLBACK_DOT_ } from "./_slerp.js";
4
- export const VEC3_ZERO = [0, 0, 0];
5
- export const VEC3_ONE = [1, 1, 1];
6
- export function vec3Abs(v) {
7
- return [Math.abs(v[0]), Math.abs(v[1]), Math.abs(v[2])];
4
+ import { vec3DotPrecise, vec3LengthScaled, vec3NormalizeScaled, } from "./vec3-componentwise.js";
5
+ /**
6
+ * Writes the cross product `a × b` into `out`, in plain arithmetic: a vector
7
+ * perpendicular to both, of length `|a||b| sin θ`, right-handed (`x × y = z`).
8
+ * Where a component's two terms cancel use {@link vec3CrossPrecise}. `out` may
9
+ * be `a` or `b`.
10
+ */
11
+ export function vec3Cross(out, a, b) {
12
+ const ax = a[0];
13
+ const ay = a[1];
14
+ const az = a[2];
15
+ const bx = b[0];
16
+ const by = b[1];
17
+ const bz = b[2];
18
+ out[0] = ay * bz - az * by;
19
+ out[1] = az * bx - ax * bz;
20
+ out[2] = ax * by - ay * bx;
21
+ return out;
8
22
  }
9
- export function vec3Add(a, b) {
10
- return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
23
+ /**
24
+ * {@link vec3Cross} with the rounding of every product recovered
25
+ * (`@ac-kit/math-scalar`'s `diffOfProducts`): each component within 1.5 ULP of
26
+ * the exact value however badly its terms cancel. About five times the cost.
27
+ * `out` may be `a` or `b`.
28
+ */
29
+ export function vec3CrossPrecise(out, a, b) {
30
+ const ax = a[0];
31
+ const ay = a[1];
32
+ const az = a[2];
33
+ const bx = b[0];
34
+ const by = b[1];
35
+ const bz = b[2];
36
+ out[0] = diffOfProducts(ay, bz, az, by);
37
+ out[1] = diffOfProducts(az, bx, ax, bz);
38
+ out[2] = diffOfProducts(ax, by, ay, bx);
39
+ return out;
11
40
  }
12
- export function vec3AddScalar(v, s) {
13
- return [v[0] + s, v[1] + s, v[2] + s];
41
+ /**
42
+ * The sine of the angle between two vectors times their lengths, `|a × b|`,
43
+ * from the compensated cross product.
44
+ */
45
+ function crossLengthPrecise_(a, b) {
46
+ const x = diffOfProducts(a[1], b[2], a[2], b[1]);
47
+ const y = diffOfProducts(a[2], b[0], a[0], b[2]);
48
+ const z = diffOfProducts(a[0], b[1], a[1], b[0]);
49
+ return Math.sqrt(x * x + y * y + z * z);
14
50
  }
15
- export function vec3AddScaled(a, b, s) {
16
- return [a[0] + b[0] * s, a[1] + b[1] * s, a[2] + b[2] * s];
51
+ /**
52
+ * The signed angle from `a` to `b`, positive counter-clockwise when seen from
53
+ * the tip of `axis` (the right-hand rule about `axis`), in `(−π, π]`: `π` for
54
+ * opposite vectors. `axis` need not be a unit vector, and only its side of the
55
+ * plane of `a` and `b` matters. Computed from the compensated cross and dot
56
+ * products, so the angle of nearly parallel vectors keeps its digits. A zero
57
+ * vector gives `0`. Right while the product of the two lengths lies between
58
+ * about `1e−150` and `1e150`, which keeps the squares of the cross product in
59
+ * range (vectors of `1e100` give `Infinity` there and a wrong angle); an angle
60
+ * does not depend on the lengths, so beyond, normalise both vectors with
61
+ * {@link vec3NormalizeScaled} first.
62
+ */
63
+ export function vec3AngleTo(a, b, axis) {
64
+ const cx = diffOfProducts(a[1], b[2], a[2], b[1]);
65
+ const cy = diffOfProducts(a[2], b[0], a[0], b[2]);
66
+ const cz = diffOfProducts(a[0], b[1], a[1], b[0]);
67
+ const angle = Math.atan2(Math.sqrt(cx * cx + cy * cy + cz * cz), vec3DotPrecise(a, b) + 0);
68
+ return angleNormalizeRad(cx * axis[0] + cy * axis[1] + cz * axis[2] < 0 ? -angle : angle);
17
69
  }
18
70
  /**
19
- * Computes the angle in radians between two 3D vectors around a given axis. The
20
- * result is in the range (-π, π].
71
+ * The unsigned angle between two vectors, `atan2(|a × b|, a · b)`, in `[0, π]`,
72
+ * from the compensated cross and dot products. A zero vector gives `0`. Right
73
+ * while the product of the two lengths lies between about `1e−150` and `1e150`,
74
+ * which keeps the squares of the cross product in range (vectors of `1e100`
75
+ * give a wrong angle); an angle does not depend on the lengths, so beyond,
76
+ * normalise both vectors with {@link vec3NormalizeScaled} first.
21
77
  */
22
- export function vec3AngleTo(v1, v2, axis) {
23
- if (vec3LengthSq(v1) === 0 || vec3LengthSq(v2) === 0) {
24
- throw new Error("Cannot compute angle to/from a zero-length vector.");
25
- }
26
- const cross = vec3Cross(v1, v2);
27
- const angle = Math.atan2(vec3Length(cross), vec3Dot(v1, v2));
28
- const sign = vec3Dot(cross, axis) < 0 ? -1 : 1;
29
- return angle * sign;
78
+ export function vec3AngleBetween(a, b) {
79
+ return Math.atan2(crossLengthPrecise_(a, b), vec3DotPrecise(a, b) + 0);
30
80
  }
31
81
  /**
32
- * Computes the angle in radians between two 3D vectors. The result is in the
33
- * range [0, π].
82
+ * Writes `[a.x, a.y, z]` into `out`: the plane vector lifted to space.
34
83
  *
35
- * @throws Error if either vector is a zero-length vector.
84
+ * @param z The third component; defaults to `0`.
36
85
  */
37
- export function vec3AngleBetween(v1, v2) {
38
- const dot = vec3Dot(v1, v2);
39
- const len1 = vec3Length(v1);
40
- const len2 = vec3Length(v2);
41
- if (len1 === 0 || len2 === 0) {
42
- throw new Error("Cannot compute angle between zero-length vectors");
43
- }
44
- const cosTheta = clamp(dot / (len1 * len2), -1, 1);
45
- return Math.acos(cosTheta);
46
- }
47
- export function vec3ApplyMat3(v, m) {
48
- return [dotPrecise(v, m[0]), dotPrecise(v, m[1]), dotPrecise(v, m[2])];
86
+ export function vec3FromVec2(out, a, z = 0) {
87
+ out[0] = a[0];
88
+ out[1] = a[1];
89
+ out[2] = z;
90
+ return out;
49
91
  }
50
92
  /**
51
- * Applies a 4x4 matrix transformation to a 3D vector. The vector is treated as
52
- * a point in homogeneous coordinates (x, y, z, 1).
93
+ * Writes `[x, y, z] / w` into `out`: the Cartesian point of a homogeneous
94
+ * vector, the perspective divide. A `w` of zero (a point at infinity) gives
95
+ * non-finite components, `±Infinity` or `NaN`, not an exception.
53
96
  */
54
- export function vec3ApplyMat4(v, m) {
55
- // The homogeneous 1 makes the translation column a fourth term of the dot.
56
- const homogeneous = [v[0], v[1], v[2], 1];
57
- return [
58
- dotPrecise(homogeneous, m[0]),
59
- dotPrecise(homogeneous, m[1]),
60
- dotPrecise(homogeneous, m[2]),
61
- ];
62
- }
63
- export function vec3ApplyQuaternion(v, q) {
64
- const [qx, qy, qz, qw] = q;
65
- // Calculate quaternion * vector
66
- const ix = dotPrecise([qw, qy, -qz], [v[0], v[2], v[1]]);
67
- const iy = dotPrecise([qw, qz, -qx], [v[1], v[0], v[2]]);
68
- const iz = dotPrecise([qw, qx, -qy], [v[2], v[1], v[0]]);
69
- const iw = dotPrecise([-qx, -qy, -qz], v);
70
- // Calculate result * inverse quaternion
71
- return [
72
- dotPrecise([ix, -iw, -iy, iz], [qw, qx, qz, qy]),
73
- dotPrecise([iy, -iw, -iz, ix], [qw, qy, qx, qz]),
74
- dotPrecise([iz, -iw, -ix, iy], [qw, qz, qy, qx]),
75
- ];
97
+ export function vec3FromHomogeneous(out, a) {
98
+ const w = a[3];
99
+ out[0] = a[0] / w;
100
+ out[1] = a[1] / w;
101
+ out[2] = a[2] / w;
102
+ return out;
76
103
  }
77
- export function vec3Clamp(v, min, max) {
78
- return [
79
- clamp(v[0], min[0], max[0]),
80
- clamp(v[1], min[1], max[1]),
81
- clamp(v[2], min[2], max[2]),
82
- ];
104
+ /**
105
+ * Writes `a` rotated by `angle` radians about the unit vector `axis`
106
+ * (Rodrigues' formula), counter-clockwise seen from the tip of `axis`, into
107
+ * `out`. `axis` is not normalised here: a vector that is not unit scales the
108
+ * result. `out` may be `a` or `axis`.
109
+ */
110
+ export function vec3RotateAxisAngle(out, a, axis, angle) {
111
+ const x = a[0];
112
+ const y = a[1];
113
+ const z = a[2];
114
+ const kx = axis[0];
115
+ const ky = axis[1];
116
+ const kz = axis[2];
117
+ const cos = Math.cos(angle);
118
+ const sin = Math.sin(angle);
119
+ const along = (kx * x + ky * y + kz * z) * (1 - cos);
120
+ out[0] = x * cos + (ky * z - kz * y) * sin + kx * along;
121
+ out[1] = y * cos + (kz * x - kx * z) * sin + ky * along;
122
+ out[2] = z * cos + (kx * y - ky * x) * sin + kz * along;
123
+ return out;
83
124
  }
84
- export function vec3ClampLength(v, minLength, maxLength) {
85
- const length = vec3Length(v);
86
- if (length === 0) {
87
- return v; // Can't scale a zero-length vector
125
+ /**
126
+ * Writes into `out` a unit vector perpendicular to the scalar components `[x,
127
+ * y, z]`: their cross product with the axis of the smallest component, which is
128
+ * as far from parallel as an axis can be, normalised at any magnitude.
129
+ */
130
+ function perpendicular_(out, x, y, z) {
131
+ const absoluteX = Math.abs(x);
132
+ const absoluteY = Math.abs(y);
133
+ const absoluteZ = Math.abs(z);
134
+ if (absoluteX <= absoluteY && absoluteX <= absoluteZ) {
135
+ out[0] = 0;
136
+ out[1] = z;
137
+ out[2] = -y;
88
138
  }
89
- const clampedLength = clamp(length, minLength, maxLength);
90
- return vec3MultiplyScalar(v, clampedLength / length);
91
- }
92
- export function vec3ClampScalar(v, min, max) {
93
- return [clamp(v[0], min, max), clamp(v[1], min, max), clamp(v[2], min, max)];
94
- }
95
- export function vec3Cross(a, b) {
96
- return [
97
- diffOfProducts(a[1], b[2], a[2], b[1]),
98
- diffOfProducts(a[2], b[0], a[0], b[2]),
99
- diffOfProducts(a[0], b[1], a[1], b[0]),
100
- ];
101
- }
102
- export function vec3DistanceTo(a, b, type = "euclidean") {
103
- const dx = Math.abs(a[0] - b[0]);
104
- const dy = Math.abs(a[1] - b[1]);
105
- const dz = Math.abs(a[2] - b[2]);
106
- switch (type) {
107
- case "euclidean":
108
- return Math.hypot(dx, dy, dz);
109
- case "manhattan":
110
- return dx + dy + dz;
111
- case "chebyshev":
112
- return Math.max(dx, dy, dz);
139
+ else if (absoluteY <= absoluteZ) {
140
+ out[0] = -z;
141
+ out[1] = 0;
142
+ out[2] = x;
113
143
  }
114
- }
115
- export function vec3DistanceToSq(a, b) {
116
- const dx = a[0] - b[0];
117
- const dy = a[1] - b[1];
118
- const dz = a[2] - b[2];
119
- return dx * dx + dy * dy + dz * dz;
120
- }
121
- export function vec3Divide(a, b) {
122
- return [a[0] / b[0], a[1] / b[1], a[2] / b[2]];
123
- }
124
- export function vec3DivideScalar(v, s) {
125
- return [v[0] / s, v[1] / s, v[2] / s];
126
- }
127
- export function vec3Dot(a, b) {
128
- return dotPrecise(a, b);
129
- }
130
- /** Exact component-wise equality. See {@link vec3IsClose} for a tolerant test. */
131
- export function vec3Equals(a, b) {
132
- return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
144
+ else {
145
+ out[0] = y;
146
+ out[1] = -x;
147
+ out[2] = 0;
148
+ }
149
+ return vec3NormalizeScaled(out, out);
133
150
  }
134
151
  /**
135
- * Component-wise approximate equality.
136
- *
137
- * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
138
- * and never a basis for ordering — use {@link vec3Equals} there. `tolerance` is
139
- * required: a vector carries no units of its own, so only the caller knows what
140
- * counts as close.
152
+ * Writes a unit vector perpendicular to `a` into `out`: `a` crossed with the
153
+ * axis of its smallest component, normalised at any magnitude. The result is
154
+ * not unique; this one is stable, so a continuous input gives it a few jumps
155
+ * only. The zero vector stays zero. `out` may be `a`.
141
156
  */
142
- export function vec3IsClose(a, b, tolerance) {
143
- return (isCloseTo(a[0], b[0], tolerance) &&
144
- isCloseTo(a[1], b[1], tolerance) &&
145
- isCloseTo(a[2], b[2], tolerance));
157
+ export function vec3Perpendicular(out, a) {
158
+ return perpendicular_(out, a[0], a[1], a[2]);
146
159
  }
147
- export function vec3FromVec2(v, z = 0) {
148
- return [v[0], v[1], z];
149
- }
150
- export function vec3Length(v, type = "euclidean") {
151
- switch (type) {
152
- case "euclidean":
153
- return Math.hypot(v[0], v[1], v[2]);
154
- case "manhattan":
155
- return Math.abs(v[0]) + Math.abs(v[1]) + Math.abs(v[2]);
156
- case "chebyshev":
157
- return Math.max(Math.abs(v[0]), Math.abs(v[1]), Math.abs(v[2]));
158
- }
160
+ /**
161
+ * Writes the tangent and the bitangent that complete the unit vector `normal`
162
+ * to a right-handed orthonormal basis (`tangent × bitangent = normal`), by
163
+ * Duff, Burgess, Christensen, Hery, Kensler, Liani and Villemin's branch-light
164
+ * construction ("Building an Orthonormal Basis, Revisited", 2017): no
165
+ * normalisation and no singularity, whatever the direction of `normal`.
166
+ * `normal` is not normalised here. The outputs may be `normal`.
167
+ */
168
+ export function vec3OrthonormalBasis(outTangent, outBitangent, normal) {
169
+ const x = normal[0];
170
+ const y = normal[1];
171
+ const z = normal[2];
172
+ const sign = z < 0 ? -1 : 1;
173
+ const a = -1 / (sign + z);
174
+ const b = x * y * a;
175
+ outTangent[0] = 1 + sign * x * x * a;
176
+ outTangent[1] = sign * b;
177
+ outTangent[2] = -sign * x;
178
+ outBitangent[0] = b;
179
+ outBitangent[1] = sign + y * y * a;
180
+ outBitangent[2] = -y;
159
181
  }
160
- export function vec3LengthSq(v) {
161
- return vec3Dot(v, v);
182
+ /**
183
+ * Writes `a` minus its component along `normal` into `out`: `a` flattened onto
184
+ * the plane through the origin whose normal is `normal`. `normal` need not be a
185
+ * unit vector; a zero `normal` gives `NaN`. Right when the largest component of
186
+ * `normal` lies between about `1e−150` and `1e150`, where its squared length
187
+ * stays in range: above about `1.3e154` the result is `a` unflattened, and
188
+ * below about `1.5e−154` it is not finite. Beyond, normalise `normal` with
189
+ * {@link vec3NormalizeScaled} first. `out` may be `a` or `normal`.
190
+ */
191
+ export function vec3ProjectOntoPlane(out, a, normal) {
192
+ const x = a[0];
193
+ const y = a[1];
194
+ const z = a[2];
195
+ const nx = normal[0];
196
+ const ny = normal[1];
197
+ const nz = normal[2];
198
+ const scale = (x * nx + y * ny + z * nz) / (nx * nx + ny * ny + nz * nz);
199
+ out[0] = x - nx * scale;
200
+ out[1] = y - ny * scale;
201
+ out[2] = z - nz * scale;
202
+ return out;
162
203
  }
163
- export function vec3Lerp(a, b, t) {
164
- return [lerp(a[0], b[0], t), lerp(a[1], b[1], t), lerp(a[2], b[2], t)];
204
+ /**
205
+ * Writes `m · a` into `out`, the matrix applied to the column vector. `out` may
206
+ * be `a`.
207
+ */
208
+ export function vec3TransformMat3x3(out, a, m) {
209
+ const x = a[0];
210
+ const y = a[1];
211
+ const z = a[2];
212
+ out[0] = m[0] * x + m[3] * y + m[6] * z;
213
+ out[1] = m[1] * x + m[4] * y + m[7] * z;
214
+ out[2] = m[2] * x + m[5] * y + m[8] * z;
215
+ return out;
165
216
  }
166
- export function vec3Max(a, b) {
167
- return [Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.max(a[2], b[2])];
217
+ /**
218
+ * {@link vec3TransformMat3x3} with each component a compensated dot product, as
219
+ * accurate as one in doubled precision rounded once. `out` may be `a`.
220
+ */
221
+ export function vec3TransformMat3x3Precise(out, a, m) {
222
+ const x = a[0];
223
+ const y = a[1];
224
+ const z = a[2];
225
+ out[0] = dotPrecise3(m[0], x, m[3], y, m[6], z);
226
+ out[1] = dotPrecise3(m[1], x, m[4], y, m[7], z);
227
+ out[2] = dotPrecise3(m[2], x, m[5], y, m[8], z);
228
+ return out;
168
229
  }
169
- export function vec3Min(a, b) {
170
- return [Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.min(a[2], b[2])];
230
+ /**
231
+ * Writes the point `a` (as `[x, y, z, 1]`) transformed by `m` into `out`,
232
+ * divided by the resulting `w`, as gl-matrix and three.js do: for an affine
233
+ * matrix `w` is exactly `1` and nothing changes, for a projection this is the
234
+ * perspective divide. A `w` of zero gives non-finite components, not an
235
+ * exception. `out` may be `a`.
236
+ */
237
+ export function vec3TransformMat4x4(out, a, m) {
238
+ const x = a[0];
239
+ const y = a[1];
240
+ const z = a[2];
241
+ const w = m[3] * x + m[7] * y + m[11] * z + m[15];
242
+ out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
243
+ out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
244
+ out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
245
+ return out;
171
246
  }
172
- export function vec3Minmax(vs) {
173
- let minX = Infinity, minY = Infinity, minZ = Infinity;
174
- let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
175
- for (const v of vs) {
176
- if (v[0] < minX)
177
- minX = v[0];
178
- if (v[1] < minY)
179
- minY = v[1];
180
- if (v[2] < minZ)
181
- minZ = v[2];
182
- if (v[0] > maxX)
183
- maxX = v[0];
184
- if (v[1] > maxY)
185
- maxY = v[1];
186
- if (v[2] > maxZ)
187
- maxZ = v[2];
188
- }
189
- return { min: [minX, minY, minZ], max: [maxX, maxY, maxZ] };
247
+ /**
248
+ * Writes the direction `a` transformed by the upper-left 3×3 part of `m`, with
249
+ * no translation and no divide, into `out`. `out` may be `a`.
250
+ */
251
+ export function vec3TransformDirectionMat4x4(out, a, m) {
252
+ const x = a[0];
253
+ const y = a[1];
254
+ const z = a[2];
255
+ out[0] = m[0] * x + m[4] * y + m[8] * z;
256
+ out[1] = m[1] * x + m[5] * y + m[9] * z;
257
+ out[2] = m[2] * x + m[6] * y + m[10] * z;
258
+ return out;
190
259
  }
191
- export function vec3Multiply(a, b) {
192
- return [a[0] * b[0], a[1] * b[1], a[2] * b[2]];
260
+ /**
261
+ * Writes the point `a` transformed by the affine transform (the linear part
262
+ * applied, then the translation added) into `out`. `out` may be `a`.
263
+ */
264
+ export function vec3TransformAffine3(out, a, transform) {
265
+ const x = a[0];
266
+ const y = a[1];
267
+ const z = a[2];
268
+ out[0] =
269
+ transform[0] * x + transform[3] * y + transform[6] * z + transform[9];
270
+ out[1] =
271
+ transform[1] * x + transform[4] * y + transform[7] * z + transform[10];
272
+ out[2] =
273
+ transform[2] * x + transform[5] * y + transform[8] * z + transform[11];
274
+ return out;
193
275
  }
194
- export function vec3MultiplyScalar(v, s) {
195
- return [v[0] * s, v[1] * s, v[2] * s];
276
+ /**
277
+ * Writes the direction `a` transformed by the linear part of the affine
278
+ * transform, with no translation, into `out`. `out` may be `a`.
279
+ */
280
+ export function vec3TransformDirectionAffine3(out, a, transform) {
281
+ const x = a[0];
282
+ const y = a[1];
283
+ const z = a[2];
284
+ out[0] = transform[0] * x + transform[3] * y + transform[6] * z;
285
+ out[1] = transform[1] * x + transform[4] * y + transform[7] * z;
286
+ out[2] = transform[2] * x + transform[5] * y + transform[8] * z;
287
+ return out;
196
288
  }
197
- export function vec3Negate(v) {
198
- return [-v[0], -v[1], -v[2]];
289
+ /**
290
+ * Writes `a` rotated by the unit quaternion `q` into `out`: `q · a · q⁻¹` in 18
291
+ * multiplications, no matrix. `q` must be unit length; it is not normalised
292
+ * here. `out` may be `a`.
293
+ */
294
+ export function vec3TransformQuaternion(out, a, q) {
295
+ const x = a[0];
296
+ const y = a[1];
297
+ const z = a[2];
298
+ const qx = q[0];
299
+ const qy = q[1];
300
+ const qz = q[2];
301
+ const qw = q[3];
302
+ const tx = 2 * (qy * z - qz * y);
303
+ const ty = 2 * (qz * x - qx * z);
304
+ const tz = 2 * (qx * y - qy * x);
305
+ out[0] = x + qw * tx + (qy * tz - qz * ty);
306
+ out[1] = y + qw * ty + (qz * tx - qx * tz);
307
+ out[2] = z + qw * tz + (qx * ty - qy * tx);
308
+ return out;
199
309
  }
200
- export function vec3Normalize(v) {
201
- const length = vec3Length(v);
202
- if (length === 0) {
203
- throw new Error("Cannot normalize a zero-length vector");
204
- }
205
- return vec3DivideScalar(v, length);
310
+ /**
311
+ * The binary exponent of the largest component of `v`; `0` when there is none
312
+ * to scale by (a zero vector, a non-finite component), which the slerp then
313
+ * carries to `NaN` as it always did.
314
+ */
315
+ function peakExponent_(v) {
316
+ const exponent = binaryExponent(Math.max(Math.abs(v[0]), Math.abs(v[1]), Math.abs(v[2])));
317
+ return Number.isFinite(exponent) ? exponent : 0;
206
318
  }
207
319
  /**
208
- * Computes a vector that is perpendicular to the given 3D vector. The result is
209
- * not unique; this function returns one of the possible perpendicular vectors.
210
- *
211
- * The returned vector is guaranteed to be perpendicular to the input vector,
212
- * but its direction is arbitrary. If the input vector is a zero vector, the
213
- * function will throw an error.
320
+ * Writes the interpolation of `a` and `b` along the great circle into `out`:
321
+ * the direction turns at constant angular speed along the shorter arc, and the
322
+ * length is interpolated linearly. The inputs need not be unit vectors. For `t`
323
+ * in `[0, 1]` and nearly parallel directions (about 1.8° apart or less) the
324
+ * directions are interpolated linearly and normalised, which traces the same
325
+ * arc. The arc lies in the plane of the two vectors, however close to opposite
326
+ * they are (`(1, 0, 0)` to `(−1, 0, 10⁻⁹)` passes through `+z`); for exactly
327
+ * opposite directions, where every great circle is as short, it turns about the
328
+ * axis of {@link vec3Perpendicular} of `a`. The length is interpolated all the
329
+ * same, so `t = 0.5` of opposite vectors has the mean length, never the origin
330
+ * that a straight line between them would give. `t` outside `[0, 1]`
331
+ * extrapolates along the same circle at the same angular speed. A zero vector
332
+ * has no direction: the result is `NaN`. The axis is the cross product of the
333
+ * vectors themselves, accurately rounded, so a pair that is a rounding away
334
+ * from opposite still turns in its own plane; lengths are taken at any
335
+ * magnitude, so vectors of `1e±200` slerp as well. `out` may be `a` or `b`.
214
336
  */
215
- export function vec3Perpendicular(v) {
216
- // Find a vector that is not parallel to v
217
- let other;
218
- if (Math.abs(v[0]) < Math.abs(v[1])) {
219
- other = [1, 0, 0];
337
+ export function vec3Slerp(out, a, b, t) {
338
+ const lengthA = vec3LengthScaled(a);
339
+ const lengthB = vec3LengthScaled(b);
340
+ const ax = a[0] / lengthA;
341
+ const ay = a[1] / lengthA;
342
+ const az = a[2] / lengthA;
343
+ const bx = b[0] / lengthB;
344
+ const by = b[1] / lengthB;
345
+ const bz = b[2] / lengthB;
346
+ const length = lerp(lengthA, lengthB, t);
347
+ const dot = ax * bx + ay * by + az * bz;
348
+ if (dot > SLERP_LINEAR_FALLBACK_DOT_ && t >= 0 && t <= 1) {
349
+ const x = lerp(ax, bx, t);
350
+ const y = lerp(ay, by, t);
351
+ const z = lerp(az, bz, t);
352
+ const scale = length / Math.sqrt(x * x + y * y + z * z);
353
+ out[0] = x * scale;
354
+ out[1] = y * scale;
355
+ out[2] = z * scale;
356
+ return out;
220
357
  }
221
- else {
222
- other = [0, 1, 0];
358
+ // The axis of the turn is `a × b`, formed from the vectors themselves (the
359
+ // unit directions of two exact antipodes of different lengths leave a
360
+ // rounding residue in theirs, and with it a spurious axis), each scaled by a
361
+ // power of two to between 1 and 2 so that the products neither overflow nor
362
+ // underflow. It is normalised after dividing by its largest component so that
363
+ // a tiny one (nearly opposite directions) keeps its digits.
364
+ const exponentA = peakExponent_(a);
365
+ const exponentB = peakExponent_(b);
366
+ const scaledAx = scaleByPowerOfTwo(a[0], -exponentA);
367
+ const scaledAy = scaleByPowerOfTwo(a[1], -exponentA);
368
+ const scaledAz = scaleByPowerOfTwo(a[2], -exponentA);
369
+ const scaledBx = scaleByPowerOfTwo(b[0], -exponentB);
370
+ const scaledBy = scaleByPowerOfTwo(b[1], -exponentB);
371
+ const scaledBz = scaleByPowerOfTwo(b[2], -exponentB);
372
+ const cx = diffOfProducts(scaledAy, scaledBz, scaledAz, scaledBy);
373
+ const cy = diffOfProducts(scaledAz, scaledBx, scaledAx, scaledBz);
374
+ const cz = diffOfProducts(scaledAx, scaledBy, scaledAy, scaledBx);
375
+ const peak = Math.max(Math.abs(cx), Math.abs(cy), Math.abs(cz));
376
+ // The unit vector, perpendicular to `a`, toward `b` in their plane.
377
+ let rx;
378
+ let ry;
379
+ let rz;
380
+ let sine = 0;
381
+ if (peak > 0) {
382
+ const ux = cx / peak;
383
+ const uy = cy / peak;
384
+ const uz = cz / peak;
385
+ const norm = Math.sqrt(ux * ux + uy * uy + uz * uz);
386
+ const nx = ux / norm;
387
+ const ny = uy / norm;
388
+ const nz = uz / norm;
389
+ rx = ny * az - nz * ay;
390
+ ry = nz * ax - nx * az;
391
+ rz = nx * ay - ny * ax;
392
+ // |a × b| over |a||b|, the lengths scaled as the vectors were.
393
+ sine =
394
+ (peak * norm) /
395
+ (scaleByPowerOfTwo(lengthA, -exponentA) *
396
+ scaleByPowerOfTwo(lengthB, -exponentB));
223
397
  }
224
- // Compute the cross product to get a perpendicular vector
225
- const perp = vec3Cross(v, other);
226
- // Normalize the perpendicular vector
227
- return vec3Normalize(perp);
228
- }
229
- export function vec3Project(v, onto) {
230
- const ontoLengthSq = vec3LengthSq(onto);
231
- if (ontoLengthSq === 0) {
232
- throw new Error("Cannot project onto a zero-length vector");
398
+ else {
399
+ // Parallel or opposite, exactly (or `NaN`, which `perpendicular_` carries
400
+ // through): any plane holds both.
401
+ perpendicular_(out, ax, ay, az);
402
+ const px = out[0];
403
+ const py = out[1];
404
+ const pz = out[2];
405
+ rx = py * az - pz * ay;
406
+ ry = pz * ax - px * az;
407
+ rz = px * ay - py * ax;
233
408
  }
234
- const scale = vec3Dot(v, onto) / ontoLengthSq;
235
- return vec3MultiplyScalar(onto, scale);
236
- }
237
- export function vec3ProjectOntoPlane(v, planeNormal) {
238
- return vec3Sub(v, vec3Project(v, planeNormal));
239
- }
240
- export function vec3Random(randFn) {
241
- const random = randFn ?? Math.random;
242
- const azimuth = random() * TWO_PI;
243
- const inclination = Math.acos(2 * random() - 1); // uniform surface distribution — not random() * π
244
- const sinI = Math.sin(inclination);
245
- return [
246
- sinI * Math.cos(azimuth),
247
- Math.cos(inclination),
248
- sinI * Math.sin(azimuth),
249
- ];
250
- }
251
- export function vec3Reflect(v, normal) {
252
- const dot = vec3Dot(v, normal);
253
- return vec3Sub(v, vec3MultiplyScalar(normal, 2 * dot));
254
- }
255
- export function vec3RotateAroundAxis(v, axis, angle) {
256
- const cosA = Math.cos(angle);
257
- const sinA = Math.sin(angle);
258
- const dot = vec3Dot(axis, v);
259
- const cross = vec3Cross(axis, v);
260
- const weights = [cosA, sinA, dot * (1 - cosA)];
261
- return [
262
- dotPrecise(weights, [v[0], cross[0], axis[0]]),
263
- dotPrecise(weights, [v[1], cross[1], axis[1]]),
264
- dotPrecise(weights, [v[2], cross[2], axis[2]]),
265
- ];
266
- }
267
- export function vec3Round(v, options) {
268
- return [round(v[0], options), round(v[1], options), round(v[2], options)];
269
- }
270
- export function vec3Sub(a, b) {
271
- return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
272
- }
273
- export function vec3SubScalar(v, s) {
274
- return [v[0] - s, v[1] - s, v[2] - s];
275
- }
276
- export function vec3SubScaled(a, b, s) {
277
- return [a[0] - b[0] * s, a[1] - b[1] * s, a[2] - b[2] * s];
409
+ const theta = Math.atan2(sine, dot) * t;
410
+ const cos = Math.cos(theta) * length;
411
+ const sin = Math.sin(theta) * length;
412
+ out[0] = ax * cos + rx * sin;
413
+ out[1] = ay * cos + ry * sin;
414
+ out[2] = az * cos + rz * sin;
415
+ return out;
278
416
  }
279
417
  /**
280
- * Spherical linear interpolation between two 3D unit vectors. Both `a` and `b`
281
- * should be unit-length for correct results. Falls back to normalized linear
282
- * interpolation when vectors are nearly parallel or anti-parallel.
418
+ * Writes a unit vector in a uniformly random direction (uniform on the sphere:
419
+ * `z` uniform in `[−1, 1]`, the azimuth uniform in `[0, 2π)`) into `out`.
420
+ *
421
+ * @param out The vector to fill.
422
+ * @param random A source of numbers in `[0, 1)`; defaults to `Math.random`.
283
423
  */
284
- export function vec3Slerp(a, b, t) {
285
- const dot = clamp(vec3Dot(a, b), -1, 1);
286
- if (dot > SLERP_LINEAR_FALLBACK_DOT_) {
287
- // Nearly parallel — use normalized lerp for numerical stability
288
- return vec3Normalize(vec3Lerp(a, b, t));
289
- }
290
- if (dot < -SLERP_LINEAR_FALLBACK_DOT_) {
291
- // Nearly anti-parallel — the great-circle arc is ambiguous; rotate `a` by
292
- // t·π around an arbitrary axis perpendicular to it
293
- return vec3RotateAroundAxis(a, vec3Normalize(vec3Perpendicular(a)), Math.PI * t);
294
- }
295
- const theta = Math.acos(dot) * t;
296
- const relB = vec3Normalize(vec3Sub(b, vec3MultiplyScalar(a, dot)));
297
- return vec3Add(vec3MultiplyScalar(a, Math.cos(theta)), vec3MultiplyScalar(relB, Math.sin(theta)));
298
- }
299
- export function vec3WeightedMean(items, getWeightedValue) {
300
- const x = new PreciseSum();
301
- const y = new PreciseSum();
302
- const z = new PreciseSum();
303
- const totalWeight = new PreciseSum();
304
- for (const item of items) {
305
- const [vec, w] = getWeightedValue(item);
306
- x.add(vec[0] * w);
307
- y.add(vec[1] * w);
308
- z.add(vec[2] * w);
309
- totalWeight.add(w);
310
- }
311
- const weight = totalWeight.value;
312
- return [x.value / weight, y.value / weight, z.value / weight];
424
+ export function vec3Random(out, random = Math.random) {
425
+ const azimuth = random() * TWO_PI;
426
+ const z = 2 * random() - 1;
427
+ const radius = Math.sqrt(1 - z * z);
428
+ out[0] = radius * Math.cos(azimuth);
429
+ out[1] = radius * Math.sin(azimuth);
430
+ out[2] = z;
431
+ return out;
313
432
  }
314
433
  //# sourceMappingURL=vec3.js.map