208 lines
12 KiB
JavaScript
208 lines
12 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { BoxGeometry, BufferGeometry, Float32BufferAttribute, Group, Mesh, PerspectiveCamera, Vector3 } from '../vendor/three/build/three.module.min.js';
|
|
import { createAssemblyShot, assemblyShotFrame, assemblyShotFits, assemblyHeadDirection, headFacingView, watchAssemblyCameraInput } from '../js/assembly-camera.js';
|
|
import { AssemblyViewer } from '../js/assembly-viewer.js';
|
|
import { ScrewTransition } from '../js/assembly-motion.js';
|
|
|
|
function fixture(screw = false) {
|
|
const parent = new Group(), instance = new Group();
|
|
parent.position.set(1, 2, 3);
|
|
parent.rotation.set(0.3, 0.8, 0.1);
|
|
parent.scale.setScalar(2);
|
|
parent.add(instance);
|
|
const part = { id: 'part', file: 'part.glb', position: [0.2, 0.3, 0.1], quaternion: [0, 0, 0, 1],
|
|
scale: [1, 1, 1], installOffset: [0, 0.2, 0] };
|
|
if (screw) part.installation = { socketPosition: [0, 0, 0.005], axis: [0, 0, -1], toolQuaternion: [0, 0, 0, 1], travel: 0.004, toolClearance: 0.012 };
|
|
instance.position.fromArray(part.position);
|
|
instance.position.add(new Vector3(...part.installOffset));
|
|
const template = new Mesh(new BoxGeometry(0.05, 0.07, 0.01));
|
|
const tool = { file: 'tool.glb', tipPosition: [0, 0, -0.05] };
|
|
const templates = new Map([['part.glb', template], ['tool.glb', new Mesh(new BoxGeometry(0.01, 0.01, 0.1))]]);
|
|
const instances = new Map([['part', instance]]), cache = new Map();
|
|
const shot = createAssemblyShot([{ part, tool: screw ? tool : null }], instances, templates, cache);
|
|
return { parent, instance, part, shot, cache, templates, instances, camera: new PerspectiveCamera(32, 1.6, 0.01, 50) };
|
|
}
|
|
const near = (a, b) => assert.ok(a.distanceTo(b) < 1e-8, `${a.toArray()} != ${b.toArray()}`);
|
|
|
|
test('revealing mobile controls keeps closeup, while drag, pinch and wheel take camera control', () => {
|
|
const element = new EventTarget(), lifecycle = new AbortController();
|
|
let changes = 0;
|
|
watchAssemblyCameraInput(element, () => changes++, lifecycle.signal);
|
|
const send = (type, id = 1, x = 10, y = 10) => element.dispatchEvent(Object.assign(new Event(type), { pointerId: id, clientX: x, clientY: y }));
|
|
send('pointerdown'); send('pointermove', 1, 12, 12); send('pointerup');
|
|
assert.equal(changes, 0, 'tap/jitter must not exit closeup');
|
|
send('pointerdown'); send('pointermove', 1, 30); send('pointermove', 1, 40); send('pointerup');
|
|
assert.equal(changes, 1, 'one handoff per drag');
|
|
send('pointerdown'); send('pointerdown', 2); send('pointercancel'); send('pointercancel', 2);
|
|
assert.equal(changes, 2);
|
|
send('wheel'); assert.equal(changes, 3);
|
|
lifecycle.abort(); send('wheel'); assert.equal(changes, 3, 'dispose removes input listeners');
|
|
});
|
|
|
|
test('ordinary part shot stays fixed throughout insertion and replay without altering the model', () => {
|
|
const f = fixture(), json = JSON.stringify(f.part);
|
|
const first = assemblyShotFrame(f.shot, f.camera);
|
|
f.instance.position.fromArray(f.part.position);
|
|
const last = assemblyShotFrame(f.shot, f.camera);
|
|
near(first.target, last.target);
|
|
near(first.position, last.position);
|
|
assert.equal(JSON.stringify(f.part), json);
|
|
const bounds = f.cache.get('part.glb');
|
|
const replay = createAssemblyShot([{ part: f.part }], f.instances, f.templates, f.cache);
|
|
near(first.target, assemblyShotFrame(replay, f.camera).target);
|
|
near(first.position, assemblyShotFrame(replay, f.camera).position);
|
|
assert.equal(f.cache.get('part.glb'), bounds, 'reuse bounds rather than clone/analyse GLB per frame');
|
|
});
|
|
|
|
test('screw camera remains on the head side and does not orbit with tightening', () => {
|
|
const f = fixture(true);
|
|
f.instance.position.fromArray(f.part.position);
|
|
const first = assemblyShotFrame(f.shot, f.camera);
|
|
const outward = new Vector3(0, 0, 1).applyQuaternion(f.parent.quaternion);
|
|
assert.ok(f.shot.direction.dot(outward) > 0.5);
|
|
assert.ok(f.shot.radius > 0.1, 'frame the full tool sweep, not only the tiny screw');
|
|
for (const angle of [0.2, Math.PI, Math.PI * 2]) {
|
|
f.instance.quaternion.setFromAxisAngle(new Vector3(0, 0, -1), angle);
|
|
f.instance.position.add(new Vector3(0.01, 0.02, 0.03));
|
|
const next = assemblyShotFrame(f.shot, f.camera);
|
|
near(first.target, next.target);
|
|
near(first.position, next.position);
|
|
}
|
|
});
|
|
|
|
test('tightened framing contains the actual tool through docking, rotation and withdrawal', () => {
|
|
const f = fixture(true), tool = new Group();
|
|
tool.add(f.templates.get('tool.glb').clone());
|
|
Object.assign(f.part.installation, { approachDuration: 1, engageDuration: 1, tightenDuration: 2, retractDuration: 1, turns: 3, direction: 'clockwise' });
|
|
const transition = new ScrewTransition(f.instance, f.part, tool, {tipPosition: [0,0,-.05]});
|
|
for(let time=1;time<5;time+=.037) {
|
|
transition.sample(time); tool.updateWorldMatrix(true,true);
|
|
const mesh=tool.children[0], points=mesh.geometry.attributes.position;
|
|
for(let i=0;i<points.count;i++) {
|
|
const point=new Vector3().fromBufferAttribute(points,i).applyMatrix4(mesh.matrixWorld);
|
|
assert.ok(point.distanceTo(f.shot.target)<=f.shot.radius,'tool leaves the stationary work area');
|
|
}
|
|
}
|
|
transition.dispose();
|
|
});
|
|
|
|
test('portrait framing fits the same work area using the narrower field of view', () => {
|
|
const f = fixture(true), desktop = assemblyShotFrame(f.shot, f.camera);
|
|
f.camera.aspect = 320 / 620;
|
|
const mobile = assemblyShotFrame(f.shot, f.camera);
|
|
assert.ok(mobile.position.distanceTo(mobile.target) > desktop.position.distanceTo(desktop.target));
|
|
const halfFov = Math.atan(Math.tan(f.camera.fov * Math.PI / 360) * f.camera.aspect);
|
|
assert.ok(mobile.position.distanceTo(mobile.target) * Math.sin(halfFov) > f.shot.radius);
|
|
near(desktop.target, mobile.target);
|
|
});
|
|
|
|
test('ordinary parts preserve the current view instead of rotating to each insertion axis', () => {
|
|
const f = fixture(), direction = new Vector3(1, 0.2, -1).normalize();
|
|
for (const offset of [[0.1, 0, 0], [-0.1, 0, 0], [0, -0.1, 0]]) {
|
|
f.part.installOffset = offset;
|
|
const shot = createAssemblyShot([{ part: f.part }], f.instances, f.templates, f.cache, 1, direction);
|
|
near(shot.direction, direction);
|
|
}
|
|
});
|
|
|
|
test('generated screw head direction is respected even without a wrench animation',()=>{
|
|
const f=fixture();
|
|
f.part.focusDirection=[-1,0,0];
|
|
const outward=new Vector3(-1,0,0).applyQuaternion(f.parent.quaternion);
|
|
const shot=createAssemblyShot([{part:f.part}],f.instances,f.templates,f.cache,1,outward.clone().negate());
|
|
assert.ok(shot.direction.dot(outward)>.4);
|
|
assert.ok(shot.headDirection.distanceTo(outward)<1e-8);
|
|
});
|
|
|
|
test('nearby work reuses a fixed shot; out-of-frame or back-facing work needs a new shot', () => {
|
|
const f = fixture(true), frame = assemblyShotFrame(f.shot, f.camera);
|
|
f.camera.position.copy(frame.position);
|
|
assert.equal(assemblyShotFits(f.shot, f.camera, frame.target), true);
|
|
const adjacent = { ...f.shot, target: f.shot.target.clone().add(new Vector3(0.001, 0, 0)) };
|
|
assert.equal(assemblyShotFits(adjacent, f.camera, frame.target), true);
|
|
const behind = { ...adjacent, headDirection: f.shot.headDirection.clone().negate() };
|
|
assert.equal(assemblyShotFits(behind, f.camera, frame.target), false);
|
|
adjacent.target.x += 10;
|
|
assert.equal(assemblyShotFits(adjacent, f.camera, frame.target), false);
|
|
});
|
|
|
|
test('a whole-model view cannot count as a closeup just because a small part fits', () => {
|
|
const f = fixture(true), frame = assemblyShotFrame(f.shot, f.camera);
|
|
f.camera.position.copy(frame.position).sub(frame.target).multiplyScalar(2).add(frame.target);
|
|
assert.equal(assemblyShotFits(f.shot, f.camera, frame.target), false);
|
|
});
|
|
|
|
test('the stationary closeup contains the entry and final part without doubling the travel', () => {
|
|
const f = fixture();
|
|
const box = f.cache.get('part.glb');
|
|
for (const t of [0, .25, .5, .75, 1]) {
|
|
f.instance.position.fromArray(f.part.position).addScaledVector(new Vector3(...f.part.installOffset),t);
|
|
f.instance.updateWorldMatrix(true,false);
|
|
for (const x of [box.min.x,box.max.x]) for (const y of [box.min.y,box.max.y]) for (const z of [box.min.z,box.max.z]) {
|
|
const p = new Vector3(x,y,z).applyMatrix4(f.instance.matrixWorld);
|
|
assert.ok(p.distanceTo(f.shot.target) <= f.shot.radius);
|
|
}
|
|
}
|
|
assert.ok(f.shot.radius < .4,'avoid wasting the closeup on empty space');
|
|
});
|
|
|
|
test('joining temporarily frames the assembly without disabling subsequent closeups', () => {
|
|
const v=Object.create(AssemblyViewer.prototype);
|
|
v.closeupEnabled=true; v.stepIndex=0; v.config={steps:[{type:'join'}]};
|
|
let calls=0;
|
|
v.fitBuildView=options=>{ calls++; assert.equal(options.animate,true); };
|
|
v.refreshCloseup(true,undefined,true);
|
|
assert.equal(calls,1); assert.equal(v.closeupEnabled,true);
|
|
assert.equal(v.closeupShot,null);
|
|
});
|
|
|
|
test('legacy screw with zero travel derives head side from geometry once, retaining all transforms',()=>{
|
|
const f=fixture(), points=[];
|
|
for (const [x,r] of [[-.005,.002],[-.003,.002],[.005,.001]])
|
|
for (const y of [-r,r]) for(const z of [-r,r]) points.push(x,y,z);
|
|
const geometry=new BufferGeometry().setAttribute('position',new Float32BufferAttribute(points,3));
|
|
const mesh=new Mesh(geometry);mesh.rotation.z=.4;
|
|
const template=new Group();template.add(mesh);
|
|
const part={...f.part,file:'1-19-STP39-0.glb',installOffset:[0,0,0]};
|
|
const cache=new Map(), before=Array.from(geometry.attributes.position.array);
|
|
const direction=assemblyHeadDirection(part,f.instance,template,cache);
|
|
const expected=new Vector3(-1,0,0).applyQuaternion(mesh.quaternion).applyQuaternion(f.parent.quaternion);
|
|
near(direction,expected);
|
|
assert.equal(cache.size,1);
|
|
const saved=cache.get(part.file);
|
|
f.instance.position.x+=3;f.instance.rotation.y=2; // Moving/screwing cannot affect the inferred final side.
|
|
near(assemblyHeadDirection(part,f.instance,template,cache),direction);
|
|
assert.equal(cache.get(part.file),saved);
|
|
assert.deepEqual(Array.from(geometry.attributes.position.array),before);
|
|
assert.equal(assemblyHeadDirection({...part,file:'spacer.glb'},f.instance,template,cache),null);
|
|
assert.equal(assemblyHeadDirection({...part,file:'bolt.glb'},f.instance,new Mesh(new BoxGeometry()),cache),null);
|
|
});
|
|
|
|
test('head-facing camera switches back/front only as needed and can see underside fasteners',()=>{
|
|
let view=new Vector3(1,.25,-.4).normalize();
|
|
const front=new Vector3(1,0,0),back=new Vector3(-1,0,0);
|
|
near(headFacingView(view,front),view);
|
|
view=headFacingView(view,back);assert.ok(view.dot(back)>.5);
|
|
near(headFacingView(view,back),view);
|
|
const bottom=headFacingView(new Vector3(0,1,0),new Vector3(0,-1,0));
|
|
assert.ok(bottom.y<-.5);
|
|
});
|
|
|
|
test('normal grouped view changes to the screw head side even when work fits on screen',()=>{
|
|
const f=fixture(),v=Object.create(AssemblyViewer.prototype);
|
|
const head=new Vector3(-1,0,0), bounds=new BoxGeometry(.1,.1,.1);
|
|
bounds.computeBoundingBox();
|
|
v.config={playback:'grouped-v1',steps:[{groupId:'leg',parts:['part']}]};v.stepIndex=0;
|
|
v.finalPartBounds=new Map();v.assembledBounds=bounds.boundingBox;v.groupBounds=new Map();
|
|
v.camera=new PerspectiveCamera(32,1.6,.01,50);v.camera.position.set(1,.2,-.3);v.camera.lookAt(0,0,0);
|
|
v.controls={target:new Vector3()};v.currentWorkBounds=()=>bounds.boundingBox;
|
|
v.stepHeadDirection=()=>head;
|
|
let moves=0, animated;v.moveCamera=(frame,animate)=>{moves++;animated=animate;v.camera.position.copy(frame.position);v.controls.target.copy(frame.target);v.camera.lookAt(frame.target);};
|
|
v.fitBuildView({animate:true});
|
|
assert.equal(moves,1,'back-facing work cannot reuse an otherwise fitting shot');
|
|
assert.ok(v.camera.position.clone().sub(v.controls.target).normalize().dot(head)>.5);
|
|
assert.equal(animated,true,'head-side changes request a transition, not an instant cut');
|
|
v.fitBuildView({animate:true});assert.equal(moves,1,'same-side next screw reuses the shot');
|
|
});
|