18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00
Files
maszyna/world/worldimport.cpp
T
maj00r 9531aaf226 feat(world): putting a flat scenery onto a curved body, and the place of the grid EU07-424 on the fictional planet
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.
2026-12-03 02:36:38 +01:00

94 lines
4.0 KiB
C++

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