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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
#ifndef MBGL_UTIL_GEO
#define MBGL_UTIL_GEO
#include <mbgl/math/wrap.hpp>
#include <mbgl/util/vec.hpp>
#include <mbgl/util/constants.hpp>
#include <cmath>
namespace mbgl {
class TileID;
using ScreenCoordinate = vec2<double>;
class LatLng {
public:
struct null {};
double latitude;
double longitude;
enum WrapMode : bool { Unwrapped, Wrapped };
LatLng(null) : latitude(std::numeric_limits<double>::quiet_NaN()), longitude(latitude) {}
LatLng(double lat = 0, double lon = 0, WrapMode mode = Unwrapped)
: latitude(lat), longitude(lon) { if (mode == Wrapped) wrap(); }
LatLng wrapped() const { return { latitude, longitude, Wrapped }; }
void wrap() {
longitude = util::wrap(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX);
}
// If the distance from start to end longitudes is between half and full
// world, unwrap the start longitude to ensure the shortest path is taken.
void unwrapForShortestPath(const LatLng& end) {
const double delta = std::abs(end.longitude - longitude);
if (delta < util::LONGITUDE_MAX || delta > util::DEGREES_MAX) return;
if (longitude > 0 && end.longitude < 0) longitude -= util::DEGREES_MAX;
else if (longitude < 0 && end.longitude > 0) longitude += util::DEGREES_MAX;
}
explicit 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);
ScreenCoordinate project() const;
};
inline bool operator==(const LatLng& a, const LatLng& b) {
return a.latitude == b.latitude && a.longitude == b.longitude;
}
inline bool operator!=(const LatLng& a, const LatLng& b) {
return !(a == b);
}
class ProjectedMeters {
public:
double northing = 0;
double easting = 0;
ProjectedMeters(double n = 0, double e = 0)
: northing(n), easting(e) {}
explicit operator bool() const {
return !(std::isnan(northing) || std::isnan(easting));
}
};
class LatLngBounds {
public:
// Return a bounds covering the entire (unwrapped) world.
static LatLngBounds world() {
return LatLngBounds({-90, -180}, {90, 180});
}
// Return the bounds consisting of the single point.
static LatLngBounds singleton(const LatLng& a) {
return LatLngBounds(a, a);
}
// Return the convex hull of two points; the smallest bounds that contains both.
static LatLngBounds hull(const LatLng& a, const LatLng& b) {
LatLngBounds bounds(a, a);
bounds.extend(b);
return bounds;
}
// Return a bounds that may serve as the identity element for the extend operation.
static LatLngBounds empty() {
LatLngBounds bounds = world();
std::swap(bounds.sw, bounds.ne);
return bounds;
}
// Constructs a LatLngBounds object with the tile's exact boundaries.
LatLngBounds(const TileID&);
double south() const { return sw.latitude; }
double west() const { return sw.longitude; }
double north() const { return ne.latitude; }
double east() const { return ne.longitude; }
LatLng southwest() const { return sw; }
LatLng northeast() const { return ne; }
LatLng southeast() const { return LatLng(south(), east()); }
LatLng northwest() const { return LatLng(north(), west()); }
LatLng center() const {
return LatLng((sw.latitude + ne.latitude) / 2,
(sw.longitude + ne.longitude) / 2);
}
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 isEmpty() const {
return sw.latitude > ne.latitude ||
sw.longitude > ne.longitude;
}
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);
}
private:
LatLng sw;
LatLng ne;
LatLngBounds(const LatLng& sw_, const LatLng& ne_)
: sw(sw_), ne(ne_) {}
friend bool operator==(const LatLngBounds&, const LatLngBounds&);
friend bool operator!=(const LatLngBounds&, const LatLngBounds&);
};
inline bool operator==(const LatLngBounds& a, const LatLngBounds& b) {
return a.sw == b.sw && a.ne == b.ne;
}
inline bool operator!=(const LatLngBounds& a, const LatLngBounds& b) {
return !(a == b);
}
class MetersBounds {
public:
ProjectedMeters sw;
ProjectedMeters ne;
MetersBounds(const ProjectedMeters& sw_, const ProjectedMeters& ne_)
: sw(sw_), ne(ne_) {}
explicit 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) {}
explicit operator bool() const {
return !(std::isnan(top) || std::isnan(left) || std::isnan(bottom) || std::isnan(right))
&& (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,
};
}
ScreenCoordinate getCenter(uint16_t width, uint16_t height) const;
};
} // namespace mbgl
#endif
|