From 19b84464a33bc50431c995e54b4b05f12823d580 Mon Sep 17 00:00:00 2001 From: Alex Cristici Date: Mon, 24 Apr 2023 18:45:09 +0300 Subject: [PATCH] Fix render tests failing on Xcode 14.3 (#1046) --- CMakeLists.txt | 1 + include/mbgl/math/angles.hpp | 49 +++++++++++++++ include/mbgl/style/position.hpp | 5 +- include/mbgl/util/constants.hpp | 4 -- include/mbgl/util/projection.hpp | 15 ++--- platform/glfw/glfw_view.cpp | 5 +- src/mbgl/gl/render_custom_layer.cpp | 3 +- src/mbgl/layout/symbol_layout.cpp | 7 ++- src/mbgl/map/map.cpp | 11 ++-- src/mbgl/map/map_projection.cpp | 5 +- src/mbgl/map/transform.cpp | 23 ++++---- src/mbgl/map/transform_state.cpp | 11 ++-- .../layers/render_hillshade_layer.cpp | 3 +- .../render_location_indicator_layer.cpp | 15 ++--- .../renderer/layers/render_raster_layer.cpp | 3 +- src/mbgl/style/expression/within.cpp | 3 +- src/mbgl/text/collision_feature.cpp | 3 +- src/mbgl/text/quads.cpp | 5 +- src/mbgl/tile/geometry_tile_data.cpp | 2 +- src/mbgl/util/camera.cpp | 7 ++- src/mbgl/util/geo.cpp | 3 +- test/map/transform.test.cpp | 59 ++++++++++--------- test/util/camera.test.cpp | 9 +-- test/util/tile_cover.test.cpp | 7 ++- 24 files changed, 162 insertions(+), 96 deletions(-) create mode 100644 include/mbgl/math/angles.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index eb2e5994b..49905a634 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,6 +165,7 @@ list(APPEND INCLUDE_FILES ${PROJECT_SOURCE_DIR}/include/mbgl/map/map_projection.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/map/mode.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/map/projection_mode.hpp + ${PROJECT_SOURCE_DIR}/include/mbgl/math/angles.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/math/clamp.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/math/log2.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/math/minmax.hpp diff --git a/include/mbgl/math/angles.hpp b/include/mbgl/math/angles.hpp new file mode 100644 index 000000000..8419ee087 --- /dev/null +++ b/include/mbgl/math/angles.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include + +namespace mbgl { +namespace util { + +/** + * @brief Converts degrees to radians + * + * @param deg Degrees as double. + * @return Radians as double. + */ +constexpr double deg2rad(double deg) noexcept { + return deg * M_PI / 180.0; +} + +/** + * @brief Converts degrees to radians + * + * @param deg Degrees as float. + * @return Radians as float. + */ +constexpr float deg2radf(float deg) noexcept { + return deg * static_cast(M_PI) / 180.0F; +} + +/** + * @brief Converts radians to degrees + * + * @param rad Radians as double. + * @return Degrees as double. + */ +constexpr double rad2deg(double rad) noexcept { + return rad * 180.0 / M_PI; +} + +/** + * @brief Converts radians to degrees + * + * @param rad Radians as float. + * @return Degrees as float. + */ +constexpr float rad2degf(float rad) noexcept { + return rad * 180.0F / static_cast(M_PI); +} + +} // namespace util +} // namespace mbgl diff --git a/include/mbgl/style/position.hpp b/include/mbgl/style/position.hpp index 461d8d6aa..43b54cb69 100644 --- a/include/mbgl/style/position.hpp +++ b/include/mbgl/style/position.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -52,8 +53,8 @@ private: void calculateCartesian() { // We abstract "north"/"up" (compass-wise) to be 0° when really this is 90° (π/2): we // correct for that here - const float _a = (azimuthal + 90) * util::DEG2RAD_F; - const float _p = polar * util::DEG2RAD_F; + const float _a = util::deg2radf(azimuthal + 90); + const float _p = util::deg2radf(polar); x = radial * std::cos(_a) * std::sin(_p); y = radial * std::sin(_a) * std::sin(_p); diff --git a/include/mbgl/util/constants.hpp b/include/mbgl/util/constants.hpp index 96554d65b..8b05fad81 100644 --- a/include/mbgl/util/constants.hpp +++ b/include/mbgl/util/constants.hpp @@ -26,10 +26,6 @@ constexpr uint16_t tileSize_I = 512; */ constexpr int32_t EXTENT = 8192; -constexpr double DEG2RAD_D = M_PI / 180.0; -constexpr double RAD2DEG_D = 180.0 / M_PI; -constexpr float DEG2RAD_F = static_cast(M_PI) / 180.0F; -constexpr float RAD2DEG_F = 180.0F / static_cast(M_PI); constexpr double M2PI = M_PI * 2; constexpr double EARTH_RADIUS_M = 6378137; constexpr double LATITUDE_MAX = 85.051128779806604; diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp index f9081a8e4..4c2db837c 100644 --- a/include/mbgl/util/projection.hpp +++ b/include/mbgl/util/projection.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include namespace mbgl { @@ -48,7 +49,7 @@ public: const double constrainedZoom = util::clamp(zoom, util::MIN_ZOOM, util::MAX_ZOOM); const double constrainedScale = std::pow(2.0, constrainedZoom); const double constrainedLatitude = util::clamp(lat, -util::LATITUDE_MAX, util::LATITUDE_MAX); - return std::cos(constrainedLatitude * util::DEG2RAD_D) * util::M2PI * util::EARTH_RADIUS_M / worldSize(constrainedScale); + return std::cos(util::deg2rad(constrainedLatitude)) * util::M2PI * util::EARTH_RADIUS_M / worldSize(constrainedScale); } static ProjectedMeters projectedMetersForLatLng(const LatLng& latLng) { @@ -56,17 +57,17 @@ public: const double constrainedLongitude = util::clamp(latLng.longitude(), -util::LONGITUDE_MAX, util::LONGITUDE_MAX); const double m = 1 - 1e-15; - const double f = util::clamp(std::sin(util::DEG2RAD_D * constrainedLatitude), -m, m); + const double f = util::clamp(std::sin(util::deg2rad(constrainedLatitude)), -m, m); - const double easting = util::EARTH_RADIUS_M * constrainedLongitude * util::DEG2RAD_D; + const double easting = util::deg2rad(util::EARTH_RADIUS_M * constrainedLongitude); const double northing = 0.5 * util::EARTH_RADIUS_M * std::log((1 + f) / (1 - f)); return {northing, easting}; } static LatLng latLngForProjectedMeters(const ProjectedMeters& projectedMeters) { - double latitude = (2 * std::atan(std::exp(projectedMeters.northing() / util::EARTH_RADIUS_M)) - (M_PI / 2.0)) * util::RAD2DEG_D; - double longitude = projectedMeters.easting() * util::RAD2DEG_D / util::EARTH_RADIUS_M; + double latitude = util::rad2deg(2 * std::atan(std::exp(projectedMeters.northing() / util::EARTH_RADIUS_M)) - (M_PI / 2.0)); + double longitude = util::rad2deg(projectedMeters.easting()) / util::EARTH_RADIUS_M; latitude = util::clamp(latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX); longitude = util::clamp(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX); @@ -86,7 +87,7 @@ public: static LatLng unproject(const Point& p, double scale, LatLng::WrapMode wrapMode = LatLng::Unwrapped) { auto p2 = p * util::DEGREES_MAX / worldSize(scale); return LatLng { - util::DEGREES_MAX / M_PI * std::atan(std::exp((util::LONGITUDE_MAX - p2.y) * util::DEG2RAD_D)) - 90.0, + std::atan(std::exp(util::deg2rad(util::LONGITUDE_MAX - p2.y))) * util::DEGREES_MAX / M_PI - 90.0, p2.x - util::LONGITUDE_MAX, wrapMode }; @@ -97,7 +98,7 @@ private: const double latitude = util::clamp(latLng.latitude(), -util::LATITUDE_MAX, util::LATITUDE_MAX); return Point { util::LONGITUDE_MAX + latLng.longitude(), - util::LONGITUDE_MAX - util::RAD2DEG_D * std::log(std::tan(M_PI / 4 + latitude * M_PI / util::DEGREES_MAX)) + util::LONGITUDE_MAX - util::rad2deg(std::log(std::tan(M_PI / 4 + latitude * M_PI / util::DEGREES_MAX))) } * (worldSize / util::DEGREES_MAX); } }; diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 46048ce84..afd5a4c5f 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -467,8 +468,8 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, } break; case GLFW_KEY_U: { auto bounds = view->map->getBounds(); - if (bounds.minPitch == mbgl::util::PITCH_MIN * mbgl::util::RAD2DEG_D && - bounds.maxPitch == mbgl::util::PITCH_MAX * mbgl::util::RAD2DEG_D) { + if (bounds.minPitch == mbgl::util::rad2deg(mbgl::util::PITCH_MIN) && + bounds.maxPitch == mbgl::util::rad2deg(mbgl::util::PITCH_MAX)) { mbgl::Log::Info(mbgl::Event::General, "Limiting pitch bounds to [30, 40] degrees"); view->map->setBounds(mbgl::BoundOptions().withMinPitch(30).withMaxPitch(40)); } else { diff --git a/src/mbgl/gl/render_custom_layer.cpp b/src/mbgl/gl/render_custom_layer.cpp index f653a9ca7..91df5537a 100644 --- a/src/mbgl/gl/render_custom_layer.cpp +++ b/src/mbgl/gl/render_custom_layer.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -86,7 +87,7 @@ void RenderCustomLayer::render(PaintParameters& paintParameters) { parameters.latitude = state.getLatLng().latitude(); parameters.longitude = state.getLatLng().longitude(); parameters.zoom = state.getZoom(); - parameters.bearing = -state.getBearing() * util::RAD2DEG_D; + parameters.bearing = util::rad2deg(-state.getBearing()); parameters.pitch = state.getPitch(); parameters.fieldOfView = state.getFieldOfView(); mat4 projMatrix; diff --git a/src/mbgl/layout/symbol_layout.cpp b/src/mbgl/layout/symbol_layout.cpp index 242ce15a0..a83787b13 100644 --- a/src/mbgl/layout/symbol_layout.cpp +++ b/src/mbgl/layout/symbol_layout.cpp @@ -1,18 +1,19 @@ #include #include #include +#include #include #include #include #include #include +#include +#include #include #include #include #include #include -#include -#include #include @@ -540,7 +541,7 @@ void SymbolLayout::addFeature(const std::size_t layoutFeatureIndex, const float symbolSpacing = tilePixelRatio * layout->get(); const float textPadding = layout->get() * tilePixelRatio; const float iconPadding = layout->get() * tilePixelRatio; - const float textMaxAngle = layout->get() * util::DEG2RAD_F; + const float textMaxAngle = util::deg2radf(layout->get()); const float iconRotation = layout->evaluate(zoom, feature, canonicalID); const float textRotation = layout->evaluate(zoom, feature, canonicalID); std::array variableTextOffset; diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 1d8e3a3c1..7bc201a16 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -156,7 +157,7 @@ void Map::moveBy(const ScreenCoordinate& point, const AnimationOptions& animatio } void Map::pitchBy(double pitch, const AnimationOptions& animation) { - easeTo(CameraOptions().withPitch((impl->transform.getPitch() * util::RAD2DEG_D) - pitch), animation); + easeTo(CameraOptions().withPitch(util::rad2deg(impl->transform.getPitch()) - pitch), animation); } void Map::scaleBy(double scale, const std::optional& anchor, const AnimationOptions& animation) { @@ -246,8 +247,8 @@ CameraOptions Map::cameraForLatLngs(const std::vector& latLngs, } return mbgl::cameraForLatLngs(latLngs, transform, padding) - .withBearing(-transform.getBearing() * util::RAD2DEG_D) - .withPitch(transform.getPitch() * util::RAD2DEG_D); + .withBearing(util::rad2deg(-transform.getBearing())) + .withPitch(util::rad2deg(transform.getPitch())); } CameraOptions Map::cameraForGeometry(const Geometry& geometry, @@ -346,8 +347,8 @@ BoundOptions Map::getBounds() const { .withLatLngBounds(impl->transform.getState().getLatLngBounds()) .withMinZoom(impl->transform.getState().getMinZoom()) .withMaxZoom(impl->transform.getState().getMaxZoom()) - .withMinPitch(impl->transform.getState().getMinPitch() * util::RAD2DEG_D) - .withMaxPitch(impl->transform.getState().getMaxPitch() * util::RAD2DEG_D); + .withMinPitch(util::rad2deg(impl->transform.getState().getMinPitch())) + .withMaxPitch(util::rad2deg(impl->transform.getState().getMaxPitch())); } // MARK: - Map options diff --git a/src/mbgl/map/map_projection.cpp b/src/mbgl/map/map_projection.cpp index 26a52eb73..9c2eda4dd 100644 --- a/src/mbgl/map/map_projection.cpp +++ b/src/mbgl/map/map_projection.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace mbgl { @@ -32,8 +33,8 @@ CameraOptions MapProjection::getCamera() const { void MapProjection::setVisibleCoordinates(const std::vector& latLngs, const EdgeInsets& padding) { transform->jumpTo(mbgl::cameraForLatLngs(latLngs, *transform, padding) - .withBearing(-transform->getBearing() * util::RAD2DEG_D) - .withPitch(transform->getPitch() * util::RAD2DEG_D)); + .withBearing(util::rad2deg(-transform->getBearing())) + .withPitch(util::rad2deg(transform->getPitch()))); } } // namespace mbgl diff --git a/src/mbgl/map/transform.cpp b/src/mbgl/map/transform.cpp index 3616365ca..fe853e0d7 100644 --- a/src/mbgl/map/transform.cpp +++ b/src/mbgl/map/transform.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -96,8 +97,8 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim const LatLng& unwrappedLatLng = camera.center.value_or(startLatLng); const LatLng& latLng = state.getLatLngBounds() != LatLngBounds() ? unwrappedLatLng : unwrappedLatLng.wrapped(); double zoom = camera.zoom.value_or(getZoom()); - double bearing = camera.bearing ? -*camera.bearing * util::DEG2RAD_D : getBearing(); - double pitch = camera.pitch ? *camera.pitch * util::DEG2RAD_D : getPitch(); + double bearing = camera.bearing ? util::deg2rad(-*camera.bearing) : getBearing(); + double pitch = camera.pitch ? util::deg2rad(*camera.pitch) : getPitch(); if (std::isnan(zoom) || std::isnan(bearing) || std::isnan(pitch)) { if (animation.transitionFinishFn) { @@ -178,8 +179,8 @@ void Transform::flyTo(const CameraOptions& camera, const AnimationOptions& anima const EdgeInsets& padding = camera.padding.value_or(state.getEdgeInsets()); const LatLng& latLng = camera.center.value_or(getLatLng(LatLng::Unwrapped)).wrapped(); double zoom = camera.zoom.value_or(getZoom()); - double bearing = camera.bearing ? -*camera.bearing * util::DEG2RAD_D : getBearing(); - double pitch = camera.pitch ? *camera.pitch * util::DEG2RAD_D : getPitch(); + double bearing = camera.bearing ? util::deg2rad(-*camera.bearing) : getBearing(); + double pitch = camera.pitch ? util::deg2rad(*camera.pitch) : getPitch(); if (std::isnan(zoom) || std::isnan(bearing) || std::isnan(pitch) || state.getSize().isEmpty()) { if (animation.transitionFinishFn) { @@ -382,22 +383,22 @@ void Transform::setMaxZoom(const double maxZoom) { void Transform::setMinPitch(const double minPitch) { if (std::isnan(minPitch)) return; - if (minPitch * util::DEG2RAD_D < util::PITCH_MIN) { + if (util::deg2rad(minPitch) < util::PITCH_MIN) { Log::Warning(Event::General, - "Trying to set minimum pitch below the limit (" + std::to_string(util::PITCH_MIN * util::RAD2DEG_D) + + "Trying to set minimum pitch below the limit (" + std::to_string(util::rad2deg(util::PITCH_MIN)) + " degrees), the value will be clamped."); } - state.setMinPitch(minPitch * util::DEG2RAD_D); + state.setMinPitch(util::deg2rad(minPitch)); } void Transform::setMaxPitch(const double maxPitch) { if (std::isnan(maxPitch)) return; - if (maxPitch * util::DEG2RAD_D > util::PITCH_MAX) { + if (util::deg2rad(maxPitch) > util::PITCH_MAX) { Log::Warning(Event::General, - "Trying to set maximum pitch above the limit (" + std::to_string(util::PITCH_MAX * util::RAD2DEG_D) + + "Trying to set maximum pitch above the limit (" + std::to_string(util::rad2deg(util::PITCH_MAX)) + " degrees), the value will be clamped."); } - state.setMaxPitch(maxPitch * util::DEG2RAD_D); + state.setMaxPitch(util::deg2rad(maxPitch)); } // MARK: - Bearing @@ -418,7 +419,7 @@ void Transform::rotateBy(const ScreenCoordinate& first, center.y = first.y + std::sin(rotateBearing) * heightOffset; } - const double bearing = -(state.getBearing() + util::angle_between(first - center, second - center)) * util::RAD2DEG_D; + const double bearing = -util::rad2deg(state.getBearing() + util::angle_between(first - center, second - center)); easeTo(CameraOptions().withBearing(bearing), animation); } diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index 008a8cf58..f8ebacbe1 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -12,7 +13,7 @@ namespace mbgl { namespace { LatLng latLngFromMercator(Point mercatorCoordinate, LatLng::WrapMode wrapMode = LatLng::WrapMode::Unwrapped) { - return {util::RAD2DEG_D * (2 * std::atan(std::exp(M_PI - mercatorCoordinate.y * util::M2PI)) - M_PI_2), + return {util::rad2deg(2 * std::atan(std::exp(M_PI - mercatorCoordinate.y * util::M2PI)) - M_PI_2), mercatorCoordinate.x * 360.0 - 180.0, wrapMode}; } @@ -420,8 +421,8 @@ CameraOptions TransformState::getCameraOptions(const std::optional& .withCenter(getLatLng()) .withPadding(padding ? padding : edgeInsets) .withZoom(getZoom()) - .withBearing(-bearing * util::RAD2DEG_D) - .withPitch(pitch * util::RAD2DEG_D); + .withBearing(util::rad2deg(-bearing)) + .withPitch(util::rad2deg(pitch)); } // MARK: - EdgeInsets @@ -436,7 +437,7 @@ void TransformState::setEdgeInsets(const EdgeInsets& val) { // MARK: - Position LatLng TransformState::getLatLng(LatLng::WrapMode wrapMode) const { - return {util::RAD2DEG_D * (2 * std::atan(std::exp(y / Cc)) - 0.5 * M_PI), -x / Bc, wrapMode}; + return {util::rad2deg(2 * std::atan(std::exp(y / Cc)) - 0.5 * M_PI), -x / Bc, wrapMode}; } double TransformState::pixel_x() const { @@ -775,7 +776,7 @@ void TransformState::setLatLngZoom(const LatLng& latLng, double zoom) { Cc = newWorldSize / util::M2PI; const double m = 1 - 1e-15; - const double f = util::clamp(std::sin(util::DEG2RAD_D * constrained.latitude()), -m, m); + const double f = util::clamp(std::sin(util::deg2rad(constrained.latitude())), -m, m); ScreenCoordinate point = { -constrained.longitude() * Bc, diff --git a/src/mbgl/renderer/layers/render_hillshade_layer.cpp b/src/mbgl/renderer/layers/render_hillshade_layer.cpp index e8955c474..6f925005c 100644 --- a/src/mbgl/renderer/layers/render_hillshade_layer.cpp +++ b/src/mbgl/renderer/layers/render_hillshade_layer.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace mbgl { @@ -41,7 +42,7 @@ std::array RenderHillshadeLayer::getLatRange(const UnwrappedTileID& id std::array RenderHillshadeLayer::getLight(const PaintParameters& parameters) { const auto& evaluated = static_cast(*evaluatedProperties).evaluated; - float azimuthal = evaluated.get() * util::DEG2RAD_F; + float azimuthal = util::deg2radf(evaluated.get()); if (evaluated.get() == HillshadeIlluminationAnchorType::Viewport) azimuthal = azimuthal - static_cast(parameters.state.getBearing()); return {{evaluated.get(), azimuthal}}; } diff --git a/src/mbgl/renderer/layers/render_location_indicator_layer.cpp b/src/mbgl/renderer/layers/render_location_indicator_layer.cpp index 85f13a028..14b55c63b 100644 --- a/src/mbgl/renderer/layers/render_location_indicator_layer.cpp +++ b/src/mbgl/renderer/layers/render_location_indicator_layer.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -94,15 +95,15 @@ protected: } float dot(const vec2& v2) const { return x * v2.x + y * v2.y; } vec2 rotated(float degrees) const { - const float cs = std::cos(degrees * util::DEG2RAD_F); - const float sn = std::sin(degrees * util::DEG2RAD_F); + const float cs = std::cos(util::deg2radf(degrees)); + const float sn = std::sin(util::deg2radf(degrees)); return vec2{x * cs + y * sn, x * sn + y * cs}.normalized(); } float bearing() const { const vec2 norm = normalized(); // From theta to bearing - return util::wrap(static_cast(M_PI_2) - std::atan2(-norm.y, norm.x), 0.0f, static_cast(M_PI * 2.0)) * util::RAD2DEG_F; + return util::rad2degf(util::wrap(static_cast(M_PI_2) - std::atan2(-norm.y, norm.x), 0.0f, static_cast(M_PI * 2.0))); } Point toPoint() const { return {x, y}; } @@ -488,7 +489,7 @@ protected: Point center = project(params.puckPosition, s); circle[0] = {0, 0}; - const auto mapBearing = static_cast(util::wrap(util::RAD2DEG_D * params.bearing, 0.0, util::DEGREES_MAX)); + const auto mapBearing = static_cast(util::wrap(util::rad2deg(params.bearing), 0.0, util::DEGREES_MAX)); for (unsigned long i = 1; i <= numVtxCircumference; ++i) { const float bearing_ = static_cast(i - 1) * bearingStep - mapBearing; Point poc = ruler.destination(centerPoint, params.errorRadiusMeters, bearing_); @@ -596,8 +597,8 @@ protected: for (unsigned long i = 0; i < 4; ++i) { const auto b = util::wrap(static_cast(params.puckBearing) + bearings[i], 0.0f, 360.0f); - const Point cornerDirection{std::sin(util::DEG2RAD_D * b), - -std::cos(util::DEG2RAD_D * b)}; + const Point cornerDirection{std::sin(util::deg2rad(b)), + -std::cos(util::deg2rad(b))}; Point shadowOffset = cornerDirection * shadowRadius; Point puckOffset = cornerDirection * puckRadius; @@ -816,7 +817,7 @@ void RenderLocationIndicatorLayer::prepare(const LayerPrepareParameters& p) { renderImpl->parameters.latitude = state.getLatLng().latitude(); renderImpl->parameters.longitude = state.getLatLng().longitude(); renderImpl->parameters.zoom = state.getZoom(); - renderImpl->parameters.bearing = -state.getBearing() * util::RAD2DEG_D; + renderImpl->parameters.bearing = util::rad2deg(-state.getBearing()); renderImpl->parameters.pitch = state.getPitch(); mat4 projMatrix; state.getProjMatrix(projMatrix); diff --git a/src/mbgl/renderer/layers/render_raster_layer.cpp b/src/mbgl/renderer/layers/render_raster_layer.cpp index 650537732..4aeff8f13 100644 --- a/src/mbgl/renderer/layers/render_raster_layer.cpp +++ b/src/mbgl/renderer/layers/render_raster_layer.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace mbgl { @@ -67,7 +68,7 @@ static float contrastFactor(float contrast) { } static std::array spinWeights(float spin) { - spin *= util::DEG2RAD_F; + spin = util::deg2radf(spin); float s = std::sin(spin); float c = std::cos(spin); std::array spin_weights = {{ diff --git a/src/mbgl/style/expression/within.cpp b/src/mbgl/style/expression/within.cpp index edc64c5dc..f56e95aa1 100644 --- a/src/mbgl/style/expression/within.cpp +++ b/src/mbgl/style/expression/within.cpp @@ -10,6 +10,7 @@ #include #include +#include #include namespace mbgl { @@ -20,7 +21,7 @@ Point latLonToTileCoodinates(const Point& point, const mbgl::Ca auto x = (point.x + util::LONGITUDE_MAX) * size / util::DEGREES_MAX; auto y = - (util::LONGITUDE_MAX - (std::log(std::tan(point.y * M_PI / util::DEGREES_MAX + M_PI / 4.0)) * util::RAD2DEG_D)) * + (util::LONGITUDE_MAX - util::rad2deg(std::log(std::tan(point.y * M_PI / util::DEGREES_MAX + M_PI / 4.0)))) * size / util::DEGREES_MAX; Point p; diff --git a/src/mbgl/text/collision_feature.cpp b/src/mbgl/text/collision_feature.cpp index 81a907749..f5201596e 100644 --- a/src/mbgl/text/collision_feature.cpp +++ b/src/mbgl/text/collision_feature.cpp @@ -1,5 +1,6 @@ #include #include +#include #include namespace mbgl { @@ -46,7 +47,7 @@ CollisionFeature::CollisionFeature(const GeometryCoordinates& line, if (rotate) { // Account for *-rotate in point collision boxes // Doesn't account for icon-text-fit - const float rotateRadians = rotate * util::DEG2RAD_F; + const float rotateRadians = util::deg2radf(rotate); const Point tl = util::rotate(Point(x1, y1), rotateRadians); const Point tr = util::rotate(Point(x2, y1), rotateRadians); diff --git a/src/mbgl/text/quads.cpp b/src/mbgl/text/quads.cpp index 2ee343cf5..6bf2afcae 100644 --- a/src/mbgl/text/quads.cpp +++ b/src/mbgl/text/quads.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -108,7 +109,7 @@ SymbolQuads getIconQuads(const PositionedIcon& shapedIcon, std::optional> matrix{std::nullopt}; if (iconRotate) { - const float angle = iconRotate * util::DEG2RAD_F; + const float angle = util::deg2radf(iconRotate); const float angle_sin = std::sin(angle); const float angle_cos = std::cos(angle); matrix = std::array{{angle_cos, -angle_sin, angle_sin, angle_cos}}; @@ -200,7 +201,7 @@ SymbolQuads getGlyphQuads(const Shaping& shapedText, const style::SymbolPlacementType placement, const ImageMap& imageMap, bool allowVerticalPlacement) { - const float textRotate = layout.get() * util::DEG2RAD_F; + const float textRotate = util::deg2radf(layout.get()); const bool alongLine = layout.get() == AlignmentType::Map && placement != SymbolPlacementType::Point; SymbolQuads quads; diff --git a/src/mbgl/tile/geometry_tile_data.cpp b/src/mbgl/tile/geometry_tile_data.cpp index c281900e7..9062ebad4 100644 --- a/src/mbgl/tile/geometry_tile_data.cpp +++ b/src/mbgl/tile/geometry_tile_data.cpp @@ -118,7 +118,7 @@ Feature::geometry_type convertGeometry(const GeometryTileFeature& geometryTileFe double y2 = 180 - (p.y + y0) * 360 / size; return Point( (p.x + x0) * 360 / size - 180, - 360.0 / M_PI * std::atan(std::exp(y2 * M_PI / 180)) - 90.0 + std::atan(std::exp(y2 * M_PI / 180)) * 360.0 / M_PI - 90.0 ); }; diff --git a/src/mbgl/util/camera.cpp b/src/mbgl/util/camera.cpp index d9010fe8f..e6a85dcfc 100644 --- a/src/mbgl/util/camera.cpp +++ b/src/mbgl/util/camera.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -34,7 +35,7 @@ static double mercatorYfromLat(double lat) { } static double latFromMercatorY(double y) { - return util::RAD2DEG_D * (2.0 * std::atan(std::exp(M_PI - y * util::M2PI)) - M_PI_2); + return util::rad2deg(2.0 * std::atan(std::exp(M_PI - y * util::M2PI)) - M_PI_2); } static double lngFromMercatorX(double x) { @@ -113,7 +114,7 @@ mat4 Camera::getWorldToCamera(double scale, bool flippedY) const { // cameraToWorld: (flip * cam^-1 * zScale)^-1 => (zScale^-1 * cam * flip^-1) const double worldSize = Projection::worldSize(scale); const double latitude = latFromMercatorY(getColumn(transform, 3)[1]); - const double pixelsPerMeter = worldSize / (std::cos(latitude * util::DEG2RAD_D) * util::M2PI * util::EARTH_RADIUS_M); + const double pixelsPerMeter = worldSize / (std::cos(util::deg2rad(latitude)) * util::M2PI * util::EARTH_RADIUS_M); // Compute inverse of the camera matrix mat4 result = orientation.conjugate().toRotationMatrix(); @@ -256,7 +257,7 @@ void FreeCameraOptions::lookAtPoint(const LatLng& location, const std::optional< } void FreeCameraOptions::setPitchBearing(double pitch, double bearing) { - orientation = util::orientationFromPitchBearing(pitch * util::DEG2RAD_D, bearing * util::DEG2RAD_D).m; + orientation = util::orientationFromPitchBearing(util::deg2rad(pitch), util::deg2rad(bearing)).m; } } // namespace mbgl diff --git a/src/mbgl/util/geo.cpp b/src/mbgl/util/geo.cpp index 8ecc78c4c..649bd8f7d 100644 --- a/src/mbgl/util/geo.cpp +++ b/src/mbgl/util/geo.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -12,7 +13,7 @@ namespace { double lat_(const uint8_t z, const int64_t y) { const double n = M_PI - 2.0 * M_PI * y / std::pow(2.0, z); - return util::RAD2DEG_D * std::atan(0.5 * (std::exp(n) - std::exp(-n))); + return util::rad2deg(std::atan(0.5 * (std::exp(n) - std::exp(-n)))); } double lon_(const uint8_t z, const int64_t x) { diff --git a/test/map/transform.test.cpp b/test/map/transform.test.cpp index 1f9d5c802..3d7931d4a 100644 --- a/test/map/transform.test.cpp +++ b/test/map/transform.test.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -68,7 +69,7 @@ TEST(Transform, InvalidBearing) { ASSERT_DOUBLE_EQ(0, transform.getLatLng().latitude()); ASSERT_DOUBLE_EQ(0, transform.getLatLng().longitude()); ASSERT_DOUBLE_EQ(1, transform.getZoom()); - ASSERT_DOUBLE_EQ(-2.0 * util::DEG2RAD_D, transform.getBearing()); + ASSERT_DOUBLE_EQ(util::deg2rad(-2.0), transform.getBearing()); const double invalid = NAN; @@ -76,7 +77,7 @@ TEST(Transform, InvalidBearing) { ASSERT_DOUBLE_EQ(0, transform.getLatLng().latitude()); ASSERT_DOUBLE_EQ(0, transform.getLatLng().longitude()); ASSERT_DOUBLE_EQ(1, transform.getZoom()); - ASSERT_DOUBLE_EQ(-2.0 * util::DEG2RAD_D, transform.getBearing()); + ASSERT_DOUBLE_EQ(util::deg2rad(-2.0), transform.getBearing()); } TEST(Transform, IntegerZoom) { @@ -272,24 +273,24 @@ TEST(Transform, Anchor) { ASSERT_DOUBLE_EQ(latLng.longitude(), transform.getLatLng().longitude()); transform.jumpTo(CameraOptions().withBearing(45.0).withAnchor(anchorPoint)); - ASSERT_DOUBLE_EQ(-45.0 * util::DEG2RAD_D, transform.getBearing()); + ASSERT_DOUBLE_EQ(util::deg2rad(-45.0), transform.getBearing()); // Anchor coordinates are imprecise because we are converting from an integer pixel. ASSERT_NEAR(anchorLatLng.latitude(), transform.getLatLng().latitude(), 0.5); ASSERT_NEAR(anchorLatLng.longitude(), transform.getLatLng().longitude(), 0.5); transform.jumpTo(CameraOptions().withCenter(latLng).withZoom(10.0).withPitch(10.0)); - ASSERT_DOUBLE_EQ(10.0 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(10.0), transform.getPitch()); ASSERT_DOUBLE_EQ(latLng.latitude(), transform.getLatLng().latitude()); ASSERT_DOUBLE_EQ(latLng.longitude(), transform.getLatLng().longitude()); transform.jumpTo(CameraOptions().withPitch(15.0)); - ASSERT_DOUBLE_EQ(15.0 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(15.0), transform.getPitch()); ASSERT_DOUBLE_EQ(latLng.latitude(), transform.getLatLng().latitude()); ASSERT_DOUBLE_EQ(latLng.longitude(), transform.getLatLng().longitude()); transform.jumpTo(CameraOptions().withPitch(20.0).withAnchor(anchorPoint)); - ASSERT_DOUBLE_EQ(20.0 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(20.0), transform.getPitch()); // Anchor coordinates are imprecise because we are converting from an integer pixel. ASSERT_NEAR(anchorLatLng.latitude(), transform.getLatLng().latitude(), 0.5); @@ -811,15 +812,15 @@ TEST(Transform, InvalidPitch) { transform.jumpTo(CameraOptions().withZoom(1.0).withPitch(45)); ASSERT_DOUBLE_EQ(1, transform.getZoom()); - ASSERT_DOUBLE_EQ(45 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(45), transform.getPitch()); const double invalid = NAN; transform.jumpTo(CameraOptions().withPitch(invalid)); - ASSERT_DOUBLE_EQ(45 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(45), transform.getPitch()); transform.jumpTo(CameraOptions().withPitch(60)); - ASSERT_DOUBLE_EQ(60 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(60), transform.getPitch()); } TEST(Transform, MinMaxPitch) { @@ -834,17 +835,17 @@ TEST(Transform, MinMaxPitch) { transform.jumpTo(CameraOptions().withZoom(1.0).withPitch(60)); ASSERT_DOUBLE_EQ(1, transform.getZoom()); ASSERT_DOUBLE_EQ(transform.getState().getMaxPitch(), transform.getPitch()); - ASSERT_DOUBLE_EQ(60 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(60), transform.getPitch()); transform.setMaxPitch(70); transform.jumpTo(CameraOptions().withPitch(70)); ASSERT_DOUBLE_EQ(transform.getState().getMaxPitch(), transform.getPitch()); - ASSERT_DOUBLE_EQ(60 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(60), transform.getPitch()); transform.setMaxPitch(45); transform.jumpTo(CameraOptions().withPitch(60)); ASSERT_DOUBLE_EQ(transform.getState().getMaxPitch(), transform.getPitch()); - ASSERT_DOUBLE_EQ(45 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(45), transform.getPitch()); transform.jumpTo(CameraOptions().withPitch(0)); ASSERT_DOUBLE_EQ(transform.getState().getMinPitch(), transform.getPitch()); @@ -858,18 +859,18 @@ TEST(Transform, MinMaxPitch) { transform.setMinPitch(15); transform.jumpTo(CameraOptions().withPitch(0)); ASSERT_DOUBLE_EQ(transform.getState().getMinPitch(), transform.getPitch()); - ASSERT_DOUBLE_EQ(15 * util::DEG2RAD_D, transform.getPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(15), transform.getPitch()); transform.setMinPitch(45); - ASSERT_DOUBLE_EQ(45 * util::DEG2RAD_D, transform.getState().getMinPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(45), transform.getState().getMinPitch()); transform.setMaxPitch(45); - ASSERT_DOUBLE_EQ(45 * util::DEG2RAD_D, transform.getState().getMaxPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(45), transform.getState().getMaxPitch()); transform.setMaxPitch(10); - ASSERT_DOUBLE_EQ(45 * util::DEG2RAD_D, transform.getState().getMaxPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(45), transform.getState().getMaxPitch()); transform.setMinPitch(60); - ASSERT_DOUBLE_EQ(45 * util::DEG2RAD_D, transform.getState().getMinPitch()); + ASSERT_DOUBLE_EQ(util::deg2rad(45), transform.getState().getMinPitch()); } static const double abs_double_error = 1e-5; @@ -968,26 +969,26 @@ TEST(Transform, FreeCameraOptionsSetOrientation) { EXPECT_DOUBLE_EQ(0.0, transform.getState().getX()); EXPECT_DOUBLE_EQ(0.0, transform.getState().getY()); - options.orientation = Quaternion::fromAxisAngle(vec3{{1.0, 0.0, 0.0}}, -60.0 * util::DEG2RAD_D).m; + options.orientation = Quaternion::fromAxisAngle(vec3{{1.0, 0.0, 0.0}}, util::deg2rad(-60.0)).m; transform.setFreeCameraOptions(options); EXPECT_DOUBLE_EQ(0.0, transform.getState().getBearing()); - EXPECT_DOUBLE_EQ(60.0 * util::DEG2RAD_D, transform.getState().getPitch()); + EXPECT_DOUBLE_EQ(util::deg2rad(60.0), transform.getState().getPitch()); EXPECT_DOUBLE_EQ(0.0, transform.getState().getX()); EXPECT_DOUBLE_EQ(206.0, transform.getState().getY()); - options.orientation = Quaternion::fromAxisAngle(vec3{{0.0, 0.0, 1.0}}, 56.0 * util::DEG2RAD_D).m; + options.orientation = Quaternion::fromAxisAngle(vec3{{0.0, 0.0, 1.0}}, util::deg2rad(56.0)).m; transform.setFreeCameraOptions(options); - EXPECT_DOUBLE_EQ(-56.0 * util::DEG2RAD_D, transform.getState().getBearing()); + EXPECT_DOUBLE_EQ(util::deg2rad(-56.0), transform.getState().getBearing()); EXPECT_DOUBLE_EQ(0.0, transform.getState().getPitch()); EXPECT_DOUBLE_EQ(0.0, transform.getState().getX()); EXPECT_NEAR(152.192378, transform.getState().getY(), 1e-6); - options.orientation = Quaternion::fromEulerAngles(0.0, 0.0, -179.0 * util::DEG2RAD_D) - .multiply(Quaternion::fromEulerAngles(-30.0 * util::DEG2RAD_D, 0.0, 0.0)) + options.orientation = Quaternion::fromEulerAngles(0.0, 0.0, util::deg2rad(-179.0)) + .multiply(Quaternion::fromEulerAngles(util::deg2rad(-30.0), 0.0, 0.0)) .m; transform.setFreeCameraOptions(options); - EXPECT_DOUBLE_EQ(179.0 * util::DEG2RAD_D, transform.getState().getBearing()); - EXPECT_DOUBLE_EQ(30.0 * util::DEG2RAD_D, transform.getState().getPitch()); + EXPECT_DOUBLE_EQ(util::deg2rad(179.0), transform.getState().getBearing()); + EXPECT_DOUBLE_EQ(util::deg2rad(30.0), transform.getState().getPitch()); EXPECT_NEAR(1.308930, transform.getState().getX(), 1e-6); EXPECT_NEAR(56.813889, transform.getState().getY(), 1e-6); } @@ -1004,7 +1005,7 @@ TEST(Transform, FreeCameraOptionsClampPitch) { FreeCameraOptions options; vec3 right, up, forward; - options.orientation = Quaternion::fromAxisAngle(vec3{{1.0, 0.0, 0.0}}, -85.0 * util::DEG2RAD_D).m; + options.orientation = Quaternion::fromAxisAngle(vec3{{1.0, 0.0, 0.0}}, util::deg2rad(-85.0)).m; transform.setFreeCameraOptions(options); EXPECT_DOUBLE_EQ(util::PITCH_MAX, transform.getState().getPitch()); std::tie(right, up, forward) = rotatedFrame(transform.getFreeCameraOptions().orientation.value()); @@ -1022,14 +1023,14 @@ TEST(Transform, FreeCameraOptionsClampToBounds) { // Place camera to an arbitrary position looking away from the map options.position = vec3{{-100.0, -10000.0, 1000.0}}; - options.orientation = Quaternion::fromEulerAngles(-45.0 * util::DEG2RAD_D, 0.0, 0.0).m; + options.orientation = Quaternion::fromEulerAngles(util::deg2rad(-45.0), 0.0, 0.0).m; transform.setFreeCameraOptions(options); // Map center should be clamped to width/2 pixels away from map borders EXPECT_DOUBLE_EQ(206.0, transform.getState().getX()); EXPECT_DOUBLE_EQ(206.0, transform.getState().getY()); EXPECT_DOUBLE_EQ(0.0, transform.getState().getBearing()); - EXPECT_DOUBLE_EQ(45.0 * util::DEG2RAD_D, transform.getState().getPitch()); + EXPECT_DOUBLE_EQ(util::deg2rad(45.0), transform.getState().getPitch()); vec3 right, up, forward; std::tie(right, up, forward) = rotatedFrame(transform.getFreeCameraOptions().orientation.value()); @@ -1070,7 +1071,7 @@ TEST(Transform, FreeCameraOptionsOrientationRoll) { EXPECT_NEAR(options.orientation.value()[2], orientationWithoutRoll.z, 1e-9); EXPECT_NEAR(options.orientation.value()[3], orientationWithoutRoll.w, 1e-9); - EXPECT_NEAR(45.0 * util::DEG2RAD_D, transform.getState().getPitch(), 1e-9); + EXPECT_NEAR(util::deg2rad(45.0), transform.getState().getPitch(), 1e-9); EXPECT_NEAR(0.0, transform.getState().getBearing(), 1e-9); EXPECT_NEAR(0.0, transform.getState().getX(), 1e-9); EXPECT_NEAR(150.0, transform.getState().getY(), 1e-9); diff --git a/test/util/camera.test.cpp b/test/util/camera.test.cpp index 7e0ebdca3..caabe0d00 100644 --- a/test/util/camera.test.cpp +++ b/test/util/camera.test.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -27,12 +28,12 @@ TEST(FreeCameraOptions, SetLocation) { ASSERT_THAT(options.position.value(), Vec3NearEquals1E7(vec3{{0.0, 0.4282409625, 0.000027532812465}})); options.setLocation( - {{util::LATITUDE_MAX, 0.0}, util::EARTH_RADIUS_M * M_PI * std::cos(util::LATITUDE_MAX * util::DEG2RAD_D)}); + {{util::LATITUDE_MAX, 0.0}, util::EARTH_RADIUS_M * M_PI * std::cos(util::deg2rad(util::LATITUDE_MAX))}); ASSERT_TRUE(options.position); ASSERT_THAT(options.position.value(), Vec3NearEquals1E7(vec3{{0.5, 0.0, 0.5}})); options.setLocation( - {{-util::LATITUDE_MAX, 0.0}, util::EARTH_RADIUS_M * M_PI * std::cos(-util::LATITUDE_MAX * util::DEG2RAD_D)}); + {{-util::LATITUDE_MAX, 0.0}, util::EARTH_RADIUS_M * M_PI * std::cos(util::deg2rad(-util::LATITUDE_MAX))}); ASSERT_TRUE(options.position); ASSERT_THAT(options.position.value(), Vec3NearEquals1E7(vec3{{0.5, 1.0, 0.5}})); } @@ -240,8 +241,8 @@ TEST(FreeCameraOptions, SetPitchBearing) { ASSERT_THAT(up, Vec3NearEquals1E7(vec3{{0.0, 1.0, 0.0}})); ASSERT_THAT(forward, Vec3NearEquals1E7(vec3{{0.0, 0.0, -1.0}})); - const double cos60 = std::cos(60.0 * util::DEG2RAD_D); - const double sin60 = std::sin(60.0 * util::DEG2RAD_D); + const double cos60 = std::cos(util::deg2rad(60.0)); + const double sin60 = std::sin(util::deg2rad(60.0)); options.setPitchBearing(60.0, 0.0); ASSERT_TRUE(options.orientation); diff --git a/test/util/tile_cover.test.cpp b/test/util/tile_cover.test.cpp index a88a2146d..07b5e78c4 100644 --- a/test/util/tile_cover.test.cpp +++ b/test/util/tile_cover.test.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include /* srand, rand */ @@ -46,8 +47,8 @@ TEST(TileCover, PitchIssue15442) { transform.resize({ 412, 691 }); transform.jumpTo(CameraOptions().withCenter(LatLng { 59.116898740996106, 91.565660781803615, }) - .withZoom(2.0551126748417214).withBearing(0.74963938256567264 * util::RAD2DEG_D) - .withPitch(1.0471975511965976 * util::RAD2DEG_D)); + .withZoom(2.0551126748417214).withBearing(util::rad2deg(0.74963938256567264)) + .withPitch(util::rad2deg(1.0471975511965976))); EXPECT_EQ((std::vector{ {2, 3, 1}, {2, 2, 1}, {2, 3, 0}, {2, 2, 0}, {2, 1, {2, 0, 0}}, {2, 1, {2, 1, 0}}}), @@ -61,7 +62,7 @@ TEST(TileCover, PitchOverAllowedByContentInsets) { transform.jumpTo(CameraOptions().withCenter(LatLng { 0.1, -0.1 }).withPadding(EdgeInsets { 376, 0, 0, 0 }) .withZoom(8.0).withBearing(45.0).withPitch(60.0)); // Top padding of 376 leads to capped pitch. See Transform::getMaxPitchForEdgeInsets. - EXPECT_LE(transform.getPitch() + 0.001, util::DEG2RAD_D * 60); + EXPECT_LE(transform.getPitch() + 0.001, util::deg2rad(60)); EXPECT_EQ( (std::vector{{3, 4, 3}, {3, 3, 3}, {3, 4, 4}, {3, 3, 4}, {3, 4, 2}, {3, 5, 3}, {3, 5, 2}}),