fix pocket

This commit is contained in:
Stewart Allen 2025-07-05 10:38:18 -04:00
commit 83a4bdebfd
7 changed files with 128 additions and 122 deletions

View file

@ -422,7 +422,7 @@ func.traceAdd = (ev) => {
CAM.traces((ids) => {
api.hide.alert(alert);
alert = api.show.alert("[esc] cancels trace editing");
kiri.api.widgets.for(widget => {
api.widgets.for(widget => {
if (ids.indexOf(widget.id) >= 0) {
unselectTraces(widget, true);
widget.trace_stack = null;
@ -454,7 +454,7 @@ func.traceAdd = (ev) => {
});
});
// ensure appropriate traces are toggled matching current record
kiri.api.widgets.for(widget => {
api.widgets.for(widget => {
widget.setVisualState({ opacity: 0.25 });
let areas = (poppedRec.areas[widget.id] || []);
let stack = widget.trace_stack;
@ -480,11 +480,11 @@ func.traceDone = () => {
func.unpop();
traceOn.classList.remove("editing");
traceOn = false;
kiri.api.widgets.opacity(1);
api.widgets.opacity(1);
api.hide.alert(alert);
api.feature.hover = false;
api.feature.hoverAdds = false;
kiri.api.widgets.for(widget => {
api.widgets.for(widget => {
widget.restoreVisualState();
if (widget.trace_stack) {
widget.trace_stack.hide();
@ -595,7 +595,7 @@ func.selectHoles = async function(individual){
func.hover = func.selectHolesHover;
func.hoverUp = func.selectHolesHoverUp;
const widgets = kiri.api.widgets.all()
const widgets = api.widgets.all()
/**
* creates a mesh for a hole and adds it to a widget
* @param {Object3D} widget - widget to add the hole mesh to
@ -623,7 +623,7 @@ func.selectHoles = async function(individual){
if( individual && meshesCached ){ // if any widget already has cached holes
// console.log("already has cached holes",poppedRec.drills)
api.hide.alert(alert)
kiri.api.widgets.for(widget => {
api.widgets.for(widget => {
if(widget.adds){
let drills = poppedRec.drills[widget.id]
let ids = drills
@ -665,7 +665,7 @@ func.selectHoles = async function(individual){
}
centers = centers ?? []
// list of all hole centers and if they are selected
kiri.api.widgets.for(widget => {
api.widgets.for(widget => {
const {holes} = centers.find(center=>center.id == widget.id)
// console.log(holes)
if (!holes.length) unselectHoles(holes);
@ -689,7 +689,7 @@ func.selectHoles = async function(individual){
setTimeout(() => {
api.hide.alert(escAlert);
}, 5000);
kiri.api.widgets.opacity(0.8);
api.widgets.opacity(0.8);
}
func.selectHolesHover = function(data) {
@ -751,12 +751,12 @@ func.selectHolesDone = () => {
func.unpop();
holeSelOn.classList.remove("editing");
holeSelOn = false;
kiri.api.widgets.opacity(1);
api.widgets.opacity(1);
api.hide.alert(alert);
api.feature.hover = false;
api.feature.hoverAdds = false;
kiri.api.widgets.for(widget => {
api.widgets.for(widget => {
for(let add of widget.adds){
add.visible = false
widget.mesh.remove(add);

View file

@ -1,20 +1,21 @@
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
import { codec } from '../../kiri/codec.js';
import { cam_slice } from './slice.js';
import { cam_slice, holes, traces } from './slice.js';
import { cam_prepare } from './prepare.js';
import { cam_export } from './export.js';
import { tool as mesh_tool } from '../../mesh/tool.js';
function surface_prep(widget, index) {
if (!widget.tool) {
let tool = widget.tool = new mesh.tool();
let tool = widget.tool = new mesh_tool();
let translate = index ? true : false;
tool.index(widget.getGeoVertices({ unroll: true, translate }));
}
};
function surface_find(widget, faces, radians) {
CAM.surface_prep(widget);
surface_prep(widget);
return widget.tool.findConnectedSurface(faces, radians || 0, 0.0);
};
@ -38,7 +39,7 @@ function init(worker) {
worker.cam_surface_find = function(data, send) {
const { id, face, radians } = data;
const widget = worker.cache[id];
const faces = CAM.surface_find(widget, [face], radians);
const faces = surface_find(widget, [face], radians);
send.done(faces);
}
@ -47,11 +48,11 @@ function init(worker) {
const widgets = Object.values(worker.cache);
const fresh = [];
for (let widget of widgets) {
if (await CAM.traces(settings, widget, single)) {
if (await traces(settings, widget, single)) {
fresh.push(widget);
}
}
// const fresh = widgets.filter(widget => CAM.traces(settings, widget, single));
// const fresh = widgets.filter(widget => traces(settings, widget, single));
send.done(codec.encode(fresh.map(widget => { return {
id: widget.id,
traces: widget.traces,
@ -72,7 +73,7 @@ function init(worker) {
const fresh = [];
for (let [i,widget] of widgets.entries() ) {
if (await CAM.holes(settings, widget, indiv, rec,
if (await holes(settings, widget, indiv, rec,
( prog, msg )=>{ send.data({progress: (i/widgets.length)+(prog/widgets.length),msg})}
)){
fresh.push(widget);

View file

@ -1,5 +1,7 @@
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
import { CAM } from './driver-be.js';
import { util as base_util } from '../../geo/base.js';
import { Tool } from './tool.js';
import { newPoint } from '../../geo/point.js';
import { newPolygon } from '../../geo/polygon.js';
@ -399,7 +401,7 @@ class OpRough extends CamOp {
let last = slices[slices.length-1];
if (workarea.bottom_z < 0)
for (let zneg of base.util.lerp(0, -workarea.bottom_cut, op.down)) {
for (let zneg of base_util.lerp(0, -workarea.bottom_cut, op.down)) {
if (!last) continue;
let add = last.clone(true);
add.z -= zneg;
@ -554,7 +556,7 @@ class OpOutline extends CamOp {
// extend cut thru (only when z bottom is 0)
if (workarea.bottom_z < 0) {
let last = slices[slices.length-1];
for (let zneg of base.util.lerp(0, -workarea.bottom_cut, op.down)) {
for (let zneg of base_util.lerp(0, -workarea.bottom_cut, op.down)) {
if (!last) continue;
let add = last.clone(true);
add.tops.forEach(top => top.poly.setZ(add.z));
@ -1046,7 +1048,7 @@ class OpTrace extends CamOp {
let diff = zTop - z;
down = diff / Math.ceil(diff / down);
}
let zs = down ? base.util.lerp(zTop, z, down) : [ z ];
let zs = down ? base_util.lerp(zTop, z, down) : [ z ];
let zpro = 0, zinc = 1 / (polys.length * zs.length);
for (let poly of polys) {
// newPocket();
@ -1178,7 +1180,7 @@ class OpTrace extends CamOp {
if (zThru && similar(zto,0)) {
zto -= zThru;
}
for (let z of base.util.lerp(zTop, zto, down)) {
for (let z of base_util.lerp(zTop, zto, down)) {
output.push(pi.clone().setZ(z));
}
} else {
@ -1297,7 +1299,7 @@ class OpPocket extends CamOp {
let diff = zTop - z;
down = diff / Math.ceil(diff / down);
}
let zs = down ? base.util.lerp(zTop, z, down) : [ z ];
let zs = down ? base_util.lerp(zTop, z, down) : [ z ];
if (engrave) {
toolDiam = toolOver;
}
@ -1373,7 +1375,7 @@ class OpPocket extends CamOp {
let vert = widget.getGeoVertices({ unroll: true, translate: true }).map(v => v.round(4));
// let vert = widget.getVertices().array.map(v => v.round(4));
let outline = [];
let faces = surface_find(widget, surfaces, (op.follow || 5) * DEG2RAG);
let faces = CAM.surface_find(widget, surfaces, (op.follow || 5) * DEG2RAG);
let zmin = Infinity;
let j=0, k=faces.length;
for (let face of faces) {
@ -1647,7 +1649,7 @@ class OpRegister extends CamOp {
tv = -tv;
}
}
for (let z of base.util.lerp(z1, z2, op.down)) {
for (let z of base_util.lerp(z1, z2, op.down)) {
let slice = newSlice(z);
addSlices(slice);
sliceOut.push(slice);
@ -1845,95 +1847,6 @@ class OpShadow extends CamOp {
}
}
// union triangles > z (opt cap < ztop) into polygon(s)
export function shadowAt(widget, z, ztop) {
const geo = widget.cache.geo;
const length = geo.length;
// cache faces with normals up
if (!widget.cache.shadow) {
const faces = [];
for (let i=0, ip=0; i<length; i += 3) {
const a = new THREE.Vector3(geo[ip++], geo[ip++], geo[ip++]);
const b = new THREE.Vector3(geo[ip++], geo[ip++], geo[ip++]);
const c = new THREE.Vector3(geo[ip++], geo[ip++], geo[ip++]);
const n = THREE.computeFaceNormal(a,b,c);
if (n.z > 0.001) {
faces.push(a,b,c);
// faces.push(newPoint(...a), newPoint(...b), newPoint(...c));
}
}
widget.cache.shadow = faces;
}
const found = [];
const faces = widget.cache.shadow;
const { checkOverUnderOn, intersectPoints } = self.kiri.cam_slicer;
for (let i=0; i<faces.length; ) {
const a = faces[i++];
const b = faces[i++];
const c = faces[i++];
let where = undefined;
if (ztop && a.z > ztop && b.z > ztop && c.z > ztop) {
// skip faces over top threshold
continue;
}
if (a.z < z && b.z < z && c.z < z) {
// skip faces under threshold
continue;
} else if (a.z > z && b.z > z && c.z > z) {
found.push([a,b,c]);
} else {
// check faces straddling threshold
const where = { under: [], over: [], on: [] };
checkOverUnderOn(newPoint(a.x, a.y, a.z), z, where);
checkOverUnderOn(newPoint(b.x, b.y, b.z), z, where);
checkOverUnderOn(newPoint(c.x, c.y, c.z), z, where);
if (where.on.length === 0 && (where.over.length === 2 || where.under.length === 2)) {
// compute two point intersections and construct line
let line = intersectPoints(where.over, where.under, z);
if (line.length === 2) {
if (where.over.length === 2) {
found.push([where.over[1], line[0], line[1]]);
found.push([where.over[0], where.over[1], line[0]]);
} else {
found.push([where.over[0], line[0], line[1]]);
}
} else {
console.log({msg: "invalid ips", line: line, where: where});
}
}
}
}
// const lines = {};
// function addline(p1, p2) {
// let key = p1.key < p2.key ? p1.key + ',' + p2.key : p2.key + ',' + p1.key;
// let rec = lines[key];
// if (rec) {
// rec.count++;
// } else {
// lines[key] = { p1, p2, count: 1 };
// }
// }
// for (let face of found) {
// addline(face[0], face[1]);
// addline(face[1], face[2]);
// addline(face[2], face[0]);
// }
// const singles = Object.entries(lines).filter(a => a[1].count === 1).map(a => a[1]);
// const loops = POLY.nest(sliceConnect(singles, z));
let polys = found.map(a => {
return newPolygon()
.add(a[0].x,a[0].y,a[0].z)
.add(a[1].x,a[1].y,a[1].z)
.add(a[2].x,a[2].y,a[2].z);
});
polys = POLY.union(polys, 0.001, true);
// console.log({z, loops, polys});
// return loops;
return polys;
};
export const ops = {
"xray": OpXRay,
"shadow": OpShadow,

View file

@ -212,7 +212,7 @@ export async function cam_slice(settings, widget, onupdate, ondone) {
minZabove = zkey;
}
}
let shadow = shadowAt(widget, z, minZabove);
let shadow = computeShadowAt(widget, z, minZabove);
if (minZabove) {
// const merge = shadow.length;
// const plus = shadows[minZabove].length;
@ -737,3 +737,92 @@ function healPolys(noff) {
}
return noff;
}
// union triangles > z (opt cap < ztop) into polygon(s)
export function computeShadowAt(widget, z, ztop) {
const geo = widget.cache.geo;
const length = geo.length;
// cache faces with normals up
if (!widget.cache.shadow) {
const faces = [];
for (let i=0, ip=0; i<length; i += 3) {
const a = new THREE.Vector3(geo[ip++], geo[ip++], geo[ip++]);
const b = new THREE.Vector3(geo[ip++], geo[ip++], geo[ip++]);
const c = new THREE.Vector3(geo[ip++], geo[ip++], geo[ip++]);
const n = THREE.computeFaceNormal(a,b,c);
if (n.z > 0.001) {
faces.push(a,b,c);
// faces.push(newPoint(...a), newPoint(...b), newPoint(...c));
}
}
widget.cache.shadow = faces;
}
const found = [];
const faces = widget.cache.shadow;
const { checkOverUnderOn, intersectPoints } = cam_slicer;
for (let i=0; i<faces.length; ) {
const a = faces[i++];
const b = faces[i++];
const c = faces[i++];
let where = undefined;
if (ztop && a.z > ztop && b.z > ztop && c.z > ztop) {
// skip faces over top threshold
continue;
}
if (a.z < z && b.z < z && c.z < z) {
// skip faces under threshold
continue;
} else if (a.z > z && b.z > z && c.z > z) {
found.push([a,b,c]);
} else {
// check faces straddling threshold
const where = { under: [], over: [], on: [] };
checkOverUnderOn(newPoint(a.x, a.y, a.z), z, where);
checkOverUnderOn(newPoint(b.x, b.y, b.z), z, where);
checkOverUnderOn(newPoint(c.x, c.y, c.z), z, where);
if (where.on.length === 0 && (where.over.length === 2 || where.under.length === 2)) {
// compute two point intersections and construct line
let line = intersectPoints(where.over, where.under, z);
if (line.length === 2) {
if (where.over.length === 2) {
found.push([where.over[1], line[0], line[1]]);
found.push([where.over[0], where.over[1], line[0]]);
} else {
found.push([where.over[0], line[0], line[1]]);
}
} else {
console.log({msg: "invalid ips", line: line, where: where});
}
}
}
}
// const lines = {};
// function addline(p1, p2) {
// let key = p1.key < p2.key ? p1.key + ',' + p2.key : p2.key + ',' + p1.key;
// let rec = lines[key];
// if (rec) {
// rec.count++;
// } else {
// lines[key] = { p1, p2, count: 1 };
// }
// }
// for (let face of found) {
// addline(face[0], face[1]);
// addline(face[1], face[2]);
// addline(face[2], face[0]);
// }
// const singles = Object.entries(lines).filter(a => a[1].count === 1).map(a => a[1]);
// const loops = POLY.nest(sliceConnect(singles, z));
let polys = found.map(a => {
return newPolygon()
.add(a[0].x,a[0].y,a[0].z)
.add(a[1].x,a[1].y,a[1].z)
.add(a[2].x,a[2].y,a[2].z);
});
polys = POLY.union(polys, 0.001, true);
// console.log({z, loops, polys});
// return loops;
return polys;
}

View file

@ -2,7 +2,8 @@
import { base } from '../geo/base.js';
import { Layers } from './layers.js';
import { Polygon } from '../geo/polygon.js';
import { newPoint } from '../geo/point.js';
import { Polygon, newPolygon } from '../geo/polygon.js';
import { Slice, newSlice, Top, newTop } from './slice.js';
import { Widget, newWidget } from './widget.js';
@ -192,7 +193,7 @@ function decodePointArray(array) {
const points = new Array(length / 3);
for (let vid=0, pid=0; vid < length; ) {
points[pid++] = base.newPoint(array[vid++], array[vid++], array[vid++]);
points[pid++] = newPoint(array[vid++], array[vid++], array[vid++]);
}
return points;
@ -207,7 +208,7 @@ function decodePointArray2D(array, z, fn) {
for (let vid=0, pid=0; vid < length; ) {
points[pid++] = fn ?
fn(array[vid++], array[vid++]) :
base.newPoint(array[vid++], array[vid++], z);
newPoint(array[vid++], array[vid++], z);
}
return points;
@ -334,10 +335,10 @@ registerDecoder(TYPE.POLY, function(v, state) {
const array = v.array;
const length = array.length;
const poly = base.newPolygon();
const poly = newPolygon();
for (let vid = 0; vid < length; ) {
poly.push(base.newPoint(array[vid++], array[vid++], array[vid++]));
poly.push(newPoint(array[vid++], array[vid++], array[vid++]));
}
state.poly[v.id] = poly;

View file

@ -7,6 +7,7 @@ import { base } from '../geo/base.js';
import { load } from '../load/file.js';
import { space } from '../moto/space.js';
import { Widget } from './widget.js';
import { Packer } from './pack.js';
import { COLOR, MODES } from './consts.js';
import { THREE } from '../ext/three.js';
@ -708,12 +709,12 @@ function platformLayout() {
mp = [sz.x, sz.y],
ms = [mp[0] / 2, mp[1] / 2],
c = Widget.Groups.blocks().sort(),
p = new kiri.Pack(ms[0], ms[1], gap).fit(c);
p = new Packer(ms[0], ms[1], gap).fit(c);
while (!p.packed) {
ms[0] *= 1.1;
ms[1] *= 1.1;
p = new kiri.Pack(ms[0], ms[1], gap).fit(c);
p = new Packer(ms[0], ms[1], gap).fit(c);
}
for (i = 0; i < c.length; i++) {

View file

@ -2,6 +2,7 @@
import { base } from '../geo/base.js';
import { verticesToPoints } from '../geo/points.js';
import { util as mesh_util } from '../mesh/util.js';
const { inRange, time } = base.util;
const solid_opacity = 1.0;
@ -333,7 +334,7 @@ class Widget {
}
selectFaces(faces) {
let groups = mesh.util.facesToGroups(faces || []);
let groups = mesh_util.facesToGroups(faces || []);
let geo = this.mesh.geometry;
geo.clearGroups();
for (let group of groups) {