diff --git a/src/geo/wasm.js b/src/geo/wasm.js index 6e414790..243c9f6e 100644 --- a/src/geo/wasm.js +++ b/src/geo/wasm.js @@ -21,6 +21,14 @@ if (!self.window) (function() { 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) { if (inner) { poly.setCounterClockwise(); @@ -54,26 +62,24 @@ if (!self.window) (function() { return poly; } - function polyOffset(polys, offset, 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.offset(memat, pcount, offset * factor), - reader = new DataReader(wasm.heap, resat), - out = []; + function readPolys(view, z, out = []) { for (;;) { - let poly = readPoly(reader, z); + let poly = readPoly(view, z); if (poly) { out.push(poly); } else { break; } } - if (debug) { - console.log({offset: polys, resat, memat, len: resat - memat}); - } + return out; + } + + 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); return polyNest(out); } @@ -81,20 +87,9 @@ if (!self.window) (function() { 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; - } - } + pcount = writePolys(new DataWriter(wasm.heap, memat), polys), + resat = wasm.union(memat, pcount), + out = readPolys(new DataReader(wasm.heap, resat), z); wasm.free(memat); return polyNest(out); } @@ -103,24 +98,10 @@ if (!self.window) (function() { let wasm = geo.wasm, memat = wasm.malloc(1024 * 128), writer = new DataWriter(wasm.heap, memat), - pcountA = 0, - pcountB = 0; - polysA.forEach(poly => pcountA += writePoly(writer, poly)); - polysB.forEach(poly => pcountB += writePoly(writer, poly)); - 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}); - // } + pcountA = writePolys(writer, polysA), + pcountB = writePolys(writer, polysB), + resat = wasm.diff(memat, pcountA, pcountB, 1, 0), + out = readPolys(new DataReader(wasm.heap, resat), z); wasm.free(memat); return polyNest(out); } diff --git a/wasm/kiri-geo.cpp b/wasm/kiri-geo.cpp index 9c82fd06..1b9d412d 100644 --- a/wasm/kiri-geo.cpp +++ b/wasm/kiri-geo.cpp @@ -135,26 +135,36 @@ Uint32 poly_union(Uint32 memat, Uint32 polys, float offset) { } __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 inB(polysB); - Paths outs; Uint32 pos = memat; pos = readPolys(inA, pos, polysA); 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; - 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; } diff --git a/wasm/kiri-geo.wasm b/wasm/kiri-geo.wasm index 2a614abe..7c4a688c 100755 Binary files a/wasm/kiri-geo.wasm and b/wasm/kiri-geo.wasm differ