diff --git a/src/data/cctvViewshed.js b/src/data/cctvViewshed.js index 6654b599c..9a8fc2fe5 100644 --- a/src/data/cctvViewshed.js +++ b/src/data/cctvViewshed.js @@ -6,17 +6,17 @@ * * Two responsibilities, both pure of layer state: * - Color identity: a stable per-camera hue (golden-angle spaced over the - * id-sorted catalog index) and the derived fill/line Cesium colors. - * - Volume geometry: a translucent frustum pyramid primitive built FROM the - * exact frustumCartesians positions the wireframe uses — welded by - * construction, never recomputed independently. 5 vertices, 6 triangles, - * synchronous build (trivially small), no picking. + * id-sorted catalog index) and the derived fill/line renderer colors. + * - Volume geometry: the vertex and index buffers of a translucent frustum + * pyramid built FROM the exact frustumCartesians positions the wireframe + * uses — welded by construction, never recomputed independently. 5 + * vertices, 6 triangles; the layer's rendering part draws them. * * Nothing here queries the scene or persists anything: colors are * presentation-only and volumes are rebuilt only at the moments the frustum * wireframe already rewrites (pose edits / visible-set changes). */ -import * as Cesium from 'cesium'; +import { hslColor } from '../render/color.js'; /** Golden angle in degrees — maximally spreads consecutive indices around the hue wheel. */ const GOLDEN_ANGLE_DEG = 137.50776405003785; @@ -43,15 +43,16 @@ export function cameraHue(index) { * Derived viewshed colors for a hue: translucent volume fills (idle/active) * and wireframe line tints (idle/active), all in the same hue family. * @param {number} hueDeg - Hue in degrees. - * @returns {{fill: Cesium.Color, fillActive: Cesium.Color, line: Cesium.Color, lineActive: Cesium.Color}} + * @returns {{fill: number[], fillActive: number[], line: number[], lineActive: number[]}} + * Renderer colours, [r, g, b, a] in 0..1. */ export function viewshedColors(hueDeg) { const hue = (((Number(hueDeg) % 360) + 360) % 360) / 360; return { - fill: Cesium.Color.fromHsl(hue, 0.85, 0.6, FILL_ALPHA_IDLE), - fillActive: Cesium.Color.fromHsl(hue, 0.85, 0.6, FILL_ALPHA_ACTIVE), - line: Cesium.Color.fromHsl(hue, 0.9, 0.65, LINE_ALPHA_IDLE), - lineActive: Cesium.Color.fromHsl(hue, 0.9, 0.7, LINE_ALPHA_ACTIVE), + fill: hslColor(hue, 0.85, 0.6, FILL_ALPHA_IDLE), + fillActive: hslColor(hue, 0.85, 0.6, FILL_ALPHA_ACTIVE), + line: hslColor(hue, 0.9, 0.65, LINE_ALPHA_IDLE), + lineActive: hslColor(hue, 0.9, 0.7, LINE_ALPHA_ACTIVE), }; } @@ -61,8 +62,8 @@ export function viewshedColors(hueDeg) { * the apex + the far cap split into 2 triangles. Pure — this is the ONLY * geometry definition (weld-by-construction with the wireframe, which draws * its rays/cap from the same 5 Cartesians). - * @param {{mount: Cesium.Cartesian3, tl: Cesium.Cartesian3, tr: Cesium.Cartesian3, - * br: Cesium.Cartesian3, bl: Cesium.Cartesian3}} positions - frustumCartesians shape. + * @param {{mount: object, tl: object, tr: object, br: object, bl: object}} + * positions - frustumCartesians shape: ECEF points with x, y and z. * @returns {{positions: Float64Array, indices: Uint16Array}} */ export function frustumVolumeGeometryData(positions) { @@ -85,46 +86,3 @@ export function frustumVolumeGeometryData(positions) { ]); return { positions: flat, indices }; } - -/** - * Builds the translucent frustum-volume primitive for one camera. - * Synchronous (6 triangles — cheaper than the async geometry pipeline's - * bookkeeping), unlit flat color, both faces visible (the viewer is routinely - * inside or behind a cone), never pickable (clicks fall through to the - * billboard/wireframe/plane, whose pick semantics are established). - * @param {Object} positions - frustumCartesians shape (see frustumVolumeGeometryData). - * @param {Cesium.Color} color - Per-camera fill color (already alpha'd). - * @returns {Cesium.Primitive} - */ -export function createFrustumVolumePrimitive(positions, color) { - const { positions: flat, indices } = frustumVolumeGeometryData(positions); - const geometry = new Cesium.Geometry({ - attributes: { - position: new Cesium.GeometryAttribute({ - componentDatatype: Cesium.ComponentDatatype.DOUBLE, - componentsPerAttribute: 3, - values: flat, - }), - }, - indices, - primitiveType: Cesium.PrimitiveType.TRIANGLES, - boundingSphere: Cesium.BoundingSphere.fromVertices(Array.from(flat)), - }); - return new Cesium.Primitive({ - geometryInstances: new Cesium.GeometryInstance({ - geometry, - attributes: { - color: Cesium.ColorGeometryInstanceAttribute.fromColor(color), - }, - }), - appearance: new Cesium.PerInstanceColorAppearance({ - flat: true, - translucent: true, - renderState: { - cull: { enabled: false }, - }, - }), - asynchronous: false, - allowPicking: false, - }); -} diff --git a/src/data/cctvViewshed.test.mjs b/src/data/cctvViewshed.test.mjs index c2359ceea..2a43044ae 100644 --- a/src/data/cctvViewshed.test.mjs +++ b/src/data/cctvViewshed.test.mjs @@ -62,14 +62,26 @@ test('cameraHue: consecutive indices separated by >= 80 degrees', () => { test('viewshedColors: fill/line pairs with the designed alphas', () => { const colors = viewshedColors(210); for (const key of ['fill', 'fillActive', 'line', 'lineActive']) { - assert.ok(colors[key] instanceof Cesium.Color, `${key} must be a Cesium.Color`); + assert.ok(Array.isArray(colors[key]) && colors[key].length === 4, `${key} must be a renderer colour`); } - assert.ok(Math.abs(colors.fill.alpha - 0.12) < 1e-6); - assert.ok(Math.abs(colors.fillActive.alpha - 0.22) < 1e-6); - assert.ok(Math.abs(colors.line.alpha - 0.85) < 1e-6); - assert.ok(Math.abs(colors.lineActive.alpha - 1.0) < 1e-6); + assert.ok(Math.abs(colors.fill[3] - 0.12) < 1e-6); + assert.ok(Math.abs(colors.fillActive[3] - 0.22) < 1e-6); + assert.ok(Math.abs(colors.line[3] - 0.85) < 1e-6); + assert.ok(Math.abs(colors.lineActive[3] - 1.0) < 1e-6); // Same hue family: fill and line of the same hue must not be gray. - assert.ok(colors.line.red !== colors.line.green || colors.line.green !== colors.line.blue); + assert.ok(colors.line[0] !== colors.line[1] || colors.line[1] !== colors.line[2]); +}); + +test('viewshedColors: the same colours the renderer derived from the hue', () => { + const asArray = (c) => [c.red, c.green, c.blue, c.alpha]; + for (const hueDeg of [0, 137.5, 210, 359.9, -30]) { + const hue = (((hueDeg % 360) + 360) % 360) / 360; + const colors = viewshedColors(hueDeg); + assert.deepEqual(colors.fill, asArray(Cesium.Color.fromHsl(hue, 0.85, 0.6, 0.12))); + assert.deepEqual(colors.fillActive, asArray(Cesium.Color.fromHsl(hue, 0.85, 0.6, 0.22))); + assert.deepEqual(colors.line, asArray(Cesium.Color.fromHsl(hue, 0.9, 0.65, 0.85))); + assert.deepEqual(colors.lineActive, asArray(Cesium.Color.fromHsl(hue, 0.9, 0.7, 1.0))); + } }); /** Minimal frustumCartesians-shaped fixture. */ diff --git a/src/layers/cctv/rendering.js b/src/layers/cctv/rendering.js index da48b77d3..e9581ba5b 100644 --- a/src/layers/cctv/rendering.js +++ b/src/layers/cctv/rendering.js @@ -21,7 +21,7 @@ import { writeHoldSegment } from '../../data/iconSegment.js'; import { IconSetPrimitive } from '../../data/iconSetPrimitive.js'; import { iconAtlas } from '../../data/iconAtlas.js'; import { cctvPicks } from './pickState.js'; -import { createFrustumVolumePrimitive } from '../../data/cctvViewshed.js'; +import { frustumVolumeGeometryData } from '../../data/cctvViewshed.js'; import { entityRecord } from '../../data/entityRecords.js'; import { toColor } from '../../render/cesium/draw.js'; // Loads the factory a viewer of a caller's own gets its camera port from, @@ -58,6 +58,70 @@ const CAMERA_SCALE_BY_DISTANCE = new Cesium.NearFarScalar( 0.42, ); +/** + * Builds the translucent frustum-volume primitive for one camera. + * Synchronous (6 triangles — cheaper than the async geometry pipeline's + * bookkeeping), unlit flat color, both faces visible (the viewer is routinely + * inside or behind a cone), never pickable (clicks fall through to the + * billboard/wireframe/plane, whose pick semantics are established). + * @param {Object} positions - frustumCartesians shape (see frustumVolumeGeometryData). + * @param {Cesium.Color} color - Per-camera fill color (already alpha'd). + * @returns {Cesium.Primitive} + */ +function createFrustumVolumePrimitive(positions, color) { + const { positions: flat, indices } = frustumVolumeGeometryData(positions); + const geometry = new Cesium.Geometry({ + attributes: { + position: new Cesium.GeometryAttribute({ + componentDatatype: Cesium.ComponentDatatype.DOUBLE, + componentsPerAttribute: 3, + values: flat, + }), + }, + indices, + primitiveType: Cesium.PrimitiveType.TRIANGLES, + boundingSphere: Cesium.BoundingSphere.fromVertices(Array.from(flat)), + }); + return new Cesium.Primitive({ + geometryInstances: new Cesium.GeometryInstance({ + geometry, + attributes: { + color: Cesium.ColorGeometryInstanceAttribute.fromColor(color), + }, + }), + appearance: new Cesium.PerInstanceColorAppearance({ + flat: true, + translucent: true, + renderState: { + cull: { enabled: false }, + }, + }), + asynchronous: false, + allowPicking: false, + }); +} + +/** + * A record's viewshed colours as Cesium colours, made once per palette + * object: a record gets a new palette rather than having one changed in place. + */ +const viewshedCesium = new WeakMap(); +function viewshedCesiumColors(record) { + const colors = record.viewshedColors; + if (!colors) return null; + let cesium = viewshedCesium.get(colors); + if (!cesium) { + cesium = { + fill: toColor(colors.fill), + fillActive: toColor(colors.fillActive), + line: toColor(colors.line), + lineActive: toColor(colors.lineActive), + }; + viewshedCesium.set(colors, cesium); + } + return cesium; +} + /** FNV-1a over a camera id: a stable, well-spread order for crowded cells. */ function idHash(id) { let hash = 0x811c9dc5; @@ -544,7 +608,7 @@ export function createRendering({ // Viewshed mode swaps the cyan/green scheme for the camera's own hue so // adjacent cones read as distinct coverage claims (design §3b); the // active camera keeps its width/alpha emphasis in both schemes. - const hue = viewshedOn ? record.viewshedColors : null; + const hue = viewshedOn ? viewshedCesiumColors(record) : null; if (entity._coverageRole === 'cap') { entity.polyline.material = lineMaterial( hue @@ -787,9 +851,8 @@ export function createRendering({ !record.viewshedColors ) return; - const color = isActive - ? record.viewshedColors.fillActive - : record.viewshedColors.fill; + const colors = viewshedCesiumColors(record); + const color = isActive ? colors.fillActive : colors.fill; const primitive = createFrustumVolumePrimitive( record.frustumPositions, color, diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 779a9bbe1..2ec2f172e 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -22,7 +22,6 @@ "src/data/bhoteKoshiLocator.js", "src/data/cameraFraming.js", "src/data/cctvGizmo.js", - "src/data/cctvViewshed.js", "src/data/contactTrailRenderer.js", "src/data/detection.js", "src/data/focusDeemphasis.js", diff --git a/src/render/color.js b/src/render/color.js index 529dfe5b5..c31d313ff 100644 --- a/src/render/color.js +++ b/src/render/color.js @@ -1,6 +1,6 @@ /** * Renderer colours (`[r, g, b, a]`, 0..1) from the CSS-style colours layers - * keep in their palettes. + * keep in their palettes, and from hue, saturation and lightness. */ /** A `#rrggbb` colour at `alpha`, as a renderer colour. */ @@ -67,10 +67,20 @@ export function cssColor(text) { ]; m = HSL.exec(css); if (!m) return null; - const hue = (Number.parseFloat(m[1]) / 360) % 1; - const saturation = Number.parseFloat(m[2]) / 100; - const lightness = Number.parseFloat(m[3]) / 100; - const alpha = Number.parseFloat(m[4] ?? '1.0'); + return hslColor( + Number.parseFloat(m[1]) / 360, + Number.parseFloat(m[2]) / 100, + Number.parseFloat(m[3]) / 100, + Number.parseFloat(m[4] ?? '1.0'), + ); +} + +/** + * A hue, saturation and lightness colour (each 0..1; the hue wraps) as a + * renderer colour, computed as the default renderer computes it. + */ +export function hslColor(hue, saturation, lightness, alpha = 1) { + hue %= 1; if (saturation === 0) return [lightness, lightness, lightness, alpha]; const m2 = lightness < 0.5 diff --git a/src/render/color.test.mjs b/src/render/color.test.mjs index dd5ba4ef2..b8cf3c68e 100644 --- a/src/render/color.test.mjs +++ b/src/render/color.test.mjs @@ -29,3 +29,18 @@ test('CSS colours read as the default renderer reads them', async () => { assert.equal(cssColor('not a colour'), null); assert.equal(cssColor(undefined), null); }); + +test('HSL colours read as the default renderer computes them', async () => { + const Cesium = await import('cesium'); + const { hslColor } = await import('./color.js'); + for (const [h, s, l, a] of [ + [0, 0.85, 0.6, 0.12], + [0.3819660112501051, 0.9, 0.65, 0.85], + [0.7639320225002102, 0.9, 0.7, 1], + [1.25, 0.5, 0.25, 0.4], + [0.5, 0, 0.4, 1], + ]) { + const c = Cesium.Color.fromHsl(h, s, l, a); + assert.deepEqual(hslColor(h, s, l, a), [c.red, c.green, c.blue, c.alpha]); + } +});