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
maj00r 7ef4cf9aa1 fix(world): the remarks of Sonar on the geometry library: no shift by a level out of range, init-statements, a helper for a nested test
ancestor() returns no tile for a level out of range, and centre_direction() and corner_direction() nothing for
a tile that isn't valid, rather than shifting by a negative count or by 64 (S3949). Init-statements (S6004),
an upper-case literal suffix (S818), references in the loops of the tests (S6032), no redundant parentheses
(S1110), and the check of the neighbours of a plane tile in a helper of its own (S134). Behaviour unchanged for
valid tiles; the geometry tests pass.
2026-12-02 23:46:40 +01:00

392 lines
17 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "world/worldgeometry.h"
#include "world/worldtiles.h"
#include <algorithm>
#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