@ac-kit/math-curves 0.2.1 → 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.
Files changed (47) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/README.md +21 -0
  3. package/dist/_catmull-rom.d.ts +23 -0
  4. package/dist/_catmull-rom.d.ts.map +1 -0
  5. package/dist/_catmull-rom.js +66 -0
  6. package/dist/_catmull-rom.js.map +1 -0
  7. package/dist/arc-length.d.ts +57 -0
  8. package/dist/arc-length.d.ts.map +1 -0
  9. package/dist/arc-length.js +121 -0
  10. package/dist/arc-length.js.map +1 -0
  11. package/dist/bezier-n.d.ts +47 -0
  12. package/dist/bezier-n.d.ts.map +1 -0
  13. package/dist/bezier-n.js +113 -0
  14. package/dist/bezier-n.js.map +1 -0
  15. package/dist/bezier2.d.ts +12 -0
  16. package/dist/bezier2.d.ts.map +1 -1
  17. package/dist/bezier2.js +19 -0
  18. package/dist/bezier2.js.map +1 -1
  19. package/dist/bezier3.d.ts +40 -0
  20. package/dist/bezier3.d.ts.map +1 -0
  21. package/dist/bezier3.js +75 -0
  22. package/dist/bezier3.js.map +1 -0
  23. package/dist/catmull-rom-curve.d.ts +39 -0
  24. package/dist/catmull-rom-curve.d.ts.map +1 -0
  25. package/dist/catmull-rom-curve.js +123 -0
  26. package/dist/catmull-rom-curve.js.map +1 -0
  27. package/dist/cubic-bezier-easing.d.ts +27 -0
  28. package/dist/cubic-bezier-easing.d.ts.map +1 -0
  29. package/dist/cubic-bezier-easing.js +104 -0
  30. package/dist/cubic-bezier-easing.js.map +1 -0
  31. package/dist/curve-to-polyline.d.ts +33 -0
  32. package/dist/curve-to-polyline.d.ts.map +1 -0
  33. package/dist/curve-to-polyline.js +121 -0
  34. package/dist/curve-to-polyline.js.map +1 -0
  35. package/dist/index.d.ts +7 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +7 -0
  38. package/dist/index.js.map +1 -1
  39. package/dist/parametric-curve.d.ts +31 -0
  40. package/dist/parametric-curve.d.ts.map +1 -0
  41. package/dist/parametric-curve.js +2 -0
  42. package/dist/parametric-curve.js.map +1 -0
  43. package/dist/spline2.d.ts +29 -2
  44. package/dist/spline2.d.ts.map +1 -1
  45. package/dist/spline2.js +48 -2
  46. package/dist/spline2.js.map +1 -1
  47. package/package.json +9 -7
package/CHANGELOG.md CHANGED
@@ -1,5 +1,54 @@
1
1
  # @ac-kit/math-curves
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5c203e7: `cubicBezierEasing(x1, y1, x2, y2)` returns the CSS `cubic-bezier()` timing
8
+ function `x ↦ y` (sample table built once per curve, Newton then bisection to
9
+ 1e-12 in `x`, end tangents outside `[0, 1]`); a `RangeError` when `x1` or `x2`
10
+ leaves `[0, 1]`.
11
+ - 5c203e7: - `linearBezier2`, and in space `linearBezier3`, `quadraticBezier3` and
12
+ `cubicBezier3`.
13
+ - `bezierN` and `bezierDerivative`: a Bézier curve of any degree and
14
+ dimension, and its tangent, by De Casteljau over flat control points.
15
+ - `ParametricCurve` (point, derivative and `breaks` over `t ∈ [0, 1]`), built
16
+ by `bezierCurve(controlPoints, dimension)` and `catmullRomCurve(points,
17
+ dimension, { alpha })` (centripetal by default, `C¹` in its normalised knot
18
+ parameter, reflected ends).
19
+ - `catmullRomSpline2(out, points, samples, alpha)`: the sampler of that
20
+ spline, beside `cardinalSpline2`.
21
+ - `curveArcLength(curve, { from, to, relativeTolerance })` and
22
+ `curveArcLengthTable(curve, samples)`: lengths by adaptive Gauss–Kronrod
23
+ quadrature of the speed, split at the breaks.
24
+ - `curveToPolyline(curves, tolerance)`: adaptive flattening of a curve or a
25
+ path of curves.
26
+
27
+ `@ac-kit/math-analysis` and `@ac-kit/algo` become dependencies.
28
+
29
+ ### Patch Changes
30
+
31
+ - Updated dependencies [5c203e7]
32
+ - Updated dependencies [5c203e7]
33
+ - Updated dependencies [5c203e7]
34
+ - Updated dependencies [5c203e7]
35
+ - Updated dependencies [5c203e7]
36
+ - Updated dependencies [5c203e7]
37
+ - Updated dependencies [5c203e7]
38
+ - Updated dependencies [5c203e7]
39
+ - Updated dependencies [5c203e7]
40
+ - Updated dependencies [5c203e7]
41
+ - Updated dependencies [5c203e7]
42
+ - Updated dependencies [5c203e7]
43
+ - Updated dependencies [5c203e7]
44
+ - Updated dependencies [5c203e7]
45
+ - Updated dependencies [5c203e7]
46
+ - Updated dependencies [5c203e7]
47
+ - @ac-kit/algo@0.7.0
48
+ - @ac-kit/core@0.14.0
49
+ - @ac-kit/math-analysis@0.3.0
50
+ - @ac-kit/math-linear@0.5.0
51
+
3
52
  ## 0.2.1
4
53
 
5
54
  ### Patch Changes
package/README.md CHANGED
@@ -18,8 +18,28 @@ return a number.
18
18
 
19
19
  | Symbol | Description |
20
20
  | --------------------------------- | --------------------------------------- |
21
+ | `linearBezier2(out, p0, p1, t)` | The segment, as a degree-1 Bézier |
21
22
  | `quadraticBezier2(out, p0, p1, p2, t)` | Quadratic Bézier in the Bernstein basis |
22
23
  | `cubicBezier2(out, p0, p1, p2, p3, t)` | Cubic Bézier in the Bernstein basis |
24
+ | `linearBezier3`, `quadraticBezier3`, `cubicBezier3` | The same in space, over `Vec3` |
25
+ | `bezierN(out, offset, controlPoints, dimension, t)` | Any degree and dimension, De Casteljau |
26
+ | `bezierDerivative(out, offset, controlPoints, dimension, t)` | Its tangent, the degree `n − 1` hodograph |
27
+ | `cubicBezierEasing(x1, y1, x2, y2)` | CSS `cubic-bezier()` timing function, `x ↦ y`, end tangents outside `[0, 1]` |
28
+
29
+ A `ParametricCurve` is `C(t)` over `[0, 1]` in any dimension, with its
30
+ derivative and the `breaks` where its pieces join; `bezierCurve` and
31
+ `catmullRomCurve` build one, or write your own:
32
+
33
+ | Symbol | Description |
34
+ | ---------------------------------------- | --------------------------------------------------------------------------- |
35
+ | `bezierCurve(controlPoints, dimension)` | The Bézier curve of any degree |
36
+ | `catmullRomCurve(points, dimension, { alpha })` | Catmull–Rom through the points, centripetal (`alpha = 0.5`) by default, `C¹` in its knot parameter |
37
+ | `curveArcLength(curve, { from, to })` | Length by adaptive Gauss–Kronrod quadrature of the speed, split at the breaks |
38
+ | `curveArcLengthTable(curve, samples)` | Cumulative length at `samples + 1` even parameters, to advance by distance |
39
+ | `curveToPolyline(curves, tolerance)` | Adaptive flattening of a curve or a path of curves, within `tolerance` |
40
+
41
+ Simplifying a sampled polyline before fitting (Ramer–Douglas–Peucker) is
42
+ `polyline2Simplify` and `polyline3Simplify` in `@ac-kit/math-geometry`.
23
43
 
24
44
  Polylines and sampled curves write into a `Float64Array` you own, out first,
25
45
  two numbers (x, y) per point:
@@ -35,6 +55,7 @@ two numbers (x, y) per point:
35
55
  | `naturalCubicSpline2(out, points, samples)` | `C²` interpolating spline, zero end curvature |
36
56
  | `uniformBSpline2(out, points, samples)` | Uniform cubic B-spline (smoothing, not interpolating), ends at the end points |
37
57
  | `cardinalSpline2(out, points, samples, tension)` | Interpolating cardinal spline (`tension = 0`: Catmull–Rom) |
58
+ | `catmullRomSpline2(out, points, samples, alpha)` | Catmull–Rom with knot exponent `alpha` (`0.5`: centripetal, no cusps) |
38
59
 
39
60
  `…Flatten` is adaptive: chords are long where the curve is flat and short
40
61
  where it bends, each one checked against a bound on its distance to the curve,
@@ -0,0 +1,23 @@
1
+ /** Throws unless the knot exponent is in `[0, 1]`. */
2
+ export declare function checkAlpha_(name: string, alpha: number): void;
3
+ /**
4
+ * Writes the tangents at both ends of span `span` (from point `span` to `span +
5
+ * 1`) of the Catmull–Rom spline with knot exponent `alpha` through `count`
6
+ * points of `dimension` coordinates, with respect to the span's own parameter
7
+ * `u ∈ [0, 1]`: `m₁` at `out[offset …)`, `m₂` right after it.
8
+ *
9
+ * With `dᵢ = |Pᵢ₊₁ − Pᵢ|^α` the knot intervals (Barry and Goldman 1988, in
10
+ * Hermite form):
11
+ *
12
+ * ```text
13
+ * m₁ = d₁·((P₁ − P₀)/d₀ − (P₂ − P₀)/(d₀ + d₁)) + (P₂ − P₁)
14
+ * m₂ = (P₂ − P₁) − d₁·((P₃ − P₁)/(d₁ + d₂) − (P₃ − P₂)/d₂)
15
+ * ```
16
+ *
17
+ * For the span `P₁ → P₂`. A missing neighbour at an end is the reflected
18
+ * phantom `2P₀ − P₁`, so a lone span is the straight segment. A zero interval
19
+ * drops its quotient (its limit for `α < 1`), so repeated points give zero
20
+ * tangents rather than `NaN`.
21
+ */
22
+ export declare function catmullRomTangents_(out: Float64Array, offset: number, points: Float64Array, dimension: number, count: number, span: number, alpha: number): void;
23
+ //# sourceMappingURL=_catmull-rom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_catmull-rom.d.ts","sourceRoot":"","sources":["../src/_catmull-rom.ts"],"names":[],"mappings":"AAEA,sDAAsD;AACtD,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAM7D;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,YAAY,EACjB,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,GACX,IAAI,CAwCN"}
@@ -0,0 +1,66 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ /** Throws unless the knot exponent is in `[0, 1]`. */
3
+ export function checkAlpha_(name, alpha) {
4
+ if (!(alpha >= 0 && alpha <= 1)) {
5
+ throw new RangeError(`${name}: alpha must be in [0, 1], got ${describeValue(alpha)}.`);
6
+ }
7
+ }
8
+ /**
9
+ * Writes the tangents at both ends of span `span` (from point `span` to `span +
10
+ * 1`) of the Catmull–Rom spline with knot exponent `alpha` through `count`
11
+ * points of `dimension` coordinates, with respect to the span's own parameter
12
+ * `u ∈ [0, 1]`: `m₁` at `out[offset …)`, `m₂` right after it.
13
+ *
14
+ * With `dᵢ = |Pᵢ₊₁ − Pᵢ|^α` the knot intervals (Barry and Goldman 1988, in
15
+ * Hermite form):
16
+ *
17
+ * ```text
18
+ * m₁ = d₁·((P₁ − P₀)/d₀ − (P₂ − P₀)/(d₀ + d₁)) + (P₂ − P₁)
19
+ * m₂ = (P₂ − P₁) − d₁·((P₃ − P₁)/(d₁ + d₂) − (P₃ − P₂)/d₂)
20
+ * ```
21
+ *
22
+ * For the span `P₁ → P₂`. A missing neighbour at an end is the reflected
23
+ * phantom `2P₀ − P₁`, so a lone span is the straight segment. A zero interval
24
+ * drops its quotient (its limit for `α < 1`), so repeated points give zero
25
+ * tangents rather than `NaN`.
26
+ */
27
+ export function catmullRomTangents_(out, offset, points, dimension, count, span, alpha) {
28
+ const p1 = span * dimension;
29
+ const p2 = p1 + dimension;
30
+ const hasBefore = span > 0;
31
+ const hasAfter = span + 2 < count;
32
+ let before = 0;
33
+ let middle = 0;
34
+ let after = 0;
35
+ for (let axis = 0; axis < dimension; axis++) {
36
+ const step = points[p2 + axis] - points[p1 + axis];
37
+ const stepBefore = hasBefore
38
+ ? points[p1 + axis] - points[p1 - dimension + axis]
39
+ : step;
40
+ const stepAfter = hasAfter
41
+ ? points[p2 + dimension + axis] - points[p2 + axis]
42
+ : step;
43
+ before += stepBefore * stepBefore;
44
+ middle += step * step;
45
+ after += stepAfter * stepAfter;
46
+ }
47
+ const d0 = Math.sqrt(before) ** alpha;
48
+ const d1 = Math.sqrt(middle) ** alpha;
49
+ const d2 = Math.sqrt(after) ** alpha;
50
+ for (let axis = 0; axis < dimension; axis++) {
51
+ const step = points[p2 + axis] - points[p1 + axis];
52
+ const stepBefore = hasBefore
53
+ ? points[p1 + axis] - points[p1 - dimension + axis]
54
+ : step;
55
+ const stepAfter = hasAfter
56
+ ? points[p2 + dimension + axis] - points[p2 + axis]
57
+ : step;
58
+ const first = (d0 > 0 ? stepBefore / d0 : 0) -
59
+ (d0 + d1 > 0 ? (stepBefore + step) / (d0 + d1) : 0);
60
+ const second = (d1 + d2 > 0 ? (step + stepAfter) / (d1 + d2) : 0) -
61
+ (d2 > 0 ? stepAfter / d2 : 0);
62
+ out[offset + axis] = d1 * first + step;
63
+ out[offset + dimension + axis] = step - d1 * second;
64
+ }
65
+ }
66
+ //# sourceMappingURL=_catmull-rom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_catmull-rom.js","sourceRoot":"","sources":["../src/_catmull-rom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,sDAAsD;AACtD,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,KAAa;IACtD,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,UAAU,CACnB,GAAG,IAAI,kCAAkC,aAAa,CAAC,KAAK,CAAC,GAAG,CAChE,CAAC;IACH,CAAC;AACF,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,mBAAmB,CAClC,GAAiB,EACjB,MAAc,EACd,MAAoB,EACpB,SAAiB,EACjB,KAAa,EACb,IAAY,EACZ,KAAa;IAEb,MAAM,EAAE,GAAG,IAAI,GAAG,SAAS,CAAC;IAC5B,MAAM,EAAE,GAAG,EAAE,GAAG,SAAS,CAAC;IAC1B,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC;IAC3B,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC;IAClC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC;QAC7C,MAAM,IAAI,GAAG,MAAM,CAAC,EAAE,GAAG,IAAI,CAAE,GAAG,MAAM,CAAC,EAAE,GA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CAAE;YACrD,CAAC,CAAC,IAAI,CAAC;QACR,MAAM,KAAK,GACV,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;YAC9B,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACrD,MAAM,MAAM,GACX,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAClD,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/B,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,IAAI,CAAC;QACvC,GAAG,CAAC,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC,GAAG,IAAI,GAAG,EAAE,GAAG,MAAM,CAAC;IACrD,CAAC;AACF,CAAC"}
@@ -0,0 +1,57 @@
1
+ import type { ParametricCurve } from "./parametric-curve.js";
2
+ /** Options of {@link curveArcLength} and {@link curveArcLengthTable}. */
3
+ export type CurveArcLengthOptions = {
4
+ /**
5
+ * Accepted error relative to the length of each smooth piece integrated, in
6
+ * `[0, 1)`. Defaults to `1e-12`.
7
+ */
8
+ readonly relativeTolerance?: number;
9
+ };
10
+ /** Options of {@link curveArcLength}. */
11
+ export type CurveArcLengthRangeOptions = CurveArcLengthOptions & {
12
+ /** Start parameter, in `[0, to]`. Defaults to `0`. */
13
+ readonly from?: number;
14
+ /** End parameter, in `[from, 1]`. Defaults to `1`. */
15
+ readonly to?: number;
16
+ };
17
+ /**
18
+ * The length of `curve` between two parameters: the integral of the speed
19
+ * `|C′(t)|` by adaptive Gauss–Kronrod quadrature (`adaptiveQuadrature` from
20
+ * `@ac-kit/math-analysis`, 7–15 points per panel, the Gauss–Legendre rule with
21
+ * its error estimate), over each smooth piece between the curve's `breaks`. The
22
+ * speed is computed by scaling, so huge or tiny coordinates neither overflow
23
+ * nor underflow. A Bézier or spline speed is smooth except at cusps, where the
24
+ * adaptive panels concentrate; the result is within `relativeTolerance` of each
25
+ * piece's length.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * // A quarter circle by its usual cubic approximation: ≈ π/2 · 1.000273.
30
+ * const k = 0.5522847498;
31
+ * curveArcLength(bezierCurve(Float64Array.of(1, 0, 1, k, k, 1, 0, 1), 2));
32
+ * ```;
33
+ *
34
+ * @param curve - The curve.
35
+ * @param options - The parameter range and tolerance.
36
+ * @returns The length, `NaN` when the derivative is.
37
+ * @throws {RangeError} When the range is not `0 ≤ from ≤ to ≤ 1` or the
38
+ * tolerance is outside `[0, 1)`.
39
+ */
40
+ export declare function curveArcLength(curve: ParametricCurve, options?: CurveArcLengthRangeOptions): number;
41
+ /**
42
+ * The cumulative length of `curve` at `samples + 1` evenly spaced parameters:
43
+ * entry `i` is the length from `t = 0` to `t = i / samples`, the first `0` and
44
+ * the last the whole length. The lookup that maps a distance along the curve
45
+ * back to a parameter (by bisection and interpolation), so a dash pattern or an
46
+ * animation can advance by length rather than by `t`. Each interval is
47
+ * integrated as {@link curveArcLength}.
48
+ *
49
+ * @param curve - The curve.
50
+ * @param samples - Intervals, a positive integer.
51
+ * @param options - The tolerance.
52
+ * @returns A new array of `samples + 1` non-decreasing lengths.
53
+ * @throws {RangeError} When `samples` is not a positive safe integer or the
54
+ * tolerance is outside `[0, 1)`.
55
+ */
56
+ export declare function curveArcLengthTable(curve: ParametricCurve, samples: number, options?: CurveArcLengthOptions): Float64Array;
57
+ //# sourceMappingURL=arc-length.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arc-length.d.ts","sourceRoot":"","sources":["../src/arc-length.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAE7D,yEAAyE;AACzE,MAAM,MAAM,qBAAqB,GAAG;IACnC;;;OAGG;IACH,QAAQ,CAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;CACpC,CAAC;AAEF,yCAAyC;AACzC,MAAM,MAAM,0BAA0B,GAAG,qBAAqB,GAAG;IAChE,sDAAsD;IACtD,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAEvB,sDAAsD;IACtD,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AA2EF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,cAAc,CAC7B,KAAK,EAAE,eAAe,EACtB,OAAO,CAAC,EAAE,0BAA0B,GAClC,MAAM,CAUR;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,eAAe,EACtB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,qBAAqB,GAC7B,YAAY,CAqBd"}
@@ -0,0 +1,121 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ import { adaptiveQuadrature } from "@ac-kit/math-analysis";
3
+ /** The derivative of the curve being integrated, grown to its dimension. */
4
+ let velocity_ = new Float64Array(3);
5
+ function speedOf_(curve) {
6
+ const dimension = curve.dimension;
7
+ if (velocity_.length < dimension) {
8
+ velocity_ = new Float64Array(dimension);
9
+ }
10
+ const velocity = velocity_;
11
+ return (t) => {
12
+ curve.derivative(velocity, 0, t);
13
+ let largest = 0;
14
+ for (let axis = 0; axis < dimension; axis++) {
15
+ const magnitude = Math.abs(velocity[axis]);
16
+ if (magnitude > largest || magnitude !== magnitude) {
17
+ largest = magnitude;
18
+ }
19
+ }
20
+ if (largest === 0 || largest !== largest || largest === Infinity) {
21
+ return largest;
22
+ }
23
+ // Scaled by the largest component, so no square overflows or underflows.
24
+ let sum = 0;
25
+ for (let axis = 0; axis < dimension; axis++) {
26
+ const ratio = velocity[axis] / largest;
27
+ sum += ratio * ratio;
28
+ }
29
+ return largest * Math.sqrt(sum);
30
+ };
31
+ }
32
+ function relativeTolerance_(name, options) {
33
+ const tolerance = options?.relativeTolerance ?? 1e-12;
34
+ if (!(tolerance >= 0 && tolerance < 1)) {
35
+ throw new RangeError(`${name}: relativeTolerance must be in [0, 1), got ${describeValue(tolerance)}.`);
36
+ }
37
+ return tolerance;
38
+ }
39
+ // The length over [from, to], split at the breaks inside it.
40
+ function lengthBetween_(curve, speed, from, to, relativeTolerance) {
41
+ const breaks = curve.breaks;
42
+ let total = 0;
43
+ let start = from;
44
+ for (let index = 0; index <= breaks.length; index++) {
45
+ const end = index < breaks.length ? Math.min(breaks[index], to) : to;
46
+ if (end > start) {
47
+ total += adaptiveQuadrature(speed, start, end, {
48
+ tolerance: 0,
49
+ relativeTolerance,
50
+ maxIterations: 200,
51
+ }).value;
52
+ start = end;
53
+ }
54
+ if (start >= to) {
55
+ break;
56
+ }
57
+ }
58
+ return total;
59
+ }
60
+ /**
61
+ * The length of `curve` between two parameters: the integral of the speed
62
+ * `|C′(t)|` by adaptive Gauss–Kronrod quadrature (`adaptiveQuadrature` from
63
+ * `@ac-kit/math-analysis`, 7–15 points per panel, the Gauss–Legendre rule with
64
+ * its error estimate), over each smooth piece between the curve's `breaks`. The
65
+ * speed is computed by scaling, so huge or tiny coordinates neither overflow
66
+ * nor underflow. A Bézier or spline speed is smooth except at cusps, where the
67
+ * adaptive panels concentrate; the result is within `relativeTolerance` of each
68
+ * piece's length.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * // A quarter circle by its usual cubic approximation: ≈ π/2 · 1.000273.
73
+ * const k = 0.5522847498;
74
+ * curveArcLength(bezierCurve(Float64Array.of(1, 0, 1, k, k, 1, 0, 1), 2));
75
+ * ```;
76
+ *
77
+ * @param curve - The curve.
78
+ * @param options - The parameter range and tolerance.
79
+ * @returns The length, `NaN` when the derivative is.
80
+ * @throws {RangeError} When the range is not `0 ≤ from ≤ to ≤ 1` or the
81
+ * tolerance is outside `[0, 1)`.
82
+ */
83
+ export function curveArcLength(curve, options) {
84
+ const relativeTolerance = relativeTolerance_("curveArcLength", options);
85
+ const from = options?.from ?? 0;
86
+ const to = options?.to ?? 1;
87
+ if (!(from >= 0 && from <= to && to <= 1)) {
88
+ throw new RangeError(`curveArcLength: the range must satisfy 0 ≤ from ≤ to ≤ 1, got [${describeValue(from)}, ${describeValue(to)}].`);
89
+ }
90
+ return lengthBetween_(curve, speedOf_(curve), from, to, relativeTolerance);
91
+ }
92
+ /**
93
+ * The cumulative length of `curve` at `samples + 1` evenly spaced parameters:
94
+ * entry `i` is the length from `t = 0` to `t = i / samples`, the first `0` and
95
+ * the last the whole length. The lookup that maps a distance along the curve
96
+ * back to a parameter (by bisection and interpolation), so a dash pattern or an
97
+ * animation can advance by length rather than by `t`. Each interval is
98
+ * integrated as {@link curveArcLength}.
99
+ *
100
+ * @param curve - The curve.
101
+ * @param samples - Intervals, a positive integer.
102
+ * @param options - The tolerance.
103
+ * @returns A new array of `samples + 1` non-decreasing lengths.
104
+ * @throws {RangeError} When `samples` is not a positive safe integer or the
105
+ * tolerance is outside `[0, 1)`.
106
+ */
107
+ export function curveArcLengthTable(curve, samples, options) {
108
+ const relativeTolerance = relativeTolerance_("curveArcLengthTable", options);
109
+ if (!Number.isSafeInteger(samples) || samples < 1) {
110
+ throw new RangeError(`curveArcLengthTable: samples must be a positive integer, got ${describeValue(samples)}.`);
111
+ }
112
+ const speed = speedOf_(curve);
113
+ const table = new Float64Array(samples + 1);
114
+ let total = 0;
115
+ for (let index = 0; index < samples; index++) {
116
+ total += lengthBetween_(curve, speed, index / samples, (index + 1) / samples, relativeTolerance);
117
+ table[index + 1] = total;
118
+ }
119
+ return table;
120
+ }
121
+ //# sourceMappingURL=arc-length.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arc-length.js","sourceR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GAAG,CAAC,CAAC;IACd,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC;QAC9C,KAAK,IAAI,cAAc,CACtB,KAAK,EACL,KAAK,EACL,KAAK,GAAG,OAAO,EACf,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,OAAO,EACrB,iBAAiB,CACjB,CAAC;QACF,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;IAC1B,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1,47 @@
1
+ import type { ParametricCurve } from "./parametric-curve.js";
2
+ /**
3
+ * Evaluates the Bézier curve of any degree at `t` by De Casteljau's algorithm:
4
+ * repeated linear interpolation between the control points, the most stable
5
+ * evaluation (every step a convex combination for `t ∈ [0, 1]`). O(n² ·
6
+ * dimension) for `n` control points; slower than the closed forms
7
+ * (`cubicBezier2`, …) but degree-agnostic.
8
+ *
9
+ * @param out - Receives the `dimension` coordinates at `offset`.
10
+ * @param offset - Index in `out` of the first coordinate written.
11
+ * @param controlPoints - The `n ≥ 1` control points, `dimension` numbers each
12
+ * (degree `n − 1`). Only read; may share memory with `out`.
13
+ * @param dimension - Coordinates per point, a positive integer.
14
+ * @param t - Curve parameter, normally in `[0, 1]`.
15
+ * @returns `out`.
16
+ * @throws {RangeError} When `dimension` or `offset` is not an integer in range,
17
+ * `controlPoints` is empty or not a multiple of `dimension` long, or `out` is
18
+ * too short.
19
+ */
20
+ export declare function bezierN<T extends Float64Array>(out: T, offset: number, controlPoints: Float64Array, dimension: number, t: number): T;
21
+ /**
22
+ * Evaluates the derivative `B′(t)` of the Bézier curve of any degree: itself a
23
+ * Bézier curve of degree `n − 2` whose control points are `(n − 1)·(Pᵢ₊₁ −
24
+ * Pᵢ)`, evaluated by De Casteljau's algorithm. The tangent direction at `t`;
25
+ * zero for a single control point. O(n² · dimension).
26
+ *
27
+ * @param out - Receives the `dimension` derivative coordinates at `offset`.
28
+ * @param offset - Index in `out` of the first coordinate written.
29
+ * @param controlPoints - The `n ≥ 1` control points, `dimension` numbers each.
30
+ * Only read; may share memory with `out`.
31
+ * @param dimension - Coordinates per point, a positive integer.
32
+ * @param t - Curve parameter, normally in `[0, 1]`.
33
+ * @returns `out`.
34
+ * @throws {RangeError} As {@link bezierN}.
35
+ */
36
+ export declare function bezierDerivative<T extends Float64Array>(out: T, offset: number, controlPoints: Float64Array, dimension: number, t: number): T;
37
+ /**
38
+ * The Bézier curve with these control points as a {@link ParametricCurve}, for
39
+ * arc length and flattening. The control points are copied.
40
+ *
41
+ * @param controlPoints - The `n ≥ 1` control points, `dimension` numbers each.
42
+ * @param dimension - Coordinates per point, a positive integer.
43
+ * @throws {RangeError} When `dimension` is not a positive integer or
44
+ * `controlPoints` is empty or not a multiple of `dimension` long.
45
+ */
46
+ export declare function bezierCurve(controlPoints: Float64Array, dimension: number): ParametricCurve;
47
+ //# sourceMappingURL=bezier-n.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bezier-n.d.ts","sourceRoot":"","sources":["../src/bezier-n.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAqD7D;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,YAAY,EAC7C,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,YAAY,EAC3B,SAAS,EAAE,MAAM,EACjB,CAAC,EAAE,MAAM,GACP,CAAC,CAOH;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,YAAY,EACtD,GAAG,EAAE,CAAC,EACN,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,YAAY,EAC3B,SAAS,EAAE,MAAM,EACjB,CAAC,EAAE,MAAM,GACP,CAAC,CAuBH;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAC1B,aAAa,EAAE,YAAY,EAC3B,SAAS,EAAE,MAAM,GACf,eAAe,CAcjB"}
@@ -0,0 +1,113 @@
1
+ import { assertInteger } from "@ac-kit/core";
2
+ /** De Casteljau's working copy, grown to the largest curve evaluated. */
3
+ let scratch_ = new Float64Array(16);
4
+ function checkBezier_(name, out, offset, controlPoints, dimension) {
5
+ assertInteger(dimension, "dimension", 1);
6
+ assertInteger(offset, "offset", 0);
7
+ if (controlPoints.length === 0 || controlPoints.length % dimension !== 0) {
8
+ throw new RangeError(`${name}: controlPoints must hold a positive multiple of ${dimension} numbers, got ${controlPoints.length}.`);
9
+ }
10
+ if (out.length < offset + dimension) {
11
+ throw new RangeError(`${name}: out needs ${offset + dimension} numbers, has ${out.length}.`);
12
+ }
13
+ return controlPoints.length / dimension;
14
+ }
15
+ // De Casteljau over scratch_[0 … count · dimension): writes the point at t.
16
+ function deCasteljau_(out, offset, count, dimension, t) {
17
+ const work = scratch_;
18
+ const u = 1 - t;
19
+ for (let level = count - 1; level > 0; level--) {
20
+ for (let index = 0; index < level * dimension; index++) {
21
+ work[index] = u * work[index] + t * work[index + dimension];
22
+ }
23
+ }
24
+ for (let axis = 0; axis < dimension; axis++) {
25
+ out[offset + axis] = work[axis];
26
+ }
27
+ }
28
+ function reserve_(length) {
29
+ if (scratch_.length < length) {
30
+ scratch_ = new Float64Array(Math.max(length, 2 * scratch_.length));
31
+ }
32
+ }
33
+ /**
34
+ * Evaluates the Bézier curve of any degree at `t` by De Casteljau's algorithm:
35
+ * repeated linear interpolation between the control points, the most stable
36
+ * evaluation (every step a convex combination for `t ∈ [0, 1]`). O(n² ·
37
+ * dimension) for `n` control points; slower than the closed forms
38
+ * (`cubicBezier2`, …) but degree-agnostic.
39
+ *
40
+ * @param out - Receives the `dimension` coordinates at `offset`.
41
+ * @param offset - Index in `out` of the first coordinate written.
42
+ * @param controlPoints - The `n ≥ 1` control points, `dimension` numbers each
43
+ * (degree `n − 1`). Only read; may share memory with `out`.
44
+ * @param dimension - Coordinates per point, a positive integer.
45
+ * @param t - Curve parameter, normally in `[0, 1]`.
46
+ * @returns `out`.
47
+ * @throws {RangeError} When `dimension` or `offset` is not an integer in range,
48
+ * `controlPoints` is empty or not a multiple of `dimension` long, or `out` is
49
+ * too short.
50
+ */
51
+ export function bezierN(out, offset, controlPoints, dimension, t) {
52
+ const count = checkBezier_("bezierN", out, offset, controlPoints, dimension);
53
+ reserve_(controlPoints.length);
54
+ scratch_.set(controlPoints);
55
+ deCasteljau_(out, offset, count, dimension, t);
56
+ return out;
57
+ }
58
+ /**
59
+ * Evaluates the derivative `B′(t)` of the Bézier curve of any degree: itself a
60
+ * Bézier curve of degree `n − 2` whose control points are `(n − 1)·(Pᵢ₊₁ −
61
+ * Pᵢ)`, evaluated by De Casteljau's algorithm. The tangent direction at `t`;
62
+ * zero for a single control point. O(n² · dimension).
63
+ *
64
+ * @param out - Receives the `dimension` derivative coordinates at `offset`.
65
+ * @param offset - Index in `out` of the first coordinate written.
66
+ * @param controlPoints - The `n ≥ 1` control points, `dimension` numbers each.
67
+ * Only read; may share memory with `out`.
68
+ * @param dimension - Coordinates per point, a positive integer.
69
+ * @param t - Curve parameter, normally in `[0, 1]`.
70
+ * @returns `out`.
71
+ * @throws {RangeError} As {@link bezierN}.
72
+ */
73
+ export function bezierDerivative(out, offset, controlPoints, dimension, t) {
74
+ const count = checkBezier_("bezierDerivative", out, offset, controlPoints, dimension);
75
+ if (count === 1) {
76
+ out.fill(0, offset, offset + dimension);
77
+ return out;
78
+ }
79
+ const degree = count - 1;
80
+ const length = degree * dimension;
81
+ reserve_(length);
82
+ const work = scratch_;
83
+ for (let index = 0; index < length; index++) {
84
+ work[index] =
85
+ degree * (controlPoints[index + dimension] - controlPoints[index]);
86
+ }
87
+ deCasteljau_(out, offset, degree, dimension, t);
88
+ return out;
89
+ }
90
+ /**
91
+ * The Bézier curve with these control points as a {@link ParametricCurve}, for
92
+ * arc length and flattening. The control points are copied.
93
+ *
94
+ * @param controlPoints - The `n ≥ 1` control points, `dimension` numbers each.
95
+ * @param dimension - Coordinates per point, a positive integer.
96
+ * @throws {RangeError} When `dimension` is not a positive integer or
97
+ * `controlPoints` is empty or not a multiple of `dimension` long.
98
+ */
99
+ export function bezierCurve(controlPoints, dimension) {
100
+ checkBezier_("bezierCurve", controlPoints, 0, controlPoints, dimension);
101
+ const points = controlPoints.slice();
102
+ return {
103
+ dimension,
104
+ point: (out, offset, t) => {
105
+ bezierN(out, offset, points, dimension, t);
106
+ },
107
+ derivative: (out, offset, t) => {
108
+ bezierDerivative(out, offset, points, dimension, t);
109
+ },
110
+ breaks: new Float64Array(0),
111
+ };
112
+ }
113
+ //# sourceMappingURL=bezier-n.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bezier-n.js","sourceRoot":"","sources":["../src/bezier-n.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAI7C,yEAAyE;AACzE,IAAI,QAAQ,GAAG,IAAI,YAAY,CAAC,EAAE,CAAC,CAAC;AAEpC,SAAS,YAAY,CACpB,IAAY,EACZ,GAAiB,EACjB,MAAc,EACd,aAA2B,EAC3B,SAAiB;IAEjB,aAAa,CAAC,SAAS,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC;IACzC,aAAa,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC;IACnC,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,IAAI,aAAa,CAAC,MAAM,GAAG,SAAS,KAAK,CAAC,EAAE,CAAC;QAC1E,MAAM,IAAI,UAAU,CACnB,GAAG,IAAI,oDAAoD,SAAS,iBAAiB,aAAa,CAAC,MAAM,GAAG,CAC5G,CAAC;IACH,CAAC;IACD,IAAI,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,CAAC;QACrC,MAAM,IAAI,UAAU,CACnB,GAAG,IAAI,eAAe,MAAM,GAAG,SAAS,iBAAiB,GAAG,CAAC,MA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IAEjB,YAAY,CAAC,aAAa,EAAE,aAAa,EAAE,CAAC,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC;IACxE,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,EAAE,CAAC;IAErC,OAAO;QACN,SAAS;QACT,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE;YACzB,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;QAC5C,CAAC;QACD,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE;YAC9B,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;QACrD,CAAC;QACD,MAAM,EAAE,IAAI,YAAY,CAAC,CAAC,CAAC;KAC3B,CAAC;AACH,CAAC"}
package/dist/bezier2.d.ts CHANGED
@@ -1,4 +1,16 @@
1
1
  import type { ReadonlyVec2, Vec2 } from "@ac-kit/math-linear";
2
+ /**
3
+ * Evaluates the linear Bézier curve — the segment — from `p0` to `p1` at `t`:
4
+ * `B(t) = (1 − t)·p0 + t·p1`, exactly `p0` at `t = 0` and `p1` at `t = 1`.
5
+ * `out` may be either point.
6
+ *
7
+ * @param out - Receives the point on the segment at `t`.
8
+ * @param p0 - Start point.
9
+ * @param p1 - End point.
10
+ * @param t - Curve parameter, normally in `[0, 1]`.
11
+ * @returns `out`.
12
+ */
13
+ export declare function linearBezier2(out: Vec2, p0: ReadonlyVec2, p1: ReadonlyVec2, t: number): Vec2;
2
14
  /**
3
15
  * Evaluates a quadratic Bézier curve at parameter `t` using the Bernstein form:
4
16
  *
@@ -1 +1 @@
1
- {"version":3,"file":"bezier2.d.ts","sourceRoot":"","sources":["../src/bezier2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;GAkBG;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,CAWN;AAED;;;;;;;;;;;;;;;;;GAiBG;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,CAcN"}
1
+ {"version":3,"file":"bezier2.d.ts","sourceRoot":"","sources":["../src/bezier2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAE9D;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAC5B,GAAG,EAAE,IAAI,EACT,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,CAAC,EAAE,MAAM,GACP,IAAI,CAQN;AAED;;;;;;;;;;;;;;;;;;GAkBG;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,CAWN;AAED;;;;;;;;;;;;;;;;;GAiBG;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,CAcN"}
package/dist/bezier2.js CHANGED
@@ -1,3 +1,22 @@
1
+ /**
2
+ * Evaluates the linear Bézier curve — the segment — from `p0` to `p1` at `t`:
3
+ * `B(t) = (1 − t)·p0 + t·p1`, exactly `p0` at `t = 0` and `p1` at `t = 1`.
4
+ * `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 function linearBezier2(out, p0, p1, t) {
13
+ const u = 1 - t;
14
+ const x = u * p0[0] + t * p1[0];
15
+ const y = u * p0[1] + t * p1[1];
16
+ out[0] = x;
17
+ out[1] = y;
18
+ return out;
19
+ }
1
20
  /**
2
21
  * Evaluates a quadratic Bézier curve at parameter `t` using the Bernstein form:
3
22
  *
@@ -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;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