Take the camera viewshed colours and volume builder off Cesium

The viewshed palette is renderer colours from a new hslColor in
render/color, which computes as the renderer's own fromHsl does (the CSS
HSL reading now goes through it). The volume primitive builder moves,
unchanged, into the camera layer's rendering part, which converts each
record's palette to Cesium colours once and draws the lines and volumes
with them as before. Tests check the palette against the renderer's
colours exactly.

cctvViewshed leaves the allow-list.
This commit is contained in:
Sameh Khamis committed 2026-11-06 23:47:17 -08:00
1 parent 0f496b9ae8
commit 2443028d0f
6 files changed
+130 -73

No files matched your search

+14 -56
View File
@@ -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,
});
}
+18 -6
View File
@@ -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. */