From e3a22326d44f800faccdbd6039c07de8bf33ddde Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Wed, 23 Jun 2021 00:34:19 -0400 Subject: [PATCH] cleanup wasm.js and add ab/ba option to wasm poly diff --- src/geo/wasm.js | 71 +++++++++++++++++---------------------------- wasm/kiri-geo.cpp | 28 ++++++++++++------ wasm/kiri-geo.wasm | Bin 64929 -> 65053 bytes 3 files changed, 45 insertions(+), 54 deletions(-) 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 2a614abe8290926d2519d9273ea399d4bd3f83c7..7c4a688c717dc98d175e4a11976ce8f0e4f38bdf 100755 GIT binary patch delta 408 zcma)#u`UE*6ov1(|IW1IP%|95$C{O!Xw#n?+kiteH&m49)A-?{#f`-NTn+;73i$;`*VC(Fn$AUBCgg;9R9 zDw7o>)B7u%6PYEaGv1zDJ?l0T_l?c5vxP+_7p|Yl*f*JbL*nGz4SMza7#UgWl^Gl_ zFlH;Us4yt7I38fkQeYHlVp3!Uay9@tOdt*$kh6d>OMq38U4c!3Re=Gh=077)?mwd< zBT$YbOCU;#L4%3GktZ8Sf<>|vSREMz92GEx*&G=`!j4P|7VM4;KsC_{>_GDxm@t$I d=qfM>EMt^XU>8`#sK5pySV08K=JOl