move roughing depth-first and ease down into prep logic so it can be shared with pocket op

This commit is contained in:
Stewart Allen 2024-06-07 18:10:21 -07:00
commit 1cbc2f59d9
3 changed files with 87 additions and 69 deletions

View file

@ -428,11 +428,10 @@ class OpRough extends CamOp {
prepare(ops, progress) { prepare(ops, progress) {
let { op, state, sliceOut, camFaces } = this; let { op, state, sliceOut, camFaces } = this;
let { setTool, setSpindle, setPrintPoint } = ops; let { setTool, setSpindle, setPrintPoint, sliceOutput } = ops;
let { polyEmit, depthRoughPath } = ops;
let { camOut, newLayer, printPoint } = ops; let { camOut, newLayer, printPoint } = ops;
let { settings, widget } = state; let { settings } = state;
let { process, controller } = settings; let { process } = settings;
let easeDown = process.camEaseDown; let easeDown = process.camEaseDown;
let cutdir = process.camConventional; let cutdir = process.camConventional;
@ -463,54 +462,13 @@ class OpRough extends CamOp {
newLayer(); newLayer();
} }
let total = 0;
for (let slice of sliceOut) {
let polys = [], t = [], c = [];
POLY.flatten(slice.camLines).forEach(function (poly) {
let child = poly.parent;
if (depthFirst) { poly = poly.clone(); poly.parent = child ? 1 : 0 }
if (child) c.push(poly); else t.push(poly);
poly.layer = depthData.layer;
polys.push(poly);
});
// set cut direction on outer polys
POLY.setWinding(t, cutdir);
// set cut direction on inner polys
POLY.setWinding(c, !cutdir);
if (depthFirst) {
depthData.push(polys);
} else {
printPoint = poly2polyEmit(polys, printPoint, function(poly, index, count) {
poly.forEachPoint(function(point, pidx, points, offset) {
camOut(point.clone(), offset !== 0, undefined, count === 1 ? 0.5 : 1);
}, poly.isClosed(), index);
}, { swapdir: false });
newLayer();
}
progress(++total / sliceOut.length, "routing");
}
function isNeg(v) {
return v < 0 || (v === 0 && 1/v === -Infinity);
}
if (depthFirst) {
let ease = op.down && easeDown ? op.down : 0;
let ins = depthData.map(a => a.filter(p => !isNeg(p.depth)));
let itops = ins.map(level => {
return POLY.nest(level.filter(poly => poly.depth === 0).clone());
});
let outs = depthData.map(a => a.filter(p => isNeg(p.depth)));
let otops = outs.map(level => {
return POLY.nest(level.filter(poly => poly.depth === 0).clone());
});
printPoint = depthRoughPath(printPoint, 0, ins, itops, polyEmit, false, ease);
printPoint = depthRoughPath(printPoint, 0, outs, otops, polyEmit, false, ease);
}
setPrintPoint(printPoint); setPrintPoint(printPoint);
sliceOutput(sliceOut, {
cutdir,
depthFirst,
easeDown: op.down && easeDown ? op.down : 0,
progress: (n,m) => progress(n/m, "routing")
});
} }
} }
@ -1419,7 +1377,8 @@ class OpPocket extends CamOp {
prepare(ops, progress) { prepare(ops, progress) {
let { op, state, sliceOut } = this; let { op, state, sliceOut } = this;
let { setTool, setSpindle, setTolerance } = ops; let { setTool, setSpindle, setTolerance, sliceOutput } = ops;
let { process } = state.settings;
setTool(op.tool, op.rate); setTool(op.tool, op.rate);
setSpindle(op.spindle); setSpindle(op.spindle);
@ -1428,11 +1387,12 @@ class OpPocket extends CamOp {
setTolerance(this.topo.tolerance); setTolerance(this.topo.tolerance);
} }
let i=0, l=sliceOut.length; sliceOutput(sliceOut, {
for (let slice of sliceOut.filter(s => s.camLines)) { cutdir: process.camConventional,
ops.emitTrace(slice); depthFirst: process.camDepthFirst && !state.isIndexed,
progress(++i / l, "pocket"); easeDown: op.down && process.easeDown ? op.down : 0,
} progress: (n,m) => progress(n/m, "pocket")
});
} }
} }

View file

@ -428,24 +428,77 @@ function prepEach(widget, settings, print, firstPoint, update) {
); );
} }
function sliceOutput(sliceOut, opts = {}) {
const { cutdir, depthFirst, easeDown, progress } = opts;
let total = 0;
let depthData = [];
for (let slice of sliceOut) {
let polys = [], t = [], c = [];
POLY.flatten(slice.camLines).forEach(function (poly) {
let child = poly.parent;
if (depthFirst) { poly = poly.clone(); poly.parent = child ? 1 : 0 }
if (child) c.push(poly); else t.push(poly);
poly.layer = depthData.layer;
polys.push(poly);
});
// set cut direction on outer polys
POLY.setWinding(t, cutdir);
// set cut direction on inner polys
POLY.setWinding(c, !cutdir);
if (depthFirst) {
depthData.push(polys);
} else {
printPoint = poly2polyEmit(polys, printPoint, function(poly, index, count) {
poly.forEachPoint(function(point, pidx, points, offset) {
camOut(point.clone(), offset !== 0, undefined, count === 1 ? 0.5 : 1);
}, poly.isClosed(), index);
}, { swapdir: false });
newLayer();
}
progress(++total, sliceOut.length);
}
// crucially returns true for -0 as well as other negative #s
function isNeg(v) {
return v < 0 || (v === 0 && 1/v === -Infinity);
}
if (depthFirst) {
let ins = depthData.map(a => a.filter(p => !isNeg(p.depth)));
let itops = ins.map(level => {
return POLY.nest(level.filter(poly => poly.depth === 0).clone());
});
let outs = depthData.map(a => a.filter(p => isNeg(p.depth)));
let otops = outs.map(level => {
return POLY.nest(level.filter(poly => poly.depth === 0).clone());
});
printPoint = depthRoughPath(printPoint, 0, ins, itops, polyEmit, false, easeDown);
printPoint = depthRoughPath(printPoint, 0, outs, otops, polyEmit, false, easeDown);
}
}
// coming from a previous widget, use previous last point // coming from a previous widget, use previous last point
lastPoint = firstPoint; lastPoint = firstPoint;
// make top start offset configurable // make top start offset configurable
printPoint = firstPoint || origin; printPoint = firstPoint || origin;
// accumulated data for depth-first optimiztions // accumulated data for depth-first optimizations
let depthData = { // let depthData = {
rough: [], // rough: [],
outline: [], // outline: [],
roughDiam: 0, // roughDiam: 0,
outlineDiam: 0, // outlineDiam: 0,
contourx: [], // contourx: [],
contoury: [], // contoury: [],
trace: [], // trace: [],
drill: [], // drill: [],
layer: 0, // layer: 0,
}; // };
let ops = { let ops = {
stock, stock,
@ -464,6 +517,7 @@ function prepEach(widget, settings, print, firstPoint, update) {
tip2tipEmit, tip2tipEmit,
depthRoughPath, depthRoughPath,
depthOutlinePath, depthOutlinePath,
sliceOutput,
emitDrills, emitDrills,
emitTrace, emitTrace,
bounds, bounds,

View file

@ -375,6 +375,9 @@ th, tr, td, label {
.dark #speedbar label { .dark #speedbar label {
color: black; color: black;
} }
.dark .widopt {
background-color: #222;
}
/* top menu bar, drop menus */ /* top menu bar, drop menus */
#top, #app-name { #top, #app-name {
@ -926,6 +929,7 @@ th, tr, td, label {
} }
.widopt { .widopt {
background-color: white; background-color: white;
border-radius: 3px;
position: absolute; position: absolute;
top: 0; top: 0;
right: 0; right: 0;