From 16a2839e23d42d4640ef028d62dc01322a0d2e5a Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 4 Apr 2017 07:35:26 -0700 Subject: [all] Make LatLng coordinates read-only --- include/mbgl/util/geo.hpp | 78 +++++++++++++++++++++------------------- include/mbgl/util/projection.hpp | 8 ++--- 2 files changed, 45 insertions(+), 41 deletions(-) (limited to 'include/mbgl/util') 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; using ScreenBox = mapbox::geometry::box; 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 project(const LatLng& latLng, double scale) { return Point { - 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; } -- cgit v1.2.1