experimental async threaded cam topo
This commit is contained in:
parent
237052b65d
commit
db13a61d3b
9 changed files with 158 additions and 35 deletions
|
|
@ -9,6 +9,7 @@
|
|||
// dep: geo.wasm
|
||||
// dep: kiri.codec
|
||||
// dep: kiri-mode.fdm.post
|
||||
// use: kiri-mode.cam.slicer
|
||||
// dep: ext.clip2
|
||||
gapp.register("kiri-run.minion", [], (root, exports) => {
|
||||
|
||||
|
|
@ -21,6 +22,7 @@ const clib = self.ClipperLib;
|
|||
const ctyp = clib.ClipType;
|
||||
const ptyp = clib.PolyType;
|
||||
const cfil = clib.PolyFillType;
|
||||
const cache = self.cache = {};
|
||||
|
||||
let name = "unknown";
|
||||
|
||||
|
|
@ -136,6 +138,38 @@ const funcs = {
|
|||
});
|
||||
},
|
||||
|
||||
camSliceZ: (data, seq) => {
|
||||
const { zarr, set } = data;
|
||||
const slicer = new kiri.cam_slicer();
|
||||
set.points = cache.camPoints;
|
||||
set.options.each = (data, index, total) => {
|
||||
// log({ data, index, total });
|
||||
datas.push({
|
||||
z: data.z,
|
||||
lines: kiri.codec.encodePointArray(data.lines.map(line => [ line.p1, line.p2 ]).flat())
|
||||
});
|
||||
};
|
||||
Object.assign(slicer, set);
|
||||
const datas = [];
|
||||
slicer.slice(zarr);
|
||||
reply({ seq, data: datas });
|
||||
},
|
||||
|
||||
camSetPoints: data => {
|
||||
const points = new Float32Array(data.data.slice());
|
||||
let i = 0, p = 0, realp = new Array(points.length / 3);
|
||||
while (i < points.length) {
|
||||
realp[p++] = base.newPoint(points[i++], points[i++], points[i++]).round(3);
|
||||
}
|
||||
// log({ camSetPoints: points, realp });
|
||||
cache.camPoints = realp;
|
||||
},
|
||||
|
||||
camClearPoints: data => {
|
||||
// log({ camClearPoints: data });
|
||||
delete cache.camPoints;
|
||||
},
|
||||
|
||||
wasm: data => {
|
||||
if (data.enable) {
|
||||
base.wasm_ctrl.enable();
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ let debug = self.debug === true,
|
|||
},
|
||||
wgroup = {},
|
||||
wcache = {},
|
||||
pcache = {},
|
||||
minions = [],
|
||||
minionq = [],
|
||||
minifns = {},
|
||||
|
|
@ -179,6 +180,7 @@ kiri.minions = {
|
|||
reject("concurrent slice unavaiable");
|
||||
}
|
||||
let { each } = options;
|
||||
// todo use shared array buffer?
|
||||
let i = 0, floatP = new Float32Array(points.length * 3);
|
||||
for (let p of points) {
|
||||
floatP[i++] = p.x;
|
||||
|
|
@ -202,11 +204,72 @@ kiri.minions = {
|
|||
});
|
||||
},
|
||||
|
||||
camSliceZ(zarr, slicer, options) {
|
||||
let { points, bounds, zFlat, zLine, zList, zSum, ox } = slicer;
|
||||
let shared = new SharedArrayBuffer(points.length * 3 * 4)
|
||||
let i = 0, floatP = new Float32Array(shared);
|
||||
options = Object.assign(options, slicer.options);
|
||||
options.each();
|
||||
delete options.each;
|
||||
for (let p of points) {
|
||||
floatP[i++] = p.x;
|
||||
floatP[i++] = p.y;
|
||||
floatP[i++] = p.z;
|
||||
}
|
||||
minwork.broadcast('camSetPoints', shared);
|
||||
let ps = [];
|
||||
let step = zarr.length / concurrent;
|
||||
while (zarr.length) {
|
||||
let arr = zarr.slice(0, step);
|
||||
zarr = zarr.slice(step);
|
||||
ps.push(minwork.queueAsync({
|
||||
cmd: "camSliceZ",
|
||||
zarr: arr,
|
||||
set: {
|
||||
bounds,
|
||||
buckets: {},
|
||||
options,
|
||||
zFlat,
|
||||
zLine,
|
||||
zList,
|
||||
zSum,
|
||||
ox
|
||||
}
|
||||
}));
|
||||
}
|
||||
return Promise.all(ps).then(data => {
|
||||
minwork.broadcast('camClearPoints');
|
||||
let result = data
|
||||
.map(data => data.data)
|
||||
.flat()
|
||||
.map(rec => {
|
||||
const slice = kiri.newSlice(rec.z);
|
||||
const points = kiri.codec.decodePointArray(rec.lines);
|
||||
slice.lines = [];
|
||||
points.forEachPair((p1, p2) => {
|
||||
slice.lines.push(base.newLine(p1, p2));
|
||||
});
|
||||
return {
|
||||
z: rec.z,
|
||||
lines: slice.lines,
|
||||
slice
|
||||
};
|
||||
});
|
||||
return result;
|
||||
});
|
||||
},
|
||||
|
||||
queue(work, ondone, direct) {
|
||||
minionq.push({work, ondone, direct});
|
||||
minwork.kick();
|
||||
},
|
||||
|
||||
queueAsync(work, direct) {
|
||||
return new Promise(resolve => {
|
||||
minwork.queue(work, resolve, direct);
|
||||
});
|
||||
},
|
||||
|
||||
kick() {
|
||||
if (minions.length && minionq.length) {
|
||||
let qrec = minionq.shift();
|
||||
|
|
@ -229,6 +292,14 @@ kiri.minions = {
|
|||
enable
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
broadcast(cmd, data, direct) {
|
||||
for (let minion of minions) {
|
||||
minion.postMessage({
|
||||
cmd, data
|
||||
}, direct);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue