refactor(flights,military,localAdsb): take vector maths from src/geo

Dead reckoning, the grounded display floor, snapshot rendering, queries and layer scratch state now use src/geo. Outputs are bit-identical to the previous batch (frozen-clock probe over flights, military, local ADS-B and orbits); per-frame paths are as fast as base 132b9e96 or faster (see docs/migration/phase1a-report.md).
This commit is contained in:
German Khokhlov committed 2026-10-15 00:17:56 +04:00
1 parent 2b83606765
commit ee8b94d987
9 files changed
+151 -143

No files matched your search

+18 -17
View File
@@ -1,4 +1,5 @@
import * as Cesium from 'cesium';
import * as geo from '../../geo/index.js';
import {
lerpAngleDeg,
speedRamp,
@@ -63,7 +64,7 @@ export function createMotion({
return null;
}
const out = result || new Cesium.Cartesian3();
const out = result || geo.cartesian3.create();
// Render one poll interval behind real time so we interpolate between
// two KNOWN fixes whenever possible (see RENDER_DELAY_SEC rationale).
const renderTime = Cesium.JulianDate.addSeconds(
@@ -93,7 +94,7 @@ export function createMotion({
// a slow turner's nose advances continuously through the segment instead
// of snapping once per poll. Helicopters always use the reported track
// (rotorcraft chords are noise-dominated at their typical speeds).
const chordLenM = Cesium.Cartesian3.distance(a.position, b.position);
const chordLenM = geo.cartesian3.distance(a.position, b.position);
const segSpeed =
span > 0 ? chordLenM / span : (info && info.speedMps) || 0;
const fallbackTrack = (info && info.track) || 0;
@@ -109,7 +110,7 @@ export function createMotion({
: trackCourse;
flightState._drSpeedMps = segSpeed;
flightState._drCourseHold = segSpeed < COURSE_HOLD_SPEED_MPS;
return Cesium.Cartesian3.lerp(a.position, b.position, t, out);
return geo.cartesian3.lerp(a.position, b.position, t, out);
}
}
@@ -181,24 +182,24 @@ export function createMotion({
flightState._drCourseHold = speed < COURSE_HOLD_SPEED_MPS;
if (speed === 0 || dt === 0) {
flightState._drCourseDeg = heading;
return Cesium.Cartesian3.clone(fix.position, out);
return geo.cartesian3.clone(fix.position, out);
}
// Constant-rate-turn arc (straight line when turnRateDps ≈ 0) — a plane in a
// standard-rate turn is ~90° of arc wrong per 30 s if extrapolated straight.
arcOffsetEnu(speed, heading, turnRateDps, dt, flightState._scratchArc);
flightState._drCourseDeg = flightState._scratchArc.endCourseDeg;
Cesium.Cartesian3.fromElements(
geo.cartesian3.fromElements(
flightState._scratchArc.east,
flightState._scratchArc.north,
0,
flightState._scratchOffset,
);
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(
const enu = geo.transforms.eastNorthUpToFixedFrame(
fix.position,
Cesium.Ellipsoid.WGS84,
geo.ellipsoid.WGS84,
flightState._scratchEnu,
);
return Cesium.Matrix4.multiplyByPoint(enu, flightState._scratchOffset, out);
return geo.matrix4.multiplyByPoint(enu, flightState._scratchOffset, out);
}
/**
@@ -226,7 +227,7 @@ export function createMotion({
const camera = flightState._viewer?.camera;
const scene = flightState._viewer?.scene;
if (!camera?.positionWC || !position || !scene) return 28;
const rangeM = Cesium.Cartesian3.distance(camera.positionWC, position);
const rangeM = geo.cartesian3.distance(camera.positionWC, position);
if (parts.rendering._modelOwnsVisual(icao24)) {
const spec = parts.rendering._modelSpec(
flightState.records.data.get(icao24)?.klass,
@@ -305,9 +306,9 @@ export function createMotion({
const speed = (info && info.speedMps) || 0;
// Plausible single-frame motion (m): real velocity × frame Δt, ×4 slack + 25 m base.
const plausible = speed * dtSec * 4 + 25;
if (Cesium.Cartesian3.distance(raw, flightState._drPrevRaw) > plausible) {
if (geo.cartesian3.distance(raw, flightState._drPrevRaw) > plausible) {
// Discontinuity — re-anchor so the DISPLAYED position stays continuous, then decay.
Cesium.Cartesian3.subtract(
geo.cartesian3.subtract(
flightState._drPrevDisplay,
raw,
flightState._drCorrection,
@@ -315,22 +316,22 @@ export function createMotion({
flightState._drCorrectionStartMs = nowMs;
}
} else {
Cesium.Cartesian3.fromElements(0, 0, 0, flightState._drCorrection);
geo.cartesian3.fromElements(0, 0, 0, flightState._drCorrection);
flightState._drCorrectionStartMs = nowMs - DR_CORRECTION_MS; // fully decayed
}
const elapsed = nowMs - flightState._drCorrectionStartMs;
const factor =
elapsed >= DR_CORRECTION_MS ? 0 : 1 - elapsed / DR_CORRECTION_MS;
const display = Cesium.Cartesian3.multiplyByScalar(
const display = geo.cartesian3.multiplyByScalar(
flightState._drCorrection,
factor,
flightState._trackedPosHolder,
);
Cesium.Cartesian3.add(raw, display, display);
geo.cartesian3.add(raw, display, display);
Cesium.Cartesian3.clone(raw, flightState._drPrevRaw);
Cesium.Cartesian3.clone(display, flightState._drPrevDisplay);
geo.cartesian3.clone(raw, flightState._drPrevRaw);
geo.cartesian3.clone(display, flightState._drPrevDisplay);
flightState._drPrevMs = nowMs;
flightState._drReconcileValid = true;
flightState._cachedDRPosition = display;
@@ -409,7 +410,7 @@ export function createMotion({
!parts.rendering._modelOwnsVisual(flightState._trackedIcao)
)
return null;
return Cesium.Matrix4.getTranslation(
return geo.matrix4.getTranslation(
flightState._trackedModel.modelMatrix,
flightState._scratchTrailClip,
);
+14 -13
View File
@@ -1,5 +1,6 @@
import { displayedKinematics } from '../../data/motionModel.js';
import * as Cesium from 'cesium';
import * as geo from '../../geo/index.js';
import { isExplicitLayerStateOrigin } from '../../data/layerState.js';
import { aircraftIncludedInNearby } from '../../data/aircraftNearbyPolicy.js';
import { modelVisualAnchor } from '../../data/modelVisualAnchor.js';
@@ -102,9 +103,9 @@ export function createQueries({
reportedSpeedMps: info?.speedMps,
reportedTrackDeg: info?.track,
});
const carto = Cesium.Cartographic.fromCartesian(
const carto = geo.cartographic.fromCartesian(
basePos,
Cesium.Ellipsoid.WGS84,
geo.ellipsoid.WGS84,
flightState._scratchCarto,
);
if (!carto) return null;
@@ -115,9 +116,9 @@ export function createQueries({
// getTrackedSubject and the voice narration can read it straight off the
// descriptor instead of reaching back into `_flightData`.
registration: _toCleanText(info?.registration) || null,
position: Cesium.Cartesian3.clone(basePos),
latitude: Cesium.Math.toDegrees(carto.latitude),
longitude: Cesium.Math.toDegrees(carto.longitude),
position: geo.cartesian3.clone(basePos),
latitude: geo.math.toDegrees(carto.latitude),
longitude: geo.math.toDegrees(carto.longitude),
// Keep the cockpit readout on the reported aviation altitude. Render
// terrain height is a separate visual datum and may be below zero.
altitudeM: Number.isFinite(info?.altitudeM)
@@ -360,7 +361,7 @@ export function createQueries({
const pos = trackedPos || bb.position;
if (!pos) continue;
const distance = Cesium.Cartesian3.distance(center, pos);
const distance = geo.cartesian3.distance(center, pos);
if (distance > maxRange) continue;
const info = flightState.records.data.get(icao24);
@@ -444,7 +445,7 @@ export function createQueries({
sourceId: icao24,
type: 'AIR',
tier: 'military',
_weldPos: new Cesium.Cartesian3(),
_weldPos: geo.cartesian3.create(),
};
flightState._detectionObjects.set(icao24, object);
}
@@ -464,7 +465,7 @@ export function createQueries({
Number.isFinite(model.computedScale)
? model.computedScale
: spec.scale,
object._weldPos || (object._weldPos = new Cesium.Cartesian3()),
object._weldPos || (object._weldPos = geo.cartesian3.create()),
)
: bb.position;
if (!pos) continue;
@@ -590,9 +591,9 @@ export function createQueries({
for (const [icao24, bb] of flightState._billboards) {
const pos = bb.position;
if (!pos) continue;
const carto = Cesium.Cartographic.fromCartesian(
const carto = geo.cartographic.fromCartesian(
pos,
Cesium.Ellipsoid.WGS84,
geo.ellipsoid.WGS84,
flightState._scratchCarto,
);
if (!carto) continue;
@@ -606,8 +607,8 @@ export function createQueries({
_toCleanText(info?.registration) ||
icao24,
position: pos,
latitude: Cesium.Math.toDegrees(carto.latitude),
longitude: Cesium.Math.toDegrees(carto.longitude),
latitude: geo.math.toDegrees(carto.latitude),
longitude: geo.math.toDegrees(carto.longitude),
altitudeM: carto.height,
});
if (result.length >= limit) break;
@@ -782,7 +783,7 @@ export function createQueries({
// Same label chain as getNearby/getDetectableObjects: a callsign-less
// contact reads as its registration, never as the raw ICAO hex.
label: described.callsign || described.registration || described.icao24,
position: Cesium.Cartesian3.clone(described.position),
position: geo.cartesian3.clone(described.position),
};
},
+12 -11
View File
@@ -1,4 +1,5 @@
import * as Cesium from 'cesium';
import * as geo from '../../geo/index.js';
import { CLASS_SCALE_2D } from '../../data/aircraftClass.js';
import {
turnRateFromFixHistory,
@@ -52,7 +53,7 @@ export function createMilitarySnapshotRenderer({
: false,
},
);
const position = Cesium.Cartesian3.fromDegrees(
const position = geo.cartesian3.fromDegrees(
meta.rawLon,
meta.rawLat,
meta.renderAltitudeM,
@@ -61,7 +62,7 @@ export function createMilitarySnapshotRenderer({
flightState._cullPositions.set(
icao24,
meta.renderAltitudeM < 10
? Cesium.Cartesian3.fromDegrees(meta.rawLon, meta.rawLat, 12)
? geo.cartesian3.fromDegrees(meta.rawLon, meta.rawLat, 12)
: null,
);
const isTracked = icao24 === flightState._trackedIcao;
@@ -84,7 +85,7 @@ export function createMilitarySnapshotRenderer({
history.push({
time: fixTime,
epochMs: fixEpochMs,
position: position.clone(),
position: geo.cartesian3.clone(position),
velocity: meta.speedMps,
track: meta.track,
});
@@ -99,7 +100,7 @@ export function createMilitarySnapshotRenderer({
// (owner): ground traffic appends too — taxi history stays live
// after touchdown; _appendTrailFix floors grounded positions at
// the surface so the ground leg drapes instead of diving.
if (isTracked) tracking._appendTrailFix(position.clone());
if (isTracked) tracking._appendTrailFix(geo.cartesian3.clone(position));
} else {
const modelOwnsGroundVisual = rendering._modelOwnsVisual(icao24);
if (!modelOwnsGroundVisual) {
@@ -120,7 +121,7 @@ export function createMilitarySnapshotRenderer({
meta.turnRateDps = turnRateFromFixHistory(history);
if (isTracked)
tracking._appendTrailFix(
Cesium.Cartesian3.clone(synthetic.position),
geo.cartesian3.clone(synthetic.position),
);
}
}
@@ -151,7 +152,7 @@ export function createMilitarySnapshotRenderer({
// projected back to that delayed time. Creating it at the raw fix drew
// it ahead until the next fleet tick snapped it back.
const displayed =
motion._deadReckon(icao24, new Cesium.Cartesian3()) || position;
motion._deadReckon(icao24, geo.cartesian3.create()) || position;
const bb = flightState._billboardCollection.add({
position: displayed,
image: aircraftIcon(rendering._iconKind(icao24, meta.klass)),
@@ -163,7 +164,7 @@ export function createMilitarySnapshotRenderer({
(meta.onGround ? GROUND_SCALE : 1),
// Screen-projected rotation lands on the next fleet tick.
rotation: 0,
alignedAxis: Cesium.Cartesian3.ZERO,
alignedAxis: geo.cartesian3.ZERO,
color: isTracked ? TRACKED_ICON_COLOR : MIL_ICON_COLOR,
sizeInMeters: false,
scaleByDistance: rendering._normalBillboardScaleByDistance(),
@@ -217,7 +218,7 @@ export function createMilitarySnapshotRenderer({
continue;
info.renderAltitudeM = lifted;
flightState._cullPositions.delete(icao24);
const liftedPos = Cesium.Cartesian3.fromDegrees(
const liftedPos = geo.cartesian3.fromDegrees(
info.rawLon,
info.rawLat,
lifted,
@@ -225,7 +226,7 @@ export function createMilitarySnapshotRenderer({
const hist = flightState._positionHistory.get(icao24);
const newest = hist?.[hist.length - 1];
if (newest)
newest.position = Cesium.Cartesian3.clone(liftedPos, newest.position);
newest.position = geo.cartesian3.clone(liftedPos, newest.position);
const bbStale = flightState._billboards.get(icao24);
if (bbStale) bbStale.position = liftedPos;
}
@@ -242,10 +243,10 @@ export function createMilitarySnapshotRenderer({
flightState._trackedModel,
].filter(Boolean),
viewerLat: viewerCarto
? Cesium.Math.toDegrees(viewerCarto.latitude)
? geo.math.toDegrees(viewerCarto.latitude)
: undefined,
viewerLon: viewerCarto
? Cesium.Math.toDegrees(viewerCarto.longitude)
? geo.math.toDegrees(viewerCarto.longitude)
: undefined,
});
}
+20 -19
View File
@@ -1,6 +1,7 @@
import { MilitaryFlightRecords } from './records.js';
import { createMilitaryFeed } from './ingestion.js';
import * as Cesium from 'cesium';
import * as geo from '../../geo/index.js';
export function createFlightState({ source, services }) {
const { createGroundSnap } = services.groundSnap;
@@ -28,9 +29,9 @@ export function createFlightState({ source, services }) {
flightState._groundSnap = createGroundSnap();
flightState._scratchGroundCarto = new Cesium.Cartographic();
flightState._scratchGroundCarto = geo.cartographic.create();
flightState._scratchGroundPos = new Cesium.Cartesian3();
flightState._scratchGroundPos = geo.cartesian3.create();
/** @type {Cesium.PrimitiveCollection|null} */
@@ -81,10 +82,10 @@ export function createFlightState({ source, services }) {
flightState._scratchModelHpr = new Cesium.HeadingPitchRoll(0, 0, 0);
flightState._scratchModelMtx = new Cesium.Matrix4();
flightState._scratchModelMtx = geo.matrix4.create();
flightState._scratchModelBS = new Cesium.BoundingSphere(
new Cesium.Cartesian3(),
geo.cartesian3.create(),
1.0,
);
// frustum-visibility test
@@ -215,25 +216,25 @@ export function createFlightState({ source, services }) {
// -- Scratch variables (reused each frame to avoid GC pressure) --
flightState._scratchOffset = new Cesium.Cartesian3();
flightState._scratchOffset = geo.cartesian3.create();
flightState._scratchCarto = new Cesium.Cartographic();
flightState._scratchCarto = geo.cartographic.create();
flightState._scratchEnu = new Cesium.Matrix4();
flightState._scratchEnu = geo.matrix4.create();
flightState._scratchArc = { east: 0, north: 0, endCourseDeg: 0 };
flightState._scratchRenderTime = new Cesium.JulianDate();
flightState._scratchFleetPos = new Cesium.Cartesian3();
flightState._scratchFleetPos = geo.cartesian3.create();
flightState._scratchSonarScreen = new Cesium.Cartesian2();
flightState._scratchSonarScreen = geo.cartesian2.create();
flightState._scratchDrRaw = new Cesium.Cartesian3();
flightState._scratchDrRaw = geo.cartesian3.create();
flightState._scratchWarmupTime = new Cesium.JulianDate();
flightState._trackedPosHolder = new Cesium.Cartesian3();
flightState._trackedPosHolder = geo.cartesian3.create();
// -- Per-frame dead-reckoning cache for the tracked entity --
/** @type {Cesium.Cartesian3|null} Cached DR position for the current render frame */
@@ -285,13 +286,13 @@ export function createFlightState({ source, services }) {
flightState._displayCourse = new Map();
flightState._drCorrection = new Cesium.Cartesian3(0, 0, 0);
flightState._drCorrection = geo.cartesian3.create(0, 0, 0);
flightState._drCorrectionStartMs = 0;
flightState._drPrevRaw = new Cesium.Cartesian3();
flightState._drPrevRaw = geo.cartesian3.create();
flightState._drPrevDisplay = new Cesium.Cartesian3();
flightState._drPrevDisplay = geo.cartesian3.create();
flightState._drPrevMs = 0;
@@ -303,18 +304,18 @@ export function createFlightState({ source, services }) {
/** @type {Cesium.Cartesian3} Scratch for the tracked model's rendered translation. */
flightState._trackedVisualPos = new Cesium.Cartesian3();
flightState._trackedVisualPos = geo.cartesian3.create();
flightState._trackedTrailPos = new Cesium.Cartesian3();
flightState._trackedTrailPos = geo.cartesian3.create();
/** Scratch for the tracked model's world-space origin (the envelope centre). */
flightState._scratchTrailClip = new Cesium.Cartesian3();
flightState._scratchTrailClip = geo.cartesian3.create();
/** Scratch for the shortened trail-head start. Owned by the head-segment
* callback alone, so nothing else can overwrite it mid-frame. */
flightState._scratchTrailHead = new Cesium.Cartesian3();
flightState._scratchTrailHead = geo.cartesian3.create();
/** Hysteresis latch for the tracked contact's zoom regime, plus the selection it
* belongs to. Scoped per selection so a NEW target re-evaluates against the ENTER
@@ -350,6 +351,6 @@ export function createFlightState({ source, services }) {
/** @type {Cesium.Cartographic} Scratch for _trailFloorPosition (per-frame safe). */
flightState._scratchTrailCarto = new Cesium.Cartographic();
flightState._scratchTrailCarto = geo.cartographic.create();
return flightState;
}