|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import * as THREE from 'three'; |
| 3 | +import { parseReport, SpacePilotDevice } from '../input/spacePilot'; |
| 4 | +import { cameraDelta, DEFAULT_MOTION, isIdle, shape } from '../input/spaceMotion'; |
| 5 | +import { applyDelta } from '../input/SpacePilotRig'; |
| 6 | + |
| 7 | +function i16(...vals: number[]): DataView { |
| 8 | + const b = new ArrayBuffer(vals.length * 2); |
| 9 | + const v = new DataView(b); |
| 10 | + vals.forEach((x, i) => v.setInt16(i * 2, x, true)); |
| 11 | + return v; |
| 12 | +} |
| 13 | + |
| 14 | +describe('SpacePilot report parsing', () => { |
| 15 | + it('decodes a 6-byte translation report', () => { |
| 16 | + expect(parseReport(1, i16(10, -20, 30))).toEqual({ axes: { tx: 10, ty: -20, tz: 30 } }); |
| 17 | + }); |
| 18 | + it('decodes a combined 12-byte 6-axis report with the driver sign convention', () => { |
| 19 | + const r = parseReport(1, i16(1, 2, 3, 4, 5, 6)); |
| 20 | + expect(r.axes).toEqual({ tx: 1, ty: 2, tz: 3, rx: -4, ry: -5, rz: 6 }); |
| 21 | + }); |
| 22 | + it('decodes rotation and button reports; ignores unknown ids', () => { |
| 23 | + expect(parseReport(2, i16(7, 8, 9)).axes).toEqual({ rx: -7, ry: -8, rz: 9 }); |
| 24 | + const b = new DataView(new ArrayBuffer(2)); |
| 25 | + b.setUint16(0, 0b101, true); |
| 26 | + expect(parseReport(3, b)).toEqual({ buttons: 5 }); |
| 27 | + expect(parseReport(9, i16(1))).toEqual({}); |
| 28 | + }); |
| 29 | + it('tolerates short payloads', () => { |
| 30 | + expect(parseReport(1, i16(5)).axes).toEqual({ tx: 5, ty: 0, tz: 0 }); |
| 31 | + expect(parseReport(3, new DataView(new ArrayBuffer(0)))).toEqual({ buttons: 0 }); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('SpacePilotDevice ingest', () => { |
| 36 | + it('merges axes across reports and fires rising-edge buttons once', () => { |
| 37 | + const d = new SpacePilotDevice(); |
| 38 | + const pressed: number[] = []; |
| 39 | + d.onButton = (i) => pressed.push(i); |
| 40 | + d.ingest(1, i16(100, 0, 0)); |
| 41 | + d.ingest(2, i16(0, 0, 50)); |
| 42 | + expect(d.sample).toMatchObject({ tx: 100, rz: 50 }); |
| 43 | + const b = new DataView(new ArrayBuffer(2)); |
| 44 | + b.setUint16(0, 0b11, true); |
| 45 | + d.ingest(3, b); |
| 46 | + d.ingest(3, b); // held, no new edge |
| 47 | + b.setUint16(0, 0b10, true); |
| 48 | + d.ingest(3, b); // button 1 released |
| 49 | + b.setUint16(0, 0b11, true); |
| 50 | + d.ingest(3, b); // button 1 pressed again |
| 51 | + expect(pressed).toEqual([1, 2, 1]); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('camera motion mapping', () => { |
| 56 | + it('applies deadzone, clamps and a monotonic curve', () => { |
| 57 | + expect(shape(0, DEFAULT_MOTION)).toBe(0); |
| 58 | + expect(shape(10, DEFAULT_MOTION)).toBe(0); |
| 59 | + expect(shape(350, DEFAULT_MOTION)).toBeCloseTo(1, 6); |
| 60 | + expect(shape(-9999, DEFAULT_MOTION)).toBeCloseTo(-1, 6); |
| 61 | + const a = shape(100, DEFAULT_MOTION); |
| 62 | + const b = shape(200, DEFAULT_MOTION); |
| 63 | + expect(a).toBeGreaterThan(0); |
| 64 | + expect(b).toBeGreaterThan(a); |
| 65 | + }); |
| 66 | + |
| 67 | + const rest = { tx: 0, ty: 0, tz: 0, rx: 0, ry: 0, rz: 0 }; |
| 68 | + |
| 69 | + it('is idle at rest and ignores nothing but the deadzone', () => { |
| 70 | + expect(isIdle(rest)).toBe(true); |
| 71 | + expect(isIdle({ ...rest, ry: 350 })).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + /** |
| 75 | + * Direction contract shared with VisionClaw's SpacePilotController (camera |
| 76 | + * mode): the same puck gesture must move the camera the same way in both |
| 77 | + * apps. Values after the driver's sign convention (rx, ry negated). |
| 78 | + * VisionClaw: theta -= ry; phi += rx; camera += -tz forward; pan +tx, +ty. |
| 79 | + */ |
| 80 | + const table: Array<[string, Partial<typeof rest>, (d: ReturnType<typeof cameraDelta>) => boolean]> = [ |
| 81 | + ['sideways tilt +ry orbits left (theta decreases)', { ry: 350 }, (d) => d.azimuth < 0], |
| 82 | + ['twist +rz orbits the same way as +ry', { rz: 350 }, (d) => d.azimuth < 0], |
| 83 | + ['forward tilt +rx orbits up (phi increases)', { rx: 350 }, (d) => d.elevation > 0], |
| 84 | + ['push +tz moves closer', { tz: 350 }, (d) => d.dollyFactor < 1], |
| 85 | + ['pull -tz moves away', { tz: -350 }, (d) => d.dollyFactor > 1], |
| 86 | + ['slide right +tx pans right', { tx: 350 }, (d) => d.panRight > 0], |
| 87 | + ['slide up +ty pans up', { ty: 350 }, (d) => d.panUp > 0], |
| 88 | + ]; |
| 89 | + for (const [name, input, check] of table) { |
| 90 | + it(name, () => { |
| 91 | + expect(check(cameraDelta({ ...rest, ...input }, 0.1))).toBe(true); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + it('clamps a stalled frame and combines yaw sources', () => { |
| 96 | + const push = cameraDelta({ ...rest, tz: 350 }, 1); |
| 97 | + expect(push.dollyFactor).toBeCloseTo(Math.exp(-DEFAULT_MOTION.dollyRate * 0.1), 6); |
| 98 | + const both = cameraDelta({ ...rest, ry: 350, rz: 350 }, 0.1); |
| 99 | + const one = cameraDelta({ ...rest, ry: 350 }, 0.1); |
| 100 | + // Yaw is clamped to full deflection, so tilt plus twist is no faster than either. |
| 101 | + expect(both.azimuth).toBeCloseTo(one.azimuth, 6); |
| 102 | + }); |
| 103 | + |
| 104 | + it('honours axis inversion', () => { |
| 105 | + const cfg = { ...DEFAULT_MOTION, invert: { ...DEFAULT_MOTION.invert, tz: true } }; |
| 106 | + const d = cameraDelta({ ...rest, tz: 350 }, 0.1, cfg); |
| 107 | + expect(d.dollyFactor).toBeGreaterThan(1); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('applyDelta on an orbit camera', () => { |
| 112 | + it('keeps the camera looking at the target and respects distance limits', () => { |
| 113 | + const cam = new THREE.PerspectiveCamera(); |
| 114 | + cam.position.set(0, 0, 10); |
| 115 | + const c = { target: new THREE.Vector3(0, 0, 0), minDistance: 2, maxDistance: 20 }; |
| 116 | + applyDelta(cam, c, { azimuth: Math.PI / 2, elevation: 0, dollyFactor: 0.5, panRight: 0, panUp: 0 }); |
| 117 | + expect(cam.position.distanceTo(c.target)).toBeCloseTo(5, 5); |
| 118 | + expect(Math.abs(cam.position.x)).toBeCloseTo(5, 5); |
| 119 | + const dir = new THREE.Vector3(0, 0, -1).applyQuaternion(cam.quaternion); |
| 120 | + expect(dir.dot(c.target.clone().sub(cam.position).normalize())).toBeCloseTo(1, 5); |
| 121 | + applyDelta(cam, c, { azimuth: 0, elevation: 0, dollyFactor: 0.01, panRight: 0, panUp: 0 }); |
| 122 | + expect(cam.position.distanceTo(c.target)).toBeCloseTo(2, 5); |
| 123 | + }); |
| 124 | + it('pans the target in the camera plane by a fraction of distance', () => { |
| 125 | + const cam = new THREE.PerspectiveCamera(); |
| 126 | + cam.position.set(0, 0, 10); |
| 127 | + cam.lookAt(0, 0, 0); |
| 128 | + const c = { target: new THREE.Vector3(0, 0, 0), minDistance: 0, maxDistance: 0 }; |
| 129 | + applyDelta(cam, c, { azimuth: 0, elevation: 0, dollyFactor: 1, panRight: 0.1, panUp: 0 }); |
| 130 | + expect(c.target.x).toBeCloseTo(1, 5); |
| 131 | + expect(cam.position.x).toBeCloseTo(1, 5); |
| 132 | + }); |
| 133 | +}); |
0 commit comments