From fe9dbaae243eb7ab9f237fe0412043b1cf9c69c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Mon, 25 Mar 2019 14:22:35 +0100 Subject: [PATCH] [core] harden fetching camera for bounds when padding is excessive --- src/mbgl/map/map.cpp | 9 +++++++-- test/map/map.test.cpp | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 24b24359237..54a95ceaeb1 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -205,8 +205,13 @@ CameraOptions cameraForLatLngs(const std::vector& latLngs, const Transfo scaleY -= (padding.top() + padding.bottom()) / height; minScale = util::min(scaleX, scaleY); } - double zoom = transform.getZoom() + util::log2(minScale); - zoom = util::clamp(zoom, transform.getState().getMinZoom(), transform.getState().getMaxZoom()); + + double zoom = transform.getZoom(); + if (minScale > 0) { + zoom = util::clamp(zoom + util::log2(minScale), transform.getState().getMinZoom(), transform.getState().getMaxZoom()); + } else { + Log::Error(Event::General, "Unable to calculate appropriate zoom level for bounds. Vertical or horizontal padding is greater than map's height or width."); + } // Calculate the center point of a virtual bounds that is extended in all directions by padding. ScreenCoordinate centerPixel = nePixel + swPixel; diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index c06fa537446..fc8a7bffbac 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -151,6 +151,18 @@ TEST(Map, LatLngBoundsToCamera) { EXPECT_NEAR(*virtualCamera.zoom, 1.55467, 1e-5); } +TEST(Map, LatLngBoundsToCameraWithExcessivePadding) { + MapTest<> test; + + test.map.jumpTo(CameraOptions().withCenter(LatLng { 40.712730, -74.005953 }).withZoom(16.0)); + + LatLngBounds bounds = LatLngBounds::hull({15.68169,73.499857}, {53.560711, 134.77281}); + + CameraOptions virtualCamera = test.map.cameraForLatLngBounds(bounds, {500, 0, 1200, 0}); + ASSERT_TRUE(bounds.contains(*virtualCamera.center)); + EXPECT_NEAR(*virtualCamera.zoom, 16.0, 1e-5); +} + TEST(Map, LatLngBoundsToCameraWithBearing) { MapTest<> test;