18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 11:15:23 +01:00
Files
maszyna/world/worldtiles.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

400 lines
14 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/worldtiles.h"
#include <algorithm>
#include <bit>
#include <cfloat>
#include <cmath>
#include <istream>
#include <ostream>
#include "scene/sn_utils.h"
namespace world_geometry {
namespace {
// the bits of the position along the Hilbert curve, 2 per level, below the 3 bits of the face
constexpr int face_shift{61};
constexpr std::int64_t leaf_size{std::int64_t{1} << max_tile_level};
// the orientation of the Hilbert curve in a quadrant: the axes swapped, the bits inverted, as S2 has them
constexpr int swap_mask{1};
constexpr int invert_mask{2};
// the quadrant (i << 1 | j) at each position along the curve, for each orientation
constexpr std::array<std::array<int, 4>, 4> position_to_ij{{
{0, 1, 3, 2}, // canonical
{0, 2, 3, 1}, // axes swapped
{3, 2, 0, 1}, // bits inverted
{3, 1, 0, 2}, // swapped and inverted
}};
// the position along the curve of each quadrant, the inverse of the above
constexpr std::array<std::array<int, 4>, 4> ij_to_position{{
{0, 1, 3, 2},
{0, 3, 1, 2},
{2, 3, 1, 0},
{2, 1, 3, 0},
}};
// how the orientation of the sub-quadrant changes at each position
constexpr std::array<int, 4> position_to_orientation{swap_mask, 0, 0, invert_mask | swap_mask};
// the point of the cube for a face and its coordinates in [-1, 1], as S2 lays the faces out
glm::dvec3 face_uv_to_xyz(int const Face, double const U, double const V) {
switch (Face) {
case 0:
return {1.0, U, V};
case 1:
return {-U, 1.0, V};
case 2:
return {-U, -V, 1.0};
case 3:
return {-1.0, -V, -U};
case 4:
return {V, -1.0, -U};
default:
return {V, U, -1.0};
}
}
// the face whose axis is the largest component of the direction
int xyz_to_face(glm::dvec3 const &Point) {
auto const ax{std::abs(Point.x)};
auto const ay{std::abs(Point.y)};
auto const az{std::abs(Point.z)};
int axis{0};
if (ay > ax)
axis = 1;
if (az > std::max(ax, ay))
axis = 2;
auto const negative{(axis == 0 && Point.x < 0.0) || (axis == 1 && Point.y < 0.0) || (axis == 2 && Point.z < 0.0)};
return negative ? axis + 3 : axis;
}
// the coordinates of the direction on the given face; the direction has to point into the face's half of space
glm::dvec2 face_xyz_to_uv(int const Face, glm::dvec3 const &Point) {
switch (Face) {
case 0:
return {Point.y / Point.x, Point.z / Point.x};
case 1:
return {-Point.x / Point.y, Point.z / Point.y};
case 2:
return {-Point.x / Point.z, -Point.y / Point.z};
case 3:
return {Point.z / Point.x, Point.y / Point.x};
case 4:
return {Point.z / Point.y, -Point.x / Point.y};
default:
return {-Point.y / Point.z, -Point.x / Point.z};
}
}
// the quadratic transform between the coordinates of the quadtree, s in [0, 1], and those of the cube face, u in [-1, 1]: it
// evens out the size of the tiles, whose edges differ by about 1.4 times instead of about 1.7 of a plain projection
double st_to_uv(double const S) {
if (S >= 0.5)
return (1.0 / 3.0) * (4.0 * S * S - 1.0);
return (1.0 / 3.0) * (1.0 - 4.0 * (1.0 - S) * (1.0 - S));
}
double uv_to_st(double const U) {
if (U >= 0.0)
return 0.5 * std::sqrt(1.0 + 3.0 * U);
return 1.0 - 0.5 * std::sqrt(1.0 - 3.0 * U);
}
// the leaf coordinate of s, clamped to the face
std::uint32_t st_to_leaf(double const S) {
auto const leaf{static_cast<std::int64_t>(std::floor(S * static_cast<double>(leaf_size)))};
return static_cast<std::uint32_t>(std::clamp<std::int64_t>(leaf, 0, leaf_size - 1));
}
// the id of the leaf tile (level 30) at the coordinates
std::uint64_t leaf_id(int const Face, std::uint32_t const I, std::uint32_t const J) {
std::uint64_t position{0};
int orientation{Face & swap_mask};
for (int bit = max_tile_level - 1; bit >= 0; --bit) {
auto const quadrant{static_cast<int>(((I >> bit) & 1u) << 1 | ((J >> bit) & 1u))};
auto const step{ij_to_position[orientation][quadrant]};
position = (position << 2) | static_cast<std::uint64_t>(step);
orientation ^= position_to_orientation[step];
}
return (static_cast<std::uint64_t>(Face) << face_shift) | (position << 1) | 1u;
}
// the raw id of the ancestor of the tile at the level
std::uint64_t ancestor(std::uint64_t const Raw, int const Level) {
// a level out of range is no tile
if (Level < 0 || Level > max_tile_level)
return 0;
auto const lowest{std::uint64_t{1} << (2 * (max_tile_level - Level))};
return (Raw & (~lowest + 1)) | lowest;
}
// the tile of the level holding the leaf at the coordinates, which may lie up to one leaf beyond the face: then it is wrapped onto
// the face next to it, through the point of the cube, as S2 does it
std::uint64_t wrapped_tile(int const Face, std::int64_t I, std::int64_t J, int const Level) {
if (I >= 0 && J >= 0 && I < leaf_size && J < leaf_size)
return ancestor(leaf_id(Face, static_cast<std::uint32_t>(I), static_cast<std::uint32_t>(J)), Level);
I = std::clamp<std::int64_t>(I, -1, leaf_size);
J = std::clamp<std::int64_t>(J, -1, leaf_size);
// a plain projection is enough to cross the edge; the point stays barely beyond the face, so that the projection back doesn't
// move it into another leaf
constexpr double scale{1.0 / static_cast<double>(leaf_size)};
constexpr double limit{1.0 + DBL_EPSILON};
auto const u{std::clamp(scale * static_cast<double>(2 * (I - leaf_size / 2) + 1), -limit, limit)};
auto const v{std::clamp(scale * static_cast<double>(2 * (J - leaf_size / 2) + 1), -limit, limit)};
auto const point{face_uv_to_xyz(Face, u, v)};
auto const face{xyz_to_face(point)};
auto const uv{face_xyz_to_uv(face, point)};
return ancestor(leaf_id(face, st_to_leaf(0.5 * (uv.x + 1.0)), st_to_leaf(0.5 * (uv.y + 1.0))), Level);
}
// the direction of a point given by the coordinates of the quadtree of a face, in [0, 1]
glm::dvec3 face_st_direction(int const Face, double const S, double const T) {
return glm::normalize(face_uv_to_xyz(Face, st_to_uv(S), st_to_uv(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) {
tile_id const tile{Raw};
return tile.valid() ? tile : tile_id{};
}
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 > plane_face || Level < 0 || Level > max_tile_level)
return {};
if (auto const side{std::uint64_t{1} << Level}; I >= side || J >= side)
return {};
auto const shift{max_tile_level - Level};
return tile_id{ancestor(leaf_id(Face, I << shift, J << shift), Level)};
}
bool tile_id::valid() const {
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;
}
int tile_id::face() const {
return static_cast<int>(m_id >> face_shift);
}
int tile_id::level() const {
return max_tile_level - std::countr_zero(m_id) / 2;
}
tile_id::face_ij tile_id::to_face_ij() const {
face_ij result;
result.face = face();
auto const levels{level()};
int orientation{result.face & swap_mask};
for (int k = 0; k < levels; ++k) {
auto const step{static_cast<int>((m_id >> (face_shift - 2 * (k + 1))) & 3u)};
auto const quadrant{position_to_ij[orientation][step]};
result.i = (result.i << 1) | static_cast<std::uint32_t>(quadrant >> 1);
result.j = (result.j << 1) | static_cast<std::uint32_t>(quadrant & 1);
orientation ^= position_to_orientation[step];
}
return result;
}
tile_id tile_id::parent() const {
return parent(level() - 1);
}
tile_id tile_id::parent(int const Level) const {
if (!valid() || Level < 0 || Level > level())
return {};
return tile_id{ancestor(m_id, Level)};
}
std::array<tile_id, 4> tile_id::children() const {
std::array<tile_id, 4> result{};
if (!valid() || level() == max_tile_level)
return result;
auto const quarter{lowest_bit() >> 2};
auto child{m_id - lowest_bit() + quarter};
for (auto &tile : result) {
tile = tile_id{child};
child += quarter << 1;
}
return result;
}
std::array<tile_id, 4> tile_id::edge_neighbours() const {
auto const levels{level()};
auto const cell{to_face_ij()};
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)},
tile_id{wrapped_tile(cell.face, i, j + size, levels)},
tile_id{wrapped_tile(cell.face, i - size, j, levels)}};
}
tile_id tile_id::next_wrapped() const {
auto const next{m_id + (lowest_bit() << 1)};
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 of the cube: the first tile of the level on face 0
return tile_id{lowest_bit()};
}
tile_id tile_at(glm::dvec3 const &Direction, int const Level) {
if (Level < 0 || Level > max_tile_level)
return {};
auto const face{xyz_to_face(Direction)};
auto const uv{face_xyz_to_uv(face, Direction)};
return tile_id::from_raw(ancestor(leaf_id(face, st_to_leaf(uv_to_st(uv.x)), st_to_leaf(uv_to_st(uv.y))), Level));
}
glm::dvec3 centre_direction(tile_id const Tile) {
if (!Tile.valid())
return {};
auto const cell{Tile.to_face_ij()};
auto const side{static_cast<double>(std::uint64_t{1} << Tile.level())};
return face_st_direction(cell.face, (cell.i + 0.5) / side, (cell.j + 0.5) / side);
}
glm::dvec3 corner_direction(tile_id const Tile, int const Corner) {
if (!Tile.valid())
return {};
auto const cell{Tile.to_face_ij()};
auto const side{static_cast<double>(std::uint64_t{1} << Tile.level())};
constexpr std::array<int, 4> corner_i{0, 1, 1, 0};
constexpr std::array<int, 4> corner_j{0, 0, 1, 1};
auto const k{static_cast<std::size_t>(Corner & 3)};
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: 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);
}
north = glm::normalize(north);
auto const west{glm::cross(up, north)};
glm::dmat3 const axes{west, up, north};
frame.orientation = glm::normalize(glm::quat_cast(axes));
return frame;
}
glm::dvec3 to_local(tile_frame const &Frame, position const &Position) {
return glm::conjugate(Frame.orientation) * to_metres(Position - Frame.anchor);
}
position to_position(tile_frame const &Frame, glm::dvec3 const &Local) {
return Frame.anchor + from_metres(Frame.orientation * Local);
}
void write(std::ostream &Output, tile_frame const &Frame) {
sn_utils::ls_uint64(Output, Frame.tile.raw());
sn_utils::ls_int64(Output, Frame.anchor.x);
sn_utils::ls_int64(Output, Frame.anchor.y);
sn_utils::ls_int64(Output, Frame.anchor.z);
sn_utils::ls_float64(Output, Frame.orientation.w);
sn_utils::ls_float64(Output, Frame.orientation.x);
sn_utils::ls_float64(Output, Frame.orientation.y);
sn_utils::ls_float64(Output, Frame.orientation.z);
}
bool read(std::istream &Input, tile_frame &Frame) {
tile_frame frame;
frame.tile = tile_id::from_raw(sn_utils::ld_uint64(Input));
frame.anchor.x = sn_utils::ld_int64(Input);
frame.anchor.y = sn_utils::ld_int64(Input);
frame.anchor.z = sn_utils::ld_int64(Input);
frame.orientation.w = sn_utils::ld_float64(Input);
frame.orientation.x = sn_utils::ld_float64(Input);
frame.orientation.y = sn_utils::ld_float64(Input);
frame.orientation.z = sn_utils::ld_float64(Input);
if (!Input || !frame.tile.valid())
return false;
if (auto const norm{glm::length(frame.orientation)}; !(std::abs(norm - 1.0) < 1e-9))
return false;
Frame = frame;
return true;
}
} // namespace world_geometry