@ac-kit/math-linear 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/CHANGELOG.md +268 -0
  2. package/README.md +229 -16
  3. package/dist/_brand.d.ts +14 -0
  4. package/dist/_brand.d.ts.map +1 -0
  5. package/dist/_brand.js +2 -0
  6. package/dist/_brand.js.map +1 -0
  7. package/dist/_invert.d.ts +19 -0
  8. package/dist/_invert.d.ts.map +1 -0
  9. package/dist/_invert.js +48 -0
  10. package/dist/_invert.js.map +1 -0
  11. package/dist/_slerp.d.ts +15 -0
  12. package/dist/_slerp.d.ts.map +1 -1
  13. package/dist/_slerp.js +47 -0
  14. package/dist/_slerp.js.map +1 -1
  15. package/dist/_strided-range.d.ts +23 -0
  16. package/dist/_strided-range.d.ts.map +1 -0
  17. package/dist/_strided-range.js +75 -0
  18. package/dist/_strided-range.js.map +1 -0
  19. package/dist/_trs.d.ts +38 -0
  20. package/dist/_trs.d.ts.map +1 -0
  21. package/dist/_trs.js +132 -0
  22. package/dist/_trs.js.map +1 -0
  23. package/dist/affine2-array.d.ts +53 -0
  24. package/dist/affine2-array.d.ts.map +1 -0
  25. package/dist/affine2-array.js +107 -0
  26. package/dist/affine2-array.js.map +1 -0
  27. package/dist/affine2-componentwise.d.ts +28 -0
  28. package/dist/affine2-componentwise.d.ts.map +1 -0
  29. package/dist/affine2-componentwise.js +66 -0
  30. package/dist/affine2-componentwise.js.map +1 -0
  31. package/dist/affine2.d.ts +144 -0
  32. package/dist/affine2.d.ts.map +1 -0
  33. package/dist/affine2.js +375 -0
  34. package/dist/affine2.js.map +1 -0
  35. package/dist/affine3-array.d.ts +53 -0
  36. package/dist/affine3-array.d.ts.map +1 -0
  37. package/dist/affine3-array.js +137 -0
  38. package/dist/affine3-array.js.map +1 -0
  39. package/dist/affine3-componentwise.d.ts +32 -0
  40. package/dist/affine3-componentwise.d.ts.map +1 -0
  41. package/dist/affine3-componentwise.js +100 -0
  42. package/dist/affine3-componentwise.js.map +1 -0
  43. package/dist/affine3.d.ts +192 -0
  44. package/dist/affine3.d.ts.map +1 -0
  45. package/dist/affine3.js +718 -0
  46. package/dist/affine3.js.map +1 -0
  47. package/dist/checked/index.d.ts +35 -0
  48. package/dist/checked/index.d.ts.map +1 -0
  49. package/dist/checked/index.js +58 -0
  50. package/dist/checked/index.js.map +1 -0
  51. package/dist/euler.d.ts +65 -0
  52. package/dist/euler.d.ts.map +1 -0
  53. package/dist/euler.js +217 -0
  54. package/dist/euler.js.map +1 -0
  55. package/dist/index.d.ts +29 -0
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +29 -0
  58. package/dist/index.js.map +1 -1
  59. package/dist/kl-activations.d.ts.map +1 -1
  60. package/dist/kl-activations.js +4 -3
  61. package/dist/kl-activations.js.map +1 -1
  62. package/dist/mat2x2-array.d.ts +36 -0
  63. package/dist/mat2x2-array.d.ts.map +1 -0
  64. package/dist/mat2x2-array.js +71 -0
  65. package/dist/mat2x2-array.js.map +1 -0
  66. package/dist/mat2x2-componentwise.d.ts +86 -0
  67. package/dist/mat2x2-componentwise.d.ts.map +1 -0
  68. package/dist/mat2x2-componentwise.js +199 -0
  69. package/dist/mat2x2-componentwise.js.map +1 -0
  70. package/dist/mat2x2.d.ts +67 -25
  71. package/dist/mat2x2.d.ts.map +1 -1
  72. package/dist/mat2x2.js +150 -94
  73. package/dist/mat2x2.js.map +1 -1
  74. package/dist/mat3x3-array.d.ts +36 -0
  75. package/dist/mat3x3-array.d.ts.map +1 -0
  76. package/dist/mat3x3-array.js +91 -0
  77. package/dist/mat3x3-array.js.map +1 -0
  78. package/dist/mat3x3-componentwise.d.ts +86 -0
  79. package/dist/mat3x3-componentwise.d.ts.map +1 -0
  80. package/dist/mat3x3-componentwise.js +299 -0
  81. package/dist/mat3x3-componentwise.js.map +1 -0
  82. package/dist/mat3x3.d.ts +131 -48
  83. package/dist/mat3x3.d.ts.map +1 -1
  84. package/dist/mat3x3.js +434 -193
  85. package/dist/mat3x3.js.map +1 -1
  86. package/dist/mat4x4-array.d.ts +46 -0
  87. package/dist/mat4x4-array.d.ts.map +1 -0
  88. package/dist/mat4x4-array.js +133 -0
  89. package/dist/mat4x4-array.js.map +1 -0
  90. package/dist/mat4x4-componentwise.d.ts +87 -0
  91. package/dist/mat4x4-componentwise.d.ts.map +1 -0
  92. package/dist/mat4x4-componentwise.js +434 -0
  93. package/dist/mat4x4-componentwise.js.map +1 -0
  94. package/dist/mat4x4.d.ts +195 -67
  95. package/dist/mat4x4.d.ts.map +1 -1
  96. package/dist/mat4x4.js +849 -335
  97. package/dist/mat4x4.js.map +1 -1
  98. package/dist/projection.d.ts +98 -0
  99. package/dist/projection.d.ts.map +1 -0
  100. package/dist/projection.js +369 -0
  101. package/dist/projection.js.map +1 -0
  102. package/dist/quaternion-array.d.ts +47 -0
  103. package/dist/quaternion-array.d.ts.map +1 -0
  104. package/dist/quaternion-array.js +86 -0
  105. package/dist/quaternion-array.js.map +1 -0
  106. package/dist/quaternion.d.ts +104 -22
  107. package/dist/quaternion.d.ts.map +1 -1
  108. package/dist/quaternion.js +294 -181
  109. package/dist/quaternion.js.map +1 -1
  110. package/dist/transform.d.ts +220 -0
  111. package/dist/transform.d.ts.map +1 -0
  112. package/dist/transform.js +412 -0
  113. package/dist/transform.js.map +1 -0
  114. package/dist/tridiagonal.d.ts +52 -0
  115. package/dist/tridiagonal.d.ts.map +1 -0
  116. package/dist/tridiagonal.js +90 -0
  117. package/dist/tridiagonal.js.map +1 -0
  118. package/dist/upload.d.ts +55 -0
  119. package/dist/upload.d.ts.map +1 -0
  120. package/dist/upload.js +129 -0
  121. package/dist/upload.js.map +1 -0
  122. package/dist/vec2-array-componentwise.d.ts +189 -0
  123. package/dist/vec2-array-componentwise.d.ts.map +1 -0
  124. package/dist/vec2-array-componentwise.js +424 -0
  125. package/dist/vec2-array-componentwise.js.map +1 -0
  126. package/dist/vec2-array.d.ts +53 -0
  127. package/dist/vec2-array.d.ts.map +1 -0
  128. package/dist/vec2-array.js +123 -0
  129. package/dist/vec2-array.js.map +1 -0
  130. package/dist/vec2-componentwise.d.ts +311 -0
  131. package/dist/vec2-componentwise.d.ts.map +1 -0
  132. package/dist/vec2-componentwise.js +542 -0
  133. package/dist/vec2-componentwise.js.map +1 -0
  134. package/dist/vec2.d.ts +97 -82
  135. package/dist/vec2.d.ts.map +1 -1
  136. package/dist/vec2.js +121 -216
  137. package/dist/vec2.js.map +1 -1
  138. package/dist/vec3-array-componentwise.d.ts +189 -0
  139. package/dist/vec3-array-componentwise.d.ts.map +1 -0
  140. package/dist/vec3-array-componentwise.js +458 -0
  141. package/dist/vec3-array-componentwise.js.map +1 -0
  142. package/dist/vec3-array.d.ts +102 -0
  143. package/dist/vec3-array.d.ts.map +1 -0
  144. package/dist/vec3-array.js +299 -0
  145. package/dist/vec3-array.js.map +1 -0
  146. package/dist/vec3-componentwise.d.ts +310 -0
  147. package/dist/vec3-componentwise.d.ts.map +1 -0
  148. package/dist/vec3-componentwise.js +594 -0
  149. package/dist/vec3-componentwise.js.map +1 -0
  150. package/dist/vec3.d.ts +177 -87
  151. package/dist/vec3.d.ts.map +1 -1
  152. package/dist/vec3.js +366 -273
  153. package/dist/vec3.js.map +1 -1
  154. package/dist/vec4-array-componentwise.d.ts +155 -0
  155. package/dist/vec4-array-componentwise.d.ts.map +1 -0
  156. package/dist/vec4-array-componentwise.js +401 -0
  157. package/dist/vec4-array-componentwise.js.map +1 -0
  158. package/dist/vec4-array.d.ts +12 -0
  159. package/dist/vec4-array.d.ts.map +1 -0
  160. package/dist/vec4-array.js +43 -0
  161. package/dist/vec4-array.js.map +1 -0
  162. package/dist/vec4-componentwise.d.ts +299 -0
  163. package/dist/vec4-componentwise.d.ts.map +1 -0
  164. package/dist/vec4-componentwise.js +637 -0
  165. package/dist/vec4-componentwise.js.map +1 -0
  166. package/dist/vec4.d.ts +57 -53
  167. package/dist/vec4.d.ts.map +1 -1
  168. package/dist/vec4.js +94 -246
  169. package/dist/vec4.js.map +1 -1
  170. package/dist/view-basis.d.ts +15 -0
  171. package/dist/view-basis.d.ts.map +1 -0
  172. package/dist/view-basis.js +29 -0
  173. package/dist/view-basis.js.map +1 -0
  174. package/dist/viewport.d.ts +114 -0
  175. package/dist/viewport.d.ts.map +1 -0
  176. package/dist/viewport.js +250 -0
  177. package/dist/viewport.js.map +1 -0
  178. package/package.json +18 -9
package/dist/vec3.js CHANGED
@@ -1,314 +1,407 @@
1
- import { clamp, isCloseTo, PreciseSum, round, } from "@ac-kit/core";
2
- import { diffOfProducts, dotPrecise, lerp, TWO_PI } from "@ac-kit/math-scalar";
1
+ import { angleNormalizeRad, diffOfProducts, dotPrecise3, lerp, TWO_PI, } from "@ac-kit/math-scalar";
3
2
  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])];
3
+ import { vec3DotPrecise, vec3Length, vec3NormalizeScaled, } from "./vec3-componentwise.js";
4
+ /**
5
+ * Writes the cross product `a × b` into `out`, in plain arithmetic: a vector
6
+ * perpendicular to both, of length `|a||b| sin θ`, right-handed (`x × y = z`).
7
+ * Where a component's two terms cancel use {@link vec3CrossPrecise}. `out` may
8
+ * be `a` or `b`.
9
+ */
10
+ export function vec3Cross(out, a, b) {
11
+ const ax = a[0];
12
+ const ay = a[1];
13
+ const az = a[2];
14
+ const bx = b[0];
15
+ const by = b[1];
16
+ const bz = b[2];
17
+ out[0] = ay * bz - az * by;
18
+ out[1] = az * bx - ax * bz;
19
+ out[2] = ax * by - ay * bx;
20
+ return out;
8
21
  }
9
- export function vec3Add(a, b) {
10
- return [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
22
+ /**
23
+ * {@link vec3Cross} with the rounding of every product recovered
24
+ * (`@ac-kit/math-scalar`'s `diffOfProducts`): each component within 1.5 ULP of
25
+ * the exact value however badly its terms cancel. About five times the cost.
26
+ * `out` may be `a` or `b`.
27
+ */
28
+ export function vec3CrossPrecise(out, a, b) {
29
+ const ax = a[0];
30
+ const ay = a[1];
31
+ const az = a[2];
32
+ const bx = b[0];
33
+ const by = b[1];
34
+ const bz = b[2];
35
+ out[0] = diffOfProducts(ay, bz, az, by);
36
+ out[1] = diffOfProducts(az, bx, ax, bz);
37
+ out[2] = diffOfProducts(ax, by, ay, bx);
38
+ return out;
11
39
  }
12
- export function vec3AddScalar(v, s) {
13
- return [v[0] + s, v[1] + s, v[2] + s];
40
+ /**
41
+ * The sine of the angle between two vectors times their lengths, `|a × b|`,
42
+ * from the compensated cross product.
43
+ */
44
+ function crossLengthPrecise_(a, b) {
45
+ const x = diffOfProducts(a[1], b[2], a[2], b[1]);
46
+ const y = diffOfProducts(a[2], b[0], a[0], b[2]);
47
+ const z = diffOfProducts(a[0], b[1], a[1], b[0]);
48
+ return Math.sqrt(x * x + y * y + z * z);
14
49
  }
15
- export function vec3AddScaled(a, b, s) {
16
- return [a[0] + b[0] * s, a[1] + b[1] * s, a[2] + b[2] * s];
50
+ /**
51
+ * The signed angle from `a` to `b`, positive counter-clockwise when seen from
52
+ * the tip of `axis` (the right-hand rule about `axis`), in `(−π, π]`: `π` for
53
+ * opposite vectors. `axis` need not be a unit vector, and only its side of the
54
+ * plane of `a` and `b` matters. Computed from the compensated cross and dot
55
+ * products, so the angle of nearly parallel vectors keeps its digits. A zero
56
+ * vector gives `0`. Right while the product of the two lengths lies between
57
+ * about `1e−150` and `1e150`, which keeps the squares of the cross product in
58
+ * range (vectors of `1e100` give `Infinity` there and a wrong angle); an angle
59
+ * does not depend on the lengths, so beyond, normalise both vectors with
60
+ * {@link vec3NormalizeScaled} first.
61
+ */
62
+ export function vec3AngleTo(a, b, axis) {
63
+ const cx = diffOfProducts(a[1], b[2], a[2], b[1]);
64
+ const cy = diffOfProducts(a[2], b[0], a[0], b[2]);
65
+ const cz = diffOfProducts(a[0], b[1], a[1], b[0]);
66
+ const angle = Math.atan2(Math.sqrt(cx * cx + cy * cy + cz * cz), vec3DotPrecise(a, b) + 0);
67
+ return angleNormalizeRad(cx * axis[0] + cy * axis[1] + cz * axis[2] < 0 ? -angle : angle);
17
68
  }
18
69
  /**
19
- * Computes the angle in radians between two 3D vectors around a given axis. The
20
- * result is in the range (-π, π].
70
+ * The unsigned angle between two vectors, `atan2(|a × b|, a · b)`, in `[0, π]`,
71
+ * from the compensated cross and dot products. A zero vector gives `0`. Right
72
+ * while the product of the two lengths lies between about `1e−150` and `1e150`,
73
+ * which keeps the squares of the cross product in range (vectors of `1e100`
74
+ * give a wrong angle); an angle does not depend on the lengths, so beyond,
75
+ * normalise both vectors with {@link vec3NormalizeScaled} first.
21
76
  */
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;
77
+ export function vec3AngleBetween(a, b) {
78
+ return Math.atan2(crossLengthPrecise_(a, b), vec3DotPrecise(a, b) + 0);
30
79
  }
31
80
  /**
32
- * Computes the angle in radians between two 3D vectors. The result is in the
33
- * range [0, π].
81
+ * Writes `[a.x, a.y, z]` into `out`: the plane vector lifted to space.
34
82
  *
35
- * @throws Error if either vector is a zero-length vector.
83
+ * @param z The third component; defaults to `0`.
36
84
  */
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])];
85
+ export function vec3FromVec2(out, a, z = 0) {
86
+ out[0] = a[0];
87
+ out[1] = a[1];
88
+ out[2] = z;
89
+ return out;
49
90
  }
50
91
  /**
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).
92
+ * Writes `[x, y, z] / w` into `out`: the Cartesian point of a homogeneous
93
+ * vector, the perspective divide. A `w` of zero (a point at infinity) gives
94
+ * non-finite components, `±Infinity` or `NaN`, not an exception.
53
95
  */
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
- ];
76
- }
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
- ];
83
- }
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
88
- }
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);
113
- }
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];
96
+ export function vec3FromHomogeneous(out, a) {
97
+ const w = a[3];
98
+ out[0] = a[0] / w;
99
+ out[1] = a[1] / w;
100
+ out[2] = a[2] / w;
101
+ return out;
133
102
  }
134
103
  /**
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.
104
+ * Writes `a` rotated by `angle` radians about the unit vector `axis`
105
+ * (Rodrigues' formula), counter-clockwise seen from the tip of `axis`, into
106
+ * `out`. `axis` is not normalised here: a vector that is not unit scales the
107
+ * result. `out` may be `a` or `axis`.
141
108
  */
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));
109
+ export function vec3RotateAxisAngle(out, a, axis, angle) {
110
+ const x = a[0];
111
+ const y = a[1];
112
+ const z = a[2];
113
+ const kx = axis[0];
114
+ const ky = axis[1];
115
+ const kz = axis[2];
116
+ const cos = Math.cos(angle);
117
+ const sin = Math.sin(angle);
118
+ const along = (kx * x + ky * y + kz * z) * (1 - cos);
119
+ out[0] = x * cos + (ky * z - kz * y) * sin + kx * along;
120
+ out[1] = y * cos + (kz * x - kx * z) * sin + ky * along;
121
+ out[2] = z * cos + (kx * y - ky * x) * sin + kz * along;
122
+ return out;
146
123
  }
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]));
124
+ /**
125
+ * Writes into `out` a unit vector perpendicular to the scalar components `[x,
126
+ * y, z]`: their cross product with the axis of the smallest component, which is
127
+ * as far from parallel as an axis can be, normalised at any magnitude.
128
+ */
129
+ function perpendicular_(out, x, y, z) {
130
+ const absoluteX = Math.abs(x);
131
+ const absoluteY = Math.abs(y);
132
+ const absoluteZ = Math.abs(z);
133
+ if (absoluteX <= absoluteY && absoluteX <= absoluteZ) {
134
+ out[0] = 0;
135
+ out[1] = z;
136
+ out[2] = -y;
158
137
  }
159
- }
160
- export function vec3LengthSq(v) {
161
- return vec3Dot(v, v);
162
- }
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)];
165
- }
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])];
168
- }
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])];
171
- }
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];
138
+ else if (absoluteY <= absoluteZ) {
139
+ out[0] = -z;
140
+ out[1] = 0;
141
+ out[2] = x;
188
142
  }
189
- return { min: [minX, minY, minZ], max: [maxX, maxY, maxZ] };
190
- }
191
- export function vec3Multiply(a, b) {
192
- return [a[0] * b[0], a[1] * b[1], a[2] * b[2]];
193
- }
194
- export function vec3MultiplyScalar(v, s) {
195
- return [v[0] * s, v[1] * s, v[2] * s];
196
- }
197
- export function vec3Negate(v) {
198
- return [-v[0], -v[1], -v[2]];
199
- }
200
- export function vec3Normalize(v) {
201
- const length = vec3Length(v);
202
- if (length === 0) {
203
- throw new Error("Cannot normalize a zero-length vector");
143
+ else {
144
+ out[0] = y;
145
+ out[1] = -x;
146
+ out[2] = 0;
204
147
  }
205
- return vec3DivideScalar(v, length);
148
+ return vec3NormalizeScaled(out, out);
206
149
  }
207
150
  /**
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.
151
+ * Writes a unit vector perpendicular to `a` into `out`: `a` crossed with the
152
+ * axis of its smallest component, normalised at any magnitude. The result is
153
+ * not unique; this one is stable, so a continuous input gives it a few jumps
154
+ * only. The zero vector stays zero. `out` may be `a`.
214
155
  */
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];
220
- }
221
- else {
222
- other = [0, 1, 0];
223
- }
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);
156
+ export function vec3Perpendicular(out, a) {
157
+ return perpendicular_(out, a[0], a[1], a[2]);
228
158
  }
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");
233
- }
234
- const scale = vec3Dot(v, onto) / ontoLengthSq;
235
- return vec3MultiplyScalar(onto, scale);
159
+ /**
160
+ * Writes the tangent and the bitangent that complete the unit vector `normal`
161
+ * to a right-handed orthonormal basis (`tangent × bitangent = normal`), by
162
+ * Duff, Burgess, Christensen, Hery, Kensler, Liani and Villemin's branch-light
163
+ * construction ("Building an Orthonormal Basis, Revisited", 2017): no
164
+ * normalisation and no singularity, whatever the direction of `normal`.
165
+ * `normal` is not normalised here. The outputs may be `normal`.
166
+ */
167
+ export function vec3OrthonormalBasis(outTangent, outBitangent, normal) {
168
+ const x = normal[0];
169
+ const y = normal[1];
170
+ const z = normal[2];
171
+ const sign = z < 0 ? -1 : 1;
172
+ const a = -1 / (sign + z);
173
+ const b = x * y * a;
174
+ outTangent[0] = 1 + sign * x * x * a;
175
+ outTangent[1] = sign * b;
176
+ outTangent[2] = -sign * x;
177
+ outBitangent[0] = b;
178
+ outBitangent[1] = sign + y * y * a;
179
+ outBitangent[2] = -y;
236
180
  }
237
- export function vec3ProjectOntoPlane(v, planeNormal) {
238
- return vec3Sub(v, vec3Project(v, planeNormal));
181
+ /**
182
+ * Writes `a` minus its component along `normal` into `out`: `a` flattened onto
183
+ * the plane through the origin whose normal is `normal`. `normal` need not be a
184
+ * unit vector; a zero `normal` gives `NaN`. Right when the largest component of
185
+ * `normal` lies between about `1e−150` and `1e150`, where its squared length
186
+ * stays in range: above about `1.3e154` the result is `a` unflattened, and
187
+ * below about `1.5e−154` it is not finite. Beyond, normalise `normal` with
188
+ * {@link vec3NormalizeScaled} first. `out` may be `a` or `normal`.
189
+ */
190
+ export function vec3ProjectOntoPlane(out, a, normal) {
191
+ const x = a[0];
192
+ const y = a[1];
193
+ const z = a[2];
194
+ const nx = normal[0];
195
+ const ny = normal[1];
196
+ const nz = normal[2];
197
+ const scale = (x * nx + y * ny + z * nz) / (nx * nx + ny * ny + nz * nz);
198
+ out[0] = x - nx * scale;
199
+ out[1] = y - ny * scale;
200
+ out[2] = z - nz * scale;
201
+ return out;
239
202
  }
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
- ];
203
+ /**
204
+ * Writes `m · a` into `out`, the matrix applied to the column vector. `out` may
205
+ * be `a`.
206
+ */
207
+ export function vec3TransformMat3x3(out, a, m) {
208
+ const x = a[0];
209
+ const y = a[1];
210
+ const z = a[2];
211
+ out[0] = m[0] * x + m[3] * y + m[6] * z;
212
+ out[1] = m[1] * x + m[4] * y + m[7] * z;
213
+ out[2] = m[2] * x + m[5] * y + m[8] * z;
214
+ return out;
250
215
  }
251
- export function vec3Reflect(v, normal) {
252
- const dot = vec3Dot(v, normal);
253
- return vec3Sub(v, vec3MultiplyScalar(normal, 2 * dot));
216
+ /**
217
+ * {@link vec3TransformMat3x3} with each component a compensated dot product, as
218
+ * accurate as one in doubled precision rounded once. `out` may be `a`.
219
+ */
220
+ export function vec3TransformMat3x3Precise(out, a, m) {
221
+ const x = a[0];
222
+ const y = a[1];
223
+ const z = a[2];
224
+ out[0] = dotPrecise3(m[0], x, m[3], y, m[6], z);
225
+ out[1] = dotPrecise3(m[1], x, m[4], y, m[7], z);
226
+ out[2] = dotPrecise3(m[2], x, m[5], y, m[8], z);
227
+ return out;
254
228
  }
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
- ];
229
+ /**
230
+ * Writes the point `a` (as `[x, y, z, 1]`) transformed by `m` into `out`,
231
+ * divided by the resulting `w`, as gl-matrix and three.js do: for an affine
232
+ * matrix `w` is exactly `1` and nothing changes, for a projection this is the
233
+ * perspective divide. A `w` of zero gives non-finite components, not an
234
+ * exception. `out` may be `a`.
235
+ */
236
+ export function vec3TransformMat4x4(out, a, m) {
237
+ const x = a[0];
238
+ const y = a[1];
239
+ const z = a[2];
240
+ const w = m[3] * x + m[7] * y + m[11] * z + m[15];
241
+ out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
242
+ out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
243
+ out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
244
+ return out;
266
245
  }
267
- export function vec3Round(v, options) {
268
- return [round(v[0], options), round(v[1], options), round(v[2], options)];
246
+ /**
247
+ * Writes the direction `a` transformed by the upper-left 3×3 part of `m`, with
248
+ * no translation and no divide, into `out`. `out` may be `a`.
249
+ */
250
+ export function vec3TransformDirectionMat4x4(out, a, m) {
251
+ const x = a[0];
252
+ const y = a[1];
253
+ const z = a[2];
254
+ out[0] = m[0] * x + m[4] * y + m[8] * z;
255
+ out[1] = m[1] * x + m[5] * y + m[9] * z;
256
+ out[2] = m[2] * x + m[6] * y + m[10] * z;
257
+ return out;
269
258
  }
270
- export function vec3Sub(a, b) {
271
- return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
259
+ /**
260
+ * Writes the point `a` transformed by the affine transform (the linear part
261
+ * applied, then the translation added) into `out`. `out` may be `a`.
262
+ */
263
+ export function vec3TransformAffine3(out, a, transform) {
264
+ const x = a[0];
265
+ const y = a[1];
266
+ const z = a[2];
267
+ out[0] =
268
+ transform[0] * x + transform[3] * y + transform[6] * z + transform[9];
269
+ out[1] =
270
+ transform[1] * x + transform[4] * y + transform[7] * z + transform[10];
271
+ out[2] =
272
+ transform[2] * x + transform[5] * y + transform[8] * z + transform[11];
273
+ return out;
272
274
  }
273
- export function vec3SubScalar(v, s) {
274
- return [v[0] - s, v[1] - s, v[2] - s];
275
+ /**
276
+ * Writes the direction `a` transformed by the linear part of the affine
277
+ * transform, with no translation, into `out`. `out` may be `a`.
278
+ */
279
+ export function vec3TransformDirectionAffine3(out, a, transform) {
280
+ const x = a[0];
281
+ const y = a[1];
282
+ const z = a[2];
283
+ out[0] = transform[0] * x + transform[3] * y + transform[6] * z;
284
+ out[1] = transform[1] * x + transform[4] * y + transform[7] * z;
285
+ out[2] = transform[2] * x + transform[5] * y + transform[8] * z;
286
+ return out;
275
287
  }
276
- export function vec3SubScaled(a, b, s) {
277
- return [a[0] - b[0] * s, a[1] - b[1] * s, a[2] - b[2] * s];
288
+ /**
289
+ * Writes `a` rotated by the unit quaternion `q` into `out`: `q · a · q⁻¹` in 18
290
+ * multiplications, no matrix. `q` must be unit length; it is not normalised
291
+ * here. `out` may be `a`.
292
+ */
293
+ export function vec3TransformQuaternion(out, a, q) {
294
+ const x = a[0];
295
+ const y = a[1];
296
+ const z = a[2];
297
+ const qx = q[0];
298
+ const qy = q[1];
299
+ const qz = q[2];
300
+ const qw = q[3];
301
+ const tx = 2 * (qy * z - qz * y);
302
+ const ty = 2 * (qz * x - qx * z);
303
+ const tz = 2 * (qx * y - qy * x);
304
+ out[0] = x + qw * tx + (qy * tz - qz * ty);
305
+ out[1] = y + qw * ty + (qz * tx - qx * tz);
306
+ out[2] = z + qw * tz + (qx * ty - qy * tx);
307
+ return out;
278
308
  }
279
309
  /**
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.
310
+ * Writes the interpolation of `a` and `b` along the great circle into `out`:
311
+ * the direction turns at constant angular speed along the shorter arc, and the
312
+ * length is interpolated linearly. The inputs need not be unit vectors. For `t`
313
+ * in `[0, 1]` and nearly parallel directions (about 1.8° apart or less) the
314
+ * directions are interpolated linearly and normalised, which traces the same
315
+ * arc. The arc lies in the plane of the two vectors, however close to opposite
316
+ * they are (`(1, 0, 0)` to `(−1, 0, 10⁻⁹)` passes through `+z`); for exactly
317
+ * opposite directions, where every great circle is as short, it turns about the
318
+ * axis of {@link vec3Perpendicular} of `a`. The length is interpolated all the
319
+ * same, so `t = 0.5` of opposite vectors has the mean length, never the origin
320
+ * that a straight line between them would give. `t` outside `[0, 1]`
321
+ * extrapolates along the same circle at the same angular speed. A zero vector
322
+ * has no direction: the result is `NaN`. The lengths are `Math.sqrt` of sums of
323
+ * squares, so the result is right for lengths between about `1e−150` and
324
+ * `1e150`; beyond, normalise with {@link vec3NormalizeScaled} first. `out` may
325
+ * be `a` or `b`.
283
326
  */
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));
327
+ export function vec3Slerp(out, a, b, t) {
328
+ const lengthA = vec3Length(a);
329
+ const lengthB = vec3Length(b);
330
+ const ax = a[0] / lengthA;
331
+ const ay = a[1] / lengthA;
332
+ const az = a[2] / lengthA;
333
+ const bx = b[0] / lengthB;
334
+ const by = b[1] / lengthB;
335
+ const bz = b[2] / lengthB;
336
+ const length = lerp(lengthA, lengthB, t);
337
+ const dot = ax * bx + ay * by + az * bz;
338
+ if (dot > SLERP_LINEAR_FALLBACK_DOT_ && t >= 0 && t <= 1) {
339
+ const x = lerp(ax, bx, t);
340
+ const y = lerp(ay, by, t);
341
+ const z = lerp(az, bz, t);
342
+ const scale = length / Math.sqrt(x * x + y * y + z * z);
343
+ out[0] = x * scale;
344
+ out[1] = y * scale;
345
+ out[2] = z * scale;
346
+ return out;
289
347
  }
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);
348
+ // The axis of the turn is `a × b`, normalised after dividing by its largest
349
+ // component so that a tiny one (nearly opposite directions) keeps its digits.
350
+ const cx = ay * bz - az * by;
351
+ const cy = az * bx - ax * bz;
352
+ const cz = ax * by - ay * bx;
353
+ const peak = Math.max(Math.abs(cx), Math.abs(cy), Math.abs(cz));
354
+ // The unit vector, perpendicular to `a`, toward `b` in their plane.
355
+ let rx;
356
+ let ry;
357
+ let rz;
358
+ let sine = 0;
359
+ if (peak > 0) {
360
+ const ux = cx / peak;
361
+ const uy = cy / peak;
362
+ const uz = cz / peak;
363
+ const norm = Math.sqrt(ux * ux + uy * uy + uz * uz);
364
+ const nx = ux / norm;
365
+ const ny = uy / norm;
366
+ const nz = uz / norm;
367
+ rx = ny * az - nz * ay;
368
+ ry = nz * ax - nx * az;
369
+ rz = nx * ay - ny * ax;
370
+ sine = peak * norm;
294
371
  }
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);
372
+ else {
373
+ // Parallel or opposite, exactly (or `NaN`, which `perpendicular_` carries
374
+ // through): any plane holds both.
375
+ perpendicular_(out, ax, ay, az);
376
+ const px = out[0];
377
+ const py = out[1];
378
+ const pz = out[2];
379
+ rx = py * az - pz * ay;
380
+ ry = pz * ax - px * az;
381
+ rz = px * ay - py * ax;
310
382
  }
311
- const weight = totalWeight.value;
312
- return [x.value / weight, y.value / weight, z.value / weight];
383
+ const theta = Math.atan2(sine, dot) * t;
384
+ const cos = Math.cos(theta) * length;
385
+ const sin = Math.sin(theta) * length;
386
+ out[0] = ax * cos + rx * sin;
387
+ out[1] = ay * cos + ry * sin;
388
+ out[2] = az * cos + rz * sin;
389
+ return out;
390
+ }
391
+ /**
392
+ * Writes a unit vector in a uniformly random direction (uniform on the sphere:
393
+ * `z` uniform in `[−1, 1]`, the azimuth uniform in `[0, 2π)`) into `out`.
394
+ *
395
+ * @param out The vector to fill.
396
+ * @param random A source of numbers in `[0, 1)`; defaults to `Math.random`.
397
+ */
398
+ export function vec3Random(out, random = Math.random) {
399
+ const azimuth = random() * TWO_PI;
400
+ const z = 2 * random() - 1;
401
+ const radius = Math.sqrt(1 - z * z);
402
+ out[0] = radius * Math.cos(azimuth);
403
+ out[1] = radius * Math.sin(azimuth);
404
+ out[2] = z;
405
+ return out;
313
406
  }
314
407
  //# sourceMappingURL=vec3.js.map