@motionscript/math 0.1.0-alpha.5 → 0.1.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +77 -27
  4. package/dist/annotate/angle-mark.d.ts.map +1 -1
  5. package/dist/annotate/angle-mark.js +80 -115
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +94 -25
  8. package/dist/annotate/curly-brace.d.ts.map +1 -1
  9. package/dist/annotate/curly-brace.js +85 -100
  10. package/dist/annotate/curly-brace.js.map +1 -1
  11. package/dist/annotate/emphasis-mark.d.ts +69 -9
  12. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  13. package/dist/annotate/emphasis-mark.js +33 -34
  14. package/dist/annotate/emphasis-mark.js.map +1 -1
  15. package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
  16. package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
  17. package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
  18. package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
  19. package/dist/browser/index.js +1 -1
  20. package/dist/browser/index.js.map +3 -3
  21. package/dist/browser/kit.js +1 -1
  22. package/dist/browser/manifest.json +1 -1
  23. package/dist/browser/tex.js +1 -1
  24. package/dist/browser/tex.js.map +2 -2
  25. package/dist/engine.d.ts +26 -19
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engine.js +17 -14
  28. package/dist/engine.js.map +1 -1
  29. package/dist/kit/component.d.ts +51 -0
  30. package/dist/kit/component.d.ts.map +1 -0
  31. package/dist/kit/component.js +30 -0
  32. package/dist/kit/component.js.map +1 -0
  33. package/dist/kit/curve.d.ts +22 -11
  34. package/dist/kit/curve.d.ts.map +1 -1
  35. package/dist/kit/curve.js +25 -18
  36. package/dist/kit/curve.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts +56 -35
  38. package/dist/kit/field-mark.d.ts.map +1 -1
  39. package/dist/kit/field-mark.js +100 -120
  40. package/dist/kit/field-mark.js.map +1 -1
  41. package/dist/kit/follow.d.ts +67 -29
  42. package/dist/kit/follow.d.ts.map +1 -1
  43. package/dist/kit/follow.js +114 -110
  44. package/dist/kit/follow.js.map +1 -1
  45. package/dist/kit/furniture.d.ts +3 -1
  46. package/dist/kit/furniture.d.ts.map +1 -1
  47. package/dist/kit/furniture.js +5 -0
  48. package/dist/kit/furniture.js.map +1 -1
  49. package/dist/kit/graph.d.ts +46 -0
  50. package/dist/kit/graph.d.ts.map +1 -0
  51. package/dist/kit/graph.js +90 -0
  52. package/dist/kit/graph.js.map +1 -0
  53. package/dist/kit/mark.d.ts +64 -39
  54. package/dist/kit/mark.d.ts.map +1 -1
  55. package/dist/kit/mark.js +80 -96
  56. package/dist/kit/mark.js.map +1 -1
  57. package/dist/kit/place.d.ts +10 -8
  58. package/dist/kit/place.d.ts.map +1 -1
  59. package/dist/kit/place.js +28 -30
  60. package/dist/kit/place.js.map +1 -1
  61. package/dist/kit/system.d.ts +27 -29
  62. package/dist/kit/system.d.ts.map +1 -1
  63. package/dist/kit/system.js +62 -58
  64. package/dist/kit/system.js.map +1 -1
  65. package/dist/kit/withdrawn.d.ts +3 -3
  66. package/dist/kit/withdrawn.js +3 -3
  67. package/dist/kit/withdrawn.js.map +1 -1
  68. package/dist/marks/curve-area.d.ts +37 -18
  69. package/dist/marks/curve-area.d.ts.map +1 -1
  70. package/dist/marks/curve-area.js +50 -73
  71. package/dist/marks/curve-area.js.map +1 -1
  72. package/dist/marks/drop-lines.d.ts +45 -10
  73. package/dist/marks/drop-lines.d.ts.map +1 -1
  74. package/dist/marks/drop-lines.js +37 -52
  75. package/dist/marks/drop-lines.js.map +1 -1
  76. package/dist/marks/plot-point.d.ts +56 -20
  77. package/dist/marks/plot-point.d.ts.map +1 -1
  78. package/dist/marks/plot-point.js +65 -91
  79. package/dist/marks/plot-point.js.map +1 -1
  80. package/dist/marks/riemann-sum.d.ts +47 -26
  81. package/dist/marks/riemann-sum.d.ts.map +1 -1
  82. package/dist/marks/riemann-sum.js +57 -85
  83. package/dist/marks/riemann-sum.js.map +1 -1
  84. package/dist/marks/tangent-line.d.ts +58 -14
  85. package/dist/marks/tangent-line.d.ts.map +1 -1
  86. package/dist/marks/tangent-line.js +73 -87
  87. package/dist/marks/tangent-line.js.map +1 -1
  88. package/dist/marks/vector-arrow.d.ts +48 -20
  89. package/dist/marks/vector-arrow.d.ts.map +1 -1
  90. package/dist/marks/vector-arrow.js +58 -78
  91. package/dist/marks/vector-arrow.js.map +1 -1
  92. package/dist/nodes.d.ts +13 -5
  93. package/dist/nodes.d.ts.map +1 -1
  94. package/dist/nodes.js +0 -6
  95. package/dist/nodes.js.map +1 -1
  96. package/dist/plots/function-plot.d.ts +135 -43
  97. package/dist/plots/function-plot.d.ts.map +1 -1
  98. package/dist/plots/function-plot.js +146 -177
  99. package/dist/plots/function-plot.js.map +1 -1
  100. package/dist/plots/points-plot.d.ts +55 -19
  101. package/dist/plots/points-plot.d.ts.map +1 -1
  102. package/dist/plots/points-plot.js +31 -58
  103. package/dist/plots/points-plot.js.map +1 -1
  104. package/dist/plots/stream-lines.d.ts +60 -19
  105. package/dist/plots/stream-lines.d.ts.map +1 -1
  106. package/dist/plots/stream-lines.js +60 -79
  107. package/dist/plots/stream-lines.js.map +1 -1
  108. package/dist/plots/traced-path.d.ts +54 -15
  109. package/dist/plots/traced-path.d.ts.map +1 -1
  110. package/dist/plots/traced-path.js +33 -48
  111. package/dist/plots/traced-path.js.map +1 -1
  112. package/dist/plots/vector-field.d.ts +48 -11
  113. package/dist/plots/vector-field.d.ts.map +1 -1
  114. package/dist/plots/vector-field.js +50 -64
  115. package/dist/plots/vector-field.js.map +1 -1
  116. package/dist/structures/network-graph.d.ts +140 -49
  117. package/dist/structures/network-graph.d.ts.map +1 -1
  118. package/dist/structures/network-graph.js +168 -217
  119. package/dist/structures/network-graph.js.map +1 -1
  120. package/dist/structures/sample-space.d.ts +90 -38
  121. package/dist/structures/sample-space.d.ts.map +1 -1
  122. package/dist/structures/sample-space.js +132 -170
  123. package/dist/structures/sample-space.js.map +1 -1
  124. package/dist/systems/axes2d.d.ts +159 -37
  125. package/dist/systems/axes2d.d.ts.map +1 -1
  126. package/dist/systems/axes2d.js +147 -183
  127. package/dist/systems/axes2d.js.map +1 -1
  128. package/dist/systems/number-line.d.ts +80 -28
  129. package/dist/systems/number-line.d.ts.map +1 -1
  130. package/dist/systems/number-line.js +82 -118
  131. package/dist/systems/number-line.js.map +1 -1
  132. package/dist/systems/number-plane.d.ts +140 -31
  133. package/dist/systems/number-plane.d.ts.map +1 -1
  134. package/dist/systems/number-plane.js +142 -149
  135. package/dist/systems/number-plane.js.map +1 -1
  136. package/dist/systems/polar-plane.d.ts +76 -26
  137. package/dist/systems/polar-plane.d.ts.map +1 -1
  138. package/dist/systems/polar-plane.js +107 -133
  139. package/dist/systems/polar-plane.js.map +1 -1
  140. package/dist/tex/index.d.ts.map +1 -1
  141. package/dist/tex/index.js +1 -0
  142. package/dist/tex/index.js.map +1 -1
  143. package/dist/tex/matrix.d.ts +88 -18
  144. package/dist/tex/matrix.d.ts.map +1 -1
  145. package/dist/tex/matrix.js +33 -50
  146. package/dist/tex/matrix.js.map +1 -1
  147. package/dist/tex/node.d.ts +84 -201
  148. package/dist/tex/node.d.ts.map +1 -1
  149. package/dist/tex/node.js +23 -593
  150. package/dist/tex/node.js.map +1 -1
  151. package/dist/tex/typeset.d.ts +133 -0
  152. package/dist/tex/typeset.d.ts.map +1 -0
  153. package/dist/tex/typeset.js +403 -0
  154. package/dist/tex/typeset.js.map +1 -0
  155. package/dist/three/axes3d.d.ts +214 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +163 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +108 -42
  160. package/dist/three/curve-plot3d.d.ts.map +1 -1
  161. package/dist/three/curve-plot3d.js +97 -132
  162. package/dist/three/curve-plot3d.js.map +1 -1
  163. package/dist/three/entries.d.ts +17 -0
  164. package/dist/three/entries.d.ts.map +1 -0
  165. package/dist/three/entries.js +15 -0
  166. package/dist/three/entries.js.map +1 -0
  167. package/dist/three/plot-point3d.d.ts +85 -12
  168. package/dist/three/plot-point3d.d.ts.map +1 -1
  169. package/dist/three/plot-point3d.js +27 -53
  170. package/dist/three/plot-point3d.js.map +1 -1
  171. package/dist/three/space.d.ts +17 -2
  172. package/dist/three/space.d.ts.map +1 -1
  173. package/dist/three/space.js +19 -8
  174. package/dist/three/space.js.map +1 -1
  175. package/dist/three/surface-plot3d.d.ts +176 -49
  176. package/dist/three/surface-plot3d.d.ts.map +1 -1
  177. package/dist/three/surface-plot3d.js +227 -261
  178. package/dist/three/surface-plot3d.js.map +1 -1
  179. package/dist/three/vector-arrow3d.d.ts +87 -16
  180. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  181. package/dist/three/vector-arrow3d.js +48 -67
  182. package/dist/three/vector-arrow3d.js.map +1 -1
  183. package/package.json +4 -4
  184. package/src/annotate/angle-mark.ts +95 -105
  185. package/src/annotate/curly-brace.ts +107 -85
  186. package/src/annotate/emphasis-mark.ts +55 -26
  187. package/src/engine.ts +35 -21
  188. package/src/kit/component.ts +57 -0
  189. package/src/kit/curve.ts +38 -20
  190. package/src/kit/field-mark.ts +124 -108
  191. package/src/kit/follow.ts +146 -120
  192. package/src/kit/furniture.ts +6 -1
  193. package/src/kit/graph.ts +116 -0
  194. package/src/kit/mark.ts +135 -114
  195. package/src/kit/place.ts +32 -56
  196. package/src/kit/system.ts +77 -71
  197. package/src/kit/withdrawn.ts +3 -3
  198. package/src/marks/curve-area.ts +65 -65
  199. package/src/marks/drop-lines.ts +50 -42
  200. package/src/marks/plot-point.ts +77 -82
  201. package/src/marks/riemann-sum.ts +73 -72
  202. package/src/marks/tangent-line.ts +87 -71
  203. package/src/marks/vector-arrow.ts +73 -73
  204. package/src/nodes.ts +41 -5
  205. package/src/plots/function-plot.ts +155 -171
  206. package/src/plots/points-plot.ts +46 -50
  207. package/src/plots/stream-lines.ts +75 -66
  208. package/src/plots/traced-path.ts +51 -33
  209. package/src/plots/vector-field.ts +68 -49
  210. package/src/structures/network-graph.ts +168 -185
  211. package/src/structures/sample-space.ts +149 -148
  212. package/src/systems/axes2d.ts +165 -156
  213. package/src/systems/number-line.ts +91 -92
  214. package/src/systems/number-plane.ts +149 -120
  215. package/src/systems/polar-plane.ts +117 -116
  216. package/src/tex/index.ts +2 -1
  217. package/src/tex/matrix.ts +62 -40
  218. package/src/tex/node.ts +54 -621
  219. package/src/tex/typeset.ts +461 -0
  220. package/src/three/axes3d.ts +187 -160
  221. package/src/three/curve-plot3d.ts +107 -110
  222. package/src/three/entries.ts +42 -0
  223. package/src/three/plot-point3d.ts +42 -45
  224. package/src/three/space.ts +34 -9
  225. package/src/three/surface-plot3d.ts +312 -323
  226. package/src/three/vector-arrow3d.ts +65 -54
  227. package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
  228. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
  229. package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
  230. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
  231. package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
  232. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
  233. package/src/kit/placement.ts +0 -8
@@ -1,29 +1,51 @@
1
1
  import {
2
- Canvas3D,
3
- colorProperty,
4
- command,
2
+ component2d,
3
+ Flow,
5
4
  Geo,
6
5
  Graphics3D,
7
6
  Mat,
8
- node,
9
- property,
10
- type AssetScope,
7
+ Paint,
8
+ provides,
9
+ Shapes,
10
+ Types,
11
+ View3D,
11
12
  type Canvas3DProps,
12
13
  type Color,
13
- type Command,
14
- type CommandArgs,
15
- type NodeConfig,
14
+ type NodeOf,
16
15
  type Scene3D,
17
16
  type Transform3D,
17
+ type Values,
18
18
  type Vector3,
19
19
  type Vector3Input,
20
+ // Named so the emitted declaration reaches these through the sdk, not core.
21
+ type Alignment as _Alignment,
22
+ type CornerRadiusResolved as _CornerRadiusResolved,
23
+ type CornerStyleResolved as _CornerStyleResolved,
24
+ type Fill as _Fill,
25
+ type FillResolved as _FillResolved,
26
+ type FlowMode as _FlowMode,
27
+ type FlowResolved as _FlowResolved,
28
+ type GapSize as _GapSize,
29
+ type Node2D as _Node2D,
30
+ type Node2DProps as _Node2DProps,
31
+ type NormalizedColor as _NormalizedColor,
32
+ type PostEffect3D as _PostEffect3D,
33
+ type RectCornerRadius as _RectCornerRadius,
34
+ type RectCornerStyle as _RectCornerStyle,
35
+ type Shadow as _Shadow,
36
+ type ShadowResolved as _ShadowResolved,
37
+ type Shadows3D as _Shadows3D,
38
+ type Stroke as _Stroke,
39
+ type StrokeResolved as _StrokeResolved,
40
+ type ToneMapping3D as _ToneMapping3D,
41
+ type Vector2 as _Vector2,
20
42
  } from "@motionscript/sdk";
21
43
  import type { Interval } from "../kit/coords";
22
44
  import { AXIS_COLOR, lerpInterval, readInterval } from "../kit/furniture";
23
- import { clamp, stated } from "../kit/geometry";
45
+ import { clamp } from "../kit/geometry";
24
46
  import { formatTick, ticksFor } from "../kit/ticks";
25
47
  import { drawLabel3D, type LabelStyle3D } from "./label3d";
26
- import { graphSpace, type GraphSpace } from "./space";
48
+ import { GRAPH_3D, graphSpace, type Graph3D, type GraphSpace } from "./space";
27
49
 
28
50
  export interface Axes3dProps extends Canvas3DProps {
29
51
  xRange?: Interval;
@@ -62,179 +84,184 @@ const ALONG: Record<AxisName, Vector3Input> = { x: [90, 0, 0], y: [0, 0, -90], z
62
84
  /** The graph axis each axis's tick marks lie along. */
63
85
  const TICK_ALONG: Record<AxisName, AxisName> = { x: "y", y: "x", z: "y" };
64
86
 
65
- /**
66
- * A 3D graph: three axes through a box of graph coordinates, z up, seen through
67
- * a camera of its own, so it stands on a 2D stage as it is, the way `axes2d`
68
- * does. Its 3D children sit in its graph coordinates: a surface, a space curve,
69
- * a point, an arrow, or any 3D node. `orbitTo` swings the camera round it.
70
- */
71
- @node({
72
- key: "axes3d",
73
- parentKey: "canvas3d",
74
- forkable: true,
75
- layout: { defaultWidthMode: "fixed", defaultHeightMode: "fixed", acceptsChildren: true },
76
- seed: { width: 800, height: 500 },
77
- })
78
- export class Axes3d<P extends Axes3dProps = Axes3dProps> extends Canvas3D<P> {
79
- @property({ default: [-3, 3], mapper: readInterval, tween: lerpInterval }) declare xRange: Interval;
80
- @property({ default: [-3, 3], mapper: readInterval, tween: lerpInterval }) declare yRange: Interval;
81
- @property({ default: [-2, 2], mapper: readInterval, tween: lerpInterval }) declare zRange: Interval;
87
+ function range(initial: Interval) {
88
+ return Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
89
+ }
82
90
 
91
+ /** The axes' own entries, beside a 3D canvas's. */
92
+ const own = {
93
+ xRange: range([-3, 3]),
94
+ yRange: range([-3, 3]),
95
+ zRange: range([-2, 2]),
83
96
  /** How long each axis is drawn, in scene units. */
84
- @property({ default: 6, min: 0.01, step: 0.1 }) declare xLength: number;
85
- @property({ default: 6, min: 0.01, step: 0.1 }) declare yLength: number;
86
- @property({ default: 4, min: 0.01, step: 0.1 }) declare zLength: number;
87
-
97
+ xLength: Types.number(6, { min: 0.01, step: 0.1 }),
98
+ yLength: Types.number(6, { min: 0.01, step: 0.1 }),
99
+ zLength: Types.number(4, { min: 0.01, step: 0.1 }),
88
100
  /** The spacing of each axis's ticks; zero picks a tidy one for the axis's length. */
89
- @property({ default: 0, min: 0 }) declare xStep: number;
90
- @property({ default: 0, min: 0 }) declare yStep: number;
91
- @property({ default: 0, min: 0 }) declare zStep: number;
92
-
93
- @colorProperty({ default: AXIS_COLOR }) declare color: Color;
101
+ xStep: Types.number(0, { min: 0 }),
102
+ yStep: Types.number(0, { min: 0 }),
103
+ zStep: Types.number(0, { min: 0 }),
104
+ color: Types.color(AXIS_COLOR),
94
105
  /** The radius of an axis line, in scene units. */
95
- @property({ default: 0.02, min: 0, step: 0.005 }) declare thickness: number;
96
- @property({ default: true, animatable: false }) declare tips: boolean;
97
- @property({ default: true, animatable: false }) declare ticks: boolean;
98
- @property({ default: 0.16, min: 0, step: 0.01 }) declare tickSize: number;
99
-
106
+ thickness: Types.number(0.02, { min: 0, step: 0.005 }),
107
+ tips: Types.boolean(true, { animatable: false }),
108
+ ticks: Types.boolean(true, { animatable: false }),
109
+ tickSize: Types.number(0.16, { min: 0, step: 0.01 }),
100
110
  /** Lines across the plane the x and y axes lie in, at each of their ticks. */
101
- @property({ default: false, animatable: false }) declare grid: boolean;
111
+ grid: Types.boolean(false, { animatable: false }),
102
112
  /** (`gridColor` is the viewport's own reference grid.) */
103
- @colorProperty({ default: "#374151" }) declare gridLineColor: Color;
104
-
113
+ gridLineColor: Types.color("#374151"),
105
114
  /** A number at each tick. */
106
- @property({ default: false, animatable: false }) declare labels: boolean;
115
+ labels: Types.boolean(false, { animatable: false }),
107
116
  /** The height of a label's text, in scene units. */
108
- @property({ default: 0.26, min: 0.01, step: 0.01 }) declare labelSize: number;
109
- @colorProperty({ default: AXIS_COLOR }) declare labelColor: Color;
110
- @property({ default: "", animatable: false }) declare fontFamily: string;
117
+ labelSize: Types.number(0.26, { min: 0.01, step: 0.01 }),
118
+ labelColor: Types.color(AXIS_COLOR),
119
+ fontFamily: Types.text("", { animatable: false }),
111
120
  /** A title at the positive end of each axis. */
112
- @property({ default: "x", animatable: false }) declare xLabel: string;
113
- @property({ default: "y", animatable: false }) declare yLabel: string;
114
- @property({ default: "z", animatable: false }) declare zLabel: string;
121
+ xLabel: Types.text("x", { animatable: false }),
122
+ yLabel: Types.text("y", { animatable: false }),
123
+ zLabel: Types.text("z", { animatable: false }),
124
+ };
115
125
 
116
- constructor(props?: NodeConfig<Axes3d<P>, P>) {
117
- super((props ?? {}) as NodeConfig<Canvas3D<P>, P>);
118
- }
126
+ type AxesValues = Values<typeof own>;
119
127
 
120
- /** Animate the window of values the axes show; everything drawn in them follows. */
121
- @command({ args: ["xRange", "yRange", "zRange"] })
122
- rangeTo(args: CommandArgs<{ xRange?: Interval; yRange?: Interval; zRange?: Interval }> & { duration: number }): Command<P> {
123
- return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
124
- }
128
+ /**
129
+ * A 3D graph: three axes through a box of graph coordinates, z up, seen through
130
+ * a camera of its own, so it stands on a 2D stage as it is, the way `axes2d`
131
+ * does. Its 3D children sit in its graph coordinates: a surface, a space curve,
132
+ * a point, an arrow, or any 3D node. `orbitTo` swings the camera round it.
133
+ */
134
+ export const Axes3d = component2d({
135
+ key: "axes3d",
136
+ state: { ...Paint.state, ...Flow.state, ...Shapes.corners, ...View3D.state, ...own },
137
+ defaults: { width: "fill", height: "fill" },
138
+ commands: {
139
+ ...Paint.commands,
140
+ ...View3D.commands,
141
+ /** Animate the window of values the axes show; everything drawn in them follows. */
142
+ rangeTo: { args: ["xRange", "yRange", "zRange"] },
143
+ },
144
+ allowedChildren: ["2d", "3d"],
145
+ load: (loader, s) => {
146
+ View3D.load(loader, furnish(s));
147
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
148
+ },
149
+ provide: (s) => [provides(GRAPH_3D, graphOfAxes(s))],
150
+ space: (s) => axesSpace(s),
151
+ measure: (measurer, s, children) => Flow.measure(measurer, s, children, Paint.inset(s)),
152
+ layout: (box, s, children) => Flow.layout(box, s, children, Paint.inset(s)),
153
+ render: (ctx, s) => View3D.draw(ctx, s, furnish(s)),
154
+ });
125
155
 
126
- override declareAssets(assets: AssetScope): void {
127
- super.declareAssets(assets);
128
- if (this.fontFamily !== "") assets.font(this.fontFamily);
129
- }
156
+ export type Axes3d = NodeOf<typeof Axes3d>;
130
157
 
131
- /** @internal */
132
- override _spaceOf(): GraphSpace {
133
- return this._graphSpace();
134
- }
158
+ /** The axes' own drawing, added to the scene: what `render` draws and `load` declares. */
159
+ function furnish(s: AxesValues): (scene: Scene3D) => void {
160
+ return (scene) => scene.draw(furniture(s));
161
+ }
162
+
163
+ /** The last space worked out for each axes, kept while its ranges and lengths are unchanged. */
164
+ const spaces = new WeakMap<object, { readonly key: string; readonly space: GraphSpace }>();
165
+
166
+ /** The coordinates of a 3D graph: what its children's `x`, `y` and `z` are in. */
167
+ function axesSpace(s: AxesValues): GraphSpace {
168
+ const { xRange, yRange, zRange, xLength, yLength, zLength } = s;
169
+ const key = [xRange, yRange, zRange, xLength, yLength, zLength].join("|");
170
+ let kept = spaces.get(s);
171
+ if (kept?.key !== key) spaces.set(s, (kept = { key, space: graphSpace(xRange, yRange, zRange, { x: xLength, y: yLength, z: zLength }) }));
172
+ return kept.space;
173
+ }
174
+
175
+ // One per axes, so the context its subtree holds is the same from frame to frame.
176
+ const graphs = new WeakMap<object, Graph3D>();
135
177
 
136
- /** @internal */
137
- _graphSpace(): GraphSpace {
138
- const { xRange, yRange, zRange, xLength, yLength, zLength } = this;
139
- const key = [xRange, yRange, zRange, xLength, yLength, zLength].join("|");
140
- if (key !== this._spaceKey) {
141
- this._space = graphSpace(xRange, yRange, zRange, { x: xLength, y: yLength, z: zLength });
142
- this._spaceKey = key;
178
+ function graphOfAxes(s: AxesValues): Graph3D {
179
+ let graph = graphs.get(s);
180
+ if (!graph) graphs.set(s, (graph = { space: () => axesSpace(s) }));
181
+ return graph;
182
+ }
183
+
184
+ /** The axes, their ticks, numbers and titles, and the grid, in the scene's units. */
185
+ function furniture(s: AxesValues): Graphics3D {
186
+ const space = axesSpace(s);
187
+ const g3 = new Graphics3D();
188
+ const ranges: Record<AxisName, Interval> = { x: space.xRange, y: space.yRange, z: space.zRange };
189
+ const lengths: Record<AxisName, number> = { x: s.xLength, y: s.yLength, z: s.zLength };
190
+ const steps: Record<AxisName, number> = { x: s.xStep, y: s.yStep, z: s.zStep };
191
+ const titles: Record<AxisName, string> = { x: s.xLabel, y: s.yLabel, z: s.zLabel };
192
+ // Where each axis is crossed by the other two: their zero, or the nearest end of a range without one.
193
+ const cross: Record<AxisName, number> = {
194
+ x: clamp(0, ranges.x[0], ranges.x[1]),
195
+ y: clamp(0, ranges.y[0], ranges.y[1]),
196
+ z: clamp(0, ranges.z[0], ranges.z[1]),
197
+ };
198
+ const at = (axis: AxisName, value: number, shift: Partial<Record<AxisName, number>> = {}): Vector3 => {
199
+ const point = { ...cross, [axis]: value };
200
+ const local = space.toLocal(point.x, point.y, point.z);
201
+ const moved = space.offset(0, 0, 0);
202
+ for (const key of ["x", "y", "z"] as const) {
203
+ const by = shift[key];
204
+ if (by === undefined) continue;
205
+ // A shift is in scene units, along the graph axis it names.
206
+ if (key === "x") moved.z += by;
207
+ else if (key === "y") moved.x += by;
208
+ else moved.y += by;
143
209
  }
144
- return this._space!;
145
- }
210
+ return { x: local.x + moved.x, y: local.y + moved.y, z: local.z + moved.z };
211
+ };
146
212
 
147
- protected override buildScene3D(): Scene3D {
148
- const scene = super.buildScene3D();
149
- scene.draw(this._furniture());
150
- return scene;
151
- }
213
+ const { thickness, tickSize } = s;
214
+ const paint = Mat.basic({ color: s.color });
215
+ const style: LabelStyle3D = { size: s.labelSize, color: s.labelColor, fontFamily: s.fontFamily };
216
+ const tipLength = s.tips ? thickness * 9 : 0;
217
+ const marks: Transform3D[] = [];
218
+ const plane: Vector3[] = [];
152
219
 
153
- private _space: GraphSpace | null = null;
154
- private _spaceKey = "";
155
-
156
- /** The axes, their ticks, numbers and titles, and the grid, in the scene's units. */
157
- private _furniture(): Graphics3D {
158
- const space = this._graphSpace();
159
- const g3 = new Graphics3D();
160
- const ranges: Record<AxisName, Interval> = { x: space.xRange, y: space.yRange, z: space.zRange };
161
- const lengths: Record<AxisName, number> = { x: this.xLength, y: this.yLength, z: this.zLength };
162
- const steps: Record<AxisName, number> = { x: this.xStep, y: this.yStep, z: this.zStep };
163
- const titles: Record<AxisName, string> = { x: this.xLabel, y: this.yLabel, z: this.zLabel };
164
- // Where each axis is crossed by the other two: their zero, or the nearest end of a range without one.
165
- const cross: Record<AxisName, number> = {
166
- x: clamp(0, ranges.x[0], ranges.x[1]),
167
- y: clamp(0, ranges.y[0], ranges.y[1]),
168
- z: clamp(0, ranges.z[0], ranges.z[1]),
169
- };
170
- const at = (axis: AxisName, value: number, shift: Partial<Record<AxisName, number>> = {}): Vector3 => {
171
- const point = { ...cross, [axis]: value };
172
- const local = space.toLocal(point.x, point.y, point.z);
173
- const moved = space.offset(0, 0, 0);
174
- for (const key of ["x", "y", "z"] as const) {
175
- const by = shift[key];
176
- if (by === undefined) continue;
177
- // A shift is in scene units, along the graph axis it names.
178
- if (key === "x") moved.z += by;
179
- else if (key === "y") moved.x += by;
180
- else moved.y += by;
181
- }
182
- return { x: local.x + moved.x, y: local.y + moved.y, z: local.z + moved.z };
183
- };
184
-
185
- const { thickness, tickSize } = this;
186
- const paint = Mat.basic({ color: this.color });
187
- const style: LabelStyle3D = { size: this.labelSize, color: this.labelColor, fontFamily: this.fontFamily };
188
- const tipLength = this.tips ? thickness * 9 : 0;
189
- const marks: Transform3D[] = [];
190
- const plane: Vector3[] = [];
191
-
192
- for (const axis of ["x", "y", "z"] as const) {
193
- const [min, max] = ranges[axis];
194
- const length = lengths[axis];
195
- if (!(max > min) || !(length > 0)) continue;
196
-
197
- if (thickness > 0) {
198
- const from = at(axis, min);
199
- const to = at(axis, max);
200
- g3.mesh(Geo.cylinder({ radius: thickness, height: length, segments: [12, 1] }), paint, {
201
- position: { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2, z: (from.z + to.z) / 2 },
220
+ for (const axis of ["x", "y", "z"] as const) {
221
+ const [min, max] = ranges[axis];
222
+ const length = lengths[axis];
223
+ if (!(max > min) || !(length > 0)) continue;
224
+
225
+ if (thickness > 0) {
226
+ const from = at(axis, min);
227
+ const to = at(axis, max);
228
+ g3.mesh(Geo.cylinder({ radius: thickness, height: length, segments: [12, 1] }), paint, {
229
+ position: { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2, z: (from.z + to.z) / 2 },
230
+ rotation: ALONG[axis],
231
+ });
232
+ if (s.tips) {
233
+ g3.mesh(Geo.cone({ radius: thickness * 3.2, height: tipLength, segments: [16, 1] }), paint, {
234
+ position: at(axis, max, { [axis]: tipLength / 2 }),
202
235
  rotation: ALONG[axis],
203
236
  });
204
- if (this.tips) {
205
- g3.mesh(Geo.cone({ radius: thickness * 3.2, height: tipLength, segments: [16, 1] }), paint, {
206
- position: at(axis, max, { [axis]: tipLength / 2 }),
207
- rotation: ALONG[axis],
208
- });
209
- }
210
237
  }
238
+ }
211
239
 
212
- const ticks = ticksFor(ranges[axis], steps[axis], length * UNIT_AS_PIXELS);
213
- const side = TICK_ALONG[axis];
214
- for (const value of ticks.values) {
215
- // The crossing is marked by the other axes, and the far end by the tip.
216
- if (Math.abs(value - cross[axis]) < 1e-9 || (this.tips && Math.abs(value - max) < 1e-9)) continue;
217
- if (this.ticks && tickSize > 0) marks.push({ position: at(axis, value), rotation: ALONG[side] });
218
- if (this.labels) {
219
- drawLabel3D(g3, formatTick(value, ticks.step), at(axis, value, { [side]: -(tickSize / 2 + style.size * 0.8) }), style);
220
- }
221
- }
222
- drawLabel3D(g3, titles[axis], at(axis, max, { [axis]: tipLength + style.size * 0.9 }), style);
223
-
224
- if (this.grid && axis !== "z") {
225
- const other = axis === "x" ? "y" : "x";
226
- for (const value of ticks.values) {
227
- const a = { x: 0, y: 0, z: cross.z, [axis]: value, [other]: ranges[other][0] };
228
- const b = { ...a, [other]: ranges[other][1] };
229
- plane.push(space.toLocal(a.x, a.y, a.z), space.toLocal(b.x, b.y, b.z));
230
- }
240
+ const ticks = ticksFor(ranges[axis], steps[axis], length * UNIT_AS_PIXELS);
241
+ const side = TICK_ALONG[axis];
242
+ for (const value of ticks.values) {
243
+ // The crossing is marked by the other axes, and the far end by the tip.
244
+ if (Math.abs(value - cross[axis]) < 1e-9 || (s.tips && Math.abs(value - max) < 1e-9)) continue;
245
+ if (s.ticks && tickSize > 0) marks.push({ position: at(axis, value), rotation: ALONG[side] });
246
+ if (s.labels) {
247
+ drawLabel3D(g3, formatTick(value, ticks.step), at(axis, value, { [side]: -(tickSize / 2 + style.size * 0.8) }), style);
231
248
  }
232
249
  }
250
+ drawLabel3D(g3, titles[axis], at(axis, max, { [axis]: tipLength + style.size * 0.9 }), style);
233
251
 
234
- if (marks.length > 0) {
235
- g3.instances(Geo.cylinder({ radius: Math.max(thickness * 0.7, 0.008), height: tickSize, segments: [8, 1] }), paint, marks);
252
+ if (s.grid && axis !== "z") {
253
+ const other = axis === "x" ? "y" : "x";
254
+ for (const value of ticks.values) {
255
+ const a = { x: 0, y: 0, z: cross.z, [axis]: value, [other]: ranges[other][0] };
256
+ const b = { ...a, [other]: ranges[other][1] };
257
+ plane.push(space.toLocal(a.x, a.y, a.z), space.toLocal(b.x, b.y, b.z));
258
+ }
236
259
  }
237
- if (plane.length > 0) g3.line({ points: plane, segments: true, stroke: { fill: this.gridLineColor } });
238
- return g3;
239
260
  }
261
+
262
+ if (marks.length > 0) {
263
+ g3.instances(Geo.cylinder({ radius: Math.max(thickness * 0.7, 0.008), height: tickSize, segments: [8, 1] }), paint, marks);
264
+ }
265
+ if (plane.length > 0) g3.line({ points: plane, segments: true, stroke: { fill: s.gridLineColor } });
266
+ return g3;
240
267
  }
@@ -1,23 +1,33 @@
1
1
  import {
2
- command,
2
+ component3d,
3
3
  Geo,
4
- Mesh3D,
5
- node,
6
- property,
7
- type Command,
8
- type CommandArgs,
4
+ Material,
5
+ Types,
9
6
  type Geometry3D,
10
7
  type Mesh3DProps,
11
- type NodeConfig,
8
+ type NodeOf,
9
+ type Values,
12
10
  type Vector3,
11
+ // Named so the emitted declaration reaches these through the sdk, not core.
12
+ type Blend3D as _Blend3D,
13
+ type Color as _Color,
14
+ type Faces3D as _Faces3D,
15
+ type Fill3D as _Fill3D,
16
+ type Material3D as _Material3D,
17
+ type Node3D as _Node3D,
18
+ type Node3DProps as _Node3DProps,
19
+ type NormalizedColor as _NormalizedColor,
20
+ type Shading3D as _Shading3D,
21
+ type Texture3D as _Texture3D,
13
22
  } from "@motionscript/sdk";
14
23
  import type { Interval } from "../kit/coords";
15
24
  import { parseSpaceCurve, spaceCurveParams, type SpaceCurveEquation } from "../kit/equation3d";
16
25
  import type { ExpressionScope } from "../kit/expression";
17
26
  import { PLOT_COLOR } from "../kit/furniture";
18
- import { clamp, stated } from "../kit/geometry";
19
- import { lerpDomain, lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
20
- import { spaceOf, type GraphSpace } from "./space";
27
+ import { clamp } from "../kit/geometry";
28
+ import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "../kit/source";
29
+ import { domainEntry, meshEntries } from "./entries";
30
+ import { graphEntry, graphOf, type GraphSpace } from "./space";
21
31
 
22
32
  export interface CurvePlot3dProps extends Mesh3DProps {
23
33
  expression?: string | PlotSource;
@@ -29,116 +39,103 @@ export interface CurvePlot3dProps extends Mesh3DProps {
29
39
  end?: number;
30
40
  }
31
41
 
32
- /** A curve through a 3D graph, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
33
- @node({
34
- key: "curvePlot3d",
35
- within: ["axes3d"],
36
- forkable: true,
37
- layout: { acceptsChildren: false },
38
- seed: { expression: "(2cos(t), 2sin(t), t / 4)", domain: [-6.28, 6.28], fill: PLOT_COLOR },
39
- })
40
- export class CurvePlot3d extends Mesh3D<CurvePlot3dProps> {
41
- @property({ default: "", mapper: readSource, tween: lerpSource, kind: "spaceCurveEquation" })
42
- declare expression: PlotSource;
43
-
42
+ const state = {
43
+ ...meshEntries,
44
+ expression: Types.custom<string | PlotSource, PlotSource>("", {
45
+ resolve: readSource,
46
+ lerp: lerpSource,
47
+ kind: "spaceCurveEquation",
48
+ }),
44
49
  /** The span of `t` that is drawn; 0 to 1 when left out. */
45
- @property({ default: null, tween: lerpDomain, kind: "range", defaultMin: 0, defaultMax: 1 }) declare domain: Interval | null;
46
-
47
- @property({ default: {}, tween: lerpParams }) declare params: ExpressionScope;
48
-
50
+ domain: domainEntry(0, 1),
51
+ params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
49
52
  /** How many points the curve is drawn through. */
50
- @property({ default: 160, min: 2, max: 2000, step: 1, animatable: false }) declare samples: number;
53
+ samples: Types.number(160, { min: 2, max: 2000, step: 1, animatable: false }),
51
54
  /** The radius of the line, in scene units. */
52
- @property({ default: 0.04, min: 0.001, step: 0.005 }) declare thickness: number;
53
-
55
+ thickness: Types.number(0.04, { min: 0.001, step: 0.005 }),
54
56
  /** The part of the curve that is drawn, as shares of its length in `t`. */
55
- @property({ default: 0, min: 0, max: 1, step: 0.01 }) declare start: number;
56
- @property({ default: 1, min: 0, max: 1, step: 0.01 }) declare end: number;
57
-
58
- constructor(props?: NodeConfig<CurvePlot3d, CurvePlot3dProps>) {
59
- super(props ?? {});
60
- if (props?.fill === undefined) this.applyProp("fill", PLOT_COLOR);
61
- }
62
-
63
- /** Change the equation, the curve bending from the one into the other. */
64
- @command({ args: ["expression"] })
65
- morphTo(args: CommandArgs<{ expression: string }> & { duration: number }): Command<CurvePlot3dProps> {
66
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
67
- }
68
-
69
- /** Animate the equation's named values. */
70
- @command({ args: ["params"] })
71
- paramsTo(args: CommandArgs<{ params: ExpressionScope }> & { duration: number }): Command<CurvePlot3dProps> {
72
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
73
- }
74
-
75
- /** Animate the span of `t` that is drawn. */
76
- @command({ args: ["domain"] })
77
- domainTo(args: CommandArgs<{ domain: Interval | null }> & { duration: number }): Command<CurvePlot3dProps> {
78
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
79
- }
57
+ start: Types.number(0, { min: 0, max: 1, step: 0.01 }),
58
+ end: Types.number(1, { min: 0, max: 1, step: 0.01 }),
59
+ graph: graphEntry(),
60
+ };
80
61
 
81
- /** Draw the curve on, or wipe it off: animates `start` and `end`. */
82
- @command({ args: ["start", "end"] })
83
- progressTo(args: CommandArgs<{ start?: number; end?: number }> & { duration: number }): Command<CurvePlot3dProps> {
84
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
85
- }
86
-
87
- protected override buildGeometry(): Geometry3D | undefined {
88
- const space = spaceOf(this);
89
- if (!space) return undefined;
90
- const points = this._points(space);
91
- if (points.length < 2) return undefined;
92
- return Geo.tube({ points, radius: this.thickness, segments: [points.length - 1, 8] });
93
- }
62
+ type CurveValues = Values<typeof state>;
94
63
 
95
- private _key = "";
96
- private _run: Vector3[] = [];
64
+ /** A curve through a 3D graph, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
65
+ export const CurvePlot3d = component3d({
66
+ key: "curvePlot3d",
67
+ state,
68
+ defaults: { fill: PLOT_COLOR },
69
+ commands: {
70
+ /** Change the equation, the curve bending from the one into the other. */
71
+ morphTo: { args: ["expression"] },
72
+ /** Animate the equation's named values. */
73
+ paramsTo: { args: ["params"] },
74
+ /** Animate the span of `t` that is drawn. */
75
+ domainTo: { args: ["domain"] },
76
+ /** Draw the curve on, or wipe it off: animates `start` and `end`. */
77
+ progressTo: { args: ["start", "end"] },
78
+ },
79
+ render: (ctx, s) => ctx.draw(Material.mesh(s, curveGeometry(s))),
80
+ });
81
+
82
+ export type CurvePlot3d = NodeOf<typeof CurvePlot3d>;
83
+
84
+ function curveGeometry(s: CurveValues): Geometry3D | undefined {
85
+ const space = graphOf(s);
86
+ if (!space) return undefined;
87
+ const points = pointsOf(s, space);
88
+ if (points.length < 2) return undefined;
89
+ return Geo.tube({ points, radius: s.thickness, segments: [points.length - 1, 8] });
90
+ }
97
91
 
98
- private _points(space: GraphSpace): Vector3[] {
99
- const { source, blend } = this.expression;
100
- const start = clamp(this.start, 0, 1);
101
- const end = clamp(this.end, 0, 1);
102
- const samples = Math.max(2, Math.round(this.samples));
103
- const key = [source, blend?.from, blend?.t, space.revision, this.domain, JSON.stringify(this.params), start, end, samples].join("|");
104
- if (key === this._key) return this._run;
105
- this._key = key;
106
- this._run = [];
107
- const to = this._curve(source);
108
- if (!to || !(end > start)) return this._run;
109
- const from = blend ? this._curve(blend.from) : null;
110
- const steps = Math.max(1, Math.round(samples * (end - start)));
111
- const a: Vector3 = { x: 0, y: 0, z: 0 };
112
- const b: Vector3 = { x: 0, y: 0, z: 0 };
113
- for (let i = 0; i <= steps; i++) {
114
- const u = start + ((end - start) * i) / steps;
115
- if (!to(u, b)) continue;
116
- if (from && blend) {
117
- if (!from(u, a)) continue;
118
- b.x = a.x + (b.x - a.x) * blend.t;
119
- b.y = a.y + (b.y - a.y) * blend.t;
120
- b.z = a.z + (b.z - a.z) * blend.t;
121
- }
122
- this._run.push(space.offset(b.x, b.y, b.z));
92
+ /** The last run drawn for each curve, kept while what it is drawn from is unchanged. */
93
+ const runs = new WeakMap<object, { readonly key: string; readonly run: Vector3[] }>();
94
+
95
+ function pointsOf(s: CurveValues, space: GraphSpace): Vector3[] {
96
+ const { source, blend } = s.expression;
97
+ const start = clamp(s.start, 0, 1);
98
+ const end = clamp(s.end, 0, 1);
99
+ const samples = Math.max(2, Math.round(s.samples));
100
+ const key = [source, blend?.from, blend?.t, space.revision, s.domain, JSON.stringify(s.params), start, end, samples].join("|");
101
+ const kept = runs.get(s);
102
+ if (kept?.key === key) return kept.run;
103
+ const run: Vector3[] = [];
104
+ runs.set(s, { key, run });
105
+ const to = curveOf(s, source);
106
+ if (!to || !(end > start)) return run;
107
+ const from = blend ? curveOf(s, blend.from) : null;
108
+ const steps = Math.max(1, Math.round(samples * (end - start)));
109
+ const a: Vector3 = { x: 0, y: 0, z: 0 };
110
+ const b: Vector3 = { x: 0, y: 0, z: 0 };
111
+ for (let i = 0; i <= steps; i++) {
112
+ const u = start + ((end - start) * i) / steps;
113
+ if (!to(u, b)) continue;
114
+ if (from && blend) {
115
+ if (!from(u, a)) continue;
116
+ b.x = a.x + (b.x - a.x) * blend.t;
117
+ b.y = a.y + (b.y - a.y) * blend.t;
118
+ b.z = a.z + (b.z - a.z) * blend.t;
123
119
  }
124
- return this._run;
120
+ run.push(space.offset(b.x, b.y, b.z));
125
121
  }
122
+ return run;
123
+ }
126
124
 
127
- /** `source` as a graph point for a share `u` of its span, or `false` where it has none. */
128
- private _curve(source: string): ((u: number, out: Vector3) => boolean) | null {
129
- const equation = equationOf(source);
130
- if (!equation) return null;
131
- const params = scopeFor(spaceCurveParams(source), this.params);
132
- const [t0, t1] = this.domain ?? [0, 1];
133
- const { x, y, z } = equation;
134
- return (u, out) => {
135
- const t = t0 + (t1 - t0) * u;
136
- out.x = x(t, 0, params);
137
- out.y = y(t, 0, params);
138
- out.z = z(t, 0, params);
139
- return Number.isFinite(out.x) && Number.isFinite(out.y) && Number.isFinite(out.z);
140
- };
141
- }
125
+ /** `source` as a graph point for a share `u` of its span, or `false` where it has none. */
126
+ function curveOf(s: CurveValues, source: string): ((u: number, out: Vector3) => boolean) | null {
127
+ const equation = equationOf(source);
128
+ if (!equation) return null;
129
+ const params = scopeFor(spaceCurveParams(source), s.params);
130
+ const [t0, t1] = s.domain ?? [0, 1];
131
+ const { x, y, z } = equation;
132
+ return (u, out) => {
133
+ const t = t0 + (t1 - t0) * u;
134
+ out.x = x(t, 0, params);
135
+ out.y = y(t, 0, params);
136
+ out.z = z(t, 0, params);
137
+ return Number.isFinite(out.x) && Number.isFinite(out.y) && Number.isFinite(out.z);
138
+ };
142
139
  }
143
140
 
144
141
  function equationOf(source: string): SpaceCurveEquation | null {