From b92cc893666d7857d8ff724d2362365ee8cbb1ff Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Tue, 7 Sep 2021 13:28:22 -0400 Subject: [PATCH] add better healing algorithm utilizing earcut --- src/geo/mesh.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) diff --git a/src/geo/mesh.js b/src/geo/mesh.js index 9e74e746..f10bde0b 100644 --- a/src/geo/mesh.js +++ b/src/geo/mesh.js @@ -229,11 +229,10 @@ // console.log({loops}); - // emit loops - let faceCount = this.faces.length; - for (let loop of loops) { + // progressive boundary run algorithm + function emitLoop1(loop) { while (loop.length > 2) { - // console.log({loop}); + console.log({loop}); let nuloop = []; // proceed through line pairs for (let i=0, l=loop.length; i= 0) { + dx = Math.max(dx, Math.abs(vertices[lastPoint] - vertices[nextPoint])); + dy = Math.max(dy, Math.abs(vertices[lastPoint+1] - vertices[nextPoint+1])); + dz = Math.max(dz, Math.abs(vertices[lastPoint+2] - vertices[nextPoint+2])); + } + lastPoint = nextPoint; + } + + // swap Z axis with least delta axis + let swap = 2; + if (dy < Math.max(dx,dz)) { + // swap y,z + swap = 1; + } else if (dx < Math.max(dy,dz)) { + // swap x,z + swap = 0; + } + if (swap < 2) { + // console.log({swap_axis: swap}); + for (let point of points) { + let tmpz = point[2]; + point[2] = point[swap]; + point[swap] = tmpz; + } + } + + let fpoints = points.flat(); + let ec = earcut(fpoints, undefined, 3); + // console.log({points, fpoints, ec, dx, dy, dz}); + + for (let point of ec) { + faces.push(pindex[point]); + } + } + + // emit loops + let faceCount = this.faces.length; + for (let loop of loops) { + emitLoop2(loop); + // emitLoop1(loop); + } this.newFaces = this.faces.length - faceCount; // console.log({newFaces: this.newFaces});