18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 13:35:26 +01:00
Files
maszyna/tests/unit/test_world_geometry.cpp
T
maj00r 1c65d70c5c feat(world): the local frame of the body at the camera, for the free camera and the sky
Step 4 of docs/world-format-eu07.md, its first part. world_geometry::local_frame gives the west, the vertical and the north at a
point of the body (exactly the identity on the plane). On a curved body the free camera turns the world into those axes before its
angles apply and moves in them. The scene block of both renderers gets world_frame, the frame at the camera of the pass, and the sky
dome, the cloud panorama and the horizon of the sun and the moon read their tables in the local axes through it.

The plane, the only body loaded today, turns nothing: the camera code skips it and world_frame is the identity.
2026-12-03 04:23:47 +01:00

441 lines
20 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "world/worldgeometry.h"
#include "world/worldtiles.h"
#include <algorithm>
#include <bit>
#include <cmath>
#include <numbers>
#include <set>
#include <sstream>
#include <string>
#include <vector>
// the geometry of the new world format (docs/world-format-eu07.md, sections 3 and 13, step 1): positions in micrometres, the bodies,
// the tiles of the cube grid and their frames written in the data
using namespace world_geometry;
namespace {
constexpr double degree{std::numbers::pi / 180.0};
// every tile of the level, face by face
std::vector<tile_id> all_tiles(int const Level) {
std::vector<tile_id> tiles;
auto const side{std::uint32_t{1} << Level};
for (int face = 0; face < face_count; ++face)
for (std::uint32_t i = 0; i < side; ++i)
for (std::uint32_t j = 0; j < side; ++j)
tiles.push_back(tile_id::from_face_ij(face, Level, i, j));
return tiles;
}
bool shares_edge(tile_id const A, tile_id const B) {
auto const neighbours{A.edge_neighbours()};
return std::ranges::find(neighbours, B) != neighbours.end();
}
// how many edges of a tile of the plane have no neighbour beyond them; the neighbours there are of the plane and have the tile back
int plane_edges_beyond(tile_id const Tile) {
int beyond{0};
for (auto const neighbour : Tile.edge_neighbours()) {
if (!neighbour.valid()) {
++beyond;
continue;
}
CHECK(neighbour.planar());
CHECK(shares_edge(neighbour, Tile));
}
return beyond;
}
// the angle between two directions, in radians
double angle(glm::dvec3 const &A, glm::dvec3 const &B) {
return std::atan2(glm::length(glm::cross(A, B)), glm::dot(A, B));
}
} // namespace
TEST_SUITE("world geometry") {
TEST_CASE("a decimal value of metres comes back the same through micrometres") {
// a coordinate of .scn with up to six decimal places converts back to the very same double
for (auto const text : {"123.4567", "-48211.25", "0.000001", "524287.999999", "-0.1", "6378137.0"}) {
auto const value{std::stod(text)};
CHECK(metres(micrometres(value)) == value);
}
CHECK(micrometres(0.001) == 1000);
CHECK(micrometres(-0.0000005) == -1); // halves away from zero
glm::dvec3 const point{6378137.123456, -12.5, 0.000003};
CHECK(to_metres(from_metres(point)) == point);
}
TEST_CASE("differences of positions are exact in integers, far from the centre too") {
position const a{6'378'137'000'000, 0, 1};
position const b{-6'378'137'000'000, 5, -1};
CHECK((a - b) + b == a);
CHECK((a - b).x == 12'756'274'000'000);
}
TEST_CASE("the vertical of the bodies") {
CHECK(body::plane().up({1000.0, -5.0, 3.0e6}) == glm::dvec3{0.0, 1.0, 0.0});
auto const earth{body::earth()};
CHECK(glm::length(earth.up({earth.a, 0.0, 0.0}) - glm::dvec3{1.0, 0.0, 0.0}) < 1e-15);
CHECK(glm::length(earth.up({0.0, 0.0, earth.b}) - glm::dvec3{0.0, 0.0, 1.0}) < 1e-15);
// on the ellipsoid the vertical is the geodetic one, not the direction from the centre
auto const at45{to_cartesian(earth, {45.0 * degree, 20.0 * degree, 0.0})};
auto const expected{glm::dvec3{std::cos(45.0 * degree) * std::cos(20.0 * degree), std::cos(45.0 * degree) * std::sin(20.0 * degree), std::sin(45.0 * degree)}};
CHECK(glm::length(earth.up(at45) - expected) < 1e-12);
auto const sphere{body::fictional_planet(1000.0)};
glm::dvec3 const point{300.0, -400.0, 500.0};
CHECK(glm::length(sphere.up(point) - glm::normalize(point)) < 1e-15);
}
TEST_CASE("geodetic coordinates go there and back to a micrometre") {
auto const earth{body::earth()};
for (auto const latitude : {-90.0, -54.3, 0.0, 0.0001, 52.2297, 89.9999, 90.0}) {
for (auto const height : {-430.0, 0.0, 8848.86, 100'000.0}) {
geodetic const point{latitude * degree, 21.0122 * degree, height};
auto const back{to_geodetic(earth, to_cartesian(earth, point))};
CHECK(std::abs(back.latitude - point.latitude) < 1e-12);
CHECK(std::abs(back.height - point.height) < 1e-6);
}
}
}
TEST_CASE("a body is written and read back, and a broken record doesn't read") {
for (auto const &value : {body::earth(), body::plane(), body::fictional_planet(), body::fictional_planet(6'371'000.0)}) {
std::stringstream stream;
write(stream, value);
CHECK(stream.str().size() == 17);
body back;
REQUIRE(read(stream, back));
CHECK(back == value);
}
std::stringstream broken;
write(broken, body{body_kind::earth, 1.0, 2.0}); // the polar axis longer than the equatorial
body back;
CHECK_FALSE(read(broken, back));
std::stringstream truncated{std::string(10, '\0')};
CHECK_FALSE(read(truncated, back));
}
TEST_CASE("a tile id gives back its face, level and coordinates") {
for (int level : {0, 1, 7, 13, 30}) {
auto const side{std::uint64_t{1} << level};
for (int face = 0; face < face_count; ++face) {
for (auto const i : {std::uint64_t{0}, side / 3, side - 1}) {
auto const j{(side - 1) - i / 2};
auto const tile{tile_id::from_face_ij(face, level, static_cast<std::uint32_t>(i), static_cast<std::uint32_t>(j))};
REQUIRE(tile.valid());
CHECK(tile.face() == face);
CHECK(tile.level() == level);
auto const cell{tile.to_face_ij()};
CHECK(cell.face == face);
CHECK(cell.i == i);
CHECK(cell.j == j);
CHECK(tile_id::from_raw(tile.raw()) == tile);
}
}
}
CHECK_FALSE(tile_id::from_face_ij(7, 3, 0, 0).valid());
CHECK(tile_id::from_face_ij(plane_face, 3, 0, 0).planar());
CHECK_FALSE(tile_id::from_face_ij(5, 3, 0, 0).planar());
CHECK_FALSE(tile_id::from_face_ij(0, 3, 8, 0).valid());
CHECK_FALSE(tile_id::from_raw(0).valid());
CHECK_FALSE(tile_id::from_raw(std::uint64_t{7} << 61 | 1).valid());
CHECK_FALSE(tile_id::from_raw(2).valid()); // the marker at an odd bit
}
TEST_CASE("parents and children") {
auto const tile{tile_id::from_face_ij(2, 13, 4100, 777)};
CHECK(tile.parent().level() == 12);
CHECK(tile.parent(10).level() == 10);
CHECK(tile.parent(10) == tile.parent().parent().parent());
CHECK(tile.parent(13) == tile);
for (auto const child : tile.children()) {
CHECK(child.level() == 14);
CHECK(child.parent() == tile);
}
auto const cell{tile.to_face_ij()};
CHECK(tile.parent(10).to_face_ij().i == cell.i >> 3);
CHECK(tile.parent(10).to_face_ij().j == cell.j >> 3);
}
TEST_CASE("the tile at the middle of a tile is the tile, and the corners surround it") {
for (auto const tile : all_tiles(4)) {
CHECK(tile_at(centre_direction(tile), 4) == tile);
auto const centre{centre_direction(tile)};
for (int corner = 0; corner < 4; ++corner) {
auto const towards{glm::normalize(centre + 0.999 * (corner_direction(tile, corner) - centre))};
CHECK(tile_at(towards, 4) == tile);
}
}
// at level 13, of about 1.2 km on the Earth
for (auto const &direction : {glm::dvec3{1.0, 0.2, 0.3}, glm::dvec3{-0.3, 0.1, -1.0}, glm::dvec3{0.6, 0.6, 0.53}}) {
auto const tile{tile_at(direction, surroundings_tile_level)};
CHECK(tile.level() == surroundings_tile_level);
CHECK(tile_at(centre_direction(tile), surroundings_tile_level) == tile);
CHECK(angle(centre_direction(tile), glm::normalize(direction)) < 1.5 * 1223.0 / 6'371'000.0);
}
}
TEST_CASE("each tile has four neighbours across the edges, across the edges of the faces too, and each one has it back") {
for (int level : {0, 1, 4}) {
for (auto const tile : all_tiles(level)) {
auto const neighbours{tile.edge_neighbours()};
std::set<tile_id> const distinct(neighbours.begin(), neighbours.end());
CHECK(distinct.size() == 4);
for (auto const neighbour : neighbours) {
REQUIRE(neighbour.valid());
CHECK(neighbour.level() == level);
CHECK(neighbour != tile);
CHECK(shares_edge(neighbour, tile));
}
}
}
// a tile on the edge of face 0 has a neighbour on another face
auto const edge{tile_id::from_face_ij(0, 5, 31, 10)};
auto const across{edge.edge_neighbours()[1]};
CHECK(across.face() != 0);
}
TEST_CASE("the Hilbert order runs through neighbouring tiles, across the faces too") {
for (int level : {1, 3, 5}) {
// the first tile of the level on face 0 starts the order
auto const first{tile_id::from_raw(std::uint64_t{1} << (2 * (max_tile_level - level)))};
auto const count{face_count * (std::size_t{1} << (2 * level))};
std::set<tile_id> seen;
auto current{first};
for (std::size_t k = 0; k + 1 < count; ++k) {
seen.insert(current);
auto const next{current.next_wrapped()};
CHECK(shares_edge(current, next));
current = next;
}
seen.insert(current);
CHECK(seen.size() == count);
CHECK(current.next_wrapped() == first);
}
}
TEST_CASE("the frame of a tile: the vertical, the north, and positions there and back") {
auto const earth{body::earth()};
auto const tile{tile_at(to_cartesian(earth, {52.2297 * degree, 21.0122 * degree, 0.0}), surroundings_tile_level)};
auto const frame{make_frame(earth, tile, 100.0)};
auto const anchor{to_metres(frame.anchor)};
// the anchor 100 m above the ellipsoid
CHECK(std::abs(to_geodetic(earth, anchor).height - 100.0) < 1e-6);
// the y axis of the tile is the vertical, the z axis points north, x to the west
auto const rotation{glm::mat3_cast(frame.orientation)};
auto const geo{to_geodetic(earth, anchor)};
glm::dvec3 const vertical{std::cos(geo.latitude) * std::cos(geo.longitude), std::cos(geo.latitude) * std::sin(geo.longitude), std::sin(geo.latitude)};
CHECK(glm::length(rotation[1] - vertical) < 1e-12);
// the vertical of the body at a point 100 m up is the normal of the ellipsoid scaled through it, off the geodetic one by about
// 5e-7 rad per kilometre of height
CHECK(glm::length(earth.up(anchor) - vertical) < 1e-7);
CHECK(rotation[2].z > 0.0);
auto const east{glm::normalize(glm::cross(glm::dvec3{0.0, 0.0, 1.0}, anchor))};
CHECK(glm::dot(rotation[0], east) < -0.999999999);
// a point 500 m east and 30 m up from the anchor
glm::dvec3 const local{-500.0, 30.0, 0.0};
auto const point{to_position(frame, local)};
CHECK(glm::length(to_local(frame, point) - local) < 1e-6);
// the curvature is in the data: the tangent plane departs from the ellipsoid by d²/2R, about 7.85 cm at 1 km
auto const kilometre{to_position(frame, {0.0, -100.0, 1000.0})};
auto const height{to_geodetic(earth, to_metres(kilometre)).height};
CHECK(height == doctest::Approx(0.0785).epsilon(0.02));
// the frames at the poles have a north too
for (auto const &direction : {glm::dvec3{0.0, 0.0, 1.0}, glm::dvec3{0.0, 0.0, -1.0}}) {
auto const polar{make_frame(earth, tile_at(direction, 0), 0.0)};
CHECK(std::abs(glm::length(polar.orientation) - 1.0) < 1e-12);
}
}
TEST_CASE("the tiles of the plane: 1024 m at level 13, edges every 1024 m from the zero, in integers") {
auto const metres_xz{[](double const X, double const Z) { return position{micrometres(X), 0, micrometres(Z)}; }};
auto const origin{plane_tile_at({}, surroundings_tile_level)};
REQUIRE(origin.valid());
CHECK(origin.planar());
CHECK(origin.level() == surroundings_tile_level);
CHECK(origin.to_face_ij().i == 4096);
CHECK(origin.to_face_ij().j == 4096);
CHECK(plane_tile_centre(origin) == metres_xz(512.0, 512.0));
// the edges every 1024 m: the last micrometre before one is still in the tile
CHECK(plane_tile_at(metres_xz(1023.999999, 0.0), surroundings_tile_level) == origin);
CHECK(plane_tile_at(metres_xz(1024.0, 0.0), surroundings_tile_level).to_face_ij().i == 4097);
CHECK(plane_tile_at(metres_xz(-0.000001, 0.0), surroundings_tile_level).to_face_ij().i == 4095);
CHECK(plane_tile_at(metres_xz(-131'072.0, 250'000.0), surroundings_tile_level).to_face_ij().i == 4096 - 128);
// a region of the logic is 8192 m
auto const region{plane_tile_at(metres_xz(8191.999999, 8191.999999), logic_region_level)};
CHECK(region == origin.parent(logic_region_level));
CHECK(plane_tile_centre(region) == metres_xz(4096.0, 4096.0));
// y doesn't count; the square ends at ±4194.304 km
CHECK(plane_tile_at({0, 2'000'000'000, 0}, surroundings_tile_level) == origin);
CHECK(plane_tile_at(metres_xz(-4'194'304.0, 0.0), surroundings_tile_level).to_face_ij().i == 0);
CHECK_FALSE(plane_tile_at(metres_xz(4'194'304.0, 0.0), surroundings_tile_level).valid());
CHECK_FALSE(plane_tile_at(metres_xz(0.0, -4'194'304.000001), surroundings_tile_level).valid());
// the middle of every tile of a level is in that tile
for (auto const tile : {tile_id::from_face_ij(plane_face, 13, 0, 8191), tile_id::from_face_ij(plane_face, 28, 12345, 99), tile_id::from_face_ij(plane_face, 2, 3, 1)})
CHECK(plane_tile_at(plane_tile_centre(tile), tile.level()) == tile);
}
TEST_CASE("the tiles of the plane have neighbours within the square, in Hilbert order, and frames with no rotation") {
auto const side{std::uint32_t{1} << 4};
for (std::uint32_t i = 0; i < side; ++i) {
for (std::uint32_t j = 0; j < side; ++j) {
auto const tile{tile_id::from_face_ij(plane_face, 4, i, j)};
auto const edges{static_cast<int>(i == 0) + static_cast<int>(j == 0) + static_cast<int>(i + 1 == side) + static_cast<int>(j + 1 == side)};
CHECK(plane_edges_beyond(tile) == edges);
}
}
// the Hilbert order runs through neighbours and comes back to the first tile of the plane, not onto the cube
auto const first{tile_id::from_raw((std::uint64_t{plane_face} << 61) | (std::uint64_t{1} << (2 * (max_tile_level - 4))))};
REQUIRE(first.planar());
auto current{first};
for (std::uint32_t k = 0; k + 1 < side * side; ++k) {
auto const next{current.next_wrapped()};
CHECK(shares_edge(current, next));
current = next;
}
CHECK(current.next_wrapped() == first);
// nor does the cube run onto the plane
auto const last_of_cube{tile_id::from_raw((std::uint64_t{plane_face} << 61) - (std::uint64_t{1} << (2 * (max_tile_level - 4))))};
REQUIRE(last_of_cube.face() == 5);
CHECK(last_of_cube.next_wrapped().face() == 0);
// the frame: the anchor in the middle at the height, no rotation, positions there and back exact
auto const tile{plane_tile_at({micrometres(-1500.25), 0, micrometres(700.5)}, surroundings_tile_level)};
auto const frame{make_frame(body::plane(), tile, 212.5)};
CHECK(frame.tile == tile);
CHECK(frame.anchor == position{micrometres(-1536.0), micrometres(212.5), micrometres(512.0)});
CHECK(frame.orientation == glm::dquat{1.0, 0.0, 0.0, 0.0});
position const point{micrometres(-1500.25), micrometres(230.0), micrometres(700.5)};
CHECK(to_local(frame, point) == to_metres(point - frame.anchor));
CHECK(to_position(frame, to_local(frame, point)) == point);
// a tile of the cube has no frame on the plane, nor a tile of the plane on the Earth
CHECK_FALSE(make_frame(body::plane(), tile_at({1.0, 0.0, 0.0}, 13)).tile.valid());
CHECK_FALSE(make_frame(body::earth(), tile).tile.valid());
}
TEST_CASE("a frame is written and read back bit for bit, and a broken record doesn't read") {
auto const earth{body::earth()};
auto const frame{make_frame(earth, tile_at({0.3, -0.8, 0.5}, surroundings_tile_level), 1234.5)};
std::stringstream stream;
write(stream, frame);
CHECK(stream.str().size() == tile_frame_record_size);
tile_frame back;
REQUIRE(read(stream, back));
CHECK(back == frame);
// no tile
auto bytes{stream.str()};
std::fill_n(bytes.begin(), 8, '\0');
std::stringstream broken{bytes};
tile_frame other;
CHECK_FALSE(read(broken, other));
// a quaternion that isn't of unit length
auto scaled{frame};
scaled.orientation = scaled.orientation * 2.0;
std::stringstream stretched;
write(stretched, scaled);
CHECK_FALSE(read(stretched, other));
std::stringstream truncated{stream.str().substr(0, 40)};
CHECK_FALSE(read(truncated, other));
}
TEST_CASE("a track around the equator closes to the micrometre and runs through neighbouring tiles") {
auto const earth{body::earth()};
// nodes every 0.005 degree, about 557 m apart, less than the smallest tile of level 13 along the equator (about 1.04 km): 72 000
// segments, 40 075 km
constexpr int segments{72'000};
std::vector<position> nodes;
nodes.reserve(segments);
for (int k = 0; k < segments; ++k) {
auto const longitude{(k * 360.0 / segments - 180.0) * degree};
nodes.push_back(from_metres(to_cartesian(earth, {0.0, longitude, 0.0})));
}
position sum;
double length{0.0};
auto previous_tile{tile_at(to_metres(nodes.front()) + glm::dvec3{0.0, 0.0, 1.0}, surroundings_tile_level)};
auto const first_tile{previous_tile};
std::set<tile_id> tiles{first_tile};
for (int k = 0; k < segments; ++k) {
// the last segment ends in the node the first one starts from
auto const &from{nodes[k]};
auto const &to{nodes[(k + 1) % segments]};
auto const step{to - from};
sum = sum + step;
length += glm::length(to_metres(step));
// the nodes stay on the equator and on the ellipsoid
auto const geo{to_geodetic(earth, to_metres(to))};
CHECK(std::abs(geo.height) < 1e-6);
CHECK(std::abs(geo.latitude) < 1e-12);
// the track goes from tile to tile through shared edges. the equator is itself an edge of tiles, and the faces with swapped
// axes put a point on it into the tile south of it, the others into the one north of it, so the tile is taken a metre north
auto const tile{tile_at(to_metres(to) + glm::dvec3{0.0, 0.0, 1.0}, surroundings_tile_level)};
if (tile != previous_tile) {
CHECK(shares_edge(previous_tile, tile));
tiles.insert(tile);
}
previous_tile = tile;
}
CHECK(sum == position{});
CHECK(previous_tile == first_tile);
// a polygon of 72 000 chords is shorter than the equator by about 2πa·(π/72000)²/6, about 1.3 cm
auto const circumference{2.0 * std::numbers::pi * earth.a};
CHECK(circumference - length > 0.0);
CHECK(circumference - length < 0.02);
// 4 faces of 8192 tiles each
CHECK(tiles.size() == 4 * 8192);
}
} // TEST_SUITE
TEST_SUITE("world geometry") {
TEST_CASE("the gradient of a track: on the plane exactly -front.y, on a curved body against the vertical at the vehicle") {
// the plane, as the physics has always had it, bit for bit
for (auto const &front : {glm::dvec3{0.6, -0.008, 0.8}, glm::dvec3{-0.0, 0.0, 1.0}, glm::dvec3{0.1, 0.0123456789, -0.99}}) {
auto const gradient{track_gradient(body::plane(), front, {1234.5, 6.0, -789.0})};
CHECK(std::bit_cast<std::uint64_t>(gradient) == std::bit_cast<std::uint64_t>(-front.y));
}
CHECK(current_body() == body::plane());
// on the Earth at the equator, where the vertical is +x: a vehicle running north and climbing by 1 per mille
auto const earth{body::earth()};
glm::dvec3 const at{earth.a, 0.0, 0.0};
auto const climbing{glm::normalize(glm::dvec3{0.001, 0.0, 1.0})};
CHECK(track_gradient(earth, climbing, at) == doctest::Approx(-0.001 / std::sqrt(1.000001)));
// level against the local vertical 1 km north: the vertical there has turned by 1000 / R
auto const north{to_cartesian(earth, {1000.0 / 6'335'439.0, 0.0, 0.0})};
auto const tangent{glm::normalize(glm::cross(earth.up(north), glm::dvec3{0.0, 1.0, 0.0}))};
CHECK(std::abs(track_gradient(earth, tangent, north)) < 1e-12);
// the same direction against the vertical of the start would show a gradient of about 0.157 per mille
CHECK(std::abs(-glm::dot(tangent, earth.up(at))) == doctest::Approx(1000.0 / 6'335'439.0).epsilon(1e-3));
}
TEST_CASE("the local frame: the identity on the plane, west, up and north on a curved body") {
auto const plane{local_frame(body::plane(), {1234.5, 6.0, -789.0})};
CHECK(plane == glm::dmat3{1.0});
auto const earth{body::earth()};
// at latitude 0, longitude 0 the vertical is +x, the east +y and the north +z of the body
auto const equator{local_frame(earth, {earth.a, 0.0, 0.0})};
auto const west{equator * glm::dvec3{0.0, -1.0, 0.0}};
CHECK(west.x == doctest::Approx(1.0));
auto const up{equator * glm::dvec3{1.0, 0.0, 0.0}};
CHECK(up.y == doctest::Approx(1.0));
auto const north{equator * glm::dvec3{0.0, 0.0, 1.0}};
CHECK(north.z == doctest::Approx(1.0));
// anywhere: orthonormal, right-handed as the axes of .scn, and the vertical in its middle row
for (auto const &point : {geodetic{0.9, 0.37, 120.0}, geodetic{-0.4, -2.5, 0.0}, geodetic{1.5707963267948966, 0.0, 0.0}}) {
auto const at{to_cartesian(earth, point)};
auto const frame{local_frame(earth, at)};
auto const rows{glm::transpose(frame)};
CHECK(glm::determinant(frame) == doctest::Approx(1.0));
CHECK(glm::dot(rows[0], rows[2]) == doctest::Approx(0.0).epsilon(1e-12));
CHECK(glm::length(rows[1] - earth.up(at)) < 1e-12);
CHECK(glm::dot(glm::cross(rows[0], rows[1]), rows[2]) == doctest::Approx(1.0));
}
}
} // TEST_SUITE