diff --git a/.gitignore b/.gitignore index 0750298a..02a385d7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ tmp node_modules package-lock.json logs/* -data/* \ No newline at end of file +data/* diff --git a/src/geo/wasm.js b/src/geo/wasm.js index 7081f37f..6e414790 100644 --- a/src/geo/wasm.js +++ b/src/geo/wasm.js @@ -71,9 +71,9 @@ if (!self.window) (function() { break; } } - // if (debug) { - // console.log({diff_resat: resat, memat}); - // } + if (debug) { + console.log({offset: polys, resat, memat, len: resat - memat}); + } wasm.free(memat); return polyNest(out); } @@ -193,9 +193,11 @@ if (!self.window) (function() { free: exports.mem_clr, diff: exports.poly_diff, union: exports.poly_union, - offset: exports.poly_offset + offset: exports.poly_offset, + set_debug: exports.set_debug }; if (debug) { + wasm.set_debug(1); runTests(); } }); diff --git a/wasm/kiri-geo.cpp b/wasm/kiri-geo.cpp index 5fd628ce..5b6a9812 100644 --- a/wasm/kiri-geo.cpp +++ b/wasm/kiri-geo.cpp @@ -11,7 +11,7 @@ typedef int int32; using namespace ClipperLib; Uint8 *mem = 0; -const Uint8 debug = 0; +Uint8 debug = 0; extern "C" { extern void polygon(Uint32 points, Uint32 inners); @@ -38,27 +38,35 @@ void mem_clr(Uint32 loc) { free((void *)loc); } +Uint32 readPoly(Path path, Uint32 pos) { + struct length16 *ls = (struct length16 *)(mem + pos); + Uint16 points = ls->length; + if (debug) polygon(points, 0); + pos += 2; + while (points-- > 0) { + struct point32 *ip = (struct point32 *)(mem + pos); + pos += 8; + path << IntPoint(ip->x, ip->y); + if (debug) point(ip->x, ip->y); + } + return pos; +} + Uint32 readPolys(Paths paths, Uint32 pos, Uint32 count) { Uint32 poly = 0; if (debug) abc(poly, count, pos); - while (count > 0) { - struct length16 *ls = (struct length16 *)(mem + pos); - Uint16 points = ls->length; - if (debug) polygon(points, 0); - pos += 2; - while (points-- > 0) { - struct point32 *ip = (struct point32 *)(mem + pos); - pos += 8; - paths[poly] << IntPoint(ip->x, ip->y); - if (debug) point(ip->x, ip->y); - } - poly++; - count--; + while (count-- > 0) { + pos = readPoly(paths[poly++], pos); } if (debug) abc(poly, count, pos); return pos; } +__attribute__ ((export_name("set_debug"))) +void set_debug(Uint8 value) { + debug = value; +} + __attribute__ ((export_name("poly_offset"))) Uint32 poly_offset(Uint32 memat, Uint32 polys, float offset) { @@ -69,6 +77,10 @@ Uint32 poly_offset(Uint32 memat, Uint32 polys, float offset) { // pos = readPolys(ins, pos, polys); + // while (polys-- > 0) { + // pos = readPoly(ins[poly++], pos); + // } + while (poly < polys) { struct length16 *ls = (struct length16 *)(mem + pos); Uint16 points = ls->length; @@ -83,7 +95,7 @@ Uint32 poly_offset(Uint32 memat, Uint32 polys, float offset) { poly++; } - if (debug) abc(memat, pos, memat - pos); + if (debug) abc(memat, pos, pos - memat); // clean and simplify polygons // Paths cleans, simples; diff --git a/wasm/kiri-geo.wasm b/wasm/kiri-geo.wasm index 332c1ef1..93ae59f1 100755 Binary files a/wasm/kiri-geo.wasm and b/wasm/kiri-geo.wasm differ