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,800 @@
1
+ import { GeoBuilder } from './geometry.js';
2
+ import { triangulate } from './topology.js';
3
+ import { lscm, readUV } from './uv.js';
4
+ import { xatlasCharts } from './xatlas.js';
5
+ import { packCharts } from './uvpack.js';
6
+ import { cutLongCharts, islandWeights, mergeThinCharts, packsDenser, worldAngles } from './uvislands.js';
7
+ import { OVERLAP_REL, triIntersectionArea, uvOverlapCheck, uvOverlapPairs, uvTriangleAreas } from './uvoverlap.js';
8
+ /** Signed uv area of triangle t in U (6 numbers per triangle), doubled. */
9
+ const cross2 = (U, t) => (U[t * 6 + 2] - U[t * 6]) * (U[t * 6 + 5] - U[t * 6 + 1]) - (U[t * 6 + 3] - U[t * 6 + 1]) * (U[t * 6 + 4] - U[t * 6]);
10
+ export function unwrapMesh(X, g, o) {
11
+ const { tris, triPrim } = triangulate(g);
12
+ const nt = triPrim.length, np = g.numPrims, P = g.P, vp = g.vertexPoint, ps = g.primStart;
13
+ const tFirst = new Int32Array(np).fill(-1), tCount = new Int32Array(np);
14
+ for (let t = 0; t < nt; t++) {
15
+ const p = triPrim[t];
16
+ if (tFirst[p] < 0)
17
+ tFirst[p] = t;
18
+ tCount[p]++;
19
+ }
20
+ // ---- pieces: points joined by polygons
21
+ const parent = Int32Array.from({ length: g.numPoints }, (_, i) => i);
22
+ const find = (x) => { while (parent[x] !== x) {
23
+ parent[x] = parent[parent[x]];
24
+ x = parent[x];
25
+ } return x; };
26
+ for (let p = 0; p < np; p++) {
27
+ if (!tCount[p])
28
+ continue;
29
+ const r0 = find(vp[ps[p]]);
30
+ for (let v = ps[p] + 1; v < ps[p + 1]; v++) {
31
+ const r = find(vp[v]);
32
+ if (r !== r0)
33
+ parent[r] = r0;
34
+ }
35
+ }
36
+ const pieceOf = new Int32Array(np).fill(-1), pieceId = new Map();
37
+ for (let p = 0; p < np; p++) {
38
+ if (!tCount[p])
39
+ continue;
40
+ const r = find(vp[ps[p]]);
41
+ let k = pieceId.get(r);
42
+ if (k === undefined)
43
+ pieceId.set(r, (k = pieceId.size));
44
+ pieceOf[p] = k;
45
+ }
46
+ const nPieces = pieceId.size;
47
+ const lo = new Float64Array(nPieces * 3).fill(Infinity), hi = new Float64Array(nPieces * 3).fill(-Infinity);
48
+ for (let p = 0; p < np; p++) {
49
+ const k = pieceOf[p];
50
+ if (k < 0)
51
+ continue;
52
+ for (let v = ps[p]; v < ps[p + 1]; v++)
53
+ for (let d = 0; d < 3; d++) {
54
+ const x = P[vp[v] * 3 + d];
55
+ if (x < lo[k * 3 + d])
56
+ lo[k * 3 + d] = x;
57
+ if (x > hi[k * 3 + d])
58
+ hi[k * 3 + d] = x;
59
+ }
60
+ }
61
+ // ---- 3D area and degeneracy per triangle (relative to its piece: xatlas drops faces below about 1e-11 of the diagonal²)
62
+ const area3 = new Float64Array(nt), deg = new Uint8Array(nt);
63
+ for (let t = 0; t < nt; t++) {
64
+ const a = vp[tris[t * 3]] * 3, b = vp[tris[t * 3 + 1]] * 3, c = vp[tris[t * 3 + 2]] * 3;
65
+ const ux = P[b] - P[a], uy = P[b + 1] - P[a + 1], uz = P[b + 2] - P[a + 2], wx = P[c] - P[a], wy = P[c + 1] - P[a + 1], wz = P[c + 2] - P[a + 2];
66
+ const cr = Math.hypot(uy * wz - uz * wy, uz * wx - ux * wz, ux * wy - uy * wx);
67
+ const e2 = ux * ux + uy * uy + uz * uz + wx * wx + wy * wy + wz * wz + (wx - ux) ** 2 + (wy - uy) ** 2 + (wz - uz) ** 2;
68
+ const k = pieceOf[triPrim[t]];
69
+ const d2 = (hi[k * 3] - lo[k * 3]) ** 2 + (hi[k * 3 + 1] - lo[k * 3 + 1]) ** 2 + (hi[k * 3 + 2] - lo[k * 3 + 2]) ** 2;
70
+ area3[t] = cr / 2;
71
+ if (!(cr > 1e-9 * e2) || cr < 4e-11 * d2)
72
+ deg[t] = 1;
73
+ }
74
+ // ---- xatlas charts per piece
75
+ const keyOf = o.vertexKey ? (v) => o.vertexKey[v] : (v) => vp[v];
76
+ const triChart = new Int32Array(nt).fill(-1), cu = new Float64Array(nt * 6);
77
+ let nCharts = 0;
78
+ const chartTriangles = (list, chart) => {
79
+ const sub = new Uint32Array(list.length * 3);
80
+ for (let i = 0; i < list.length; i++)
81
+ for (let c = 0; c < 3; c++)
82
+ sub[i * 3 + c] = tris[list[i] * 3 + c];
83
+ const r = xatlasCharts(X, g, sub, { keyOf, group: o.faceGroup ? (i) => o.faceGroup[triPrim[list[i]]] : null, chart });
84
+ for (let i = 0; i < list.length; i++) {
85
+ const t = list[i];
86
+ if (r.triChart[i] < 0) {
87
+ triChart[t] = -1;
88
+ deg[t] = 1;
89
+ continue;
90
+ }
91
+ triChart[t] = nCharts + r.triChart[i];
92
+ for (let k = 0; k < 6; k++)
93
+ cu[t * 6 + k] = r.cornerUV[i * 6 + k];
94
+ }
95
+ nCharts += r.chartCount;
96
+ };
97
+ const byPiece = Array.from({ length: nPieces }, () => []);
98
+ for (let t = 0; t < nt; t++)
99
+ if (!deg[t])
100
+ byPiece[pieceOf[triPrim[t]]].push(t);
101
+ for (const list of byPiece)
102
+ if (list.length)
103
+ chartTriangles(list, o.chart);
104
+ // ---- winding: mirror charts back to the polygon winding
105
+ let mirrored = 0;
106
+ const unmirror = (from) => {
107
+ const sum = new Map();
108
+ for (let t = 0; t < nt; t++)
109
+ if (triChart[t] >= from)
110
+ sum.set(triChart[t], (sum.get(triChart[t]) ?? 0) + cross2(cu, t));
111
+ for (let t = 0; t < nt; t++)
112
+ if (triChart[t] >= from && sum.get(triChart[t]) < 0)
113
+ for (let k = 0; k < 6; k += 2)
114
+ cu[t * 6 + k] = -cu[t * 6 + k];
115
+ for (const [, s] of sum)
116
+ if (s < 0)
117
+ mirrored++;
118
+ };
119
+ unmirror(0);
120
+ // ---- stretch: charts stretched one way (thin strips xatlas widened) are flattened again by a conformal map
121
+ let unstretched = 0;
122
+ for (const list of stretchedCharts(g, tris, cu, triChart, nCharts)) {
123
+ lscmChart(g, tris, list, keyOf, cu);
124
+ unstretched++;
125
+ }
126
+ // ---- folds: chart again until no chart overlaps itself
127
+ const refolded = [0, 0, 0];
128
+ let splitInto = 0;
129
+ for (let stage = 0; stage < 3; stage++) {
130
+ const folded = foldedCharts(cu, triChart, nCharts);
131
+ if (!folded.length)
132
+ break;
133
+ const first = nCharts;
134
+ for (const list of folded) {
135
+ refolded[stage]++;
136
+ if (stage === 0) {
137
+ // xatlas again on the chart alone, with a quarter of the cost: smaller, flatter charts
138
+ const chart = { ...o.chart, maxCost: (o.chart.maxCost ?? 2) * 0.25, maxChartArea: o.chart.maxChartArea ? o.chart.maxChartArea * 0.25 : 0 };
139
+ chartTriangles(list, chart);
140
+ }
141
+ else if (stage === 1) {
142
+ // a least squares conformal map of the chart (xatlas may have projected it, folding a ridge over)
143
+ lscmChart(g, tris, list, keyOf, cu);
144
+ for (const t of list)
145
+ triChart[t] = nCharts;
146
+ nCharts++;
147
+ }
148
+ else {
149
+ // still folded (a crack or cone no flat map avoids): cut it where it overlaps itself, keeping its uv
150
+ for (const part of splitFolded(tris, list, keyOf, cu)) {
151
+ for (const t of part)
152
+ triChart[t] = nCharts;
153
+ nCharts++;
154
+ splitInto++;
155
+ }
156
+ }
157
+ }
158
+ if (stage < 2)
159
+ unmirror(first);
160
+ }
161
+ // ---- polygons: one chart each, or split (straddling), or collapsed onto a neighbour (degenerate)
162
+ const primChart = new Int32Array(np).fill(-1), split = new Uint8Array(np);
163
+ let straddled = 0, firstStraddled = -1;
164
+ const deadPrims = [];
165
+ for (let p = 0; p < np; p++) {
166
+ if (!tCount[p])
167
+ continue;
168
+ const acc = new Map();
169
+ for (let t = tFirst[p]; t < tFirst[p] + tCount[p]; t++)
170
+ if (triChart[t] >= 0)
171
+ acc.set(triChart[t], (acc.get(triChart[t]) ?? 0) + area3[t] + 1e-300);
172
+ if (!acc.size) {
173
+ deadPrims.push(p);
174
+ continue;
175
+ }
176
+ if (acc.size > 1) {
177
+ straddled++;
178
+ if (firstStraddled < 0)
179
+ firstStraddled = p;
180
+ if (o.straddling === 'split') {
181
+ split[p] = 1;
182
+ continue;
183
+ }
184
+ }
185
+ let best = -1, bw = -1;
186
+ for (const [c, a] of acc)
187
+ if (a > bw || (a === bw && c < best)) {
188
+ bw = a;
189
+ best = c;
190
+ }
191
+ primChart[p] = best;
192
+ }
193
+ // per-vertex uv for whole polygons (prims that are not unwrapped, such as polylines, keep their input uv)
194
+ const nv = g.numVertices, vset = new Uint8Array(nv), vuv = readUV(g) ?? new Float64Array(nv * 2);
195
+ /** uv of point pt in chart c, from the nearest triangle (in 3D) of `cand` in chart c, by barycentric coordinates */
196
+ const mapOnto = (pt, c, cand) => {
197
+ let bt = -1, bd = Infinity;
198
+ const x = P[pt * 3], y = P[pt * 3 + 1], z = P[pt * 3 + 2];
199
+ for (let i = 0; i < cand.length; i++) {
200
+ const t = cand[i];
201
+ if (triChart[t] !== c)
202
+ continue;
203
+ let d = 0;
204
+ for (let k = 0; k < 3; k++) {
205
+ const q = vp[tris[t * 3 + k]] * 3;
206
+ d += Math.hypot(P[q] - x, P[q + 1] - y, P[q + 2] - z);
207
+ }
208
+ if (d < bd) {
209
+ bd = d;
210
+ bt = t;
211
+ }
212
+ }
213
+ if (bt < 0)
214
+ return null;
215
+ const a = vp[tris[bt * 3]] * 3, b = vp[tris[bt * 3 + 1]] * 3, q = vp[tris[bt * 3 + 2]] * 3;
216
+ const e1 = [P[b] - P[a], P[b + 1] - P[a + 1], P[b + 2] - P[a + 2]], e2 = [P[q] - P[a], P[q + 1] - P[a + 1], P[q + 2] - P[a + 2]], r = [x - P[a], y - P[a + 1], z - P[a + 2]];
217
+ const d11 = e1[0] * e1[0] + e1[1] * e1[1] + e1[2] * e1[2], d12 = e1[0] * e2[0] + e1[1] * e2[1] + e1[2] * e2[2], d22 = e2[0] * e2[0] + e2[1] * e2[1] + e2[2] * e2[2];
218
+ const r1 = r[0] * e1[0] + r[1] * e1[1] + r[2] * e1[2], r2 = r[0] * e2[0] + r[1] * e2[1] + r[2] * e2[2];
219
+ const det = d11 * d22 - d12 * d12;
220
+ const l1 = det > 0 ? (r1 * d22 - r2 * d12) / det : 0, l2 = det > 0 ? (r2 * d11 - r1 * d12) / det : 0;
221
+ const o6 = bt * 6;
222
+ return [cu[o6] + l1 * (cu[o6 + 2] - cu[o6]) + l2 * (cu[o6 + 4] - cu[o6]), cu[o6 + 1] + l1 * (cu[o6 + 3] - cu[o6 + 1]) + l2 * (cu[o6 + 5] - cu[o6 + 1])];
223
+ };
224
+ const primTris = (p) => Array.from({ length: tCount[p] }, (_, i) => tFirst[p] + i);
225
+ for (let p = 0; p < np; p++) {
226
+ const c = primChart[p];
227
+ if (c < 0)
228
+ continue;
229
+ for (let t = tFirst[p]; t < tFirst[p] + tCount[p]; t++) {
230
+ if (triChart[t] !== c)
231
+ continue;
232
+ for (let k = 0; k < 3; k++) {
233
+ const v = tris[t * 3 + k];
234
+ if (!vset[v]) {
235
+ vuv[v * 2] = cu[t * 6 + k * 2];
236
+ vuv[v * 2 + 1] = cu[t * 6 + k * 2 + 1];
237
+ vset[v] = 1;
238
+ }
239
+ }
240
+ }
241
+ for (let v = ps[p]; v < ps[p + 1]; v++) {
242
+ if (vset[v])
243
+ continue;
244
+ const m = mapOnto(vp[v], c, primTris(p));
245
+ if (m) {
246
+ vuv[v * 2] = m[0];
247
+ vuv[v * 2 + 1] = m[1];
248
+ vset[v] = 1;
249
+ }
250
+ }
251
+ }
252
+ // split polygons: a triangle with no chart (degenerate) takes a charted neighbour triangle's chart in its polygon
253
+ for (let p = 0; p < np; p++) {
254
+ if (!split[p])
255
+ continue;
256
+ const own = primTris(p);
257
+ for (const t of own) {
258
+ if (triChart[t] >= 0)
259
+ continue;
260
+ let c = -1;
261
+ for (const s of own)
262
+ if (triChart[s] >= 0 && [0, 1, 2].some((k) => [0, 1, 2].some((j) => tris[s * 3 + j] === tris[t * 3 + k]))) {
263
+ c = triChart[s];
264
+ break;
265
+ }
266
+ if (c < 0)
267
+ for (const s of own)
268
+ if (triChart[s] >= 0) {
269
+ c = triChart[s];
270
+ break;
271
+ }
272
+ for (let k = 0; k < 3; k++) {
273
+ const m = mapOnto(vp[tris[t * 3 + k]], c, own);
274
+ if (m) {
275
+ cu[t * 6 + k * 2] = m[0];
276
+ cu[t * 6 + k * 2 + 1] = m[1];
277
+ }
278
+ }
279
+ triChart[t] = c;
280
+ }
281
+ }
282
+ // degenerate polygons: the chart most of their neighbours (sharing points) use, collapsed onto it
283
+ let degenerate = 0, isolated = 0;
284
+ const firstDegenerate = deadPrims.length ? deadPrims[0] : -1;
285
+ if (deadPrims.length) {
286
+ const want = new Uint8Array(g.numPoints);
287
+ for (const p of deadPrims)
288
+ for (let v = ps[p]; v < ps[p + 1]; v++)
289
+ want[vp[v]] = 1;
290
+ const ptPrims = new Map();
291
+ for (let p = 0; p < np; p++)
292
+ for (let v = ps[p]; v < ps[p + 1]; v++)
293
+ if (want[vp[v]]) {
294
+ let l = ptPrims.get(vp[v]);
295
+ if (!l)
296
+ ptPrims.set(vp[v], (l = []));
297
+ l.push(p);
298
+ }
299
+ let todo = deadPrims.slice();
300
+ for (let pass = 0; pass < 8 && todo.length; pass++) {
301
+ const next = [];
302
+ for (const p of todo) {
303
+ const votes = new Map(), cand = [];
304
+ for (let v = ps[p]; v < ps[p + 1]; v++)
305
+ for (const q of ptPrims.get(vp[v]) ?? []) {
306
+ if (q === p)
307
+ continue;
308
+ if (primChart[q] >= 0) {
309
+ votes.set(primChart[q], (votes.get(primChart[q]) ?? 0) + 1);
310
+ for (const t of primTris(q))
311
+ cand.push(t);
312
+ }
313
+ else if (split[q])
314
+ for (const t of primTris(q))
315
+ if (triChart[t] >= 0) {
316
+ votes.set(triChart[t], (votes.get(triChart[t]) ?? 0) + 1);
317
+ cand.push(t);
318
+ }
319
+ }
320
+ let c = -1, bn = 0;
321
+ for (const [k, n] of votes)
322
+ if (n > bn || (n === bn && k < c)) {
323
+ bn = n;
324
+ c = k;
325
+ }
326
+ if (c < 0) {
327
+ next.push(p);
328
+ continue;
329
+ }
330
+ primChart[p] = c;
331
+ degenerate++;
332
+ for (let v = ps[p]; v < ps[p + 1]; v++) {
333
+ // the uv this point already has in chart c (a neighbour's corner), else mapped onto c
334
+ let found = false;
335
+ for (const q of ptPrims.get(vp[v]) ?? []) {
336
+ if (q === p || primChart[q] !== c)
337
+ continue;
338
+ for (let w = ps[q]; w < ps[q + 1]; w++)
339
+ if (vp[w] === vp[v] && vset[w]) {
340
+ vuv[v * 2] = vuv[w * 2];
341
+ vuv[v * 2 + 1] = vuv[w * 2 + 1];
342
+ found = true;
343
+ break;
344
+ }
345
+ if (found)
346
+ break;
347
+ }
348
+ if (!found) {
349
+ const m = mapOnto(vp[v], c, cand);
350
+ if (m) {
351
+ vuv[v * 2] = m[0];
352
+ vuv[v * 2 + 1] = m[1];
353
+ found = true;
354
+ }
355
+ }
356
+ vset[v] = 1;
357
+ }
358
+ for (const t of primTris(p))
359
+ triChart[t] = c;
360
+ }
361
+ if (next.length === todo.length)
362
+ break;
363
+ todo = next;
364
+ }
365
+ // no charted neighbour at all: one collapsed chart per piece (uv 0; the packer gives it a place of its own)
366
+ const loose = new Map();
367
+ for (const p of todo) {
368
+ let c = loose.get(pieceOf[p]);
369
+ if (c === undefined)
370
+ loose.set(pieceOf[p], (c = nCharts++));
371
+ primChart[p] = c;
372
+ isolated++;
373
+ for (let v = ps[p]; v < ps[p + 1]; v++) {
374
+ vuv[v * 2] = 0;
375
+ vuv[v * 2 + 1] = 0;
376
+ vset[v] = 1;
377
+ }
378
+ for (const t of primTris(p))
379
+ triChart[t] = c;
380
+ }
381
+ }
382
+ /**
383
+ * Split polygon p as output triangles (vertices, corner uv, chart). Vertices the triangulation dropped (collinear on
384
+ * a polygon edge) are put back by fanning the triangle on that edge, uv interpolated along it, so no T-junction opens.
385
+ */
386
+ const splitTris = (p) => {
387
+ const res = [];
388
+ for (const t of primTris(p))
389
+ res.push({ v: [tris[t * 3], tris[t * 3 + 1], tris[t * 3 + 2]], uv: Array.from(cu.subarray(t * 6, t * 6 + 6)), c: triChart[t] });
390
+ const used = new Set();
391
+ for (const tr of res)
392
+ for (const v of tr.v)
393
+ used.add(v);
394
+ const s = ps[p], n = ps[p + 1] - s;
395
+ if (used.size >= n)
396
+ return res;
397
+ let start = -1;
398
+ for (let i = 0; i < n; i++)
399
+ if (used.has(s + i)) {
400
+ start = i;
401
+ break;
402
+ }
403
+ if (start < 0)
404
+ return res;
405
+ let i = start;
406
+ do {
407
+ const a = s + i, run = [];
408
+ let j = (i + 1) % n;
409
+ while (!used.has(s + j)) {
410
+ run.push(s + j);
411
+ j = (j + 1) % n;
412
+ }
413
+ const b = s + j;
414
+ const k = run.length ? res.findIndex((tr) => [0, 1, 2].some((q) => tr.v[q] === a && tr.v[(q + 1) % 3] === b)) : -1;
415
+ if (k >= 0) {
416
+ const tr = res[k], q = tr.v.indexOf(a), qb = (q + 1) % 3, qc = (q + 2) % 3, c = tr.v[qc];
417
+ const chain = [a, ...run, b], len = [0];
418
+ for (let m = 1; m < chain.length; m++) {
419
+ const x = vp[chain[m - 1]] * 3, y = vp[chain[m]] * 3;
420
+ len.push(len[m - 1] + Math.hypot(P[y] - P[x], P[y + 1] - P[x + 1], P[y + 2] - P[x + 2]));
421
+ }
422
+ const L = len[len.length - 1] || 1;
423
+ const at = (m) => [tr.uv[q * 2] + (tr.uv[qb * 2] - tr.uv[q * 2]) * (len[m] / L), tr.uv[q * 2 + 1] + (tr.uv[qb * 2 + 1] - tr.uv[q * 2 + 1]) * (len[m] / L)];
424
+ const fan = [];
425
+ for (let m = 0; m + 1 < chain.length; m++)
426
+ fan.push({ v: [chain[m], chain[m + 1], c], uv: [...at(m), ...at(m + 1), tr.uv[qc * 2], tr.uv[qc * 2 + 1]], c: tr.c });
427
+ res.splice(k, 1, ...fan);
428
+ }
429
+ i = j;
430
+ } while (i !== start);
431
+ return res;
432
+ };
433
+ // ---- output geometry: split polygons become their triangles
434
+ const anySplit = split.some((x) => x === 1);
435
+ let out = g, outChart, outUV;
436
+ const oTris = [], oTriPrim = [];
437
+ if (anySplit) {
438
+ const b = new GeoBuilder(g), map = new Int32Array(g.numPoints);
439
+ for (let i = 0; i < g.numPoints; i++)
440
+ map[i] = b.copyPoint(i);
441
+ const ch = [], uvs = [];
442
+ let vo = 0, po = 0;
443
+ for (let p = 0; p < np; p++) {
444
+ if (split[p]) {
445
+ for (const tr of splitTris(p)) {
446
+ b.addPrim(tr.v.map((v) => map[vp[v]]), true, p, tr.v);
447
+ uvs.push(...tr.uv);
448
+ oTris.push(vo, vo + 1, vo + 2);
449
+ oTriPrim.push(po);
450
+ ch.push(tr.c);
451
+ vo += 3;
452
+ po++;
453
+ }
454
+ continue;
455
+ }
456
+ const vs = [], pts = [];
457
+ for (let v = ps[p]; v < ps[p + 1]; v++) {
458
+ vs.push(v);
459
+ pts.push(map[vp[v]]);
460
+ uvs.push(vuv[v * 2], vuv[v * 2 + 1]);
461
+ }
462
+ b.addPrim(pts, !!g.primClosed[p], p, vs);
463
+ for (const t of primTris(p))
464
+ for (let k = 0; k < 3; k++)
465
+ oTris.push(vo + (tris[t * 3 + k] - ps[p]));
466
+ for (let i = 0; i < tCount[p]; i++)
467
+ oTriPrim.push(po);
468
+ ch.push(primChart[p]);
469
+ vo += vs.length;
470
+ po++;
471
+ }
472
+ out = b.build();
473
+ outChart = Int32Array.from(ch);
474
+ outUV = Float64Array.from(uvs);
475
+ }
476
+ else {
477
+ outChart = primChart;
478
+ outUV = vuv;
479
+ for (let t = 0; t < nt; t++) {
480
+ oTris.push(tris[t * 3], tris[t * 3 + 1], tris[t * 3 + 2]);
481
+ oTriPrim.push(triPrim[t]);
482
+ }
483
+ }
484
+ return finishUnwrap(out, o, outChart, outUV, Uint32Array.from(oTris), Uint32Array.from(oTriPrim), {
485
+ pieces: nPieces, degenerate, firstDegenerate, isolated, straddled, firstStraddled, mirrored, unstretched, refolded, splitInto,
486
+ });
487
+ }
488
+ /** Pack the charts (islands) into the tile(s), lay stacked atlases back into 0..1, and check the result. */
489
+ function finishUnwrap(g, o, chart, uv, T, TP, st) {
490
+ const nt = TP.length, P = g.P, vp = g.vertexPoint;
491
+ // islands: the charts in use, renumbered from 0 (in triangle order)
492
+ const ids = new Map();
493
+ for (let t = 0; t < nt; t++) {
494
+ const c = chart[TP[t]];
495
+ if (c >= 0 && !ids.has(c))
496
+ ids.set(c, ids.size);
497
+ }
498
+ let newChart = new Int32Array(g.numPrims).fill(-1), count = ids.size;
499
+ for (let p = 0; p < g.numPrims; p++) {
500
+ const k = chart[p] >= 0 ? ids.get(chart[p]) : undefined;
501
+ if (k !== undefined)
502
+ newChart[p] = k;
503
+ }
504
+ let merged = 0;
505
+ if (o.mergeThin) {
506
+ const mt = mergeThinCharts(g, uv, newChart, count, { resolution: o.resolution, padding: o.padding });
507
+ if (mt.merged) {
508
+ newChart = mt.labels;
509
+ count = mt.count;
510
+ merged = mt.merged;
511
+ }
512
+ }
513
+ const spill = o.texelsPerUnit > 0 && o.fit !== 'atlas';
514
+ const solid = new Uint8Array(nt);
515
+ for (let t = 0; t < nt; t++) {
516
+ const a = vp[T[t * 3]] * 3, b = vp[T[t * 3 + 1]] * 3, d = vp[T[t * 3 + 2]] * 3;
517
+ const ux = P[b] - P[a], uy = P[b + 1] - P[a + 1], uz = P[b + 2] - P[a + 2], wx = P[d] - P[a], wy = P[d + 1] - P[a + 1], wz = P[d + 2] - P[a + 2];
518
+ const cr = Math.hypot(uy * wz - uz * wy, uz * wx - ux * wz, ux * wy - uy * wx);
519
+ const e2 = ux * ux + uy * uy + uz * uz + wx * wx + wy * wy + wz * wz + (wx - ux) ** 2 + (wy - uy) ** 2 + (wz - uz) ** 2;
520
+ if (cr > 1e-9 * e2)
521
+ solid[t] = 1;
522
+ }
523
+ /** islands of a labelling (triangles, corners, 3D and uv area), packed */
524
+ const pack = (lab, n) => {
525
+ const isl = Array.from({ length: n }, () => ({ verts: [], tris: [], area3: 0, areaUV: 0 }));
526
+ for (let t = 0; t < nt; t++) {
527
+ const k = lab[TP[t]];
528
+ if (k < 0)
529
+ continue;
530
+ const I = isl[k], a = vp[T[t * 3]] * 3, b = vp[T[t * 3 + 1]] * 3, d = vp[T[t * 3 + 2]] * 3;
531
+ const ux = P[b] - P[a], uy = P[b + 1] - P[a + 1], uz = P[b + 2] - P[a + 2], wx = P[d] - P[a], wy = P[d + 1] - P[a + 1], wz = P[d + 2] - P[a + 2];
532
+ I.tris.push(T[t * 3], T[t * 3 + 1], T[t * 3 + 2]);
533
+ I.area3 += Math.hypot(uy * wz - uz * wy, uz * wx - ux * wz, ux * wy - uy * wx) / 2;
534
+ const p0 = T[t * 3] * 2, p1 = T[t * 3 + 1] * 2, p2 = T[t * 3 + 2] * 2;
535
+ I.areaUV += Math.abs((uv[p1] - uv[p0]) * (uv[p2 + 1] - uv[p0 + 1]) - (uv[p2] - uv[p0]) * (uv[p1 + 1] - uv[p0 + 1])) / 2;
536
+ }
537
+ for (let p = 0; p < g.numPrims; p++) {
538
+ const k = lab[p];
539
+ if (k >= 0)
540
+ for (let v = g.primStart[p]; v < g.primStart[p + 1]; v++)
541
+ isl[k].verts.push(v);
542
+ }
543
+ const weights = o.texelWeight ? islandWeights(g, lab, n, o.texelWeight) : null;
544
+ const angles = o.orient === 'world' ? worldAngles(g, uv, lab, n) : null;
545
+ const run = (padding) => packCharts(uv, isl, {
546
+ resolution: o.resolution, padding, turns: o.orient === 'min_rect', orient: o.orient === 'min_rect' ? 'min_rect' : 'keep', scaleMode: 'uniform',
547
+ tiles: spill ? 100 : 1, iterations: o.iterations, seed: 0, weights, angles,
548
+ texelsPerUnit: spill ? o.texelsPerUnit : 0, maxTexelsPerUnit: o.fit === 'atlas' ? o.texelsPerUnit : 0,
549
+ });
550
+ let r = run(o.padding);
551
+ if (!r.packed && o.padding > 0)
552
+ r = run(0);
553
+ return { lab, n, isl, r, weights };
554
+ };
555
+ let best = pack(newChart, count), cut = { cut: 0, parts: count, firstCut: -1 };
556
+ if (o.cutLong) {
557
+ const fixed = o.texelsPerUnit > 0 ? o.texelsPerUnit / o.resolution : 0;
558
+ const c = cutLongCharts(g, uv, newChart, count, { scaleMode: 'uniform', weights: best.weights, tiles: 1, tileScale: spill ? fixed : 0, maxTileScale: spill ? 0 : fixed });
559
+ if (c.cut) {
560
+ // the parts only when they pack denser: a ring's hole often takes the small charts well enough
561
+ const alt = pack(c.labels, c.count);
562
+ if (packsDenser(alt.r, best.r)) {
563
+ best = alt;
564
+ cut = c;
565
+ }
566
+ }
567
+ }
568
+ newChart = best.lab;
569
+ count = best.n;
570
+ const isl = best.isl, r = best.r, weighted = !!best.weights;
571
+ const out = r.packed ? r.uv : uv;
572
+ // fit stack: every atlas in 0..1, told apart by prim atlas
573
+ let atlas = null;
574
+ if (o.fit === 'stack' && r.tilesUsed > 1) {
575
+ atlas = new Int32Array(g.numPrims).fill(-1);
576
+ for (let k = 0; k < isl.length; k++) {
577
+ const v0 = isl[k].verts[0];
578
+ if (v0 === undefined)
579
+ continue;
580
+ const tu = Math.floor(out[v0 * 2]), tv = Math.floor(out[v0 * 2 + 1]);
581
+ for (const v of isl[k].verts) {
582
+ out[v * 2] -= tu;
583
+ out[v * 2 + 1] -= tv;
584
+ }
585
+ const tile = tv * 10 + tu;
586
+ for (let p = 0; p < g.numPrims; p++)
587
+ if (newChart[p] === k)
588
+ atlas[p] = tile;
589
+ }
590
+ }
591
+ // the check: overlaps between charts (never expected; atlases in a stack overlap on purpose) and inside charts (folds)
592
+ const chk = uvOverlapCheck(out, T, TP, newChart, atlas ? { skip: (a, b) => atlas[TP[a]] !== atlas[TP[b]] } : {});
593
+ const U = new Float64Array(nt * 6);
594
+ for (let c = 0; c < nt * 3; c++) {
595
+ U[c * 2] = out[T[c] * 2];
596
+ U[c * 2 + 1] = out[T[c] * 2 + 1];
597
+ }
598
+ const { ok } = uvTriangleAreas(U, nt);
599
+ let collapsed = 0;
600
+ for (let t = 0; t < nt; t++)
601
+ if (solid[t] && !ok[t])
602
+ collapsed++;
603
+ return {
604
+ geo: g, chart: newChart, atlas, ...st, charts: isl.length, tiles: Math.max(1, r.tilesUsed),
605
+ folds: chk.within, foldPair: chk.firstWithin ? chk.firstWithin.t : null, across: chk.across, acrossPair: chk.firstAcross ? chk.firstAcross.t : null,
606
+ collapsed, tris: T, triPrim: TP, uv: out, coverage: r.packed ? r.uvArea / Math.max(1, r.tilesUsed) : 0, cappedBy: r.cappedBy >= 0 ? r.cappedBy : -1,
607
+ cut: cut.cut, cutParts: cut.parts, firstCut: cut.firstCut, weighted, merged,
608
+ };
609
+ }
610
+ /** Charts (triangle lists) whose triangles overlap each other in uv. */
611
+ function foldedCharts(cu, triChart, nCharts) {
612
+ const lists = Array.from({ length: nCharts }, () => []);
613
+ for (let t = 0; t < triChart.length; t++)
614
+ if (triChart[t] >= 0)
615
+ lists[triChart[t]].push(t);
616
+ const out = [];
617
+ for (const list of lists) {
618
+ if (list.length < 2)
619
+ continue;
620
+ const U = new Float64Array(list.length * 6);
621
+ for (let i = 0; i < list.length; i++)
622
+ for (let k = 0; k < 6; k++)
623
+ U[i * 6 + k] = cu[list[i] * 6 + k];
624
+ const { ok, signed } = uvTriangleAreas(U, list.length);
625
+ const valid = [];
626
+ for (let i = 0; i < list.length; i++)
627
+ if (ok[i])
628
+ valid.push(i);
629
+ let fold = false;
630
+ uvOverlapPairs(U, valid, signed, () => { fold = true; }, { skip: () => fold });
631
+ if (fold)
632
+ out.push(list);
633
+ }
634
+ return out;
635
+ }
636
+ /** A chart is stretched when the mean ratio (by 3D area) of the larger to the smaller stretch of its triangles' 3D → uv maps passes this. */
637
+ const STRETCH_MAX = 1.25;
638
+ /**
639
+ * Charts (triangle lists) whose uv is stretched more one way than the other (STRETCH_MAX): xatlas gives very thin charts
640
+ * (bevel strips) a minimum width in its atlas, so they come out stretched across. A conformal map flattens them again.
641
+ */
642
+ function stretchedCharts(g, tris, cu, triChart, nCharts) {
643
+ const P = g.P, vp = g.vertexPoint, sum = new Float64Array(nCharts), wsum = new Float64Array(nCharts);
644
+ for (let t = 0; t < triChart.length; t++) {
645
+ const c = triChart[t];
646
+ if (c < 0)
647
+ continue;
648
+ const a = vp[tris[t * 3]] * 3, b = vp[tris[t * 3 + 1]] * 3, d = vp[tris[t * 3 + 2]] * 3;
649
+ const e1x = P[b] - P[a], e1y = P[b + 1] - P[a + 1], e1z = P[b + 2] - P[a + 2], e2x = P[d] - P[a], e2y = P[d + 1] - P[a + 1], e2z = P[d + 2] - P[a + 2];
650
+ const l1 = Math.hypot(e1x, e1y, e1z), nx = e1y * e2z - e1z * e2y, ny = e1z * e2x - e1x * e2z, nz = e1x * e2y - e1y * e2x, nl = Math.hypot(nx, ny, nz);
651
+ if (!(l1 > 0) || !(nl > 0))
652
+ continue;
653
+ // the triangle in its own plane: (0, 0), (l1, 0), (qx, qy)
654
+ const qx = (e2x * e1x + e2y * e1y + e2z * e1z) / l1, qy = nl / l1;
655
+ const o = t * 6, du1 = cu[o + 2] - cu[o], dv1 = cu[o + 3] - cu[o + 1], du2 = cu[o + 4] - cu[o], dv2 = cu[o + 5] - cu[o + 1];
656
+ // Jacobian J with J (l1, 0) = (du1, dv1) and J (qx, qy) = (du2, dv2)
657
+ const j00 = du1 / l1, j10 = dv1 / l1, j01 = (du2 - j00 * qx) / qy, j11 = (dv2 - j10 * qx) / qy;
658
+ const f = j00 * j00 + j01 * j01 + j10 * j10 + j11 * j11, det = Math.abs(j00 * j11 - j01 * j10);
659
+ if (!(det > 0) || !Number.isFinite(f))
660
+ continue;
661
+ const s1 = Math.sqrt((f + Math.sqrt(Math.max(0, f * f - 4 * det * det))) / 2), w = nl / 2;
662
+ sum[c] += (s1 / (det / s1)) * w;
663
+ wsum[c] += w;
664
+ }
665
+ const lists = Array.from({ length: nCharts }, () => []);
666
+ for (let t = 0; t < triChart.length; t++) {
667
+ const c = triChart[t];
668
+ if (c >= 0 && wsum[c] > 0 && sum[c] / wsum[c] > STRETCH_MAX)
669
+ lists[c].push(t);
670
+ }
671
+ return lists.filter((l) => l.length > 0);
672
+ }
673
+ /** uv for triangles `list` by a least squares conformal map over their shared corners (keyOf), pinned on the border. */
674
+ function lscmChart(g, tris, list, keyOf, cu) {
675
+ const P = g.P, vp = g.vertexPoint, local = new Map(), pos = [], T = new Int32Array(list.length * 3);
676
+ for (let i = 0; i < list.length; i++)
677
+ for (let k = 0; k < 3; k++) {
678
+ const v = tris[list[i] * 3 + k], key = keyOf(v);
679
+ let l = local.get(key);
680
+ if (l === undefined) {
681
+ local.set(key, (l = local.size));
682
+ const p = vp[v] * 3;
683
+ pos.push(P[p], P[p + 1], P[p + 2]);
684
+ }
685
+ T[i * 3 + k] = l;
686
+ }
687
+ // border vertices: on edges used by one triangle
688
+ const edges = new Map(), n = local.size;
689
+ for (let i = 0; i < list.length; i++)
690
+ for (let k = 0; k < 3; k++) {
691
+ const a = T[i * 3 + k], b = T[i * 3 + ((k + 1) % 3)], e = Math.min(a, b) * n + Math.max(a, b);
692
+ edges.set(e, (edges.get(e) ?? 0) + 1);
693
+ }
694
+ const border = new Set();
695
+ for (const [e, c] of edges)
696
+ if (c === 1) {
697
+ border.add(Math.floor(e / n));
698
+ border.add(e % n);
699
+ }
700
+ const r = lscm(Float64Array.from(pos), T, null, border.size ? [...border] : null);
701
+ for (let i = 0; i < list.length; i++)
702
+ for (let k = 0; k < 3; k++) {
703
+ const l = T[i * 3 + k], u = r.uv[l * 2], w = r.uv[l * 2 + 1];
704
+ cu[list[i] * 6 + k * 2] = Number.isFinite(u) ? u : 0;
705
+ cu[list[i] * 6 + k * 2 + 1] = Number.isFinite(w) ? w : 0;
706
+ }
707
+ }
708
+ /**
709
+ * Cut a folded chart (triangles `list`, uv in cu) into parts that do not overlap themselves, keeping the uv: grow a part
710
+ * from its largest remaining triangle across shared edges, adding a neighbour only when it overlaps nothing already in
711
+ * the part; what is left starts the next part.
712
+ */
713
+ function splitFolded(tris, list, keyOf, cu) {
714
+ const n = list.length;
715
+ const edgeOwners = new Map();
716
+ for (let i = 0; i < n; i++)
717
+ for (let k = 0; k < 3; k++) {
718
+ const a = keyOf(tris[list[i] * 3 + k]), b = keyOf(tris[list[i] * 3 + ((k + 1) % 3)]);
719
+ const e = a < b ? `${a},${b}` : `${b},${a}`;
720
+ let l = edgeOwners.get(e);
721
+ if (!l)
722
+ edgeOwners.set(e, (l = []));
723
+ l.push(i);
724
+ }
725
+ const adj = Array.from({ length: n }, () => []);
726
+ for (const l of edgeOwners.values())
727
+ for (const x of l)
728
+ for (const y of l)
729
+ if (x !== y)
730
+ adj[x].push(y);
731
+ // counter-clockwise uv copies, areas and bounding boxes
732
+ const U = new Float64Array(n * 6), area = new Float64Array(n), bb = new Float64Array(n * 4);
733
+ let span = 0;
734
+ for (let i = 0; i < n; i++) {
735
+ const o6 = list[i] * 6, cr = cross2(cu, list[i]);
736
+ if (cr >= 0)
737
+ for (let k = 0; k < 6; k++)
738
+ U[i * 6 + k] = cu[o6 + k];
739
+ else {
740
+ U[i * 6] = cu[o6];
741
+ U[i * 6 + 1] = cu[o6 + 1];
742
+ U[i * 6 + 2] = cu[o6 + 4];
743
+ U[i * 6 + 3] = cu[o6 + 5];
744
+ U[i * 6 + 4] = cu[o6 + 2];
745
+ U[i * 6 + 5] = cu[o6 + 3];
746
+ }
747
+ area[i] = Math.abs(cr) / 2;
748
+ bb[i * 4] = Math.min(U[i * 6], U[i * 6 + 2], U[i * 6 + 4]);
749
+ bb[i * 4 + 1] = Math.min(U[i * 6 + 1], U[i * 6 + 3], U[i * 6 + 5]);
750
+ bb[i * 4 + 2] = Math.max(U[i * 6], U[i * 6 + 2], U[i * 6 + 4]);
751
+ bb[i * 4 + 3] = Math.max(U[i * 6 + 1], U[i * 6 + 3], U[i * 6 + 5]);
752
+ span = Math.max(span, bb[i * 4 + 2] - bb[i * 4], bb[i * 4 + 3] - bb[i * 4 + 1]);
753
+ }
754
+ const cs = Math.max(span, 1e-12);
755
+ const cell = (x) => Math.floor(x / cs);
756
+ const part = new Int32Array(n).fill(-1), parts = [];
757
+ const order = Array.from({ length: n }, (_, i) => i).sort((x, y) => area[y] - area[x] || x - y);
758
+ for (const seed of order) {
759
+ if (part[seed] >= 0)
760
+ continue;
761
+ const id = parts.length, members = [], grid = new Map();
762
+ const overlaps = (i) => {
763
+ for (let y = cell(bb[i * 4 + 1]); y <= cell(bb[i * 4 + 3]); y++)
764
+ for (let x = cell(bb[i * 4]); x <= cell(bb[i * 4 + 2]); x++) {
765
+ for (const j of grid.get(`${x},${y}`) ?? []) {
766
+ if (Math.max(bb[i * 4], bb[j * 4]) >= Math.min(bb[i * 4 + 2], bb[j * 4 + 2]) || Math.max(bb[i * 4 + 1], bb[j * 4 + 1]) >= Math.min(bb[i * 4 + 3], bb[j * 4 + 3]))
767
+ continue;
768
+ if (triIntersectionArea(U.subarray(i * 6, i * 6 + 6), U.subarray(j * 6, j * 6 + 6)) > OVERLAP_REL * Math.min(area[i], area[j]))
769
+ return true;
770
+ }
771
+ }
772
+ return false;
773
+ };
774
+ const add = (i) => {
775
+ part[i] = id;
776
+ members.push(list[i]);
777
+ for (let y = cell(bb[i * 4 + 1]); y <= cell(bb[i * 4 + 3]); y++)
778
+ for (let x = cell(bb[i * 4]); x <= cell(bb[i * 4 + 2]); x++) {
779
+ const k = `${x},${y}`;
780
+ let l = grid.get(k);
781
+ if (!l)
782
+ grid.set(k, (l = []));
783
+ l.push(i);
784
+ }
785
+ };
786
+ add(seed);
787
+ const queue = [...adj[seed]];
788
+ for (let q = 0; q < queue.length; q++) {
789
+ const i = queue[q];
790
+ if (part[i] >= 0 || overlaps(i))
791
+ continue;
792
+ add(i);
793
+ for (const j of adj[i])
794
+ if (part[j] < 0)
795
+ queue.push(j);
796
+ }
797
+ parts.push(members);
798
+ }
799
+ return parts;
800
+ }