From e1e54494760bf6ebc12d0b6d3bea59d459d804d7 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Sun, 26 Sep 2021 19:12:30 -0400 Subject: [PATCH] add 'smart' trace centerlines when open poly/lines are separated by selected tool diameter --- src/mode/cam/ops.js | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/mode/cam/ops.js b/src/mode/cam/ops.js index e9154b10..a9c88c7c 100644 --- a/src/mode/cam/ops.js +++ b/src/mode/cam/ops.js @@ -787,8 +787,63 @@ } } } + function similar(v1, v2, epsilon = 0.01) { + return Math.abs(v1-v2) <= epsilon; + } + function centerPoly(p1, p2) { + // follow poly with most points + if (p2.length > p1.length) { + let t = p1; + p1 = p2; + p2 = t; + } + let np = newPolygon().setOpen(true); + for (let p of p1.points) { + let q = p2.findClosestPointTo(p); + np.push(p.midPointTo3D(q.point)); + } + return np; + } + function centerPolys(polys) { + // select open polys and sort by length + let ptst = polys.filter(p => p.isOpen()).sort((a,b) => b.perimeter() - a.perimeter()); + if (ptst.length < 2) { + return polys; + } + let pt = newPoint(0,0,0); + // ensure polys are ordered with start point closest to 0,0 + ptst.forEach(p => { + if (p.last().distTo2D(pt) < p.first().distTo2D(pt)) { + p.reverse(); + } + }); + let pout = polys.filter(p => p.isClosed()); + outer: for (let i=0,l=ptst.length; i p)); + return pout; + } // connect selected segments if open and touching polys = healPolys(polys); + // find center line for open polys spaced by tool diameter + polys = centerPolys(polys); switch (op.mode) { case "follow": let routed = [];