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

Commit

Permalink
[core] Added {set,get}{Min,Max}Pitch
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Apr 4, 2017
1 parent 372df88 commit 713974b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
4 changes: 4 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class Map : private util::noncopyable {
double getMinZoom() const;
void setMaxZoom(double);
double getMaxZoom() const;
void setMinPitch(double);
double getMinPitch() const;
void setMaxPitch(double);
double getMaxPitch() const;

// Rotation
void rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& second, const AnimationOptions& = {});
Expand Down
22 changes: 22 additions & 0 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,28 @@ double Map::getMaxZoom() const {
return impl->transform.getState().getMaxZoom();
}

void Map::setMinPitch(double minPitch) {
impl->transform.setMinPitch(minPitch * util::DEG2RAD);
if (getPitch() < minPitch) {
setPitch(minPitch);
}
}

double Map::getMinPitch() const {
return impl->transform.getState().getMinPitch() * util::RAD2DEG;
}

void Map::setMaxPitch(double maxPitch) {
impl->transform.setMaxPitch(maxPitch * util::DEG2RAD);
if (getPitch() > maxPitch) {
setPitch(maxPitch);
}
}

double Map::getMaxPitch() const {
return impl->transform.getState().getMaxPitch() * util::RAD2DEG;
}

#pragma mark - Size

void Map::setSize(const Size size) {
Expand Down
14 changes: 12 additions & 2 deletions src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
// Constrain camera options.
zoom = util::clamp(zoom, state.getMinZoom(), state.getMaxZoom());
const double scale = state.zoomScale(zoom);
pitch = util::clamp(pitch, 0., util::PITCH_MAX);
pitch = util::clamp(pitch, state.min_pitch, state.max_pitch);

Update update = state.getZoom() == zoom ? Update::Repaint : Update::RecalculateStyle;

Expand Down Expand Up @@ -186,7 +186,7 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima

// Constrain camera options.
zoom = util::clamp(zoom, state.getMinZoom(), state.getMaxZoom());
pitch = util::clamp(pitch, 0., util::PITCH_MAX);
pitch = util::clamp(pitch, state.min_pitch, state.max_pitch);

// Minimize rotation by taking the shorter path around the circle.
angle = _normalizeAngle(angle, state.angle);
Expand Down Expand Up @@ -456,6 +456,16 @@ void Transform::setMaxZoom(const double maxZoom) {
state.setMaxZoom(maxZoom);
}

void Transform::setMinPitch(double minPitch) {
if (std::isnan(minPitch)) return;
state.setMinPitch(minPitch);
}

void Transform::setMaxPitch(double maxPitch) {
if (std::isnan(maxPitch)) return;
state.setMaxPitch(maxPitch);
}

#pragma mark - Angle

void Transform::rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& second, const AnimationOptions& animation) {
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/map/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Transform : private util::noncopyable {
void setLatLngBounds(const LatLngBounds&);
void setMinZoom(double);
void setMaxZoom(double);
void setMinPitch(double);
void setMaxPitch(double);

// Zoom

Expand Down
19 changes: 19 additions & 0 deletions src/mbgl/map/transform_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ double TransformState::getMaxZoom() const {
return scaleZoom(max_scale);
}

void TransformState::setMinPitch(double minPitch) {
if (minPitch <= getMaxPitch()) {
min_pitch = minPitch;
}
}

double TransformState::getMinPitch() const {
return min_pitch;
}

void TransformState::setMaxPitch(double maxPitch) {
if (maxPitch >= getMinPitch()) {
max_pitch = maxPitch;
}
}

double TransformState::getMaxPitch() const {
return max_pitch;
}

#pragma mark - Rotation

Expand Down
6 changes: 6 additions & 0 deletions src/mbgl/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class TransformState {
double getMinZoom() const;
void setMaxZoom(double);
double getMaxZoom() const;
void setMinPitch(double);
double getMinPitch() const;
void setMaxPitch(double);
double getMaxPitch() const;

// Rotation
float getAngle() const;
Expand Down Expand Up @@ -87,6 +91,8 @@ class TransformState {
// Limit the amount of zooming possible on the map.
double min_scale = std::pow(2, 0);
double max_scale = std::pow(2, 20);
double min_pitch = 0.0;
double max_pitch = util::PITCH_MAX;

NorthOrientation orientation = NorthOrientation::Upwards;

Expand Down
18 changes: 18 additions & 0 deletions test/map/transform.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,21 @@ TEST(Transform, LatLngBounds) {
ASSERT_EQ(transform.getLatLng().latitude, 0.0);
ASSERT_EQ(transform.getLatLng().longitude, 0.0);
}

TEST(Transform, PitchBounds) {
Transform transform;
transform.resize({ 1000, 1000 });
transform.setLatLngZoom({ 0, 0 }, transform.getState().getMaxZoom());

ASSERT_DOUBLE_EQ(transform.getState().getPitch() * util::RAD2DEG, 0.0);
ASSERT_DOUBLE_EQ(transform.getState().getMinPitch() * util::RAD2DEG, 0.0);
ASSERT_DOUBLE_EQ(transform.getState().getMaxPitch() * util::RAD2DEG, 60.0);

transform.setMinPitch(45.0 * util::DEG2RAD);
transform.setPitch(0.0 * util::DEG2RAD);
ASSERT_NEAR(transform.getState().getPitch() * util::RAD2DEG, 45.0, 1e-5);

transform.setMaxPitch(55.0 * util::DEG2RAD);
transform.setPitch(60.0 * util::DEG2RAD);
ASSERT_NEAR(transform.getState().getPitch() * util::RAD2DEG, 55.0, 1e-5);
}

0 comments on commit 713974b

Please sign in to comment.