Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Include padding when testing camera scale in cameraForLatLngBounds
Browse files Browse the repository at this point in the history
Repurpose LatLngBoundsToCameraWithBearingAndPitch to test scaling and camera setup, both with and without padding.
This adds testing of path not covered in mapbox/mapbox-gl-native-ios#59.
  • Loading branch information
astojilj committed Dec 17, 2019
1 parent 5693f3b commit 2fd9d4a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ CameraOptions cameraForLatLngs(const std::vector<LatLng>& latLngs, const Transfo
ScreenCoordinate centerPixel = nePixel + swPixel;
centerPixel /= 2.0;

return CameraOptions().withCenter(transform.screenCoordinateToLatLng(centerPixel)).withPadding(padding).withZoom(zoom);
return CameraOptions()
.withCenter(transform.screenCoordinateToLatLng(centerPixel))
.withPadding(padding)
.withZoom(zoom);
}

CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, const EdgeInsets& padding, optional<double> bearing, optional<double> pitch) const {
Expand Down
34 changes: 24 additions & 10 deletions test/map/map.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
#include <mbgl/test/fixture_log_observer.hpp>
#include <mbgl/test/map_adapter.hpp>

#include <mbgl/map/map_options.hpp>
#include <mbgl/gfx/backend_scope.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/gfx/headless_frontend.hpp>
#include <mbgl/storage/resource_options.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/map/map_options.hpp>
#include <mbgl/math/log2.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/network_status.hpp>
#include <mbgl/storage/online_file_source.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/storage/resource_options.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/raster_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/image_source.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>

using namespace mbgl;
using namespace mbgl::style;
Expand Down Expand Up @@ -177,7 +178,7 @@ TEST(Map, LatLngBoundsToCameraWithBearing) {
EXPECT_NEAR(virtualCamera.bearing.value_or(0), 35.0, 1e-5);
}

TEST(Map, LatLngBoundsToCameraWithBearingAndPitch) {
TEST(Map, LatLngBoundsToCameraWithBearingPitchAndPadding) {
MapTest<> test;

test.map.jumpTo(CameraOptions().withCenter(LatLng { 40.712730, -74.005953 }).withZoom(16.0));
Expand All @@ -189,6 +190,19 @@ TEST(Map, LatLngBoundsToCameraWithBearingAndPitch) {
EXPECT_NEAR(*virtualCamera.zoom, 13.66272, 1e-5);
ASSERT_DOUBLE_EQ(*virtualCamera.pitch, 20.0);
EXPECT_NEAR(virtualCamera.bearing.value_or(0), 35.0, 1e-5);

const EdgeInsets padding = EdgeInsets{10, 20, 30, 40};
const CameraOptions virtualCameraPadded = test.map.cameraForLatLngBounds(bounds, padding, 35, 20);
ASSERT_TRUE(bounds.contains(*virtualCameraPadded.center));
ASSERT_DOUBLE_EQ(virtualCameraPadded.center->latitude(), virtualCamera.center->latitude());
ASSERT_DOUBLE_EQ(virtualCameraPadded.center->longitude(), virtualCamera.center->longitude());

const Size size = test.map.getMapOptions().size();
const auto scaleChange = std::min((size.width - padding.left() - padding.right()) / size.width,
(size.height - padding.top() - padding.bottom()) / size.height);
ASSERT_DOUBLE_EQ(*virtualCameraPadded.zoom, *virtualCamera.zoom + util::log2(scaleChange));
ASSERT_DOUBLE_EQ(*virtualCameraPadded.pitch, *virtualCamera.pitch);
ASSERT_DOUBLE_EQ(*virtualCameraPadded.bearing, *virtualCamera.bearing);
}

TEST(Map, LatLngsToCamera) {
Expand Down

0 comments on commit 2fd9d4a

Please sign in to comment.