@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/vec2.d.ts CHANGED
@@ -1,103 +1,118 @@
1
- import { type IsCloseToOptions, type RandomFn, type RoundOptions } from "@ac-kit/core";
2
- import { Mat2x2 } from "./mat2x2.js";
3
- import { Mat3x3 } from "./mat3x3.js";
4
- export type DistanceType = "euclidean" | "manhattan" | "chebyshev";
5
- export type Vec2 = [number, number];
6
- export declare const VEC2_ZERO: [0, 0];
7
- export declare const VEC2_ONE: [1, 1];
8
- export declare function vec2Abs(v: Vec2): Vec2;
9
- export declare function vec2Add(a: Vec2, b: Vec2): Vec2;
10
- export declare function vec2AddScalar(v: Vec2, s: number): Vec2;
11
- export declare function vec2AddScaled(a: Vec2, b: Vec2, s: number): Vec2;
1
+ import type { RandomFn } from "@ac-kit/core";
2
+ import type { Brand_ } from "./_brand.js";
3
+ import type { ReadonlyAffine2 } from "./affine2.js";
4
+ import type { ReadonlyMat2x2 } from "./mat2x2.js";
5
+ import type { ReadonlyMat3x3 } from "./mat3x3.js";
12
6
  /**
13
- * Computes the angle in radians of the vector from the positive x-axis, in the
14
- * range (-π, π].
7
+ * A point, a vector, a size or a direction of the plane, stored as `[x, y]`: a
8
+ * plain array of two binary64 numbers carrying a type-level brand, so an array
9
+ * literal, a `number[]`, a `Float64Array` or a value of another type with two
10
+ * numbers is rejected where a `Vec2` is expected. A buffer of them is a
11
+ * `Float64Array` of `count × 2` numbers, element `i` at `[2i, 2i + 2)`.
12
+ *
13
+ * Only {@link vec2Create}, {@link vec2Of}, {@link vec2Clone},
14
+ * {@link vec2FromArray} and the functions that write an `out` make one. They
15
+ * all start from an array of non-integer doubles, so every `Vec2` is
16
+ * `PACKED_DOUBLE_ELEMENTS` in V8 even when its components are whole numbers,
17
+ * and the hot loops that read it stay monomorphic. An array literal such as
18
+ * `[1, 2]` is an array of small integers instead, and storing one into a shared
19
+ * `out` turns it into a generic array for good.
20
+ *
21
+ * Rules for callers who bypass the types (JavaScript, casts, `any`): never
22
+ * store anything but a number in a vector, not even briefly; no holes, no
23
+ * `push`, no change of `length`; do not make one with `structuredClone` or
24
+ * `[...float64Array]` (use {@link vec2Clone}, {@link vec2Load},
25
+ * {@link vec2FromArray}); and never pass a `Float64Array` or a hand-made array
26
+ * where a `Vec2` is expected, even once, even in a test.
15
27
  */
16
- export declare function vec2Angle(v: Vec2): number;
28
+ export type Vec2 = [x: number, y: number] & Brand_<"Vec2">;
29
+ /** A {@link Vec2} that is only read. */
30
+ export type ReadonlyVec2 = readonly [x: number, y: number] & Brand_<"Vec2">;
17
31
  /**
18
- * Computes the angle in radians between two vectors. The result is in the range
19
- * (-π, π].
32
+ * The cross product of two plane vectors, `a.x·b.y − a.y·b.x`, in plain
33
+ * arithmetic: the signed area of the parallelogram they span, positive when `b`
34
+ * lies counter-clockwise of `a`. Where the two terms cancel (nearly parallel
35
+ * vectors) use {@link vec2CrossPrecise}.
20
36
  */
21
- export declare function vec2AngleTo(v1: Vec2, v2: Vec2): number;
22
- export declare function vec2ApplyMat2(v: Vec2, m: Mat2x2): Vec2;
23
- export declare function vec2ApplyMat3(v: Vec2, m: Mat3x3): Vec2;
24
- export declare function vec2Clamp(v: Vec2, min: Vec2, max: Vec2): Vec2;
25
- export declare function vec2ClampLength(v: Vec2, minLength: number, maxLength: number): Vec2;
26
- export declare function vec2ClampScalar(v: Vec2, min: number, max: number): Vec2;
27
- export declare function vec2Cross(a: Vec2, b: Vec2): number;
28
- export declare function vec2DistanceTo(a: Vec2, b: Vec2, type?: DistanceType): number;
29
- export declare function vec2DistanceToSq(a: Vec2, b: Vec2): number;
30
- export declare function vec2Divide(a: Vec2, b: Vec2): Vec2;
31
- export declare function vec2DivideScalar(v: Vec2, s: number): Vec2;
32
- export declare function vec2Dot(a: Vec2, b: Vec2): number;
33
- /** Exact component-wise equality. See {@link vec2IsClose} for a tolerant test. */
34
- export declare function vec2Equals(a: Vec2, b: Vec2): boolean;
37
+ export declare function vec2Cross(a: ReadonlyVec2, b: ReadonlyVec2): number;
35
38
  /**
36
- * Component-wise approximate equality.
37
- *
38
- * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
39
- * and never a basis for ordering — use {@link vec2Equals} there. `tolerance` is
40
- * required: a vector carries no units of its own, so only the caller knows what
41
- * counts as close.
39
+ * {@link vec2Cross} with the rounding of both products recovered
40
+ * (`@ac-kit/math-scalar`'s `diffOfProducts`): within 1.5 ULP of the exact value
41
+ * however badly the terms cancel. About five times the cost.
42
42
  */
43
- export declare function vec2IsClose(a: Vec2, b: Vec2, tolerance: IsCloseToOptions): boolean;
43
+ export declare function vec2CrossPrecise(a: ReadonlyVec2, b: ReadonlyVec2): number;
44
44
  /**
45
- * Creates a 2D vector from an angle in radians. The resulting vector will have
46
- * a length of 1 and point in the direction of the given angle.
47
- *
48
- * @param angle The angle in radians from the positive x-axis.
45
+ * The angle of `a` from the positive x axis, counter-clockwise, in `(−π, π]`:
46
+ * `Math.atan2(y, x)` with `−π` (a negative zero `y`, or a rounding to it)
47
+ * returned as `+π`. The zero vector gives `0`.
49
48
  */
50
- export declare function vec2FromAngle(angle: number): Vec2;
51
- export declare function vec2Length(v: Vec2, type?: DistanceType): number;
52
- export declare function vec2LengthSq(v: Vec2): number;
53
- export declare function vec2Lerp(a: Vec2, b: Vec2, t: number): Vec2;
54
- export declare function vec2Max(a: Vec2, b: Vec2): Vec2;
55
- export declare function vec2Min(a: Vec2, b: Vec2): Vec2;
56
- export declare function vec2Minmax(vs: readonly Vec2[]): {
57
- min: Vec2;
58
- max: Vec2;
59
- };
60
- export declare function vec2Multiply(a: Vec2, b: Vec2): Vec2;
61
- export declare function vec2MultiplyScalar(v: Vec2, s: number): Vec2;
62
- export declare function vec2Negate(v: Vec2): Vec2;
63
- export declare function vec2Normalize(v: Vec2): Vec2;
49
+ export declare function vec2Angle(a: ReadonlyVec2): number;
64
50
  /**
65
- * Computes a vector that is perpendicular to the given 2D vector. The result is
66
- * rotated 90 degrees counter-clockwise.
51
+ * The signed angle from `a` to `b`, positive counter-clockwise, in `(−π, π]`:
52
+ * `π` for opposite vectors. Computed from the compensated cross and dot
53
+ * products, so the angle of nearly parallel vectors keeps its digits. A zero
54
+ * vector gives `0`.
67
55
  */
68
- export declare function vec2Perpendicular(v: Vec2): Vec2;
56
+ export declare function vec2AngleTo(a: ReadonlyVec2, b: ReadonlyVec2): number;
69
57
  /**
70
- * Projects vector `a` onto vector `b`. The result is a vector that lies on the
71
- * line defined by `b` and has the same direction as `b`.
58
+ * The unsigned angle between two vectors, `atan2(|a × b|, a · b)`, in `[0, π]`,
59
+ * from the compensated cross and dot products. A zero vector gives `0`.
72
60
  */
73
- export declare function vec2Project(v: Vec2, onto: Vec2): Vec2;
61
+ export declare function vec2AngleBetween(a: ReadonlyVec2, b: ReadonlyVec2): number;
62
+ /** Writes the unit vector `[cos angle, sin angle]` into `out`. */
63
+ export declare function vec2FromAngle(out: Vec2, angle: number): Vec2;
74
64
  /**
75
- * Generates a random 2D unit vector (a vector of length 1) in a random
76
- * direction.
77
- *
78
- * @param randFn Optional random number generator function that returns a number
79
- * in the range [0, 1).
80
- * @returns A random 2D unit vector.
65
+ * Writes `a` rotated counter-clockwise by `angle` radians about the origin into
66
+ * `out`. Plain arithmetic. `out` may be `a`.
67
+ */
68
+ export declare function vec2Rotate(out: Vec2, a: ReadonlyVec2, angle: number): Vec2;
69
+ /**
70
+ * Writes `a` rotated a quarter turn counter-clockwise, `[−y, x]`, into `out`.
71
+ * `out` may be `a`.
72
+ */
73
+ export declare function vec2Perpendicular(out: Vec2, a: ReadonlyVec2): Vec2;
74
+ /**
75
+ * Writes `m · a` into `out`, the matrix applied to the column vector. `out` may
76
+ * be `a`.
77
+ */
78
+ export declare function vec2TransformMat2x2(out: Vec2, a: ReadonlyVec2, m: ReadonlyMat2x2): Vec2;
79
+ /**
80
+ * {@link vec2TransformMat2x2} with each component a compensated sum of products,
81
+ * within 1.5 ULP of the exact value. `out` may be `a`.
82
+ */
83
+ export declare function vec2TransformMat2x2Precise(out: Vec2, a: ReadonlyVec2, m: ReadonlyMat2x2): Vec2;
84
+ /**
85
+ * Writes the point `a` (as `[x, y, 1]`) transformed by `m` into `out`; the
86
+ * bottom row of `m` is ignored, as in gl-matrix and three.js, so a projective
87
+ * matrix is read as its affine part. `out` may be `a`.
88
+ */
89
+ export declare function vec2TransformMat3x3(out: Vec2, a: ReadonlyVec2, m: ReadonlyMat3x3): Vec2;
90
+ /**
91
+ * Writes the point `a` transformed by the affine transform into `out`: `[a·x +
92
+ * c·y + e, b·x + d·y + f]` for a transform `[a, b, c, d, e, f]`. `out` may be
93
+ * `a`.
81
94
  */
82
- export declare function vec2Random(randFn?: RandomFn | null): Vec2;
95
+ export declare function vec2TransformAffine2(out: Vec2, a: ReadonlyVec2, transform: ReadonlyAffine2): Vec2;
83
96
  /**
84
- * Reflects a 2D vector `v` across a given normal vector. The normal vector
85
- * should be normalized (have a length of 1) for correct results.
97
+ * Writes the direction `a` transformed by the linear part of the affine
98
+ * transform, with no translation, into `out`. `out` may be `a`.
86
99
  */
87
- export declare function vec2Reflect(v: Vec2, normal: Vec2): Vec2;
100
+ export declare function vec2TransformDirectionAffine2(out: Vec2, a: ReadonlyVec2, transform: ReadonlyAffine2): Vec2;
88
101
  /**
89
- * Rotates a 2D vector by a given angle in radians. The rotation is
90
- * counter-clockwise.
102
+ * Writes the interpolation of `a` and `b` along the arc of the circle into
103
+ * `out`: the angle and the length are interpolated, each linearly, with the
104
+ * angle turning the shorter way from `a` to `b`, counter-clockwise for opposite
105
+ * vectors. `t` outside `[0, 1]` extrapolates. A zero vector has the angle `0`.
106
+ * The lengths are `Math.sqrt` of sums of squares, so the result is right for
107
+ * lengths between about `1e−150` and `1e150`; beyond, normalise with
108
+ * {@link vec2NormalizeScaled} first. `out` may be `a` or `b`.
91
109
  */
92
- export declare function vec2Rotate(v: Vec2, angle: number): Vec2;
93
- export declare function vec2Round(v: Vec2, options?: RoundOptions): Vec2;
94
- export declare function vec2Sub(a: Vec2, b: Vec2): Vec2;
95
- export declare function vec2SubScalar(v: Vec2, s: number): Vec2;
96
- export declare function vec2SubScaled(a: Vec2, b: Vec2, s: number): Vec2;
110
+ export declare function vec2Slerp(out: Vec2, a: ReadonlyVec2, b: ReadonlyVec2, t: number): Vec2;
97
111
  /**
98
- * Spherical linear interpolation between two 2D unit vectors along the arc of
99
- * the unit circle. Both `a` and `b` should be unit-length for correct results.
112
+ * Writes a unit vector in a uniformly random direction into `out`.
113
+ *
114
+ * @param out The vector to fill.
115
+ * @param random A source of numbers in `[0, 1)`; defaults to `Math.random`.
100
116
  */
101
- export declare function vec2Slerp(a: Vec2, b: Vec2, t: number): Vec2;
102
- export declare function vec2WeightedMean<T>(items: readonly T[], getWeightedValue: (item: T) => [v: Vec2, w: number]): Vec2;
117
+ export declare function vec2Random(out: Vec2, random?: RandomFn): Vec2;
103
118
  //# sourceMappingURL=vec2.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"vec2.d.ts","sourceRoot":"","sources":["../src/vec2.ts"],"names":[],"mappings":"AAAA,OAAO,EAMN,KAAK,gBAAgB,EACrB,KAAK,QAAQ,EACb,KAAK,YAAY,EACjB,MAAM,cAAc,CAAC;AAStB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,MAAM,YAAY,GAAG,WAAW,GAAG,WAAW,GAAG,WAAW,CAAC;AAEnE,MAAM,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEpC,eAAO,MAAM,SAAS,QAAiC,CAAC;AAC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AM,GACP,IAAI,CAQN;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,GAAE,QAAsB,GAAG,IAAI,CAE1E"}
package/dist/vec2.js CHANGED
@@ -1,253 +1,158 @@
1
- import { clamp, isCloseTo, mod, PreciseSum, round, } from "@ac-kit/core";
2
- import { diffOfProducts, dotPrecise, lerp, sumOfProducts, TWO_PI, } from "@ac-kit/math-scalar";
3
- export const VEC2_ZERO = [0, 0];
4
- export const VEC2_ONE = [1, 1];
5
- export function vec2Abs(v) {
6
- return [Math.abs(v[0]), Math.abs(v[1])];
7
- }
8
- export function vec2Add(a, b) {
9
- return [a[0] + b[0], a[1] + b[1]];
10
- }
11
- export function vec2AddScalar(v, s) {
12
- return [v[0] + s, v[1] + s];
13
- }
14
- export function vec2AddScaled(a, b, s) {
15
- return [a[0] + b[0] * s, a[1] + b[1] * s];
16
- }
1
+ import { angleNormalizeRad, diffOfProducts, lerp, sumOfProducts, TWO_PI, } from "@ac-kit/math-scalar";
2
+ import { vec2DotPrecise, vec2Length } from "./vec2-componentwise.js";
17
3
  /**
18
- * Computes the angle in radians of the vector from the positive x-axis, in the
19
- * range (-π, π].
4
+ * The cross product of two plane vectors, `a.x·b.y − a.y·b.x`, in plain
5
+ * arithmetic: the signed area of the parallelogram they span, positive when `b`
6
+ * lies counter-clockwise of `a`. Where the two terms cancel (nearly parallel
7
+ * vectors) use {@link vec2CrossPrecise}.
20
8
  */
21
- export function vec2Angle(v) {
22
- return Math.atan2(v[1], v[0]);
9
+ export function vec2Cross(a, b) {
10
+ return a[0] * b[1] - a[1] * b[0];
23
11
  }
24
12
  /**
25
- * Computes the angle in radians between two vectors. The result is in the range
26
- * (-π, π].
13
+ * {@link vec2Cross} with the rounding of both products recovered
14
+ * (`@ac-kit/math-scalar`'s `diffOfProducts`): within 1.5 ULP of the exact value
15
+ * however badly the terms cancel. About five times the cost.
27
16
  */
28
- export function vec2AngleTo(v1, v2) {
29
- return Math.atan2(vec2Cross(v1, v2), vec2Dot(v1, v2));
30
- }
31
- export function vec2ApplyMat2(v, m) {
32
- return [
33
- sumOfProducts(v[0], m[0][0], v[1], m[0][1]),
34
- sumOfProducts(v[0], m[1][0], v[1], m[1][1]),
35
- ];
36
- }
37
- export function vec2ApplyMat3(v, m) {
38
- // The homogeneous 1 makes the translation column a third term of the dot.
39
- const homogeneous = [v[0], v[1], 1];
40
- return [dotPrecise(homogeneous, m[0]), dotPrecise(homogeneous, m[1])];
41
- }
42
- export function vec2Clamp(v, min, max) {
43
- return [clamp(v[0], min[0], max[0]), clamp(v[1], min[1], max[1])];
44
- }
45
- export function vec2ClampLength(v, minLength, maxLength) {
46
- const length = vec2Length(v);
47
- if (length === 0) {
48
- return v; // Can't scale a zero-length vector
49
- }
50
- const clampedLength = clamp(length, minLength, maxLength);
51
- return vec2MultiplyScalar(v, clampedLength / length);
52
- }
53
- export function vec2ClampScalar(v, min, max) {
54
- return [clamp(v[0], min, max), clamp(v[1], min, max)];
55
- }
56
- export function vec2Cross(a, b) {
17
+ export function vec2CrossPrecise(a, b) {
57
18
  return diffOfProducts(a[0], b[1], a[1], b[0]);
58
19
  }
59
- export function vec2DistanceTo(a, b, type = "euclidean") {
60
- switch (type) {
61
- case "euclidean":
62
- return Math.hypot(a[0] - b[0], a[1] - b[1]);
63
- case "manhattan":
64
- return Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
65
- case "chebyshev":
66
- return Math.max(Math.abs(a[0] - b[0]), Math.abs(a[1] - b[1]));
67
- }
68
- }
69
- export function vec2DistanceToSq(a, b) {
70
- const dx = a[0] - b[0];
71
- const dy = a[1] - b[1];
72
- return dx * dx + dy * dy;
73
- }
74
- export function vec2Divide(a, b) {
75
- return [a[0] / b[0], a[1] / b[1]];
76
- }
77
- export function vec2DivideScalar(v, s) {
78
- return [v[0] / s, v[1] / s];
79
- }
80
- export function vec2Dot(a, b) {
81
- return sumOfProducts(a[0], b[0], a[1], b[1]);
82
- }
83
- /** Exact component-wise equality. See {@link vec2IsClose} for a tolerant test. */
84
- export function vec2Equals(a, b) {
85
- return a[0] === b[0] && a[1] === b[1];
86
- }
87
20
  /**
88
- * Component-wise approximate equality.
89
- *
90
- * Not transitive, so it is not an equality in the sense a `Set` or `Map` needs,
91
- * and never a basis for ordering — use {@link vec2Equals} there. `tolerance` is
92
- * required: a vector carries no units of its own, so only the caller knows what
93
- * counts as close.
21
+ * The angle of `a` from the positive x axis, counter-clockwise, in `(−π, π]`:
22
+ * `Math.atan2(y, x)` with `−π` (a negative zero `y`, or a rounding to it)
23
+ * returned as `+π`. The zero vector gives `0`.
94
24
  */
95
- export function vec2IsClose(a, b, tolerance) {
96
- return isCloseTo(a[0], b[0], tolerance) && isCloseTo(a[1], b[1], tolerance);
25
+ export function vec2Angle(a) {
26
+ return angleNormalizeRad(Math.atan2(a[1], a[0]));
97
27
  }
98
28
  /**
99
- * Creates a 2D vector from an angle in radians. The resulting vector will have
100
- * a length of 1 and point in the direction of the given angle.
101
- *
102
- * @param angle The angle in radians from the positive x-axis.
29
+ * The signed angle from `a` to `b`, positive counter-clockwise, in `(−π, π]`:
30
+ * `π` for opposite vectors. Computed from the compensated cross and dot
31
+ * products, so the angle of nearly parallel vectors keeps its digits. A zero
32
+ * vector gives `0`.
103
33
  */
104
- export function vec2FromAngle(angle) {
105
- return [Math.cos(angle), Math.sin(angle)];
106
- }
107
- export function vec2Length(v, type = "euclidean") {
108
- switch (type) {
109
- case "euclidean":
110
- // Euclidean length is the square root of the sum of the squares of the components
111
- return Math.hypot(v[0], v[1]);
112
- case "manhattan":
113
- // Manhattan length is the sum of the absolute values of the components
114
- return Math.abs(v[0]) + Math.abs(v[1]);
115
- case "chebyshev":
116
- // Chebyshev length is the maximum of the absolute values of the components
117
- return Math.max(Math.abs(v[0]), Math.abs(v[1]));
118
- }
119
- }
120
- export function vec2LengthSq(v) {
121
- return vec2Dot(v, v);
34
+ export function vec2AngleTo(a, b) {
35
+ return angleNormalizeRad(Math.atan2(vec2CrossPrecise(a, b), vec2DotPrecise(a, b) + 0));
122
36
  }
123
- export function vec2Lerp(a, b, t) {
124
- return [lerp(a[0], b[0], t), lerp(a[1], b[1], t)];
125
- }
126
- export function vec2Max(a, b) {
127
- return [Math.max(a[0], b[0]), Math.max(a[1], b[1])];
128
- }
129
- export function vec2Min(a, b) {
130
- return [Math.min(a[0], b[0]), Math.min(a[1], b[1])];
131
- }
132
- export function vec2Minmax(vs) {
133
- let minX = Infinity, minY = Infinity;
134
- let maxX = -Infinity, maxY = -Infinity;
135
- for (const v of vs) {
136
- if (v[0] < minX)
137
- minX = v[0];
138
- if (v[1] < minY)
139
- minY = v[1];
140
- if (v[0] > maxX)
141
- maxX = v[0];
142
- if (v[1] > maxY)
143
- maxY = v[1];
144
- }
145
- return { min: [minX, minY], max: [maxX, maxY] };
146
- }
147
- export function vec2Multiply(a, b) {
148
- return [a[0] * b[0], a[1] * b[1]];
149
- }
150
- export function vec2MultiplyScalar(v, s) {
151
- return [v[0] * s, v[1] * s];
152
- }
153
- export function vec2Negate(v) {
154
- return [-v[0], -v[1]];
37
+ /**
38
+ * The unsigned angle between two vectors, `atan2(|a × b|, a · b)`, in `[0, π]`,
39
+ * from the compensated cross and dot products. A zero vector gives `0`.
40
+ */
41
+ export function vec2AngleBetween(a, b) {
42
+ return Math.atan2(Math.abs(vec2CrossPrecise(a, b)), vec2DotPrecise(a, b) + 0);
155
43
  }
156
- export function vec2Normalize(v) {
157
- const length = vec2Length(v);
158
- if (length === 0) {
159
- throw new Error("Cannot normalize a zero-length vector.");
160
- }
161
- return vec2DivideScalar(v, length);
44
+ /** Writes the unit vector `[cos angle, sin angle]` into `out`. */
45
+ export function vec2FromAngle(out, angle) {
46
+ out[0] = Math.cos(angle);
47
+ out[1] = Math.sin(angle);
48
+ return out;
162
49
  }
163
50
  /**
164
- * Computes a vector that is perpendicular to the given 2D vector. The result is
165
- * rotated 90 degrees counter-clockwise.
51
+ * Writes `a` rotated counter-clockwise by `angle` radians about the origin into
52
+ * `out`. Plain arithmetic. `out` may be `a`.
166
53
  */
167
- export function vec2Perpendicular(v) {
168
- return [-v[1], v[0]];
54
+ export function vec2Rotate(out, a, angle) {
55
+ const x = a[0];
56
+ const y = a[1];
57
+ const cos = Math.cos(angle);
58
+ const sin = Math.sin(angle);
59
+ out[0] = x * cos - y * sin;
60
+ out[1] = x * sin + y * cos;
61
+ return out;
169
62
  }
170
63
  /**
171
- * Projects vector `a` onto vector `b`. The result is a vector that lies on the
172
- * line defined by `b` and has the same direction as `b`.
64
+ * Writes `a` rotated a quarter turn counter-clockwise, `[−y, x]`, into `out`.
65
+ * `out` may be `a`.
173
66
  */
174
- export function vec2Project(v, onto) {
175
- const bLengthSq = vec2LengthSq(onto);
176
- if (bLengthSq === 0) {
177
- throw new Error("Cannot project onto a zero-length vector.");
178
- }
179
- const scale = vec2Dot(v, onto) / bLengthSq;
180
- return vec2MultiplyScalar(onto, scale);
67
+ export function vec2Perpendicular(out, a) {
68
+ const x = a[0];
69
+ const y = a[1];
70
+ out[0] = -y;
71
+ out[1] = x;
72
+ return out;
181
73
  }
182
74
  /**
183
- * Generates a random 2D unit vector (a vector of length 1) in a random
184
- * direction.
185
- *
186
- * @param randFn Optional random number generator function that returns a number
187
- * in the range [0, 1).
188
- * @returns A random 2D unit vector.
75
+ * Writes `m · a` into `out`, the matrix applied to the column vector. `out` may
76
+ * be `a`.
189
77
  */
190
- export function vec2Random(randFn) {
191
- const random = randFn ?? Math.random;
192
- return vec2FromAngle(random() * TWO_PI);
78
+ export function vec2TransformMat2x2(out, a, m) {
79
+ const x = a[0];
80
+ const y = a[1];
81
+ out[0] = m[0] * x + m[2] * y;
82
+ out[1] = m[1] * x + m[3] * y;
83
+ return out;
193
84
  }
194
85
  /**
195
- * Reflects a 2D vector `v` across a given normal vector. The normal vector
196
- * should be normalized (have a length of 1) for correct results.
86
+ * {@link vec2TransformMat2x2} with each component a compensated sum of products,
87
+ * within 1.5 ULP of the exact value. `out` may be `a`.
197
88
  */
198
- export function vec2Reflect(v, normal) {
199
- const dot = vec2Dot(v, normal);
200
- return vec2Sub(v, vec2MultiplyScalar(normal, 2 * dot));
89
+ export function vec2TransformMat2x2Precise(out, a, m) {
90
+ const x = a[0];
91
+ const y = a[1];
92
+ out[0] = sumOfProducts(m[0], x, m[2], y);
93
+ out[1] = sumOfProducts(m[1], x, m[3], y);
94
+ return out;
201
95
  }
202
96
  /**
203
- * Rotates a 2D vector by a given angle in radians. The rotation is
204
- * counter-clockwise.
97
+ * Writes the point `a` (as `[x, y, 1]`) transformed by `m` into `out`; the
98
+ * bottom row of `m` is ignored, as in gl-matrix and three.js, so a projective
99
+ * matrix is read as its affine part. `out` may be `a`.
205
100
  */
206
- export function vec2Rotate(v, angle) {
207
- const cos = Math.cos(angle);
208
- const sin = Math.sin(angle);
209
- return [
210
- diffOfProducts(v[0], cos, v[1], sin),
211
- sumOfProducts(v[0], sin, v[1], cos),
212
- ];
213
- }
214
- export function vec2Round(v, options) {
215
- return [round(v[0], options), round(v[1], options)];
101
+ export function vec2TransformMat3x3(out, a, m) {
102
+ const x = a[0];
103
+ const y = a[1];
104
+ out[0] = m[0] * x + m[3] * y + m[6];
105
+ out[1] = m[1] * x + m[4] * y + m[7];
106
+ return out;
216
107
  }
217
- export function vec2Sub(a, b) {
218
- return [a[0] - b[0], a[1] - b[1]];
219
- }
220
- export function vec2SubScalar(v, s) {
221
- return [v[0] - s, v[1] - s];
108
+ /**
109
+ * Writes the point `a` transformed by the affine transform into `out`: `[a·x +
110
+ * c·y + e, b·x + d·y + f]` for a transform `[a, b, c, d, e, f]`. `out` may be
111
+ * `a`.
112
+ */
113
+ export function vec2TransformAffine2(out, a, transform) {
114
+ const x = a[0];
115
+ const y = a[1];
116
+ out[0] = transform[0] * x + transform[2] * y + transform[4];
117
+ out[1] = transform[1] * x + transform[3] * y + transform[5];
118
+ return out;
222
119
  }
223
- export function vec2SubScaled(a, b, s) {
224
- return [a[0] - b[0] * s, a[1] - b[1] * s];
120
+ /**
121
+ * Writes the direction `a` transformed by the linear part of the affine
122
+ * transform, with no translation, into `out`. `out` may be `a`.
123
+ */
124
+ export function vec2TransformDirectionAffine2(out, a, transform) {
125
+ const x = a[0];
126
+ const y = a[1];
127
+ out[0] = transform[0] * x + transform[2] * y;
128
+ out[1] = transform[1] * x + transform[3] * y;
129
+ return out;
225
130
  }
226
131
  /**
227
- * Spherical linear interpolation between two 2D unit vectors along the arc of
228
- * the unit circle. Both `a` and `b` should be unit-length for correct results.
132
+ * Writes the interpolation of `a` and `b` along the arc of the circle into
133
+ * `out`: the angle and the length are interpolated, each linearly, with the
134
+ * angle turning the shorter way from `a` to `b`, counter-clockwise for opposite
135
+ * vectors. `t` outside `[0, 1]` extrapolates. A zero vector has the angle `0`.
136
+ * The lengths are `Math.sqrt` of sums of squares, so the result is right for
137
+ * lengths between about `1e−150` and `1e150`; beyond, normalise with
138
+ * {@link vec2NormalizeScaled} first. `out` may be `a` or `b`.
229
139
  */
230
- export function vec2Slerp(a, b, t) {
231
- const angleA = vec2Angle(a);
232
- const angleB = vec2Angle(b);
233
- // Normalize delta to (-π, π] (inlined angleNormalizeRad)
234
- const rawDelta = angleB - angleA;
235
- const delta = mod(rawDelta + Math.PI, TWO_PI) - Math.PI;
236
- const angle = angleA + delta * t;
237
- const r = lerp(vec2Length(a), vec2Length(b), t);
238
- return [r * Math.cos(angle), r * Math.sin(angle)];
140
+ export function vec2Slerp(out, a, b, t) {
141
+ const angleA = Math.atan2(a[1], a[0]);
142
+ const angleB = Math.atan2(b[1], b[0]);
143
+ const angle = angleA + angleNormalizeRad(angleB - angleA) * t;
144
+ const length = lerp(vec2Length(a), vec2Length(b), t);
145
+ out[0] = length * Math.cos(angle);
146
+ out[1] = length * Math.sin(angle);
147
+ return out;
239
148
  }
240
- export function vec2WeightedMean(items, getWeightedValue) {
241
- const x = new PreciseSum();
242
- const y = new PreciseSum();
243
- const totalWeight = new PreciseSum();
244
- for (const item of items) {
245
- const [vec, w] = getWeightedValue(item);
246
- x.add(vec[0] * w);
247
- y.add(vec[1] * w);
248
- totalWeight.add(w);
249
- }
250
- const weight = totalWeight.value;
251
- return [x.value / weight, y.value / weight];
149
+ /**
150
+ * Writes a unit vector in a uniformly random direction into `out`.
151
+ *
152
+ * @param out The vector to fill.
153
+ * @param random A source of numbers in `[0, 1)`; defaults to `Math.random`.
154
+ */
155
+ export function vec2Random(out, random = Math.random) {
156
+ return vec2FromAngle(out, random() * TWO_PI);
252
157
  }
253
158
  //# sourceMappingURL=vec2.js.map