/** Copyright Stewart Allen -- All Rights Reserved */ "use strict"; (function() { const BASE = self.base = self.base || {}; if (BASE.Mesh) { return; } BASE.Mesh = class Mesh { constructor(params = {}) { this.precision = params.precision || 6; if (params.vertices) { this.setData(params.vertices, params.faces); } } /** * set vertices and faces of the mesh. if only vertices are provided * then the data is assumed to be un-indexed and will be indexed into * faces. if faces are provided, they are assumed to be index references * into the vertex data. * * the end result is face data mapped to an indexed set of unique vertices. * * @param {number[]} vertices * @param {number[]} faces */ setData(vertices, faces) { if (!vertices) { throw "missing vertices"; } if (vertices.length % 3 !== 0) { throw "invalid vertices"; } // when face/index data is missing, vertices need to be normalized if (!faces) { faces = []; let nuvt = []; // new vertex list let hash = {}; // find vertex matches let prec = this.precision; for (let i=0, l=vertices.length; i Math.abs(v))); // console.log({d, max}); return this.unit = d.map(v => v / max); } } function diffUnitVector(uv1, uv2) { let diff = 0; for (let i=0; i<3; i++) { diff += Math.abs(uv1[i] - uv2[i]); } return diff; } function getLine(v1, v2) { let s1 = vertices[v1] + vertices[v1+1]*1000 + vertices[v1+2]*100000; let s2 = vertices[v2] + vertices[v2+1]*1000 + vertices[v2+2]*100000; let key = s1 <= s2 ? [v1,v2].join('-') : [v2,v1].join('-'); let line = hash[key]; if (!line) { // point order will be swapped for reconstructed faces // to preserve chirality (assuming face normals are correct) line = hash[key] = new Line(v1, v2); line.key = key; lines.push(line); let lm1 = vmap[v1] = vmap[v1] || []; let lm2 = vmap[v2] = vmap[v2] || []; lm1.push(line); lm2.push(line); } return line; } for (let i=0, l=this.faces.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 l !== line && !l.used && l.faces.length === 1); if (adjacent.length === 0) { break; } if (adjacent.length > 2) { console.log('error adjacent', line, adjacent, loops.length, loop.length); line.error = adjacent; // break; } if (adjacent.length === 1) { if (line.v2 !== adjacent[0].v1) { let tmp = adjacent[0].v1; adjacent[0].v1 = adjacent[0].v2; adjacent[0].v2 = tmp; // console.log('chirality mismatch fixed'); } line = adjacent[0]; } else { // follow edges according to chirality line = adjacent[0].v1 === line.v2 ? adjacent[0] : adjacent[1] } loop.push(line); line.used = true; } // drop co-linear points because they can't be connected into valid faces // fixup remaining line by dropping common point and using dropped line's point // todo: disabled until faces are fixed up, too. otherwise, problems if (false) for (let j=0, jl=loop.length; j !l.del); loops.push(loop); } // console.log({loops}); // progressive boundary run algorithm function emitLoop1(loop) { while (loop.length > 2) { 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 < dx && dy < dz) { // swap y,z swap = 1; } else if (dx < dy && dx < 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}); return this; } }; })();