summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2016-05-19 19:03:41 +0300
committerBruno de Oliveira Abinader <bruno@mapbox.com>2016-05-20 16:18:07 +0300
commitf751d2c46528ac4301ef3753c468950051937940 (patch)
treede8859aadb87cdcb02a1d828087e53bef19d75b6
parentb962b56072255c818f441a2f0c8fbeb27e7f78bc (diff)
downloadqtlocation-mapboxgl-f751d2c46528ac4301ef3753c468950051937940.tar.gz
[core] Avoid NaNs in TransformState unit conversions
Fix cases where e.g. state has either zero width or height, causing the unit convertion functions would return NaNs.
-rw-r--r--src/mbgl/map/transform.cpp27
-rw-r--r--src/mbgl/map/transform_state.cpp8
-rw-r--r--test/map/transform.cpp40
3 files changed, 61 insertions, 14 deletions
diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp
index d2f32acb67..bf95dd0714 100644
--- a/src/mbgl/map/transform.cpp
+++ b/src/mbgl/map/transform.cpp
@@ -45,24 +45,23 @@ Transform::Transform(std::function<void(MapChange)> callback_,
#pragma mark - Map View
bool Transform::resize(const std::array<uint16_t, 2> size) {
- if (state.width != size[0] || state.height != size[1]) {
-
- if (callback) {
- callback(MapChangeRegionWillChange);
- }
+ if (state.width == size[0] && state.height == size[1]) {
+ return false;
+ }
- state.width = size[0];
- state.height = size[1];
- state.constrain(state.scale, state.x, state.y);
+ if (callback) {
+ callback(MapChangeRegionWillChange);
+ }
- if (callback) {
- callback(MapChangeRegionDidChange);
- }
+ state.width = size[0];
+ state.height = size[1];
+ state.constrain(state.scale, state.x, state.y);
- return true;
- } else {
- return false;
+ if (callback) {
+ callback(MapChangeRegionDidChange);
}
+
+ return true;
}
#pragma mark - Camera
diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp
index ad41c9eea2..ac8b6396c7 100644
--- a/src/mbgl/map/transform_state.cpp
+++ b/src/mbgl/map/transform_state.cpp
@@ -230,6 +230,10 @@ double TransformState::worldSize() const {
}
ScreenCoordinate TransformState::latLngToScreenCoordinate(const LatLng& latLng) const {
+ if (width == 0 || height == 0) {
+ return {};
+ }
+
mat4 mat = coordinatePointMatrix(getZoom());
vec4 p;
Point<double> pt = project(latLng) / double(util::tileSize);
@@ -239,6 +243,10 @@ ScreenCoordinate TransformState::latLngToScreenCoordinate(const LatLng& latLng)
}
LatLng TransformState::screenCoordinateToLatLng(const ScreenCoordinate& point, LatLng::WrapMode wrapMode) const {
+ if (width == 0 || height == 0) {
+ return {};
+ }
+
float targetZ = 0;
mat4 mat = coordinatePointMatrix(getZoom());
diff --git a/test/map/transform.cpp b/test/map/transform.cpp
index 3cf542c4f3..afb655abca 100644
--- a/test/map/transform.cpp
+++ b/test/map/transform.cpp
@@ -474,3 +474,43 @@ TEST(Transform, Camera) {
transform.updateTransitions(transform.getTransitionStart() + transform.getTransitionDuration());
ASSERT_FALSE(transform.inTransition());
}
+
+TEST(Transform, DefaultTransform) {
+ Transform transform;
+ const TransformState& state = transform.getState();
+
+ LatLng nullIsland, latLng = {};
+ ScreenCoordinate center, point = {};
+ const uint16_t min = std::numeric_limits<uint16_t>::min();
+ const uint16_t max = std::numeric_limits<uint16_t>::max();
+
+ auto testConversions = [&](const LatLng& coord, const ScreenCoordinate& screenCoord) {
+ latLng = state.screenCoordinateToLatLng(center);
+ ASSERT_NEAR(latLng.latitude, coord.latitude, 0.000001);
+ ASSERT_NEAR(latLng.longitude, coord.longitude, 0.000001);
+ point = state.latLngToScreenCoordinate(nullIsland);
+ ASSERT_DOUBLE_EQ(point.x, screenCoord.x);
+ ASSERT_DOUBLE_EQ(point.y, screenCoord.y);
+ };
+
+ testConversions(nullIsland, center);
+
+ // Cannot assign the current size.
+ ASSERT_FALSE(transform.resize({{}}));
+
+ ASSERT_TRUE(transform.resize({{ min, max }}));
+ testConversions(nullIsland, center);
+
+ ASSERT_TRUE(transform.resize({{ max, min }}));
+ testConversions(nullIsland, center);
+
+ ASSERT_TRUE(transform.resize({{ min, min }}));
+ testConversions(nullIsland, center);
+
+ center = { max / 2., max / 2. };
+
+ // -1 evaluates to UINT_MAX.
+ ASSERT_TRUE(transform.resize({{ static_cast<uint16_t>(-1), static_cast<uint16_t>(-1) }}));
+ ASSERT_FALSE(transform.resize({{ max, max }}));
+ testConversions(nullIsland, center);
+}