mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 02:02:21 +00:00
Find a selected vessel or aircraft again from its card
While a selected vessel, aircraft or military aircraft is off screen, behind the globe or out of its layer's feed, its Context card shows Find it. One still in its feed is flown to (an aircraft followed). One out of its feed is looked up on its own track at the layer's time (the world clock's while the layer follows it), the card moves to that reading and says how old it is, and the camera flies there; once the feed has it again its layer selects it again. With no reading at or before the time the card says so and the camera stays. A vessel its layer announces is named by its own id, as a click names it, not by the context store's.
This commit is contained in:
1 parent
b57d50fe18
commit
22e9c58d84
27 files changed
+833
-22
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { fromDegreesInto } from '../geo/ellipsoid.js';
|
||||
|
||||
/**
|
||||
* Finding a selected vessel or aircraft again once it has left the screen or
|
||||
* its layer's feed: where its own track has it at a time, and whether a point
|
||||
* is on screen at all.
|
||||
*/
|
||||
|
||||
/**
|
||||
* The newest point of a track at or before `atMs` (any time when `atMs` is
|
||||
* not given), from a live source's `getTrack` records
|
||||
* (`{observedAtMs, latitude, longitude, baroAltitudeM?}`).
|
||||
* @param {Array<object>} points
|
||||
* @param {number} [atMs]
|
||||
* @returns {{lat: number, lon: number, altitudeM: ?number, observedAtMs: number}|null}
|
||||
*/
|
||||
export function readingAt(points, atMs) {
|
||||
let best = null;
|
||||
for (const point of points ?? []) {
|
||||
const lat = Number(point?.latitude);
|
||||
const lon = Number(point?.longitude);
|
||||
const observedAtMs = point?.observedAtMs;
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) continue;
|
||||
if (!Number.isFinite(observedAtMs)) continue;
|
||||
if (Number.isFinite(atMs) && observedAtMs > atMs) continue;
|
||||
if (best && best.observedAtMs >= observedAtMs) continue;
|
||||
const altitude = Number(point.baroAltitudeM ?? point.altitudeM);
|
||||
best = {
|
||||
lat,
|
||||
lon,
|
||||
altitudeM: Number.isFinite(altitude) ? altitude : null,
|
||||
observedAtMs,
|
||||
};
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a source's track has a vessel or aircraft at `atMs` (now, when not
|
||||
* given): `{status: 'found', lat, lon, altitudeM, observedAtMs, atMs}`, or
|
||||
* `{status: 'none'}` when the track has no point at or before the time, the
|
||||
* source keeps no tracks or the lookup fails.
|
||||
* @param {{getTrack?: Function}|null} source
|
||||
* @param {string} reference The track reference the layer's records carry.
|
||||
* @param {{atMs?: number, signal?: AbortSignal}} [options]
|
||||
*/
|
||||
export async function locateOnTrack(source, reference, { atMs, signal } = {}) {
|
||||
if (typeof source?.getTrack !== 'function' || !reference)
|
||||
return { status: 'none' };
|
||||
try {
|
||||
const track = await source.getTrack(
|
||||
reference,
|
||||
Number.isFinite(atMs) ? { signal, atMs } : { signal },
|
||||
);
|
||||
const reading = readingAt(track?.records, atMs);
|
||||
// The time it was looked up for, so its reading's age can be told.
|
||||
const at = Number.isFinite(atMs) ? atMs : Date.now();
|
||||
return reading
|
||||
? { status: 'found', ...reading, atMs: at }
|
||||
: { status: 'none' };
|
||||
} catch (error) {
|
||||
if (signal?.aborted) throw error;
|
||||
return { status: 'none' };
|
||||
}
|
||||
}
|
||||
|
||||
/** Mean Earth radius (m), for the horizon test. */
|
||||
const EARTH_RADIUS_M = 6_371_000;
|
||||
|
||||
/**
|
||||
* Whether a point is in the camera's view: in front of it, inside its field
|
||||
* of view and on the near side of the globe.
|
||||
* @param {{position: {x, y, z}, direction: {x, y, z}, up: {x, y, z},
|
||||
* right: {x, y, z}, fovy: number, aspectRatio: number}} pose
|
||||
* @param {number} lat
|
||||
* @param {number} lon
|
||||
* @param {number} [heightM]
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function onScreen(pose, lat, lon, heightM = 0) {
|
||||
if (!pose?.position || !Number.isFinite(lat) || !Number.isFinite(lon))
|
||||
return false;
|
||||
const p = fromDegreesInto(lon, lat, Math.max(0, heightM || 0), {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: 0,
|
||||
});
|
||||
const c = pose.position;
|
||||
const v = { x: p.x - c.x, y: p.y - c.y, z: p.z - c.z };
|
||||
const dot = (a, b) => a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
const ahead = dot(v, pose.direction);
|
||||
if (!(ahead > 0)) return false;
|
||||
const tanY = Math.tan(pose.fovy / 2);
|
||||
const tanX = tanY * (pose.aspectRatio || 1);
|
||||
if (Math.abs(dot(v, pose.right) / ahead) > tanX) return false;
|
||||
if (Math.abs(dot(v, pose.up) / ahead) > tanY) return false;
|
||||
// Behind the globe: the camera is below the point's horizon plane. Judged
|
||||
// on a sphere through the point, which is close enough for a button.
|
||||
const length = Math.hypot(p.x, p.y, p.z) || EARTH_RADIUS_M;
|
||||
const normal = { x: p.x / length, y: p.y / length, z: p.z / length };
|
||||
return dot({ x: c.x - p.x, y: c.y - p.y, z: c.z - p.z }, normal) > 0;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { locateOnTrack, onScreen, readingAt } from './locate.js';
|
||||
import { fromDegreesInto } from '../geo/ellipsoid.js';
|
||||
|
||||
const point = (observedAtMs, latitude, longitude, extra = {}) => ({
|
||||
observedAtMs,
|
||||
latitude,
|
||||
longitude,
|
||||
...extra,
|
||||
});
|
||||
|
||||
test('the reading at a time is the newest point at or before it', () => {
|
||||
const track = [
|
||||
point(3_000, 3, 30),
|
||||
point(1_000, 1, 10, { baroAltitudeM: 900 }),
|
||||
point(2_000, 2, 20),
|
||||
point(Number.NaN, 9, 90),
|
||||
point(4_000, Number.NaN, 40),
|
||||
];
|
||||
assert.deepEqual(readingAt(track, 2_500), {
|
||||
lat: 2,
|
||||
lon: 20,
|
||||
altitudeM: null,
|
||||
observedAtMs: 2_000,
|
||||
});
|
||||
assert.equal(readingAt(track, 1_000).altitudeM, 900, 'at its own time');
|
||||
assert.equal(readingAt(track).observedAtMs, 3_000, 'no time: the newest');
|
||||
assert.equal(readingAt(track, 500), null, 'before its first point');
|
||||
assert.equal(readingAt([], 500), null);
|
||||
});
|
||||
|
||||
test('a track lookup asks for the time and reads the point at it', async () => {
|
||||
const asked = [];
|
||||
const source = {
|
||||
async getTrack(reference, options) {
|
||||
asked.push([reference, options.atMs]);
|
||||
return { records: [point(1_000, 1, 10), point(5_000, 5, 50)] };
|
||||
},
|
||||
};
|
||||
assert.deepEqual(await locateOnTrack(source, 'vessel:a', { atMs: 2_000 }), {
|
||||
status: 'found',
|
||||
lat: 1,
|
||||
lon: 10,
|
||||
altitudeM: null,
|
||||
observedAtMs: 1_000,
|
||||
atMs: 2_000,
|
||||
});
|
||||
assert.deepEqual(asked, [['vessel:a', 2_000]]);
|
||||
assert.deepEqual(await locateOnTrack(source, 'vessel:a', { atMs: 500 }), {
|
||||
status: 'none',
|
||||
});
|
||||
const live = await locateOnTrack(source, 'vessel:a');
|
||||
assert.equal(live.observedAtMs, 5_000, 'live: its newest point');
|
||||
assert.equal(asked.at(-1)[1], undefined, 'live asks for no time');
|
||||
});
|
||||
|
||||
test('a source without tracks, or one that fails, finds nothing', async () => {
|
||||
assert.deepEqual(await locateOnTrack({}, 'a'), { status: 'none' });
|
||||
assert.deepEqual(await locateOnTrack(null, 'a'), { status: 'none' });
|
||||
const failing = {
|
||||
getTrack: async () => {
|
||||
throw new Error('down');
|
||||
},
|
||||
};
|
||||
assert.deepEqual(await locateOnTrack(failing, 'a'), { status: 'none' });
|
||||
assert.deepEqual(
|
||||
await locateOnTrack({ getTrack: async () => ({ records: [] }) }, ''),
|
||||
{ status: 'none' },
|
||||
'no reference',
|
||||
);
|
||||
});
|
||||
|
||||
/** A camera 1000 km above a point, looking straight down. */
|
||||
function poseAbove(lat, lon) {
|
||||
const at = fromDegreesInto(lon, lat, 0, { x: 0, y: 0, z: 0 });
|
||||
const length = Math.hypot(at.x, at.y, at.z);
|
||||
const up = { x: at.x / length, y: at.y / length, z: at.z / length };
|
||||
const position = {
|
||||
x: at.x + up.x * 1e6,
|
||||
y: at.y + up.y * 1e6,
|
||||
z: at.z + up.z * 1e6,
|
||||
};
|
||||
const direction = { x: -up.x, y: -up.y, z: -up.z };
|
||||
// North-ish screen up, east right: any pair at right angles will do.
|
||||
const east = {
|
||||
x: -Math.sin((lon * Math.PI) / 180),
|
||||
y: Math.cos((lon * Math.PI) / 180),
|
||||
z: 0,
|
||||
};
|
||||
const north = {
|
||||
x: up.y * east.z - up.z * east.y,
|
||||
y: up.z * east.x - up.x * east.z,
|
||||
z: up.x * east.y - up.y * east.x,
|
||||
};
|
||||
return {
|
||||
position,
|
||||
direction,
|
||||
up: north,
|
||||
right: east,
|
||||
fovy: Math.PI / 3,
|
||||
aspectRatio: 1.5,
|
||||
};
|
||||
}
|
||||
|
||||
test('a point is on screen in view, and not out of view or behind the globe', () => {
|
||||
const pose = poseAbove(10, 20);
|
||||
assert.equal(onScreen(pose, 10, 20), true, 'under the camera');
|
||||
assert.equal(onScreen(pose, 11, 21), true, 'near it');
|
||||
assert.equal(onScreen(pose, 30, 20), false, 'outside the view');
|
||||
assert.equal(onScreen(pose, -10, -160), false, 'the far side of the globe');
|
||||
assert.equal(onScreen(pose, Number.NaN, 20), false, 'no position');
|
||||
assert.equal(onScreen(null, 10, 20), false, 'no camera');
|
||||
});
|
||||
Reference in new issue
Block a user