From 8b44cfeb7642c74307c4c946fded280fe7e7bbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Sat, 18 Apr 2015 22:14:39 -0700 Subject: Ensure correct spin direction Ported mapbox/mapbox-gl-js#821 as well as `util.wrap()` from mapbox-gl-js. Fixes #1199. --- include/mbgl/util/math.hpp | 6 ++++++ src/mbgl/map/transform.cpp | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/mbgl/util/math.hpp b/include/mbgl/util/math.hpp index e800fc7847..647fa5e67f 100644 --- a/include/mbgl/util/math.hpp +++ b/include/mbgl/util/math.hpp @@ -98,6 +98,12 @@ T clamp(T value, T min, T max) { return value < min ? min : (value > max ? max : value); } +template +T wrap(T value, T min, T max) { + T d = max - min; + return value == max ? value : std::fmod((std::fmod((value - min), d) + d), d) + min; +} + template T smoothstep(T edge0, T edge1, T x) { T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1)); diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 10f69e2edc..cb73915e36 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -12,6 +12,21 @@ using namespace mbgl; +/** Converts the given angle (in radians) to be numerically close to the anchor angle, allowing it to be interpolated properly without sudden jumps. */ +static double _normalizeAngle(double angle, double anchorAngle) +{ + angle = util::wrap(angle, -M_PI, M_PI); + double diff = std::abs(angle - anchorAngle); + if (std::abs(angle - util::M2PI - anchorAngle) < diff) { + angle -= util::M2PI; + } + if (std::abs(angle + util::M2PI - anchorAngle) < diff) { + angle += util::M2PI; + } + + return angle; +} + Transform::Transform(View &view_) : view(view_) { @@ -330,12 +345,7 @@ void Transform::_setAngle(double new_angle, const Duration duration) { MapChangeRegionWillChangeAnimated : MapChangeRegionWillChange); - while (new_angle > M_PI) - new_angle -= util::M2PI; - while (new_angle <= -M_PI) - new_angle += util::M2PI; - - final.angle = new_angle; + final.angle = _normalizeAngle(new_angle, current.angle); if (duration == Duration::zero()) { current.angle = final.angle; -- cgit v1.2.1