mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-04 23:45:17 +01:00
0d615c7127
- xyz_to_face takes the later axis on equal components, as S2's LargestAbsComponent does, so a direction on a face edge gets S2's id. - centre_direction and corner_direction give zero for a tile of the plane, plane_tile_centre the zero position for a tile of the cube. - to_cartesian and to_geodetic give zero on the plane; up() at the very centre gives the polar axis; fictional_planet asserts a positive radius. - A body record of the Earth with other axes than WGS84's doesn't read, nor a frame of the plane with a rotation or its anchor outside its tile. - checked_micrometres() for the importers: none for NaN, infinities and values that don't fit in an int64. - The header says the ids are S2's for the geocentric direction, and that a geodetic latitude goes through to_cartesian() first. - GCC and Clang build with -ffp-contract=off, so no multiply and add are fused where the target has FMA, and the results stay MSVC's.
161 lines
4.9 KiB
C++
161 lines
4.9 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/worldgeometry.h"
|
|
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <istream>
|
|
#include <ostream>
|
|
|
|
#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<double>(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);
|
|
}
|
|
|
|
std::optional<std::int64_t> checked_micrometres(double const Metres) {
|
|
// within ±9.2·10¹² m, so that the product and its rounding stay inside an int64
|
|
constexpr double limit{9.2e12};
|
|
if (!std::isfinite(Metres) || std::abs(Metres) > limit)
|
|
return std::nullopt;
|
|
return micrometres(Metres);
|
|
}
|
|
|
|
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<double>(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) {
|
|
assert(Radius > 0.0 && std::isfinite(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)};
|
|
if (!(length > 0.0))
|
|
return {0.0, 0.0, 1.0};
|
|
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<std::uint8_t>(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<std::uint8_t>(body_kind::plane)) {
|
|
if (a != 0.0 || b != 0.0)
|
|
return false;
|
|
}
|
|
else if (kind > static_cast<std::uint8_t>(body_kind::plane) || !(b > 0.0) || b > a || !std::isfinite(a)) {
|
|
return false;
|
|
}
|
|
else if (kind == static_cast<std::uint8_t>(body_kind::earth) && (a != wgs84_a || b != wgs84_b)) {
|
|
// the kind says WGS84, so other axes are a broken record
|
|
return false;
|
|
}
|
|
Body = {static_cast<body_kind>(kind), a, b};
|
|
return true;
|
|
}
|
|
|
|
glm::dvec3 to_cartesian(body const &Body, geodetic const &Point) {
|
|
if (!Body.curved())
|
|
return {};
|
|
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) {
|
|
if (!Body.curved())
|
|
return {};
|
|
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
|