diff --git a/changelog.d/1277-annotation-issue-fix.md b/changelog.d/1277-annotation-issue-fix.md index ba5eb97f3b..29437bebd8 100644 --- a/changelog.d/1277-annotation-issue-fix.md +++ b/changelog.d/1277-annotation-issue-fix.md @@ -1,4 +1,5 @@ - Stop screen annotations sampling surface height for marks that are hidden, behind the camera or over the horizon. Visible marks still waiting for - terrain share 32 samples per projection pass and retry once a second, so a - large board no longer stalls the frame (asturksever, #1277). + terrain share 32 samples per projection pass and retry once a second, so + drawing a large board no longer stalls the app for seconds (asturksever, + #1277). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index c4514ceb9c..2976dd53af 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -148,12 +148,13 @@ injection. Screen annotations skip hidden records and, before surface sampling, reject an unsettled anchor only when its whole accepted height range (-430 to 9000 m) is behind the camera or below the globe horizon; a settled height is tested as the -actual point. Anchors outside the conservative frustum still project at their -cached or zero height, so a partly visible arrow keeps its connector, but do -not sample. Unresolved visible heights share a 32-sample projection-pass budget, -served oldest-refused first, retry at most once per second per anchor on -rendered frames, and retain no extra timer or listener. Validated heights remain -settled in the bounded cache. +actual point. Anchors outside the conservative frustum do not sample and still +project at their cached or zero height, without the horizon test that this +stand-in height cannot decide, so a partly visible arrow keeps its connector. +Unresolved visible heights share a 32-sample budget per projection pass (each +board change and each rendered frame runs one), served oldest-refused first, +retry at most once per second per anchor, and retain no extra timer or +listener. Validated heights remain settled in the bounded cache. Terrain height requests accept at most 64 points, matching the browser service's existing batch size and one bounded upstream chunk. Larger requests return HTTP diff --git a/src/annotations/screenAnnotationRenderer.js b/src/annotations/screenAnnotationRenderer.js index 301402f983..8c0d724e79 100644 --- a/src/annotations/screenAnnotationRenderer.js +++ b/src/annotations/screenAnnotationRenderer.js @@ -415,13 +415,15 @@ export function createScreenAnnotationRenderer( const key = heightKey(lon, lat); const cached = heightCache.get(key); let height; + let horizonTest = true; if (cached?.settled) { height = groundHeight(lon, lat, key, cached); } else { if (heightRangeHidden(lon, lat)) return null; // The frustum restricts sampling only; an endpoint outside it still - // projects (at its cached or zero height) so a partly visible arrow, - // route or ring keeps its on-screen connector and label. + // projects (at its cached or zero height, without the horizon test) + // so a partly visible arrow, route or ring keeps its on-screen + // connector and label. Cesium.Cartesian3.fromDegrees( lon, lat, @@ -433,14 +435,17 @@ export function createScreenAnnotationRenderer( cullingVolume?.computeVisibility(heightBounds) !== Cesium.Intersect.OUTSIDE; height = groundHeight(lon, lat, key, cached, inFrustum); + horizonTest = inFrustum; } const world = Cesium.Cartesian3.fromDegrees(lon, lat, height); // Reject points behind the camera (they produce wrapped/extreme coords). Cesium.Cartesian3.subtract(world, scene.camera.positionWC, scratchDir); if (Cesium.Cartesian3.dot(scratchDir, scene.camera.directionWC) <= 0) return null; - // Reject points beyond the globe horizon. - if (!occluder.isPointVisible(world)) return null; + // Reject points beyond the globe horizon. An unsampled stand-in height + // outside the frustum cannot decide it: the range test above found no + // proof that the real height is hidden. + if (horizonTest && !occluder.isPointVisible(world)) return null; const win = Cesium.SceneTransforms.worldToWindowCoordinates( scene, world, diff --git a/src/annotations/screenAnnotationRenderer.test.mjs b/src/annotations/screenAnnotationRenderer.test.mjs index 3dec5314e0..c6f2afe06d 100644 --- a/src/annotations/screenAnnotationRenderer.test.mjs +++ b/src/annotations/screenAnnotationRenderer.test.mjs @@ -686,7 +686,6 @@ function samplingBoard(t) { return useScreenAnnotationsStore(createPinia()); } -/** The renderer under test, on the given board and scene. */ function samplingRenderer(store, scene) { return createScreenAnnotationRenderer( { scene, camera: scene.camera, trackedEntity: null }, @@ -757,6 +756,99 @@ test('an arrow with one endpoint outside a real frustum keeps its visible connec renderer.destroy(); }); +// An arrow whose far end lies outside the frustum (so it is not sampled) and +// beyond the horizon at the 0 m stand-in height, but above it at its real +// 100 m height, keeps its connector and label. +test('an arrow whose unsampled far end is past the 0 m horizon keeps its connector', (t) => { + const previousRaf = globalThis.requestAnimationFrame; + globalThis.requestAnimationFrame = (fn) => { + fn(); + return 1; + }; + t.after(() => { + globalThis.requestAnimationFrame = previousRaf; + }); + const W = 800; + const H = 800; + const half = Cesium.Math.toRadians(30); + const frustum = new Cesium.PerspectiveFrustum(); + frustum.fov = 2 * half; + frustum.aspectRatio = 1; + frustum.near = 1; + frustum.far = 1e8; + // 100 m above the ellipsoid at (0, 0), level, looking due east. + const camera = { + positionWC: Cesium.Cartesian3.fromDegrees(0, 0, 100), + directionWC: new Cesium.Cartesian3(0, 1, 0), + upWC: new Cesium.Cartesian3(1, 0, 0), + positionCartographic: { height: 100 }, + frustum, + }; + // A real perspective projection matching the frustum above. + t.mock.method(Cesium.SceneTransforms, 'worldToWindowCoordinates', (_scene, world, result) => { + const right = Cesium.Cartesian3.cross(camera.directionWC, camera.upWC, new Cesium.Cartesian3()); + const v = Cesium.Cartesian3.subtract(world, camera.positionWC, new Cesium.Cartesian3()); + const z = Cesium.Cartesian3.dot(v, camera.directionWC); + const out = result || new Cesium.Cartesian2(); + out.x = ((Cesium.Cartesian3.dot(v, right) / z / Math.tan(half) + 1) / 2) * W; + out.y = ((1 - Cesium.Cartesian3.dot(v, camera.upWC) / z / Math.tan(half)) / 2) * H; + return out; + }); + // Near end 2 km east; far end 56 km away, 50 degrees north of the view axis. + const near = { lon: Number((2000 / 111319.49).toFixed(5)), lat: 0 }; + const bearing = Cesium.Math.toRadians(50); + const far = { + lon: Number(((56000 * Math.cos(bearing)) / 111319.49).toFixed(5)), + lat: Number(((56000 * Math.sin(bearing)) / 110574).toFixed(5)), + }; + const surface = { [near.lon]: 10, [far.lon]: 100 }; + const postRender = new Cesium.Event(); + const scene = { + camera, + canvas: { width: W, height: H }, + postRender, + clampToHeightSupported: true, + clampToHeight(world) { + const c = Cesium.Cartographic.fromCartesian(world); + const h = surface[Number(Cesium.Math.toDegrees(c.longitude).toFixed(5))]; + return h === undefined ? undefined : Cesium.Cartesian3.fromRadians(c.longitude, c.latitude, h); + }, + }; + + // Preconditions: the scenario is the one described above. + const occluder = new Cesium.EllipsoidalOccluder(Cesium.Ellipsoid.WGS84, camera.positionWC); + const farAt = (h) => Cesium.Cartesian3.fromDegrees(far.lon, far.lat, h); + assert.equal(occluder.isPointVisible(farAt(100)), true, 'far end above the horizon at 100 m'); + assert.equal(occluder.isPointVisible(farAt(0)), false, 'far end past the horizon at 0 m'); + const volume = frustum.computeCullingVolume(camera.positionWC, camera.directionWC, camera.upWC); + assert.equal( + volume.computeVisibility(new Cesium.BoundingSphere(farAt(0), 9500)), + Cesium.Intersect.OUTSIDE, + 'far end outside the frustum plus the 9.5 km sampling sphere', + ); + const farWin = Cesium.SceneTransforms.worldToWindowCoordinates(scene, farAt(100)); + assert.ok(farWin.x > -W && farWin.x < 0, 'far end off-screen left, inside [-w, 2w]'); + + const store = useScreenAnnotationsStore(createPinia()); + const renderer = createScreenAnnotationRenderer( + { scene, camera, trackedEntity: null }, + { store, activeTrackedReadoutId: () => null }, + ); + t.after(() => renderer.destroy()); + renderer.add({ + id: 'long-arrow', + type: 'arrow', + label: 'Ridge', + anchor: near, + to: far, + }); + for (let i = 0; i < 3; i++) postRender.raiseEvent(); + const [mark] = store.board.marks; + assert.equal(mark.display, '', 'the arrow stays drawn'); + const path = mark.children.find((child) => child.attrs.class?.includes('gev-anno-arrow')); + assert.match(path.attrs.d, /^M 400\.0 4\d\d\.\d Q /, 'the connector starts at the on-screen end'); +}); + test('a low-angle cached roof stays visible when range endpoints fail different tests', (t) => { const store = samplingBoard(t); t.mock.method(Cesium.SceneTransforms, 'worldToWindowCoordinates', () => ({ x: 400, y: 380 }));