@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 @@
1
+ {"version":3,"file":"equation.js","sourceRoot":"","sources":["../../src/kit/equation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAuB,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EAAE,uBAAuB,EAAE,YAAY,EAA2B,MAAM,cAAc,CAAC;AAoB9F,2EAA2E;AAC3E,MAAM,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AAC,CAAC;IACd,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,CAAC,KAAK,GAAG;YAAE,KAAK,EAAE,CAAC;aAClB,IAAI,CAAC,KAAK,GAAG;YAAE,KAAK,EAAE,CAAC;aACvB,IAAI,CAAC,KAAK,SAAS,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;YACtC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;YAClC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,CAAC;IACL,CAAC;IACD,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC/B,OAAO,KAAK,CAAC;AACjB,CAAC","sourcesContent":["import { expressionOps, type ExpressionNode } from \"@motionscript/sdk\";\r\nimport { createBoundedCache } from \"./cache\";\r\nimport { compileExpressionCached, MATH_DIALECT, type CompiledExpression } from \"./expression\";\r\n\r\n/**\r\n * A plotted equation, read the way it is written rather than by a `kind` prop:\r\n *\r\n * | Written as | Is |\r\n * |---|---|\r\n * | `sin(x)`, `y = sin(x)` | a function of `x` |\r\n * | `x = y^2` | a function of `y` |\r\n * | `r = 1 + cos(theta)` | polar, in `theta` |\r\n * | `(cos(t), sin(t))` | parametric, in `t` |\r\n * | `x^2 + y^2 = 4` | implicit, in `x` and `y` |\r\n */\r\nexport type Equation =\r\n | { kind: \"function\"; of: \"x\" | \"y\"; f: CompiledExpression }\r\n | { kind: \"polar\"; f: CompiledExpression }\r\n | { kind: \"parametric\"; x: CompiledExpression; y: CompiledExpression }\r\n /** Drawn where `f(x, y)` is zero. */\r\n | { kind: \"implicit\"; f: CompiledExpression };\r\n\r\n/** The free variable each kind is sampled over, for a host's help text. */\r\nexport const EQUATION_VARIABLES = {\r\n function: [\"x\"],\r\n polar: [\"theta\"],\r\n parametric: [\"t\"],\r\n implicit: [\"x\", \"y\"],\r\n} as const satisfies Record<Equation[\"kind\"], readonly string[]>;\r\n\r\nconst parsed = createBoundedCache<Equation | Error>(256);\r\n\r\n/**\r\n * `source` as an {@link Equation}, or the reason it is not one. Memoised, so an\r\n * unchanged equation is the identical object and a geometry cache can key on it.\r\n */\r\nexport function parseEquation(source: string, params: readonly string[] = []): Equation | Error {\r\n const key = `${params.join(\",\")}|${source}`;\r\n const hit = parsed.get(key);\r\n if (hit !== undefined) return hit;\r\n let result: Equation | Error;\r\n try {\r\n result = read(source.trim(), params);\r\n } catch (error) {\r\n result = error instanceof Error ? error : new Error(String(error));\r\n }\r\n parsed.set(key, result);\r\n return result;\r\n}\r\n\r\n/**\r\n * Why `source` will not plot, or `null` when it will. An empty one is not an\r\n * error (nobody has written it yet), and neither is a name it does not know:\r\n * that is a named value, see {@link equationParams}.\r\n */\r\nexport function equationError(source: string): string | null {\r\n if (source.trim() === \"\") return null;\r\n const equation = parseEquation(source, equationParams(source));\r\n return equation instanceof Error ? equation.message : null;\r\n}\r\n\r\n/** A name that is always a variable of some kind of equation, so never a named value. */\r\nconst VARIABLE_NAMES: ReadonlySet<string> = new Set([\"r\", ...Object.values(EQUATION_VARIABLES).flat()]);\r\n\r\nconst named = createBoundedCache<readonly string[]>(256);\r\n\r\n/**\r\n * Every name `source` reads, in the order it first names them, variables\r\n * included. Empty for text that does not parse.\r\n */\r\nexport function equationNames(source: string): readonly string[] {\r\n const hit = named.get(source);\r\n if (hit !== undefined) return hit;\r\n const names = new Set<string>();\r\n const text = source.trim();\r\n try {\r\n for (const part of splitTuple(text) ?? splitTopLevel(text, \"=\")) {\r\n collectNames(expressionOps.parse(part.trim(), MATH_DIALECT).root, names);\r\n }\r\n } catch {\r\n names.clear();\r\n }\r\n const result = [...names];\r\n named.set(source, result);\r\n return result;\r\n}\r\n\r\n/**\r\n * The named values `source` reads: every name that is not a variable, a\r\n * function or a constant.\r\n */\r\nexport function equationParams(source: string): readonly string[] {\r\n return equationNames(source).filter((name) => !VARIABLE_NAMES.has(name));\r\n}\r\n\r\nfunction collectNames(node: ExpressionNode, into: Set<string>): void {\r\n switch (node.kind) {\r\n case \"ref\":\r\n into.add(node.name);\r\n return;\r\n case \"unary\":\r\n collectNames(node.operand, into);\r\n return;\r\n case \"binary\":\r\n collectNames(node.left, into);\r\n collectNames(node.right, into);\r\n return;\r\n case \"call\":\r\n for (const arg of node.args) collectNames(arg, into);\r\n return;\r\n default:\r\n }\r\n}\r\n\r\nfunction read(source: string, params: readonly string[]): Equation {\r\n if (source === \"\") throw new Error(\"Empty equation\");\r\n const compile = (text: string, variables: readonly string[]): CompiledExpression => {\r\n const result = compileExpressionCached(text.trim(), variables, params);\r\n if (result instanceof Error) throw result;\r\n return result;\r\n };\r\n\r\n const pair = splitTuple(source);\r\n if (pair?.length === 2) return { kind: \"parametric\", x: compile(pair[0], [\"t\"]), y: compile(pair[1], [\"t\"]) };\r\n\r\n const sides = splitTopLevel(source, \"=\");\r\n if (sides.length > 2) throw new Error(\"An equation has one '='\");\r\n if (sides.length === 1) return { kind: \"function\", of: \"x\", f: compile(source, [\"x\"]) };\r\n\r\n const [left, right] = sides;\r\n const subject = left.trim();\r\n const implicit = (): Equation => {\r\n const l = compile(left, [\"x\", \"y\"]);\r\n const r = compile(right, [\"x\", \"y\"]);\r\n return { kind: \"implicit\", f: (x, y, scope) => l(x, y, scope) - r(x, y, scope) };\r\n };\r\n if (subject === \"r\") return { kind: \"polar\", f: compile(right, [\"theta\"]) };\r\n if (subject === \"y\" || subject === \"x\") {\r\n const of = subject === \"y\" ? \"x\" : \"y\";\r\n // `y = x + y` names its own subject on the right, so it is not a function of one variable.\r\n try {\r\n return { kind: \"function\", of, f: compile(right, [of]) };\r\n } catch (error) {\r\n try {\r\n return implicit();\r\n } catch {\r\n throw error;\r\n }\r\n }\r\n }\r\n return implicit();\r\n}\r\n\r\n/** `(a, b, …)` as its parts, or `null` when `source` is not one parenthesised list of two or more. */\r\nexport function splitTuple(source: string): string[] | null {\r\n if (!source.startsWith(\"(\") || !source.endsWith(\")\")) return null;\r\n let depth = 0;\r\n for (let i = 0; i < source.length - 1; i++) {\r\n if (source[i] === \"(\") depth++;\r\n else if (source[i] === \")\" && --depth === 0) return null;\r\n }\r\n const parts = splitTopLevel(source.slice(1, -1), \",\");\r\n return parts.length >= 2 ? parts : null;\r\n}\r\n\r\nexport function splitTopLevel(source: string, separator: string): string[] {\r\n const parts: string[] = [];\r\n let depth = 0;\r\n let from = 0;\r\n for (let i = 0; i < source.length; i++) {\r\n const c = source[i];\r\n if (c === \"(\") depth++;\r\n else if (c === \")\") depth--;\r\n else if (c === separator && depth === 0) {\r\n parts.push(source.slice(from, i));\r\n from = i + 1;\r\n }\r\n }\r\n parts.push(source.slice(from));\r\n return parts;\r\n}\r\n"]}
@@ -0,0 +1,50 @@
1
+ import { type CompiledExpression } from "./expression.js";
2
+ /**
3
+ * A surface's equation, read the way it is written:
4
+ *
5
+ * | Written as | Is |
6
+ * |---|---|
7
+ * | `sin(x) * cos(y)`, `z = x^2 - y^2` | a height over `x` and `y` |
8
+ * | `(cos(u) sin(v), sin(u) sin(v), cos(v))` | parametric, in `u` and `v` |
9
+ */
10
+ export type SurfaceEquation = {
11
+ kind: "height";
12
+ f: CompiledExpression;
13
+ } | {
14
+ kind: "parametric";
15
+ x: CompiledExpression;
16
+ y: CompiledExpression;
17
+ z: CompiledExpression;
18
+ };
19
+ /** A curve through space, three parts in `t`: `(cos(t), sin(t), t / 4)`. */
20
+ export interface SpaceCurveEquation {
21
+ x: CompiledExpression;
22
+ y: CompiledExpression;
23
+ z: CompiledExpression;
24
+ }
25
+ /** What each 3D form is written over, for a host's help text. */
26
+ export declare const EQUATION_VARIABLES_3D: {
27
+ readonly height: readonly ["x", "y"];
28
+ readonly surface: readonly ["u", "v"];
29
+ readonly spaceCurve: readonly ["t"];
30
+ };
31
+ /** The named values a surface's equation reads: every name that is not one of its variables. */
32
+ export declare function surfaceParams(source: string): readonly string[];
33
+ /** The named values a space curve's equation reads. */
34
+ export declare function spaceCurveParams(source: string): readonly string[];
35
+ /** `source` as a surface, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
36
+ export declare function parseSurface(source: string, params?: readonly string[]): SurfaceEquation | Error;
37
+ /** `source` as a space curve, or the reason it is not one. */
38
+ export declare function parseSpaceCurve(source: string, params?: readonly string[]): SpaceCurveEquation | Error;
39
+ /** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
40
+ export declare function surfaceError(source: string): string | null;
41
+ /** Why `source` will not draw as a space curve, or `null`. */
42
+ export declare function spaceCurveError(source: string): string | null;
43
+ /** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */
44
+ export interface EquationLanguage {
45
+ error(source: string): string | null;
46
+ params(source: string): readonly string[];
47
+ variables: readonly string[];
48
+ }
49
+ export declare const EQUATION_KINDS: Readonly<Record<"equation" | "surfaceEquation" | "spaceCurveEquation" | "fieldEquation", EquationLanguage>>;
50
+ //# sourceMappingURL=equation3d.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"equation3d.d.ts","sourceRoot":"","sources":["../../src/kit/equation3d.ts"],"names":[],"mappings":"AAEA,OAAO,EAA2B,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGhF;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GACrB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,GACzC;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAC;IAAC,CAAC,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAElG,4EAA4E;AAC5E,MAAM,WAAW,kBAAkB;IAC/B,CAAC,EAAE,kBAAkB,CAAC;IACtB,CAAC,EAAE,kBAAkB,CAAC;IACtB,CAAC,EAAE,kBAAkB,CAAC;CACzB;AAED,iEAAiE;AACjE,eAAO,MAAM,qBAAqB;;;;CAIxB,CAAC;AAQX,gGAAgG;AAChG,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAE/D;AAED,uDAAuD;AACvD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAElE;AAED,sHAAsH;AACtH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,SAAS,MAAM,EAAO,GAAG,eAAe,GAAG,KAAK,CAcpG;AAED,8DAA8D;AAC9D,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,GAAE,SAAS,MAAM,EAAO,GAAG,kBAAkB,GAAG,KAAK,CAQ1G;AAED,sFAAsF;AACtF,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI1D;AAED,8DAA8D;AAC9D,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI7D;AA+BD,qHAAqH;AACrH,MAAM,WAAW,gBAAgB;IAC7B,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACrC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IAC1C,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CAChC;AAED,eAAO,MAAM,cAAc,EAAE,QAAQ,CACjC,MAAM,CAAC,UAAU,GAAG,iBAAiB,GAAG,oBAAoB,GAAG,eAAe,EAAE,gBAAgB,CAAC,CAUpG,CAAC"}
@@ -0,0 +1,103 @@
1
+ import { createBoundedCache } from "./cache.js";
2
+ import { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from "./equation.js";
3
+ import { compileExpressionCached } from "./expression.js";
4
+ import { fieldError, fieldParams, FIELD_VARIABLES } from "./field.js";
5
+ /** What each 3D form is written over, for a host's help text. */
6
+ export const EQUATION_VARIABLES_3D = {
7
+ height: ["x", "y"],
8
+ surface: ["u", "v"],
9
+ spaceCurve: ["t"],
10
+ };
11
+ const SURFACE_NAMES = new Set(["x", "y", "z", "u", "v"]);
12
+ const SPACE_CURVE_NAMES = new Set(["t"]);
13
+ const surfaces = createBoundedCache(128);
14
+ const curves = createBoundedCache(128);
15
+ /** The named values a surface's equation reads: every name that is not one of its variables. */
16
+ export function surfaceParams(source) {
17
+ return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));
18
+ }
19
+ /** The named values a space curve's equation reads. */
20
+ export function spaceCurveParams(source) {
21
+ return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));
22
+ }
23
+ /** `source` as a surface, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */
24
+ export function parseSurface(source, params = []) {
25
+ return memo(surfaces, source, params, (text) => {
26
+ const compile = compiler(params);
27
+ const parts = splitTuple(text);
28
+ if (parts) {
29
+ if (parts.length !== 3)
30
+ throw new Error("A parametric surface has three parts: (x, y, z)");
31
+ const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));
32
+ return { kind: "parametric", x, y, z };
33
+ }
34
+ const sides = splitTopLevel(text, "=");
35
+ if (sides.length > 2)
36
+ throw new Error("An equation has one '='");
37
+ if (sides.length === 2 && sides[0].trim() !== "z")
38
+ throw new Error("A surface is written z = …, or as three parts (x, y, z)");
39
+ return { kind: "height", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };
40
+ });
41
+ }
42
+ /** `source` as a space curve, or the reason it is not one. */
43
+ export function parseSpaceCurve(source, params = []) {
44
+ return memo(curves, source, params, (text) => {
45
+ const compile = compiler(params);
46
+ const parts = splitTuple(text);
47
+ if (parts?.length !== 3)
48
+ throw new Error("A space curve has three parts: (x, y, z)");
49
+ const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));
50
+ return { x, y, z };
51
+ });
52
+ }
53
+ /** Why `source` will not draw as a surface, or `null`; empty text is not an error. */
54
+ export function surfaceError(source) {
55
+ if (source.trim() === "")
56
+ return null;
57
+ const equation = parseSurface(source, surfaceParams(source));
58
+ return equation instanceof Error ? equation.message : null;
59
+ }
60
+ /** Why `source` will not draw as a space curve, or `null`. */
61
+ export function spaceCurveError(source) {
62
+ if (source.trim() === "")
63
+ return null;
64
+ const equation = parseSpaceCurve(source, spaceCurveParams(source));
65
+ return equation instanceof Error ? equation.message : null;
66
+ }
67
+ function compiler(params) {
68
+ return (text, variables) => {
69
+ const result = compileExpressionCached(text.trim(), variables, params);
70
+ if (result instanceof Error)
71
+ throw result;
72
+ return result;
73
+ };
74
+ }
75
+ function memo(cache, source, params, read) {
76
+ const key = `${params.join(",")}|${source}`;
77
+ const hit = cache.get(key);
78
+ if (hit !== undefined)
79
+ return hit;
80
+ let result;
81
+ try {
82
+ const text = source.trim();
83
+ if (text === "")
84
+ throw new Error("Empty equation");
85
+ result = read(text);
86
+ }
87
+ catch (error) {
88
+ result = error instanceof Error ? error : new Error(String(error));
89
+ }
90
+ cache.set(key, result);
91
+ return result;
92
+ }
93
+ export const EQUATION_KINDS = {
94
+ equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },
95
+ surfaceEquation: {
96
+ error: surfaceError,
97
+ params: surfaceParams,
98
+ variables: [...EQUATION_VARIABLES_3D.height, ...EQUATION_VARIABLES_3D.surface],
99
+ },
100
+ spaceCurveEquation: { error: spaceCurveError, params: spaceCurveParams, variables: EQUATION_VARIABLES_3D.spaceCurve },
101
+ fieldEquation: { error: fieldError, params: fieldParams, variables: FIELD_VARIABLES },
102
+ };
103
+ //# sourceMappingURL=equation3d.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"equation3d.js","sourceRoot":"","sources":["../../src/kit/equation3d.ts"],"names":[],"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AC,IAAI,EAAE,CAAC,CAAC,EAAE;IAC7H,eAAe,EAAE;QACb,KAAK,EAAE,YAAY;QACnB,MAAM,EAAE,aAAa;QACrB,SAAS,EAAE,CAAC,GAAG,qBAAqB,CAAC,MAAM,EAAE,GAAG,qBAAqB,CAAC,OAAO,CAAC;KACjF;IACD,kBAAkB,EAAE,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,qBAAqB,CAAC,UAAU,EAAE;IACrH,aAAa,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE;CACxF,CAAC","sourcesContent":["import { createBoundedCache } from \"./cache\";\r\nimport { equationError, equationNames, equationParams, EQUATION_VARIABLES, splitTopLevel, splitTuple } from \"./equation\";\r\nimport { compileExpressionCached, type CompiledExpression } from \"./expression\";\r\nimport { fieldError, fieldParams, FIELD_VARIABLES } from \"./field\";\r\n\r\n/**\r\n * A surface's equation, read the way it is written:\r\n *\r\n * | Written as | Is |\r\n * |---|---|\r\n * | `sin(x) * cos(y)`, `z = x^2 - y^2` | a height over `x` and `y` |\r\n * | `(cos(u) sin(v), sin(u) sin(v), cos(v))` | parametric, in `u` and `v` |\r\n */\r\nexport type SurfaceEquation =\r\n | { kind: \"height\"; f: CompiledExpression }\r\n | { kind: \"parametric\"; x: CompiledExpression; y: CompiledExpression; z: CompiledExpression };\r\n\r\n/** A curve through space, three parts in `t`: `(cos(t), sin(t), t / 4)`. */\r\nexport interface SpaceCurveEquation {\r\n x: CompiledExpression;\r\n y: CompiledExpression;\r\n z: CompiledExpression;\r\n}\r\n\r\n/** What each 3D form is written over, for a host's help text. */\r\nexport const EQUATION_VARIABLES_3D = {\r\n height: [\"x\", \"y\"],\r\n surface: [\"u\", \"v\"],\r\n spaceCurve: [\"t\"],\r\n} as const;\r\n\r\nconst SURFACE_NAMES: ReadonlySet<string> = new Set([\"x\", \"y\", \"z\", \"u\", \"v\"]);\r\nconst SPACE_CURVE_NAMES: ReadonlySet<string> = new Set([\"t\"]);\r\n\r\nconst surfaces = createBoundedCache<SurfaceEquation | Error>(128);\r\nconst curves = createBoundedCache<SpaceCurveEquation | Error>(128);\r\n\r\n/** The named values a surface's equation reads: every name that is not one of its variables. */\r\nexport function surfaceParams(source: string): readonly string[] {\r\n return equationNames(source).filter((name) => !SURFACE_NAMES.has(name));\r\n}\r\n\r\n/** The named values a space curve's equation reads. */\r\nexport function spaceCurveParams(source: string): readonly string[] {\r\n return equationNames(source).filter((name) => !SPACE_CURVE_NAMES.has(name));\r\n}\r\n\r\n/** `source` as a surface, or the reason it is not one. Memoised, so an unchanged equation is the identical object. */\r\nexport function parseSurface(source: string, params: readonly string[] = []): SurfaceEquation | Error {\r\n return memo(surfaces, source, params, (text) => {\r\n const compile = compiler(params);\r\n const parts = splitTuple(text);\r\n if (parts) {\r\n if (parts.length !== 3) throw new Error(\"A parametric surface has three parts: (x, y, z)\");\r\n const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.surface));\r\n return { kind: \"parametric\", x, y, z };\r\n }\r\n const sides = splitTopLevel(text, \"=\");\r\n if (sides.length > 2) throw new Error(\"An equation has one '='\");\r\n if (sides.length === 2 && sides[0].trim() !== \"z\") throw new Error(\"A surface is written z = …, or as three parts (x, y, z)\");\r\n return { kind: \"height\", f: compile(sides[sides.length - 1], EQUATION_VARIABLES_3D.height) };\r\n });\r\n}\r\n\r\n/** `source` as a space curve, or the reason it is not one. */\r\nexport function parseSpaceCurve(source: string, params: readonly string[] = []): SpaceCurveEquation | Error {\r\n return memo(curves, source, params, (text) => {\r\n const compile = compiler(params);\r\n const parts = splitTuple(text);\r\n if (parts?.length !== 3) throw new Error(\"A space curve has three parts: (x, y, z)\");\r\n const [x, y, z] = parts.map((part) => compile(part, EQUATION_VARIABLES_3D.spaceCurve));\r\n return { x, y, z };\r\n });\r\n}\r\n\r\n/** Why `source` will not draw as a surface, or `null`; empty text is not an error. */\r\nexport function surfaceError(source: string): string | null {\r\n if (source.trim() === \"\") return null;\r\n const equation = parseSurface(source, surfaceParams(source));\r\n return equation instanceof Error ? equation.message : null;\r\n}\r\n\r\n/** Why `source` will not draw as a space curve, or `null`. */\r\nexport function spaceCurveError(source: string): string | null {\r\n if (source.trim() === \"\") return null;\r\n const equation = parseSpaceCurve(source, spaceCurveParams(source));\r\n return equation instanceof Error ? equation.message : null;\r\n}\r\n\r\nfunction compiler(params: readonly string[]) {\r\n return (text: string, variables: readonly string[]): CompiledExpression => {\r\n const result = compileExpressionCached(text.trim(), variables, params);\r\n if (result instanceof Error) throw result;\r\n return result;\r\n };\r\n}\r\n\r\nfunction memo<T extends object>(\r\n cache: ReturnType<typeof createBoundedCache<T | Error>>,\r\n source: string,\r\n params: readonly string[],\r\n read: (text: string) => T,\r\n): T | Error {\r\n const key = `${params.join(\",\")}|${source}`;\r\n const hit = cache.get(key);\r\n if (hit !== undefined) return hit;\r\n let result: T | Error;\r\n try {\r\n const text = source.trim();\r\n if (text === \"\") throw new Error(\"Empty equation\");\r\n result = read(text);\r\n } catch (error) {\r\n result = error instanceof Error ? error : new Error(String(error));\r\n }\r\n cache.set(key, result);\r\n return result;\r\n}\r\n\r\n/** How a host checks an equation as it is typed, and finds its named values, by the `kind` its property declares. */\r\nexport interface EquationLanguage {\r\n error(source: string): string | null;\r\n params(source: string): readonly string[];\r\n variables: readonly string[];\r\n}\r\n\r\nexport const EQUATION_KINDS: Readonly<\r\n Record<\"equation\" | \"surfaceEquation\" | \"spaceCurveEquation\" | \"fieldEquation\", EquationLanguage>\r\n> = {\r\n equation: { error: equationError, params: equationParams, variables: [...new Set(Object.values(EQUATION_VARIABLES).flat())] },\r\n surfaceEquation: {\r\n error: surfaceError,\r\n params: surfaceParams,\r\n variables: [...EQUATION_VARIABLES_3D.height, ...EQUATION_VARIABLES_3D.surface],\r\n },\r\n spaceCurveEquation: { error: spaceCurveError, params: spaceCurveParams, variables: EQUATION_VARIABLES_3D.spaceCurve },\r\n fieldEquation: { error: fieldError, params: fieldParams, variables: FIELD_VARIABLES },\r\n};\r\n"]}
@@ -0,0 +1,35 @@
1
+ import type { ExpressionDialect } from "@motionscript/sdk";
2
+ /**
3
+ * The language a plot's equation is written in: the engine's own expression
4
+ * grammar (`expressionOps.parse`) under a dialect of its own, where trig is in
5
+ * radians, names are bare identifiers, and the logs and hyperbolics a plot
6
+ * wants are present. Never `new Function`: an equation is text stored in a
7
+ * document, and the shared parser can only produce arithmetic.
8
+ */
9
+ /** Named values an expression reads at call time, beside its two variables. */
10
+ export type ExpressionScope = Readonly<Record<string, number>>;
11
+ /**
12
+ * A compiled expression. The two variables are fixed slots because this is the
13
+ * hot path of every sampled curve; a one-variable expression never reads `b`.
14
+ */
15
+ export type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;
16
+ export declare const MATH_DIALECT: ExpressionDialect;
17
+ /** What an expression may name, for a host's "what can I write here" help. */
18
+ export interface ExpressionVocabulary {
19
+ variables: readonly string[];
20
+ functions: readonly string[];
21
+ constants: readonly string[];
22
+ }
23
+ export declare function expressionVocabulary(variables: readonly string[]): ExpressionVocabulary;
24
+ /**
25
+ * Compile `source` over up to two `variables`, in slot order, and any `params`
26
+ * it may read from the scope. Throws on a syntax error or an unknown name.
27
+ */
28
+ export declare function compileExpression(source: string, variables: readonly string[], params?: readonly string[]): CompiledExpression;
29
+ /**
30
+ * {@link compileExpression}, memoised: equal source under equal names is the
31
+ * identical closure, so "did this expression change" is an `===`, and a failure
32
+ * is remembered rather than re-parsed every frame.
33
+ */
34
+ export declare function compileExpressionCached(source: string, variables: readonly string[], params?: readonly string[]): CompiledExpression | Error;
35
+ //# sourceMappingURL=expression.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expression.d.ts","sourceRoot":"","sources":["../../src/kit/expression.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAkB,MAAM,mBAAmB,CAAC;AAG3E;;;;;;GAMG;AAEH,+EAA+E;AAC/E,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE/D;;;GAGG;AACH,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,eAAe,KAAK,MAAM,CAAC;AAwB3F,eAAO,MAAM,YAAY,EAAE,iBAQ1B,CAAC;AAEF,8EAA8E;AAC9E,MAAM,WAAW,oBAAoB;IACjC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CAChC;AAED,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,oBAAoB,CAMvF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,MAAM,EAAE,EAC5B,MAAM,GAAE,SAAS,MAAM,EAAO,GAC/B,kBAAkB,CAOpB;AAsDD;;;;GAIG;AACH,wBAAgB,uBAAuB,CACnC,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,SAAS,MAAM,EAAE,EAC5B,MAAM,GAAE,SAAS,MAAM,EAAO,GAC/B,kBAAkB,GAAG,KAAK,CAY5B"}
@@ -0,0 +1,124 @@
1
+ import { expressionOps } from "@motionscript/sdk";
2
+ import { createBoundedCache } from "./cache.js";
3
+ const UNARY = {
4
+ sin: Math.sin, cos: Math.cos, tan: Math.tan,
5
+ asin: Math.asin, acos: Math.acos, atan: Math.atan,
6
+ sinh: Math.sinh, cosh: Math.cosh, tanh: Math.tanh,
7
+ sqrt: Math.sqrt, cbrt: Math.cbrt, abs: Math.abs, exp: Math.exp,
8
+ log: Math.log, ln: Math.log, log2: Math.log2, log10: Math.log10,
9
+ floor: Math.floor, ceil: Math.ceil, round: Math.round, sign: Math.sign, trunc: Math.trunc,
10
+ };
11
+ const NARY = {
12
+ atan2: { fn: Math.atan2, arity: [2, 2] },
13
+ pow: { fn: Math.pow, arity: [2, 2] },
14
+ mod: { fn: (a, b) => a % b, arity: [2, 2] },
15
+ hypot: { fn: Math.hypot, arity: [1, Infinity] },
16
+ min: { fn: Math.min, arity: [1, Infinity] },
17
+ max: { fn: Math.max, arity: [1, Infinity] },
18
+ };
19
+ const CONSTANTS = {
20
+ pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,
21
+ };
22
+ export const MATH_DIALECT = {
23
+ functions: {
24
+ ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] }])),
25
+ ...Object.fromEntries(Object.entries(NARY).map(([name, { arity }]) => [name, { arity }])),
26
+ },
27
+ constants: CONSTANTS,
28
+ refs: "bare",
29
+ unknownName: (name) => `Unknown name '${name}'`,
30
+ };
31
+ export function expressionVocabulary(variables) {
32
+ return {
33
+ variables,
34
+ functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),
35
+ constants: ["pi", "e", "tau", "phi"],
36
+ };
37
+ }
38
+ /**
39
+ * Compile `source` over up to two `variables`, in slot order, and any `params`
40
+ * it may read from the scope. Throws on a syntax error or an unknown name.
41
+ */
42
+ export function compileExpression(source, variables, params = []) {
43
+ // The parser folds a constant before it yields a reference, so a name that
44
+ // is both would silently stop being the variable it was passed as.
45
+ for (const name of [...variables, ...params]) {
46
+ if (Object.hasOwn(CONSTANTS, name))
47
+ throw new Error(`'${name}' is a constant, so it cannot be a variable`);
48
+ }
49
+ return emit(expressionOps.parse(source, MATH_DIALECT).root, variables, params);
50
+ }
51
+ function emit(node, variables, params) {
52
+ switch (node.kind) {
53
+ case "const": {
54
+ const { value } = node;
55
+ return () => value;
56
+ }
57
+ case "ref": {
58
+ const { name } = node;
59
+ const slot = variables.indexOf(name);
60
+ if (slot === 0)
61
+ return (a) => a;
62
+ if (slot === 1)
63
+ return (_a, b) => b;
64
+ if (params.includes(name))
65
+ return (_a, _b, scope) => scope?.[name] ?? NaN;
66
+ throw new Error(MATH_DIALECT.unknownName(name));
67
+ }
68
+ case "unary": {
69
+ const operand = emit(node.operand, variables, params);
70
+ return (a, b, s) => -operand(a, b, s);
71
+ }
72
+ case "binary": {
73
+ const l = emit(node.left, variables, params);
74
+ const r = emit(node.right, variables, params);
75
+ switch (node.op) {
76
+ case "+": return (a, b, s) => l(a, b, s) + r(a, b, s);
77
+ case "-": return (a, b, s) => l(a, b, s) - r(a, b, s);
78
+ case "*": return (a, b, s) => l(a, b, s) * r(a, b, s);
79
+ case "/": return (a, b, s) => l(a, b, s) / r(a, b, s);
80
+ case "%": return (a, b, s) => l(a, b, s) % r(a, b, s);
81
+ case "^": return (a, b, s) => Math.pow(l(a, b, s), r(a, b, s));
82
+ }
83
+ break;
84
+ }
85
+ case "call": {
86
+ const args = node.args.map((arg) => emit(arg, variables, params));
87
+ const unary = UNARY[node.name];
88
+ if (unary) {
89
+ const [x] = args;
90
+ return (a, b, s) => unary(x(a, b, s));
91
+ }
92
+ const { fn } = NARY[node.name];
93
+ // The common arity, so the hot path allocates no array per sample.
94
+ if (args.length === 2) {
95
+ const [x, y] = args;
96
+ return (a, b, s) => fn(x(a, b, s), y(a, b, s));
97
+ }
98
+ return (a, b, s) => fn(...args.map((arg) => arg(a, b, s)));
99
+ }
100
+ }
101
+ throw new Error("Unsupported expression");
102
+ }
103
+ const compiled = createBoundedCache(256);
104
+ /**
105
+ * {@link compileExpression}, memoised: equal source under equal names is the
106
+ * identical closure, so "did this expression change" is an `===`, and a failure
107
+ * is remembered rather than re-parsed every frame.
108
+ */
109
+ export function compileExpressionCached(source, variables, params = []) {
110
+ const key = `${variables.join(",")}|${params.join(",")}|${source}`;
111
+ const hit = compiled.get(key);
112
+ if (hit !== undefined)
113
+ return hit;
114
+ let result;
115
+ try {
116
+ result = compileExpression(source, variables, params);
117
+ }
118
+ catch (error) {
119
+ result = error instanceof Error ? error : new Error(String(error));
120
+ }
121
+ compiled.set(key, result);
122
+ return result;
123
+ }
124
+ //# sourceMappingURL=expression.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"expression.js","sourceRoot":"","sources":["../../src/kit/expression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAmB7C,MAAM,KAAK,GAA0C;IACjD,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG;IAC3C,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI;IACjD,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI;IACjD,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG;IAC9D,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK;IAC/D,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK;CAC5F,CAAC;AAEF,MAAM,IAAI,GAAmF;IACzF,KAAK,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,C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AkB,CAA6B,GAAG,CAAC,CAAC;AAErE;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CACnC,MAAc,EACd,SAA4B,EAC5B,SAA4B,EAAE;IAE9B,MAAM,GAAG,GAAG,GAAG,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,EAAE,CAAC;IACnE,MAAM,GAAG,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,GAAG,CAAC;IAClC,IAAI,MAAkC,CAAC;IACvC,IAAI,CAAC;QACD,MAAM,GAAG,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IAC1D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACvE,CAAC;IACD,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAC1B,OAAO,MAAM,CAAC;AAClB,CAAC","sourcesContent":["import { expressionOps } from \"@motionscript/sdk\";\r\nimport type { ExpressionDialect, ExpressionNode } from \"@motionscript/sdk\";\r\nimport { createBoundedCache } from \"./cache\";\r\n\r\n/**\r\n * The language a plot's equation is written in: the engine's own expression\r\n * grammar (`expressionOps.parse`) under a dialect of its own, where trig is in\r\n * radians, names are bare identifiers, and the logs and hyperbolics a plot\r\n * wants are present. Never `new Function`: an equation is text stored in a\r\n * document, and the shared parser can only produce arithmetic.\r\n */\r\n\r\n/** Named values an expression reads at call time, beside its two variables. */\r\nexport type ExpressionScope = Readonly<Record<string, number>>;\r\n\r\n/**\r\n * A compiled expression. The two variables are fixed slots because this is the\r\n * hot path of every sampled curve; a one-variable expression never reads `b`.\r\n */\r\nexport type CompiledExpression = (a: number, b: number, scope?: ExpressionScope) => number;\r\n\r\nconst UNARY: Record<string, (v: number) => number> = {\r\n sin: Math.sin, cos: Math.cos, tan: Math.tan,\r\n asin: Math.asin, acos: Math.acos, atan: Math.atan,\r\n sinh: Math.sinh, cosh: Math.cosh, tanh: Math.tanh,\r\n sqrt: Math.sqrt, cbrt: Math.cbrt, abs: Math.abs, exp: Math.exp,\r\n log: Math.log, ln: Math.log, log2: Math.log2, log10: Math.log10,\r\n floor: Math.floor, ceil: Math.ceil, round: Math.round, sign: Math.sign, trunc: Math.trunc,\r\n};\r\n\r\nconst NARY: Record<string, { fn: (...args: number[]) => number; arity: [number, number] }> = {\r\n atan2: { fn: Math.atan2, arity: [2, 2] },\r\n pow: { fn: Math.pow, arity: [2, 2] },\r\n mod: { fn: (a, b) => a % b, arity: [2, 2] },\r\n hypot: { fn: Math.hypot, arity: [1, Infinity] },\r\n min: { fn: Math.min, arity: [1, Infinity] },\r\n max: { fn: Math.max, arity: [1, Infinity] },\r\n};\r\n\r\nconst CONSTANTS: Record<string, number> = {\r\n pi: Math.PI, PI: Math.PI, e: Math.E, E: Math.E, tau: Math.PI * 2, phi: (1 + Math.sqrt(5)) / 2,\r\n};\r\n\r\nexport const MATH_DIALECT: ExpressionDialect = {\r\n functions: {\r\n ...Object.fromEntries(Object.keys(UNARY).map((name) => [name, { arity: [1, 1] as [number, number] }])),\r\n ...Object.fromEntries(Object.entries(NARY).map(([name, { arity }]) => [name, { arity }])),\r\n },\r\n constants: CONSTANTS,\r\n refs: \"bare\",\r\n unknownName: (name) => `Unknown name '${name}'`,\r\n};\r\n\r\n/** What an expression may name, for a host's \"what can I write here\" help. */\r\nexport interface ExpressionVocabulary {\r\n variables: readonly string[];\r\n functions: readonly string[];\r\n constants: readonly string[];\r\n}\r\n\r\nexport function expressionVocabulary(variables: readonly string[]): ExpressionVocabulary {\r\n return {\r\n variables,\r\n functions: [...Object.keys(UNARY), ...Object.keys(NARY)].sort(),\r\n constants: [\"pi\", \"e\", \"tau\", \"phi\"],\r\n };\r\n}\r\n\r\n/**\r\n * Compile `source` over up to two `variables`, in slot order, and any `params`\r\n * it may read from the scope. Throws on a syntax error or an unknown name.\r\n */\r\nexport function compileExpression(\r\n source: string,\r\n variables: readonly string[],\r\n params: readonly string[] = [],\r\n): CompiledExpression {\r\n // The parser folds a constant before it yields a reference, so a name that\r\n // is both would silently stop being the variable it was passed as.\r\n for (const name of [...variables, ...params]) {\r\n if (Object.hasOwn(CONSTANTS, name)) throw new Error(`'${name}' is a constant, so it cannot be a variable`);\r\n }\r\n return emit(expressionOps.parse(source, MATH_DIALECT).root, variables, params);\r\n}\r\n\r\nfunction emit(node: ExpressionNode, variables: readonly string[], params: readonly string[]): CompiledExpression {\r\n switch (node.kind) {\r\n case \"const\": {\r\n const { value } = node;\r\n return () => value;\r\n }\r\n case \"ref\": {\r\n const { name } = node;\r\n const slot = variables.indexOf(name);\r\n if (slot === 0) return (a) => a;\r\n if (slot === 1) return (_a, b) => b;\r\n if (params.includes(name)) return (_a, _b, scope) => scope?.[name] ?? NaN;\r\n throw new Error(MATH_DIALECT.unknownName(name));\r\n }\r\n case \"unary\": {\r\n const operand = emit(node.operand, variables, params);\r\n return (a, b, s) => -operand(a, b, s);\r\n }\r\n case \"binary\": {\r\n const l = emit(node.left, variables, params);\r\n const r = emit(node.right, variables, params);\r\n switch (node.op) {\r\n case \"+\": return (a, b, s) => l(a, b, s) + r(a, b, s);\r\n case \"-\": return (a, b, s) => l(a, b, s) - r(a, b, s);\r\n case \"*\": return (a, b, s) => l(a, b, s) * r(a, b, s);\r\n case \"/\": return (a, b, s) => l(a, b, s) / r(a, b, s);\r\n case \"%\": return (a, b, s) => l(a, b, s) % r(a, b, s);\r\n case \"^\": return (a, b, s) => Math.pow(l(a, b, s), r(a, b, s));\r\n }\r\n break;\r\n }\r\n case \"call\": {\r\n const args = node.args.map((arg) => emit(arg, variables, params));\r\n const unary = UNARY[node.name];\r\n if (unary) {\r\n const [x] = args;\r\n return (a, b, s) => unary(x(a, b, s));\r\n }\r\n const { fn } = NARY[node.name];\r\n // The common arity, so the hot path allocates no array per sample.\r\n if (args.length === 2) {\r\n const [x, y] = args;\r\n return (a, b, s) => fn(x(a, b, s), y(a, b, s));\r\n }\r\n return (a, b, s) => fn(...args.map((arg) => arg(a, b, s)));\r\n }\r\n }\r\n throw new Error(\"Unsupported expression\");\r\n}\r\n\r\nconst compiled = createBoundedCache<CompiledExpression | Error>(256);\r\n\r\n/**\r\n * {@link compileExpression}, memoised: equal source under equal names is the\r\n * identical closure, so \"did this expression change\" is an `===`, and a failure\r\n * is remembered rather than re-parsed every frame.\r\n */\r\nexport function compileExpressionCached(\r\n source: string,\r\n variables: readonly string[],\r\n params: readonly string[] = [],\r\n): CompiledExpression | Error {\r\n const key = `${variables.join(\",\")}|${params.join(\",\")}|${source}`;\r\n const hit = compiled.get(key);\r\n if (hit !== undefined) return hit;\r\n let result: CompiledExpression | Error;\r\n try {\r\n result = compileExpression(source, variables, params);\r\n } catch (error) {\r\n result = error instanceof Error ? error : new Error(String(error));\r\n }\r\n compiled.set(key, result);\r\n return result;\r\n}\r\n"]}
@@ -0,0 +1,58 @@
1
+ import { type Command, type CommandArgs, type Fill, type PaintProps, type RenderContext2D, type Vector2 } from "@motionscript/sdk";
2
+ import type { CoordinateMap } from "./coords.js";
3
+ import type { ExpressionScope } from "./expression.js";
4
+ import { Mark } from "./mark.js";
5
+ import { type PlotSource } from "./source.js";
6
+ export interface FieldProps extends PaintProps {
7
+ expression?: string | PlotSource;
8
+ params?: ExpressionScope;
9
+ strengthFills?: Fill[];
10
+ maxMagnitude?: number;
11
+ }
12
+ /** A field's vector at a graph point, written into `out`; `false` where it has none. */
13
+ export type FieldSampler = (x: number, y: number, out: Vector2) => boolean;
14
+ /** One thing a field draws, and how strong the field is there: what it is coloured by. */
15
+ export interface FieldStroke {
16
+ runs: Vector2[][];
17
+ magnitude: number;
18
+ }
19
+ /**
20
+ * A mark drawn from a vector field, `(P(x, y), Q(x, y))`: arrows, or the lines
21
+ * that follow it, all in one stroke. Without strength fills it is one path in
22
+ * the stroke's paint; with them, what it draws is sorted by the field's
23
+ * strength into a few shades.
24
+ */
25
+ export declare abstract class FieldMark<P extends FieldProps = FieldProps> extends Mark<P> {
26
+ expression: PlotSource;
27
+ /** The equation's named values: any name in it that is not `x`, `y`, a function or a constant. */
28
+ params: ExpressionScope;
29
+ /** Paints by the field's strength, weakest first. Empty draws everything in the stroke's own paint. */
30
+ strengthFills: Fill[];
31
+ /** The strength the last paint stands for; zero takes the strongest the field is where it is drawn. */
32
+ maxMagnitude: number;
33
+ /** Change the field, each vector turning from the one into the other. */
34
+ morphTo(args: CommandArgs<{
35
+ expression: string;
36
+ }> & {
37
+ duration: number;
38
+ }): Command<P>;
39
+ /** Animate the equation's named values. */
40
+ paramsTo(args: CommandArgs<{
41
+ params: ExpressionScope;
42
+ }> & {
43
+ duration: number;
44
+ }): Command<P>;
45
+ /** What is drawn of `field` through `map`. */
46
+ protected abstract strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[];
47
+ /** The props of its own that change what {@link strokes} returns. */
48
+ protected abstract own(): string;
49
+ protected trace(map: CoordinateMap): Vector2[][];
50
+ protected renderInk(ctx: RenderContext2D): void;
51
+ private _key;
52
+ private _drawn;
53
+ private _runs;
54
+ /** The field at a graph point, measured from this mark's own shift, mid-morph a blend of two. */
55
+ private _sampler;
56
+ private _field;
57
+ }
58
+ //# sourceMappingURL=field-mark.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"field-mark.d.ts","sourceRoot":"","sources":["../../src/kit/field-mark.ts"],"names":[],"mappings":"AAAA,OAAO,EAKH,KAAK,OAAO,EACZ,KAAK,WAAW,EAChB,KAAK,IAAI,EAET,KAAK,UAAU,EACf,KAAK,eAAe,EAEpB,KAAK,OAAO,EACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OA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,CAAC,CAAC;IAI3F,8CAA8C;IAC9C,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,aAAa,EAAE,KAAK,EAAE,YAAY,GAAG,WAAW,EAAE;IAElF,qEAAqE;IACrE,SAAS,CAAC,QAAQ,CAAC,GAAG,IAAI,MAAM;IAEhC,SAAS,CAAC,KAAK,CAAC,GAAG,EAAE,aAAa,GAAG,OAAO,EAAE,EAAE;cAe7B,SAAS,CAAC,GAAG,EAAE,eAAe,GAAG,IAAI;IA0BxD,OAAO,CAAC,IAAI,CAAM;IAClB,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,KAAK,CAAmB;IAEhC,iGAAiG;IACjG,OAAO,CAAC,QAAQ;IAehB,OAAO,CAAC,MAAM;CAcjB"}
@@ -0,0 +1,129 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { command, Graphics2D, property, withdraw, } from "@motionscript/sdk";
8
+ import { fieldParams, parseField } from "./field.js";
9
+ import { boundsOfRuns, runsToPath, stated } from "./geometry.js";
10
+ import { Mark } from "./mark.js";
11
+ import { fillAt, lerpFillScale, readFillScale } from "./scale.js";
12
+ import { lerpParams, lerpSource, readSource, scopeFor } from "./source.js";
13
+ import { FILL_PAINT, OUTLINE_TRIM } from "./withdrawn.js";
14
+ /** How many shades the strength fills are drawn in: each is one path. */
15
+ const SHADES = 12;
16
+ /**
17
+ * A mark drawn from a vector field, `(P(x, y), Q(x, y))`: arrows, or the lines
18
+ * that follow it, all in one stroke. Without strength fills it is one path in
19
+ * the stroke's paint; with them, what it draws is sorted by the field's
20
+ * strength into a few shades.
21
+ */
22
+ let FieldMark = class FieldMark extends Mark {
23
+ /** Change the field, each vector turning from the one into the other. */
24
+ morphTo(args) {
25
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
26
+ }
27
+ /** Animate the equation's named values. */
28
+ paramsTo(args) {
29
+ return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
30
+ }
31
+ trace(map) {
32
+ const { area } = map;
33
+ if (!Number.isFinite(area.maxX - area.minX))
34
+ return [];
35
+ const { source, blend } = this.expression;
36
+ const shift = this.shift();
37
+ const key = [source, blend?.from, blend?.t, map.revision, JSON.stringify(this.params), shift.x, shift.y, this.own()].join("|");
38
+ if (key !== this._key) {
39
+ this._key = key;
40
+ const field = this._sampler(shift);
41
+ this._drawn = field ? this.strokes(map, field) : [];
42
+ this._runs = this._drawn.flatMap((stroke) => stroke.runs);
43
+ }
44
+ return this._runs;
45
+ }
46
+ renderInk(ctx) {
47
+ const scale = this.strengthFills;
48
+ const stroke = this.stroke;
49
+ const ink = scale.length > 0 && stroke.length > 0 ? boundsOfRuns(this._runs) : null;
50
+ if (!ink) {
51
+ super.renderInk(ctx);
52
+ return;
53
+ }
54
+ const top = this.maxMagnitude > 0 ? this.maxMagnitude : this._drawn.reduce((most, drawn) => Math.max(most, drawn.magnitude), 0);
55
+ const shades = Array.from({ length: SHADES }, () => []);
56
+ for (const drawn of this._drawn) {
57
+ const share = top > 0 ? Math.min(1, drawn.magnitude / top) : 0;
58
+ shades[Math.min(SHADES - 1, Math.floor(share * SHADES))].push(...drawn.runs);
59
+ }
60
+ shades.forEach((runs, index) => {
61
+ if (runs.length === 0)
62
+ return;
63
+ const fill = fillAt(scale, (index + 0.5) / SHADES);
64
+ ctx.draw(new Graphics2D()
65
+ .path({ data: runsToPath(runs), centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY], start: this.start, end: this.end })
66
+ .shadow(this.shadow)
67
+ .stroke(stroke.map((layer) => ({ ...layer, fill }))));
68
+ });
69
+ }
70
+ _key = "";
71
+ _drawn = [];
72
+ _runs = [];
73
+ /** The field at a graph point, measured from this mark's own shift, mid-morph a blend of two. */
74
+ _sampler(shift) {
75
+ const { source, blend } = this.expression;
76
+ const to = this._field(source, shift);
77
+ if (!to || !blend)
78
+ return to;
79
+ const from = this._field(blend.from, shift);
80
+ if (!from)
81
+ return to;
82
+ const start = { x: 0, y: 0 };
83
+ return (x, y, out) => {
84
+ if (!from(x, y, start) || !to(x, y, out))
85
+ return false;
86
+ out.x = start.x + (out.x - start.x) * blend.t;
87
+ out.y = start.y + (out.y - start.y) * blend.t;
88
+ return true;
89
+ };
90
+ }
91
+ _field(source, shift) {
92
+ const names = fieldParams(source);
93
+ const field = parseField(source, names);
94
+ if (field instanceof Error)
95
+ return null;
96
+ const params = scopeFor(names, this.params);
97
+ const { x, y } = field;
98
+ return (gx, gy, out) => {
99
+ const vx = x(gx - shift.x, gy - shift.y, params);
100
+ const vy = y(gx - shift.x, gy - shift.y, params);
101
+ out.x = vx;
102
+ out.y = vy;
103
+ return Number.isFinite(vx) && Number.isFinite(vy);
104
+ };
105
+ }
106
+ };
107
+ __decorate([
108
+ property({ default: "", mapper: readSource, tween: lerpSource, kind: "fieldEquation" })
109
+ ], FieldMark.prototype, "expression", void 0);
110
+ __decorate([
111
+ property({ default: {}, tween: lerpParams })
112
+ ], FieldMark.prototype, "params", void 0);
113
+ __decorate([
114
+ property({ default: [], mapper: readFillScale, tween: lerpFillScale, kind: "vector" })
115
+ ], FieldMark.prototype, "strengthFills", void 0);
116
+ __decorate([
117
+ property({ default: 0, min: 0, step: 0.1 })
118
+ ], FieldMark.prototype, "maxMagnitude", void 0);
119
+ __decorate([
120
+ command({ args: ["expression"] })
121
+ ], FieldMark.prototype, "morphTo", null);
122
+ __decorate([
123
+ command({ args: ["params"] })
124
+ ], FieldMark.prototype, "paramsTo", null);
125
+ FieldMark = __decorate([
126
+ withdraw(...FILL_PAINT, ...OUTLINE_TRIM)
127
+ ], FieldMark);
128
+ export { FieldMark };
129
+ //# sourceMappingURL=field-mark.js.map