Files
vibemake/scripts/refine-duck-numbered-entry.mjs
Jerry 1c837766cd feat: integrate robot assembly and simulation with resilient loading
Add backend-driven assembly and simulation experiences, MAKE branding, mobile rendering budgets and desktop-only CAD/PCB routes. Improve logo loading progress, document image recovery and URDF request handling; include dev packaging and regression coverage.
2026-10-30 18:38:39 +08:00

95 lines
5.9 KiB
JavaScript

// Visual refinement inside the finite available entry space. Sampled checks are
// explicitly NOT continuous collision certification, nor tool/reachability checks.
import { readFileSync,writeFileSync } from 'node:fs';
import assert from 'node:assert/strict';
import { Vector3,Quaternion,Matrix4 } from 'three';
import { validatePackage } from '../js/experience-packages.js';
import { assemblyColliders } from './lib/assembly-colliders.mjs';
import { checkFiniteAxis } from './lib/assembly-axis-planner.mjs';
const folder=new URL('../deliverables/experience-packages/microduck-numbered-body/',import.meta.url);
const raw=JSON.parse(readFileSync(new URL('manifest.json',folder))),next=structuredClone(raw);
const review=JSON.parse(readFileSync(new URL('axis-review.json',folder)));
const notes=JSON.parse(readFileSync(new URL('manifest.notes.json',folder)));
const config=validatePackage(raw,'assembly','http://localhost:8090/manifest.json');
const get=assemblyColliders(config,'/Users/jerry/Downloads/小鸭子-扳手装配/零件模型');
const map=new Map(config.parts.map(p=>[p.id,p])),nextMap=new Map(next.parts.map(p=>[p.id,p]));
const inverse=new Quaternion(...config.displayQuaternion).invert();
const installed=[],changes=[];
for(const [i,step]of config.steps.entries()){
const moving=await Promise.all(step.parts.map(id=>get(map.get(id))));
const record=review.steps[i];
if(i>=52&&record.candidates){
const candidates=record.candidates.map(c=>({name:c.name,vector:c.vector,travel:c.travel}));
// A yaw fork or circuit board can also enter diagonally; neither is a closed
// bearing requiring an axial press fit. Keep bearing/screw constraints intact.
if(['trunk-3-10','trunk-3-22','trunk-3-38'].includes(step.id)){
for(const x of [-1,0,1])for(const y of [-1,0,1])for(const z of [-1,0,1]){
if(!(x||y||z))continue;
const vector=new Vector3(x,y,z).normalize().toArray();
if(!candidates.some(c=>new Vector3(...c.vector).distanceTo(new Vector3(...vector))<1e-5))
candidates.push({name:`diagonal-${x}-${y}-${z}`,vector,travel:.055});
}
}
const windows=[];
for(const candidate of candidates){
const count=Math.ceil(candidate.travel/.0005),spacing=candidate.travel/count;
const offset=new Vector3(),translation=new Matrix4(),states=[];
for(let n=0;n<=count;n++){
offset.fromArray(candidate.vector).multiplyScalar(n*spacing);
translation.makeTranslation(...offset.toArray());
let blocker=null;
for(const m of moving){
const box=m.box.clone().translate(offset);
const hit=installed.find(b=>b.box.intersectsBox(box)&&b.bvh.intersectsGeometry(m.geometry,translation));
if(hit){blocker=hit.id;break;}
}
states.push(blocker);
}
// Prefer the clear interval nearest the seat, instead of flying through an
// opposite limb to get there. Require a visible >=4 mm interval.
for(let a=0;a<states.length;){
if(states[a]){a++;continue;}
let b=a;while(b+1<states.length&&!states[b+1])b++;
const travel=b===count?candidate.travel:Math.max(0,(b-1)*spacing);
if(travel-a*spacing>=.004){
windows.push({...candidate,travel,firstSampledContact:Math.max(0,(a-1)*spacing),
nearestBlocker:a?states[a-1]:null,sampleSpacing:spacing,samples:states.length});
break;
}
a=b+1;
}
}
windows.sort((a,b)=>a.firstSampledContact-b.firstSampledContact);
const current=windows.find(c=>c.name===record.selected.name);
const best=windows[0];
// Avoid gratuitous changes for an insignificant submillimetre sampling difference.
const selected=current&&best&&current.firstSampledContact<=best.firstSampledContact+.00075?current:best;
record.finiteVisualReview={method:'0.5 mm or finer triangle-intersection samples, clear entry window >=4 mm',
collisionCertified:false,candidates:windows,selected:selected??null};
if(selected){
const offset=new Vector3(...selected.vector).multiplyScalar(selected.travel).applyQuaternion(inverse).toArray().map(v=>Math.abs(v)<1e-12?0:v);
const changed=step.parts.some(id=>offset.some((v,k)=>Math.abs(v-nextMap.get(id).installOffset[k])>1e-8));
if(changed){
for(const id of step.parts)nextMap.get(id).installOffset=[...offset];
record.finiteVisualReview.continuousRecheck=checkFiniteAxis(moving,installed,selected.vector,selected.travel,{maxMilliseconds:5000});
record.finalOffsets=step.parts.map(id=>nextMap.get(id).installOffset);
const note=`按实际进入空间再次筛选,装入距离 ${(selected.travel*1000).toFixed(1)} mm;有限采样未发现接触的行程到距终点约 ${(selected.firstSampledContact*1000).toFixed(1)} mm 处,仍不作为全程无碰撞认证。`;
notes.steps[i].reason=note;
changes.push({id:step.id,label:step.label,from:raw.parts.find(p=>p.id===step.parts[0]).installOffset,to:offset,selection:selected});
}
}
console.log(`${i+1}/${config.steps.length} ${step.label}: ${selected?.name??'no-free-entry-window'} ${selected?(selected.travel*1000).toFixed(1)+'mm':''}`);
}
installed.push(...moving);
}
assert.deepEqual(raw.steps,next.steps);assert.deepEqual(raw.files,next.files);assert.deepEqual(raw.tools,next.tools);
for(let i=0;i<raw.parts.length;i++){
const{installOffset:a,...p}=raw.parts[i],{installOffset:b,...q}=next.parts[i];assert.deepEqual(p,q);
}
validatePackage(next,'assembly','http://localhost:8090/manifest.json');
review.finiteVisualChanges=changes;
writeFileSync(new URL('manifest.json',folder),JSON.stringify(next,null,2)+'\n');
writeFileSync(new URL('manifest.notes.json',folder),JSON.stringify(notes,null,2)+'\n');
writeFileSync(new URL('axis-review.json',folder),JSON.stringify(review,null,2)+'\n');
console.log(JSON.stringify({changed:changes.map(c=>({id:c.id,travelMm:c.selection.travel*1000,contactMm:c.selection.firstSampledContact*1000}))}));