add smarter route planning when there are multiple tops to choose from

This commit is contained in:
Stewart Allen 2021-03-18 19:05:48 -04:00
commit 535273b9e0
2 changed files with 46 additions and 1 deletions

View file

@ -29,6 +29,7 @@
trimTo,
expand,
expand_lines,
route,
union,
inset,
nest,
@ -865,4 +866,43 @@
return true;
}
// plan a route through an array of polygon center points
// starting with the polygon center closest to "start"
function route(polys, start) {
let centers = [];
let first, minDist = Infinity;
for (let poly of polys) {
let center = poly.average();
let rec = {poly, center, used: false};
let dist = center.distTo2D(start);
if (dist < minDist) {
first = rec;
minDist = dist;
}
centers.push(rec);
}
first.used = true;
let routed = [ first ];
for (;;) {
let closest;
let minDist = Infinity;
for (let rec of centers) {
if (!rec.used) {
let dist = rec.center.distTo2D(first.center);
if (dist < minDist) {
minDist = dist;
closest = rec;
}
}
}
if (!closest) {
break;
} else {
closest.used = true;
routed.push(first = closest);
}
}
return routed.map(r => r.poly);
}
})();

View file

@ -418,7 +418,12 @@
}
let ltops = tops[depth];
let fitted = fit ? ltops.filter(poly => poly.isInside(fit, 0.01)) : ltops;
fitted.filter(top => !top.level_emit).forEach(top => {
let ftops = fitted.filter(top => !top.level_emit);
if (ftops.length > 1) {
console.log({depth, ftops});
ftops = POLY.route(ftops, start);
}
ftops.forEach(top => {
top.level_emit = true;
let inside = level.filter(poly => poly.isInside(top));
if (ease) {