From f9b7b6651b108136a16df44d91fb31735645f5a7 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sun, 28 Sep 2025 08:35:06 +0200 Subject: [PATCH] Support Eigen3 5.0.0 Change-Id: I5cf476a6d8c090861f4ea4a254a35d39e36a6d68 --- CMakeLists.txt | 27 ++++++++++++++++++++++++++- internal/ceres/covariance_impl.cc | 5 +++++ internal/ceres/invert_psd_matrix.h | 4 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 754f069bf..19ee2a864 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -232,9 +232,34 @@ endif (IOS) unset(CERES_COMPILE_OPTIONS) +# We call find_package multiple time with different versioning scheme to support +# both Eigen3 3.x and 5.x. If the first invocation fails and Eigen3_DIR was set +# to a possible location, e.g., during cross-compilation, failure to locate the +# package will unset the variable. To avoid losing the path hint, we save it and +# reuse it in the second find_package call. +set (_Ceres_Eigen3_DIR) + +if (Eigen3_DIR) + set (_Ceres_Eigen3_DIR "${Eigen3_DIR}") +endif (Eigen3_DIR) + # Eigen. # Eigen delivers Eigen3Config.cmake since v3.3.3 -find_package(Eigen3 3.3.4 REQUIRED NO_MODULE) +find_package(Eigen3 3.3.4...5 NO_MODULE) + +if (NOT Eigen3_FOUND) + if (_Ceres_Eigen3_DIR) + set (Eigen3_DIR "${_Ceres_Eigen3_DIR}") + endif (_Ceres_Eigen3_DIR) + # Eigen3's CMake package config prior to 5.0.0 does not support version ranges + # with different major version components. To ensure backward compatibility, + # we locate the package using the previous version scheme as a fallback + # mechanism. + find_package(Eigen3 3.3.4 REQUIRED NO_MODULE) +endif (NOT Eigen3_FOUND) + +unset (_Ceres_Eigen3_DIR) + if (Eigen3_FOUND) message("-- Found Eigen version ${Eigen3_VERSION}: ${Eigen3_DIR}") if (EIGENSPARSE) diff --git a/internal/ceres/covariance_impl.cc b/internal/ceres/covariance_impl.cc index 56221945a..930711fd0 100644 --- a/internal/ceres/covariance_impl.cc +++ b/internal/ceres/covariance_impl.cc @@ -724,8 +724,13 @@ bool CovarianceImpl::ComputeCovarianceValuesUsingDenseSVD() { } event_logger.AddEvent("ConvertToDenseMatrix"); +#if EIGEN_VERSION_AT_LEAST(5, 0, 0) + Eigen::BDCSVD svd( + dense_jacobian); +#else // !EIGEN_VERSION_AT_LEAST(5, 0, 0) Eigen::BDCSVD svd(dense_jacobian, Eigen::ComputeThinU | Eigen::ComputeThinV); +#endif // EIGEN_VERSION_AT_LEAST(5, 0, 0) event_logger.AddEvent("SingularValueDecomposition"); diff --git a/internal/ceres/invert_psd_matrix.h b/internal/ceres/invert_psd_matrix.h index 21ba2dc8a..dd3a0a34b 100644 --- a/internal/ceres/invert_psd_matrix.h +++ b/internal/ceres/invert_psd_matrix.h @@ -67,7 +67,11 @@ typename EigenTypes::Matrix InvertPSDMatrix( // For a thin SVD the number of columns of the matrix need to be dynamic. using SVDMType = typename EigenTypes::Matrix; +#if EIGEN_VERSION_AT_LEAST(5, 0, 0) + Eigen::JacobiSVD svd(m); +#else // !EIGEN_VERSION_AT_LEAST(5, 0, 0) Eigen::JacobiSVD svd(m, Eigen::ComputeThinU | Eigen::ComputeThinV); +#endif // EIGEN_VERSION_AT_LEAST(5, 0, 0) return svd.solve(MType::Identity(size, size)); }