@ac-kit/math-linear 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +257 -0
- package/README.md +229 -16
- package/dist/_brand.d.ts +14 -0
- package/dist/_brand.d.ts.map +1 -0
- package/dist/_brand.js +2 -0
- package/dist/_brand.js.map +1 -0
- package/dist/_invert.d.ts +19 -0
- package/dist/_invert.d.ts.map +1 -0
- package/dist/_invert.js +48 -0
- package/dist/_invert.js.map +1 -0
- package/dist/_slerp.d.ts +15 -0
- package/dist/_slerp.d.ts.map +1 -1
- package/dist/_slerp.js +47 -0
- package/dist/_slerp.js.map +1 -1
- package/dist/_strided-range.d.ts +23 -0
- package/dist/_strided-range.d.ts.map +1 -0
- package/dist/_strided-range.js +75 -0
- package/dist/_strided-range.js.map +1 -0
- package/dist/_trs.d.ts +38 -0
- package/dist/_trs.d.ts.map +1 -0
- package/dist/_trs.js +132 -0
- package/dist/_trs.js.map +1 -0
- package/dist/affine2-array.d.ts +53 -0
- package/dist/affine2-array.d.ts.map +1 -0
- package/dist/affine2-array.js +107 -0
- package/dist/affine2-array.js.map +1 -0
- package/dist/affine2-componentwise.d.ts +28 -0
- package/dist/affine2-componentwise.d.ts.map +1 -0
- package/dist/affine2-componentwise.js +66 -0
- package/dist/affine2-componentwise.js.map +1 -0
- package/dist/affine2.d.ts +144 -0
- package/dist/affine2.d.ts.map +1 -0
- package/dist/affine2.js +375 -0
- package/dist/affine2.js.map +1 -0
- package/dist/affine3-array.d.ts +53 -0
- package/dist/affine3-array.d.ts.map +1 -0
- package/dist/affine3-array.js +137 -0
- package/dist/affine3-array.js.map +1 -0
- package/dist/affine3-componentwise.d.ts +32 -0
- package/dist/affine3-componentwise.d.ts.map +1 -0
- package/dist/affine3-componentwise.js +100 -0
- package/dist/affine3-componentwise.js.map +1 -0
- package/dist/affine3.d.ts +192 -0
- package/dist/affine3.d.ts.map +1 -0
- package/dist/affine3.js +718 -0
- package/dist/affine3.js.map +1 -0
- package/dist/checked/index.d.ts +35 -0
- package/dist/checked/index.d.ts.map +1 -0
- package/dist/checked/index.js +58 -0
- package/dist/checked/index.js.map +1 -0
- package/dist/euler.d.ts +65 -0
- package/dist/euler.d.ts.map +1 -0
- package/dist/euler.js +217 -0
- package/dist/euler.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -1
- package/dist/kl-activations.d.ts.map +1 -1
- package/dist/kl-activations.js +4 -3
- package/dist/kl-activations.js.map +1 -1
- package/dist/mat2x2-array.d.ts +36 -0
- package/dist/mat2x2-array.d.ts.map +1 -0
- package/dist/mat2x2-array.js +71 -0
- package/dist/mat2x2-array.js.map +1 -0
- package/dist/mat2x2-componentwise.d.ts +86 -0
- package/dist/mat2x2-componentwise.d.ts.map +1 -0
- package/dist/mat2x2-componentwise.js +199 -0
- package/dist/mat2x2-componentwise.js.map +1 -0
- package/dist/mat2x2.d.ts +67 -25
- package/dist/mat2x2.d.ts.map +1 -1
- package/dist/mat2x2.js +150 -94
- package/dist/mat2x2.js.map +1 -1
- package/dist/mat3x3-array.d.ts +36 -0
- package/dist/mat3x3-array.d.ts.map +1 -0
- package/dist/mat3x3-array.js +91 -0
- package/dist/mat3x3-array.js.map +1 -0
- package/dist/mat3x3-componentwise.d.ts +86 -0
- package/dist/mat3x3-componentwise.d.ts.map +1 -0
- package/dist/mat3x3-componentwise.js +299 -0
- package/dist/mat3x3-componentwise.js.map +1 -0
- package/dist/mat3x3.d.ts +131 -48
- package/dist/mat3x3.d.ts.map +1 -1
- package/dist/mat3x3.js +434 -193
- package/dist/mat3x3.js.map +1 -1
- package/dist/mat4x4-array.d.ts +46 -0
- package/dist/mat4x4-array.d.ts.map +1 -0
- package/dist/mat4x4-array.js +133 -0
- package/dist/mat4x4-array.js.map +1 -0
- package/dist/mat4x4-componentwise.d.ts +87 -0
- package/dist/mat4x4-componentwise.d.ts.map +1 -0
- package/dist/mat4x4-componentwise.js +434 -0
- package/dist/mat4x4-componentwise.js.map +1 -0
- package/dist/mat4x4.d.ts +195 -67
- package/dist/mat4x4.d.ts.map +1 -1
- package/dist/mat4x4.js +849 -335
- package/dist/mat4x4.js.map +1 -1
- package/dist/projection.d.ts +98 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +369 -0
- package/dist/projection.js.map +1 -0
- package/dist/quaternion-array.d.ts +47 -0
- package/dist/quaternion-array.d.ts.map +1 -0
- package/dist/quaternion-array.js +86 -0
- package/dist/quaternion-array.js.map +1 -0
- package/dist/quaternion.d.ts +104 -22
- package/dist/quaternion.d.ts.map +1 -1
- package/dist/quaternion.js +294 -181
- package/dist/quaternion.js.map +1 -1
- package/dist/transform.d.ts +220 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +412 -0
- package/dist/transform.js.map +1 -0
- package/dist/tridiagonal.d.ts +52 -0
- package/dist/tridiagonal.d.ts.map +1 -0
- package/dist/tridiagonal.js +90 -0
- package/dist/tridiagonal.js.map +1 -0
- package/dist/upload.d.ts +55 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +129 -0
- package/dist/upload.js.map +1 -0
- package/dist/vec2-array-componentwise.d.ts +189 -0
- package/dist/vec2-array-componentwise.d.ts.map +1 -0
- package/dist/vec2-array-componentwise.js +424 -0
- package/dist/vec2-array-componentwise.js.map +1 -0
- package/dist/vec2-array.d.ts +53 -0
- package/dist/vec2-array.d.ts.map +1 -0
- package/dist/vec2-array.js +123 -0
- package/dist/vec2-array.js.map +1 -0
- package/dist/vec2-componentwise.d.ts +311 -0
- package/dist/vec2-componentwise.d.ts.map +1 -0
- package/dist/vec2-componentwise.js +542 -0
- package/dist/vec2-componentwise.js.map +1 -0
- package/dist/vec2.d.ts +97 -82
- package/dist/vec2.d.ts.map +1 -1
- package/dist/vec2.js +121 -216
- package/dist/vec2.js.map +1 -1
- package/dist/vec3-array-componentwise.d.ts +189 -0
- package/dist/vec3-array-componentwise.d.ts.map +1 -0
- package/dist/vec3-array-componentwise.js +458 -0
- package/dist/vec3-array-componentwise.js.map +1 -0
- package/dist/vec3-array.d.ts +102 -0
- package/dist/vec3-array.d.ts.map +1 -0
- package/dist/vec3-array.js +299 -0
- package/dist/vec3-array.js.map +1 -0
- package/dist/vec3-componentwise.d.ts +310 -0
- package/dist/vec3-componentwise.d.ts.map +1 -0
- package/dist/vec3-componentwise.js +594 -0
- package/dist/vec3-componentwise.js.map +1 -0
- package/dist/vec3.d.ts +177 -87
- package/dist/vec3.d.ts.map +1 -1
- package/dist/vec3.js +366 -273
- package/dist/vec3.js.map +1 -1
- package/dist/vec4-array-componentwise.d.ts +155 -0
- package/dist/vec4-array-componentwise.d.ts.map +1 -0
- package/dist/vec4-array-componentwise.js +401 -0
- package/dist/vec4-array-componentwise.js.map +1 -0
- package/dist/vec4-array.d.ts +12 -0
- package/dist/vec4-array.d.ts.map +1 -0
- package/dist/vec4-array.js +43 -0
- package/dist/vec4-array.js.map +1 -0
- package/dist/vec4-componentwise.d.ts +299 -0
- package/dist/vec4-componentwise.d.ts.map +1 -0
- package/dist/vec4-componentwise.js +637 -0
- package/dist/vec4-componentwise.js.map +1 -0
- package/dist/vec4.d.ts +57 -53
- package/dist/vec4.d.ts.map +1 -1
- package/dist/vec4.js +94 -246
- package/dist/vec4.js.map +1 -1
- package/dist/view-basis.d.ts +15 -0
- package/dist/view-basis.d.ts.map +1 -0
- package/dist/view-basis.js +29 -0
- package/dist/view-basis.js.map +1 -0
- package/dist/viewport.d.ts +114 -0
- package/dist/viewport.d.ts.map +1 -0
- package/dist/viewport.js +250 -0
- package/dist/viewport.js.map +1 -0
- package/package.json +18 -9
package/dist/vec2.d.ts
CHANGED
|
@@ -1,103 +1,118 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
*
|
|
14
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
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
|
|
43
|
+
export declare function vec2CrossPrecise(a: ReadonlyVec2, b: ReadonlyVec2): number;
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
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
|
|
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
|
-
*
|
|
66
|
-
*
|
|
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
|
|
56
|
+
export declare function vec2AngleTo(a: ReadonlyVec2, b: ReadonlyVec2): number;
|
|
69
57
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
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
|
|
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
|
-
*
|
|
76
|
-
*
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
*
|
|
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
|
|
95
|
+
export declare function vec2TransformAffine2(out: Vec2, a: ReadonlyVec2, transform: ReadonlyAffine2): Vec2;
|
|
83
96
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
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
|
|
100
|
+
export declare function vec2TransformDirectionAffine2(out: Vec2, a: ReadonlyVec2, transform: ReadonlyAffine2): Vec2;
|
|
88
101
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
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
|
|
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
|
-
*
|
|
99
|
-
*
|
|
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
|
|
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
|
package/dist/vec2.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vec2.d.ts","sourceRoot":"","sources":["../src/vec2.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"vec2.d.ts","sourceRoot":"","sources":["../src/vec2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAS7C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAGlD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAE3D,wCAAwC;AACxC,MAAM,MAAM,YAAY,GAAG,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAE5E;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,MAAM,CAElE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,MAAM,CAEzE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,YAAY,GAAG,MAAM,CAEjD;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,MAAM,CAIpE;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,MAAM,CAEzE;AAED,kEAAkE;AAClE,wBAAgB,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAI5D;AAED;;;GAGG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAQ1E;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC,EAAE,YAAY,GAAG,IAAI,CAMlE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,IAAI,EACT,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,cAAc,GACf,IAAI,CAMN;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACzC,GAAG,EAAE,IAAI,EACT,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,cAAc,GACf,IAAI,CAMN;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,IAAI,EACT,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,cAAc,GACf,IAAI,CAMN;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,IAAI,EACT,CAAC,EAAE,YAAY,EACf,SAAS,EAAE,eAAe,GACxB,IAAI,CAMN;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,CAC5C,GAAG,EAAE,IAAI,EACT,CAAC,EAAE,YAAY,EACf,SAAS,EAAE,eAAe,GACxB,IAAI,CAMN;AAED;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CACxB,GAAG,EAAE,IAAI,EACT,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,YAAY,EACf,CAAC,EAAE,MAAM,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 {
|
|
2
|
-
import {
|
|
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
|
-
*
|
|
19
|
-
*
|
|
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
|
|
22
|
-
return
|
|
9
|
+
export function vec2Cross(a, b) {
|
|
10
|
+
return a[0] * b[1] - a[1] * b[0];
|
|
23
11
|
}
|
|
24
12
|
/**
|
|
25
|
-
*
|
|
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
|
|
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
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
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
|
|
96
|
-
return
|
|
25
|
+
export function vec2Angle(a) {
|
|
26
|
+
return angleNormalizeRad(Math.atan2(a[1], a[0]));
|
|
97
27
|
}
|
|
98
28
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
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
|
|
105
|
-
return
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
*
|
|
165
|
-
*
|
|
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
|
|
168
|
-
|
|
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
|
-
*
|
|
172
|
-
*
|
|
64
|
+
* Writes `a` rotated a quarter turn counter-clockwise, `[−y, x]`, into `out`.
|
|
65
|
+
* `out` may be `a`.
|
|
173
66
|
*/
|
|
174
|
-
export function
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
*
|
|
184
|
-
*
|
|
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
|
|
191
|
-
const
|
|
192
|
-
|
|
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
|
-
*
|
|
196
|
-
*
|
|
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
|
|
199
|
-
const
|
|
200
|
-
|
|
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
|
-
*
|
|
204
|
-
*
|
|
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
|
|
207
|
-
const
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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
|
-
|
|
224
|
-
|
|
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
|
-
*
|
|
228
|
-
* the
|
|
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 =
|
|
232
|
-
const angleB =
|
|
233
|
-
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|