fix poly union bug

This commit is contained in:
Stewart Allen 2020-04-26 20:19:02 -04:00
commit 5e49c3191a
3 changed files with 33 additions and 16 deletions

View file

@ -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;
}
}

View file

@ -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<out.length; j++) {
if (!out[j]) continue;
union = out[i].union(out[j]);
union = out[i].union(out[j], minarea);
if (union) {
out[i] = null;
out[j] = null;

View file

@ -98,7 +98,7 @@ let gs_kiri_sla = exports;
polys.appendAll(slice.tops.map(t => 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<layers + gap; s++) {
@ -165,7 +165,7 @@ let gs_kiri_sla = exports;
let traces = POLY.nest(POLY.flatten(slice.gatherTraces([])));
let trims = slice.solids.trimmed || [];
traces.appendAll(trims);
let union = POLY.union(traces);
let union = POLY.union(traces,0);
slice.solids.unioned = union;
}, "solid");
} else {
@ -301,8 +301,7 @@ let gs_kiri_sla = exports;
// union support pillars
slices.forEach(slice => {
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;
}