stub in wasm poly diff
This commit is contained in:
parent
501873caf3
commit
5d6d68e2aa
4 changed files with 100 additions and 5 deletions
|
|
@ -299,13 +299,14 @@
|
||||||
min = minArea || 0.1,
|
min = minArea || 0.1,
|
||||||
out = [];
|
out = [];
|
||||||
|
|
||||||
function filter(from, to) {
|
function filter(from, to = []) {
|
||||||
from.forEach(function(poly) {
|
from.forEach(function(poly) {
|
||||||
if (poly.area() >= min) {
|
if (poly.area() >= min) {
|
||||||
to.push(poly);
|
to.push(poly);
|
||||||
out.push(poly);
|
out.push(poly);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return to;
|
||||||
}
|
}
|
||||||
|
|
||||||
// expensive but worth it?
|
// expensive but worth it?
|
||||||
|
|
@ -319,6 +320,9 @@
|
||||||
cleanClipperTree(ctre);
|
cleanClipperTree(ctre);
|
||||||
filter(fromClipperTree(ctre, z, null, null, min), outA);
|
filter(fromClipperTree(ctre, z, null, null, min), outA);
|
||||||
}
|
}
|
||||||
|
let woutA = filter(geo.poly.diff(setA, setB, z));
|
||||||
|
// outA.appendAll(woutA);
|
||||||
|
// console.log({outA, woutA});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (outB) {
|
if (outB) {
|
||||||
|
|
@ -334,6 +338,9 @@
|
||||||
cleanClipperTree(ctre);
|
cleanClipperTree(ctre);
|
||||||
filter(fromClipperTree(ctre, z, null, null, min), outB);
|
filter(fromClipperTree(ctre, z, null, null, min), outB);
|
||||||
}
|
}
|
||||||
|
let woutB = filter(geo.poly.diff(setB, setA, z));
|
||||||
|
// outB.appendAll(woutB);
|
||||||
|
// console.log({outB, woutB});
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ if (!self.window) (function() {
|
||||||
const geo = self.geo;
|
const geo = self.geo;
|
||||||
|
|
||||||
geo.poly = {
|
geo.poly = {
|
||||||
|
diff: polyDiff,
|
||||||
union: polyUnion,
|
union: polyUnion,
|
||||||
offset: polyOffset
|
offset: polyOffset
|
||||||
};
|
};
|
||||||
|
|
@ -94,6 +95,29 @@ if (!self.window) (function() {
|
||||||
return polyNest(out);
|
return polyNest(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function polyDiff(polysA, polysB, z) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wasm.free(memat);
|
||||||
|
return polyNest(out);
|
||||||
|
}
|
||||||
|
|
||||||
// nest closed polygons without existing parent / child relationships
|
// nest closed polygons without existing parent / child relationships
|
||||||
function polyNest(polys) {
|
function polyNest(polys) {
|
||||||
polys.sort((a,b) => {
|
polys.sort((a,b) => {
|
||||||
|
|
@ -149,6 +173,7 @@ if (!self.window) (function() {
|
||||||
malloc: exports.mem_get,
|
malloc: exports.mem_get,
|
||||||
start: exports._start,
|
start: exports._start,
|
||||||
free: exports.mem_clr,
|
free: exports.mem_clr,
|
||||||
|
diff: exports.poly_diff,
|
||||||
union: exports.poly_union,
|
union: exports.poly_union,
|
||||||
offset: exports.poly_offset
|
offset: exports.poly_offset
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ Uint32 poly_offset(Uint32 memat, Uint32 polys, float offset) {
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__ ((export_name("poly_union")))
|
__attribute__ ((export_name("poly_union")))
|
||||||
Uint32 poly_union(Uint32 memat, Uint32 polys) {
|
Uint32 poly_union(Uint32 memat, Uint32 polys, float offset) {
|
||||||
|
|
||||||
using namespace ClipperLib;
|
using namespace ClipperLib;
|
||||||
|
|
||||||
|
|
@ -116,9 +116,72 @@ Uint32 poly_union(Uint32 memat, Uint32 polys) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Clipper clip;
|
Clipper clip;
|
||||||
clip.AddPath(ins[0], ptSubject, true);
|
clip.AddPaths(ins, ptSubject, true);
|
||||||
clip.AddPath(ins[1], ptClip, true);
|
clip.Execute(ctUnion, outs);
|
||||||
clip.Execute(ctUnion, outs, pftEvenOdd, pftEvenOdd);
|
|
||||||
|
Uint32 resat = pos;
|
||||||
|
|
||||||
|
for (Path po : outs) {
|
||||||
|
struct length16 *ls = (struct length16 *)(mem + pos);
|
||||||
|
ls->length = po.size();
|
||||||
|
pos += 2;
|
||||||
|
for (IntPoint pt : po) {
|
||||||
|
struct point32 *ip = (struct point32 *)(mem + pos);
|
||||||
|
ip->x = (int)pt.X;
|
||||||
|
ip->y = (int)pt.Y;
|
||||||
|
pos += 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// null terminate
|
||||||
|
struct length16 *ls = (struct length16 *)(mem + pos);
|
||||||
|
ls->length = 0;
|
||||||
|
|
||||||
|
clip.Clear();
|
||||||
|
|
||||||
|
return resat;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
__attribute__ ((export_name("poly_diff")))
|
||||||
|
Uint32 poly_diff(Uint32 memat, Uint32 polysA, Uint32 polysB, float offset) {
|
||||||
|
|
||||||
|
using namespace ClipperLib;
|
||||||
|
|
||||||
|
Paths inA(polysA);
|
||||||
|
Paths inB(polysB);
|
||||||
|
Paths outs;
|
||||||
|
Uint32 pos = memat;
|
||||||
|
|
||||||
|
for (Uint32 poly = 0; poly < polysA; poly++ ) {
|
||||||
|
struct length16 *ls = (struct length16 *)(mem + pos);
|
||||||
|
Uint16 points = ls->length;
|
||||||
|
pos += 2;
|
||||||
|
while (points-- > 0) {
|
||||||
|
struct point32 *ip = (struct point32 *)(mem + pos);
|
||||||
|
pos += 8;
|
||||||
|
inA[poly] << IntPoint(ip->x, ip->y);
|
||||||
|
}
|
||||||
|
poly++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Uint32 poly = 0; poly < polysB; poly++ ) {
|
||||||
|
struct length16 *ls = (struct length16 *)(mem + pos);
|
||||||
|
Uint16 points = ls->length;
|
||||||
|
pos += 2;
|
||||||
|
while (points-- > 0) {
|
||||||
|
struct point32 *ip = (struct point32 *)(mem + pos);
|
||||||
|
pos += 8;
|
||||||
|
inB[poly] << IntPoint(ip->x, ip->y);
|
||||||
|
}
|
||||||
|
poly++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Clipper clip;
|
||||||
|
clip.AddPaths(inA, ptSubject, true);
|
||||||
|
clip.AddPaths(inB, ptClip, true);
|
||||||
|
clip.Execute(ctDifference, outs, pftEvenOdd, pftEvenOdd);
|
||||||
|
|
||||||
Uint32 resat = pos;
|
Uint32 resat = pos;
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in a new issue