diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f8d2318d..0e9a8d06f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -883,6 +883,8 @@ set(CORE_SOURCES # the geometry of the new world format: positions in micrometres, the bodies, the tiles of the cube grid and their frames "world/worldgeometry.cpp" "world/worldtiles.cpp" +# and the import of a flat scenery onto a curved body +"world/worldimport.cpp" # and its cache files: the header, the name table, the directory of tiles and the tile blocks "world/worldcache.cpp" # and the cache of the logic of a scenery: the tape of its parser, replayed while it is current diff --git a/tests/unit/test_world_import.cpp b/tests/unit/test_world_import.cpp new file mode 100644 index 000000000..727b908df --- /dev/null +++ b/tests/unit/test_world_import.cpp @@ -0,0 +1,114 @@ +#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 diff --git a/world/worldimport.cpp b/world/worldimport.cpp new file mode 100644 index 000000000..8052dcf79 --- /dev/null +++ b/world/worldimport.cpp @@ -0,0 +1,93 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#include "stdafx.h" +#include "world/worldimport.h" + +#include + +namespace world_geometry { + +namespace { + +// steps of the iteration of Vincenty's formula; it settles to the last bit in a few for any distance a scenery has +constexpr int vincenty_iterations{20}; + +struct geodesic_point { + double latitude{0.0}; + double longitude{0.0}; +}; + +// the point at the distance along the geodesic of the body leaving the start in the azimuth: Vincenty's direct formula (1975), to a +// fraction of a millimetre at any distance short of the antipodes; on a sphere the great circle +geodesic_point geodesic_direct(body const &Body, double const Latitude, double const Longitude, double const Azimuth, double const Distance) { + auto const flattening{1.0 - Body.b / Body.a}; + auto const reduced{std::atan((1.0 - flattening) * std::tan(Latitude))}; + auto const sinu{std::sin(reduced)}; + auto const cosu{std::cos(reduced)}; + auto const sina1{std::sin(Azimuth)}; + auto const cosa1{std::cos(Azimuth)}; + auto const sigma1{std::atan2(std::tan(reduced), cosa1)}; + auto const sinalpha{cosu * sina1}; + auto const cos2alpha{1.0 - sinalpha * sinalpha}; + auto const u2{cos2alpha * (Body.a * Body.a - Body.b * Body.b) / (Body.b * Body.b)}; + auto const a{1.0 + u2 / 16384.0 * (4096.0 + u2 * (-768.0 + u2 * (320.0 - 175.0 * u2)))}; + auto const b{u2 / 1024.0 * (256.0 + u2 * (-128.0 + u2 * (74.0 - 47.0 * u2)))}; + auto sigma{Distance / (Body.b * a)}; + double cos2sigmam{0.0}; + for (int step = 0; step < vincenty_iterations; ++step) { + cos2sigmam = std::cos(2.0 * sigma1 + sigma); + auto const sinsigma{std::sin(sigma)}; + auto const cossigma{std::cos(sigma)}; + auto const deltasigma{b * sinsigma + * (cos2sigmam + b / 4.0 * (cossigma * (-1.0 + 2.0 * cos2sigmam * cos2sigmam) - b / 6.0 * cos2sigmam * (-3.0 + 4.0 * sinsigma * sinsigma) * (-3.0 + 4.0 * cos2sigmam * cos2sigmam)))}; + auto const next{Distance / (Body.b * a) + deltasigma}; + auto const settled{std::abs(next - sigma) < 1e-14}; + sigma = next; + if (settled) + break; + } + cos2sigmam = std::cos(2.0 * sigma1 + sigma); + auto const sinsigma{std::sin(sigma)}; + auto const cossigma{std::cos(sigma)}; + auto const across{sinu * sinsigma - cosu * cossigma * cosa1}; + geodesic_point result; + result.latitude = std::atan2(sinu * cossigma + cosu * sinsigma * cosa1, (1.0 - flattening) * std::sqrt(sinalpha * sinalpha + across * across)); + auto const lambda{std::atan2(sinsigma * sina1, cosu * cossigma - sinu * sinsigma * cosa1)}; + auto const c{flattening / 16.0 * cos2alpha * (4.0 + flattening * (4.0 - 3.0 * cos2alpha))}; + auto const l{lambda - (1.0 - c) * flattening * sinalpha * (sigma + c * sinsigma * (cos2sigmam + c * cossigma * (-1.0 + 2.0 * cos2sigmam * cos2sigmam)))}; + result.longitude = Longitude + l; + return result; +} + +} // namespace + +position place_on_body(body const &Body, placement const &Placement, glm::dvec3 const &Local) { + // the distance from the zero along the surface, and the bearing of the point clockwise from the north of the body; east is -x + auto const distance{std::hypot(Local.x, Local.z)}; + geodesic_point point{Placement.latitude, Placement.longitude}; + if (distance > 0.0) { + auto const bearing{std::atan2(-Local.x, Local.z) + Placement.azimuth}; + point = geodesic_direct(Body, Placement.latitude, Placement.longitude, bearing, distance); + } + return from_metres(to_cartesian(Body, {point.latitude, point.longitude, Placement.height + Local.y})); +} + +placement eu07_424_anchor() { + return {}; +} + +glm::dvec3 eu07_424_grid_point(double const EastKm, double const NorthKm, glm::dvec3 const &Local) { + // the zero of the scenery lies east and north of the anchor by the difference of the grid points; x of .scn grows to the west + auto const east{(EastKm - eu07_424_anchor_east) * 1000.0}; + auto const north{(NorthKm - eu07_424_anchor_north) * 1000.0}; + return {Local.x - east, Local.y, Local.z + north}; +} + +} // namespace world_geometry diff --git a/world/worldimport.h b/world/worldimport.h new file mode 100644 index 000000000..91145ba5a --- /dev/null +++ b/world/worldimport.h @@ -0,0 +1,45 @@ +/* +This Source Code Form is subject to the +terms of the Mozilla Public License, v. +2.0. If a copy of the MPL was not +distributed with this file, You can +obtain one at +http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include + +#include "world/worldgeometry.h" + +// putting a flat scenery onto a curved body (docs/world-format-eu07.md, section 4, step 6 of section 13): an explicit import, not the +// default path; an old .scn read directly stays on the plane body. the azimuthal equidistant projection around the zero of the scenery: +// the distance and the direction from the zero are kept exactly, the lengths across grow on the sphere by about (rho / R)^2 / 6, under +// 2 cm per kilometre for a scenery of 127 km across. heights and the vertical stay: the plane bends over the body +namespace world_geometry { + +// where a flat scenery goes on a curved body: the point of its zero (latitude and longitude in radians, height above the body in +// metres) and the azimuth of its z axis, its north, in radians clockwise from the north of the body +struct placement { + double latitude{0.0}; + double longitude{0.0}; + double azimuth{0.0}; + double height{0.0}; +}; + +// the point of the body for a point of the flat scenery, in the axes of .scn (x to the west, y up, z to the north), by the azimuthal +// equidistant projection around the placement: the point at the distance of the local one from the zero, along the geodesic of the +// body that leaves the zero in its direction (Vincenty's direct formula; on a sphere the great circle), the height along the vertical +position place_on_body(body const &Body, placement const &Placement, glm::dvec3 const &Local); + +// the grid EU07-424 of the fictional sceneries (the //$g EU07-424 lines, kilometres) on the shared fictional planet: +// the whole grid is one projection around its anchor, the point of latitude 0 and longitude 0, where the grid point of 500 km east +// and 500 km north lies, with the north of the grid the north of the planet. so the sceneries of the grid join each other exactly +inline constexpr double eu07_424_anchor_east{500.0}; +inline constexpr double eu07_424_anchor_north{500.0}; +placement eu07_424_anchor(); +// a point of a scenery whose zero lies at the grid point given in kilometres, in the axes of .scn around the anchor of the grid +glm::dvec3 eu07_424_grid_point(double EastKm, double NorthKm, glm::dvec3 const &Local); + +} // namespace world_geometry