summaryrefslogtreecommitdiff
path: root/include/mbgl/util/geo.hpp
blob: 643b82f89032f844192f2099a925d356c6aaee1c (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef MBGL_UTIL_GEO
#define MBGL_UTIL_GEO

#include <mbgl/util/vec.hpp>

#include <cmath>

namespace mbgl {

class TileID;

using PrecisionPoint = vec2<double>;

class LatLng {
public:
    double latitude = 0;
    double longitude = 0;

    LatLng(double lat = 0, double lon = 0)
        : latitude(lat), longitude(lon) {}

    operator bool() const {
        return !(std::isnan(latitude) || std::isnan(longitude));
    }

    // Constructs a LatLng object with the top left position of the specified tile.
    LatLng(const TileID& id);

    PrecisionPoint project() const;
};

class ProjectedMeters {
public:
    double northing = 0;
    double easting = 0;

    ProjectedMeters(double n = 0, double e = 0)
        : northing(n), easting(e) {}

    operator bool() const {
        return !(std::isnan(northing) || std::isnan(easting));
    }
};

class LatLngBounds {
public:
    LatLng sw = {-90, -180};
    LatLng ne = {90, 180};

    LatLngBounds() {}

    LatLngBounds(const LatLng& sw_, const LatLng& ne_)
        : sw(sw_), ne(ne_) {}

    static LatLngBounds getExtendable() {
        LatLngBounds bounds;
        return { bounds.ne, bounds.sw };
    }

    // Constructs a LatLngBounds object with the tile's exact boundaries.
    LatLngBounds(const TileID& id);

    void extend(const LatLng& point) {
        if (point.latitude < sw.latitude) sw.latitude = point.latitude;
        if (point.latitude > ne.latitude) ne.latitude = point.latitude;
        if (point.longitude < sw.longitude) sw.longitude = point.longitude;
        if (point.longitude > ne.longitude) ne.longitude = point.longitude;
    }

    void extend(const LatLngBounds& bounds) {
        extend(bounds.sw);
        extend(bounds.ne);
    }

    bool contains(const LatLng& point) const {
        return (point.latitude  >= sw.latitude  &&
                point.latitude  <= ne.latitude  &&
                point.longitude >= sw.longitude &&
                point.longitude <= ne.longitude);
    }

    bool intersects(const LatLngBounds area) const {
        return (area.ne.latitude  > sw.latitude  &&
                area.sw.latitude  < ne.latitude  &&
                area.ne.longitude > sw.longitude &&
                area.sw.longitude < ne.longitude);
    }
};

class MetersBounds {
public:
    ProjectedMeters sw;
    ProjectedMeters ne;

    MetersBounds(const ProjectedMeters& sw_, const ProjectedMeters& ne_)
        : sw(sw_), ne(ne_) {}

    operator bool() const {
        return sw && ne;
    }
};

// Determines the orientation of the map.
enum class NorthOrientation : uint8_t {
    Upwards, // Default
    Rightwards,
    Downwards,
    Leftwards,
};

/// The distance on each side between a rectangle and a rectangle within.
class EdgeInsets {
public:
    double top = 0;     ///< Number of pixels inset from the top edge.
    double left = 0;    ///< Number of pixels inset from the left edge.
    double bottom = 0;  ///< Number of pixels inset from the bottom edge.
    double right = 0;   ///< Number of pixels inset from the right edge.
    
    EdgeInsets() {}
    
    EdgeInsets(const double t, const double l, const double b, const double r)
        : top(t), left(l), bottom(b), right(r) {}
    
    operator bool() const {
        return top || left || bottom || right;
    }
    
    void operator+=(const EdgeInsets& o) {
        top += o.top;
        left += o.left;
        bottom += o.bottom;
        right += o.right;
    }

    EdgeInsets operator+(const EdgeInsets& o) const {
        return {
            top + o.top, left + o.left, bottom + o.bottom, right + o.right,
        };
    }
    
    PrecisionPoint getCenter(uint16_t width, uint16_t height) const;
};

} // namespace mbgl

#endif