use a custom struct for Tile IDs

This commit is contained in:
Konstantin Käfer
2014-03-12 16:42:37 +01:00
parent 8fd4d855d5
commit 86e6e6bed8
7 changed files with 53 additions and 32 deletions

View File

@@ -8,10 +8,10 @@ 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) {
Tile::ID Tile::ID::parent(int32_t parent_z) const {
assert(parent_z < z);
ID pos(z, x, y);
while (pos.z > parent_z) {
--pos.z;
pos.x = floor(pos.x / 2);
pos.y = floor(pos.y / 2);
@@ -19,25 +19,27 @@ Tile::ID Tile::parent(const ID& id, int32_t z) {
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<Tile::ID> Tile::ID::children(int32_t child_z) const {
assert(child_z > z);
int32_t factor = pow(2, child_z - 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);
for (int32_t ty = y * factor, y_max = (y + 1) * factor; ty < y_max; ++ty) {
for (int32_t tx = x * factor, x_max = (x + 1) * factor; tx < x_max; ++tx) {
children.emplace_front(child_z, tx, ty);
}
}
return children;
}
void Tile::ID::normalize() {
int32_t dim = pow(2, z);
while (x < 0) x += dim;
while (x >= dim) x -= dim;
}
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;
Tile::ID Tile::ID::normalized() const {
ID pos(z, x, y);
pos.normalize();
return pos;
}