From 955d9b9a3fdaf5ff6af47fa0883b36e266c01ae5 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Thu, 9 Dec 2021 21:33:05 -0500 Subject: [PATCH] speed up face generation for healing --- src/geo/mesh.js | 48 +++++++++++++++++++++++++++++----------------- src/kiri/client.js | 4 ++-- src/kiri/widget.js | 4 ++-- src/kiri/worker.js | 4 ++-- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/geo/mesh.js b/src/geo/mesh.js index 4469e6bd..8e72e04b 100644 --- a/src/geo/mesh.js +++ b/src/geo/mesh.js @@ -5,6 +5,7 @@ (function() { const BASE = self.base = self.base || {}; + const TEST = 0; if (BASE.Mesh) { return; @@ -12,7 +13,7 @@ BASE.Mesh = class Mesh { constructor(params = {}) { - this.precision = params.precision || 6; + this.precision = Math.pow(10, params.precision || 6); if (params.vertices) { this.setData(params.vertices, params.faces); } @@ -38,21 +39,24 @@ } // when face/index data is missing, vertices need to be normalized if (!faces) { + if (TEST) console.time('generate faces'); faces = []; let fcac = {}; // seen face hash let fnew = []; // accumulate vertex triplets let nuvt = []; // new vertex list let hash = {}; // find vertex matches let prec = this.precision; + let dups = 0; + // let cull = 0; for (let i=0, l=vertices.length; i l.faces.length === 1); let loops = this.loops = []; - // console.log({edges}); // connect edge lines into closed loops for (let i=0, l=edges.length; i 1) console.log('chirality mismatch fixed'); } line = adjacent[0]; } else if (adjacent.length === 2) { @@ -246,7 +256,7 @@ adjacent[0] : adjacent[1] } else { - // console.log('error adjacent', line, adjacent, loops.length, loop.length); + if (TEST > 1) console.log('error adjacent', line, adjacent, loops.length, loop.length); line.split = adjacent; line = adjacent[0]; } @@ -290,13 +300,14 @@ } } - // console.log({loops}) // store invalid loops for display / culling this.shorts = loops.filter(l => l && l.length <= 2); + if (TEST) console.log({edges, loops, shorts: this.shorts}); + // filter out null and short loops this.loops = loops = loops.filter(l => l && l.length > 2); - // console.log({loops}) + if (TEST > 1) console.log({loops_filtered: loops}); // rotate/flatten to Z plane and use earcut to generate faces // because earcut emits point indexes, no need to un-rotate @@ -353,7 +364,7 @@ let fpoints = points.flat(); let ec = earcut(fpoints, undefined, 3); - // console.log({points, fpoints, ec, dx, dy, dz}); + if (TEST > 2) console.log({points, fpoints, ec, dx, dy, dz}); for (let point of ec) { faces.push(pindex[point]); @@ -367,8 +378,9 @@ } this.newFaces = this.faces.length - faceCount; - // console.log({newFaces: this.newFaces}); + if (TEST) console.log({newFaces: this.newFaces}); + if (TEST) console.timeEnd('heal'); return this; } }; diff --git a/src/kiri/client.js b/src/kiri/client.js index 74deea29..223fceeb 100644 --- a/src/kiri/client.js +++ b/src/kiri/client.js @@ -126,8 +126,8 @@ KIRI.work = { }); }, - heal: function(vertices, callback) { - send("heal", {vertices}, function(output) { + heal: function(vertices, callback, refresh) { + send("heal", {vertices, refresh}, function(output) { callback(output); }); }, diff --git a/src/kiri/widget.js b/src/kiri/widget.js index d059d3fd..ab3acaa4 100644 --- a/src/kiri/widget.js +++ b/src/kiri/widget.js @@ -386,7 +386,7 @@ console.log/** Copyright Stewart Allen -- All Rights Reserved */ } }; - PRO.heal = function(debug) { + PRO.heal = function(debug, refresh) { if (debug) { let mesh = this.debugMesh().heal(); let verts = mesh.vertices; @@ -430,7 +430,7 @@ console.log/** Copyright Stewart Allen -- All Rights Reserved */ this.modified = false; } resolve(this.modified); - }); + }, refresh); }); }; diff --git a/src/kiri/worker.js b/src/kiri/worker.js index b66fe395..5834c677 100644 --- a/src/kiri/worker.js +++ b/src/kiri/worker.js @@ -203,9 +203,9 @@ KIRI.worker = { }, heal: function(data, send) { - let { vertices } = data; + let { vertices, refresh } = data; let mesh = new base.Mesh({vertices}).heal(); - if (mesh.newFaces) { + if (mesh.newFaces || refresh) { vertices = mesh.unrolled().toFloat32(); send.done({vertices}, [vertices]); } else {