rewrite drill finding code to be more robust

This commit is contained in:
bakedpotatolord 2025-05-08 11:13:14 -06:00
commit 97e4b640b6
2 changed files with 55 additions and 20 deletions

View file

@ -167,7 +167,7 @@ kiri.load(api => {
// const fresh = widgets.filter(widget => CAM.traces(settings, widget, single)); // const fresh = widgets.filter(widget => CAM.traces(settings, widget, single));
send.done(kiri.codec.encode(fresh.map(widget => { return { send.done(kiri.codec.encode(fresh.map(widget => { return {
id: widget.id, id: widget.id,
holes: widget.holes, holes: widget.drills,
shadowed:widget.shadowedDrills shadowed:widget.shadowedDrills
} } ))); } } )));
} }

View file

@ -474,12 +474,14 @@ CAM.holes = async function(settings, widget, diam) {
.map(kv => parseFloat(kv).round(5)) .map(kv => parseFloat(kv).round(5))
.filter(z => z !== null) .filter(z => z !== null)
)] )]
let individual = (diam <= 0);
diam = individual ? 1 : diam; // sets default diameter when select individual used
let centerDiff = diam * 0.1, let centerDiff = diam * 0.1,
area = (diam/2) * (diam/2) * Math.PI, area = (diam/2) * (diam/2) * Math.PI,
areaDelta = area * 0.05, circles = [],
drills = [], slices = [];
slices = [],
individual = (diam <= 0);
function onEach(slice) { function onEach(slice) {
slices.push(slice); slices.push(slice);
@ -499,38 +501,71 @@ CAM.holes = async function(settings, widget, diam) {
for (let poly of inner) { for (let poly of inner) {
let center = poly.calcCircleCenter(); let center = poly.calcCircleCenter();
center.area = poly.area(); center.area = poly.area();
center.overlapping = [center]
center.depth = 0;
// console.log("center",center) // console.log("center",center)
center.selected = (!individual && poly.circularity() >= 0.985 && Math.abs(center.area - area) <= areaDelta ); if ( poly.circularity() < 0.985 ){
// if not circular, don't add to holes
continue;
}
if (center.isInPolygon(slice.shadow)) { if (center.isInPolygon(slice.shadow)) {
// if shadowed, don't add, and inform client // if shadowed, don't add, and inform client
shadowedDrills = true; shadowedDrills = true;
continue; continue;
} }
let overlap = false; let overlap = false;
for (let [i,drill] of drills.entries()) { for (let [i,circle] of circles.entries()) {
let dist = drill.distTo2D(center) let dist = circle.distTo2D(center)
//if on the same xy point, and both have the same selection value
if (dist <= centerDiff && drill.selected == center.selected) { // //if on the same xy point,
// console.log("overlap",center,drill); if (dist <= centerDiff ) {
if(center.z > drill.z) { //if current is higher than old
drills[i] = center; //replace with top point // console.log("overlap",center,circle);
drills[i].depth = center.z-drill.z circle.overlapping.push(center);
}else if(center.z <= drill.z-drill.depth){// if current is lower or same as old
drills[i].depth = drill.z-center.z
}
// if overlapping, don't add and continue // if overlapping, don't add and continue
overlap = true; overlap = true;
continue; continue;
} }
} }
center.depth = 0; if (!overlap) circles.push(center);
if(!overlap) drills.push(center);
} }
} }
} }
let drills = []
for (let c of circles) {
let overlapping = c.overlapping
.sort((a,b) => b.z - a.z)
console.log("overlapping",structuredClone(overlapping))
let last = overlapping.shift();
while (overlapping.length) {
let circ = overlapping.shift();
let aveArea = (circ.area + last.area) / 2;
let areaDelta = Math.abs(circ.area -last.area)
if (areaDelta < aveArea * 0.05){ // if area delta less than 5% of average area
//keep top circle selected
last.depth = last.z - circ.z;
}else{ // if not the same area
//push and move on
drills.push(last);
last = circ;
}
}
if (last.depth != 0) drills.push(last) //add last circle
}
drills.forEach( h=>{
delete h.overlapping //for encoding
h.selected = (!individual && Math.abs(h.area - area) <= area * 0.05 ); //for same size selection
})
// console.log("unfiltered circles",circles)
console.log("drills",drills)
drills = drills.filter(drill => drill.depth > 0) drills = drills.filter(drill => drill.depth > 0)
widget.shadowedDrills = shadowedDrills widget.shadowedDrills = shadowedDrills
widget.holes = drills; widget.drills = drills;
return drills; return drills;
} }