@motionscript/math 0.1.0-alpha.7 → 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 +4 -0
- package/LICENSE +201 -201
- package/NOTICE +49 -48
- package/README.md +390 -391
- package/dist/annotate/angle-mark.d.ts +6 -6
- package/dist/annotate/angle-mark.js.map +1 -1
- package/dist/annotate/curly-brace.d.ts +9 -9
- package/dist/annotate/curly-brace.js.map +1 -1
- package/dist/annotate/emphasis-mark.d.ts +9 -9
- package/dist/annotate/emphasis-mark.js.map +1 -1
- package/dist/browser/chunks/{chunk-ABYBW64J.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 +3 -3
- package/dist/browser/kit.js +1 -1
- package/dist/browser/manifest.json +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 +2 -2
- 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 +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.d.ts +6 -6
- package/dist/marks/drop-lines.js.map +1 -1
- package/dist/marks/plot-point.d.ts +3 -3
- package/dist/marks/plot-point.js.map +1 -1
- package/dist/marks/riemann-sum.d.ts +3 -3
- package/dist/marks/riemann-sum.js.map +1 -1
- package/dist/marks/tangent-line.d.ts +6 -6
- package/dist/marks/tangent-line.js.map +1 -1
- package/dist/marks/vector-arrow.d.ts +6 -6
- package/dist/marks/vector-arrow.js.map +1 -1
- package/dist/nodes.js.map +1 -1
- package/dist/plots/function-plot.d.ts +8 -8
- 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.d.ts +2 -2
- package/dist/tex/matrix.js.map +1 -1
- package/dist/tex/node.d.ts +2 -2
- package/dist/tex/node.js.map +1 -1
- package/dist/tex/tween.js.map +1 -1
- package/dist/tex/typeset.d.ts +4 -4
- 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.d.ts +27 -27
- package/dist/three/curve-plot3d.d.ts.map +1 -1
- package/dist/three/curve-plot3d.js +3 -2
- package/dist/three/curve-plot3d.js.map +1 -1
- package/dist/three/entries.js.map +1 -1
- package/dist/three/label3d.d.ts.map +1 -1
- package/dist/three/label3d.js +3 -1
- package/dist/three/label3d.js.map +1 -1
- package/dist/three/plot-point3d.d.ts +27 -27
- package/dist/three/plot-point3d.js.map +1 -1
- package/dist/three/space.js.map +1 -1
- package/dist/three/surface-plot3d.d.ts +36 -36
- package/dist/three/surface-plot3d.js +6 -1
- package/dist/three/surface-plot3d.js.map +1 -1
- package/dist/three/vector-arrow3d.d.ts +27 -27
- package/dist/three/vector-arrow3d.js.map +1 -1
- package/package.json +5 -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 -144
- package/src/three/entries.ts +42 -42
- package/src/three/label3d.ts +52 -50
- package/src/three/plot-point3d.ts +63 -63
- package/src/three/space.ts +92 -92
- package/src/three/surface-plot3d.ts +318 -312
- package/src/three/vector-arrow3d.ts +88 -88
- package/dist/browser/chunks/chunk-ABYBW64J.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/cache.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A bounded, least-recently-used memo.
|
|
3
|
-
*
|
|
4
|
-
* Sits under `geometry.ts`, and imports nothing, so it is testable in bare Node:
|
|
5
|
-
* everything else in this package reaches MathJax's browser adaptor at module
|
|
6
|
-
* scope, which means a test that touches it needs a DOM. The one piece of logic
|
|
7
|
-
* here worth being sure about — what gets thrown away when the memo is full —
|
|
8
|
-
* is exactly the piece that does not need one.
|
|
9
|
-
*
|
|
10
|
-
* Least-recently-used rather than oldest-first, and that matters for the shape
|
|
11
|
-
* of work this sees. A `Latex` node re-resolves its formula whenever `fontSize`
|
|
12
|
-
* changes, so animating the size of one formula inserts a fresh key every frame
|
|
13
|
-
* and would, under a first-in-first-out rule, walk the whole memo out of it in a
|
|
14
|
-
* couple of seconds — including the entries for every *other* formula in the
|
|
15
|
-
* scene, none of which had done anything wrong. Touching an entry on read keeps
|
|
16
|
-
* the formulas that are actually being asked for ahead of the ones streaming
|
|
17
|
-
* past.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
export interface BoundedCache<T> {
|
|
21
|
-
get(key: string): T | undefined;
|
|
22
|
-
set(key: string, value: T): void;
|
|
23
|
-
/** Entries held. For tests and for anyone measuring. */
|
|
24
|
-
readonly size: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @param limit Most entries to hold. One more arriving evicts the least
|
|
29
|
-
* recently read or written.
|
|
30
|
-
*/
|
|
31
|
-
export function createBoundedCache<T>(limit: number): BoundedCache<T> {
|
|
32
|
-
// Insertion order is the eviction order, which is what makes a plain `Map`
|
|
33
|
-
// an LRU as long as every read re-inserts.
|
|
34
|
-
const entries = new Map<string, T>();
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
get(key) {
|
|
38
|
-
if (!entries.has(key)) return undefined;
|
|
39
|
-
const value = entries.get(key) as T;
|
|
40
|
-
// Delete-then-set moves the key to the end, so the next eviction
|
|
41
|
-
// takes the genuinely coldest entry rather than the oldest one.
|
|
42
|
-
entries.delete(key);
|
|
43
|
-
entries.set(key, value);
|
|
44
|
-
return value;
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
set(key, value) {
|
|
48
|
-
entries.delete(key);
|
|
49
|
-
entries.set(key, value);
|
|
50
|
-
while (entries.size > limit) {
|
|
51
|
-
const coldest = entries.keys().next().value;
|
|
52
|
-
if (coldest === undefined) break;
|
|
53
|
-
entries.delete(coldest);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
get size() {
|
|
58
|
-
return entries.size;
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* A bounded, least-recently-used memo.
|
|
3
|
+
*
|
|
4
|
+
* Sits under `geometry.ts`, and imports nothing, so it is testable in bare Node:
|
|
5
|
+
* everything else in this package reaches MathJax's browser adaptor at module
|
|
6
|
+
* scope, which means a test that touches it needs a DOM. The one piece of logic
|
|
7
|
+
* here worth being sure about — what gets thrown away when the memo is full —
|
|
8
|
+
* is exactly the piece that does not need one.
|
|
9
|
+
*
|
|
10
|
+
* Least-recently-used rather than oldest-first, and that matters for the shape
|
|
11
|
+
* of work this sees. A `Latex` node re-resolves its formula whenever `fontSize`
|
|
12
|
+
* changes, so animating the size of one formula inserts a fresh key every frame
|
|
13
|
+
* and would, under a first-in-first-out rule, walk the whole memo out of it in a
|
|
14
|
+
* couple of seconds — including the entries for every *other* formula in the
|
|
15
|
+
* scene, none of which had done anything wrong. Touching an entry on read keeps
|
|
16
|
+
* the formulas that are actually being asked for ahead of the ones streaming
|
|
17
|
+
* past.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface BoundedCache<T> {
|
|
21
|
+
get(key: string): T | undefined;
|
|
22
|
+
set(key: string, value: T): void;
|
|
23
|
+
/** Entries held. For tests and for anyone measuring. */
|
|
24
|
+
readonly size: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param limit Most entries to hold. One more arriving evicts the least
|
|
29
|
+
* recently read or written.
|
|
30
|
+
*/
|
|
31
|
+
export function createBoundedCache<T>(limit: number): BoundedCache<T> {
|
|
32
|
+
// Insertion order is the eviction order, which is what makes a plain `Map`
|
|
33
|
+
// an LRU as long as every read re-inserts.
|
|
34
|
+
const entries = new Map<string, T>();
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
get(key) {
|
|
38
|
+
if (!entries.has(key)) return undefined;
|
|
39
|
+
const value = entries.get(key) as T;
|
|
40
|
+
// Delete-then-set moves the key to the end, so the next eviction
|
|
41
|
+
// takes the genuinely coldest entry rather than the oldest one.
|
|
42
|
+
entries.delete(key);
|
|
43
|
+
entries.set(key, value);
|
|
44
|
+
return value;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
set(key, value) {
|
|
48
|
+
entries.delete(key);
|
|
49
|
+
entries.set(key, value);
|
|
50
|
+
while (entries.size > limit) {
|
|
51
|
+
const coldest = entries.keys().next().value;
|
|
52
|
+
if (coldest === undefined) break;
|
|
53
|
+
entries.delete(coldest);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
get size() {
|
|
58
|
+
return entries.size;
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
package/src/kit/component.ts
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Paint,
|
|
3
|
-
Types,
|
|
4
|
-
type EntryOptions,
|
|
5
|
-
type RenderContext,
|
|
6
|
-
type Type,
|
|
7
|
-
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
8
|
-
type Fill as _Fill,
|
|
9
|
-
type FillResolved as _FillResolved,
|
|
10
|
-
type Shadow as _Shadow,
|
|
11
|
-
type ShadowResolved as _ShadowResolved,
|
|
12
|
-
type Stroke as _Stroke,
|
|
13
|
-
type StrokeResolved as _StrokeResolved,
|
|
14
|
-
} from "@motionscript/sdk";
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* What a component in this kit shares beyond the factory: the paints a math
|
|
18
|
-
* node draws with, listed choices, and the order a node paints in.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
const { overlay: _overlay, ...paint } = Paint.state;
|
|
22
|
-
const { overlayTo: _overlayTo, ...paintCommands } = Paint.commands;
|
|
23
|
-
|
|
24
|
-
/** A math node's paints: fill, stroke, shadow and the outline trim, with no overlay. */
|
|
25
|
-
export const PAINT = paint;
|
|
26
|
-
export const PAINT_COMMANDS = paintCommands;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* One of `values`, held as text with its choices listed (no `kind: "enum"`).
|
|
30
|
-
*/
|
|
31
|
-
export function listed<const V extends readonly string[]>(values: V, initial: V[number], options?: ListedOptions): Type<V[number]>;
|
|
32
|
-
/** With no default: an arg left out is one the command was not given. */
|
|
33
|
-
export function listed<const V extends readonly string[]>(values: V, initial: undefined, options?: ListedOptions): Type<V[number] | undefined>;
|
|
34
|
-
export function listed(values: readonly string[], initial: string | undefined, options: ListedOptions = {}): Type<string | undefined> {
|
|
35
|
-
return Types.custom<string | undefined, string | undefined, EntryOptions & { readonly options: readonly string[] }>(initial, { ...options, options: values });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
interface ListedOptions {
|
|
39
|
-
readonly animatable?: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** `record` without `keys`: the paints a node has no use for. */
|
|
43
|
-
export function omit<T extends object, const K extends keyof T>(record: T, ...keys: K[]): Omit<T, K> {
|
|
44
|
-
const out = { ...record };
|
|
45
|
-
for (const key of keys) delete out[key];
|
|
46
|
-
return out;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* A node's paint order: its own paint under its children, then what it lays
|
|
51
|
-
* over them, each cut at the near plane of a space it crosses.
|
|
52
|
-
*/
|
|
53
|
-
export function painted(ctx: RenderContext, own: () => void, over: () => void = () => { }): void {
|
|
54
|
-
ctx.inFront(own);
|
|
55
|
-
ctx.children();
|
|
56
|
-
ctx.inFront(over);
|
|
57
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Paint,
|
|
3
|
+
Types,
|
|
4
|
+
type EntryOptions,
|
|
5
|
+
type RenderContext,
|
|
6
|
+
type Type,
|
|
7
|
+
// Named so the emitted declaration reaches these through the sdk, not core.
|
|
8
|
+
type Fill as _Fill,
|
|
9
|
+
type FillResolved as _FillResolved,
|
|
10
|
+
type Shadow as _Shadow,
|
|
11
|
+
type ShadowResolved as _ShadowResolved,
|
|
12
|
+
type Stroke as _Stroke,
|
|
13
|
+
type StrokeResolved as _StrokeResolved,
|
|
14
|
+
} from "@motionscript/sdk";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* What a component in this kit shares beyond the factory: the paints a math
|
|
18
|
+
* node draws with, listed choices, and the order a node paints in.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const { overlay: _overlay, ...paint } = Paint.state;
|
|
22
|
+
const { overlayTo: _overlayTo, ...paintCommands } = Paint.commands;
|
|
23
|
+
|
|
24
|
+
/** A math node's paints: fill, stroke, shadow and the outline trim, with no overlay. */
|
|
25
|
+
export const PAINT = paint;
|
|
26
|
+
export const PAINT_COMMANDS = paintCommands;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* One of `values`, held as text with its choices listed (no `kind: "enum"`).
|
|
30
|
+
*/
|
|
31
|
+
export function listed<const V extends readonly string[]>(values: V, initial: V[number], options?: ListedOptions): Type<V[number]>;
|
|
32
|
+
/** With no default: an arg left out is one the command was not given. */
|
|
33
|
+
export function listed<const V extends readonly string[]>(values: V, initial: undefined, options?: ListedOptions): Type<V[number] | undefined>;
|
|
34
|
+
export function listed(values: readonly string[], initial: string | undefined, options: ListedOptions = {}): Type<string | undefined> {
|
|
35
|
+
return Types.custom<string | undefined, string | undefined, EntryOptions & { readonly options: readonly string[] }>(initial, { ...options, options: values });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ListedOptions {
|
|
39
|
+
readonly animatable?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `record` without `keys`: the paints a node has no use for. */
|
|
43
|
+
export function omit<T extends object, const K extends keyof T>(record: T, ...keys: K[]): Omit<T, K> {
|
|
44
|
+
const out = { ...record };
|
|
45
|
+
for (const key of keys) delete out[key];
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A node's paint order: its own paint under its children, then what it lays
|
|
51
|
+
* over them, each cut at the near plane of a space it crosses.
|
|
52
|
+
*/
|
|
53
|
+
export function painted(ctx: RenderContext, own: () => void, over: () => void = () => { }): void {
|
|
54
|
+
ctx.inFront(own);
|
|
55
|
+
ctx.children();
|
|
56
|
+
ctx.inFront(over);
|
|
57
|
+
}
|
package/src/kit/contour.ts
CHANGED
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
import type { Vector2 } from "@motionscript/sdk";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The lines along which a function of two variables is zero, over a box of
|
|
5
|
-
* graph values: marching squares, its crossings placed by linear interpolation
|
|
6
|
-
* and joined end to end into runs. Points are graph coordinates.
|
|
7
|
-
*/
|
|
8
|
-
export function traceContour(
|
|
9
|
-
f: (x: number, y: number) => number,
|
|
10
|
-
[x0, x1]: readonly [number, number],
|
|
11
|
-
[y0, y1]: readonly [number, number],
|
|
12
|
-
columns: number,
|
|
13
|
-
rows: number,
|
|
14
|
-
): Vector2[][] {
|
|
15
|
-
if (!(x1 > x0) || !(y1 > y0) || columns < 1 || rows < 1) return [];
|
|
16
|
-
const dx = (x1 - x0) / columns;
|
|
17
|
-
const dy = (y1 - y0) / rows;
|
|
18
|
-
const stride = columns + 1;
|
|
19
|
-
const values = new Float64Array(stride * (rows + 1));
|
|
20
|
-
for (let j = 0; j <= rows; j++) {
|
|
21
|
-
for (let i = 0; i <= columns; i++) values[j * stride + i] = f(x0 + i * dx, y0 + j * dy);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// A crossing lives on a grid edge, which is its name: two cells sharing the edge share the point.
|
|
25
|
-
const points = new Map<number, Vector2>();
|
|
26
|
-
const edgeCount = stride * (rows + 1);
|
|
27
|
-
const crossing = (i: number, j: number, vertical: boolean): number | null => {
|
|
28
|
-
const a = values[j * stride + i];
|
|
29
|
-
const b = vertical ? values[(j + 1) * stride + i] : values[j * stride + i + 1];
|
|
30
|
-
if (!Number.isFinite(a) || !Number.isFinite(b) || a < 0 === b < 0) return null;
|
|
31
|
-
const key = (vertical ? edgeCount : 0) + j * stride + i;
|
|
32
|
-
if (!points.has(key)) {
|
|
33
|
-
const t = a / (a - b);
|
|
34
|
-
points.set(key, vertical ? { x: x0 + i * dx, y: y0 + (j + t) * dy } : { x: x0 + (i + t) * dx, y: y0 + j * dy });
|
|
35
|
-
}
|
|
36
|
-
return key;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const next = new Map<number, number[]>();
|
|
40
|
-
const join = (a: number | null, b: number | null): void => {
|
|
41
|
-
if (a === null || b === null) return;
|
|
42
|
-
(next.get(a) ?? next.set(a, []).get(a)!).push(b);
|
|
43
|
-
(next.get(b) ?? next.set(b, []).get(b)!).push(a);
|
|
44
|
-
};
|
|
45
|
-
for (let j = 0; j < rows; j++) {
|
|
46
|
-
for (let i = 0; i < columns; i++) {
|
|
47
|
-
const bottom = crossing(i, j, false);
|
|
48
|
-
const top = crossing(i, j + 1, false);
|
|
49
|
-
const left = crossing(i, j, true);
|
|
50
|
-
const right = crossing(i + 1, j, true);
|
|
51
|
-
const found = [bottom, right, top, left].filter((edge) => edge !== null);
|
|
52
|
-
if (found.length === 2) join(found[0], found[1]);
|
|
53
|
-
else if (found.length === 4) {
|
|
54
|
-
// A saddle: the centre's sign says which pairs of edges the curve joins.
|
|
55
|
-
const centre = f(x0 + (i + 0.5) * dx, y0 + (j + 0.5) * dy);
|
|
56
|
-
const corner = values[j * stride + i];
|
|
57
|
-
if (centre < 0 === corner < 0) {
|
|
58
|
-
join(bottom, right);
|
|
59
|
-
join(top, left);
|
|
60
|
-
} else {
|
|
61
|
-
join(bottom, left);
|
|
62
|
-
join(top, right);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const runs: Vector2[][] = [];
|
|
69
|
-
const used = new Set<number>();
|
|
70
|
-
const walk = (from: number): Vector2[] => {
|
|
71
|
-
const run: Vector2[] = [points.get(from)!];
|
|
72
|
-
used.add(from);
|
|
73
|
-
let at = from;
|
|
74
|
-
for (;;) {
|
|
75
|
-
const onward = (next.get(at) ?? []).find((key) => !used.has(key));
|
|
76
|
-
if (onward === undefined) break;
|
|
77
|
-
used.add(onward);
|
|
78
|
-
run.push(points.get(onward)!);
|
|
79
|
-
at = onward;
|
|
80
|
-
}
|
|
81
|
-
return run;
|
|
82
|
-
};
|
|
83
|
-
// Open runs first, from their ends, so each is one run and not two halves.
|
|
84
|
-
for (const [key, links] of next) {
|
|
85
|
-
if (links.length === 1 && !used.has(key)) runs.push(walk(key));
|
|
86
|
-
}
|
|
87
|
-
for (const key of next.keys()) {
|
|
88
|
-
if (used.has(key)) continue;
|
|
89
|
-
const run = walk(key);
|
|
90
|
-
run.push(run[0]);
|
|
91
|
-
runs.push(run);
|
|
92
|
-
}
|
|
93
|
-
return runs.filter((run) => run.length > 1);
|
|
94
|
-
}
|
|
1
|
+
import type { Vector2 } from "@motionscript/sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The lines along which a function of two variables is zero, over a box of
|
|
5
|
+
* graph values: marching squares, its crossings placed by linear interpolation
|
|
6
|
+
* and joined end to end into runs. Points are graph coordinates.
|
|
7
|
+
*/
|
|
8
|
+
export function traceContour(
|
|
9
|
+
f: (x: number, y: number) => number,
|
|
10
|
+
[x0, x1]: readonly [number, number],
|
|
11
|
+
[y0, y1]: readonly [number, number],
|
|
12
|
+
columns: number,
|
|
13
|
+
rows: number,
|
|
14
|
+
): Vector2[][] {
|
|
15
|
+
if (!(x1 > x0) || !(y1 > y0) || columns < 1 || rows < 1) return [];
|
|
16
|
+
const dx = (x1 - x0) / columns;
|
|
17
|
+
const dy = (y1 - y0) / rows;
|
|
18
|
+
const stride = columns + 1;
|
|
19
|
+
const values = new Float64Array(stride * (rows + 1));
|
|
20
|
+
for (let j = 0; j <= rows; j++) {
|
|
21
|
+
for (let i = 0; i <= columns; i++) values[j * stride + i] = f(x0 + i * dx, y0 + j * dy);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// A crossing lives on a grid edge, which is its name: two cells sharing the edge share the point.
|
|
25
|
+
const points = new Map<number, Vector2>();
|
|
26
|
+
const edgeCount = stride * (rows + 1);
|
|
27
|
+
const crossing = (i: number, j: number, vertical: boolean): number | null => {
|
|
28
|
+
const a = values[j * stride + i];
|
|
29
|
+
const b = vertical ? values[(j + 1) * stride + i] : values[j * stride + i + 1];
|
|
30
|
+
if (!Number.isFinite(a) || !Number.isFinite(b) || a < 0 === b < 0) return null;
|
|
31
|
+
const key = (vertical ? edgeCount : 0) + j * stride + i;
|
|
32
|
+
if (!points.has(key)) {
|
|
33
|
+
const t = a / (a - b);
|
|
34
|
+
points.set(key, vertical ? { x: x0 + i * dx, y: y0 + (j + t) * dy } : { x: x0 + (i + t) * dx, y: y0 + j * dy });
|
|
35
|
+
}
|
|
36
|
+
return key;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const next = new Map<number, number[]>();
|
|
40
|
+
const join = (a: number | null, b: number | null): void => {
|
|
41
|
+
if (a === null || b === null) return;
|
|
42
|
+
(next.get(a) ?? next.set(a, []).get(a)!).push(b);
|
|
43
|
+
(next.get(b) ?? next.set(b, []).get(b)!).push(a);
|
|
44
|
+
};
|
|
45
|
+
for (let j = 0; j < rows; j++) {
|
|
46
|
+
for (let i = 0; i < columns; i++) {
|
|
47
|
+
const bottom = crossing(i, j, false);
|
|
48
|
+
const top = crossing(i, j + 1, false);
|
|
49
|
+
const left = crossing(i, j, true);
|
|
50
|
+
const right = crossing(i + 1, j, true);
|
|
51
|
+
const found = [bottom, right, top, left].filter((edge) => edge !== null);
|
|
52
|
+
if (found.length === 2) join(found[0], found[1]);
|
|
53
|
+
else if (found.length === 4) {
|
|
54
|
+
// A saddle: the centre's sign says which pairs of edges the curve joins.
|
|
55
|
+
const centre = f(x0 + (i + 0.5) * dx, y0 + (j + 0.5) * dy);
|
|
56
|
+
const corner = values[j * stride + i];
|
|
57
|
+
if (centre < 0 === corner < 0) {
|
|
58
|
+
join(bottom, right);
|
|
59
|
+
join(top, left);
|
|
60
|
+
} else {
|
|
61
|
+
join(bottom, left);
|
|
62
|
+
join(top, right);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const runs: Vector2[][] = [];
|
|
69
|
+
const used = new Set<number>();
|
|
70
|
+
const walk = (from: number): Vector2[] => {
|
|
71
|
+
const run: Vector2[] = [points.get(from)!];
|
|
72
|
+
used.add(from);
|
|
73
|
+
let at = from;
|
|
74
|
+
for (;;) {
|
|
75
|
+
const onward = (next.get(at) ?? []).find((key) => !used.has(key));
|
|
76
|
+
if (onward === undefined) break;
|
|
77
|
+
used.add(onward);
|
|
78
|
+
run.push(points.get(onward)!);
|
|
79
|
+
at = onward;
|
|
80
|
+
}
|
|
81
|
+
return run;
|
|
82
|
+
};
|
|
83
|
+
// Open runs first, from their ends, so each is one run and not two halves.
|
|
84
|
+
for (const [key, links] of next) {
|
|
85
|
+
if (links.length === 1 && !used.has(key)) runs.push(walk(key));
|
|
86
|
+
}
|
|
87
|
+
for (const key of next.keys()) {
|
|
88
|
+
if (used.has(key)) continue;
|
|
89
|
+
const run = walk(key);
|
|
90
|
+
run.push(run[0]);
|
|
91
|
+
runs.push(run);
|
|
92
|
+
}
|
|
93
|
+
return runs.filter((run) => run.length > 1);
|
|
94
|
+
}
|