Files
MapLibreParticleLab/src/map/tile.cpp
Konstantin Käfer 02bd08388a disable debug statement
2014-03-05 17:00:13 +01:00

44 lines
944 B
C++

#include <llmr/map/tile.hpp>
#include <cassert>
using namespace llmr;
Tile::Tile(const ID& id)
: id(id) {
}
Tile::ID Tile::parent(const ID& id, int32_t z) {
assert(z < id.z);
ID pos(id);
while (pos.z > z) {
--pos.z;
pos.x = floor(pos.x / 2);
pos.y = floor(pos.y / 2);
}
return pos;
}
std::forward_list<Tile::ID> Tile::children(const ID& id, int32_t z) {
assert(z > id.z);
int32_t factor = pow(2, z - id.z);
std::forward_list<ID> children;
for (int32_t y = id.y * factor, y_max = (id.y + 1) * factor; y < y_max; ++y) {
for (int32_t x = id.x * factor, x_max = (id.x + 1) * factor; x < x_max; ++x) {
children.emplace_front(x, y, z);
}
}
return children;
}
Tile::ID Tile::normalize(const ID& id) {
ID pos(id);
int32_t dim = pow(2, id.z);
while (pos.x < 0) pos.x += dim;
while (pos.x >= dim) pos.x -= dim;
return pos;
}