@motionscript/math 0.1.0-alpha.8 → 0.1.0-alpha.9
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 +2 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -49
- package/README.md +390 -391
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
- package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
- package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
- package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
- package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
- package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/kit.js +1 -1
- package/dist/browser/tex.js +1 -1
- package/dist/browser/tex.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.js +1 -1
- package/dist/engine.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kit/brace.js.map +1 -1
- package/dist/kit/cache.js.map +1 -1
- package/dist/kit/component.d.ts.map +1 -1
- package/dist/kit/component.js.map +1 -1
- package/dist/kit/contour.js.map +1 -1
- package/dist/kit/coords.js.map +1 -1
- package/dist/kit/curve.js.map +1 -1
- package/dist/kit/equation.js.map +1 -1
- package/dist/kit/equation3d.js.map +1 -1
- package/dist/kit/expression.js.map +1 -1
- package/dist/kit/field-mark.d.ts.map +1 -1
- package/dist/kit/field-mark.js.map +1 -1
- package/dist/kit/field.js.map +1 -1
- package/dist/kit/follow.d.ts.map +1 -1
- package/dist/kit/follow.js.map +1 -1
- package/dist/kit/furniture.js.map +1 -1
- package/dist/kit/geometry.js.map +1 -1
- package/dist/kit/graph.js.map +1 -1
- package/dist/kit/mark.js.map +1 -1
- package/dist/kit/part-fills.js.map +1 -1
- package/dist/kit/place.js.map +1 -1
- package/dist/kit/sampler.js.map +1 -1
- package/dist/kit/scale.js.map +1 -1
- package/dist/kit/source.js.map +1 -1
- package/dist/kit/system.js.map +1 -1
- package/dist/kit/ticks.js.map +1 -1
- package/dist/kit/withdrawn.js.map +1 -1
- package/dist/marks/curve-area.js.map +1 -1
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts.map +1 -1
- package/dist/plots/function-plot.js.map +1 -1
- package/dist/plots/points-plot.js.map +1 -1
- package/dist/plots/stream-lines.js.map +1 -1
- package/dist/plots/traced-path.js.map +1 -1
- package/dist/plots/vector-field.js.map +1 -1
- package/dist/structures/graph-layout.js.map +1 -1
- package/dist/structures/network-graph.js.map +1 -1
- package/dist/structures/sample-space.js.map +1 -1
- package/dist/systems/axes2d.d.ts.map +1 -1
- package/dist/systems/axes2d.js.map +1 -1
- package/dist/systems/number-line.js.map +1 -1
- package/dist/systems/number-plane.js.map +1 -1
- package/dist/systems/polar-plane.js.map +1 -1
- package/dist/tex/geometry.js.map +1 -1
- package/dist/tex/index.js.map +1 -1
- package/dist/tex/mathjax.js.map +1 -1
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts.map +1 -1
- package/dist/tex/typeset.js.map +1 -1
- package/dist/three/axes3d.js.map +1 -1
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +4 -4
- package/registry.json +251 -251
- package/src/annotate/angle-mark.ts +137 -137
- package/src/annotate/curly-brace.ts +148 -148
- package/src/annotate/emphasis-mark.ts +118 -118
- package/src/engine.ts +86 -86
- package/src/index.ts +29 -29
- package/src/kit/brace.ts +37 -37
- package/src/kit/cache.ts +61 -61
- package/src/kit/component.ts +57 -57
- package/src/kit/contour.ts +94 -94
- package/src/kit/coords.ts +278 -278
- package/src/kit/curve.ts +47 -47
- package/src/kit/equation.ts +181 -181
- package/src/kit/equation3d.ts +137 -137
- package/src/kit/expression.ts +159 -159
- package/src/kit/field-mark.ts +171 -171
- package/src/kit/field.ts +50 -50
- package/src/kit/follow.ts +229 -229
- package/src/kit/furniture.ts +113 -113
- package/src/kit/geometry.ts +49 -49
- package/src/kit/graph.ts +116 -116
- package/src/kit/mark.ts +152 -152
- package/src/kit/part-fills.ts +61 -61
- package/src/kit/place.ts +77 -77
- package/src/kit/sampler.ts +281 -281
- package/src/kit/scale.ts +39 -39
- package/src/kit/source.ts +57 -57
- package/src/kit/system.ts +105 -105
- package/src/kit/ticks.ts +85 -85
- package/src/kit/withdrawn.ts +8 -8
- package/src/marks/curve-area.ts +89 -89
- package/src/marks/drop-lines.ts +71 -71
- package/src/marks/plot-point.ts +102 -102
- package/src/marks/riemann-sum.ts +99 -99
- package/src/marks/tangent-line.ts +119 -119
- package/src/marks/vector-arrow.ts +98 -98
- package/src/nodes.ts +99 -99
- package/src/plots/function-plot.ts +220 -220
- package/src/plots/points-plot.ts +103 -103
- package/src/plots/stream-lines.ts +160 -160
- package/src/plots/traced-path.ts +70 -70
- package/src/plots/vector-field.ts +107 -107
- package/src/structures/graph-layout.ts +145 -145
- package/src/structures/network-graph.ts +291 -291
- package/src/structures/sample-space.ts +210 -210
- package/src/systems/axes2d.ts +226 -226
- package/src/systems/number-line.ts +130 -130
- package/src/systems/number-plane.ts +232 -232
- package/src/systems/polar-plane.ts +177 -177
- package/src/tex/geometry.ts +703 -703
- package/src/tex/index.ts +15 -15
- package/src/tex/mathjax.ts +82 -82
- package/src/tex/matrix.ts +97 -97
- package/src/tex/node.ts +74 -74
- package/src/tex/tween.ts +308 -308
- package/src/tex/typeset.ts +461 -461
- package/src/three/axes3d.ts +267 -267
- package/src/three/curve-plot3d.ts +146 -146
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -52
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -318
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
- package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
- package/dist/kit/placement.d.ts +0 -8
- package/dist/kit/placement.d.ts.map +0 -1
- package/dist/kit/placement.js +0 -8
- package/dist/kit/placement.js.map +0 -1
package/src/kit/sampler.ts
CHANGED
|
@@ -1,281 +1,281 @@
|
|
|
1
|
-
import type { Extents, Vector2 } from "@motionscript/sdk";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Turns a curve into the polylines that draw it: adaptively, so a straight
|
|
5
|
-
* stretch costs two points and a tight bend as many as it needs, and broken
|
|
6
|
-
* wherever the curve is, at an asymptote or the edge of its domain.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/** A curve's parameter to its pixel, written into `out`. `false` where the curve is undefined. */
|
|
10
|
-
export type CurveProjection = (t: number, out: Vector2) => boolean;
|
|
11
|
-
|
|
12
|
-
export interface SampleOptions {
|
|
13
|
-
/** The equal steps the parameter range starts as, before any is split. */
|
|
14
|
-
seeds?: number;
|
|
15
|
-
/** A parameter step that is not split again, however bent. */
|
|
16
|
-
minStep?: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Path coordinates are 32-bit floats in the renderer; past this a clamped point still clips to the same edge. */
|
|
20
|
-
const COORD_LIMIT = 16_000;
|
|
21
|
-
/** How far, in pixels, the drawn chord may stray from the curve. */
|
|
22
|
-
const TOLERANCE_PX = 0.25;
|
|
23
|
-
/** A turn, in radians, a chord is allowed to hide: what keeps a symmetric bump from reading as flat. */
|
|
24
|
-
const ANGLE_TOLERANCE = 0.05;
|
|
25
|
-
const MAX_DEPTH = 12;
|
|
26
|
-
const MAX_EVALUATIONS = 40_000;
|
|
27
|
-
const MAX_POINTS = 24_000;
|
|
28
|
-
/** Bisections spent finding where a domain ends. */
|
|
29
|
-
const BOUNDARY_STEPS = 60;
|
|
30
|
-
const DEFAULT_SEEDS = 256;
|
|
31
|
-
|
|
32
|
-
interface Sample {
|
|
33
|
-
t: number;
|
|
34
|
-
px: number;
|
|
35
|
-
py: number;
|
|
36
|
-
/** The pixel before clamping: what tells an asymptote from a steep line. */
|
|
37
|
-
rx: number;
|
|
38
|
-
ry: number;
|
|
39
|
-
ok: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Midpoints are pooled: a refine is a depth-first walk, so a slot is free again
|
|
43
|
-
// as soon as the seed interval it was taken for is done.
|
|
44
|
-
const arena: Sample[] = [];
|
|
45
|
-
let arenaMark = 0;
|
|
46
|
-
|
|
47
|
-
function takeSample(): Sample {
|
|
48
|
-
let slot = arena[arenaMark];
|
|
49
|
-
if (slot === undefined) {
|
|
50
|
-
slot = blank();
|
|
51
|
-
arena.push(slot);
|
|
52
|
-
}
|
|
53
|
-
arenaMark++;
|
|
54
|
-
return slot;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function blank(): Sample {
|
|
58
|
-
return { t: 0, px: 0, py: 0, rx: 0, ry: 0, ok: false };
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const scratch: Vector2 = { x: 0, y: 0 };
|
|
62
|
-
|
|
63
|
-
/** The runs of points that draw `project` over `[t0, t1]`, clipped to `clip`. */
|
|
64
|
-
export function sampleCurve(
|
|
65
|
-
project: CurveProjection,
|
|
66
|
-
t0: number,
|
|
67
|
-
t1: number,
|
|
68
|
-
clip: Extents,
|
|
69
|
-
{ seeds = DEFAULT_SEEDS, minStep = 0 }: SampleOptions = {},
|
|
70
|
-
): Vector2[][] {
|
|
71
|
-
if (!(t1 > t0) || !Number.isFinite(t0) || !Number.isFinite(t1)) return [];
|
|
72
|
-
const pen = new CurvePen(clip);
|
|
73
|
-
let spent = 0;
|
|
74
|
-
|
|
75
|
-
const write = (slot: Sample, t: number): Sample => {
|
|
76
|
-
const defined = project(t, scratch);
|
|
77
|
-
spent++;
|
|
78
|
-
const ok = defined && Number.isFinite(scratch.x) && Number.isFinite(scratch.y);
|
|
79
|
-
slot.t = t;
|
|
80
|
-
slot.rx = ok ? scratch.x : 0;
|
|
81
|
-
slot.ry = ok ? scratch.y : 0;
|
|
82
|
-
slot.px = clampCoord(slot.rx);
|
|
83
|
-
slot.py = clampCoord(slot.ry);
|
|
84
|
-
slot.ok = ok;
|
|
85
|
-
return slot;
|
|
86
|
-
};
|
|
87
|
-
const at = (t: number): Sample => write(takeSample(), t);
|
|
88
|
-
|
|
89
|
-
// The last defined point before `to`, which is not: where `sqrt(x)` meets its axis.
|
|
90
|
-
const edgeOf = (from: Sample, to: Sample): Sample => {
|
|
91
|
-
let inside = from;
|
|
92
|
-
let outsideT = to.t;
|
|
93
|
-
for (let i = 0; i < BOUNDARY_STEPS; i++) {
|
|
94
|
-
if (spent >= MAX_EVALUATIONS || offFrame(inside, clip)) break;
|
|
95
|
-
const midT = (inside.t + outsideT) / 2;
|
|
96
|
-
if (midT === inside.t || midT === outsideT) break;
|
|
97
|
-
const mid = at(midT);
|
|
98
|
-
if (!mid.ok) {
|
|
99
|
-
outsideT = midT;
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
const settled = Math.abs(mid.px - inside.px) < TOLERANCE_PX && Math.abs(mid.py - inside.py) < TOLERANCE_PX;
|
|
103
|
-
inside = mid;
|
|
104
|
-
if (settled) break;
|
|
105
|
-
}
|
|
106
|
-
return inside;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const emit = (a: Sample, m: Sample | null, b: Sample, floored: boolean): void => {
|
|
110
|
-
if (a.ok && b.ok) {
|
|
111
|
-
if (floored && diverges(a, m, b, clip)) pen.break();
|
|
112
|
-
else pen.draw(a, b);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
if (a.ok) {
|
|
116
|
-
const edge = edgeOf(a, b);
|
|
117
|
-
if (edge.t !== a.t) pen.draw(a, edge);
|
|
118
|
-
pen.break();
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
pen.break();
|
|
122
|
-
if (b.ok) {
|
|
123
|
-
const edge = edgeOf(b, a);
|
|
124
|
-
if (edge.t !== b.t) pen.draw(edge, b);
|
|
125
|
-
else pen.start(b);
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
const refine = (a: Sample, b: Sample, depth: number): void => {
|
|
130
|
-
if (spent >= MAX_EVALUATIONS || pen.points >= MAX_POINTS) {
|
|
131
|
-
emit(a, null, b, false);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const m = at((a.t + b.t) / 2);
|
|
135
|
-
const floored = depth >= MAX_DEPTH || b.t - a.t <= minStep;
|
|
136
|
-
if (floored || flat(a, m, b)) {
|
|
137
|
-
emit(a, m, b, floored);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
refine(a, m, depth + 1);
|
|
141
|
-
refine(m, b, depth + 1);
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
const count = Math.max(1, Math.round(seeds));
|
|
145
|
-
const step = (t1 - t0) / count;
|
|
146
|
-
const ends: [Sample, Sample] = [blank(), blank()];
|
|
147
|
-
let previous = write(ends[0], t0);
|
|
148
|
-
for (let i = 1; i <= count; i++) {
|
|
149
|
-
const next = write(ends[i & 1], i === count ? t1 : t0 + i * step);
|
|
150
|
-
arenaMark = 0;
|
|
151
|
-
refine(previous, next, 0);
|
|
152
|
-
previous = next;
|
|
153
|
-
}
|
|
154
|
-
arenaMark = 0;
|
|
155
|
-
return pen.finish();
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function flat(a: Sample, m: Sample, b: Sample): boolean {
|
|
159
|
-
if (a.ok !== m.ok || m.ok !== b.ok) return false;
|
|
160
|
-
if (!a.ok) return true;
|
|
161
|
-
const dx = b.px - a.px;
|
|
162
|
-
const dy = b.py - a.py;
|
|
163
|
-
const length = Math.hypot(dx, dy) || 1;
|
|
164
|
-
if (Math.abs((m.px - a.px) * dy - (m.py - a.py) * dx) / length > TOLERANCE_PX) return false;
|
|
165
|
-
let turn = Math.abs(Math.atan2(m.py - a.py, m.px - a.px) - Math.atan2(b.py - m.py, b.px - m.px));
|
|
166
|
-
if (turn > Math.PI) turn = 2 * Math.PI - turn;
|
|
167
|
-
return turn < ANGLE_TOLERANCE;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Two neighbours on opposite sides of the frame, with nothing between them that
|
|
171
|
-
// is between them: an asymptote, not a steep line.
|
|
172
|
-
function diverges(a: Sample, m: Sample | null, b: Sample, clip: Extents): boolean {
|
|
173
|
-
const straddles =
|
|
174
|
-
(a.py > clip.maxY && b.py < clip.minY) || (a.py < clip.minY && b.py > clip.maxY) ||
|
|
175
|
-
(a.px > clip.maxX && b.px < clip.minX) || (a.px < clip.minX && b.px > clip.maxX);
|
|
176
|
-
if (!straddles || m === null) return false;
|
|
177
|
-
if (!m.ok) return true;
|
|
178
|
-
return (
|
|
179
|
-
m.ry < Math.min(a.ry, b.ry) || m.ry > Math.max(a.ry, b.ry) ||
|
|
180
|
-
m.rx < Math.min(a.rx, b.rx) || m.rx > Math.max(a.rx, b.rx)
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function offFrame(sample: Sample, clip: Extents): boolean {
|
|
185
|
-
return sample.py > clip.maxY || sample.py < clip.minY || sample.px > clip.maxX || sample.px < clip.minX;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function clampCoord(value: number): number {
|
|
189
|
-
if (value > COORD_LIMIT) return COORD_LIMIT;
|
|
190
|
-
if (value < -COORD_LIMIT) return -COORD_LIMIT;
|
|
191
|
-
return value;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
class CurvePen {
|
|
195
|
-
points = 0;
|
|
196
|
-
private readonly runs: Vector2[][] = [];
|
|
197
|
-
private open: Vector2[] = [];
|
|
198
|
-
private cursor: Vector2 | null = null;
|
|
199
|
-
|
|
200
|
-
constructor(private readonly clip: Extents) {}
|
|
201
|
-
|
|
202
|
-
draw(a: Sample, b: Sample): void {
|
|
203
|
-
const segment = clipSegment(a.px, a.py, b.px, b.py, this.clip);
|
|
204
|
-
if (!segment) {
|
|
205
|
-
this.break();
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
const [start, end] = segment;
|
|
209
|
-
if (this.cursor && samePoint(this.cursor, start)) {
|
|
210
|
-
this.push(end);
|
|
211
|
-
} else {
|
|
212
|
-
this.break();
|
|
213
|
-
this.push(start);
|
|
214
|
-
this.push(end);
|
|
215
|
-
}
|
|
216
|
-
this.cursor = end;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
start(b: Sample): void {
|
|
220
|
-
this.break();
|
|
221
|
-
if (!inside(b.px, b.py, this.clip)) return;
|
|
222
|
-
this.cursor = { x: b.px, y: b.py };
|
|
223
|
-
this.push(this.cursor);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
break(): void {
|
|
227
|
-
if (this.open.length >= 2) this.runs.push(this.open);
|
|
228
|
-
this.open = [];
|
|
229
|
-
this.cursor = null;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
finish(): Vector2[][] {
|
|
233
|
-
this.break();
|
|
234
|
-
return this.runs;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
private push(point: Vector2): void {
|
|
238
|
-
this.open.push(point);
|
|
239
|
-
this.points++;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function samePoint(a: Vector2, b: Vector2): boolean {
|
|
244
|
-
return Math.abs(a.x - b.x) < 0.1 && Math.abs(a.y - b.y) < 0.1;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
function inside(x: number, y: number, clip: Extents): boolean {
|
|
248
|
-
return x >= clip.minX && x <= clip.maxX && y >= clip.minY && y <= clip.maxY;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Liang–Barsky: the part of a segment inside the clip, or null when none is.
|
|
252
|
-
function clipSegment(ax: number, ay: number, bx: number, by: number, clip: Extents): [Vector2, Vector2] | null {
|
|
253
|
-
const dx = bx - ax;
|
|
254
|
-
const dy = by - ay;
|
|
255
|
-
let t0 = 0;
|
|
256
|
-
let t1 = 1;
|
|
257
|
-
const edges: [number, number][] = [
|
|
258
|
-
[-dx, ax - clip.minX],
|
|
259
|
-
[dx, clip.maxX - ax],
|
|
260
|
-
[-dy, ay - clip.minY],
|
|
261
|
-
[dy, clip.maxY - ay],
|
|
262
|
-
];
|
|
263
|
-
for (const [p, q] of edges) {
|
|
264
|
-
if (p === 0) {
|
|
265
|
-
if (q < 0) return null;
|
|
266
|
-
continue;
|
|
267
|
-
}
|
|
268
|
-
const r = q / p;
|
|
269
|
-
if (p < 0) {
|
|
270
|
-
if (r > t1) return null;
|
|
271
|
-
if (r > t0) t0 = r;
|
|
272
|
-
} else {
|
|
273
|
-
if (r < t0) return null;
|
|
274
|
-
if (r < t1) t1 = r;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return [
|
|
278
|
-
{ x: ax + t0 * dx, y: ay + t0 * dy },
|
|
279
|
-
{ x: ax + t1 * dx, y: ay + t1 * dy },
|
|
280
|
-
];
|
|
281
|
-
}
|
|
1
|
+
import type { Extents, Vector2 } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Turns a curve into the polylines that draw it: adaptively, so a straight
|
|
5
|
+
* stretch costs two points and a tight bend as many as it needs, and broken
|
|
6
|
+
* wherever the curve is, at an asymptote or the edge of its domain.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** A curve's parameter to its pixel, written into `out`. `false` where the curve is undefined. */
|
|
10
|
+
export type CurveProjection = (t: number, out: Vector2) => boolean;
|
|
11
|
+
|
|
12
|
+
export interface SampleOptions {
|
|
13
|
+
/** The equal steps the parameter range starts as, before any is split. */
|
|
14
|
+
seeds?: number;
|
|
15
|
+
/** A parameter step that is not split again, however bent. */
|
|
16
|
+
minStep?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Path coordinates are 32-bit floats in the renderer; past this a clamped point still clips to the same edge. */
|
|
20
|
+
const COORD_LIMIT = 16_000;
|
|
21
|
+
/** How far, in pixels, the drawn chord may stray from the curve. */
|
|
22
|
+
const TOLERANCE_PX = 0.25;
|
|
23
|
+
/** A turn, in radians, a chord is allowed to hide: what keeps a symmetric bump from reading as flat. */
|
|
24
|
+
const ANGLE_TOLERANCE = 0.05;
|
|
25
|
+
const MAX_DEPTH = 12;
|
|
26
|
+
const MAX_EVALUATIONS = 40_000;
|
|
27
|
+
const MAX_POINTS = 24_000;
|
|
28
|
+
/** Bisections spent finding where a domain ends. */
|
|
29
|
+
const BOUNDARY_STEPS = 60;
|
|
30
|
+
const DEFAULT_SEEDS = 256;
|
|
31
|
+
|
|
32
|
+
interface Sample {
|
|
33
|
+
t: number;
|
|
34
|
+
px: number;
|
|
35
|
+
py: number;
|
|
36
|
+
/** The pixel before clamping: what tells an asymptote from a steep line. */
|
|
37
|
+
rx: number;
|
|
38
|
+
ry: number;
|
|
39
|
+
ok: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Midpoints are pooled: a refine is a depth-first walk, so a slot is free again
|
|
43
|
+
// as soon as the seed interval it was taken for is done.
|
|
44
|
+
const arena: Sample[] = [];
|
|
45
|
+
let arenaMark = 0;
|
|
46
|
+
|
|
47
|
+
function takeSample(): Sample {
|
|
48
|
+
let slot = arena[arenaMark];
|
|
49
|
+
if (slot === undefined) {
|
|
50
|
+
slot = blank();
|
|
51
|
+
arena.push(slot);
|
|
52
|
+
}
|
|
53
|
+
arenaMark++;
|
|
54
|
+
return slot;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function blank(): Sample {
|
|
58
|
+
return { t: 0, px: 0, py: 0, rx: 0, ry: 0, ok: false };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const scratch: Vector2 = { x: 0, y: 0 };
|
|
62
|
+
|
|
63
|
+
/** The runs of points that draw `project` over `[t0, t1]`, clipped to `clip`. */
|
|
64
|
+
export function sampleCurve(
|
|
65
|
+
project: CurveProjection,
|
|
66
|
+
t0: number,
|
|
67
|
+
t1: number,
|
|
68
|
+
clip: Extents,
|
|
69
|
+
{ seeds = DEFAULT_SEEDS, minStep = 0 }: SampleOptions = {},
|
|
70
|
+
): Vector2[][] {
|
|
71
|
+
if (!(t1 > t0) || !Number.isFinite(t0) || !Number.isFinite(t1)) return [];
|
|
72
|
+
const pen = new CurvePen(clip);
|
|
73
|
+
let spent = 0;
|
|
74
|
+
|
|
75
|
+
const write = (slot: Sample, t: number): Sample => {
|
|
76
|
+
const defined = project(t, scratch);
|
|
77
|
+
spent++;
|
|
78
|
+
const ok = defined && Number.isFinite(scratch.x) && Number.isFinite(scratch.y);
|
|
79
|
+
slot.t = t;
|
|
80
|
+
slot.rx = ok ? scratch.x : 0;
|
|
81
|
+
slot.ry = ok ? scratch.y : 0;
|
|
82
|
+
slot.px = clampCoord(slot.rx);
|
|
83
|
+
slot.py = clampCoord(slot.ry);
|
|
84
|
+
slot.ok = ok;
|
|
85
|
+
return slot;
|
|
86
|
+
};
|
|
87
|
+
const at = (t: number): Sample => write(takeSample(), t);
|
|
88
|
+
|
|
89
|
+
// The last defined point before `to`, which is not: where `sqrt(x)` meets its axis.
|
|
90
|
+
const edgeOf = (from: Sample, to: Sample): Sample => {
|
|
91
|
+
let inside = from;
|
|
92
|
+
let outsideT = to.t;
|
|
93
|
+
for (let i = 0; i < BOUNDARY_STEPS; i++) {
|
|
94
|
+
if (spent >= MAX_EVALUATIONS || offFrame(inside, clip)) break;
|
|
95
|
+
const midT = (inside.t + outsideT) / 2;
|
|
96
|
+
if (midT === inside.t || midT === outsideT) break;
|
|
97
|
+
const mid = at(midT);
|
|
98
|
+
if (!mid.ok) {
|
|
99
|
+
outsideT = midT;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const settled = Math.abs(mid.px - inside.px) < TOLERANCE_PX && Math.abs(mid.py - inside.py) < TOLERANCE_PX;
|
|
103
|
+
inside = mid;
|
|
104
|
+
if (settled) break;
|
|
105
|
+
}
|
|
106
|
+
return inside;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const emit = (a: Sample, m: Sample | null, b: Sample, floored: boolean): void => {
|
|
110
|
+
if (a.ok && b.ok) {
|
|
111
|
+
if (floored && diverges(a, m, b, clip)) pen.break();
|
|
112
|
+
else pen.draw(a, b);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (a.ok) {
|
|
116
|
+
const edge = edgeOf(a, b);
|
|
117
|
+
if (edge.t !== a.t) pen.draw(a, edge);
|
|
118
|
+
pen.break();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
pen.break();
|
|
122
|
+
if (b.ok) {
|
|
123
|
+
const edge = edgeOf(b, a);
|
|
124
|
+
if (edge.t !== b.t) pen.draw(edge, b);
|
|
125
|
+
else pen.start(b);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const refine = (a: Sample, b: Sample, depth: number): void => {
|
|
130
|
+
if (spent >= MAX_EVALUATIONS || pen.points >= MAX_POINTS) {
|
|
131
|
+
emit(a, null, b, false);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const m = at((a.t + b.t) / 2);
|
|
135
|
+
const floored = depth >= MAX_DEPTH || b.t - a.t <= minStep;
|
|
136
|
+
if (floored || flat(a, m, b)) {
|
|
137
|
+
emit(a, m, b, floored);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
refine(a, m, depth + 1);
|
|
141
|
+
refine(m, b, depth + 1);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const count = Math.max(1, Math.round(seeds));
|
|
145
|
+
const step = (t1 - t0) / count;
|
|
146
|
+
const ends: [Sample, Sample] = [blank(), blank()];
|
|
147
|
+
let previous = write(ends[0], t0);
|
|
148
|
+
for (let i = 1; i <= count; i++) {
|
|
149
|
+
const next = write(ends[i & 1], i === count ? t1 : t0 + i * step);
|
|
150
|
+
arenaMark = 0;
|
|
151
|
+
refine(previous, next, 0);
|
|
152
|
+
previous = next;
|
|
153
|
+
}
|
|
154
|
+
arenaMark = 0;
|
|
155
|
+
return pen.finish();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function flat(a: Sample, m: Sample, b: Sample): boolean {
|
|
159
|
+
if (a.ok !== m.ok || m.ok !== b.ok) return false;
|
|
160
|
+
if (!a.ok) return true;
|
|
161
|
+
const dx = b.px - a.px;
|
|
162
|
+
const dy = b.py - a.py;
|
|
163
|
+
const length = Math.hypot(dx, dy) || 1;
|
|
164
|
+
if (Math.abs((m.px - a.px) * dy - (m.py - a.py) * dx) / length > TOLERANCE_PX) return false;
|
|
165
|
+
let turn = Math.abs(Math.atan2(m.py - a.py, m.px - a.px) - Math.atan2(b.py - m.py, b.px - m.px));
|
|
166
|
+
if (turn > Math.PI) turn = 2 * Math.PI - turn;
|
|
167
|
+
return turn < ANGLE_TOLERANCE;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Two neighbours on opposite sides of the frame, with nothing between them that
|
|
171
|
+
// is between them: an asymptote, not a steep line.
|
|
172
|
+
function diverges(a: Sample, m: Sample | null, b: Sample, clip: Extents): boolean {
|
|
173
|
+
const straddles =
|
|
174
|
+
(a.py > clip.maxY && b.py < clip.minY) || (a.py < clip.minY && b.py > clip.maxY) ||
|
|
175
|
+
(a.px > clip.maxX && b.px < clip.minX) || (a.px < clip.minX && b.px > clip.maxX);
|
|
176
|
+
if (!straddles || m === null) return false;
|
|
177
|
+
if (!m.ok) return true;
|
|
178
|
+
return (
|
|
179
|
+
m.ry < Math.min(a.ry, b.ry) || m.ry > Math.max(a.ry, b.ry) ||
|
|
180
|
+
m.rx < Math.min(a.rx, b.rx) || m.rx > Math.max(a.rx, b.rx)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function offFrame(sample: Sample, clip: Extents): boolean {
|
|
185
|
+
return sample.py > clip.maxY || sample.py < clip.minY || sample.px > clip.maxX || sample.px < clip.minX;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function clampCoord(value: number): number {
|
|
189
|
+
if (value > COORD_LIMIT) return COORD_LIMIT;
|
|
190
|
+
if (value < -COORD_LIMIT) return -COORD_LIMIT;
|
|
191
|
+
return value;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
class CurvePen {
|
|
195
|
+
points = 0;
|
|
196
|
+
private readonly runs: Vector2[][] = [];
|
|
197
|
+
private open: Vector2[] = [];
|
|
198
|
+
private cursor: Vector2 | null = null;
|
|
199
|
+
|
|
200
|
+
constructor(private readonly clip: Extents) {}
|
|
201
|
+
|
|
202
|
+
draw(a: Sample, b: Sample): void {
|
|
203
|
+
const segment = clipSegment(a.px, a.py, b.px, b.py, this.clip);
|
|
204
|
+
if (!segment) {
|
|
205
|
+
this.break();
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const [start, end] = segment;
|
|
209
|
+
if (this.cursor && samePoint(this.cursor, start)) {
|
|
210
|
+
this.push(end);
|
|
211
|
+
} else {
|
|
212
|
+
this.break();
|
|
213
|
+
this.push(start);
|
|
214
|
+
this.push(end);
|
|
215
|
+
}
|
|
216
|
+
this.cursor = end;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
start(b: Sample): void {
|
|
220
|
+
this.break();
|
|
221
|
+
if (!inside(b.px, b.py, this.clip)) return;
|
|
222
|
+
this.cursor = { x: b.px, y: b.py };
|
|
223
|
+
this.push(this.cursor);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
break(): void {
|
|
227
|
+
if (this.open.length >= 2) this.runs.push(this.open);
|
|
228
|
+
this.open = [];
|
|
229
|
+
this.cursor = null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
finish(): Vector2[][] {
|
|
233
|
+
this.break();
|
|
234
|
+
return this.runs;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private push(point: Vector2): void {
|
|
238
|
+
this.open.push(point);
|
|
239
|
+
this.points++;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function samePoint(a: Vector2, b: Vector2): boolean {
|
|
244
|
+
return Math.abs(a.x - b.x) < 0.1 && Math.abs(a.y - b.y) < 0.1;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function inside(x: number, y: number, clip: Extents): boolean {
|
|
248
|
+
return x >= clip.minX && x <= clip.maxX && y >= clip.minY && y <= clip.maxY;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Liang–Barsky: the part of a segment inside the clip, or null when none is.
|
|
252
|
+
function clipSegment(ax: number, ay: number, bx: number, by: number, clip: Extents): [Vector2, Vector2] | null {
|
|
253
|
+
const dx = bx - ax;
|
|
254
|
+
const dy = by - ay;
|
|
255
|
+
let t0 = 0;
|
|
256
|
+
let t1 = 1;
|
|
257
|
+
const edges: [number, number][] = [
|
|
258
|
+
[-dx, ax - clip.minX],
|
|
259
|
+
[dx, clip.maxX - ax],
|
|
260
|
+
[-dy, ay - clip.minY],
|
|
261
|
+
[dy, clip.maxY - ay],
|
|
262
|
+
];
|
|
263
|
+
for (const [p, q] of edges) {
|
|
264
|
+
if (p === 0) {
|
|
265
|
+
if (q < 0) return null;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const r = q / p;
|
|
269
|
+
if (p < 0) {
|
|
270
|
+
if (r > t1) return null;
|
|
271
|
+
if (r > t0) t0 = r;
|
|
272
|
+
} else {
|
|
273
|
+
if (r < t0) return null;
|
|
274
|
+
if (r < t1) t1 = r;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return [
|
|
278
|
+
{ x: ax + t0 * dx, y: ay + t0 * dy },
|
|
279
|
+
{ x: ax + t1 * dx, y: ay + t1 * dy },
|
|
280
|
+
];
|
|
281
|
+
}
|
package/src/kit/scale.ts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { colorOps, fillOps, type Color, type Fill, type FillResolved, type NormalizedColor } from "@motionscript/sdk";
|
|
2
|
-
import { clamp } from "./geometry";
|
|
3
|
-
|
|
4
|
-
/** A run of colours a value is read off, lowest first: a surface's height, a field's strength. */
|
|
5
|
-
|
|
6
|
-
export function readScale(value: readonly Color[] | undefined): NormalizedColor[] {
|
|
7
|
-
return (value ?? []).map((color) => colorOps.resolve(color));
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function lerpScale(from: NormalizedColor[], to: NormalizedColor[], t: number): NormalizedColor[] {
|
|
11
|
-
if (from.length !== to.length) return t < 1 ? from : to;
|
|
12
|
-
return to.map((color, index) => colorOps.lerp(from[index], color, t));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** The colour `share` of the way along `scale`, 0 to 1. */
|
|
16
|
-
export function colorAt(scale: readonly NormalizedColor[], share: number): NormalizedColor {
|
|
17
|
-
if (scale.length === 1) return scale[0];
|
|
18
|
-
const at = clamp(share, 0, 1) * (scale.length - 1);
|
|
19
|
-
const index = Math.min(scale.length - 2, Math.floor(at));
|
|
20
|
-
return colorOps.lerp(scale[index], scale[index + 1], at - index);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** A run of paints a value is read off, lowest first: a field's strength. Every entry is kept as written. */
|
|
24
|
-
export function readFillScale(value: readonly Fill[] | undefined): FillResolved[][] {
|
|
25
|
-
return (Array.isArray(value) ? value : []).map((fill) => fillOps.resolve(fill ?? []));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function lerpFillScale(from: FillResolved[][], to: FillResolved[][], t: number): FillResolved[][] {
|
|
29
|
-
if (from.length !== to.length) return t < 1 ? from : to;
|
|
30
|
-
return to.map((fill, index) => fillOps.lerp(from[index], fill, t));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** The paint `share` of the way along `scale`, 0 to 1. */
|
|
34
|
-
export function fillAt(scale: readonly FillResolved[][], share: number): FillResolved[] {
|
|
35
|
-
if (scale.length === 1) return scale[0];
|
|
36
|
-
const at = clamp(share, 0, 1) * (scale.length - 1);
|
|
37
|
-
const index = Math.min(scale.length - 2, Math.floor(at));
|
|
38
|
-
return fillOps.lerp(scale[index], scale[index + 1], at - index);
|
|
39
|
-
}
|
|
1
|
+
import { colorOps, fillOps, type Color, type Fill, type FillResolved, type NormalizedColor } from "@motionscript/sdk";
|
|
2
|
+
import { clamp } from "./geometry";
|
|
3
|
+
|
|
4
|
+
/** A run of colours a value is read off, lowest first: a surface's height, a field's strength. */
|
|
5
|
+
|
|
6
|
+
export function readScale(value: readonly Color[] | undefined): NormalizedColor[] {
|
|
7
|
+
return (value ?? []).map((color) => colorOps.resolve(color));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function lerpScale(from: NormalizedColor[], to: NormalizedColor[], t: number): NormalizedColor[] {
|
|
11
|
+
if (from.length !== to.length) return t < 1 ? from : to;
|
|
12
|
+
return to.map((color, index) => colorOps.lerp(from[index], color, t));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The colour `share` of the way along `scale`, 0 to 1. */
|
|
16
|
+
export function colorAt(scale: readonly NormalizedColor[], share: number): NormalizedColor {
|
|
17
|
+
if (scale.length === 1) return scale[0];
|
|
18
|
+
const at = clamp(share, 0, 1) * (scale.length - 1);
|
|
19
|
+
const index = Math.min(scale.length - 2, Math.floor(at));
|
|
20
|
+
return colorOps.lerp(scale[index], scale[index + 1], at - index);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A run of paints a value is read off, lowest first: a field's strength. Every entry is kept as written. */
|
|
24
|
+
export function readFillScale(value: readonly Fill[] | undefined): FillResolved[][] {
|
|
25
|
+
return (Array.isArray(value) ? value : []).map((fill) => fillOps.resolve(fill ?? []));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function lerpFillScale(from: FillResolved[][], to: FillResolved[][], t: number): FillResolved[][] {
|
|
29
|
+
if (from.length !== to.length) return t < 1 ? from : to;
|
|
30
|
+
return to.map((fill, index) => fillOps.lerp(from[index], fill, t));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The paint `share` of the way along `scale`, 0 to 1. */
|
|
34
|
+
export function fillAt(scale: readonly FillResolved[][], share: number): FillResolved[] {
|
|
35
|
+
if (scale.length === 1) return scale[0];
|
|
36
|
+
const at = clamp(share, 0, 1) * (scale.length - 1);
|
|
37
|
+
const index = Math.min(scale.length - 2, Math.floor(at));
|
|
38
|
+
return fillOps.lerp(scale[index], scale[index + 1], at - index);
|
|
39
|
+
}
|