summaryrefslogtreecommitdiff
path: root/src/mbgl/map/tile_id.hpp
blob: a193b633920fc390c587bcfeeeac50eed6f9363b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef MBGL_MAP_TILE_ID
#define MBGL_MAP_TILE_ID

#include <cstdint>
#include <cmath>
#include <string>
#include <functional>
#include <forward_list>
#include <limits>

namespace mbgl {

class TileID {
public:
    const int16_t w = 0;
    const int8_t z = 0;
    const int32_t x = 0, y = 0;
    const int8_t sourceZ;
    const float overscaling;

    inline explicit TileID(int8_t z_, int32_t x_, int32_t y_, int8_t sourceZ_)
        : w((x_ < 0 ? x_ - (1 << z_) + 1 : x_) / (1 << z_)), z(z_), x(x_), y(y_),
        sourceZ(sourceZ_), overscaling(std::pow(2, z_ - sourceZ_)) {}

    inline uint64_t to_uint64() const {
        return ((std::pow(2, z) * y + x) * 32) + z;
    }

    inline bool operator==(const TileID& rhs) const {
        return w == rhs.w && z == rhs.z && x == rhs.x && y == rhs.y;
    }

    inline bool operator!=(const TileID& rhs) const {
        return !operator==(rhs);
    }

    inline bool operator<(const TileID& rhs) const {
        if (w != rhs.w) return w < rhs.w;
        if (z != rhs.z) return z < rhs.z;
        if (x != rhs.x) return x < rhs.x;
        return y < rhs.y;
    }

    TileID parent(int8_t z, int8_t sourceMaxZoom) const;
    TileID normalized() const;
    std::forward_list<TileID>
    children(int8_t sourceMaxZoom = std::numeric_limits<int8_t>::max()) const;
    bool isChildOf(const TileID&) const;
    operator std::string() const;
};

} // namespace mbgl

namespace std {
template <>
struct hash<mbgl::TileID> {
    typedef mbgl::TileID argument_type;
    typedef std::size_t result_type;

    result_type operator()(const mbgl::TileID& id) const {
            return std::hash<uint64_t>()(id.to_uint64());
    }
};
} // namespace std

#endif