@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/index.js
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.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","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.js","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"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Writes the `dimension` coordinates of a curve's point, or derivative, at the
|
|
4
|
+
* parameter `t` into `out[offset … offset + dimension)`.
|
|
5
|
+
*/
|
|
6
|
+
export type CurveEvaluator = Callable<[
|
|
7
|
+
out: Float64Array,
|
|
8
|
+
offset: number,
|
|
9
|
+
t: number
|
|
10
|
+
], void>;
|
|
11
|
+
/**
|
|
12
|
+
* A parametric curve `C(t)` over `t ∈ [0, 1]` in `dimension` coordinates: the
|
|
13
|
+
* shape `curveArcLength`, `curveArcLengthTable` and `curveToPolyline` work on,
|
|
14
|
+
* built by `bezierCurve` and `catmullRomCurve` or by hand for any other
|
|
15
|
+
* family.
|
|
16
|
+
*/
|
|
17
|
+
export type ParametricCurve = {
|
|
18
|
+
/** Coordinates per point: 2 for the plane, 3 for space. */
|
|
19
|
+
readonly dimension: number;
|
|
20
|
+
/** Writes `C(t)`. */
|
|
21
|
+
readonly point: CurveEvaluator;
|
|
22
|
+
/** Writes `C′(t)`, the derivative with respect to `t`. */
|
|
23
|
+
readonly derivative: CurveEvaluator;
|
|
24
|
+
/**
|
|
25
|
+
* The parameters in `(0, 1)`, ascending, where the curve is only piecewise
|
|
26
|
+
* smooth (the joins of a spline's spans): quadrature and flattening split
|
|
27
|
+
* there. Empty for a curve smooth over `[0, 1]`.
|
|
28
|
+
*/
|
|
29
|
+
readonly breaks: Float64Array;
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=parametric-curve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parametric-curve.d.ts","sourceRoot":"","sources":["../src/parametric-curve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,QAAQ,CACpC;IAAC,GAAG,EAAE,YAAY;IAAE,MAAM,EAAE,MAAM;IAAE,CAAC,EAAE,MAAM;CAAC,EAC9C,IAAI,CACJ,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,eAAe,GAAG;IAC7B,2DAA2D;IAC3D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,qBAAqB;IACrB,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAE/B,0DAA0D;IAC1D,QAAQ,CAAC,UAAU,EAAE,cAAc,CAAC;IAEpC;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,YAAY,CAAC;CAC9B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parametric-curve.js","sourceRoot":"","sources":["../src/parametric-curve.ts"],"names":[],"mappings":""}
|
package/dist/spline2.d.ts
CHANGED
|
@@ -16,11 +16,14 @@
|
|
|
16
16
|
* curve; steps whose ratio leaves [1e−150, 1e150] (a subnormal step beside a
|
|
17
17
|
* unit one) can overflow a slope.
|
|
18
18
|
*
|
|
19
|
-
* Complexity: O(n · samples) time,
|
|
19
|
+
* Complexity: O(n · samples) time, no allocation: the tangents are kept in
|
|
20
|
+
* `out` until the samples replace them (see `out` for the overlapping case).
|
|
20
21
|
*
|
|
21
22
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
22
|
-
* and the rest is left untouched. May share memory with `points
|
|
23
|
-
* are then copied
|
|
23
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
24
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
25
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
26
|
+
* (the only allocation).
|
|
24
27
|
* @param points - N points, 2 numbers (x, y) each.
|
|
25
28
|
* @param samples - Points per span, a positive integer.
|
|
26
29
|
* @returns `out`.
|
|
@@ -32,16 +35,20 @@ export declare function monotoneCubicSpline2<T extends Float64Array>(out: T, poi
|
|
|
32
35
|
* Samples the natural cubic spline through the points: the `C²` piecewise cubic
|
|
33
36
|
* that interpolates every point with zero second derivative at both ends, as a
|
|
34
37
|
* parametric curve `(x(s), y(s))` with the points at integer parameters (each
|
|
35
|
-
* coordinate is solved separately, the moments by
|
|
36
|
-
*
|
|
37
|
-
* points need not be sorted. It overshoots between points where the data is
|
|
38
|
-
* smooth; use {@link monotoneCubicSpline2} to forbid that.
|
|
38
|
+
* coordinate is solved separately, the moments by the Thomas algorithm on the
|
|
39
|
+
* spline's (1, 4, 1) system; e.g. Burden & Faires, Numerical Analysis, §3.5).
|
|
40
|
+
* The points need not be sorted. It overshoots between points where the data is
|
|
41
|
+
* not smooth; use {@link monotoneCubicSpline2} to forbid that.
|
|
39
42
|
*
|
|
40
|
-
* Complexity: O(n · samples) time,
|
|
43
|
+
* Complexity: O(n · samples) time, no allocation: the elimination's
|
|
44
|
+
* intermediate values are kept in `out` until the samples replace them (see
|
|
45
|
+
* `out` for the overlapping case).
|
|
41
46
|
*
|
|
42
47
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
43
|
-
* and the rest is left untouched. May share memory with `points
|
|
44
|
-
* are then copied
|
|
48
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
49
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
50
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
51
|
+
* (the only allocation).
|
|
45
52
|
* @param points - N points, 2 numbers (x, y) each.
|
|
46
53
|
* @param samples - Points per span, a positive integer.
|
|
47
54
|
* @returns `out`.
|
|
@@ -60,11 +67,14 @@ export declare function naturalCubicSpline2<T extends Float64Array>(out: T, poin
|
|
|
60
67
|
* inner points are not interpolated: the curve passes through `(Pᵢ₋₁ + 4·Pᵢ +
|
|
61
68
|
* Pᵢ₊₁) / 6`. De Boor, "On calculating with B-splines", J. Approx. Theory 6, 1972.
|
|
62
69
|
*
|
|
63
|
-
* Complexity: O(n · samples) time, no allocation
|
|
70
|
+
* Complexity: O(n · samples) time, no allocation (see `out` for the overlapping
|
|
71
|
+
* case).
|
|
64
72
|
*
|
|
65
73
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
66
|
-
* and the rest is left untouched. May share memory with `points
|
|
67
|
-
* are then copied
|
|
74
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
75
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
76
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
77
|
+
* (the only allocation).
|
|
68
78
|
* @param points - N control points, 2 numbers (x, y) each.
|
|
69
79
|
* @param samples - Points per span, a positive integer.
|
|
70
80
|
* @returns `out`.
|
|
@@ -80,14 +90,18 @@ export declare function uniformBSpline2<T extends Float64Array>(out: T, points:
|
|
|
80
90
|
* Catmull–Rom spline, `tension = 1` zero tangents (an eased polyline), and a
|
|
81
91
|
* negative tension loosens the curve; d3's `curveCardinal` parameterisation.
|
|
82
92
|
* The points need not be sorted. Uniform parameterisation: unevenly spaced
|
|
83
|
-
* points can overshoot or form cusps (
|
|
84
|
-
* Catmull & Rom, "A class of local
|
|
93
|
+
* points can overshoot or form cusps ({@link catmullRomSpline2} with `alpha =
|
|
94
|
+
* 0.5`, the centripetal spline, does not). Catmull & Rom, "A class of local
|
|
95
|
+
* interpolating splines", 1974.
|
|
85
96
|
*
|
|
86
|
-
* Complexity: O(n · samples) time, no allocation
|
|
97
|
+
* Complexity: O(n · samples) time, no allocation (see `out` for the overlapping
|
|
98
|
+
* case).
|
|
87
99
|
*
|
|
88
100
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
89
|
-
* and the rest is left untouched. May share memory with `points
|
|
90
|
-
* are then copied
|
|
101
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
102
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
103
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
104
|
+
* (the only allocation).
|
|
91
105
|
* @param points - N points, 2 numbers (x, y) each.
|
|
92
106
|
* @param samples - Points per span, a positive integer.
|
|
93
107
|
* @param tension - Tangent shrink, usually in [0, 1].
|
|
@@ -96,4 +110,30 @@ export declare function uniformBSpline2<T extends Float64Array>(out: T, points:
|
|
|
96
110
|
* integer, or `out` is too short.
|
|
97
111
|
*/
|
|
98
112
|
export declare function cardinalSpline2<T extends Float64Array>(out: T, points: Float64Array, samples: number, tension: number): T;
|
|
113
|
+
/**
|
|
114
|
+
* Samples the Catmull–Rom spline with knot exponent `alpha` through the points:
|
|
115
|
+
* a `C¹` piecewise cubic interpolating every point, with knot intervals `|Pᵢ₊₁
|
|
116
|
+
* − Pᵢ|^α` (Barry and Goldman 1988). `alpha = 0.5`, the centripetal spline,
|
|
117
|
+
* never forms a cusp or a self-intersection within a span (Yuksel, Schaefer and
|
|
118
|
+
* Keyser, 2011); `0` is the uniform spline (the same as `cardinalSpline2` with
|
|
119
|
+
* `tension = 0` except at the ends) and `1` the chordal one. The missing
|
|
120
|
+
* neighbours at the ends are reflected (`2P₀ − P₁`), so two points give the
|
|
121
|
+
* straight segment; repeated points give zero tangents. The points need not be
|
|
122
|
+
* sorted. `catmullRomCurve` evaluates the same spline at any parameter, in any
|
|
123
|
+
* dimension.
|
|
124
|
+
*
|
|
125
|
+
* Complexity: O(n · samples) time, no allocation (see `out` for the overlapping
|
|
126
|
+
* case).
|
|
127
|
+
*
|
|
128
|
+
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
129
|
+
* and the rest is left untouched. May share memory with `points`, as for
|
|
130
|
+
* {@link cardinalSpline2}.
|
|
131
|
+
* @param points - N points, 2 numbers (x, y) each.
|
|
132
|
+
* @param samples - Points per span, a positive integer.
|
|
133
|
+
* @param alpha - The knot exponent, in `[0, 1]`.
|
|
134
|
+
* @returns `out`.
|
|
135
|
+
* @throws {RangeError} When `points.length` is odd, `samples` is not a positive
|
|
136
|
+
* integer, `alpha` is outside `[0, 1]`, or `out` is too short.
|
|
137
|
+
*/
|
|
138
|
+
export declare function catmullRomSpline2<T extends Float64Array>(out: T, points: Float64Array, samples: number, alpha: number): T;
|
|
99
139
|
//# sourceMappingURL=spline2.d.ts.map
|
package/dist/spline2.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spline2.d.ts","sourceRoot":"","sources":["../src/spline2.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"spline2.d.ts","sourceRoot":"","sources":["../src/spline2.ts"],"names":[],"mappings":"AAsHA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,YAAY,EAC1D,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,GACb,CAAC,CAUH;AAwGD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,YAAY,EACzD,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,GACb,CAAC,CAUH;AAqFD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EACrD,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,GACb,CAAC,CAoDH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,YAAY,EACrD,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GACb,CAAC,CAiDH;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,YAAY,EACvD,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,GACX,CAAC,CAkCH"}
|
package/dist/spline2.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { assertInteger, viewsMayOverlap } from "@ac-kit/core";
|
|
2
|
-
import {
|
|
2
|
+
import { catmullRomTangents_, checkAlpha_ } from "./_catmull-rom.js";
|
|
3
3
|
import { cubicHermite2Samples_ } from "./_hermite2-samples.js";
|
|
4
|
+
/** The two tangents of a Catmull–Rom span, `[m₁x, m₁y, m₂x, m₂y]`. */
|
|
5
|
+
const CATMULL_ROM_TANGENTS_ = new Float64Array(4);
|
|
4
6
|
// Sampled interpolating and approximating splines through `Vec2` buffers (x, y
|
|
5
7
|
// pairs), for the line interpolations of a chart. Every function writes the
|
|
6
8
|
// first point, then `samples` points per span between consecutive input points,
|
|
@@ -29,11 +31,58 @@ function expectedLength_(name, out, points, samples) {
|
|
|
29
31
|
}
|
|
30
32
|
return count;
|
|
31
33
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
/**
|
|
35
|
+
* The points copied out of `out`'s memory when the two may overlap: every
|
|
36
|
+
* spline writes samples over a longer run than the points it still has to read
|
|
37
|
+
* (and the first two keep their tangents in `out`), so at some offset of `out`
|
|
38
|
+
* into `points` any evaluation order overwrites a point still to be read. Grown
|
|
39
|
+
* geometrically and kept, so a sampler allocates only for a larger overlapping
|
|
40
|
+
* input than any before; no sampler runs caller code while it holds the copy,
|
|
41
|
+
* so one buffer serves them all.
|
|
42
|
+
*/
|
|
43
|
+
let pointsCopy_ = new Float64Array(0);
|
|
44
|
+
// `points` itself, or its copy in `pointsCopy_` (possibly longer: the samplers
|
|
45
|
+
// read only the first `points.length` numbers) when it may share memory with
|
|
46
|
+
// `out` (the same array, overlapping views, or wrappers of one shared memory).
|
|
35
47
|
function separate_(out, points) {
|
|
36
|
-
|
|
48
|
+
if (!viewsMayOverlap(out, points)) {
|
|
49
|
+
return points;
|
|
50
|
+
}
|
|
51
|
+
if (pointsCopy_.length < points.length) {
|
|
52
|
+
pointsCopy_ = new Float64Array(Math.max(points.length, 2 * pointsCopy_.length));
|
|
53
|
+
}
|
|
54
|
+
pointsCopy_.set(points);
|
|
55
|
+
return pointsCopy_;
|
|
56
|
+
}
|
|
57
|
+
// The first-row coefficients c′ of the Thomas algorithm on the natural
|
|
58
|
+
// spline's (1, 4, 1) system: c′₁ = 1/4, c′ⱼ = 1 / (4 − c′ⱼ₋₁). They depend on
|
|
59
|
+
// nothing else and reach a fixed point in about twenty steps (the ratio is
|
|
60
|
+
// (2 − √3)² per step), so they are tabulated once, the last entry standing for
|
|
61
|
+
// every later one.
|
|
62
|
+
function thomasCoefficients_() {
|
|
63
|
+
const values = [0, 0.25];
|
|
64
|
+
for (;;) {
|
|
65
|
+
const next = 1 / (4 - values.at(-1));
|
|
66
|
+
if (next === values.at(-1)) {
|
|
67
|
+
return Float64Array.from(values);
|
|
68
|
+
}
|
|
69
|
+
values.push(next);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const THOMAS_COEFFICIENTS_ = thomasCoefficients_();
|
|
73
|
+
function thomasCoefficient_(index) {
|
|
74
|
+
const last = THOMAS_COEFFICIENTS_.length - 1;
|
|
75
|
+
return THOMAS_COEFFICIENTS_[index < last ? index : last];
|
|
76
|
+
}
|
|
77
|
+
// The secant slope of span `index` in the scaled units (y / scaleY) /
|
|
78
|
+
// (x / scaleX), 0 for a vertical step.
|
|
79
|
+
function secant_(points, index, scaleX, scaleY) {
|
|
80
|
+
const dx = points[2 * index + 2] - points[2 * index];
|
|
81
|
+
return dx === 0
|
|
82
|
+
? 0
|
|
83
|
+
: (points[2 * index + 3] - points[2 * index + 1]) /
|
|
84
|
+
scaleY /
|
|
85
|
+
(dx / scaleX);
|
|
37
86
|
}
|
|
38
87
|
/**
|
|
39
88
|
* Samples the monotone cubic interpolant through points sorted by x: a
|
|
@@ -53,11 +102,14 @@ function separate_(out, points) {
|
|
|
53
102
|
* curve; steps whose ratio leaves [1e−150, 1e150] (a subnormal step beside a
|
|
54
103
|
* unit one) can overflow a slope.
|
|
55
104
|
*
|
|
56
|
-
* Complexity: O(n · samples) time,
|
|
105
|
+
* Complexity: O(n · samples) time, no allocation: the tangents are kept in
|
|
106
|
+
* `out` until the samples replace them (see `out` for the overlapping case).
|
|
57
107
|
*
|
|
58
108
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
59
|
-
* and the rest is left untouched. May share memory with `points
|
|
60
|
-
* are then copied
|
|
109
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
110
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
111
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
112
|
+
* (the only allocation).
|
|
61
113
|
* @param points - N points, 2 numbers (x, y) each.
|
|
62
114
|
* @param samples - Points per span, a positive integer.
|
|
63
115
|
* @returns `out`.
|
|
@@ -69,17 +121,22 @@ export function monotoneCubicSpline2(out, points, samples) {
|
|
|
69
121
|
if (count === 0) {
|
|
70
122
|
return out;
|
|
71
123
|
}
|
|
72
|
-
|
|
73
|
-
out
|
|
74
|
-
|
|
124
|
+
monotoneSamples_(out, separate_(out, points), count, samples);
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
// The monotone spline of the first `count` points of `points`, which does not
|
|
128
|
+
// share memory with `out`; no check.
|
|
129
|
+
function monotoneSamples_(out, points, count, samples) {
|
|
75
130
|
if (count === 1) {
|
|
76
|
-
|
|
131
|
+
out[0] = points[0];
|
|
132
|
+
out[1] = points[1];
|
|
133
|
+
return;
|
|
77
134
|
}
|
|
78
135
|
let scaleX = 0;
|
|
79
136
|
let scaleY = 0;
|
|
80
137
|
for (let index = 0; index < count - 1; index++) {
|
|
81
|
-
scaleX = Math.max(scaleX, Math.abs(
|
|
82
|
-
scaleY = Math.max(scaleY, Math.abs(
|
|
138
|
+
scaleX = Math.max(scaleX, Math.abs(points[2 * index + 2] - points[2 * index]));
|
|
139
|
+
scaleY = Math.max(scaleY, Math.abs(points[2 * index + 3] - points[2 * index + 1]));
|
|
83
140
|
}
|
|
84
141
|
if (scaleX === 0) {
|
|
85
142
|
scaleX = 1;
|
|
@@ -87,65 +144,63 @@ export function monotoneCubicSpline2(out, points, samples) {
|
|
|
87
144
|
if (scaleY === 0) {
|
|
88
145
|
scaleY = 1;
|
|
89
146
|
}
|
|
90
|
-
//
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
dx === 0
|
|
96
|
-
? 0
|
|
97
|
-
: (source[2 * index + 3] - source[2 * index + 1]) /
|
|
98
|
-
scaleY /
|
|
99
|
-
(dx / scaleX);
|
|
100
|
-
}
|
|
101
|
-
const tangents = new Float64Array(count);
|
|
102
|
-
tangents[0] = secants[0];
|
|
103
|
-
tangents[count - 1] = secants[count - 2];
|
|
147
|
+
// The tangent at point i is kept in out[2i + 1] until the spans are
|
|
148
|
+
// sampled, last span first: span i writes from out[2(i·samples + 1)] on,
|
|
149
|
+
// past every tangent a span before it still reads.
|
|
150
|
+
let before = secant_(points, 0, scaleX, scaleY);
|
|
151
|
+
out[1] = before;
|
|
104
152
|
for (let index = 1; index < count - 1; index++) {
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
153
|
+
const after = secant_(points, index, scaleX, scaleY);
|
|
154
|
+
out[2 * index + 1] = before * after <= 0 ? 0 : (before + after) / 2;
|
|
155
|
+
before = after;
|
|
108
156
|
}
|
|
157
|
+
out[2 * count - 1] = before;
|
|
109
158
|
for (let index = 0; index < count - 1; index++) {
|
|
110
|
-
const secant =
|
|
159
|
+
const secant = secant_(points, index, scaleX, scaleY);
|
|
111
160
|
if (secant === 0) {
|
|
112
|
-
|
|
113
|
-
|
|
161
|
+
out[2 * index + 1] = 0;
|
|
162
|
+
out[2 * index + 3] = 0;
|
|
114
163
|
continue;
|
|
115
164
|
}
|
|
116
|
-
const alpha =
|
|
117
|
-
const beta =
|
|
165
|
+
const alpha = out[2 * index + 1] / secant;
|
|
166
|
+
const beta = out[2 * index + 3] / secant;
|
|
118
167
|
const radiusSquared = alpha * alpha + beta * beta;
|
|
119
168
|
if (radiusSquared > 9) {
|
|
120
169
|
const scale = (3 / Math.sqrt(radiusSquared)) * secant;
|
|
121
|
-
|
|
122
|
-
|
|
170
|
+
out[2 * index + 1] = scale * alpha;
|
|
171
|
+
out[2 * index + 3] = scale * beta;
|
|
123
172
|
}
|
|
124
173
|
}
|
|
125
|
-
let
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
const x1 = source[2 * index + 2];
|
|
174
|
+
for (let index = count - 2; index >= 0; index--) {
|
|
175
|
+
const x0 = points[2 * index];
|
|
176
|
+
const x1 = points[2 * index + 2];
|
|
129
177
|
const dx = x1 - x0;
|
|
130
178
|
const step = dx / scaleX;
|
|
131
|
-
|
|
179
|
+
const startTangent = out[2 * index + 1];
|
|
180
|
+
const endTangent = out[2 * index + 3];
|
|
181
|
+
cubicHermite2Samples_(out, 2 * (index * samples + 1), samples, x0, points[2 * index + 1], dx, startTangent * step * scaleY, x1, points[2 * index + 3], dx, endTangent * step * scaleY);
|
|
132
182
|
}
|
|
133
|
-
|
|
183
|
+
out[0] = points[0];
|
|
184
|
+
out[1] = points[1];
|
|
134
185
|
}
|
|
135
186
|
/**
|
|
136
187
|
* Samples the natural cubic spline through the points: the `C²` piecewise cubic
|
|
137
188
|
* that interpolates every point with zero second derivative at both ends, as a
|
|
138
189
|
* parametric curve `(x(s), y(s))` with the points at integer parameters (each
|
|
139
|
-
* coordinate is solved separately, the moments by
|
|
140
|
-
*
|
|
141
|
-
* points need not be sorted. It overshoots between points where the data is
|
|
142
|
-
* smooth; use {@link monotoneCubicSpline2} to forbid that.
|
|
190
|
+
* coordinate is solved separately, the moments by the Thomas algorithm on the
|
|
191
|
+
* spline's (1, 4, 1) system; e.g. Burden & Faires, Numerical Analysis, §3.5).
|
|
192
|
+
* The points need not be sorted. It overshoots between points where the data is
|
|
193
|
+
* not smooth; use {@link monotoneCubicSpline2} to forbid that.
|
|
143
194
|
*
|
|
144
|
-
* Complexity: O(n · samples) time,
|
|
195
|
+
* Complexity: O(n · samples) time, no allocation: the elimination's
|
|
196
|
+
* intermediate values are kept in `out` until the samples replace them (see
|
|
197
|
+
* `out` for the overlapping case).
|
|
145
198
|
*
|
|
146
199
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
147
|
-
* and the rest is left untouched. May share memory with `points
|
|
148
|
-
* are then copied
|
|
200
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
201
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
202
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
203
|
+
* (the only allocation).
|
|
149
204
|
* @param points - N points, 2 numbers (x, y) each.
|
|
150
205
|
* @param samples - Points per span, a positive integer.
|
|
151
206
|
* @returns `out`.
|
|
@@ -157,56 +212,64 @@ export function naturalCubicSpline2(out, points, samples) {
|
|
|
157
212
|
if (count === 0) {
|
|
158
213
|
return out;
|
|
159
214
|
}
|
|
160
|
-
|
|
161
|
-
out
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
215
|
+
naturalSamples_(out, separate_(out, points), count, samples);
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
218
|
+
// The natural spline of the first `count` points of `points`, which does not
|
|
219
|
+
// share memory with `out`; no check.
|
|
220
|
+
function naturalSamples_(out, points, count, samples) {
|
|
166
221
|
// The second derivatives M at the inner points satisfy
|
|
167
222
|
// M_{i−1} + 4·M_i + M_{i+1} = 6 · ((P_{i+1} − P_i) − (P_i − P_{i−1})),
|
|
168
223
|
// with M_0 = M_{n−1} = 0: strictly diagonally dominant, so the Thomas
|
|
169
|
-
// algorithm is stable and meets no zero pivot.
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
(
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
224
|
+
// algorithm is stable and meets no zero pivot. Its forward sweep leaves d′ᵢ
|
|
225
|
+
// in out[2i], out[2i + 1]; the back substitution runs with the sampling,
|
|
226
|
+
// last span first, span i writing from out[2(i·samples + 1)] on, past every
|
|
227
|
+
// d′ a span before it still reads.
|
|
228
|
+
let previousX = 0;
|
|
229
|
+
let previousY = 0;
|
|
230
|
+
for (let index = 1; index < count - 1; index++) {
|
|
231
|
+
const coefficient = thomasCoefficient_(index);
|
|
232
|
+
previousX =
|
|
233
|
+
(6 *
|
|
234
|
+
(points[2 * index + 2] -
|
|
235
|
+
points[2 * index] -
|
|
236
|
+
(points[2 * index] - points[2 * index - 2])) -
|
|
237
|
+
previousX) *
|
|
238
|
+
coefficient;
|
|
239
|
+
previousY =
|
|
240
|
+
(6 *
|
|
241
|
+
(points[2 * index + 3] -
|
|
242
|
+
points[2 * index + 1] -
|
|
243
|
+
(points[2 * index + 1] - points[2 * index - 1])) -
|
|
244
|
+
previousY) *
|
|
245
|
+
coefficient;
|
|
246
|
+
out[2 * index] = previousX;
|
|
247
|
+
out[2 * index + 1] = previousY;
|
|
192
248
|
}
|
|
193
|
-
let
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
249
|
+
let endX = 0;
|
|
250
|
+
let endY = 0;
|
|
251
|
+
for (let index = count - 2; index >= 0; index--) {
|
|
252
|
+
let startX = 0;
|
|
253
|
+
let startY = 0;
|
|
254
|
+
if (index > 0) {
|
|
255
|
+
const coefficient = thomasCoefficient_(index);
|
|
256
|
+
startX = out[2 * index] - coefficient * endX;
|
|
257
|
+
startY = out[2 * index + 1] - coefficient * endY;
|
|
258
|
+
}
|
|
259
|
+
const x0 = points[2 * index];
|
|
260
|
+
const y0 = points[2 * index + 1];
|
|
261
|
+
const x1 = points[2 * index + 2];
|
|
262
|
+
const y1 = points[2 * index + 3];
|
|
203
263
|
const dx = x1 - x0;
|
|
204
264
|
const dy = y1 - y0;
|
|
205
265
|
// S(u) = (1 − u)·P_i + u·P_{i+1} + ((1 − u)³ − (1 − u))·M_i / 6 +
|
|
206
266
|
// (u³ − u)·M_{i+1} / 6, whose end derivatives are these tangents.
|
|
207
|
-
|
|
267
|
+
cubicHermite2Samples_(out, 2 * (index * samples + 1), samples, x0, y0, dx - (2 * startX + endX) / 6, dy - (2 * startY + endY) / 6, x1, y1, dx + (startX + 2 * endX) / 6, dy + (startY + 2 * endY) / 6);
|
|
268
|
+
endX = startX;
|
|
269
|
+
endY = startY;
|
|
208
270
|
}
|
|
209
|
-
|
|
271
|
+
out[0] = points[0];
|
|
272
|
+
out[1] = points[1];
|
|
210
273
|
}
|
|
211
274
|
/**
|
|
212
275
|
* Samples the uniform cubic B-spline whose control points are the given points:
|
|
@@ -219,11 +282,14 @@ export function naturalCubicSpline2(out, points, samples) {
|
|
|
219
282
|
* inner points are not interpolated: the curve passes through `(Pᵢ₋₁ + 4·Pᵢ +
|
|
220
283
|
* Pᵢ₊₁) / 6`. De Boor, "On calculating with B-splines", J. Approx. Theory 6, 1972.
|
|
221
284
|
*
|
|
222
|
-
* Complexity: O(n · samples) time, no allocation
|
|
285
|
+
* Complexity: O(n · samples) time, no allocation (see `out` for the overlapping
|
|
286
|
+
* case).
|
|
223
287
|
*
|
|
224
288
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
225
|
-
* and the rest is left untouched. May share memory with `points
|
|
226
|
-
* are then copied
|
|
289
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
290
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
291
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
292
|
+
* (the only allocation).
|
|
227
293
|
* @param points - N control points, 2 numbers (x, y) each.
|
|
228
294
|
* @param samples - Points per span, a positive integer.
|
|
229
295
|
* @returns `out`.
|
|
@@ -271,14 +337,18 @@ export function uniformBSpline2(out, points, samples) {
|
|
|
271
337
|
* Catmull–Rom spline, `tension = 1` zero tangents (an eased polyline), and a
|
|
272
338
|
* negative tension loosens the curve; d3's `curveCardinal` parameterisation.
|
|
273
339
|
* The points need not be sorted. Uniform parameterisation: unevenly spaced
|
|
274
|
-
* points can overshoot or form cusps (
|
|
275
|
-
* Catmull & Rom, "A class of local
|
|
340
|
+
* points can overshoot or form cusps ({@link catmullRomSpline2} with `alpha =
|
|
341
|
+
* 0.5`, the centripetal spline, does not). Catmull & Rom, "A class of local
|
|
342
|
+
* interpolating splines", 1974.
|
|
276
343
|
*
|
|
277
|
-
* Complexity: O(n · samples) time, no allocation
|
|
344
|
+
* Complexity: O(n · samples) time, no allocation (see `out` for the overlapping
|
|
345
|
+
* case).
|
|
278
346
|
*
|
|
279
347
|
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
280
|
-
* and the rest is left untouched. May share memory with `points
|
|
281
|
-
* are then copied
|
|
348
|
+
* and the rest is left untouched. May share memory with `points`, even
|
|
349
|
+
* through two wrappers of one `SharedArrayBuffer`: the points are then copied
|
|
350
|
+
* first, into a buffer this module keeps and grows to the largest such input
|
|
351
|
+
* (the only allocation).
|
|
282
352
|
* @param points - N points, 2 numbers (x, y) each.
|
|
283
353
|
* @param samples - Points per span, a positive integer.
|
|
284
354
|
* @param tension - Tangent shrink, usually in [0, 1].
|
|
@@ -316,4 +386,46 @@ export function cardinalSpline2(out, points, samples, tension) {
|
|
|
316
386
|
}
|
|
317
387
|
return out;
|
|
318
388
|
}
|
|
389
|
+
/**
|
|
390
|
+
* Samples the Catmull–Rom spline with knot exponent `alpha` through the points:
|
|
391
|
+
* a `C¹` piecewise cubic interpolating every point, with knot intervals `|Pᵢ₊₁
|
|
392
|
+
* − Pᵢ|^α` (Barry and Goldman 1988). `alpha = 0.5`, the centripetal spline,
|
|
393
|
+
* never forms a cusp or a self-intersection within a span (Yuksel, Schaefer and
|
|
394
|
+
* Keyser, 2011); `0` is the uniform spline (the same as `cardinalSpline2` with
|
|
395
|
+
* `tension = 0` except at the ends) and `1` the chordal one. The missing
|
|
396
|
+
* neighbours at the ends are reflected (`2P₀ − P₁`), so two points give the
|
|
397
|
+
* straight segment; repeated points give zero tangents. The points need not be
|
|
398
|
+
* sorted. `catmullRomCurve` evaluates the same spline at any parameter, in any
|
|
399
|
+
* dimension.
|
|
400
|
+
*
|
|
401
|
+
* Complexity: O(n · samples) time, no allocation (see `out` for the overlapping
|
|
402
|
+
* case).
|
|
403
|
+
*
|
|
404
|
+
* @param out - Destination, ((n − 1) · samples + 1) points; longer is allowed
|
|
405
|
+
* and the rest is left untouched. May share memory with `points`, as for
|
|
406
|
+
* {@link cardinalSpline2}.
|
|
407
|
+
* @param points - N points, 2 numbers (x, y) each.
|
|
408
|
+
* @param samples - Points per span, a positive integer.
|
|
409
|
+
* @param alpha - The knot exponent, in `[0, 1]`.
|
|
410
|
+
* @returns `out`.
|
|
411
|
+
* @throws {RangeError} When `points.length` is odd, `samples` is not a positive
|
|
412
|
+
* integer, `alpha` is outside `[0, 1]`, or `out` is too short.
|
|
413
|
+
*/
|
|
414
|
+
export function catmullRomSpline2(out, points, samples, alpha) {
|
|
415
|
+
checkAlpha_("catmullRomSpline2", alpha);
|
|
416
|
+
const count = expectedLength_("catmullRomSpline2", out, points, samples);
|
|
417
|
+
if (count === 0) {
|
|
418
|
+
return out;
|
|
419
|
+
}
|
|
420
|
+
const source = separate_(out, points);
|
|
421
|
+
out[0] = source[0];
|
|
422
|
+
out[1] = source[1];
|
|
423
|
+
const tangents = CATMULL_ROM_TANGENTS_;
|
|
424
|
+
let offset = 2;
|
|
425
|
+
for (let index = 0; index < count - 1; index++) {
|
|
426
|
+
catmullRomTangents_(tangents, 0, source, 2, count, index, alpha);
|
|
427
|
+
offset = cubicHermite2Samples_(out, offset, samples, source[2 * index], source[2 * index + 1], tangents[0], tangents[1], source[2 * index + 2], source[2 * index + 3], tangents[2], tangents[3]);
|
|
428
|
+
}
|
|
429
|
+
return out;
|
|
430
|
+
}
|
|
319
431
|
//# sourceMappingURL=spline2.js.map
|