@margoengine/splines 0.0.0-stage → 1.0.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/dist/fit.js ADDED
@@ -0,0 +1,175 @@
1
+ import { Spline as e } from "./index.js";
2
+ //#region src/fit.ts
3
+ var t = (e, t, n, r) => [
4
+ e[t] - n[r],
5
+ e[t + 1] - n[r + 1],
6
+ e[t + 2] - n[r + 2]
7
+ ], n = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], r = (e) => {
8
+ let t = Math.hypot(e[0], e[1], e[2]);
9
+ return t > 0 ? [
10
+ e[0] / t,
11
+ e[1] / t,
12
+ e[2] / t
13
+ ] : [
14
+ 0,
15
+ 0,
16
+ 0
17
+ ];
18
+ }, i = (e, t, n) => {
19
+ let r = 1 - t, i = n === 0 ? [
20
+ r * r * r,
21
+ 3 * r * r * t,
22
+ 3 * r * t * t,
23
+ t * t * t
24
+ ] : n === 1 ? [
25
+ -3 * r * r,
26
+ 3 * r * r - 6 * r * t,
27
+ 6 * r * t - 3 * t * t,
28
+ 3 * t * t
29
+ ] : [
30
+ 6 * r,
31
+ -12 * r + 6 * t,
32
+ 6 * r - 12 * t,
33
+ 6 * t
34
+ ], a = [
35
+ 0,
36
+ 0,
37
+ 0
38
+ ];
39
+ for (let t = 0; t < 3; t++) a[t] = i[0] * e[t] + i[1] * e[3 + t] + i[2] * e[6 + t] + i[3] * e[9 + t];
40
+ return a;
41
+ };
42
+ function a(e, t, r, i, a, o) {
43
+ let s = r - t + 1, c = /* @__PURE__ */ new Float64Array(12), l = t * 3, u = r * 3;
44
+ for (let t = 0; t < 3; t++) c[t] = e[l + t], c[9 + t] = e[u + t];
45
+ let d = 0, f = 0, p = 0, m = 0, h = 0;
46
+ for (let r = 0; r < s; r++) {
47
+ let s = i[r], l = 1 - s, u = l * l * l, g = 3 * l * l * s, _ = 3 * l * s * s, v = s * s * s, y = [
48
+ a[0] * g,
49
+ a[1] * g,
50
+ a[2] * g
51
+ ], b = [
52
+ o[0] * _,
53
+ o[1] * _,
54
+ o[2] * _
55
+ ], x = (t + r) * 3;
56
+ d += n(y, y), f += n(y, b), p += n(b, b);
57
+ let S = [
58
+ 0,
59
+ 0,
60
+ 0
61
+ ];
62
+ for (let t = 0; t < 3; t++) S[t] = e[x + t] - (c[t] * (u + g) + c[9 + t] * (_ + v));
63
+ m += n(y, S), h += n(b, S);
64
+ }
65
+ let g = d * p - f * f, _ = Math.hypot(e[u] - e[l], e[u + 1] - e[l + 1], e[u + 2] - e[l + 2]), v = g === 0 ? 0 : (m * p - h * f) / g, y = g === 0 ? 0 : (d * h - f * m) / g;
66
+ (!(v > 1e-6 * _) || !(y > 1e-6 * _)) && (v = y = _ / 3);
67
+ for (let e = 0; e < 3; e++) c[3 + e] = c[e] + a[e] * v, c[6 + e] = c[9 + e] + o[e] * y;
68
+ return c;
69
+ }
70
+ function o(e, t, n, r, a) {
71
+ let o = 0, s = Math.floor((t + n) / 2);
72
+ for (let c = t + 1; c < n; c++) {
73
+ let n = i(r, a[c - t], 0), l = Math.hypot(n[0] - e[c * 3], n[1] - e[c * 3 + 1], n[2] - e[c * 3 + 2]);
74
+ l > o && (o = l, s = c);
75
+ }
76
+ return [o, s];
77
+ }
78
+ function s(e, t, r, a, o) {
79
+ for (let s = t + 1; s < r; s++) {
80
+ let r = o[s - t], c = i(a, r, 0), l = i(a, r, 1), u = i(a, r, 2), d = [
81
+ c[0] - e[s * 3],
82
+ c[1] - e[s * 3 + 1],
83
+ c[2] - e[s * 3 + 2]
84
+ ], f = n(d, l), p = n(l, l) + n(d, u);
85
+ Math.abs(p) > 1e-12 && (o[s - t] = Math.min(1, Math.max(0, r - f / p)));
86
+ }
87
+ }
88
+ function c(n, { tolerance: i, refinements: c = 4, divisions: l = 256 }) {
89
+ if (!(Number.isFinite(i) && i > 0)) throw RangeError("tolerance must be above 0");
90
+ if (!Number.isInteger(c) || c < 0 || c > 20) throw RangeError("refinements must be an integer from 0 to 20");
91
+ if (n.length < 2 || n.length > 1e7) throw RangeError("Give 2 to 10,000,000 points");
92
+ let u = [];
93
+ for (let e of n) {
94
+ if (e.length !== 3 || !(Number.isFinite(e[0]) && Number.isFinite(e[1]) && Number.isFinite(e[2]))) throw TypeError("Every point must be a finite 3D point");
95
+ let t = u.length;
96
+ (!t || Math.hypot(e[0] - u[t - 3], e[1] - u[t - 2], e[2] - u[t - 1]) > 1e-12) && u.push(e[0], e[1], e[2]);
97
+ }
98
+ let d = Float64Array.from(u), f = d.length / 3;
99
+ if (f < 2) throw RangeError("Need at least two distinct points");
100
+ let p = [...d.subarray(0, 3)], m = 0, h = (e, n) => {
101
+ let i = e + n, a = e + 2 * n;
102
+ if (a < 0 || a >= f) return r(t(d, i * 3, d, e * 3));
103
+ let o = Math.hypot(...t(d, i * 3, d, e * 3)), s = o + Math.hypot(...t(d, a * 3, d, i * 3)), c = -(o + s) / (o * s), l = s / (o * (s - o)), u = -o / (s * (s - o)), p = [
104
+ 0,
105
+ 0,
106
+ 0
107
+ ];
108
+ for (let t = 0; t < 3; t++) p[t] = c * d[e * 3 + t] + l * d[i * 3 + t] + u * d[a * 3 + t];
109
+ return r(p);
110
+ }, g = [[
111
+ 0,
112
+ f - 1,
113
+ h(0, 1),
114
+ h(f - 1, -1)
115
+ ]];
116
+ for (; g.length;) {
117
+ let [e, n, l, u] = g.pop(), f;
118
+ if (n - e === 1) {
119
+ let r = Math.hypot(...t(d, n * 3, d, e * 3)) / 3;
120
+ f = /* @__PURE__ */ new Float64Array(12);
121
+ for (let t = 0; t < 3; t++) f[t] = d[e * 3 + t], f[3 + t] = f[t] + l[t] * r, f[9 + t] = d[n * 3 + t], f[6 + t] = f[9 + t] + u[t] * r;
122
+ _(f, 0);
123
+ continue;
124
+ }
125
+ let p = n - e + 1, m = new Float64Array(p);
126
+ for (let n = 1; n < p; n++) m[n] = m[n - 1] + Math.hypot(...t(d, (e + n) * 3, d, (e + n - 1) * 3));
127
+ let h = m[p - 1];
128
+ for (let e = 1; e < p; e++) m[e] /= h;
129
+ f = a(d, e, n, m, l, u);
130
+ let [v, y] = o(d, e, n, f, m);
131
+ for (let t = 0; t < c && v > i; t++) s(d, e, n, f, m), f = a(d, e, n, m, l, u), [v, y] = o(d, e, n, f, m);
132
+ if (v <= i) {
133
+ _(f, v);
134
+ continue;
135
+ }
136
+ y = Math.min(n - 1, Math.max(e + 1, y));
137
+ let b = r(t(d, (y - 1) * 3, d, (y + 1) * 3));
138
+ g.push([
139
+ y,
140
+ n,
141
+ [
142
+ -b[0],
143
+ -b[1],
144
+ -b[2]
145
+ ],
146
+ u
147
+ ], [
148
+ e,
149
+ y,
150
+ l,
151
+ b
152
+ ]);
153
+ }
154
+ function _(e, t) {
155
+ for (let t = 3; t < 12; t++) p.push(e[t]);
156
+ t > m && (m = t);
157
+ }
158
+ let v = Float64Array.from(p), y = (p.length / 3 - 1) / 3, b = [];
159
+ for (let e = 0; e < v.length; e += 3) b.push([
160
+ v[e],
161
+ v[e + 1],
162
+ v[e + 2]
163
+ ]);
164
+ return {
165
+ points: v,
166
+ spline: new e(b, {
167
+ kind: "bezier",
168
+ divisions: l
169
+ }),
170
+ pieces: y,
171
+ maxError: m
172
+ };
173
+ }
174
+ //#endregion
175
+ export { c as fitSpline };
@@ -0,0 +1,48 @@
1
+ import type { Spline } from './index.js';
2
+ /** Number of cubic segments in a spline (the parameter range 0..1 is split evenly across them). */
3
+ export declare function splineSegmentCount(spline: Spline): number;
4
+ /** Number of knots a per-knot array needs: one per segment boundary, and a closed curve's last knot is its first. */
5
+ export declare function knotCount(spline: Spline): number;
6
+ /** Curve parameter at an arc-length position (the inverse of {@link distanceAt}). */
7
+ export declare function parameterAt(spline: Spline, distance: number): number;
8
+ /** An orthonormal frame on a curve: `binormal = tangent x normal`. */
9
+ export interface SplineFrame {
10
+ position: Float64Array;
11
+ tangent: Float64Array;
12
+ normal: Float64Array;
13
+ binormal: Float64Array;
14
+ }
15
+ /** Roll in radians: one value per knot (see {@link knotCount}), blended linearly, or a function of the parameter. */
16
+ export type Roll = ArrayLike<number> | ((t: number) => number);
17
+ /** Options for {@link SplineFrames}. */
18
+ export interface SplineFramesOptions {
19
+ /** Intervals the curve is walked in, an integer 1..1,000,000. Default 256. */
20
+ samples?: number;
21
+ /** Which way "up" points at the start. Default [0, 1, 0]; used when it is not parallel to the first tangent. */
22
+ up?: ArrayLike<number>;
23
+ /** Roll about the tangent, added on top of the minimal rotation. */
24
+ roll?: Roll;
25
+ /** Closed curves only: spread the twist the loop picks up over its length so the frame meets itself. Default true. */
26
+ closeLoop?: boolean;
27
+ }
28
+ /** A frame with fresh output arrays. */
29
+ export declare function newFrame(): SplineFrame;
30
+ /**
31
+ * Rotation-minimising frames along a spline, so objects and cameras travel along it without flipping. Frames are
32
+ * propagated along the curve by double reflection (Wang, Juttler, Zheng and Liu, 2008) from the first tangent and
33
+ * `up`, and `roll` adds a twist about the tangent on top. Build once, then read frames at any distance.
34
+ */
35
+ export declare class SplineFrames {
36
+ readonly spline: Spline;
37
+ readonly samples: number;
38
+ /** Reference normal at each of `samples + 1` evenly spaced arc positions, 3 numbers each. */
39
+ readonly normals: Float64Array;
40
+ private readonly roll?;
41
+ constructor(spline: Spline, { samples, up, roll, closeLoop }?: SplineFramesOptions);
42
+ /** Roll in radians at curve parameter `t`. */
43
+ rollAt(t: number): number;
44
+ /** Frame at an arc-length position (0 to the spline length), written into `out`. */
45
+ frameAt(distance: number, out?: SplineFrame): SplineFrame;
46
+ /** Frame at curve parameter `t` (0..1). */
47
+ frameAtParameter(t: number, out?: SplineFrame): SplineFrame;
48
+ }
package/dist/frames.js ADDED
@@ -0,0 +1,113 @@
1
+ import { distanceAt as e } from "./closest.js";
2
+ //#region src/frames.ts
3
+ function t(e) {
4
+ let t = e.points.length / 3;
5
+ return e.kind === "bezier" ? (t - 1) / 3 : e.closed ? t : t - 1;
6
+ }
7
+ function n(e) {
8
+ let n = t(e);
9
+ return e.closed ? n : n + 1;
10
+ }
11
+ function r(e, t) {
12
+ if (!Number.isFinite(t) || t < 0 || t > e.length) throw RangeError("Distance must be from 0 to the spline length");
13
+ let n = e.arcLengths;
14
+ if (!e.length) return 0;
15
+ let r = 0, i = n.length - 1;
16
+ for (; r + 1 < i;) {
17
+ let e = r + i >>> 1;
18
+ n[e] < t ? r = e : i = e;
19
+ }
20
+ let a = n[i] - n[r];
21
+ return (r + (a ? (t - n[r]) / a : 0)) / (n.length - 1);
22
+ }
23
+ var i = (e, t, n, r) => e[t] * n[r] + e[t + 1] * n[r + 1] + e[t + 2] * n[r + 2], a = (e, t, n, r, a, o, s) => {
24
+ let c = Math.cos(a), l = Math.sin(a), u = i(n, r, e, t) * (1 - c), d = n[r + 1] * e[t + 2] - n[r + 2] * e[t + 1], f = n[r + 2] * e[t] - n[r] * e[t + 2], p = n[r] * e[t + 1] - n[r + 1] * e[t];
25
+ o[s] = e[t] * c + d * l + n[r] * u, o[s + 1] = e[t + 1] * c + f * l + n[r + 1] * u, o[s + 2] = e[t + 2] * c + p * l + n[r + 2] * u;
26
+ };
27
+ function o() {
28
+ return {
29
+ position: /* @__PURE__ */ new Float64Array(3),
30
+ tangent: /* @__PURE__ */ new Float64Array(3),
31
+ normal: /* @__PURE__ */ new Float64Array(3),
32
+ binormal: /* @__PURE__ */ new Float64Array(3)
33
+ };
34
+ }
35
+ var s = class {
36
+ constructor(e, { samples: t = 256, up: o = [
37
+ 0,
38
+ 1,
39
+ 0
40
+ ], roll: s, closeLoop: c = !0 } = {}) {
41
+ if (!Number.isInteger(t) || t < 1 || t > 1e6) throw RangeError("samples must be an integer from 1 to 1,000,000");
42
+ if (o.length !== 3 || !(Number.isFinite(o[0]) && Number.isFinite(o[1]) && Number.isFinite(o[2]))) throw TypeError("up must be a finite 3D vector");
43
+ if (s && typeof s != "function") {
44
+ let t = n(e);
45
+ if (s.length !== t) throw RangeError(`roll needs ${t} values, one per knot`);
46
+ for (let e = 0; e < s.length; e++) if (!Number.isFinite(s[e])) throw RangeError("roll must be finite");
47
+ }
48
+ this.spline = e, this.samples = t, this.roll = s;
49
+ let l = t + 1, u = new Float64Array(l * 3), d = new Float64Array(l * 3), f = new Float64Array(l * 3), p = /* @__PURE__ */ new Float64Array(3), m = e.length;
50
+ for (let n = 0; n < l; n++) {
51
+ let i = r(e, n / t * m);
52
+ e.at(i, p), u.set(p, n * 3), e.tangent(i, p), d.set(p, n * 3);
53
+ }
54
+ let h = o[0], g = o[1], _ = o[2], v = h * d[0] + g * d[1] + _ * d[2];
55
+ h -= v * d[0], g -= v * d[1], _ -= v * d[2];
56
+ let y = Math.hypot(h, g, _);
57
+ if (y < 1e-6) {
58
+ let e = Math.abs(d[0]), t = Math.abs(d[1]), n = Math.abs(d[2]);
59
+ h = +(e <= t && e <= n), g = +(h === 0 && t <= n), _ = +(h === 0 && g === 0);
60
+ let r = h * d[0] + g * d[1] + _ * d[2];
61
+ h -= r * d[0], g -= r * d[1], _ -= r * d[2], y = Math.hypot(h, g, _);
62
+ }
63
+ f[0] = h / y, f[1] = g / y, f[2] = _ / y;
64
+ for (let e = 0; e < t; e++) {
65
+ let t = e * 3, n = t + 3, r = u[n] - u[t], i = u[n + 1] - u[t + 1], a = u[n + 2] - u[t + 2], o = r * r + i * i + a * a, s = f[t], c = f[t + 1], l = f[t + 2];
66
+ if (o > 1e-24) {
67
+ let e = 2 / o, u = e * (r * s + i * c + a * l), f = e * (r * d[t] + i * d[t + 1] + a * d[t + 2]), p = s - u * r, m = c - u * i, h = l - u * a, g = d[n] - (d[t] - f * r), _ = d[n + 1] - (d[t + 1] - f * i), v = d[n + 2] - (d[t + 2] - f * a), y = g * g + _ * _ + v * v;
68
+ if (y > 1e-24) {
69
+ let e = 2 / y * (g * p + _ * m + v * h);
70
+ s = p - e * g, c = m - e * _, l = h - e * v;
71
+ } else s = p, c = m, l = h;
72
+ }
73
+ let p = s * d[n] + c * d[n + 1] + l * d[n + 2];
74
+ s -= p * d[n], c -= p * d[n + 1], l -= p * d[n + 2];
75
+ let m = Math.hypot(s, c, l) || 1;
76
+ f[n] = s / m, f[n + 1] = c / m, f[n + 2] = l / m;
77
+ }
78
+ if (e.closed && c && m > 0) {
79
+ let e = t * 3, n = f[e + 1] * f[2] - f[e + 2] * f[1], r = f[e + 2] * f[0] - f[e] * f[2], o = f[e] * f[1] - f[e + 1] * f[0], s = Math.atan2(n * d[0] + r * d[1] + o * d[2], i(f, e, f, 0));
80
+ for (let e = 1; e <= t; e++) a(f, e * 3, d, e * 3, s * e / t, f, e * 3);
81
+ f.set(f.subarray(0, 3), e);
82
+ }
83
+ this.normals = f;
84
+ }
85
+ rollAt(e) {
86
+ let n = this.roll;
87
+ if (!n) return 0;
88
+ if (typeof n == "function") return n(e);
89
+ let r = t(this.spline), i = e * r, a = Math.min(r - 1, Math.floor(i)), o = i - a;
90
+ return n[a] * (1 - o) + n[this.spline.closed ? (a + 1) % n.length : a + 1] * o;
91
+ }
92
+ frameAt(e, t = o()) {
93
+ let n = this.spline, i = r(n, e), s = n.length ? e / n.length * this.samples : 0, c = Math.min(this.samples - 1, Math.floor(s)), l = s - c, u = this.normals;
94
+ n.at(i, t.position), n.tangent(i, t.tangent);
95
+ let d = t.tangent, f = u[c * 3] * (1 - l) + u[c * 3 + 3] * l, p = u[c * 3 + 1] * (1 - l) + u[c * 3 + 4] * l, m = u[c * 3 + 2] * (1 - l) + u[c * 3 + 5] * l, h = f * d[0] + p * d[1] + m * d[2];
96
+ f -= h * d[0], p -= h * d[1], m -= h * d[2];
97
+ let g = Math.hypot(f, p, m);
98
+ if (g < 1e-9) {
99
+ let e = (l < .5 ? c : c + 1) * 3;
100
+ f = u[e], p = u[e + 1], m = u[e + 2];
101
+ } else f /= g, p /= g, m /= g;
102
+ t.normal[0] = f, t.normal[1] = p, t.normal[2] = m;
103
+ let _ = this.rollAt(i);
104
+ _ && a(t.normal, 0, d, 0, _, t.normal, 0);
105
+ let v = t.normal;
106
+ return t.binormal[0] = d[1] * v[2] - d[2] * v[1], t.binormal[1] = d[2] * v[0] - d[0] * v[2], t.binormal[2] = d[0] * v[1] - d[1] * v[0], t;
107
+ }
108
+ frameAtParameter(t, n) {
109
+ return this.frameAt(e(this.spline, t), n);
110
+ }
111
+ };
112
+ //#endregion
113
+ export { s as SplineFrames, n as knotCount, o as newFrame, r as parameterAt, t as splineSegmentCount };
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Segment basis: 'catmull-rom' passes through every point, 'bezier' uses 3n+1 control points, 'hermite' takes a
3
+ * tangent per point, 'kochanek-bartels' passes through every point with tension, continuity and bias controls, and
4
+ * 'bspline' is the uniform cubic B-spline (smooth, pulled toward but not through interior points; open curves are
5
+ * clamped to start and end at the first and last point).
6
+ */
7
+ export type CurveKind = 'catmull-rom' | 'bezier' | 'hermite' | 'kochanek-bartels' | 'bspline';
8
+ /** Options for `new Spline(...)`; invalid combinations throw a RangeError. */
9
+ export interface SplineOptions {
10
+ /** Default 'catmull-rom'. */
11
+ kind?: CurveKind;
12
+ /** Join the last point back to the first; not allowed for 'bezier'. Default false. */
13
+ closed?: boolean;
14
+ /** One finite 3D tangent per point; required for 'hermite'. */
15
+ tangents?: readonly ArrayLike<number>[];
16
+ /** 'kochanek-bartels' only: -1 (looser) to 1 (tighter) at each point. Default 0. */
17
+ tension?: number;
18
+ /** 'kochanek-bartels' only: -1 (rounded) to 1 (sharp corners) at each point. Default 0. */
19
+ continuity?: number;
20
+ /** 'kochanek-bartels' only: -1 (undershoot) to 1 (overshoot) at each point. Default 0. */
21
+ bias?: number;
22
+ /** Samples of the arc-length lookup, an integer 2..1,000,000. Default 256. */
23
+ divisions?: number;
24
+ }
25
+ /** Piecewise cubic curves with a cached arc-length lookup and caller-owned outputs. */
26
+ export declare class Spline {
27
+ readonly points: Float64Array;
28
+ readonly arcLengths: Float64Array;
29
+ readonly kind: CurveKind;
30
+ readonly closed: boolean;
31
+ /** Kochanek-Bartels settings, as given (0 when unused). */
32
+ readonly tension: number;
33
+ readonly continuity: number;
34
+ readonly bias: number;
35
+ private readonly tangents?;
36
+ private readonly output;
37
+ private readonly previous;
38
+ private readonly next;
39
+ private readonly count;
40
+ constructor(points: readonly ArrayLike<number>[], { kind, closed, tangents, divisions, tension, continuity, bias, }?: SplineOptions);
41
+ at(t: number, output?: Float64Array | Float32Array): Float64Array | Float32Array;
42
+ tangent(t: number, output?: Float64Array | Float32Array): Float64Array | Float32Array;
43
+ get length(): number;
44
+ updateArcLengths(): void;
45
+ atDistance(distance: number, output?: Float64Array | Float32Array): Float64Array | Float32Array;
46
+ }
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ //#region src/index.ts
2
+ var e = [
3
+ "catmull-rom",
4
+ "bezier",
5
+ "hermite",
6
+ "kochanek-bartels",
7
+ "bspline"
8
+ ], t = (e) => {
9
+ if (e.length !== 3) throw TypeError("Expected 3D point");
10
+ for (let t = 0; t < 3; t++) if (!Number.isFinite(e[t])) throw RangeError("Curve points must be finite");
11
+ }, n = class {
12
+ constructor(n, { kind: r = "catmull-rom", closed: i = !1, tangents: a, divisions: o = 256, tension: s = 0, continuity: c = 0, bias: l = 0 } = {}) {
13
+ if (this.output = /* @__PURE__ */ new Float64Array(3), this.previous = /* @__PURE__ */ new Float64Array(3), this.next = /* @__PURE__ */ new Float64Array(3), !e.includes(r) || ![
14
+ s,
15
+ c,
16
+ l
17
+ ].every((e) => Number.isFinite(e) && e >= -1 && e <= 1) || typeof i != "boolean" || !Number.isInteger(o) || o < 2 || o > 1e6 || n.length < 2 || n.length > 1e6) throw RangeError("Invalid spline options; give 2 to 1,000,000 finite points, 2 to 1,000,000 divisions and a boolean closed.");
18
+ if (r === "bezier" && (n.length < 4 || (n.length - 1) % 3 || i)) throw RangeError("Bezier needs 3n+1 points and an open curve");
19
+ if (r === "hermite" && (!a || a.length !== n.length)) throw RangeError("Hermite needs a tangent for each point");
20
+ this.kind = r, this.closed = i, this.tension = s, this.continuity = c, this.bias = l, this.count = n.length, this.points = new Float64Array(n.length * 3);
21
+ for (let e = 0; e < n.length; e++) t(n[e]), this.points.set(n[e], e * 3);
22
+ if (a) {
23
+ this.tangents = new Float64Array(a.length * 3);
24
+ for (let e = 0; e < a.length; e++) t(a[e]), this.tangents.set(a[e], e * 3);
25
+ }
26
+ this.arcLengths = new Float64Array(o + 1), this.updateArcLengths();
27
+ }
28
+ at(e, t = this.output) {
29
+ if (!Number.isFinite(e) || e < 0 || e > 1 || t.length !== 3) throw RangeError("Curve parameter must be 0..1 and output length 3");
30
+ let n = this.points, r = this.count, i = this.kind, a = this.closed, o = i === "bezier" ? (r - 1) / 3 : a ? r : r - 1, s = (a && e === 1 ? 0 : e) * o, c = Math.min(o - 1, Math.floor(s)), l = s - c, u = l * l, d = u * l;
31
+ if (i === "bezier") {
32
+ let e = c * 9, r = 1 - l, i = r * r * r, a = 3 * r * r * l, o = 3 * r * u;
33
+ for (let r = 0; r < 3; r++) t[r] = i * n[e + r] + a * n[e + 3 + r] + o * n[e + 6 + r] + d * n[e + 9 + r];
34
+ return t;
35
+ }
36
+ let f = 2 * d - 3 * u + 1, p = d - 2 * u + l, m = -2 * d + 3 * u, h = d - u, g = c * 3, _ = a ? (c + 1) % r * 3 : g + 3;
37
+ if (i === "hermite") {
38
+ let e = this.tangents;
39
+ for (let i = 0; i < 3; i++) t[i] = f * n[g + i] + p * e[g + i] + m * n[_ + i] + h * e[(c + 1) % r * 3 + i];
40
+ return t;
41
+ }
42
+ let v = a ? (c + r - 1) % r * 3 : c > 0 ? g - 3 : -1, y = a ? (c + 2) % r * 3 : c + 2 < r ? _ + 3 : -1, b = (r - 1) * 3;
43
+ if (i === "bspline") {
44
+ let e = 1 - l, r = e * e * e / 6, i = (3 * d - 6 * u + 4) / 6, a = (-3 * d + 3 * u + 3 * l + 1) / 6, o = d / 6;
45
+ for (let e = 0; e < 3; e++) {
46
+ let s = v < 0 ? 2 * n[e] - n[3 + e] : n[v + e], c = y < 0 ? 2 * n[b + e] - n[b - 3 + e] : n[y + e];
47
+ t[e] = r * s + i * n[g + e] + a * n[_ + e] + o * c;
48
+ }
49
+ return t;
50
+ }
51
+ let x = i === "kochanek-bartels", S = 1 - this.tension, C = x ? S * (1 + this.bias) * (1 + this.continuity) / 2 : .5, w = x ? S * (1 - this.bias) * (1 - this.continuity) / 2 : .5, T = x ? S * (1 + this.bias) * (1 - this.continuity) / 2 : .5, E = x ? S * (1 - this.bias) * (1 + this.continuity) / 2 : .5;
52
+ for (let e = 0; e < 3; e++) {
53
+ let r = n[g + e], i = n[_ + e], a = v < 0 ? 2 * n[e] - n[3 + e] : n[v + e], o = y < 0 ? 2 * n[b + e] - n[b - 3 + e] : n[y + e], s = x ? C * (r - a) + w * (i - r) : (i - a) * .5, c = x ? T * (i - r) + E * (o - i) : (o - r) * .5;
54
+ t[e] = f * r + p * s + m * i + h * c;
55
+ }
56
+ return t;
57
+ }
58
+ tangent(e, t = this.output) {
59
+ if (t.length !== 3 || !Number.isFinite(e) || e < 0 || e > 1) throw RangeError("Invalid tangent parameter; pass t from 0 to 1 and an output array of 3 elements.");
60
+ let n = this.closed && e === 1 ? 0 : e, r = n - 1e-5, i = n + 1e-5;
61
+ this.at(this.closed && r < 0 ? r + 1 : Math.max(0, r), this.previous), this.at(this.closed && i > 1 ? i - 1 : Math.min(1, i), this.next);
62
+ let a = this.next[0] - this.previous[0], o = this.next[1] - this.previous[1], s = this.next[2] - this.previous[2], c = Math.hypot(a, o, s) || 1;
63
+ return t[0] = a / c, t[1] = o / c, t[2] = s / c, t;
64
+ }
65
+ get length() {
66
+ return this.arcLengths[this.arcLengths.length - 1];
67
+ }
68
+ updateArcLengths() {
69
+ let e = this.arcLengths.length - 1;
70
+ this.at(0, this.previous), this.arcLengths[0] = 0;
71
+ for (let t = 1; t <= e; t++) this.at(t / e, this.next), this.arcLengths[t] = this.arcLengths[t - 1] + Math.hypot(this.next[0] - this.previous[0], this.next[1] - this.previous[1], this.next[2] - this.previous[2]), this.previous.set(this.next);
72
+ }
73
+ atDistance(e, t = this.output) {
74
+ if (!Number.isFinite(e) || e < 0 || e > this.length) throw RangeError("Distance must be from 0 to the spline length; pass a finite distance within the length.");
75
+ if (!this.length) return this.at(0, t);
76
+ let n = this.arcLengths, r = 0, i = n.length - 1;
77
+ for (; r + 1 < i;) {
78
+ let t = r + i >>> 1;
79
+ n[t] < e ? r = t : i = t;
80
+ }
81
+ let a = n[i] - n[r], o = a ? (e - n[r]) / a : 0;
82
+ return this.at((r + o) / (n.length - 1), t);
83
+ }
84
+ };
85
+ //#endregion
86
+ export { n as Spline };
@@ -0,0 +1,27 @@
1
+ import { Spline } from './index.js';
2
+ /** Options for a cubic Bezier spline; closure requires matching first and last control points. */
3
+ export interface CubicSplineOptions {
4
+ /** Periodic parameter and tangent handling. Default false. */
5
+ closed?: boolean;
6
+ /** Arc-length table divisions. Default 256. */
7
+ divisions?: number;
8
+ }
9
+ /** A Bezier-backed Spline with analytic tangents, including closed paths. */
10
+ export declare class CubicSpline extends Spline {
11
+ readonly closed: boolean;
12
+ constructor(points: readonly ArrayLike<number>[], options?: CubicSplineOptions);
13
+ tangent(t: number, output?: Float64Array | Float32Array): Float64Array | Float32Array;
14
+ }
15
+ /** Nonuniform Catmull-Rom settings. */
16
+ export interface CatmullRomOptions extends CubicSplineOptions {
17
+ /** Default centripetal (alpha=0.5); chordal uses alpha=1, uniform uses alpha=0. */
18
+ parameterization?: 'centripetal' | 'chordal' | 'uniform';
19
+ }
20
+ /** Interpolate knots using centripetal (default), chordal or uniform Catmull-Rom, converted exactly to Bezier. */
21
+ export declare function catmullRomSpline(points: readonly ArrayLike<number>[], options?: CatmullRomOptions): CubicSpline;
22
+ /** Exact first, second or third derivative with respect to global t; joins use the right-hand piece. */
23
+ export declare function splineDerivative(spline: Spline, t: number, order?: 1 | 2 | 3, output?: Float64Array<ArrayBuffer>): Float64Array;
24
+ /** Curvature |r' x r''| / |r'|^3; stationary points return NaN. */
25
+ export declare function splineCurvature(spline: Spline, t: number): number;
26
+ /** Torsion det(r',r'',r''') / |r' x r''|^2; undefined osculating planes return NaN. */
27
+ export declare function splineTorsion(spline: Spline, t: number): number;
package/dist/kinds.js ADDED
@@ -0,0 +1,76 @@
1
+ import { Spline as e } from "./index.js";
2
+ import { a as t, i as n, n as r, r as i, t as a } from "./polynomial-we3se7Ae.js";
3
+ //#region src/kinds.ts
4
+ var o = class extends e {
5
+ constructor(e, t = {}) {
6
+ if (super(e, {
7
+ kind: "bezier",
8
+ divisions: t.divisions
9
+ }), t.closed !== void 0 && typeof t.closed != "boolean") throw RangeError("Invalid closure; pass a boolean closed option.");
10
+ if (this.closed = t.closed ?? !1, this.closed && [
11
+ 0,
12
+ 1,
13
+ 2
14
+ ].some((e) => this.points[e] !== this.points[this.points.length - 3 + e])) throw RangeError("Closed cubic endpoints differ; repeat the first point at the end.");
15
+ }
16
+ tangent(e, t = /* @__PURE__ */ new Float64Array(3)) {
17
+ if (t.length !== 3) throw RangeError("Invalid output; pass an array of length three.");
18
+ let n = c(this, e), r = Math.hypot(...n);
19
+ for (let e = 0; e < 3; e++) t[e] = r ? n[e] / r : 0;
20
+ return t;
21
+ }
22
+ };
23
+ function s(e, t = {}) {
24
+ let n = t.parameterization ?? "centripetal";
25
+ if (![
26
+ "centripetal",
27
+ "chordal",
28
+ "uniform"
29
+ ].includes(n) || e.length < 2 || e.length > 1e6) throw RangeError("Invalid Catmull-Rom input; pass 2..1,000,000 points and a supported parameterization.");
30
+ e.forEach(a);
31
+ let r = n === "uniform" ? 0 : n === "chordal" ? 1 : .5, i = e.length, s = t.closed ? i : i - 1, c = [], l = (n) => {
32
+ if (t.closed) return e[(n + i) % i];
33
+ if (n >= 0 && n < i) return e[n];
34
+ let r = n < 0 ? 0 : i - 1, a = n < 0 ? 1 : i - 2;
35
+ return new Float64Array([
36
+ 0,
37
+ 1,
38
+ 2
39
+ ].map((t) => 2 * e[r][t] - e[a][t]));
40
+ };
41
+ for (let e = 0; e < s; e++) {
42
+ let [t, n, i, a] = [
43
+ e - 1,
44
+ e,
45
+ e + 1,
46
+ e + 2
47
+ ].map(l), o = (e, t) => r === 0 ? 1 : Math.hypot(e[0] - t[0], e[1] - t[1], e[2] - t[2]) ** r, s = o(n, i), u = o(t, n) || s || 1, d = o(i, a) || s || 1, f = /* @__PURE__ */ new Float64Array(3), p = /* @__PURE__ */ new Float64Array(3);
48
+ for (let e = 0; e < 3; e++) {
49
+ let r = s ? s * ((n[e] - t[e]) / u - (i[e] - t[e]) / (u + s) + (i[e] - n[e]) / s) : 0, o = s ? s * ((i[e] - n[e]) / s - (a[e] - n[e]) / (s + d) + (a[e] - i[e]) / d) : 0;
50
+ f[e] = n[e] + r / 3, p[e] = i[e] - o / 3;
51
+ }
52
+ e || c.push(Float64Array.from(n)), c.push(f, p, Float64Array.from(i));
53
+ }
54
+ return new o(c, t);
55
+ }
56
+ function c(e, i, a = 1, o = /* @__PURE__ */ new Float64Array(3)) {
57
+ if (!Number.isFinite(i) || i < 0 || i > 1 || ![
58
+ 1,
59
+ 2,
60
+ 3
61
+ ].includes(a) || o.length !== 3) throw RangeError("Invalid derivative; pass t in 0..1, order 1..3 and output length three.");
62
+ let s = t(e), c = (e.closed && i === 1 ? 0 : i) * s, l = Math.min(s - 1, Math.floor(c));
63
+ n(r(e, l), c - l, a, o);
64
+ for (let e = 0; e < 3; e++) o[e] *= s ** a;
65
+ return o;
66
+ }
67
+ function l(e, t) {
68
+ let n = c(e, t), r = c(e, t, 2), a = Math.hypot(...n);
69
+ return a ? Math.hypot(...i(n, r)) / a ** 3 : NaN;
70
+ }
71
+ function u(e, t) {
72
+ let n = i(c(e, t), c(e, t, 2)), r = c(e, t, 3), a = n[0] ** 2 + n[1] ** 2 + n[2] ** 2;
73
+ return a ? (n[0] * r[0] + n[1] * r[1] + n[2] * r[2]) / a : NaN;
74
+ }
75
+ //#endregion
76
+ export { o as CubicSpline, s as catmullRomSpline, l as splineCurvature, c as splineDerivative, u as splineTorsion };
@@ -0,0 +1,81 @@
1
+ import { Spline, type SplineOptions } from './index.js';
2
+ /**
3
+ * How a knot's two handles are set: 'auto' makes a smooth pass through the knot from its neighbors, 'linear' points
4
+ * both handles at the neighbors (a segment between two linear knots is a straight line), and 'broken' keeps
5
+ * `tangentIn` and `tangentOut` exactly as stored, so a corner can point any way.
6
+ */
7
+ export type TangentMode = 'auto' | 'linear' | 'broken';
8
+ type KnotVec = [number, number, number];
9
+ /** One editable control point of a {@link KnotPath}. */
10
+ export interface Knot {
11
+ position: KnotVec;
12
+ mode: TangentMode;
13
+ /** Handle offset from `position` toward the previous knot; read for 'broken' knots only. */
14
+ tangentIn: KnotVec;
15
+ /** Handle offset from `position` toward the next knot; read for 'broken' knots only. */
16
+ tangentOut: KnotVec;
17
+ /** Numbers attached to the knot (width, colour channel, speed...), blended linearly along each segment. */
18
+ data: number[];
19
+ }
20
+ /** A knot as passed in; everything but `position` is optional. */
21
+ export interface KnotInput {
22
+ position: ArrayLike<number>;
23
+ mode?: TangentMode;
24
+ tangentIn?: ArrayLike<number>;
25
+ tangentOut?: ArrayLike<number>;
26
+ data?: ArrayLike<number>;
27
+ }
28
+ /** Options for `new KnotPath(...)`. */
29
+ export interface KnotPathOptions {
30
+ /** Join the last knot back to the first (needs 3 or more knots). Default false. */
31
+ closed?: boolean;
32
+ }
33
+ /**
34
+ * An editable chain of knots that builds a cubic Bezier {@link Spline}: insert, remove and split knots, set how each
35
+ * knot's tangents behave, and attach numbers to each knot that you can read back along the curve. A closed path
36
+ * builds an open Bezier whose last point is its first, so `Spline.closed` stays false for it.
37
+ */
38
+ export declare class KnotPath {
39
+ readonly knots: Knot[];
40
+ readonly closed: boolean;
41
+ constructor(knots: readonly KnotInput[], { closed }?: KnotPathOptions);
42
+ private make;
43
+ private checkData;
44
+ /** A 'broken' knot given without a handle takes the handle 'auto' would make, once all knots are known. */
45
+ private freezeBroken;
46
+ get count(): number;
47
+ /** Number of segments between knots (one more than `count - 1` when closed). */
48
+ get segments(): number;
49
+ private neighbor;
50
+ /** The handle offsets a knot builds with, whatever its mode (for 'broken' knots, as stored). */
51
+ handles(index: number, ignoreStored?: boolean): {
52
+ tangentIn: KnotVec;
53
+ tangentOut: KnotVec;
54
+ };
55
+ /** Change a knot's mode; 'broken' freezes the handles it currently builds with unless new ones are given. */
56
+ setMode(index: number, mode: TangentMode, handles?: {
57
+ tangentIn?: ArrayLike<number>;
58
+ tangentOut?: ArrayLike<number>;
59
+ }): void;
60
+ /** Insert a knot before `index` (0..count; `count` appends). Returns the index it took. */
61
+ insert(index: number, input: KnotInput): number;
62
+ /**
63
+ * Split segment `segment` at local parameter `u` (0..1, in the Bezier parameter of that segment) and insert a knot
64
+ * on the curve there; its data is blended from the two ends. With `preserveShape` (default) the curve does not
65
+ * move: the new knot and its two neighbors become 'broken' with the handles de Casteljau gives them. Without it the
66
+ * new knot is 'auto' and neighbors keep their modes, so the curve reshapes around it.
67
+ */
68
+ insertAt(segment: number, u: number, preserveShape?: boolean): number;
69
+ /** Remove a knot. Its neighbors keep their modes; 'broken' handles stay as they are. */
70
+ remove(index: number): void;
71
+ /**
72
+ * Cut the path at a knot. An open path gives two open paths that share that knot (it needs an interior knot);
73
+ * a closed path gives one open path that starts and ends there. The shape of every segment is unchanged.
74
+ */
75
+ split(index: number): KnotPath[];
76
+ /** Blended data at curve parameter `t` (0..1) of the spline this path builds. */
77
+ dataAt(t: number, output?: number[]): number[];
78
+ /** Build the Bezier spline (3 control points per segment, plus the last knot). */
79
+ toSpline(options?: Omit<SplineOptions, 'kind' | 'closed' | 'tangents'>): Spline;
80
+ }
81
+ export {};