mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
refactor(camera,data): take vector maths from src/geo
Move the Cartesian3/Cartographic/Matrix/Quaternion/Transforms/Math calls of the first batch (src/data motion, anchor, tracked-camera and icon helpers; the root camera, locations, HUD, share-link, world-focus, ground-guard, orbit and cloud modules; services/groundSnap) to src/geo. Behaviour is unchanged: same argument order, same `result` semantics. Kept on Cesium: out-of-scope classes (JulianDate, Axis, HeadingPitchRange, BoundingSphere, Rectangle, EasingFunction, EllipsoidalOccluder with its WGS84 and seed vector, SceneTransforms) and Matrix4.IDENTITY wherever it is handed straight to a Cesium camera call (locations.test pins its identity). synthesizeForwardKinematicsFix now returns a plain position, so the two snapshot renderers clone it with the static Cesium.Cartesian3.clone instead of the instance method. The package-boundary groups that reach these modules now own the src/geo files.
This commit is contained in:
1 parent
4a92ed8377
commit
d886be68a8
17 files changed
+481
-285
No files matched your search
+22
-21
@@ -22,6 +22,7 @@
|
||||
* unverified math swap, decides whether exact projection becomes preferred.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../geo/index.js';
|
||||
|
||||
/** Meters used to give the course vector a stable projection magnitude. */
|
||||
const FORWARD_PROBE_M = 2000;
|
||||
@@ -31,11 +32,11 @@ const FORWARD_PROBE_M = 2000;
|
||||
*/
|
||||
const MIN_SCREEN_COMPONENT_M = 0.5;
|
||||
/** Ignore sub-degree projection noise while retaining deliberate camera-orbit rotation. */
|
||||
const ROTATION_DEADBAND_RAD = Cesium.Math.toRadians(0.5);
|
||||
const ROTATION_DEADBAND_RAD = geo.math.toRadians(0.5);
|
||||
|
||||
const _scratchEnu = new Cesium.Matrix4();
|
||||
const _scratchForward = new Cesium.Cartesian3();
|
||||
const _scratchWorldForward = new Cesium.Cartesian3();
|
||||
const _scratchEnu = geo.matrix4.create();
|
||||
const _scratchForward = geo.cartesian3.create();
|
||||
const _scratchWorldForward = geo.cartesian3.create();
|
||||
|
||||
/**
|
||||
* Computes the billboard rotation (radians, CCW-positive, for
|
||||
@@ -58,19 +59,19 @@ export function screenProjectedRotation(
|
||||
const camera = scene?.camera;
|
||||
if (!camera?.rightWC || !camera?.upWC || !position) return previous;
|
||||
|
||||
const courseRad = Cesium.Math.toRadians(courseDeg || 0);
|
||||
Cesium.Cartesian3.fromElements(
|
||||
const courseRad = geo.math.toRadians(courseDeg || 0);
|
||||
geo.cartesian3.fromElements(
|
||||
Math.sin(courseRad) * FORWARD_PROBE_M,
|
||||
Math.cos(courseRad) * FORWARD_PROBE_M,
|
||||
0,
|
||||
_scratchForward,
|
||||
);
|
||||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(
|
||||
const enu = geo.transforms.eastNorthUpToFixedFrame(
|
||||
position,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
_scratchEnu,
|
||||
);
|
||||
Cesium.Matrix4.multiplyByPointAsVector(
|
||||
geo.matrix4.multiplyByPointAsVector(
|
||||
enu,
|
||||
_scratchForward,
|
||||
_scratchWorldForward,
|
||||
@@ -79,8 +80,8 @@ export function screenProjectedRotation(
|
||||
// Screen x follows camera-right. Window y grows downward, the opposite of
|
||||
// camera-up. Projecting the vector itself avoids clipping/behind-camera
|
||||
// failure modes from the old forward-point worldToWindowCoordinates probe.
|
||||
const dx = Cesium.Cartesian3.dot(_scratchWorldForward, camera.rightWC);
|
||||
const dy = -Cesium.Cartesian3.dot(_scratchWorldForward, camera.upWC);
|
||||
const dx = geo.cartesian3.dot(_scratchWorldForward, camera.rightWC);
|
||||
const dy = -geo.cartesian3.dot(_scratchWorldForward, camera.upWC);
|
||||
if (dx * dx + dy * dy < MIN_SCREEN_COMPONENT_M * MIN_SCREEN_COMPONENT_M)
|
||||
return previous;
|
||||
|
||||
@@ -129,7 +130,7 @@ const _occluder = new Cesium.EllipsoidalOccluder(
|
||||
*/
|
||||
export const HORIZON_FEATHER_RAD = 0.019;
|
||||
|
||||
const _wgs84OneOverRadii = Cesium.Ellipsoid.WGS84.oneOverRadii;
|
||||
const _wgs84OneOverRadii = geo.ellipsoid.WGS84.oneOverRadii;
|
||||
|
||||
/**
|
||||
* How much SKY sits behind a world point from this camera: 0 (planet behind)
|
||||
@@ -271,9 +272,9 @@ export function cameraPoseSignature(camera) {
|
||||
* frustum the vehicle is nowhere near.
|
||||
*/
|
||||
const NEAR_PROBE_M = 30;
|
||||
const _scratchProbe = new Cesium.Cartesian3();
|
||||
const _scratchWindowAt = new Cesium.Cartesian2();
|
||||
const _scratchWindowAhead = new Cesium.Cartesian2();
|
||||
const _scratchProbe = geo.cartesian3.create();
|
||||
const _scratchWindowAt = geo.cartesian2.create();
|
||||
const _scratchWindowAhead = geo.cartesian2.create();
|
||||
|
||||
/**
|
||||
* Billboard rotation for a SURFACE contact, projected exactly — perspective
|
||||
@@ -311,24 +312,24 @@ export function perspectiveProjectedRotation(
|
||||
if (!scene?.camera || !position || !Number.isFinite(courseDeg)) {
|
||||
return previous;
|
||||
}
|
||||
const courseRad = Cesium.Math.toRadians(courseDeg);
|
||||
Cesium.Cartesian3.fromElements(
|
||||
const courseRad = geo.math.toRadians(courseDeg);
|
||||
geo.cartesian3.fromElements(
|
||||
Math.sin(courseRad) * NEAR_PROBE_M,
|
||||
Math.cos(courseRad) * NEAR_PROBE_M,
|
||||
0,
|
||||
_scratchForward,
|
||||
);
|
||||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(
|
||||
const enu = geo.transforms.eastNorthUpToFixedFrame(
|
||||
position,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
_scratchEnu,
|
||||
);
|
||||
Cesium.Matrix4.multiplyByPointAsVector(
|
||||
geo.matrix4.multiplyByPointAsVector(
|
||||
enu,
|
||||
_scratchForward,
|
||||
_scratchWorldForward,
|
||||
);
|
||||
Cesium.Cartesian3.add(position, _scratchWorldForward, _scratchProbe);
|
||||
geo.cartesian3.add(position, _scratchWorldForward, _scratchProbe);
|
||||
|
||||
const at = Cesium.SceneTransforms.worldToWindowCoordinates(
|
||||
scene,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../geo/index.js';
|
||||
/**
|
||||
* Model-space visual centres measured from the shipped GLBs' scene-space AABBs.
|
||||
*
|
||||
@@ -76,13 +77,13 @@ export const MODEL_TRAIL_ANCHOR_NATIVE = Object.freeze({
|
||||
* 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(
|
||||
const AXIS_CORRECTION = geo.matrix4.multiplyTransformation(
|
||||
Cesium.Axis.Y_UP_TO_Z_UP,
|
||||
Cesium.Axis.Z_UP_TO_X_UP,
|
||||
new Cesium.Matrix4(),
|
||||
geo.matrix4.create(),
|
||||
);
|
||||
const _anchorScratch = new Cesium.Cartesian3();
|
||||
const _chainScratch = new Cesium.Matrix4();
|
||||
const _anchorScratch = geo.cartesian3.create();
|
||||
const _chainScratch = geo.matrix4.create();
|
||||
|
||||
/**
|
||||
* World position of a model-local anchor, through the SAME transform chain
|
||||
@@ -121,17 +122,17 @@ export function modelAnchorWorld(model, nativeAnchor, result) {
|
||||
// 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(
|
||||
geo.matrix4.multiplyTransformation(
|
||||
model.modelMatrix,
|
||||
components || Cesium.Matrix4.IDENTITY,
|
||||
components || geo.matrix4.IDENTITY,
|
||||
_chainScratch,
|
||||
);
|
||||
Cesium.Matrix4.multiplyTransformation(
|
||||
geo.matrix4.multiplyTransformation(
|
||||
_chainScratch,
|
||||
AXIS_CORRECTION,
|
||||
_chainScratch,
|
||||
);
|
||||
return Cesium.Matrix4.multiplyByPoint(_chainScratch, _anchorScratch, result);
|
||||
return geo.matrix4.multiplyByPoint(_chainScratch, _anchorScratch, result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+18
-17
@@ -7,10 +7,11 @@
|
||||
* skylight (https://github.com/cpaczek/skylight, MIT) shared/src/aim.ts.
|
||||
*/
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../geo/index.js';
|
||||
|
||||
const DEG = Math.PI / 180;
|
||||
const _mmCarto = new Cesium.Cartographic();
|
||||
const _mmCandidateCarto = new Cesium.Cartographic();
|
||||
const _mmCarto = geo.cartographic.create();
|
||||
const _mmCandidateCarto = geo.cartographic.create();
|
||||
|
||||
export function norm360(deg) {
|
||||
return ((deg % 360) + 360) % 360;
|
||||
@@ -28,22 +29,22 @@ export function norm360(deg) {
|
||||
*/
|
||||
export function liftRepeatedGroundFix(newest, candidatePosition, onGround) {
|
||||
if (!onGround || !newest?.position || !candidatePosition) return false;
|
||||
const stored = Cesium.Cartographic.fromCartesian(
|
||||
const stored = geo.cartographic.fromCartesian(
|
||||
newest.position,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
_mmCarto,
|
||||
);
|
||||
const candidate = Cesium.Cartographic.fromCartesian(
|
||||
const candidate = geo.cartographic.fromCartesian(
|
||||
candidatePosition,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
_mmCandidateCarto,
|
||||
);
|
||||
if (!stored || !candidate || candidate.height <= stored.height) return false;
|
||||
Cesium.Cartesian3.fromRadians(
|
||||
geo.cartesian3.fromRadians(
|
||||
stored.longitude,
|
||||
stored.latitude,
|
||||
candidate.height,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
newest.position,
|
||||
);
|
||||
return true;
|
||||
@@ -66,9 +67,9 @@ export function courseBetweenCartesians(from, to, minChordM = 25) {
|
||||
const dy = to.y - from.y;
|
||||
const dz = to.z - from.z;
|
||||
if (dx * dx + dy * dy + dz * dz < minChordM * minChordM) return null;
|
||||
const carto = Cesium.Cartographic.fromCartesian(
|
||||
const carto = geo.cartographic.fromCartesian(
|
||||
from,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
_mmCarto,
|
||||
);
|
||||
if (!carto) return null;
|
||||
@@ -436,8 +437,8 @@ export function arcOffsetEnu(speedMps, trackDeg, turnRateDps, dtSec, result) {
|
||||
}
|
||||
|
||||
const _forwardFixArc = { east: 0, north: 0, endCourseDeg: 0 };
|
||||
const _forwardFixEnu = new Cesium.Matrix4();
|
||||
const _forwardFixOffset = new Cesium.Cartesian3();
|
||||
const _forwardFixEnu = geo.matrix4.create();
|
||||
const _forwardFixOffset = geo.cartesian3.create();
|
||||
|
||||
/**
|
||||
* Create a forward-only synthetic fix when a repeated position epoch carries
|
||||
@@ -474,21 +475,21 @@ export function synthesizeForwardKinematicsFix(newest, next = {}) {
|
||||
dtSec,
|
||||
_forwardFixArc,
|
||||
);
|
||||
Cesium.Cartesian3.fromElements(
|
||||
geo.cartesian3.fromElements(
|
||||
_forwardFixArc.east,
|
||||
_forwardFixArc.north,
|
||||
0,
|
||||
_forwardFixOffset,
|
||||
);
|
||||
const enu = Cesium.Transforms.eastNorthUpToFixedFrame(
|
||||
const enu = geo.transforms.eastNorthUpToFixedFrame(
|
||||
newest.position,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
_forwardFixEnu,
|
||||
);
|
||||
const position = Cesium.Matrix4.multiplyByPoint(
|
||||
const position = geo.matrix4.multiplyByPoint(
|
||||
enu,
|
||||
_forwardFixOffset,
|
||||
new Cesium.Cartesian3(),
|
||||
geo.cartesian3.create(),
|
||||
);
|
||||
return {
|
||||
time: Cesium.JulianDate.fromDate(new Date(next.epochMs)),
|
||||
|
||||
+20
-20
@@ -1,4 +1,4 @@
|
||||
import * as Cesium from 'cesium';
|
||||
import * as geo from '../geo/index.js';
|
||||
|
||||
const MAX_FRAME_ATTEMPTS = 120;
|
||||
const MIN_TRACKED_RANGE_M = 150;
|
||||
@@ -59,7 +59,7 @@ export function trackedModelScaleForPixelCap({
|
||||
export function trackedDisplayPositionForCamera(entity, time, result) {
|
||||
const displayedPosition = entity?.gevDisplayPosition?.();
|
||||
return displayedPosition
|
||||
? Cesium.Cartesian3.clone(displayedPosition, result)
|
||||
? geo.cartesian3.clone(displayedPosition, result)
|
||||
: entity?.position?.getValue(time, result);
|
||||
}
|
||||
|
||||
@@ -106,15 +106,15 @@ export function clampTrackedCameraPosition(
|
||||
minimumRangeM = MIN_TRACKED_RANGE_M,
|
||||
) {
|
||||
const crossedOrigin =
|
||||
Cesium.Cartesian3.dot(camera.position, previousPosition) <= 0;
|
||||
const forwardDistance = -Cesium.Cartesian3.dot(
|
||||
geo.cartesian3.dot(camera.position, previousPosition) <= 0;
|
||||
const forwardDistance = -geo.cartesian3.dot(
|
||||
camera.position,
|
||||
camera.direction,
|
||||
);
|
||||
const rangeSquared = Cesium.Cartesian3.magnitudeSquared(camera.position);
|
||||
const rangeSquared = geo.cartesian3.magnitudeSquared(camera.position);
|
||||
if (crossedOrigin) {
|
||||
Cesium.Cartesian3.normalize(previousPosition, camera.position);
|
||||
Cesium.Cartesian3.multiplyByScalar(
|
||||
geo.cartesian3.normalize(previousPosition, camera.position);
|
||||
geo.cartesian3.multiplyByScalar(
|
||||
camera.position,
|
||||
minimumRangeM,
|
||||
camera.position,
|
||||
@@ -125,7 +125,7 @@ export function clampTrackedCameraPosition(
|
||||
forwardDistance < minimumRangeM ||
|
||||
rangeSquared < minimumRangeM * minimumRangeM
|
||||
) {
|
||||
Cesium.Cartesian3.multiplyByScalar(
|
||||
geo.cartesian3.multiplyByScalar(
|
||||
camera.direction,
|
||||
-minimumRangeM,
|
||||
camera.position,
|
||||
@@ -155,11 +155,11 @@ export function clampTrackedCameraPosition(
|
||||
export function applyTrackedCameraFrame(viewer, entity, viewFrom) {
|
||||
if (!viewer || !entity || !viewFrom) return;
|
||||
let attempts = 0;
|
||||
const trackedTransform = new Cesium.Matrix4();
|
||||
const trackedPosition = new Cesium.Cartesian3();
|
||||
const resolvedViewFrom = new Cesium.Cartesian3();
|
||||
const cameraOffset = new Cesium.Cartesian3();
|
||||
const previousCameraPosition = new Cesium.Cartesian3();
|
||||
const trackedTransform = geo.matrix4.create();
|
||||
const trackedPosition = geo.cartesian3.create();
|
||||
const resolvedViewFrom = geo.cartesian3.create();
|
||||
const cameraOffset = geo.cartesian3.create();
|
||||
const previousCameraPosition = geo.cartesian3.create();
|
||||
let framed = false;
|
||||
const restoreZoomInertia = acquireStableTrackedZoom(
|
||||
viewer.scene.screenSpaceCameraController,
|
||||
@@ -201,24 +201,24 @@ export function applyTrackedCameraFrame(viewer, entity, viewFrom) {
|
||||
if (attempts >= MAX_FRAME_ATTEMPTS) stop();
|
||||
return;
|
||||
}
|
||||
const transform = Cesium.Transforms.eastNorthUpToFixedFrame(
|
||||
const transform = geo.transforms.eastNorthUpToFixedFrame(
|
||||
position,
|
||||
Cesium.Ellipsoid.WGS84,
|
||||
geo.ellipsoid.WGS84,
|
||||
trackedTransform,
|
||||
);
|
||||
const offset =
|
||||
typeof viewFrom.getValue === 'function'
|
||||
? viewFrom.getValue(viewer.clock.currentTime, resolvedViewFrom)
|
||||
: Cesium.Cartesian3.clone(viewFrom, resolvedViewFrom);
|
||||
: geo.cartesian3.clone(viewFrom, resolvedViewFrom);
|
||||
if (
|
||||
!offset ||
|
||||
Cesium.Cartesian3.magnitudeSquared(offset) < Cesium.Math.EPSILON12
|
||||
geo.cartesian3.magnitudeSquared(offset) < geo.math.EPSILON12
|
||||
) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
viewer.camera.lookAtTransform(transform, offset);
|
||||
Cesium.Cartesian3.clone(viewer.camera.position, previousCameraPosition);
|
||||
geo.cartesian3.clone(viewer.camera.position, previousCameraPosition);
|
||||
framed = true;
|
||||
return;
|
||||
}
|
||||
@@ -227,10 +227,10 @@ export function applyTrackedCameraFrame(viewer, entity, viewFrom) {
|
||||
// continuous camera writer. Only a programmatic zoom that bypasses the
|
||||
// controller's minimumZoomDistance needs correction.
|
||||
if (clampTrackedCameraPosition(viewer.camera, previousCameraPosition)) {
|
||||
Cesium.Cartesian3.clone(viewer.camera.position, cameraOffset);
|
||||
geo.cartesian3.clone(viewer.camera.position, cameraOffset);
|
||||
viewer.camera.lookAtTransform(viewer.camera.transform, cameraOffset);
|
||||
}
|
||||
Cesium.Cartesian3.clone(viewer.camera.position, previousCameraPosition);
|
||||
geo.cartesian3.clone(viewer.camera.position, previousCameraPosition);
|
||||
});
|
||||
return stop;
|
||||
}
|
||||
Reference in new issue
Block a user