diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a4ec446b..f6012273f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452) - Misc: - CHANGED: Use std::string_view::starts_with instead of boost::starts_with. [#6918](https://github.com/Project-OSRM/osrm-backend/pull/6918) + - CHANGED: Get rid of boost::math::constants::* and M_PI in favor of std::numbers. [#6916](https://github.com/Project-OSRM/osrm-backend/pull/6916) - CHANGED: Make constants in PackedVector constexpr. [#6917](https://github.com/Project-OSRM/osrm-backend/pull/6917) - CHANGED: Use std::variant instead of mapbox::util::variant. [#6903](https://github.com/Project-OSRM/osrm-backend/pull/6903) - CHANGED: Bump rapidjson to version f9d53419e912910fd8fa57d5705fa41425428c35 [#6906](https://github.com/Project-OSRM/osrm-backend/pull/6906) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1686da493..1876d6b85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -266,7 +266,6 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") add_dependency_defines(-DBOOST_LIB_DIAGNOSTIC) add_dependency_defines(-D_CRT_SECURE_NO_WARNINGS) add_dependency_defines(-DNOMINMAX) # avoid min and max macros that can break compilation - add_dependency_defines(-D_USE_MATH_DEFINES) #needed for M_PI with cmath.h add_dependency_defines(-D_WIN32_WINNT=0x0501) add_dependency_defines(-DXML_STATIC) find_library(ws2_32_LIBRARY_PATH ws2_32) diff --git a/include/engine/map_matching/bayes_classifier.hpp b/include/engine/map_matching/bayes_classifier.hpp index 5dd5813c3..7d89e3d64 100644 --- a/include/engine/map_matching/bayes_classifier.hpp +++ b/include/engine/map_matching/bayes_classifier.hpp @@ -6,7 +6,7 @@ #include #include -#include +#include namespace osrm::engine::map_matching { @@ -21,10 +21,8 @@ struct NormalDistribution // FIXME implement log-probability version since it's faster double Density(const double val) const { - using namespace boost::math::constants; - const double x = val - mean; - return 1.0 / (std::sqrt(two_pi()) * standard_deviation) * + return 1.0 / (std::sqrt(2 * std::numbers::pi) * standard_deviation) * std::exp(-x * x / (standard_deviation * standard_deviation)); } diff --git a/include/engine/map_matching/hidden_markov_model.hpp b/include/engine/map_matching/hidden_markov_model.hpp index 54505c8ec..f719b076e 100644 --- a/include/engine/map_matching/hidden_markov_model.hpp +++ b/include/engine/map_matching/hidden_markov_model.hpp @@ -4,7 +4,7 @@ #include "util/integer_range.hpp" #include -#include +#include #include @@ -14,7 +14,7 @@ namespace osrm::engine::map_matching { -static const double log_2_pi = std::log(2. * boost::math::constants::pi()); +static const double log_2_pi = std::log(2. * std::numbers::pi); static const double IMPOSSIBLE_LOG_PROB = -std::numeric_limits::infinity(); static const double MINIMAL_LOG_PROB = std::numeric_limits::lowest(); static const std::size_t INVALID_STATE = std::numeric_limits::max(); diff --git a/include/engine/map_matching/matching_confidence.hpp b/include/engine/map_matching/matching_confidence.hpp index 4b6b00a83..41d4a731d 100644 --- a/include/engine/map_matching/matching_confidence.hpp +++ b/include/engine/map_matching/matching_confidence.hpp @@ -2,7 +2,7 @@ #define ENGINE_MAP_MATCHING_CONFIDENCE_HPP #include "engine/map_matching/bayes_classifier.hpp" - +#include #include namespace osrm::engine::map_matching diff --git a/include/util/cheap_ruler.hpp b/include/util/cheap_ruler.hpp index 67df8d6f2..386a1785d 100644 --- a/include/util/cheap_ruler.hpp +++ b/include/util/cheap_ruler.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -37,7 +38,7 @@ class CheapRuler static constexpr double FE = 1.0 / 298.257223563; // flattening static constexpr double E2 = FE * (2 - FE); - static constexpr double RAD = M_PI / 180.0; + static constexpr double RAD = std::numbers::pi / 180.0; public: explicit CheapRuler(double latitude) diff --git a/include/util/coordinate_calculation.hpp b/include/util/coordinate_calculation.hpp index 1013555ed..48a5753e2 100644 --- a/include/util/coordinate_calculation.hpp +++ b/include/util/coordinate_calculation.hpp @@ -3,7 +3,7 @@ #include "util/coordinate.hpp" -#include +#include #include #include @@ -23,17 +23,9 @@ const constexpr double RAD_TO_DEGREE = 1. / DEGREE_TO_RAD; // The IUGG value for the equatorial radius is 6378.137 km (3963.19 miles) const constexpr long double EARTH_RADIUS = 6372797.560856; -inline double degToRad(const double degree) -{ - using namespace boost::math::constants; - return degree * (pi() / 180.0); -} +inline double degToRad(const double degree) { return degree * (std::numbers::pi / 180.0); } -inline double radToDeg(const double radian) -{ - using namespace boost::math::constants; - return radian * (180.0 * (1. / pi())); -} +inline double radToDeg(const double radian) { return radian * (180.0 * std::numbers::inv_pi); } } // namespace detail const constexpr static double METERS_PER_DEGREE_LAT = 110567.0; diff --git a/include/util/trigonometry_table.hpp b/include/util/trigonometry_table.hpp index 412674960..3186df831 100644 --- a/include/util/trigonometry_table.hpp +++ b/include/util/trigonometry_table.hpp @@ -6,7 +6,7 @@ #include -#include +#include namespace osrm::util { @@ -356,25 +356,21 @@ constexpr unsigned short atan_table[4096] = { 0xffe0, 0xffea, 0xfff4, 0xffff}; // max value is pi/4 -#ifdef _MSC_VER // TODO: remove as soon as boost allows C++14 features with Visual Studio -const constexpr double SCALING_FACTOR = 4. / M_PI * 0xFFFF; -#else -const constexpr double SCALING_FACTOR = 4. / boost::math::constants::pi() * 0xFFFF; -#endif +const constexpr double SCALING_FACTOR = 4. * std::numbers::inv_pi * 0xFFFF; inline double atan2_lookup(double y, double x) { - using namespace boost::math::constants; + static constexpr auto half_pi = std::numbers::pi * 0.5; if (std::abs(x) < std::numeric_limits::epsilon()) { if (y >= 0.) { - return half_pi(); + return half_pi; } else { - return -half_pi(); + return -half_pi; } } @@ -405,25 +401,25 @@ inline double atan2_lookup(double y, double x) case 0: break; case 1: - angle = pi() - angle; + angle = std::numbers::pi - angle; break; case 2: angle = -angle; break; case 3: - angle = -pi() + angle; + angle = -std::numbers::pi + angle; break; case 4: - angle = half_pi() - angle; + angle = half_pi - angle; break; case 5: - angle = half_pi() + angle; + angle = half_pi + angle; break; case 6: - angle = -half_pi() + angle; + angle = -half_pi + angle; break; case 7: - angle = -half_pi() - angle; + angle = -half_pi - angle; break; } return angle; diff --git a/include/util/web_mercator.hpp b/include/util/web_mercator.hpp index 0d428486b..9fcfa07c3 100644 --- a/include/util/web_mercator.hpp +++ b/include/util/web_mercator.hpp @@ -3,7 +3,7 @@ #include "util/coordinate.hpp" -#include +#include namespace osrm::util::web_mercator { @@ -14,7 +14,7 @@ const constexpr double RAD_TO_DEGREE = 1. / DEGREE_TO_RAD; // radius used by WGS84 const constexpr double EARTH_RADIUS_WGS84 = 6378137.0; // earth circumference devided by 2 -const constexpr double MAXEXTENT = EARTH_RADIUS_WGS84 * boost::math::constants::pi(); +const constexpr double MAXEXTENT = EARTH_RADIUS_WGS84 * std::numbers::pi; // ^ math functions are not constexpr since they have side-effects (setting errno) :( const constexpr double EPSG3857_MAX_LATITUDE = 85.051128779806592378; // 90(4*atan(exp(pi))/pi-1) const constexpr double MAX_LONGITUDE = 180.0; @@ -103,8 +103,8 @@ inline void pixelToDegree(const double shift, double &x, double &y) const double b = shift / 2.0; x = (x - b) / shift * 360.0; // FIXME needs to be simplified - const double g = (y - b) / -(shift / (2 * M_PI)) / detail::DEGREE_TO_RAD; - static_assert(detail::DEGREE_TO_RAD / (2 * M_PI) - 1 / 360. < 0.0001, ""); + const double g = (y - b) / -(shift * 0.5 * std::numbers::inv_pi) / detail::DEGREE_TO_RAD; + static_assert(detail::DEGREE_TO_RAD * 0.5 * std::numbers::inv_pi - 1 / 360. < 0.0001, ""); y = static_cast(yToLat(g)); } diff --git a/src/extractor/intersection/intersection_analysis.cpp b/src/extractor/intersection/intersection_analysis.cpp index 4ea3985ec..7ee296f11 100644 --- a/src/extractor/intersection/intersection_analysis.cpp +++ b/src/extractor/intersection/intersection_analysis.cpp @@ -6,6 +6,7 @@ #include "util/coordinate_calculation.hpp" #include +#include namespace osrm::extractor::intersection { @@ -79,11 +80,11 @@ namespace { double findAngleBisector(double alpha, double beta) { - alpha *= M_PI / 180.; - beta *= M_PI / 180.; + alpha *= std::numbers::pi / 180.; + beta *= std::numbers::pi / 180.; const auto average = - 180. * std::atan2(std::sin(alpha) + std::sin(beta), std::cos(alpha) + std::cos(beta)) / - M_PI; + 180. * std::atan2(std::sin(alpha) + std::sin(beta), std::cos(alpha) + std::cos(beta)) * + std::numbers::inv_pi; return std::fmod(average + 360., 360.); } diff --git a/src/extractor/intersection/mergable_road_detector.cpp b/src/extractor/intersection/mergable_road_detector.cpp index 449a15e9e..adfb601cb 100644 --- a/src/extractor/intersection/mergable_road_detector.cpp +++ b/src/extractor/intersection/mergable_road_detector.cpp @@ -351,7 +351,7 @@ bool MergableRoadDetector::IsCircularShape(const NodeID intersection_node, // ---- ---- // \ / // ------- - const auto constexpr CIRCULAR_POLYGON_ISOPERIMETRIC_LOWER_BOUND = 0.85 / (4 * M_PI); + const auto constexpr CIRCULAR_POLYGON_ISOPERIMETRIC_LOWER_BOUND = 0.85 / (4 * std::numbers::pi); if (connect_again && coordinates_to_the_left.front() == coordinates_to_the_left.back()) { // if the left and right roads connect again and are closed polygons ... const auto area = util::coordinate_calculation::computeArea(coordinates_to_the_left); @@ -359,7 +359,7 @@ bool MergableRoadDetector::IsCircularShape(const NodeID intersection_node, const auto area_to_squared_perimeter_ratio = std::abs(area) / (perimeter * perimeter); // then don't merge roads if A/L² is greater than the lower bound - BOOST_ASSERT(area_to_squared_perimeter_ratio <= 1. / (4 * M_PI)); + BOOST_ASSERT(area_to_squared_perimeter_ratio <= 1. / (4 * std::numbers::pi)); if (area_to_squared_perimeter_ratio >= CIRCULAR_POLYGON_ISOPERIMETRIC_LOWER_BOUND) return true; } diff --git a/src/guidance/roundabout_handler.cpp b/src/guidance/roundabout_handler.cpp index 82998a4e1..451ebf7bf 100644 --- a/src/guidance/roundabout_handler.cpp +++ b/src/guidance/roundabout_handler.cpp @@ -337,7 +337,7 @@ RoundaboutType RoundaboutHandler::getRoundaboutType(const NodeID nid) const return RoundaboutType::RoundaboutIntersection; } - const double radius = roundabout_length / (2 * M_PI); + const double radius = roundabout_length * 0.5 * std::numbers::inv_pi; // Looks like a rotary: large roundabout with dedicated name // do we have a dedicated name for the rotary, if not its a roundabout diff --git a/src/util/coordinate_calculation.cpp b/src/util/coordinate_calculation.cpp index 00c5efbf5..2e43d2abd 100644 --- a/src/util/coordinate_calculation.cpp +++ b/src/util/coordinate_calculation.cpp @@ -146,7 +146,6 @@ double bearing(const Coordinate coordinate_1, const Coordinate coordinate_2) double computeAngle(const Coordinate first, const Coordinate second, const Coordinate third) { - using namespace boost::math::constants; using namespace coordinate_calculation; if (first == second || second == third) @@ -163,7 +162,7 @@ double computeAngle(const Coordinate first, const Coordinate second, const Coord const double v2y = web_mercator::latToY(toFloating(third.lat)) - web_mercator::latToY(toFloating(second.lat)); - double angle = (atan2_lookup(v2y, v2x) - atan2_lookup(v1y, v1x)) * 180. / pi(); + double angle = (atan2_lookup(v2y, v2x) - atan2_lookup(v1y, v1x)) * 180. * std::numbers::inv_pi; while (angle < 0.) {