mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Draw the CCTV calibration gizmo through the renderer ports and take it off the Cesium list
The gizmo's rings and move arrows are draw-port lines that dim where geometry hides them, and its handles and arrow tips are depth-test-free draw-port points; their item ids keep the cctv-gizmo-<part> scheme the layer's pick owner matches. Picks, rays and the cursor go through the query port and the renderer's canvas, and the drag math runs on plain vectors. The layer reports what the gizmo draws for the QA harness, which reads the parts there instead of from the viewer's entities.
This commit is contained in:
1 parent
393f1686f2
commit
bbad02b73e
6 files changed
+507
-278
No files matched your search
+272
-65
@@ -12,7 +12,6 @@
|
||||
// - the controller ends an open drag, and its camera hold, on the next press.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
GIZMO_ID_PREFIX,
|
||||
closestParamOnAxis,
|
||||
@@ -22,8 +21,11 @@ import {
|
||||
signedAngleDelta,
|
||||
} from './cctvGizmo.js';
|
||||
import { FAKE_EVENT_TYPES, fakeInput } from '../input/testDoubles.mjs';
|
||||
import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs';
|
||||
import { fromDegrees } from '../geo/ellipsoid.js';
|
||||
import { add, distance, negate, normalize, scale } from '../geo/vector.js';
|
||||
|
||||
const c3 = (x, y, z) => new Cesium.Cartesian3(x, y, z);
|
||||
const c3 = (x, y, z) => ({ x, y, z });
|
||||
|
||||
test('closestParamOnAxis: perpendicular ray hits the axis at its own offset', () => {
|
||||
// Axis along +X from origin. Ray shooting straight down (-Z) from (10, 0, 5):
|
||||
@@ -40,10 +42,7 @@ test('closestParamOnAxis: perpendicular ray hits the axis at its own offset', ()
|
||||
test('closestParamOnAxis: skew ray resolves to the geometric closest point', () => {
|
||||
// Axis +X. Ray from (0, 10, 0) toward (1, -1, 0)/√2 passes closest to the
|
||||
// axis around x=10 (it reaches y=0 at x=10).
|
||||
const dir = Cesium.Cartesian3.normalize(
|
||||
c3(1, -1, 0),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const dir = normalize(c3(1, -1, 0));
|
||||
const t = closestParamOnAxis(c3(0, 10, 0), dir, c3(0, 0, 0), c3(1, 0, 0));
|
||||
assert.ok(Math.abs(t - 10) < 1e-6, `expected ~10, got ${t}`);
|
||||
});
|
||||
@@ -63,10 +62,7 @@ test('closestParamOnAxis: near-parallel ray/axis returns null', () => {
|
||||
closestParamOnAxis(c3(0, 1, 0), c3(1, 0, 0), c3(0, 0, 0), c3(1, 0, 0)),
|
||||
null,
|
||||
);
|
||||
const nearly = Cesium.Cartesian3.normalize(
|
||||
c3(1, 1e-9, 0),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const nearly = normalize(c3(1, 1e-9, 0));
|
||||
assert.equal(
|
||||
closestParamOnAxis(c3(0, 1, 0), nearly, c3(0, 0, 0), c3(1, 0, 0)),
|
||||
null,
|
||||
@@ -81,25 +77,19 @@ test('rayPlaneIntersect: straight-on hit lands at the expected point', () => {
|
||||
c3(0, 0, 1),
|
||||
);
|
||||
assert.ok(hit, 'expected a hit');
|
||||
assert.ok(Cesium.Cartesian3.distance(hit, c3(0, 0, 0)) < 1e-9);
|
||||
assert.ok(distance(hit, c3(0, 0, 0)) < 1e-9);
|
||||
});
|
||||
|
||||
test('rayPlaneIntersect: oblique hit resolves correctly', () => {
|
||||
const dir = Cesium.Cartesian3.normalize(
|
||||
c3(1, 0, -1),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const dir = normalize(c3(1, 0, -1));
|
||||
const hit = rayPlaneIntersect(c3(0, 0, 5), dir, c3(0, 0, 0), c3(0, 0, 1));
|
||||
assert.ok(hit);
|
||||
assert.ok(Cesium.Cartesian3.distance(hit, c3(5, 0, 0)) < 1e-9, `got ${hit}`);
|
||||
assert.ok(distance(hit, c3(5, 0, 0)) < 1e-9, `got ${JSON.stringify(hit)}`);
|
||||
});
|
||||
|
||||
test('rayPlaneIntersect: grazing ray (|dir·n| < 0.08) returns null', () => {
|
||||
// dir almost in-plane: z component 0.05 < 0.08 threshold.
|
||||
const dir = Cesium.Cartesian3.normalize(
|
||||
c3(1, 0, -0.05),
|
||||
new Cesium.Cartesian3(),
|
||||
);
|
||||
const dir = normalize(c3(1, 0, -0.05));
|
||||
assert.equal(
|
||||
rayPlaneIntersect(c3(0, 0, 5), dir, c3(0, 0, 0), c3(0, 0, 1)),
|
||||
null,
|
||||
@@ -136,60 +126,133 @@ test('signedAngleDelta: shortest-path wrap', () => {
|
||||
assert.ok(Math.abs(signedAngleDelta(d2r(10), d2r(30)) - d2r(20)) < 1e-9);
|
||||
});
|
||||
|
||||
test('a press after a release Cesium never reported ends the open drag and its camera hold', (t) => {
|
||||
/**
|
||||
* A viewer double drawing through a fake draw port, whose query port picks
|
||||
* `picked` and casts the ray `ray` (a ray,
|
||||
* or a function of the window position).
|
||||
*/
|
||||
function gizmoViewer(t, { picked, ray }) {
|
||||
const viewer = {
|
||||
entities: {
|
||||
byId: new Map(),
|
||||
getById(id) {
|
||||
return this.byId.get(id);
|
||||
},
|
||||
add(options) {
|
||||
this.byId.set(options.id, options);
|
||||
return options;
|
||||
},
|
||||
remove(entity) {
|
||||
this.byId.delete(entity.id);
|
||||
},
|
||||
},
|
||||
scene: {
|
||||
canvas: { style: {} },
|
||||
screenSpaceCameraController: { enableInputs: true },
|
||||
pick: () => ({ id: { id: `${GIZMO_ID_PREFIX}move-up` } }),
|
||||
drillPick: () => [],
|
||||
},
|
||||
};
|
||||
const mount = Cesium.Cartesian3.fromDegrees(0, 0, 10);
|
||||
const east = new Cesium.Cartesian3(0, 1, 0); // east at lon 0, lat 0
|
||||
viewer.camera = {
|
||||
getPickRay: () => ({
|
||||
origin: Cesium.Cartesian3.add(
|
||||
mount,
|
||||
Cesium.Cartesian3.multiplyByScalar(east, 100, new Cesium.Cartesian3()),
|
||||
new Cesium.Cartesian3(),
|
||||
),
|
||||
direction: Cesium.Cartesian3.negate(east, new Cesium.Cartesian3()),
|
||||
const queries = [];
|
||||
const { draw, unbind } = bindFakeDraw(viewer, {
|
||||
query: {
|
||||
pick: (x, y, window) => (queries.push(['pick', window]), picked()),
|
||||
drillPick: (x, y, options) => (queries.push(['drill', options]), []),
|
||||
pickRay: (x, y) => (typeof ray === 'function' ? ray(x, y) : ray),
|
||||
},
|
||||
});
|
||||
t.after(unbind);
|
||||
return { viewer, draw, queries };
|
||||
}
|
||||
|
||||
const mount = fromDegrees(0, 0, 10);
|
||||
const east = c3(0, 1, 0); // east at lon 0, lat 0
|
||||
const gizmoRecord = () => ({
|
||||
frustumPositions: {
|
||||
mount,
|
||||
capCenter: mount,
|
||||
tl: mount,
|
||||
tr: mount,
|
||||
bl: mount,
|
||||
br: mount,
|
||||
},
|
||||
frustumGeometry: { rangeM: 50 },
|
||||
camera: {
|
||||
headingDeg: 0,
|
||||
pitchDeg: 0,
|
||||
rangeM: 50,
|
||||
calibration: {},
|
||||
basePose: {},
|
||||
},
|
||||
});
|
||||
|
||||
test('the gizmo draws its parts through the draw port, shown in ADJUST mode only', (t) => {
|
||||
const { viewer, draw } = gizmoViewer(t, { picked: () => null, ray: null });
|
||||
const { input } = fakeInput(viewer);
|
||||
t.after(() => input.destroy());
|
||||
const record = gizmoRecord();
|
||||
const gizmo = createCalibrationGizmo({
|
||||
viewer,
|
||||
getActiveRecord: () => record,
|
||||
applyPatch() {},
|
||||
endPatch() {},
|
||||
});
|
||||
const lines = draw.find('cctv-gizmo:');
|
||||
const points = [...draw.sets.values()].find(
|
||||
(set) => set.kind === 'points' && set.spec.id.startsWith('cctv-gizmo:'),
|
||||
);
|
||||
assert.equal(lines.kind, 'lines');
|
||||
assert.equal(lines.items.size, 0, 'nothing drawn before ADJUST');
|
||||
gizmo.setEnabled(true);
|
||||
assert.deepEqual(
|
||||
[...lines.items.keys()],
|
||||
['ring-heading', 'ring-pitch', 'move-east', 'move-north', 'move-up'].map(
|
||||
(part) => GIZMO_ID_PREFIX + part,
|
||||
),
|
||||
);
|
||||
assert.deepEqual(
|
||||
[...points.items.keys()],
|
||||
[
|
||||
'move-east',
|
||||
'move-north',
|
||||
'move-up',
|
||||
'handle-range',
|
||||
'handle-fov-l',
|
||||
'handle-fov-r',
|
||||
].map((part) => GIZMO_ID_PREFIX + part),
|
||||
);
|
||||
const ring = lines.items.get(`${GIZMO_ID_PREFIX}ring-heading`);
|
||||
assert.equal(ring.mode, '3d');
|
||||
assert.equal(ring.widthPx, 3);
|
||||
assert.equal(ring.positions.length, 97, 'a closed 96-segment ring');
|
||||
assert.ok(Math.abs(ring.style.color[3] - 0.9) < 1e-12);
|
||||
assert.ok(Math.abs(ring.depthFailColor[3] - 0.45) < 1e-12);
|
||||
const arrow = lines.items.get(`${GIZMO_ID_PREFIX}move-up`);
|
||||
assert.equal(arrow.style.kind, 'arrow');
|
||||
assert.equal(arrow.widthPx, 12);
|
||||
// The up arrow runs 1.6 ring radii (6 m minimum at a 50 m range) up.
|
||||
assert.ok(
|
||||
distance(arrow.positions[1], add(mount, scale(c3(1, 0, 0), 9.6))) < 1e-6,
|
||||
);
|
||||
const tip = points.items.get(`${GIZMO_ID_PREFIX}move-up`);
|
||||
assert.deepEqual(tip.pos, arrow.positions[1]);
|
||||
assert.equal(tip.pixelSize, 14);
|
||||
assert.equal(tip.disableDepthTest, Number.POSITIVE_INFINITY);
|
||||
assert.ok(
|
||||
[...lines.items.values(), ...points.items.values()].every(
|
||||
(item) => item.show,
|
||||
),
|
||||
);
|
||||
gizmo.setEnabled(false);
|
||||
assert.ok(
|
||||
[...lines.items.values(), ...points.items.values()].every(
|
||||
(item) => !item.show,
|
||||
),
|
||||
'hidden off ADJUST',
|
||||
);
|
||||
gizmo.destroy();
|
||||
assert.equal(lines.disposed, true);
|
||||
assert.equal(points.disposed, true);
|
||||
});
|
||||
|
||||
test('a press after a release Cesium never reported ends the open drag and its camera hold', (t) => {
|
||||
let lineSet = null;
|
||||
const { viewer, draw, queries } = gizmoViewer(t, {
|
||||
picked: () => ({
|
||||
set: lineSet,
|
||||
id: `${GIZMO_ID_PREFIX}move-up`,
|
||||
surface: false,
|
||||
}),
|
||||
};
|
||||
ray: { origin: add(mount, scale(east, 100)), direction: negate(east) },
|
||||
});
|
||||
const { input, handler } = fakeInput(viewer);
|
||||
t.after(() => input.destroy());
|
||||
const record = {
|
||||
frustumPositions: {
|
||||
mount,
|
||||
capCenter: mount,
|
||||
tl: mount,
|
||||
tr: mount,
|
||||
bl: mount,
|
||||
br: mount,
|
||||
},
|
||||
frustumGeometry: { rangeM: 50 },
|
||||
camera: {
|
||||
headingDeg: 0,
|
||||
pitchDeg: 0,
|
||||
rangeM: 50,
|
||||
calibration: {},
|
||||
basePose: {},
|
||||
},
|
||||
};
|
||||
const record = gizmoRecord();
|
||||
const ended = [];
|
||||
const gizmo = createCalibrationGizmo({
|
||||
viewer,
|
||||
@@ -198,11 +261,13 @@ test('a press after a release Cesium never reported ends the open drag and its c
|
||||
endPatch: (r) => ended.push(r),
|
||||
});
|
||||
t.after(() => gizmo.destroy());
|
||||
lineSet = draw.find('cctv-gizmo:').spec.id;
|
||||
gizmo.setEnabled(true);
|
||||
const controller = viewer.scene.screenSpaceCameraController;
|
||||
|
||||
handler.fire(FAKE_EVENT_TYPES.LEFT_DOWN, { position: { x: 5, y: 5 } });
|
||||
assert.equal(gizmo.isDragging(), true);
|
||||
assert.deepEqual(queries[0], ['pick', { width: 14, height: 14 }]);
|
||||
assert.equal(controller.enableInputs, false);
|
||||
// The release came with a modifier key held: Cesium raised no LEFT_UP.
|
||||
handler.fire(FAKE_EVENT_TYPES.LEFT_DOWN, { position: { x: 5, y: 5 } });
|
||||
@@ -211,3 +276,145 @@ test('a press after a release Cesium never reported ends the open drag and its c
|
||||
assert.equal(gizmo.isDragging(), false);
|
||||
assert.equal(controller.enableInputs, true, 'no hold was stranded');
|
||||
});
|
||||
|
||||
test('a pick of another set or a stray id is no gizmo part', (t) => {
|
||||
let picked = null;
|
||||
const { viewer, draw } = gizmoViewer(t, {
|
||||
picked: () => picked,
|
||||
ray: { origin: add(mount, scale(east, 100)), direction: negate(east) },
|
||||
});
|
||||
const { input, handler } = fakeInput(viewer);
|
||||
t.after(() => input.destroy());
|
||||
const record = gizmoRecord();
|
||||
const gizmo = createCalibrationGizmo({
|
||||
viewer,
|
||||
getActiveRecord: () => record,
|
||||
applyPatch() {},
|
||||
endPatch() {},
|
||||
});
|
||||
t.after(() => gizmo.destroy());
|
||||
gizmo.setEnabled(true);
|
||||
picked = { set: 'other', id: `${GIZMO_ID_PREFIX}move-up`, surface: false };
|
||||
handler.fire(FAKE_EVENT_TYPES.LEFT_DOWN, { position: { x: 5, y: 5 } });
|
||||
assert.equal(gizmo.isDragging(), false, 'another set');
|
||||
handler.fire(FAKE_EVENT_TYPES.LEFT_UP, { position: { x: 5, y: 5 } });
|
||||
picked = {
|
||||
set: draw.find('cctv-gizmo:').spec.id,
|
||||
id: `${GIZMO_ID_PREFIX}nothing`,
|
||||
surface: false,
|
||||
};
|
||||
handler.fire(FAKE_EVENT_TYPES.LEFT_DOWN, { position: { x: 5, y: 5 } });
|
||||
assert.equal(gizmo.isDragging(), false, 'no such part');
|
||||
});
|
||||
|
||||
test('dragging the east arrow moves the camera east by what the pointer travelled', (t) => {
|
||||
const up = c3(1, 0, 0); // up at lon 0, lat 0
|
||||
let lineSet = null;
|
||||
const { viewer, draw } = gizmoViewer(t, {
|
||||
picked: () => ({
|
||||
set: lineSet,
|
||||
id: `${GIZMO_ID_PREFIX}move-east`,
|
||||
surface: false,
|
||||
}),
|
||||
// Straight down onto the east axis, `x` metres east of the mount.
|
||||
ray: (x) => ({
|
||||
origin: add(add(mount, scale(east, x)), scale(up, 100)),
|
||||
direction: negate(up),
|
||||
}),
|
||||
});
|
||||
const { input, handler } = fakeInput(viewer);
|
||||
t.after(() => input.destroy());
|
||||
const record = gizmoRecord();
|
||||
record.camera.calibration = { offsetEastM: 2 };
|
||||
const patches = [];
|
||||
const gizmo = createCalibrationGizmo({
|
||||
viewer,
|
||||
getActiveRecord: () => record,
|
||||
applyPatch: (patch, pinned) => patches.push([patch, pinned]),
|
||||
endPatch() {},
|
||||
});
|
||||
t.after(() => gizmo.destroy());
|
||||
lineSet = draw.find('cctv-gizmo:').spec.id;
|
||||
gizmo.setEnabled(true);
|
||||
|
||||
handler.fire(FAKE_EVENT_TYPES.LEFT_DOWN, { position: { x: 5, y: 5 } });
|
||||
assert.equal(gizmo.isDragging(), true);
|
||||
assert.equal(viewer.scene.canvas.style.cursor, 'grabbing');
|
||||
handler.fire(FAKE_EVENT_TYPES.MOUSE_MOVE, {
|
||||
startPosition: { x: 5, y: 5 },
|
||||
endPosition: { x: 12, y: 5 },
|
||||
});
|
||||
assert.equal(patches.length, 1);
|
||||
const [patch, pinned] = patches[0];
|
||||
assert.equal(pinned, record, 'patched on the record dragged');
|
||||
assert.ok(Math.abs(patch.offsetEastM - 9) < 1e-6, `${patch.offsetEastM}`);
|
||||
handler.fire(FAKE_EVENT_TYPES.LEFT_UP, { position: { x: 12, y: 5 } });
|
||||
assert.equal(gizmo.isDragging(), false);
|
||||
});
|
||||
|
||||
test('hovering a handle grows it and shows the grab cursor', (t) => {
|
||||
let pointSet = null;
|
||||
let over = true;
|
||||
const { viewer, draw } = gizmoViewer(t, {
|
||||
picked: () =>
|
||||
over
|
||||
? {
|
||||
set: pointSet,
|
||||
id: `${GIZMO_ID_PREFIX}handle-range`,
|
||||
surface: false,
|
||||
}
|
||||
: null,
|
||||
ray: null,
|
||||
});
|
||||
const { input, handler } = fakeInput(viewer);
|
||||
t.after(() => input.destroy());
|
||||
const record = gizmoRecord();
|
||||
const gizmo = createCalibrationGizmo({
|
||||
viewer,
|
||||
getActiveRecord: () => record,
|
||||
applyPatch() {},
|
||||
endPatch() {},
|
||||
});
|
||||
t.after(() => gizmo.destroy());
|
||||
const points = [...draw.sets.values()].find(
|
||||
(set) => set.kind === 'points' && set.spec.id.startsWith('cctv-gizmo:'),
|
||||
);
|
||||
pointSet = points.spec.id;
|
||||
gizmo.setEnabled(true);
|
||||
const size = () =>
|
||||
points.items.get(`${GIZMO_ID_PREFIX}handle-range`).pixelSize;
|
||||
assert.equal(size(), 13);
|
||||
handler.fire(FAKE_EVENT_TYPES.MOUSE_MOVE, {
|
||||
startPosition: { x: 5, y: 5 },
|
||||
endPosition: { x: 6, y: 5 },
|
||||
});
|
||||
assert.equal(size(), 17);
|
||||
assert.equal(viewer.scene.canvas.style.cursor, 'grab');
|
||||
});
|
||||
|
||||
test('the harness reads each part as drawn, an arrow with its line and tip', (t) => {
|
||||
const { viewer } = gizmoViewer(t, { picked: () => null, ray: null });
|
||||
const { input } = fakeInput(viewer);
|
||||
t.after(() => input.destroy());
|
||||
const record = gizmoRecord();
|
||||
const gizmo = createCalibrationGizmo({
|
||||
viewer,
|
||||
getActiveRecord: () => record,
|
||||
applyPatch() {},
|
||||
endPatch() {},
|
||||
});
|
||||
t.after(() => gizmo.destroy());
|
||||
assert.deepEqual(gizmo.partsForTest(), {}, 'nothing drawn before ADJUST');
|
||||
gizmo.setEnabled(true);
|
||||
const parts = gizmo.partsForTest();
|
||||
const arrow = parts[`${GIZMO_ID_PREFIX}move-east`];
|
||||
assert.equal(arrow.positions.length, 2, 'its line');
|
||||
assert.deepEqual(arrow.pos, arrow.positions[1], 'its tip');
|
||||
assert.equal(arrow.show, true);
|
||||
assert.equal(parts[`${GIZMO_ID_PREFIX}ring-heading`].show, true);
|
||||
assert.ok(parts[`${GIZMO_ID_PREFIX}handle-range`].pos);
|
||||
gizmo.setEnabled(false);
|
||||
assert.ok(
|
||||
Object.values(gizmo.partsForTest()).every((part) => part.show === false),
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user