mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 06:35:21 +01:00
9531aaf226
The core of step 6 of docs/world-format-eu07.md (sections 4 and 13): the azimuthal equidistant projection around the zero of a scenery. place_on_body() takes a point in the axes of .scn and puts it at its distance from the zero along the geodesic of the body leaving the zero in its direction (Vincenty's direct formula; on a sphere the great circle), its height along the vertical. A sphere of the mean radius of curvature would have made the lengths across the meridians wrong by about 1.3 m per kilometre on the Earth, so the geodesics of the ellipsoid. The grid EU07-424 of the fictional sceneries is one projection around the point of latitude 0 and longitude 0 of the fictional planet, where its grid point of 500 km east and 500 km north lies (eu07_424_anchor, eu07_424_grid_point), so the sceneries of the grid join each other exactly. Nothing in the game uses it yet. Tests: the distance from the zero kept to the micrometre on a sphere in every direction out to 130 km; the lengths across shorter by sin(delta) / delta, (rho / R)^2 / 6, under 2 cm/km at 65 km; the azimuth of the scenery's north and the heights along the vertical; on the Earth 65 km east and north kept to a millimetre, the north against the meridian arc computed apart; a point of the grid seen from two sceneries landing on the same micrometre.
115 lines
5.3 KiB
C++
115 lines
5.3 KiB
C++
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
#include "world/worldimport.h"
|
|
|
|
#include <cmath>
|
|
#include <numbers>
|
|
|
|
// putting a flat scenery onto a curved body (docs/world-format-eu07.md, section 4, step 6): the azimuthal equidistant projection around
|
|
// the zero of the scenery, and the shared place of the grid EU07-424 on the fictional planet
|
|
using namespace world_geometry;
|
|
|
|
namespace {
|
|
|
|
constexpr double degree{std::numbers::pi / 180.0};
|
|
constexpr double radius{6'371'000.0};
|
|
|
|
// the distance along a great circle of the sphere between two points of it
|
|
double arc(position const &A, position const &B) {
|
|
auto const chord{glm::length(to_metres(A) - to_metres(B))};
|
|
return 2.0 * radius * std::asin(chord / (2.0 * radius));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_SUITE("world import") {
|
|
|
|
TEST_CASE("the distance from the zero is kept exactly, in every direction") {
|
|
auto const planet{body::fictional_planet(radius)};
|
|
placement const at{52.0 * degree, 21.0 * degree, 0.0, 0.0};
|
|
auto const zero{place_on_body(planet, at, {0.0, 0.0, 0.0})};
|
|
for (auto const rho : {1.0, 1000.0, 65'000.0, 130'000.0}) {
|
|
for (auto const turn : {0.0, 0.7, 2.0, -2.9}) {
|
|
glm::dvec3 const local{-rho * std::sin(turn), 0.0, rho * std::cos(turn)};
|
|
// to the micrometre the positions are rounded to
|
|
CHECK(std::abs(arc(zero, place_on_body(planet, at, local)) - rho) < 3e-6);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("lengths across grow as the projection says: about (rho / R)^2 / 6, under 2 cm/km at 65 km") {
|
|
auto const planet{body::fictional_planet(radius)};
|
|
placement const at{10.0 * degree, 0.0, 0.0, 0.0};
|
|
for (auto const rho : {65'000.0, 130'000.0}) {
|
|
constexpr double step{1e-4};
|
|
glm::dvec3 const a{-rho * std::sin(0.3), 0.0, rho * std::cos(0.3)};
|
|
glm::dvec3 const b{-rho * std::sin(0.3 + step), 0.0, rho * std::cos(0.3 + step)};
|
|
auto const flat{rho * step};
|
|
auto const curved{arc(place_on_body(planet, at, a), place_on_body(planet, at, b))};
|
|
auto const delta{rho / radius};
|
|
// on the sphere the circle of radius rho is shorter by sin(delta) / delta
|
|
CHECK(curved / flat == doctest::Approx(std::sin(delta) / delta).epsilon(1e-6));
|
|
CHECK(1.0 - curved / flat == doctest::Approx(delta * delta / 6.0).epsilon(2e-2));
|
|
}
|
|
CHECK((65'000.0 / radius) * (65'000.0 / radius) / 6.0 * 1000.0 < 0.02);
|
|
}
|
|
|
|
TEST_CASE("the north of the scenery goes where the azimuth points, and heights go along the vertical") {
|
|
auto const planet{body::fictional_planet(radius)};
|
|
placement const turned{0.0, 0.0, 90.0 * degree, 0.0};
|
|
// 1 km along z of a scenery turned by 90 degrees goes east: along +y of the body at the point of latitude 0 and longitude 0
|
|
auto const east{to_metres(place_on_body(planet, turned, {0.0, 0.0, 1000.0}))};
|
|
CHECK(east.y > 999.0);
|
|
CHECK(std::abs(east.z) < 1e-6);
|
|
placement const raised{30.0 * degree, 40.0 * degree, 0.0, 200.0};
|
|
auto const point{place_on_body(planet, raised, {300.0, 25.0, -400.0})};
|
|
CHECK(glm::length(to_metres(point)) == doctest::Approx(radius + 225.0).epsilon(1e-12));
|
|
// on the Earth too, the height above the ellipsoid
|
|
auto const earth{body::earth()};
|
|
auto const high{place_on_body(earth, raised, {300.0, 25.0, -400.0})};
|
|
CHECK(to_geodetic(earth, to_metres(high)).height == doctest::Approx(225.0).epsilon(1e-6));
|
|
}
|
|
|
|
TEST_CASE("on the Earth the distance from the zero is kept along the geodesic, east and north alike") {
|
|
auto const earth{body::earth()};
|
|
placement const at{51.3 * degree, 20.1 * degree, 0.0, 0.0};
|
|
auto previous{place_on_body(earth, at, {0.0, 0.0, 0.0})};
|
|
double length{0.0};
|
|
// 65 km east in steps of 100 m: the chords add up to the arc
|
|
for (int k = 1; k <= 650; ++k) {
|
|
auto const next{place_on_body(earth, at, {-100.0 * k, 0.0, 0.0})};
|
|
length += glm::length(to_metres(next - previous));
|
|
previous = next;
|
|
}
|
|
CHECK(std::abs(length - 65'000.0) < 1e-3);
|
|
// and north: the meridian arc from the latitude of the zero, against the length of the meridian computed apart
|
|
auto const north{place_on_body(earth, at, {0.0, 0.0, 65'000.0})};
|
|
auto const reached{to_geodetic(earth, to_metres(north)).latitude};
|
|
double meridian{0.0};
|
|
constexpr int pieces{10'000};
|
|
auto const e2{earth.eccentricity_squared()};
|
|
for (int k = 0; k < pieces; ++k) {
|
|
auto const phi{at.latitude + (k + 0.5) * (reached - at.latitude) / pieces};
|
|
auto const w{1.0 - e2 * std::sin(phi) * std::sin(phi)};
|
|
meridian += earth.a * (1.0 - e2) / (w * std::sqrt(w)) * (reached - at.latitude) / pieces;
|
|
}
|
|
CHECK(std::abs(meridian - 65'000.0) < 1e-3);
|
|
}
|
|
|
|
TEST_CASE("the sceneries of the grid EU07-424 join each other exactly on the fictional planet") {
|
|
auto const planet{body::fictional_planet()};
|
|
auto const anchor{eu07_424_anchor()};
|
|
// the same point of the grid, 3 km east and 2 km north of the zero of Drawinowo (514, 441), as seen from a scenery at (459, 503)
|
|
glm::dvec3 const in_drawinowo{-3000.0, 12.5, 2000.0};
|
|
glm::dvec3 const in_other{-(514.0 - 459.0) * 1000.0 - 3000.0, 12.5, (441.0 - 503.0) * 1000.0 + 2000.0};
|
|
auto const a{place_on_body(planet, anchor, eu07_424_grid_point(514.0, 441.0, in_drawinowo))};
|
|
auto const b{place_on_body(planet, anchor, eu07_424_grid_point(459.0, 503.0, in_other))};
|
|
CHECK(a == b);
|
|
// the anchor of the grid is the grid point (500, 500), at latitude 0 and longitude 0
|
|
auto const zero{place_on_body(planet, anchor, eu07_424_grid_point(500.0, 500.0, {}))};
|
|
CHECK(to_geodetic(planet, to_metres(zero)).latitude == doctest::Approx(0.0));
|
|
CHECK(to_metres(zero).x == doctest::Approx(planet.a).epsilon(1e-12));
|
|
}
|
|
|
|
} // TEST_SUITE
|