improve object cleaning

This commit is contained in:
Stewart Allen 2024-08-10 14:34:16 -04:00
commit 8f8483058d
6 changed files with 37 additions and 39 deletions

View file

@ -1097,7 +1097,6 @@ const tool = {
let promises = []; let promises = [];
let mcore = new Matrix4().makeRotationX(Math.PI / 2); let mcore = new Matrix4().makeRotationX(Math.PI / 2);
for (let m of models) { for (let m of models) {
// todo - translate vertices with source model's matrix
let p = worker.model_analyze({ id: m.id, opt }).then(data => { let p = worker.model_analyze({ id: m.id, opt }).then(data => {
let { areas, edges } = data.mapped; let { areas, edges } = data.mapped;
let nm = areas.map(area => new mesh.model({ let nm = areas.map(area => new mesh.model({
@ -1161,7 +1160,7 @@ const tool = {
let p = worker.model_heal({ let p = worker.model_heal({
id: m.id, opt id: m.id, opt
}).then(data => { }).then(data => {
data?.length && m.reload(data.vertices); data?.vertices && m.reload(data.vertices);
}); });
promises.push(p); promises.push(p);
} }
@ -1181,7 +1180,12 @@ const tool = {
clean(models) { clean(models) {
models = fallback(models); models = fallback(models);
tool.heal(models, { merge: false }).then(() => { tool.heal(models, {
merge: false,
precision: 2,
dedup: true,
clean: true
}).then(() => {
log('cleaning complete').unpin(); log('cleaning complete').unpin();
util.defer(selection.update); util.defer(selection.update);
}); });

View file

@ -133,11 +133,12 @@ const log = api.log = {
let { age, data, lines, render } = log; let { age, data, lines, render } = log;
let now = Date.now(); let now = Date.now();
data.push({ data.push({
objs: [ dbug.since(), '|', ...arguments ],
text: `${dbug.since()} | ${[...arguments].join(' ')}`, text: `${dbug.since()} | ${[...arguments].join(' ')}`,
time: now time: now
}); });
if (api.isDebug) { if (api.isDebug) {
console.log(data.peek().text); console.log(...data.peek().objs);
} }
while (!log.pinned && data.length && (data.length > lines || now - data[0].time > age)) { while (!log.pinned && data.length && (data.length > lines || now - data[0].time > age)) {
data.shift(); data.shift();

View file

@ -90,12 +90,14 @@ let edges = {
async add() { async add() {
if (selected.length === 2 && selected[0].object === selected[1].object) { if (selected.length === 2 && selected[0].object === selected[1].object) {
let model = selected[0].object.model;
let pos = new Vector3().fromArray(model.meta.pos);
let verts = selected.map(s => [ s.side.p0, s.side.p1 ]).flat(); let verts = selected.map(s => [ s.side.p0, s.side.p1 ]).flat();
// clean up floating point cruft for comparison // clean up floating point cruft for comparison
for (let v of verts) { for (let v of verts) {
v.x = v.x.round(3); v.x = (v.x - pos.x).round(3);
v.y = v.y.round(3); v.y = (v.y - pos.z).round(3);
v.z = v.z.round(3); v.z = (v.z + pos.y).round(3);
} }
// sort points so dups show up next to each other // sort points so dups show up next to each other
let sort = verts.sort((a,b) => { let sort = verts.sort((a,b) => {
@ -112,7 +114,6 @@ let edges = {
point.y !== arr[index - 1].y || point.y !== arr[index - 1].y ||
point.z !== arr[index - 1].z; point.z !== arr[index - 1].z;
}); });
let model = selected[0].object.model;
if (sort.length === 3) { if (sort.length === 3) {
await model.duplicate({ append: [ await model.duplicate({ append: [
sort[0].x, -sort[0].z, sort[0].y, sort[0].x, -sort[0].z, sort[0].y,

View file

@ -13,8 +13,7 @@
gapp.register("mesh.model", [], (root, exports) => { gapp.register("mesh.model", [], (root, exports) => {
const { MeshPhongMaterial, MeshBasicMaterial, LineBasicMaterial } = THREE; const { MeshPhongMaterial, MeshBasicMaterial, LineBasicMaterial } = THREE;
const { BufferGeometry, BufferAttribute, DoubleSide, Mesh} = THREE; const { BufferGeometry, BufferAttribute, DoubleSide, Mesh, Vector3 } = THREE;
const { Quaternion, Box3, Vector3 } = THREE;
const { mesh, moto } = root; const { mesh, moto } = root;
const { space } = moto; const { space } = moto;
const { api } = mesh; const { api } = mesh;
@ -132,21 +131,21 @@ mesh.model = class MeshModel extends mesh.object {
return this.bounds.translate(this.position()); return this.bounds.translate(this.position());
} }
get world_positions() { // get world_positions() {
let pos = this.position(); // let pos = this.position();
return this.geometry // return this.geometry
.clone() // .clone()
.translate(pos.x,pos.y,pos.z) // .translate(pos.x,pos.y,pos.z)
.attributes.position.array; // .attributes.position.array;
} // }
get positions() { get positions() {
return this.geometry.attributes.position.array; return this.geometry.attributes.position.array;
} }
get matrix() { // get matrix() {
return this.mesh.matrixWorld.elements; // return this.matrixWorld.elements;
} // }
get matrixWorld() { get matrixWorld() {
return this.mesh.matrixWorld; return this.mesh.matrixWorld;

View file

@ -9,7 +9,7 @@
// use: mesh.util // use: mesh.util
gapp.register("mesh.object", [], (root, exports) => { gapp.register("mesh.object", [], (root, exports) => {
const { Matrix4, Vector3, Box3, Box3Helper, Quaternion, Euler } = THREE; const { Matrix4, Vector3, Box3, Box3Helper, Quaternion } = THREE;
const { mesh, moto } = root; const { mesh, moto } = root;
const { space } = moto; const { space } = moto;
@ -27,14 +27,14 @@ mesh.object = class MeshObject {
constructor(id) { constructor(id) {
this.id = id || mesh.util.uuid(); this.id = id || mesh.util.uuid();
worker.object_create({id: this.id, type: this.type}); worker.object_create({ id: this.id, type: this.type });
this.log('NEW');
// storage location for meta data like position, scale, rotation // storage location for meta data like position, scale, rotation
this.meta = {}; this.meta = { pos: [0,0,0] };
this.log('NEW');
} }
log() { log() {
// mesh.api.log.emit(this.id, this.type, ...arguments); mesh.api.log.emit(this.id, this.type, ...arguments);
} }
get type() { get type() {
@ -63,7 +63,7 @@ mesh.object = class MeshObject {
// used during state restoration // used during state restoration
applyMeta(meta = {}) { applyMeta(meta = {}) {
this.log('apply-meta', meta); this.log('apply-meta', meta);
this.applyMatrix(meta.matrix); if (meta.pos) this.position(...meta.pos);
this.visible(meta.visible ?? true); this.visible(meta.visible ?? true);
this.metaChanged(meta); this.metaChanged(meta);
return this; return this;
@ -199,7 +199,7 @@ mesh.object = class MeshObject {
this.updateBoundsBox(); this.updateBoundsBox();
space.update(); space.update();
Object.assign(this.meta, values, { Object.assign(this.meta, values, {
matrix: this.object.matrix.elements // matrix: this.object.matrix.elements
}); });
worker.object_meta({ id: this.id, meta: this.meta }); worker.object_meta({ id: this.id, meta: this.meta });
publish.meta({ id: this.id, meta: this.meta }); publish.meta({ id: this.id, meta: this.meta });

View file

@ -50,17 +50,11 @@ function cacheUpdate(id, data) {
// translate original mesh vertices into UI world view (PI/2 rotation on X) // translate original mesh vertices into UI world view (PI/2 rotation on X)
function translate_encode(id) { function translate_encode(id) {
let rec = cache[id]; let rec = cache[id];
let { pos, matrix } = rec; let { pos } = rec;
let mkey = matrix.map(v => v.round(6)).join('-'); let mkey = pos.map(v => v.round(6)).join('-');
// re-translate on missing cache or changed matrix // re-translate on missing cache or changed matrix
if (!rec || !rec.trans || rec.mkey !== mkey) { if (!rec || !rec.trans || rec.mkey !== mkey) {
let geo = rec.geo.clone(); let geo = rec.geo.clone().translate(new Vector3().fromArray(pos));
if (pos) {
geo.translate(new Vector3().fromArray(pos));
} else {
let mat = core_matrix.clone().multiply(new Matrix4().fromArray(matrix));
geo.applyMatrix4(mat);
}
rec.mkey = mkey; rec.mkey = mkey;
rec.trans = geo.attributes.position.array rec.trans = geo.attributes.position.array
} }
@ -71,13 +65,13 @@ function analyze(id, opt = {}) {
let rec = cache[id]; let rec = cache[id];
let { geo, tool } = rec; let { geo, tool } = rec;
if (!tool) { if (!tool) {
tool = rec.tool = new mesh.tool(); tool = rec.tool = new mesh.tool(opt);
} }
if (tool.faces) { if (tool.faces) {
log(`${id} | analysis cached`); log(`${id} | analysis cached`);
} else { } else {
log(`${id} | analyzing...`); log(`${id} | analyzing...`);
tool.generateFaces(geo.attributes.position.array); tool.generateFaces(geo.attributes.position.array, opt);
log(`${id} | patching...`); log(`${id} | patching...`);
tool.patch(opt); tool.patch(opt);
} }
@ -382,7 +376,6 @@ let object = {
meta(data) { meta(data) {
let { id, meta } = data; let { id, meta } = data;
cacheUpdate(id, meta); cacheUpdate(id, meta);
console.log('object_meta', data);
}, },
create(data) { create(data) {