diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-04 07:35:26 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-06 09:22:51 -0700 |
commit | 16a2839e23d42d4640ef028d62dc01322a0d2e5a (patch) | |
tree | b8b8ba6e7c9f2e91cc3ec230bb9420e3827f3c27 /include | |
parent | a261f7ff532584b3bbbe4ddef2d0ff96a1ee65a5 (diff) | |
download | qtlocation-mapboxgl-16a2839e23d42d4640ef028d62dc01322a0d2e5a.tar.gz |
[all] Make LatLng coordinates read-only
Diffstat (limited to 'include')
-rw-r--r-- | include/mbgl/util/geo.hpp | 78 | ||||
-rw-r--r-- | include/mbgl/util/projection.hpp | 8 |
2 files changed, 45 insertions, 41 deletions
diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index d0d0518c2a..6adbadca0d 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -21,14 +21,15 @@ using ScreenLineString = mapbox::geometry::line_string<double>; using ScreenBox = mapbox::geometry::box<double>; class LatLng { -public: - double latitude; - double longitude; +private: + double lat; + double lon; +public: enum WrapMode : bool { Unwrapped, Wrapped }; - LatLng(double lat = 0, double lon = 0, WrapMode mode = Unwrapped) - : latitude(lat), longitude(lon) { + LatLng(double lat_ = 0, double lon_ = 0, WrapMode mode = Unwrapped) + : lat(lat_), lon(lon_) { if (std::isnan(lat)) { throw std::domain_error("latitude must not be NaN"); } @@ -46,33 +47,36 @@ public: } } - LatLng wrapped() const { return { latitude, longitude, Wrapped }; } + double latitude() const { return lat; } + double longitude() const { return lon; } + + LatLng wrapped() const { return { lat, lon, Wrapped }; } void wrap() { - longitude = util::wrap(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX); + lon = util::wrap(lon, -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); + const double delta = std::abs(end.lon - lon); 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; + if (lon > 0 && end.lon < 0) lon -= util::DEGREES_MAX; + else if (lon < 0 && end.lon > 0) lon += util::DEGREES_MAX; } // Constructs a LatLng object with the top left position of the specified tile. LatLng(const CanonicalTileID& id); LatLng(const UnwrappedTileID& id); -}; -constexpr bool operator==(const LatLng& a, const LatLng& b) { - return a.latitude == b.latitude && a.longitude == b.longitude; -} + friend constexpr bool operator==(const LatLng& a, const LatLng& b) { + return a.lat == b.lat && a.lon == b.lon; + } -constexpr bool operator!=(const LatLng& a, const LatLng& b) { - return !(a == b); -} + friend constexpr bool operator!=(const LatLng& a, const LatLng& b) { + return !(a == b); + } +}; class ProjectedMeters { public: @@ -116,10 +120,10 @@ public: // Constructs a LatLngBounds object with the tile's exact boundaries. LatLngBounds(const CanonicalTileID&); - double south() const { return sw.latitude; } - double west() const { return sw.longitude; } - double north() const { return ne.latitude; } - double east() const { return ne.longitude; } + 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; } @@ -127,15 +131,15 @@ public: LatLng northwest() const { return LatLng(north(), west()); } LatLng center() const { - return LatLng((sw.latitude + ne.latitude) / 2, - (sw.longitude + ne.longitude) / 2); + 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; + sw = LatLng(std::min(point.latitude(), sw.latitude()), + std::min(point.longitude(), sw.longitude())); + ne = LatLng(std::max(point.latitude(), ne.latitude()), + std::max(point.longitude(), ne.longitude())); } void extend(const LatLngBounds& bounds) { @@ -144,22 +148,22 @@ public: } bool isEmpty() const { - return sw.latitude > ne.latitude || - sw.longitude > ne.longitude; + 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); + 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); + return (area.ne.latitude() > sw.latitude() && + area.sw.latitude() < ne.latitude() && + area.ne.longitude() > sw.longitude() && + area.sw.longitude() < ne.longitude()); } private: diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp index 9ca3cd4ab5..e50e29e22b 100644 --- a/include/mbgl/util/projection.hpp +++ b/include/mbgl/util/projection.hpp @@ -25,8 +25,8 @@ public: } static ProjectedMeters projectedMetersForLatLng(const LatLng& latLng) { - const double constrainedLatitude = util::clamp(latLng.latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX); - const double constrainedLongitude = util::clamp(latLng.longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX); + const double constrainedLatitude = util::clamp(latLng.latitude(), -util::LATITUDE_MAX, util::LATITUDE_MAX); + const double constrainedLongitude = util::clamp(latLng.longitude(), -util::LONGITUDE_MAX, util::LONGITUDE_MAX); const double m = 1 - 1e-15; const double f = util::clamp(std::sin(util::DEG2RAD * constrainedLatitude), -m, m); @@ -49,8 +49,8 @@ public: static Point<double> project(const LatLng& latLng, double scale) { return Point<double> { - util::LONGITUDE_MAX + latLng.longitude, - util::LONGITUDE_MAX - util::RAD2DEG * std::log(std::tan(M_PI / 4 + latLng.latitude * M_PI / util::DEGREES_MAX)) + util::LONGITUDE_MAX + latLng.longitude(), + util::LONGITUDE_MAX - util::RAD2DEG * std::log(std::tan(M_PI / 4 + latLng.latitude() * M_PI / util::DEGREES_MAX)) } * worldSize(scale) / util::DEGREES_MAX; } |