From 646441e5e54ca512ce27914d959e5554cadc12f0 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Thu, 20 Jan 2022 19:21:26 -0500 Subject: [PATCH] partial implementation of regrouping --- src/main/mesh.js | 10 ++++++++-- src/mesh/api.js | 17 +++++++++++++++++ src/mesh/group.js | 5 +++-- src/mesh/model.js | 30 +++++++++++++++++------------- 4 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/main/mesh.js b/src/main/mesh.js index e8fbbb71..5ed209f1 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -102,8 +102,13 @@ function restore_space() { .map(id => { return { id, md: cached[id] } }) .filter(r => r.md) // filter cache misses .map(r => new mesh.model(r.md, r.id).applyMatrix(mcache[r.id])); - mesh.api.log.emit(`restored ${models.length} model(s)`); - mesh.api.group.new(models, id).applyMatrix(mcache[id]); + if (models.length) { + mesh.api.log.emit(`restored ${models.length} model(s)`); + mesh.api.group.new(models, id).applyMatrix(mcache[id]); + } else { + mesh.api.log.emit(`removed empty group ${id}`); + mesh.db.space.remove(id); + } } } // restore global cache only after objects are restored @@ -292,6 +297,7 @@ function space_init(data) { case 'KeyW': return api.wireframe(); case 'KeyG': + if (shiftKey) return api.tool.regroup(); return api.grid(); case 'KeyL': return api.log.toggle(); diff --git a/src/mesh/api.js b/src/mesh/api.js index 658295cd..e6b6356e 100644 --- a/src/mesh/api.js +++ b/src/mesh/api.js @@ -355,6 +355,23 @@ let tool = { tempedit.focus(); }, + regroup(models) { + models = fallback(models, true); + if (models.length === 0) { + return; + } + api.log.emit(`regrouping ${models.length} model(s)`); + let bounds = util.bounds(models); + let { mid} = bounds; + for (let m of models) { + m.ungroup(); + } + mesh.api.group.new(models) + .centerModels() + .position(mid.x, mid.y, mid.z) + .select(); + }, + analyze(models, opt = { compound: true }) { models = fallback(models); api.log.emit('analyzing mesh(es)...').pin(); diff --git a/src/mesh/group.js b/src/mesh/group.js index 3adf83ed..670f90c9 100644 --- a/src/mesh/group.js +++ b/src/mesh/group.js @@ -59,7 +59,8 @@ mesh.group = class MeshGroup extends mesh.object { } // @param model {MeshModel} - remove(model) { + // @param free {boolean} prevent cleanup of model so it can be re-used + remove(model, opt = { free: true }) { // console.log('group',this,'remove',model); // remove all models and group if (arguments.length === 0) { @@ -75,7 +76,7 @@ mesh.group = class MeshGroup extends mesh.object { // trigger sync with worker worker.group_remove({id: this.id, model: model.id}); // ensure worker cleanup of model - model.remove(true); + if (opt.free) model.remove(true); // auto-remove group when empty if (this.group3.children.length === 0) { mesh.api.group.remove(this); diff --git a/src/mesh/model.js b/src/mesh/model.js index 16f3100a..623e9ed4 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -224,19 +224,6 @@ mesh.model = class MeshModel extends mesh.object { return this.vertices / 3; } - // todo -- put model into its own group - ungroup() { - // get current world coordinates - let { mid } = meh.getBoundingBox(); - // transform mesh into world coordinates - // move mesh to origin - // create and add to group - // move group center back to original center - geo.moveMesh(-mid.x, -mid.y, -mid.z); - geo.computeBoundingBox(); - this.move(mid.x, mid.y, mid.z); - } - // get, set, or toggle visibility of model and wireframe visible(bool) { if (bool === undefined) { @@ -417,6 +404,23 @@ mesh.model = class MeshModel extends mesh.object { }); } + // release from a group but remain in memory and storage + // so it can be re-assigned to another group + ungroup() { + this.group.remove(this, { free: false }); + this.group = undefined; + + // get current world coordinates + // let { mid } = this.mesh.getBoundingBox(); + // let { geometry } = this.mesh; + // move group center back to original center + // geometry.moveMesh(-mid.x, -mid.y, -mid.z); + // geometry.computeBoundingBox(); + // this.move(mid.x, mid.y, mid.z); + + return this; + } + // remove model from group and space remove() { if (arguments.length === 0) {