import * as Cesium from 'cesium'; /** * Model-space anchors on the aircraft layers' fleet models: where brackets * and readouts sit (a model's visual centre) and where a tracked trail * attaches (its aft-belly point, measured per model in fleetTrailAnchors.js * and handed to the layers with each model's spec by fleetModels.js). * * Values are native model units in raw glTF axes; callers supply the Model's * effective rendered `computedScale` because Cesium does not bake it into * modelMatrix and may raise it above `scale` to satisfy minimumPixelSize. */ /** @constant Cesium's OWN axis correction for a glTF loaded with the * `Model.fromGltfAsync` defaults (upAxis Y, forwardAxis Z), assembled from its * exported constants rather than restated as numbers here. `ModelSceneGraph` * renders with `modelMatrix x components.transform x axisCorrection`, and the * trail anchor has to ride the same chain or it lands on a different axis than * the aircraft it is supposed to be attached to. */ const AXIS_CORRECTION = Cesium.Matrix4.multiplyTransformation( Cesium.Axis.Y_UP_TO_Z_UP, Cesium.Axis.Z_UP_TO_X_UP, new Cesium.Matrix4(), ); const _anchorScratch = new Cesium.Cartesian3(); const _chainScratch = new Cesium.Matrix4(); /** * World position of a model-local anchor, through the SAME transform chain * Cesium renders the model with. * * This exists because doing it by hand got it wrong. The anchors used to be * stored after a single glTF Y-up -> Z-up step (`[x, y, z]` -> `[x, -z, y]`) and * multiplied straight by `modelMatrix`. That is only half of Cesium's * correction: with the `fromGltfAsync` defaults it also applies Z_UP_TO_X_UP, * and the full correction maps raw glTF `[x, y, z]` -> `[z, x, y]`. The * aircraft's longitudinal axis is raw glTF X, so the old conversion put the * aft offset on the RENDERED model's lateral axis — a 90-degree disagreement. * Because `modelMatrix` carries the heading, both frames rotated together and * the offset came out sideways in world space, flipping which side it appeared * on as the course changed. Measured on the live rig at N/E/S/W plus a * helicopter: the old path put 81.47 m of a civil airliner's anchor entirely on * cross-track with 0 aft, at every heading; this path puts 81.47 m aft with 0 * cross. * * The anchors are therefore stored in RAW glTF coordinates and transformed * here, so exactly one transform — Cesium's own — stands between the table and * the world, and there is no second convention to drift. * * @param {object} model Cesium Model (needs modelMatrix, computedScale, sceneGraph). * @param {readonly number[]} nativeAnchor Raw glTF `[x, y, z]` native-unit offset. * @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result. * @returns {{x:number,y:number,z:number}|null} World position of the anchor. */ export function modelAnchorWorld(model, nativeAnchor, result) { if (!model || !model.modelMatrix || !nativeAnchor || !result) return null; const scale = Number.isFinite(model.computedScale) ? model.computedScale : 1; _anchorScratch.x = (nativeAnchor[0] || 0) * scale; _anchorScratch.y = (nativeAnchor[1] || 0) * scale; _anchorScratch.z = (nativeAnchor[2] || 0) * scale; // The model's own root transform, when it exposes one, then Cesium's axis // correction. Read from the instance rather than assumed, so an asset with a // baked root transform is carried too. const components = model?.sceneGraph?.components?.transform; Cesium.Matrix4.multiplyTransformation( model.modelMatrix, components || Cesium.Matrix4.IDENTITY, _chainScratch, ); Cesium.Matrix4.multiplyTransformation( _chainScratch, AXIS_CORRECTION, _chainScratch, ); return Cesium.Matrix4.multiplyByPoint(_chainScratch, _anchorScratch, result); } /** * Where a trail head segment should START being drawn: the whole segment, a * shortened piece of it, or nothing at all. * * The head segment bridges the last displayed trail point to the aft-belly * anchor. On a moving contact that point is a poll interval behind, so the * segment arrives from open air and terminates ON the hull, which is what it * should do. On a PARKED one it sits where the aircraft is: the segment * degenerates into a line from inside the model out to its own anchor — a stick * through half the fuselage on an aeroplane going nowhere. Grounded tracking * starts a trail unconditionally, so every parked selection drew one. * * Three earlier cuts were wrong, and each is recorded because the shape of the * mistake matters: * * - Testing segment LENGTH against the radius hid real trail. With * an airliner model's 34.41 m radius and 24.09 m tail anchor, a prior fix 58 m * aft gives a 33.98 m segment — under the radius, so suppressed — even * though ~23.6 m of it is in open air behind the aircraft. It also made the * whole segment pop in at once on crossing ~58.4 m. * - CLIPPING the segment at the envelope boundary and keeping only what lay * OUTSIDE fixed the pop but broke the thing the trail is for: a bounding * sphere encloses a lot of empty space around a slender airframe, so * trimming at sphere entry stops the trail visibly SHORT of the aircraft it * is attached to. The head end is never cut. That end is the whole point. * - CONTAINMENT — the whole segment when the start is outside the envelope, * nothing when it is inside — kept both of those ends honest but decided it * with a BOOLEAN, and a boolean at a 34.41 m boundary is a 10.33 m flash. * Moving the start from 34.40 m to 34.42 m turned the entire segment on at * once, and a contact whose next fix landed back inside turned it off again. * Nothing in the world changes by 10 m across 2 cm of travel. * * So the far end MOVES and the near end does not. The drawn start slides along * the segment, from the anchor itself out to the segment's true start, over the * shell between the two lengths the model already defines: * * - the ANCHOR'S OWN STATION, `|anchor − centre|`. Inside that, the last body * point is no further from the aircraft's centre than its own attachment * point is, so every millimetre of the segment would be drawn forward of * the anchor, into the fuselage. Nothing is drawn. A parked contact sits at * exactly zero here, which is the case this whole guard exists for. * - the ENVELOPE, `radiusM`. At and beyond it the segment is drawn in full, * bit-identical to the containment rule it replaces — so every contact that * has moved more than its own size (any airborne contact: 30 s of flight is * kilometres) is completely unaffected by any of this. * * Between them the visible length grows continuously from zero, so a contact * creeping across the boundary reveals its trail a few centimetres at a time * and an in/out/in flap costs a few centimetres each way instead of ten metres. * * Scale-free rather than tuned: both ends of that shell come from the model — * `radiusM` carries Cesium's `computedScale`, so it tracks whatever * `minimumPixelSize` inflated the model to, and the station comes from the * anchor being drawn to. * * @param {{x:number,y:number,z:number}} start Segment start (last trail point). * @param {{x:number,y:number,z:number}|null} anchor Segment end, world space — * the aft-belly attachment point actually being drawn to. * @param {{x:number,y:number,z:number}|null} center Model origin, world space. * @param {number} radiusM Rendered bounding radius (nativeRadiusM x computedScale). * @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result. * @returns {{x:number,y:number,z:number}|null} The point to draw FROM, or null * when the segment should not be drawn. May be `start` itself. */ export function trailHeadStart(start, anchor, center, radiusM, result) { if (!start) return null; // No envelope to judge against (no model drawing this contact, a radius we // cannot trust, or nowhere to put the answer) — draw it all rather than lose // a real trail. if (!anchor || !center || !result) return start; if (!Number.isFinite(radiusM) || radiusM <= 0) return start; const startD = Math.hypot( start.x - center.x, start.y - center.y, start.z - center.z, ); if (startD >= radiusM) return start; const anchorD = Math.hypot( anchor.x - center.x, anchor.y - center.y, anchor.z - center.z, ); // A degenerate shell (an anchor at or outside the envelope — no shipped asset // has one, since every anchor is a measured hull point) leaves nothing to // ramp across; fall back to the containment verdict rather than divide by it. if (!(radiusM > anchorD)) return null; const revealed = (startD - anchorD) / (radiusM - anchorD); if (revealed <= 0) return null; result.x = anchor.x + (start.x - anchor.x) * revealed; result.y = anchor.y + (start.y - anchor.y) * revealed; result.z = anchor.z + (start.z - anchor.z) * revealed; return result; } /** * Transform a model-local visual centre into world coordinates without * allocating. `modelMatrix` continues to own the model origin (including * ground snapping); bracket/readout and trail consumers supply their own * model-local anchor. * @param {ArrayLike} modelMatrix Cesium Matrix4-compatible value. * @param {readonly number[]} nativeCenter Cesium-local native-unit offset. * @param {number} scale Cesium Model.computedScale applied outside modelMatrix. * @param {{x:number,y:number,z:number}} result Mutable Cartesian-like result. * @returns {{x:number,y:number,z:number}|null} World visual centre. */ export function modelVisualAnchor(modelMatrix, nativeCenter, scale, result) { if (!modelMatrix || !result) return null; const safeScale = Number.isFinite(scale) ? scale : 1; const x = (nativeCenter?.[0] || 0) * safeScale; const y = (nativeCenter?.[1] || 0) * safeScale; const z = (nativeCenter?.[2] || 0) * safeScale; result.x = modelMatrix[0] * x + modelMatrix[4] * y + modelMatrix[8] * z + modelMatrix[12]; result.y = modelMatrix[1] * x + modelMatrix[5] * y + modelMatrix[9] * z + modelMatrix[13]; result.z = modelMatrix[2] * x + modelMatrix[6] * y + modelMatrix[10] * z + modelMatrix[14]; return result; }