@motionscript/math 0.0.0-stage → 0.1.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (322) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +48 -0
  4. package/README.md +385 -3
  5. package/dist/annotate/angle-mark.d.ts +46 -0
  6. package/dist/annotate/angle-mark.d.ts.map +1 -0
  7. package/dist/annotate/angle-mark.js +132 -0
  8. package/dist/annotate/angle-mark.js.map +1 -0
  9. package/dist/annotate/curly-brace.d.ts +46 -0
  10. package/dist/annotate/curly-brace.d.ts.map +1 -0
  11. package/dist/annotate/curly-brace.js +114 -0
  12. package/dist/annotate/curly-brace.js.map +1 -0
  13. package/dist/annotate/emphasis-mark.d.ts +20 -0
  14. package/dist/annotate/emphasis-mark.d.ts.map +1 -0
  15. package/dist/annotate/emphasis-mark.js +81 -0
  16. package/dist/annotate/emphasis-mark.js.map +1 -0
  17. package/dist/browser/THIRD_PARTY_LICENSES.txt +420 -0
  18. package/dist/browser/chunks/chunk-3GJB6AVO.js +2 -0
  19. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +7 -0
  20. package/dist/browser/chunks/chunk-D5WHCVBC.js +2 -0
  21. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +7 -0
  22. package/dist/browser/chunks/chunk-WL4KEX7Y.js +2 -0
  23. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +7 -0
  24. package/dist/browser/chunks/mathjax-DNP3YRES.js +61 -0
  25. package/dist/browser/chunks/mathjax-DNP3YRES.js.map +7 -0
  26. package/dist/browser/index.js +2 -0
  27. package/dist/browser/index.js.map +7 -0
  28. package/dist/browser/kit.js +2 -0
  29. package/dist/browser/kit.js.map +7 -0
  30. package/dist/browser/manifest.json +30 -0
  31. package/dist/browser/tex.js +2 -0
  32. package/dist/browser/tex.js.map +7 -0
  33. package/dist/engine.d.ts +64 -0
  34. package/dist/engine.d.ts.map +1 -0
  35. package/dist/engine.js +42 -0
  36. package/dist/engine.js.map +1 -0
  37. package/dist/index.d.ts +30 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +30 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/kit/brace.d.ts +8 -0
  42. package/dist/kit/brace.d.ts.map +1 -0
  43. package/dist/kit/brace.js +36 -0
  44. package/dist/kit/brace.js.map +1 -0
  45. package/dist/kit/cache.d.ts +30 -0
  46. package/dist/kit/cache.d.ts.map +1 -0
  47. package/dist/kit/cache.js +53 -0
  48. package/dist/kit/cache.js.map +1 -0
  49. package/dist/kit/contour.d.ts +8 -0
  50. package/dist/kit/contour.d.ts.map +1 -0
  51. package/dist/kit/contour.js +93 -0
  52. package/dist/kit/contour.js.map +1 -0
  53. package/dist/kit/coords.d.ts +69 -0
  54. package/dist/kit/coords.d.ts.map +1 -0
  55. package/dist/kit/coords.js +224 -0
  56. package/dist/kit/coords.js.map +1 -0
  57. package/dist/kit/curve.d.ts +17 -0
  58. package/dist/kit/curve.d.ts.map +1 -0
  59. package/dist/kit/curve.js +22 -0
  60. package/dist/kit/curve.js.map +1 -0
  61. package/dist/kit/equation.d.ts +61 -0
  62. package/dist/kit/equation.d.ts.map +1 -0
  63. package/dist/kit/equation.js +168 -0
  64. package/dist/kit/equation.js.map +1 -0
  65. package/dist/kit/equation3d.d.ts +50 -0
  66. package/dist/kit/equation3d.d.ts.map +1 -0
  67. package/dist/kit/equation3d.js +103 -0
  68. package/dist/kit/equation3d.js.map +1 -0
  69. package/dist/kit/expression.d.ts +35 -0
  70. package/dist/kit/expression.d.ts.map +1 -0
  71. package/dist/kit/expression.js +124 -0
  72. package/dist/kit/expression.js.map +1 -0
  73. package/dist/kit/field-mark.d.ts +58 -0
  74. package/dist/kit/field-mark.d.ts.map +1 -0
  75. package/dist/kit/field-mark.js +129 -0
  76. package/dist/kit/field-mark.js.map +1 -0
  77. package/dist/kit/field.d.ts +14 -0
  78. package/dist/kit/field.d.ts.map +1 -0
  79. package/dist/kit/field.js +46 -0
  80. package/dist/kit/field.js.map +1 -0
  81. package/dist/kit/follow.d.ts +71 -0
  82. package/dist/kit/follow.d.ts.map +1 -0
  83. package/dist/kit/follow.js +159 -0
  84. package/dist/kit/follow.js.map +1 -0
  85. package/dist/kit/furniture.d.ts +30 -0
  86. package/dist/kit/furniture.d.ts.map +1 -0
  87. package/dist/kit/furniture.js +87 -0
  88. package/dist/kit/furniture.js.map +1 -0
  89. package/dist/kit/geometry.d.ts +13 -0
  90. package/dist/kit/geometry.d.ts.map +1 -0
  91. package/dist/kit/geometry.js +54 -0
  92. package/dist/kit/geometry.js.map +1 -0
  93. package/dist/kit/mark.d.ts +47 -0
  94. package/dist/kit/mark.d.ts.map +1 -0
  95. package/dist/kit/mark.js +103 -0
  96. package/dist/kit/mark.js.map +1 -0
  97. package/dist/kit/part-fills.d.ts +23 -0
  98. package/dist/kit/part-fills.d.ts.map +1 -0
  99. package/dist/kit/part-fills.js +46 -0
  100. package/dist/kit/part-fills.js.map +1 -0
  101. package/dist/kit/place.d.ts +18 -0
  102. package/dist/kit/place.d.ts.map +1 -0
  103. package/dist/kit/place.js +68 -0
  104. package/dist/kit/place.js.map +1 -0
  105. package/dist/kit/placement.d.ts +8 -0
  106. package/dist/kit/placement.d.ts.map +1 -0
  107. package/dist/kit/placement.js +8 -0
  108. package/dist/kit/placement.js.map +1 -0
  109. package/dist/kit/sampler.d.ts +17 -0
  110. package/dist/kit/sampler.d.ts.map +1 -0
  111. package/dist/kit/sampler.js +250 -0
  112. package/dist/kit/sampler.js.map +1 -0
  113. package/dist/kit/scale.d.ts +12 -0
  114. package/dist/kit/scale.d.ts.map +1 -0
  115. package/dist/kit/scale.js +37 -0
  116. package/dist/kit/scale.js.map +1 -0
  117. package/dist/kit/source.d.ts +22 -0
  118. package/dist/kit/source.d.ts.map +1 -0
  119. package/dist/kit/source.js +45 -0
  120. package/dist/kit/source.js.map +1 -0
  121. package/dist/kit/system.d.ts +33 -0
  122. package/dist/kit/system.d.ts.map +1 -0
  123. package/dist/kit/system.js +67 -0
  124. package/dist/kit/system.js.map +1 -0
  125. package/dist/kit/ticks.d.ts +19 -0
  126. package/dist/kit/ticks.d.ts.map +1 -0
  127. package/dist/kit/ticks.js +78 -0
  128. package/dist/kit/ticks.js.map +1 -0
  129. package/dist/kit/withdrawn.d.ts +9 -0
  130. package/dist/kit/withdrawn.d.ts.map +1 -0
  131. package/dist/kit/withdrawn.js +9 -0
  132. package/dist/kit/withdrawn.js.map +1 -0
  133. package/dist/marks/curve-area.d.ts +32 -0
  134. package/dist/marks/curve-area.d.ts.map +1 -0
  135. package/dist/marks/curve-area.js +82 -0
  136. package/dist/marks/curve-area.js.map +1 -0
  137. package/dist/marks/drop-lines.d.ts +22 -0
  138. package/dist/marks/drop-lines.d.ts.map +1 -0
  139. package/dist/marks/drop-lines.js +58 -0
  140. package/dist/marks/drop-lines.js.map +1 -0
  141. package/dist/marks/plot-point.d.ts +35 -0
  142. package/dist/marks/plot-point.d.ts.map +1 -0
  143. package/dist/marks/plot-point.js +97 -0
  144. package/dist/marks/plot-point.js.map +1 -0
  145. package/dist/marks/riemann-sum.d.ts +37 -0
  146. package/dist/marks/riemann-sum.d.ts.map +1 -0
  147. package/dist/marks/riemann-sum.js +91 -0
  148. package/dist/marks/riemann-sum.js.map +1 -0
  149. package/dist/marks/tangent-line.d.ts +27 -0
  150. package/dist/marks/tangent-line.d.ts.map +1 -0
  151. package/dist/marks/tangent-line.js +103 -0
  152. package/dist/marks/tangent-line.js.map +1 -0
  153. package/dist/marks/vector-arrow.d.ts +32 -0
  154. package/dist/marks/vector-arrow.d.ts.map +1 -0
  155. package/dist/marks/vector-arrow.js +87 -0
  156. package/dist/marks/vector-arrow.js.map +1 -0
  157. package/dist/nodes.d.ts +35 -0
  158. package/dist/nodes.d.ts.map +1 -0
  159. package/dist/nodes.js +63 -0
  160. package/dist/nodes.js.map +1 -0
  161. package/dist/plots/function-plot.d.ts +67 -0
  162. package/dist/plots/function-plot.d.ts.map +1 -0
  163. package/dist/plots/function-plot.js +215 -0
  164. package/dist/plots/function-plot.js.map +1 -0
  165. package/dist/plots/points-plot.d.ts +28 -0
  166. package/dist/plots/points-plot.d.ts.map +1 -0
  167. package/dist/plots/points-plot.js +99 -0
  168. package/dist/plots/points-plot.js.map +1 -0
  169. package/dist/plots/stream-lines.d.ts +35 -0
  170. package/dist/plots/stream-lines.d.ts.map +1 -0
  171. package/dist/plots/stream-lines.js +141 -0
  172. package/dist/plots/stream-lines.js.map +1 -0
  173. package/dist/plots/traced-path.d.ts +31 -0
  174. package/dist/plots/traced-path.d.ts.map +1 -0
  175. package/dist/plots/traced-path.js +56 -0
  176. package/dist/plots/traced-path.js.map +1 -0
  177. package/dist/plots/vector-field.d.ts +23 -0
  178. package/dist/plots/vector-field.d.ts.map +1 -0
  179. package/dist/plots/vector-field.js +91 -0
  180. package/dist/plots/vector-field.js.map +1 -0
  181. package/dist/structures/graph-layout.d.ts +11 -0
  182. package/dist/structures/graph-layout.d.ts.map +1 -0
  183. package/dist/structures/graph-layout.js +135 -0
  184. package/dist/structures/graph-layout.js.map +1 -0
  185. package/dist/structures/network-graph.d.ts +83 -0
  186. package/dist/structures/network-graph.d.ts.map +1 -0
  187. package/dist/structures/network-graph.js +289 -0
  188. package/dist/structures/network-graph.js.map +1 -0
  189. package/dist/structures/sample-space.d.ts +62 -0
  190. package/dist/structures/sample-space.d.ts.map +1 -0
  191. package/dist/structures/sample-space.js +199 -0
  192. package/dist/structures/sample-space.js.map +1 -0
  193. package/dist/systems/axes2d.d.ts +75 -0
  194. package/dist/systems/axes2d.d.ts.map +1 -0
  195. package/dist/systems/axes2d.js +193 -0
  196. package/dist/systems/axes2d.js.map +1 -0
  197. package/dist/systems/number-line.d.ts +49 -0
  198. package/dist/systems/number-line.d.ts.map +1 -0
  199. package/dist/systems/number-line.js +127 -0
  200. package/dist/systems/number-line.js.map +1 -0
  201. package/dist/systems/number-plane.d.ts +59 -0
  202. package/dist/systems/number-plane.d.ts.map +1 -0
  203. package/dist/systems/number-plane.js +193 -0
  204. package/dist/systems/number-plane.js.map +1 -0
  205. package/dist/systems/polar-plane.d.ts +48 -0
  206. package/dist/systems/polar-plane.d.ts.map +1 -0
  207. package/dist/systems/polar-plane.js +165 -0
  208. package/dist/systems/polar-plane.js.map +1 -0
  209. package/dist/tex/geometry.d.ts +85 -0
  210. package/dist/tex/geometry.d.ts.map +1 -0
  211. package/dist/tex/geometry.js +632 -0
  212. package/dist/tex/geometry.js.map +1 -0
  213. package/dist/tex/index.d.ts +11 -0
  214. package/dist/tex/index.d.ts.map +1 -0
  215. package/dist/tex/index.js +9 -0
  216. package/dist/tex/index.js.map +1 -0
  217. package/dist/tex/mathjax.d.ts +33 -0
  218. package/dist/tex/mathjax.d.ts.map +1 -0
  219. package/dist/tex/mathjax.js +74 -0
  220. package/dist/tex/mathjax.js.map +1 -0
  221. package/dist/tex/matrix.d.ts +31 -0
  222. package/dist/tex/matrix.d.ts.map +1 -0
  223. package/dist/tex/matrix.js +73 -0
  224. package/dist/tex/matrix.js.map +1 -0
  225. package/dist/tex/node.d.ts +215 -0
  226. package/dist/tex/node.d.ts.map +1 -0
  227. package/dist/tex/node.js +595 -0
  228. package/dist/tex/node.js.map +1 -0
  229. package/dist/tex/tween.d.ts +50 -0
  230. package/dist/tex/tween.d.ts.map +1 -0
  231. package/dist/tex/tween.js +253 -0
  232. package/dist/tex/tween.js.map +1 -0
  233. package/dist/three/axes3d.d.ts +87 -0
  234. package/dist/three/axes3d.d.ts.map +1 -0
  235. package/dist/three/axes3d.js +228 -0
  236. package/dist/three/axes3d.js.map +1 -0
  237. package/dist/three/curve-plot3d.d.ts +60 -0
  238. package/dist/three/curve-plot3d.d.ts.map +1 -0
  239. package/dist/three/curve-plot3d.js +143 -0
  240. package/dist/three/curve-plot3d.js.map +1 -0
  241. package/dist/three/label3d.d.ts +11 -0
  242. package/dist/three/label3d.d.ts.map +1 -0
  243. package/dist/three/label3d.js +40 -0
  244. package/dist/three/label3d.js.map +1 -0
  245. package/dist/three/plot-point3d.d.ts +26 -0
  246. package/dist/three/plot-point3d.d.ts.map +1 -0
  247. package/dist/three/plot-point3d.js +60 -0
  248. package/dist/three/plot-point3d.js.map +1 -0
  249. package/dist/three/space.d.ts +28 -0
  250. package/dist/three/space.d.ts.map +1 -0
  251. package/dist/three/space.js +47 -0
  252. package/dist/three/space.js.map +1 -0
  253. package/dist/three/surface-plot3d.d.ts +80 -0
  254. package/dist/three/surface-plot3d.d.ts.map +1 -0
  255. package/dist/three/surface-plot3d.js +290 -0
  256. package/dist/three/surface-plot3d.js.map +1 -0
  257. package/dist/three/vector-arrow3d.d.ts +25 -0
  258. package/dist/three/vector-arrow3d.d.ts.map +1 -0
  259. package/dist/three/vector-arrow3d.js +72 -0
  260. package/dist/three/vector-arrow3d.js.map +1 -0
  261. package/package.json +79 -3
  262. package/registry.json +251 -0
  263. package/src/annotate/angle-mark.ts +147 -0
  264. package/src/annotate/curly-brace.ts +126 -0
  265. package/src/annotate/emphasis-mark.ts +89 -0
  266. package/src/engine.ts +72 -0
  267. package/src/index.ts +29 -0
  268. package/src/kit/brace.ts +37 -0
  269. package/src/kit/cache.ts +61 -0
  270. package/src/kit/contour.ts +94 -0
  271. package/src/kit/coords.ts +278 -0
  272. package/src/kit/curve.ts +29 -0
  273. package/src/kit/equation.ts +181 -0
  274. package/src/kit/equation3d.ts +137 -0
  275. package/src/kit/expression.ts +159 -0
  276. package/src/kit/field-mark.ts +155 -0
  277. package/src/kit/field.ts +50 -0
  278. package/src/kit/follow.ts +203 -0
  279. package/src/kit/furniture.ts +108 -0
  280. package/src/kit/geometry.ts +49 -0
  281. package/src/kit/mark.ts +131 -0
  282. package/src/kit/part-fills.ts +61 -0
  283. package/src/kit/place.ts +101 -0
  284. package/src/kit/placement.ts +8 -0
  285. package/src/kit/sampler.ts +281 -0
  286. package/src/kit/scale.ts +39 -0
  287. package/src/kit/source.ts +57 -0
  288. package/src/kit/system.ts +99 -0
  289. package/src/kit/ticks.ts +85 -0
  290. package/src/kit/withdrawn.ts +8 -0
  291. package/src/marks/curve-area.ts +89 -0
  292. package/src/marks/drop-lines.ts +63 -0
  293. package/src/marks/plot-point.ts +107 -0
  294. package/src/marks/riemann-sum.ts +98 -0
  295. package/src/marks/tangent-line.ts +103 -0
  296. package/src/marks/vector-arrow.ts +98 -0
  297. package/src/nodes.ts +63 -0
  298. package/src/plots/function-plot.ts +236 -0
  299. package/src/plots/points-plot.ts +107 -0
  300. package/src/plots/stream-lines.ts +151 -0
  301. package/src/plots/traced-path.ts +52 -0
  302. package/src/plots/vector-field.ts +88 -0
  303. package/src/structures/graph-layout.ts +145 -0
  304. package/src/structures/network-graph.ts +308 -0
  305. package/src/structures/sample-space.ts +209 -0
  306. package/src/systems/axes2d.ts +217 -0
  307. package/src/systems/number-line.ts +131 -0
  308. package/src/systems/number-plane.ts +203 -0
  309. package/src/systems/polar-plane.ts +176 -0
  310. package/src/tex/geometry.ts +703 -0
  311. package/src/tex/index.ts +14 -0
  312. package/src/tex/mathjax.ts +82 -0
  313. package/src/tex/matrix.ts +75 -0
  314. package/src/tex/node.ts +641 -0
  315. package/src/tex/tween.ts +308 -0
  316. package/src/three/axes3d.ts +240 -0
  317. package/src/three/curve-plot3d.ts +147 -0
  318. package/src/three/label3d.ts +50 -0
  319. package/src/three/plot-point3d.ts +66 -0
  320. package/src/three/space.ts +67 -0
  321. package/src/three/surface-plot3d.ts +323 -0
  322. package/src/three/vector-arrow3d.ts +77 -0
@@ -0,0 +1,308 @@
1
+ import { lerpNumber, type PathCommand } from "@motionscript/sdk";
2
+ import type { LatexToken } from "./geometry";
3
+
4
+ export interface AnimatedToken {
5
+ token: string;
6
+ path: LatexToken["path"];
7
+ /** The top-level term it belongs to at rest; a morph matches on it. */
8
+ term?: string;
9
+ /** 0 = invisible, 1 = fully visible */
10
+ opacity: number;
11
+ /** Interpolated position offset applied during morph (x, y in formula space). */
12
+ x: number;
13
+ y: number;
14
+ /** Mid-morph: the index of the token it was in the formula left, and of the one it becomes. */
15
+ from?: number;
16
+ to?: number;
17
+ }
18
+
19
+ /**
20
+ * How a `Latex` node interpolates between two formulas: given the tokens it
21
+ * currently holds and the tokens the target formula resolves to, return a
22
+ * pure `t → AnimatedToken[]` frame function. `prepareLatexTween` below is the
23
+ * default implementation (exported as `defaultLatexMorph`) — pass a `morph`
24
+ * of this shape to `<Latex>` to replace it with your own.
25
+ */
26
+ export type LatexMorphStrategy = (from: LatexToken[], to: LatexToken[]) => (t: number) => AnimatedToken[];
27
+
28
+ /** The coordinate pairs a `PathCommand` can carry, as (x, y) field names. */
29
+ const POINT_FIELDS = [["x", "y"], ["x1", "y1"], ["x2", "y2"]] as const;
30
+
31
+ /**
32
+ * Compute the centroid of a token's path for position-based interpolation.
33
+ */
34
+ function centroid(path: LatexToken["path"]): { x: number; y: number } {
35
+ let sx = 0, sy = 0, n = 0;
36
+ for (const cmd of path) {
37
+ if ("x" in cmd && "y" in cmd) {
38
+ sx += (cmd as any).x;
39
+ sy += (cmd as any).y;
40
+ n++;
41
+ }
42
+ }
43
+ return n > 0 ? { x: sx / n, y: sy / n } : { x: 0, y: 0 };
44
+ }
45
+
46
+ /**
47
+ * A glyph's on-screen extent, as the diagonal of its control-point bbox.
48
+ *
49
+ * Only ever used as a *ratio* between the two ends of a match, so the fact that
50
+ * control points overshoot the true outline doesn't matter: both ends overshoot
51
+ * by the same proportion, because they are the same outline.
52
+ */
53
+ function extent(path: LatexToken["path"]): number {
54
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
55
+ for (const cmd of path) {
56
+ const c = cmd as any;
57
+ for (const [kx, ky] of POINT_FIELDS) {
58
+ if (kx in c) {
59
+ if (c[kx] < minX) minX = c[kx];
60
+ if (c[kx] > maxX) maxX = c[kx];
61
+ if (c[ky] < minY) minY = c[ky];
62
+ if (c[ky] > maxY) maxY = c[ky];
63
+ }
64
+ }
65
+ }
66
+ if (minX > maxX) return 0;
67
+ return Math.hypot(maxX - minX, maxY - minY);
68
+ }
69
+
70
+ /**
71
+ * Whether two paths can be interpolated point by point: same command count,
72
+ * same command types in the same order.
73
+ *
74
+ * For a matched pair this is the overwhelmingly common case, and not by luck.
75
+ * Tokens are matched on the character their MathJax glyph id decodes to, and
76
+ * that id *is* the key into the `<defs>` dictionary — so the same character in
77
+ * two formulas is the same outline, emitted twice under two different
78
+ * transforms. Same commands, different numbers.
79
+ */
80
+ function isPointwiseCompatible(from: PathCommand[], to: PathCommand[]): boolean {
81
+ if (from.length !== to.length) return false;
82
+ for (let i = 0; i < from.length; i++) {
83
+ if (from[i].type !== to[i].type) return false;
84
+ }
85
+ return true;
86
+ }
87
+
88
+ /**
89
+ * Lerp two structurally identical paths point by point.
90
+ *
91
+ * When the two ends are the same outline under two affine transforms — which is
92
+ * what a matched glyph is — this is exact at both ends *and* correct in
93
+ * between: `(1-t)·A·q + t·B·q = ((1-t)A + tB)·q`, and a blend of two
94
+ * uniform-scale-plus-translate transforms is another one. The intermediate is a
95
+ * properly formed glyph at an intermediate size, not a smeared one.
96
+ */
97
+ function lerpPath(from: PathCommand[], to: PathCommand[], t: number): PathCommand[] {
98
+ const out: PathCommand[] = new Array(from.length);
99
+ for (let i = 0; i < from.length; i++) {
100
+ const f = from[i] as any;
101
+ const g = to[i] as any;
102
+ const c: any = { ...g };
103
+ for (const [kx, ky] of POINT_FIELDS) {
104
+ if (kx in g && kx in f) {
105
+ c[kx] = lerpNumber(f[kx], g[kx], t);
106
+ c[ky] = lerpNumber(f[ky], g[ky], t);
107
+ }
108
+ }
109
+ out[i] = c as PathCommand;
110
+ }
111
+ return out;
112
+ }
113
+
114
+ /**
115
+ * Scale a path uniformly about (cx, cy), then translate by (dx, dy).
116
+ *
117
+ * The fallback for a matched pair whose paths *aren't* structurally identical —
118
+ * a stretchy delimiter assembled from a different number of pieces at the two
119
+ * sizes, say. Point-by-point lerping is impossible there, so the target outline
120
+ * is shrunk to the source's size instead and grown back over the morph: the
121
+ * size still changes continuously, which is the whole point, at the cost of
122
+ * showing the target's shape from the start.
123
+ */
124
+ function scaleAbout(
125
+ path: PathCommand[],
126
+ cx: number,
127
+ cy: number,
128
+ s: number,
129
+ dx: number,
130
+ dy: number,
131
+ ): PathCommand[] {
132
+ return path.map(cmd => {
133
+ const c: any = { ...(cmd as any) };
134
+ for (const [kx, ky] of POINT_FIELDS) {
135
+ if (kx in c) {
136
+ c[kx] = cx + (c[kx] - cx) * s + dx;
137
+ c[ky] = cy + (c[ky] - cy) * s + dy;
138
+ }
139
+ }
140
+ return c as PathCommand;
141
+ });
142
+ }
143
+
144
+ /**
145
+ * Match tokens from `from` to `to` by character key, a term that both formulas
146
+ * write first: the `x^2` of `x^2 + 1` travels to the `x^2` of `y = x^2` as
147
+ * one, instead of lending its `2` to whichever `2` comes first. What is left
148
+ * is matched greedily by character alone.
149
+ * Returns three lists: matched pairs, deleted tokens (only in from), added tokens (only in to).
150
+ */
151
+ function matchTokens(
152
+ from: LatexToken[],
153
+ to: LatexToken[],
154
+ ): {
155
+ matched: Array<{ from: LatexToken; to: LatexToken }>;
156
+ deleted: LatexToken[];
157
+ added: LatexToken[];
158
+ } {
159
+ const remaining = [...to];
160
+ const partner = new Map<LatexToken, LatexToken>();
161
+ // Synthetic shapes (rects/paths) have no natural token key.
162
+ const keyed = (t: LatexToken) => !t.token.startsWith("__");
163
+ const pair = (same: (f: LatexToken, t: LatexToken) => boolean) => {
164
+ for (const ft of from) {
165
+ if (!keyed(ft) || partner.has(ft)) continue;
166
+ const idx = remaining.findIndex(t => keyed(t) && same(ft, t));
167
+ if (idx === -1) continue;
168
+ partner.set(ft, remaining[idx]);
169
+ remaining.splice(idx, 1);
170
+ }
171
+ };
172
+ pair((f, t) => f.token === t.token && f.term !== undefined && f.term === t.term);
173
+ pair((f, t) => f.token === t.token);
174
+
175
+ const matched: Array<{ from: LatexToken; to: LatexToken }> = [];
176
+ const deleted: LatexToken[] = [];
177
+ for (const ft of from) {
178
+ const tt = partner.get(ft);
179
+ if (tt) matched.push({ from: ft, to: tt });
180
+ else deleted.push(ft);
181
+ }
182
+
183
+ return { matched, deleted, added: remaining };
184
+ }
185
+
186
+ /**
187
+ * Precompute a formula-change morph and return a pure `t → AnimatedToken[]`
188
+ * frame function:
189
+ * - Deleted tokens fade out over the first half of `t`.
190
+ * - Matched tokens are interpolated — place *and* size — across the full range
191
+ * of `t`.
192
+ * - Added tokens fade in over the second half of `t`.
193
+ *
194
+ * A matched glyph is rarely the same size at both ends: the `2` of `b^2` is set
195
+ * at script size and the `2` of `2a` at full size, and a `\frac`'s arguments
196
+ * come back a step smaller than the same symbols on a baseline. So the
197
+ * interpolation has to carry the glyph's *geometry*, not just where it sits.
198
+ * Sliding the target outline from one centroid to the other — which is all this
199
+ * used to do — put the whole size change between the last static frame and the
200
+ * morph's first one: a snap to the new size, then a smooth glide to the new
201
+ * place. Point-by-point lerping (see {@link lerpPath}) is that same
202
+ * interpolation generalised from a glyph's average point to all of them, and it
203
+ * costs nothing extra — the path was already being rebuilt every frame to apply
204
+ * the slide.
205
+ *
206
+ * `t` is normalized `[0, 1]` and already eased — the caller (a `Command`'s
207
+ * `at`) applies easing once, up front, the same eased value driving every
208
+ * concurrent aspect of the morph (props, intrinsic size, tokens) in lockstep.
209
+ */
210
+ export function prepareLatexTween(
211
+ from: LatexToken[],
212
+ to: LatexToken[],
213
+ ): (t: number) => AnimatedToken[] {
214
+ const { matched, deleted, added } = matchTokens(from, to);
215
+
216
+ // Per-match interpolation data. Structure compatibility, and the centroids
217
+ // and extents behind the fallback, are properties of the pair rather than
218
+ // of `t`, so they are settled once here instead of at every frame.
219
+ const fromIndex = new Map(from.map((token, i) => [token, i]));
220
+ const toIndex = new Map(to.map((token, i) => [token, i]));
221
+ const matchedData = matched.map(({ from: f, to: t }) => {
222
+ const toExtent = extent(t.path);
223
+ return {
224
+ from: fromIndex.get(f),
225
+ to: toIndex.get(t),
226
+ pointwise: isPointwiseCompatible(f.path, t.path),
227
+ fromPath: f.path,
228
+ toPath: t.path,
229
+ token: t.token,
230
+ fromCenter: centroid(f.path),
231
+ toCenter: centroid(t.path),
232
+ /** Only the fallback needs it, and only as a ratio. */
233
+ fromScale: toExtent > 0 ? extent(f.path) / toExtent : 1,
234
+ };
235
+ });
236
+
237
+ return (t: number): AnimatedToken[] => {
238
+ const tokens: AnimatedToken[] = [];
239
+
240
+ // Matched tokens: interpolate place and size across the full duration.
241
+ for (const m of matchedData) {
242
+ if (m.pointwise) {
243
+ tokens.push({
244
+ token: m.token,
245
+ path: lerpPath(m.fromPath, m.toPath, t),
246
+ opacity: 1,
247
+ // Baked into the path above: the lerp carries every point,
248
+ // which includes where the glyph sits.
249
+ x: 0,
250
+ y: 0,
251
+ from: m.from,
252
+ to: m.to,
253
+ });
254
+ continue;
255
+ }
256
+
257
+ const s = lerpNumber(m.fromScale, 1, t);
258
+ const cx = lerpNumber(m.fromCenter.x, m.toCenter.x, t);
259
+ const cy = lerpNumber(m.fromCenter.y, m.toCenter.y, t);
260
+ tokens.push({
261
+ token: m.token,
262
+ path: scaleAbout(
263
+ m.toPath,
264
+ m.toCenter.x,
265
+ m.toCenter.y,
266
+ s,
267
+ cx - m.toCenter.x,
268
+ cy - m.toCenter.y,
269
+ ),
270
+ opacity: 1,
271
+ x: 0,
272
+ y: 0,
273
+ from: m.from,
274
+ to: m.to,
275
+ });
276
+ }
277
+
278
+ // Deleted tokens: fade out over the first half, gone by t=0.5
279
+ for (const d of deleted) {
280
+ const fadeT = Math.min(t * 2, 1);
281
+ tokens.push({
282
+ token: d.token,
283
+ path: d.path,
284
+ opacity: lerpNumber(1, 0, fadeT),
285
+ x: 0,
286
+ y: 0,
287
+ from: fromIndex.get(d),
288
+ });
289
+ }
290
+
291
+ // Added tokens: fade in over the second half, starting at t=0.5
292
+ for (const a of added) {
293
+ const fadeT = Math.max(t * 2 - 1, 0);
294
+ tokens.push({
295
+ token: a.token,
296
+ path: a.path,
297
+ opacity: lerpNumber(0, 1, fadeT),
298
+ x: 0,
299
+ y: 0,
300
+ to: toIndex.get(a),
301
+ });
302
+ }
303
+
304
+ return tokens;
305
+ };
306
+ }
307
+
308
+ export { prepareLatexTween as defaultLatexMorph };
@@ -0,0 +1,240 @@
1
+ import {
2
+ Canvas3D,
3
+ colorProperty,
4
+ command,
5
+ Geo,
6
+ Graphics3D,
7
+ Mat,
8
+ node,
9
+ property,
10
+ type AssetScope,
11
+ type Canvas3DProps,
12
+ type Color,
13
+ type Command,
14
+ type CommandArgs,
15
+ type NodeConfig,
16
+ type Scene3D,
17
+ type Transform3D,
18
+ type Vector3,
19
+ type Vector3Input,
20
+ } from "@motionscript/sdk";
21
+ import type { Interval } from "../kit/coords";
22
+ import { AXIS_COLOR, lerpInterval, readInterval } from "../kit/furniture";
23
+ import { clamp, stated } from "../kit/geometry";
24
+ import { formatTick, ticksFor } from "../kit/ticks";
25
+ import { drawLabel3D, type LabelStyle3D } from "./label3d";
26
+ import { graphSpace, type GraphSpace } from "./space";
27
+
28
+ export interface Axes3dProps extends Canvas3DProps {
29
+ xRange?: Interval;
30
+ yRange?: Interval;
31
+ zRange?: Interval;
32
+ xLength?: number;
33
+ yLength?: number;
34
+ zLength?: number;
35
+ xStep?: number;
36
+ yStep?: number;
37
+ zStep?: number;
38
+ color?: Color;
39
+ thickness?: number;
40
+ tips?: boolean;
41
+ ticks?: boolean;
42
+ tickSize?: number;
43
+ grid?: boolean;
44
+ gridLineColor?: Color;
45
+ labels?: boolean;
46
+ labelSize?: number;
47
+ labelColor?: Color;
48
+ fontFamily?: string;
49
+ xLabel?: string;
50
+ yLabel?: string;
51
+ zLabel?: string;
52
+ }
53
+
54
+ /** How many pixels of a 2D axis one scene unit counts as, when an axis picks its own tick spacing. */
55
+ const UNIT_AS_PIXELS = 64;
56
+
57
+ type AxisName = "x" | "y" | "z";
58
+
59
+ /** The turn that lays a shape built along the scene's +y along each graph axis. */
60
+ const ALONG: Record<AxisName, Vector3Input> = { x: [90, 0, 0], y: [0, 0, -90], z: [0, 0, 0] };
61
+
62
+ /** The graph axis each axis's tick marks lie along. */
63
+ const TICK_ALONG: Record<AxisName, AxisName> = { x: "y", y: "x", z: "y" };
64
+
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;
82
+
83
+ /** 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
+
88
+ /** 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;
94
+ /** 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
+
100
+ /** 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;
102
+ /** (`gridColor` is the viewport's own reference grid.) */
103
+ @colorProperty({ default: "#374151" }) declare gridLineColor: Color;
104
+
105
+ /** A number at each tick. */
106
+ @property({ default: false, animatable: false }) declare labels: boolean;
107
+ /** 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;
111
+ /** 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;
115
+
116
+ constructor(props?: NodeConfig<Axes3d<P>, P>) {
117
+ super((props ?? {}) as NodeConfig<Canvas3D<P>, P>);
118
+ }
119
+
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
+ }
125
+
126
+ override declareAssets(assets: AssetScope): void {
127
+ super.declareAssets(assets);
128
+ if (this.fontFamily !== "") assets.font(this.fontFamily);
129
+ }
130
+
131
+ /** @internal */
132
+ override _spaceOf(): GraphSpace {
133
+ return this._graphSpace();
134
+ }
135
+
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;
143
+ }
144
+ return this._space!;
145
+ }
146
+
147
+ protected override buildScene3D(): Scene3D {
148
+ const scene = super.buildScene3D();
149
+ scene.draw(this._furniture());
150
+ return scene;
151
+ }
152
+
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 },
202
+ rotation: ALONG[axis],
203
+ });
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
+ }
211
+
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
+ }
231
+ }
232
+ }
233
+
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);
236
+ }
237
+ if (plane.length > 0) g3.line({ points: plane, segments: true, stroke: { fill: this.gridLineColor } });
238
+ return g3;
239
+ }
240
+ }
@@ -0,0 +1,147 @@
1
+ import {
2
+ command,
3
+ Geo,
4
+ Mesh3D,
5
+ node,
6
+ property,
7
+ type Command,
8
+ type CommandArgs,
9
+ type Geometry3D,
10
+ type Mesh3DProps,
11
+ type NodeConfig,
12
+ type Vector3,
13
+ } from "@motionscript/sdk";
14
+ import type { Interval } from "../kit/coords";
15
+ import { parseSpaceCurve, spaceCurveParams, type SpaceCurveEquation } from "../kit/equation3d";
16
+ import type { ExpressionScope } from "../kit/expression";
17
+ 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";
21
+
22
+ export interface CurvePlot3dProps extends Mesh3DProps {
23
+ expression?: string | PlotSource;
24
+ domain?: Interval | null;
25
+ params?: ExpressionScope;
26
+ samples?: number;
27
+ thickness?: number;
28
+ start?: number;
29
+ end?: number;
30
+ }
31
+
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
+
44
+ /** 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
+
49
+ /** How many points the curve is drawn through. */
50
+ @property({ default: 160, min: 2, max: 2000, step: 1, animatable: false }) declare samples: number;
51
+ /** The radius of the line, in scene units. */
52
+ @property({ default: 0.04, min: 0.001, step: 0.005 }) declare thickness: number;
53
+
54
+ /** 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
+ }
80
+
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
+ }
94
+
95
+ private _key = "";
96
+ private _run: Vector3[] = [];
97
+
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));
123
+ }
124
+ return this._run;
125
+ }
126
+
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
+ }
142
+ }
143
+
144
+ function equationOf(source: string): SpaceCurveEquation | null {
145
+ const equation = parseSpaceCurve(source, spaceCurveParams(source));
146
+ return equation instanceof Error ? null : equation;
147
+ }