18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 12:25:27 +01:00
Files
maszyna/world/worldgeometry.cpp
maj00r 24e21d083d feat(world): the geometry library of the new world format: positions in micrometres, the bodies, the tiles of a cube grid and their frames
Step 1 of the plan in docs/world-format-eu07.md (#516). Nothing in the game uses it yet, so nothing it does or
shows changes.

- world_geometry::position: an absolute position as int64 micrometres, exact differences; a value of metres
  with up to six decimals comes back as the same double.
- body: the Earth (WGS84), a fictional planet (WGS84 or a sphere) and the plane, the vertical as the normal
  of the ellipsoid with no trigonometry ((0, 1, 0) on the plane), geodetic coordinates for import, a record
  of 17 bytes.
- tile_id: the cube grid with S2's layout of faces, quadratic transform and Hilbert order; face, level and
  coordinates, parents and children, the four edge neighbours across the edges of the faces, the next tile.
- tile_frame: the anchor in micrometres and the rotation of the tile's axes (x west, y up, z north), made
  once when a world is compiled and written as a record of 64 bytes, so the game never computes it.

Tests: values of metres back and forth, the verticals, geodetic coordinates to a micrometre, the records
written and read back and refused when broken, ids and their coordinates, the neighbours of every tile of
levels 0, 1 and 4 having each other back, the Hilbert order through neighbours across the faces, the frame
of a tile, and a track around the equator of 72 000 segments closing to the micrometre through 32 768
neighbouring tiles of level 13.
2026-12-02 20:42:52 +01:00

141 lines
4.3 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 <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);
}
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) {
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<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;
}
Body = {static_cast<body_kind>(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