curvy 0.1.1 → 0.3.2
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/axis.cjs +325 -0
- package/dist/axis.d.cts +20 -0
- package/dist/axis.d.mts +20 -0
- package/dist/axis.d.ts +20 -0
- package/dist/axis.mjs +314 -0
- package/dist/convert.cjs +74 -0
- package/dist/convert.d.cts +6 -0
- package/dist/convert.d.mts +6 -0
- package/dist/convert.d.ts +6 -0
- package/dist/convert.mjs +68 -0
- package/dist/curve.cjs +194 -0
- package/dist/curve.d.cts +18 -0
- package/dist/curve.d.mts +18 -0
- package/dist/curve.d.ts +18 -0
- package/dist/curve.mjs +184 -0
- package/dist/shared/curvy.262f6070.cjs +16 -0
- package/dist/shared/curvy.9cd8b44c.mjs +13 -0
- package/dist/shared/curvy.df73166c.d.cts +38 -0
- package/dist/shared/curvy.df73166c.d.mts +38 -0
- package/dist/shared/curvy.df73166c.d.ts +38 -0
- package/dist/splines.cjs +150 -0
- package/dist/splines.d.cts +27 -0
- package/dist/splines.d.mts +27 -0
- package/dist/splines.d.ts +27 -0
- package/dist/splines.mjs +138 -0
- package/package.json +24 -12
- package/LICENSE.md +0 -21
- package/dist/index.cjs +0 -670
- package/dist/index.d.cts +0 -95
- package/dist/index.d.mts +0 -95
- package/dist/index.d.ts +0 -95
- package/dist/index.mjs +0 -636
package/dist/axis.cjs
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const invariant = require('tiny-invariant');
|
|
4
|
+
const splines = require('./splines.cjs');
|
|
5
|
+
|
|
6
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
7
|
+
|
|
8
|
+
const invariant__default = /*#__PURE__*/_interopDefaultCompat(invariant);
|
|
9
|
+
|
|
10
|
+
const UNIT_INTERVAL = [0, 1];
|
|
11
|
+
function createLinearPolynomial(coefficients, domain = UNIT_INTERVAL) {
|
|
12
|
+
const [a, b] = coefficients;
|
|
13
|
+
invariant__default(domain[0] <= domain[1], "invalid domain");
|
|
14
|
+
let root = a === 0 ? null : b === 0 ? 0 : -b / a;
|
|
15
|
+
if (root !== null && (root < domain[0] || root > domain[1])) {
|
|
16
|
+
root = null;
|
|
17
|
+
}
|
|
18
|
+
const range = a >= 0 ? [a * domain[0] + b, a * domain[1] + b] : [a * domain[1] + b, a * domain[0] + b];
|
|
19
|
+
const solve = (x) => {
|
|
20
|
+
invariant__default(x >= domain[0] && x <= domain[1], "x out of domain");
|
|
21
|
+
return a * x + b;
|
|
22
|
+
};
|
|
23
|
+
const solveInverse = (y) => {
|
|
24
|
+
invariant__default(y >= range[0] && y <= range[1], "y out of range");
|
|
25
|
+
return a === 0 ? 0 : (y - b) / a;
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
domain,
|
|
29
|
+
range,
|
|
30
|
+
monotonicity: a === 0 ? "constant" : a > 0 ? "increasing" : "decreasing",
|
|
31
|
+
coefficients,
|
|
32
|
+
root,
|
|
33
|
+
solve,
|
|
34
|
+
solveInverse
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function createQuadraticRootSolver(coefficients, defaultDomain, range) {
|
|
38
|
+
const [a, b, c] = coefficients;
|
|
39
|
+
if (a !== 0) {
|
|
40
|
+
return function solveInverse(y, domain = defaultDomain) {
|
|
41
|
+
invariant__default(domain[0] <= domain[1], "invalid domain");
|
|
42
|
+
invariant__default(y >= range[0] && y <= range[1], "y out of range");
|
|
43
|
+
const discriminant = b ** 2 - 4 * a * (c - y);
|
|
44
|
+
if (discriminant < 0) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
if (discriminant === 0) {
|
|
48
|
+
const root = -b / (2 * a);
|
|
49
|
+
return root < domain[0] || root > domain[1] ? [] : [root];
|
|
50
|
+
}
|
|
51
|
+
const sqrtDiscriminant = Math.sqrt(discriminant);
|
|
52
|
+
const denominator = 2 * a;
|
|
53
|
+
const left = (-b - sqrtDiscriminant) / denominator;
|
|
54
|
+
const right = (-b + sqrtDiscriminant) / denominator;
|
|
55
|
+
const leftInDomain = left >= domain[0] && left <= domain[1];
|
|
56
|
+
const rightInDomain = right >= domain[0] && right <= domain[1];
|
|
57
|
+
if (leftInDomain && rightInDomain) {
|
|
58
|
+
return left < right ? [left, right] : [right, left];
|
|
59
|
+
}
|
|
60
|
+
if (leftInDomain) {
|
|
61
|
+
return [left];
|
|
62
|
+
}
|
|
63
|
+
if (rightInDomain) {
|
|
64
|
+
return [right];
|
|
65
|
+
}
|
|
66
|
+
return [];
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (b !== 0) {
|
|
70
|
+
return function solveInverse(y, domain = defaultDomain) {
|
|
71
|
+
invariant__default(domain[0] <= domain[1], "invalid domain");
|
|
72
|
+
invariant__default(y >= range[0] && y <= range[1], "y out of range");
|
|
73
|
+
const root = -(c - y) / b;
|
|
74
|
+
return root < domain[0] || root > domain[1] ? [] : [root];
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return function solveInverse() {
|
|
78
|
+
return [];
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function createQuadraticPolynomial(coefficients, domain = UNIT_INTERVAL) {
|
|
82
|
+
const [a, b, c] = coefficients;
|
|
83
|
+
invariant__default(domain[0] <= domain[1], "invalid domain");
|
|
84
|
+
const derivative = createLinearPolynomial([2 * a, b], domain);
|
|
85
|
+
const extreme = derivative.root;
|
|
86
|
+
const solve = (x) => {
|
|
87
|
+
invariant__default(x >= domain[0] && x <= domain[1], "x out of domain");
|
|
88
|
+
return a * x ** 2 + b * x + c;
|
|
89
|
+
};
|
|
90
|
+
const rangeCandidates = extreme === null ? [solve(domain[0]), solve(domain[1])] : [solve(domain[0]), solve(extreme), solve(domain[1])];
|
|
91
|
+
const range = [
|
|
92
|
+
Math.min(...rangeCandidates),
|
|
93
|
+
Math.max(...rangeCandidates)
|
|
94
|
+
];
|
|
95
|
+
const solveInverse = createQuadraticRootSolver(coefficients, domain, range);
|
|
96
|
+
const roots = range[0] > 0 || range[1] < 0 ? [] : solveInverse(0);
|
|
97
|
+
return {
|
|
98
|
+
domain,
|
|
99
|
+
range,
|
|
100
|
+
coefficients,
|
|
101
|
+
monotonicity: a !== 0 ? "none" : b === 0 ? "constant" : b > 0 ? "increasing" : "decreasing",
|
|
102
|
+
extreme,
|
|
103
|
+
roots,
|
|
104
|
+
derivative,
|
|
105
|
+
solve,
|
|
106
|
+
solveInverse
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const TWO_THIRDS_PI = 2 * Math.PI / 3;
|
|
110
|
+
function createCubicRootSolver(coefficients, defaultDomain, range) {
|
|
111
|
+
if (coefficients[0] === 0) {
|
|
112
|
+
return createQuadraticRootSolver(
|
|
113
|
+
[coefficients[1], coefficients[2], coefficients[3]],
|
|
114
|
+
defaultDomain,
|
|
115
|
+
range
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
const oneOverA = 1 / coefficients[0];
|
|
119
|
+
const a0 = coefficients[1] * oneOverA;
|
|
120
|
+
const a1 = coefficients[2] * oneOverA;
|
|
121
|
+
const a2OverThree = a0 / 3;
|
|
122
|
+
const q = a1 / 3 - a0 ** 2 / 9;
|
|
123
|
+
const r0 = a1 * a0 / 6 - a0 ** 3 / 27;
|
|
124
|
+
return function solveInverse(y, domain = defaultDomain) {
|
|
125
|
+
invariant__default(domain[0] <= domain[1], "invalid domain");
|
|
126
|
+
invariant__default(y >= range[0] && y <= range[1], "y out of range");
|
|
127
|
+
const a02 = (coefficients[3] - y) * oneOverA;
|
|
128
|
+
const r = r0 - a02 * 0.5;
|
|
129
|
+
const discriminant = q ** 3 + r ** 2;
|
|
130
|
+
if (discriminant > 0) {
|
|
131
|
+
const a = Math.cbrt(Math.abs(r) + Math.sqrt(discriminant));
|
|
132
|
+
const root = (r < 0 ? q / a - a : a - q / a) - a2OverThree;
|
|
133
|
+
return root < domain[0] || root > domain[1] ? [] : [root];
|
|
134
|
+
}
|
|
135
|
+
const theta = Math.acos(r / Math.sqrt((-q) ** 3)) / 3;
|
|
136
|
+
const twoRootQ = 2 * Math.sqrt(-q);
|
|
137
|
+
const root1 = twoRootQ * Math.cos(theta - TWO_THIRDS_PI) - a2OverThree;
|
|
138
|
+
const root2 = twoRootQ * Math.cos(theta) - a2OverThree;
|
|
139
|
+
const root3 = twoRootQ * Math.cos(theta + TWO_THIRDS_PI) - a2OverThree;
|
|
140
|
+
const candidates = [root1, root2, root3];
|
|
141
|
+
const roots = [];
|
|
142
|
+
for (const candidate of candidates) {
|
|
143
|
+
if (candidate >= domain[0] && candidate <= domain[1] && !roots.includes(candidate)) {
|
|
144
|
+
roots.push(candidate);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
roots.sort((a, b) => a - b);
|
|
148
|
+
return roots;
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function getMonotonicity(extrema, derivative, domain) {
|
|
152
|
+
if (derivative.coefficients[0] === 0 && derivative.coefficients[1] === 0 && derivative.coefficients[2] === 0) {
|
|
153
|
+
return "constant";
|
|
154
|
+
}
|
|
155
|
+
let hasInnerExtrema = false;
|
|
156
|
+
for (const extreme of extrema) {
|
|
157
|
+
if (extreme > domain[0] && extreme > domain[1]) {
|
|
158
|
+
hasInnerExtrema = true;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (hasInnerExtrema) {
|
|
163
|
+
return "none";
|
|
164
|
+
}
|
|
165
|
+
let test = derivative.solve(domain[0]);
|
|
166
|
+
if (test === 0) {
|
|
167
|
+
test = derivative.solve((domain[0] + domain[1]) * 0.5);
|
|
168
|
+
}
|
|
169
|
+
return Math.sign(test) > 0 ? "increasing" : "decreasing";
|
|
170
|
+
}
|
|
171
|
+
function createCubicPolynomial(coefficients, domain = UNIT_INTERVAL) {
|
|
172
|
+
invariant__default(domain[0] <= domain[1], "invalid domain");
|
|
173
|
+
const [a, b, c, d] = coefficients;
|
|
174
|
+
const derivative = createQuadraticPolynomial([3 * a, 2 * b, c], domain);
|
|
175
|
+
const extrema = derivative.roots.length === 1 && derivative.coefficients[0] !== 0 ? [] : derivative.roots;
|
|
176
|
+
const solve = (x) => {
|
|
177
|
+
invariant__default(x >= domain[0] && x <= domain[1], "x out of domain");
|
|
178
|
+
return a * x ** 3 + b * x ** 2 + c * x + d;
|
|
179
|
+
};
|
|
180
|
+
const rangeCandidates = [
|
|
181
|
+
solve(domain[0]),
|
|
182
|
+
...extrema.map((extreme) => solve(extreme)),
|
|
183
|
+
solve(domain[1])
|
|
184
|
+
];
|
|
185
|
+
const range = [
|
|
186
|
+
Math.min(...rangeCandidates),
|
|
187
|
+
Math.max(...rangeCandidates)
|
|
188
|
+
];
|
|
189
|
+
const solveInverse = createCubicRootSolver(coefficients, domain, range);
|
|
190
|
+
const roots = range[0] > 0 || range[1] < 0 ? [] : solveInverse(0, domain);
|
|
191
|
+
const monotonicity = getMonotonicity(extrema, derivative, domain);
|
|
192
|
+
return {
|
|
193
|
+
domain,
|
|
194
|
+
range,
|
|
195
|
+
monotonicity,
|
|
196
|
+
coefficients,
|
|
197
|
+
extrema,
|
|
198
|
+
roots,
|
|
199
|
+
derivative,
|
|
200
|
+
solve,
|
|
201
|
+
solveInverse
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function getCurveExtrema(lookup) {
|
|
206
|
+
const extremaLookup = new Map(lookup);
|
|
207
|
+
const extremaCandidates = Array.from(extremaLookup.keys());
|
|
208
|
+
for (const [index, t] of extremaCandidates.entries()) {
|
|
209
|
+
if (index === 0 || index === extremaCandidates.length - 1) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const previous = extremaLookup.get(extremaCandidates[index - 1]);
|
|
213
|
+
const current = extremaLookup.get(t);
|
|
214
|
+
const next = extremaLookup.get(extremaCandidates[index + 1]);
|
|
215
|
+
if (previous <= current && current < next || previous >= current && current > next) {
|
|
216
|
+
extremaLookup.delete(t);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const extrema = Array.from(extremaLookup.keys());
|
|
220
|
+
return extrema;
|
|
221
|
+
}
|
|
222
|
+
function createCurveAxis(coefficients) {
|
|
223
|
+
invariant__default(coefficients.length > 0, "Curve must have at least one segment");
|
|
224
|
+
const segments = [];
|
|
225
|
+
const extremaLookup = /* @__PURE__ */ new Map();
|
|
226
|
+
let monotonicity = "none";
|
|
227
|
+
const normalizedToCurve = coefficients.length;
|
|
228
|
+
const curveToNormalized = 1 / coefficients.length;
|
|
229
|
+
for (let i = 0; i < coefficients.length; i++) {
|
|
230
|
+
const segment = createCubicPolynomial(coefficients[i], UNIT_INTERVAL);
|
|
231
|
+
segments.push(segment);
|
|
232
|
+
extremaLookup.set(i * curveToNormalized, segment.solve(0));
|
|
233
|
+
for (const extreme of segment.extrema) {
|
|
234
|
+
extremaLookup.set(
|
|
235
|
+
(i + extreme) * curveToNormalized,
|
|
236
|
+
segment.solve(extreme)
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
if (i === 0) {
|
|
240
|
+
monotonicity = segment.monotonicity;
|
|
241
|
+
} else if (monotonicity !== segment.monotonicity) {
|
|
242
|
+
monotonicity = "none";
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
extremaLookup.set(1, segments[segments.length - 1].solve(1));
|
|
246
|
+
const extrema = getCurveExtrema(extremaLookup);
|
|
247
|
+
const range = [
|
|
248
|
+
Math.min(...extremaLookup.values()),
|
|
249
|
+
Math.max(...extremaLookup.values())
|
|
250
|
+
];
|
|
251
|
+
const getSegmentAndInput = (t) => {
|
|
252
|
+
invariant__default(t >= 0 && t <= 1, "t out of range");
|
|
253
|
+
if (t === 1) {
|
|
254
|
+
return { segment: segments[segments.length - 1], input: 1 };
|
|
255
|
+
}
|
|
256
|
+
const curveT = t * normalizedToCurve;
|
|
257
|
+
const index = Math.floor(curveT);
|
|
258
|
+
const segment = segments[index];
|
|
259
|
+
const input = curveT - index;
|
|
260
|
+
return { segment, input };
|
|
261
|
+
};
|
|
262
|
+
const solvePosition = (t) => {
|
|
263
|
+
const { segment, input } = getSegmentAndInput(t);
|
|
264
|
+
return segment.solve(input);
|
|
265
|
+
};
|
|
266
|
+
const solveVelocity = (t) => {
|
|
267
|
+
const { segment, input } = getSegmentAndInput(t);
|
|
268
|
+
return segment.derivative.solve(input);
|
|
269
|
+
};
|
|
270
|
+
const solveT = (position, domain) => {
|
|
271
|
+
invariant__default(
|
|
272
|
+
position >= range[0] && position <= range[1],
|
|
273
|
+
"position out of range"
|
|
274
|
+
);
|
|
275
|
+
invariant__default(domain[0] <= domain[1], "invalid domain");
|
|
276
|
+
const result = [];
|
|
277
|
+
for (const [index, segment] of segments.entries()) {
|
|
278
|
+
if (position >= segment.range[0] && position <= segment.range[1]) {
|
|
279
|
+
const t = segment.solveInverse(position, domain);
|
|
280
|
+
for (const tValue of t) {
|
|
281
|
+
result.push((index + tValue) * curveToNormalized);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return result;
|
|
286
|
+
};
|
|
287
|
+
return {
|
|
288
|
+
domain: UNIT_INTERVAL,
|
|
289
|
+
range,
|
|
290
|
+
monotonicity,
|
|
291
|
+
extrema,
|
|
292
|
+
segments,
|
|
293
|
+
solvePosition,
|
|
294
|
+
solveVelocity,
|
|
295
|
+
solveT
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
function createBasisAxis(parameters, triplicateEndpoints = true) {
|
|
299
|
+
return createCurveAxis(
|
|
300
|
+
splines.splines.basis(triplicateEndpoints).chunkCoefficients(parameters)
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
function createBezierAxis(parameters) {
|
|
304
|
+
return createCurveAxis(splines.splines.bezier.chunkCoefficients(parameters));
|
|
305
|
+
}
|
|
306
|
+
function createCardinalAxis(parameters, a = 0.5, duplicateEndpoints = true) {
|
|
307
|
+
return createCurveAxis(
|
|
308
|
+
splines.splines.cardinal(a, duplicateEndpoints).chunkCoefficients(parameters)
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
function createCatmullRomAxis(parameters, duplicateEndpoints = true) {
|
|
312
|
+
return createCurveAxis(
|
|
313
|
+
splines.splines.catmullRom(duplicateEndpoints).chunkCoefficients(parameters)
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
function createHermiteAxis(parameters) {
|
|
317
|
+
return createCurveAxis(splines.splines.hermite.chunkCoefficients(parameters));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
exports.createBasisAxis = createBasisAxis;
|
|
321
|
+
exports.createBezierAxis = createBezierAxis;
|
|
322
|
+
exports.createCardinalAxis = createCardinalAxis;
|
|
323
|
+
exports.createCatmullRomAxis = createCatmullRomAxis;
|
|
324
|
+
exports.createCurveAxis = createCurveAxis;
|
|
325
|
+
exports.createHermiteAxis = createHermiteAxis;
|
package/dist/axis.d.cts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { I as Interval, M as Monotonicity, C as CubicPolynomial, a as CubicCoefficients } from './shared/curvy.df73166c.cjs';
|
|
2
|
+
|
|
3
|
+
type CurveAxis = {
|
|
4
|
+
readonly domain: Interval;
|
|
5
|
+
readonly range: Interval;
|
|
6
|
+
readonly monotonicity: Monotonicity;
|
|
7
|
+
readonly extrema: ReadonlyArray<number>;
|
|
8
|
+
readonly segments: ReadonlyArray<CubicPolynomial>;
|
|
9
|
+
readonly solvePosition: (t: number) => number;
|
|
10
|
+
readonly solveVelocity: (t: number) => number;
|
|
11
|
+
readonly solveT: (position: number, domain: Interval) => ReadonlyArray<number>;
|
|
12
|
+
};
|
|
13
|
+
declare function createCurveAxis(coefficients: ReadonlyArray<CubicCoefficients>): CurveAxis;
|
|
14
|
+
declare function createBasisAxis(parameters: ReadonlyArray<number>, triplicateEndpoints?: boolean): CurveAxis;
|
|
15
|
+
declare function createBezierAxis(parameters: ReadonlyArray<number>): CurveAxis;
|
|
16
|
+
declare function createCardinalAxis(parameters: ReadonlyArray<number>, a?: number, duplicateEndpoints?: boolean): CurveAxis;
|
|
17
|
+
declare function createCatmullRomAxis(parameters: ReadonlyArray<number>, duplicateEndpoints?: boolean): CurveAxis;
|
|
18
|
+
declare function createHermiteAxis(parameters: ReadonlyArray<number>): CurveAxis;
|
|
19
|
+
|
|
20
|
+
export { type CurveAxis, createBasisAxis, createBezierAxis, createCardinalAxis, createCatmullRomAxis, createCurveAxis, createHermiteAxis };
|
package/dist/axis.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { I as Interval, M as Monotonicity, C as CubicPolynomial, a as CubicCoefficients } from './shared/curvy.df73166c.mjs';
|
|
2
|
+
|
|
3
|
+
type CurveAxis = {
|
|
4
|
+
readonly domain: Interval;
|
|
5
|
+
readonly range: Interval;
|
|
6
|
+
readonly monotonicity: Monotonicity;
|
|
7
|
+
readonly extrema: ReadonlyArray<number>;
|
|
8
|
+
readonly segments: ReadonlyArray<CubicPolynomial>;
|
|
9
|
+
readonly solvePosition: (t: number) => number;
|
|
10
|
+
readonly solveVelocity: (t: number) => number;
|
|
11
|
+
readonly solveT: (position: number, domain: Interval) => ReadonlyArray<number>;
|
|
12
|
+
};
|
|
13
|
+
declare function createCurveAxis(coefficients: ReadonlyArray<CubicCoefficients>): CurveAxis;
|
|
14
|
+
declare function createBasisAxis(parameters: ReadonlyArray<number>, triplicateEndpoints?: boolean): CurveAxis;
|
|
15
|
+
declare function createBezierAxis(parameters: ReadonlyArray<number>): CurveAxis;
|
|
16
|
+
declare function createCardinalAxis(parameters: ReadonlyArray<number>, a?: number, duplicateEndpoints?: boolean): CurveAxis;
|
|
17
|
+
declare function createCatmullRomAxis(parameters: ReadonlyArray<number>, duplicateEndpoints?: boolean): CurveAxis;
|
|
18
|
+
declare function createHermiteAxis(parameters: ReadonlyArray<number>): CurveAxis;
|
|
19
|
+
|
|
20
|
+
export { type CurveAxis, createBasisAxis, createBezierAxis, createCardinalAxis, createCatmullRomAxis, createCurveAxis, createHermiteAxis };
|
package/dist/axis.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { I as Interval, M as Monotonicity, C as CubicPolynomial, a as CubicCoefficients } from './shared/curvy.df73166c.js';
|
|
2
|
+
|
|
3
|
+
type CurveAxis = {
|
|
4
|
+
readonly domain: Interval;
|
|
5
|
+
readonly range: Interval;
|
|
6
|
+
readonly monotonicity: Monotonicity;
|
|
7
|
+
readonly extrema: ReadonlyArray<number>;
|
|
8
|
+
readonly segments: ReadonlyArray<CubicPolynomial>;
|
|
9
|
+
readonly solvePosition: (t: number) => number;
|
|
10
|
+
readonly solveVelocity: (t: number) => number;
|
|
11
|
+
readonly solveT: (position: number, domain: Interval) => ReadonlyArray<number>;
|
|
12
|
+
};
|
|
13
|
+
declare function createCurveAxis(coefficients: ReadonlyArray<CubicCoefficients>): CurveAxis;
|
|
14
|
+
declare function createBasisAxis(parameters: ReadonlyArray<number>, triplicateEndpoints?: boolean): CurveAxis;
|
|
15
|
+
declare function createBezierAxis(parameters: ReadonlyArray<number>): CurveAxis;
|
|
16
|
+
declare function createCardinalAxis(parameters: ReadonlyArray<number>, a?: number, duplicateEndpoints?: boolean): CurveAxis;
|
|
17
|
+
declare function createCatmullRomAxis(parameters: ReadonlyArray<number>, duplicateEndpoints?: boolean): CurveAxis;
|
|
18
|
+
declare function createHermiteAxis(parameters: ReadonlyArray<number>): CurveAxis;
|
|
19
|
+
|
|
20
|
+
export { type CurveAxis, createBasisAxis, createBezierAxis, createCardinalAxis, createCatmullRomAxis, createCurveAxis, createHermiteAxis };
|
package/dist/axis.mjs
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import invariant from 'tiny-invariant';
|
|
2
|
+
import { splines } from './splines.mjs';
|
|
3
|
+
|
|
4
|
+
const UNIT_INTERVAL = [0, 1];
|
|
5
|
+
function createLinearPolynomial(coefficients, domain = UNIT_INTERVAL) {
|
|
6
|
+
const [a, b] = coefficients;
|
|
7
|
+
invariant(domain[0] <= domain[1], "invalid domain");
|
|
8
|
+
let root = a === 0 ? null : b === 0 ? 0 : -b / a;
|
|
9
|
+
if (root !== null && (root < domain[0] || root > domain[1])) {
|
|
10
|
+
root = null;
|
|
11
|
+
}
|
|
12
|
+
const range = a >= 0 ? [a * domain[0] + b, a * domain[1] + b] : [a * domain[1] + b, a * domain[0] + b];
|
|
13
|
+
const solve = (x) => {
|
|
14
|
+
invariant(x >= domain[0] && x <= domain[1], "x out of domain");
|
|
15
|
+
return a * x + b;
|
|
16
|
+
};
|
|
17
|
+
const solveInverse = (y) => {
|
|
18
|
+
invariant(y >= range[0] && y <= range[1], "y out of range");
|
|
19
|
+
return a === 0 ? 0 : (y - b) / a;
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
domain,
|
|
23
|
+
range,
|
|
24
|
+
monotonicity: a === 0 ? "constant" : a > 0 ? "increasing" : "decreasing",
|
|
25
|
+
coefficients,
|
|
26
|
+
root,
|
|
27
|
+
solve,
|
|
28
|
+
solveInverse
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function createQuadraticRootSolver(coefficients, defaultDomain, range) {
|
|
32
|
+
const [a, b, c] = coefficients;
|
|
33
|
+
if (a !== 0) {
|
|
34
|
+
return function solveInverse(y, domain = defaultDomain) {
|
|
35
|
+
invariant(domain[0] <= domain[1], "invalid domain");
|
|
36
|
+
invariant(y >= range[0] && y <= range[1], "y out of range");
|
|
37
|
+
const discriminant = b ** 2 - 4 * a * (c - y);
|
|
38
|
+
if (discriminant < 0) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
if (discriminant === 0) {
|
|
42
|
+
const root = -b / (2 * a);
|
|
43
|
+
return root < domain[0] || root > domain[1] ? [] : [root];
|
|
44
|
+
}
|
|
45
|
+
const sqrtDiscriminant = Math.sqrt(discriminant);
|
|
46
|
+
const denominator = 2 * a;
|
|
47
|
+
const left = (-b - sqrtDiscriminant) / denominator;
|
|
48
|
+
const right = (-b + sqrtDiscriminant) / denominator;
|
|
49
|
+
const leftInDomain = left >= domain[0] && left <= domain[1];
|
|
50
|
+
const rightInDomain = right >= domain[0] && right <= domain[1];
|
|
51
|
+
if (leftInDomain && rightInDomain) {
|
|
52
|
+
return left < right ? [left, right] : [right, left];
|
|
53
|
+
}
|
|
54
|
+
if (leftInDomain) {
|
|
55
|
+
return [left];
|
|
56
|
+
}
|
|
57
|
+
if (rightInDomain) {
|
|
58
|
+
return [right];
|
|
59
|
+
}
|
|
60
|
+
return [];
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (b !== 0) {
|
|
64
|
+
return function solveInverse(y, domain = defaultDomain) {
|
|
65
|
+
invariant(domain[0] <= domain[1], "invalid domain");
|
|
66
|
+
invariant(y >= range[0] && y <= range[1], "y out of range");
|
|
67
|
+
const root = -(c - y) / b;
|
|
68
|
+
return root < domain[0] || root > domain[1] ? [] : [root];
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return function solveInverse() {
|
|
72
|
+
return [];
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function createQuadraticPolynomial(coefficients, domain = UNIT_INTERVAL) {
|
|
76
|
+
const [a, b, c] = coefficients;
|
|
77
|
+
invariant(domain[0] <= domain[1], "invalid domain");
|
|
78
|
+
const derivative = createLinearPolynomial([2 * a, b], domain);
|
|
79
|
+
const extreme = derivative.root;
|
|
80
|
+
const solve = (x) => {
|
|
81
|
+
invariant(x >= domain[0] && x <= domain[1], "x out of domain");
|
|
82
|
+
return a * x ** 2 + b * x + c;
|
|
83
|
+
};
|
|
84
|
+
const rangeCandidates = extreme === null ? [solve(domain[0]), solve(domain[1])] : [solve(domain[0]), solve(extreme), solve(domain[1])];
|
|
85
|
+
const range = [
|
|
86
|
+
Math.min(...rangeCandidates),
|
|
87
|
+
Math.max(...rangeCandidates)
|
|
88
|
+
];
|
|
89
|
+
const solveInverse = createQuadraticRootSolver(coefficients, domain, range);
|
|
90
|
+
const roots = range[0] > 0 || range[1] < 0 ? [] : solveInverse(0);
|
|
91
|
+
return {
|
|
92
|
+
domain,
|
|
93
|
+
range,
|
|
94
|
+
coefficients,
|
|
95
|
+
monotonicity: a !== 0 ? "none" : b === 0 ? "constant" : b > 0 ? "increasing" : "decreasing",
|
|
96
|
+
extreme,
|
|
97
|
+
roots,
|
|
98
|
+
derivative,
|
|
99
|
+
solve,
|
|
100
|
+
solveInverse
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const TWO_THIRDS_PI = 2 * Math.PI / 3;
|
|
104
|
+
function createCubicRootSolver(coefficients, defaultDomain, range) {
|
|
105
|
+
if (coefficients[0] === 0) {
|
|
106
|
+
return createQuadraticRootSolver(
|
|
107
|
+
[coefficients[1], coefficients[2], coefficients[3]],
|
|
108
|
+
defaultDomain,
|
|
109
|
+
range
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
const oneOverA = 1 / coefficients[0];
|
|
113
|
+
const a0 = coefficients[1] * oneOverA;
|
|
114
|
+
const a1 = coefficients[2] * oneOverA;
|
|
115
|
+
const a2OverThree = a0 / 3;
|
|
116
|
+
const q = a1 / 3 - a0 ** 2 / 9;
|
|
117
|
+
const r0 = a1 * a0 / 6 - a0 ** 3 / 27;
|
|
118
|
+
return function solveInverse(y, domain = defaultDomain) {
|
|
119
|
+
invariant(domain[0] <= domain[1], "invalid domain");
|
|
120
|
+
invariant(y >= range[0] && y <= range[1], "y out of range");
|
|
121
|
+
const a02 = (coefficients[3] - y) * oneOverA;
|
|
122
|
+
const r = r0 - a02 * 0.5;
|
|
123
|
+
const discriminant = q ** 3 + r ** 2;
|
|
124
|
+
if (discriminant > 0) {
|
|
125
|
+
const a = Math.cbrt(Math.abs(r) + Math.sqrt(discriminant));
|
|
126
|
+
const root = (r < 0 ? q / a - a : a - q / a) - a2OverThree;
|
|
127
|
+
return root < domain[0] || root > domain[1] ? [] : [root];
|
|
128
|
+
}
|
|
129
|
+
const theta = Math.acos(r / Math.sqrt((-q) ** 3)) / 3;
|
|
130
|
+
const twoRootQ = 2 * Math.sqrt(-q);
|
|
131
|
+
const root1 = twoRootQ * Math.cos(theta - TWO_THIRDS_PI) - a2OverThree;
|
|
132
|
+
const root2 = twoRootQ * Math.cos(theta) - a2OverThree;
|
|
133
|
+
const root3 = twoRootQ * Math.cos(theta + TWO_THIRDS_PI) - a2OverThree;
|
|
134
|
+
const candidates = [root1, root2, root3];
|
|
135
|
+
const roots = [];
|
|
136
|
+
for (const candidate of candidates) {
|
|
137
|
+
if (candidate >= domain[0] && candidate <= domain[1] && !roots.includes(candidate)) {
|
|
138
|
+
roots.push(candidate);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
roots.sort((a, b) => a - b);
|
|
142
|
+
return roots;
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function getMonotonicity(extrema, derivative, domain) {
|
|
146
|
+
if (derivative.coefficients[0] === 0 && derivative.coefficients[1] === 0 && derivative.coefficients[2] === 0) {
|
|
147
|
+
return "constant";
|
|
148
|
+
}
|
|
149
|
+
let hasInnerExtrema = false;
|
|
150
|
+
for (const extreme of extrema) {
|
|
151
|
+
if (extreme > domain[0] && extreme > domain[1]) {
|
|
152
|
+
hasInnerExtrema = true;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (hasInnerExtrema) {
|
|
157
|
+
return "none";
|
|
158
|
+
}
|
|
159
|
+
let test = derivative.solve(domain[0]);
|
|
160
|
+
if (test === 0) {
|
|
161
|
+
test = derivative.solve((domain[0] + domain[1]) * 0.5);
|
|
162
|
+
}
|
|
163
|
+
return Math.sign(test) > 0 ? "increasing" : "decreasing";
|
|
164
|
+
}
|
|
165
|
+
function createCubicPolynomial(coefficients, domain = UNIT_INTERVAL) {
|
|
166
|
+
invariant(domain[0] <= domain[1], "invalid domain");
|
|
167
|
+
const [a, b, c, d] = coefficients;
|
|
168
|
+
const derivative = createQuadraticPolynomial([3 * a, 2 * b, c], domain);
|
|
169
|
+
const extrema = derivative.roots.length === 1 && derivative.coefficients[0] !== 0 ? [] : derivative.roots;
|
|
170
|
+
const solve = (x) => {
|
|
171
|
+
invariant(x >= domain[0] && x <= domain[1], "x out of domain");
|
|
172
|
+
return a * x ** 3 + b * x ** 2 + c * x + d;
|
|
173
|
+
};
|
|
174
|
+
const rangeCandidates = [
|
|
175
|
+
solve(domain[0]),
|
|
176
|
+
...extrema.map((extreme) => solve(extreme)),
|
|
177
|
+
solve(domain[1])
|
|
178
|
+
];
|
|
179
|
+
const range = [
|
|
180
|
+
Math.min(...rangeCandidates),
|
|
181
|
+
Math.max(...rangeCandidates)
|
|
182
|
+
];
|
|
183
|
+
const solveInverse = createCubicRootSolver(coefficients, domain, range);
|
|
184
|
+
const roots = range[0] > 0 || range[1] < 0 ? [] : solveInverse(0, domain);
|
|
185
|
+
const monotonicity = getMonotonicity(extrema, derivative, domain);
|
|
186
|
+
return {
|
|
187
|
+
domain,
|
|
188
|
+
range,
|
|
189
|
+
monotonicity,
|
|
190
|
+
coefficients,
|
|
191
|
+
extrema,
|
|
192
|
+
roots,
|
|
193
|
+
derivative,
|
|
194
|
+
solve,
|
|
195
|
+
solveInverse
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function getCurveExtrema(lookup) {
|
|
200
|
+
const extremaLookup = new Map(lookup);
|
|
201
|
+
const extremaCandidates = Array.from(extremaLookup.keys());
|
|
202
|
+
for (const [index, t] of extremaCandidates.entries()) {
|
|
203
|
+
if (index === 0 || index === extremaCandidates.length - 1) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const previous = extremaLookup.get(extremaCandidates[index - 1]);
|
|
207
|
+
const current = extremaLookup.get(t);
|
|
208
|
+
const next = extremaLookup.get(extremaCandidates[index + 1]);
|
|
209
|
+
if (previous <= current && current < next || previous >= current && current > next) {
|
|
210
|
+
extremaLookup.delete(t);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const extrema = Array.from(extremaLookup.keys());
|
|
214
|
+
return extrema;
|
|
215
|
+
}
|
|
216
|
+
function createCurveAxis(coefficients) {
|
|
217
|
+
invariant(coefficients.length > 0, "Curve must have at least one segment");
|
|
218
|
+
const segments = [];
|
|
219
|
+
const extremaLookup = /* @__PURE__ */ new Map();
|
|
220
|
+
let monotonicity = "none";
|
|
221
|
+
const normalizedToCurve = coefficients.length;
|
|
222
|
+
const curveToNormalized = 1 / coefficients.length;
|
|
223
|
+
for (let i = 0; i < coefficients.length; i++) {
|
|
224
|
+
const segment = createCubicPolynomial(coefficients[i], UNIT_INTERVAL);
|
|
225
|
+
segments.push(segment);
|
|
226
|
+
extremaLookup.set(i * curveToNormalized, segment.solve(0));
|
|
227
|
+
for (const extreme of segment.extrema) {
|
|
228
|
+
extremaLookup.set(
|
|
229
|
+
(i + extreme) * curveToNormalized,
|
|
230
|
+
segment.solve(extreme)
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
if (i === 0) {
|
|
234
|
+
monotonicity = segment.monotonicity;
|
|
235
|
+
} else if (monotonicity !== segment.monotonicity) {
|
|
236
|
+
monotonicity = "none";
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
extremaLookup.set(1, segments[segments.length - 1].solve(1));
|
|
240
|
+
const extrema = getCurveExtrema(extremaLookup);
|
|
241
|
+
const range = [
|
|
242
|
+
Math.min(...extremaLookup.values()),
|
|
243
|
+
Math.max(...extremaLookup.values())
|
|
244
|
+
];
|
|
245
|
+
const getSegmentAndInput = (t) => {
|
|
246
|
+
invariant(t >= 0 && t <= 1, "t out of range");
|
|
247
|
+
if (t === 1) {
|
|
248
|
+
return { segment: segments[segments.length - 1], input: 1 };
|
|
249
|
+
}
|
|
250
|
+
const curveT = t * normalizedToCurve;
|
|
251
|
+
const index = Math.floor(curveT);
|
|
252
|
+
const segment = segments[index];
|
|
253
|
+
const input = curveT - index;
|
|
254
|
+
return { segment, input };
|
|
255
|
+
};
|
|
256
|
+
const solvePosition = (t) => {
|
|
257
|
+
const { segment, input } = getSegmentAndInput(t);
|
|
258
|
+
return segment.solve(input);
|
|
259
|
+
};
|
|
260
|
+
const solveVelocity = (t) => {
|
|
261
|
+
const { segment, input } = getSegmentAndInput(t);
|
|
262
|
+
return segment.derivative.solve(input);
|
|
263
|
+
};
|
|
264
|
+
const solveT = (position, domain) => {
|
|
265
|
+
invariant(
|
|
266
|
+
position >= range[0] && position <= range[1],
|
|
267
|
+
"position out of range"
|
|
268
|
+
);
|
|
269
|
+
invariant(domain[0] <= domain[1], "invalid domain");
|
|
270
|
+
const result = [];
|
|
271
|
+
for (const [index, segment] of segments.entries()) {
|
|
272
|
+
if (position >= segment.range[0] && position <= segment.range[1]) {
|
|
273
|
+
const t = segment.solveInverse(position, domain);
|
|
274
|
+
for (const tValue of t) {
|
|
275
|
+
result.push((index + tValue) * curveToNormalized);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return result;
|
|
280
|
+
};
|
|
281
|
+
return {
|
|
282
|
+
domain: UNIT_INTERVAL,
|
|
283
|
+
range,
|
|
284
|
+
monotonicity,
|
|
285
|
+
extrema,
|
|
286
|
+
segments,
|
|
287
|
+
solvePosition,
|
|
288
|
+
solveVelocity,
|
|
289
|
+
solveT
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function createBasisAxis(parameters, triplicateEndpoints = true) {
|
|
293
|
+
return createCurveAxis(
|
|
294
|
+
splines.basis(triplicateEndpoints).chunkCoefficients(parameters)
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
function createBezierAxis(parameters) {
|
|
298
|
+
return createCurveAxis(splines.bezier.chunkCoefficients(parameters));
|
|
299
|
+
}
|
|
300
|
+
function createCardinalAxis(parameters, a = 0.5, duplicateEndpoints = true) {
|
|
301
|
+
return createCurveAxis(
|
|
302
|
+
splines.cardinal(a, duplicateEndpoints).chunkCoefficients(parameters)
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
function createCatmullRomAxis(parameters, duplicateEndpoints = true) {
|
|
306
|
+
return createCurveAxis(
|
|
307
|
+
splines.catmullRom(duplicateEndpoints).chunkCoefficients(parameters)
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
function createHermiteAxis(parameters) {
|
|
311
|
+
return createCurveAxis(splines.hermite.chunkCoefficients(parameters));
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export { createBasisAxis, createBezierAxis, createCardinalAxis, createCatmullRomAxis, createCurveAxis, createHermiteAxis };
|