Skip to content

Commit fb9d1ce

Browse files
Get rid of boost::math::constants::* and M_PI in favor of std::numbers (#6916)
1 parent a9b1bd8 commit fb9d1ce

13 files changed

+35
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- NodeJS:
2424
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
2525
- Misc:
26+
- 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)
2627
- CHANGED: Make constants in PackedVector constexpr. [#6917](https://github.com/Project-OSRM/osrm-backend/pull/6917)
2728
- CHANGED: Use std::variant instead of mapbox::util::variant. [#6903](https://github.com/Project-OSRM/osrm-backend/pull/6903)
2829
- CHANGED: Bump rapidjson to version f9d53419e912910fd8fa57d5705fa41425428c35 [#6906](https://github.com/Project-OSRM/osrm-backend/pull/6906)

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
266266
add_dependency_defines(-DBOOST_LIB_DIAGNOSTIC)
267267
add_dependency_defines(-D_CRT_SECURE_NO_WARNINGS)
268268
add_dependency_defines(-DNOMINMAX) # avoid min and max macros that can break compilation
269-
add_dependency_defines(-D_USE_MATH_DEFINES) #needed for M_PI with cmath.h
270269
add_dependency_defines(-D_WIN32_WINNT=0x0501)
271270
add_dependency_defines(-DXML_STATIC)
272271
find_library(ws2_32_LIBRARY_PATH ws2_32)

include/engine/map_matching/bayes_classifier.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <utility>
77
#include <vector>
88

9-
#include <boost/math/constants/constants.hpp>
9+
#include <numbers>
1010

1111
namespace osrm::engine::map_matching
1212
{
@@ -21,10 +21,8 @@ struct NormalDistribution
2121
// FIXME implement log-probability version since it's faster
2222
double Density(const double val) const
2323
{
24-
using namespace boost::math::constants;
25-
2624
const double x = val - mean;
27-
return 1.0 / (std::sqrt(two_pi<double>()) * standard_deviation) *
25+
return 1.0 / (std::sqrt(2 * std::numbers::pi) * standard_deviation) *
2826
std::exp(-x * x / (standard_deviation * standard_deviation));
2927
}
3028

include/engine/map_matching/hidden_markov_model.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "util/integer_range.hpp"
55

66
#include <boost/assert.hpp>
7-
#include <boost/math/constants/constants.hpp>
7+
#include <numbers>
88

99
#include <cmath>
1010

@@ -14,7 +14,7 @@
1414
namespace osrm::engine::map_matching
1515
{
1616

17-
static const double log_2_pi = std::log(2. * boost::math::constants::pi<double>());
17+
static const double log_2_pi = std::log(2. * std::numbers::pi);
1818
static const double IMPOSSIBLE_LOG_PROB = -std::numeric_limits<double>::infinity();
1919
static const double MINIMAL_LOG_PROB = std::numeric_limits<double>::lowest();
2020
static const std::size_t INVALID_STATE = std::numeric_limits<std::size_t>::max();

include/engine/map_matching/matching_confidence.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define ENGINE_MAP_MATCHING_CONFIDENCE_HPP
33

44
#include "engine/map_matching/bayes_classifier.hpp"
5-
5+
#include <boost/assert.hpp>
66
#include <cmath>
77

88
namespace osrm::engine::map_matching

include/util/cheap_ruler.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cmath>
55
#include <cstdint>
66
#include <limits>
7+
#include <numbers>
78
#include <tuple>
89
#include <utility>
910

@@ -37,7 +38,7 @@ class CheapRuler
3738
static constexpr double FE = 1.0 / 298.257223563; // flattening
3839

3940
static constexpr double E2 = FE * (2 - FE);
40-
static constexpr double RAD = M_PI / 180.0;
41+
static constexpr double RAD = std::numbers::pi / 180.0;
4142

4243
public:
4344
explicit CheapRuler(double latitude)

include/util/coordinate_calculation.hpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "util/coordinate.hpp"
55

6-
#include <boost/math/constants/constants.hpp>
6+
#include <numbers>
77

88
#include <algorithm>
99
#include <cmath>
@@ -23,17 +23,9 @@ const constexpr double RAD_TO_DEGREE = 1. / DEGREE_TO_RAD;
2323
// The IUGG value for the equatorial radius is 6378.137 km (3963.19 miles)
2424
const constexpr long double EARTH_RADIUS = 6372797.560856;
2525

26-
inline double degToRad(const double degree)
27-
{
28-
using namespace boost::math::constants;
29-
return degree * (pi<double>() / 180.0);
30-
}
26+
inline double degToRad(const double degree) { return degree * (std::numbers::pi / 180.0); }
3127

32-
inline double radToDeg(const double radian)
33-
{
34-
using namespace boost::math::constants;
35-
return radian * (180.0 * (1. / pi<double>()));
36-
}
28+
inline double radToDeg(const double radian) { return radian * (180.0 * std::numbers::inv_pi); }
3729
} // namespace detail
3830

3931
const constexpr static double METERS_PER_DEGREE_LAT = 110567.0;

include/util/trigonometry_table.hpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <limits>
88

9-
#include <boost/math/constants/constants.hpp>
9+
#include <numbers>
1010

1111
namespace osrm::util
1212
{
@@ -356,25 +356,21 @@ constexpr unsigned short atan_table[4096] = {
356356
0xffe0, 0xffea, 0xfff4, 0xffff};
357357

358358
// max value is pi/4
359-
#ifdef _MSC_VER // TODO: remove as soon as boost allows C++14 features with Visual Studio
360-
const constexpr double SCALING_FACTOR = 4. / M_PI * 0xFFFF;
361-
#else
362-
const constexpr double SCALING_FACTOR = 4. / boost::math::constants::pi<double>() * 0xFFFF;
363-
#endif
359+
const constexpr double SCALING_FACTOR = 4. * std::numbers::inv_pi * 0xFFFF;
364360

365361
inline double atan2_lookup(double y, double x)
366362
{
367-
using namespace boost::math::constants;
363+
static constexpr auto half_pi = std::numbers::pi * 0.5;
368364

369365
if (std::abs(x) < std::numeric_limits<double>::epsilon())
370366
{
371367
if (y >= 0.)
372368
{
373-
return half_pi<double>();
369+
return half_pi;
374370
}
375371
else
376372
{
377-
return -half_pi<double>();
373+
return -half_pi;
378374
}
379375
}
380376

@@ -405,25 +401,25 @@ inline double atan2_lookup(double y, double x)
405401
case 0:
406402
break;
407403
case 1:
408-
angle = pi<double>() - angle;
404+
angle = std::numbers::pi - angle;
409405
break;
410406
case 2:
411407
angle = -angle;
412408
break;
413409
case 3:
414-
angle = -pi<double>() + angle;
410+
angle = -std::numbers::pi + angle;
415411
break;
416412
case 4:
417-
angle = half_pi<double>() - angle;
413+
angle = half_pi - angle;
418414
break;
419415
case 5:
420-
angle = half_pi<double>() + angle;
416+
angle = half_pi + angle;
421417
break;
422418
case 6:
423-
angle = -half_pi<double>() + angle;
419+
angle = -half_pi + angle;
424420
break;
425421
case 7:
426-
angle = -half_pi<double>() - angle;
422+
angle = -half_pi - angle;
427423
break;
428424
}
429425
return angle;

include/util/web_mercator.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "util/coordinate.hpp"
55

6-
#include <boost/math/constants/constants.hpp>
6+
#include <numbers>
77

88
namespace osrm::util::web_mercator
99
{
@@ -14,7 +14,7 @@ const constexpr double RAD_TO_DEGREE = 1. / DEGREE_TO_RAD;
1414
// radius used by WGS84
1515
const constexpr double EARTH_RADIUS_WGS84 = 6378137.0;
1616
// earth circumference devided by 2
17-
const constexpr double MAXEXTENT = EARTH_RADIUS_WGS84 * boost::math::constants::pi<double>();
17+
const constexpr double MAXEXTENT = EARTH_RADIUS_WGS84 * std::numbers::pi;
1818
// ^ math functions are not constexpr since they have side-effects (setting errno) :(
1919
const constexpr double EPSG3857_MAX_LATITUDE = 85.051128779806592378; // 90(4*atan(exp(pi))/pi-1)
2020
const constexpr double MAX_LONGITUDE = 180.0;
@@ -103,8 +103,8 @@ inline void pixelToDegree(const double shift, double &x, double &y)
103103
const double b = shift / 2.0;
104104
x = (x - b) / shift * 360.0;
105105
// FIXME needs to be simplified
106-
const double g = (y - b) / -(shift / (2 * M_PI)) / detail::DEGREE_TO_RAD;
107-
static_assert(detail::DEGREE_TO_RAD / (2 * M_PI) - 1 / 360. < 0.0001, "");
106+
const double g = (y - b) / -(shift * 0.5 * std::numbers::inv_pi) / detail::DEGREE_TO_RAD;
107+
static_assert(detail::DEGREE_TO_RAD * 0.5 * std::numbers::inv_pi - 1 / 360. < 0.0001, "");
108108
y = static_cast<double>(yToLat(g));
109109
}
110110

src/extractor/intersection/intersection_analysis.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "util/coordinate_calculation.hpp"
77

88
#include <boost/optional/optional_io.hpp>
9+
#include <numbers>
910

1011
namespace osrm::extractor::intersection
1112
{
@@ -79,11 +80,11 @@ namespace
7980
{
8081
double findAngleBisector(double alpha, double beta)
8182
{
82-
alpha *= M_PI / 180.;
83-
beta *= M_PI / 180.;
83+
alpha *= std::numbers::pi / 180.;
84+
beta *= std::numbers::pi / 180.;
8485
const auto average =
85-
180. * std::atan2(std::sin(alpha) + std::sin(beta), std::cos(alpha) + std::cos(beta)) /
86-
M_PI;
86+
180. * std::atan2(std::sin(alpha) + std::sin(beta), std::cos(alpha) + std::cos(beta)) *
87+
std::numbers::inv_pi;
8788
return std::fmod(average + 360., 360.);
8889
}
8990

0 commit comments

Comments
 (0)