From 5a56453390da1f74b3f1b9469fe84d6a31110724 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sun, 8 Jan 2023 20:33:13 -0500 Subject: [PATCH] borrow bucket map from cam slicer to allow negative z indices --- src/add/three.js | 8 ++++++++ src/geo/slicer.js | 14 ++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/add/three.js b/src/add/three.js index e6e1bae5..b2db4c8c 100644 --- a/src/add/three.js +++ b/src/add/three.js @@ -22,6 +22,14 @@ THREE.computeFaceNormal = function(vA,vB,vC) { return cb; }; +// https://www.npmjs.com/package/three-mesh-bvh +THREE.addBVH = function() { + const { computeBoundsTree, disposeBoundsTree, acceleratedRaycast } = MeshBVHLib; + THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree; + THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree; + THREE.Mesh.prototype.raycast = acceleratedRaycast; +}; + THREE.dispose = function(obj) { if (!obj) { return; diff --git a/src/geo/slicer.js b/src/geo/slicer.js index ea020d2d..88bbaf9e 100644 --- a/src/geo/slicer.js +++ b/src/geo/slicer.js @@ -45,6 +45,7 @@ async function slice(points, options = {}) { zScale, // bucket span in z units zSum = 0.0, // sanity check that points enclose non-zere volume buckets = [], // banded/grouped faces to speed up slice/search + bucketMap = {}, overlapMax = options.overlap || 0.75, bucketMax = options.bucketMax || 100, onupdate = options.onupdate || function() {}, @@ -161,11 +162,6 @@ async function slice(points, options = {}) { return {}; } - // create empty buckets - for (i = 0; i < bucketCount; i++) { - buckets.push({ points: [], slices: [] }); - } - if (bucketCount > 1) { let failAt = (points.length * overlapMax) | 0, bucket; // copy triples into all matching z-buckets @@ -179,7 +175,12 @@ async function slice(points, options = {}) { bM = Math.min(Math.ceil(zM * zScale), bucketCount); // add point to all buckets in range for (j = bm; j < bM; j++) { - bucket = buckets[j].points; + let map = bucketMap[j]; + if (!map) { + map = bucketMap[j] = { points: [], slices: [] }; + buckets.push(map); + } + bucket = map.points; bucket.push(p1); bucket.push(p2); bucket.push(p3); @@ -196,6 +197,7 @@ async function slice(points, options = {}) { // fallback if we can't partition point space if (bucketCount === 1) { buckets = [{ points, slices: [] }]; + bucketMap[0] = buckets[0]; } // create buckets data structure