mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Draw submarine cables through the renderer's draw and camera ports
Cable lines, landing pins, polygon fills, reference stems and reference points are now five draw sets per layer (submarine-cables:N:lines and so on) instead of GeoJSON data sources and entities. Clicks resolve through the draw hit; flights and the sweep's eye go through the camera port. The layer keeps the styling the GeoJSON loader gave it: cable colours, widths and alphas, simplestyle stroke, fill and marker properties, fills that keep the source's alpha, raised polygons at their heights, the default pin image, and features without a reference drawn but neither picked nor culled. Lines are densified along rhumb lines at the renderer's own granularity, so the draped geodesic arcs land on the same vertices the rhumb-line polylines did. Supporting pieces: - geo/rhumb, geo/horizon and geo/geodesic port the default renderer's rhumb line, ellipsoidal occluder and Vincenty inverse; the stub renderer now uses the shared horizon test and Vincenty inverse instead of its own copies. render/color gains cssColor, which reads CSS colours as the renderer does (hex, rgb, hsl; not named colours). - The Cesium draw port changes a line in place when only whether it shows changes (draped, on its ground primitive, waiting for it to be ready rather than restarting its build) or where an undraped line runs; anything else still rebuilds. Icon sets honour their blend. Ingestion, interaction, overlay, policy, rendering, state and surface no longer import Cesium and leave the allow-list. Pins are culled at the horizon each frame by the draw port instead of on the sweep, and marker-symbol glyphs are not drawn inside pins.
This commit is contained in:
1 parent
f24b66d80d
commit
e74c7c1f44
29 files changed
+2260
-2064
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import {
|
||||
probeTranslucentMarkerBlend,
|
||||
commitTranslucentMarkerBlend,
|
||||
} from './surface.js';
|
||||
import { fromDegrees, fromRadians } from '../../geo/ellipsoid.js';
|
||||
import { horizonFrom } from '../../geo/horizon.js';
|
||||
import { densifyRhumb } from '../../geo/rhumb.js';
|
||||
import { distance, toRadians } from '../../geo/vector.js';
|
||||
import { cssColor } from '../../render/color.js';
|
||||
import { cameraOf, drawOf } from '../../render/rendererOf.js';
|
||||
import {
|
||||
clampLabel,
|
||||
createCableOverlayEntry,
|
||||
@@ -10,9 +11,149 @@ import {
|
||||
selectCableReferenceLabelWinners,
|
||||
cableReferencePriority,
|
||||
} from './overlay.js';
|
||||
import { cablePicks } from './pickState.js';
|
||||
import { featureLabel, featureReference, geometryParts } from './geometry.js';
|
||||
import { cablePinImage } from './pin.js';
|
||||
import {
|
||||
CABLE_LINE_GRANULARITY_M,
|
||||
CABLE_PIN_SIZE_PX,
|
||||
CABLE_PIN_SIZES,
|
||||
} from './policy.js';
|
||||
|
||||
/**
|
||||
* The layer's draw sets start with this; each layer has its own
|
||||
* (`submarine-cables:1:lines`, …), so several can share a renderer. Picks
|
||||
* name them as `Hit.set`.
|
||||
*/
|
||||
export const CABLE_SET_PREFIX = 'submarine-cables';
|
||||
let layers = 0;
|
||||
|
||||
const BLACK_OUTLINE = [0, 0, 0, 0.65];
|
||||
|
||||
/** A polygon's edges are placed along rhumb lines a degree apart at most. */
|
||||
const POLYGON_GRANULARITY_M = (6378137 * Math.PI) / 180;
|
||||
|
||||
/** A renderer colour with its alpha replaced. */
|
||||
const withAlpha = (color, alpha) => [color[0], color[1], color[2], alpha];
|
||||
|
||||
/** A feature's CSS colour property, or null when absent or unreadable. */
|
||||
const propColor = (value) =>
|
||||
value === undefined || value === null ? null : cssColor(String(value));
|
||||
|
||||
/** Whether a GeoJSON property is set, as the GeoJSON loader asked. */
|
||||
const given = (value) => value !== undefined && value !== null;
|
||||
|
||||
/**
|
||||
* How a line draws. A cable with a reference takes the cable's own colour
|
||||
* (or the base colour) and one width, whatever the feature asks; every other
|
||||
* line keeps the GeoJSON styling it always had: the source's colour,
|
||||
* overridable by the feature's simplestyle `stroke`, `stroke-opacity` and
|
||||
* `stroke-width`, draped on every surface.
|
||||
*/
|
||||
function lineStyle(props, cable, styled, baseColor) {
|
||||
if (cable && styled) {
|
||||
const color = propColor(props.color) ?? baseColor;
|
||||
return {
|
||||
widthPx: 2.5,
|
||||
style: { kind: 'solid', color: withAlpha(color, 0.92) },
|
||||
};
|
||||
}
|
||||
return {
|
||||
widthPx: props['stroke-width'] ?? 2,
|
||||
clampTo: 'both',
|
||||
style: { kind: 'solid', color: strokeColor(props, cable, baseColor) },
|
||||
};
|
||||
}
|
||||
|
||||
/** A line's or raised polygon's simplestyle stroke colour. */
|
||||
function strokeColor(props, cable, baseColor) {
|
||||
let color = given(props.stroke) ? propColor(props.stroke) : null;
|
||||
const opacity = props['stroke-opacity'];
|
||||
if (given(opacity) && opacity !== 1)
|
||||
color = withAlpha(
|
||||
color ?? withAlpha(baseColor, cable ? 0.95 : 0.9),
|
||||
opacity,
|
||||
);
|
||||
return color ?? withAlpha(baseColor, cable ? 0.95 : 0.9);
|
||||
}
|
||||
|
||||
/**
|
||||
* A polygon's fill, as GeoJSON polygons were filled: simplestyle `fill`
|
||||
* takes the source's fill alpha, and `fill-opacity` replaces it.
|
||||
*/
|
||||
function polygonFill(props, cable, baseColor) {
|
||||
const alpha = cable ? 0.18 : 0.35;
|
||||
let color = given(props.fill) ? propColor(props.fill) : null;
|
||||
if (color) color = withAlpha(color, alpha);
|
||||
const opacity = props['fill-opacity'];
|
||||
if (given(opacity) && opacity !== alpha)
|
||||
color = withAlpha(color ?? withAlpha(baseColor, alpha), opacity);
|
||||
return color ?? withAlpha(baseColor, alpha);
|
||||
}
|
||||
|
||||
/** A finite `[lon, lat, height?]`, or null. */
|
||||
function lonLat(coordinate) {
|
||||
if (!Array.isArray(coordinate) || coordinate.length < 2) return null;
|
||||
const lon = Number(coordinate[0]);
|
||||
const lat = Number(coordinate[1]);
|
||||
if (!Number.isFinite(lon) || !Number.isFinite(lat)) return null;
|
||||
return { lon: toRadians(lon), lat: toRadians(lat) };
|
||||
}
|
||||
|
||||
/** A path's points along rhumb lines, as ECEF, the way GeoJSON drew them. */
|
||||
function rhumbPath(coordinates, granularity = CABLE_LINE_GRANULARITY_M) {
|
||||
const points = [];
|
||||
for (const coordinate of coordinates ?? []) {
|
||||
const point = lonLat(coordinate);
|
||||
if (point) points.push(point);
|
||||
}
|
||||
return densifyRhumb(points, granularity).map((p) =>
|
||||
fromRadians(p.lon, p.lat),
|
||||
);
|
||||
}
|
||||
|
||||
export function createRendering({ state }) {
|
||||
/** Make the layer's draw sets on the viewer's renderer (once). */
|
||||
function attach(viewer) {
|
||||
if (state._sets || !viewer) return;
|
||||
const draw = drawOf(viewer);
|
||||
const prefix = `${CABLE_SET_PREFIX}:${++layers}`;
|
||||
const show = state._enabled;
|
||||
state._sets = {
|
||||
lines: draw.lines({
|
||||
id: `${prefix}:lines`,
|
||||
clampTo: state._clampTo,
|
||||
show,
|
||||
}),
|
||||
fills: draw.shapes({ id: `${prefix}:fills`, clampTo: 'both', show }),
|
||||
// Pins cull past the horizon each frame, as the sweep culled them;
|
||||
// the globe hides them there anyway.
|
||||
pins: draw.icons({
|
||||
id: `${prefix}:pins`,
|
||||
clampTo: 'both',
|
||||
blend: 'translucent',
|
||||
horizonCull: true,
|
||||
show,
|
||||
}),
|
||||
stems: draw.lines({ id: `${prefix}:stems`, show }),
|
||||
references: draw.points({
|
||||
id: `${prefix}:references`,
|
||||
blend: 'translucent',
|
||||
show,
|
||||
}),
|
||||
};
|
||||
state._setIds = new Set(Object.values(state._sets).map((set) => set.id));
|
||||
state._picks = new Map([...state._setIds].map((id) => [id, new Map()]));
|
||||
}
|
||||
|
||||
/** Dispose the draw sets (destroy); `attach` makes new ones. */
|
||||
function detach() {
|
||||
if (!state._sets) return;
|
||||
for (const set of Object.values(state._sets)) set.dispose();
|
||||
state._sets = null;
|
||||
state._setIds = new Set();
|
||||
state._picks = new Map();
|
||||
}
|
||||
|
||||
function resetPublishSignature() {
|
||||
state._lastPublishedIds.length = 0;
|
||||
state._lastPublishedPriorities.length = 0;
|
||||
@@ -20,215 +161,309 @@ export function createRendering({ state }) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the three data sources from the viewer and forget the built
|
||||
* entities. Hiding them (`show = false`) is NOT enough: Cesium's
|
||||
* DataSourceDisplay still walks every visualizer of a hidden source on
|
||||
* every frame, and the thousands of hidden polylines/billboards keep
|
||||
* costing CPU and GC pressure long after the user toggled the layer off —
|
||||
* that is the "everything gets sluggish after I turn layers off" report.
|
||||
* The fetched JSON stays cached, so the next enable rebuilds without a
|
||||
* network round trip or a re-parse.
|
||||
* Clear the draw sets and forget the built items. Hiding them is NOT
|
||||
* enough: thousands of hidden lines and markers keep costing memory long
|
||||
* after the user toggled the layer off — that is the "everything gets
|
||||
* sluggish after I turn layers off" report. The fetched JSON stays
|
||||
* cached, so the next enable rebuilds without a network round trip or a
|
||||
* re-parse.
|
||||
*/
|
||||
|
||||
function releaseDataSources(viewer) {
|
||||
const sources = [
|
||||
state._cableDataSource,
|
||||
state._landingDataSource,
|
||||
state._referenceDataSource,
|
||||
];
|
||||
state._cableDataSource = null;
|
||||
state._landingDataSource = null;
|
||||
state._referenceDataSource = null;
|
||||
/**
|
||||
* Forget what was drawn; `clearSets: false` (destroy, where `detach`
|
||||
* disposes the sets next) leaves the sets alone.
|
||||
*/
|
||||
function releaseDrawing({ clearSets = true } = {}) {
|
||||
state._referenceRecords = [];
|
||||
state._surfaceRecords = [];
|
||||
state._surfaceItems = [];
|
||||
state._referenceLabelCount = 0;
|
||||
state._publishScratch.length = 0;
|
||||
// The blend pass targets the collections being dropped; the rebuilt
|
||||
// sources need their own pass.
|
||||
state._markerBlendDone = false;
|
||||
state._loaded = false;
|
||||
state._referenceSweepGate.reset();
|
||||
for (const source of sources) {
|
||||
if (!source) continue;
|
||||
try {
|
||||
viewer?.dataSources?.remove(source, true);
|
||||
} catch {
|
||||
/* collection gone */
|
||||
}
|
||||
}
|
||||
if (!state._sets) return;
|
||||
if (clearSets) for (const set of Object.values(state._sets)) set.clear();
|
||||
for (const picks of state._picks.values()) picks.clear();
|
||||
}
|
||||
|
||||
function updateVisibility() {
|
||||
if (state._cableDataSource) state._cableDataSource.show = state._enabled;
|
||||
if (state._landingDataSource)
|
||||
state._landingDataSource.show = state._enabled;
|
||||
if (state._referenceDataSource)
|
||||
state._referenceDataSource.show = state._enabled;
|
||||
}
|
||||
|
||||
function styleCableEntity(entity, feature) {
|
||||
if (!entity?.polyline) return;
|
||||
const color = feature?.properties?.color
|
||||
? Cesium.Color.fromCssColorString(String(feature.properties.color))
|
||||
: state.cableColor;
|
||||
entity.polyline.material = color.withAlpha(0.92);
|
||||
entity.polyline.width = 2.5;
|
||||
entity.polyline.clampToGround = true;
|
||||
entity.polyline.classificationType = state._classificationType;
|
||||
entity.show = true;
|
||||
if (!state._sets) return;
|
||||
for (const set of Object.values(state._sets)) set.show = state._enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the single translucent blend pass to the landing-billboard and
|
||||
* reference-point collections. Landing entities carry only billboards and
|
||||
* reference entities only points, so completion is one applied collection
|
||||
* per source; a failed shape probe disables the optimization permanently
|
||||
* for this session and says so loudly in dev builds.
|
||||
* Both sources are probed BEFORE either is committed, so the helper's
|
||||
* "defaults untouched on shape drift" fallback holds across the pair too —
|
||||
* a reference-source failure can never leave the landing source already
|
||||
* forced to TRANSLUCENT.
|
||||
*/
|
||||
|
||||
function applyMarkerBlendOnce() {
|
||||
const landing = probeTranslucentMarkerBlend(state._landingDataSource);
|
||||
const reference = probeTranslucentMarkerBlend(state._referenceDataSource);
|
||||
if (landing.invariantFailed || reference.invariantFailed) {
|
||||
state._markerBlendDone = true;
|
||||
if (!state._markerBlendInvariantWarned && import.meta.env?.DEV === true) {
|
||||
state._markerBlendInvariantWarned = true;
|
||||
console.error(
|
||||
'[Data:telegeography-submarine-cables] EntityCluster marker-collection ' +
|
||||
'shape changed — single-pass translucent blend skipped; markers fall ' +
|
||||
"back to Cesium's default two-pass blend.",
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const landingBlend = commitTranslucentMarkerBlend(landing);
|
||||
const referenceBlend = commitTranslucentMarkerBlend(reference);
|
||||
if (landingBlend.applied > 0 && referenceBlend.applied > 0)
|
||||
state._markerBlendDone = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-classify every cable ground line for the active surface. One batched
|
||||
* ground-primitive rebuild per stack switch — never per frame.
|
||||
* @param {Cesium.ClassificationType} next
|
||||
* Re-drape every cable ground line for the active surface. One ground
|
||||
* rebuild per stack switch — never per frame.
|
||||
* @param {'tiles'|'terrain'|'both'} next
|
||||
*/
|
||||
|
||||
function applyCableClassification(next) {
|
||||
if (next === undefined || next === state._classificationType) return;
|
||||
state._classificationType = next;
|
||||
if (!state._cableDataSource) return;
|
||||
const entities = state._cableDataSource.entities.values;
|
||||
for (let i = 0; i < entities.length; i++) {
|
||||
const polyline = entities[i].polyline;
|
||||
if (polyline) polyline.classificationType = next;
|
||||
}
|
||||
state._viewer?.scene?.requestRender?.();
|
||||
if (next === undefined || next === state._clampTo) return;
|
||||
state._clampTo = next;
|
||||
state._sets?.lines.setOptions({ clampTo: next });
|
||||
}
|
||||
|
||||
function styleLandingEntity(entity, feature) {
|
||||
if (!entity?.point) return;
|
||||
entity.point.color = state.landingColor.withAlpha(0.92);
|
||||
entity.point.pixelSize = feature?.properties?.is_tbd ? 6 : 7;
|
||||
entity.point.outlineColor = state.cableOutline;
|
||||
entity.point.outlineWidth = 1;
|
||||
entity.point.disableDepthTestDistance = 0;
|
||||
entity.show = true;
|
||||
}
|
||||
|
||||
function addReferenceStem({ reference, label, kind, color, feature }) {
|
||||
if (!state._referenceDataSource || !reference) return;
|
||||
|
||||
const base = Cesium.Cartesian3.fromDegrees(reference.lon, reference.lat, 0);
|
||||
const tip = Cesium.Cartesian3.fromDegrees(
|
||||
reference.lon,
|
||||
reference.lat,
|
||||
2500,
|
||||
);
|
||||
const info = {
|
||||
kind,
|
||||
reference,
|
||||
label,
|
||||
featureId: feature?.id || feature?.properties?.id || null,
|
||||
};
|
||||
const pointColor = color.withAlpha(kind === 'cable' ? 0.84 : 0.94);
|
||||
const stemColor = color.withAlpha(kind === 'cable' ? 0.58 : 0.68);
|
||||
// Constant properties on the sweep cadence, never per-frame callbacks:
|
||||
// the sweep redefines them through the alternating buffers below.
|
||||
const stemPositionBuffers = [
|
||||
[base, tip],
|
||||
[base, tip],
|
||||
];
|
||||
|
||||
const entity = state._referenceDataSource.entities.add({
|
||||
id: `${kind}-reference-${state._referenceRecords.length}-${info.featureId || 'feature'}`,
|
||||
position: tip,
|
||||
polyline: {
|
||||
positions: stemPositionBuffers[0],
|
||||
width: kind === 'cable' ? 2 : 2.4,
|
||||
material: new Cesium.ColorMaterialProperty(stemColor),
|
||||
},
|
||||
point: {
|
||||
pixelSize: kind === 'cable' ? 7 : 8,
|
||||
color: pointColor,
|
||||
outlineColor: Cesium.Color.BLACK.withAlpha(0.65),
|
||||
outlineWidth: 1,
|
||||
disableDepthTestDistance: 0,
|
||||
},
|
||||
/**
|
||||
* The items one source's features draw, by set: lines, fills and pins,
|
||||
* each culled by its feature's reference, and a reference stem and point
|
||||
* per feature. `kind` is `cable` or `landing-point`.
|
||||
*/
|
||||
function buildFeatures(features, kind, out) {
|
||||
const cable = kind === 'cable';
|
||||
const baseColor = cssColor(cable ? state.cableColor : state.landingColor);
|
||||
features.forEach((feature, index) => {
|
||||
// A feature with no reference still draws, as the GeoJSON loader drew
|
||||
// it: unstyled, not picked and never culled.
|
||||
const reference = featureReference(feature);
|
||||
const label = featureLabel(feature);
|
||||
const info = reference && {
|
||||
kind,
|
||||
reference,
|
||||
label,
|
||||
featureId: feature?.id || feature?.properties?.id || null,
|
||||
};
|
||||
const base = reference && fromDegrees(reference.lon, reference.lat, 0);
|
||||
const keep = (set, item) => {
|
||||
if (!reference) return;
|
||||
out.picks[set].set(item.id, info);
|
||||
if (set !== 'pins') out.surface.push({ set, item, base });
|
||||
};
|
||||
const props = feature?.properties || {};
|
||||
const parts = geometryParts(feature.geometry);
|
||||
const key = `${kind}:${index}`;
|
||||
parts.lines.forEach((coordinates, part) => {
|
||||
const positions = rhumbPath(coordinates);
|
||||
if (positions.length < 2) return;
|
||||
const item = {
|
||||
id: `${key}:line:${part}`,
|
||||
positions,
|
||||
mode: 'draped',
|
||||
...lineStyle(props, cable, Boolean(reference), baseColor),
|
||||
show: true,
|
||||
};
|
||||
out.lines.push(item);
|
||||
keep('lines', item);
|
||||
});
|
||||
parts.polygons.forEach((rings, part) => {
|
||||
const fill = polygonFill(props, cable, baseColor);
|
||||
// Polygons with heights stand at them, outlined, as the GeoJSON
|
||||
// loader drew them; flat ones lie on the surface, edges along
|
||||
// rhumb lines.
|
||||
const raised = rings[0]?.[0]?.length > 2;
|
||||
const drawn = rings
|
||||
.map((ring) =>
|
||||
raised
|
||||
? ring
|
||||
.filter((coordinate) => lonLat(coordinate))
|
||||
.map(([lon, lat, height]) =>
|
||||
fromDegrees(Number(lon), Number(lat), Number(height) || 0),
|
||||
)
|
||||
: rhumbPath(ring, POLYGON_GRANULARITY_M).map((p) => ({
|
||||
x: p.x,
|
||||
y: p.y,
|
||||
z: p.z,
|
||||
})),
|
||||
)
|
||||
.filter((ring) => ring.length >= 3);
|
||||
if (!drawn.length) return;
|
||||
const item = {
|
||||
id: `${key}:fill:${part}`,
|
||||
geometry: raised
|
||||
? { kind: 'polygon', rings: drawn, perPositionHeight: true }
|
||||
: { kind: 'polygon', rings: drawn },
|
||||
fill,
|
||||
...(raised
|
||||
? {
|
||||
height: 'absolute',
|
||||
// WebGL draws the loader's outline one pixel wide.
|
||||
outline: strokeColor(props, cable, baseColor),
|
||||
outlineWidth: 1,
|
||||
}
|
||||
: { height: 'clampGround' }),
|
||||
show: true,
|
||||
};
|
||||
out.fills.push(item);
|
||||
keep('fills', item);
|
||||
});
|
||||
parts.points.forEach((coordinate, part) => {
|
||||
const point = lonLat(coordinate);
|
||||
if (!point) return;
|
||||
const color =
|
||||
(given(props['marker-color']) && cssColor(props['marker-color'])) ||
|
||||
baseColor;
|
||||
const size = CABLE_PIN_SIZES[props['marker-size']] ?? CABLE_PIN_SIZE_PX;
|
||||
const raised =
|
||||
coordinate.length > 2 && Number.isFinite(Number(coordinate[2]));
|
||||
const item = {
|
||||
id: `${key}:pin:${part}`,
|
||||
pos: {
|
||||
lon: Number(coordinate[0]),
|
||||
lat: Number(coordinate[1]),
|
||||
height: raised ? Number(coordinate[2]) : 0,
|
||||
},
|
||||
height: raised ? 'absolute' : 'clampGround',
|
||||
image: cablePinImage(color, size),
|
||||
size: [size, size],
|
||||
anchor: 'bottom',
|
||||
};
|
||||
out.pins.push(item);
|
||||
keep('pins', item);
|
||||
});
|
||||
if (!reference) return;
|
||||
addReference(out, {
|
||||
reference,
|
||||
label,
|
||||
kind,
|
||||
color: baseColor,
|
||||
info,
|
||||
base,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
cablePicks.write(entity).info = info;
|
||||
function addReference(out, { reference, label, kind, color, info, base }) {
|
||||
const cable = kind === 'cable';
|
||||
const tip = fromDegrees(reference.lon, reference.lat, 2500);
|
||||
const id = `${kind}-reference-${out.references.length}-${info.featureId || 'feature'}`;
|
||||
const record = {
|
||||
id: entity.id,
|
||||
entity,
|
||||
id,
|
||||
base,
|
||||
// Moved in place: the overlay entry anchors to this object.
|
||||
tip,
|
||||
nextTip: Cesium.Cartesian3.clone(tip),
|
||||
stemPositionBuffers,
|
||||
stemPositionBufferIndex: 0,
|
||||
reference,
|
||||
kind,
|
||||
label: clampLabel(label),
|
||||
visible: false,
|
||||
visible: true,
|
||||
distanceM: Infinity,
|
||||
entry: null,
|
||||
stem: {
|
||||
id,
|
||||
positions: [base, { ...tip }],
|
||||
widthPx: cable ? 2 : 2.4,
|
||||
mode: '3d',
|
||||
style: {
|
||||
kind: 'solid',
|
||||
color: withAlpha(color, cable ? 0.58 : 0.68),
|
||||
},
|
||||
show: true,
|
||||
},
|
||||
point: {
|
||||
id,
|
||||
pos: { ...tip },
|
||||
pixelSize: cable ? 7 : 8,
|
||||
color: withAlpha(color, cable ? 0.84 : 0.94),
|
||||
outlineColor: BLACK_OUTLINE,
|
||||
outlineWidth: 1,
|
||||
show: true,
|
||||
},
|
||||
};
|
||||
record.entry = createCableOverlayEntry(record);
|
||||
state._referenceRecords.push(record);
|
||||
out.references.push(record);
|
||||
out.picks.stems.set(id, info);
|
||||
out.picks.references.set(id, info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw both sources' features, replacing what was drawn. Items start
|
||||
* culled for the camera as it is, so the first sweep has nothing to redo.
|
||||
*/
|
||||
function drawFeatures(cableFeatures, landingFeatures) {
|
||||
const out = {
|
||||
lines: [],
|
||||
fills: [],
|
||||
pins: [],
|
||||
references: [],
|
||||
surface: [],
|
||||
picks: {
|
||||
lines: new Map(),
|
||||
fills: new Map(),
|
||||
pins: new Map(),
|
||||
stems: new Map(),
|
||||
references: new Map(),
|
||||
},
|
||||
};
|
||||
buildFeatures(cableFeatures, 'cable', out);
|
||||
buildFeatures(landingFeatures, 'landing-point', out);
|
||||
state._referenceRecords = out.references;
|
||||
state._surfaceItems = out.surface;
|
||||
sweep({ draw: false });
|
||||
if (!state._sets) return;
|
||||
const sets = state._sets;
|
||||
for (const [name, picks] of Object.entries(out.picks)) {
|
||||
const map = state._picks.get(sets[name].id);
|
||||
map.clear();
|
||||
for (const [id, info] of picks) map.set(id, info);
|
||||
}
|
||||
sets.lines.set(out.lines);
|
||||
sets.fills.set(out.fills);
|
||||
sets.pins.set(out.pins);
|
||||
sets.stems.set(out.references.map((record) => record.stem));
|
||||
sets.references.set(out.references.map((record) => record.point));
|
||||
}
|
||||
|
||||
/** What a pick on one of the layer's sets names, or null. */
|
||||
function pickInfo(hit) {
|
||||
if (!hit || !state._setIds.has(hit.set)) return null;
|
||||
return state._picks.get(hit.set)?.get(hit.id) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* One sweep: cull references and surface items by whether their
|
||||
* reference is past the horizon, re-size visible stems, and redraw only
|
||||
* what changed (`draw: false` changes the items without drawing them).
|
||||
* Returns false when there is no camera to sweep from.
|
||||
*/
|
||||
function sweep({ draw = true } = {}) {
|
||||
const viewer = state._viewer;
|
||||
if (!viewer) return false;
|
||||
const pose = cameraOf(viewer).pose();
|
||||
const cameraPos = pose?.position;
|
||||
if (!cameraPos) return false;
|
||||
const canvasHeight = viewer.scene?.canvas?.clientHeight || 1080;
|
||||
// The field of view along the canvas's longer side.
|
||||
const fov =
|
||||
pose.aspectRatio > 1
|
||||
? 2 * Math.atan(Math.tan(pose.fovy / 2) * pose.aspectRatio)
|
||||
: pose.fovy;
|
||||
const horizon = horizonFrom(cameraPos);
|
||||
const stems = [];
|
||||
const points = [];
|
||||
for (const record of state._referenceRecords) {
|
||||
const visible = horizon.isPointVisible(record.base);
|
||||
const flipped = record.visible !== visible;
|
||||
record.visible = visible;
|
||||
record.distanceM = distance(cameraPos, record.base);
|
||||
// Hidden stems keep their last geometry; this same sweep refreshes them
|
||||
// in the pass where they turn visible again.
|
||||
const moved =
|
||||
visible &&
|
||||
updateCableReferenceStem(record, cameraPos, canvasHeight, fov);
|
||||
if (!flipped && !moved) continue;
|
||||
record.stem.positions = [record.base, { ...record.tip }];
|
||||
record.stem.show = visible;
|
||||
record.point.pos = { ...record.tip };
|
||||
record.point.show = visible;
|
||||
stems.push(record.stem);
|
||||
points.push(record.point);
|
||||
}
|
||||
const surface = { lines: [], fills: [] };
|
||||
for (const entry of state._surfaceItems) {
|
||||
const visible = horizon.isPointVisible(entry.base);
|
||||
if (entry.item.show === visible) continue;
|
||||
entry.item.show = visible;
|
||||
surface[entry.set].push(entry.item);
|
||||
}
|
||||
if (draw && state._sets) {
|
||||
if (stems.length) state._sets.stems.upsert(stems);
|
||||
if (points.length) state._sets.references.upsert(points);
|
||||
if (surface.lines.length) state._sets.lines.upsert(surface.lines);
|
||||
if (surface.fills.length) state._sets.fills.upsert(surface.fills);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function updateReferenceVisibility() {
|
||||
if (!state._enabled || !state._viewer?.camera) return;
|
||||
const cameraPos = state._viewer.camera.positionWC;
|
||||
if (!cameraPos) return;
|
||||
|
||||
const canvasHeight = state._viewer.scene?.canvas?.clientHeight || 1080;
|
||||
const fov = state._viewer.camera.frustum?.fov || Math.PI / 3;
|
||||
const occluder = new Cesium.EllipsoidalOccluder(
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
cameraPos,
|
||||
);
|
||||
for (const record of state._referenceRecords) {
|
||||
const visible = occluder.isPointVisible(record.base);
|
||||
record.visible = visible;
|
||||
record.distanceM = Cesium.Cartesian3.distance(cameraPos, record.base);
|
||||
if (record.entity.show !== visible) {
|
||||
record.entity.show = visible;
|
||||
}
|
||||
// Hidden stems keep their last geometry; this same sweep refreshes them
|
||||
// in the pass where they turn visible again.
|
||||
if (visible)
|
||||
updateCableReferenceStem(record, cameraPos, canvasHeight, fov);
|
||||
}
|
||||
for (const record of state._surfaceRecords) {
|
||||
const visible = occluder.isPointVisible(record.base);
|
||||
if (record.entity.show !== visible) {
|
||||
record.entity.show = visible;
|
||||
}
|
||||
}
|
||||
if (!state._enabled || !state._viewer) return;
|
||||
if (!sweep()) return;
|
||||
const winners = selectCableReferenceLabelWinners(state._referenceRecords);
|
||||
let changed = winners.length !== state._lastPublishedCount;
|
||||
for (let i = 0; i < winners.length; i++) {
|
||||
@@ -246,9 +481,6 @@ export function createRendering({ state }) {
|
||||
}
|
||||
state._publishScratch.length = winners.length;
|
||||
state._referenceLabelCount = winners.length;
|
||||
// The marker collections are created lazily by the visualizers, so the
|
||||
// single-pass blend is applied on the sweep cadence until both landed.
|
||||
if (!state._markerBlendDone) applyMarkerBlendOnce();
|
||||
if (!changed) return;
|
||||
state._overlayPublisher.publish(state._publishScratch);
|
||||
state._lastPublishedCount = winners.length;
|
||||
@@ -260,14 +492,14 @@ export function createRendering({ state }) {
|
||||
state._lastPublishedPriorities.length = winners.length;
|
||||
}
|
||||
return {
|
||||
attach,
|
||||
detach,
|
||||
resetPublishSignature,
|
||||
releaseDataSources,
|
||||
releaseDrawing,
|
||||
updateVisibility,
|
||||
styleCableEntity,
|
||||
applyMarkerBlendOnce,
|
||||
applyCableClassification,
|
||||
styleLandingEntity,
|
||||
addReferenceStem,
|
||||
drawFeatures,
|
||||
pickInfo,
|
||||
updateReferenceVisibility,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user