Revert "simplify union to clipper wrappers"

This reverts commit f8cf66e7d5.
This commit is contained in:
Stewart Allen 2021-12-12 13:49:30 -05:00
commit 636bf653fc
2 changed files with 87 additions and 13 deletions

View file

@ -1757,6 +1757,10 @@
return (a1 > a2) ? a2 / a1 : a1 / a2;
};
PRO.areaOrLength = function(poly) {
return this.length === poly.length || this.areaDiff(poly) > 0.98;
};
/**
* return logical OR of two polygons' enclosed areas
*
@ -1765,7 +1769,65 @@
*/
PRO.union = function(poly, min, all) {
if (!this.overlaps(poly)) return null;
return POLY.union([this,poly], min, all);
let fillang = this.fillang && this.area() > poly.area() ? this.fillang : poly.fillang,
clib = self.ClipperLib,
ctyp = clib.ClipType,
ptyp = clib.PolyType,
cfil = clib.PolyFillType,
clip = new clib.Clipper(),
ctre = new clib.PolyTree(),
sp1 = this.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)) {
let union = POLY().fromClipperTreeUnion(ctre, poly.getZ(), minarea);
if (all) {
if (union.length === 2) {
let src = [ this.leftmost(), poly.leftmost() ];
let dst = [ union[0].leftmost(), union[1].leftmost() ];
if (leftmost(src[0], src[1]) === src[1]) {
src.reverse();
}
if (leftmost(dst[0], dst[1]) === dst[1]) {
dst.reverse();
}
if (
src[0].poly && src[1].poly && // missing for cam tabs
src[0].isEqual2D(dst[0]) &&
src[1].isEqual2D(dst[1]) &&
src[0].poly.areaOrLength(dst[0].poly) &&
src[1].poly.areaOrLength(dst[1].poly)
) {
return null;
// } else {
// console.log("union debug", {
// src,
// dst,
// d0: src[0].poly.areaDiff(dst[0].poly),
// d1: src[1].poly.areaDiff(dst[1].poly),
// m0: src[0].isEqual2D(dst[0]),
// m1: src[1].isEqual2D(dst[1])
// });
}
}
return union;
}
if (union.length === 1) {
union = union[0];
union.fillang = fillang;
return union;
} else {
console.trace({check_union_call_path: union, this: this, poly});
}
}
return null;
};
/** ******************************************************************

View file

@ -392,19 +392,31 @@
}
}
let clib = self.ClipperLib,
ctyp = clib.ClipType,
ptyp = clib.PolyType,
clip = new clib.Clipper(),
ctre = new clib.PolyTree();
let out = polys.slice(), i, j, union, uset = [];
clip.AddPaths(toClipper(polys), ptyp.ptSubject, true);
if (clip.Execute(ctyp.ctUnion, ctre)) {
return fromClipperTree(ctre, polys[0].getZ());
} else {
return [];
outer: for (i=0; i<out.length; i++) {
if (!out[i]) continue;
for (j=i+1; j<out.length; j++) {
if (!out[j]) continue;
union = out[i].union(out[j], minarea, all);
if (union) {
out[i] = null;
out[j] = null;
if (all) {
out.appendAll(union);
} else {
out.push(union);
}
continue outer;
}
}
}
for (i=0; i<out.length; i++) {
if (out[i]) uset.push(out[i]);
}
return uset;
}
/**