From 65942008bf1b23b1ff2a88046bee9d9934f87141 Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Wed, 18 Nov 2026 21:23:41 +0400 Subject: [PATCH] fix(cctv): count a lone camera in the marker it sits against A ground cell with one camera always drew its icon, so at some zooms a camera poked out from under a count marker with its own callout. An icon closer than the merge gap to a count marker now joins that marker: the count grows by one and the icon, its callout and its card are not drawn. Two lone cameras with no count beside them stay two icons, and the active camera always keeps its own icon. --- changelog.d/1808-cctv-camera-counts.md | 9 ++-- docs/CURRENT-STATE.md | 4 +- src/data/cctvCellCounts.js | 4 ++ src/data/cctvCellCounts.test.mjs | 41 ++++++++++++++++++ src/data/cctvViewThinning.test.mjs | 60 ++++++++++++++++++++------ src/layers/cctv/countMarkers.js | 18 ++++++-- src/layers/cctv/policy.js | 4 ++ src/layers/cctv/rendering.js | 9 ++-- 8 files changed, 125 insertions(+), 24 deletions(-) diff --git a/changelog.d/1808-cctv-camera-counts.md b/changelog.d/1808-cctv-camera-counts.md index 7ffb4bd262..8de3034801 100644 --- a/changelog.d/1808-cctv-camera-counts.md +++ b/changelog.d/1808-cctv-camera-counts.md @@ -1,7 +1,8 @@ - Show how many cameras stand behind a thinned CCTV marker. Zoomed out, a ground cell with two or more cameras is one round marker with a small camera icon and the count (`999`, `1.7K`, `12K` in every language); it grows with - the count, merges with a neighbour it would overlap, and a click zooms in - until the group splits. The camera layer's colour is now green: count - markers and idle single cameras share it, the active camera stays gold and - live-video cameras stay violet (gekh, #1808). + the count, merges with a neighbour it would overlap, takes in a lone camera + whose icon would sit against it, and a click zooms in until the group + splits. The camera layer's colour is now green: count markers and idle + single cameras share it, the active camera stays gold and live-video cameras + stay violet (gekh, #1808). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 4c1669a931..a6069e262b 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -1956,7 +1956,9 @@ The CCTV layer draws only the cameras on or near the screen. Zoomed out, a ground cell containing multiple cameras shows, at their centre, a marker with a small green camera pictogram under its top edge and the count just below its middle. The marker grows with the count and is drawn on the world overlay at -the same resolution as camera callouts. +the same resolution as camera callouts. A lone camera whose icon would sit +within 4 px of a marker is counted in that marker instead of being drawn; the +active camera always keeps its own icon. The count includes cameras beyond the screen edge and excludes cameras beyond the horizon. Counts that would overlap on screen merge into one that carries their sum. Clicking a count zooms to its group. Single cameras keep their icons, diff --git a/src/data/cctvCellCounts.js b/src/data/cctvCellCounts.js index 0543925ed2..bf746f6e1a 100644 --- a/src/data/cctvCellCounts.js +++ b/src/data/cctvCellCounts.js @@ -91,6 +91,8 @@ function collectNeighbors(marker, grid, members, gap) { const bucket = grid.get(`${marker.col + col},${marker.row + row}`); if (!bucket) continue; for (const other of bucket) { + // Lone cameras join a count beside them, never each other. + if (marker.cell.count === 1 && other.cell.count === 1) continue; // Distance between the horizontal centre segments of the two pills. const extra = marker.halfSegment + other.halfSegment; const dx = Math.max(0, Math.abs(marker.x - other.x) - extra); @@ -107,6 +109,8 @@ function collectNeighbors(marker, grid, members, gap) { /** * Merge connected groups of overlapping horizontal pills. Each marker occupies * one grid bucket. Even the widest pill can only touch its eight neighbors. + * A cell with one camera takes the footprint `sizeFor(1)` gives its icon and + * joins a count it touches; two such cells alone stay apart. * Ground cells stay intact for the camera picker and individual-icon pass. */ export function mergeCountCells(cells, project, sizeFor, gap) { diff --git a/src/data/cctvCellCounts.test.mjs b/src/data/cctvCellCounts.test.mjs index 49eb59faaf..66d9702c36 100644 --- a/src/data/cctvCellCounts.test.mjs +++ b/src/data/cctvCellCounts.test.mjs @@ -94,3 +94,44 @@ test('stadiums merge by their own radii and segment lengths across grid boundari // Large pills can reach across a row boundary at their full diameter. check([7350, 7350], [origin, { x: 35, y: 88 }], [14700]); }); + +test('a lone camera joins a count it touches, and lone cameras alone stay apart', () => { + const sizes = { + 1: { width: 24, height: 24 }, + 9: { width: 40, height: 40 }, + }; + const gap = 4; + const check = (counts, positions, expected) => { + const cells = counts.map((count, i) => ({ + ...createCameraCell(String(i)), + count, + })); + let i = 0; + const groups = mergeCountCells( + cells, + () => positions[i++], + (count) => sizes[count], + gap, + ); + assert.deepEqual( + groups.map((cell) => cell.count), + expected, + ); + // A lone camera that stays apart is still its own cell object. + assert.deepEqual( + groups.filter((cell) => cell.count === 1), + cells.filter((cell) => groups.includes(cell) && cell.count === 1), + ); + }; + const at = (x, y = 100) => ({ x, y }); + // Radii 20 and 12: the four-pixel gap is clear at 36 pixels. + check([9, 1], [at(100), at(135)], [10]); + check([9, 1], [at(100), at(136)], [9, 1]); + check([1, 9], [at(135), at(100)], [10]); + // Overlapping icons with no count beside them stay two icons. + check([1, 1], [at(100), at(110)], [1, 1]); + // A count takes every lone camera it touches, on either side. + check([1, 9, 1], [at(70), at(100), at(130)], [11]); + // A lone camera that only touches another lone camera is left alone. + check([1, 1, 9], [at(40), at(70), at(100)], [1, 10]); +}); diff --git a/src/data/cctvViewThinning.test.mjs b/src/data/cctvViewThinning.test.mjs index e78810b87a..0b0be24703 100644 --- a/src/data/cctvViewThinning.test.mjs +++ b/src/data/cctvViewThinning.test.mjs @@ -332,6 +332,43 @@ test('a crowded cell shows its count and a singleton keeps its icon', () => { ); }); +test('a lone camera beside a count joins it, unless it is the active camera', () => { + // Three cameras north of a cell boundary, one 111 m away south of it. + const { records } = recordsAt([ + [0.0005, 0.0005], + [0.0006, 0.0005], + [0.0007, 0.0005], + [0.0006, -0.0005], + [0.2, 0], + ]); + seedCoverage({ viewer: viewerAt(20_000), records }); + refreshHorizonCulling(); + assert.deepEqual(shownIds(records), ['cam-4']); + assert.deepEqual(_viewRecordIdsForTest(), ['cam-4']); + assert.deepEqual( + markers().map((item) => item.cell.count), + [4], + ); + assert.deepEqual( + cctvLayer.getDetectableObjects().map((o) => o.sourceId), + ['cam-4'], + ); + // The picker still offers one camera from each ground cell. + assert.equal(cctvLayer.getUIState().cameraIndex.length, 3); + + seedCoverage({ + viewer: viewerAt(20_000), + records, + activeCameraId: 'cam-3', + }); + refreshHorizonCulling(); + assert.deepEqual(shownIds(records), ['cam-3', 'cam-4']); + assert.deepEqual( + markers().map((item) => item.cell.count), + [3], + ); +}); + test('a pan keeps the same winners, and an unchanged view keeps its list', () => { const { records } = recordsAt(CLUSTER); seedCoverage({ @@ -698,20 +735,22 @@ test('overlapping cells merge at their weighted centre and keep every camera in }); refreshHorizonCulling(); const shown = markers(); - assert.deepEqual(shownIds(records), ['cam-7', 'cam-8']); + // The lone camera 111 m south sits under the merged marker and joins it; + // the active one beside it keeps its own icon. + assert.deepEqual(shownIds(records), ['cam-7']); assert.equal( shown.reduce((sum, m) => sum + m.cell.count, 0), - 7, + 8, ); assert.deepEqual( shown.map((m) => m.cell.count).sort((a, b) => a - b), - [2, 5], + [2, 6], ); - const merged = shown.find((m) => m.cell.count === 5); + const merged = shown.find((m) => m.cell.count === 6); assert.ok( Cesium.Cartesian3.distance( merged.pos, - Cesium.Cartesian3.fromDegrees(0.0008, 0, 10), + Cesium.Cartesian3.fromDegrees(0.004 / 6, -0.001 / 6, 10), ) < 0.01, ); const positions = shown.map((m) => [m.cell.key, m.pos, m.height, m.width]); @@ -720,13 +759,10 @@ test('overlapping cells merge at their weighted centre and keep every camera in clickMarker(merged); const { bounds } = flights.at(-1); assert.ok( - records - .slice(0, 5) - .every( - (r) => - Cesium.Cartesian3.distance(r.position, bounds.center) < - bounds.radius, - ), + [...records.slice(0, 5), records[8]].every( + (r) => + Cesium.Cartesian3.distance(r.position, bounds.center) < bounds.radius, + ), ); assert.ok( bounds.radius < 300, diff --git a/src/layers/cctv/countMarkers.js b/src/layers/cctv/countMarkers.js index b6f38e7fe7..84831c12d4 100644 --- a/src/layers/cctv/countMarkers.js +++ b/src/layers/cctv/countMarkers.js @@ -19,6 +19,7 @@ import { COUNT_GLYPH_PADDING_PX, COUNT_LABEL_DROP, COUNT_MERGE_GAP_PX, + COUNT_SINGLE_ICON_PX, COUNT_MERGE_PASSES, } from './policy.js'; @@ -28,6 +29,10 @@ const GLYPH_ASPECT = 1.5; const RING_PX = 2; const RIM_PX = 1; const STROKE_INSET = RIM_PX + RING_PX / 2; +const SINGLE_ICON = Object.freeze({ + width: COUNT_SINGLE_ICON_PX, + height: COUNT_SINGLE_ICON_PX, +}); /** Apply the billboard's RGB tint to the sprite and find its painted bounds. */ function rasterizeGlyph(canvas, image, size, dpr, color) { @@ -198,9 +203,9 @@ export function createCountMarkers( /** Reuse marker slots in ground-key order, and invalidate only changed frames. */ function update(cells, project) { - let visible = cells - .filter((cell) => cell.count > 1) - .sort((a, b) => a.key.localeCompare(b.key)); + // Lone cameras take part so that one beside a count joins it. + let visible = cells.slice().sort((a, b) => a.key.localeCompare(b.key)); + const footprint = (count) => (count > 1 ? sizeFor(count) : SINGLE_ICON); for ( let pass = 0; pass < COUNT_MERGE_PASSES && visible.length > 1; @@ -209,12 +214,17 @@ export function createCountMarkers( const merged = mergeCountCells( visible, project, - sizeFor, + footprint, COUNT_MERGE_GAP_PX, ); if (merged.length === visible.length) break; visible = merged; } + // A lone camera that is no longer its own entry now counts in a marker. + const apart = new Set(visible); + for (const cell of cells) + if (cell.count === 1) cell.absorbed = !apart.has(cell); + visible = visible.filter((cell) => cell.count > 1); let changed = visible.length !== markers.length; for (let i = 0; i < visible.length; i++) { const cell = visible[i]; diff --git a/src/layers/cctv/policy.js b/src/layers/cctv/policy.js index 17ed1ae561..06fc5788fd 100644 --- a/src/layers/cctv/policy.js +++ b/src/layers/cctv/policy.js @@ -104,6 +104,10 @@ export const COUNT_LABEL_DROP = 0.2; // Minimum gap between the outer rims of two separate markers. export const COUNT_MERGE_GAP_PX = 4; +// On-screen size of a lone camera's icon when testing it against a count +// marker: an icon closer than the merge gap joins that count. +export const COUNT_SINGLE_ICON_PX = 24; + // Recheck moved centres and wider labels while bounding work on motion ticks. export const COUNT_MERGE_PASSES = 3; diff --git a/src/layers/cctv/rendering.js b/src/layers/cctv/rendering.js index c794519295..2d50592ac3 100644 --- a/src/layers/cctv/rendering.js +++ b/src/layers/cctv/rendering.js @@ -435,23 +435,26 @@ export function createRendering({ cell.rank = rank; } } + // Marks each lone camera that a count marker beside it has taken in. + layerState._counts?.update(visibleCells, project); for (const cell of visibleCells) { const record = cell.winner; picker.push(record); record.pickerPass = pass; - if (cell.count === 1) { + if (cell.count === 1 && !cell.absorbed) { record.viewPass = pass; shown.push(record); } } - layerState._counts?.update(visibleCells, project); const writes = []; let coverageChanged = false; let cardsChanged = false; for (const record of layerState._records) { const cell = record.viewCell; record.viewCell = null; - const grouped = Boolean(cell?.winner && cell.count > 1); + const grouped = Boolean( + cell?.winner && (cell.count > 1 || cell.absorbed), + ); if (grouped !== !!record.viewGrouped) { const id = record.camera.id; coverageChanged ||= layerState._coverageCohort?.has(id);