@motionscript/math 0.0.0-stage → 0.1.0-alpha.5
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 +110 -0
- package/LICENSE +201 -0
- package/NOTICE +48 -0
- package/README.md +385 -3
- package/dist/annotate/angle-mark.d.ts +46 -0
- package/dist/annotate/angle-mark.d.ts.map +1 -0
- package/dist/annotate/angle-mark.js +132 -0
- package/dist/annotate/angle-mark.js.map +1 -0
- package/dist/annotate/curly-brace.d.ts +46 -0
- package/dist/annotate/curly-brace.d.ts.map +1 -0
- package/dist/annotate/curly-brace.js +114 -0
- package/dist/annotate/curly-brace.js.map +1 -0
- package/dist/annotate/emphasis-mark.d.ts +20 -0
- package/dist/annotate/emphasis-mark.d.ts.map +1 -0
- package/dist/annotate/emphasis-mark.js +81 -0
- package/dist/annotate/emphasis-mark.js.map +1 -0
- package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js +2 -0
- package/dist/browser/chunks/chunk-3GJB6AVO.js.map +7 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js +2 -0
- package/dist/browser/chunks/chunk-D5WHCVBC.js.map +7 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js +2 -0
- package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +7 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.js +61 -0
- package/dist/browser/chunks/mathjax-DNP3YRES.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 +64 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +42 -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/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 +17 -0
- package/dist/kit/curve.d.ts.map +1 -0
- package/dist/kit/curve.js +22 -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 +58 -0
- package/dist/kit/field-mark.d.ts.map +1 -0
- package/dist/kit/field-mark.js +129 -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 +71 -0
- package/dist/kit/follow.d.ts.map +1 -0
- package/dist/kit/follow.js +159 -0
- package/dist/kit/follow.js.map +1 -0
- package/dist/kit/furniture.d.ts +30 -0
- package/dist/kit/furniture.d.ts.map +1 -0
- package/dist/kit/furniture.js +87 -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/mark.d.ts +47 -0
- package/dist/kit/mark.d.ts.map +1 -0
- package/dist/kit/mark.js +103 -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 +18 -0
- package/dist/kit/place.d.ts.map +1 -0
- package/dist/kit/place.js +68 -0
- package/dist/kit/place.js.map +1 -0
- package/dist/kit/placement.d.ts +8 -0
- package/dist/kit/placement.d.ts.map +1 -0
- package/dist/kit/placement.js +8 -0
- package/dist/kit/placement.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 +33 -0
- package/dist/kit/system.d.ts.map +1 -0
- package/dist/kit/system.js +67 -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 +32 -0
- package/dist/marks/curve-area.d.ts.map +1 -0
- package/dist/marks/curve-area.js +82 -0
- package/dist/marks/curve-area.js.map +1 -0
- package/dist/marks/drop-lines.d.ts +22 -0
- package/dist/marks/drop-lines.d.ts.map +1 -0
- package/dist/marks/drop-lines.js +58 -0
- package/dist/marks/drop-lines.js.map +1 -0
- package/dist/marks/plot-point.d.ts +35 -0
- package/dist/marks/plot-point.d.ts.map +1 -0
- package/dist/marks/plot-point.js +97 -0
- package/dist/marks/plot-point.js.map +1 -0
- package/dist/marks/riemann-sum.d.ts +37 -0
- package/dist/marks/riemann-sum.d.ts.map +1 -0
- package/dist/marks/riemann-sum.js +91 -0
- package/dist/marks/riemann-sum.js.map +1 -0
- package/dist/marks/tangent-line.d.ts +27 -0
- package/dist/marks/tangent-line.d.ts.map +1 -0
- package/dist/marks/tangent-line.js +103 -0
- package/dist/marks/tangent-line.js.map +1 -0
- package/dist/marks/vector-arrow.d.ts +32 -0
- package/dist/marks/vector-arrow.d.ts.map +1 -0
- package/dist/marks/vector-arrow.js +87 -0
- package/dist/marks/vector-arrow.js.map +1 -0
- package/dist/nodes.d.ts +35 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +63 -0
- package/dist/nodes.js.map +1 -0
- package/dist/plots/function-plot.d.ts +67 -0
- package/dist/plots/function-plot.d.ts.map +1 -0
- package/dist/plots/function-plot.js +215 -0
- package/dist/plots/function-plot.js.map +1 -0
- package/dist/plots/points-plot.d.ts +28 -0
- package/dist/plots/points-plot.d.ts.map +1 -0
- package/dist/plots/points-plot.js +99 -0
- package/dist/plots/points-plot.js.map +1 -0
- package/dist/plots/stream-lines.d.ts +35 -0
- package/dist/plots/stream-lines.d.ts.map +1 -0
- package/dist/plots/stream-lines.js +141 -0
- package/dist/plots/stream-lines.js.map +1 -0
- package/dist/plots/traced-path.d.ts +31 -0
- package/dist/plots/traced-path.d.ts.map +1 -0
- package/dist/plots/traced-path.js +56 -0
- package/dist/plots/traced-path.js.map +1 -0
- package/dist/plots/vector-field.d.ts +23 -0
- package/dist/plots/vector-field.d.ts.map +1 -0
- package/dist/plots/vector-field.js +91 -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 +83 -0
- package/dist/structures/network-graph.d.ts.map +1 -0
- package/dist/structures/network-graph.js +289 -0
- package/dist/structures/network-graph.js.map +1 -0
- package/dist/structures/sample-space.d.ts +62 -0
- package/dist/structures/sample-space.d.ts.map +1 -0
- package/dist/structures/sample-space.js +199 -0
- package/dist/structures/sample-space.js.map +1 -0
- package/dist/systems/axes2d.d.ts +75 -0
- package/dist/systems/axes2d.d.ts.map +1 -0
- package/dist/systems/axes2d.js +193 -0
- package/dist/systems/axes2d.js.map +1 -0
- package/dist/systems/number-line.d.ts +49 -0
- package/dist/systems/number-line.d.ts.map +1 -0
- package/dist/systems/number-line.js +127 -0
- package/dist/systems/number-line.js.map +1 -0
- package/dist/systems/number-plane.d.ts +59 -0
- package/dist/systems/number-plane.d.ts.map +1 -0
- package/dist/systems/number-plane.js +193 -0
- package/dist/systems/number-plane.js.map +1 -0
- package/dist/systems/polar-plane.d.ts +48 -0
- package/dist/systems/polar-plane.d.ts.map +1 -0
- package/dist/systems/polar-plane.js +165 -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 +9 -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 +31 -0
- package/dist/tex/matrix.d.ts.map +1 -0
- package/dist/tex/matrix.js +73 -0
- package/dist/tex/matrix.js.map +1 -0
- package/dist/tex/node.d.ts +215 -0
- package/dist/tex/node.d.ts.map +1 -0
- package/dist/tex/node.js +595 -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/three/axes3d.d.ts +87 -0
- package/dist/three/axes3d.d.ts.map +1 -0
- package/dist/three/axes3d.js +228 -0
- package/dist/three/axes3d.js.map +1 -0
- package/dist/three/curve-plot3d.d.ts +60 -0
- package/dist/three/curve-plot3d.d.ts.map +1 -0
- package/dist/three/curve-plot3d.js +143 -0
- package/dist/three/curve-plot3d.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 +40 -0
- package/dist/three/label3d.js.map +1 -0
- package/dist/three/plot-point3d.d.ts +26 -0
- package/dist/three/plot-point3d.d.ts.map +1 -0
- package/dist/three/plot-point3d.js +60 -0
- package/dist/three/plot-point3d.js.map +1 -0
- package/dist/three/space.d.ts +28 -0
- package/dist/three/space.d.ts.map +1 -0
- package/dist/three/space.js +47 -0
- package/dist/three/space.js.map +1 -0
- package/dist/three/surface-plot3d.d.ts +80 -0
- package/dist/three/surface-plot3d.d.ts.map +1 -0
- package/dist/three/surface-plot3d.js +290 -0
- package/dist/three/surface-plot3d.js.map +1 -0
- package/dist/three/vector-arrow3d.d.ts +25 -0
- package/dist/three/vector-arrow3d.d.ts.map +1 -0
- package/dist/three/vector-arrow3d.js +72 -0
- package/dist/three/vector-arrow3d.js.map +1 -0
- package/package.json +79 -3
- package/registry.json +251 -0
- package/src/annotate/angle-mark.ts +147 -0
- package/src/annotate/curly-brace.ts +126 -0
- package/src/annotate/emphasis-mark.ts +89 -0
- package/src/engine.ts +72 -0
- package/src/index.ts +29 -0
- package/src/kit/brace.ts +37 -0
- package/src/kit/cache.ts +61 -0
- package/src/kit/contour.ts +94 -0
- package/src/kit/coords.ts +278 -0
- package/src/kit/curve.ts +29 -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 +155 -0
- package/src/kit/field.ts +50 -0
- package/src/kit/follow.ts +203 -0
- package/src/kit/furniture.ts +108 -0
- package/src/kit/geometry.ts +49 -0
- package/src/kit/mark.ts +131 -0
- package/src/kit/part-fills.ts +61 -0
- package/src/kit/place.ts +101 -0
- package/src/kit/placement.ts +8 -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 +99 -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 +63 -0
- package/src/marks/plot-point.ts +107 -0
- package/src/marks/riemann-sum.ts +98 -0
- package/src/marks/tangent-line.ts +103 -0
- package/src/marks/vector-arrow.ts +98 -0
- package/src/nodes.ts +63 -0
- package/src/plots/function-plot.ts +236 -0
- package/src/plots/points-plot.ts +107 -0
- package/src/plots/stream-lines.ts +151 -0
- package/src/plots/traced-path.ts +52 -0
- package/src/plots/vector-field.ts +88 -0
- package/src/structures/graph-layout.ts +145 -0
- package/src/structures/network-graph.ts +308 -0
- package/src/structures/sample-space.ts +209 -0
- package/src/systems/axes2d.ts +217 -0
- package/src/systems/number-line.ts +131 -0
- package/src/systems/number-plane.ts +203 -0
- package/src/systems/polar-plane.ts +176 -0
- package/src/tex/geometry.ts +703 -0
- package/src/tex/index.ts +14 -0
- package/src/tex/mathjax.ts +82 -0
- package/src/tex/matrix.ts +75 -0
- package/src/tex/node.ts +641 -0
- package/src/tex/tween.ts +308 -0
- package/src/three/axes3d.ts +240 -0
- package/src/three/curve-plot3d.ts +147 -0
- package/src/three/label3d.ts +50 -0
- package/src/three/plot-point3d.ts +66 -0
- package/src/three/space.ts +67 -0
- package/src/three/surface-plot3d.ts +323 -0
- package/src/three/vector-arrow3d.ts +77 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import type { Extents, Vector2 } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/** A closed span of graph values, low to high. */
|
|
4
|
+
export type Interval = readonly [number, number];
|
|
5
|
+
|
|
6
|
+
export type AxisScaling = "linear" | "log";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* How a coordinate system's graph values land on its pixels. Pixels are y-up
|
|
10
|
+
* from the centre of the system's own box, which is the frame a `Graphics2D` op
|
|
11
|
+
* is written in.
|
|
12
|
+
*/
|
|
13
|
+
export interface CoordinateMap {
|
|
14
|
+
/** Graph to pixels. Writes into `out` when given, so a sampler allocates nothing. */
|
|
15
|
+
toPoint(x: number, y: number, out?: Vector2): Vector2;
|
|
16
|
+
toCoords(x: number, y: number, out?: Vector2): Vector2;
|
|
17
|
+
/** The plot area. */
|
|
18
|
+
readonly area: Extents;
|
|
19
|
+
readonly xRange: Interval;
|
|
20
|
+
readonly yRange: Interval;
|
|
21
|
+
/** Equal for two maps that place every point alike: what a geometry cache keys on. */
|
|
22
|
+
readonly revision: string;
|
|
23
|
+
/** Set where a straight run of graph values is not a straight run of pixels, so a line through the map is sampled. */
|
|
24
|
+
readonly curved?: boolean;
|
|
25
|
+
/** Set where what is ruled over the ranges can leave the plot area, so the system keeps it in. */
|
|
26
|
+
readonly spills?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One axis: a graph value to a pixel and back. */
|
|
30
|
+
export interface AxisScale {
|
|
31
|
+
to(value: number): number;
|
|
32
|
+
from(pixel: number): number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `range`, made usable: finite, ordered, non-empty, and positive on a log axis. */
|
|
36
|
+
export function resolveInterval(range: Interval | undefined, scaling: AxisScaling = "linear"): Interval {
|
|
37
|
+
let [min, max] = range ?? [0, 1];
|
|
38
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) [min, max] = [0, 1];
|
|
39
|
+
if (min > max) [min, max] = [max, min];
|
|
40
|
+
if (scaling === "log") {
|
|
41
|
+
if (!(max > 0)) max = 10;
|
|
42
|
+
if (!(min > 0)) min = max / 1000;
|
|
43
|
+
}
|
|
44
|
+
return max > min ? [min, max] : [min, min + 1];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** `range` spread over the pixels from `lo` to `hi`. */
|
|
48
|
+
export function axisScale(range: Interval, lo: number, hi: number, scaling: AxisScaling = "linear"): AxisScale {
|
|
49
|
+
const [min, max] = range;
|
|
50
|
+
if (scaling === "log") {
|
|
51
|
+
const from = Math.log10(min);
|
|
52
|
+
const k = (hi - lo) / (Math.log10(max) - from);
|
|
53
|
+
return { to: (v) => lo + (Math.log10(v) - from) * k, from: (p) => 10 ** (from + (p - lo) / k) };
|
|
54
|
+
}
|
|
55
|
+
const k = (hi - lo) / (max - min);
|
|
56
|
+
return { to: (v) => lo + (v - min) * k, from: (p) => min + (p - lo) / k };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CartesianOptions {
|
|
60
|
+
xRange: Interval;
|
|
61
|
+
yRange: Interval;
|
|
62
|
+
area: Extents;
|
|
63
|
+
xScaling?: AxisScaling;
|
|
64
|
+
yScaling?: AxisScaling;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Two independent axes at right angles: what a number line, axes and a number plane share. */
|
|
68
|
+
export function cartesianMap({ xRange, yRange, area, xScaling = "linear", yScaling = "linear" }: CartesianOptions): CoordinateMap {
|
|
69
|
+
const x = axisScale(xRange, area.minX, area.maxX, xScaling);
|
|
70
|
+
const y = axisScale(yRange, area.minY, area.maxY, yScaling);
|
|
71
|
+
return {
|
|
72
|
+
toPoint: (gx, gy, out = { x: 0, y: 0 }) => {
|
|
73
|
+
out.x = x.to(gx);
|
|
74
|
+
out.y = y.to(gy);
|
|
75
|
+
return out;
|
|
76
|
+
},
|
|
77
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
78
|
+
out.x = x.from(px);
|
|
79
|
+
out.y = y.from(py);
|
|
80
|
+
return out;
|
|
81
|
+
},
|
|
82
|
+
area,
|
|
83
|
+
xRange,
|
|
84
|
+
yRange,
|
|
85
|
+
revision: [xRange, yRange, area.minX, area.minY, area.maxX, area.maxY, xScaling, yScaling].join("|"),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const UNBOUNDED: Extents = { minX: -Infinity, minY: -Infinity, maxX: Infinity, maxY: Infinity };
|
|
90
|
+
|
|
91
|
+
/** One graph unit is one pixel: what a mark outside any system is drawn through. */
|
|
92
|
+
export const PIXEL_MAP: CoordinateMap = {
|
|
93
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
94
|
+
out.x = x;
|
|
95
|
+
out.y = y;
|
|
96
|
+
return out;
|
|
97
|
+
},
|
|
98
|
+
toCoords: (x, y, out = { x: 0, y: 0 }) => {
|
|
99
|
+
out.x = x;
|
|
100
|
+
out.y = y;
|
|
101
|
+
return out;
|
|
102
|
+
},
|
|
103
|
+
area: UNBOUNDED,
|
|
104
|
+
xRange: [-Infinity, Infinity],
|
|
105
|
+
yRange: [-Infinity, Infinity],
|
|
106
|
+
revision: "pixels",
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** A 2 × 2 matrix, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
|
|
110
|
+
export type Matrix2 = readonly [number, number, number, number];
|
|
111
|
+
|
|
112
|
+
export const IDENTITY_MATRIX: Matrix2 = [1, 0, 0, 1];
|
|
113
|
+
|
|
114
|
+
/** A matrix from its rows, or flat; anything else is the identity. */
|
|
115
|
+
export function readMatrix(value: unknown, previous: Matrix2 = IDENTITY_MATRIX): Matrix2 {
|
|
116
|
+
const flat = Array.isArray(value) ? value.flat() : [];
|
|
117
|
+
if (flat.length !== 4 || flat.some((entry) => typeof entry !== "number" || !Number.isFinite(entry))) return previous;
|
|
118
|
+
return [flat[0], flat[1], flat[2], flat[3]];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function lerpMatrix(from: Matrix2, to: Matrix2, t: number): Matrix2 {
|
|
122
|
+
return [0, 1, 2, 3].map((i) => from[i] + (to[i] - from[i]) * t) as unknown as Matrix2;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function isIdentityMatrix([a, b, c, d]: Matrix2): boolean {
|
|
126
|
+
return a === 1 && b === 0 && c === 0 && d === 1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* How far a transformed plane reaches, in half-widths of its own ranges about
|
|
131
|
+
* their middle. Finite on purpose: a plane squeezed towards a line shows the
|
|
132
|
+
* pre-image of its box growing as 1/det, and ruling all of it packs the box
|
|
133
|
+
* with lines, a solid wash just before the plane goes flat.
|
|
134
|
+
*/
|
|
135
|
+
const REACH = 4;
|
|
136
|
+
|
|
137
|
+
function reach([min, max]: Interval): Interval {
|
|
138
|
+
const middle = (min + max) / 2;
|
|
139
|
+
const half = ((max - min) / 2) * REACH;
|
|
140
|
+
return [middle - half, middle + half];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* `base` with every graph point sent through `matrix` first: a plane sheared,
|
|
145
|
+
* turned or squeezed about its origin. Its ranges are what the plot area then
|
|
146
|
+
* shows, up to {@link REACH}, so a grid ruled over them fills the box under a
|
|
147
|
+
* turn, a shear or a fourfold shrink, and folds into a band as it goes flat.
|
|
148
|
+
*/
|
|
149
|
+
export function transformedMap(base: CoordinateMap, matrix: Matrix2): CoordinateMap {
|
|
150
|
+
const [a, b, c, d] = matrix;
|
|
151
|
+
const det = a * d - b * c;
|
|
152
|
+
// A plane squeezed onto a line has no way back: a point is read as the untransformed plane has it.
|
|
153
|
+
const flat = Math.abs(det) < 1e-9;
|
|
154
|
+
const back = (u: number, v: number, out: Vector2): Vector2 => {
|
|
155
|
+
if (flat) {
|
|
156
|
+
out.x = u;
|
|
157
|
+
out.y = v;
|
|
158
|
+
} else {
|
|
159
|
+
out.x = (d * u - b * v) / det;
|
|
160
|
+
out.y = (a * v - c * u) / det;
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
};
|
|
164
|
+
const xReach = reach(base.xRange);
|
|
165
|
+
const yReach = reach(base.yRange);
|
|
166
|
+
let xRange = xReach;
|
|
167
|
+
let yRange = yReach;
|
|
168
|
+
if (!flat) {
|
|
169
|
+
const corner: Vector2 = { x: 0, y: 0 };
|
|
170
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
171
|
+
for (const u of base.xRange) {
|
|
172
|
+
for (const v of base.yRange) {
|
|
173
|
+
back(u, v, corner);
|
|
174
|
+
minX = Math.min(minX, corner.x);
|
|
175
|
+
maxX = Math.max(maxX, corner.x);
|
|
176
|
+
minY = Math.min(minY, corner.y);
|
|
177
|
+
maxY = Math.max(maxY, corner.y);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
xRange = [Math.max(minX, xReach[0]), Math.min(maxX, xReach[1])];
|
|
181
|
+
yRange = [Math.max(minY, yReach[0]), Math.min(maxY, yReach[1])];
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => base.toPoint(a * x + b * y, c * x + d * y, out),
|
|
185
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
186
|
+
base.toCoords(px, py, out);
|
|
187
|
+
return back(out.x, out.y, out);
|
|
188
|
+
},
|
|
189
|
+
area: base.area,
|
|
190
|
+
xRange,
|
|
191
|
+
yRange,
|
|
192
|
+
revision: `${base.revision}|m${matrix.join(",")}`,
|
|
193
|
+
spills: true,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Where a warp sends a graph point, written into `out`; `false` where it has no value. */
|
|
198
|
+
export type Warp = (x: number, y: number, out: Vector2) => boolean;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* `base` with every graph point sent to where `warp` sends it: a plane bent by
|
|
202
|
+
* a function of its own points. `key` names the warp for the revision, since a
|
|
203
|
+
* function has no text of its own.
|
|
204
|
+
*/
|
|
205
|
+
export function warpedMap(base: CoordinateMap, warp: Warp, key: string): CoordinateMap {
|
|
206
|
+
const bend = (x: number, y: number, out: Vector2): Vector2 => {
|
|
207
|
+
// A point the warp has no value at stays where it is, so a pole does not tear the grid.
|
|
208
|
+
if (!warp(x, y, out)) {
|
|
209
|
+
out.x = x;
|
|
210
|
+
out.y = y;
|
|
211
|
+
}
|
|
212
|
+
return out;
|
|
213
|
+
};
|
|
214
|
+
const at: Vector2 = { x: 0, y: 0 };
|
|
215
|
+
const dx: Vector2 = { x: 0, y: 0 };
|
|
216
|
+
const dy: Vector2 = { x: 0, y: 0 };
|
|
217
|
+
const H = 1e-4;
|
|
218
|
+
return {
|
|
219
|
+
toPoint: (x, y, out = { x: 0, y: 0 }) => {
|
|
220
|
+
bend(x, y, out);
|
|
221
|
+
return base.toPoint(out.x, out.y, out);
|
|
222
|
+
},
|
|
223
|
+
// No closed form: a few Newton steps from where the unbent plane has the point.
|
|
224
|
+
toCoords: (px, py, out = { x: 0, y: 0 }) => {
|
|
225
|
+
const target = base.toCoords(px, py);
|
|
226
|
+
let gx = target.x;
|
|
227
|
+
let gy = target.y;
|
|
228
|
+
for (let i = 0; i < 8; i++) {
|
|
229
|
+
bend(gx, gy, at);
|
|
230
|
+
const ex = at.x - target.x;
|
|
231
|
+
const ey = at.y - target.y;
|
|
232
|
+
if (Math.abs(ex) + Math.abs(ey) < 1e-9) break;
|
|
233
|
+
bend(gx + H, gy, dx);
|
|
234
|
+
bend(gx, gy + H, dy);
|
|
235
|
+
const j00 = (dx.x - at.x) / H;
|
|
236
|
+
const j10 = (dx.y - at.y) / H;
|
|
237
|
+
const j01 = (dy.x - at.x) / H;
|
|
238
|
+
const j11 = (dy.y - at.y) / H;
|
|
239
|
+
const det = j00 * j11 - j01 * j10;
|
|
240
|
+
if (!(Math.abs(det) > 1e-12)) break;
|
|
241
|
+
gx -= (j11 * ex - j01 * ey) / det;
|
|
242
|
+
gy -= (j00 * ey - j10 * ex) / det;
|
|
243
|
+
}
|
|
244
|
+
out.x = Number.isFinite(gx) ? gx : target.x;
|
|
245
|
+
out.y = Number.isFinite(gy) ? gy : target.y;
|
|
246
|
+
return out;
|
|
247
|
+
},
|
|
248
|
+
area: base.area,
|
|
249
|
+
// A bent plane pulls its edges in somewhere: it is ruled a little past them so the box stays full.
|
|
250
|
+
xRange: widened(base.xRange),
|
|
251
|
+
yRange: widened(base.yRange),
|
|
252
|
+
revision: `${base.revision}|w${key}`,
|
|
253
|
+
curved: true,
|
|
254
|
+
spills: true,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** How much further than its range, as a share of it, a warped plane is ruled each way. */
|
|
259
|
+
const WARP_MARGIN = 0.3;
|
|
260
|
+
|
|
261
|
+
function widened([min, max]: Interval): Interval {
|
|
262
|
+
const margin = (max - min) * WARP_MARGIN;
|
|
263
|
+
return Number.isFinite(margin) ? [min - margin, max + margin] : [min, max];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** How many pieces a line through a curved map is drawn in. */
|
|
267
|
+
const CURVED_STEPS = 48;
|
|
268
|
+
|
|
269
|
+
/** The pixel points that draw the straight run of graph values from one point to another. */
|
|
270
|
+
export function lineThrough(map: CoordinateMap, x0: number, y0: number, x1: number, y1: number): Vector2[] {
|
|
271
|
+
if (!map.curved) return [map.toPoint(x0, y0), map.toPoint(x1, y1)];
|
|
272
|
+
const run: Vector2[] = [];
|
|
273
|
+
for (let i = 0; i <= CURVED_STEPS; i++) {
|
|
274
|
+
const t = i / CURVED_STEPS;
|
|
275
|
+
run.push(map.toPoint(x0 + (x1 - x0) * t, y0 + (y1 - y0) * t));
|
|
276
|
+
}
|
|
277
|
+
return run;
|
|
278
|
+
}
|
package/src/kit/curve.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { findNode2D, type Node2D } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A node with a height at each `x`: what a point, a tangent or an area rides.
|
|
5
|
+
* A protocol rather than a class, so a copied plot is ridden like the kit's own.
|
|
6
|
+
*/
|
|
7
|
+
export interface Curve {
|
|
8
|
+
/** The curve's height at graph `x`, or `NaN` where it has none. */
|
|
9
|
+
valueAt(x: number): number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The curve `id` names in `from`'s tree, or `null`. */
|
|
13
|
+
export function curveNamed(from: Node2D, id: string): Curve | null {
|
|
14
|
+
if (id === "") return null;
|
|
15
|
+
const found = findNode2D(from, id) as (Node2D & Partial<Curve>) | null;
|
|
16
|
+
return found && typeof found.valueAt === "function" ? (found as Node2D & Curve) : null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The curve a mark reads: the one `on` names, or, naming none, the nearest
|
|
21
|
+
* curve the mark sits inside. Putting an area in a plot is how it is told which.
|
|
22
|
+
*/
|
|
23
|
+
export function curveOf(mark: Node2D, on: string): Curve | null {
|
|
24
|
+
if (on !== "") return curveNamed(mark, on);
|
|
25
|
+
for (let at: Node2D | null = mark.parent; at; at = at.parent) {
|
|
26
|
+
if (typeof (at as Partial<Curve>).valueAt === "function") return at as Node2D & Curve;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { expressionOps, type ExpressionNode } from "@motionscript/sdk";
|
|
2
|
+
import { createBoundedCache } from "./cache";
|
|
3
|
+
import { compileExpressionCached, MATH_DIALECT, type CompiledExpression } from "./expression";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A plotted equation, read the way it is written rather than by a `kind` prop:
|
|
7
|
+
*
|
|
8
|
+
* | Written as | Is |
|
|
9
|
+
* |---|---|
|
|
10
|
+
* | `sin(x)`, `y = sin(x)` | a function of `x` |
|
|
11
|
+
* | `x = y^2` | a function of `y` |
|
|
12
|
+
* | `r = 1 + cos(theta)` | polar, in `theta` |
|
|
13
|
+
* | `(cos(t), sin(t))` | parametric, in `t` |
|
|
14
|
+
* | `x^2 + y^2 = 4` | implicit, in `x` and `y` |
|
|
15
|
+
*/
|
|
16
|
+
export type Equation =
|
|
17
|
+
| { kind: "function"; of: "x" | "y"; f: CompiledExpression }
|
|
18
|
+
| { kind: "polar"; f: CompiledExpression }
|
|
19
|
+
| { kind: "parametric"; x: CompiledExpression; y: CompiledExpression }
|
|
20
|
+
/** Drawn where `f(x, y)` is zero. */
|
|
21
|
+
| { kind: "implicit"; f: CompiledExpression };
|
|
22
|
+
|
|
23
|
+
/** The free variable each kind is sampled over, for a host's help text. */
|
|
24
|
+
export const EQUATION_VARIABLES = {
|
|
25
|
+
function: ["x"],
|
|
26
|
+
polar: ["theta"],
|
|
27
|
+
parametric: ["t"],
|
|
28
|
+
implicit: ["x", "y"],
|
|
29
|
+
} as const satisfies Record<Equation["kind"], readonly string[]>;
|
|
30
|
+
|
|
31
|
+
const parsed = createBoundedCache<Equation | Error>(256);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* `source` as an {@link Equation}, or the reason it is not one. Memoised, so an
|
|
35
|
+
* unchanged equation is the identical object and a geometry cache can key on it.
|
|
36
|
+
*/
|
|
37
|
+
export function parseEquation(source: string, params: readonly string[] = []): Equation | Error {
|
|
38
|
+
const key = `${params.join(",")}|${source}`;
|
|
39
|
+
const hit = parsed.get(key);
|
|
40
|
+
if (hit !== undefined) return hit;
|
|
41
|
+
let result: Equation | Error;
|
|
42
|
+
try {
|
|
43
|
+
result = read(source.trim(), params);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
result = error instanceof Error ? error : new Error(String(error));
|
|
46
|
+
}
|
|
47
|
+
parsed.set(key, result);
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Why `source` will not plot, or `null` when it will. An empty one is not an
|
|
53
|
+
* error (nobody has written it yet), and neither is a name it does not know:
|
|
54
|
+
* that is a named value, see {@link equationParams}.
|
|
55
|
+
*/
|
|
56
|
+
export function equationError(source: string): string | null {
|
|
57
|
+
if (source.trim() === "") return null;
|
|
58
|
+
const equation = parseEquation(source, equationParams(source));
|
|
59
|
+
return equation instanceof Error ? equation.message : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A name that is always a variable of some kind of equation, so never a named value. */
|
|
63
|
+
const VARIABLE_NAMES: ReadonlySet<string> = new Set(["r", ...Object.values(EQUATION_VARIABLES).flat()]);
|
|
64
|
+
|
|
65
|
+
const named = createBoundedCache<readonly string[]>(256);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Every name `source` reads, in the order it first names them, variables
|
|
69
|
+
* included. Empty for text that does not parse.
|
|
70
|
+
*/
|
|
71
|
+
export function equationNames(source: string): readonly string[] {
|
|
72
|
+
const hit = named.get(source);
|
|
73
|
+
if (hit !== undefined) return hit;
|
|
74
|
+
const names = new Set<string>();
|
|
75
|
+
const text = source.trim();
|
|
76
|
+
try {
|
|
77
|
+
for (const part of splitTuple(text) ?? splitTopLevel(text, "=")) {
|
|
78
|
+
collectNames(expressionOps.parse(part.trim(), MATH_DIALECT).root, names);
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
names.clear();
|
|
82
|
+
}
|
|
83
|
+
const result = [...names];
|
|
84
|
+
named.set(source, result);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The named values `source` reads: every name that is not a variable, a
|
|
90
|
+
* function or a constant.
|
|
91
|
+
*/
|
|
92
|
+
export function equationParams(source: string): readonly string[] {
|
|
93
|
+
return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function collectNames(node: ExpressionNode, into: Set<string>): void {
|
|
97
|
+
switch (node.kind) {
|
|
98
|
+
case "ref":
|
|
99
|
+
into.add(node.name);
|
|
100
|
+
return;
|
|
101
|
+
case "unary":
|
|
102
|
+
collectNames(node.operand, into);
|
|
103
|
+
return;
|
|
104
|
+
case "binary":
|
|
105
|
+
collectNames(node.left, into);
|
|
106
|
+
collectNames(node.right, into);
|
|
107
|
+
return;
|
|
108
|
+
case "call":
|
|
109
|
+
for (const arg of node.args) collectNames(arg, into);
|
|
110
|
+
return;
|
|
111
|
+
default:
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function read(source: string, params: readonly string[]): Equation {
|
|
116
|
+
if (source === "") throw new Error("Empty equation");
|
|
117
|
+
const compile = (text: string, variables: readonly string[]): CompiledExpression => {
|
|
118
|
+
const result = compileExpressionCached(text.trim(), variables, params);
|
|
119
|
+
if (result instanceof Error) throw result;
|
|
120
|
+
return result;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const pair = splitTuple(source);
|
|
124
|
+
if (pair?.length === 2) return { kind: "parametric", x: compile(pair[0], ["t"]), y: compile(pair[1], ["t"]) };
|
|
125
|
+
|
|
126
|
+
const sides = splitTopLevel(source, "=");
|
|
127
|
+
if (sides.length > 2) throw new Error("An equation has one '='");
|
|
128
|
+
if (sides.length === 1) return { kind: "function", of: "x", f: compile(source, ["x"]) };
|
|
129
|
+
|
|
130
|
+
const [left, right] = sides;
|
|
131
|
+
const subject = left.trim();
|
|
132
|
+
const implicit = (): Equation => {
|
|
133
|
+
const l = compile(left, ["x", "y"]);
|
|
134
|
+
const r = compile(right, ["x", "y"]);
|
|
135
|
+
return { kind: "implicit", f: (x, y, scope) => l(x, y, scope) - r(x, y, scope) };
|
|
136
|
+
};
|
|
137
|
+
if (subject === "r") return { kind: "polar", f: compile(right, ["theta"]) };
|
|
138
|
+
if (subject === "y" || subject === "x") {
|
|
139
|
+
const of = subject === "y" ? "x" : "y";
|
|
140
|
+
// `y = x + y` names its own subject on the right, so it is not a function of one variable.
|
|
141
|
+
try {
|
|
142
|
+
return { kind: "function", of, f: compile(right, [of]) };
|
|
143
|
+
} catch (error) {
|
|
144
|
+
try {
|
|
145
|
+
return implicit();
|
|
146
|
+
} catch {
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return implicit();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */
|
|
155
|
+
export function splitTuple(source: string): string[] | null {
|
|
156
|
+
if (!source.startsWith("(") || !source.endsWith(")")) return null;
|
|
157
|
+
let depth = 0;
|
|
158
|
+
for (let i = 0; i < source.length - 1; i++) {
|
|
159
|
+
if (source[i] === "(") depth++;
|
|
160
|
+
else if (source[i] === ")" && --depth === 0) return null;
|
|
161
|
+
}
|
|
162
|
+
const parts = splitTopLevel(source.slice(1, -1), ",");
|
|
163
|
+
return parts.length >= 2 ? parts : null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function splitTopLevel(source: string, separator: string): string[] {
|
|
167
|
+
const parts: string[] = [];
|
|
168
|
+
let depth = 0;
|
|
169
|
+
let from = 0;
|
|
170
|
+
for (let i = 0; i < source.length; i++) {
|
|
171
|
+
const c = source[i];
|
|
172
|
+
if (c === "(") depth++;
|
|
173
|
+
else if (c === ")") depth--;
|
|
174
|
+
else if (c === separator && depth === 0) {
|
|
175
|
+
parts.push(source.slice(from, i));
|
|
176
|
+
from = i + 1;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
parts.push(source.slice(from));
|
|
180
|
+
return parts;
|
|
181
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { createBoundedCache } from "./cache";
|
|
2
|
+
import { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from "./equation";
|
|
3
|
+
import { compileExpressionCached, type CompiledExpression } from "./expression";
|
|
4
|
+
import { fieldError, fieldParams, FIELD_VARIABLES } from "./field";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A surface's equation, read the way it is written:
|
|
8
|
+
*
|
|
9
|
+
* | Written as | Is |
|
|
10
|
+
* |---|---|
|
|
11
|
+
* | `sin(x) * cos(y)`, `z = x^2 - y^2` | a height over `x` and `y` |
|
|
12
|
+
* | `(cos(u) sin(v), sin(u) sin(v), cos(v))` | parametric, in `u` and `v` |
|
|
13
|
+
*/
|
|
14
|
+
export type SurfaceEquation =
|
|
15
|
+
| { kind: "height"; f: CompiledExpression }
|
|
16
|
+
| { kind: "parametric"; x: CompiledExpression; y: CompiledExpression; z: CompiledExpression };
|
|
17
|
+
|
|
18
|
+
/** A curve through space, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
|
|
19
|
+
export interface SpaceCurveEquation {
|
|
20
|
+
x: CompiledExpression;
|
|
21
|
+
y: CompiledExpression;
|
|
22
|
+
z: CompiledExpression;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What each 3D form is written over, for a host's help text. */
|
|
26
|
+
export const EQUATION_VARIABLES_3D = {
|
|
27
|
+
height: ["x", "y"],
|
|
28
|
+
surface: ["u", "v"],
|
|
29
|
+
spaceCurve: ["t"],
|
|
30
|
+
} as const;
|
|
31
|
+
|
|
32
|
+
const SURFACE_NAMES: ReadonlySet<string> = new Set(["x", "y", "z", "u", "v"]);
|
|
33
|
+
const SPACE_CURVE_NAMES: ReadonlySet<string> = new Set(["t"]);
|
|
34
|
+
|
|
35
|
+
const surfaces = createBoundedCache<SurfaceEquation | Error>(128);
|
|
36
|
+
const curves = createBoundedCache<SpaceCurveEquation | Error>(128);
|
|
37
|
+
|
|
38
|
+
/** The named values a surface's equation reads: every name that is not one of its variables. */
|
|
39
|
+
export function surfaceParams(source: string): readonly string[] {
|
|
40
|
+
return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The named values a space curve's equation reads. */
|
|
44
|
+
export function spaceCurveParams(source: string): readonly string[] {
|
|
45
|
+
return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** `source` as a surface, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
|
|
49
|
+
export function parseSurface(source: string, params: readonly string[] = []): SurfaceEquation | Error {
|
|
50
|
+
return memo(surfaces, source, params, (text) => {
|
|
51
|
+
const compile = compiler(params);
|
|
52
|
+
const parts = splitTuple(text);
|
|
53
|
+
if (parts) {
|
|
54
|
+
if (parts.length !== 3) throw new Error("A parametric surface has three parts: (x, y, z)");
|
|
55
|
+
const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));
|
|
56
|
+
return { kind: "parametric", x, y, z };
|
|
57
|
+
}
|
|
58
|
+
const sides = splitTopLevel(text, "=");
|
|
59
|
+
if (sides.length > 2) throw new Error("An equation has one '='");
|
|
60
|
+
if (sides.length === 2 && sides[0].trim() !== "z") throw new Error("A surface is written z = …, or as three parts (x, y, z)");
|
|
61
|
+
return { kind: "height", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** `source` as a space curve, or the reason it is not one. */
|
|
66
|
+
export function parseSpaceCurve(source: string, params: readonly string[] = []): SpaceCurveEquation | Error {
|
|
67
|
+
return memo(curves, source, params, (text) => {
|
|
68
|
+
const compile = compiler(params);
|
|
69
|
+
const parts = splitTuple(text);
|
|
70
|
+
if (parts?.length !== 3) throw new Error("A space curve has three parts: (x, y, z)");
|
|
71
|
+
const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));
|
|
72
|
+
return { x, y, z };
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
|
|
77
|
+
export function surfaceError(source: string): string | null {
|
|
78
|
+
if (source.trim() === "") return null;
|
|
79
|
+
const equation = parseSurface(source, surfaceParams(source));
|
|
80
|
+
return equation instanceof Error ? equation.message : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Why `source` will not draw as a space curve, or `null`. */
|
|
84
|
+
export function spaceCurveError(source: string): string | null {
|
|
85
|
+
if (source.trim() === "") return null;
|
|
86
|
+
const equation = parseSpaceCurve(source, spaceCurveParams(source));
|
|
87
|
+
return equation instanceof Error ? equation.message : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function compiler(params: readonly string[]) {
|
|
91
|
+
return (text: string, variables: readonly string[]): CompiledExpression => {
|
|
92
|
+
const result = compileExpressionCached(text.trim(), variables, params);
|
|
93
|
+
if (result instanceof Error) throw result;
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function memo<T extends object>(
|
|
99
|
+
cache: ReturnType<typeof createBoundedCache<T | Error>>,
|
|
100
|
+
source: string,
|
|
101
|
+
params: readonly string[],
|
|
102
|
+
read: (text: string) => T,
|
|
103
|
+
): T | Error {
|
|
104
|
+
const key = `${params.join(",")}|${source}`;
|
|
105
|
+
const hit = cache.get(key);
|
|
106
|
+
if (hit !== undefined) return hit;
|
|
107
|
+
let result: T | Error;
|
|
108
|
+
try {
|
|
109
|
+
const text = source.trim();
|
|
110
|
+
if (text === "") throw new Error("Empty equation");
|
|
111
|
+
result = read(text);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
result = error instanceof Error ? error : new Error(String(error));
|
|
114
|
+
}
|
|
115
|
+
cache.set(key, result);
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */
|
|
120
|
+
export interface EquationLanguage {
|
|
121
|
+
error(source: string): string | null;
|
|
122
|
+
params(source: string): readonly string[];
|
|
123
|
+
variables: readonly string[];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export const EQUATION_KINDS: Readonly<
|
|
127
|
+
Record<"equation" | "surfaceEquation" | "spaceCurveEquation" | "fieldEquation", EquationLanguage>
|
|
128
|
+
> = {
|
|
129
|
+
equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },
|
|
130
|
+
surfaceEquation: {
|
|
131
|
+
error: surfaceError,
|
|
132
|
+
params: surfaceParams,
|
|
133
|
+
variables: [...EQUATION_VARIABLES_3D.height, ...EQUATION_VARIABLES_3D.surface],
|
|
134
|
+
},
|
|
135
|
+
spaceCurveEquation: { error: spaceCurveError, params: spaceCurveParams, variables: EQUATION_VARIABLES_3D.spaceCurve },
|
|
136
|
+
fieldEquation: { error: fieldError, params: fieldParams, variables: FIELD_VARIABLES },
|
|
137
|
+
};
|