mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Compute launch paths, cameras and colours without Cesium
The launches layer's path building, replay camera maths, policy constants, state, marker colours and mission panel no longer import Cesium. Paths and camera poses are plain ECEF points computed by the geo helpers with the default renderer's own formulas in the same order, so trajectories, orbits and camera framing land where they did. Geodesic interpolation moves from the stub renderer into geo/geodesic (geodesicLine, solved once per geodesic), and geo/sphere gains the renderer's sphere union. Marker colours are renderer colours, with the panel's CSS from the same palette. The rendering part, still Cesium for its entities, converts the plain points where the entities take them, culls with the shared horizon test, loads the unbound camera factory, and hands the panel the live payload position directly instead of through the entity. Camera, model, panel, paths, policy, policyHelpers and state leave the allow-list.
This commit is contained in:
1 parent
0782b073e2
commit
a2503476de
19 files changed
+561
-548
No files matched your search
@@ -148,3 +148,96 @@ export function geodesicInverse(lon1, lat1, lon2, lat2) {
|
||||
export function surfaceDistance(start, end) {
|
||||
return geodesicInverse(start.lon, start.lat, end.lon, end.lat).distance;
|
||||
}
|
||||
|
||||
/**
|
||||
* The WGS84 geodesic from (lon1, lat1) to (lon2, lat2), radians, solved once
|
||||
* as the default renderer's `EllipsoidGeodesic` solves it: its length in
|
||||
* metres, its start heading, and `at(fraction)`, the longitude and latitude
|
||||
* (radians) that fraction of the way along it, by the series the renderer
|
||||
* interpolates with, term for term. The longitude is not wrapped, as the
|
||||
* renderer's is not.
|
||||
*/
|
||||
export function geodesicLine(lon1, lat1, lon2, lat2) {
|
||||
const { distance, startHeading, uSquared } = geodesicInverse(
|
||||
lon1,
|
||||
lat1,
|
||||
lon2,
|
||||
lat2,
|
||||
);
|
||||
const cosH = Math.cos(startHeading);
|
||||
const sinH = Math.sin(startHeading);
|
||||
const tanU = (1 - FLATTENING) * Math.tan(lat1);
|
||||
const cosU = 1 / Math.sqrt(1 + tanU * tanU);
|
||||
const sinU = cosU * tanU;
|
||||
const sigma1 = Math.atan2(tanU, cosH);
|
||||
const sineAlpha = cosU * sinH;
|
||||
const cos2Alpha = 1 - sineAlpha * sineAlpha;
|
||||
const cosAlpha = Math.sqrt(cos2Alpha);
|
||||
const u2 = uSquared / 4;
|
||||
const u4 = u2 * u2;
|
||||
const u6 = u4 * u2;
|
||||
const u8 = u4 * u4;
|
||||
const a0 = 1 + u2 - (3 * u4) / 4 + (5 * u6) / 4 - (175 * u8) / 64;
|
||||
const a1 = 1 - u2 + (15 * u4) / 8 - (35 * u6) / 8;
|
||||
const a2 = 1 - 3 * u2 + (35 * u4) / 4;
|
||||
const a3 = 1 - 5 * u2;
|
||||
const distanceRatio =
|
||||
a0 * sigma1 -
|
||||
(a1 * Math.sin(2 * sigma1) * u2) / 2 -
|
||||
(a2 * Math.sin(4 * sigma1) * u4) / 16 -
|
||||
(a3 * Math.sin(6 * sigma1) * u6) / 48 -
|
||||
(Math.sin(8 * sigma1) * 5 * u8) / 512;
|
||||
|
||||
return {
|
||||
distance,
|
||||
startHeading,
|
||||
at(fraction) {
|
||||
const s = distanceRatio + (distance * fraction) / MINOR;
|
||||
const cos2S = Math.cos(2 * s);
|
||||
const cos4S = Math.cos(4 * s);
|
||||
const cos6S = Math.cos(6 * s);
|
||||
const sin2S = Math.sin(2 * s);
|
||||
const sin4S = Math.sin(4 * s);
|
||||
const sin6S = Math.sin(6 * s);
|
||||
const sin8S = Math.sin(8 * s);
|
||||
const s2 = s * s;
|
||||
const s3 = s * s2;
|
||||
let sigma =
|
||||
(2 * s3 * u8 * cos2S) / 3 +
|
||||
s *
|
||||
(1 -
|
||||
u2 +
|
||||
(7 * u4) / 4 -
|
||||
(15 * u6) / 4 +
|
||||
(579 * u8) / 64 -
|
||||
(u4 - (15 * u6) / 4 + (187 * u8) / 16) * cos2S -
|
||||
((5 * u6) / 4 - (115 * u8) / 16) * cos4S -
|
||||
(29 * u8 * cos6S) / 16) +
|
||||
(u2 / 2 - u4 + (71 * u6) / 32 - (85 * u8) / 16) * sin2S +
|
||||
((5 * u4) / 16 - (5 * u6) / 4 + (383 * u8) / 96) * sin4S -
|
||||
s2 * ((u6 - (11 * u8) / 2) * sin2S + (5 * u8 * sin4S) / 2) +
|
||||
((29 * u6) / 96 - (29 * u8) / 16) * sin6S +
|
||||
(539 * u8 * sin8S) / 1536;
|
||||
|
||||
const theta = Math.asin(Math.sin(sigma) * cosAlpha);
|
||||
const lat = Math.atan((MAJOR / MINOR) * Math.tan(theta));
|
||||
sigma -= sigma1;
|
||||
const cos2Sm = Math.cos(2 * sigma1 + sigma);
|
||||
const sinSigma = Math.sin(sigma);
|
||||
const cosSigma = Math.cos(sigma);
|
||||
const lambda = Math.atan2(
|
||||
sinSigma * sinH,
|
||||
cosU * cosSigma - sinU * sinSigma * cosH,
|
||||
);
|
||||
const l =
|
||||
lambda -
|
||||
deltaLambda(sineAlpha, cos2Alpha, sigma, sinSigma, cosSigma, cos2Sm);
|
||||
return { lon: lon1 + l, lat };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** `geodesicLine(lon1, lat1, lon2, lat2).at(fraction)`. */
|
||||
export function geodesicAt(lon1, lat1, lon2, lat2, fraction) {
|
||||
return geodesicLine(lon1, lat1, lon2, lat2).at(fraction);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import test from 'node:test';
|
||||
import { surfaceDistance } from './geodesic.js';
|
||||
import { geodesicAt, surfaceDistance } from './geodesic.js';
|
||||
|
||||
test('surface distance matches the default renderer’s geodesic', () => {
|
||||
let seed = 11;
|
||||
@@ -45,3 +45,39 @@ test('the same point is no distance away, and nearly antipodal points still answ
|
||||
);
|
||||
assert.ok(Number.isFinite(far) && far > 1.9e7);
|
||||
});
|
||||
|
||||
test('a point along the geodesic is the default renderer’s', () => {
|
||||
let seed = 23;
|
||||
const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647;
|
||||
const d = Math.PI / 180;
|
||||
for (let i = 0; i < 200; i++) {
|
||||
const start = new Cesium.Cartographic(
|
||||
(random() * 360 - 180) * d,
|
||||
(random() * 170 - 85) * d,
|
||||
);
|
||||
const reach = i % 2 ? 3 : 90;
|
||||
const end = new Cesium.Cartographic(
|
||||
start.longitude + (random() * 2 - 1) * reach * d,
|
||||
Math.max(
|
||||
-89,
|
||||
Math.min(89, start.latitude / d + (random() * 2 - 1) * reach),
|
||||
) * d,
|
||||
);
|
||||
const geodesic = new Cesium.EllipsoidGeodesic(start, end);
|
||||
for (const fraction of [0, 0.25, 0.5, 0.9, 1]) {
|
||||
const theirs = geodesic.interpolateUsingFraction(fraction);
|
||||
const ours = geodesicAt(
|
||||
start.longitude,
|
||||
start.latitude,
|
||||
end.longitude,
|
||||
end.latitude,
|
||||
fraction,
|
||||
);
|
||||
assert.ok(
|
||||
Math.abs(ours.lon - theirs.longitude) <= 1e-12 &&
|
||||
Math.abs(ours.lat - theirs.latitude) <= 1e-12,
|
||||
`${i} @ ${fraction}: ${ours.lon},${ours.lat} vs ${theirs.longitude},${theirs.latitude}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -81,3 +81,36 @@ export function sphereFromPoints(points) {
|
||||
? { center: ritter, radius: ritterRadius }
|
||||
: { center: naive, radius: naiveRadius };
|
||||
}
|
||||
|
||||
/**
|
||||
* The smallest sphere holding both spheres, by the default renderer's own
|
||||
* method (`BoundingSphere.union`).
|
||||
* @param {{center: {x: number, y: number, z: number}, radius: number}} left
|
||||
* @param {{center: {x: number, y: number, z: number}, radius: number}} right
|
||||
* @returns {{center: {x: number, y: number, z: number}, radius: number}}
|
||||
*/
|
||||
export function sphereUnion(left, right) {
|
||||
const toRight = {
|
||||
x: right.center.x - left.center.x,
|
||||
y: right.center.y - left.center.y,
|
||||
z: right.center.z - left.center.z,
|
||||
};
|
||||
const separation = Math.sqrt(
|
||||
toRight.x * toRight.x + toRight.y * toRight.y + toRight.z * toRight.z,
|
||||
);
|
||||
if (left.radius >= separation + right.radius)
|
||||
return { center: copy(left.center), radius: left.radius };
|
||||
if (right.radius >= separation + left.radius)
|
||||
return { center: copy(right.center), radius: right.radius };
|
||||
// Halfway between the two tangent points, one on the far side of each.
|
||||
const half = (left.radius + separation + right.radius) * 0.5;
|
||||
const along = (-left.radius + half) / separation;
|
||||
return {
|
||||
center: {
|
||||
x: toRight.x * along + left.center.x,
|
||||
y: toRight.y * along + left.center.y,
|
||||
z: toRight.z * along + left.center.z,
|
||||
},
|
||||
radius: half,
|
||||
};
|
||||
}
|
||||
+30
-1
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict';
|
||||
import * as Cesium from 'cesium';
|
||||
import test from 'node:test';
|
||||
import { fromDegrees } from './ellipsoid.js';
|
||||
import { sphereFromPoints } from './sphere.js';
|
||||
import { sphereFromPoints, sphereUnion } from './sphere.js';
|
||||
|
||||
test('a sphere from points is the default renderer’s, to the bit', () => {
|
||||
let seed = 7;
|
||||
@@ -28,3 +28,32 @@ test('a sphere from points is the default renderer’s, to the bit', () => {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('the union of two spheres is the default renderer’s, to the bit', () => {
|
||||
const sphere = (x, y, z, radius) => ({ center: { x, y, z }, radius });
|
||||
const cases = [
|
||||
// Apart, overlapping, one inside the other either way, concentric.
|
||||
[sphere(0, 0, 0, 6378137), sphere(4.2e7, 1e6, -3e5, 2.1e7)],
|
||||
[sphere(1, 2, 3, 10), sphere(5, -4, 2, 8)],
|
||||
[sphere(0, 0, 0, 6378137), sphere(1e5, 0, 0, 7000)],
|
||||
[sphere(1e5, 0, 0, 7000), sphere(0, 0, 0, 6378137)],
|
||||
[sphere(3, 3, 3, 5), sphere(3, 3, 3, 5)],
|
||||
];
|
||||
for (const [left, right] of cases) {
|
||||
const ours = sphereUnion(left, right);
|
||||
const theirs = Cesium.BoundingSphere.union(
|
||||
new Cesium.BoundingSphere(
|
||||
new Cesium.Cartesian3(left.center.x, left.center.y, left.center.z),
|
||||
left.radius,
|
||||
),
|
||||
new Cesium.BoundingSphere(
|
||||
new Cesium.Cartesian3(right.center.x, right.center.y, right.center.z),
|
||||
right.radius,
|
||||
),
|
||||
);
|
||||
assert.deepEqual(
|
||||
[ours.center.x, ours.center.y, ours.center.z, ours.radius],
|
||||
[theirs.center.x, theirs.center.y, theirs.center.z, theirs.radius],
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user