Files
vibemake/scripts/lib/thread-fit.mjs
T

89 lines
4.4 KiB
JavaScript

// Visual fit of a modeled thread in its pilot hole. Only the shaft perimeter
// has an allowance; the head and the bore core must have a continuous passage.
import { Box3, Matrix4, Vector3 } from 'three';
const sides = Array.from({length:16}, (_, i) => [Math.cos(i*Math.PI/8), Math.sin(i*Math.PI/8)]);
// Clip the obstacle triangle against a closed convex prism. A triangle wholly
// inside the swept volume counts too. Tangent planes circumscribe the circle.
function intersectsPrism(triangle, inverse, floor, ceiling, radius) {
let polygon = [triangle.a,triangle.b,triangle.c].map(v=>v.clone().applyMatrix4(inverse));
const planes = [[0,0,-1,-floor],[0,0,1,ceiling],...sides.map(([x,y])=>[x,y,0,radius])];
for (const [x,y,z,limit] of planes) {
const next=[];
let prev=polygon.at(-1);
let a=prev.x*x+prev.y*y+prev.z*z-limit;
for (const point of polygon) {
const b=point.x*x+point.y*y+point.z*z-limit;
if ((a>0)!==(b>0)) next.push(prev.clone().lerp(point,a/(a-b)));
if (b<=0) next.push(point);
prev=point; a=b;
}
if (!next.length) return false;
polygon=next;
}
return true;
}
export function threadFit(local, world, headAxis, obstacles, travel) {
const scale=new Vector3().setFromMatrixScale(world);
if (Math.max(...scale.toArray())-Math.min(...scale.toArray())>scale.z*1e-5) return null;
const up=headAxis, x=new Vector3().crossVectors(up, Math.abs(up.x)<.8?new Vector3(1,0,0):new Vector3(0,1,0)).normalize();
const y=new Vector3().crossVectors(up,x);
const center=local.boundingBox.getCenter(new Vector3());
const frame=new Matrix4().makeBasis(x,y,up).setPosition(center);
const localToFrame=frame.clone().invert();
const frameToWorld=world.clone().multiply(frame);
const inverse=frameToWorld.clone().invert();
const coords=local.attributes.position;
const vertices=[];
let bottom=Infinity,top=-Infinity,radius=0;
for (let i=0;i<coords.count;i++) {
const p=new Vector3().fromBufferAttribute(coords,i).applyMatrix4(localToFrame);
const r=Math.hypot(p.x,p.y);
vertices.push({h:p.z,r});
radius=Math.max(radius,r); bottom=Math.min(bottom,p.z); top=Math.max(top,p.z);
}
const span=top-bottom;
const shaft=vertices.reduce((max,p)=>p.h<bottom+.45*span?Math.max(max,p.r):max,0);
if (!(shaft>0 && radius>shaft*1.18)) return null;
// Include the full transition in the head envelope. A triangle spanning
// directly from tip to head (a taper, not a cylindrical screw) is excluded.
let shoulder=Infinity;
const pos=local.index.array;
for(let i=0;i<pos.length;i+=3) {
const face=[vertices[pos[i]],vertices[pos[i+1]],vertices[pos[i+2]]];
if(face.some(v=>v.r>shaft*1.02)) for(const v of face) shoulder=Math.min(shoulder,v.h);
}
if (!(shoulder>bottom+.45*span)) return null;
const localTravel=travel/scale.z, seat=.00015/scale.z;
const sweptBox=(r,lo,hi)=>new Box3(new Vector3(-r,-r,lo),new Vector3(r,r,hi)).applyMatrix4(frameToWorld).expandByScalar(1e-7);
const clear=(r,lo)=>{
const start=lo+seat, end=top+localTravel, sweep=sweptBox(r*1.022,start,end);
for(const obstacle of obstacles){
if(!sweep.intersectsBox(obstacle.box)) continue;
// Extend past this obstacle only, so an initially enclosed prism cannot pass.
const ceiling=Math.max(end,obstacle.box.clone().applyMatrix4(inverse).max.z+.00001);
const bounds=sweptBox(r*1.022,start,ceiling);
if(obstacle.bvh.shapecast({
intersectsBounds:b=>bounds.intersectsBox(b),
intersectsTriangle:t=>intersectsPrism(t,inverse,start,ceiling,r),
})) return false;
}
return true;
};
if(!clear(radius,shoulder)) return null;
// A core check is an explicit visual thread-fit assumption. Never classify
// it as clearance for the original full-diameter screw mesh.
for(const fraction of [.10,.20,.30]) {
const allowance=Math.min(shaft*fraction,.00035/scale.x);
if(clear(shaft-allowance,bottom)) return {status:'visual-thread-fit',travel,coreRadiusMeters:(shaft-allowance)*scale.x,
shaftRadiusMeters:shaft*scale.x,radialAllowanceMeters:allowance*scale.x,headRadiusMeters:radius*scale.x};
}
// A coarse polygonal pilot hole can confirm the entry side without
// establishing the fit diameter. Expose that distinction in the report.
if(clear(shaft/2,bottom)) return {status:'visual-bore-axis',travel,coreRadiusMeters:shaft*.5*scale.x,
shaftRadiusMeters:shaft*scale.x,headRadiusMeters:radius*scale.x};
return null;
}