From 14023d7c517a7d254adaecb75757786b8405a9e4 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sat, 21 Mar 2020 22:25:31 -0400 Subject: [PATCH] add poly xy alignment and earcut. mostly complete new export --- js/geo-polygon.js | 95 +++++++++++++++++++++++++---------------------- js/meta.js | 90 ++++++++++++++++++++++++++++---------------- 2 files changed, 109 insertions(+), 76 deletions(-) diff --git a/js/geo-polygon.js b/js/geo-polygon.js index c9f83850..d7a25e16 100644 --- a/js/geo-polygon.js +++ b/js/geo-polygon.js @@ -112,13 +112,12 @@ var gs_base_polygon = exports; * Polygon Prototype Functions ******************************************************************* */ - PRO.earcut = function(raw) { - // gather all points into a single array including inner polys - // keeping track of array offset indices for inners - let out = []; - let holes = []; - let last = undefined; + // return which plane (x,y,z) this polygon is coplanar with + PRO.alignment = function() { + if (this._aligned) return this._aligned; + let diff = {x: false, y: false, z: false}; + let last = undefined; // flatten points into array for earcut() this.points.forEach(p => { @@ -128,21 +127,46 @@ var gs_base_polygon = exports; diff.z = diff.z || last.z !== p.z; } last = p; - out.push(p.x, p.y, p.z); }); - // check which plane polygon arrived in - let swapz = diff.x === false ? 0 : - diff.y === false ? 1 : 2; + return this._aligned = + diff.x === false ? 'yz' : + diff.y === false ? 'xz' : 'xy'; + }; - // swap planar coordinates into X,Y if not in Z plane - if (swapz < 2) { - for (let i=0; i { + out.push(p.x, p.y, p.z); + }); // add hole offsets for inner polygons if (this.inner) { @@ -154,37 +178,20 @@ var gs_base_polygon = exports; }); } - // perform earcut(); + // perform earcut() let cut = earcut(out,holes,3); - - // un-swap planar coordinates into X,Y if not in Z plane - // so that polygons are returned in their original plane - if (swapz < 2) { - for (let i=0; i