From c86c90d91507621b61d3dfa386c600804d901160 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Mon, 3 Jan 2022 22:57:19 -0500 Subject: [PATCH] add heal test --- src/main/mesh.js | 17 +++++++++++++++-- src/mesh/model.js | 7 ++----- src/mesh/tool.js | 3 +++ src/mesh/work.js | 15 ++++++++++++++- src/moto/client.js | 3 ++- src/moto/space.js | 11 ++++++++++- 6 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/main/mesh.js b/src/main/mesh.js index 20f77d5e..1664fd59 100644 --- a/src/main/mesh.js +++ b/src/main/mesh.js @@ -67,10 +67,11 @@ function init() { function restore_space() { mesh.db.space.iterate({ map: true }).then(cached => { + let loaded = false; for (let [id, data] of Object.entries(cached)) { // restore group if (Array.isArray(data)) { - let models = data.map(id => { + let models = loaded = data.map(id => { let md = cached[id]; return new mesh.model(md, id); }); @@ -81,6 +82,10 @@ function restore_space() { .focus(); } } + if (loaded) { + let space = moto.Space; + space.view.load(space.preset.top); + } }) } @@ -188,7 +193,15 @@ function space_init(data) { space.view.home(); break; case 'KeyF': - space.view.front(); + for (let m of selection.models()) { + moto.client.fn.model_heal(m.id) + .then(data => { + if (data) { + m.wireframe(false); + m.reload(data.vertices.toFloat32()); + } + }); + } break; case 'KeyT': space.view.top(); diff --git a/src/mesh/model.js b/src/mesh/model.js index 78271169..91b9f896 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -95,7 +95,6 @@ mesh.model = class MeshModel extends mesh.object { geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); let meh = new THREE.Mesh(geo, materials.unselected); geo.computeVertexNormals(); - // geo.computeFaceNormals(); meh.receiveShadow = true; meh.castShadow = true; meh.renderOrder = 1; @@ -108,15 +107,13 @@ mesh.model = class MeshModel extends mesh.object { reload(vertices, indices) { let geo = this.mesh.geometry; - geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); + geo.attributes.position.array = vertices; + geo.attributes.position.needsUpdate = true; geo.setAttribute('normal', undefined); if (indices) { geo.setIndex(new THREE.BufferAttribute(indices, 1)); - geo.attributes.index.needsUpdate = true; } - geo.attributes.position.needsUpdate = true; geo.computeVertexNormals(); - // geo.computeFaceNormals(); worker.model_load({id: this.id, name: this.name, vertices, indices}); } diff --git a/src/mesh/tool.js b/src/mesh/tool.js index eba6f59a..4cd82c37 100644 --- a/src/mesh/tool.js +++ b/src/mesh/tool.js @@ -13,6 +13,7 @@ if (mesh.tool) return; /** tool for identiying defects and healing them **/ mesh.tool = class MeshTool { constructor(params = {}) { + this.debug = params.debug; this.precision = Math.pow(10, params.precision || 6); if (params.vertices) { this.setData(params.vertices, params.faces); @@ -37,6 +38,7 @@ mesh.tool = class MeshTool { if (vertices.length % 3 !== 0) { throw "invalid vertices"; } + let TEST = this.debug; // when face/index data is missing, vertices need to be normalized if (!faces) { if (TEST) console.time('generate faces'); @@ -110,6 +112,7 @@ mesh.tool = class MeshTool { * connect lines into polys. earcut polys into new faces. */ heal() { + let TEST = this.debug; if (TEST) console.time('heal'); let vertices = this.vertices; diff --git a/src/mesh/work.js b/src/mesh/work.js index 3f68d49a..47f55125 100644 --- a/src/mesh/work.js +++ b/src/mesh/work.js @@ -32,7 +32,6 @@ let model = { let { vertices, name, id } = data; let geo = new THREE.BufferGeometry(); geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); - geo.computeVertexNormals(); cacheUpdate(id, { name, geo, matrix: core_matrix.clone() }); }, @@ -44,6 +43,20 @@ let model = { geo.applyMatrix4(m4); // for debugging state / matrix ops return geo.attributes.position.array; + }, + + heal(id) { + dbug.log({healing: id}); + let tool = new mesh.tool({ + vertices: cache[id].geo.attributes.position.array + }); + dbug.log('...imported data'); + tool.heal(); + dbug.log('...healed'); + dbug.log(tool); + return tool.newFaces ? { + vertices: tool.unrolled(), + } : 0; } }; diff --git a/src/moto/client.js b/src/moto/client.js index fe3357ff..3f7743c6 100644 --- a/src/moto/client.js +++ b/src/moto/client.js @@ -71,11 +71,12 @@ let client = moto.client = { let ondata, ondone, onerror = reject; if (!stream) { let accum = []; + let simple = options.simple !== false; ondata = (data) => { accum.push(data); }; ondone = () => { - resolve(accum); + resolve(simple && accum.length === 1 ? accum[0] : accum); }; } else { ondata = stream; diff --git a/src/moto/space.js b/src/moto/space.js index c2d7b6ff..3f296d5d 100644 --- a/src/moto/space.js +++ b/src/moto/space.js @@ -1019,6 +1019,15 @@ get world() { throw "platform.world deprecated" } }, + preset: { + top: {left: home, up: 0, panX, panY, panZ}, + back: {left: PI, up: PI2, panX, panY, panZ}, + home: {left: home, up, panX, panY, panZ}, + front: {left: 0, up: PI2, panX, panY, panZ}, + right: {left: PI2, up: PI2, panX, panY, panZ}, + left: {left: -PI2, up: PI2, panX, panY, panZ}, + }, + view: { top: (then) => { tweenCam({left: home, up: 0, panX, panY, panZ, then}) }, back: (then) => { tweenCam({left: PI, up: PI2, panX, panY, panZ, then}) }, @@ -1027,7 +1036,7 @@ right: (then) => { tweenCam({left: PI2, up: PI2, panX, panY, panZ, then}) }, left: (then) => { tweenCam({left: -PI2, up: PI2, panX, panY, panZ, then}) }, reset: () => { viewControl.reset(); requestRefresh() }, - load: (cam) => { viewControl.setPosition(cam) }, + load: (cam) => { viewControl.setPosition(cam); requestRefresh() }, save: () => { return viewControl.getPosition(true) }, panTo: (x,y,z) => { tweenCamPan(x,y,z) }, setZoom: (r,v) => { viewControl.setZoom(r,v) },