diff --git a/src/layers/cctv/cards.js b/src/layers/cctv/cards.js index 04b8fb86f..2aeccf9ca 100644 --- a/src/layers/cctv/cards.js +++ b/src/layers/cctv/cards.js @@ -8,7 +8,6 @@ import { CCTV_FRAME_CANVAS_H, applyFrameResult, } from '../../data/cctvCards.js'; -import * as Cesium from 'cesium'; import { horizonOccluder } from '../../data/iconOrientation.js'; import { selectCctvLod, applyEvictionGrace } from '../../data/cctvLod.js'; import { @@ -84,9 +83,10 @@ export function createCards({ state: layerState, services, parts, source }) { return; } const scene = layerState._viewer.scene; - const carto = layerState._viewer.camera.positionCartographic; - const viewerLat = carto ? Cesium.Math.toDegrees(carto.latitude) : 0; - const viewerLon = carto ? Cesium.Math.toDegrees(carto.longitude) : 0; + // An eye with no geographic position (the earth's centre) measures from 0, 0. + const pose = parts.model.eyePose(); + const viewerLat = pose ? pose.lat : 0; + const viewerLon = pose ? pose.lon : 0; // The active camera is excluded from ambient selection ALWAYS (not just // after its async activation settles). It is published separately in the // protected lane below, and grace never applies to it. @@ -146,7 +146,7 @@ export function createCards({ state: layerState, services, parts, source }) { // the budget fill through the screen-distribution grid, so periphery // cells hold cards instead of everything clustering at screen center. const { cardIds, budgets } = selectCctvLod(candidates, { - cameraHeightM: carto?.height, + cameraHeightM: pose?.height, incumbentIds: layerState._cardIds, viewW: width, viewH: height, diff --git a/src/layers/cctv/controls.js b/src/layers/cctv/controls.js index 492ff1758..1f83b7468 100644 --- a/src/layers/cctv/controls.js +++ b/src/layers/cctv/controls.js @@ -202,7 +202,7 @@ export function createControls({ state: layerState, services, parts, source }) { * @param {Object} [options={}] * @param {number} [options.maxCount] - Maximum number of objects to return. * @param {number} [options.seed] - Offset seed for deterministic stride sampling. - * @returns {{ position: Cesium.Cartesian3, id: string, type: string }[]} + * @returns {{ position: {x: number, y: number, z: number}, id: string, type: string }[]} */ getDetectableObjects(options = {}) { // The cameras the view pass shows, not icons it has hidden. diff --git a/src/layers/cctv/geometryQueue.js b/src/layers/cctv/geometryQueue.js index dabed2373..214afb863 100644 --- a/src/layers/cctv/geometryQueue.js +++ b/src/layers/cctv/geometryQueue.js @@ -1,5 +1,4 @@ import { followedOf } from '../../data/followed.js'; -import * as Cesium from 'cesium'; import { GEO_PROGRESS_NOTIFY_INTERVAL_MS, GEO_PROGRESS_NOTIFY_BATCH_LIMIT, @@ -304,13 +303,9 @@ export function createGeometryQueue({ layerState._tilesReadyReenqueued = false; if (!layerState._records.length) return; const active = parts.selection.getActiveRecord(); - const carto = layerState._viewer?.camera?.positionCartographic; - const refLat = carto - ? Cesium.Math.toDegrees(carto.latitude) - : (active?.camera.lat ?? 0); - const refLon = carto - ? Cesium.Math.toDegrees(carto.longitude) - : (active?.camera.lon ?? 0); + const pose = parts.model.eyePose(); + const refLat = pose ? pose.lat : (active?.camera.lat ?? 0); + const refLon = pose ? pose.lon : (active?.camera.lon ?? 0); const pending = geometryWorkingSet() .filter((record) => record !== active) .map((record) => ({ diff --git a/src/layers/cctv/lifecycle.js b/src/layers/cctv/lifecycle.js index 7545f70c1..4adb3d787 100644 --- a/src/layers/cctv/lifecycle.js +++ b/src/layers/cctv/lifecycle.js @@ -1,4 +1,6 @@ -import * as Cesium from 'cesium'; +import { fromDegrees } from '../../geo/ellipsoid.js'; +import { distance, dot } from '../../geo/vector.js'; +import { findCamera } from '../../render/rendererOf.js'; import { viewshedColors, cameraHue } from '../../data/cctvViewshed.js'; import { CCTV_OVERLAY_SOURCE_ID } from '../../data/cctvCards.js'; import { GIZMO_ID_PREFIX } from '../../data/cctvGizmo.js'; @@ -45,12 +47,11 @@ export function createLifecycle({ /** Records the camera pose the last view pass ran at. */ function noteViewPose() { - const camera = layerState._viewer?.camera; - if (!camera?.positionWC) return; + const pose = findCamera(layerState._viewer)?.pose(); + if (!pose) return; layerState._viewPose = { - position: Cesium.Cartesian3.clone(camera.positionWC), - direction: - camera.directionWC && Cesium.Cartesian3.clone(camera.directionWC), + position: pose.position, + direction: pose.direction, }; } @@ -60,16 +61,14 @@ export function createLifecycle({ * would otherwise rerun the full pass on every tick. */ function viewPoseChanged() { - const camera = layerState._viewer?.camera; + const now = findCamera(layerState._viewer)?.pose(); const pose = layerState._viewPose; - if (!pose || !camera?.positionWC) return true; - const height = Math.max(1, camera.positionCartographic?.height ?? 0); - const moved = Cesium.Cartesian3.distance(camera.positionWC, pose.position); + if (!pose || !now) return true; + // An eye with no geographic position (the earth's centre) has no height. + const height = Math.max(1, Number.isFinite(now.height) ? now.height : 0); + const moved = distance(now.position, pose.position); if (moved > height * VIEW_MOTION_MIN_MOVE_FRACTION) return true; - if (!pose.direction || !camera.directionWC) return false; - return ( - Cesium.Cartesian3.dot(camera.directionWC, pose.direction) < MIN_TURN_COS - ); + return dot(now.direction, pose.direction) < MIN_TURN_COS; } function viewMotionTick() { @@ -134,7 +133,7 @@ export function createLifecycle({ * Initializes the CCTV layer: loads camera sources, builds the catalog, * restores calibration from localStorage, creates camera icons, sets up click * handling, and performs initial health sync. Coverage entities stay lazy. - * @param {Cesium.Viewer} viewer - The Cesium viewer instance. + * @param {object} viewer - The viewer. */ async init(viewer) { layerState._sourceAbort?.abort(); @@ -225,7 +224,7 @@ export function createLifecycle({ ? groundPrior.ellipsoid : Number(camera.groundElevationM) || 0; camera.absoluteHeightM = priorGround + camera.mountHeightM; - const position = Cesium.Cartesian3.fromDegrees( + const position = fromDegrees( camera.lon, camera.lat, camera.absoluteHeightM, @@ -406,7 +405,7 @@ export function createLifecycle({ // Item A (owner round 2): the scene pick found no camera — try the // painted ambient cards. The cards canvas is pointer-events:none (this // handler owns the events), so a click landing on a card's rect selects - // its camera exactly like a click on the icon. Cesium click positions + // its camera exactly like a click on the icon. Click positions // and the recorded rects are both CSS px — direct comparison. const cardId = layerState._cctvOverlayHost.hitTest( event.screenPosition.x, @@ -540,7 +539,7 @@ export function createLifecycle({ /** * Tears down the layer: destroys click handler, projection loop, coverage * entities, icons, and clears all runtime state and subscribers. - * @param {Cesium.Viewer} [viewer] - Viewer instance (falls back to stored ref). + * @param {object} [viewer] - Viewer instance (falls back to stored ref). */ destroy(viewer) { services.credits?.hideOsmCredit?.(layerState._viewer, 'cctv'); diff --git a/src/layers/cctv/model.js b/src/layers/cctv/model.js index b1d127100..41a932aa3 100644 --- a/src/layers/cctv/model.js +++ b/src/layers/cctv/model.js @@ -334,10 +334,20 @@ export function createModel({ state: layerState, services, parts, source }) { * @returns {string} Grid key in the form "zoomBucket:latGrid:lonGrid". */ - function currentViewContext() { + /** + * The eye's pose from the camera port, or null when there is no camera or + * the eye has no geographic position (the earth's centre). + * @returns {object|null} + */ + + function eyePose() { const pose = findCamera(layerState._viewer)?.pose(); - // An eye at the earth's centre has no geographic position. - if (!pose || !Number.isFinite(pose.lat)) return 'none'; + return Number.isFinite(pose?.lat) ? pose : null; + } + + function currentViewContext() { + const pose = eyePose(); + if (!pose) return 'none'; const { lat, lon } = pose; const alt = pose.height || 0; const zoomBucket = @@ -505,6 +515,7 @@ export function createModel({ state: layerState, services, parts, source }) { return cameraId !== activeCameraId || record?.activationDone !== true; } return { + eyePose, toRad, normalizeHeading, clamp, diff --git a/src/layers/cctv/navigation.js b/src/layers/cctv/navigation.js index 1d5641913..60accad83 100644 --- a/src/layers/cctv/navigation.js +++ b/src/layers/cctv/navigation.js @@ -1,7 +1,6 @@ import { isFollowing } from '../../data/followed.js'; -import * as Cesium from 'cesium'; import { CCTV_FOCUS_RESULT } from './policy.js'; -import { cameraOf } from '../../render/cesium/camera.js'; +import { cameraOf } from '../../render/rendererOf.js'; export function createNavigation({ state: layerState, @@ -10,15 +9,14 @@ export function createNavigation({ source, }) { /** - * Finds the camera closest to the Cesium viewer's current position. + * Finds the camera closest to the viewer's current position. * @returns {string|null} Camera ID of the nearest camera, or null. */ function nearestCameraIdToViewer() { - const carto = layerState._viewer?.camera?.positionCartographic; - if (!carto || !layerState._records.length) return null; - const lat = Cesium.Math.toDegrees(carto.latitude); - const lon = Cesium.Math.toDegrees(carto.longitude); + const pose = parts.model.eyePose(); + if (!pose || !layerState._records.length) return null; + const { lat, lon } = pose; let best = null; for (const record of layerState._records) { @@ -36,9 +34,9 @@ export function createNavigation({ } /** - * Flies the Cesium viewer camera to frame the specified CCTV camera, + * Flies the viewer's camera to frame the specified CCTV camera, * looking along its heading from above. - * @param {Cesium.Viewer|null} viewer Cesium viewer that owns the camera. + * @param {object|null} viewer The viewer that owns the camera. * @param {Object|null} record CCTV camera runtime record. * @param {number} [duration=2.2] - Flight duration in seconds. * @returns {'focused'|'no-active-camera'|'tracking-holds-view'|'cockpit-active'} Focus result. diff --git a/src/layers/cctv/rendering.js b/src/layers/cctv/rendering.js index a636b2359..07a021d48 100644 --- a/src/layers/cctv/rendering.js +++ b/src/layers/cctv/rendering.js @@ -20,6 +20,9 @@ import { writeHoldSegment } from '../../data/iconSegment.js'; import { IconSetPrimitive } from '../../data/iconSetPrimitive.js'; import { iconAtlas } from '../../data/iconAtlas.js'; import { toColor } from '../../render/cesium/draw.js'; +// Loads the factory a viewer of a caller's own gets its camera port from, +// which the rest of the layer reads the eye through. +import '../../render/cesium/camera.js'; // The policy's renderer colours as the Cesium colours the icons and lines take. const ACTIVE_CAMERA_COLOR = toColor(ACTIVE_CAMERA_RGBA); diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 85bfe5767..9593ce662 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -40,11 +40,7 @@ "src/input/dispatcher.js", "src/input/sensorPick.js", "src/layers/airQuality/index.js", - "src/layers/cctv/cards.js", "src/layers/cctv/geometry.js", - "src/layers/cctv/geometryQueue.js", - "src/layers/cctv/lifecycle.js", - "src/layers/cctv/navigation.js", "src/layers/cctv/projection.js", "src/layers/cctv/rendering.js", "src/layers/directions/index.js",