From d61600fbee0436c5a1a0acc137a764db0ced03be Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Mon, 24 Oct 2022 23:34:15 -0400 Subject: [PATCH] abstract csg ops. add error handling in client --- src/geo/csg.js | 49 +++++++++++++++++++++---------------------------- src/mesh/api.js | 10 ++++++++++ 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/geo/csg.js b/src/geo/csg.js index a5a6ef2b..a222fb6d 100644 --- a/src/geo/csg.js +++ b/src/geo/csg.js @@ -13,9 +13,27 @@ const CSG = { // accepts 2 or more arguments with threejs vertex position arrays union() { - mesh.log(`manifold.union ${arguments.length} meshes`); - const args = [...arguments].map(a => new Module.Manifold(a)); - const result = Module.union(...args); + return CSG.moduleOp('manifold.union', 'union', ...arguments); + }, + + // accepts 2 or more arguments with threejs vertex position arrays + // the fist argument is the target mesh. the rest are negatives + subtract() { + return CSG.moduleOp('manifold.subtract', 'difference', ...arguments); + }, + + // accepts 2 or more arguments with threejs vertex position arrays + intersect() { + return CSG.moduleOp('manifold.intersect', 'intersection', ...arguments); + }, + + moduleOp(name, op) { + mesh.log(`${name} ${arguments.length} meshes`); + const args = [...arguments].slice(2).map(a => new Module.Manifold(a)); + if (args.length < 2) { + throw "missing one or more meshes"; + } + const result = Module[op](...args); const output = result.getMesh({ mode: 2 }); const errors = [ ...args, result ].map(m => m.status().value); for (let m of [ ...args, result ]) { @@ -27,31 +45,6 @@ const CSG = { return output; }, - // accepts 2 or more arguments with threejs vertex position arrays - // the fist argument is the target mesh. the rest are negatives - subtract() { - mesh.log(`manifold.subtract ${arguments.length} meshes`); - const args = [...arguments].map(a => new Module.Manifold(a)); - const result = Module.difference(...args); - const output = result.getMesh({ mode: 2 }); - for (let m of [ ...args, result ]) { - m.delete(); - } - return output; - }, - - // accepts 2 or more arguments with threejs vertex position arrays - intersect() { - mesh.log(`manifold.intersect ${arguments.length} meshes`); - const args = [...arguments].map(a => new Module.Manifold(a)); - const result = Module.intersection(...args); - const output = result.getMesh({ mode: 2 }); - for (let m of [ ...args, result ]) { - m.delete(); - } - return output; - }, - toPositionArray(mesh) { const { vertPos, triVerts, vertex, index } = mesh; if (vertex) { diff --git a/src/mesh/api.js b/src/mesh/api.js index 9acaaeea..02d59014 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -428,6 +428,11 @@ const tool = { mesh: data })]).promote(); api.selection.set([group]); + }) + .catch(error => { + api.log.emit(`intersect error: ${error}`); + }) + .finally(() => { api.log.emit('intersect complete').unpin(); }); }, @@ -447,6 +452,11 @@ const tool = { mesh: data })]).promote(); api.selection.set([group]); + }) + .catch(error => { + api.log.emit(`subtract error: ${error}`); + }) + .finally(() => { api.log.emit('subtract complete').unpin(); }); },