add (flawed) faces to sketch

This commit is contained in:
Stewart Allen 2024-08-15 13:46:12 -04:00
commit 2ca2dc1c70
5 changed files with 41 additions and 5 deletions

View file

@ -1041,7 +1041,6 @@ function fingerprintCompare(a, b) {
let rb = b[i]; let rb = b[i];
// test poly area // test poly area
if (Math.abs(ra.a - rb.a) > 1) { if (Math.abs(ra.a - rb.a) > 1) {
console.log('area', Math.abs(ra.a - rb.a));
return false; return false;
} }
// test circularity // test circularity

View file

@ -272,6 +272,8 @@ function space_init(data) {
return selection.boundsBox({toggle:true}); return selection.boundsBox({toggle:true});
case 'KeyH': case 'KeyH':
return shiftKey ? selection.hide() : space.view.home(); return shiftKey ? selection.hide() : space.view.home();
case 'KeyS':
return shiftKey ? api.tool.toSketch() : undefined;
case 'KeyT': case 'KeyT':
return shiftKey ? api.tool.triangulate() : space.view.top(); return shiftKey ? api.tool.triangulate() : space.view.top();
case 'KeyZ': case 'KeyZ':

View file

@ -680,7 +680,9 @@ let sketch = {
let add = { let add = {
sketch(opt) { sketch(opt) {
selection.set([ sketch.add(new mesh.sketch(opt)) ]); let nu = new mesh.sketch(opt);
selection.set([ sketch.add(nu) ]);
return nu;
}, },
/** add sketch components */ /** add sketch components */
@ -1071,6 +1073,12 @@ const tool = {
} }
}, },
toSketch() {
for (let m of selection.models()) {
m.selectionToSketch();
}
},
rename(models, type = 'model') { rename(models, type = 'model') {
models = fallback(models, true); models = fallback(models, true);
let model = models[0]; let model = models[0];

View file

@ -496,6 +496,7 @@ function ui_build() {
div({ class: "menu-items" }, [ div({ class: "menu-items" }, [
menu_item('Flip Normals', tool.invert, ['bi-shift','I']), menu_item('Flip Normals', tool.invert, ['bi-shift','I']),
menu_item('Triangulate', tool.triangulate, ['bi-shift','T']), menu_item('Triangulate', tool.triangulate, ['bi-shift','T']),
menu_item('To Sketch', tool.toSketch, ['bi-shift','S']),
menu_item('Analyze', tool.analyze), menu_item('Analyze', tool.analyze),
menu_item('Clean', tool.clean), menu_item('Clean', tool.clean),
]) ])

View file

@ -19,7 +19,7 @@ const { BufferGeometry, BufferAttribute, DoubleSide, Mesh, Vector3, Triangle } =
const { base, mesh, moto } = root; const { base, mesh, moto } = root;
const { space } = moto; const { space } = moto;
const { api } = mesh; const { api } = mesh;
const { newPolygon, polygons } = base; const { newBounds, newPolygon, polygons } = base;
const mapp = mesh; const mapp = mesh;
const worker = moto.client.fn; const worker = moto.client.fn;
@ -637,6 +637,31 @@ mesh.model = class MeshModel extends mesh.object {
this.updateSelections(); this.updateSelections();
} }
selectionToSketch() {
let { polys, quaternion } = this.selectionToRotationPolys();
if (!polys) {
return;
}
let bounds = newBounds();
let zs = 0;
for (let poly of polys) {
bounds.merge(poly.bounds);
zs += poly.getZ();
poly.setZ(0);
}
quaternion.invert();
let normal = new Vector3(0,0,1).applyQuaternion(quaternion);
let center = bounds.center(zs / polys.length).toVector3().applyQuaternion(quaternion);
let sketch = mesh.api.add.sketch({
normal,
center
});
for (let poly of polys) {
sketch.add.polygon({ poly });
}
return sketch;
}
selectionToRotationPolys() { selectionToRotationPolys() {
let selverts = this.collectFacesByMaterialIndex(1); let selverts = this.collectFacesByMaterialIndex(1);
if (selverts?.length) { if (selverts?.length) {
@ -651,7 +676,7 @@ mesh.model = class MeshModel extends mesh.object {
// union and earcut the result // union and earcut the result
let polys = tris.map(t => newPolygon().fromVectors([ t.a, t.b, t.c ])); let polys = tris.map(t => newPolygon().fromVectors([ t.a, t.b, t.c ]));
let union = polygons.union(polys,0,true); let union = polygons.union(polys,0,true);
return { polys: union, normal: targetNorm, quaternion: rotato }; return { polys: union, quaternion: rotato };
} else { } else {
return {}; return {};
} }
@ -663,7 +688,8 @@ mesh.model = class MeshModel extends mesh.object {
let ears = polys.map(p => p.earcut()).flat(); let ears = polys.map(p => p.earcut()).flat();
let nupoints = ears.map(p => p.points).flat().map(p => p.toVector3()); let nupoints = ears.map(p => p.points).flat().map(p => p.toVector3());
// invert quaternion to restore points alignment to original face // invert quaternion to restore points alignment to original face
nupoints.forEach(p => p.applyQuaternion(quaternion.invert())); quaternion.invert();
nupoints.forEach(p => p.applyQuaternion(quaternion));
let nuverts = nupoints.map(p => p.toArray()).flat().toFloat32(); let nuverts = nupoints.map(p => p.toArray()).flat().toFloat32();
// remove selection and append nuverts // remove selection and append nuverts
this.deleteSelections(nuverts); this.deleteSelections(nuverts);