topotopo 0.3.0 → 0.5.0

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 (354) hide show
  1. package/NOTICE +40 -10
  2. package/README.md +475 -111
  3. package/dist/cli.js +328 -133
  4. package/dist/config.d.ts +48 -0
  5. package/dist/config.js +170 -0
  6. package/dist/core/analysis.d.ts +5 -1
  7. package/dist/core/analysis.js +8 -3
  8. package/dist/core/bake.d.ts +16 -8
  9. package/dist/core/bake.js +165 -29
  10. package/dist/core/bevel.d.ts +3 -1
  11. package/dist/core/bevel.js +238 -97
  12. package/dist/core/decimate.d.ts +65 -0
  13. package/dist/core/decimate.js +409 -3
  14. package/dist/core/erosion.d.ts +37 -0
  15. package/dist/core/erosion.js +41 -1
  16. package/dist/core/geometry.d.ts +5 -1
  17. package/dist/core/geometry.js +204 -4
  18. package/dist/core/heightfield.d.ts +27 -4
  19. package/dist/core/heightfield.js +54 -16
  20. package/dist/core/imageops.d.ts +6 -0
  21. package/dist/core/imageops.js +51 -0
  22. package/dist/core/intrinsics.d.ts +7 -0
  23. package/dist/core/intrinsics.js +94 -0
  24. package/dist/core/manifold.d.ts +12 -1
  25. package/dist/core/manifold.js +78 -6
  26. package/dist/core/materials.d.ts +11 -0
  27. package/dist/core/materials.js +56 -0
  28. package/dist/core/noise.d.ts +9 -0
  29. package/dist/core/noise.js +10 -1
  30. package/dist/core/packed.js +11 -0
  31. package/dist/core/particles.d.ts +1 -1
  32. package/dist/core/particles.js +1 -1
  33. package/dist/core/polymerge.d.ts +21 -0
  34. package/dist/core/polymerge.js +327 -0
  35. package/dist/core/rbd.js +13 -1
  36. package/dist/core/repair.d.ts +2 -0
  37. package/dist/core/repair.js +2 -0
  38. package/dist/core/report.d.ts +70 -0
  39. package/dist/core/report.js +275 -0
  40. package/dist/core/scatter.d.ts +1 -1
  41. package/dist/core/scatter.js +1 -1
  42. package/dist/core/scene.d.ts +20 -0
  43. package/dist/core/scene.js +72 -0
  44. package/dist/core/sha256.d.ts +4 -0
  45. package/dist/core/sha256.js +69 -0
  46. package/dist/core/shapes.d.ts +3 -0
  47. package/dist/core/shapes.js +34 -0
  48. package/dist/core/snippet-lib.d.ts +15 -2
  49. package/dist/core/snippet-lib.js +40 -14
  50. package/dist/core/solidify.d.ts +38 -0
  51. package/dist/core/solidify.js +356 -0
  52. package/dist/core/tangents.d.ts +54 -0
  53. package/dist/core/tangents.js +218 -0
  54. package/dist/core/texcoord.d.ts +4 -0
  55. package/dist/core/texcoord.js +14 -0
  56. package/dist/core/texnoise.d.ts +6 -1
  57. package/dist/core/texnoise.js +6 -2
  58. package/dist/core/texsdf.d.ts +131 -0
  59. package/dist/core/texsdf.js +637 -0
  60. package/dist/core/texture.d.ts +163 -9
  61. package/dist/core/texture.js +844 -20
  62. package/dist/core/topology.d.ts +32 -0
  63. package/dist/core/topology.js +238 -7
  64. package/dist/core/traceimage.d.ts +7 -0
  65. package/dist/core/traceimage.js +221 -0
  66. package/dist/core/tritri.d.ts +5 -0
  67. package/dist/core/tritri.js +120 -0
  68. package/dist/core/unwrap.d.ts +72 -0
  69. package/dist/core/unwrap.js +800 -0
  70. package/dist/core/usercode.d.ts +27 -0
  71. package/dist/core/usercode.js +87 -0
  72. package/dist/core/uv.d.ts +15 -16
  73. package/dist/core/uv.js +36 -189
  74. package/dist/core/uvislands.d.ts +62 -0
  75. package/dist/core/uvislands.js +574 -0
  76. package/dist/core/uvoverlap.d.ts +58 -0
  77. package/dist/core/uvoverlap.js +220 -0
  78. package/dist/core/uvpack.d.ts +64 -0
  79. package/dist/core/uvpack.js +628 -0
  80. package/dist/core/uvreport.d.ts +127 -0
  81. package/dist/core/uvreport.js +372 -0
  82. package/dist/core/wrangle.d.ts +6 -1
  83. package/dist/core/wrangle.js +27 -16
  84. package/dist/core/xatlas.d.ts +16 -1
  85. package/dist/core/xatlas.js +95 -61
  86. package/dist/engines.d.ts +63 -0
  87. package/dist/engines.js +180 -0
  88. package/dist/explain.d.ts +4 -3
  89. package/dist/explain.js +129 -12
  90. package/dist/graph/engine.d.ts +92 -29
  91. package/dist/graph/engine.js +298 -148
  92. package/dist/graph/expr.d.ts +6 -3
  93. package/dist/graph/expr.js +18 -8
  94. package/dist/graph/outputs.d.ts +23 -0
  95. package/dist/graph/outputs.js +41 -0
  96. package/dist/graph/packages.d.ts +72 -0
  97. package/dist/graph/packages.js +261 -0
  98. package/dist/graph/parallel.d.ts +3 -1
  99. package/dist/graph/parallel.js +4 -1
  100. package/dist/graph/params.d.ts +6 -0
  101. package/dist/graph/params.js +34 -7
  102. package/dist/graph/scope.d.ts +55 -0
  103. package/dist/graph/scope.js +14 -0
  104. package/dist/graph/types.d.ts +75 -12
  105. package/dist/graph/types.js +4 -2
  106. package/dist/graph/vars.d.ts +16 -0
  107. package/dist/graph/vars.js +80 -0
  108. package/dist/host/artifacts.d.ts +10 -1
  109. package/dist/host/artifacts.js +37 -6
  110. package/dist/host/cook.d.ts +241 -0
  111. package/dist/host/cook.js +844 -0
  112. package/dist/host/export-targets.d.ts +42 -5
  113. package/dist/host/export-targets.js +72 -13
  114. package/dist/host/godot-commands.d.ts +4 -0
  115. package/dist/host/godot-commands.js +21 -0
  116. package/dist/host/godot.d.ts +183 -0
  117. package/dist/host/godot.js +916 -0
  118. package/dist/host/node.d.ts +12 -7
  119. package/dist/host/node.js +27 -9
  120. package/dist/host/pool.js +51 -2
  121. package/dist/host/sidecars.d.ts +8 -0
  122. package/dist/host/sidecars.js +50 -0
  123. package/dist/host/types.d.ts +19 -6
  124. package/dist/host/version.d.ts +1 -0
  125. package/dist/host/version.js +3 -0
  126. package/dist/host/worker.js +5 -2
  127. package/dist/index.d.ts +5 -3
  128. package/dist/index.js +3 -2
  129. package/dist/inspect.d.ts +84 -1
  130. package/dist/inspect.js +271 -24
  131. package/dist/io/common.d.ts +33 -5
  132. package/dist/io/export.js +2 -1
  133. package/dist/io/files.d.ts +23 -4
  134. package/dist/io/files.js +60 -8
  135. package/dist/io/gltf.js +355 -51
  136. package/dist/io/heightmap.d.ts +3 -0
  137. package/dist/io/heightmap.js +1 -0
  138. package/dist/io/import.js +10 -0
  139. package/dist/io/inspect-scene.d.ts +55 -0
  140. package/dist/io/inspect-scene.js +155 -0
  141. package/dist/io/kinds.d.ts +81 -0
  142. package/dist/io/kinds.js +163 -0
  143. package/dist/io/points.d.ts +2 -1
  144. package/dist/io/points.js +3 -1
  145. package/dist/io/sidecars.d.ts +24 -0
  146. package/dist/io/sidecars.js +57 -0
  147. package/dist/io/source.d.ts +65 -0
  148. package/dist/io/source.js +112 -0
  149. package/dist/io/targets.d.ts +47 -3
  150. package/dist/io/targets.js +149 -14
  151. package/dist/io/textures.js +21 -1
  152. package/dist/io/unreal.js +5 -1
  153. package/dist/mcp/docs.d.ts +10 -1
  154. package/dist/mcp/docs.js +816 -119
  155. package/dist/mcp/inputs.d.ts +12 -5
  156. package/dist/mcp/inputs.js +50 -13
  157. package/dist/mcp/server.js +70 -24
  158. package/dist/mcp/tools.d.ts +14 -7
  159. package/dist/mcp/tools.js +207 -108
  160. package/dist/mcp/verify.d.ts +2 -2
  161. package/dist/mcp/verify.js +1 -1
  162. package/dist/mcp/workspace.d.ts +15 -0
  163. package/dist/mcp/workspace.js +349 -0
  164. package/dist/nodes/attributes.js +198 -1
  165. package/dist/nodes/bake.js +207 -83
  166. package/dist/nodes/beam.d.ts +1 -0
  167. package/dist/nodes/beam.js +201 -0
  168. package/dist/nodes/bevel.js +54 -5
  169. package/dist/nodes/cloth.js +2 -0
  170. package/dist/nodes/code.js +22 -10
  171. package/dist/nodes/curves.js +440 -93
  172. package/dist/nodes/flow.js +53 -33
  173. package/dist/nodes/generate.js +13 -39
  174. package/dist/nodes/heightfield.js +248 -77
  175. package/dist/nodes/imageops.d.ts +1 -0
  176. package/dist/nodes/imageops.js +203 -0
  177. package/dist/nodes/index.d.ts +4 -0
  178. package/dist/nodes/index.js +4 -0
  179. package/dist/nodes/material.js +91 -23
  180. package/dist/nodes/measure.js +45 -18
  181. package/dist/nodes/particles.js +2 -0
  182. package/dist/nodes/pcg.js +6 -5
  183. package/dist/nodes/procedural.js +4 -3
  184. package/dist/nodes/rbd.js +1 -1
  185. package/dist/nodes/remesh.js +71 -15
  186. package/dist/nodes/scene.d.ts +1 -0
  187. package/dist/nodes/scene.js +55 -0
  188. package/dist/nodes/sdf.js +10 -5
  189. package/dist/nodes/solid.js +20 -5
  190. package/dist/nodes/solidify.d.ts +1 -0
  191. package/dist/nodes/solidify.js +52 -0
  192. package/dist/nodes/splines.d.ts +15 -1
  193. package/dist/nodes/splines.js +112 -105
  194. package/dist/nodes/texture.d.ts +21 -6
  195. package/dist/nodes/texture.js +1318 -185
  196. package/dist/nodes/topology.d.ts +7 -0
  197. package/dist/nodes/topology.js +133 -6
  198. package/dist/nodes/transform.js +11 -3
  199. package/dist/nodes/util.d.ts +1 -1
  200. package/dist/nodes/uv.js +192 -174
  201. package/dist/nodes/wasmcall.js +6 -2
  202. package/dist/open.d.ts +19 -0
  203. package/dist/open.js +12 -0
  204. package/dist/package.d.ts +288 -0
  205. package/dist/package.js +772 -0
  206. package/dist/packages/check.d.ts +32 -0
  207. package/dist/packages/check.js +169 -0
  208. package/dist/packages/commands.d.ts +57 -0
  209. package/dist/packages/commands.js +537 -0
  210. package/dist/packages/registry.d.ts +796 -0
  211. package/dist/packages/registry.js +2998 -0
  212. package/dist/packages/update.d.ts +124 -0
  213. package/dist/packages/update.js +476 -0
  214. package/dist/render/options.d.ts +2 -0
  215. package/dist/render/options.js +44 -5
  216. package/dist/render/pbr.d.ts +28 -0
  217. package/dist/render/pbr.js +378 -0
  218. package/dist/render/preview.d.ts +69 -0
  219. package/dist/render/preview.js +26 -0
  220. package/dist/render/raster.d.ts +78 -7
  221. package/dist/render/raster.js +600 -145
  222. package/dist/render/texview.d.ts +7 -1
  223. package/dist/render/texview.js +34 -2
  224. package/dist/scan.d.ts +61 -0
  225. package/dist/scan.js +228 -0
  226. package/dist/session.d.ts +311 -0
  227. package/dist/session.js +849 -0
  228. package/dist/viewer/app/api.js +48 -6
  229. package/dist/viewer/app/camera.js +1 -1
  230. package/dist/viewer/app/cooking.js +78 -0
  231. package/dist/viewer/app/embed.js +100 -0
  232. package/dist/viewer/app/export-popover.js +118 -16
  233. package/dist/viewer/app/grid.js +6 -2
  234. package/dist/viewer/app/guides.js +123 -1
  235. package/dist/viewer/app/keymap.js +7 -4
  236. package/dist/viewer/app/layers.js +5 -5
  237. package/dist/viewer/app/main.js +186 -53
  238. package/dist/viewer/app/markers.js +61 -1
  239. package/dist/viewer/app/outliner.js +72 -27
  240. package/dist/viewer/app/output-info.js +45 -0
  241. package/dist/viewer/app/outputs.css +147 -8
  242. package/dist/viewer/app/outputs.js +168 -54
  243. package/dist/viewer/app/packages.js +614 -0
  244. package/dist/viewer/app/props.js +177 -53
  245. package/dist/viewer/app/scene.js +35 -6
  246. package/dist/viewer/app/select.js +5 -4
  247. package/dist/viewer/app/selection.js +1 -1
  248. package/dist/viewer/app/status.js +12 -15
  249. package/dist/viewer/app/style.css +66 -8
  250. package/dist/viewer/app/units.js +1 -1
  251. package/dist/viewer/app/vars.js +48 -1
  252. package/dist/viewer/app/welcome.js +72 -0
  253. package/dist/viewer/graph-file.d.ts +62 -0
  254. package/dist/viewer/graph-file.js +175 -0
  255. package/dist/viewer/index.html +24 -11
  256. package/dist/viewer/json-edit.d.ts +13 -0
  257. package/dist/viewer/json-edit.js +274 -0
  258. package/dist/viewer/outputs.d.ts +49 -5
  259. package/dist/viewer/outputs.js +264 -56
  260. package/dist/viewer/packages.d.ts +121 -0
  261. package/dist/viewer/packages.js +584 -0
  262. package/dist/viewer/server.d.ts +24 -7
  263. package/dist/viewer/server.js +289 -31
  264. package/dist/web/app/web.css +419 -0
  265. package/dist/web/app/web.js +188 -0
  266. package/dist/web/bake.d.ts +34 -0
  267. package/dist/web/bake.js +153 -0
  268. package/dist/web/folder-page.d.ts +101 -0
  269. package/dist/web/folder-page.js +146 -0
  270. package/dist/web/folders.d.ts +280 -0
  271. package/dist/web/folders.js +970 -0
  272. package/dist/web/library.d.ts +91 -0
  273. package/dist/web/library.js +228 -0
  274. package/dist/web/markdown.d.ts +45 -0
  275. package/dist/web/markdown.js +677 -0
  276. package/dist/web/pages.d.ts +258 -0
  277. package/dist/web/pages.js +301 -0
  278. package/dist/web/server.d.ts +24 -0
  279. package/dist/web/server.js +1068 -0
  280. package/dist/workspace.d.ts +88 -105
  281. package/dist/workspace.js +359 -375
  282. package/integrations/godot/addons/topotopo/LICENSE +202 -0
  283. package/integrations/godot/addons/topotopo/README.md +57 -0
  284. package/integrations/godot/addons/topotopo/context_menu.gd +43 -0
  285. package/integrations/godot/addons/topotopo/dock.gd +266 -0
  286. package/integrations/godot/addons/topotopo/dock.tscn +72 -0
  287. package/integrations/godot/addons/topotopo/export_filter.gd +72 -0
  288. package/integrations/godot/addons/topotopo/gltf_instancing.gd +131 -0
  289. package/integrations/godot/addons/topotopo/icons/topotopo.svg +1 -0
  290. package/integrations/godot/addons/topotopo/inspector.gd +38 -0
  291. package/integrations/godot/addons/topotopo/instances_post_import.gd +111 -0
  292. package/integrations/godot/addons/topotopo/package.gd +23 -0
  293. package/integrations/godot/addons/topotopo/package_importer.gd +187 -0
  294. package/integrations/godot/addons/topotopo/plugin.cfg +7 -0
  295. package/integrations/godot/addons/topotopo/plugin.gd +970 -0
  296. package/integrations/godot/addons/topotopo/record.gd +435 -0
  297. package/integrations/godot/addons/topotopo/runner.gd +458 -0
  298. package/integrations/godot/addons/topotopo/tools/check.gd +42 -0
  299. package/integrations/godot/addons/topotopo/watcher.gd +199 -0
  300. package/kernels/pkg/README.md +23 -29
  301. package/kernels/pkg-simd/README.md +23 -29
  302. package/library/architecture/versions/1.0/architecture.topo +12 -0
  303. package/{shelf → library/architecture/versions/1.0/graphs}/arch.json +3 -3
  304. package/{shelf → library/architecture/versions/1.0/graphs}/beam_truss.json +6 -6
  305. package/{shelf → library/architecture/versions/1.0/graphs}/brick_wall.json +8 -8
  306. package/{shelf → library/architecture/versions/1.0/graphs}/door_frame.json +3 -3
  307. package/{shelf → library/architecture/versions/1.0/graphs}/hex_grid_floor.json +3 -2
  308. package/{shelf → library/architecture/versions/1.0/graphs}/house.json +8 -8
  309. package/library/architecture/versions/1.0/graphs/lineup.json +42 -0
  310. package/{shelf → library/architecture/versions/1.0/graphs}/pillar.json +3 -2
  311. package/{shelf → library/architecture/versions/1.0/graphs}/railing.json +5 -5
  312. package/{shelf → library/architecture/versions/1.0/graphs}/roof_gable.json +2 -2
  313. package/{shelf → library/architecture/versions/1.0/graphs}/roof_hip.json +2 -2
  314. package/{shelf → library/architecture/versions/1.0/graphs}/roof_skeleton.json +2 -1
  315. package/{shelf → library/architecture/versions/1.0/graphs}/scaffold.json +4 -4
  316. package/{shelf → library/architecture/versions/1.0/graphs}/spiral_stairs.json +3 -2
  317. package/{shelf → library/architecture/versions/1.0/graphs}/stairs.json +6 -6
  318. package/{shelf → library/architecture/versions/1.0/graphs}/tile_floor.json +3 -2
  319. package/{shelf → library/architecture/versions/1.0/graphs}/wall_segment.json +10 -10
  320. package/{shelf → library/architecture/versions/1.0/graphs}/window_frame.json +2 -2
  321. package/library/architecture/versions/1.0/version.json +1119 -0
  322. package/{shelf → library/nature/versions/1.0/graphs}/bush.json +2 -2
  323. package/{shelf → library/nature/versions/1.0/graphs}/cliff_block.json +4 -3
  324. package/{shelf → library/nature/versions/1.0/graphs}/forest_patch.json +5 -5
  325. package/{shelf → library/nature/versions/1.0/graphs}/grass_clumps.json +2 -2
  326. package/library/nature/versions/1.0/graphs/lineup.json +24 -0
  327. package/{shelf → library/nature/versions/1.0/graphs}/log.json +6 -6
  328. package/{shelf → library/nature/versions/1.0/graphs}/rock.json +5 -4
  329. package/{shelf → library/nature/versions/1.0/graphs}/scatter_rocks.json +5 -5
  330. package/{shelf → library/nature/versions/1.0/graphs}/tree_lowpoly.json +3 -2
  331. package/library/nature/versions/1.0/nature.topo +12 -0
  332. package/library/nature/versions/1.0/version.json +534 -0
  333. package/{shelf → library/props/versions/1.0/graphs}/barrel.json +5 -4
  334. package/{shelf → library/props/versions/1.0/graphs}/crate.json +3 -3
  335. package/library/props/versions/1.0/graphs/lineup.json +18 -0
  336. package/{shelf → library/props/versions/1.0/graphs}/pallet.json +4 -3
  337. package/{shelf → library/props/versions/1.0/graphs}/pipe_run.json +3 -3
  338. package/{shelf → library/props/versions/1.0/graphs}/sandbag_wall.json +5 -5
  339. package/library/props/versions/1.0/props.topo +12 -0
  340. package/library/props/versions/1.0/version.json +336 -0
  341. package/{shelf → library/street/versions/1.0/graphs}/bench.json +5 -5
  342. package/{shelf → library/street/versions/1.0/graphs}/bollard.json +2 -1
  343. package/{shelf → library/street/versions/1.0/graphs}/cable.json +4 -4
  344. package/{shelf → library/street/versions/1.0/graphs}/fence.json +7 -7
  345. package/{shelf → library/street/versions/1.0/graphs}/lamp_post.json +2 -2
  346. package/library/street/versions/1.0/graphs/lineup.json +22 -0
  347. package/{shelf → library/street/versions/1.0/graphs}/road_strip.json +2 -2
  348. package/{shelf → library/street/versions/1.0/graphs}/sign_post.json +1 -1
  349. package/library/street/versions/1.0/street.topo +12 -0
  350. package/library/street/versions/1.0/version.json +421 -0
  351. package/package.json +23 -8
  352. package/dist/graph/migrate.d.ts +0 -23
  353. package/dist/graph/migrate.js +0 -58
  354. package/shelf/index.json +0 -190
@@ -1,3 +1,4 @@
1
+ import type { Host } from '../host/types.ts';
1
2
  /**
2
3
  * Library functions that go through the wrangle's geometry API (`C()` below): they read other elements or
3
4
  * inputs, edit the geometry or log. A point wrangle that calls none of them can run in chunks on worker threads
@@ -10,19 +11,31 @@ export declare const GEOMETRY_FUNCTIONS: string[];
10
11
  * global property (a setter that never returns), and the engine sets the API outside the time limit.
11
12
  */
12
13
  export declare function installSnippetLib(g: any, host: any): (api: any) => void;
14
+ /** The names the snippet library installs (the helpers of the scripting realm), read off a stand-in global object. */
15
+ export declare const SNIPPET_LIB_NAMES: readonly string[];
16
+ /**
17
+ * What code that still reads graph vars as `vars.height` gets for `vars`: an object whose every use fails with what to
18
+ * write instead ("vars.height: read graph vars by their bare name: height"). Graph vars are bound by their bare name
19
+ * (src/core/usercode.ts). Self-contained like installSnippetLib: each realm compiles it from its source text and calls
20
+ * it before any user code, so it holds that realm's ReferenceError and Proxy.
21
+ */
22
+ export declare function makeVarsGuard(): object;
13
23
  /** Name of the scripting realm's control object (a global `const` declared by SCRIPT_REALM_SETUP). */
14
24
  export declare const REALM_CONTROL = "__topotopo";
25
+ /** The scripting realm's `vars` guard (makeVarsGuard), for code that binds graph vars by name. */
26
+ export declare function scriptVarsGuard(host: Host): object;
15
27
  /**
16
28
  * The script a host runs once in a new scripting realm, before any user code, with its primitives (rand, noise, curl)
17
29
  * on the realm's global `__hostApi` (deleted again). It installs the snippet library and declares the realm's control
18
- * object as a global `const` (REALM_CONTROL), a frozen object of realm functions:
30
+ * object as a global `const` (REALM_CONTROL), a frozen object of realm functions and values:
19
31
  * run() calls the current job: the host's timed script is `__topotopo.run()` (Host.runGuarded)
20
32
  * setJob(f) sets the current job
21
33
  * setCur(api) sets the current wrangle's geometry API (wrangle.ts; null after the run)
34
+ * varsGuard what code gets for `vars` (makeVarsGuard)
22
35
  * The host keeps the object and calls setJob/setCur directly, outside the time limit, so they must not run user code:
23
36
  * the job and the API are closure variables, the object is frozen (own data properties only) and a global `const`
24
37
  * can be neither reassigned nor redefined by later code (user code never runs at the realm's top level, and a
25
- * property of the global object of the same name is shadowed by the binding). Before R-18 the host assigned
38
+ * property of the global object of the same name is shadowed by the binding). Previously the host assigned
26
39
  * global properties `__job` and `__cur`, and a script that gave them a looping setter hung the process.
27
40
  */
28
41
  export declare const SCRIPT_REALM_SETUP: string;
@@ -1,20 +1,10 @@
1
- // The function below is stringified and evaluated INSIDE the scripting realm, so it
2
- // must be completely self-contained: no imports, no references to outer scope.
3
- // It installs VEX-flavoured helpers as globals. Numeric results cross realms freely;
4
- // arrays are always created here so they get the realm's Array.prototype accessors
5
- // (.x .y .z .w / .r .g .b .a / .u .v).
6
- //
7
- // `host` supplies number-returning primitives implemented in the engine realm
8
- // (noise, hashing). Geometry query/edit functions dispatch through the current wrangle's
9
- // geometry API, a closure variable of the library that the engine sets for each wrangle run
10
- // through the realm's control object (see SCRIPT_REALM_SETUP).
11
1
  /**
12
2
  * Library functions that go through the wrangle's geometry API (`C()` below): they read other elements or
13
3
  * inputs, edit the geometry or log. A point wrangle that calls none of them can run in chunks on worker threads
14
4
  * (wrangle.ts runWrangleParallel); test/pool.ts checks this list against the functions installed below.
15
5
  */
16
6
  export const GEOMETRY_FUNCTIONS = [
17
- 'npoints', 'nprims', 'nvertices', 'point', 'prim', 'vertex', 'detail', 'haspointattrib', 'hasprimattrib', 'primpoints',
7
+ 'npoints', 'nprims', 'nvertices', 'point', 'prim', 'primintrinsic', 'vertex', 'detail', 'haspointattrib', 'hasprimattrib', 'primpoints',
18
8
  'pointprims', 'neighbours', 'neighbors', 'neighbourcount', 'primvertexcount', 'nearpoint', 'nearpoints', 'xyzdist', 'minpos',
19
9
  'closest', 'intersect', 'inpointgroup', 'inprimgroup', 'invertexgroup', 'inedgegroup', 'pointattribtype', 'addpoint',
20
10
  'addprim', 'removepoint', 'removeprim', 'setpointattrib', 'setprimattrib', 'setvertexattrib', 'setdetailattrib',
@@ -314,6 +304,8 @@ export function installSnippetLib(g, host) {
314
304
  L.nvertices = (i = 0) => C().nvertices(i);
315
305
  L.point = (i, name, pt) => toArr(C().attr(i, 'point', name, pt));
316
306
  L.prim = (i, name, pr) => toArr(C().attr(i, 'prim', name, pr));
307
+ // read-only values computed per prim: bounds, typename, vertexcount, closed; packed: packedbounds, transform, … (intrinsics.ts)
308
+ L.primintrinsic = (i, name, pr) => toArr(C().primintrinsic(i, name, pr));
317
309
  L.vertex = (i, name, v) => toArr(C().attr(i, 'vertex', name, v));
318
310
  L.detail = (i, name) => toArr(C().attr(i, 'detail', name, 0));
319
311
  L.haspointattrib = (i, name) => C().hasattr(i, 'point', name);
@@ -461,24 +453,58 @@ export function installSnippetLib(g, host) {
461
453
  g.__snippetLibNames = Object.keys(L).filter((k) => !k.startsWith('__'));
462
454
  return (api) => { cur = api; };
463
455
  }
456
+ /** The names the snippet library installs (the helpers of the scripting realm), read off a stand-in global object. */
457
+ export const SNIPPET_LIB_NAMES = (() => {
458
+ const g = { Array: { prototype: {} } };
459
+ installSnippetLib(g, {});
460
+ return Object.freeze(g.__snippetLibNames.slice());
461
+ })();
462
+ /**
463
+ * What code that still reads graph vars as `vars.height` gets for `vars`: an object whose every use fails with what to
464
+ * write instead ("vars.height: read graph vars by their bare name: height"). Graph vars are bound by their bare name
465
+ * (src/core/usercode.ts). Self-contained like installSnippetLib: each realm compiles it from its source text and calls
466
+ * it before any user code, so it holds that realm's ReferenceError and Proxy.
467
+ */
468
+ export function makeVarsGuard() {
469
+ 'use strict';
470
+ const RE = ReferenceError;
471
+ const fail = (k) => {
472
+ throw new RE(typeof k === 'string'
473
+ ? 'vars.' + k + ': read graph vars by their bare name: ' + k
474
+ : 'vars: read graph vars by their bare name (height, not vars.height)');
475
+ };
476
+ const any = () => fail(undefined);
477
+ const byKey = (_t, k) => fail(k);
478
+ const handler = Object.freeze({
479
+ get: byKey, has: byKey, set: byKey, deleteProperty: byKey, defineProperty: byKey, getOwnPropertyDescriptor: byKey,
480
+ ownKeys: any, getPrototypeOf: any, setPrototypeOf: any, isExtensible: any, preventExtensions: any,
481
+ });
482
+ return new Proxy(Object.freeze(Object.create(null)), handler);
483
+ }
464
484
  /** Name of the scripting realm's control object (a global `const` declared by SCRIPT_REALM_SETUP). */
465
485
  export const REALM_CONTROL = '__topotopo';
486
+ /** The scripting realm's `vars` guard (makeVarsGuard), for code that binds graph vars by name. */
487
+ export function scriptVarsGuard(host) {
488
+ return host.compile(REALM_CONTROL).varsGuard;
489
+ }
466
490
  /**
467
491
  * The script a host runs once in a new scripting realm, before any user code, with its primitives (rand, noise, curl)
468
492
  * on the realm's global `__hostApi` (deleted again). It installs the snippet library and declares the realm's control
469
- * object as a global `const` (REALM_CONTROL), a frozen object of realm functions:
493
+ * object as a global `const` (REALM_CONTROL), a frozen object of realm functions and values:
470
494
  * run() calls the current job: the host's timed script is `__topotopo.run()` (Host.runGuarded)
471
495
  * setJob(f) sets the current job
472
496
  * setCur(api) sets the current wrangle's geometry API (wrangle.ts; null after the run)
497
+ * varsGuard what code gets for `vars` (makeVarsGuard)
473
498
  * The host keeps the object and calls setJob/setCur directly, outside the time limit, so they must not run user code:
474
499
  * the job and the API are closure variables, the object is frozen (own data properties only) and a global `const`
475
500
  * can be neither reassigned nor redefined by later code (user code never runs at the realm's top level, and a
476
- * property of the global object of the same name is shadowed by the binding). Before R-18 the host assigned
501
+ * property of the global object of the same name is shadowed by the binding). Previously the host assigned
477
502
  * global properties `__job` and `__cur`, and a script that gave them a looping setter hung the process.
478
503
  */
479
504
  export const SCRIPT_REALM_SETUP = `const ${REALM_CONTROL} = (() => {
480
505
  let job;
481
506
  const setCur = (${installSnippetLib.toString()})(globalThis, __hostApi);
482
- return Object.freeze({ run: () => job(), setJob: (f) => { job = f; }, setCur });
507
+ const varsGuard = (${makeVarsGuard.toString()})();
508
+ return Object.freeze({ run: () => job(), setJob: (f) => { job = f; }, setCur, varsGuard });
483
509
  })();
484
510
  delete globalThis.__hostApi;`;
@@ -0,0 +1,38 @@
1
+ import type { Geometry } from './geometry.ts';
2
+ export interface SolidifyOptions {
3
+ thickness: number;
4
+ /** -1 inside … 0 centred … +1 outside, relative to the face winding */
5
+ offset: number;
6
+ fillRim: boolean;
7
+ /** largest allowed |δ| (offset distance / face distance) */
8
+ miterLimit: number;
9
+ /** strict self-intersection pair tests (default 4,000,000) */
10
+ maxTests?: number;
11
+ }
12
+ export interface SolidifyResult {
13
+ geo: Geometry;
14
+ stats: {
15
+ outer: number;
16
+ inner: number;
17
+ rim: number;
18
+ maxMiter: number;
19
+ maxResidual: number;
20
+ tested: number;
21
+ };
22
+ }
23
+ /** A rejected input or result: message says what and where, hint what to do. */
24
+ export declare class SolidifyError extends Error {
25
+ hint: string;
26
+ constructor(message: string, hint: string);
27
+ }
28
+ /** Largest allowed least-squares residual, as a fraction of the offset distance. */
29
+ export declare const RESIDUAL_LIMIT = 0.02;
30
+ /**
31
+ * Minimum-length least-squares δ with n_f · δ = 1 for the unit normals N (3 per face): δ = (NᵀN)⁺ Nᵀ 1, dropping
32
+ * eigen-directions below 1e-9 of the largest (the free directions of an underconstrained point).
33
+ */
34
+ export declare function offsetDirection(N: number[]): {
35
+ d: number[];
36
+ residual: number;
37
+ };
38
+ export declare function solidify(g: Geometry, o: SolidifyOptions): SolidifyResult;
@@ -0,0 +1,356 @@
1
+ // Solidify (shell): a constant-thickness solid from a consistently wound polygon surface (a sheet, a folded panel, a
2
+ // closed hull).
3
+ //
4
+ // Offsets follow the face winding: the face normal n points to the "outside". offset +1 grows the wall outward (the
5
+ // input becomes the inner surface), -1 inward (the input stays the outer surface), 0 centres it. The two surfaces sit
6
+ // at signed distances d_out = (1 + offset)/2 · thickness and d_in = -(1 - offset)/2 · thickness.
7
+ //
8
+ // Each point moves by d · δ, where δ solves n_f · δ = 1 for every face f around the point: the offset planes of the
9
+ // incident faces meet there, so every face of a surface copy is exactly d away from its source face. δ is the
10
+ // minimum-length least-squares solution (pseudo-inverse of the stacked normals, from a deterministic Jacobi eigen
11
+ // decomposition): at a boundary point with one face it is that face's normal; along a fold it is the mitre of the two
12
+ // planes; at a corner of three or more planes their intersection. Where the planes do not meet (a saddle, a twisted
13
+ // corner) the least-squares residual is what thickness the faces lose; above RESIDUAL_LIMIT of the offset the node
14
+ // stops instead of silently thinning the wall, as it does when |δ| exceeds miter_limit (a fold so sharp the mitre runs
15
+ // away).
16
+ //
17
+ // Topology: an outer copy (input winding), an inner copy (reversed) and, with fill_rim, one quad per boundary edge
18
+ // joining them, oriented outward: a closed manifold for an open sheet, two nested closed surfaces for a closed one.
19
+ // Groups inner / outer / rim. Attributes come through GeoBuilder provenance (N and tangent are dropped: they would
20
+ // point the wrong way on the inner copy; recompute them downstream). Surface uvs are copied; rim uvs are metric: u =
21
+ // distance along the boundary loop, v = 0 on the inner surface to thickness on the outer.
22
+ //
23
+ // Rejected with a diagnostic, never repaired: open polylines, non-manifold edges or points, inconsistent winding,
24
+ // collapsed faces, and results that fold over (a face of a surface copy flips) or intersect themselves (strict check:
25
+ // crossings, coplanar overlaps and contacts beyond the shared vertices and edges). If the intersection check runs out
26
+ // of its budget the node fails rather than passing unchecked geometry.
27
+ import { GeoBuilder } from './geometry.js';
28
+ import { buildEdges, primNewell } from './topology.js';
29
+ import { halfEdges } from './halfedge.js';
30
+ import { selfIntersections } from './analysis.js';
31
+ /** A rejected input or result: message says what and where, hint what to do. */
32
+ export class SolidifyError extends Error {
33
+ hint;
34
+ constructor(message, hint) { super(message); this.hint = hint; }
35
+ }
36
+ /** Largest allowed least-squares residual, as a fraction of the offset distance. */
37
+ export const RESIDUAL_LIMIT = 0.02;
38
+ const list = (a, n = 8) => a.slice(0, n).join(', ') + (a.length > n ? `, … (${a.length})` : '');
39
+ /** Eigen decomposition of a symmetric 3×3 (row-major) by cyclic Jacobi: values w, vectors as columns of V. */
40
+ function jacobi3(A) {
41
+ const a = A.slice(), V = [1, 0, 0, 0, 1, 0, 0, 0, 1];
42
+ for (let sweep = 0; sweep < 32; sweep++) {
43
+ const off = a[1] * a[1] + a[2] * a[2] + a[5] * a[5];
44
+ if (off < 1e-30 * (a[0] * a[0] + a[4] * a[4] + a[8] * a[8] + 1e-300))
45
+ break;
46
+ for (const [p, q] of [[0, 1], [0, 2], [1, 2]]) {
47
+ const apq = a[p * 3 + q];
48
+ if (apq === 0)
49
+ continue;
50
+ const theta = (a[q * 3 + q] - a[p * 3 + p]) / (2 * apq);
51
+ const t = Math.sign(theta || 1) / (Math.abs(theta) + Math.sqrt(theta * theta + 1));
52
+ const c = 1 / Math.sqrt(t * t + 1), s = t * c;
53
+ for (let k = 0; k < 3; k++) {
54
+ const akp = a[k * 3 + p], akq = a[k * 3 + q];
55
+ a[k * 3 + p] = c * akp - s * akq;
56
+ a[k * 3 + q] = s * akp + c * akq;
57
+ }
58
+ for (let k = 0; k < 3; k++) {
59
+ const apk = a[p * 3 + k], aqk = a[q * 3 + k];
60
+ a[p * 3 + k] = c * apk - s * aqk;
61
+ a[q * 3 + k] = s * apk + c * aqk;
62
+ }
63
+ for (let k = 0; k < 3; k++) {
64
+ const vkp = V[k * 3 + p], vkq = V[k * 3 + q];
65
+ V[k * 3 + p] = c * vkp - s * vkq;
66
+ V[k * 3 + q] = s * vkp + c * vkq;
67
+ }
68
+ }
69
+ }
70
+ return { w: [a[0], a[4], a[8]], V };
71
+ }
72
+ /**
73
+ * Minimum-length least-squares δ with n_f · δ = 1 for the unit normals N (3 per face): δ = (NᵀN)⁺ Nᵀ 1, dropping
74
+ * eigen-directions below 1e-9 of the largest (the free directions of an underconstrained point).
75
+ */
76
+ export function offsetDirection(N) {
77
+ const M = [0, 0, 0, 0, 0, 0, 0, 0, 0], b = [0, 0, 0];
78
+ const k = N.length / 3;
79
+ for (let f = 0; f < k; f++) {
80
+ const x = N[f * 3], y = N[f * 3 + 1], z = N[f * 3 + 2];
81
+ M[0] += x * x;
82
+ M[1] += x * y;
83
+ M[2] += x * z;
84
+ M[4] += y * y;
85
+ M[5] += y * z;
86
+ M[8] += z * z;
87
+ b[0] += x;
88
+ b[1] += y;
89
+ b[2] += z;
90
+ }
91
+ M[3] = M[1];
92
+ M[6] = M[2];
93
+ M[7] = M[5];
94
+ const { w, V } = jacobi3(M);
95
+ const wmax = Math.max(w[0], w[1], w[2]);
96
+ const d = [0, 0, 0];
97
+ for (let e = 0; e < 3; e++) {
98
+ if (!(w[e] > 1e-9 * wmax))
99
+ continue;
100
+ const vx = V[e], vy = V[3 + e], vz = V[6 + e];
101
+ const s = (vx * b[0] + vy * b[1] + vz * b[2]) / w[e];
102
+ d[0] += vx * s;
103
+ d[1] += vy * s;
104
+ d[2] += vz * s;
105
+ }
106
+ let residual = 0;
107
+ for (let f = 0; f < k; f++)
108
+ residual = Math.max(residual, Math.abs(N[f * 3] * d[0] + N[f * 3 + 1] * d[1] + N[f * 3 + 2] * d[2] - 1));
109
+ return { d, residual };
110
+ }
111
+ export function solidify(g, o) {
112
+ const t = o.thickness, off = o.offset;
113
+ if (!(t > 0) || !Number.isFinite(t))
114
+ throw new SolidifyError(`solidify: thickness must be a positive number of metres (got ${t})`, 'e.g. 0.01 for 1 cm');
115
+ if (!(off >= -1 && off <= 1))
116
+ throw new SolidifyError(`solidify: offset must be between -1 (inside) and +1 (outside) (got ${off})`, '0 centres the wall on the surface');
117
+ if (!(o.miterLimit >= 1))
118
+ throw new SolidifyError(`solidify: miter_limit must be at least 1 (got ${o.miterLimit})`, 'the default 4 allows folds up to about 150°');
119
+ const np = g.numPoints, npr = g.numPrims, P = g.P, vp = g.vertexPoint;
120
+ // ---- input checks
121
+ const open = [], polys = [];
122
+ for (let p = 0; p < npr; p++) {
123
+ if (g.primKind && g.primKind[p]) {
124
+ open.push(p);
125
+ continue;
126
+ }
127
+ if (!g.primClosed[p])
128
+ open.push(p);
129
+ else
130
+ polys.push(p);
131
+ }
132
+ if (open.length)
133
+ throw new SolidifyError(`solidify: ${open.length} prim(s) are not polygons (prims ${list(open)})`, 'solidify works on a polygon surface; delete polylines/points/packed prims upstream (delete, or blast by group)');
134
+ if (!polys.length)
135
+ throw new SolidifyError('solidify: the input has no polygons', 'connect a polygon surface (grid, a selected part of a mesh)');
136
+ const fn = new Float64Array(npr * 3), collapsed = [], nrm = [0, 0, 0];
137
+ for (const p of polys) {
138
+ const s = g.primStart[p], e = g.primStart[p + 1];
139
+ let e2 = 0;
140
+ for (let v = s; v < e; v++) {
141
+ const a = vp[v] * 3, b = vp[v + 1 < e ? v + 1 : s] * 3;
142
+ e2 = Math.max(e2, (P[b] - P[a]) ** 2 + (P[b + 1] - P[a + 1]) ** 2 + (P[b + 2] - P[a + 2]) ** 2);
143
+ }
144
+ primNewell(g, p, nrm);
145
+ const l = Math.hypot(nrm[0], nrm[1], nrm[2]);
146
+ if (e - s < 3 || !(l > 1e-9 * e2)) {
147
+ collapsed.push(p);
148
+ continue;
149
+ }
150
+ fn[p * 3] = nrm[0] / l;
151
+ fn[p * 3 + 1] = nrm[1] / l;
152
+ fn[p * 3 + 2] = nrm[2] / l;
153
+ }
154
+ if (collapsed.length)
155
+ throw new SolidifyError(`solidify: ${collapsed.length} collapsed polygon(s) without a usable normal (prims ${list(collapsed)})`, 'remove them first (clean / mesh_repair with degenerate removal, or fuse points)');
156
+ const E = buildEdges(g);
157
+ const nonManifold = [];
158
+ for (let e = 0; e < E.a.length; e++)
159
+ if (E.count[e] > 2)
160
+ nonManifold.push(`${E.a[e]}-${E.b[e]}`);
161
+ if (nonManifold.length)
162
+ throw new SolidifyError(`solidify: ${nonManifold.length} non-manifold edge(s) shared by more than two polygons (points ${list(nonManifold)})`, 'select one sheet upstream (split or delete the extra faces)');
163
+ const he = halfEdges(g);
164
+ if (he.flippedEdges.size) {
165
+ const bad = [...he.flippedEdges].slice(0, 8).map((e) => `${he.edgeA[e]}-${he.edgeB[e]}`);
166
+ throw new SolidifyError(`solidify: inconsistent winding: ${he.flippedEdges.size} edge(s) where neighbouring polygons run the same way (points ${list(bad)})`, 'make the winding consistent first (reverse part of the mesh, or clean fix_winding); the winding decides which side is outside');
167
+ }
168
+ // incident faces per point; a point's faces must form one fan (else two sheets touch at a point)
169
+ const inc = Array.from({ length: np }, () => []);
170
+ for (const p of polys)
171
+ for (let v = g.primStart[p]; v < g.primStart[p + 1]; v++)
172
+ inc[vp[v]].push(p);
173
+ const edgeKey = (a, b) => (a < b ? a * np + b : b * np + a);
174
+ const edgePrims = new Map();
175
+ for (const p of polys) {
176
+ const s = g.primStart[p], e = g.primStart[p + 1];
177
+ for (let v = s; v < e; v++) {
178
+ const k = edgeKey(vp[v], vp[v + 1 < e ? v + 1 : s]);
179
+ const l = edgePrims.get(k);
180
+ if (l)
181
+ l.push(p);
182
+ else
183
+ edgePrims.set(k, [p]);
184
+ }
185
+ }
186
+ const bowtie = [];
187
+ for (let i = 0; i < np; i++) {
188
+ const F = [...new Set(inc[i])];
189
+ if (F.length < 2)
190
+ continue;
191
+ const idx = new Map(F.map((f, k) => [f, k])), par = F.map((_, k) => k);
192
+ const find = (x) => { while (par[x] !== x)
193
+ x = par[x] = par[par[x]]; return x; };
194
+ for (const f of F) {
195
+ const s = g.primStart[f], e = g.primStart[f + 1];
196
+ for (let v = s; v < e; v++) {
197
+ const a = vp[v], b = vp[v + 1 < e ? v + 1 : s];
198
+ if (a !== i && b !== i)
199
+ continue;
200
+ for (const h of edgePrims.get(edgeKey(a, b)) ?? [])
201
+ if (h !== f) {
202
+ const x = find(idx.get(f)), y = find(idx.get(h));
203
+ if (x !== y)
204
+ par[x] = y;
205
+ }
206
+ }
207
+ }
208
+ if (new Set(F.map((_, k) => find(k))).size > 1)
209
+ bowtie.push(i);
210
+ }
211
+ if (bowtie.length)
212
+ throw new SolidifyError(`solidify: ${bowtie.length} non-manifold point(s) where separate fans of polygons touch (points ${list(bowtie)})`, 'split the sheets at those points (fuse off / polysplit) or remove one of the touching parts');
213
+ // ---- offset directions
214
+ const dOut = ((1 + off) / 2) * t, dIn = -((1 - off) / 2) * t;
215
+ const delta = new Float64Array(np * 3), used = new Uint8Array(np);
216
+ let maxMiter = 0, maxResidual = 0;
217
+ const miterBad = [], residualBad = [];
218
+ for (let i = 0; i < np; i++) {
219
+ const F = [...new Set(inc[i])];
220
+ if (!F.length)
221
+ continue;
222
+ used[i] = 1;
223
+ const N = [];
224
+ for (const f of F)
225
+ N.push(fn[f * 3], fn[f * 3 + 1], fn[f * 3 + 2]);
226
+ const { d, residual } = offsetDirection(N);
227
+ const len = Math.hypot(d[0], d[1], d[2]);
228
+ maxMiter = Math.max(maxMiter, len);
229
+ maxResidual = Math.max(maxResidual, residual);
230
+ if (len > o.miterLimit)
231
+ miterBad.push(`${i} (×${len.toFixed(2)})`);
232
+ if (residual > RESIDUAL_LIMIT)
233
+ residualBad.push(`${i} (${(residual * 100).toFixed(1)}%)`);
234
+ delta[i * 3] = d[0];
235
+ delta[i * 3 + 1] = d[1];
236
+ delta[i * 3 + 2] = d[2];
237
+ }
238
+ if (miterBad.length)
239
+ throw new SolidifyError(`solidify: ${miterBad.length} point(s) need an offset longer than miter_limit ${o.miterLimit} × the thickness (points ${list(miterBad)}): the fold is too sharp for a mitred wall`, 'bevel or round the fold first, lower the thickness, or raise miter_limit if the long mitre is intended');
240
+ if (residualBad.length)
241
+ throw new SolidifyError(`solidify: at ${residualBad.length} point(s) the offset planes of the surrounding faces do not meet (thickness lost: points ${list(residualBad)}; limit ${RESIDUAL_LIMIT * 100}%)`, 'the surface is twisted or saddle-shaped there: subdivide or flatten it, or use a thinner wall');
242
+ // ---- build
243
+ const b = new GeoBuilder(g);
244
+ for (const k of ['N', 'tangent']) {
245
+ b.skip.point.add(k);
246
+ b.skip.vertex.add(k);
247
+ b.skip.prim.add(k);
248
+ }
249
+ const uvF = g.findAttrib('uv', ['vertex', 'point']);
250
+ const uvOn = !!uvF && uvF.attr.type === 'float' && !uvF.attr.isArray;
251
+ const uvA = uvOn ? b.attrib('vertex', 'uv', 3) : null;
252
+ if (uvOn) {
253
+ b.skip.vertex.add('uv');
254
+ b.skip.point.add('uv');
255
+ }
256
+ const uvOf = (v) => {
257
+ const a = uvF.attr, i = uvF.cls === 'vertex' ? v : vp[v], s = a.size, d = a.data;
258
+ return [d[i * s], s > 1 ? d[i * s + 1] : 0, s > 2 ? d[i * s + 2] : 0];
259
+ };
260
+ for (let i = 0; i < np; i++)
261
+ if (!used[i])
262
+ b.copyPoint(i); // loose points pass through
263
+ const outerPt = new Int32Array(np).fill(-1), innerPt = new Int32Array(np).fill(-1);
264
+ for (let i = 0; i < np; i++) {
265
+ if (!used[i])
266
+ continue;
267
+ const x = P[i * 3], y = P[i * 3 + 1], z = P[i * 3 + 2], dx = delta[i * 3], dy = delta[i * 3 + 1], dz = delta[i * 3 + 2];
268
+ outerPt[i] = b.addPoint(x + dx * dOut, y + dy * dOut, z + dz * dOut, i);
269
+ innerPt[i] = b.addPoint(x + dx * dIn, y + dy * dIn, z + dz * dIn, i);
270
+ }
271
+ const gOuter = b.group('prim', 'outer'), gInner = b.group('prim', 'inner'), gRim = b.group('prim', 'rim');
272
+ let vOut = 0;
273
+ for (const p of polys) {
274
+ const s = g.primStart[p], e = g.primStart[p + 1], vs = [];
275
+ for (let v = s; v < e; v++)
276
+ vs.push(v);
277
+ gOuter.add(b.addPrim(vs.map((v) => outerPt[vp[v]]), true, p, vs));
278
+ if (uvA)
279
+ for (const v of vs)
280
+ uvA.set(vOut++, uvOf(v));
281
+ }
282
+ for (const p of polys) {
283
+ const s = g.primStart[p], e = g.primStart[p + 1], vs = [s];
284
+ for (let v = e - 1; v > s; v--)
285
+ vs.push(v);
286
+ gInner.add(b.addPrim(vs.map((v) => innerPt[vp[v]]), true, p, vs));
287
+ if (uvA)
288
+ for (const v of vs)
289
+ uvA.set(vOut++, uvOf(v));
290
+ }
291
+ let rims = 0;
292
+ if (o.fillRim) {
293
+ // boundary half-edges (a → b in face winding), chained into loops for the rim's u coordinate
294
+ const bnd = [];
295
+ for (const p of polys) {
296
+ const s = g.primStart[p], e = g.primStart[p + 1];
297
+ for (let v = s; v < e; v++) {
298
+ const w = v + 1 < e ? v + 1 : s;
299
+ if ((edgePrims.get(edgeKey(vp[v], vp[w]))?.length ?? 0) === 1)
300
+ bnd.push({ a: vp[v], b: vp[w], va: v, vb: w, p });
301
+ }
302
+ }
303
+ const fromPt = new Map();
304
+ bnd.forEach((h, k) => fromPt.set(h.a, k));
305
+ const seen = new Uint8Array(bnd.length), u0 = new Float64Array(bnd.length);
306
+ for (let k0 = 0; k0 < bnd.length; k0++) {
307
+ if (seen[k0])
308
+ continue;
309
+ let s = 0;
310
+ for (let k = k0; k !== undefined && !seen[k]; k = fromPt.get(bnd[k].b)) {
311
+ seen[k] = 1;
312
+ u0[k] = s;
313
+ const a = bnd[k].a * 3, c = bnd[k].b * 3;
314
+ s += Math.hypot(P[c] - P[a], P[c + 1] - P[a + 1], P[c + 2] - P[a + 2]);
315
+ }
316
+ }
317
+ bnd.forEach((h, k) => {
318
+ gRim.add(b.addPrim([outerPt[h.a], innerPt[h.a], innerPt[h.b], outerPt[h.b]], true, h.p, [h.va, h.va, h.vb, h.vb]));
319
+ if (uvA) {
320
+ const a = h.a * 3, c = h.b * 3, u1 = u0[k] + Math.hypot(P[c] - P[a], P[c + 1] - P[a + 1], P[c + 2] - P[a + 2]);
321
+ uvA.set(vOut++, [u0[k], t, 0]);
322
+ uvA.set(vOut++, [u0[k], 0, 0]);
323
+ uvA.set(vOut++, [u1, 0, 0]);
324
+ uvA.set(vOut++, [u1, t, 0]);
325
+ }
326
+ rims++;
327
+ });
328
+ }
329
+ const out = b.build();
330
+ // ---- result checks: no face of a surface copy flips, no self-intersection
331
+ const flipped = [];
332
+ const n2 = [0, 0, 0];
333
+ for (let q = 0; q < out.numPrims; q++) {
334
+ const src = q < polys.length ? polys[q] : q < 2 * polys.length ? polys[q - polys.length] : -1;
335
+ if (src < 0)
336
+ break;
337
+ primNewell(out, q, n2);
338
+ const sgn = q < polys.length ? 1 : -1;
339
+ if (!((n2[0] * fn[src * 3] + n2[1] * fn[src * 3 + 1] + n2[2] * fn[src * 3 + 2]) * sgn > 0))
340
+ flipped.push(src);
341
+ }
342
+ if (flipped.length)
343
+ throw new SolidifyError(`solidify: the ${flipped.length} offset cop${flipped.length === 1 ? 'y' : 'ies'} of polygon(s) ${list([...new Set(flipped)])} turn inside out: the wall is thicker than the surface's local radius of curvature`, 'lower the thickness, move the offset towards the convex side, or subdivide less tightly curved');
344
+ const si = selfIntersections(out, o.maxTests ?? 4_000_000, { strict: true });
345
+ if (si.capped)
346
+ throw new SolidifyError(`solidify: the self-intersection check ran out of its budget after ${si.tested.toLocaleString('en-US')} triangle pairs before finishing; the result was not verified`, 'reduce the polygon count (decimate / fewer divisions) or solidify parts separately and merge them');
347
+ if (si.pairs) {
348
+ const prims = [];
349
+ for (let q = 0; q < out.numPrims && prims.length < 8; q++)
350
+ if (si.prims[q])
351
+ prims.push(q);
352
+ const where = prims.map((q) => `${q} (${q < polys.length ? 'outer' : q < 2 * polys.length ? 'inner' : 'rim'})`);
353
+ throw new SolidifyError(`solidify: the shell intersects itself: ${si.pairs} triangle pair(s) cross, overlap or touch beyond their shared edges (result prims ${where.join(', ')}${si.pairs > 8 ? ', …' : ''})`, 'the wall is too thick for the clearance between parts of the surface: lower the thickness or change offset');
354
+ }
355
+ return { geo: out, stats: { outer: polys.length, inner: polys.length, rim: rims, maxMiter, maxResidual, tested: si.tested } };
356
+ }
@@ -0,0 +1,54 @@
1
+ import type { Geometry } from './geometry.ts';
2
+ /** Load (once) and initialise the MikkTSpace WebAssembly module. */
3
+ export declare function loadMikkTSpace(): Promise<void>;
4
+ /** True once loadMikkTSpace() has finished. */
5
+ export declare const mikkTSpaceReady: () => boolean;
6
+ export interface TangentDiagnostics {
7
+ triangles: number;
8
+ /** triangles with a non-finite uv corner */
9
+ uvNonFinite: number;
10
+ /** triangles whose uvs are collinear or coincide (zero uv area) */
11
+ uvDegenerate: number;
12
+ /** corners that received the fallback tangent */
13
+ fallbackCorners: number;
14
+ /** the first triangle with a non-finite or collapsed uv, or -1 (warnings say where it is) */
15
+ firstBad: number;
16
+ }
17
+ /**
18
+ * MikkTSpace tangents for a triangle soup: pos and nrm 9 floats per triangle, uv 6 (TopoTopo convention). Returns 12
19
+ * floats per triangle: xyz unit tangent and w = ±1 (bitangent = w · cross(N, T)). Throws when the module is not
20
+ * loaded.
21
+ */
22
+ export declare function mikkTangents(pos: ArrayLike<number>, nrm: ArrayLike<number>, uv: ArrayLike<number>): {
23
+ tangents: Float32Array;
24
+ diag: TangentDiagnostics;
25
+ };
26
+ /**
27
+ * MikkTSpace tangents for the triangles `tris` (vertex indices, as topology.triangulate returns them) of g, with the
28
+ * per-vertex normals N (3 per vertex, e.g. resolveVertexNormals) and per-vertex uv (2 per vertex, TopoTopo
29
+ * convention). 12 floats per triangle, in g's space.
30
+ */
31
+ export declare function cornerTangents(g: Geometry, tris: ArrayLike<number>, N: ArrayLike<number>, uv: ArrayLike<number>): {
32
+ tangents: Float32Array;
33
+ diag: TangentDiagnostics;
34
+ };
35
+ /** Per-vertex uv (2 per vertex) of a vertex or point attribute, or null when g has no such float attribute. */
36
+ export declare function vertexUV(g: Geometry, name?: string): Float64Array | null;
37
+ /**
38
+ * The orthonormal shading frame at a surface sample, as bake_normal writes and the PBR preview reads it: N normalised,
39
+ * T = the interpolated tangent made perpendicular to N and normalised, B = w · cross(N, T) with w the sign of the
40
+ * interpolated handedness. Fills out[0..8] with T, B, N; returns false (out untouched) when T is parallel to N.
41
+ */
42
+ export declare function shadingFrame(nx: number, ny: number, nz: number, tx: number, ty: number, tz: number, w: number, out: number[] | Float64Array): boolean;
43
+ /** The standard tangent attribute: float, 4 components (xyz unit tangent, w = ±1 handedness), point or vertex class. */
44
+ export declare const TANGENT_ATTRIB = "tangent";
45
+ /**
46
+ * Check a supplied tangent attribute: every element finite, |xyz| within 1e-3 of 1, w exactly ±1. Returns null when
47
+ * valid, else a description of the first problem (element class, index and value).
48
+ */
49
+ export declare function tangentProblem(g: Geometry): string | null;
50
+ /**
51
+ * Transform tangent vectors in place by the 3×3 linear part L (column-major, 9 numbers): xyz → normalize(L · xyz),
52
+ * w → w · sign(det L) (a mirror flips the bitangent relative to cross(N, T)). sel limits the elements.
53
+ */
54
+ export declare function transformTangents(d: Float64Array, count: number, L: ArrayLike<number>, sel?: (i: number) => boolean): void;