add concurrent fill

This commit is contained in:
Stewart Allen 2021-06-20 13:03:30 -04:00
commit cb15501a23
4 changed files with 95 additions and 42 deletions

View file

@ -15,7 +15,9 @@
encode: encode,
decode: decode,
registerDecoder: registerDecoder,
allocFloat32Array: allocFloat32Array
allocFloat32Array: allocFloat32Array,
encodePointArray,
decodePointArray
};
function allocFloat32Array(arg) {

View file

@ -13,32 +13,50 @@ self.alert = function(o) {
console.log(o);
};
function reply(msg) {
self.postMessage(msg);
}
self.onmessage = function(msg) {
let data = msg.data;
switch (data.cmd) {
case "union":
if (!(data.polys && data.polys.length)) {
reply({union: CODEC.encode([])});
return;
}
let polys = CODEC.decode(data.polys);
let union = POLY.union(polys, data.minarea || 0, true);
reply({union: CODEC.encode(union)});
break;
case "top.shells":
let top = CODEC.decode(data.top, {full: true});
let {z, count, offset1, offsetN, fillOffset, opt} = data;
KIRI.driver.FDM.share.doTopShells(z, top, count, offset1, offsetN, fillOffset, opt);
reply({top: CODEC.encode(top, {full: true})});
break;
default:
reply({error: "invalid command"});
break;
}
let cmd = data.cmd;
(funcs[cmd] || funcs.bad)(data);
};
// console.log(`kiri | init mini | ${KIRI.version || "rogue"}`);
function reply(msg, direct) {
self.postMessage(msg, direct);
}
const funcs = {
union: data => {
if (!(data.polys && data.polys.length)) {
reply({union: CODEC.encode([])});
return;
}
let polys = CODEC.decode(data.polys);
let union = POLY.union(polys, data.minarea || 0, true);
reply({union: CODEC.encode(union)});
},
topShells: data => {
let top = CODEC.decode(data.top, {full: true});
let {z, count, offset1, offsetN, fillOffset, opt} = data;
KIRI.driver.FDM.share.doTopShells(z, top, count, offset1, offsetN, fillOffset, opt);
reply({top: CODEC.encode(top, {full: true})});
},
fill: data => {
let polys = CODEC.decode(data.polys);
let { angle, spacing, minLen, maxLen } = data;
let fill = POLY.fillArea(polys, angle, spacing, [], minLen, maxLen);
let arr = new Float32Array(fill.length * 4);
for (let i=0, p=0; p<fill.length; ) {
let pt = fill[p++];
arr[i++] = pt.x;
arr[i++] = pt.y;
arr[i++] = pt.z;
arr[i++] = pt.index;
}
reply({ fill: arr }, [ arr.buffer ]);
},
bad: data => {
reply({error: "invalid command"});
}
};

View file

@ -60,6 +60,29 @@ KIRI.minions = {
});
},
fill: function(polys, angle, spacing, output, minLen, maxLen) {
return new Promise((resolve, reject) => {
if (concurrent === 0) {
resolve(POLY.fillArea(polys, angle, spacing, [], minLen, maxLen));
}
minwork.queue({
cmd: "fill",
polys: KIRI.codec.encode(polys),
angle, spacing, minLen, maxLen
}, data => {
let arr = data.fill;
let fill = [];
for (let i=0; i<arr.length; ) {
let pt = BASE.newPoint(arr[i++], arr[i++], arr[i++]);
pt.index = arr[i++];
fill.push(pt);
}
output.appendAll(fill);
resolve(fill);
});
});
},
queue: function(work, ondone) {
minionq.push({work, ondone});
minwork.kick();