curvy 0.3.1 → 0.3.3

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.
@@ -0,0 +1,335 @@
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
+ function computeCubicAntiderivative(coefficients, integrationConstant) {
205
+ return [
206
+ coefficients[0] / 4,
207
+ coefficients[1] / 3,
208
+ coefficients[2] / 2,
209
+ coefficients[3],
210
+ integrationConstant
211
+ ];
212
+ }
213
+
214
+ function getCurveExtrema(lookup) {
215
+ const extremaLookup = new Map(lookup);
216
+ const extremaCandidates = Array.from(extremaLookup.keys());
217
+ for (const [index, t] of extremaCandidates.entries()) {
218
+ if (index === 0 || index === extremaCandidates.length - 1) {
219
+ continue;
220
+ }
221
+ const previous = extremaLookup.get(extremaCandidates[index - 1]);
222
+ const current = extremaLookup.get(t);
223
+ const next = extremaLookup.get(extremaCandidates[index + 1]);
224
+ if (previous <= current && current < next || previous >= current && current > next) {
225
+ extremaLookup.delete(t);
226
+ }
227
+ }
228
+ const extrema = Array.from(extremaLookup.keys());
229
+ return extrema;
230
+ }
231
+ function createCurveAxis(coefficients) {
232
+ invariant__default(coefficients.length > 0, "Curve must have at least one segment");
233
+ const segments = [];
234
+ const extremaLookup = /* @__PURE__ */ new Map();
235
+ let monotonicity = "none";
236
+ const normalizedToCurve = coefficients.length;
237
+ const curveToNormalized = 1 / coefficients.length;
238
+ for (let i = 0; i < coefficients.length; i++) {
239
+ const segment = createCubicPolynomial(coefficients[i], UNIT_INTERVAL);
240
+ segments.push(segment);
241
+ extremaLookup.set(i * curveToNormalized, segment.solve(0));
242
+ for (const extreme of segment.extrema) {
243
+ extremaLookup.set(
244
+ (i + extreme) * curveToNormalized,
245
+ segment.solve(extreme)
246
+ );
247
+ }
248
+ if (i === 0) {
249
+ monotonicity = segment.monotonicity;
250
+ } else if (monotonicity !== segment.monotonicity) {
251
+ monotonicity = "none";
252
+ }
253
+ }
254
+ extremaLookup.set(1, segments[segments.length - 1].solve(1));
255
+ const extrema = getCurveExtrema(extremaLookup);
256
+ const range = [
257
+ Math.min(...extremaLookup.values()),
258
+ Math.max(...extremaLookup.values())
259
+ ];
260
+ const getSegmentAndInput = (t) => {
261
+ invariant__default(t >= 0 && t <= 1, "t out of range");
262
+ if (t === 1) {
263
+ return { segment: segments[segments.length - 1], input: 1 };
264
+ }
265
+ const curveT = t * normalizedToCurve;
266
+ const index = Math.floor(curveT);
267
+ const segment = segments[index];
268
+ const input = curveT - index;
269
+ return { segment, input };
270
+ };
271
+ const solvePosition = (t) => {
272
+ const { segment, input } = getSegmentAndInput(t);
273
+ return segment.solve(input);
274
+ };
275
+ const solveVelocity = (t) => {
276
+ const { segment, input } = getSegmentAndInput(t);
277
+ return segment.derivative.solve(input);
278
+ };
279
+ const solveT = (position, domain) => {
280
+ invariant__default(
281
+ position >= range[0] && position <= range[1],
282
+ "position out of range"
283
+ );
284
+ invariant__default(domain[0] <= domain[1], "invalid domain");
285
+ const result = [];
286
+ for (const [index, segment] of segments.entries()) {
287
+ if (position >= segment.range[0] && position <= segment.range[1]) {
288
+ const t = segment.solveInverse(position, domain);
289
+ for (const tValue of t) {
290
+ result.push((index + tValue) * curveToNormalized);
291
+ }
292
+ }
293
+ }
294
+ return result;
295
+ };
296
+ return {
297
+ domain: UNIT_INTERVAL,
298
+ range,
299
+ monotonicity,
300
+ extrema,
301
+ segments,
302
+ solvePosition,
303
+ solveVelocity,
304
+ solveT
305
+ };
306
+ }
307
+ function createBasisAxis(parameters, triplicateEndpoints = true) {
308
+ return createCurveAxis(
309
+ splines.splines.basis(triplicateEndpoints).chunkCoefficients(parameters)
310
+ );
311
+ }
312
+ function createBezierAxis(parameters) {
313
+ return createCurveAxis(splines.splines.bezier.chunkCoefficients(parameters));
314
+ }
315
+ function createCardinalAxis(parameters, a = 0.5, duplicateEndpoints = true) {
316
+ return createCurveAxis(
317
+ splines.splines.cardinal(a, duplicateEndpoints).chunkCoefficients(parameters)
318
+ );
319
+ }
320
+ function createCatmullRomAxis(parameters, duplicateEndpoints = true) {
321
+ return createCurveAxis(
322
+ splines.splines.catmullRom(duplicateEndpoints).chunkCoefficients(parameters)
323
+ );
324
+ }
325
+ function createHermiteAxis(parameters) {
326
+ return createCurveAxis(splines.splines.hermite.chunkCoefficients(parameters));
327
+ }
328
+
329
+ exports.computeCubicAntiderivative = computeCubicAntiderivative;
330
+ exports.createBasisAxis = createBasisAxis;
331
+ exports.createBezierAxis = createBezierAxis;
332
+ exports.createCardinalAxis = createCardinalAxis;
333
+ exports.createCatmullRomAxis = createCatmullRomAxis;
334
+ exports.createCurveAxis = createCurveAxis;
335
+ exports.createHermiteAxis = createHermiteAxis;
@@ -4,7 +4,7 @@ type LinearCoefficients = readonly [number, number];
4
4
  type LinearPolynomial = {
5
5
  readonly domain: Interval;
6
6
  readonly range: Interval;
7
- readonly monotonocity: Monotonicity;
7
+ readonly monotonicity: Monotonicity;
8
8
  readonly coefficients: LinearCoefficients;
9
9
  readonly root: number | null;
10
10
  readonly solve: (x: number) => number;
@@ -14,7 +14,7 @@ type QuadraticCoefficients = readonly [number, number, number];
14
14
  type QuadraticPolynomial = {
15
15
  readonly domain: Interval;
16
16
  readonly range: Interval;
17
- readonly monotonocity: Monotonicity;
17
+ readonly monotonicity: Monotonicity;
18
18
  readonly coefficients: QuadraticCoefficients;
19
19
  readonly extreme: number | null;
20
20
  readonly roots: ReadonlyArray<number>;
@@ -26,7 +26,7 @@ type CubicCoefficients = readonly [number, number, number, number];
26
26
  type CubicPolynomial = {
27
27
  readonly domain: Interval;
28
28
  readonly range: Interval;
29
- readonly monotonocity: Monotonicity;
29
+ readonly monotonicity: Monotonicity;
30
30
  readonly coefficients: CubicCoefficients;
31
31
  readonly extrema: ReadonlyArray<number>;
32
32
  readonly roots: ReadonlyArray<number>;
@@ -4,7 +4,7 @@ type LinearCoefficients = readonly [number, number];
4
4
  type LinearPolynomial = {
5
5
  readonly domain: Interval;
6
6
  readonly range: Interval;
7
- readonly monotonocity: Monotonicity;
7
+ readonly monotonicity: Monotonicity;
8
8
  readonly coefficients: LinearCoefficients;
9
9
  readonly root: number | null;
10
10
  readonly solve: (x: number) => number;
@@ -14,7 +14,7 @@ type QuadraticCoefficients = readonly [number, number, number];
14
14
  type QuadraticPolynomial = {
15
15
  readonly domain: Interval;
16
16
  readonly range: Interval;
17
- readonly monotonocity: Monotonicity;
17
+ readonly monotonicity: Monotonicity;
18
18
  readonly coefficients: QuadraticCoefficients;
19
19
  readonly extreme: number | null;
20
20
  readonly roots: ReadonlyArray<number>;
@@ -26,7 +26,7 @@ type CubicCoefficients = readonly [number, number, number, number];
26
26
  type CubicPolynomial = {
27
27
  readonly domain: Interval;
28
28
  readonly range: Interval;
29
- readonly monotonocity: Monotonicity;
29
+ readonly monotonicity: Monotonicity;
30
30
  readonly coefficients: CubicCoefficients;
31
31
  readonly extrema: ReadonlyArray<number>;
32
32
  readonly roots: ReadonlyArray<number>;
@@ -4,7 +4,7 @@ type LinearCoefficients = readonly [number, number];
4
4
  type LinearPolynomial = {
5
5
  readonly domain: Interval;
6
6
  readonly range: Interval;
7
- readonly monotonocity: Monotonicity;
7
+ readonly monotonicity: Monotonicity;
8
8
  readonly coefficients: LinearCoefficients;
9
9
  readonly root: number | null;
10
10
  readonly solve: (x: number) => number;
@@ -14,7 +14,7 @@ type QuadraticCoefficients = readonly [number, number, number];
14
14
  type QuadraticPolynomial = {
15
15
  readonly domain: Interval;
16
16
  readonly range: Interval;
17
- readonly monotonocity: Monotonicity;
17
+ readonly monotonicity: Monotonicity;
18
18
  readonly coefficients: QuadraticCoefficients;
19
19
  readonly extreme: number | null;
20
20
  readonly roots: ReadonlyArray<number>;
@@ -26,7 +26,7 @@ type CubicCoefficients = readonly [number, number, number, number];
26
26
  type CubicPolynomial = {
27
27
  readonly domain: Interval;
28
28
  readonly range: Interval;
29
- readonly monotonocity: Monotonicity;
29
+ readonly monotonicity: Monotonicity;
30
30
  readonly coefficients: CubicCoefficients;
31
31
  readonly extrema: ReadonlyArray<number>;
32
32
  readonly roots: ReadonlyArray<number>;
@@ -1,4 +1,4 @@
1
- import { a as CubicCoefficients } from './shared/curvy.0f1371d7.cjs';
1
+ import { a as CubicCoefficients } from './shared/curvy.df73166c.cjs';
2
2
 
3
3
  type Matrix4x4 = readonly [
4
4
  CubicCoefficients,
@@ -1,4 +1,4 @@
1
- import { a as CubicCoefficients } from './shared/curvy.0f1371d7.mjs';
1
+ import { a as CubicCoefficients } from './shared/curvy.df73166c.mjs';
2
2
 
3
3
  type Matrix4x4 = readonly [
4
4
  CubicCoefficients,
package/dist/splines.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as CubicCoefficients } from './shared/curvy.0f1371d7.js';
1
+ import { a as CubicCoefficients } from './shared/curvy.df73166c.js';
2
2
 
3
3
  type Matrix4x4 = readonly [
4
4
  CubicCoefficients,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "curvy",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Spline and Curve utilities",
5
5
  "author": "Tim Morris <timthewebguy@gmail.com>",
6
6
  "homepage": "https://github.com/tkofh/curvy#readme",
@@ -32,7 +32,9 @@
32
32
  "types": "./dist/splines.d.ts"
33
33
  }
34
34
  },
35
- "files": ["dist"],
35
+ "files": [
36
+ "dist"
37
+ ],
36
38
  "publishConfig": {
37
39
  "access": "public"
38
40
  },