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