diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-03-13 19:05:15 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-03-14 11:45:31 +0200 |
commit | b5bb48edaa0a553a4b91c3632148bf86ac9e2bf6 (patch) | |
tree | 85d0d979f59488b772f63c22b99f1e0913ee3f0e /include | |
parent | fe7e208b4224101f3feeddf9808ecedd3b0f486f (diff) | |
download | qtlocation-mapboxgl-b5bb48edaa0a553a4b91c3632148bf86ac9e2bf6.tar.gz |
[core] Simplify LatLng::unwrapForShortestPath usage
Simplify LatLng::{wrap,unwrapForShortestPath} code, avoiding duplicated
code between Transform::{latLngToScreenCoordinate,easeTo,flyTo}.
Added unit tests for camera usage in Transform to detect cases like e.g.
crossing the antimeridian as a shortest path between two coordinates.
Transform::flyTo precision loss to be handled in #4298.
Diffstat (limited to 'include')
-rw-r--r-- | include/mbgl/util/geo.hpp | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index 0b6ee17507..84128caae7 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -1,6 +1,7 @@ #ifndef MBGL_UTIL_GEO #define MBGL_UTIL_GEO +#include <mbgl/util/math.hpp> #include <mbgl/util/vec.hpp> #include <mbgl/util/constants.hpp> @@ -29,18 +30,16 @@ public: LatLng wrapped() const { return { latitude, longitude, Wrapped }; } void wrap() { - if (longitude < -util::LONGITUDE_MAX) longitude = util::LONGITUDE_MAX + std::fmod(longitude + util::LONGITUDE_MAX, util::DEGREES_MAX); - if (longitude > util::LONGITUDE_MAX) longitude = -util::LONGITUDE_MAX + std::fmod(longitude + util::LONGITUDE_MAX, util::DEGREES_MAX); + longitude = util::wrap(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX); } - // If we pass through the antimeridian, we update the start coordinate to make sure - // the end coordinate is always wrapped. + // 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) { - if (end.longitude < -util::LONGITUDE_MAX) { - longitude += util::DEGREES_MAX; - } else if (end.longitude > util::LONGITUDE_MAX) { - longitude -= util::DEGREES_MAX; - } + 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 { |