90 lines
5.0 KiB
JavaScript
90 lines
5.0 KiB
JavaScript
import { Box3, Matrix4, Vector3 } from 'three';
|
|
import { checkFiniteAxis, VIEW_AXES } from './assembly-axis-planner.mjs';
|
|
import { threadFit } from './thread-fit.mjs';
|
|
|
|
import { screwHead, isAssemblyFastener } from '../../js/assembly-head.js';
|
|
export { screwHead } from '../../js/assembly-head.js';
|
|
|
|
export function planEntry(item, local, world, installed, name) {
|
|
const fastener = isAssemblyFastener(name);
|
|
const head = fastener ? screwHead(local) : null;
|
|
const meta = { fastener, headWidthRatio:head?.widthRatio ?? null,
|
|
...(head ? {headDirection:head.vector.clone().transformDirection(world).toArray()} : {}) };
|
|
if (!installed.length) return {...meta,status:'foundation',offset:[0,0,0],checks:[]};
|
|
const axes = [];
|
|
function add(label,v) {
|
|
v.normalize();
|
|
if (!axes.some(c=>c.v.dot(v)>1-1e-8)) axes.push({label,v});
|
|
}
|
|
if (head) add('从螺丝头一侧沿杆轴进入',head.vector.clone().transformDirection(world));
|
|
else if (fastener) return {...meta,status:'review-head',offset:[0,0,0],checks:[]};
|
|
else {
|
|
for (const a of VIEW_AXES) add(a.name,new Vector3(...a.vector));
|
|
for (const a of VIEW_AXES) add('local '+a.name,new Vector3(...a.vector).transformDirection(world));
|
|
for (const x of [-1,0,1]) for (const y of [-1,0,1]) for (const z of [-1,0,1]) {
|
|
if (Math.abs(x)+Math.abs(y)+Math.abs(z)>1) add(`斜向 (${x},${y},${z})`,new Vector3(x,y,z));
|
|
}
|
|
}
|
|
const size = item.box.getSize(new Vector3()).length();
|
|
const travel = Math.min(fastener ? .024 : .08, Math.max(fastener ? .008 : .012, size*(fastener ? 1.7 : 1.05)));
|
|
const checks = [];
|
|
for (const length of [travel, travel*0.5]) {
|
|
for (const {label,v} of axes) {
|
|
// Reject explicit contacts cheaply before the more expensive conservative
|
|
// continuous query. Passing these samples alone is never enough.
|
|
const sweep = item.box.clone().union(item.box.clone().translate(v.clone().multiplyScalar(length)));
|
|
const obstacles = installed.filter(b=>sweep.intersectsBox(b.box));
|
|
const matrix = new Matrix4();
|
|
let block;
|
|
for (const t of [1,.75,.5,.25,.08]) {
|
|
const delta = v.clone().multiplyScalar(length*t);
|
|
const box = item.box.clone().translate(delta);
|
|
matrix.makeTranslation(delta.x,delta.y,delta.z);
|
|
const b = obstacles.find(b=>box.intersectsBox(b.box) && b.bvh.intersectsGeometry(item.geometry,matrix));
|
|
if (b) { block={status:'blocked-or-touching',blocker:b.id,remaining:length*t}; break; }
|
|
}
|
|
const checked = block ?? checkFiniteAxis([item],installed,v.toArray(),length,{maxMilliseconds:650,maxIterations:100});
|
|
checks.push({axis:label,vector:v.toArray(),length,...checked});
|
|
if (['foundation','clear-to-seating'].includes(checked.status)) {
|
|
return {...meta,status:'clear-to-seating',offset:v.clone().multiplyScalar(length).toArray(),checks};
|
|
}
|
|
}
|
|
}
|
|
if (head) {
|
|
for (const length of [travel, travel*0.5]) {
|
|
const fit=threadFit(local,world,head.vector,installed,length);
|
|
if (fit) return {...meta,status:fit.status,offset:axes[0].v.clone().multiplyScalar(length).toArray(),fit,checks};
|
|
}
|
|
}
|
|
// No verified linear route: reveal in place. Do not turn a colliding path
|
|
// or a query timeout into an apparently approved insertion direction.
|
|
return {...meta,status:'review-path',offset:[0,0,0],checks};
|
|
}
|
|
|
|
// The entire completed component moves as one rigid body. Internal contacts
|
|
// within the component are intentional; only already joined components block it.
|
|
export function planGroupEntry(moving, installed) {
|
|
const bounds=new Box3(); for(const item of moving) bounds.union(item.box);
|
|
const travel=Math.max(.04, Math.min(.2, bounds.getSize(new Vector3()).length() * .65));
|
|
if (!installed.length) return { status:'foundation', offset:[0,travel,0], checks:[] };
|
|
const checks=[];
|
|
for(const length of [travel, travel * .5]) for(const axis of VIEW_AXES) {
|
|
const checked=checkFiniteAxis(moving,installed,axis.vector,length,{maxMilliseconds:1800,maxIterations:180});
|
|
checks.push({axis:axis.name,vector:axis.vector,length,...checked});
|
|
if (['foundation','clear-to-seating'].includes(checked.status)) return {
|
|
status:'clear-to-seating',offset:axis.vector.map(v=>v*length),checks,
|
|
};
|
|
}
|
|
// A presentation move is useful even when exact rigid-mesh insertion cannot
|
|
// be certified (interlocking/press-fit geometry). Keep it EXPLICITLY marked
|
|
// as unverified, and pick the axis that got closest to seating. Never relabel
|
|
// such a route as collision-free just to make the preview animate.
|
|
const obstacleBox=new Box3();for(const item of installed) obstacleBox.union(item.box);
|
|
const outward=bounds.getCenter(new Vector3()).sub(obstacleBox.getCenter(new Vector3())).normalize();
|
|
const ranked=checks.filter(c=>Number.isFinite(c.remaining)).sort((a,b)=>
|
|
a.remaining-b.remaining || new Vector3(...b.vector).dot(outward)-new Vector3(...a.vector).dot(outward));
|
|
const chosen=ranked[0];
|
|
const vector=chosen?.vector ?? (outward.lengthSq() ? outward.toArray() : [0,1,0]);
|
|
return {status:'review-path',presentationOnly:true,offset:vector.map(v=>v*travel),checks};
|
|
}
|