@motionscript/math 0.0.0-stage → 0.1.0-alpha.10
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 +144 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +389 -2
- package/dist/annotate/angle-mark.d.ts +96 -0
- package/dist/annotate/angle-mark.d.ts.map +1 -0
- package/dist/annotate/angle-mark.js +97 -0
- package/dist/annotate/angle-mark.js.map +1 -0
- package/dist/annotate/curly-brace.d.ts +115 -0
- package/dist/annotate/curly-brace.d.ts.map +1 -0
- package/dist/annotate/curly-brace.js +99 -0
- package/dist/annotate/curly-brace.js.map +1 -0
- package/dist/annotate/emphasis-mark.d.ts +80 -0
- package/dist/annotate/emphasis-mark.d.ts.map +1 -0
- package/dist/annotate/emphasis-mark.js +80 -0
- package/dist/annotate/emphasis-mark.js.map +1 -0
- package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
- package/dist/browser/chunks/chunk-IRZCG6PM.js +2 -0
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/chunk-WMVW6ZIV.js +2 -0
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/mathjax-WV55AC2P.js +61 -0
- package/dist/browser/chunks/mathjax-WV55AC2P.js.map +7 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +7 -0
- package/dist/browser/kit.js +2 -0
- package/dist/browser/kit.js.map +7 -0
- package/dist/browser/manifest.json +30 -0
- package/dist/browser/tex.js +2 -0
- package/dist/browser/tex.js.map +7 -0
- package/dist/engine.d.ts +71 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +45 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/brace.d.ts +8 -0
- package/dist/kit/brace.d.ts.map +1 -0
- package/dist/kit/brace.js +36 -0
- package/dist/kit/brace.js.map +1 -0
- package/dist/kit/cache.d.ts +30 -0
- package/dist/kit/cache.d.ts.map +1 -0
- package/dist/kit/cache.js +53 -0
- package/dist/kit/cache.js.map +1 -0
- package/dist/kit/component.d.ts +51 -0
- package/dist/kit/component.d.ts.map +1 -0
- package/dist/kit/component.js +30 -0
- package/dist/kit/component.js.map +1 -0
- package/dist/kit/contour.d.ts +8 -0
- package/dist/kit/contour.d.ts.map +1 -0
- package/dist/kit/contour.js +93 -0
- package/dist/kit/contour.js.map +1 -0
- package/dist/kit/coords.d.ts +69 -0
- package/dist/kit/coords.d.ts.map +1 -0
- package/dist/kit/coords.js +224 -0
- package/dist/kit/coords.js.map +1 -0
- package/dist/kit/curve.d.ts +28 -0
- package/dist/kit/curve.d.ts.map +1 -0
- package/dist/kit/curve.js +29 -0
- package/dist/kit/curve.js.map +1 -0
- package/dist/kit/equation.d.ts +61 -0
- package/dist/kit/equation.d.ts.map +1 -0
- package/dist/kit/equation.js +168 -0
- package/dist/kit/equation.js.map +1 -0
- package/dist/kit/equation3d.d.ts +50 -0
- package/dist/kit/equation3d.d.ts.map +1 -0
- package/dist/kit/equation3d.js +103 -0
- package/dist/kit/equation3d.js.map +1 -0
- package/dist/kit/expression.d.ts +35 -0
- package/dist/kit/expression.d.ts.map +1 -0
- package/dist/kit/expression.js +124 -0
- package/dist/kit/expression.js.map +1 -0
- package/dist/kit/field-mark.d.ts +79 -0
- package/dist/kit/field-mark.d.ts.map +1 -0
- package/dist/kit/field-mark.js +109 -0
- package/dist/kit/field-mark.js.map +1 -0
- package/dist/kit/field.d.ts +14 -0
- package/dist/kit/field.d.ts.map +1 -0
- package/dist/kit/field.js +46 -0
- package/dist/kit/field.js.map +1 -0
- package/dist/kit/follow.d.ts +109 -0
- package/dist/kit/follow.d.ts.map +1 -0
- package/dist/kit/follow.js +163 -0
- package/dist/kit/follow.js.map +1 -0
- package/dist/kit/furniture.d.ts +32 -0
- package/dist/kit/furniture.d.ts.map +1 -0
- package/dist/kit/furniture.js +92 -0
- package/dist/kit/furniture.js.map +1 -0
- package/dist/kit/geometry.d.ts +13 -0
- package/dist/kit/geometry.d.ts.map +1 -0
- package/dist/kit/geometry.js +54 -0
- package/dist/kit/geometry.js.map +1 -0
- package/dist/kit/graph.d.ts +46 -0
- package/dist/kit/graph.d.ts.map +1 -0
- package/dist/kit/graph.js +90 -0
- package/dist/kit/graph.js.map +1 -0
- package/dist/kit/mark.d.ts +72 -0
- package/dist/kit/mark.d.ts.map +1 -0
- package/dist/kit/mark.js +87 -0
- package/dist/kit/mark.js.map +1 -0
- package/dist/kit/part-fills.d.ts +23 -0
- package/dist/kit/part-fills.d.ts.map +1 -0
- package/dist/kit/part-fills.js +46 -0
- package/dist/kit/part-fills.js.map +1 -0
- package/dist/kit/place.d.ts +20 -0
- package/dist/kit/place.d.ts.map +1 -0
- package/dist/kit/place.js +66 -0
- package/dist/kit/place.js.map +1 -0
- package/dist/kit/sampler.d.ts +17 -0
- package/dist/kit/sampler.d.ts.map +1 -0
- package/dist/kit/sampler.js +250 -0
- package/dist/kit/sampler.js.map +1 -0
- package/dist/kit/scale.d.ts +12 -0
- package/dist/kit/scale.d.ts.map +1 -0
- package/dist/kit/scale.js +37 -0
- package/dist/kit/scale.js.map +1 -0
- package/dist/kit/source.d.ts +22 -0
- package/dist/kit/source.d.ts.map +1 -0
- package/dist/kit/source.js +45 -0
- package/dist/kit/source.js.map +1 -0
- package/dist/kit/system.d.ts +31 -0
- package/dist/kit/system.d.ts.map +1 -0
- package/dist/kit/system.js +71 -0
- package/dist/kit/system.js.map +1 -0
- package/dist/kit/ticks.d.ts +19 -0
- package/dist/kit/ticks.d.ts.map +1 -0
- package/dist/kit/ticks.js +78 -0
- package/dist/kit/ticks.js.map +1 -0
- package/dist/kit/withdrawn.d.ts +9 -0
- package/dist/kit/withdrawn.d.ts.map +1 -0
- package/dist/kit/withdrawn.js +9 -0
- package/dist/kit/withdrawn.js.map +1 -0
- package/dist/marks/curve-area.d.ts +51 -0
- package/dist/marks/curve-area.d.ts.map +1 -0
- package/dist/marks/curve-area.js +59 -0
- package/dist/marks/curve-area.js.map +1 -0
- package/dist/marks/drop-lines.d.ts +57 -0
- package/dist/marks/drop-lines.d.ts.map +1 -0
- package/dist/marks/drop-lines.js +43 -0
- package/dist/marks/drop-lines.js.map +1 -0
- package/dist/marks/plot-point.d.ts +71 -0
- package/dist/marks/plot-point.d.ts.map +1 -0
- package/dist/marks/plot-point.js +71 -0
- package/dist/marks/plot-point.js.map +1 -0
- package/dist/marks/riemann-sum.d.ts +58 -0
- package/dist/marks/riemann-sum.d.ts.map +1 -0
- package/dist/marks/riemann-sum.js +63 -0
- package/dist/marks/riemann-sum.js.map +1 -0
- package/dist/marks/tangent-line.d.ts +71 -0
- package/dist/marks/tangent-line.d.ts.map +1 -0
- package/dist/marks/tangent-line.js +89 -0
- package/dist/marks/tangent-line.js.map +1 -0
- package/dist/marks/vector-arrow.d.ts +60 -0
- package/dist/marks/vector-arrow.d.ts.map +1 -0
- package/dist/marks/vector-arrow.js +67 -0
- package/dist/marks/vector-arrow.js.map +1 -0
- package/dist/nodes.d.ts +43 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +57 -0
- package/dist/nodes.js.map +1 -0
- package/dist/plots/function-plot.d.ts +159 -0
- package/dist/plots/function-plot.d.ts.map +1 -0
- package/dist/plots/function-plot.js +184 -0
- package/dist/plots/function-plot.js.map +1 -0
- package/dist/plots/points-plot.d.ts +64 -0
- package/dist/plots/points-plot.d.ts.map +1 -0
- package/dist/plots/points-plot.js +72 -0
- package/dist/plots/points-plot.js.map +1 -0
- package/dist/plots/stream-lines.d.ts +76 -0
- package/dist/plots/stream-lines.d.ts.map +1 -0
- package/dist/plots/stream-lines.js +122 -0
- package/dist/plots/stream-lines.js.map +1 -0
- package/dist/plots/traced-path.d.ts +70 -0
- package/dist/plots/traced-path.d.ts.map +1 -0
- package/dist/plots/traced-path.js +41 -0
- package/dist/plots/traced-path.js.map +1 -0
- package/dist/plots/vector-field.d.ts +60 -0
- package/dist/plots/vector-field.d.ts.map +1 -0
- package/dist/plots/vector-field.js +77 -0
- package/dist/plots/vector-field.js.map +1 -0
- package/dist/structures/graph-layout.d.ts +11 -0
- package/dist/structures/graph-layout.d.ts.map +1 -0
- package/dist/structures/graph-layout.js +135 -0
- package/dist/structures/graph-layout.js.map +1 -0
- package/dist/structures/network-graph.d.ts +174 -0
- package/dist/structures/network-graph.d.ts.map +1 -0
- package/dist/structures/network-graph.js +240 -0
- package/dist/structures/network-graph.js.map +1 -0
- package/dist/structures/sample-space.d.ts +114 -0
- package/dist/structures/sample-space.d.ts.map +1 -0
- package/dist/structures/sample-space.js +161 -0
- package/dist/structures/sample-space.js.map +1 -0
- package/dist/systems/axes2d.d.ts +197 -0
- package/dist/systems/axes2d.d.ts.map +1 -0
- package/dist/systems/axes2d.js +157 -0
- package/dist/systems/axes2d.js.map +1 -0
- package/dist/systems/number-line.d.ts +101 -0
- package/dist/systems/number-line.d.ts.map +1 -0
- package/dist/systems/number-line.js +91 -0
- package/dist/systems/number-line.js.map +1 -0
- package/dist/systems/number-plane.d.ts +168 -0
- package/dist/systems/number-plane.d.ts.map +1 -0
- package/dist/systems/number-plane.js +186 -0
- package/dist/systems/number-plane.js.map +1 -0
- package/dist/systems/polar-plane.d.ts +98 -0
- package/dist/systems/polar-plane.d.ts.map +1 -0
- package/dist/systems/polar-plane.js +139 -0
- package/dist/systems/polar-plane.js.map +1 -0
- package/dist/tex/geometry.d.ts +85 -0
- package/dist/tex/geometry.d.ts.map +1 -0
- package/dist/tex/geometry.js +632 -0
- package/dist/tex/geometry.js.map +1 -0
- package/dist/tex/index.d.ts +11 -0
- package/dist/tex/index.d.ts.map +1 -0
- package/dist/tex/index.js +10 -0
- package/dist/tex/index.js.map +1 -0
- package/dist/tex/mathjax.d.ts +33 -0
- package/dist/tex/mathjax.d.ts.map +1 -0
- package/dist/tex/mathjax.js +74 -0
- package/dist/tex/mathjax.js.map +1 -0
- package/dist/tex/matrix.d.ts +101 -0
- package/dist/tex/matrix.d.ts.map +1 -0
- package/dist/tex/matrix.js +56 -0
- package/dist/tex/matrix.js.map +1 -0
- package/dist/tex/node.d.ts +98 -0
- package/dist/tex/node.d.ts.map +1 -0
- package/dist/tex/node.js +25 -0
- package/dist/tex/node.js.map +1 -0
- package/dist/tex/tween.d.ts +50 -0
- package/dist/tex/tween.d.ts.map +1 -0
- package/dist/tex/tween.js +253 -0
- package/dist/tex/tween.js.map +1 -0
- package/dist/tex/typeset.d.ts +133 -0
- package/dist/tex/typeset.d.ts.map +1 -0
- package/dist/tex/typeset.js +403 -0
- package/dist/tex/typeset.js.map +1 -0
- package/dist/three/axes3d.d.ts +247 -0
- package/dist/three/axes3d.d.ts.map +1 -0
- package/dist/three/axes3d.js +187 -0
- package/dist/three/axes3d.js.map +1 -0
- package/dist/three/curve-plot3d.d.ts +126 -0
- package/dist/three/curve-plot3d.d.ts.map +1 -0
- package/dist/three/curve-plot3d.js +109 -0
- package/dist/three/curve-plot3d.js.map +1 -0
- package/dist/three/entries.d.ts +17 -0
- package/dist/three/entries.d.ts.map +1 -0
- package/dist/three/entries.js +15 -0
- package/dist/three/entries.js.map +1 -0
- package/dist/three/label3d.d.ts +11 -0
- package/dist/three/label3d.d.ts.map +1 -0
- package/dist/three/label3d.js +42 -0
- package/dist/three/label3d.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +99 -0
- package/dist/three/plot-point3d.d.ts.map +1 -0
- package/dist/three/plot-point3d.js +34 -0
- package/dist/three/plot-point3d.js.map +1 -0
- package/dist/three/space.d.ts +43 -0
- package/dist/three/space.d.ts.map +1 -0
- package/dist/three/space.js +58 -0
- package/dist/three/space.js.map +1 -0
- package/dist/three/surface-plot3d.d.ts +207 -0
- package/dist/three/surface-plot3d.d.ts.map +1 -0
- package/dist/three/surface-plot3d.js +261 -0
- package/dist/three/surface-plot3d.js.map +1 -0
- package/dist/three/vector-arrow3d.d.ts +96 -0
- package/dist/three/vector-arrow3d.d.ts.map +1 -0
- package/dist/three/vector-arrow3d.js +53 -0
- package/dist/three/vector-arrow3d.js.map +1 -0
- package/package.json +80 -3
- package/registry.json +251 -0
- package/src/annotate/angle-mark.ts +137 -0
- package/src/annotate/curly-brace.ts +148 -0
- package/src/annotate/emphasis-mark.ts +118 -0
- package/src/engine.ts +86 -0
- package/src/index.ts +29 -0
- package/src/kit/brace.ts +37 -0
- package/src/kit/cache.ts +61 -0
- package/src/kit/component.ts +57 -0
- package/src/kit/contour.ts +94 -0
- package/src/kit/coords.ts +278 -0
- package/src/kit/curve.ts +47 -0
- package/src/kit/equation.ts +181 -0
- package/src/kit/equation3d.ts +137 -0
- package/src/kit/expression.ts +159 -0
- package/src/kit/field-mark.ts +171 -0
- package/src/kit/field.ts +50 -0
- package/src/kit/follow.ts +229 -0
- package/src/kit/furniture.ts +113 -0
- package/src/kit/geometry.ts +49 -0
- package/src/kit/graph.ts +116 -0
- package/src/kit/mark.ts +152 -0
- package/src/kit/part-fills.ts +61 -0
- package/src/kit/place.ts +77 -0
- package/src/kit/sampler.ts +281 -0
- package/src/kit/scale.ts +39 -0
- package/src/kit/source.ts +57 -0
- package/src/kit/system.ts +105 -0
- package/src/kit/ticks.ts +85 -0
- package/src/kit/withdrawn.ts +8 -0
- package/src/marks/curve-area.ts +89 -0
- package/src/marks/drop-lines.ts +71 -0
- package/src/marks/plot-point.ts +102 -0
- package/src/marks/riemann-sum.ts +99 -0
- package/src/marks/tangent-line.ts +119 -0
- package/src/marks/vector-arrow.ts +98 -0
- package/src/nodes.ts +99 -0
- package/src/plots/function-plot.ts +220 -0
- package/src/plots/points-plot.ts +103 -0
- package/src/plots/stream-lines.ts +160 -0
- package/src/plots/traced-path.ts +70 -0
- package/src/plots/vector-field.ts +107 -0
- package/src/structures/graph-layout.ts +145 -0
- package/src/structures/network-graph.ts +291 -0
- package/src/structures/sample-space.ts +210 -0
- package/src/systems/axes2d.ts +226 -0
- package/src/systems/number-line.ts +130 -0
- package/src/systems/number-plane.ts +232 -0
- package/src/systems/polar-plane.ts +177 -0
- package/src/tex/geometry.ts +703 -0
- package/src/tex/index.ts +15 -0
- package/src/tex/mathjax.ts +82 -0
- package/src/tex/matrix.ts +97 -0
- package/src/tex/node.ts +74 -0
- package/src/tex/tween.ts +308 -0
- package/src/tex/typeset.ts +461 -0
- package/src/three/axes3d.ts +267 -0
- package/src/three/curve-plot3d.ts +146 -0
- package/src/three/entries.ts +42 -0
- package/src/three/label3d.ts +52 -0
- package/src/three/plot-point3d.ts +63 -0
- package/src/three/space.ts +92 -0
- package/src/three/surface-plot3d.ts +318 -0
- package/src/three/vector-arrow3d.ts +88 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The lines along which a function of two variables is zero, over a box of
|
|
3
|
+
* graph values: marching squares, its crossings placed by linear interpolation
|
|
4
|
+
* and joined end to end into runs. Points are graph coordinates.
|
|
5
|
+
*/
|
|
6
|
+
export function traceContour(f, [x0, x1], [y0, y1], columns, rows) {
|
|
7
|
+
if (!(x1 > x0) || !(y1 > y0) || columns < 1 || rows < 1)
|
|
8
|
+
return [];
|
|
9
|
+
const dx = (x1 - x0) / columns;
|
|
10
|
+
const dy = (y1 - y0) / rows;
|
|
11
|
+
const stride = columns + 1;
|
|
12
|
+
const values = new Float64Array(stride * (rows + 1));
|
|
13
|
+
for (let j = 0; j <= rows; j++) {
|
|
14
|
+
for (let i = 0; i <= columns; i++)
|
|
15
|
+
values[j * stride + i] = f(x0 + i * dx, y0 + j * dy);
|
|
16
|
+
}
|
|
17
|
+
// A crossing lives on a grid edge, which is its name: two cells sharing the edge share the point.
|
|
18
|
+
const points = new Map();
|
|
19
|
+
const edgeCount = stride * (rows + 1);
|
|
20
|
+
const crossing = (i, j, vertical) => {
|
|
21
|
+
const a = values[j * stride + i];
|
|
22
|
+
const b = vertical ? values[(j + 1) * stride + i] : values[j * stride + i + 1];
|
|
23
|
+
if (!Number.isFinite(a) || !Number.isFinite(b) || a < 0 === b < 0)
|
|
24
|
+
return null;
|
|
25
|
+
const key = (vertical ? edgeCount : 0) + j * stride + i;
|
|
26
|
+
if (!points.has(key)) {
|
|
27
|
+
const t = a / (a - b);
|
|
28
|
+
points.set(key, vertical ? { x: x0 + i * dx, y: y0 + (j + t) * dy } : { x: x0 + (i + t) * dx, y: y0 + j * dy });
|
|
29
|
+
}
|
|
30
|
+
return key;
|
|
31
|
+
};
|
|
32
|
+
const next = new Map();
|
|
33
|
+
const join = (a, b) => {
|
|
34
|
+
if (a === null || b === null)
|
|
35
|
+
return;
|
|
36
|
+
(next.get(a) ?? next.set(a, []).get(a)).push(b);
|
|
37
|
+
(next.get(b) ?? next.set(b, []).get(b)).push(a);
|
|
38
|
+
};
|
|
39
|
+
for (let j = 0; j < rows; j++) {
|
|
40
|
+
for (let i = 0; i < columns; i++) {
|
|
41
|
+
const bottom = crossing(i, j, false);
|
|
42
|
+
const top = crossing(i, j + 1, false);
|
|
43
|
+
const left = crossing(i, j, true);
|
|
44
|
+
const right = crossing(i + 1, j, true);
|
|
45
|
+
const found = [bottom, right, top, left].filter((edge) => edge !== null);
|
|
46
|
+
if (found.length === 2)
|
|
47
|
+
join(found[0], found[1]);
|
|
48
|
+
else if (found.length === 4) {
|
|
49
|
+
// A saddle: the centre's sign says which pairs of edges the curve joins.
|
|
50
|
+
const centre = f(x0 + (i + 0.5) * dx, y0 + (j + 0.5) * dy);
|
|
51
|
+
const corner = values[j * stride + i];
|
|
52
|
+
if (centre < 0 === corner < 0) {
|
|
53
|
+
join(bottom, right);
|
|
54
|
+
join(top, left);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
join(bottom, left);
|
|
58
|
+
join(top, right);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const runs = [];
|
|
64
|
+
const used = new Set();
|
|
65
|
+
const walk = (from) => {
|
|
66
|
+
const run = [points.get(from)];
|
|
67
|
+
used.add(from);
|
|
68
|
+
let at = from;
|
|
69
|
+
for (;;) {
|
|
70
|
+
const onward = (next.get(at) ?? []).find((key) => !used.has(key));
|
|
71
|
+
if (onward === undefined)
|
|
72
|
+
break;
|
|
73
|
+
used.add(onward);
|
|
74
|
+
run.push(points.get(onward));
|
|
75
|
+
at = onward;
|
|
76
|
+
}
|
|
77
|
+
return run;
|
|
78
|
+
};
|
|
79
|
+
// Open runs first, from their ends, so each is one run and not two halves.
|
|
80
|
+
for (const [key, links] of next) {
|
|
81
|
+
if (links.length === 1 && !used.has(key))
|
|
82
|
+
runs.push(walk(key));
|
|
83
|
+
}
|
|
84
|
+
for (const key of next.keys()) {
|
|
85
|
+
if (used.has(key))
|
|
86
|
+
continue;
|
|
87
|
+
const run = walk(key);
|
|
88
|
+
run.push(run[0]);
|
|
89
|
+
runs.push(run);
|
|
90
|
+
}
|
|
91
|
+
return runs.filter((run) => run.length > 1);
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=contour.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contour.js","sourceRoot":"","sources":["../../src/kit/contour.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,YAAY,CACxB,CAAmC,EACnC,CAAC,EAAE,EAAE,EAAE,CAA4B,EACnC,CAAC,EAAE,EAAE,EAAE,CAA4B,EACnC,OAAe,EACf,IAAY;IAEZ,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,IAAI,OAAO,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC;QAAE,OAAO,EAAE,CAAC;IACnE,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC;IAC/B,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC;IAC5B,MAAM,MAAM,GAAG,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,CAAC;YAAE,SAAS;QAC5B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QACtB,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACjB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;IACD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import type { Vector2 } from \"@motionscript/sdk\";\n\n/**\n * The lines along which a function of two variables is zero, over a box of\n * graph values: marching squares, its crossings placed by linear interpolation\n * and joined end to end into runs. Points are graph coordinates.\n */\nexport function traceContour(\n f: (x: number, y: number) => number,\n [x0, x1]: readonly [number, number],\n [y0, y1]: readonly [number, number],\n columns: number,\n rows: number,\n): Vector2[][] {\n if (!(x1 > x0) || !(y1 > y0) || columns < 1 || rows < 1) return [];\n const dx = (x1 - x0) / columns;\n const dy = (y1 - y0) / rows;\n const stride = columns + 1;\n const values = new Float64Array(stride * (rows + 1));\n for (let j = 0; j <= rows; j++) {\n for (let i = 0; i <= columns; i++) values[j * stride + i] = f(x0 + i * dx, y0 + j * dy);\n }\n\n // A crossing lives on a grid edge, which is its name: two cells sharing the edge share the point.\n const points = new Map<number, Vector2>();\n const edgeCount = stride * (rows + 1);\n const crossing = (i: number, j: number, vertical: boolean): number | null => {\n const a = values[j * stride + i];\n const b = vertical ? values[(j + 1) * stride + i] : values[j * stride + i + 1];\n if (!Number.isFinite(a) || !Number.isFinite(b) || a < 0 === b < 0) return null;\n const key = (vertical ? edgeCount : 0) + j * stride + i;\n if (!points.has(key)) {\n const t = a / (a - b);\n points.set(key, vertical ? { x: x0 + i * dx, y: y0 + (j + t) * dy } : { x: x0 + (i + t) * dx, y: y0 + j * dy });\n }\n return key;\n };\n\n const next = new Map<number, number[]>();\n const join = (a: number | null, b: number | null): void => {\n if (a === null || b === null) return;\n (next.get(a) ?? next.set(a, []).get(a)!).push(b);\n (next.get(b) ?? next.set(b, []).get(b)!).push(a);\n };\n for (let j = 0; j < rows; j++) {\n for (let i = 0; i < columns; i++) {\n const bottom = crossing(i, j, false);\n const top = crossing(i, j + 1, false);\n const left = crossing(i, j, true);\n const right = crossing(i + 1, j, true);\n const found = [bottom, right, top, left].filter((edge) => edge !== null);\n if (found.length === 2) join(found[0], found[1]);\n else if (found.length === 4) {\n // A saddle: the centre's sign says which pairs of edges the curve joins.\n const centre = f(x0 + (i + 0.5) * dx, y0 + (j + 0.5) * dy);\n const corner = values[j * stride + i];\n if (centre < 0 === corner < 0) {\n join(bottom, right);\n join(top, left);\n } else {\n join(bottom, left);\n join(top, right);\n }\n }\n }\n }\n\n const runs: Vector2[][] = [];\n const used = new Set<number>();\n const walk = (from: number): Vector2[] => {\n const run: Vector2[] = [points.get(from)!];\n used.add(from);\n let at = from;\n for (;;) {\n const onward = (next.get(at) ?? []).find((key) => !used.has(key));\n if (onward === undefined) break;\n used.add(onward);\n run.push(points.get(onward)!);\n at = onward;\n }\n return run;\n };\n // Open runs first, from their ends, so each is one run and not two halves.\n for (const [key, links] of next) {\n if (links.length === 1 && !used.has(key)) runs.push(walk(key));\n }\n for (const key of next.keys()) {\n if (used.has(key)) continue;\n const run = walk(key);\n run.push(run[0]);\n runs.push(run);\n }\n return runs.filter((run) => run.length > 1);\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Extents, Vector2 } from "@motionscript/sdk";
|
|
2
|
+
/** A closed span of graph values, low to high. */
|
|
3
|
+
export type Interval = readonly [number, number];
|
|
4
|
+
export type AxisScaling = "linear" | "log";
|
|
5
|
+
/**
|
|
6
|
+
* How a coordinate system's graph values land on its pixels. Pixels are y-up
|
|
7
|
+
* from the centre of the system's own box, which is the frame a `Graphics2D` op
|
|
8
|
+
* is written in.
|
|
9
|
+
*/
|
|
10
|
+
export interface CoordinateMap {
|
|
11
|
+
/** Graph to pixels. Writes into `out` when given, so a sampler allocates nothing. */
|
|
12
|
+
toPoint(x: number, y: number, out?: Vector2): Vector2;
|
|
13
|
+
toCoords(x: number, y: number, out?: Vector2): Vector2;
|
|
14
|
+
/** The plot area. */
|
|
15
|
+
readonly area: Extents;
|
|
16
|
+
readonly xRange: Interval;
|
|
17
|
+
readonly yRange: Interval;
|
|
18
|
+
/** Equal for two maps that place every point alike: what a geometry cache keys on. */
|
|
19
|
+
readonly revision: string;
|
|
20
|
+
/** Set where a straight run of graph values is not a straight run of pixels, so a line through the map is sampled. */
|
|
21
|
+
readonly curved?: boolean;
|
|
22
|
+
/** Set where what is ruled over the ranges can leave the plot area, so the system keeps it in. */
|
|
23
|
+
readonly spills?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** One axis: a graph value to a pixel and back. */
|
|
26
|
+
export interface AxisScale {
|
|
27
|
+
to(value: number): number;
|
|
28
|
+
from(pixel: number): number;
|
|
29
|
+
}
|
|
30
|
+
/** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */
|
|
31
|
+
export declare function resolveInterval(range: Interval | undefined, scaling?: AxisScaling): Interval;
|
|
32
|
+
/** `range` spread over the pixels from `lo` to `hi`. */
|
|
33
|
+
export declare function axisScale(range: Interval, lo: number, hi: number, scaling?: AxisScaling): AxisScale;
|
|
34
|
+
export interface CartesianOptions {
|
|
35
|
+
xRange: Interval;
|
|
36
|
+
yRange: Interval;
|
|
37
|
+
area: Extents;
|
|
38
|
+
xScaling?: AxisScaling;
|
|
39
|
+
yScaling?: AxisScaling;
|
|
40
|
+
}
|
|
41
|
+
/** Two independent axes at right angles: what a number line, axes and a number plane share. */
|
|
42
|
+
export declare function cartesianMap({ xRange, yRange, area, xScaling, yScaling }: CartesianOptions): CoordinateMap;
|
|
43
|
+
/** One graph unit is one pixel: what a mark outside any system is drawn through. */
|
|
44
|
+
export declare const PIXEL_MAP: CoordinateMap;
|
|
45
|
+
/** A 2 × 2 matrix, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
|
|
46
|
+
export type Matrix2 = readonly [number, number, number, number];
|
|
47
|
+
export declare const IDENTITY_MATRIX: Matrix2;
|
|
48
|
+
/** A matrix from its rows, or flat; anything else is the identity. */
|
|
49
|
+
export declare function readMatrix(value: unknown, previous?: Matrix2): Matrix2;
|
|
50
|
+
export declare function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2;
|
|
51
|
+
export declare function isIdentityMatrix([a, b, c, d]: Matrix2): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* `base` with every graph point sent through `matrix` first: a plane sheared,
|
|
54
|
+
* turned or squeezed about its origin. Its ranges are what the plot area then
|
|
55
|
+
* shows, up to {@link REACH}, so a grid ruled over them fills the box under a
|
|
56
|
+
* turn, a shear or a fourfold shrink, and folds into a band as it goes flat.
|
|
57
|
+
*/
|
|
58
|
+
export declare function transformedMap(base: CoordinateMap, matrix: Matrix2): CoordinateMap;
|
|
59
|
+
/** Where a warp sends a graph point, written into `out`; `false` where it has no value. */
|
|
60
|
+
export type Warp = (x: number, y: number, out: Vector2) => boolean;
|
|
61
|
+
/**
|
|
62
|
+
* `base` with every graph point sent to where `warp` sends it: a plane bent by
|
|
63
|
+
* a function of its own points. `key` names the warp for the revision, since a
|
|
64
|
+
* function has no text of its own.
|
|
65
|
+
*/
|
|
66
|
+
export declare function warpedMap(base: CoordinateMap, warp: Warp, key: string): CoordinateMap;
|
|
67
|
+
/** The pixel points that draw the straight run of graph values from one point to another. */
|
|
68
|
+
export declare function lineThrough(map: CoordinateMap, x0: number, y0: number, x1: number, y1: number): Vector2[];
|
|
69
|
+
//# sourceMappingURL=coords.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coords.d.ts","sourceRoot":"","sources":["../../src/kit/coords.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE1D,kDAAkD;AAClD,MAAM,MAAM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,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,EAAE,CAQzG"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */
|
|
2
|
+
export function resolveInterval(range, scaling = "linear") {
|
|
3
|
+
let [min, max] = range ?? [0, 1];
|
|
4
|
+
if (!Number.isFinite(min) || !Number.isFinite(max))
|
|
5
|
+
[min, max] = [0, 1];
|
|
6
|
+
if (min > max)
|
|
7
|
+
[min, max] = [max, min];
|
|
8
|
+
if (scaling === "log") {
|
|
9
|
+
if (!(max > 0))
|
|
10
|
+
max = 10;
|
|
11
|
+
if (!(min > 0))
|
|
12
|
+
min = max / 1000;
|
|
13
|
+
}
|
|
14
|
+
return max > min ? [min, max] : [min, min + 1];
|
|
15
|
+
}
|
|
16
|
+
/** `range` spread over the pixels from `lo` to `hi`. */
|
|
17
|
+
export function axisScale(range, lo, hi, scaling = "linear") {
|
|
18
|
+
const [min, max] = range;
|
|
19
|
+
if (scaling === "log") {
|
|
20
|
+
const from = Math.log10(min);
|
|
21
|
+
const k = (hi - lo) / (Math.log10(max) - from);
|
|
22
|
+
return { to: (v) => lo + (Math.log10(v) - from) * k, from: (p) => 10 ** (from + (p - lo) / k) };
|
|
23
|
+
}
|
|
24
|
+
const k = (hi - lo) / (max - min);
|
|
25
|
+
return { to: (v) => lo + (v - min) * k, from: (p) => min + (p - lo) / k };
|
|
26
|
+
}
|
|
27
|
+
/** Two independent axes at right angles: what a number line, axes and a number plane share. */
|
|
28
|
+
export function cartesianMap({ xRange, yRange, area, xScaling = "linear", yScaling = "linear" }) {
|
|
29
|
+
const x = axisScale(xRange, area.minX, area.maxX, xScaling);
|
|
30
|
+
const y = axisScale(yRange, area.minY, area.maxY, yScaling);
|
|
31
|
+
return {
|
|
32
|
+
toPoint: (gx, gy, out = { x: 0, y: 0 }) => {
|
|
33
|
+
out.x = x.to(gx);
|
|
34
|
+
out.y = y.to(gy);
|
|
35
|
+
return out;
|
|
36
|
+
},
|
|
37
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
38
|
+
out.x = x.from(px);
|
|
39
|
+
out.y = y.from(py);
|
|
40
|
+
return out;
|
|
41
|
+
},
|
|
42
|
+
area,
|
|
43
|
+
xRange,
|
|
44
|
+
yRange,
|
|
45
|
+
revision: [xRange, yRange, area.minX, area.minY, area.maxX, area.maxY, xScaling, yScaling].join("|"),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const UNBOUNDED = { minX: -Infinity, minY: -Infinity, maxX: Infinity, maxY: Infinity };
|
|
49
|
+
/** One graph unit is one pixel: what a mark outside any system is drawn through. */
|
|
50
|
+
export const PIXEL_MAP = {
|
|
51
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
52
|
+
out.x = x;
|
|
53
|
+
out.y = y;
|
|
54
|
+
return out;
|
|
55
|
+
},
|
|
56
|
+
toCoords: (x, y, out = { x: 0, y: 0 }) => {
|
|
57
|
+
out.x = x;
|
|
58
|
+
out.y = y;
|
|
59
|
+
return out;
|
|
60
|
+
},
|
|
61
|
+
area: UNBOUNDED,
|
|
62
|
+
xRange: [-Infinity, Infinity],
|
|
63
|
+
yRange: [-Infinity, Infinity],
|
|
64
|
+
revision: "pixels",
|
|
65
|
+
};
|
|
66
|
+
export const IDENTITY_MATRIX = [1, 0, 0, 1];
|
|
67
|
+
/** A matrix from its rows, or flat; anything else is the identity. */
|
|
68
|
+
export function readMatrix(value, previous = IDENTITY_MATRIX) {
|
|
69
|
+
const flat = Array.isArray(value) ? value.flat() : [];
|
|
70
|
+
if (flat.length !== 4 || flat.some((entry) => typeof entry !== "number" || !Number.isFinite(entry)))
|
|
71
|
+
return previous;
|
|
72
|
+
return [flat[0], flat[1], flat[2], flat[3]];
|
|
73
|
+
}
|
|
74
|
+
export function lerpMatrix(from, to, t) {
|
|
75
|
+
return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t);
|
|
76
|
+
}
|
|
77
|
+
export function isIdentityMatrix([a, b, c, d]) {
|
|
78
|
+
return a === 1 && b === 0 && c === 0 && d === 1;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* How far a transformed plane reaches, in half-widths of its own ranges about
|
|
82
|
+
* their middle. Finite on purpose: a plane squeezed towards a line shows the
|
|
83
|
+
* pre-image of its box growing as 1/det, and ruling all of it packs the box
|
|
84
|
+
* with lines, a solid wash just before the plane goes flat.
|
|
85
|
+
*/
|
|
86
|
+
const REACH = 4;
|
|
87
|
+
function reach([min, max]) {
|
|
88
|
+
const middle = (min + max) / 2;
|
|
89
|
+
const half = ((max - min) / 2) * REACH;
|
|
90
|
+
return [middle - half, middle + half];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* `base` with every graph point sent through `matrix` first: a plane sheared,
|
|
94
|
+
* turned or squeezed about its origin. Its ranges are what the plot area then
|
|
95
|
+
* shows, up to {@link REACH}, so a grid ruled over them fills the box under a
|
|
96
|
+
* turn, a shear or a fourfold shrink, and folds into a band as it goes flat.
|
|
97
|
+
*/
|
|
98
|
+
export function transformedMap(base, matrix) {
|
|
99
|
+
const [a, b, c, d] = matrix;
|
|
100
|
+
const det = a * d - b * c;
|
|
101
|
+
// A plane squeezed onto a line has no way back: a point is read as the untransformed plane has it.
|
|
102
|
+
const flat = Math.abs(det) < 1e-9;
|
|
103
|
+
const back = (u, v, out) => {
|
|
104
|
+
if (flat) {
|
|
105
|
+
out.x = u;
|
|
106
|
+
out.y = v;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
out.x = (d * u - b * v) / det;
|
|
110
|
+
out.y = (a * v - c * u) / det;
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
};
|
|
114
|
+
const xReach = reach(base.xRange);
|
|
115
|
+
const yReach = reach(base.yRange);
|
|
116
|
+
let xRange = xReach;
|
|
117
|
+
let yRange = yReach;
|
|
118
|
+
if (!flat) {
|
|
119
|
+
const corner = { x: 0, y: 0 };
|
|
120
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
121
|
+
for (const u of base.xRange) {
|
|
122
|
+
for (const v of base.yRange) {
|
|
123
|
+
back(u, v, corner);
|
|
124
|
+
minX = Math.min(minX, corner.x);
|
|
125
|
+
maxX = Math.max(maxX, corner.x);
|
|
126
|
+
minY = Math.min(minY, corner.y);
|
|
127
|
+
maxY = Math.max(maxY, corner.y);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
xRange = [Math.max(minX, xReach[0]), Math.min(maxX, xReach[1])];
|
|
131
|
+
yRange = [Math.max(minY, yReach[0]), Math.min(maxY, yReach[1])];
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => base.toPoint(a * x + b * y, c * x + d * y, out),
|
|
135
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
136
|
+
base.toCoords(px, py, out);
|
|
137
|
+
return back(out.x, out.y, out);
|
|
138
|
+
},
|
|
139
|
+
area: base.area,
|
|
140
|
+
xRange,
|
|
141
|
+
yRange,
|
|
142
|
+
revision: `${base.revision}|m${matrix.join(",")}`,
|
|
143
|
+
spills: true,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* `base` with every graph point sent to where `warp` sends it: a plane bent by
|
|
148
|
+
* a function of its own points. `key` names the warp for the revision, since a
|
|
149
|
+
* function has no text of its own.
|
|
150
|
+
*/
|
|
151
|
+
export function warpedMap(base, warp, key) {
|
|
152
|
+
const bend = (x, y, out) => {
|
|
153
|
+
// A point the warp has no value at stays where it is, so a pole does not tear the grid.
|
|
154
|
+
if (!warp(x, y, out)) {
|
|
155
|
+
out.x = x;
|
|
156
|
+
out.y = y;
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
};
|
|
160
|
+
const at = { x: 0, y: 0 };
|
|
161
|
+
const dx = { x: 0, y: 0 };
|
|
162
|
+
const dy = { x: 0, y: 0 };
|
|
163
|
+
const H = 1e-4;
|
|
164
|
+
return {
|
|
165
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
166
|
+
bend(x, y, out);
|
|
167
|
+
return base.toPoint(out.x, out.y, out);
|
|
168
|
+
},
|
|
169
|
+
// No closed form: a few Newton steps from where the unbent plane has the point.
|
|
170
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
171
|
+
const target = base.toCoords(px, py);
|
|
172
|
+
let gx = target.x;
|
|
173
|
+
let gy = target.y;
|
|
174
|
+
for (let i = 0; i < 8; i++) {
|
|
175
|
+
bend(gx, gy, at);
|
|
176
|
+
const ex = at.x - target.x;
|
|
177
|
+
const ey = at.y - target.y;
|
|
178
|
+
if (Math.abs(ex) + Math.abs(ey) < 1e-9)
|
|
179
|
+
break;
|
|
180
|
+
bend(gx + H, gy, dx);
|
|
181
|
+
bend(gx, gy + H, dy);
|
|
182
|
+
const j00 = (dx.x - at.x) / H;
|
|
183
|
+
const j10 = (dx.y - at.y) / H;
|
|
184
|
+
const j01 = (dy.x - at.x) / H;
|
|
185
|
+
const j11 = (dy.y - at.y) / H;
|
|
186
|
+
const det = j00 * j11 - j01 * j10;
|
|
187
|
+
if (!(Math.abs(det) > 1e-12))
|
|
188
|
+
break;
|
|
189
|
+
gx -= (j11 * ex - j01 * ey) / det;
|
|
190
|
+
gy -= (j00 * ey - j10 * ex) / det;
|
|
191
|
+
}
|
|
192
|
+
out.x = Number.isFinite(gx) ? gx : target.x;
|
|
193
|
+
out.y = Number.isFinite(gy) ? gy : target.y;
|
|
194
|
+
return out;
|
|
195
|
+
},
|
|
196
|
+
area: base.area,
|
|
197
|
+
// A bent plane pulls its edges in somewhere: it is ruled a little past them so the box stays full.
|
|
198
|
+
xRange: widened(base.xRange),
|
|
199
|
+
yRange: widened(base.yRange),
|
|
200
|
+
revision: `${base.revision}|w${key}`,
|
|
201
|
+
curved: true,
|
|
202
|
+
spills: true,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/** How much further than its range, as a share of it, a warped plane is ruled each way. */
|
|
206
|
+
const WARP_MARGIN = 0.3;
|
|
207
|
+
function widened([min, max]) {
|
|
208
|
+
const margin = (max - min) * WARP_MARGIN;
|
|
209
|
+
return Number.isFinite(margin) ? [min - margin, max + margin] : [min, max];
|
|
210
|
+
}
|
|
211
|
+
/** How many pieces a line through a curved map is drawn in. */
|
|
212
|
+
const CURVED_STEPS = 48;
|
|
213
|
+
/** The pixel points that draw the straight run of graph values from one point to another. */
|
|
214
|
+
export function lineThrough(map, x0, y0, x1, y1) {
|
|
215
|
+
if (!map.curved)
|
|
216
|
+
return [map.toPoint(x0, y0), map.toPoint(x1, y1)];
|
|
217
|
+
const run = [];
|
|
218
|
+
for (let i = 0; i <= CURVED_STEPS; i++) {
|
|
219
|
+
const t = i / CURVED_STEPS;
|
|
220
|
+
run.push(map.toPoint(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t));
|
|
221
|
+
}
|
|
222
|
+
return run;
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=coords.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coords.js","sourceRoot":"","sources":["../../src/kit/coords.ts"],"names":[],"mappings":"AAkCA,oFAAoF;AACpF,MAAM,UAAU,eAAe,CAAC,KAA2B,EAAE,UAAuB,QAAQ;IACxF,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,KAAK,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACjC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACxE,IAAI,GAAG,GAAG,GAAG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D,OAAO,GAAG,CAAC;AACf,CAAC","sourcesContent":["import type { Extents, Vector2 } from \"@motionscript/sdk\";\n\n/** A closed span of graph values, low to high. */\nexport type Interval = readonly [number, number];\n\nexport type AxisScaling = \"linear\" | \"log\";\n\n/**\n * How a coordinate system's graph values land on its pixels. Pixels are y-up\n * from the centre of the system's own box, which is the frame a `Graphics2D` op\n * is written in.\n */\nexport interface CoordinateMap {\n /** Graph to pixels. Writes into `out` when given, so a sampler allocates nothing. */\n toPoint(x: number, y: number, out?: Vector2): Vector2;\n toCoords(x: number, y: number, out?: Vector2): Vector2;\n /** The plot area. */\n readonly area: Extents;\n readonly xRange: Interval;\n readonly yRange: Interval;\n /** Equal for two maps that place every point alike: what a geometry cache keys on. */\n readonly revision: string;\n /** Set where a straight run of graph values is not a straight run of pixels, so a line through the map is sampled. */\n readonly curved?: boolean;\n /** Set where what is ruled over the ranges can leave the plot area, so the system keeps it in. */\n readonly spills?: boolean;\n}\n\n/** One axis: a graph value to a pixel and back. */\nexport interface AxisScale {\n to(value: number): number;\n from(pixel: number): number;\n}\n\n/** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */\nexport function resolveInterval(range: Interval | undefined, scaling: AxisScaling = \"linear\"): Interval {\n let [min, max] = range ?? [0, 1];\n if (!Number.isFinite(min) || !Number.isFinite(max)) [min, max] = [0, 1];\n if (min > max) [min, max] = [max, min];\n if (scaling === \"log\") {\n if (!(max > 0)) max = 10;\n if (!(min > 0)) min = max / 1000;\n }\n return max > min ? [min, max] : [min, min + 1];\n}\n\n/** `range` spread over the pixels from `lo` to `hi`. */\nexport function axisScale(range: Interval, lo: number, hi: number, scaling: AxisScaling = \"linear\"): AxisScale {\n const [min, max] = range;\n if (scaling === \"log\") {\n const from = Math.log10(min);\n const k = (hi - lo) / (Math.log10(max) - from);\n return { to: (v) => lo + (Math.log10(v) - from) * k, from: (p) => 10 ** (from + (p - lo) / k) };\n }\n const k = (hi - lo) / (max - min);\n return { to: (v) => lo + (v - min) * k, from: (p) => min + (p - lo) / k };\n}\n\nexport interface CartesianOptions {\n xRange: Interval;\n yRange: Interval;\n area: Extents;\n xScaling?: AxisScaling;\n yScaling?: AxisScaling;\n}\n\n/** Two independent axes at right angles: what a number line, axes and a number plane share. */\nexport function cartesianMap({ xRange, yRange, area, xScaling = \"linear\", yScaling = \"linear\" }: CartesianOptions): CoordinateMap {\n const x = axisScale(xRange, area.minX, area.maxX, xScaling);\n const y = axisScale(yRange, area.minY, area.maxY, yScaling);\n return {\n toPoint: (gx, gy, out = { x: 0, y: 0 }) => {\n out.x = x.to(gx);\n out.y = y.to(gy);\n return out;\n },\n toCoords: (px, py, out = { x: 0, y: 0 }) => {\n out.x = x.from(px);\n out.y = y.from(py);\n return out;\n },\n area,\n xRange,\n yRange,\n revision: [xRange, yRange, area.minX, area.minY, area.maxX, area.maxY, xScaling, yScaling].join(\"|\"),\n };\n}\n\nconst UNBOUNDED: Extents = { minX: -Infinity, minY: -Infinity, maxX: Infinity, maxY: Infinity };\n\n/** One graph unit is one pixel: what a mark outside any system is drawn through. */\nexport const PIXEL_MAP: CoordinateMap = {\n toPoint: (x, y, out = { x: 0, y: 0 }) => {\n out.x = x;\n out.y = y;\n return out;\n },\n toCoords: (x, y, out = { x: 0, y: 0 }) => {\n out.x = x;\n out.y = y;\n return out;\n },\n area: UNBOUNDED,\n xRange: [-Infinity, Infinity],\n yRange: [-Infinity, Infinity],\n revision: \"pixels\",\n};\n\n/** A 2 × 2 matrix, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */\nexport type Matrix2 = readonly [number, number, number, number];\n\nexport const IDENTITY_MATRIX: Matrix2 = [1, 0, 0, 1];\n\n/** A matrix from its rows, or flat; anything else is the identity. */\nexport function readMatrix(value: unknown, previous: Matrix2 = IDENTITY_MATRIX): Matrix2 {\n const flat = Array.isArray(value) ? value.flat() : [];\n if (flat.length !== 4 || flat.some((entry) => typeof entry !== \"number\" || !Number.isFinite(entry))) return previous;\n return [flat[0], flat[1], flat[2], flat[3]];\n}\n\nexport function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2 {\n return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t) as unknown as Matrix2;\n}\n\nexport function isIdentityMatrix([a, b, c, d]: Matrix2): boolean {\n return a === 1 && b === 0 && c === 0 && d === 1;\n}\n\n/**\n * How far a transformed plane reaches, in half-widths of its own ranges about\n * their middle. Finite on purpose: a plane squeezed towards a line shows the\n * pre-image of its box growing as 1/det, and ruling all of it packs the box\n * with lines, a solid wash just before the plane goes flat.\n */\nconst REACH = 4;\n\nfunction reach([min, max]: Interval): Interval {\n const middle = (min + max) / 2;\n const half = ((max - min) / 2) * REACH;\n return [middle - half, middle + half];\n}\n\n/**\n * `base` with every graph point sent through `matrix` first: a plane sheared,\n * turned or squeezed about its origin. Its ranges are what the plot area then\n * shows, up to {@link REACH}, so a grid ruled over them fills the box under a\n * turn, a shear or a fourfold shrink, and folds into a band as it goes flat.\n */\nexport function transformedMap(base: CoordinateMap, matrix: Matrix2): CoordinateMap {\n const [a, b, c, d] = matrix;\n const det = a * d - b * c;\n // A plane squeezed onto a line has no way back: a point is read as the untransformed plane has it.\n const flat = Math.abs(det) < 1e-9;\n const back = (u: number, v: number, out: Vector2): Vector2 => {\n if (flat) {\n out.x = u;\n out.y = v;\n } else {\n out.x = (d * u - b * v) / det;\n out.y = (a * v - c * u) / det;\n }\n return out;\n };\n const xReach = reach(base.xRange);\n const yReach = reach(base.yRange);\n let xRange = xReach;\n let yRange = yReach;\n if (!flat) {\n const corner: Vector2 = { x: 0, y: 0 };\n let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;\n for (const u of base.xRange) {\n for (const v of base.yRange) {\n back(u, v, corner);\n minX = Math.min(minX, corner.x);\n maxX = Math.max(maxX, corner.x);\n minY = Math.min(minY, corner.y);\n maxY = Math.max(maxY, corner.y);\n }\n }\n xRange = [Math.max(minX, xReach[0]), Math.min(maxX, xReach[1])];\n yRange = [Math.max(minY, yReach[0]), Math.min(maxY, yReach[1])];\n }\n return {\n toPoint: (x, y, out = { x: 0, y: 0 }) => base.toPoint(a * x + b * y, c * x + d * y, out),\n toCoords: (px, py, out = { x: 0, y: 0 }) => {\n base.toCoords(px, py, out);\n return back(out.x, out.y, out);\n },\n area: base.area,\n xRange,\n yRange,\n revision: `${base.revision}|m${matrix.join(\",\")}`,\n spills: true,\n };\n}\n\n/** Where a warp sends a graph point, written into `out`; `false` where it has no value. */\nexport type Warp = (x: number, y: number, out: Vector2) => boolean;\n\n/**\n * `base` with every graph point sent to where `warp` sends it: a plane bent by\n * a function of its own points. `key` names the warp for the revision, since a\n * function has no text of its own.\n */\nexport function warpedMap(base: CoordinateMap, warp: Warp, key: string): CoordinateMap {\n const bend = (x: number, y: number, out: Vector2): Vector2 => {\n // A point the warp has no value at stays where it is, so a pole does not tear the grid.\n if (!warp(x, y, out)) {\n out.x = x;\n out.y = y;\n }\n return out;\n };\n const at: Vector2 = { x: 0, y: 0 };\n const dx: Vector2 = { x: 0, y: 0 };\n const dy: Vector2 = { x: 0, y: 0 };\n const H = 1e-4;\n return {\n toPoint: (x, y, out = { x: 0, y: 0 }) => {\n bend(x, y, out);\n return base.toPoint(out.x, out.y, out);\n },\n // No closed form: a few Newton steps from where the unbent plane has the point.\n toCoords: (px, py, out = { x: 0, y: 0 }) => {\n const target = base.toCoords(px, py);\n let gx = target.x;\n let gy = target.y;\n for (let i = 0; i < 8; i++) {\n bend(gx, gy, at);\n const ex = at.x - target.x;\n const ey = at.y - target.y;\n if (Math.abs(ex) + Math.abs(ey) < 1e-9) break;\n bend(gx + H, gy, dx);\n bend(gx, gy + H, dy);\n const j00 = (dx.x - at.x) / H;\n const j10 = (dx.y - at.y) / H;\n const j01 = (dy.x - at.x) / H;\n const j11 = (dy.y - at.y) / H;\n const det = j00 * j11 - j01 * j10;\n if (!(Math.abs(det) > 1e-12)) break;\n gx -= (j11 * ex - j01 * ey) / det;\n gy -= (j00 * ey - j10 * ex) / det;\n }\n out.x = Number.isFinite(gx) ? gx : target.x;\n out.y = Number.isFinite(gy) ? gy : target.y;\n return out;\n },\n area: base.area,\n // A bent plane pulls its edges in somewhere: it is ruled a little past them so the box stays full.\n xRange: widened(base.xRange),\n yRange: widened(base.yRange),\n revision: `${base.revision}|w${key}`,\n curved: true,\n spills: true,\n };\n}\n\n/** How much further than its range, as a share of it, a warped plane is ruled each way. */\nconst WARP_MARGIN = 0.3;\n\nfunction widened([min, max]: Interval): Interval {\n const margin = (max - min) * WARP_MARGIN;\n return Number.isFinite(margin) ? [min - margin, max + margin] : [min, max];\n}\n\n/** How many pieces a line through a curved map is drawn in. */\nconst CURVED_STEPS = 48;\n\n/** The pixel points that draw the straight run of graph values from one point to another. */\nexport function lineThrough(map: CoordinateMap, x0: number, y0: number, x1: number, y1: number): Vector2[] {\n if (!map.curved) return [map.toPoint(x0, y0), map.toPoint(x1, y1)];\n const run: Vector2[] = [];\n for (let i = 0; i <= CURVED_STEPS; i++) {\n const t = i / CURVED_STEPS;\n run.push(map.toPoint(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t));\n }\n return run;\n}\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { NodeView } from "@motionscript/sdk";
|
|
2
|
+
import type { ExpressionScope } from "./expression.js";
|
|
3
|
+
import { type PlotSource } from "./source.js";
|
|
4
|
+
/**
|
|
5
|
+
* A curve as a mark reads it: what a plot is drawn from, its own `x`/`y`, and
|
|
6
|
+
* whether a system laid it out, which is when those are a shift in graph units.
|
|
7
|
+
* Data rather than a class, so a copied plot is ridden like the kit's own.
|
|
8
|
+
*/
|
|
9
|
+
export interface CurveValues {
|
|
10
|
+
readonly expression: PlotSource;
|
|
11
|
+
readonly params: ExpressionScope;
|
|
12
|
+
readonly x: number;
|
|
13
|
+
readonly y: number;
|
|
14
|
+
/** The plane the plot was laid out on; `null` outside any system. */
|
|
15
|
+
readonly graph?: object | null;
|
|
16
|
+
}
|
|
17
|
+
/** The node types a point, a tangent or an area can ride: they have a height at each `x`. */
|
|
18
|
+
export declare const CURVE_KEYS: readonly string[];
|
|
19
|
+
/**
|
|
20
|
+
* The curve's height at graph `x`, or `NaN` where it has none or is not a
|
|
21
|
+
* function of `x`. What a point, a tangent or an area riding a plot reads.
|
|
22
|
+
*/
|
|
23
|
+
export declare function curveValue(curve: CurveValues, x: number): number;
|
|
24
|
+
/** Whether `view` is a node a mark can ride. */
|
|
25
|
+
export declare function isCurve(view: unknown): view is NodeView & {
|
|
26
|
+
readonly state: CurveValues;
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=curve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"curve.d.ts","sourceRoot":"","sources":["../../src/kit/curve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAY,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAErD;;;;GAIG;AACH,MAAM,WAAW,WAAW;IACxB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,qEAAqE;IACrE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAClC;AAED,6FAA6F;AAC7F,eAAO,MAAM,UAAU,EAAE,SAAS,MAAM,EAAmC,CAAC;AAE5E;;;GAGG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAOhE;AAED,gDAAgD;AAChD,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,QAAQ,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAA;CAAE,CAEzF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { equationParams, parseEquation } from "./equation.js";
|
|
2
|
+
import { scopeFor } from "./source.js";
|
|
3
|
+
/** The node types a point, a tangent or an area can ride: they have a height at each `x`. */
|
|
4
|
+
export const CURVE_KEYS = ["functionPlot", "tracedPath"];
|
|
5
|
+
/**
|
|
6
|
+
* The curve's height at graph `x`, or `NaN` where it has none or is not a
|
|
7
|
+
* function of `x`. What a point, a tangent or an area riding a plot reads.
|
|
8
|
+
*/
|
|
9
|
+
export function curveValue(curve, x) {
|
|
10
|
+
const { source, blend } = curve.expression;
|
|
11
|
+
const shift = curve.graph ? curve : NO_SHIFT;
|
|
12
|
+
const value = heightOf(source, curve.params, x - shift.x) + shift.y;
|
|
13
|
+
if (!blend)
|
|
14
|
+
return value;
|
|
15
|
+
const from = heightOf(blend.from, curve.params, x - shift.x) + shift.y;
|
|
16
|
+
return from + (value - from) * blend.t;
|
|
17
|
+
}
|
|
18
|
+
/** Whether `view` is a node a mark can ride. */
|
|
19
|
+
export function isCurve(view) {
|
|
20
|
+
return typeof view === "object" && view !== null && CURVE_KEYS.includes(view.key ?? "");
|
|
21
|
+
}
|
|
22
|
+
const NO_SHIFT = { x: 0, y: 0 };
|
|
23
|
+
function heightOf(source, params, x) {
|
|
24
|
+
const equation = parseEquation(source, equationParams(source));
|
|
25
|
+
if (equation instanceof Error || equation.kind !== "function" || equation.of !== "x")
|
|
26
|
+
return NaN;
|
|
27
|
+
return equation.f(x, 0, scopeFor(equationParams(source), params));
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=curve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"curve.js","sourceRoot":"","sources":["../../src/kit/curve.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE3D,OAAO,EAAE,QAAQ,EAAmB,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,KAAK,IAAI,QAAQ,CAAC,IAAI,KAAK,UAAU,IAAI,QAAQ,CAAC,EAAE,KAAK,GAAG;QAAE,OAAO,GAAG,CAAC;IACjG,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;AACtE,CAAC","sourcesContent":["import type { NodeView } from \"@motionscript/sdk\";\nimport { equationParams, parseEquation } from \"./equation\";\nimport type { ExpressionScope } from \"./expression\";\nimport { scopeFor, type PlotSource } from \"./source\";\n\n/**\n * A curve as a mark reads it: what a plot is drawn from, its own `x`/`y`, and\n * whether a system laid it out, which is when those are a shift in graph units.\n * Data rather than a class, so a copied plot is ridden like the kit's own.\n */\nexport interface CurveValues {\n readonly expression: PlotSource;\n readonly params: ExpressionScope;\n readonly x: number;\n readonly y: number;\n /** The plane the plot was laid out on; `null` outside any system. */\n readonly graph?: object | null;\n}\n\n/** The node types a point, a tangent or an area can ride: they have a height at each `x`. */\nexport const CURVE_KEYS: readonly string[] = [\"functionPlot\", \"tracedPath\"];\n\n/**\n * The curve's height at graph `x`, or `NaN` where it has none or is not a\n * function of `x`. What a point, a tangent or an area riding a plot reads.\n */\nexport function curveValue(curve: CurveValues, x: number): number {\n const { source, blend } = curve.expression;\n const shift = curve.graph ? curve : NO_SHIFT;\n const value = heightOf(source, curve.params, x - shift.x) + shift.y;\n if (!blend) return value;\n const from = heightOf(blend.from, curve.params, x - shift.x) + shift.y;\n return from + (value - from) * blend.t;\n}\n\n/** Whether `view` is a node a mark can ride. */\nexport function isCurve(view: unknown): view is NodeView & { readonly state: CurveValues } {\n return typeof view === \"object\" && view !== null && CURVE_KEYS.includes((view as { key?: string }).key ?? \"\");\n}\n\nconst NO_SHIFT = { x: 0, y: 0 };\n\nfunction heightOf(source: string, params: ExpressionScope, x: number): number {\n const equation = parseEquation(source, equationParams(source));\n if (equation instanceof Error || equation.kind !== \"function\" || equation.of !== \"x\") return NaN;\n return equation.f(x, 0, scopeFor(equationParams(source), params));\n}\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type CompiledExpression } from "./expression.js";
|
|
2
|
+
/**
|
|
3
|
+
* A plotted equation, read the way it is written rather than by a `kind` prop:
|
|
4
|
+
*
|
|
5
|
+
* | Written as | Is |
|
|
6
|
+
* |---|---|
|
|
7
|
+
* | `sin(x)`, `y = sin(x)` | a function of `x` |
|
|
8
|
+
* | `x = y^2` | a function of `y` |
|
|
9
|
+
* | `r = 1 + cos(theta)` | polar, in `theta` |
|
|
10
|
+
* | `(cos(t), sin(t))` | parametric, in `t` |
|
|
11
|
+
* | `x^2 + y^2 = 4` | implicit, in `x` and `y` |
|
|
12
|
+
*/
|
|
13
|
+
export type Equation = {
|
|
14
|
+
kind: "function";
|
|
15
|
+
of: "x" | "y";
|
|
16
|
+
f: CompiledExpression;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "polar";
|
|
19
|
+
f: CompiledExpression;
|
|
20
|
+
} | {
|
|
21
|
+
kind: "parametric";
|
|
22
|
+
x: CompiledExpression;
|
|
23
|
+
y: CompiledExpression;
|
|
24
|
+
}
|
|
25
|
+
/** Drawn where `f(x, y)` is zero. */
|
|
26
|
+
| {
|
|
27
|
+
kind: "implicit";
|
|
28
|
+
f: CompiledExpression;
|
|
29
|
+
};
|
|
30
|
+
/** The free variable each kind is sampled over, for a host's help text. */
|
|
31
|
+
export declare const EQUATION_VARIABLES: {
|
|
32
|
+
readonly function: readonly ["x"];
|
|
33
|
+
readonly polar: readonly ["theta"];
|
|
34
|
+
readonly parametric: readonly ["t"];
|
|
35
|
+
readonly implicit: readonly ["x", "y"];
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* `source` as an {@link Equation}, or the reason it is not one. Memoised, so an
|
|
39
|
+
* unchanged equation is the identical object and a geometry cache can key on it.
|
|
40
|
+
*/
|
|
41
|
+
export declare function parseEquation(source: string, params?: readonly string[]): Equation | Error;
|
|
42
|
+
/**
|
|
43
|
+
* Why `source` will not plot, or `null` when it will. An empty one is not an
|
|
44
|
+
* error (nobody has written it yet), and neither is a name it does not know:
|
|
45
|
+
* that is a named value, see {@link equationParams}.
|
|
46
|
+
*/
|
|
47
|
+
export declare function equationError(source: string): string | null;
|
|
48
|
+
/**
|
|
49
|
+
* Every name `source` reads, in the order it first names them, variables
|
|
50
|
+
* included. Empty for text that does not parse.
|
|
51
|
+
*/
|
|
52
|
+
export declare function equationNames(source: string): readonly string[];
|
|
53
|
+
/**
|
|
54
|
+
* The named values `source` reads: every name that is not a variable, a
|
|
55
|
+
* function or a constant.
|
|
56
|
+
*/
|
|
57
|
+
export declare function equationParams(source: string): readonly string[];
|
|
58
|
+
/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */
|
|
59
|
+
export declare function splitTuple(source: string): string[] | null;
|
|
60
|
+
export declare function splitTopLevel(source: string, separator: string): string[];
|
|
61
|
+
//# sourceMappingURL=equation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"equation.d.ts","sourceRoot":"","sources":["../../src/kit/equation.ts"],"names":[],"mappings":"AAEA,OAAO,EAAyC,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAE9F;;;;;;;;;;GAUG;AACH,MAAM,MAAM,QAAQ,GACd;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,GAAG,GAAG,GAAG,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GAC1D;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE;AACtE,qCAAqC;GACnC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,CAAC;A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