fixed hole detection, and added shadow detection

This commit is contained in:
bakedpotatolord 2025-05-01 09:39:54 -06:00
commit 3a5dd483b4
3 changed files with 68 additions and 37 deletions

View file

@ -1190,9 +1190,7 @@ CAM.init = function(kiri, api) {
const {tool,mark} = poppedRec //TODO: display some visual difference if mark is selected const {tool,mark} = poppedRec //TODO: display some visual difference if mark is selected
let diam = new CAM.Tool(settings,tool).fluteDiameter() let diam = new CAM.Tool(settings,tool).fluteDiameter()
api.hide.alert(alert);
alert = api.show.alert("[esc] cancels drill editing");
kiri.api.widgets.opacity(0.8);
const widgets = kiri.api.widgets.all() const widgets = kiri.api.widgets.all()
/** /**
* creates a mesh for a hole and adds it to a widget * creates a mesh for a hole and adds it to a widget
@ -1242,10 +1240,17 @@ CAM.init = function(kiri, api) {
}else{ // if no widget has cached holes }else{ // if no widget has cached holes
await CAM.holes(individual?0:diam,(centers)=>{ await CAM.holes(individual?0:diam,(centers)=>{
let shadow = centers.some(c=>c.shadowed)
if(shadow){
alert = api.show.alert("Some holes are shadowed by part and are not shown.");
}
console.log("holes centers",centers)
centers = centers ?? [] centers = centers ?? []
//flattened list of all hole centers and if they are selected //flattened list of all hole centers and if they are selected
kiri.api.widgets.for(widget => { kiri.api.widgets.for(widget => {
const {holes} = centers.find(center=>center.id = widget.id) const {holes} = centers.find(center=>center.id = widget.id)
console.log(holes)
if (!holes.length) unselectHoles(holes); if (!holes.length) unselectHoles(holes);
holes.forEach(hole => { holes.forEach(hole => {
createHoleMesh(widget, hole) createHoleMesh(widget, hole)
@ -1260,6 +1265,10 @@ CAM.init = function(kiri, api) {
}) })
}); });
} }
//hide the alert once hole meshes are calculated on the worker, and then added to the scene
api.hide.alert(alert);
alert = api.show.alert("[esc] cancels drill editing");
kiri.api.widgets.opacity(0.8);
} }
func.selectHolesHover = function(data) { func.selectHolesHover = function(data) {

View file

@ -168,6 +168,7 @@ kiri.load(api => {
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.holes,
shadowed:widget.shadowedDrills
} } ))); } } )));
} }
} }

View file

@ -460,14 +460,29 @@ CAM.holes = async function(settings, widget, diam) {
// keeps the z bottom relative to the part when z align changes // keeps the z bottom relative to the part when z align changes
zBottom = isIndexed ? camZBottom : camZBottom - zbOff; zBottom = isIndexed ? camZBottom : camZBottom - zbOff;
let slicer = new kiri.cam_slicer(widget);
let zees = Object.assign({}, slicer.zFlat); let slicerOpts = {
let indices = [...new Set(Object.keys(zees) flatoff: 0.01,
}
let slicer = new kiri.cam_slicer(widget,slicerOpts);
// console.log("zFlats",slicer.zFlat)
let zFlats = Object.keys(slicer.zFlat).map(Number).map(z=>[z]).flat()
let intervals = slicer.interval(1,{
fit: false, off: -0.01, flats: true
})
let zees = [...zFlats,...intervals]
let indices = [...new Set(zees
.map(kv => parseFloat(kv).round(5)) .map(kv => parseFloat(kv).round(5))
.filter(z => z !== null) .filter(z => z !== null)
)] )]
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, areaDelta = area * 0.05,
@ -478,45 +493,51 @@ CAM.holes = async function(settings, widget, diam) {
function onEach(slice) { function onEach(slice) {
slices.push(slice); slices.push(slice);
} }
let opts = { each: onEach };
let opts = { each: onEach, over: false, flatoff: 0, edges: true, openok: false, lines: false };
await slicer.slice(indices, opts); await slicer.slice(indices, opts);
const tslices = slices.map(s=>s.tops).flat() let shadowedDrills = false;
// console.log("tslices",tslices) // console.log("slices",slices)
for (let slice of tslices) { for (let slice of slices) {
slice.shadow = CAM.shadowAt(widget,slice.z, 0) for(let top of slice.tops){
let inner = slice.inner; // console.log("slicing",slice.z,top)
if (!inner) { //no holes slice.shadow = CAM.shadowAt(widget,slice.z, 0)
continue; let inner = top.inner;
} if (!inner) { //no holes
for (let poly of inner) {
let center = poly.calcCircleCenter();
center.selected = (!individual && poly.circularity() >= 0.985 && Math.abs(poly.area() - area) <= areaDelta );
if (center.isInPolygon(slice.shadow)) {
//TODO: this does not work yet. needs to be implemented
continue; continue;
} }
let overlap = false; for (let poly of inner) {
for (let [i,drill] of drills.entries()) { let center = poly.calcCircleCenter();
let dist = drill.distTo2D(center) // console.log("center",center)
if (dist <= centerDiff) { // too close center.selected = (!individual && poly.circularity() >= 0.985 && Math.abs(poly.area() - area) <= areaDelta );
// console.log("overlap",center,drill); if (center.isInPolygon(slice.shadow)) {
if(center.z > drill.z) { //if current is higher than old // if shadowed, don't add, and inform client
drills[i] = center; //replace with top point shadowedDrills = true;
drills[i].depth = center.z-drill.z
}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
overlap = true;
continue; continue;
} }
let overlap = false;
for (let [i,drill] of drills.entries()) {
let dist = drill.distTo2D(center)
if (dist <= centerDiff) { // too close
// console.log("overlap",center,drill);
if(center.z > drill.z) { //if current is higher than old
drills[i] = center; //replace with top point
drills[i].depth = center.z-drill.z
}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
overlap = true;
continue;
}
}
center.depth = 0;
if(!overlap) drills.push(center);
} }
center.depth = 0;
if(!overlap) drills.push(center);
} }
} }
console.log("drills",drills)
drills = drills.filter(drill => drill.depth > 0) drills = drills.filter(drill => drill.depth > 0)
widget.shadowedDrills = shadowedDrills
widget.holes = drills; widget.holes = drills;
return drills; return drills;
} }