cleanup wasm poly offsetting

This commit is contained in:
Stewart Allen 2020-05-04 20:08:22 -04:00
commit ec5531dbe4
4 changed files with 52 additions and 30 deletions

View file

@ -112,14 +112,18 @@ var gs_base_polygon = exports;
* Polygon Prototype Functions
******************************************************************* */
PRO.toString = function() {
PRO.toString = function(verbose) {
let l;
if (this.inner && this.inner.length) {
l = '/' + this.inner.map(i => i.toString()).join(',');
l = '/' + this.inner.map(i => i.toString(verbose)).join(',');
} else {
l = '';
}
return `P[${this.points.length,this.area().toFixed(2)}${l}]`;
if (verbose) {
return `P[{${this.area().toFixed(2)}}[${this.points.length}](${this.points.map(p=>`${p.x},${p.y}`).join('|')})${l}]`;
} else {
return `P[${this.points.length,this.area().toFixed(2)}${l}]`;
}
};
// return which plane (x,y,z) this polygon is coplanar with
@ -1069,6 +1073,9 @@ var gs_base_polygon = exports;
* @returns {?Polygon[]} returns output array provided as input or new array if not provided
*/
PRO.offset = function(offset, output) {
// let offs = geo.poly.offset([this], -offset, this.getZ());
// if (output) output.appendAll(offs);
// return output || offs;
return POLY().expand([this], -offset, this.getZ(), output);
};

View file

@ -68,7 +68,35 @@ if (!self.window)
}
}
wasm.free(memat);
return out;
return polyNest(out);
}
// nest closed polygons without existing parent / child relationships
function polyNest(polys) {
polys.sort((a,b) => {
return b.bounds.minx - a.bounds.minx;
});
// from smallest to largest, check for enclosing bounds and nest
for (let i=0, il=polys.length; i<il; i++) {
let smaller = polys[i];
// prevent parent poly from being consumed
if (smaller.inner) continue;
for (let j=i+1; j<il; j++) {
let larger = polys[j];
if (larger.bounds.contains(smaller.bounds)) {
larger.addInner(smaller);
break;
}
}
}
let tops = [];
for (let i=0, il=polys.length; i<il; i++) {
let poly = polys[i];
if (!poly.parent) {
tops.push(poly);
}
}
return tops;
}
fetch('/wasm/kiri-geo.wasm')
@ -101,29 +129,6 @@ if (!self.window)
};
wasm.start();
// console.log(polyOffset(
// [ self.base.newPolygon().centerRectangle({x:6,y:6},10,10) ], 1
// ));
// let memat = wasm.malloc(4096);
// let writer = new DataWriter(heap, memat);
// let pcount = writePoly(writer, self.base.newPolygon().centerRectangle({x:6,y:6},10,10));
//
// log({instance, exports, heap, memat});
// log(wasm.heap.buffer.slice(memat, memat+100));
//
// let resat = wasm.offset(memat, pcount, 0.5 * factor);
//
// log('offset', resat);
//
// log(wasm.heap.buffer.slice(resat, resat+100));
//
// let reader = new DataReader(heap, resat);
// let poly = readPoly(reader);
// console.log({poly});
//
// wasm.free(memat);
});
})();

View file

@ -58,6 +58,12 @@ Uint32 poly_offset(Uint32 memat, Uint32 polys, float offset) {
poly++;
}
// clean and simplify polygons
// Paths cleans, simples;
// CleanPolygons(ins, cleans, 250);
// SimplifyPolygons(cleans, simples, pftNonZero);
// ins = simples;
ClipperOffset co;
co.AddPaths(ins, jtMiter, etClosedPolygon);
co.Execute(outs, offset);
@ -65,19 +71,23 @@ Uint32 poly_offset(Uint32 memat, Uint32 polys, float offset) {
Uint32 resat = pos;
for (Path po : outs) {
// polygon(po.size(), 0);
// polygon(po.size(), 1);
struct length16 *ls = (struct length16 *)(mem + pos);
ls->length = po.size();
pos += 2;
for (IntPoint pt : po) {
// point(pt.X, pt.Y);
struct point32 *ip = (struct point32 *)(mem + pos);
ip->x = pt.X;
ip->y = pt.Y;
ip->x = (int)pt.X;
ip->y = (int)pt.Y;
pos += 8;
}
}
// null terminate
struct length16 *ls = (struct length16 *)(mem + pos);
ls->length = 0;
co.Clear();
return resat;

Binary file not shown.