From 2f4d162debd7e4accfc0b20360058304dce40801 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Tue, 11 Apr 2017 17:07:25 +0300 Subject: [core] Tighten geo.hpp ctors --- include/mbgl/util/geo.hpp | 111 ++++++++++++++++++++++++--------------- include/mbgl/util/projection.hpp | 4 +- 2 files changed, 72 insertions(+), 43 deletions(-) (limited to 'include/mbgl') diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index edd40e73ca..5f5f8bb33b 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -70,27 +70,42 @@ public: LatLng(const CanonicalTileID& id); LatLng(const UnwrappedTileID& id); - friend constexpr bool operator==(const LatLng& a, const LatLng& b) { + friend bool operator==(const LatLng& a, const LatLng& b) { return a.lat == b.lat && a.lon == b.lon; } - friend constexpr bool operator!=(const LatLng& a, const LatLng& b) { + friend bool operator!=(const LatLng& a, const LatLng& b) { return !(a == b); } }; class ProjectedMeters { +private: + double _northing; // Distance measured northwards. + double _easting; // Distance measured eastwards. + public: - double northing; - double easting; + ProjectedMeters(double n_ = 0, double e_ = 0) + : _northing(n_), _easting(e_) { + if (std::isnan(_northing)) { + throw std::domain_error("northing must not be NaN"); + } + if (std::isnan(_easting)) { + throw std::domain_error("easting must not be NaN"); + } + } - ProjectedMeters(double n = 0, double e = 0) - : northing(n), easting(e) {} -}; + double northing() const { return _northing; } + double easting() const { return _easting; } -constexpr bool operator==(const ProjectedMeters& a, const ProjectedMeters& b) { - return a.northing == b.northing && a.easting == b.easting; -} + friend bool operator==(const ProjectedMeters& a, const ProjectedMeters& b) { + return a._northing == b._northing && a._easting == b._easting; + } + + friend bool operator!=(const ProjectedMeters& a, const ProjectedMeters& b) { + return !(a == b); + } +}; class LatLngBounds { public: @@ -188,17 +203,14 @@ private: LatLngBounds(LatLng sw_, LatLng ne_) : sw(std::move(sw_)), ne(std::move(ne_)) {} - friend constexpr bool operator==(const LatLngBounds&, const LatLngBounds&); - friend constexpr bool operator!=(const LatLngBounds&, const LatLngBounds&); -}; - -constexpr bool operator==(const LatLngBounds& a, const LatLngBounds& b) { - return a.sw == b.sw && a.ne == b.ne; -} + friend bool operator==(const LatLngBounds& a, const LatLngBounds& b) { + return a.sw == b.sw && a.ne == b.ne; + } -constexpr bool operator!=(const LatLngBounds& a, const LatLngBounds& b) { - return !(a == b); -} + friend bool operator!=(const LatLngBounds& a, const LatLngBounds& b) { + return !(a == b); + } +}; // Determines the orientation of the map. enum class NorthOrientation : uint8_t { @@ -210,43 +222,60 @@ enum class NorthOrientation : uint8_t { /// 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. +private: + double _top; // Number of pixels inset from the top edge. + double _left; // Number of pixels inset from the left edge. + double _bottom; // Number of pixels inset from the bottom edge. + double _right; // Number of pixels inset from the right edge. - EdgeInsets() {} +public: + EdgeInsets(double t_ = 0, double l_ = 0, double b_ = 0, double r_ = 0) + : _top(t_), _left(l_), _bottom(b_), _right(r_) { + if (std::isnan(_top)) { + throw std::domain_error("top must not be NaN"); + } + if (std::isnan(_left)) { + throw std::domain_error("left must not be NaN"); + } + if (std::isnan(_bottom)) { + throw std::domain_error("bottom must not be NaN"); + } + if (std::isnan(_right)) { + throw std::domain_error("right must not be NaN"); + } + } - EdgeInsets(const double t, const double l, const double b, const double r) - : top(t), left(l), bottom(b), right(r) {} + double top() const { return _top; } + double left() const { return _left; } + double bottom() const { return _bottom; } + double right() const { return _right; } bool isFlush() const { - return top == 0 && left == 0 && bottom == 0 && right == 0; + return _top == 0 && _left == 0 && _bottom == 0 && _right == 0; } void operator+=(const EdgeInsets& o) { - top += o.top; - left += o.left; - bottom += o.bottom; - right += o.right; + _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, + _top + o._top, _left + o._left, _bottom + o._bottom, _right + o._right, }; } ScreenCoordinate getCenter(uint16_t width, uint16_t height) const; -}; -constexpr bool operator==(const EdgeInsets& a, const EdgeInsets& b) { - return a.top == b.top && a.left == b.left && a.bottom == b.bottom && a.right == b.right; -} + friend bool operator==(const EdgeInsets& a, const EdgeInsets& b) { + return a._top == b._top && a._left == b._left && a._bottom == b._bottom && a._right == b._right; + } -constexpr bool operator!=(const EdgeInsets& a, const EdgeInsets& b) { - return !(a == b); -} + friend bool operator!=(const EdgeInsets& a, const EdgeInsets& b) { + return !(a == b); + } +}; } // namespace mbgl diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp index e50e29e22b..ed34261b18 100644 --- a/include/mbgl/util/projection.hpp +++ b/include/mbgl/util/projection.hpp @@ -38,8 +38,8 @@ public: } static LatLng latLngForProjectedMeters(const ProjectedMeters& projectedMeters) { - double latitude = (2 * std::atan(std::exp(projectedMeters.northing / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG; - double longitude = projectedMeters.easting * util::RAD2DEG / util::EARTH_RADIUS_M; + double latitude = (2 * std::atan(std::exp(projectedMeters.northing() / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG; + double longitude = projectedMeters.easting() * util::RAD2DEG / util::EARTH_RADIUS_M; latitude = util::clamp(latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX); longitude = util::clamp(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX); -- cgit v1.2.1