mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
Fly to places, landmarks and imported files through the camera and query ports
The camera ground guard reads the eye from the camera port's pose, the flight state from the port, and the surface under it from the query port's heights, and lifts through the port. Place and landmark flights read the terrain height from the query port and build their targets with geo. Framing ranges read the field of view from the pose (the frustum's wider-side angle, as it read before). An imported file's flight frames its footprint, plain lon/lat/height points, with geo's bounding sphere and the port's flyToBounds; walking the file's Cesium data source for those points moves into the file importer, which stays on the Cesium allow-list. The Cesium query port answers terrain heights without depth textures, as the height service always has: only the drawn surface needs them. cameraGroundGuard, locations, cameraFraming and flyToDataSource leave the allow-list. The annotation resolver, still listed, loads the camera factory for the application operations entry, which reached it through locations before.
This commit is contained in:
1 parent
a2c4d27b5a
commit
7ab969599c
14 files changed
+303
-214
No files matched your search
+37
-100
@@ -1,96 +1,38 @@
|
||||
/** Frame the complete finite footprint of a session-local file. */
|
||||
import * as Cesium from 'cesium';
|
||||
import { guardCameraAboveGround } from '../cameraGroundGuard.js';
|
||||
import { rangeForBoundingSphere } from '../data/cameraFraming.js';
|
||||
import { cancelCameraArrival } from '../data/cameraArrival.js';
|
||||
import { heightsFor } from '../queries/heights.js';
|
||||
import { distance, fromDegrees, toLonLatH } from '../geo/index.js';
|
||||
import { sphereFromPoints } from '../geo/sphere.js';
|
||||
import { findCamera, findQuery } from '../render/rendererOf.js';
|
||||
|
||||
/** All visible positions, path vertices, polygon rings, walls and overlay bounds. */
|
||||
export function dataSourcePoints(dataSource, time = Cesium.JulianDate.now()) {
|
||||
const points = [];
|
||||
const add = (position, grounded = false) => {
|
||||
if (
|
||||
!position ||
|
||||
![position.x, position.y, position.z].every(Number.isFinite)
|
||||
)
|
||||
return;
|
||||
const c = Cesium.Cartographic.fromCartesian(position);
|
||||
if (c && Number.isFinite(c.longitude) && Number.isFinite(c.latitude))
|
||||
points.push([
|
||||
Cesium.Math.toDegrees(c.longitude),
|
||||
Cesium.Math.toDegrees(c.latitude),
|
||||
grounded ? 0 : c.height,
|
||||
]);
|
||||
};
|
||||
if (dataSource?.show === false) return points;
|
||||
for (const entity of dataSource?.importedEntities ??
|
||||
dataSource?.entities?.values ??
|
||||
[]) {
|
||||
if (entity.isShowing === false) continue;
|
||||
add(
|
||||
entity.position?.getValue?.(time),
|
||||
entity.billboard?.heightReference?.getValue?.(time) ===
|
||||
Cesium.HeightReference.CLAMP_TO_GROUND,
|
||||
);
|
||||
for (const graphic of [entity.polyline, entity.wall])
|
||||
for (const position of graphic?.positions?.getValue?.(time) || [])
|
||||
add(position, graphic.clampToGround?.getValue?.(time) === true);
|
||||
const rings = [entity.polygon?.hierarchy?.getValue?.(time)];
|
||||
while (rings.length) {
|
||||
const ring = rings.pop();
|
||||
for (const position of ring?.positions || [])
|
||||
add(
|
||||
position,
|
||||
entity.polygon?.perPositionHeight?.getValue?.(time) !== true,
|
||||
);
|
||||
for (const hole of ring?.holes || []) rings.push(hole);
|
||||
}
|
||||
const rectangle = entity.rectangle?.coordinates?.getValue?.(time);
|
||||
if (rectangle) {
|
||||
const bounds = Cesium.RectangleGeometry.computeRectangle({
|
||||
rectangle,
|
||||
rotation: entity.rectangle.rotation?.getValue?.(time) || 0,
|
||||
});
|
||||
for (const corner of [
|
||||
'southwest',
|
||||
'southeast',
|
||||
'northwest',
|
||||
'northeast',
|
||||
]) {
|
||||
const c = Cesium.Rectangle[corner](bounds);
|
||||
if (Number.isFinite(c.longitude) && Number.isFinite(c.latitude))
|
||||
points.push([
|
||||
Cesium.Math.toDegrees(c.longitude),
|
||||
Cesium.Math.toDegrees(c.latitude),
|
||||
entity.rectangle.height?.getValue?.(time) || 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
/** Fly synchronously to the footprint; later navigation always owns its camera. */
|
||||
export function flyToDataSource(
|
||||
/**
|
||||
* Fly synchronously to a footprint, `[lon, lat, height]` points (degrees,
|
||||
* metres) such as a file's (`dataSourcePoints`); later navigation always
|
||||
* owns its camera. False when there is nothing to frame or no camera.
|
||||
*/
|
||||
export function flyToFootprint(
|
||||
viewer,
|
||||
dataSource,
|
||||
points,
|
||||
{ duration = 1.5, ground = null } = {},
|
||||
) {
|
||||
const points = dataSourcePoints(dataSource);
|
||||
if (!points.length) return false;
|
||||
if (!points?.length) return false;
|
||||
const camera = findCamera(viewer);
|
||||
if (!camera) return false;
|
||||
cancelCameraArrival(viewer);
|
||||
const positions = points.map(([lon, lat, height]) =>
|
||||
Cesium.Cartesian3.fromDegrees(lon, lat, height),
|
||||
fromDegrees(lon, lat, height),
|
||||
);
|
||||
const sphere = Cesium.BoundingSphere.fromPoints(positions);
|
||||
const center =
|
||||
Cesium.Cartographic.fromCartesian(sphere.center) ||
|
||||
Cesium.Cartographic.fromDegrees(points[0][0], points[0][1]);
|
||||
const lat = Cesium.Math.toDegrees(center.latitude),
|
||||
lon = Cesium.Math.toDegrees(center.longitude);
|
||||
const terrain = viewer.scene
|
||||
? heightsFor(viewer.scene).now(center, { against: 'terrain' })
|
||||
: null;
|
||||
const sphere = sphereFromPoints(positions);
|
||||
const center = toLonLatH(sphere.center) || {
|
||||
lon: points[0][0],
|
||||
lat: points[0][1],
|
||||
height: 0,
|
||||
};
|
||||
const { lat, lon } = center;
|
||||
const terrain =
|
||||
findQuery(viewer)?.sampleHeightNow({ lon, lat }, { against: 'terrain' }) ??
|
||||
null;
|
||||
const cached = ground?.cachedGroundFloor?.(lat, lon);
|
||||
const height =
|
||||
Number.isFinite(terrain) && terrain > 0
|
||||
@@ -101,25 +43,20 @@ export function flyToDataSource(
|
||||
? terrain
|
||||
: 0;
|
||||
ground?.warmGroundFloor?.([{ lat, lon }]);
|
||||
sphere.center = Cesium.Cartesian3.fromRadians(
|
||||
center.longitude,
|
||||
center.latitude,
|
||||
Math.max(0, height, center.height),
|
||||
);
|
||||
sphere.radius = 0;
|
||||
const framed = {
|
||||
center: fromDegrees(lon, lat, Math.max(0, height, center.height)),
|
||||
radius: 0,
|
||||
};
|
||||
for (const position of positions)
|
||||
sphere.radius = Math.max(
|
||||
sphere.radius,
|
||||
Cesium.Cartesian3.distance(sphere.center, position),
|
||||
);
|
||||
viewer.camera?.flyToBoundingSphere?.(sphere, {
|
||||
offset: new Cesium.HeadingPitchRange(
|
||||
viewer.camera.heading || 0,
|
||||
-Math.PI / 2,
|
||||
Math.max(800, rangeForBoundingSphere(viewer, sphere.radius)),
|
||||
),
|
||||
duration,
|
||||
complete: () => guardCameraAboveGround(viewer, { lat, lon }),
|
||||
framed.radius = Math.max(framed.radius, distance(framed.center, position));
|
||||
camera.flyToBounds(framed, {
|
||||
offset: {
|
||||
heading: camera.pose().heading || 0,
|
||||
pitch: -Math.PI / 2,
|
||||
range: Math.max(800, rangeForBoundingSphere(viewer, framed.radius)),
|
||||
},
|
||||
durationS: duration,
|
||||
onComplete: () => guardCameraAboveGround(viewer, { lat, lon }),
|
||||
});
|
||||
return true;
|
||||
}
|
||||
@@ -1,8 +1,22 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import { cancelCameraArrival } from '../data/cameraArrival.js';
|
||||
import { dataSourcePoints, flyToDataSource } from './flyToDataSource.js';
|
||||
import {
|
||||
cancelCameraArrival,
|
||||
ownCameraArrival,
|
||||
} from '../data/cameraArrival.js';
|
||||
import { dataSourcePoints } from '../data/geoFileImport.js';
|
||||
import { bindFakeCesiumCamera } from '../testSupport/fakeCameraPort.mjs';
|
||||
import { fakeSceneQuery } from '../testSupport/fakeCesiumScene.mjs';
|
||||
import { flyToFootprint } from './flyToDataSource.js';
|
||||
|
||||
/** As the file import flies to one of its files. */
|
||||
const flyToDataSource = (viewer, dataSource) =>
|
||||
flyToFootprint(viewer, dataSourcePoints(dataSource));
|
||||
|
||||
/** A Cesium-shaped viewer double, bound to the camera and query ports. */
|
||||
const bound = (viewer) =>
|
||||
bindFakeCesiumCamera(viewer, { query: fakeSceneQuery(viewer) });
|
||||
|
||||
function source() {
|
||||
const ds = new Cesium.CustomDataSource('t');
|
||||
@@ -36,22 +50,27 @@ test('points come from positions, lines and polygons, as lon/lat only', () => {
|
||||
|
||||
test('the camera frames the footprint at the surface, never a buried center', () => {
|
||||
const calls = [];
|
||||
const viewer = {
|
||||
const viewer = bound({
|
||||
scene: {},
|
||||
camera: {
|
||||
heading: 0,
|
||||
positionWC: Cesium.Cartesian3.fromDegrees(-122.49, 37.77, 900),
|
||||
directionWC: new Cesium.Cartesian3(0, 0, -1),
|
||||
upWC: new Cesium.Cartesian3(0, 1, 0),
|
||||
flyToBoundingSphere: (sphere, options) => calls.push({ sphere, options }),
|
||||
},
|
||||
flyTo: () => calls.push('viewer.flyTo'),
|
||||
};
|
||||
});
|
||||
assert.equal(flyToDataSource(viewer, source()), true);
|
||||
const center = Cesium.Cartographic.fromCartesian(calls[0].sphere.center);
|
||||
assert.ok(Math.abs(center.height) < 50, `center height ${center.height}`);
|
||||
assert.ok(calls[0].options.offset.range >= 800);
|
||||
assert.equal(
|
||||
typeof calls[0].options.complete,
|
||||
'function',
|
||||
'the ground guard runs on landing',
|
||||
);
|
||||
// Landing starts the ground guard, which takes over the arrival.
|
||||
let handedOver = false;
|
||||
ownCameraArrival(viewer, () => (handedOver = true));
|
||||
calls[0].options.complete();
|
||||
assert.equal(handedOver, true, 'the ground guard runs on landing');
|
||||
cancelCameraArrival(viewer);
|
||||
assert.equal(
|
||||
flyToDataSource(viewer, new Cesium.CustomDataSource('empty')),
|
||||
false,
|
||||
@@ -113,13 +132,13 @@ test('portrait framing accounts for FOV, high ground and symmetric extents', ()
|
||||
},
|
||||
});
|
||||
const calls = [];
|
||||
const viewer = {
|
||||
const viewer = bound({
|
||||
scene: { globe: { getHeight: () => 2600 } },
|
||||
camera: {
|
||||
frustum: { fov: Math.PI / 3, aspectRatio: 390 / 844 },
|
||||
frustum: { fovy: Math.PI / 3, aspectRatio: 390 / 844 },
|
||||
flyToBoundingSphere: (sphere, options) => calls.push({ sphere, options }),
|
||||
},
|
||||
};
|
||||
});
|
||||
flyToDataSource(viewer, ds);
|
||||
assert.ok(
|
||||
Math.abs(
|
||||
|
||||
@@ -2,10 +2,15 @@
|
||||
import {
|
||||
GeoImportError,
|
||||
MAX_IMPORTED_FILES,
|
||||
dataSourcePoints,
|
||||
loadGeoFile,
|
||||
} from '../data/geoFileImport.js';
|
||||
import { formatNumber, t, tn, translateKnown } from '../i18n/index.js';
|
||||
import { flyToDataSource } from './flyToDataSource.js';
|
||||
import { flyToFootprint } from './flyToDataSource.js';
|
||||
|
||||
/** Fly to the whole footprint of one imported file's data source. */
|
||||
const flyToDataSource = (viewer, dataSource, options) =>
|
||||
flyToFootprint(viewer, dataSourcePoints(dataSource), options);
|
||||
|
||||
// One color per file, in import order, from the whiteboard palette.
|
||||
const COLORS = ['#39d0ff', '#ffb547', '#5dff9f', '#ff6b6b', '#8be9ff'];
|
||||
|
||||
Reference in new issue
Block a user