add poly union to wasm geo helper

This commit is contained in:
Stewart Allen 2021-06-21 23:26:54 -04:00
commit c9cf45bc40
4 changed files with 89 additions and 6 deletions

View file

@ -1817,7 +1817,14 @@
) {
return null;
} else if (debug) {
console.log("union debug", { src, dst });
console.log("union debug", {
src,
dst,
d0: Math.abs(src[0].poly.area() - dst[0].poly.area()),
d1: Math.abs(src[1].poly.area() - dst[1].poly.area()),
m0: src[0].isEqual2D(dst[0]),
m1: src[1].isEqual2D(dst[1])
});
}
return null;
}

View file

@ -10,7 +10,10 @@ if (!self.window) (function() {
const factor = self.base.config.clipper;
const geo = self.geo;
geo.poly = { offset : polyOffset };
geo.poly = {
union: polyUnion,
offset: polyOffset
};
function log() {
console.log(...arguments);
@ -70,6 +73,27 @@ if (!self.window) (function() {
return polyNest(out);
}
function polyUnion(polys, z) {
let wasm = geo.wasm,
memat = wasm.malloc(1024 * 128),
writer = new DataWriter(wasm.heap, memat),
pcount = 0;
polys.forEach(poly => pcount += writePoly(writer, poly));
let resat = wasm.union(memat, pcount, pcount * factor),
reader = new DataReader(wasm.heap, resat),
out = [];
for (;;) {
let poly = readPoly(reader, z);
if (poly) {
out.push(poly);
} else {
break;
}
}
wasm.free(memat);
return polyNest(out);
}
// nest closed polygons without existing parent / child relationships
function polyNest(polys) {
polys.sort((a,b) => {
@ -120,14 +144,16 @@ if (!self.window) (function() {
let heap = new DataView(exports.memory.buffer);
let wasm = geo.wasm = {
heap,
exports,
memory: exports.memory,
start: exports._start,
offset: exports.poly_offset,
malloc: exports.mem_get,
free: exports.mem_clr
start: exports._start,
free: exports.mem_clr,
union: exports.poly_union,
offset: exports.poly_offset
};
wasm.start();
// wasm.start();
});
})();