From 275666459ee8493f7a471c456f87b7bb06096732 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Wed, 20 Apr 2022 19:35:57 -0400 Subject: [PATCH] add boolean intersect --- src/mesh/api.js | 22 +++++++++++++++++++--- src/mesh/build.js | 1 + src/mesh/work.js | 7 +++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/mesh/api.js b/src/mesh/api.js index dcd36a46..e5e21ac8 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -79,7 +79,7 @@ const selection = { // if model, remove selcted group if (selected.contains(object.group)) { selection.remove(object.group); - tools.remove(objec.group); + tools.remove(object.group); } } selected.addOnce(object); @@ -369,7 +369,7 @@ const tool = { })) .then(data => { let group = api.group.new([new mesh.model({ - file: `unioned`, + file: `union`, mesh: data })]).promote(); api.selection.set([group]); @@ -377,6 +377,22 @@ const tool = { }); }, + intersect(models) { + models = fallback(models); + api.log.emit(`intersect ${models.length} model(s)`).pin(); + worker.model_intersect(models.map(m => { + return { id: m.id, matrix: m.matrix } + })) + .then(data => { + let group = api.group.new([new mesh.model({ + file: `intersect`, + mesh: data + })]).promote(); + api.selection.set([group]); + api.log.emit('intersect complete').unpin(); + }); + }, + subtract(models) { models = fallback(models); api.log.emit(`subtract ${models.length} model(s)`).pin(); @@ -385,7 +401,7 @@ const tool = { })) .then(data => { let group = api.group.new([new mesh.model({ - file: `subtracted`, + file: `subtract`, mesh: data })]).promote(); api.selection.set([group]); diff --git a/src/mesh/build.js b/src/mesh/build.js index 96ad9b50..f83a685e 100644 --- a/src/mesh/build.js +++ b/src/mesh/build.js @@ -382,6 +382,7 @@ function ui_build() { h.div({ _: "bool", class: "head" }), h.button({ _: 'union', onclick: tool.union }), h.button({ _: 'subtract', onclick: tool.subtract }), + h.button({ _: 'intersect', onclick: tool.intersect }), ]), h.div([ h.div({ _: "normal", class: "head" }), diff --git a/src/mesh/work.js b/src/mesh/work.js index 926f2f81..0fdc44a7 100644 --- a/src/mesh/work.js +++ b/src/mesh/work.js @@ -150,6 +150,13 @@ let model = { return base.CSG.toPositionArray(union); }, + intersect(recs) { + let arrays = recs.map(rec => translate_encode(rec.id, rec.matrix)); + let solids = arrays.map(a => base.CSG.fromPositionArray(a)); + let union = base.CSG.intersect(...solids); + return base.CSG.toPositionArray(union); + }, + subtract(recs) { let bases = recs.filter(rec => !rec.tool) .map(rec => translate_encode(rec.id, rec.matrix))