diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2018-07-02 21:40:04 +0300 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-03-06 09:40:58 +0200 |
commit | 3e080ec3adbb12677173b23c37ea12e7f9586f87 (patch) | |
tree | c2dff184596de10409c04f6e0da46a01b2439a44 | |
parent | d5db80536c6ee2e065cbff35d1682019dfc9a2db (diff) | |
download | qtlocation-mapboxgl-3e080ec3adbb12677173b23c37ea12e7f9586f87.tar.gz |
[core] De-friend Transform from TransformState
-rw-r--r-- | src/mbgl/map/transform.cpp | 108 | ||||
-rw-r--r-- | src/mbgl/map/transform_state.cpp | 56 | ||||
-rw-r--r-- | src/mbgl/map/transform_state.hpp | 37 | ||||
-rw-r--r-- | src/mbgl/renderer/renderer_state.cpp | 4 | ||||
-rw-r--r-- | test/map/map.test.cpp | 4 |
5 files changed, 141 insertions, 68 deletions
diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 8c55a03186..ec4d3b292a 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -49,14 +49,13 @@ void Transform::resize(const Size size) { throw std::runtime_error("failed to resize: size is empty"); } - if (state.size == size) { + if (state.getSize() == size) { return; } observer.onCameraWillChange(MapObserver::CameraChangeMode::Immediate); - state.size = size; - state.constrain(state.scale, state.x, state.y); + state.setSize(size); observer.onCameraDidChange(MapObserver::CameraChangeMode::Immediate); } @@ -85,7 +84,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim const EdgeInsets& padding = camera.padding; LatLng startLatLng = getLatLng(padding, LatLng::Unwrapped); const LatLng& unwrappedLatLng = camera.center.value_or(startLatLng); - const LatLng& latLng = state.bounds ? unwrappedLatLng : unwrappedLatLng.wrapped(); + const LatLng& latLng = state.getLatLngBounds() ? unwrappedLatLng : unwrappedLatLng.wrapped(); double zoom = camera.zoom.value_or(getZoom()); double bearing = camera.bearing ? -*camera.bearing * util::DEG2RAD : getBearing(); double pitch = camera.pitch ? *camera.pitch * util::DEG2RAD : getPitch(); @@ -94,7 +93,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim return; } - if (!state.bounds) { + if (!state.getLatLngBounds()) { if (isGestureInProgress()) { // If gesture in progress, we transfer the wrap rounds from the end longitude into // start, so the "scroll effect" of rounding the world is the same while assuring the @@ -107,26 +106,26 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim } } - const Point<double> startPoint = Projection::project(startLatLng, state.scale); - const Point<double> endPoint = Projection::project(latLng, state.scale); + const Point<double> startPoint = Projection::project(startLatLng, state.getScale()); + const Point<double> endPoint = Projection::project(latLng, state.getScale()); ScreenCoordinate center = getScreenCoordinate(padding); - center.y = state.size.height - center.y; + center.y = state.getSize().height - center.y; // Constrain camera options. zoom = util::clamp(zoom, state.getMinZoom(), state.getMaxZoom()); const double scale = state.zoomScale(zoom); - pitch = util::clamp(pitch, state.min_pitch, state.max_pitch); + pitch = util::clamp(pitch, state.getMinPitch(), state.getMaxPitch()); // Minimize rotation by taking the shorter path around the circle. - bearing = _normalizeAngle(bearing, state.bearing); - state.bearing = _normalizeAngle(state.bearing, bearing); + bearing = _normalizeAngle(bearing, state.getBearing()); + state.setBearing(_normalizeAngle(state.getBearing(), bearing)); Duration duration = animation.duration ? *animation.duration : Duration::zero(); - const double startScale = state.scale; - const double startBearing = state.bearing; - const double startPitch = state.pitch; + const double startScale = state.getScale(); + const double startBearing = state.getBearing(); + const double startPitch = state.getPitch(); state.setTransitionInProgress(unwrappedLatLng != startLatLng || scale != startScale || bearing != startBearing); startTransition(camera, animation, [=](double t) { @@ -136,10 +135,10 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim state.setLatLngZoom(frameLatLng, state.scaleZoom(frameScale)); if (bearing != startBearing) { - state.bearing = util::wrap(util::interpolate(startBearing, bearing, t), -M_PI, M_PI); + state.setBearing(util::wrap(util::interpolate(startBearing, bearing, t), -M_PI, M_PI)); } if (pitch != startPitch) { - state.pitch = util::interpolate(startPitch, pitch, t); + state.setPitch(util::interpolate(startPitch, pitch, t)); } if (!padding.isFlush()) { @@ -163,7 +162,7 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima double bearing = camera.bearing ? -*camera.bearing * util::DEG2RAD : getBearing(); double pitch = camera.pitch ? *camera.pitch * util::DEG2RAD : getPitch(); - if (std::isnan(zoom) || std::isnan(bearing) || std::isnan(pitch) || state.size.isEmpty()) { + if (std::isnan(zoom) || std::isnan(bearing) || std::isnan(pitch) || state.getSize().isEmpty()) { return; } @@ -171,28 +170,29 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima LatLng startLatLng = getLatLng(padding, LatLng::Unwrapped).wrapped(); startLatLng.unwrapForShortestPath(latLng); - const Point<double> startPoint = Projection::project(startLatLng, state.scale); - const Point<double> endPoint = Projection::project(latLng, state.scale); + const Point<double> startPoint = Projection::project(startLatLng, state.getScale()); + const Point<double> endPoint = Projection::project(latLng, state.getScale()); ScreenCoordinate center = getScreenCoordinate(padding); - center.y = state.size.height - center.y; + center.y = state.getSize().height - center.y; // Constrain camera options. zoom = util::clamp(zoom, state.getMinZoom(), state.getMaxZoom()); - pitch = util::clamp(pitch, state.min_pitch, state.max_pitch); + pitch = util::clamp(pitch, state.getMinPitch(), state.getMaxPitch()); // Minimize rotation by taking the shorter path around the circle. - bearing = _normalizeAngle(bearing, state.bearing); - state.bearing = _normalizeAngle(state.bearing, bearing); + bearing = _normalizeAngle(bearing, state.getBearing()); + state.setBearing(_normalizeAngle(state.getBearing(), bearing)); - const double startZoom = state.scaleZoom(state.scale); - const double startBearing = state.bearing; - const double startPitch = state.pitch; + const double startZoom = state.scaleZoom(state.getScale()); + const double startBearing= state.getBearing(); + const double startPitch = state.getPitch(); /// w₀: Initial visible span, measured in pixels at the initial scale. /// Known henceforth as a <i>screenful</i>. - double w0 = std::max(state.size.width - padding.left() - padding.right(), - state.size.height - padding.top() - padding.bottom()); + const Size& size = state.getSize(); + double w0 = std::max(size.width - padding.left() - padding.right(), + size.height - padding.top() - padding.bottom()); /// w₁: Final visible span, measured in pixels with respect to the initial /// scale. double w1 = w0 / state.zoomScale(zoom - startZoom); @@ -272,7 +272,7 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima return; } - const double startScale = state.scale; + const double startScale = state.getScale(); state.setTransitionInProgress(true); startTransition(camera, animation, [=](double k) { @@ -295,10 +295,10 @@ void Transform::flyTo(const CameraOptions &camera, const AnimationOptions &anima state.setLatLngZoom(frameLatLng, frameZoom); if (bearing != startBearing) { - state.bearing = util::wrap(util::interpolate(startBearing, bearing, k), -M_PI, M_PI); + state.setBearing(util::wrap(util::interpolate(startBearing, bearing, k), -M_PI, M_PI)); } if (pitch != startPitch) { - state.pitch = util::interpolate(startPitch, pitch, k); + state.setPitch(util::interpolate(startPitch, pitch, k)); } if (!padding.isFlush()) { @@ -319,15 +319,17 @@ LatLng Transform::getLatLng(const EdgeInsets& padding, LatLng::WrapMode wrap) co if (padding.isFlush()) { return state.getLatLng(wrap); } else { - return screenCoordinateToLatLng(padding.getCenter(state.size.width, state.size.height)); + const Size& size = state.getSize(); + return screenCoordinateToLatLng(padding.getCenter(size.width, size.height)); } } ScreenCoordinate Transform::getScreenCoordinate(const EdgeInsets& padding) const { + const Size& size = state.getSize(); if (padding.isFlush()) { - return { state.size.width / 2., state.size.height / 2. }; + return { size.width / 2.0, size.height / 2.0 }; } else { - return padding.getCenter(state.size.width, state.size.height); + return padding.getCenter(size.width, size.height); } } @@ -383,25 +385,24 @@ void Transform::rotateBy(const ScreenCoordinate& first, const ScreenCoordinate& center.y = first.y + std::sin(rotateBearing) * heightOffset; } - const double bearing = -(state.bearing + util::angle_between(first - center, second - center)) * util::RAD2DEG; + const double bearing = -(state.getBearing() + util::angle_between(first - center, second - center)) * util::RAD2DEG; easeTo(CameraOptions().withBearing(bearing), animation); } double Transform::getBearing() const { - return state.bearing; + return state.getBearing(); } #pragma mark - Pitch double Transform::getPitch() const { - return state.pitch; + return state.getPitch(); } #pragma mark - North Orientation void Transform::setNorthOrientation(NorthOrientation orientation) { - state.orientation = orientation; - state.constrain(state.scale, state.x, state.y); + state.setNorthOrientation(orientation); } NorthOrientation Transform::getNorthOrientation() const { @@ -410,9 +411,8 @@ NorthOrientation Transform::getNorthOrientation() const { #pragma mark - Constrain mode -void Transform::setConstrainMode(mbgl::ConstrainMode mode) { - state.constrainMode = mode; - state.constrain(state.scale, state.x, state.y); +void Transform::setConstrainMode(mbgl::ConstrainMode constrainMode) { + state.setConstrainMode(constrainMode); } ConstrainMode Transform::getConstrainMode() const { @@ -422,37 +422,37 @@ ConstrainMode Transform::getConstrainMode() const { #pragma mark - Viewport mode void Transform::setViewportMode(mbgl::ViewportMode mode) { - state.viewportMode = mode; + state.setViewportMode(mode); } ViewportMode Transform::getViewportMode() const { return state.getViewportMode(); } -#pragma mark - Projection mode +#pragma mark - Projection void Transform::setAxonometric(bool axonometric) { - state.axonometric = axonometric; + state.setAxonometric(axonometric); } bool Transform::getAxonometric() const { - return state.axonometric; + return state.getAxonometric(); } void Transform::setXSkew(double xSkew) { - state.xSkew = xSkew; + state.setXSkew(xSkew); } double Transform::getXSkew() const { - return state.xSkew; + return state.getXSkew(); } void Transform::setYSkew(double ySkew) { - state.ySkew = ySkew; + state.setYSkew(ySkew); } double Transform::getYSkew() const { - return state.ySkew; + return state.getYSkew(); } #pragma mark - Transition @@ -474,7 +474,7 @@ void Transform::startTransition(const CameraOptions& camera, optional<ScreenCoordinate> anchor = camera.center ? nullopt : camera.anchor; LatLng anchorLatLng; if (anchor) { - anchor->y = state.size.height - anchor->y; + anchor->y = state.getSize().height - anchor->y; anchorLatLng = state.screenCoordinateToLatLng(*anchor); } @@ -580,20 +580,20 @@ void Transform::cancelTransitions() { } void Transform::setGestureInProgress(bool inProgress) { - state.gestureInProgress = inProgress; + state.setGestureInProgress(inProgress); } #pragma mark Conversion and projection ScreenCoordinate Transform::latLngToScreenCoordinate(const LatLng& latLng) const { ScreenCoordinate point = state.latLngToScreenCoordinate(latLng); - point.y = state.size.height - point.y; + point.y = state.getSize().height - point.y; return point; } LatLng Transform::screenCoordinateToLatLng(const ScreenCoordinate& point) const { ScreenCoordinate flippedPoint = point; - flippedPoint.y = state.size.height - flippedPoint.y; + flippedPoint.y = state.getSize().height - flippedPoint.y; return state.screenCoordinateToLatLng(flippedPoint).wrapped(); } diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index d14a936bbd..eb079a7828 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -100,12 +100,22 @@ Size TransformState::getSize() const { return size; } +void TransformState::setSize(const Size& size_) { + size = size_; + constrain(scale, x, y); +} + #pragma mark - North Orientation NorthOrientation TransformState::getNorthOrientation() const { return orientation; } +void TransformState::setNorthOrientation(NorthOrientation orientation_) { + orientation = orientation_; + constrain(scale, x, y); +} + double TransformState::getNorthOrientationAngle() const { double angleOrientation = 0; if (orientation == NorthOrientation::Rightwards) { @@ -124,12 +134,47 @@ ConstrainMode TransformState::getConstrainMode() const { return constrainMode; } +void TransformState::setConstrainMode(ConstrainMode constrainMode_) { + constrainMode = constrainMode_; + constrain(scale, x, y); +} + #pragma mark - ViewportMode ViewportMode TransformState::getViewportMode() const { return viewportMode; } +void TransformState::setViewportMode(ViewportMode viewportMode_) { + viewportMode = viewportMode_; +} + +#pragma mark - Projection + +void TransformState::setAxonometric(bool axonometric_) { + axonometric = axonometric_; +} + +bool TransformState::getAxonometric() const { + return axonometric; +} + +void TransformState::setXSkew(double xSkew_) { + xSkew = xSkew_; +} + +double TransformState::getXSkew() const { + return xSkew; +} + +void TransformState::setYSkew(double ySkew_) { + ySkew = ySkew_; +} + +double TransformState::getYSkew() const { + return ySkew; +} + #pragma mark - Camera options CameraOptions TransformState::getCameraOptions(const EdgeInsets& padding) const { @@ -175,6 +220,10 @@ double TransformState::getZoom() const { return scaleZoom(scale); } +double TransformState::getScale() const { + return scale; +} + uint8_t TransformState::getIntegerZoom() const { return getZoom(); } @@ -243,6 +292,10 @@ float TransformState::getBearing() const { return bearing; } +void TransformState::setBearing(double bearing_) { + bearing = bearing_; +} + float TransformState::getFieldOfView() const { return fov; } @@ -255,6 +308,9 @@ float TransformState::getPitch() const { return pitch; } +void TransformState::setPitch(double pitch_) { + pitch = pitch_; +} #pragma mark - State diff --git a/src/mbgl/map/transform_state.hpp b/src/mbgl/map/transform_state.hpp index 3ffd149685..931f8a49ce 100644 --- a/src/mbgl/map/transform_state.hpp +++ b/src/mbgl/map/transform_state.hpp @@ -19,9 +19,6 @@ namespace mbgl { class UnwrappedTileID; class TransformState { - friend class Transform; - friend class RendererState; - public: TransformState(ConstrainMode = ConstrainMode::HeightOnly, ViewportMode = ViewportMode::Default); @@ -31,16 +28,30 @@ public: // Dimensions Size getSize() const; + void setSize(const Size& size); // North Orientation NorthOrientation getNorthOrientation() const; + void setNorthOrientation(NorthOrientation); double getNorthOrientationAngle() const; // Constrain mode ConstrainMode getConstrainMode() const; + void setConstrainMode(ConstrainMode); // Viewport mode ViewportMode getViewportMode() const; + void setViewportMode(ViewportMode); + + // Projection mode + bool getAxonometric() const; + void setAxonometric(bool axonometric); + + double getXSkew() const; + void setXSkew(double xSkew); + + double getYSkew() const; + void setYSkew(double ySkew); CameraOptions getCameraOptions(const EdgeInsets&) const; @@ -51,6 +62,7 @@ public: // Zoom double getZoom() const; + double getScale() const; uint8_t getIntegerZoom() const; // Bounds @@ -67,9 +79,15 @@ public: // Rotation float getBearing() const; + void setBearing(double bearing); + + // Camera float getFieldOfView() const; float getCameraToCenterDistance() const; + + // Tilt float getPitch() const; + void setPitch(double pitch); // State bool isChanging() const; @@ -94,6 +112,12 @@ public: float getCameraToTileDistance(const UnwrappedTileID&) const; float maxPitchScaleFactor() const; + /** Recenter the map so that the given coordinate is located at the given + point on screen. */ + void moveLatLng(const LatLng&, const ScreenCoordinate&); + void setScalePoint(const double scale, const ScreenCoordinate& point); + void setLatLngZoom(const LatLng &latLng, double zoom); + private: bool rotatedNorth() const; void constrain(double& scale, double& x, double& y) const; @@ -114,13 +138,6 @@ private: mat4 coordinatePointMatrix(double z) const; mat4 getPixelMatrix() const; - /** Recenter the map so that the given coordinate is located at the given - point on screen. */ - void moveLatLng(const LatLng&, const ScreenCoordinate&); - void setLatLngZoom(const LatLng &latLng, double zoom); - void setScalePoint(const double scale, const ScreenCoordinate& point); - -private: ConstrainMode constrainMode; ViewportMode viewportMode; diff --git a/src/mbgl/renderer/renderer_state.cpp b/src/mbgl/renderer/renderer_state.cpp index 33f6eb27dd..492c8b5d4a 100644 --- a/src/mbgl/renderer/renderer_state.cpp +++ b/src/mbgl/renderer/renderer_state.cpp @@ -45,12 +45,12 @@ ScreenCoordinate RendererState::pixelForLatLng(const UpdateParameters& updatePar LatLng unwrappedLatLng = latLng.wrapped(); unwrappedLatLng.unwrapForShortestPath(updateParameters.transformState.getLatLng()); const ScreenCoordinate point = updateParameters.transformState.latLngToScreenCoordinate(latLng); - return ScreenCoordinate { point.x, updateParameters.transformState.size.height - point.y }; + return ScreenCoordinate { point.x, updateParameters.transformState.getSize().height - point.y }; } LatLng RendererState::latLngForPixel(const UpdateParameters& updateParameters, const ScreenCoordinate& point) { ScreenCoordinate flippedPoint = point; - flippedPoint.y = updateParameters.transformState.size.height - flippedPoint.y; + flippedPoint.y = updateParameters.transformState.getSize().height - flippedPoint.y; return updateParameters.transformState.screenCoordinateToLatLng(flippedPoint); } diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 6d1650b82c..0df846d41e 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -171,8 +171,8 @@ TEST(Map, LatLngBoundsToCameraWithBearingAndPitch) { CameraOptions virtualCamera = test.map.cameraForLatLngBounds(bounds, {}, 35, 20); ASSERT_TRUE(bounds.contains(*virtualCamera.center)); 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); + EXPECT_NEAR(*virtualCamera.pitch, 20.0, 1e-5); + EXPECT_NEAR(*virtualCamera.bearing, 35.0, 1e-5); } TEST(Map, LatLngsToCamera) { |