diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f17d881a..8e49c19e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -516,12 +516,6 @@ endif (MINGW) # After the tweaks for the compile settings, disable some warnings on MSVC. if (MSVC) - # On MSVC, math constants are not included in or unless - # _USE_MATH_DEFINES is defined [1]. As we use M_PI in the examples, ensure - # that _USE_MATH_DEFINES is defined before the first inclusion of . - # - # [1] https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx - add_compile_definitions(_USE_MATH_DEFINES) # Insecure standard library functions add_compile_definitions(_CRT_SECURE_NO_WARNINGS) # std::numeric_limits::has_denorm is deprecated in C++23 diff --git a/examples/ellipse_approximation.cc b/examples/ellipse_approximation.cc index 5f31b126d..eefc72d54 100644 --- a/examples/ellipse_approximation.cc +++ b/examples/ellipse_approximation.cc @@ -396,7 +396,7 @@ int main(int argc, char** argv) { // // Initialize `X` to points on the unit circle. VectorXd w(num_segments + 1); - w.setLinSpaced(num_segments + 1, 0.0, 2.0 * M_PI); + w.setLinSpaced(num_segments + 1, 0.0, 2.0 * ceres::constants::pi); w.conservativeResize(num_segments); MatrixXd X(num_segments, 2); X.col(0) = w.array().cos(); diff --git a/examples/more_garbow_hillstrom.cc b/examples/more_garbow_hillstrom.cc index 35936f413..b2fe61ba2 100644 --- a/examples/more_garbow_hillstrom.cc +++ b/examples/more_garbow_hillstrom.cc @@ -223,7 +223,7 @@ BEGIN_MGH_PROBLEM(TestProblem7, 3, 3) const T x1 = x[0]; const T x2 = x[1]; const T x3 = x[2]; - const T theta = (0.5 / M_PI) * atan(x2 / x1) + (x1 > 0.0 ? 0.0 : 0.5); + const T theta = (0.5 / constants::pi) * atan(x2 / x1) + (x1 > 0.0 ? 0.0 : 0.5); residual[0] = 10.0 * (x3 - 10.0 * theta); residual[1] = 10.0 * (sqrt(x1 * x1 + x2 * x2) - 1.0); residual[2] = x3; diff --git a/examples/slam/pose_graph_2d/normalize_angle.h b/examples/slam/pose_graph_2d/normalize_angle.h index d54547abe..060287857 100644 --- a/examples/slam/pose_graph_2d/normalize_angle.h +++ b/examples/slam/pose_graph_2d/normalize_angle.h @@ -41,9 +41,9 @@ namespace ceres::examples { template inline T NormalizeAngle(const T& angle_radians) { // Use ceres::floor because it is specialized for double and Jet types. - T two_pi(2.0 * M_PI); + T two_pi(2.0 * constants::pi); return angle_radians - - two_pi * ceres::floor((angle_radians + T(M_PI)) / two_pi); + two_pi * ceres::floor((angle_radians + T(constants::pi)) / two_pi); } } // namespace ceres::examples diff --git a/include/ceres/ceres.h b/include/ceres/ceres.h index 5beec9ca8..51f9d8963 100644 --- a/include/ceres/ceres.h +++ b/include/ceres/ceres.h @@ -39,8 +39,8 @@ #include "ceres/autodiff_first_order_function.h" #include "ceres/autodiff_manifold.h" #include "ceres/conditioned_cost_function.h" -#include "ceres/context.h" #include "ceres/constants.h" +#include "ceres/context.h" #include "ceres/cost_function.h" #include "ceres/cost_function_to_functor.h" #include "ceres/covariance.h" diff --git a/include/ceres/constants.h b/include/ceres/constants.h index 8558160a1..584b6697b 100644 --- a/include/ceres/constants.h +++ b/include/ceres/constants.h @@ -36,6 +36,7 @@ namespace ceres::constants { template inline constexpr T pi_v(3.141592653589793238462643383279502884); +inline constexpr double pi = pi_v; } // namespace ceres::constants #endif // CERES_PUBLIC_CONSTANTS_H_ diff --git a/include/ceres/internal/sphere_manifold_functions.h b/include/ceres/internal/sphere_manifold_functions.h index 184edf903..479344261 100644 --- a/include/ceres/internal/sphere_manifold_functions.h +++ b/include/ceres/internal/sphere_manifold_functions.h @@ -32,6 +32,7 @@ #ifndef CERES_PUBLIC_INTERNAL_SPHERE_MANIFOLD_HELPERS_H_ #define CERES_PUBLIC_INTERNAL_SPHERE_MANIFOLD_HELPERS_H_ +#include "ceres/constants.h" #include "ceres/internal/householder_vector.h" // This module contains functions to compute the SphereManifold plus and minus @@ -122,7 +123,7 @@ inline void ComputeSphereManifoldMinus( const double hy_norm = hy.template head(tangent_size).norm(); if (hy_norm == 0.0) { y_minus_x->setZero(); - y_minus_x->data()[tangent_size - 1] = y_last >= 0 ? 0.0 : M_PI; + y_minus_x->data()[tangent_size - 1] = y_last >= 0 ? 0.0 : constants::pi; } else { *y_minus_x = std::atan2(hy_norm, y_last) / hy_norm * hy.template head(tangent_size); diff --git a/include/ceres/rotation.h b/include/ceres/rotation.h index 7d2d79ed7..0cccfa7f3 100644 --- a/include/ceres/rotation.h +++ b/include/ceres/rotation.h @@ -610,7 +610,7 @@ void RotationMatrixToEulerAngles( // [-pi, pi) x [0, pi / 2) x [-pi, pi) // which is enforced here if constexpr (EulerSystem::kIsProperEuler) { - const T kPi(constants::pi_v); + const T kPi(constants::pi); const T kTwoPi(2.0 * kPi); if (euler[1] < T(0.0) || ea[1] > kPi) { euler[0] += kPi; diff --git a/internal/ceres/autodiff_benchmarks/brdf_cost_function.h b/internal/ceres/autodiff_benchmarks/brdf_cost_function.h index 87842c050..41cfcfa21 100644 --- a/internal/ceres/autodiff_benchmarks/brdf_cost_function.h +++ b/internal/ceres/autodiff_benchmarks/brdf_cost_function.h @@ -35,6 +35,8 @@ #include #include +#include "ceres/constants.h" + namespace ceres { // The brdf is based on: @@ -139,7 +141,7 @@ struct Brdf { const T gr = cggxn_dot_l * cggxn_dot_v; const Vec3 result_no_cosine = - (T(1.0 / M_PI) * Lerp(fd, ss, subsurface) * c + f_sheen) * + (T(1.0 / constants::pi) * Lerp(fd, ss, subsurface) * c + f_sheen) * (T(1) - metallic) + gs * fs * ds + Vec3(T(0.25), T(0.25), T(0.25)) * clearcoat * gr * fr * dr; @@ -177,11 +179,11 @@ struct Brdf { T result = T(0); if (a >= T(1)) { - result = T(1 / M_PI); + result = T(1 / constants::pi); } else { const T a2 = a * a; const T t = T(1) + (a2 - T(1)) * n_dot_h * n_dot_h; - result = (a2 - T(1)) / (T(M_PI) * T(log(a2) * t)); + result = (a2 - T(1)) / (T(constants::pi) * T(log(a2) * t)); } return result; } @@ -192,7 +194,7 @@ struct Brdf { const T& h_dot_y, const T& ax, const T& ay) const { - return T(1) / (T(M_PI) * ax * ay * + return T(1) / (T(constants::pi) * ax * ay * Square(Square(h_dot_x / ax) + Square(h_dot_y / ay) + n_dot_h * n_dot_h)); } diff --git a/internal/ceres/autodiff_manifold_test.cc b/internal/ceres/autodiff_manifold_test.cc index 6e5c4fce4..c687719d5 100644 --- a/internal/ceres/autodiff_manifold_test.cc +++ b/internal/ceres/autodiff_manifold_test.cc @@ -32,6 +32,7 @@ #include +#include "ceres/constants.h" #include "ceres/manifold.h" #include "ceres/manifold_test_utils.h" #include "ceres/rotation.h" @@ -197,7 +198,7 @@ TEST(AutoDiffManifoldTest, QuaternionPlusPiBy2) { for (int i = 0; i < 3; ++i) { Vector delta = Vector::Zero(3); - delta[i] = M_PI / 2; + delta[i] = constants::pi / 2; Vector x_plus_delta = Vector::Zero(4); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data())); @@ -285,7 +286,7 @@ TEST(AutoDiffManifold, QuaternionDeltaJustBelowPi) { const Vector y = RandomQuaternion(); Vector delta = Vector::Random(3); delta.normalize(); - delta *= (M_PI - 1e-6); + delta *= (constants::pi - 1e-6); EXPECT_THAT(manifold, QuaternionPlusIsCorrectAt(x, delta)); EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance); } diff --git a/internal/ceres/dynamic_sparsity_test.cc b/internal/ceres/dynamic_sparsity_test.cc index c1b971da5..0c29595ec 100644 --- a/internal/ceres/dynamic_sparsity_test.cc +++ b/internal/ceres/dynamic_sparsity_test.cc @@ -383,7 +383,7 @@ TEST(DynamicSparsity, StaticAndDynamicSparsityProduceSameSolution) { // // Initialize `X` to points on the unit circle. Vector w(num_segments + 1); - w.setLinSpaced(num_segments + 1, 0.0, 2.0 * M_PI); + w.setLinSpaced(num_segments + 1, 0.0, 2.0 * constants::pi); w.conservativeResize(num_segments); Matrix X(num_segments, 2); X.col(0) = w.array().cos(); diff --git a/internal/ceres/manifold_test.cc b/internal/ceres/manifold_test.cc index dc0fe53e6..33f9386d5 100644 --- a/internal/ceres/manifold_test.cc +++ b/internal/ceres/manifold_test.cc @@ -36,6 +36,7 @@ #include #include "Eigen/Geometry" +#include "ceres/constants.h" #include "ceres/dynamic_numeric_diff_cost_function.h" #include "ceres/internal/eigen.h" #include "ceres/internal/port.h" @@ -504,7 +505,7 @@ TEST(QuaternionManifold, PlusPiBy2) { for (int i = 0; i < 3; ++i) { Vector delta = Vector::Zero(3); - delta[i] = M_PI / 2; + delta[i] = constants::pi / 2; Vector x_plus_delta = Vector::Zero(4); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), x_plus_delta.data())); @@ -598,7 +599,7 @@ TEST(QuaternionManifold, DeltaJustBelowPi) { const Vector y = RandomQuaternion(); Vector delta = Vector::Random(3); delta.normalize(); - delta *= (M_PI - 1e-6); + delta *= (constants::pi - 1e-6); EXPECT_THAT(manifold, QuaternionManifoldPlusIsCorrectAt(x, delta)); EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance); } @@ -669,7 +670,7 @@ TEST(EigenQuaternionManifold, DeltaJustBelowPi) { const Vector y = RandomQuaternion(); Vector delta = Vector::Random(3); delta.normalize(); - delta *= (M_PI - 1e-6); + delta *= (constants::pi - 1e-6); EXPECT_THAT(manifold, EigenQuaternionManifoldPlusIsCorrectAt(x, delta)); EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance); } @@ -732,7 +733,7 @@ TEST(SphereManifold, Plus2DTest) { } { - double delta[1]{M_PI}; + double delta[1]{constants::pi}; Eigen::Vector2d y = Eigen::Vector2d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data())); const Eigen::Vector2d gtY = -Eigen::Vector2d::UnitY(); @@ -740,7 +741,7 @@ TEST(SphereManifold, Plus2DTest) { } { - double delta[1]{2.0 * M_PI}; + double delta[1]{2.0 * constants::pi}; Eigen::Vector2d y = Eigen::Vector2d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta, y.data())); const Eigen::Vector2d gtY = Eigen::Vector2d::UnitY(); @@ -761,7 +762,7 @@ TEST(SphereManifold, Plus3DTest) { } { - Eigen::Vector2d delta{M_PI, 0.0}; + Eigen::Vector2d delta{constants::pi, 0.0}; Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ(); @@ -769,7 +770,7 @@ TEST(SphereManifold, Plus3DTest) { } { - Eigen::Vector2d delta{2.0 * M_PI, 0.0}; + Eigen::Vector2d delta{2.0 * constants::pi, 0.0}; Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = Eigen::Vector3d::UnitZ(); @@ -785,7 +786,7 @@ TEST(SphereManifold, Plus3DTest) { } { - Eigen::Vector2d delta{0.0, M_PI}; + Eigen::Vector2d delta{0.0, constants::pi}; Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ(); @@ -793,7 +794,7 @@ TEST(SphereManifold, Plus3DTest) { } { - Eigen::Vector2d delta{0.0, 2.0 * M_PI}; + Eigen::Vector2d delta{0.0, 2.0 * constants::pi}; Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = Eigen::Vector3d::UnitZ(); @@ -809,7 +810,7 @@ TEST(SphereManifold, Plus3DTest) { } { - Eigen::Vector2d delta = Eigen::Vector2d(1, 1).normalized() * M_PI; + Eigen::Vector2d delta = Eigen::Vector2d(1, 1).normalized() * constants::pi; Eigen::Vector3d y = Eigen::Vector3d::Zero(); EXPECT_TRUE(manifold.Plus(x.data(), delta.data(), y.data())); const Eigen::Vector3d gtY = -Eigen::Vector3d::UnitZ(); @@ -832,7 +833,7 @@ TEST(SphereManifold, Minus2DTest) { { double delta[1]; const Eigen::Vector2d y(-1, 0); - const double gtDelta{M_PI}; + const double gtDelta{constants::pi}; EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta)); EXPECT_LT(std::abs(delta[0] - gtDelta), kTolerance); } @@ -853,7 +854,7 @@ TEST(SphereManifold, Minus3DTest) { { Eigen::Vector2d delta; const Eigen::Vector3d y(-1, 0, 0); - const Eigen::Vector2d gtDelta(0.0, M_PI); + const Eigen::Vector2d gtDelta(0.0, constants::pi); EXPECT_TRUE(manifold.Minus(y.data(), x.data(), delta.data())); EXPECT_LT((delta - gtDelta).norm(), kTolerance); } diff --git a/internal/ceres/rotation_test.cc b/internal/ceres/rotation_test.cc index 87f9de78d..f1ea0714c 100644 --- a/internal/ceres/rotation_test.cc +++ b/internal/ceres/rotation_test.cc @@ -53,7 +53,7 @@ namespace ceres { namespace internal { -inline constexpr double kPi = constants::pi_v; +inline constexpr double kPi = constants::pi; const double kHalfSqrt2 = 0.707106781186547524401; // A tolerance value for floating-point comparisons.