18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

feat(world): the tiles of the plane body, on the face value the cube leaves free

The plane body has no cube, so its tiles take face 6 of the id: a flat square of 8192 x 1024 m with its middle
at the zero of the .scn system, split by the same quadtree in the same Hilbert order. A tile of level 13 is
exactly 1024 m, a region of the logic (level 10) 8192 m, and the edges of tiles lie every 1024 m from the zero;
the square reaches +-4194 km. The tile of a position is computed in integers (a leaf is 7.8125 mm), a tile at
the edge of the square has no neighbour beyond it, the Hilbert order stays within the plane (and no longer runs
from the last face of the cube onto it), and the frame of a plane tile has its anchor in the middle at the
height and no rotation. So the directory, the order in the files, the regions and the neighbours are the same
code for all bodies (docs/world-format-eu07.md, section 3).

The comment on the frames at the poles said the opposite of what the code does; it now says it: z points as
the north just short of the pole on the prime meridian.
This commit is contained in:
maj00r
2026-12-02 21:05:15 +01:00
parent 24e21d083d
commit c1e32ad2aa
3 changed files with 167 additions and 16 deletions
+77 -1
View File
@@ -124,7 +124,9 @@ TEST_CASE("a tile id gives back its face, level and coordinates") {
}
}
}
CHECK_FALSE(tile_id::from_face_ij(6, 3, 0, 0).valid());
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());
@@ -236,6 +238,80 @@ TEST_CASE("the frame of a tile: the vertical, the north, and positions there and
}
}
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)};
int beyond{0};
for (auto const neighbour : tile.edge_neighbours()) {
if (!neighbour.valid()) {
++beyond;
continue;
}
CHECK(neighbour.planar());
CHECK(shares_edge(neighbour, tile));
}
CHECK(beyond == 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)};
+61 -5
View File
@@ -163,6 +163,19 @@ glm::dvec3 face_st_direction(int const Face, double const S, double const T) {
constexpr double unit_tolerance{1e-12};
// the middle of the square of the plane lies at the zero of .scn
constexpr std::int64_t plane_half{plane_side_micrometres / 2};
// a leaf of the square in micrometres is 15625 / 2: the side, 2^29 · 15625 µm, over 2^30 leaves
constexpr std::int64_t plane_leaf_numerator{15'625};
// the leaf coordinate of a coordinate of the plane in micrometres, or -1 outside the square
std::int64_t plane_leaf(std::int64_t const Micrometres) {
auto const offset{Micrometres + plane_half};
if (offset < 0 || offset >= plane_side_micrometres)
return -1;
return (2 * offset) / plane_leaf_numerator;
}
} // namespace
tile_id tile_id::from_raw(std::uint64_t const Raw) {
@@ -171,7 +184,7 @@ tile_id tile_id::from_raw(std::uint64_t const Raw) {
}
tile_id tile_id::from_face_ij(int const Face, int const Level, std::uint32_t const I, std::uint32_t const J) {
if (Face < 0 || Face >= face_count || Level < 0 || Level > max_tile_level)
if (Face < 0 || Face > plane_face || Level < 0 || Level > max_tile_level)
return {};
auto const side{std::uint64_t{1} << Level};
if (I >= side || J >= side)
@@ -181,7 +194,7 @@ tile_id tile_id::from_face_ij(int const Face, int const Level, std::uint32_t con
}
bool tile_id::valid() const {
if (m_id == 0 || (m_id >> face_shift) >= static_cast<std::uint64_t>(face_count))
if (m_id == 0 || (m_id >> face_shift) > static_cast<std::uint64_t>(plane_face))
return false;
// the marker bit sits at an even distance from the bottom
return (lowest_bit() & 0x1555'5555'5555'5555ull) != 0;
@@ -239,6 +252,15 @@ std::array<tile_id, 4> tile_id::edge_neighbours() const {
auto const size{std::int64_t{1} << (max_tile_level - levels)};
auto const i{static_cast<std::int64_t>(cell.i) * size};
auto const j{static_cast<std::int64_t>(cell.j) * size};
if (planar()) {
// the plane doesn't wrap: no tile beyond the edge of the square
auto const at{[levels](std::int64_t const I, std::int64_t const J) {
if (I < 0 || J < 0 || I >= leaf_size || J >= leaf_size)
return tile_id{};
return tile_id::from_raw(ancestor(leaf_id(plane_face, static_cast<std::uint32_t>(I), static_cast<std::uint32_t>(J)), levels));
}};
return {at(i, j - size), at(i + size, j), at(i, j + size), at(i - size, j)};
}
return {
tile_id{wrapped_tile(cell.face, i, j - size, levels)},
tile_id{wrapped_tile(cell.face, i + size, j, levels)},
@@ -248,9 +270,16 @@ std::array<tile_id, 4> tile_id::edge_neighbours() const {
tile_id tile_id::next_wrapped() const {
auto const next{m_id + (lowest_bit() << 1)};
if ((next >> face_shift) < static_cast<std::uint64_t>(face_count))
auto const next_face{static_cast<int>(next >> face_shift)};
if (planar()) {
// past the last tile of the plane: its first one
if (next_face == plane_face)
return tile_id{next};
return tile_id{(static_cast<std::uint64_t>(plane_face) << face_shift) | lowest_bit()};
}
if (next_face < face_count)
return tile_id{next};
// past the last face: the first tile of the level on face 0
// past the last face of the cube: the first tile of the level on face 0
return tile_id{lowest_bit()};
}
@@ -277,16 +306,43 @@ glm::dvec3 corner_direction(tile_id const Tile, int const Corner) {
return face_st_direction(cell.face, (cell.i + corner_i[k]) / side, (cell.j + corner_j[k]) / side);
}
tile_id plane_tile_at(position const &Position, int const Level) {
if (Level < 0 || Level > max_tile_level)
return {};
auto const i{plane_leaf(Position.x)};
auto const j{plane_leaf(Position.z)};
if (i < 0 || j < 0)
return {};
return tile_id::from_raw(ancestor(leaf_id(plane_face, static_cast<std::uint32_t>(i), static_cast<std::uint32_t>(j)), Level));
}
position plane_tile_centre(tile_id const Tile) {
auto const cell{Tile.to_face_ij()};
// the middle of the tile is (2i + 1) halves of its size, and the size is 15625 · 2^(30 - level) / 2 µm
auto const quarters{plane_leaf_numerator << (max_tile_level - Tile.level())};
auto const middle{[quarters](std::uint32_t const Index) {
return (static_cast<std::int64_t>(2 * static_cast<std::uint64_t>(Index) + 1) * quarters) / 4 - plane_half;
}};
return {middle(cell.i), 0, middle(cell.j)};
}
tile_frame make_frame(body const &Body, tile_id const Tile, double const Height) {
if (Tile.planar() == Body.curved())
return {};
tile_frame frame;
frame.tile = Tile;
if (!Body.curved()) {
frame.anchor = plane_tile_centre(Tile);
frame.anchor.y = micrometres(Height);
return frame;
}
auto const surface{Body.surface_along(centre_direction(Tile))};
auto const up{Body.up(surface)};
frame.anchor = from_metres(surface + up * Height);
glm::dvec3 const pole{0.0, 0.0, 1.0};
auto north{pole - up * glm::dot(pole, up)};
if (glm::length(north) < unit_tolerance) {
// at a pole: away from the meridian of 180 degrees, that is along +x towards the prime meridian on the north pole
// at a pole: as the north just short of the pole on the prime meridian, towards -x on the north pole and +x on the south one
glm::dvec3 const meridian{up.z > 0.0 ? -1.0 : 1.0, 0.0, 0.0};
north = meridian - up * glm::dot(meridian, up);
}
+29 -10
View File
@@ -24,10 +24,18 @@ http://mozilla.org/MPL/2.0/.
// projected onto the body, each split by a quadtree. the layout of the faces, the quadratic transform between the face and the cube
// and the Hilbert order follow the documented scheme of S2, so the ids are those S2 gives its cells: neighbouring tiles lie close
// together in the order, across the edges of the faces too. level 13 gives tiles of about 1.2 km on the Earth. the grid is defined
// on the directions from the centre of the body, so it is the same on any ellipsoid; the plane body has no cube and isn't tiled here
// on the directions from the centre of the body, so it is the same on any ellipsoid. the plane body has no cube: it takes the face
// value 6, which the cube leaves free, for a flat square split by the same quadtree in the same order, so the directory, the order in
// the files, the regions of the logic and the neighbours are the same code for all bodies
namespace world_geometry {
// the faces of the cube, 0 to 5
inline constexpr int face_count{6};
// the face of the tiles of the plane body
inline constexpr int plane_face{6};
// the side of the square of the plane, 8192 × 1024 m, with its middle at the zero of the .scn system: a tile of level 13 is exactly
// 1024 m, a region of level 10 8192 m, and the edges of the tiles lie every 1024 m from the zero
inline constexpr std::int64_t plane_side_micrometres{8'192LL * 1'024LL * micrometres_per_metre};
inline constexpr int max_tile_level{30};
// the level of the tiles of the surroundings, about 1.2 km (section 14)
inline constexpr int surroundings_tile_level{13};
@@ -41,13 +49,16 @@ public:
constexpr tile_id() = default;
// a tile from its raw id; no tile when the id isn't a valid one
static tile_id from_raw(std::uint64_t Raw);
// the tile of the face at the level whose quadtree coordinates are I and J, each below 2^Level; no tile out of range
// the tile of the face (0 to 5 for the cube, plane_face for the plane) at the level whose quadtree coordinates are I and J, each
// below 2^Level; no tile out of range
static tile_id from_face_ij(int Face, int Level, std::uint32_t I, std::uint32_t J);
constexpr std::uint64_t raw() const { return m_id; }
bool valid() const;
int face() const;
int level() const;
// a tile of the plane body rather than of the cube
bool planar() const { return face() == plane_face; }
struct face_ij {
int face{0};
@@ -61,9 +72,10 @@ public:
tile_id parent(int Level) const;
std::array<tile_id, 4> children() const;
// the four tiles of the same level that share an edge with this one, across the edges of the faces too: below, right, above
// and left in the coordinates of this tile's face
// and left in the coordinates of this tile's face. a tile of the plane at the edge of the square gets no tile beyond it
std::array<tile_id, 4> edge_neighbours() const;
// the next tile of the same level along the Hilbert order, from the last tile of the last face back to the first of the first
// the next tile of the same level along the Hilbert order, within its own grid: from the last tile of face 5 back to the first
// of face 0, from the last tile of the plane back to its first
tile_id next_wrapped() const;
friend constexpr auto operator<=>(tile_id const &, tile_id const &) = default;
@@ -75,13 +87,19 @@ private:
std::uint64_t m_id{0};
};
// the tile of the level that holds the direction from the centre of the body (of any non-zero length)
// the tile of the cube at the level that holds the direction from the centre of the body (of any non-zero length)
tile_id tile_at(glm::dvec3 const &Direction, int Level);
// the direction from the centre of the body to the middle of the tile, of unit length
// the direction from the centre of the body to the middle of a tile of the cube, of unit length
glm::dvec3 centre_direction(tile_id Tile);
// the direction to the corner of the tile, of unit length: 0 at the lowest coordinates of the face, then counter-clockwise
// the direction to the corner of a tile of the cube, of unit length: 0 at the lowest coordinates of the face, then counter-clockwise
glm::dvec3 corner_direction(tile_id Tile, int Corner);
// the tile of the plane at the level that holds the position (x and z of .scn; y doesn't count), computed in integers: i along x and
// j along z, a leaf of the square 7.8125 mm. no tile outside the square
tile_id plane_tile_at(position const &Position, int Level);
// the middle of a tile of the plane, at the height zero; exact down to level 28
position plane_tile_centre(tile_id Tile);
// the local frame of a tile, written in the data so that the game never computes it: the anchor, in micrometres, and the rotation
// from the tile's axes to the body's. the axes are those of .scn: x to the west, y up, z to the north
struct tile_frame {
@@ -92,10 +110,11 @@ struct tile_frame {
friend bool operator==(tile_frame const &, tile_frame const &) = default;
};
// the frame of a tile of a curved body, made when a world is compiled or imported: the anchor on the ellipsoid's normal through the
// the frame of a tile, made when a world is compiled or imported. on a curved body: the anchor on the ellipsoid's normal through the
// point of the surface in the middle of the tile, raised by the height (the mean height of the tile's terrain, so that the int32
// micrometres of the tile cover mountains too), and the axes of the vertical there and of the north. at a pole, where the north
// has no direction, z points away from the meridian of 180 degrees
// micrometres of the tile cover mountains too), and the axes of the vertical there and of the north. at a pole, where the north has
// no direction, z points as the north does just short of the pole on the prime meridian. on the plane: the anchor in the middle of
// the tile at the height, and no rotation. no frame (no tile) for a tile of the cube on the plane or of the plane on a curved body
tile_frame make_frame(body const &Body, tile_id Tile, double Height = 0.0);
// a position in metres in the tile's frame; the difference is taken in integers, exactly, before it turns into double