cleanup wasm.js and add ab/ba option to wasm poly diff

This commit is contained in:
Stewart Allen 2021-06-23 00:34:19 -04:00
commit e3a22326d4
3 changed files with 45 additions and 54 deletions

View file

@ -21,6 +21,14 @@ if (!self.window) (function() {
console.log(...arguments); console.log(...arguments);
} }
function writePolys(view, polys) {
let pcount = 0;
for (let poly of polys) {
pcount += writePoly(view, poly);
}
return pcount;
}
function writePoly(view, poly, inner) { function writePoly(view, poly, inner) {
if (inner) { if (inner) {
poly.setCounterClockwise(); poly.setCounterClockwise();
@ -54,26 +62,24 @@ if (!self.window) (function() {
return poly; return poly;
} }
function polyOffset(polys, offset, z) { function readPolys(view, z, out = []) {
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.offset(memat, pcount, offset * factor),
reader = new DataReader(wasm.heap, resat),
out = [];
for (;;) { for (;;) {
let poly = readPoly(reader, z); let poly = readPoly(view, z);
if (poly) { if (poly) {
out.push(poly); out.push(poly);
} else { } else {
break; break;
} }
} }
if (debug) { return out;
console.log({offset: polys, resat, memat, len: resat - memat}); }
}
function polyOffset(polys, offset, z) {
let wasm = geo.wasm,
memat = wasm.malloc(1024 * 128),
pcount = writePolys(new DataWriter(wasm.heap, memat), polys),
resat = wasm.offset(memat, pcount, offset * factor),
out = readPolys(new DataReader(wasm.heap, resat), z);
wasm.free(memat); wasm.free(memat);
return polyNest(out); return polyNest(out);
} }
@ -81,20 +87,9 @@ if (!self.window) (function() {
function polyUnion(polys, z) { function polyUnion(polys, z) {
let wasm = geo.wasm, let wasm = geo.wasm,
memat = wasm.malloc(1024 * 128), memat = wasm.malloc(1024 * 128),
writer = new DataWriter(wasm.heap, memat), pcount = writePolys(new DataWriter(wasm.heap, memat), polys),
pcount = 0; resat = wasm.union(memat, pcount),
polys.forEach(poly => pcount += writePoly(writer, poly)); out = readPolys(new DataReader(wasm.heap, resat), z);
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); wasm.free(memat);
return polyNest(out); return polyNest(out);
} }
@ -103,24 +98,10 @@ if (!self.window) (function() {
let wasm = geo.wasm, let wasm = geo.wasm,
memat = wasm.malloc(1024 * 128), memat = wasm.malloc(1024 * 128),
writer = new DataWriter(wasm.heap, memat), writer = new DataWriter(wasm.heap, memat),
pcountA = 0, pcountA = writePolys(writer, polysA),
pcountB = 0; pcountB = writePolys(writer, polysB),
polysA.forEach(poly => pcountA += writePoly(writer, poly)); resat = wasm.diff(memat, pcountA, pcountB, 1, 0),
polysB.forEach(poly => pcountB += writePoly(writer, poly)); out = readPolys(new DataReader(wasm.heap, resat), z);
let resat = wasm.diff(memat, pcountA, pcountB, (pcountA + pcountB) * factor),
reader = new DataReader(wasm.heap, resat),
out = [];
for (;;) {
let poly = readPoly(reader, z);
if (poly) {
out.push(poly);
} else {
break;
}
}
// if (debug) {
// console.log({diff_resat: resat, memat, delta: resat-memat});
// }
wasm.free(memat); wasm.free(memat);
return polyNest(out); return polyNest(out);
} }

View file

@ -135,26 +135,36 @@ Uint32 poly_union(Uint32 memat, Uint32 polys, float offset) {
} }
__attribute__ ((export_name("poly_diff"))) __attribute__ ((export_name("poly_diff")))
Uint32 poly_diff(Uint32 memat, Uint32 polysA, Uint32 polysB, float offset) { Uint32 poly_diff(Uint32 memat, Uint32 polysA, Uint32 polysB, Uint8 AB, Uint8 BA) {
Paths inA(polysA); Paths inA(polysA);
Paths inB(polysB); Paths inB(polysB);
Paths outs;
Uint32 pos = memat; Uint32 pos = memat;
pos = readPolys(inA, pos, polysA); pos = readPolys(inA, pos, polysA);
pos = readPolys(inB, pos, polysB); pos = readPolys(inB, pos, polysB);
Clipper clip;
clip.AddPaths(inA, ptSubject, true);
clip.AddPaths(inB, ptClip, true);
clip.Execute(ctDifference, outs, pftEvenOdd, pftEvenOdd);
Uint32 resat = pos; Uint32 resat = pos;
pos = writePolys(outs, pos); if (AB) {
Paths outs;
Clipper clip;
clip.AddPaths(inA, ptSubject, true);
clip.AddPaths(inB, ptClip, true);
clip.Execute(ctDifference, outs, pftEvenOdd, pftEvenOdd);
pos = writePolys(outs, pos);
clip.Clear();
}
clip.Clear(); if (BA) {
Paths outs;
Clipper clip;
clip.AddPaths(inB, ptSubject, true);
clip.AddPaths(inA, ptClip, true);
clip.Execute(ctDifference, outs, pftEvenOdd, pftEvenOdd);
pos = writePolys(outs, pos);
clip.Clear();
}
return resat; return resat;
} }

Binary file not shown.