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
@@ -0,0 +1,220 @@
1
+ import { primWhere } from './report.js';
2
+ /** A triangle is collapsed (no uv area) when |cross| <= COLLAPSED_Q · (sum of squared edge lengths). */
3
+ export const COLLAPSED_Q = 1e-9;
4
+ /** A pair overlaps when the intersection area exceeds OVERLAP_REL of the smaller triangle. */
5
+ export const OVERLAP_REL = 1e-7;
6
+ /** Area of the intersection of two counter-clockwise triangles (6 numbers each) by convex clipping. */
7
+ export function triIntersectionArea(a, b) {
8
+ let poly = [a[0], a[1], a[2], a[3], a[4], a[5]];
9
+ for (let e = 0; e < 3 && poly.length >= 6; e++) {
10
+ const ax = b[e * 2], ay = b[e * 2 + 1], bx = b[((e + 1) % 3) * 2], by = b[((e + 1) % 3) * 2 + 1];
11
+ const out = [];
12
+ const n = poly.length / 2;
13
+ for (let i = 0; i < n; i++) {
14
+ const cx = poly[i * 2], cy = poly[i * 2 + 1], dx = poly[((i + 1) % n) * 2], dy = poly[((i + 1) % n) * 2 + 1];
15
+ const sc = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax), sd = (bx - ax) * (dy - ay) - (by - ay) * (dx - ax);
16
+ if (sc >= 0)
17
+ out.push(cx, cy);
18
+ if ((sc >= 0) !== (sd >= 0)) {
19
+ const t = sc / (sc - sd);
20
+ out.push(cx + (dx - cx) * t, cy + (dy - cy) * t);
21
+ }
22
+ }
23
+ poly = out;
24
+ }
25
+ if (poly.length < 6)
26
+ return 0;
27
+ let s = 0;
28
+ for (let i = 0, n = poly.length / 2; i < n; i++) {
29
+ const j = (i + 1) % n;
30
+ s += poly[i * 2] * poly[j * 2 + 1] - poly[j * 2] * poly[i * 2 + 1];
31
+ }
32
+ return Math.abs(s) / 2;
33
+ }
34
+ /** Per triangle of U (6 uv numbers each): ok = finite and not collapsed; signed = signed uv area (0 when not ok). */
35
+ export function uvTriangleAreas(U, nt) {
36
+ const ok = new Uint8Array(nt), signed = new Float64Array(nt);
37
+ for (let t = 0; t < nt; t++) {
38
+ const o6 = t * 6;
39
+ let finite = true;
40
+ for (let k = 0; k < 6; k++)
41
+ if (!Number.isFinite(U[o6 + k]))
42
+ finite = false;
43
+ if (!finite)
44
+ continue;
45
+ const du1 = U[o6 + 2] - U[o6], dv1 = U[o6 + 3] - U[o6 + 1], du2 = U[o6 + 4] - U[o6], dv2 = U[o6 + 5] - U[o6 + 1];
46
+ const cr = du1 * dv2 - du2 * dv1, e2 = du1 * du1 + dv1 * dv1 + du2 * du2 + dv2 * dv2 + (du2 - du1) ** 2 + (dv2 - dv1) ** 2;
47
+ if (!(Math.abs(cr) > COLLAPSED_Q * e2))
48
+ continue;
49
+ ok[t] = 1;
50
+ signed[t] = cr / 2;
51
+ }
52
+ return { ok, signed };
53
+ }
54
+ /** U for a triangle list over per-vertex uv (2 numbers per vertex): 6 numbers per triangle. */
55
+ export function trianglesUV(uv, tris) {
56
+ const nt = Math.floor(tris.length / 3), U = new Float64Array(nt * 6);
57
+ for (let c = 0; c < nt * 3; c++) {
58
+ const v = tris[c];
59
+ U[c * 2] = uv[v * 2];
60
+ U[c * 2 + 1] = uv[v * 2 + 1];
61
+ }
62
+ return U;
63
+ }
64
+ /**
65
+ * The most cell entries the candidate grid holds. Each triangle is listed in every cell its box touches; many large
66
+ * triangles sharing the tile (hundreds of instanced copies of one mesh, all on the same uv) would list billions,
67
+ * past the Int32 offsets and any memory. Past this the scan stops, unfinished, instead.
68
+ */
69
+ const MAX_LISTED = 1 << 26;
70
+ /**
71
+ * Every overlapping pair among the triangles `valid` (ids into U, which holds 6 uv numbers per triangle; signed: signed
72
+ * uv area per triangle, from uvTriangleAreas). onPair(ta, tb, area) runs with ta < tb for each pair whose intersection
73
+ * area exceeds OVERLAP_REL of the smaller triangle; the order of calls is deterministic.
74
+ */
75
+ export function uvOverlapPairs(U, valid, signed, onPair, o = {}) {
76
+ const scan = { candidatePairs: 0, testedPairs: 0, stopped: false };
77
+ const nv = valid.length;
78
+ if (nv < 2)
79
+ return scan;
80
+ const maxTests = o.maxTests ?? Infinity, skip = o.skip;
81
+ let minU = Infinity, minV = Infinity, maxU = -Infinity, maxV = -Infinity;
82
+ for (let i = 0; i < nv; i++) {
83
+ const t = valid[i];
84
+ for (let k = 0; k < 3; k++) {
85
+ const u = U[t * 6 + k * 2], v = U[t * 6 + k * 2 + 1];
86
+ if (u < minU)
87
+ minU = u;
88
+ if (u > maxU)
89
+ maxU = u;
90
+ if (v < minV)
91
+ minV = v;
92
+ if (v > maxV)
93
+ maxV = v;
94
+ }
95
+ }
96
+ // counter-clockwise copies for clipping
97
+ const nt = U.length / 6, ccw = new Float64Array(nt * 6), bb = new Float64Array(nt * 4);
98
+ for (let i = 0; i < nv; i++) {
99
+ const t = valid[i], o6 = t * 6;
100
+ if (signed[t] > 0)
101
+ for (let k = 0; k < 6; k++)
102
+ ccw[o6 + k] = U[o6 + k];
103
+ else {
104
+ ccw[o6] = U[o6];
105
+ ccw[o6 + 1] = U[o6 + 1];
106
+ ccw[o6 + 2] = U[o6 + 4];
107
+ ccw[o6 + 3] = U[o6 + 5];
108
+ ccw[o6 + 4] = U[o6 + 2];
109
+ ccw[o6 + 5] = U[o6 + 3];
110
+ }
111
+ bb[t * 4] = Math.min(U[o6], U[o6 + 2], U[o6 + 4]);
112
+ bb[t * 4 + 1] = Math.min(U[o6 + 1], U[o6 + 3], U[o6 + 5]);
113
+ bb[t * 4 + 2] = Math.max(U[o6], U[o6 + 2], U[o6 + 4]);
114
+ bb[t * 4 + 3] = Math.max(U[o6 + 1], U[o6 + 3], U[o6 + 5]);
115
+ }
116
+ const span = Math.max(maxU - minU, maxV - minV, 1e-12);
117
+ const cells = Math.max(1, Math.min(1024, Math.ceil(Math.sqrt(nv / 2))));
118
+ const cs = span / cells;
119
+ const nx = Math.max(1, Math.min(1024, Math.ceil((maxU - minU) / cs) || 1)), ny = Math.max(1, Math.min(1024, Math.ceil((maxV - minV) / cs) || 1));
120
+ const cellOf = (u, n, lo) => Math.max(0, Math.min(n - 1, Math.floor((u - lo) / cs)));
121
+ let listed = 0;
122
+ for (let i = 0; i < nv; i++) {
123
+ const t = valid[i];
124
+ listed += (cellOf(bb[t * 4 + 3], ny, minV) - cellOf(bb[t * 4 + 1], ny, minV) + 1) * (cellOf(bb[t * 4 + 2], nx, minU) - cellOf(bb[t * 4], nx, minU) + 1);
125
+ }
126
+ if (listed > MAX_LISTED) {
127
+ scan.stopped = true;
128
+ scan.reason = `${nv.toLocaleString('en-US')} triangles cover the uv grid ${Math.round(listed / nv).toLocaleString('en-US')} cells deep on average (many large triangles on the same uv, as instanced copies are): too many to compare`;
129
+ return scan;
130
+ }
131
+ const counts = new Int32Array(nx * ny + 1);
132
+ for (let i = 0; i < nv; i++) {
133
+ const t = valid[i];
134
+ for (let y = cellOf(bb[t * 4 + 1], ny, minV); y <= cellOf(bb[t * 4 + 3], ny, minV); y++)
135
+ for (let x = cellOf(bb[t * 4], nx, minU); x <= cellOf(bb[t * 4 + 2], nx, minU); x++)
136
+ counts[y * nx + x + 1]++;
137
+ }
138
+ for (let i = 0; i < nx * ny; i++)
139
+ counts[i + 1] += counts[i];
140
+ const items = new Int32Array(counts[nx * ny]), fill = counts.slice(0, nx * ny);
141
+ for (let i = 0; i < nv; i++) {
142
+ const t = valid[i];
143
+ for (let y = cellOf(bb[t * 4 + 1], ny, minV); y <= cellOf(bb[t * 4 + 3], ny, minV); y++)
144
+ for (let x = cellOf(bb[t * 4], nx, minU); x <= cellOf(bb[t * 4 + 2], nx, minU); x++)
145
+ items[fill[y * nx + x]++] = t;
146
+ }
147
+ for (let c = 0; c < nx * ny; c++) {
148
+ const s = counts[c], e = counts[c + 1], cx = c % nx, cy = Math.floor(c / nx);
149
+ for (let i = s; i < e; i++)
150
+ for (let j = i + 1; j < e; j++) {
151
+ const ta = items[i], tb = items[j];
152
+ // bounding boxes overlap with positive extent, and this cell owns the pair
153
+ const lu = Math.max(bb[ta * 4], bb[tb * 4]), lv = Math.max(bb[ta * 4 + 1], bb[tb * 4 + 1]);
154
+ if (lu >= Math.min(bb[ta * 4 + 2], bb[tb * 4 + 2]) || lv >= Math.min(bb[ta * 4 + 3], bb[tb * 4 + 3]))
155
+ continue;
156
+ if (cellOf(lu, nx, minU) !== cx || cellOf(lv, ny, minV) !== cy)
157
+ continue;
158
+ scan.candidatePairs++;
159
+ const t0 = Math.min(ta, tb), t1 = Math.max(ta, tb);
160
+ if (skip && skip(t0, t1))
161
+ continue;
162
+ if (scan.testedPairs >= maxTests) {
163
+ scan.stopped = true;
164
+ continue;
165
+ }
166
+ scan.testedPairs++;
167
+ const A = triIntersectionArea(ccw.subarray(t0 * 6, t0 * 6 + 6), ccw.subarray(t1 * 6, t1 * 6 + 6));
168
+ if (A > OVERLAP_REL * Math.min(Math.abs(signed[t0]), Math.abs(signed[t1])))
169
+ onPair(t0, t1, A);
170
+ }
171
+ }
172
+ return scan;
173
+ }
174
+ /**
175
+ * Overlapping uv triangle pairs of a triangulation (tris: vertex triples; triPrim: prim per triangle) over per-vertex uv,
176
+ * split by groupOfPrim (an island id per prim; -1 = fixed prims, whose pairs among themselves are not tested).
177
+ */
178
+ export function uvOverlapCheck(uv, tris, triPrim, groupOfPrim, o = {}) {
179
+ const U = trianglesUV(uv, tris), nt = U.length / 6;
180
+ const { ok, signed } = uvTriangleAreas(U, nt);
181
+ const valid = [];
182
+ for (let t = 0; t < nt; t++)
183
+ if (ok[t])
184
+ valid.push(t);
185
+ const grp = (t) => groupOfPrim[triPrim[t]];
186
+ const r = { across: 0, within: 0, firstAcross: null, firstWithin: null, scan: { candidatePairs: 0, testedPairs: 0, stopped: false } };
187
+ r.scan = uvOverlapPairs(U, valid, signed, (t0, t1, area) => {
188
+ if (grp(t0) === grp(t1)) {
189
+ r.within++;
190
+ if (!r.firstWithin || area > r.firstWithin.area)
191
+ r.firstWithin = { t: [t0, t1], area };
192
+ }
193
+ else {
194
+ r.across++;
195
+ if (!r.firstAcross || area > r.firstAcross.area)
196
+ r.firstAcross = { t: [t0, t1], area };
197
+ }
198
+ }, { ...o, skip: (t0, t1) => (grp(t0) < 0 && grp(t1) < 0) || (o.skip ? o.skip(t0, t1) : false) });
199
+ return r;
200
+ }
201
+ /** A pair of overlapping triangles in words: both prims' parts and 3D positions, and where in uv they meet. */
202
+ export function uvPairText(g, uv, tris, triPrim, t0, t1) {
203
+ const at = (t) => {
204
+ const P = g.P, c = [0, 0, 0];
205
+ for (let k = 0; k < 3; k++) {
206
+ const i = g.vertexPoint[tris[t * 3 + k]] * 3;
207
+ c[0] += P[i] / 3;
208
+ c[1] += P[i + 1] / 3;
209
+ c[2] += P[i + 2] / 3;
210
+ }
211
+ return primWhere(g, triPrim[t], c);
212
+ };
213
+ let u = 0, v = 0;
214
+ for (const t of [t0, t1])
215
+ for (let k = 0; k < 3; k++) {
216
+ u += uv[tris[t * 3 + k] * 2] / 6;
217
+ v += uv[tris[t * 3 + k] * 2 + 1] / 6;
218
+ }
219
+ return `${at(t0)} × ${at(t1)}, near uv (${u.toFixed(3)}, ${v.toFixed(3)})`;
220
+ }
@@ -0,0 +1,64 @@
1
+ export interface PackIsland {
2
+ /** distinct vertices (corners) of the island; their uv is rewritten */
3
+ verts: ArrayLike<number>;
4
+ /** triangles as vertex index triples: the shapes rasterised (use the triangulation the result is checked with) */
5
+ tris: ArrayLike<number>;
6
+ /** 3D surface area and uv area (scale_mode uniform: uv area ∝ 3D area) */
7
+ area3: number;
8
+ areaUV: number;
9
+ }
10
+ export interface PackOptions {
11
+ /** texture size in texels per tile side; padding is in its texels */
12
+ resolution: number;
13
+ /** texels between islands (half of it, rounded up, to the tile border) */
14
+ padding: number;
15
+ /** allow 90° turns while packing */
16
+ turns: boolean;
17
+ /** orientation before packing: min_rect = the island's minimum-area bounding rectangle, keep = as it is */
18
+ orient: 'min_rect' | 'keep';
19
+ /** uniform: equal texel density (uv area ∝ 3D area); keep: the islands' relative uv scale */
20
+ scaleMode: 'uniform' | 'keep';
21
+ /** UDIM tiles to fill (1 = the unit tile), right then up, ten per row; islands go to the first tile they fit */
22
+ tiles: number;
23
+ /** island orderings tried (longest side first, then tallest, largest bounding box, largest area, then shuffles of the first) */
24
+ iterations: number;
25
+ seed?: number;
26
+ /** per island texel weight: its scale is multiplied by this (2 = twice the texels per metre); null = 1 */
27
+ weights?: ArrayLike<number> | null;
28
+ /** per island fixed rotation in radians before packing (NaN = by orient); null = by orient */
29
+ angles?: ArrayLike<number> | null;
30
+ /** fixed uv triangles (6 numbers each) the packed islands keep clear of, with padding (e.g. prims outside a group) */
31
+ obstacles?: Float64Array | null;
32
+ /**
33
+ * > 0: islands at this density (texels per island unit: per metre with scale_mode uniform), filling the tiles first-fit;
34
+ * when they do not fit `tiles`, or an island is larger than a tile, the density drops until they do
35
+ */
36
+ texelsPerUnit?: number;
37
+ /** > 0: the densest the search may go (texels per island unit) */
38
+ maxTexelsPerUnit?: number;
39
+ }
40
+ export interface PackResult {
41
+ uv: Float64Array;
42
+ /** false when there was nothing to pack, or the islands did not fit at any scale (uv unchanged) */
43
+ packed: boolean;
44
+ /** raster cells per tile side, and the padding in cells */
45
+ grid: number;
46
+ padCells: number;
47
+ /** tiles holding islands */
48
+ tilesUsed: number;
49
+ /** summed uv area of the packed islands (their share of one tile; over several tiles it can exceed 1) */
50
+ uvArea: number;
51
+ /** packs run by the scale search */
52
+ packs: number;
53
+ /** share of the used tiles' cells covered by the islands' rasters, and by the rasters grown by half the padding */
54
+ cells: {
55
+ island: number;
56
+ padded: number;
57
+ };
58
+ /** the island whose size capped the scale (it spans a tile), or -1 when the tiles' area set the scale */
59
+ cappedBy: number;
60
+ /** the texel density packed at: texels (of resolution) per island unit (0 when not packed) */
61
+ texelsPerUnit: number;
62
+ }
63
+ /** Pack islands into the unit tile (or UDIM tiles); returns new per-vertex uv (other vertices keep theirs). */
64
+ export declare function packCharts(uv: Float64Array, islands: PackIsland[], o: PackOptions): PackResult;