#include "stdafx.h" #include "doctest.h" #include "world/worldimport.h" #include #include // 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