@ac-kit/math-curves 0.2.0 → 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 +83 -0
- package/README.md +27 -3
- package/dist/_catmull-rom.d.ts +23 -0
- package/dist/_catmull-rom.d.ts.map +1 -0
- package/dist/_catmull-rom.js +66 -0
- package/dist/_catmull-rom.js.map +1 -0
- package/dist/arc-length.d.ts +57 -0
- package/dist/arc-length.d.ts.map +1 -0
- package/dist/arc-length.js +121 -0
- package/dist/arc-length.js.map +1 -0
- package/dist/bezier-n.d.ts +47 -0
- package/dist/bezier-n.d.ts.map +1 -0
- package/dist/bezier-n.js +113 -0
- package/dist/bezier-n.js.map +1 -0
- package/dist/bezier2.d.ts +12 -0
- package/dist/bezier2.d.ts.map +1 -1
- package/dist/bezier2.js +19 -0
- package/dist/bezier2.js.map +1 -1
- package/dist/bezier3.d.ts +40 -0
- package/dist/bezier3.d.ts.map +1 -0
- package/dist/bezier3.js +75 -0
- package/dist/bezier3.js.map +1 -0
- package/dist/catmull-rom-curve.d.ts +39 -0
- package/dist/catmull-rom-curve.d.ts.map +1 -0
- package/dist/catmull-rom-curve.js +123 -0
- package/dist/catmull-rom-curve.js.map +1 -0
- package/dist/cubic-bezier-easing.d.ts +27 -0
- package/dist/cubic-bezier-easing.d.ts.map +1 -0
- package/dist/cubic-bezier-easing.js +104 -0
- package/dist/cubic-bezier-easing.js.map +1 -0
- package/dist/curve-to-polyline.d.ts +33 -0
- package/dist/curve-to-polyline.d.ts.map +1 -0
- package/dist/curve-to-polyline.js +121 -0
- package/dist/curve-to-polyline.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/parametric-curve.d.ts +31 -0
- package/dist/parametric-curve.d.ts.map +1 -0
- package/dist/parametric-curve.js +2 -0
- package/dist/parametric-curve.js.map +1 -0
- package/dist/spline2.d.ts +58 -18
- package/dist/spline2.d.ts.map +1 -1
- package/dist/spline2.js +212 -100
- package/dist/spline2.js.map +1 -1
- package/package.json +9 -6
package/dist/bezier2.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bezier2.js","sourceRoot":"","sources":["../src/bezier2.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAC/B,GAAS,EACT,EAAgB,EAChB,EAAgB,EAChB,EAAgB,EAChB,CAAS;IAET,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChB,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IACjB,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACrB,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IACjB,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC/C,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC/C,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACX,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAEX,OAAO,GAAG,CAAC;AACZ,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,YAAY,CAC3B,GAAS,EACT,EAAgB,EAChB,EAAgB,EAChB,EAAgB,EAChB,EAAgB,EAChB,CAAS;IAET,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChB,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;IACjB,MAAM,EAAE,GAAG,CA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AC;IACtB,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;IACtB,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAClB,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5D,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5D,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACX,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAEX,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ReadonlyVec3, Vec3 } from "@ac-kit/math-linear";
|
|
2
|
+
/**
|
|
3
|
+
* Evaluates the linear Bézier curve — the segment — from `p0` to `p1` at `t` in
|
|
4
|
+
* space: `B(t) = (1 − t)·p0 + t·p1`. `out` may be either point.
|
|
5
|
+
*
|
|
6
|
+
* @param out - Receives the point on the segment at `t`.
|
|
7
|
+
* @param p0 - Start point.
|
|
8
|
+
* @param p1 - End point.
|
|
9
|
+
* @param t - Curve parameter, normally in `[0, 1]`.
|
|
10
|
+
* @returns `out`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function linearBezier3(out: Vec3, p0: ReadonlyVec3, p1: ReadonlyVec3, t: number): Vec3;
|
|
13
|
+
/**
|
|
14
|
+
* Evaluates a quadratic Bézier curve in space at `t`, in the Bernstein form
|
|
15
|
+
* `B(t) = (1 − t)²·p0 + 2(1 − t)t·p1 + t²·p2`, as `quadraticBezier2`. Plain
|
|
16
|
+
* floating-point sums. `out` may be any of the points.
|
|
17
|
+
*
|
|
18
|
+
* @param out - Receives the point on the curve at `t`.
|
|
19
|
+
* @param p0 - Start point.
|
|
20
|
+
* @param p1 - Control point.
|
|
21
|
+
* @param p2 - End point.
|
|
22
|
+
* @param t - Curve parameter, normally in `[0, 1]`.
|
|
23
|
+
* @returns `out`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function quadraticBezier3(out: Vec3, p0: ReadonlyVec3, p1: ReadonlyVec3, p2: ReadonlyVec3, t: number): Vec3;
|
|
26
|
+
/**
|
|
27
|
+
* Evaluates a cubic Bézier curve in space at `t`: `B(t) = (1 − t)³·p0 + 3(1 −
|
|
28
|
+
* t)²t·p1 + 3(1 − t)t²·p2 + t³·p3`, as `cubicBezier2`. Plain floating-point
|
|
29
|
+
* sums. `out` may be any of the points.
|
|
30
|
+
*
|
|
31
|
+
* @param out - Receives the point on the curve at `t`.
|
|
32
|
+
* @param p0 - Start point.
|
|
33
|
+
* @param p1 - First control point.
|
|
34
|
+
* @param p2 - Second control point.
|
|
35
|
+
* @param p3 - End point.
|
|
36
|
+
* @param t - Curve parameter, normally in `[0, 1]`.
|
|
37
|
+
* @returns `out`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function cubicBezier3(out: Vec3, p0: ReadonlyVec3, p1: ReadonlyVec3, p2: ReadonlyVec3, p3: ReadonlyVec3, t: number): Vec3;
|
|
40
|
+
//# sourceMappingURL=bezier3.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bezier3.d.ts","sourceRoot":"","sources":["../src/bezier3.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAE9D;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAC5B,GAAG,EAAE,IAAI,EACT,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,CAAC,EAAE,MAAM,GACP,IAAI,CAUN;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAC/B,GAAG,EAAE,IAAI,EACT,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,CAAC,EAAE,MAAM,GACP,IAAI,CAaN;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAC3B,GAAG,EAAE,IAAI,EACT,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,CAAC,EAAE,MAAM,GACP,IAAI,CAgBN"}
|
package/dist/bezier3.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evaluates the linear Bézier curve — the segment — from `p0` to `p1` at `t` in
|
|
3
|
+
* space: `B(t) = (1 − t)·p0 + t·p1`. `out` may be either point.
|
|
4
|
+
*
|
|
5
|
+
* @param out - Receives the point on the segment at `t`.
|
|
6
|
+
* @param p0 - Start point.
|
|
7
|
+
* @param p1 - End point.
|
|
8
|
+
* @param t - Curve parameter, normally in `[0, 1]`.
|
|
9
|
+
* @returns `out`.
|
|
10
|
+
*/
|
|
11
|
+
export function linearBezier3(out, p0, p1, t) {
|
|
12
|
+
const u = 1 - t;
|
|
13
|
+
const x = u * p0[0] + t * p1[0];
|
|
14
|
+
const y = u * p0[1] + t * p1[1];
|
|
15
|
+
const z = u * p0[2] + t * p1[2];
|
|
16
|
+
out[0] = x;
|
|
17
|
+
out[1] = y;
|
|
18
|
+
out[2] = z;
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Evaluates a quadratic Bézier curve in space at `t`, in the Bernstein form
|
|
23
|
+
* `B(t) = (1 − t)²·p0 + 2(1 − t)t·p1 + t²·p2`, as `quadraticBezier2`. Plain
|
|
24
|
+
* floating-point sums. `out` may be any of the points.
|
|
25
|
+
*
|
|
26
|
+
* @param out - Receives the point on the curve at `t`.
|
|
27
|
+
* @param p0 - Start point.
|
|
28
|
+
* @param p1 - Control point.
|
|
29
|
+
* @param p2 - End point.
|
|
30
|
+
* @param t - Curve parameter, normally in `[0, 1]`.
|
|
31
|
+
* @returns `out`.
|
|
32
|
+
*/
|
|
33
|
+
export function quadraticBezier3(out, p0, p1, p2, t) {
|
|
34
|
+
const u = 1 - t;
|
|
35
|
+
const w0 = u * u;
|
|
36
|
+
const w1 = 2 * u * t;
|
|
37
|
+
const w2 = t * t;
|
|
38
|
+
const x = w0 * p0[0] + w1 * p1[0] + w2 * p2[0];
|
|
39
|
+
const y = w0 * p0[1] + w1 * p1[1] + w2 * p2[1];
|
|
40
|
+
const z = w0 * p0[2] + w1 * p1[2] + w2 * p2[2];
|
|
41
|
+
out[0] = x;
|
|
42
|
+
out[1] = y;
|
|
43
|
+
out[2] = z;
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Evaluates a cubic Bézier curve in space at `t`: `B(t) = (1 − t)³·p0 + 3(1 −
|
|
48
|
+
* t)²t·p1 + 3(1 − t)t²·p2 + t³·p3`, as `cubicBezier2`. Plain floating-point
|
|
49
|
+
* sums. `out` may be any of the points.
|
|
50
|
+
*
|
|
51
|
+
* @param out - Receives the point on the curve at `t`.
|
|
52
|
+
* @param p0 - Start point.
|
|
53
|
+
* @param p1 - First control point.
|
|
54
|
+
* @param p2 - Second control point.
|
|
55
|
+
* @param p3 - End point.
|
|
56
|
+
* @param t - Curve parameter, normally in `[0, 1]`.
|
|
57
|
+
* @returns `out`.
|
|
58
|
+
*/
|
|
59
|
+
export function cubicBezier3(out, p0, p1, p2, p3, t) {
|
|
60
|
+
const u = 1 - t;
|
|
61
|
+
const uu = u * u;
|
|
62
|
+
const tt = t * t;
|
|
63
|
+
const w0 = uu * u;
|
|
64
|
+
const w1 = 3 * uu * t;
|
|
65
|
+
const w2 = 3 * u * tt;
|
|
66
|
+
const w3 = tt * t;
|
|
67
|
+
const x = w0 * p0[0] + w1 * p1[0] + w2 * p2[0] + w3 * p3[0];
|
|
68
|
+
const y = w0 * p0[1] + w1 * p1[1] + w2 * p2[1] + w3 * p3[1];
|
|
69
|
+
const z = w0 * p0[2] + w1 * p1[2] + w2 * p2[2] + w3 * p3[2];
|
|
70
|
+
out[0] = x;
|
|
71
|
+
out[1] = y;
|
|
72
|
+
out[2] = z;
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=bezier3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bezier3.js","sourceRoot":"","sources":["../src/bezier3.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,UAAU,aAAa,CAC5B,GAAS,EACT,EAAgB,EAChB,EAAgB,EAChB,CAAS;IAET,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAChB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAChC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAChC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAChC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACX,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACX,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAEX,OAAO,GAAG,CAAC;AACZ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,gBAAgB,CAC/B,GAAS,EACT,EAAgB,EAChB,EAAgB,EAChB,EAAgB,EAChB,CAAS;IAET,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACh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,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAClB,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5D,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5D,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5D,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACX,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACX,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAEX,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ParametricCurve } from "./parametric-curve.js";
|
|
2
|
+
/** Options of {@link catmullRomCurve}. */
|
|
3
|
+
export type CatmullRomOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* The knot exponent `α ∈ [0, 1]`: knot intervals `|Pᵢ₊₁ − Pᵢ|^α`. `0` is the
|
|
6
|
+
* uniform spline, `0.5` the centripetal one (the default), `1` the chordal
|
|
7
|
+
* one. Only the centripetal spline is guaranteed free of cusps and
|
|
8
|
+
* self-intersections within a span (Yuksel, Schaefer and Keyser, 2011), which
|
|
9
|
+
* the uniform one forms on unevenly spaced points.
|
|
10
|
+
*/
|
|
11
|
+
readonly alpha?: number;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The Catmull–Rom spline through `points` as a {@link ParametricCurve}: a
|
|
15
|
+
* piecewise cubic interpolating every point, its tangents set by the knot
|
|
16
|
+
* exponent `alpha` (centripetal by default), the missing neighbours at the ends
|
|
17
|
+
* reflected (`2P₀ − P₁`). `t ∈ [0, 1]` is the knot parameter normalised, so
|
|
18
|
+
* span `i` covers the share of `t` its knot interval `|Pᵢ₊₁ − Pᵢ|^α` has of
|
|
19
|
+
* their sum, and the curve is `C¹` in `t` (the joins are its `breaks`); with
|
|
20
|
+
* `alpha = 0` the spans share `t` evenly. Repeated points make an empty span; a
|
|
21
|
+
* single point, or only repeated ones, a constant curve. The points are copied
|
|
22
|
+
* and the tangents computed once; an evaluation is O(log n + dimension).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const curve = catmullRomCurve(Float64Array.of(0, 0, 1, 2, 4, 2, 5, 0), 2);
|
|
27
|
+
* const point = new Float64Array(2);
|
|
28
|
+
* curve.point(point, 0, 0.5); // a point between (1, 2) and (4, 2)
|
|
29
|
+
* ```;
|
|
30
|
+
*
|
|
31
|
+
* @param points - The `n ≥ 1` points, `dimension` numbers each.
|
|
32
|
+
* @param dimension - Coordinates per point, a positive integer.
|
|
33
|
+
* @param options - The knot exponent.
|
|
34
|
+
* @throws {RangeError} When `dimension` is not a positive integer, `points` is
|
|
35
|
+
* empty or not a multiple of `dimension` long, or `alpha` is outside `[0,
|
|
36
|
+
* 1]`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function catmullRomCurve(points: Float64Array, dimension: number, options?: CatmullRomOptions): ParametricCurve;
|
|
39
|
+
//# sourceMappingURL=catmull-rom-curve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catmull-rom-curve.d.ts","sourceRoot":"","sources":["../src/catmull-rom-curve.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D,0CAA0C;AAC1C,MAAM,MAAM,iBAAiB,GAAG;IAC/B;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,eAAe,CAC9B,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,iBAAiB,GACzB,eAAe,CA4GjB"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { bisectRightNumbers } from "@ac-kit/algo";
|
|
2
|
+
import { assertInteger } from "@ac-kit/core";
|
|
3
|
+
import { catmullRomTangents_, checkAlpha_ } from "./_catmull-rom.js";
|
|
4
|
+
/**
|
|
5
|
+
* The Catmull–Rom spline through `points` as a {@link ParametricCurve}: a
|
|
6
|
+
* piecewise cubic interpolating every point, its tangents set by the knot
|
|
7
|
+
* exponent `alpha` (centripetal by default), the missing neighbours at the ends
|
|
8
|
+
* reflected (`2P₀ − P₁`). `t ∈ [0, 1]` is the knot parameter normalised, so
|
|
9
|
+
* span `i` covers the share of `t` its knot interval `|Pᵢ₊₁ − Pᵢ|^α` has of
|
|
10
|
+
* their sum, and the curve is `C¹` in `t` (the joins are its `breaks`); with
|
|
11
|
+
* `alpha = 0` the spans share `t` evenly. Repeated points make an empty span; a
|
|
12
|
+
* single point, or only repeated ones, a constant curve. The points are copied
|
|
13
|
+
* and the tangents computed once; an evaluation is O(log n + dimension).
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const curve = catmullRomCurve(Float64Array.of(0, 0, 1, 2, 4, 2, 5, 0), 2);
|
|
18
|
+
* const point = new Float64Array(2);
|
|
19
|
+
* curve.point(point, 0, 0.5); // a point between (1, 2) and (4, 2)
|
|
20
|
+
* ```;
|
|
21
|
+
*
|
|
22
|
+
* @param points - The `n ≥ 1` points, `dimension` numbers each.
|
|
23
|
+
* @param dimension - Coordinates per point, a positive integer.
|
|
24
|
+
* @param options - The knot exponent.
|
|
25
|
+
* @throws {RangeError} When `dimension` is not a positive integer, `points` is
|
|
26
|
+
* empty or not a multiple of `dimension` long, or `alpha` is outside `[0,
|
|
27
|
+
* 1]`.
|
|
28
|
+
*/
|
|
29
|
+
export function catmullRomCurve(points, dimension, options) {
|
|
30
|
+
assertInteger(dimension, "dimension", 1);
|
|
31
|
+
if (points.length === 0 || points.length % dimension !== 0) {
|
|
32
|
+
throw new RangeError(`catmullRomCurve: points must hold a positive multiple of ${dimension} numbers, got ${points.length}.`);
|
|
33
|
+
}
|
|
34
|
+
const alpha = options?.alpha ?? 0.5;
|
|
35
|
+
checkAlpha_("catmullRomCurve", alpha);
|
|
36
|
+
const count = points.length / dimension;
|
|
37
|
+
const spans = count - 1;
|
|
38
|
+
const copy = points.slice();
|
|
39
|
+
const tangents = new Float64Array(2 * dimension * spans);
|
|
40
|
+
// knots[i]: the knot parameter of point i, from 0.
|
|
41
|
+
const knots = new Float64Array(count);
|
|
42
|
+
for (let span = 0; span < spans; span++) {
|
|
43
|
+
catmullRomTangents_(tangents, 2 * dimension * span, copy, dimension, count, span, alpha);
|
|
44
|
+
let squared = 0;
|
|
45
|
+
for (let axis = 0; axis < dimension; axis++) {
|
|
46
|
+
const step = copy[(span + 1) * dimension + axis] - copy[span * dimension + axis];
|
|
47
|
+
squared += step * step;
|
|
48
|
+
}
|
|
49
|
+
knots[span + 1] = knots[span] + Math.sqrt(squared) ** alpha;
|
|
50
|
+
}
|
|
51
|
+
const total = knots[spans];
|
|
52
|
+
const interior = [];
|
|
53
|
+
for (let index = 1; index < spans; index++) {
|
|
54
|
+
const join = knots[index] / total;
|
|
55
|
+
if (join > 0 && join < 1 && join !== interior.at(-1)) {
|
|
56
|
+
interior.push(join);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const breaks = Float64Array.from(interior);
|
|
60
|
+
// The span holding t; its local parameter and knot interval go to local_.
|
|
61
|
+
const locate = (t) => {
|
|
62
|
+
const knot = t * total;
|
|
63
|
+
let span = Math.min(Math.max(bisectRightNumbers(knots, knot) - 1, 0), spans - 1);
|
|
64
|
+
while (span > 0 && knots[span + 1] === knots[span]) {
|
|
65
|
+
span--;
|
|
66
|
+
}
|
|
67
|
+
const width = knots[span + 1] - knots[span];
|
|
68
|
+
local_[0] = width > 0 ? (knot - knots[span]) / width : 0;
|
|
69
|
+
local_[1] = width;
|
|
70
|
+
return span;
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
dimension,
|
|
74
|
+
point: (out, offset, t) => {
|
|
75
|
+
if (!(total > 0)) {
|
|
76
|
+
out.set(copy.subarray(0, dimension), offset);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const span = locate(t);
|
|
80
|
+
const u = local_[0];
|
|
81
|
+
const uu = u * u;
|
|
82
|
+
const h10 = u * (1 - u) * (1 - u);
|
|
83
|
+
const h01 = uu * (3 - 2 * u);
|
|
84
|
+
const h11 = uu * (u - 1);
|
|
85
|
+
const p1 = span * dimension;
|
|
86
|
+
const m = 2 * dimension * span;
|
|
87
|
+
for (let axis = 0; axis < dimension; axis++) {
|
|
88
|
+
const start = copy[p1 + axis];
|
|
89
|
+
out[offset + axis] =
|
|
90
|
+
start +
|
|
91
|
+
h01 * (copy[p1 + dimension + axis] - start) +
|
|
92
|
+
h10 * tangents[m + axis] +
|
|
93
|
+
h11 * tangents[m + dimension + axis];
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
derivative: (out, offset, t) => {
|
|
97
|
+
if (!(total > 0)) {
|
|
98
|
+
out.fill(0, offset, offset + dimension);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const span = locate(t);
|
|
102
|
+
const u = local_[0];
|
|
103
|
+
const width = local_[1];
|
|
104
|
+
// dC/dt = (total / width) · dH/du.
|
|
105
|
+
const scale = total / width;
|
|
106
|
+
const d01 = 6 * u * (1 - u);
|
|
107
|
+
const d10 = (1 - u) * (1 - 3 * u);
|
|
108
|
+
const d11 = u * (3 * u - 2);
|
|
109
|
+
const p1 = span * dimension;
|
|
110
|
+
const m = 2 * dimension * span;
|
|
111
|
+
for (let axis = 0; axis < dimension; axis++) {
|
|
112
|
+
out[offset + axis] =
|
|
113
|
+
scale *
|
|
114
|
+
(d01 * (copy[p1 + dimension + axis] - copy[p1 + axis]) +
|
|
115
|
+
d10 * tangents[m + axis] +
|
|
116
|
+
d11 * tangents[m + dimension + axis]);
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
breaks,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const local_ = new Float64Array(2);
|
|
123
|
+
//# sourceMappingURL=catmull-rom-curve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catmull-rom-curve.js","sourceRoot":"","sources":["../src/catmull-rom-curve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAerE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,eAAe,CAC9B,MAAoB,EACpB,SAAiB,EACjB,OAA2B;IAE3B,aAAa,CAAC,SAAS,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC;IACzC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,SAAS,KAAK,CAAC,EAAE,CAAC;QAC5D,MAAM,IAAI,UAAU,CACnB,4DAA4D,SAAS,iBAAiB,MAAM,CAAC,MAAM,GAAG,CACtG,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,EAAE,KAAK,IAAI,GAAG,CAAC;IACpC,WAAW,CAAC,iBAAiB,EAAE,KAAK,CAAC,CA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AM,MAAM,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CSS `cubic-bezier(x1, y1, x2, y2)` timing function (CSS Easing 1 §2.3):
|
|
3
|
+
* the cubic Bézier from `(0, 0)` to `(1, 1)` with control points `(x1, y1)` and
|
|
4
|
+
* `(x2, y2)`, read as `y` against `x` — `ease` is `(0.25, 0.1, 0.25, 1)`,
|
|
5
|
+
* `ease-in-out` `(0.42, 0, 0.58, 1)`.
|
|
6
|
+
*
|
|
7
|
+
* `x(t)` is inverted numerically: a table of 11 samples of `x`, computed once
|
|
8
|
+
* per curve when the function is made, gives the starting parameter, Newton's
|
|
9
|
+
* method refines it while the slope is usable, and bisection finishes
|
|
10
|
+
* otherwise, to within `1e-12` in `x` — far below a frame. Outside `[0, 1]` the
|
|
11
|
+
* output extends along the end tangents, as the specification has it for inputs
|
|
12
|
+
* a `steps()` or a spring may produce. `y1` and `y2` may leave `[0, 1]`
|
|
13
|
+
* (overshoot), `x1` and `x2` may not, which keeps `x(t)` monotone.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const ease = cubicBezierEasing(0.25, 0.1, 0.25, 1);
|
|
18
|
+
* ease(0.5); // ≈ 0.8024
|
|
19
|
+
* cubicBezierEasing(0, 0, 1, 1)(0.3); // 0.3 — linear
|
|
20
|
+
* ```;
|
|
21
|
+
*
|
|
22
|
+
* @returns The timing function, `x ↦ y`.
|
|
23
|
+
* @throws {RangeError} When `x1` or `x2` is outside `[0, 1]`, or a coordinate
|
|
24
|
+
* is not finite.
|
|
25
|
+
*/
|
|
26
|
+
export declare function cubicBezierEasing(x1: number, y1: number, x2: number, y2: number): (x: number) => number;
|
|
27
|
+
//# sourceMappingURL=cubic-bezier-easing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cubic-bezier-easing.d.ts","sourceRoot":"","sources":["../src/cubic-bezier-easing.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,iBAAiB,CAChC,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,GACR,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAkFvB"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
/** Samples of the `x` component per curve, for the initial guess. */
|
|
3
|
+
const SAMPLES_ = 11;
|
|
4
|
+
/**
|
|
5
|
+
* The CSS `cubic-bezier(x1, y1, x2, y2)` timing function (CSS Easing 1 §2.3):
|
|
6
|
+
* the cubic Bézier from `(0, 0)` to `(1, 1)` with control points `(x1, y1)` and
|
|
7
|
+
* `(x2, y2)`, read as `y` against `x` — `ease` is `(0.25, 0.1, 0.25, 1)`,
|
|
8
|
+
* `ease-in-out` `(0.42, 0, 0.58, 1)`.
|
|
9
|
+
*
|
|
10
|
+
* `x(t)` is inverted numerically: a table of 11 samples of `x`, computed once
|
|
11
|
+
* per curve when the function is made, gives the starting parameter, Newton's
|
|
12
|
+
* method refines it while the slope is usable, and bisection finishes
|
|
13
|
+
* otherwise, to within `1e-12` in `x` — far below a frame. Outside `[0, 1]` the
|
|
14
|
+
* output extends along the end tangents, as the specification has it for inputs
|
|
15
|
+
* a `steps()` or a spring may produce. `y1` and `y2` may leave `[0, 1]`
|
|
16
|
+
* (overshoot), `x1` and `x2` may not, which keeps `x(t)` monotone.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const ease = cubicBezierEasing(0.25, 0.1, 0.25, 1);
|
|
21
|
+
* ease(0.5); // ≈ 0.8024
|
|
22
|
+
* cubicBezierEasing(0, 0, 1, 1)(0.3); // 0.3 — linear
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @returns The timing function, `x ↦ y`.
|
|
26
|
+
* @throws {RangeError} When `x1` or `x2` is outside `[0, 1]`, or a coordinate
|
|
27
|
+
* is not finite.
|
|
28
|
+
*/
|
|
29
|
+
export function cubicBezierEasing(x1, y1, x2, y2) {
|
|
30
|
+
if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1) ||
|
|
31
|
+
y1 - y1 !== 0 ||
|
|
32
|
+
y2 - y2 !== 0) {
|
|
33
|
+
throw new RangeError(`cubicBezierEasing: x1 and x2 must be in [0, 1] and y1, y2 finite, got (${describeValue(x1)}, ${describeValue(y1)}, ${describeValue(x2)}, ${describeValue(y2)})`);
|
|
34
|
+
}
|
|
35
|
+
// Power-basis coefficients of B(t) = ((a·t + b)·t + c)·t.
|
|
36
|
+
const cx = 3 * x1;
|
|
37
|
+
const bx = 3 * (x2 - x1) - cx;
|
|
38
|
+
const ax = 1 - cx - bx;
|
|
39
|
+
const cy = 3 * y1;
|
|
40
|
+
const by = 3 * (y2 - y1) - cy;
|
|
41
|
+
const ay = 1 - cy - by;
|
|
42
|
+
const curveX = (t) => ((ax * t + bx) * t + cx) * t;
|
|
43
|
+
const curveY = (t) => ((ay * t + by) * t + cy) * t;
|
|
44
|
+
const slopeX = (t) => (3 * ax * t + 2 * bx) * t + cx;
|
|
45
|
+
const table = new Float64Array(SAMPLES_);
|
|
46
|
+
for (let index = 0; index < SAMPLES_; index++) {
|
|
47
|
+
table[index] = curveX(index / (SAMPLES_ - 1));
|
|
48
|
+
}
|
|
49
|
+
// CSS Easing §2.3: the tangent at each end, from the first control point
|
|
50
|
+
// that is not on the end.
|
|
51
|
+
const startSlope = x1 > 0 ? y1 / x1 : y1 === 0 && x2 > 0 ? y2 / x2 : 0;
|
|
52
|
+
const endSlope = x2 < 1 ? (y2 - 1) / (x2 - 1) : y2 === 1 && x1 < 1 ? (y1 - 1) / (x1 - 1) : 0;
|
|
53
|
+
const solve = (x) => {
|
|
54
|
+
let interval = 0;
|
|
55
|
+
while (interval < SAMPLES_ - 2 && table[interval + 1] <= x) {
|
|
56
|
+
interval++;
|
|
57
|
+
}
|
|
58
|
+
const low = table[interval];
|
|
59
|
+
const high = table[interval + 1];
|
|
60
|
+
const step = 1 / (SAMPLES_ - 1);
|
|
61
|
+
let t = (interval + (high > low ? (x - low) / (high - low) : 0)) * step;
|
|
62
|
+
for (let iteration = 0; iteration < 8; iteration++) {
|
|
63
|
+
const error = curveX(t) - x;
|
|
64
|
+
if (Math.abs(error) <= 1e-12) {
|
|
65
|
+
return t;
|
|
66
|
+
}
|
|
67
|
+
const slope = slopeX(t);
|
|
68
|
+
if (Math.abs(slope) < 1e-6) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
t -= error / slope;
|
|
72
|
+
}
|
|
73
|
+
let lower = 0;
|
|
74
|
+
let upper = 1;
|
|
75
|
+
t = Math.min(Math.max(t, 0), 1);
|
|
76
|
+
for (let iteration = 0; iteration < 60; iteration++) {
|
|
77
|
+
const value = curveX(t);
|
|
78
|
+
if (Math.abs(value - x) <= 1e-12) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
if (value < x) {
|
|
82
|
+
lower = t;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
upper = t;
|
|
86
|
+
}
|
|
87
|
+
t = (lower + upper) / 2;
|
|
88
|
+
}
|
|
89
|
+
return t;
|
|
90
|
+
};
|
|
91
|
+
return (x) => {
|
|
92
|
+
if (x !== x) {
|
|
93
|
+
return Number.NaN;
|
|
94
|
+
}
|
|
95
|
+
if (x <= 0) {
|
|
96
|
+
return x === 0 ? 0 : startSlope * x;
|
|
97
|
+
}
|
|
98
|
+
if (x >= 1) {
|
|
99
|
+
return x === 1 ? 1 : 1 + endSlope * (x - 1);
|
|
100
|
+
}
|
|
101
|
+
return curveY(solve(x));
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=cubic-bezier-easing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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E,SAAS,EAAE,EAAE,CAAC;YACrD,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YACxB,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,IAAI,KAAK,EAAE,CAAC;gBAClC,MAAM;YACP,CAAC;YACD,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACf,KAAK,GAAG,CAAC,CAAC;YACX,CAAC;iBAAM,CAAC;gBACP,KAAK,GAAG,CAAC,CAAC;YACX,CAAC;YACD,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC;QACD,OAAO,CAAC,CAAC;IACV,CAAC,CAAC;IAEF,OAAO,CAAC,CAAS,EAAU,EAAE;QAC5B,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACb,OAAO,MAAM,CAAC,GAAG,CAAC;QACnB,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YACZ,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YACZ,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACzB,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ParametricCurve } from "./parametric-curve.js";
|
|
2
|
+
/**
|
|
3
|
+
* Flattens a curve, or a path of curves joined end to end, into a polyline
|
|
4
|
+
* within `tolerance` of it: adaptive subdivision of each smooth piece (the
|
|
5
|
+
* spans between the curve's `breaks`, each first cut into four chords), a chord
|
|
6
|
+
* being halved until the curve at a quarter, half and three quarters of its
|
|
7
|
+
* interval lies within `tolerance` of it. Works on any {@link ParametricCurve};
|
|
8
|
+
* the Bézier segments of a 2D path have the tighter, bounded
|
|
9
|
+
* `quadraticBezier2Flatten` and `cubicBezier2Flatten`.
|
|
10
|
+
*
|
|
11
|
+
* Consecutive curves of a path share their join: the first point of a curve is
|
|
12
|
+
* left out when it equals the last one written.
|
|
13
|
+
*
|
|
14
|
+
* O(chords · evaluation) time; the polyline is a new array.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const polyline = curveToPolyline(
|
|
19
|
+
* [bezierCurve(Float64Array.of(0, 0, 1, 2, 2, 0), 2), catmullRomCurve(points, 2)],
|
|
20
|
+
* 0.01,
|
|
21
|
+
* );
|
|
22
|
+
* ```;
|
|
23
|
+
*
|
|
24
|
+
* @param curves - A curve or the curves of a path, all of one dimension.
|
|
25
|
+
* @param tolerance - The largest distance from a chord to the curve probed, a
|
|
26
|
+
* positive number; `Infinity` gives the initial chords.
|
|
27
|
+
* @returns The vertices, `dimension` numbers each, from `C(0)` of the first
|
|
28
|
+
* curve to `C(1)` of the last.
|
|
29
|
+
* @throws {RangeError} When `tolerance` is not positive, or the curves differ
|
|
30
|
+
* in dimension.
|
|
31
|
+
*/
|
|
32
|
+
export declare function curveToPolyline(curves: ParametricCurve | readonly ParametricCurve[], tolerance: number): Float64Array;
|
|
33
|
+
//# sourceMappingURL=curve-to-polyline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"curve-to-polyline.d.ts","sourceRoot":"","sources":["../src/curve-to-polyline.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAW7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,eAAe,CAC9B,MAAM,EAAE,eAAe,GAAG,SAAS,eAAe,EAAE,EACpD,SAAS,EAAE,MAAM,GACf,YAAY,CA4Ed"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
/** Chords each smooth piece starts with, so a loop or an S is never skipped. */
|
|
3
|
+
const INITIAL_CHORDS_ = 4;
|
|
4
|
+
/** Halvings after which an interval is accepted whatever its deviation. */
|
|
5
|
+
const MAX_DEPTH_ = 40;
|
|
6
|
+
/** Where a chord's deviation is probed, as fractions of its interval. */
|
|
7
|
+
const PROBES_ = Float64Array.of(0.25, 0.5, 0.75);
|
|
8
|
+
/**
|
|
9
|
+
* Flattens a curve, or a path of curves joined end to end, into a polyline
|
|
10
|
+
* within `tolerance` of it: adaptive subdivision of each smooth piece (the
|
|
11
|
+
* spans between the curve's `breaks`, each first cut into four chords), a chord
|
|
12
|
+
* being halved until the curve at a quarter, half and three quarters of its
|
|
13
|
+
* interval lies within `tolerance` of it. Works on any {@link ParametricCurve};
|
|
14
|
+
* the Bézier segments of a 2D path have the tighter, bounded
|
|
15
|
+
* `quadraticBezier2Flatten` and `cubicBezier2Flatten`.
|
|
16
|
+
*
|
|
17
|
+
* Consecutive curves of a path share their join: the first point of a curve is
|
|
18
|
+
* left out when it equals the last one written.
|
|
19
|
+
*
|
|
20
|
+
* O(chords · evaluation) time; the polyline is a new array.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const polyline = curveToPolyline(
|
|
25
|
+
* [bezierCurve(Float64Array.of(0, 0, 1, 2, 2, 0), 2), catmullRomCurve(points, 2)],
|
|
26
|
+
* 0.01,
|
|
27
|
+
* );
|
|
28
|
+
* ```;
|
|
29
|
+
*
|
|
30
|
+
* @param curves - A curve or the curves of a path, all of one dimension.
|
|
31
|
+
* @param tolerance - The largest distance from a chord to the curve probed, a
|
|
32
|
+
* positive number; `Infinity` gives the initial chords.
|
|
33
|
+
* @returns The vertices, `dimension` numbers each, from `C(0)` of the first
|
|
34
|
+
* curve to `C(1)` of the last.
|
|
35
|
+
* @throws {RangeError} When `tolerance` is not positive, or the curves differ
|
|
36
|
+
* in dimension.
|
|
37
|
+
*/
|
|
38
|
+
export function curveToPolyline(curves, tolerance) {
|
|
39
|
+
if (!(tolerance > 0)) {
|
|
40
|
+
throw new RangeError(`curveToPolyline: tolerance must be positive, got ${describeValue(tolerance)}.`);
|
|
41
|
+
}
|
|
42
|
+
const list = "dimension" in curves ? [curves] : curves;
|
|
43
|
+
if (list.length === 0) {
|
|
44
|
+
return new Float64Array(0);
|
|
45
|
+
}
|
|
46
|
+
const dimension = list[0].dimension;
|
|
47
|
+
for (const curve of list) {
|
|
48
|
+
if (curve.dimension !== dimension) {
|
|
49
|
+
throw new RangeError(`curveToPolyline: every curve must have dimension ${dimension}, got ${curve.dimension}.`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const vertices = [];
|
|
53
|
+
const point = new Float64Array(dimension);
|
|
54
|
+
const start = new Float64Array(dimension);
|
|
55
|
+
const end = new Float64Array(dimension);
|
|
56
|
+
const limit = tolerance * tolerance;
|
|
57
|
+
// Intervals still to flatten, [from, to, depth], the next one on top.
|
|
58
|
+
const stack = [];
|
|
59
|
+
for (const curve of list) {
|
|
60
|
+
curve.point(point, 0, 0);
|
|
61
|
+
const count = vertices.length;
|
|
62
|
+
let joined = count >= dimension;
|
|
63
|
+
for (let axis = 0; axis < dimension && joined; axis++) {
|
|
64
|
+
joined = vertices[count - dimension + axis] === point[axis];
|
|
65
|
+
}
|
|
66
|
+
if (!joined) {
|
|
67
|
+
vertices.push(...point);
|
|
68
|
+
}
|
|
69
|
+
const breaks = curve.breaks;
|
|
70
|
+
for (let piece = breaks.length; piece >= 0; piece--) {
|
|
71
|
+
const from = piece === 0 ? 0 : breaks[piece - 1];
|
|
72
|
+
const to = piece === breaks.length ? 1 : breaks[piece];
|
|
73
|
+
for (let chord = INITIAL_CHORDS_ - 1; chord >= 0; chord--) {
|
|
74
|
+
stack.push(from + ((to - from) * chord) / INITIAL_CHORDS_, chord === INITIAL_CHORDS_ - 1
|
|
75
|
+
? to
|
|
76
|
+
: from + ((to - from) * (chord + 1)) / INITIAL_CHORDS_, 0);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
while (stack.length > 0) {
|
|
80
|
+
const depth = stack.pop();
|
|
81
|
+
const to = stack.pop();
|
|
82
|
+
const from = stack.pop();
|
|
83
|
+
curve.point(start, 0, from);
|
|
84
|
+
curve.point(end, 0, to);
|
|
85
|
+
let flat = depth >= MAX_DEPTH_ || tolerance === Infinity;
|
|
86
|
+
for (let probe = 0; probe < PROBES_.length && !flat; probe++) {
|
|
87
|
+
curve.point(point, 0, from + (to - from) * PROBES_[probe]);
|
|
88
|
+
if (!(segmentDistanceSquared_(point, start, end, dimension) <= limit)) {
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
flat = probe === PROBES_.length - 1;
|
|
92
|
+
}
|
|
93
|
+
if (flat) {
|
|
94
|
+
vertices.push(...end);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
const middle = from / 2 + to / 2;
|
|
98
|
+
stack.push(middle, to, depth + 1, from, middle, depth + 1);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return Float64Array.from(vertices);
|
|
103
|
+
}
|
|
104
|
+
function segmentDistanceSquared_(point, start, end, dimension) {
|
|
105
|
+
let along = 0;
|
|
106
|
+
let projection = 0;
|
|
107
|
+
for (let axis = 0; axis < dimension; axis++) {
|
|
108
|
+
const step = end[axis] - start[axis];
|
|
109
|
+
along += step * step;
|
|
110
|
+
projection += (point[axis] - start[axis]) * step;
|
|
111
|
+
}
|
|
112
|
+
const t = along > 0 ? Math.min(1, Math.max(0, projection / along)) : 0;
|
|
113
|
+
let squared = 0;
|
|
114
|
+
for (let axis = 0; axis < dimension; axis++) {
|
|
115
|
+
const from = start[axis];
|
|
116
|
+
const deviation = point[axis] - (from + t * (end[axis] - from));
|
|
117
|
+
squared += deviation * deviation;
|
|
118
|
+
}
|
|
119
|
+
return squared;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=curve-to-polyline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"curve-to-polyline.js","sourceRoot":"","sources":["../src/curve-to-polyline.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAI7C,gFAAgF;AAChF,MAAM,eAAe,GAAG,CAAC,CAAC;AAE1B,2EAA2E;AAC3E,MAAM,UAAU,GAAG,EAAE,CAAC;AAEtB,yEAAyE;AACzE,MAAM,OAAO,GAAG,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,UAAU,eAAe,CAC9B,MAAoD,EACpD,SAAiB;IAEjB,IAAI,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,UAAU,CACnB,oDAAoD,aAAa,CAAC,SAAS,CAAC,GAAG,CAC/E,CAAC;IACH,CAAC;IACD,MAAM,IAAI,GACT,WAAW,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAC3C,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IAC5B,CAAC;IACD,MAAM,SAAS,GAAG,IAAI,CAAC,CAAC,CAAE,CAAC,SAAS,CAAC;IACrC,KAAK,MAAM,KAAK,IAAI,IAAI,EAAE,CAAC;QAC1B,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YACnC,MAAM,IAAI,UAAU,CACnB,oDAAoD,SAAS,SAAS,KAAK,CAAC,SAAS,GAAG,CACxF,CAAC;QACH,CAAC;IACF,CAAC;IACD,MAAM,QAAQ,GAAa,EAAE,CA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GAAG,CAAC,CAAC;IAChB,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC;QAC7C,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAE,CAAC;QAC1B,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAE,GAAG,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAE,GAAG,IAAI,CAAC,CAAC,CAAC;QAClE,OAAO,IAAI,SAAS,GAAG,SAAS,CAAC;IAClC,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
export * from "./arc-length.js";
|
|
2
|
+
export * from "./bezier-n.js";
|
|
1
3
|
export * from "./bezier2.js";
|
|
2
4
|
export * from "./bezier2-flatten.js";
|
|
5
|
+
export * from "./bezier3.js";
|
|
6
|
+
export * from "./catmull-rom-curve.js";
|
|
7
|
+
export * from "./cubic-bezier-easing.js";
|
|
8
|
+
export * from "./curve-to-polyline.js";
|
|
3
9
|
export * from "./hermite2.js";
|
|
10
|
+
export * from "./parametric-curve.js";
|
|
4
11
|
export * from "./spline2.js";
|
|
5
12
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,cAAc,CAAC;AAC7B,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,wBAAwB,CAAC;AACvC,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC"}
|