| 662 | // costs are ~0, prefer collapsing shorter edges first for better triangle |
| 663 | // quality and fewer guard rejections. |
| 664 | const dx = positions[v2*3] - positions[v1*3]; |
| 665 | const dy = positions[v2*3+1] - positions[v1*3+1]; |
| 666 | const dz = positions[v2*3+2] - positions[v1*3+2]; |
| 667 | heap.push(cost + (dx*dx + dy*dy + dz*dz) * 1e-8, |
| 668 | v1, v2, version[v1], version[v2], px, py, pz); |
| 669 | } |
| 670 | |
| 671 | // ── Indexed <-> Non-indexed conversion ────────────────────────────────────── |
| 672 | |
| 673 | // Spatial-hash vertex deduplication via the shared integer-keyed point map |
| 674 | // (open addressing over typed arrays — no BigInt boxing, no Map overhead). |
| 675 | // Same 1e6 weld grid as before: QuantizedPointMap keys on Math.round(c*QUANT), |
| 676 | // which groups identically to the old offset-packed BigInt keys. |
| 677 | function buildIndexed(geometry) { |
| 678 | const QUANT = QUANT_DEFAULT; |
| 679 | const posAttr = geometry.attributes.position; |
| 680 | const n = posAttr.count; |
| 681 | |
| 682 | const positions = new Float64Array(n * 3); // over-allocated, trimmed later |
| 683 | const indexRemap = new Int32Array(n); |
| 684 | let vertCount = 0; |
| 685 | |
| 686 | const vertMap = new QuantizedPointMap(QUANT, Math.min(n, 1 << 22)); |
| 687 | |
| 688 | for (let i = 0; i < n; i++) { |
| 689 | const x = posAttr.getX(i), y = posAttr.getY(i), z = posAttr.getZ(i); |
| 690 | const idx = vertMap.getOrSet(x, y, z, vertCount); |
| 691 | if (vertMap.inserted) { |
| 692 | vertCount++; |
| 693 | positions[idx * 3] = x; |
| 694 | positions[idx * 3 + 1] = y; |
| 695 | positions[idx * 3 + 2] = z; |
| 696 | } |
| 697 | indexRemap[i] = idx; |
| 698 | } |
| 699 | |
| 700 | const faceCount = n / 3; |
| 701 | const faces = new Int32Array(faceCount * 3); |
| 702 | for (let i = 0; i < n; i++) faces[i] = indexRemap[i]; |
| 703 | |
| 704 | return { positions: positions.subarray(0, vertCount * 3), faces, vertCount, faceCount }; |
| 705 | } |
| 706 | |