🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

trimesh-boolean

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trimesh-boolean - npm Package Compare versions

Comparing version
0.5.9
to
0.5.10
+87
src/util/indexGroups.js
/**
* @module util/indexGroups
*
* Convert the boolean split GROUPS ({v0,v1,v2} object soup) into a compact INDEXED
* representation: one shared vertex pool + per-group triangles as [i,j,k] index
* triples into that pool.
*
* Why: the soup form stores every triangle's three vertices as separate objects
* (~5-10x heavier than indexed), so consumers that need to render/persist a
* multi-million-triangle result are forced to re-dedupe it themselves — or run out
* of memory. This returns the indexed twin ONCE, cheaply, sharing the pool ACROSS
* all four groups so the seam between aInside/aOutside welds automatically.
*
* Back-compatible: this is additive. The soup `groups` are unchanged; callers opt
* in (bmsBooleanOp `{ indexed: true }`) or call this directly on any soup groups.
*/
/**
* @param {{ aInside?: Array, aOutside?: Array, bInside?: Array, bOutside?: Array }} groups
* soup groups ({ v0, v1, v2 } triangles)
* @param {number} [tolerance=1e-4] - vertex-weld quantization (world units)
* @returns {{
* points: Array<{x:number,y:number,z:number}>,
* groups: { aInside: number[][], aOutside: number[][], bInside: number[][], bOutside: number[][] }
* }}
*/
export function indexGroups(groups, tolerance) {
tolerance = tolerance || 1e-4;
var inv = 1 / tolerance;
var points = [];
var map = new Map();
function id(v) {
// Quantized coordinate key. The groups are already seam-deduplicated upstream,
// so value-identical vertices map to one index; genuinely distinct stay apart.
var key = Math.round(v.x * inv) + "," + Math.round(v.y * inv) + "," + Math.round(v.z * inv);
var i = map.get(key);
if (i === undefined) { i = points.length; points.push({ x: v.x, y: v.y, z: v.z }); map.set(key, i); }
return i;
}
var names = ["aInside", "aOutside", "bInside", "bOutside"];
var out = { points: points, groups: { aInside: [], aOutside: [], bInside: [], bOutside: [] } };
for (var g = 0; g < names.length; g++) {
var arr = groups[names[g]] || [];
var tris = out.groups[names[g]];
for (var i = 0; i < arr.length; i++) {
var t = arr[i];
tris.push([id(t.v0), id(t.v1), id(t.v2)]);
}
}
return out;
}
/**
* Flatten indexed groups into typed arrays: one shared Float64Array of positions
* and a Uint32Array of triangle indices per group. Convenient for transfer/GPU
* upload. Positions are the SAME pool across all groups (indices are global).
*
* @param {ReturnType<typeof indexGroups>} indexed
* @returns {{
* positions: Float64Array,
* index: { aInside: Uint32Array, aOutside: Uint32Array, bInside: Uint32Array, bOutside: Uint32Array }
* }}
*/
export function indexGroupsToTypedArrays(indexed) {
var pts = indexed.points;
var positions = new Float64Array(pts.length * 3);
for (var i = 0; i < pts.length; i++) {
positions[i * 3] = pts[i].x;
positions[i * 3 + 1] = pts[i].y;
positions[i * 3 + 2] = pts[i].z;
}
var names = ["aInside", "aOutside", "bInside", "bOutside"];
var index = {};
for (var g = 0; g < names.length; g++) {
var tris = indexed.groups[names[g]] || [];
var arr = new Uint32Array(tris.length * 3);
for (var t = 0; t < tris.length; t++) {
arr[t * 3] = tris[t][0];
arr[t * 3 + 1] = tris[t][1];
arr[t * 3 + 2] = tris[t][2];
}
index[names[g]] = arr;
}
return { positions: positions, index: index };
}
+1
-1
{
"name": "trimesh-boolean",
"version": "0.5.9",
"version": "0.5.10",
"description": "Triangle mesh boolean operations — supports open surfaces, terrain intersection, and mesh repair",

@@ -5,0 +5,0 @@ "type": "module",

@@ -27,2 +27,3 @@ /**

import { deduplicateSeamVertices } from "../repair/deduplicateVertices.js";
import { indexGroups } from "../util/indexGroups.js";

@@ -66,2 +67,5 @@ /**

* @param {number} [options.tolerance] - Vertex pool tolerance
* @param {boolean} [options.indexed] - Also attach `result.indexed` — a compact
* indexed twin of the groups (shared points pool + per-group [i,j,k] triples).
* Back-compatible: the soup `groups` are unchanged; this is additive + opt-in.
* @returns {{

@@ -293,2 +297,10 @@ * groups: { aInside: Array, aOutside: Array, bInside: Array, bOutside: Array },

// Opt-in INDEXED twin of the groups (back-compatible; soup `groups` unchanged).
// One shared vertex pool + per-group [i,j,k] triples — ~5-10x lighter than soup,
// so consumers can render/persist a multi-million-triangle result without
// re-deduping it themselves (which is where large booleans OOM).
if (opts.indexed) {
result.indexed = indexGroups(groups, opts.tolerance !== undefined ? opts.tolerance : 1e-4);
}
// Step 8) If operation specified, combine groups

@@ -295,0 +307,0 @@ if (operation) {

@@ -257,2 +257,35 @@ /**

// ── Indexed group output ──
export interface SoupGroups {
aInside?: TriangleSoup;
aOutside?: TriangleSoup;
bInside?: TriangleSoup;
bOutside?: TriangleSoup;
}
/** Per-group triangles as [i,j,k] index triples into a shared points pool. */
export interface IndexedGroups {
points: Vertex[];
groups: {
aInside: number[][];
aOutside: number[][];
bInside: number[][];
bOutside: number[][];
};
}
/**
* Convert soup split groups into a compact indexed representation: one shared
* vertex pool + per-group [i,j,k] triples. Also available directly on a
* bmsBooleanOp result via `{ indexed: true }`.
*/
export function indexGroups(groups: SoupGroups, tolerance?: number): IndexedGroups;
/** Flatten indexed groups into a shared Float64Array of positions + per-group Uint32Array indices. */
export function indexGroupsToTypedArrays(indexed: IndexedGroups): {
positions: Float64Array;
index: { aInside: Uint32Array; aOutside: Uint32Array; bInside: Uint32Array; bOutside: Uint32Array };
};
// ── Normals ──

@@ -259,0 +292,0 @@

@@ -38,2 +38,6 @@ /**

export { weldedToSoup as indexedToSoup } from "./repair/weldVertices.js";
// ── Indexed group output (shared pool + [i,j,k] triples) ──
// Also available directly on a bmsBooleanOp result via { indexed: true }.
export { indexGroups, indexGroupsToTypedArrays } from "./util/indexGroups.js";
export { removeDegenerateTriangles } from "./repair/removeDegenerates.js";

@@ -40,0 +44,0 @@ export { extractBoundaryLoops, triangulateLoop, capBoundaryLoops, capBoundaryLoopsSequential } from "./repair/boundaryLoops.js";

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display