/** Copyright Stewart Allen -- All Rights Reserved */ "use strict"; import { config } from './base.js'; import { newPoint } from '../geo/point.js'; /** * converts a geometry point array into a kiri point array * with optional decimation */ export function verticesToPoints(array, options) { let parr = new Array(array.length / 3), i = 0, j = 0, t = Date.now(), hash = {}, unique = 0, passes = 0, points, oldpoints = parr.length, newpoints; let {threshold, precision, maxpass} = options || {}; // threshold = point count for triggering decimation // precision = under which points are merged // maxpass = max number of decimations threshold = threshold > 0 ? threshold : config.decimate_threshold; precision = precision >= 0 ? precision : config.precision_decimate; maxpass = maxpass >= 0 ? maxpass : 0; // replace point objects with their equivalents if (maxpass) { while (i < array.length) { let p = newPoint(array[i++], array[i++], array[i++]), k = p.key, m = hash[k]; if (!m) { m = p; hash[k] = p; unique++; } parr[j++] = m; } } else { while (i < array.length) { parr[j++] = newPoint(array[i++], array[i++], array[i++]); } } // decimate until all point spacing > precision if (maxpass && precision > 0.0) while (parr.length > threshold) { let lines = [], line, dec = 0; for (i=0; i= precision) break; // skip lines where one of the points is already offset if (line.p1.op || line.p2.op) continue; // todo skip dropping lines where either point is a "sharp" on 3 vectors // todo skip dropping lines where either point connects to a "long" line line.p1.op = line.p2.op = line.p1.midPointTo3D(line.p2); dec++; } // exit if nothing to decimate if (dec === 0) break; passes++; // create new facets points = new Array(oldpoints); newpoints = 0; for (i=0; i= maxpass) { break; } } // if (passes) console.trace({passes, threshold, precision, maxpass}); if (passes) console.log({ before: array.length / 3, after: parr.length, unique: unique, decimations: passes, time: (Date.now() - t) }); return parr; } export function pointsToVertices(points) { let vertices = new Float32Array(points.length * 3), i = 0, vi = 0; while (i < points.length) { vertices[vi++] = points[i].x; vertices[vi++] = points[i].y; vertices[vi++] = points[i++].z; } return vertices; }