remove rebuild and other unused code

This commit is contained in:
Stewart Allen 2024-08-10 11:40:24 -04:00
commit 0b28616bfd
4 changed files with 28 additions and 140 deletions

View file

@ -1190,10 +1190,6 @@ const tool = {
log('cleaning complete').unpin();
util.defer(selection.update);
});
},
rebuild(models) {
Promise.all(fallback(models).map(m => m.rebuild()));
}
};

View file

@ -484,8 +484,6 @@ function ui_build() {
div({ class: "menu-items" }, [
menu_item('Flip Normals', tool.invert),
menu_item('Analyze', tool.analyze),
// div({ _: 'Patch', onclick: tool.repair }),
// div({ _: 'rebuild', onclick: tool.rebuild }),
menu_item('Clean', tool.clean),
])
]),

View file

@ -132,6 +132,14 @@ mesh.model = class MeshModel extends mesh.object {
return this.bounds.translate(this.position());
}
get world_positions() {
let pos = this.position();
return this.geometry
.clone()
.translate(pos.x,pos.y,pos.z)
.attributes.position.array;
}
get matrix() {
return this.mesh.matrixWorld.elements;
}
@ -225,9 +233,7 @@ mesh.model = class MeshModel extends mesh.object {
this.log('model-recenter');
let pos = this.position();
let { mid } = this.bounds;
// let moveTo = mid.clone().add(pos);
this.translate(-mid.x, -mid.y, -mid.z);
// this.position(moveTo.x, moveTo.y, moveTo.z);
this.position(mid.x + pos.x, mid.y + pos.y, mid.z + pos.z);
return this;
}
@ -239,7 +245,7 @@ mesh.model = class MeshModel extends mesh.object {
this.log('model-centerto', to);
let pos = this.position();
let { mid } = this.bounds;
let abs = this.world_bounds.mid;//mid.clone().add(pos);
let abs = this.world_bounds.mid;
this.translate(
-mid.x + (abs.x - to.x),
-mid.y + (abs.y - to.y),
@ -293,32 +299,6 @@ mesh.model = class MeshModel extends mesh.object {
return model;
}
rebuild(opt = {}) {
if (this.lines) {
space.scene.remove(this.lines);
this.lines = undefined;
}
if (opt.clean) {
return;
}
worker.model_rebuild({
matrix: this.matrix,
id: this.id
}).then(data => {
let { lines } = data;
if (lines) {
let points = [];
for (let i=0; i<lines.length;) {
points.push(new THREE.Vector3(lines[i++], lines[i++], lines[i++]));
}
let material = new THREE.LineBasicMaterial( { color: 0xdddddd } );
let geometry = new THREE.BufferGeometry().setFromPoints(points);
let segments = this.lines = new THREE.LineSegments(geometry, material);
space.scene.add(segments);
}
});
}
load(vertices) {
this.log('load');
let geo = new BufferGeometry();
@ -575,26 +555,22 @@ mesh.model = class MeshModel extends mesh.object {
split(plane) {
// extract axes from plane and split when present (only z for now)
let { z } = plane;
let m4 = this.mesh.matrix;
return new Promise((resolve,reject) => {
let { id, matrix } = this;
worker.model_split({id, matrix, z}).then(data => {
worker.model_split({ pos: this.world_positions, z }).then(data => {
let { o1, o2 } = data;
let model;
// new model becomes top
if (o1.length)
this.group.add(new mesh.model({
file: `${this.file}-top`,
mesh: o1
}).reCenter());
if (o2.length)
this.group.add(model = new mesh.model({
file: `${this.file}`,
file: `${this.file}-bot`,
mesh: o2
}).applyMatrix4(m4));
if (o1.length) {
// o1 becomes bottom
this.reload(o1);
resolve(model);
} else {
this.remove();
resolve(model);
}
}).reCenter());
this.remove();
resolve(this.group);
});
});
}
@ -628,7 +604,6 @@ mesh.model = class MeshModel extends mesh.object {
// clear face selections (since they've been deleted);
this.sel.faces = [];
this.updateSelections();
this.rebuild({ clean: true });
}
deleteSelections(mode) {
@ -742,7 +717,7 @@ mesh.model = class MeshModel extends mesh.object {
}, 150);
// let mark = Date.now();
worker.model_select({
id: this.id, x, y:-z, z:y, a, b, c, matrix: this.matrix, surface
id: this.id, x, y:-z, z:y, a, b, c, surface
}).then(data => {
// mesh.api.log.emit(`... data time = ${Date.now() - mark}`); mark = Date.now();
if (timer) {

View file

@ -110,34 +110,6 @@ let model = {
});
},
// return new vertices in world coordinates
duplicate(data) {
let { id, matrix, opt } = data;
let array = translate_encode(id, matrix);
if (opt.mirror) {
array = array.slice();
// find max z and invert z
let maxz = -Infinity;
for (let i=2, l=array.length; i<l; i += 3) {
maxz = Math.max(maxz, array[i]);
array[i] = -array[i];
}
for (let i=0, l=array.length; i<l; i += 9) {
// swap first two vertices in face to invert normals
let v1 = array.slice(i, i+3);
for (let j=0; j<3; j++) {
array[i+j] = array[i+j+3];
array[i+j+3] = v1[j];
}
// move part up by maxz to compensate for z inversion
array[i+2] += maxz;
array[i+5] += maxz;
array[i+8] += maxz;
}
}
return array;
},
// merge several model vertices into a single array
merge(recs) {
let arrays = recs.map(rec => translate_encode(rec.id, rec.matrix));
@ -189,11 +161,9 @@ let model = {
// split a model along an axis at a given point
// return two arrays of vertices for each resulting object
split(data) {
let { id, matrix, z } = data;
let { pos, z } = data;
let o1 = []; // new bottom
let o2 = []; // new top
let pos = translate_encode(id, matrix);
let split = [];
let on = [];
let over = [];
let under = [];
@ -276,13 +246,13 @@ let model = {
}
on.length = over.length = under.length = 0;
}
let mi4 = core_matrix.clone().multiply(new Matrix4().fromArray(matrix)).invert();
let b1 = new BufferAttribute(o1.toFloat32(), 3);
let b2 = new BufferAttribute(o2.toFloat32(), 3);
o1 = b1.applyMatrix4(mi4).array;
o2 = b2.applyMatrix4(mi4).array;
// let mi4 = core_matrix.clone().multiply(new Matrix4().fromArray(matrix)).invert();
// let b1 = new BufferAttribute(o1.toFloat32(), 3);
// let b2 = new BufferAttribute(o2.toFloat32(), 3);
// o1 = b1.applyMatrix4(mi4).array;
// o2 = b2.applyMatrix4(mi4).array;
console.log({ edges, split_face: sliceConnect(edges, z), o1, o2 });
return { o1, o2 };
return { o1: o1.toFloat32(), o2: o2.toFloat32() };
},
analyze(data) {
@ -316,13 +286,8 @@ let model = {
// given model and point, locate matching vertices, lines, and faces
select(data) {
let { id, x, y, z, a, b, c, matrix, surface } = data;
let { id, x, y, z, a, b, c, surface } = data;
let { radians, radius, filterZ } = surface;
// translate point into mesh matrix space
let v3 = new Vector3(x,y,z).applyMatrix4(
core_matrix.clone().multiply(new Matrix4().fromArray(matrix)).invert()
);
x = v3.x; y = v3.y; z = v3.z;
const rec = cache[id];
const arr = rec.geo.attributes.position.array;
// distance tolerance for click to vertex (rough distance)
@ -368,52 +333,6 @@ let model = {
return { faces, edges, verts, point };
},
rebuild(data, send) {
let { id, matrix } = data;
log(`${id} | rebuilding...`);
let points = translate_encode(id, matrix);
log(`${id} | ${points.length} points`);
send.async();
let layers = [];
base.slice(points, {
autoDim: true,
flat: true,
both: true,
debug: true,
minstep: 0.25,
}).then(output => {
let { points, slices } = output;
log(`${id} | ${slices.length} slices Z`);
for (let slice of slices) {
for (let line of slice.lines) {
layers.appendAll(util.extract(line.p1));
layers.appendAll(util.extract(line.p2));
}
}
for (let p of points) p.swapXZ();
return base.slice(points, {
autoDim: true,
both: true,
debug: true,
minstep: 0.25,
}).then(output => {
let { points, slices } = output;
log(`${id} | ${slices.length} slices X`);
for (let slice of slices) {
for (let line of slice.lines) {
if (!line.p1.swapped) { line.p1.swapXZ().swapped = true }
if (!line.p2.swapped) { line.p2.swapXZ().swapped = true }
layers.appendAll(util.extract(line.p1));
layers.appendAll(util.extract(line.p2));
}
}
});
}).finally(() => {
log(`${id} | rebuild complete`);
send.done({ lines: layers });
});;
},
gen_gear(data, send) {
const { teeth, module, angle, twist, shaft, offset, height, chamfer } = data;
const { gear, pitch } = new mesh.tool().generateGear(teeth, module, angle, offset);