/* 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