/* 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/worldgeometry.h" #include #include #include #include "scene/sn_utils.h" namespace world_geometry { namespace { // WGS84: the semi-major axis and the flattening, as the standard defines them constexpr double wgs84_a{6'378'137.0}; constexpr double wgs84_flattening{1.0 / 298.257223563}; constexpr double wgs84_b{wgs84_a * (1.0 - wgs84_flattening)}; constexpr double micrometres_in_metre{static_cast(micrometres_per_metre)}; // enough for the latitude to settle to the last bit anywhere on the Earth; it settles in 3 to 4 steps constexpr int geodetic_iterations{12}; } // namespace std::int64_t micrometres(double const Metres) { return std::llround(Metres * micrometres_in_metre); } double metres(std::int64_t const Micrometres) { // an int64 below 2^53 converts exactly, and the division rounds correctly: the nearest double to the decimal value return static_cast(Micrometres) / micrometres_in_metre; } position from_metres(glm::dvec3 const &Metres) { return {micrometres(Metres.x), micrometres(Metres.y), micrometres(Metres.z)}; } glm::dvec3 to_metres(position const &Position) { return {metres(Position.x), metres(Position.y), metres(Position.z)}; } body body::earth() { return {body_kind::earth, wgs84_a, wgs84_b}; } body body::plane() { return {body_kind::plane, 0.0, 0.0}; } body body::fictional_planet() { return {body_kind::fictional_planet, wgs84_a, wgs84_b}; } body body::fictional_planet(double const Radius) { return {body_kind::fictional_planet, Radius, Radius}; } glm::dvec3 body::up(glm::dvec3 const &Point) const { if (!curved()) return {0.0, 1.0, 0.0}; auto const a2{a * a}; auto const b2{b * b}; glm::dvec3 const normal{Point.x / a2, Point.y / a2, Point.z / b2}; auto const length{std::sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z)}; return normal / length; } glm::dvec3 body::surface_along(glm::dvec3 const &Direction) const { auto const scale{1.0 / std::sqrt((Direction.x * Direction.x + Direction.y * Direction.y) / (a * a) + Direction.z * Direction.z / (b * b))}; return Direction * scale; } double body::eccentricity_squared() const { return 1.0 - (b * b) / (a * a); } void write(std::ostream &Output, body const &Body) { sn_utils::s_uint8(Output, static_cast(Body.kind)); sn_utils::ls_float64(Output, Body.a); sn_utils::ls_float64(Output, Body.b); } bool read(std::istream &Input, body &Body) { auto const kind{sn_utils::d_uint8(Input)}; auto const a{sn_utils::ld_float64(Input)}; auto const b{sn_utils::ld_float64(Input)}; if (!Input) return false; if (kind == static_cast(body_kind::plane)) { if (a != 0.0 || b != 0.0) return false; } else if (kind > static_cast(body_kind::plane) || !(b > 0.0) || b > a || !std::isfinite(a)) { return false; } Body = {static_cast(kind), a, b}; return true; } glm::dvec3 to_cartesian(body const &Body, geodetic const &Point) { auto const e2{Body.eccentricity_squared()}; auto const sine{std::sin(Point.latitude)}; auto const cosine{std::cos(Point.latitude)}; auto const n{Body.a / std::sqrt(1.0 - e2 * sine * sine)}; return { (n + Point.height) * cosine * std::cos(Point.longitude), (n + Point.height) * cosine * std::sin(Point.longitude), (n * (1.0 - e2) + Point.height) * sine}; } geodetic to_geodetic(body const &Body, glm::dvec3 const &Point) { auto const e2{Body.eccentricity_squared()}; auto const p{std::hypot(Point.x, Point.y)}; geodetic result; result.longitude = std::atan2(Point.y, Point.x); auto latitude{std::atan2(Point.z, p * (1.0 - e2))}; for (int step = 0; step < geodetic_iterations; ++step) { auto const sine{std::sin(latitude)}; auto const n{Body.a / std::sqrt(1.0 - e2 * sine * sine)}; latitude = std::atan2(Point.z + e2 * n * sine, p); } auto const sine{std::sin(latitude)}; auto const n{Body.a / std::sqrt(1.0 - e2 * sine * sine)}; result.latitude = latitude; // valid at the poles too, unlike p / cos(latitude) - n result.height = p * std::cos(latitude) + Point.z * sine - Body.a * Body.a / n; return result; } } // namespace world_geometry