From 5e49c3191a64957ea719c4242015f16902ee0e00 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sun, 26 Apr 2020 20:19:02 -0400 Subject: [PATCH] fix poly union bug --- js/geo-polygon.js | 15 ++++++++------- js/geo-polygons.js | 23 ++++++++++++++++++++--- js/kiri-driver-sla.js | 11 +++++------ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/js/geo-polygon.js b/js/geo-polygon.js index 8a2abf6d..19ab6055 100644 --- a/js/geo-polygon.js +++ b/js/geo-polygon.js @@ -1224,7 +1224,7 @@ var gs_base_polygon = exports; * @param {Polygon} poly * @returns {?Polygon} intersected polygon or null if no intersection */ - PRO.union = function(poly) { + PRO.union = function(poly, min) { if (!this.overlaps(poly)) return null; var fillang = this.fillang && this.area() > poly.area() ? this.fillang : poly.fillang, @@ -1235,17 +1235,18 @@ var gs_base_polygon = exports; clip = new clib.Clipper(), ctre = new clib.PolyTree(), sp1 = this.toClipper(), - sp2 = poly.toClipper(); + sp2 = poly.toClipper(), + minarea = min >= 0 ? min : 0.1; clip.AddPaths(sp1, ptyp.ptSubject, true); clip.AddPaths(sp2, ptyp.ptClip, true); if (clip.Execute(ctyp.ctUnion, ctre, cfil.pftEvenOdd, cfil.pftEvenOdd)) { - poly = POLY().fromClipperTree(ctre, poly.getZ()); - if (poly.length === 1) { - poly = poly[0]; - poly.fillang = fillang; - return poly; + let union = POLY().fromClipperTreeUnion(ctre, poly.getZ(), minarea); + if (union.length === 1) { + union = union[0]; + union.fillang = fillang; + return union; } } diff --git a/js/geo-polygons.js b/js/geo-polygons.js index 26dc0871..cfea45e0 100644 --- a/js/geo-polygons.js +++ b/js/geo-polygons.js @@ -37,6 +37,7 @@ var gs_base_polygons = exports; toClipper : toClipper, fromClipperNode : fromClipperNode, fromClipperTree : fromClipperTree, + fromClipperTreeUnion: fromClipperTreeUnion, cleanClipperTree : cleanClipperTree, fingerprintCompare: fingerprintCompare, fingerprint: fingerprint @@ -82,6 +83,23 @@ var gs_base_polygons = exports; return polys; }; + function fromClipperTreeUnion(tnode, z, minarea) { + let polys = [], poly; + + tnode.m_Childs.forEach(function(child) { + poly = fromClipperNode(child, z); + if (minarea && poly.area() < minarea) { + return; + } + polys.push(poly); + if (child.m_Childs) { + fromClipperTreeUnion(child, z, minarea); + } + }); + + return polys; + }; + function cleanClipperTree(tree) { var clib = self.ClipperLib, clip = clib.Clipper; @@ -303,10 +321,9 @@ var gs_base_polygons = exports; * Print.init w/ brims * * @param {Polygon[]} polys - * @param {number} [z] * @returns {Polygon[]} */ - function union(polys) { + function union(polys, minarea) { if (polys.length < 2) return polys; var out = polys.slice(), i, j, union, uset = []; @@ -315,7 +332,7 @@ var gs_base_polygons = exports; if (!out[i]) continue; for (j=i+1; j t.poly.clone())); }); // p.clone prevents inner voids from forming - let union = POLY.union(polys).map(p => p.clone()); + let union = POLY.union(polys,0).map(p => p.clone()); let expand = POLY.expand(union, off, zoff, [], 1); let lastraft; for (let s=0; s { if (slice.supports) { - // todo unioning seems to be failing - // slice.supports = POLY.union(slice.supports); + slice.supports = POLY.union(slice.supports,0); } }); } @@ -548,14 +547,14 @@ let gs_kiri_sla = exports; } if (!cached) { - fill = POLY.union(fill); + fill = POLY.union(fill,0); fill_cache[seq_c] = fill; } else { fill = cached.slice().map(p => p.clone(true).setZ(slice.z)); } fill = POLY.trimTo(fill, slice.tops.map(t => t.poly)); - fill = POLY.union(slice.solids.unioned.appendAll(fill)); + fill = POLY.union(slice.solids.unioned.appendAll(fill),0); slice.solids.unioned = fill; }