From c8658c8992acd1a862c6a193b3f21e1ee0274344 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sun, 20 Feb 2022 02:22:17 +0100 Subject: [PATCH] Modernize more Apply clang-tidy Google and modernize fixes without trailing return type using: $ clang-tidy -p \ -checks='-*,google-*,modernize-*,-modernize-use-trailing-return-type' {} -fix Change-Id: I7450cc58ea9abf928f73a467e87876083217fa26 --- examples/bal_problem.cc | 15 +-- examples/bicubic_interpolation.cc | 6 +- examples/bicubic_interpolation_analytic.cc | 8 +- examples/bundle_adjuster.cc | 2 +- examples/denoising.cc | 6 +- examples/ellipse_approximation.cc | 15 +-- examples/fields_of_experts.h | 8 +- examples/helloworld_analytic_diff.cc | 7 +- examples/libmv_bundle_adjuster.cc | 43 ++++--- examples/libmv_homography.cc | 46 ++++---- examples/nist.cc | 11 +- examples/random.h | 6 +- examples/robot_pose_mle.cc | 13 +-- examples/rosenbrock_analytic_diff.cc | 1 - examples/slam/pose_graph_2d/pose_graph_2d.cc | 20 +--- examples/slam/pose_graph_3d/pose_graph_3d.cc | 26 +---- .../pose_graph_3d/pose_graph_3d_error_term.h | 9 +- examples/slam/pose_graph_3d/types.h | 14 +-- include/ceres/autodiff_cost_function.h | 2 +- include/ceres/autodiff_first_order_function.h | 2 +- include/ceres/autodiff_manifold.h | 4 +- include/ceres/crs_matrix.h | 6 +- include/ceres/cubic_interpolation.h | 2 +- .../ceres/dynamic_autodiff_cost_function.h | 6 +- .../dynamic_numeric_diff_cost_function.h | 3 +- include/ceres/internal/array_selector.h | 8 +- include/ceres/internal/autodiff.h | 2 +- include/ceres/internal/eigen.h | 46 ++++---- include/ceres/internal/numeric_diff.h | 26 ++--- include/ceres/jet.h | 14 +-- include/ceres/local_parameterization.h | 13 +-- include/ceres/manifold.h | 5 +- include/ceres/numeric_diff_cost_function.h | 4 +- .../ceres/numeric_diff_first_order_function.h | 2 +- include/ceres/ordered_groups.h | 2 +- include/ceres/problem.h | 2 +- include/ceres/tiny_solver.h | 6 +- include/ceres/tiny_solver_autodiff_function.h | 4 +- .../ceres/tiny_solver_cost_function_adapter.h | 4 +- .../autodiff_benchmarks/relative_pose_error.h | 6 +- internal/ceres/autodiff_cost_function_test.cc | 8 +- .../autodiff_local_parameterization_test.cc | 12 +- internal/ceres/autodiff_test.cc | 10 +- internal/ceres/block_jacobi_preconditioner.cc | 9 +- .../ceres/block_jacobi_preconditioner_test.cc | 3 +- internal/ceres/block_jacobian_writer.cc | 7 +- .../block_random_access_diagonal_matrix.cc | 14 +-- internal/ceres/block_random_access_matrix.h | 4 +- .../block_random_access_sparse_matrix.cc | 8 +- .../ceres/block_random_access_sparse_matrix.h | 2 +- internal/ceres/block_sparse_matrix.cc | 66 +++++------ internal/ceres/block_sparse_matrix.h | 2 +- internal/ceres/block_sparse_matrix_test.cc | 6 +- internal/ceres/block_structure.h | 20 ++-- internal/ceres/bundle_adjustment_test_util.h | 2 +- internal/ceres/c_api.cc | 4 +- internal/ceres/c_api_test.cc | 2 +- internal/ceres/canonical_views_clustering.cc | 8 +- internal/ceres/casts.h | 2 +- internal/ceres/cgnr_solver.cc | 5 +- internal/ceres/cgnr_solver.h | 2 +- .../ceres/compressed_row_jacobian_writer.cc | 21 ++-- .../ceres/compressed_row_sparse_matrix.cc | 11 +- .../compressed_row_sparse_matrix_test.cc | 2 +- internal/ceres/concurrent_queue.h | 4 +- .../ceres/conditioned_cost_function_test.cc | 4 +- internal/ceres/conjugate_gradients_solver.cc | 5 +- internal/ceres/conjugate_gradients_solver.h | 2 +- .../ceres/coordinate_descent_minimizer.cc | 18 ++- internal/ceres/corrector_test.cc | 8 +- .../ceres/cost_function_to_functor_test.cc | 7 +- internal/ceres/covariance_impl.cc | 20 ++-- internal/ceres/covariance_test.cc | 110 +++++++++--------- internal/ceres/cxsparse.cc | 4 +- internal/ceres/cxsparse.h | 2 +- internal/ceres/dense_cholesky_test.cc | 2 +- internal/ceres/dense_linear_solver_test.cc | 5 +- .../ceres/dense_normal_cholesky_solver.cc | 7 +- internal/ceres/dense_normal_cholesky_solver.h | 2 +- internal/ceres/dense_qr_test.cc | 2 +- internal/ceres/dense_sparse_matrix.cc | 3 +- internal/ceres/dense_sparse_matrix.h | 4 +- internal/ceres/detect_structure_test.cc | 86 +++++++------- internal/ceres/dogleg_strategy.h | 4 +- .../dynamic_autodiff_cost_function_test.cc | 26 ++--- .../ceres/dynamic_compressed_row_finalizer.h | 2 +- .../dynamic_compressed_row_jacobian_writer.cc | 12 +- .../dynamic_compressed_row_sparse_matrix.cc | 6 +- ...namic_compressed_row_sparse_matrix_test.cc | 2 +- ...dynamic_numeric_diff_cost_function_test.cc | 22 ++-- .../dynamic_sparse_normal_cholesky_solver.cc | 5 +- .../dynamic_sparse_normal_cholesky_solver.h | 3 +- internal/ceres/dynamic_sparsity_test.cc | 5 +- internal/ceres/eigensparse.cc | 34 +++--- internal/ceres/evaluation_callback_test.cc | 2 +- internal/ceres/evaluator.h | 2 +- internal/ceres/evaluator_test.cc | 2 +- internal/ceres/execution_summary.h | 13 ++- internal/ceres/fixed_array_test.cc | 20 ++-- internal/ceres/float_cxsparse.cc | 2 +- internal/ceres/float_suitesparse.cc | 2 +- internal/ceres/gradient_checker.cc | 2 +- internal/ceres/gradient_checker_test.cc | 5 +- .../ceres/gradient_checking_cost_function.cc | 10 +- .../ceres/gradient_problem_solver_test.cc | 6 +- internal/ceres/implicit_schur_complement.cc | 8 +- internal/ceres/inner_product_computer.cc | 4 +- internal/ceres/inner_product_computer_test.cc | 7 +- internal/ceres/iterative_refiner.h | 2 +- internal/ceres/iterative_refiner_test.cc | 6 +- .../iterative_schur_complement_solver.cc | 5 +- .../ceres/iterative_schur_complement_solver.h | 2 +- internal/ceres/jet_operator_benchmark.cc | 14 +-- internal/ceres/jet_test.cc | 6 +- .../levenberg_marquardt_strategy_test.cc | 8 +- internal/ceres/line_search.cc | 4 +- internal/ceres/line_search_direction.h | 23 ++-- .../ceres/linear_least_squares_problems.cc | 86 +++++++------- .../ceres/linear_least_squares_problems.h | 4 +- internal/ceres/linear_solver.h | 2 +- internal/ceres/local_parameterization.cc | 6 +- internal/ceres/local_parameterization_test.cc | 10 +- internal/ceres/low_rank_inverse_hessian.cc | 4 +- internal/ceres/manifold_adapter.h | 2 +- internal/ceres/map_util.h | 2 +- internal/ceres/miniglog/glog/logging.h | 2 +- internal/ceres/normal_prior.cc | 2 +- internal/ceres/normal_prior_test.cc | 6 +- internal/ceres/numeric_diff_test_utils.cc | 22 ++-- internal/ceres/parameter_block.h | 2 +- internal/ceres/parameter_block_ordering.cc | 20 ++-- .../ceres/parameter_block_ordering_test.cc | 2 +- internal/ceres/parameter_block_test.cc | 4 +- internal/ceres/partitioned_matrix_view_impl.h | 35 +++--- internal/ceres/polynomial.cc | 3 +- internal/ceres/preconditioner.h | 6 +- internal/ceres/preprocessor.h | 4 +- internal/ceres/problem_impl.cc | 33 +++--- internal/ceres/problem_impl.h | 8 +- internal/ceres/problem_test.cc | 17 ++- internal/ceres/program.cc | 80 ++++++------- internal/ceres/random.h | 2 +- internal/ceres/reorder_program.cc | 24 ++-- internal/ceres/rotation_test.cc | 68 +++++------ internal/ceres/schur_complement_solver.cc | 10 +- internal/ceres/schur_eliminator.h | 4 +- internal/ceres/schur_eliminator_benchmark.cc | 2 +- internal/ceres/schur_eliminator_impl.h | 4 +- internal/ceres/schur_eliminator_test.cc | 2 +- internal/ceres/schur_jacobi_preconditioner.cc | 5 +- internal/ceres/schur_jacobi_preconditioner.h | 2 +- internal/ceres/scoped_thread_token.h | 7 +- internal/ceres/small_blas.h | 16 +-- internal/ceres/solver_test.cc | 6 +- internal/ceres/sparse_cholesky_test.cc | 18 +-- internal/ceres/subset_preconditioner.cc | 9 +- internal/ceres/subset_preconditioner.h | 2 +- internal/ceres/subset_preconditioner_test.cc | 2 +- internal/ceres/suitesparse.h | 2 +- internal/ceres/system_test.cc | 2 +- internal/ceres/thread_pool.cc | 2 +- internal/ceres/thread_token_provider.h | 2 +- .../tiny_solver_autodiff_function_test.cc | 8 +- .../tiny_solver_cost_function_adapter_test.cc | 4 +- internal/ceres/tiny_solver_test.cc | 14 +-- internal/ceres/triplet_sparse_matrix.cc | 3 +- internal/ceres/triplet_sparse_matrix.h | 2 +- internal/ceres/trust_region_preprocessor.cc | 4 +- internal/ceres/visibility.cc | 18 +-- .../ceres/visibility_based_preconditioner.cc | 19 ++- .../ceres/visibility_based_preconditioner.h | 2 +- internal/ceres/visibility_test.cc | 48 ++++---- 172 files changed, 918 insertions(+), 983 deletions(-) diff --git a/examples/bal_problem.cc b/examples/bal_problem.cc index 8500b06bf..f3032bd63 100644 --- a/examples/bal_problem.cc +++ b/examples/bal_problem.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -30,6 +30,7 @@ #include "bal_problem.h" +#include #include #include #include @@ -44,8 +45,8 @@ namespace ceres { namespace examples { namespace { -typedef Eigen::Map VectorRef; -typedef Eigen::Map ConstVectorRef; +using VectorRef = Eigen::Map; +using ConstVectorRef = Eigen::Map; template void FscanfOrDie(FILE* fptr, const char* format, T* value) { @@ -63,7 +64,7 @@ void PerturbPoint3(const double sigma, double* point) { double Median(std::vector* data) { int n = data->size(); - std::vector::iterator mid_point = data->begin() + n / 2; + auto mid_point = data->begin() + n / 2; std::nth_element(data->begin(), mid_point, data->end()); return *mid_point; } @@ -111,7 +112,7 @@ BALProblem::BALProblem(const std::string& filename, bool use_quaternions) { if (use_quaternions) { // Switch the angle-axis rotations to quaternions. num_parameters_ = 10 * num_cameras_ + 3 * num_points_; - double* quaternion_parameters = new double[num_parameters_]; + auto* quaternion_parameters = new double[num_parameters_]; double* original_cursor = parameters_; double* quaternion_cursor = quaternion_parameters; for (int i = 0; i < num_cameras_; ++i) { @@ -161,8 +162,8 @@ void BALProblem::WriteToFile(const std::string& filename) const { } else { memcpy(angleaxis, parameters_ + 9 * i, 9 * sizeof(double)); } - for (int j = 0; j < 9; ++j) { - fprintf(fptr, "%.16g\n", angleaxis[j]); + for (double coeff : angleaxis) { + fprintf(fptr, "%.16g\n", coeff); } } diff --git a/examples/bicubic_interpolation.cc b/examples/bicubic_interpolation.cc index f097436ec..818839ecc 100644 --- a/examples/bicubic_interpolation.cc +++ b/examples/bicubic_interpolation.cc @@ -37,6 +37,8 @@ // Bicubic interpolation of f(x, y) will be exact, thus we can expect close to // perfect convergence +#include + #include "ceres/ceres.h" #include "ceres/cubic_interpolation.h" #include "glog/logging.h" @@ -63,9 +65,9 @@ struct AutoDiffBiCubicCost { } AutoDiffBiCubicCost(const Interpolator& interpolator, - const Eigen::Vector2d& point, + Eigen::Vector2d point, double value) - : point_(point), value_(value), interpolator_(interpolator) {} + : point_(std::move(point)), value_(value), interpolator_(interpolator) {} static ceres::CostFunction* Create(const Interpolator& interpolator, const Eigen::Vector2d& point, diff --git a/examples/bicubic_interpolation_analytic.cc b/examples/bicubic_interpolation_analytic.cc index 84c99bb55..772192e9a 100644 --- a/examples/bicubic_interpolation_analytic.cc +++ b/examples/bicubic_interpolation_analytic.cc @@ -37,6 +37,8 @@ // Bicubic interpolation of f(x, y) will be exact, thus we can expect close to // perfect convergence +#include + #include "ceres/ceres.h" #include "ceres/cubic_interpolation.h" #include "glog/logging.h" @@ -50,7 +52,7 @@ struct AnalyticBiCubicCost : public ceres::CostFunction { bool Evaluate(double const* const* parameters, double* residuals, - double** jacobians) const { + double** jacobians) const override { Eigen::Map shift(parameters[0]); const Eigen::Vector2d point = point_ + shift; @@ -72,9 +74,9 @@ struct AnalyticBiCubicCost : public ceres::CostFunction { } AnalyticBiCubicCost(const Interpolator& interpolator, - const Eigen::Vector2d& point, + Eigen::Vector2d point, double value) - : point_(point), value_(value), interpolator_(interpolator) { + : point_(std::move(point)), value_(value), interpolator_(interpolator) { set_num_residuals(1); *mutable_parameter_block_sizes() = {2}; } diff --git a/examples/bundle_adjuster.cc b/examples/bundle_adjuster.cc index 1940cf26e..9727137ab 100644 --- a/examples/bundle_adjuster.cc +++ b/examples/bundle_adjuster.cc @@ -228,7 +228,7 @@ void SetOrdering(BALProblem* bal_problem, Solver::Options* options) { return; } - ceres::ParameterBlockOrdering* ordering = new ceres::ParameterBlockOrdering; + auto* ordering = new ceres::ParameterBlockOrdering; // The points come before the cameras. for (int i = 0; i < num_points; ++i) { diff --git a/examples/denoising.cc b/examples/denoising.cc index b885aedfb..ce637f042 100644 --- a/examples/denoising.cc +++ b/examples/denoising.cc @@ -113,9 +113,9 @@ namespace { class QuadraticCostFunction : public ceres::SizedCostFunction<1, 1> { public: QuadraticCostFunction(double a, double b) : sqrta_(std::sqrt(a)), b_(b) {} - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const { + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override { const double x = parameters[0][0]; residuals[0] = sqrta_ * (x - b_); if (jacobians != nullptr && jacobians[0] != nullptr) { diff --git a/examples/ellipse_approximation.cc b/examples/ellipse_approximation.cc index b3a328447..c549ffb15 100644 --- a/examples/ellipse_approximation.cc +++ b/examples/ellipse_approximation.cc @@ -36,6 +36,7 @@ // dense but dynamically sparse. #include +#include #include #include "ceres/ceres.h" @@ -275,8 +276,8 @@ ceres::ConstMatrixRef kY(kYData, kYRows, kYCols); class PointToLineSegmentContourCostFunction : public ceres::CostFunction { public: PointToLineSegmentContourCostFunction(const int num_segments, - const Eigen::Vector2d& y) - : num_segments_(num_segments), y_(y) { + Eigen::Vector2d y) + : num_segments_(num_segments), y_(std::move(y)) { // The first parameter is the preimage position. mutable_parameter_block_sizes()->push_back(1); // The next parameters are the control points for the line segment contour. @@ -286,9 +287,9 @@ class PointToLineSegmentContourCostFunction : public ceres::CostFunction { set_num_residuals(2); } - virtual bool Evaluate(const double* const* x, - double* residuals, - double** jacobians) const { + bool Evaluate(const double* const* x, + double* residuals, + double** jacobians) const override { // Convert the preimage position `t` into a segment index `i0` and the // line segment interpolation parameter `u`. `i1` is the index of the next // control point. @@ -385,8 +386,8 @@ int main(int argc, char** argv) { // Eigen::MatrixXd is column major so we define our own MatrixXd which is // row major. Eigen::VectorXd can be used directly. - typedef Eigen::Matrix - MatrixXd; + using MatrixXd = + Eigen::Matrix; using Eigen::VectorXd; // `X` is the matrix of control points which make up the contour of line diff --git a/examples/fields_of_experts.h b/examples/fields_of_experts.h index 429881d7b..b942924e7 100644 --- a/examples/fields_of_experts.h +++ b/examples/fields_of_experts.h @@ -63,9 +63,9 @@ class FieldsOfExpertsCost : public ceres::CostFunction { explicit FieldsOfExpertsCost(const std::vector& filter); // The number of scalar parameters passed to Evaluate must equal the number of // filter coefficients passed to the constructor. - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const; + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override; private: const std::vector& filter_; @@ -78,7 +78,7 @@ class FieldsOfExpertsCost : public ceres::CostFunction { class FieldsOfExpertsLoss : public ceres::LossFunction { public: explicit FieldsOfExpertsLoss(double alpha) : alpha_(alpha) {} - virtual void Evaluate(double, double*) const; + void Evaluate(double, double*) const override; private: const double alpha_; diff --git a/examples/helloworld_analytic_diff.cc b/examples/helloworld_analytic_diff.cc index 5e452cc4a..273a10282 100644 --- a/examples/helloworld_analytic_diff.cc +++ b/examples/helloworld_analytic_diff.cc @@ -49,11 +49,10 @@ class QuadraticCostFunction : public SizedCostFunction<1 /* number of residuals */, 1 /* size of first parameter */> { public: - virtual ~QuadraticCostFunction() {} - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const { + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override { double x = parameters[0][0]; // f(x) = 10 - x. diff --git a/examples/libmv_bundle_adjuster.cc b/examples/libmv_bundle_adjuster.cc index 91629c981..4f004069c 100644 --- a/examples/libmv_bundle_adjuster.cc +++ b/examples/libmv_bundle_adjuster.cc @@ -100,9 +100,10 @@ #define close _close typedef unsigned __int32 uint32_t; #else -#include #include +#include + // O_BINARY is not defined on unix like platforms, as there is no // difference between binary and text files. #define O_BINARY 0 @@ -114,10 +115,10 @@ typedef unsigned __int32 uint32_t; #include "gflags/gflags.h" #include "glog/logging.h" -typedef Eigen::Matrix Mat3; -typedef Eigen::Matrix Vec6; -typedef Eigen::Vector3d Vec3; -typedef Eigen::Vector4d Vec4; +using Mat3 = Eigen::Matrix; +using Vec6 = Eigen::Matrix; +using Vec3 = Eigen::Vector3d; +using Vec4 = Eigen::Vector4d; using std::vector; @@ -135,10 +136,10 @@ namespace { // R is a 3x3 matrix representing the rotation of the camera. // t is a translation vector representing its positions. struct EuclideanCamera { - EuclideanCamera() : image(-1) {} - EuclideanCamera(const EuclideanCamera& c) : image(c.image), R(c.R), t(c.t) {} + EuclideanCamera() = default; + EuclideanCamera(const EuclideanCamera& c) = default; - int image; + int image{-1}; Mat3 R; Vec3 t; }; @@ -148,9 +149,9 @@ struct EuclideanCamera { // track identifies which track this point corresponds to. // X represents the 3D position of the track. struct EuclideanPoint { - EuclideanPoint() : track(-1) {} - EuclideanPoint(const EuclideanPoint& p) : track(p.track), X(p.X) {} - int track; + EuclideanPoint() = default; + EuclideanPoint(const EuclideanPoint& p) = default; + int track{-1}; Vec3 X; }; @@ -262,7 +263,7 @@ EuclideanPoint* PointForTrack(vector* all_points, // denotes file endianness in this way. class EndianAwareFileReader { public: - EndianAwareFileReader(void) : file_descriptor_(-1) { + EndianAwareFileReader() { // Get an endian type of the host machine. union { unsigned char bytes[4]; @@ -272,7 +273,7 @@ class EndianAwareFileReader { file_endian_type_ = host_endian_type_; } - ~EndianAwareFileReader(void) { + ~EndianAwareFileReader() { if (file_descriptor_ > 0) { close(file_descriptor_); } @@ -284,7 +285,7 @@ class EndianAwareFileReader { return false; } // Get an endian tpye of data in the file. - unsigned char file_endian_type_flag = Read(); + auto file_endian_type_flag = Read(); if (file_endian_type_flag == 'V') { file_endian_type_ = kBigEndian; } else if (file_endian_type_flag == 'v') { @@ -297,7 +298,7 @@ class EndianAwareFileReader { // Read value from the file, will switch endian if needed. template - T Read(void) const { + T Read() const { T value; CHECK_GT(read(file_descriptor_, &value, sizeof(value)), 0); // Switch endian type if file contains data in different type @@ -316,7 +317,7 @@ class EndianAwareFileReader { template T SwitchEndian(const T value) const { if (sizeof(T) == 4) { - unsigned int temp_value = static_cast(value); + auto temp_value = static_cast(value); // clang-format off return ((temp_value >> 24)) | ((temp_value << 8) & 0x00ff0000) | @@ -333,7 +334,7 @@ class EndianAwareFileReader { int host_endian_type_; int file_endian_type_; - int file_descriptor_; + int file_descriptor_{-1}; }; // Read 3x3 column-major matrix from the file @@ -379,7 +380,7 @@ bool ReadProblemFromFile(const std::string& file_name, } // Read markers' space flag. - unsigned char is_image_space_flag = file_reader.Read(); + auto is_image_space_flag = file_reader.Read(); if (is_image_space_flag == 'P') { *is_image_space = true; } else if (is_image_space_flag == 'N') { @@ -692,8 +693,7 @@ void EuclideanBundleCommonIntrinsics(const vector& all_markers, int num_residuals = 0; bool have_locked_camera = false; - for (int i = 0; i < all_markers.size(); ++i) { - const Marker& marker = all_markers[i]; + for (const auto& marker : all_markers) { EuclideanCamera* camera = CameraForImage(all_cameras, marker.image); EuclideanPoint* point = PointForTrack(all_points, marker.track); if (camera == nullptr || point == nullptr) { @@ -759,8 +759,7 @@ void EuclideanBundleCommonIntrinsics(const vector& all_markers, // Always set K3 constant, it's not used at the moment. constant_intrinsics.push_back(OFFSET_K3); - ceres::SubsetManifold* subset_manifold = - new ceres::SubsetManifold(8, constant_intrinsics); + auto* subset_manifold = new ceres::SubsetManifold(8, constant_intrinsics); problem.SetManifold(camera_intrinsics, subset_manifold); } diff --git a/examples/libmv_homography.cc b/examples/libmv_homography.cc index 1b51b829c..b795ab1ff 100644 --- a/examples/libmv_homography.cc +++ b/examples/libmv_homography.cc @@ -60,17 +60,19 @@ // This example demonstrates custom exit criterion by having a callback check // for image-space error. +#include + #include "ceres/ceres.h" #include "glog/logging.h" -typedef Eigen::NumTraits EigenDouble; +using EigenDouble = Eigen::NumTraits; -typedef Eigen::MatrixXd Mat; -typedef Eigen::VectorXd Vec; -typedef Eigen::Matrix Mat3; -typedef Eigen::Matrix Vec2; -typedef Eigen::Matrix MatX8; -typedef Eigen::Vector3d Vec3; +using Mat = Eigen::MatrixXd; +using Vec = Eigen::VectorXd; +using Mat3 = Eigen::Matrix; +using Vec2 = Eigen::Matrix; +using MatX8 = Eigen::Matrix; +using Vec3 = Eigen::Vector3d; namespace { @@ -82,11 +84,10 @@ namespace { struct EstimateHomographyOptions { // Default settings for homography estimation which should be suitable // for a wide range of use cases. - EstimateHomographyOptions() - : max_num_iterations(50), expected_average_symmetric_distance(1e-16) {} + EstimateHomographyOptions() = default; // Maximal number of iterations for the refinement step. - int max_num_iterations; + int max_num_iterations{50}; // Expected average of symmetric geometric distance between // actual destination points and original ones transformed by @@ -96,7 +97,7 @@ struct EstimateHomographyOptions { // geometric distance is less or equal to this value. // // This distance is measured in the same units as input points are. - double expected_average_symmetric_distance; + double expected_average_symmetric_distance{1e-16}; }; // Calculate symmetric geometric cost terms: @@ -111,7 +112,7 @@ void SymmetricGeometricDistanceTerms(const Eigen::Matrix& H, const Eigen::Matrix& x2, T forward_error[2], T backward_error[2]) { - typedef Eigen::Matrix Vec3; + using Vec3 = Eigen::Matrix; Vec3 x(x1(0), x1(1), T(1.0)); Vec3 y(x2(0), x2(1), T(1.0)); @@ -152,8 +153,8 @@ double SymmetricGeometricDistance(const Mat3& H, template class Homography2DNormalizedParameterization { public: - typedef Eigen::Matrix Parameters; // a, b, ... g, h - typedef Eigen::Matrix Parameterized; // H + using Parameters = Eigen::Matrix; // a, b, ... g, h + using Parameterized = Eigen::Matrix; // H // Convert from the 8 parameters to a H matrix. static void To(const Parameters& p, Parameterized* h) { @@ -242,13 +243,13 @@ bool Homography2DFromCorrespondencesLinearEuc(const Mat& x1, // used for homography matrix refinement. class HomographySymmetricGeometricCostFunctor { public: - HomographySymmetricGeometricCostFunctor(const Vec2& x, const Vec2& y) - : x_(x), y_(y) {} + HomographySymmetricGeometricCostFunctor(Vec2 x, Vec2 y) + : x_(std::move(x)), y_(std::move(y)) {} template bool operator()(const T* homography_parameters, T* residuals) const { - typedef Eigen::Matrix Mat3; - typedef Eigen::Matrix Vec2; + using Mat3 = Eigen::Matrix; + using Vec2 = Eigen::Matrix; Mat3 H(homography_parameters); Vec2 x(T(x_(0)), T(x_(1))); @@ -277,8 +278,8 @@ class TerminationCheckingCallback : public ceres::IterationCallback { Mat3* H) : options_(options), x1_(x1), x2_(x2), H_(H) {} - virtual ceres::CallbackReturnType operator()( - const ceres::IterationSummary& summary) { + ceres::CallbackReturnType operator()( + const ceres::IterationSummary& summary) override { // If the step wasn't successful, there's nothing to do. if (!summary.step_is_successful) { return ceres::SOLVER_CONTINUE; @@ -326,9 +327,8 @@ bool EstimateHomography2DFromCorrespondences( // Step 2: Refine matrix using Ceres minimizer. ceres::Problem problem; for (int i = 0; i < x1.cols(); i++) { - HomographySymmetricGeometricCostFunctor* - homography_symmetric_geometric_cost_function = - new HomographySymmetricGeometricCostFunctor(x1.col(i), x2.col(i)); + auto* homography_symmetric_geometric_cost_function = + new HomographySymmetricGeometricCostFunctor(x1.col(i), x2.col(i)); problem.AddResidualBlock( new ceres::AutoDiffCostFunction Vector; -typedef Eigen::Matrix Matrix; +using Vector = Eigen::Matrix; +using Matrix = Eigen::Matrix; using std::atof; using std::atoi; @@ -528,7 +528,7 @@ template CostFunction* CreateCostFunction(const Matrix& predictor, const Matrix& response, const int num_observations) { - Model* model = new Model(predictor.data(), response.data(), num_observations); + auto* model = new Model(predictor.data(), response.data(), num_observations); ceres::CostFunction* cost_function = nullptr; if (CERES_GET_FLAG(FLAGS_use_numeric_diff)) { ceres::NumericDiffOptions options; @@ -620,9 +620,8 @@ int RegressionDriver(const string& filename) { } else { ceres::TinySolverCostFunctionAdapter cfa( *cost_function); - typedef ceres::TinySolver< - ceres::TinySolverCostFunctionAdapter> - Solver; + using Solver = ceres::TinySolver< + ceres::TinySolverCostFunctionAdapter>; Solver solver; solver.options.max_num_iterations = CERES_GET_FLAG(FLAGS_num_iterations); solver.options.gradient_tolerance = diff --git a/examples/random.h b/examples/random.h index ace071123..e5024be53 100644 --- a/examples/random.h +++ b/examples/random.h @@ -31,8 +31,8 @@ #ifndef CERES_EXAMPLES_RANDOM_H_ #define CERES_EXAMPLES_RANDOM_H_ -#include -#include +#include +#include namespace ceres { namespace examples { @@ -40,7 +40,7 @@ namespace examples { // Return a random number sampled from a uniform distribution in the range // [0,1]. inline double RandDouble() { - double r = static_cast(rand()); + auto r = static_cast(rand()); return r / RAND_MAX; } diff --git a/examples/robot_pose_mle.cc b/examples/robot_pose_mle.cc index 90d5ab669..5fb4749eb 100644 --- a/examples/robot_pose_mle.cc +++ b/examples/robot_pose_mle.cc @@ -125,8 +125,7 @@ // will be computed by a DynamicAutoDiffCostFunction since the number of // odoemtry observations will only be known at run time. -#include - +#include #include #include @@ -169,7 +168,7 @@ DEFINE_double(range_stddev, static constexpr int kStride = 10; struct OdometryConstraint { - typedef AutoDiffCostFunction OdometryCostFunction; + using OdometryCostFunction = AutoDiffCostFunction; OdometryConstraint(double odometry_mean, double odometry_stddev) : odometry_mean(odometry_mean), odometry_stddev(odometry_stddev) {} @@ -190,8 +189,8 @@ struct OdometryConstraint { }; struct RangeConstraint { - typedef DynamicAutoDiffCostFunction - RangeCostFunction; + using RangeCostFunction = + DynamicAutoDiffCostFunction; RangeConstraint(int pose_index, double range_reading, @@ -219,12 +218,12 @@ struct RangeConstraint { const double range_reading, vector* odometry_values, vector* parameter_blocks) { - RangeConstraint* constraint = + auto* constraint = new RangeConstraint(pose_index, range_reading, CERES_GET_FLAG(FLAGS_range_stddev), CERES_GET_FLAG(FLAGS_corridor_length)); - RangeCostFunction* cost_function = new RangeCostFunction(constraint); + auto* cost_function = new RangeCostFunction(constraint); // Add all the parameter blocks that affect this constraint. parameter_blocks->clear(); for (int i = 0; i <= pose_index; ++i) { diff --git a/examples/rosenbrock_analytic_diff.cc b/examples/rosenbrock_analytic_diff.cc index b133f5a3f..96db3482e 100644 --- a/examples/rosenbrock_analytic_diff.cc +++ b/examples/rosenbrock_analytic_diff.cc @@ -34,7 +34,6 @@ // f(x,y) = (1-x)^2 + 100(y - x^2)^2; class Rosenbrock final : public ceres::FirstOrderFunction { public: - ~Rosenbrock() override {} bool Evaluate(const double* parameters, double* cost, diff --git a/examples/slam/pose_graph_2d/pose_graph_2d.cc b/examples/slam/pose_graph_2d/pose_graph_2d.cc index b57f4e729..410e8ba9a 100644 --- a/examples/slam/pose_graph_2d/pose_graph_2d.cc +++ b/examples/slam/pose_graph_2d/pose_graph_2d.cc @@ -68,18 +68,11 @@ void BuildOptimizationProblem(const std::vector& constraints, ceres::LossFunction* loss_function = nullptr; ceres::Manifold* angle_manifold = AngleManifold::Create(); - for (std::vector::const_iterator constraints_iter = - constraints.begin(); - constraints_iter != constraints.end(); - ++constraints_iter) { - const Constraint2d& constraint = *constraints_iter; - - std::map::iterator pose_begin_iter = - poses->find(constraint.id_begin); + for (const auto& constraint : constraints) { + auto pose_begin_iter = poses->find(constraint.id_begin); CHECK(pose_begin_iter != poses->end()) << "Pose with ID: " << constraint.id_begin << " not found."; - std::map::iterator pose_end_iter = - poses->find(constraint.id_end); + auto pose_end_iter = poses->find(constraint.id_end); CHECK(pose_end_iter != poses->end()) << "Pose with ID: " << constraint.id_end << " not found."; @@ -108,7 +101,7 @@ void BuildOptimizationProblem(const std::vector& constraints, // internal damping which mitigate this issue, but it is better to properly // constrain the gauge freedom. This can be done by setting one of the poses // as constant so the optimizer cannot change it. - std::map::iterator pose_start_iter = poses->begin(); + auto pose_start_iter = poses->begin(); CHECK(pose_start_iter != poses->end()) << "There are no poses."; problem->SetParameterBlockConstant(&pose_start_iter->second.x); problem->SetParameterBlockConstant(&pose_start_iter->second.y); @@ -140,10 +133,7 @@ bool OutputPoses(const std::string& filename, std::cerr << "Error opening the file: " << filename << '\n'; return false; } - for (std::map::const_iterator poses_iter = poses.begin(); - poses_iter != poses.end(); - ++poses_iter) { - const std::map::value_type& pair = *poses_iter; + for (const auto& pair : poses) { outfile << pair.first << " " << pair.second.x << " " << pair.second.y << ' ' << pair.second.yaw_radians << '\n'; } diff --git a/examples/slam/pose_graph_3d/pose_graph_3d.cc b/examples/slam/pose_graph_3d/pose_graph_3d.cc index 798e2366f..dcd0cbedd 100644 --- a/examples/slam/pose_graph_3d/pose_graph_3d.cc +++ b/examples/slam/pose_graph_3d/pose_graph_3d.cc @@ -60,16 +60,11 @@ void BuildOptimizationProblem(const VectorOfConstraints& constraints, ceres::LossFunction* loss_function = nullptr; ceres::Manifold* quaternion_manifold = new EigenQuaternionManifold; - for (VectorOfConstraints::const_iterator constraints_iter = - constraints.begin(); - constraints_iter != constraints.end(); - ++constraints_iter) { - const Constraint3d& constraint = *constraints_iter; - - MapOfPoses::iterator pose_begin_iter = poses->find(constraint.id_begin); + for (const auto& constraint : constraints) { + auto pose_begin_iter = poses->find(constraint.id_begin); CHECK(pose_begin_iter != poses->end()) << "Pose with ID: " << constraint.id_begin << " not found."; - MapOfPoses::iterator pose_end_iter = poses->find(constraint.id_end); + auto pose_end_iter = poses->find(constraint.id_end); CHECK(pose_end_iter != poses->end()) << "Pose with ID: " << constraint.id_end << " not found."; @@ -99,7 +94,7 @@ void BuildOptimizationProblem(const VectorOfConstraints& constraints, // internal damping which mitigates this issue, but it is better to properly // constrain the gauge freedom. This can be done by setting one of the poses // as constant so the optimizer cannot change it. - MapOfPoses::iterator pose_start_iter = poses->begin(); + auto pose_start_iter = poses->begin(); CHECK(pose_start_iter != poses->end()) << "There are no poses."; problem->SetParameterBlockConstant(pose_start_iter->second.p.data()); problem->SetParameterBlockConstant(pose_start_iter->second.q.coeffs().data()); @@ -129,18 +124,7 @@ bool OutputPoses(const std::string& filename, const MapOfPoses& poses) { LOG(ERROR) << "Error opening the file: " << filename; return false; } - for (std::map, - Eigen::aligned_allocator>>:: - const_iterator poses_iter = poses.begin(); - poses_iter != poses.end(); - ++poses_iter) { - const std::map, - Eigen::aligned_allocator>>:: - value_type& pair = *poses_iter; + for (const auto& pair : poses) { outfile << pair.first << " " << pair.second.p.transpose() << " " << pair.second.q.x() << " " << pair.second.q.y() << " " << pair.second.q.z() << " " << pair.second.q.w() << '\n'; diff --git a/examples/slam/pose_graph_3d/pose_graph_3d_error_term.h b/examples/slam/pose_graph_3d/pose_graph_3d_error_term.h index 1f3e8dedd..0e4afe97b 100644 --- a/examples/slam/pose_graph_3d/pose_graph_3d_error_term.h +++ b/examples/slam/pose_graph_3d/pose_graph_3d_error_term.h @@ -31,6 +31,8 @@ #ifndef EXAMPLES_CERES_POSE_GRAPH_3D_ERROR_TERM_H_ #define EXAMPLES_CERES_POSE_GRAPH_3D_ERROR_TERM_H_ +#include + #include "Eigen/Core" #include "ceres/autodiff_cost_function.h" #include "types.h" @@ -69,9 +71,10 @@ namespace examples { // where I is the information matrix which is the inverse of the covariance. class PoseGraph3dErrorTerm { public: - PoseGraph3dErrorTerm(const Pose3d& t_ab_measured, - const Eigen::Matrix& sqrt_information) - : t_ab_measured_(t_ab_measured), sqrt_information_(sqrt_information) {} + PoseGraph3dErrorTerm(Pose3d t_ab_measured, + Eigen::Matrix sqrt_information) + : t_ab_measured_(std::move(t_ab_measured)), + sqrt_information_(std::move(sqrt_information)) {} template bool operator()(const T* const p_a_ptr, diff --git a/examples/slam/pose_graph_3d/types.h b/examples/slam/pose_graph_3d/types.h index d3f19ed9d..67b07154d 100644 --- a/examples/slam/pose_graph_3d/types.h +++ b/examples/slam/pose_graph_3d/types.h @@ -61,11 +61,11 @@ inline std::istream& operator>>(std::istream& input, Pose3d& pose) { return input; } -typedef std::map, - Eigen::aligned_allocator>> - MapOfPoses; +using MapOfPoses = + std::map, + Eigen::aligned_allocator>>; // The constraint between two vertices in the pose graph. The constraint is the // transformation from vertex id_begin to vertex id_end. @@ -103,8 +103,8 @@ inline std::istream& operator>>(std::istream& input, Constraint3d& constraint) { return input; } -typedef std::vector> - VectorOfConstraints; +using VectorOfConstraints = + std::vector>; } // namespace examples } // namespace ceres diff --git a/include/ceres/autodiff_cost_function.h b/include/ceres/autodiff_cost_function.h index c080fef96..cd256432a 100644 --- a/include/ceres/autodiff_cost_function.h +++ b/include/ceres/autodiff_cost_function.h @@ -179,7 +179,7 @@ class AutoDiffCostFunction final SizedCostFunction::set_num_residuals(num_residuals); } - explicit AutoDiffCostFunction(AutoDiffCostFunction&& other) + AutoDiffCostFunction(AutoDiffCostFunction&& other) : functor_(std::move(other.functor_)), ownership_(other.ownership_) {} virtual ~AutoDiffCostFunction() { diff --git a/include/ceres/autodiff_first_order_function.h b/include/ceres/autodiff_first_order_function.h index 5a51952ed..7c13f4239 100644 --- a/include/ceres/autodiff_first_order_function.h +++ b/include/ceres/autodiff_first_order_function.h @@ -117,7 +117,7 @@ class AutoDiffFirstOrderFunction final : public FirstOrderFunction { return (*functor_)(parameters, cost); } - typedef Jet JetT; + using JetT = Jet; internal::FixedArray x(kNumParameters); for (int i = 0; i < kNumParameters; ++i) { x[i].a = parameters[i]; diff --git a/include/ceres/autodiff_manifold.h b/include/ceres/autodiff_manifold.h index 978e13e7f..3063e19e8 100644 --- a/include/ceres/autodiff_manifold.h +++ b/include/ceres/autodiff_manifold.h @@ -184,7 +184,7 @@ namespace internal { // expects a Functor with operator(). template struct PlusWrapper { - PlusWrapper(const Functor& functor) : functor(functor) {} + explicit PlusWrapper(const Functor& functor) : functor(functor) {} template bool operator()(const T* x, const T* delta, T* x_plus_delta) const { return functor.Plus(x, delta, x_plus_delta); @@ -194,7 +194,7 @@ struct PlusWrapper { template struct MinusWrapper { - MinusWrapper(const Functor& functor) : functor(functor) {} + explicit MinusWrapper(const Functor& functor) : functor(functor) {} template bool operator()(const T* y, const T* x, T* y_minus_x) const { return functor.Minus(y, x, y_minus_x); diff --git a/include/ceres/crs_matrix.h b/include/ceres/crs_matrix.h index 286733c59..faa0f9885 100644 --- a/include/ceres/crs_matrix.h +++ b/include/ceres/crs_matrix.h @@ -41,10 +41,10 @@ namespace ceres { // A compressed row sparse matrix used primarily for communicating the // Jacobian matrix to the user. struct CERES_EXPORT CRSMatrix { - CRSMatrix() : num_rows(0), num_cols(0) {} + CRSMatrix() = default; - int num_rows; - int num_cols; + int num_rows{0}; + int num_cols{0}; // A compressed row matrix stores its contents in three arrays, // rows, cols and values. diff --git a/include/ceres/cubic_interpolation.h b/include/ceres/cubic_interpolation.h index f84417e8d..134bad285 100644 --- a/include/ceres/cubic_interpolation.h +++ b/include/ceres/cubic_interpolation.h @@ -69,7 +69,7 @@ void CubicHermiteSpline(const Eigen::Matrix& p0, const double x, double* f, double* dfdx) { - typedef Eigen::Matrix VType; + using VType = Eigen::Matrix; const VType a = 0.5 * (-p0 + 3.0 * p1 - 3.0 * p2 + p3); const VType b = 0.5 * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3); const VType c = 0.5 * (-p0 + p2); diff --git a/include/ceres/dynamic_autodiff_cost_function.h b/include/ceres/dynamic_autodiff_cost_function.h index d72be6d20..c21d0517f 100644 --- a/include/ceres/dynamic_autodiff_cost_function.h +++ b/include/ceres/dynamic_autodiff_cost_function.h @@ -80,11 +80,11 @@ template class DynamicAutoDiffCostFunction final : public DynamicCostFunction { public: // Takes ownership by default. - DynamicAutoDiffCostFunction(CostFunctor* functor, - Ownership ownership = TAKE_OWNERSHIP) + explicit DynamicAutoDiffCostFunction(CostFunctor* functor, + Ownership ownership = TAKE_OWNERSHIP) : functor_(functor), ownership_(ownership) {} - explicit DynamicAutoDiffCostFunction(DynamicAutoDiffCostFunction&& other) + DynamicAutoDiffCostFunction(DynamicAutoDiffCostFunction&& other) : functor_(std::move(other.functor_)), ownership_(other.ownership_) {} ~DynamicAutoDiffCostFunction() override { diff --git a/include/ceres/dynamic_numeric_diff_cost_function.h b/include/ceres/dynamic_numeric_diff_cost_function.h index 793fb0554..e1892e8ba 100644 --- a/include/ceres/dynamic_numeric_diff_cost_function.h +++ b/include/ceres/dynamic_numeric_diff_cost_function.h @@ -85,8 +85,7 @@ class DynamicNumericDiffCostFunction final : public DynamicCostFunction { const NumericDiffOptions& options = NumericDiffOptions()) : functor_(functor), ownership_(ownership), options_(options) {} - explicit DynamicNumericDiffCostFunction( - DynamicNumericDiffCostFunction&& other) + DynamicNumericDiffCostFunction(DynamicNumericDiffCostFunction&& other) : functor_(std::move(other.functor_)), ownership_(other.ownership_) {} ~DynamicNumericDiffCostFunction() override { diff --git a/include/ceres/internal/array_selector.h b/include/ceres/internal/array_selector.h index 841797f4c..b4db012f0 100644 --- a/include/ceres/internal/array_selector.h +++ b/include/ceres/internal/array_selector.h @@ -73,20 +73,22 @@ struct ArraySelector : ceres::internal::FixedArray { - ArraySelector(int s) + explicit ArraySelector(int s) : ceres::internal::FixedArray(s) {} }; template struct ArraySelector : std::array { - ArraySelector(int s) { CHECK_EQ(s, num_elements); } + explicit ArraySelector(int s) { CHECK_EQ(s, num_elements); } }; template struct ArraySelector : std::vector { - ArraySelector(int s) : std::vector(s) { CHECK_EQ(s, num_elements); } + explicit ArraySelector(int s) : std::vector(s) { + CHECK_EQ(s, num_elements); + } }; } // namespace internal diff --git a/include/ceres/internal/autodiff.h b/include/ceres/internal/autodiff.h index 4ceb56ee5..fe64e824f 100644 --- a/include/ceres/internal/autodiff.h +++ b/include/ceres/internal/autodiff.h @@ -310,7 +310,7 @@ inline bool AutoDifferentiate(const Functor& functor, int dynamic_num_outputs, T* function_value, T** jacobians) { - typedef Jet JetT; + using JetT = Jet; using Parameters = typename ParameterDims::Parameters; if (kNumResiduals != DYNAMIC) { diff --git a/include/ceres/internal/eigen.h b/include/ceres/internal/eigen.h index b6d0b7f61..111cc7a07 100644 --- a/include/ceres/internal/eigen.h +++ b/include/ceres/internal/eigen.h @@ -35,39 +35,39 @@ namespace ceres { -typedef Eigen::Matrix Vector; -typedef Eigen::Matrix - Matrix; -typedef Eigen::Map VectorRef; -typedef Eigen::Map MatrixRef; -typedef Eigen::Map ConstVectorRef; -typedef Eigen::Map ConstMatrixRef; +using Vector = Eigen::Matrix; +using Matrix = + Eigen::Matrix; +using VectorRef = Eigen::Map; +using MatrixRef = Eigen::Map; +using ConstVectorRef = Eigen::Map; +using ConstMatrixRef = Eigen::Map; // Column major matrices for DenseSparseMatrix/DenseQRSolver -typedef Eigen::Matrix - ColMajorMatrix; +using ColMajorMatrix = + Eigen::Matrix; -typedef Eigen::Map> - ColMajorMatrixRef; +using ColMajorMatrixRef = + Eigen::Map>; -typedef Eigen::Map> - ConstColMajorMatrixRef; +using ConstColMajorMatrixRef = + Eigen::Map>; // C++ does not support templated typdefs, thus the need for this // struct so that we can support statically sized Matrix and Maps. template struct EigenTypes { - typedef Eigen::Matrix - Matrix; + using Matrix = + Eigen::Matrix; - typedef Eigen::Map MatrixRef; - typedef Eigen::Map ConstMatrixRef; - typedef Eigen::Matrix Vector; - typedef Eigen::Map> VectorRef; - typedef Eigen::Map> ConstVectorRef; + using MatrixRef = Eigen::Map; + using ConstMatrixRef = Eigen::Map; + using Vector = Eigen::Matrix; + using VectorRef = Eigen::Map>; + using ConstVectorRef = Eigen::Map>; }; } // namespace ceres diff --git a/include/ceres/internal/numeric_diff.h b/include/ceres/internal/numeric_diff.h index ff7a2c345..c44733f6d 100644 --- a/include/ceres/internal/numeric_diff.h +++ b/include/ceres/internal/numeric_diff.h @@ -86,18 +86,18 @@ struct NumericDiff { (kParameterBlockSize != ceres::DYNAMIC ? kParameterBlockSize : parameter_block_size); - typedef Matrix ResidualVector; - typedef Matrix ParameterVector; + using ResidualVector = Matrix; + using ParameterVector = Matrix; // The convoluted reasoning for choosing the Row/Column major // ordering of the matrix is an artifact of the restrictions in // Eigen that prevent it from creating RowMajor matrices with a // single column. In these cases, we ask for a ColMajor matrix. - typedef Matrix - JacobianMatrix; + using JacobianMatrix = + Matrix; Map parameter_jacobian( jacobian, num_residuals_internal, parameter_block_size_internal); @@ -184,8 +184,8 @@ struct NumericDiff { using Eigen::Map; using Eigen::Matrix; - typedef Matrix ResidualVector; - typedef Matrix ParameterVector; + using ResidualVector = Matrix; + using ParameterVector = Matrix; Map x(x_ptr, parameter_block_size); Map x_plus_delta(x_plus_delta_ptr, parameter_block_size); @@ -260,10 +260,10 @@ struct NumericDiff { using Eigen::Map; using Eigen::Matrix; - typedef Matrix ResidualVector; - typedef Matrix - ResidualCandidateMatrix; - typedef Matrix ParameterVector; + using ResidualVector = Matrix; + using ResidualCandidateMatrix = + Matrix; + using ParameterVector = Matrix; Map x(x_ptr, parameter_block_size); Map x_plus_delta(x_plus_delta_ptr, parameter_block_size); diff --git a/include/ceres/jet.h b/include/ceres/jet.h index dee27e496..10ce51e70 100644 --- a/include/ceres/jet.h +++ b/include/ceres/jet.h @@ -210,7 +210,7 @@ namespace ceres { template struct Jet { enum { DIMENSION = N }; - typedef T Scalar; + using Scalar = T; // Default-construct "a" because otherwise this can lead to false errors about // uninitialized uses when other classes relying on default constructed T @@ -1284,10 +1284,10 @@ namespace Eigen { // Eigen arrays, getting all the goodness of Eigen combined with autodiff. template struct NumTraits> { - typedef ceres::Jet Real; - typedef ceres::Jet NonInteger; - typedef ceres::Jet Nested; - typedef ceres::Jet Literal; + using Real = ceres::Jet; + using NonInteger = ceres::Jet; + using Nested = ceres::Jet; + using Literal = ceres::Jet; static typename ceres::Jet dummy_precision() { return ceres::Jet(1e-12); @@ -1338,11 +1338,11 @@ struct NumTraits> { // is only available on Eigen versions >= 3.3 template struct ScalarBinaryOpTraits, T, BinaryOp> { - typedef ceres::Jet ReturnType; + using ReturnType = ceres::Jet; }; template struct ScalarBinaryOpTraits, BinaryOp> { - typedef ceres::Jet ReturnType; + using ReturnType = ceres::Jet; }; } // namespace Eigen diff --git a/include/ceres/local_parameterization.h b/include/ceres/local_parameterization.h index 07093d25a..5815dd17d 100644 --- a/include/ceres/local_parameterization.h +++ b/include/ceres/local_parameterization.h @@ -322,11 +322,8 @@ class CERES_DEPRECATED_WITH_MSG("Use ProductManifold instead.") // parameterizations. // template - ProductParameterization(LocalParams*... local_params) - : local_params_(sizeof...(LocalParams)), - local_size_{0}, - global_size_{0}, - buffer_size_{0} { + explicit ProductParameterization(LocalParams*... local_params) + : local_params_(sizeof...(LocalParams)) { constexpr int kNumLocalParams = sizeof...(LocalParams); static_assert(kNumLocalParams >= 2, "At least two local parameterizations must be specified."); @@ -358,9 +355,9 @@ class CERES_DEPRECATED_WITH_MSG("Use ProductManifold instead.") private: std::vector> local_params_; - int local_size_; - int global_size_; - int buffer_size_; + int local_size_{0}; + int global_size_{0}; + int buffer_size_{0}; }; } // namespace ceres diff --git a/include/ceres/manifold.h b/include/ceres/manifold.h index 964baec78..14a5792cc 100644 --- a/include/ceres/manifold.h +++ b/include/ceres/manifold.h @@ -225,7 +225,7 @@ class CERES_EXPORT Manifold { // Minus(y, x) = y - x. class CERES_EXPORT EuclideanManifold final : public Manifold { public: - EuclideanManifold(int size); + explicit EuclideanManifold(int size); int AmbientSize() const override; int TangentSize() const override; bool Plus(const double* x, @@ -290,7 +290,8 @@ class CERES_EXPORT ProductManifold final : public Manifold { // manifolds. // template - ProductManifold(Manifolds*... manifolds) : manifolds_(sizeof...(Manifolds)) { + explicit ProductManifold(Manifolds*... manifolds) + : manifolds_(sizeof...(Manifolds)) { constexpr int kNumManifolds = sizeof...(Manifolds); static_assert(kNumManifolds >= 2, "At least two manifolds must be specified."); diff --git a/include/ceres/numeric_diff_cost_function.h b/include/ceres/numeric_diff_cost_function.h index 7235327af..6ec531750 100644 --- a/include/ceres/numeric_diff_cost_function.h +++ b/include/ceres/numeric_diff_cost_function.h @@ -182,7 +182,7 @@ template { public: - NumericDiffCostFunction( + explicit NumericDiffCostFunction( CostFunctor* functor, Ownership ownership = TAKE_OWNERSHIP, int num_residuals = kNumResiduals, @@ -193,7 +193,7 @@ class NumericDiffCostFunction final } } - explicit NumericDiffCostFunction(NumericDiffCostFunction&& other) + NumericDiffCostFunction(NumericDiffCostFunction&& other) : functor_(std::move(other.functor_)), ownership_(other.ownership_) {} virtual ~NumericDiffCostFunction() { diff --git a/include/ceres/numeric_diff_first_order_function.h b/include/ceres/numeric_diff_first_order_function.h index 5841765b1..f5bb005be 100644 --- a/include/ceres/numeric_diff_first_order_function.h +++ b/include/ceres/numeric_diff_first_order_function.h @@ -104,7 +104,7 @@ template class NumericDiffFirstOrderFunction final : public FirstOrderFunction { public: - NumericDiffFirstOrderFunction( + explicit NumericDiffFirstOrderFunction( FirstOrderFunctor* functor, Ownership ownership = TAKE_OWNERSHIP, const NumericDiffOptions& options = NumericDiffOptions()) diff --git a/include/ceres/ordered_groups.h b/include/ceres/ordered_groups.h index 76a1c8ecb..c1531cce6 100644 --- a/include/ceres/ordered_groups.h +++ b/include/ceres/ordered_groups.h @@ -190,7 +190,7 @@ class OrderedGroups { }; // Typedef for the most commonly used version of OrderedGroups. -typedef OrderedGroups ParameterBlockOrdering; +using ParameterBlockOrdering = OrderedGroups; } // namespace ceres diff --git a/include/ceres/problem.h b/include/ceres/problem.h index 88bff7433..819fa454b 100644 --- a/include/ceres/problem.h +++ b/include/ceres/problem.h @@ -67,7 +67,7 @@ class ResidualBlock; // A ResidualBlockId is an opaque handle clients can use to remove residual // blocks from a Problem after adding them. -typedef internal::ResidualBlock* ResidualBlockId; +using ResidualBlockId = internal::ResidualBlock*; // A class to represent non-linear least squares problems. Such // problems have a cost function that is a sum of error terms (known diff --git a/include/ceres/tiny_solver.h b/include/ceres/tiny_solver.h index a8e699b04..16fd2a84a 100644 --- a/include/ceres/tiny_solver.h +++ b/include/ceres/tiny_solver.h @@ -140,8 +140,8 @@ class TinySolver { NUM_RESIDUALS = Function::NUM_RESIDUALS, NUM_PARAMETERS = Function::NUM_PARAMETERS }; - typedef typename Function::Scalar Scalar; - typedef typename Eigen::Matrix Parameters; + using Scalar = typename Function::Scalar; + using Parameters = typename Eigen::Matrix; enum Status { // max_norm |J'(x) * f(x)| < gradient_tolerance @@ -349,7 +349,7 @@ class TinySolver { template struct enable_if { - typedef T type; + using type = T; }; // The number of parameters and residuals are dynamically sized. diff --git a/include/ceres/tiny_solver_autodiff_function.h b/include/ceres/tiny_solver_autodiff_function.h index 59ede83e2..3e3675ff0 100644 --- a/include/ceres/tiny_solver_autodiff_function.h +++ b/include/ceres/tiny_solver_autodiff_function.h @@ -113,12 +113,12 @@ class TinySolverAutoDiffFunction { // as a member a Jet type, which itself has a fixed-size Eigen type as member. EIGEN_MAKE_ALIGNED_OPERATOR_NEW - TinySolverAutoDiffFunction(const CostFunctor& cost_functor) + explicit TinySolverAutoDiffFunction(const CostFunctor& cost_functor) : cost_functor_(cost_functor) { Initialize(cost_functor); } - typedef T Scalar; + using Scalar = T; enum { NUM_PARAMETERS = kNumParameters, NUM_RESIDUALS = kNumResiduals, diff --git a/include/ceres/tiny_solver_cost_function_adapter.h b/include/ceres/tiny_solver_cost_function_adapter.h index 8ceb7aa4d..cc5ca16af 100644 --- a/include/ceres/tiny_solver_cost_function_adapter.h +++ b/include/ceres/tiny_solver_cost_function_adapter.h @@ -75,7 +75,7 @@ template class TinySolverCostFunctionAdapter { public: - typedef double Scalar; + using Scalar = double; enum ComponentSizeType { NUM_PARAMETERS = kNumParameters, NUM_RESIDUALS = kNumResiduals @@ -85,7 +85,7 @@ class TinySolverCostFunctionAdapter { // fixed-size Eigen types. EIGEN_MAKE_ALIGNED_OPERATOR_NEW - TinySolverCostFunctionAdapter(const CostFunction& cost_function) + explicit TinySolverCostFunctionAdapter(const CostFunction& cost_function) : cost_function_(cost_function) { CHECK_EQ(cost_function_.parameter_block_sizes().size(), 1) << "Only CostFunctions with exactly one parameter blocks are allowed."; diff --git a/internal/ceres/autodiff_benchmarks/relative_pose_error.h b/internal/ceres/autodiff_benchmarks/relative_pose_error.h index b5c1a936b..a54a92f09 100644 --- a/internal/ceres/autodiff_benchmarks/relative_pose_error.h +++ b/internal/ceres/autodiff_benchmarks/relative_pose_error.h @@ -33,6 +33,7 @@ #define CERES_INTERNAL_AUTODIFF_BENCHMARK_RELATIVE_POSE_ERROR_H_ #include +#include #include "ceres/rotation.h" @@ -43,9 +44,8 @@ namespace ceres { // poses T_w_i and T_w_j. For the residual we use the log of the the residual // pose, in split representation SO(3) x R^3. struct RelativePoseError { - RelativePoseError(const Eigen::Quaterniond& q_i_j, - const Eigen::Vector3d& t_i_j) - : meas_q_i_j_(q_i_j), meas_t_i_j_(t_i_j) {} + RelativePoseError(Eigen::Quaterniond q_i_j, Eigen::Vector3d t_i_j) + : meas_q_i_j_(std::move(q_i_j)), meas_t_i_j_(std::move(t_i_j)) {} template inline bool operator()(const T* const pose_i_ptr, diff --git a/internal/ceres/autodiff_cost_function_test.cc b/internal/ceres/autodiff_cost_function_test.cc index cc340f6d9..a59d524d5 100644 --- a/internal/ceres/autodiff_cost_function_test.cc +++ b/internal/ceres/autodiff_cost_function_test.cc @@ -57,7 +57,7 @@ TEST(AutodiffCostFunction, BilinearDifferentiationTest) { new AutoDiffCostFunction( new BinaryScalarCost(1.0)); - double** parameters = new double*[2]; + auto** parameters = new double*[2]; parameters[0] = new double[2]; parameters[1] = new double[2]; @@ -67,7 +67,7 @@ TEST(AutodiffCostFunction, BilinearDifferentiationTest) { parameters[1][0] = 3; parameters[1][1] = 4; - double** jacobians = new double*[2]; + auto** jacobians = new double*[2]; jacobians[0] = new double[2]; jacobians[1] = new double[2]; @@ -126,8 +126,8 @@ TEST(AutodiffCostFunction, ManyParameterAutodiffInstantiates) { 1, 1>(new TenParameterCost); - double** parameters = new double*[10]; - double** jacobians = new double*[10]; + auto** parameters = new double*[10]; + auto** jacobians = new double*[10]; for (int i = 0; i < 10; ++i) { parameters[i] = new double[1]; parameters[i][0] = i; diff --git a/internal/ceres/autodiff_local_parameterization_test.cc b/internal/ceres/autodiff_local_parameterization_test.cc index 36fd3c90f..9dc25510e 100644 --- a/internal/ceres/autodiff_local_parameterization_test.cc +++ b/internal/ceres/autodiff_local_parameterization_test.cc @@ -194,13 +194,13 @@ TEST(AutoDiffLocalParameterization, QuaternionParameterizationNearZeroTest) { x[2] * x[2] + x[3] * x[3]); // clang-format on - for (int i = 0; i < 4; ++i) { - x[i] = x[i] / norm_x; + for (double& x_i : x) { + x_i = x_i / norm_x; } double delta[3] = {0.24, 0.15, 0.10}; - for (int i = 0; i < 3; ++i) { - delta[i] = delta[i] * 1e-14; + for (double& delta_i : delta) { + delta_i = delta_i * 1e-14; } QuaternionParameterizationTestHelper(x, delta); @@ -215,8 +215,8 @@ TEST(AutoDiffLocalParameterization, QuaternionParameterizationNonZeroTest) { x[3] * x[3]); // clang-format on - for (int i = 0; i < 4; ++i) { - x[i] = x[i] / norm_x; + for (double& x_i : x) { + x_i = x_i / norm_x; } double delta[3] = {0.24, 0.15, 0.10}; diff --git a/internal/ceres/autodiff_test.cc b/internal/ceres/autodiff_test.cc index 2d564000e..69ce584c8 100644 --- a/internal/ceres/autodiff_test.cc +++ b/internal/ceres/autodiff_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -172,8 +172,8 @@ TEST(AutoDiff, ProjectiveCameraModel) { // Make random P and X, in a single vector. double PX[12 + 4]; - for (int i = 0; i < 12 + 4; ++i) { - PX[i] = RandDouble(); + for (double& PX_i : PX) { + PX_i = RandDouble(); } // Handy names for the P and X parts. @@ -292,7 +292,7 @@ TEST(AutoDiff, Metric) { // Make random parameter vector. double qcX[4 + 3 + 3]; - for (int i = 0; i < 4 + 3 + 3; ++i) qcX[i] = RandDouble(); + for (double& qcX_i : qcX) qcX_i = RandDouble(); // Handy names. double* q = qcX; @@ -658,7 +658,7 @@ TEST(AutoDiff, AlignedAllocationTest) { // this function. y += 1; - typedef Jet JetT; + using JetT = Jet; FixedArray x(3); // Need this to makes sure that x does not get optimized out. diff --git a/internal/ceres/block_jacobi_preconditioner.cc b/internal/ceres/block_jacobi_preconditioner.cc index da8fc9482..6e979dea9 100644 --- a/internal/ceres/block_jacobi_preconditioner.cc +++ b/internal/ceres/block_jacobi_preconditioner.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -60,16 +60,15 @@ bool BlockJacobiPreconditioner::UpdateImpl(const BlockSparseMatrix& A, for (int i = 0; i < bs->rows.size(); ++i) { const int row_block_size = bs->rows[i].block.size; const std::vector& cells = bs->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - const int block_id = cells[j].block_id; + for (const auto& cell : cells) { + const int block_id = cell.block_id; const int col_block_size = bs->cols[block_id].size; int r, c, row_stride, col_stride; CellInfo* cell_info = m_->GetCell(block_id, block_id, &r, &c, &row_stride, &col_stride); MatrixRef m(cell_info->values, row_stride, col_stride); - ConstMatrixRef b( - values + cells[j].position, row_block_size, col_block_size); + ConstMatrixRef b(values + cell.position, row_block_size, col_block_size); m.block(r, c, col_block_size, col_block_size) += b.transpose() * b; } } diff --git a/internal/ceres/block_jacobi_preconditioner_test.cc b/internal/ceres/block_jacobi_preconditioner_test.cc index 6d124f1aa..06b69a209 100644 --- a/internal/ceres/block_jacobi_preconditioner_test.cc +++ b/internal/ceres/block_jacobi_preconditioner_test.cc @@ -67,8 +67,7 @@ class BlockJacobiPreconditionerTest : public ::testing::Test { BlockJacobiPreconditioner pre(*A); pre.Update(*A, D.get()); - BlockRandomAccessDiagonalMatrix* m = - const_cast(&pre.matrix()); + auto* m = const_cast(&pre.matrix()); EXPECT_EQ(m->num_rows(), A->num_cols()); EXPECT_EQ(m->num_cols(), A->num_cols()); diff --git a/internal/ceres/block_jacobian_writer.cc b/internal/ceres/block_jacobian_writer.cc index e0f6ec01e..a70660f86 100644 --- a/internal/ceres/block_jacobian_writer.cc +++ b/internal/ceres/block_jacobian_writer.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -69,8 +69,7 @@ void BuildJacobianLayout(const Program& program, // matrix. Also compute the number of jacobian blocks. int f_block_pos = 0; int num_jacobian_blocks = 0; - for (int i = 0; i < residual_blocks.size(); ++i) { - ResidualBlock* residual_block = residual_blocks[i]; + for (auto* residual_block : residual_blocks) { const int num_residuals = residual_block->NumResiduals(); const int num_parameter_blocks = residual_block->NumParameterBlocks(); @@ -152,7 +151,7 @@ BlockJacobianWriter::CreateEvaluatePreparers(int num_threads) { } std::unique_ptr BlockJacobianWriter::CreateJacobian() const { - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; + auto* bs = new CompressedRowBlockStructure; const vector& parameter_blocks = program_->parameter_blocks(); diff --git a/internal/ceres/block_random_access_diagonal_matrix.cc b/internal/ceres/block_random_access_diagonal_matrix.cc index af372ad57..f55f3b30c 100644 --- a/internal/ceres/block_random_access_diagonal_matrix.cc +++ b/internal/ceres/block_random_access_diagonal_matrix.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -58,10 +58,10 @@ BlockRandomAccessDiagonalMatrix::BlockRandomAccessDiagonalMatrix( int num_cols = 0; int num_nonzeros = 0; vector block_positions; - for (int i = 0; i < blocks_.size(); ++i) { + for (int block_size : blocks_) { block_positions.push_back(num_cols); - num_cols += blocks_[i]; - num_nonzeros += blocks_[i] * blocks_[i]; + num_cols += block_size; + num_nonzeros += block_size * block_size; } VLOG(1) << "Matrix Size [" << num_cols << "," << num_cols << "] " @@ -123,8 +123,7 @@ void BlockRandomAccessDiagonalMatrix::SetZero() { void BlockRandomAccessDiagonalMatrix::Invert() { double* values = tsm_->mutable_values(); - for (int i = 0; i < blocks_.size(); ++i) { - const int block_size = blocks_[i]; + for (int block_size : blocks_) { MatrixRef block(values, block_size, block_size); block = block.selfadjointView().llt().solve( Matrix::Identity(block_size, block_size)); @@ -137,8 +136,7 @@ void BlockRandomAccessDiagonalMatrix::RightMultiply(const double* x, CHECK(x != nullptr); CHECK(y != nullptr); const double* values = tsm_->values(); - for (int i = 0; i < blocks_.size(); ++i) { - const int block_size = blocks_[i]; + for (int block_size : blocks_) { ConstMatrixRef block(values, block_size, block_size); VectorRef(y, block_size).noalias() += block * ConstVectorRef(x, block_size); x += block_size; diff --git a/internal/ceres/block_random_access_matrix.h b/internal/ceres/block_random_access_matrix.h index ec2c9e1cf..48759b79a 100644 --- a/internal/ceres/block_random_access_matrix.h +++ b/internal/ceres/block_random_access_matrix.h @@ -86,10 +86,10 @@ namespace internal { // Structure to carry a pointer to the array containing a cell and the // mutex guarding it. struct CERES_NO_EXPORT CellInfo { - CellInfo() : values(nullptr) {} + CellInfo() = default; explicit CellInfo(double* values) : values(values) {} - double* values; + double* values{nullptr}; std::mutex m; }; diff --git a/internal/ceres/block_random_access_sparse_matrix.cc b/internal/ceres/block_random_access_sparse_matrix.cc index 0bedf3c58..a026daa5d 100644 --- a/internal/ceres/block_random_access_sparse_matrix.cc +++ b/internal/ceres/block_random_access_sparse_matrix.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -58,9 +58,9 @@ BlockRandomAccessSparseMatrix::BlockRandomAccessSparseMatrix( // rows/columns. int num_cols = 0; block_positions_.reserve(blocks_.size()); - for (int i = 0; i < blocks_.size(); ++i) { + for (int block_size : blocks_) { block_positions_.push_back(num_cols); - num_cols += blocks_[i]; + num_cols += block_size; } // Count the number of scalar non-zero entries and build the layout @@ -87,7 +87,7 @@ BlockRandomAccessSparseMatrix::BlockRandomAccessSparseMatrix( for (const auto& block_pair : block_pairs) { const int row_block_size = blocks_[block_pair.first]; const int col_block_size = blocks_[block_pair.second]; - cell_values_.push_back(make_pair(block_pair, values + pos)); + cell_values_.emplace_back(block_pair, values + pos); layout_[IntPairToLong(block_pair.first, block_pair.second)] = new CellInfo(values + pos); pos += row_block_size * col_block_size; diff --git a/internal/ceres/block_random_access_sparse_matrix.h b/internal/ceres/block_random_access_sparse_matrix.h index 43886bd9a..b31a2ade8 100644 --- a/internal/ceres/block_random_access_sparse_matrix.h +++ b/internal/ceres/block_random_access_sparse_matrix.h @@ -112,7 +112,7 @@ class CERES_NO_EXPORT BlockRandomAccessSparseMatrix // A mapping from to the position in // the values array of tsm_ where the block is stored. - typedef std::unordered_map LayoutType; + using LayoutType = std::unordered_map; LayoutType layout_; // In order traversal of contents of the matrix. This allows us to diff --git a/internal/ceres/block_sparse_matrix.cc b/internal/ceres/block_sparse_matrix.cc index a0f55f88d..31ea39dae 100644 --- a/internal/ceres/block_sparse_matrix.cc +++ b/internal/ceres/block_sparse_matrix.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -56,8 +56,8 @@ BlockSparseMatrix::BlockSparseMatrix( CHECK(block_structure_ != nullptr); // Count the number of columns in the matrix. - for (int i = 0; i < block_structure_->cols.size(); ++i) { - num_cols_ += block_structure_->cols[i].size; + for (auto& col : block_structure_->cols) { + num_cols_ += col.size; } // Count the number of non-zero entries and the number of rows in @@ -67,8 +67,8 @@ BlockSparseMatrix::BlockSparseMatrix( num_rows_ += row_block_size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + int col_block_id = cell.block_id; int col_block_size = block_structure_->cols[col_block_id].size; num_nonzeros_ += col_block_size * row_block_size; } @@ -96,12 +96,12 @@ void BlockSparseMatrix::RightMultiply(const double* x, double* y) const { int row_block_pos = block_structure_->rows[i].block.position; int row_block_size = block_structure_->rows[i].block.size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + int col_block_id = cell.block_id; int col_block_size = block_structure_->cols[col_block_id].size; int col_block_pos = block_structure_->cols[col_block_id].position; MatrixVectorMultiply( - values_.get() + cells[j].position, + values_.get() + cell.position, row_block_size, col_block_size, x + col_block_pos, @@ -118,12 +118,12 @@ void BlockSparseMatrix::LeftMultiply(const double* x, double* y) const { int row_block_pos = block_structure_->rows[i].block.position; int row_block_size = block_structure_->rows[i].block.size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + int col_block_id = cell.block_id; int col_block_size = block_structure_->cols[col_block_id].size; int col_block_pos = block_structure_->cols[col_block_id].position; MatrixTransposeVectorMultiply( - values_.get() + cells[j].position, + values_.get() + cell.position, row_block_size, col_block_size, x + row_block_pos, @@ -138,12 +138,12 @@ void BlockSparseMatrix::SquaredColumnNorm(double* x) const { for (int i = 0; i < block_structure_->rows.size(); ++i) { int row_block_size = block_structure_->rows[i].block.size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + int col_block_id = cell.block_id; int col_block_size = block_structure_->cols[col_block_id].size; int col_block_pos = block_structure_->cols[col_block_id].position; const MatrixRef m( - values_.get() + cells[j].position, row_block_size, col_block_size); + values_.get() + cell.position, row_block_size, col_block_size); VectorRef(x + col_block_pos, col_block_size) += m.colwise().squaredNorm(); } } @@ -155,12 +155,12 @@ void BlockSparseMatrix::ScaleColumns(const double* scale) { for (int i = 0; i < block_structure_->rows.size(); ++i) { int row_block_size = block_structure_->rows[i].block.size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + int col_block_id = cell.block_id; int col_block_size = block_structure_->cols[col_block_id].size; int col_block_pos = block_structure_->cols[col_block_id].position; MatrixRef m( - values_.get() + cells[j].position, row_block_size, col_block_size); + values_.get() + cell.position, row_block_size, col_block_size); m *= ConstVectorRef(scale + col_block_pos, col_block_size).asDiagonal(); } } @@ -177,11 +177,11 @@ void BlockSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const { int row_block_pos = block_structure_->rows[i].block.position; int row_block_size = block_structure_->rows[i].block.size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + int col_block_id = cell.block_id; int col_block_size = block_structure_->cols[col_block_id].size; int col_block_pos = block_structure_->cols[col_block_id].position; - int jac_pos = cells[j].position; + int jac_pos = cell.position; m.block(row_block_pos, col_block_pos, row_block_size, col_block_size) += MatrixRef(values_.get() + jac_pos, row_block_size, col_block_size); } @@ -200,11 +200,11 @@ void BlockSparseMatrix::ToTripletSparseMatrix( int row_block_pos = block_structure_->rows[i].block.position; int row_block_size = block_structure_->rows[i].block.size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + int col_block_id = cell.block_id; int col_block_size = block_structure_->cols[col_block_id].size; int col_block_pos = block_structure_->cols[col_block_id].position; - int jac_pos = cells[j].position; + int jac_pos = cell.position; for (int r = 0; r < row_block_size; ++r) { for (int c = 0; c < col_block_size; ++c, ++jac_pos) { matrix->mutable_rows()[jac_pos] = row_block_pos + r; @@ -229,11 +229,11 @@ void BlockSparseMatrix::ToTextFile(FILE* file) const { const int row_block_pos = block_structure_->rows[i].block.position; const int row_block_size = block_structure_->rows[i].block.size; const vector& cells = block_structure_->rows[i].cells; - for (int j = 0; j < cells.size(); ++j) { - const int col_block_id = cells[j].block_id; + for (const auto& cell : cells) { + const int col_block_id = cell.block_id; const int col_block_size = block_structure_->cols[col_block_id].size; const int col_block_pos = block_structure_->cols[col_block_id].position; - int jac_pos = cells[j].position; + int jac_pos = cell.position; for (int r = 0; r < row_block_size; ++r) { for (int c = 0; c < col_block_size; ++c) { fprintf(file, @@ -250,7 +250,7 @@ void BlockSparseMatrix::ToTextFile(FILE* file) const { std::unique_ptr BlockSparseMatrix::CreateDiagonalMatrix( const double* diagonal, const std::vector& column_blocks) { // Create the block structure for the diagonal matrix. - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure(); + auto* bs = new CompressedRowBlockStructure(); bs->cols = column_blocks; int position = 0; bs->rows.resize(column_blocks.size(), CompressedRow(1)); @@ -269,8 +269,8 @@ std::unique_ptr BlockSparseMatrix::CreateDiagonalMatrix( // Fill the values array of the block sparse matrix. double* values = matrix->mutable_values(); - for (int i = 0; i < column_blocks.size(); ++i) { - const int size = column_blocks[i].size; + for (const auto& column_block : column_blocks) { + const int size = column_block.size; for (int j = 0; j < size; ++j) { // (j + 1) * size is compact way of accessing the (j,j) entry. values[j * (size + 1)] = diagonal[j]; @@ -346,7 +346,7 @@ std::unique_ptr BlockSparseMatrix::CreateRandomMatrix( CHECK_GT(options.block_density, 0.0); CHECK_LE(options.block_density, 1.0); - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure(); + auto* bs = new CompressedRowBlockStructure(); if (options.col_blocks.empty()) { CHECK_GT(options.num_col_blocks, 0); CHECK_GT(options.min_col_block_size, 0); @@ -360,7 +360,7 @@ std::unique_ptr BlockSparseMatrix::CreateRandomMatrix( const int delta_block_size = Uniform(options.max_col_block_size - options.min_col_block_size); const int col_block_size = options.min_col_block_size + delta_block_size; - bs->cols.push_back(Block(col_block_size, col_block_position)); + bs->cols.emplace_back(col_block_size, col_block_position); col_block_position += col_block_size; } } else { @@ -377,7 +377,7 @@ std::unique_ptr BlockSparseMatrix::CreateRandomMatrix( const int delta_block_size = Uniform(options.max_row_block_size - options.min_row_block_size); const int row_block_size = options.min_row_block_size + delta_block_size; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = row_block_size; row.block.position = row_block_position; @@ -385,7 +385,7 @@ std::unique_ptr BlockSparseMatrix::CreateRandomMatrix( for (int c = 0; c < bs->cols.size(); ++c) { if (RandDouble() > options.block_density) continue; - row.cells.push_back(Cell()); + row.cells.emplace_back(); Cell& cell = row.cells.back(); cell.block_id = c; cell.position = value_position; diff --git a/internal/ceres/block_sparse_matrix.h b/internal/ceres/block_sparse_matrix.h index 8e555b055..75b0deb59 100644 --- a/internal/ceres/block_sparse_matrix.h +++ b/internal/ceres/block_sparse_matrix.h @@ -141,7 +141,7 @@ class CERES_NO_EXPORT BlockSparseMatrix final : public SparseMatrix { // information class CERES_NO_EXPORT BlockSparseMatrixData { public: - BlockSparseMatrixData(const BlockSparseMatrix& m) + explicit BlockSparseMatrixData(const BlockSparseMatrix& m) : block_structure_(m.block_structure()), values_(m.values()){}; BlockSparseMatrixData(const CompressedRowBlockStructure* block_structure, diff --git a/internal/ceres/block_sparse_matrix_test.cc b/internal/ceres/block_sparse_matrix_test.cc index 747cdeff5..d8cf98775 100644 --- a/internal/ceres/block_sparse_matrix_test.cc +++ b/internal/ceres/block_sparse_matrix_test.cc @@ -187,9 +187,9 @@ TEST_F(BlockSparseMatrixTest, AppendAndDeleteBlockDiagonalMatrix) { TEST(BlockSparseMatrix, CreateDiagonalMatrix) { std::vector column_blocks; - column_blocks.push_back(Block(2, 0)); - column_blocks.push_back(Block(1, 2)); - column_blocks.push_back(Block(3, 3)); + column_blocks.emplace_back(2, 0); + column_blocks.emplace_back(1, 2); + column_blocks.emplace_back(3, 3); const int num_cols = column_blocks.back().size + column_blocks.back().position; Vector diagonal(num_cols); diff --git a/internal/ceres/block_structure.h b/internal/ceres/block_structure.h index 203966457..fe7574c68 100644 --- a/internal/ceres/block_structure.h +++ b/internal/ceres/block_structure.h @@ -46,25 +46,25 @@ namespace ceres { namespace internal { -typedef int32_t BlockSize; +using BlockSize = int32_t; struct CERES_NO_EXPORT Block { - Block() : size(-1), position(-1) {} + Block() = default; Block(int size_, int position_) : size(size_), position(position_) {} - BlockSize size; - int position; // Position along the row/column. + BlockSize size{-1}; + int position{-1}; // Position along the row/column. }; struct CERES_NO_EXPORT Cell { - Cell() : block_id(-1), position(-1) {} + Cell() = default; Cell(int block_id_, int position_) : block_id(block_id_), position(position_) {} // Column or row block id as the case maybe. - int block_id; + int block_id{-1}; // Where in the values array of the jacobian is this cell located. - int position; + int position{-1}; }; // Order cell by their block_id; @@ -75,13 +75,13 @@ struct CERES_NO_EXPORT CompressedList { // Construct a CompressedList with the cells containing num_cells // entries. - CompressedList(int num_cells) : cells(num_cells) {} + explicit CompressedList(int num_cells) : cells(num_cells) {} Block block; std::vector cells; }; -typedef CompressedList CompressedRow; -typedef CompressedList CompressedColumn; +using CompressedRow = CompressedList; +using CompressedColumn = CompressedList; struct CERES_NO_EXPORT CompressedRowBlockStructure { std::vector cols; diff --git a/internal/ceres/bundle_adjustment_test_util.h b/internal/ceres/bundle_adjustment_test_util.h index fe874055d..44281b608 100644 --- a/internal/ceres/bundle_adjustment_test_util.h +++ b/internal/ceres/bundle_adjustment_test_util.h @@ -242,7 +242,7 @@ class BundleAdjustmentProblem { }; double BundleAdjustmentProblem::kResidualTolerance = 1e-4; -typedef SystemTest BundleAdjustmentTest; +using BundleAdjustmentTest = SystemTest; } // namespace internal } // namespace ceres diff --git a/internal/ceres/c_api.cc b/internal/ceres/c_api.cc index 784504fc1..8ea344dd5 100644 --- a/internal/ceres/c_api.cc +++ b/internal/ceres/c_api.cc @@ -145,7 +145,7 @@ ceres_residual_block_id_t* ceres_problem_add_residual_block( int num_parameter_blocks, int* parameter_block_sizes, double** parameters) { - Problem* ceres_problem = reinterpret_cast(problem); + auto* ceres_problem = reinterpret_cast(problem); auto callback_cost_function = std::make_unique(cost_function, @@ -169,7 +169,7 @@ ceres_residual_block_id_t* ceres_problem_add_residual_block( } void ceres_solve(ceres_problem_t* c_problem) { - Problem* problem = reinterpret_cast(c_problem); + auto* problem = reinterpret_cast(c_problem); // TODO(keir): Obviously, this way of setting options won't scale or last. // Instead, figure out a way to specify some of the options without diff --git a/internal/ceres/c_api_test.cc b/internal/ceres/c_api_test.cc index 247311635..112674351 100644 --- a/internal/ceres/c_api_test.cc +++ b/internal/ceres/c_api_test.cc @@ -114,7 +114,7 @@ static int exponential_residual(void* user_data, double** parameters, double* residuals, double** jacobians) { - double* measurement = (double*)user_data; + auto* measurement = static_cast(user_data); double x = measurement[0]; double y = measurement[1]; double m = parameters[0][0]; diff --git a/internal/ceres/canonical_views_clustering.cc b/internal/ceres/canonical_views_clustering.cc index 68998fa22..c7565c139 100644 --- a/internal/ceres/canonical_views_clustering.cc +++ b/internal/ceres/canonical_views_clustering.cc @@ -44,8 +44,8 @@ namespace internal { using std::vector; -typedef std::unordered_map IntMap; -typedef std::unordered_set IntSet; +using IntMap = std::unordered_map; +using IntSet = std::unordered_set; class CERES_NO_EXPORT CanonicalViewsClustering { public: @@ -174,9 +174,9 @@ double CanonicalViewsClustering::ComputeClusteringQualityDifference( difference -= options_.size_penalty_weight; // Orthogonality. - for (int i = 0; i < centers.size(); ++i) { + for (int center : centers) { difference -= options_.similarity_penalty_weight * - graph_->EdgeWeight(centers[i], candidate); + graph_->EdgeWeight(center, candidate); } return difference; diff --git a/internal/ceres/casts.h b/internal/ceres/casts.h index 21445c8f5..04d8ba4fe 100644 --- a/internal/ceres/casts.h +++ b/internal/ceres/casts.h @@ -38,7 +38,7 @@ namespace ceres { // Identity metafunction. template struct identity_ { - typedef T type; + using type = T; }; // Use implicit_cast as a safe version of static_cast or const_cast diff --git a/internal/ceres/cgnr_solver.cc b/internal/ceres/cgnr_solver.cc index e89305f89..cca72bca9 100644 --- a/internal/ceres/cgnr_solver.cc +++ b/internal/ceres/cgnr_solver.cc @@ -31,6 +31,7 @@ #include "ceres/cgnr_solver.h" #include +#include #include "ceres/block_jacobi_preconditioner.h" #include "ceres/cgnr_linear_operator.h" @@ -44,8 +45,8 @@ namespace ceres { namespace internal { -CgnrSolver::CgnrSolver(const LinearSolver::Options& options) - : options_(options) { +CgnrSolver::CgnrSolver(LinearSolver::Options options) + : options_(std::move(options)) { if (options_.preconditioner_type != JACOBI && options_.preconditioner_type != IDENTITY && options_.preconditioner_type != SUBSET) { diff --git a/internal/ceres/cgnr_solver.h b/internal/ceres/cgnr_solver.h index 06a611808..25e62e9ab 100644 --- a/internal/ceres/cgnr_solver.h +++ b/internal/ceres/cgnr_solver.h @@ -52,7 +52,7 @@ class BlockJacobiPreconditioner; // block diagonal preconditioning is supported. class CERES_NO_EXPORT CgnrSolver final : public BlockSparseMatrixSolver { public: - explicit CgnrSolver(const LinearSolver::Options& options); + explicit CgnrSolver(LinearSolver::Options options); CgnrSolver(const CgnrSolver&) = delete; void operator=(const CgnrSolver&) = delete; ~CgnrSolver() override; diff --git a/internal/ceres/compressed_row_jacobian_writer.cc b/internal/ceres/compressed_row_jacobian_writer.cc index c94d719cc..55b30a290 100644 --- a/internal/ceres/compressed_row_jacobian_writer.cc +++ b/internal/ceres/compressed_row_jacobian_writer.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -97,8 +97,7 @@ std::unique_ptr CompressedRowJacobianWriter::CreateJacobian() // Count the number of jacobian nonzeros. int num_jacobian_nonzeros = 0; - for (int i = 0; i < residual_blocks.size(); ++i) { - ResidualBlock* residual_block = residual_blocks[i]; + for (auto* residual_block : residual_blocks) { const int num_residuals = residual_block->NumResiduals(); const int num_parameter_blocks = residual_block->NumParameterBlocks(); for (int j = 0; j < num_parameter_blocks; ++j) { @@ -126,8 +125,7 @@ std::unique_ptr CompressedRowJacobianWriter::CreateJacobian() int row_pos = 0; rows[0] = 0; - for (int i = 0; i < residual_blocks.size(); ++i) { - const ResidualBlock* residual_block = residual_blocks[i]; + for (auto* residual_block : residual_blocks) { const int num_parameter_blocks = residual_block->NumParameterBlocks(); // Count the number of derivatives for a row of this residual block and @@ -169,9 +167,9 @@ std::unique_ptr CompressedRowJacobianWriter::CreateJacobian() // parameter vector. This code mirrors that in Write(), where jacobian // values are updated. int col_pos = 0; - for (int j = 0; j < parameter_indices.size(); ++j) { + for (int parameter_index : parameter_indices) { ParameterBlock* parameter_block = - program_->parameter_blocks()[parameter_indices[j]]; + program_->parameter_blocks()[parameter_index]; const int parameter_block_size = parameter_block->TangentSize(); for (int r = 0; r < num_residuals; ++r) { @@ -198,8 +196,7 @@ void CompressedRowJacobianWriter::Write(int residual_id, int residual_offset, double** jacobians, SparseMatrix* base_jacobian) { - CompressedRowSparseMatrix* jacobian = - down_cast(base_jacobian); + auto* jacobian = down_cast(base_jacobian); double* jacobian_values = jacobian->mutable_values(); const int* jacobian_rows = jacobian->rows(); @@ -216,10 +213,10 @@ void CompressedRowJacobianWriter::Write(int residual_id, // Iterate over the jacobian blocks in increasing order of their // positions in the reduced parameter vector. - for (int i = 0; i < evaluated_jacobian_blocks.size(); ++i) { + for (auto& evaluated_jacobian_block : evaluated_jacobian_blocks) { const ParameterBlock* parameter_block = - program_->parameter_blocks()[evaluated_jacobian_blocks[i].first]; - const int argument = evaluated_jacobian_blocks[i].second; + program_->parameter_blocks()[evaluated_jacobian_block.first]; + const int argument = evaluated_jacobian_block.second; const int parameter_block_size = parameter_block->TangentSize(); // Copy one row of the jacobian block at a time. diff --git a/internal/ceres/compressed_row_sparse_matrix.cc b/internal/ceres/compressed_row_sparse_matrix.cc index d8743cbd4..db103d9c0 100644 --- a/internal/ceres/compressed_row_sparse_matrix.cc +++ b/internal/ceres/compressed_row_sparse_matrix.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2017 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -542,9 +542,9 @@ CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( const double* diagonal, const vector& blocks) { int num_rows = 0; int num_nonzeros = 0; - for (int i = 0; i < blocks.size(); ++i) { - num_rows += blocks[i]; - num_nonzeros += blocks[i] * blocks[i]; + for (int block_size : blocks) { + num_rows += block_size; + num_nonzeros += block_size * block_size; } std::unique_ptr matrix = @@ -558,8 +558,7 @@ CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( int idx_cursor = 0; int col_cursor = 0; - for (int i = 0; i < blocks.size(); ++i) { - const int block_size = blocks[i]; + for (int block_size : blocks) { for (int r = 0; r < block_size; ++r) { *(rows++) = idx_cursor; values[idx_cursor + r] = diagonal[col_cursor + r]; diff --git a/internal/ceres/compressed_row_sparse_matrix_test.cc b/internal/ceres/compressed_row_sparse_matrix_test.cc index 3a2768cdc..e898a5aa0 100644 --- a/internal/ceres/compressed_row_sparse_matrix_test.cc +++ b/internal/ceres/compressed_row_sparse_matrix_test.cc @@ -381,7 +381,7 @@ TEST(CompressedRowSparseMatrix, FromTripletSparseMatrixTransposed) { } } -typedef ::testing::tuple Param; +using Param = ::testing::tuple; static std::string ParamInfoToString(testing::TestParamInfo info) { if (::testing::get<0>(info.param) == diff --git a/internal/ceres/concurrent_queue.h b/internal/ceres/concurrent_queue.h index a04d147c5..1e7415356 100644 --- a/internal/ceres/concurrent_queue.h +++ b/internal/ceres/concurrent_queue.h @@ -78,7 +78,7 @@ template class ConcurrentQueue { public: // Defaults the queue to blocking on Wait calls. - ConcurrentQueue() : wait_(true) {} + ConcurrentQueue() = default; // Atomically push an element onto the queue. If a thread was waiting for an // element, wake it up. @@ -149,7 +149,7 @@ class ConcurrentQueue { std::queue queue_; // If true, signals that callers of Wait will block waiting to pop an // element off the queue. - bool wait_; + bool wait_{true}; }; } // namespace internal diff --git a/internal/ceres/conditioned_cost_function_test.cc b/internal/ceres/conditioned_cost_function_test.cc index f21f84c2d..d0d5d2d98 100644 --- a/internal/ceres/conditioned_cost_function_test.cc +++ b/internal/ceres/conditioned_cost_function_test.cc @@ -85,7 +85,7 @@ TEST(ConditionedCostFunction, NormalOperation) { VectorRef v2_vector(v2, kTestCostFunctionSize, 1); Matrix identity(kTestCostFunctionSize, kTestCostFunctionSize); identity.setIdentity(); - NormalPrior* difference_cost_function = new NormalPrior(identity, v2_vector); + auto* difference_cost_function = new NormalPrior(identity, v2_vector); std::vector conditioners; for (int i = 0; i < kTestCostFunctionSize; i++) { @@ -127,7 +127,7 @@ TEST(ConditionedCostFunction, SharedConditionersDoNotTriggerDoubleFree) { VectorRef v2_vector(v2, kTestCostFunctionSize, 1); Matrix identity = Matrix::Identity(kTestCostFunctionSize, kTestCostFunctionSize); - NormalPrior* difference_cost_function = new NormalPrior(identity, v2_vector); + auto* difference_cost_function = new NormalPrior(identity, v2_vector); CostFunction* conditioner = new LinearCostFunction(2, 7); std::vector conditioners; for (int i = 0; i < kTestCostFunctionSize; i++) { diff --git a/internal/ceres/conjugate_gradients_solver.cc b/internal/ceres/conjugate_gradients_solver.cc index 436a5b1ce..62ae9201c 100644 --- a/internal/ceres/conjugate_gradients_solver.cc +++ b/internal/ceres/conjugate_gradients_solver.cc @@ -41,6 +41,7 @@ #include #include +#include #include "ceres/internal/eigen.h" #include "ceres/linear_operator.h" @@ -57,8 +58,8 @@ bool IsZeroOrInfinity(double x) { return ((x == 0.0) || std::isinf(x)); } } // namespace ConjugateGradientsSolver::ConjugateGradientsSolver( - const LinearSolver::Options& options) - : options_(options) {} + LinearSolver::Options options) + : options_(std::move(options)) {} LinearSolver::Summary ConjugateGradientsSolver::Solve( LinearOperator* A, diff --git a/internal/ceres/conjugate_gradients_solver.h b/internal/ceres/conjugate_gradients_solver.h index 418508a0f..99ddb5d48 100644 --- a/internal/ceres/conjugate_gradients_solver.h +++ b/internal/ceres/conjugate_gradients_solver.h @@ -58,7 +58,7 @@ class LinearOperator; // LinearSolver::PerSolveOptions::q_tolerance in linear_solver.h. class CERES_NO_EXPORT ConjugateGradientsSolver final : public LinearSolver { public: - explicit ConjugateGradientsSolver(const LinearSolver::Options& options); + explicit ConjugateGradientsSolver(LinearSolver::Options options); Summary Solve(LinearOperator* A, const double* b, const LinearSolver::PerSolveOptions& per_solve_options, diff --git a/internal/ceres/coordinate_descent_minimizer.cc b/internal/ceres/coordinate_descent_minimizer.cc index 86ab83885..a6e149d1c 100644 --- a/internal/ceres/coordinate_descent_minimizer.cc +++ b/internal/ceres/coordinate_descent_minimizer.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -94,9 +94,9 @@ bool CoordinateDescentMinimizer::Init( // assign zero offsets/empty independent sets to these parameter // blocks. const vector& parameter_blocks = program.parameter_blocks(); - for (int i = 0; i < parameter_blocks.size(); ++i) { - if (!ordering.IsMember(parameter_blocks[i]->mutable_user_state())) { - parameter_blocks_.push_back(parameter_blocks[i]); + for (auto* parameter_block : parameter_blocks) { + if (!ordering.IsMember(parameter_block->mutable_user_state())) { + parameter_blocks_.push_back(parameter_block); independent_set_offsets_.push_back(independent_set_offsets_.back()); } } @@ -105,8 +105,7 @@ bool CoordinateDescentMinimizer::Init( // block. residual_blocks_.resize(parameter_block_index.size()); const vector& residual_blocks = program.residual_blocks(); - for (int i = 0; i < residual_blocks.size(); ++i) { - ResidualBlock* residual_block = residual_blocks[i]; + for (auto* residual_block : residual_blocks) { const int num_parameter_blocks = residual_block->NumParameterBlocks(); for (int j = 0; j < num_parameter_blocks; ++j) { ParameterBlock* parameter_block = residual_block->parameter_blocks()[j]; @@ -129,8 +128,7 @@ void CoordinateDescentMinimizer::Minimize(const Minimizer::Options& options, double* parameters, Solver::Summary* summary) { // Set the state and mark all parameter blocks constant. - for (int i = 0; i < parameter_blocks_.size(); ++i) { - ParameterBlock* parameter_block = parameter_blocks_[i]; + for (auto* parameter_block : parameter_blocks_) { parameter_block->SetState(parameters + parameter_block->state_offset()); parameter_block->SetConstant(); } @@ -202,8 +200,8 @@ void CoordinateDescentMinimizer::Minimize(const Minimizer::Options& options, }); } - for (int i = 0; i < parameter_blocks_.size(); ++i) { - parameter_blocks_[i]->SetVarying(); + for (auto* parameter_block : parameter_blocks_) { + parameter_block->SetVarying(); } // for (int i = 0; i < options.num_threads; ++i) { diff --git a/internal/ceres/corrector_test.cc b/internal/ceres/corrector_test.cc index 951041e38..d2f7c84e8 100644 --- a/internal/ceres/corrector_test.cc +++ b/internal/ceres/corrector_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -164,8 +164,8 @@ TEST(Corrector, MultidimensionalGaussNewtonApproximation) { srand(5); for (int iter = 0; iter < 10000; ++iter) { // Initialize the jacobian and residual. - for (int i = 0; i < 2 * 3; ++i) jacobian[i] = RandDouble(); - for (int i = 0; i < 3; ++i) residuals[i] = RandDouble(); + for (double& jacobian_entry : jacobian) jacobian_entry = RandDouble(); + for (double& residual : residuals) residual = RandDouble(); const double sq_norm = res.dot(res); @@ -230,7 +230,7 @@ TEST(Corrector, MultidimensionalGaussNewtonApproximationZeroResidual) { srand(5); for (int iter = 0; iter < 10000; ++iter) { // Initialize the jacobian. - for (int i = 0; i < 2 * 3; ++i) jacobian[i] = RandDouble(); + for (double& jacobian_entry : jacobian) jacobian_entry = RandDouble(); // Zero residuals res.setZero(); diff --git a/internal/ceres/cost_function_to_functor_test.cc b/internal/ceres/cost_function_to_functor_test.cc index 80efd6699..dfa1a92be 100644 --- a/internal/ceres/cost_function_to_functor_test.cc +++ b/internal/ceres/cost_function_to_functor_test.cc @@ -376,10 +376,9 @@ TEST(CostFunctionToFunctor, DynamicNumberOfResiduals) { } TEST(CostFunctionToFunctor, DynamicCostFunctionToFunctor) { - DynamicAutoDiffCostFunction* - actual_cost_function( - new DynamicAutoDiffCostFunction( - new DynamicTwoParameterBlockFunctor)); + auto* actual_cost_function( + new DynamicAutoDiffCostFunction( + new DynamicTwoParameterBlockFunctor)); actual_cost_function->AddParameterBlock(2); actual_cost_function->AddParameterBlock(2); actual_cost_function->SetNumResiduals(2); diff --git a/internal/ceres/covariance_impl.cc b/internal/ceres/covariance_impl.cc index 507560922..324b5531a 100644 --- a/internal/ceres/covariance_impl.cc +++ b/internal/ceres/covariance_impl.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -84,8 +84,7 @@ CovarianceImpl::~CovarianceImpl() = default; template void CheckForDuplicates(std::vector blocks) { std::sort(blocks.begin(), blocks.end()); - typename std::vector::iterator it = - std::adjacent_find(blocks.begin(), blocks.end()); + auto it = std::adjacent_find(blocks.begin(), blocks.end()); if (it != blocks.end()) { // In case there are duplicates, we search for their location. std::map> blocks_map; @@ -384,8 +383,7 @@ bool CovarianceImpl::ComputeCovarianceSparsity( std::vector residual_blocks; problem->GetResidualBlocks(&residual_blocks); - for (int i = 0; i < residual_blocks.size(); ++i) { - ResidualBlock* residual_block = residual_blocks[i]; + for (auto* residual_block : residual_blocks) { parameter_blocks_in_use.insert(residual_block->parameter_blocks(), residual_block->parameter_blocks() + residual_block->NumParameterBlocks()); @@ -395,8 +393,7 @@ bool CovarianceImpl::ComputeCovarianceSparsity( std::vector& active_parameter_blocks = evaluate_options_.parameter_blocks; active_parameter_blocks.clear(); - for (int i = 0; i < all_parameter_blocks.size(); ++i) { - double* parameter_block = all_parameter_blocks[i]; + for (auto* parameter_block : all_parameter_blocks) { ParameterBlock* block = FindOrDie(parameter_map, parameter_block); if (!block->IsConstant() && (parameter_blocks_in_use.count(block) > 0)) { active_parameter_blocks.push_back(parameter_block); @@ -412,8 +409,7 @@ bool CovarianceImpl::ComputeCovarianceSparsity( // ordering of parameter blocks just constructed. int num_rows = 0; parameter_block_to_row_index_.clear(); - for (int i = 0; i < active_parameter_blocks.size(); ++i) { - double* parameter_block = active_parameter_blocks[i]; + for (auto* parameter_block : active_parameter_blocks) { const int parameter_block_size = problem->ParameterBlockTangentSize(parameter_block); parameter_block_to_row_index_[parameter_block] = num_rows; @@ -425,9 +421,7 @@ bool CovarianceImpl::ComputeCovarianceSparsity( // triangular part of the matrix. int num_nonzeros = 0; CovarianceBlocks covariance_blocks; - for (int i = 0; i < original_covariance_blocks.size(); ++i) { - const std::pair& block_pair = - original_covariance_blocks[i]; + for (const auto& block_pair : original_covariance_blocks) { if (constant_parameter_blocks_.count(block_pair.first) > 0 || constant_parameter_blocks_.count(block_pair.second) > 0) { continue; @@ -823,7 +817,7 @@ bool CovarianceImpl::ComputeCovarianceValuesUsingEigenSparseQR() { problem_->Evaluate(evaluate_options_, nullptr, nullptr, nullptr, &jacobian); event_logger.AddEvent("Evaluate"); - typedef Eigen::SparseMatrix EigenSparseMatrix; + using EigenSparseMatrix = Eigen::SparseMatrix; // Convert the matrix to column major order as required by SparseQR. EigenSparseMatrix sparse_jacobian = diff --git a/internal/ceres/covariance_test.cc b/internal/ceres/covariance_test.cc index 20b480fb6..7854be394 100644 --- a/internal/ceres/covariance_test.cc +++ b/internal/ceres/covariance_test.cc @@ -185,12 +185,12 @@ TEST(CovarianceImpl, ComputeCovarianceSparsity) { // clang-format on vector> covariance_blocks; - covariance_blocks.push_back(make_pair(block1, block1)); - covariance_blocks.push_back(make_pair(block4, block4)); - covariance_blocks.push_back(make_pair(block2, block2)); - covariance_blocks.push_back(make_pair(block3, block3)); - covariance_blocks.push_back(make_pair(block2, block3)); - covariance_blocks.push_back(make_pair(block4, block1)); // reversed + covariance_blocks.emplace_back(block1, block1); + covariance_blocks.emplace_back(block4, block4); + covariance_blocks.emplace_back(block2, block2); + covariance_blocks.emplace_back(block3, block3); + covariance_blocks.emplace_back(block2, block3); + covariance_blocks.emplace_back(block4, block1); // reversed Covariance::Options options; CovarianceImpl covariance_impl(options); @@ -266,12 +266,12 @@ TEST(CovarianceImpl, ComputeCovarianceSparsityWithConstantParameterBlock) { // clang-format on vector> covariance_blocks; - covariance_blocks.push_back(make_pair(block1, block1)); - covariance_blocks.push_back(make_pair(block4, block4)); - covariance_blocks.push_back(make_pair(block2, block2)); - covariance_blocks.push_back(make_pair(block3, block3)); - covariance_blocks.push_back(make_pair(block2, block3)); - covariance_blocks.push_back(make_pair(block4, block1)); // reversed + covariance_blocks.emplace_back(block1, block1); + covariance_blocks.emplace_back(block4, block4); + covariance_blocks.emplace_back(block2, block2); + covariance_blocks.emplace_back(block3, block3); + covariance_blocks.emplace_back(block2, block3); + covariance_blocks.emplace_back(block4, block1); // reversed Covariance::Options options; CovarianceImpl covariance_impl(options); @@ -345,12 +345,12 @@ TEST(CovarianceImpl, ComputeCovarianceSparsityWithFreeParameterBlock) { // clang-format on vector> covariance_blocks; - covariance_blocks.push_back(make_pair(block1, block1)); - covariance_blocks.push_back(make_pair(block4, block4)); - covariance_blocks.push_back(make_pair(block2, block2)); - covariance_blocks.push_back(make_pair(block3, block3)); - covariance_blocks.push_back(make_pair(block2, block3)); - covariance_blocks.push_back(make_pair(block4, block1)); // reversed + covariance_blocks.emplace_back(block1, block1); + covariance_blocks.emplace_back(block4, block4); + covariance_blocks.emplace_back(block2, block2); + covariance_blocks.emplace_back(block3, block3); + covariance_blocks.emplace_back(block2, block3); + covariance_blocks.emplace_back(block4, block1); // reversed Covariance::Options options; CovarianceImpl covariance_impl(options); @@ -432,7 +432,7 @@ class PolynomialManifold : public Manifold { class CovarianceTest : public ::testing::Test { protected: - typedef map> BoundsMap; + using BoundsMap = map>; void SetUp() override { double* x = parameters_; @@ -478,12 +478,12 @@ class CovarianceTest : public ::testing::Test { new BinaryCostFunction(1, 1, 2, jacobian1, jacobian2), nullptr, z, x); } - all_covariance_blocks_.push_back(make_pair(x, x)); - all_covariance_blocks_.push_back(make_pair(y, y)); - all_covariance_blocks_.push_back(make_pair(z, z)); - all_covariance_blocks_.push_back(make_pair(x, y)); - all_covariance_blocks_.push_back(make_pair(x, z)); - all_covariance_blocks_.push_back(make_pair(y, z)); + all_covariance_blocks_.emplace_back(x, x); + all_covariance_blocks_.emplace_back(y, y); + all_covariance_blocks_.emplace_back(z, z); + all_covariance_blocks_.emplace_back(x, y); + all_covariance_blocks_.emplace_back(x, z); + all_covariance_blocks_.emplace_back(y, z); column_bounds_[x] = make_pair(0, 2); column_bounds_[y] = make_pair(2, 5); @@ -543,9 +543,9 @@ class CovarianceTest : public ::testing::Test { Covariance covariance(options); EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem_)); - for (int i = 0; i < covariance_blocks.size(); ++i) { - const double* block1 = covariance_blocks[i].first; - const double* block2 = covariance_blocks[i].second; + for (auto& covariance_block : covariance_blocks) { + const double* block1 = covariance_block.first; + const double* block2 = covariance_block.second; // block1, block2 GetCovarianceBlockAndCompare(block1, block2, @@ -1350,10 +1350,10 @@ TEST_F(CovarianceTest, ComputeCovarianceFailure) { "Covariance::Compute called with duplicate blocks " "at indices \\(0, 1\\) and \\(2, 3\\)"); vector> covariance_blocks; - covariance_blocks.push_back(make_pair(x, x)); - covariance_blocks.push_back(make_pair(x, x)); - covariance_blocks.push_back(make_pair(y, y)); - covariance_blocks.push_back(make_pair(y, y)); + covariance_blocks.emplace_back(x, x); + covariance_blocks.emplace_back(x, x); + covariance_blocks.emplace_back(y, y); + covariance_blocks.emplace_back(y, y); EXPECT_DEATH_IF_SUPPORTED(covariance.Compute(covariance_blocks, &problem_), "Covariance::Compute called with duplicate blocks " "at indices \\(0, 1\\) and \\(2, 3\\)"); @@ -1398,12 +1398,12 @@ class RankDeficientCovarianceTest : public CovarianceTest { new BinaryCostFunction(1, 1, 2, jacobian1, jacobian2), nullptr, z, x); } - all_covariance_blocks_.push_back(make_pair(x, x)); - all_covariance_blocks_.push_back(make_pair(y, y)); - all_covariance_blocks_.push_back(make_pair(z, z)); - all_covariance_blocks_.push_back(make_pair(x, y)); - all_covariance_blocks_.push_back(make_pair(x, z)); - all_covariance_blocks_.push_back(make_pair(y, z)); + all_covariance_blocks_.emplace_back(x, x); + all_covariance_blocks_.emplace_back(y, y); + all_covariance_blocks_.emplace_back(z, z); + all_covariance_blocks_.emplace_back(x, y); + all_covariance_blocks_.emplace_back(x, z); + all_covariance_blocks_.emplace_back(y, z); column_bounds_[x] = make_pair(0, 2); column_bounds_[y] = make_pair(2, 5); @@ -1475,10 +1475,10 @@ TEST(Covariance, ZeroSizedLocalParameterizationGetCovariance) { options.algorithm_type = DENSE_SVD; Covariance covariance(options); vector> covariance_blocks; - covariance_blocks.push_back(std::make_pair(&x, &x)); - covariance_blocks.push_back(std::make_pair(&x, &y)); - covariance_blocks.push_back(std::make_pair(&y, &x)); - covariance_blocks.push_back(std::make_pair(&y, &y)); + covariance_blocks.emplace_back(&x, &x); + covariance_blocks.emplace_back(&x, &y); + covariance_blocks.emplace_back(&y, &x); + covariance_blocks.emplace_back(&y, &y); EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem)); double value = -1; @@ -1510,10 +1510,10 @@ TEST(Covariance, ZeroSizedLocalParameterizationGetCovarianceInTangentSpace) { options.algorithm_type = DENSE_SVD; Covariance covariance(options); vector> covariance_blocks; - covariance_blocks.push_back(std::make_pair(&x, &x)); - covariance_blocks.push_back(std::make_pair(&x, &y)); - covariance_blocks.push_back(std::make_pair(&y, &x)); - covariance_blocks.push_back(std::make_pair(&y, &y)); + covariance_blocks.emplace_back(&x, &x); + covariance_blocks.emplace_back(&x, &y); + covariance_blocks.emplace_back(&y, &x); + covariance_blocks.emplace_back(&y, &y); EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem)); double value = -1; @@ -1543,10 +1543,10 @@ TEST(Covariance, ZeroSizedManifoldGetCovariance) { options.algorithm_type = DENSE_SVD; Covariance covariance(options); vector> covariance_blocks; - covariance_blocks.push_back(std::make_pair(&x, &x)); - covariance_blocks.push_back(std::make_pair(&x, &y)); - covariance_blocks.push_back(std::make_pair(&y, &x)); - covariance_blocks.push_back(std::make_pair(&y, &y)); + covariance_blocks.emplace_back(&x, &x); + covariance_blocks.emplace_back(&x, &y); + covariance_blocks.emplace_back(&y, &x); + covariance_blocks.emplace_back(&y, &y); EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem)); double value = -1; @@ -1578,10 +1578,10 @@ TEST(Covariance, ZeroSizedManifoldGetCovarianceInTangentSpace) { options.algorithm_type = DENSE_SVD; Covariance covariance(options); vector> covariance_blocks; - covariance_blocks.push_back(std::make_pair(&x, &x)); - covariance_blocks.push_back(std::make_pair(&x, &y)); - covariance_blocks.push_back(std::make_pair(&y, &x)); - covariance_blocks.push_back(std::make_pair(&y, &y)); + covariance_blocks.emplace_back(&x, &x); + covariance_blocks.emplace_back(&x, &y); + covariance_blocks.emplace_back(&y, &x); + covariance_blocks.emplace_back(&y, &y); EXPECT_TRUE(covariance.Compute(covariance_blocks, &problem)); double value = -1; @@ -1620,7 +1620,7 @@ class LargeScaleCovarianceTest : public ::testing::Test { block_i); for (int j = i; j < num_parameter_blocks_; ++j) { double* block_j = parameters_.get() + j * parameter_block_size_; - all_covariance_blocks_.push_back(make_pair(block_i, block_j)); + all_covariance_blocks_.emplace_back(block_i, block_j); } } } diff --git a/internal/ceres/cxsparse.cc b/internal/ceres/cxsparse.cc index 7aa39fa03..b1eb2055e 100644 --- a/internal/ceres/cxsparse.cc +++ b/internal/ceres/cxsparse.cc @@ -127,7 +127,7 @@ cs_dis* CXSparse::BlockAnalyzeCholesky(cs_di* A, vector scalar_ordering; BlockOrderingToScalarOrdering(row_blocks, block_ordering, &scalar_ordering); - cs_dis* symbolic_factor = + auto* symbolic_factor = reinterpret_cast(cs_calloc(1, sizeof(cs_dis))); symbolic_factor->pinv = cs_pinv(&scalar_ordering[0], A->n); cs* permuted_A = cs_symperm(A, symbolic_factor->pinv, 0); @@ -139,7 +139,7 @@ cs_dis* CXSparse::BlockAnalyzeCholesky(cs_di* A, cs_free(postordering); cs_spfree(permuted_A); - symbolic_factor->cp = (int*)cs_malloc(A->n + 1, sizeof(int)); + symbolic_factor->cp = static_cast(cs_malloc(A->n + 1, sizeof(int))); symbolic_factor->lnz = cs_cumsum(symbolic_factor->cp, column_counts, A->n); symbolic_factor->unz = symbolic_factor->lnz; diff --git a/internal/ceres/cxsparse.h b/internal/ceres/cxsparse.h index 74135f9cb..97fc04594 100644 --- a/internal/ceres/cxsparse.h +++ b/internal/ceres/cxsparse.h @@ -154,7 +154,7 @@ class CERES_NO_EXPORT CXSparseCholesky final : public SparseCholesky { std::string* message) final; private: - CXSparseCholesky(const OrderingType ordering_type); + explicit CXSparseCholesky(const OrderingType ordering_type); void FreeSymbolicFactorization(); void FreeNumericFactorization(); diff --git a/internal/ceres/dense_cholesky_test.cc b/internal/ceres/dense_cholesky_test.cc index 034206a56..0e842074f 100644 --- a/internal/ceres/dense_cholesky_test.cc +++ b/internal/ceres/dense_cholesky_test.cc @@ -45,7 +45,7 @@ namespace ceres { namespace internal { -typedef DenseLinearAlgebraLibraryType Param; +using Param = DenseLinearAlgebraLibraryType; namespace { diff --git a/internal/ceres/dense_linear_solver_test.cc b/internal/ceres/dense_linear_solver_test.cc index 8110d8d4c..9a9dd2d86 100644 --- a/internal/ceres/dense_linear_solver_test.cc +++ b/internal/ceres/dense_linear_solver_test.cc @@ -42,9 +42,8 @@ namespace ceres { namespace internal { -typedef ::testing:: - tuple - Param; +using Param = ::testing:: + tuple; static std::string ParamInfoToString(testing::TestParamInfo info) { Param param = info.param; diff --git a/internal/ceres/dense_normal_cholesky_solver.cc b/internal/ceres/dense_normal_cholesky_solver.cc index 52bf1b818..30a0c023f 100644 --- a/internal/ceres/dense_normal_cholesky_solver.cc +++ b/internal/ceres/dense_normal_cholesky_solver.cc @@ -30,6 +30,8 @@ #include "ceres/dense_normal_cholesky_solver.h" +#include + #include "Eigen/Dense" #include "ceres/dense_sparse_matrix.h" #include "ceres/internal/eigen.h" @@ -41,8 +43,9 @@ namespace ceres { namespace internal { DenseNormalCholeskySolver::DenseNormalCholeskySolver( - const LinearSolver::Options& options) - : options_(options), cholesky_(DenseCholesky::Create(options_)) {} + LinearSolver::Options options) + : options_(std::move(options)), + cholesky_(DenseCholesky::Create(options_)) {} LinearSolver::Summary DenseNormalCholeskySolver::SolveImpl( DenseSparseMatrix* A, diff --git a/internal/ceres/dense_normal_cholesky_solver.h b/internal/ceres/dense_normal_cholesky_solver.h index 395943606..5b3c74069 100644 --- a/internal/ceres/dense_normal_cholesky_solver.h +++ b/internal/ceres/dense_normal_cholesky_solver.h @@ -81,7 +81,7 @@ class DenseSparseMatrix; class CERES_NO_EXPORT DenseNormalCholeskySolver : public DenseSparseMatrixSolver { public: - explicit DenseNormalCholeskySolver(const LinearSolver::Options& options); + explicit DenseNormalCholeskySolver(LinearSolver::Options options); private: LinearSolver::Summary SolveImpl( diff --git a/internal/ceres/dense_qr_test.cc b/internal/ceres/dense_qr_test.cc index f79618694..402a7e225 100644 --- a/internal/ceres/dense_qr_test.cc +++ b/internal/ceres/dense_qr_test.cc @@ -46,7 +46,7 @@ namespace ceres { namespace internal { -typedef DenseLinearAlgebraLibraryType Param; +using Param = DenseLinearAlgebraLibraryType; namespace { diff --git a/internal/ceres/dense_sparse_matrix.cc b/internal/ceres/dense_sparse_matrix.cc index 9e6979d6f..8b967f2ad 100644 --- a/internal/ceres/dense_sparse_matrix.cc +++ b/internal/ceres/dense_sparse_matrix.cc @@ -31,6 +31,7 @@ #include "ceres/dense_sparse_matrix.h" #include +#include #include "ceres/internal/eigen.h" #include "ceres/internal/export.h" @@ -55,7 +56,7 @@ DenseSparseMatrix::DenseSparseMatrix(const TripletSparseMatrix& m) } } -DenseSparseMatrix::DenseSparseMatrix(const Matrix& m) : m_(m) {} +DenseSparseMatrix::DenseSparseMatrix(Matrix m) : m_(std::move(m)) {} void DenseSparseMatrix::SetZero() { m_.setZero(); } diff --git a/internal/ceres/dense_sparse_matrix.h b/internal/ceres/dense_sparse_matrix.h index 9f0835b73..655cbb8a3 100644 --- a/internal/ceres/dense_sparse_matrix.h +++ b/internal/ceres/dense_sparse_matrix.h @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -49,7 +49,7 @@ class CERES_NO_EXPORT DenseSparseMatrix final : public SparseMatrix { // Build a matrix with the same content as the TripletSparseMatrix // m. This assumes that m does not have any repeated entries. explicit DenseSparseMatrix(const TripletSparseMatrix& m); - explicit DenseSparseMatrix(const Matrix& m); + explicit DenseSparseMatrix(Matrix m); DenseSparseMatrix(int num_rows, int num_cols); // SparseMatrix interface. diff --git a/internal/ceres/detect_structure_test.cc b/internal/ceres/detect_structure_test.cc index 8f9c5ed23..3cf95cf5e 100644 --- a/internal/ceres/detect_structure_test.cc +++ b/internal/ceres/detect_structure_test.cc @@ -45,34 +45,34 @@ TEST(DetectStructure, EverythingStatic) { CompressedRowBlockStructure bs; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 0; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 4; bs.cols.back().position = 3; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 4; bs.cols.back().position = 7; { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(1, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(1, 0); } { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 2; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(2, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(2, 0); } int row_block_size = 0; @@ -94,34 +94,34 @@ TEST(DetectStructure, DynamicRow) { CompressedRowBlockStructure bs; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 0; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 4; bs.cols.back().position = 3; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 4; bs.cols.back().position = 7; { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(1, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(1, 0); } { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 1; row.block.position = 2; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(2, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(2, 0); } int row_block_size = 0; @@ -143,34 +143,34 @@ TEST(DetectStructure, DynamicFBlockDifferentRows) { CompressedRowBlockStructure bs; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 0; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 4; bs.cols.back().position = 3; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 7; { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(1, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(1, 0); } { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 2; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(2, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(2, 0); } int row_block_size = 0; @@ -192,34 +192,34 @@ TEST(DetectStructure, DynamicEBlock) { CompressedRowBlockStructure bs; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 0; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 4; bs.cols.back().position = 3; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 7; { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(2, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(2, 0); } { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 2; - row.cells.push_back(Cell(1, 0)); - row.cells.push_back(Cell(2, 0)); + row.cells.emplace_back(1, 0); + row.cells.emplace_back(2, 0); } int row_block_size = 0; @@ -241,26 +241,26 @@ TEST(DetectStructure, DynamicFBlockSameRow) { CompressedRowBlockStructure bs; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 0; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 4; bs.cols.back().position = 3; - bs.cols.push_back(Block()); + bs.cols.emplace_back(); bs.cols.back().size = 3; bs.cols.back().position = 7; { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(1, 0)); - row.cells.push_back(Cell(2, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(1, 0); + row.cells.emplace_back(2, 0); } int row_block_size = 0; diff --git a/internal/ceres/dogleg_strategy.h b/internal/ceres/dogleg_strategy.h index 17f0cc743..1d219afe8 100644 --- a/internal/ceres/dogleg_strategy.h +++ b/internal/ceres/dogleg_strategy.h @@ -76,8 +76,8 @@ class CERES_NO_EXPORT DoglegStrategy final : public TrustRegionStrategy { Matrix subspace_B() const { return subspace_B_; } private: - typedef Eigen::Matrix Vector2d; - typedef Eigen::Matrix Matrix2d; + using Vector2d = Eigen::Matrix; + using Matrix2d = Eigen::Matrix; LinearSolver::Summary ComputeGaussNewtonStep( const PerSolveOptions& per_solve_options, diff --git a/internal/ceres/dynamic_autodiff_cost_function_test.cc b/internal/ceres/dynamic_autodiff_cost_function_test.cc index 7da81a95e..7ee820a28 100644 --- a/internal/ceres/dynamic_autodiff_cost_function_test.cc +++ b/internal/ceres/dynamic_autodiff_cost_function_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -149,8 +149,8 @@ TEST(DynamicAutodiffCostFunctionTest, TestJacobian) { EXPECT_EQ(4 * p - 8, jacobian_vect[0][20 * 10 + p]); jacobian_vect[0][20 * 10 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[0].size(); ++i) { - EXPECT_EQ(0.0, jacobian_vect[0][i]); + for (double entry : jacobian_vect[0]) { + EXPECT_EQ(0.0, entry); } // Check "C" Jacobian for second parameter block. @@ -158,8 +158,8 @@ TEST(DynamicAutodiffCostFunctionTest, TestJacobian) { EXPECT_EQ(1.0, jacobian_vect[1][20 * 5 + p]); jacobian_vect[1][20 * 5 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[1].size(); ++i) { - EXPECT_EQ(0.0, jacobian_vect[1][i]); + for (double entry : jacobian_vect[1]) { + EXPECT_EQ(0.0, entry); } } @@ -207,8 +207,8 @@ TEST(DynamicAutodiffCostFunctionTest, JacobianWithFirstParameterBlockConstant) { EXPECT_EQ(1.0, jacobian_vect[1][20 * 5 + p]); jacobian_vect[1][20 * 5 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[1].size(); ++i) { - EXPECT_EQ(0.0, jacobian_vect[1][i]); + for (double& i : jacobian_vect[1]) { + EXPECT_EQ(0.0, i); } } @@ -265,8 +265,8 @@ TEST(DynamicAutodiffCostFunctionTest, EXPECT_EQ(4 * p - 8, jacobian_vect[0][20 * 10 + p]); jacobian_vect[0][20 * 10 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[0].size(); ++i) { - EXPECT_EQ(0.0, jacobian_vect[0][i]); + for (double& i : jacobian_vect[0]) { + EXPECT_EQ(0.0, i); } } @@ -327,8 +327,8 @@ class ThreeParameterCostFunctorTest : public ::testing::Test { parameter_blocks_[2] = &z_[0]; // Prepare the cost function. - typedef DynamicAutoDiffCostFunction - DynamicMyThreeParameterCostFunction; + using DynamicMyThreeParameterCostFunction = + DynamicAutoDiffCostFunction; auto cost_function = std::make_unique( new MyThreeParameterCostFunctor()); cost_function->AddParameterBlock(1); @@ -559,8 +559,8 @@ class SixParameterCostFunctorTest : public ::testing::Test { parameter_blocks_[5] = &z2_; // Prepare the cost function. - typedef DynamicAutoDiffCostFunction - DynamicMySixParameterCostFunction; + using DynamicMySixParameterCostFunction = + DynamicAutoDiffCostFunction; auto cost_function = std::make_unique( new MySixParameterCostFunctor()); for (int i = 0; i < 6; ++i) { diff --git a/internal/ceres/dynamic_compressed_row_finalizer.h b/internal/ceres/dynamic_compressed_row_finalizer.h index 1645ece59..fedee3b7a 100644 --- a/internal/ceres/dynamic_compressed_row_finalizer.h +++ b/internal/ceres/dynamic_compressed_row_finalizer.h @@ -40,7 +40,7 @@ namespace internal { struct CERES_NO_EXPORT DynamicCompressedRowJacobianFinalizer { void operator()(SparseMatrix* base_jacobian, int num_parameters) { - DynamicCompressedRowSparseMatrix* jacobian = + auto* jacobian = down_cast(base_jacobian); jacobian->Finalize(num_parameters); } diff --git a/internal/ceres/dynamic_compressed_row_jacobian_writer.cc b/internal/ceres/dynamic_compressed_row_jacobian_writer.cc index 4ae951852..8c254e98f 100644 --- a/internal/ceres/dynamic_compressed_row_jacobian_writer.cc +++ b/internal/ceres/dynamic_compressed_row_jacobian_writer.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -62,8 +62,7 @@ void DynamicCompressedRowJacobianWriter::Write(int residual_id, int residual_offset, double** jacobians, SparseMatrix* base_jacobian) { - DynamicCompressedRowSparseMatrix* jacobian = - down_cast(base_jacobian); + auto* jacobian = down_cast(base_jacobian); // Get the `residual_block` of interest. const ResidualBlock* residual_block = @@ -79,11 +78,10 @@ void DynamicCompressedRowJacobianWriter::Write(int residual_id, jacobian->ClearRows(residual_offset, num_residuals); // Iterate over each parameter block. - for (int i = 0; i < evaluated_jacobian_blocks.size(); ++i) { + for (const auto& evaluated_jacobian_block : evaluated_jacobian_blocks) { const ParameterBlock* parameter_block = - program_->parameter_blocks()[evaluated_jacobian_blocks[i].first]; - const int parameter_block_jacobian_index = - evaluated_jacobian_blocks[i].second; + program_->parameter_blocks()[evaluated_jacobian_block.first]; + const int parameter_block_jacobian_index = evaluated_jacobian_block.second; const int parameter_block_size = parameter_block->TangentSize(); const double* parameter_jacobian = jacobians[parameter_block_jacobian_index]; diff --git a/internal/ceres/dynamic_compressed_row_sparse_matrix.cc b/internal/ceres/dynamic_compressed_row_sparse_matrix.cc index 936e682b7..7185e14a4 100644 --- a/internal/ceres/dynamic_compressed_row_sparse_matrix.cc +++ b/internal/ceres/dynamic_compressed_row_sparse_matrix.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -70,8 +70,8 @@ void DynamicCompressedRowSparseMatrix::Finalize(int num_additional_elements) { // Count the number of non-zeros and resize `cols_` and `values_`. int num_jacobian_nonzeros = 0; - for (int i = 0; i < dynamic_cols_.size(); ++i) { - num_jacobian_nonzeros += dynamic_cols_[i].size(); + for (const auto& dynamic_col : dynamic_cols_) { + num_jacobian_nonzeros += dynamic_col.size(); } SetMaxNumNonZeros(num_jacobian_nonzeros + num_additional_elements); diff --git a/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc b/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc index 3b673895a..269858fe2 100644 --- a/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc +++ b/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc @@ -141,7 +141,7 @@ class DynamicCompressedRowSparseMatrixTest : public ::testing::Test { } void ExpectEqualToCompressedRowSparseMatrixReference() { - typedef Eigen::Map ConstIntVectorRef; + using ConstIntVectorRef = Eigen::Map; ConstIntVectorRef crsm_rows(crsm->rows(), crsm->num_rows() + 1); ConstIntVectorRef dcrsm_rows(dcrsm->rows(), dcrsm->num_rows() + 1); diff --git a/internal/ceres/dynamic_numeric_diff_cost_function_test.cc b/internal/ceres/dynamic_numeric_diff_cost_function_test.cc index ad3f479b5..e35e1bcf4 100644 --- a/internal/ceres/dynamic_numeric_diff_cost_function_test.cc +++ b/internal/ceres/dynamic_numeric_diff_cost_function_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -149,8 +149,8 @@ TEST(DynamicNumericdiffCostFunctionTest, TestJacobian) { EXPECT_NEAR(4 * p - 8, jacobian_vect[0][20 * 10 + p], kTolerance); jacobian_vect[0][20 * 10 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[0].size(); ++i) { - EXPECT_NEAR(0.0, jacobian_vect[0][i], kTolerance); + for (double entry : jacobian_vect[0]) { + EXPECT_NEAR(0.0, entry, kTolerance); } // Check "C" Jacobian for second parameter block. @@ -158,8 +158,8 @@ TEST(DynamicNumericdiffCostFunctionTest, TestJacobian) { EXPECT_NEAR(1.0, jacobian_vect[1][20 * 5 + p], kTolerance); jacobian_vect[1][20 * 5 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[1].size(); ++i) { - EXPECT_NEAR(0.0, jacobian_vect[1][i], kTolerance); + for (double entry : jacobian_vect[1]) { + EXPECT_NEAR(0.0, entry, kTolerance); } } @@ -208,8 +208,8 @@ TEST(DynamicNumericdiffCostFunctionTest, EXPECT_NEAR(1.0, jacobian_vect[1][20 * 5 + p], kTolerance); jacobian_vect[1][20 * 5 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[1].size(); ++i) { - EXPECT_EQ(0.0, jacobian_vect[1][i]); + for (double& i : jacobian_vect[1]) { + EXPECT_EQ(0.0, i); } } @@ -266,8 +266,8 @@ TEST(DynamicNumericdiffCostFunctionTest, EXPECT_NEAR(4 * p - 8, jacobian_vect[0][20 * 10 + p], kTolerance); jacobian_vect[0][20 * 10 + p] = 0.0; } - for (int i = 0; i < jacobian_vect[0].size(); ++i) { - EXPECT_EQ(0.0, jacobian_vect[0][i]); + for (double& i : jacobian_vect[0]) { + EXPECT_EQ(0.0, i); } } @@ -328,8 +328,8 @@ class ThreeParameterCostFunctorTest : public ::testing::Test { parameter_blocks_[2] = &z_[0]; // Prepare the cost function. - typedef DynamicNumericDiffCostFunction - DynamicMyThreeParameterCostFunction; + using DynamicMyThreeParameterCostFunction = + DynamicNumericDiffCostFunction; auto cost_function = std::make_unique( new MyThreeParameterCostFunctor()); cost_function->AddParameterBlock(1); diff --git a/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc b/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc index d101aef6d..078edbf64 100644 --- a/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc +++ b/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc @@ -35,6 +35,7 @@ #include #include #include +#include #include "Eigen/SparseCore" #include "ceres/compressed_row_sparse_matrix.h" @@ -54,8 +55,8 @@ namespace ceres { namespace internal { DynamicSparseNormalCholeskySolver::DynamicSparseNormalCholeskySolver( - const LinearSolver::Options& options) - : options_(options) {} + LinearSolver::Options options) + : options_(std::move(options)) {} LinearSolver::Summary DynamicSparseNormalCholeskySolver::SolveImpl( CompressedRowSparseMatrix* A, diff --git a/internal/ceres/dynamic_sparse_normal_cholesky_solver.h b/internal/ceres/dynamic_sparse_normal_cholesky_solver.h index 9ab212463..6f73c9612 100644 --- a/internal/ceres/dynamic_sparse_normal_cholesky_solver.h +++ b/internal/ceres/dynamic_sparse_normal_cholesky_solver.h @@ -57,8 +57,7 @@ class CompressedRowSparseMatrix; class CERES_NO_EXPORT DynamicSparseNormalCholeskySolver : public CompressedRowSparseMatrixSolver { public: - explicit DynamicSparseNormalCholeskySolver( - const LinearSolver::Options& options); + explicit DynamicSparseNormalCholeskySolver(LinearSolver::Options options); private: LinearSolver::Summary SolveImpl(CompressedRowSparseMatrix* A, diff --git a/internal/ceres/dynamic_sparsity_test.cc b/internal/ceres/dynamic_sparsity_test.cc index 6c7b24ccd..8c98528e3 100644 --- a/internal/ceres/dynamic_sparsity_test.cc +++ b/internal/ceres/dynamic_sparsity_test.cc @@ -32,6 +32,7 @@ // Based on examples/ellipse_approximation.cc #include +#include #include #include "ceres/ceres.h" @@ -280,8 +281,8 @@ class PointToLineSegmentContourCostFunction : public CostFunction { EIGEN_MAKE_ALIGNED_OPERATOR_NEW PointToLineSegmentContourCostFunction(const int num_segments, - const Eigen::Vector2d& y) - : num_segments_(num_segments), y_(y) { + Eigen::Vector2d y) + : num_segments_(num_segments), y_(std::move(y)) { // The first parameter is the preimage position. mutable_parameter_block_sizes()->push_back(1); // The next parameters are the control points for the line segment contour. diff --git a/internal/ceres/eigensparse.cc b/internal/ceres/eigensparse.cc index 38055dcd9..81668c82e 100644 --- a/internal/ceres/eigensparse.cc +++ b/internal/ceres/eigensparse.cc @@ -49,7 +49,7 @@ namespace internal { template class EigenSparseCholeskyTemplate final : public SparseCholesky { public: - EigenSparseCholeskyTemplate() : analyzed_(false) {} + EigenSparseCholeskyTemplate() = default; CompressedRowSparseMatrix::StorageType StorageType() const final { return CompressedRowSparseMatrix::LOWER_TRIANGULAR; } @@ -136,20 +136,19 @@ class EigenSparseCholeskyTemplate final : public SparseCholesky { private: Eigen::Matrix values_, scalar_rhs_, scalar_solution_; - bool analyzed_; + bool analyzed_{false}; Solver solver_; }; std::unique_ptr EigenSparseCholesky::Create( const OrderingType ordering_type) { - typedef Eigen::SimplicialLDLT, - Eigen::Upper, - Eigen::AMDOrdering> - WithAMDOrdering; - typedef Eigen::SimplicialLDLT, - Eigen::Upper, - Eigen::NaturalOrdering> - WithNaturalOrdering; + using WithAMDOrdering = Eigen::SimplicialLDLT, + Eigen::Upper, + Eigen::AMDOrdering>; + using WithNaturalOrdering = + Eigen::SimplicialLDLT, + Eigen::Upper, + Eigen::NaturalOrdering>; if (ordering_type == AMD) { return std::make_unique>(); @@ -162,14 +161,13 @@ EigenSparseCholesky::~EigenSparseCholesky() = default; std::unique_ptr FloatEigenSparseCholesky::Create( const OrderingType ordering_type) { - typedef Eigen::SimplicialLDLT, - Eigen::Upper, - Eigen::AMDOrdering> - WithAMDOrdering; - typedef Eigen::SimplicialLDLT, - Eigen::Upper, - Eigen::NaturalOrdering> - WithNaturalOrdering; + using WithAMDOrdering = Eigen::SimplicialLDLT, + Eigen::Upper, + Eigen::AMDOrdering>; + using WithNaturalOrdering = + Eigen::SimplicialLDLT, + Eigen::Upper, + Eigen::NaturalOrdering>; if (ordering_type == AMD) { return std::make_unique>(); } else { diff --git a/internal/ceres/evaluation_callback_test.cc b/internal/ceres/evaluation_callback_test.cc index e9809a3fc..179c10668 100644 --- a/internal/ceres/evaluation_callback_test.cc +++ b/internal/ceres/evaluation_callback_test.cc @@ -50,7 +50,7 @@ namespace internal { template uint64_t Djb2Hash(const T* data, const int size) { uint64_t hash = 5381; - const uint8_t* data_as_bytes = reinterpret_cast(data); + const auto* data_as_bytes = reinterpret_cast(data); for (int i = 0; i < sizeof(*data) * size; ++i) { hash = hash * 33 + data_as_bytes[i]; } diff --git a/internal/ceres/evaluator.h b/internal/ceres/evaluator.h index 28e8ce21f..68a4fb28a 100644 --- a/internal/ceres/evaluator.h +++ b/internal/ceres/evaluator.h @@ -160,7 +160,7 @@ class CERES_NO_EXPORT Evaluator { // life time issues. Further, these calls are not expected to be // frequent or performance sensitive. virtual std::map Statistics() const { - return std::map(); + return {}; } }; diff --git a/internal/ceres/evaluator_test.cc b/internal/ceres/evaluator_test.cc index f5703f702..10c0d1d23 100644 --- a/internal/ceres/evaluator_test.cc +++ b/internal/ceres/evaluator_test.cc @@ -60,7 +60,7 @@ using std::vector; template class ParameterIgnoringCostFunction : public SizedCostFunction { - typedef SizedCostFunction Base; + using Base = SizedCostFunction; public: explicit ParameterIgnoringCostFunction(bool succeeds = true) diff --git a/internal/ceres/execution_summary.h b/internal/ceres/execution_summary.h index aac7ad6ad..fbee75fc0 100644 --- a/internal/ceres/execution_summary.h +++ b/internal/ceres/execution_summary.h @@ -34,6 +34,7 @@ #include #include #include +#include #include "ceres/internal/export.h" #include "ceres/wall_time.h" @@ -42,9 +43,9 @@ namespace ceres { namespace internal { struct CallStatistics { - CallStatistics() : time(0.), calls(0) {} - double time; - int calls; + CallStatistics() = default; + double time{0.}; + int calls{0}; }; // Struct used by various objects to report statistics about their @@ -69,8 +70,10 @@ class ExecutionSummary { class ScopedExecutionTimer { public: - ScopedExecutionTimer(const std::string& name, ExecutionSummary* summary) - : start_time_(WallTimeInSeconds()), name_(name), summary_(summary) {} + ScopedExecutionTimer(std::string name, ExecutionSummary* summary) + : start_time_(WallTimeInSeconds()), + name_(std::move(name)), + summary_(summary) {} ~ScopedExecutionTimer() { summary_->IncrementTimeBy(name_, WallTimeInSeconds() - start_time_); diff --git a/internal/ceres/fixed_array_test.cc b/internal/ceres/fixed_array_test.cc index 2982a988f..d6c56052a 100644 --- a/internal/ceres/fixed_array_test.cc +++ b/internal/ceres/fixed_array_test.cc @@ -14,8 +14,7 @@ #include "ceres/internal/fixed_array.h" -#include - +#include #include #include #include @@ -54,7 +53,7 @@ static bool IsOnStack(const ArrayType& a) { class ConstructionTester { public: - ConstructionTester() : self_ptr_(this), value_(0) { constructions++; } + ConstructionTester() : self_ptr_(this) { constructions++; } ~ConstructionTester() { assert(self_ptr_ == this); self_ptr_ = nullptr; @@ -75,7 +74,7 @@ class ConstructionTester { // self_ptr_ should always point to 'this' -- that's how we can be sure the // constructor has been called. ConstructionTester* self_ptr_; - int value_; + int value_{0}; }; int ConstructionTester::constructions = 0; @@ -117,7 +116,7 @@ TEST(FixedArrayTest, CopyCtor) { TEST(FixedArrayTest, MoveCtor) { ceres::internal::FixedArray, 10> on_stack(5); for (int i = 0; i < 5; ++i) { - on_stack[i] = std::unique_ptr(new int(i)); + on_stack[i] = std::make_unique(i); } ceres::internal::FixedArray, 10> stack_copy = @@ -127,7 +126,7 @@ TEST(FixedArrayTest, MoveCtor) { ceres::internal::FixedArray, 10> allocated(15); for (int i = 0; i < 15; ++i) { - allocated[i] = std::unique_ptr(new int(i)); + allocated[i] = std::make_unique(i); } ceres::internal::FixedArray, 10> alloced_copy = @@ -655,9 +654,8 @@ class CountingAllocator : public std::allocator { using Alloc = std::allocator; using size_type = typename Alloc::size_type; - CountingAllocator() : bytes_used_(nullptr), instance_count_(nullptr) {} - explicit CountingAllocator(int64_t* b) - : bytes_used_(b), instance_count_(nullptr) {} + CountingAllocator() = default; + explicit CountingAllocator(int64_t* b) : bytes_used_(b) {} CountingAllocator(int64_t* b, int64_t* a) : bytes_used_(b), instance_count_(a) {} @@ -679,8 +677,8 @@ class CountingAllocator : public std::allocator { *bytes_used_ -= n * sizeof(T); } - int64_t* bytes_used_; - int64_t* instance_count_; + int64_t* bytes_used_{nullptr}; + int64_t* instance_count_{nullptr}; }; TEST(AllocatorSupportTest, CountInlineAllocations) { diff --git a/internal/ceres/float_cxsparse.cc b/internal/ceres/float_cxsparse.cc index a5cc527e4..a6d5e811e 100644 --- a/internal/ceres/float_cxsparse.cc +++ b/internal/ceres/float_cxsparse.cc @@ -40,7 +40,7 @@ namespace internal { std::unique_ptr FloatCXSparseCholesky::Create( OrderingType ordering_type) { LOG(FATAL) << "FloatCXSparseCholesky is not available."; - return std::unique_ptr(); + return {}; } } // namespace internal diff --git a/internal/ceres/float_suitesparse.cc b/internal/ceres/float_suitesparse.cc index 731a1e949..dc1d0e45b 100644 --- a/internal/ceres/float_suitesparse.cc +++ b/internal/ceres/float_suitesparse.cc @@ -40,7 +40,7 @@ namespace internal { std::unique_ptr FloatSuiteSparseCholesky::Create( OrderingType ordering_type) { LOG(FATAL) << "FloatSuiteSparseCholesky is not available."; - return std::unique_ptr(); + return {}; } } // namespace internal diff --git a/internal/ceres/gradient_checker.cc b/internal/ceres/gradient_checker.cc index fd302f099..777001e01 100644 --- a/internal/ceres/gradient_checker.cc +++ b/internal/ceres/gradient_checker.cc @@ -150,7 +150,7 @@ GradientChecker::GradientChecker( GradientChecker::GradientChecker(const CostFunction* function, const vector* manifolds, const NumericDiffOptions& options) - : delete_manifolds_(false), function_(function) { + : function_(function) { CHECK(function != nullptr); if (manifolds != nullptr) { manifolds_ = *manifolds; diff --git a/internal/ceres/gradient_checker_test.cc b/internal/ceres/gradient_checker_test.cc index 1d59e2f0a..bef766808 100644 --- a/internal/ceres/gradient_checker_test.cc +++ b/internal/ceres/gradient_checker_test.cc @@ -34,6 +34,7 @@ #include #include +#include #include #include "ceres/cost_function.h" @@ -289,8 +290,8 @@ TEST(GradientChecker, SmokeTest) { */ class LinearCostFunction : public CostFunction { public: - explicit LinearCostFunction(const Vector& residuals_offset) - : residuals_offset_(residuals_offset) { + explicit LinearCostFunction(Vector residuals_offset) + : residuals_offset_(std::move(residuals_offset)) { set_num_residuals(residuals_offset_.size()); } diff --git a/internal/ceres/gradient_checking_cost_function.cc b/internal/ceres/gradient_checking_cost_function.cc index 3251806d6..1c3b318ed 100644 --- a/internal/ceres/gradient_checking_cost_function.cc +++ b/internal/ceres/gradient_checking_cost_function.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -37,6 +37,7 @@ #include #include #include +#include #include #include "ceres/dynamic_numeric_diff_cost_function.h" @@ -67,12 +68,12 @@ class GradientCheckingCostFunction final : public CostFunction { const std::vector* manifolds, const NumericDiffOptions& options, double relative_precision, - const string& extra_info, + string extra_info, GradientCheckingIterationCallback* callback) : function_(function), gradient_checker_(function, manifolds, options), relative_precision_(relative_precision), - extra_info_(extra_info), + extra_info_(std::move(extra_info)), callback_(callback) { CHECK(callback_ != nullptr); const vector& parameter_block_sizes = @@ -198,8 +199,7 @@ std::unique_ptr CreateGradientCheckingProblemImpl( // For every ParameterBlock in problem_impl, create a new parameter block with // the same manifold and constancy. const vector& parameter_blocks = program->parameter_blocks(); - for (int i = 0; i < parameter_blocks.size(); ++i) { - ParameterBlock* parameter_block = parameter_blocks[i]; + for (auto* parameter_block : parameter_blocks) { gradient_checking_problem_impl->AddParameterBlock( parameter_block->mutable_user_state(), parameter_block->Size(), diff --git a/internal/ceres/gradient_problem_solver_test.cc b/internal/ceres/gradient_problem_solver_test.cc index 47a69f3dd..407fb17cc 100644 --- a/internal/ceres/gradient_problem_solver_test.cc +++ b/internal/ceres/gradient_problem_solver_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -113,8 +113,8 @@ TEST(Solver, UpdateStateEveryIterationOption) { ceres::Solve(options, problem, &x, &summary); num_iterations = summary.iterations.size() - 1; EXPECT_GT(num_iterations, 1); - for (int i = 0; i < callback.x_values.size(); ++i) { - EXPECT_EQ(50.0, callback.x_values[i]); + for (double value : callback.x_values) { + EXPECT_EQ(50.0, value); } // Second try: with updating diff --git a/internal/ceres/implicit_schur_complement.cc b/internal/ceres/implicit_schur_complement.cc index 82a776c9c..677d767fa 100644 --- a/internal/ceres/implicit_schur_complement.cc +++ b/internal/ceres/implicit_schur_complement.cc @@ -136,10 +136,10 @@ void ImplicitSchurComplement::AddDiagonalAndInvert( const double* D, BlockSparseMatrix* block_diagonal) { const CompressedRowBlockStructure* block_diagonal_structure = block_diagonal->block_structure(); - for (int r = 0; r < block_diagonal_structure->rows.size(); ++r) { - const int row_block_pos = block_diagonal_structure->rows[r].block.position; - const int row_block_size = block_diagonal_structure->rows[r].block.size; - const Cell& cell = block_diagonal_structure->rows[r].cells[0]; + for (const auto& row : block_diagonal_structure->rows) { + const int row_block_pos = row.block.position; + const int row_block_size = row.block.size; + const Cell& cell = row.cells[0]; MatrixRef m(block_diagonal->mutable_values() + cell.position, row_block_size, row_block_size); diff --git a/internal/ceres/inner_product_computer.cc b/internal/ceres/inner_product_computer.cc index c24f6fc73..fbc43bfed 100644 --- a/internal/ceres/inner_product_computer.cc +++ b/internal/ceres/inner_product_computer.cc @@ -167,8 +167,8 @@ void InnerProductComputer::Init( for (int c2 = c2_begin; c2 < c2_end; ++c2) { const Cell& cell2 = row.cells[c2]; - product_terms.push_back(InnerProductComputer::ProductTerm( - cell1.block_id, cell2.block_id, product_terms.size())); + product_terms.emplace_back( + cell1.block_id, cell2.block_id, product_terms.size()); } } } diff --git a/internal/ceres/inner_product_computer_test.cc b/internal/ceres/inner_product_computer_test.cc index b672f8caa..b01ad8eba 100644 --- a/internal/ceres/inner_product_computer_test.cc +++ b/internal/ceres/inner_product_computer_test.cc @@ -117,8 +117,7 @@ TEST(InnerProductComputer, NormalOperation) { random_matrix->ToTripletSparseMatrix(&tsm); std::vector> triplets; for (int i = 0; i < tsm.num_nonzeros(); ++i) { - triplets.push_back(Eigen::Triplet( - tsm.rows()[i], tsm.cols()[i], tsm.values()[i])); + triplets.emplace_back(tsm.rows()[i], tsm.cols()[i], tsm.values()[i]); } Eigen::SparseMatrix eigen_random_matrix( random_matrix->num_rows(), random_matrix->num_cols()); @@ -189,8 +188,8 @@ TEST(InnerProductComputer, SubMatrix) { std::vector> triplets; for (int i = 0; i < tsm.num_nonzeros(); ++i) { if (tsm.rows()[i] >= start_row && tsm.rows()[i] < end_row) { - triplets.push_back(Eigen::Triplet( - tsm.rows()[i], tsm.cols()[i], tsm.values()[i])); + triplets.emplace_back( + tsm.rows()[i], tsm.cols()[i], tsm.values()[i]); } } diff --git a/internal/ceres/iterative_refiner.h b/internal/ceres/iterative_refiner.h index 87e45b141..837af178a 100644 --- a/internal/ceres/iterative_refiner.h +++ b/internal/ceres/iterative_refiner.h @@ -62,7 +62,7 @@ class CERES_NO_EXPORT IterativeRefiner { public: // max_num_iterations is the number of refinement iterations to // perform. - IterativeRefiner(int max_num_iterations); + explicit IterativeRefiner(int max_num_iterations); // Needed for mocking. virtual ~IterativeRefiner(); diff --git a/internal/ceres/iterative_refiner_test.cc b/internal/ceres/iterative_refiner_test.cc index 5464a275d..e2167b0b9 100644 --- a/internal/ceres/iterative_refiner_test.cc +++ b/internal/ceres/iterative_refiner_test.cc @@ -30,6 +30,8 @@ #include "ceres/iterative_refiner.h" +#include + #include "Eigen/Dense" #include "ceres/internal/eigen.h" #include "ceres/sparse_cholesky.h" @@ -53,7 +55,7 @@ namespace internal { // A fake SparseMatrix, which uses an Eigen matrix to do the real work. class FakeSparseMatrix : public SparseMatrix { public: - FakeSparseMatrix(const Matrix& m) : m_(m) {} + explicit FakeSparseMatrix(Matrix m) : m_(std::move(m)) {} // y += Ax void RightMultiply(const double* x, double* y) const final { @@ -88,7 +90,7 @@ class FakeSparseMatrix : public SparseMatrix { template class FakeSparseCholesky : public SparseCholesky { public: - FakeSparseCholesky(const Matrix& lhs) { lhs_ = lhs.cast(); } + explicit FakeSparseCholesky(const Matrix& lhs) { lhs_ = lhs.cast(); } LinearSolverTerminationType Solve(const double* rhs_ptr, double* solution_ptr, diff --git a/internal/ceres/iterative_schur_complement_solver.cc b/internal/ceres/iterative_schur_complement_solver.cc index 6ff84b5fc..bc22d68bc 100644 --- a/internal/ceres/iterative_schur_complement_solver.cc +++ b/internal/ceres/iterative_schur_complement_solver.cc @@ -32,6 +32,7 @@ #include #include +#include #include #include "Eigen/Dense" @@ -54,8 +55,8 @@ namespace ceres { namespace internal { IterativeSchurComplementSolver::IterativeSchurComplementSolver( - const LinearSolver::Options& options) - : options_(options) {} + LinearSolver::Options options) + : options_(std::move(options)) {} IterativeSchurComplementSolver::~IterativeSchurComplementSolver() = default; diff --git a/internal/ceres/iterative_schur_complement_solver.h b/internal/ceres/iterative_schur_complement_solver.h index 0794f361d..50f469484 100644 --- a/internal/ceres/iterative_schur_complement_solver.h +++ b/internal/ceres/iterative_schur_complement_solver.h @@ -73,7 +73,7 @@ class Preconditioner; class CERES_NO_EXPORT IterativeSchurComplementSolver final : public BlockSparseMatrixSolver { public: - explicit IterativeSchurComplementSolver(const LinearSolver::Options& options); + explicit IterativeSchurComplementSolver(LinearSolver::Options options); IterativeSchurComplementSolver(const IterativeSchurComplementSolver&) = delete; void operator=(const IterativeSchurComplementSolver&) = delete; diff --git a/internal/ceres/jet_operator_benchmark.cc b/internal/ceres/jet_operator_benchmark.cc index 570155629..8d8900c3b 100644 --- a/internal/ceres/jet_operator_benchmark.cc +++ b/internal/ceres/jet_operator_benchmark.cc @@ -42,7 +42,7 @@ class JetInputData { static constexpr std::size_t SIZE = 20; public: - JetInputData() : index_{0}, a_{}, b_{}, c_{}, d_{}, e_{} { + JetInputData() { for (int i = 0; i < static_cast(SIZE); i++) { const T ti = static_cast(i + 1); @@ -83,12 +83,12 @@ class JetInputData { T scalar_e() const { return scalar_e_[index_]; } private: - std::size_t index_; - std::array a_; - std::array b_; - std::array c_; - std::array d_; - std::array e_; + std::size_t index_{0}; + std::array a_{}; + std::array b_{}; + std::array c_{}; + std::array d_{}; + std::array e_{}; std::array scalar_a_; std::array scalar_b_; std::array scalar_c_; diff --git a/internal/ceres/jet_test.cc b/internal/ceres/jet_test.cc index 544575cce..f06c599e5 100644 --- a/internal/ceres/jet_test.cc +++ b/internal/ceres/jet_test.cc @@ -547,7 +547,7 @@ TEST(Jet, Pow) { TEST(Jet, Hypot2) { // Resolve the ambiguity between two and three argument hypot overloads using Hypot2 = J(const J&, const J&); - Hypot2* const hypot2 = static_cast(&hypot); + auto* const hypot2 = static_cast(&hypot); // clang-format off NumericalTest2("hypot2", hypot2, 0.0, 1e-5); @@ -1172,8 +1172,8 @@ TEST(JetTraitsTest, ArrayScalarBinaryOps) { } TEST(Jet, Nested3X) { - typedef Jet JJ; - typedef Jet JJJ; + using JJ = Jet; + using JJJ = Jet; JJJ x; x.a = JJ(J(1, 0), 0); diff --git a/internal/ceres/levenberg_marquardt_strategy_test.cc b/internal/ceres/levenberg_marquardt_strategy_test.cc index 818b7f53a..0e7ec8dc0 100644 --- a/internal/ceres/levenberg_marquardt_strategy_test.cc +++ b/internal/ceres/levenberg_marquardt_strategy_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -70,7 +70,7 @@ class RegularizationCheckingLinearSolver : public DenseSparseMatrixSolver { EXPECT_NEAR(per_solve_options.D[i], diagonal_[i], kTolerance) << i << " " << per_solve_options.D[i] << " " << diagonal_[i]; } - return LinearSolver::Summary(); + return {}; } const int num_cols_; @@ -131,8 +131,8 @@ TEST(LevenbergMarquardtStrategy, CorrectDiagonalToLinearSolver) { diagonal[0] = options.min_lm_diagonal; diagonal[1] = 2.0; diagonal[2] = options.max_lm_diagonal; - for (int i = 0; i < 3; ++i) { - diagonal[i] = sqrt(diagonal[i] / options.initial_radius); + for (double& diagonal_entry : diagonal) { + diagonal_entry = sqrt(diagonal_entry / options.initial_radius); } RegularizationCheckingLinearSolver linear_solver(3, diagonal); diff --git a/internal/ceres/line_search.cc b/internal/ceres/line_search.cc index d64858fee..7e7d97f6d 100644 --- a/internal/ceres/line_search.cc +++ b/internal/ceres/line_search.cc @@ -249,12 +249,12 @@ double LineSearch::InterpolatingPolynomialMinimizingStepSize( if (interpolation_type == QUADRATIC) { // Two point interpolation using function values and the // gradient at the lower bound. - samples.push_back(FunctionSample(current.x, current.value)); + samples.emplace_back(current.x, current.value); if (previous.value_is_valid) { // Three point interpolation, using function values and the // gradient at the lower bound. - samples.push_back(FunctionSample(previous.x, previous.value)); + samples.emplace_back(previous.x, previous.value); } } else if (interpolation_type == CUBIC) { // Two point interpolation using the function values and the gradients. diff --git a/internal/ceres/line_search_direction.h b/internal/ceres/line_search_direction.h index be7497e22..47b256d71 100644 --- a/internal/ceres/line_search_direction.h +++ b/internal/ceres/line_search_direction.h @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -44,20 +44,13 @@ namespace internal { class CERES_NO_EXPORT LineSearchDirection { public: struct Options { - Options() - : num_parameters(0), - type(LBFGS), - nonlinear_conjugate_gradient_type(FLETCHER_REEVES), - function_tolerance(1e-12), - max_lbfgs_rank(20), - use_approximate_eigenvalue_bfgs_scaling(true) {} - - int num_parameters; - LineSearchDirectionType type; - NonlinearConjugateGradientType nonlinear_conjugate_gradient_type; - double function_tolerance; - int max_lbfgs_rank; - bool use_approximate_eigenvalue_bfgs_scaling; + int num_parameters{0}; + LineSearchDirectionType type{LBFGS}; + NonlinearConjugateGradientType nonlinear_conjugate_gradient_type{ + FLETCHER_REEVES}; + double function_tolerance{1e-12}; + int max_lbfgs_rank{20}; + bool use_approximate_eigenvalue_bfgs_scaling{true}; }; static std::unique_ptr Create(const Options& options); diff --git a/internal/ceres/linear_least_squares_problems.cc b/internal/ceres/linear_least_squares_problems.cc index 4b37e00ef..2d415af33 100644 --- a/internal/ceres/linear_least_squares_problems.cc +++ b/internal/ceres/linear_least_squares_problems.cc @@ -299,12 +299,12 @@ std::unique_ptr LinearLeastSquaresProblem2() { problem->D = std::make_unique(num_cols); problem->num_eliminate_blocks = 2; - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; + auto* bs = new CompressedRowBlockStructure; std::unique_ptr values = std::make_unique(num_rows * num_cols); for (int c = 0; c < num_cols; ++c) { - bs->cols.push_back(Block()); + bs->cols.emplace_back(); bs->cols.back().size = 1; bs->cols.back().position = c; } @@ -316,12 +316,12 @@ std::unique_ptr LinearLeastSquaresProblem2() { values[nnz++] = 1; values[nnz++] = 2; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(2, 1)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(2, 1); } // Row 2 @@ -329,12 +329,12 @@ std::unique_ptr LinearLeastSquaresProblem2() { values[nnz++] = 3; values[nnz++] = 4; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 1; - row.cells.push_back(Cell(0, 2)); - row.cells.push_back(Cell(3, 3)); + row.cells.emplace_back(0, 2); + row.cells.emplace_back(3, 3); } // Row 3 @@ -342,12 +342,12 @@ std::unique_ptr LinearLeastSquaresProblem2() { values[nnz++] = 5; values[nnz++] = 6; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 2; - row.cells.push_back(Cell(1, 4)); - row.cells.push_back(Cell(4, 5)); + row.cells.emplace_back(1, 4); + row.cells.emplace_back(4, 5); } // Row 4 @@ -355,12 +355,12 @@ std::unique_ptr LinearLeastSquaresProblem2() { values[nnz++] = 7; values[nnz++] = 8; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 3; - row.cells.push_back(Cell(1, 6)); - row.cells.push_back(Cell(2, 7)); + row.cells.emplace_back(1, 6); + row.cells.emplace_back(2, 7); } // Row 5 @@ -368,12 +368,12 @@ std::unique_ptr LinearLeastSquaresProblem2() { values[nnz++] = 9; values[nnz++] = 1; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 4; - row.cells.push_back(Cell(1, 8)); - row.cells.push_back(Cell(2, 9)); + row.cells.emplace_back(1, 8); + row.cells.emplace_back(2, 9); } // Row 6 @@ -382,13 +382,13 @@ std::unique_ptr LinearLeastSquaresProblem2() { values[nnz++] = 1; values[nnz++] = 1; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 5; - row.cells.push_back(Cell(2, 10)); - row.cells.push_back(Cell(3, 11)); - row.cells.push_back(Cell(4, 12)); + row.cells.emplace_back(2, 10); + row.cells.emplace_back(3, 11); + row.cells.emplace_back(4, 12); } auto A = std::make_unique(bs); @@ -434,12 +434,12 @@ std::unique_ptr LinearLeastSquaresProblem3() { problem->D = std::make_unique(num_cols); problem->num_eliminate_blocks = 2; - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; + auto* bs = new CompressedRowBlockStructure; std::unique_ptr values = std::make_unique(num_rows * num_cols); for (int c = 0; c < num_cols; ++c) { - bs->cols.push_back(Block()); + bs->cols.emplace_back(); bs->cols.back().size = 1; bs->cols.back().position = c; } @@ -449,51 +449,51 @@ std::unique_ptr LinearLeastSquaresProblem3() { // Row 1 { values[nnz++] = 1; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); + row.cells.emplace_back(0, 0); } // Row 2 { values[nnz++] = 3; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 1; - row.cells.push_back(Cell(0, 1)); + row.cells.emplace_back(0, 1); } // Row 3 { values[nnz++] = 5; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 2; - row.cells.push_back(Cell(1, 2)); + row.cells.emplace_back(1, 2); } // Row 4 { values[nnz++] = 7; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 3; - row.cells.push_back(Cell(1, 3)); + row.cells.emplace_back(1, 3); } // Row 5 { values[nnz++] = 9; - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 4; - row.cells.push_back(Cell(1, 4)); + row.cells.emplace_back(1, 4); } auto A = std::make_unique(bs); @@ -543,20 +543,20 @@ std::unique_ptr LinearLeastSquaresProblem4() { problem->D = std::make_unique(num_cols); problem->num_eliminate_blocks = 1; - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; + auto* bs = new CompressedRowBlockStructure; std::unique_ptr values = std::make_unique(num_rows * num_cols); // Column block structure - bs->cols.push_back(Block()); + bs->cols.emplace_back(); bs->cols.back().size = 2; bs->cols.back().position = 0; - bs->cols.push_back(Block()); + bs->cols.emplace_back(); bs->cols.back().size = 3; bs->cols.back().position = 2; - bs->cols.push_back(Block()); + bs->cols.emplace_back(); bs->cols.back().size = 2; bs->cols.back().position = 5; @@ -564,18 +564,18 @@ std::unique_ptr LinearLeastSquaresProblem4() { // Row 1 & 2 { - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, nnz)); + row.cells.emplace_back(0, nnz); values[nnz++] = 1; values[nnz++] = 2; values[nnz++] = 1; values[nnz++] = 4; - row.cells.push_back(Cell(2, nnz)); + row.cells.emplace_back(2, nnz); values[nnz++] = 1; values[nnz++] = 1; values[nnz++] = 5; @@ -584,17 +584,17 @@ std::unique_ptr LinearLeastSquaresProblem4() { // Row 3 { - bs->rows.push_back(CompressedRow()); + bs->rows.emplace_back(); CompressedRow& row = bs->rows.back(); row.block.size = 1; row.block.position = 2; - row.cells.push_back(Cell(1, nnz)); + row.cells.emplace_back(1, nnz); values[nnz++] = 9; values[nnz++] = 0; values[nnz++] = 0; - row.cells.push_back(Cell(2, nnz)); + row.cells.emplace_back(2, nnz); values[nnz++] = 3; values[nnz++] = 1; } diff --git a/internal/ceres/linear_least_squares_problems.h b/internal/ceres/linear_least_squares_problems.h index 35ba24635..a1f67eb30 100644 --- a/internal/ceres/linear_least_squares_problems.h +++ b/internal/ceres/linear_least_squares_problems.h @@ -45,14 +45,14 @@ namespace internal { // Structure defining a linear least squares problem and if possible // ground truth solutions. To be used by various LinearSolver tests. struct CERES_NO_EXPORT LinearLeastSquaresProblem { - LinearLeastSquaresProblem() : num_eliminate_blocks(0) {} + LinearLeastSquaresProblem() = default; std::unique_ptr A; std::unique_ptr b; std::unique_ptr D; // If using the schur eliminator then how many of the variable // blocks are e_type blocks. - int num_eliminate_blocks; + int num_eliminate_blocks{0}; // Solution to min_x |Ax - b|^2 std::unique_ptr x; diff --git a/internal/ceres/linear_solver.h b/internal/ceres/linear_solver.h index 962208cda..2f709c297 100644 --- a/internal/ceres/linear_solver.h +++ b/internal/ceres/linear_solver.h @@ -286,7 +286,7 @@ class CERES_NO_EXPORT LinearSolver { // issues. Further, this calls are not expected to be frequent or // performance sensitive. virtual std::map Statistics() const { - return std::map(); + return {}; } // Factory diff --git a/internal/ceres/local_parameterization.cc b/internal/ceres/local_parameterization.cc index 33e2aa0eb..db6f95a19 100644 --- a/internal/ceres/local_parameterization.cc +++ b/internal/ceres/local_parameterization.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -107,8 +107,8 @@ SubsetParameterization::SubsetParameterization( << "of the parameter block."; CHECK(std::adjacent_find(constant.begin(), constant.end()) == constant.end()) << "The set of constant parameters cannot contain duplicates"; - for (int i = 0; i < constant_parameters.size(); ++i) { - constancy_mask_[constant_parameters[i]] = 1; + for (int parameter : constant_parameters) { + constancy_mask_[parameter] = 1; } } diff --git a/internal/ceres/local_parameterization_test.cc b/internal/ceres/local_parameterization_test.cc index cb2ad7462..38dbe2bae 100644 --- a/internal/ceres/local_parameterization_test.cc +++ b/internal/ceres/local_parameterization_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -332,8 +332,8 @@ TEST(QuaternionParameterization, NearZeroTest) { Normalize<4>(x); double delta[3] = {0.24, 0.15, 0.10}; - for (int i = 0; i < 3; ++i) { - delta[i] = delta[i] * 1e-14; + for (double& delta_i : delta) { + delta_i = delta_i * 1e-14; } double q_delta[4]; @@ -411,8 +411,8 @@ TEST(EigenQuaternionParameterization, NearZeroTest) { x.normalize(); double delta[3] = {0.24, 0.15, 0.10}; - for (int i = 0; i < 3; ++i) { - delta[i] = delta[i] * 1e-14; + for (double& delta_i : delta) { + delta_i = delta_i * 1e-14; } // Note: w is first in the constructor. diff --git a/internal/ceres/low_rank_inverse_hessian.cc b/internal/ceres/low_rank_inverse_hessian.cc index c73e5dbf8..2fd1ac83f 100644 --- a/internal/ceres/low_rank_inverse_hessian.cc +++ b/internal/ceres/low_rank_inverse_hessian.cc @@ -127,9 +127,7 @@ void LowRankInverseHessian::RightMultiply(const double* x_ptr, const int num_corrections = indices_.size(); Vector alpha(num_corrections); - for (list::const_reverse_iterator it = indices_.rbegin(); - it != indices_.rend(); - ++it) { + for (auto it = indices_.rbegin(); it != indices_.rend(); ++it) { const double alpha_i = delta_x_history_.col(*it).dot(search_direction) / delta_x_dot_delta_gradient_(*it); search_direction -= alpha_i * delta_gradient_history_.col(*it); diff --git a/internal/ceres/manifold_adapter.h b/internal/ceres/manifold_adapter.h index 552e650d8..9a21456a7 100644 --- a/internal/ceres/manifold_adapter.h +++ b/internal/ceres/manifold_adapter.h @@ -11,7 +11,7 @@ namespace internal { // ManifoldAdapter NEVER takes ownership of local_parameterization. class CERES_NO_EXPORT ManifoldAdapter final : public Manifold { public: - ManifoldAdapter(const LocalParameterization* local_parameterization) + explicit ManifoldAdapter(const LocalParameterization* local_parameterization) : local_parameterization_(local_parameterization) { CHECK(local_parameterization != nullptr); } diff --git a/internal/ceres/map_util.h b/internal/ceres/map_util.h index bb6241da5..5632c22e9 100644 --- a/internal/ceres/map_util.h +++ b/internal/ceres/map_util.h @@ -121,7 +121,7 @@ template void InsertOrDie(Collection* const collection, const typename Collection::value_type::first_type& key, const typename Collection::value_type::second_type& data) { - typedef typename Collection::value_type value_type; + using value_type = typename Collection::value_type; CHECK(collection->insert(value_type(key, data)).second) << "duplicate key: " << key; } diff --git a/internal/ceres/miniglog/glog/logging.h b/internal/ceres/miniglog/glog/logging.h index 28010ece0..f03c9140f 100644 --- a/internal/ceres/miniglog/glog/logging.h +++ b/internal/ceres/miniglog/glog/logging.h @@ -120,7 +120,7 @@ const int INFO = 0; namespace google { -typedef int LogSeverity; +using LogSeverity = int; // clang-format off const int INFO = ::INFO; const int WARNING = ::WARNING; diff --git a/internal/ceres/normal_prior.cc b/internal/ceres/normal_prior.cc index 473d05ceb..17de40f2e 100644 --- a/internal/ceres/normal_prior.cc +++ b/internal/ceres/normal_prior.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without diff --git a/internal/ceres/normal_prior_test.cc b/internal/ceres/normal_prior_test.cc index 39a7eb6b1..c66f9d66e 100644 --- a/internal/ceres/normal_prior_test.cc +++ b/internal/ceres/normal_prior_test.cc @@ -66,10 +66,10 @@ TEST(NormalPriorTest, ResidualAtRandomPosition) { Matrix A(num_rows, num_cols); RandomMatrix(&A); - double* x = new double[num_cols]; + auto* x = new double[num_cols]; for (int i = 0; i < num_cols; ++i) x[i] = 2 * RandDouble() - 1; - double* jacobian = new double[num_rows * num_cols]; + auto* jacobian = new double[num_rows * num_cols]; Vector residuals(num_rows); NormalPrior prior(A, b); @@ -102,7 +102,7 @@ TEST(NormalPriorTest, ResidualAtRandomPositionNullJacobians) { Matrix A(num_rows, num_cols); RandomMatrix(&A); - double* x = new double[num_cols]; + auto* x = new double[num_cols]; for (int i = 0; i < num_cols; ++i) x[i] = 2 * RandDouble() - 1; double* jacobians[1]; diff --git a/internal/ceres/numeric_diff_test_utils.cc b/internal/ceres/numeric_diff_test_utils.cc index d833bbb88..95fde0bbe 100644 --- a/internal/ceres/numeric_diff_test_utils.cc +++ b/internal/ceres/numeric_diff_test_utils.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -149,9 +149,9 @@ void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( }; // clang-format on - for (int k = 0; k < kTests.size(); ++k) { - double* x1 = &(kTests[k].x1[0]); - double* x2 = &(kTests[k].x2[0]); + for (auto& test : kTests) { + double* x1 = &(test.x1[0]); + double* x2 = &(test.x2[0]); double* parameters[] = {x1, x2}; double dydx1[10]; @@ -207,8 +207,8 @@ void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( // Minimal tolerance w.r.t. the cost function and the tests. const double kTolerance = 2e-14; - for (int k = 0; k < kTests.size(); ++k) { - double* parameters[] = {&kTests[k]}; + for (double& test : kTests) { + double* parameters[] = {&test}; double dydx; double* jacobians[1] = {&dydx}; double residual; @@ -216,7 +216,7 @@ void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( ASSERT_TRUE( cost_function.Evaluate(¶meters[0], &residual, &jacobians[0])); - double expected_result = exp(kTests[k]); + double expected_result = exp(test); // Expect residual to be close to exp(x). ExpectClose(residual, expected_result, kTolerance); @@ -248,8 +248,8 @@ void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( // Initialize random number generator with given seed. srand(random_seed_); - for (int k = 0; k < kTests.size(); ++k) { - double* parameters[] = {&kTests[k]}; + for (double& test : kTests) { + double* parameters[] = {&test}; double dydx; double* jacobians[1] = {&dydx}; double residual; @@ -258,10 +258,10 @@ void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( cost_function.Evaluate(¶meters[0], &residual, &jacobians[0])); // Expect residual to be close to x^2 w.r.t. noise factor. - ExpectClose(residual, kTests[k] * kTests[k], noise_factor_); + ExpectClose(residual, test * test, noise_factor_); // Check evaluated differences. (dy/dx = ~2x) - ExpectClose(dydx, 2 * kTests[k], kTolerance); + ExpectClose(dydx, 2 * test, kTolerance); } } diff --git a/internal/ceres/parameter_block.h b/internal/ceres/parameter_block.h index ff238fb5d..a9845a3a9 100644 --- a/internal/ceres/parameter_block.h +++ b/internal/ceres/parameter_block.h @@ -64,7 +64,7 @@ class ResidualBlock; // proper disposal of the manifold. class CERES_NO_EXPORT ParameterBlock { public: - typedef std::unordered_set ResidualBlockSet; + using ResidualBlockSet = std::unordered_set; // Create a parameter block with the user state, size, and index specified. // The size is the size of the parameter block and the index is the position diff --git a/internal/ceres/parameter_block_ordering.cc b/internal/ceres/parameter_block_ordering.cc index 50a30c9d0..570a09c60 100644 --- a/internal/ceres/parameter_block_ordering.cc +++ b/internal/ceres/parameter_block_ordering.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -59,9 +59,9 @@ int ComputeStableSchurOrdering(const Program& program, const vector& parameter_blocks = program.parameter_blocks(); const std::unordered_set& vertices = graph->vertices(); - for (int i = 0; i < parameter_blocks.size(); ++i) { - if (vertices.count(parameter_blocks[i]) > 0) { - ordering->push_back(parameter_blocks[i]); + for (auto* parameter_block : parameter_blocks) { + if (vertices.count(parameter_block) > 0) { + ordering->push_back(parameter_block); } } event_logger.AddEvent("Preordering"); @@ -70,8 +70,7 @@ int ComputeStableSchurOrdering(const Program& program, event_logger.AddEvent("StableIndependentSet"); // Add the excluded blocks to back of the ordering vector. - for (int i = 0; i < parameter_blocks.size(); ++i) { - ParameterBlock* parameter_block = parameter_blocks[i]; + for (auto* parameter_block : parameter_blocks) { if (parameter_block->IsConstant()) { ordering->push_back(parameter_block); } @@ -91,8 +90,7 @@ int ComputeSchurOrdering(const Program& program, const vector& parameter_blocks = program.parameter_blocks(); // Add the excluded blocks to back of the ordering vector. - for (int i = 0; i < parameter_blocks.size(); ++i) { - ParameterBlock* parameter_block = parameter_blocks[i]; + for (auto* parameter_block : parameter_blocks) { if (parameter_block->IsConstant()) { ordering->push_back(parameter_block); } @@ -129,16 +127,14 @@ std::unique_ptr> CreateHessianGraph( auto graph = std::make_unique>(); CHECK(graph != nullptr); const vector& parameter_blocks = program.parameter_blocks(); - for (int i = 0; i < parameter_blocks.size(); ++i) { - ParameterBlock* parameter_block = parameter_blocks[i]; + for (auto* parameter_block : parameter_blocks) { if (!parameter_block->IsConstant()) { graph->AddVertex(parameter_block); } } const vector& residual_blocks = program.residual_blocks(); - for (int i = 0; i < residual_blocks.size(); ++i) { - const ResidualBlock* residual_block = residual_blocks[i]; + for (auto* residual_block : residual_blocks) { const int num_parameter_blocks = residual_block->NumParameterBlocks(); ParameterBlock* const* parameter_blocks = residual_block->parameter_blocks(); diff --git a/internal/ceres/parameter_block_ordering_test.cc b/internal/ceres/parameter_block_ordering_test.cc index a64f81bfa..00ae5f1fc 100644 --- a/internal/ceres/parameter_block_ordering_test.cc +++ b/internal/ceres/parameter_block_ordering_test.cc @@ -46,7 +46,7 @@ namespace ceres { namespace internal { -typedef std::unordered_set VertexSet; +using VertexSet = std::unordered_set; template class DummyCostFunction : public SizedCostFunction { diff --git a/internal/ceres/parameter_block_test.cc b/internal/ceres/parameter_block_test.cc index a532a9a97..53786da81 100644 --- a/internal/ceres/parameter_block_test.cc +++ b/internal/ceres/parameter_block_test.cc @@ -164,7 +164,7 @@ TEST(ParameterBlock, PlusWithNoManifold) { // Stops computing the plus_jacobian after the first time. class BadManifold : public Manifold { public: - BadManifold() : calls_(0) {} + BadManifold() = default; bool Plus(const double* x, const double* delta, @@ -195,7 +195,7 @@ class BadManifold : public Manifold { int TangentSize() const final { return 1; } private: - mutable int calls_; + mutable int calls_{0}; }; TEST(ParameterBlock, DetectBadManifold) { diff --git a/internal/ceres/partitioned_matrix_view_impl.h b/internal/ceres/partitioned_matrix_view_impl.h index c66c4d767..2e818caa6 100644 --- a/internal/ceres/partitioned_matrix_view_impl.h +++ b/internal/ceres/partitioned_matrix_view_impl.h @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -58,8 +58,8 @@ PartitionedMatrixView:: // e_blocks. For a definition of what an e_block is, please see // explicit_schur_complement_solver.h num_row_blocks_e_ = 0; - for (int r = 0; r < bs->rows.size(); ++r) { - const std::vector& cells = bs->rows[r].cells; + for (const auto& row : bs->rows) { + const std::vector& cells = row.cells; if (cells[0].block_id < num_col_blocks_e_) { ++num_row_blocks_e_; } @@ -142,13 +142,13 @@ void PartitionedMatrixView:: const int row_block_pos = bs->rows[r].block.position; const int row_block_size = bs->rows[r].block.size; const std::vector& cells = bs->rows[r].cells; - for (int c = 0; c < cells.size(); ++c) { - const int col_block_id = cells[c].block_id; + for (const auto& cell : cells) { + const int col_block_id = cell.block_id; const int col_block_pos = bs->cols[col_block_id].position; const int col_block_size = bs->cols[col_block_id].size; // clang-format off MatrixVectorMultiply( - values + cells[c].position, row_block_size, col_block_size, + values + cell.position, row_block_size, col_block_size, x + col_block_pos - num_cols_e_, y + row_block_pos); // clang-format on @@ -212,13 +212,13 @@ void PartitionedMatrixView:: const int row_block_pos = bs->rows[r].block.position; const int row_block_size = bs->rows[r].block.size; const std::vector& cells = bs->rows[r].cells; - for (int c = 0; c < cells.size(); ++c) { - const int col_block_id = cells[c].block_id; + for (const auto& cell : cells) { + const int col_block_id = cell.block_id; const int col_block_pos = bs->cols[col_block_id].position; const int col_block_size = bs->cols[col_block_id].size; // clang-format off MatrixTransposeVectorMultiply( - values + cells[c].position, row_block_size, col_block_size, + values + cell.position, row_block_size, col_block_size, x + row_block_pos, y + col_block_pos - num_cols_e_); // clang-format on @@ -237,8 +237,7 @@ PartitionedMatrixView:: CreateBlockDiagonalMatrixLayout(int start_col_block, int end_col_block) const { const CompressedRowBlockStructure* bs = matrix_.block_structure(); - CompressedRowBlockStructure* block_diagonal_structure = - new CompressedRowBlockStructure; + auto* block_diagonal_structure = new CompressedRowBlockStructure; int block_position = 0; int diagonal_cell_position = 0; @@ -247,16 +246,16 @@ PartitionedMatrixView:: // each column block. for (int c = start_col_block; c < end_col_block; ++c) { const Block& block = bs->cols[c]; - block_diagonal_structure->cols.push_back(Block()); + block_diagonal_structure->cols.emplace_back(); Block& diagonal_block = block_diagonal_structure->cols.back(); diagonal_block.size = block.size; diagonal_block.position = block_position; - block_diagonal_structure->rows.push_back(CompressedRow()); + block_diagonal_structure->rows.emplace_back(); CompressedRow& row = block_diagonal_structure->rows.back(); row.block = diagonal_block; - row.cells.push_back(Cell()); + row.cells.emplace_back(); Cell& cell = row.cells.back(); cell.block_id = c - start_col_block; cell.position = diagonal_cell_position; @@ -362,8 +361,8 @@ void PartitionedMatrixView:: for (int r = num_row_blocks_e_; r < bs->rows.size(); ++r) { const int row_block_size = bs->rows[r].block.size; const std::vector& cells = bs->rows[r].cells; - for (int c = 0; c < cells.size(); ++c) { - const int col_block_id = cells[c].block_id; + for (const auto& cell : cells) { + const int col_block_id = cell.block_id; const int col_block_size = bs->cols[col_block_id].size; const int diagonal_block_id = col_block_id - num_col_blocks_e_; const int cell_position = @@ -372,8 +371,8 @@ void PartitionedMatrixView:: // clang-format off MatrixTransposeMatrixMultiply ( - values + cells[c].position, row_block_size, col_block_size, - values + cells[c].position, row_block_size, col_block_size, + values + cell.position, row_block_size, col_block_size, + values + cell.position, row_block_size, col_block_size, block_diagonal->mutable_values() + cell_position, 0, 0, col_block_size, col_block_size); // clang-format on diff --git a/internal/ceres/polynomial.cc b/internal/ceres/polynomial.cc index c263f98fb..96267aae9 100644 --- a/internal/ceres/polynomial.cc +++ b/internal/ceres/polynomial.cc @@ -376,8 +376,7 @@ void MinimizeInterpolatingPolynomial(const vector& samples, double* optimal_value) { const Vector polynomial = FindInterpolatingPolynomial(samples); MinimizePolynomial(polynomial, x_min, x_max, optimal_x, optimal_value); - for (int i = 0; i < samples.size(); ++i) { - const FunctionSample& sample = samples[i]; + for (const auto& sample : samples) { if ((sample.x < x_min) || (sample.x > x_max)) { continue; } diff --git a/internal/ceres/preconditioner.h b/internal/ceres/preconditioner.h index 7e6314730..6433cc7dd 100644 --- a/internal/ceres/preconditioner.h +++ b/internal/ceres/preconditioner.h @@ -161,9 +161,9 @@ class CERES_NO_EXPORT TypedPreconditioner : public Preconditioner { // Preconditioners that depend on access to the low level structure // of a SparseMatrix. // clang-format off -typedef TypedPreconditioner SparseMatrixPreconditioner; -typedef TypedPreconditioner BlockSparseMatrixPreconditioner; -typedef TypedPreconditioner CompressedRowSparseMatrixPreconditioner; +using SparseMatrixPreconditioner = TypedPreconditioner; +using BlockSparseMatrixPreconditioner = TypedPreconditioner; +using CompressedRowSparseMatrixPreconditioner = TypedPreconditioner; // clang-format on // Wrap a SparseMatrix object as a preconditioner. diff --git a/internal/ceres/preprocessor.h b/internal/ceres/preprocessor.h index 8b99dd54e..b5db80af7 100644 --- a/internal/ceres/preprocessor.h +++ b/internal/ceres/preprocessor.h @@ -81,7 +81,7 @@ class CERES_NO_EXPORT Preprocessor { // A PreprocessedProblem is the result of running the Preprocessor on // a Problem and Solver::Options object. struct CERES_NO_EXPORT PreprocessedProblem { - PreprocessedProblem() : fixed_cost(0.0) {} + PreprocessedProblem() = default; std::string error; Solver::Options options; @@ -101,7 +101,7 @@ struct CERES_NO_EXPORT PreprocessedProblem { std::vector removed_parameter_blocks; Vector reduced_parameters; - double fixed_cost; + double fixed_cost{0.0}; }; // Common functions used by various preprocessors. diff --git a/internal/ceres/problem_impl.cc b/internal/ceres/problem_impl.cc index 27ada0ff0..01a22c128 100644 --- a/internal/ceres/problem_impl.cc +++ b/internal/ceres/problem_impl.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2021 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -127,7 +127,7 @@ ParameterBlock* ProblemImpl::InternalAddParameterBlock(double* values, << "for a parameter with size " << size; // Ignore the request if there is a block for the given pointer already. - ParameterMap::iterator it = parameter_block_map_.find(values); + auto it = parameter_block_map_.find(values); if (it != parameter_block_map_.end()) { if (!options_.disable_all_safety_checks) { int existing_size = it->second->Size(); @@ -143,11 +143,11 @@ ParameterBlock* ProblemImpl::InternalAddParameterBlock(double* values, // Before adding the parameter block, also check that it doesn't alias any // other parameter blocks. if (!parameter_block_map_.empty()) { - ParameterMap::iterator lb = parameter_block_map_.lower_bound(values); + auto lb = parameter_block_map_.lower_bound(values); // If lb is not the first block, check the previous block for aliasing. if (lb != parameter_block_map_.begin()) { - ParameterMap::iterator previous = lb; + auto previous = lb; --previous; CheckForNoAliasing( previous->first, previous->second->Size(), values, size); @@ -162,7 +162,7 @@ ParameterBlock* ProblemImpl::InternalAddParameterBlock(double* values, // Pass the index of the new parameter block as well to keep the index in // sync with the position of the parameter in the program's parameter vector. - ParameterBlock* new_parameter_block = + auto* new_parameter_block = new ParameterBlock(values, size, program_->parameter_blocks_.size()); // For dynamic problems, add the list of dependent residual blocks, which is @@ -189,7 +189,7 @@ void ProblemImpl::InternalRemoveResidualBlock(ResidualBlock* residual_block) { residual_block); } - ResidualBlockSet::iterator it = residual_block_set_.find(residual_block); + auto it = residual_block_set_.find(residual_block); residual_block_set_.erase(it); } DeleteBlockInVector(program_->mutable_residual_blocks(), residual_block); @@ -204,13 +204,13 @@ void ProblemImpl::DeleteBlock(ResidualBlock* residual_block) { // The const casts here are legit, since ResidualBlock holds these // pointers as const pointers but we have ownership of them and // have the right to destroy them when the destructor is called. - CostFunction* cost_function = + auto* cost_function = const_cast(residual_block->cost_function()); if (options_.cost_function_ownership == TAKE_OWNERSHIP) { DecrementValueOrDeleteKey(cost_function, &cost_function_ref_count_); } - LossFunction* loss_function = + auto* loss_function = const_cast(residual_block->loss_function()); if (options_.loss_function_ownership == TAKE_OWNERSHIP && loss_function != nullptr) { @@ -253,8 +253,8 @@ ProblemImpl::~ProblemImpl() { } // Collect the unique parameterizations and delete the parameters. - for (int i = 0; i < program_->parameter_blocks_.size(); ++i) { - DeleteBlock(program_->parameter_blocks_[i]); + for (auto* parameter_block : program_->parameter_blocks_) { + DeleteBlock(parameter_block); } // Delete the owned parameterizations. @@ -327,7 +327,7 @@ ResidualBlockId ProblemImpl::AddResidualBlock( } } - ResidualBlock* new_residual_block = + auto* new_residual_block = new ResidualBlock(cost_function, loss_function, parameter_block_ptrs, @@ -483,8 +483,8 @@ void ProblemImpl::RemoveParameterBlock(const double* values) { std::vector residual_blocks_to_remove( parameter_block->mutable_residual_blocks()->begin(), parameter_block->mutable_residual_blocks()->end()); - for (int i = 0; i < residual_blocks_to_remove.size(); ++i) { - InternalRemoveResidualBlock(residual_blocks_to_remove[i]); + for (auto* residual_block : residual_blocks_to_remove) { + InternalRemoveResidualBlock(residual_block); } } else { // Scan all the residual blocks to remove ones that depend on the parameter @@ -710,8 +710,7 @@ bool ProblemImpl::Evaluate(const Problem::EvaluateOptions& evaluate_options, back_inserter(excluded_parameter_blocks)); variable_parameter_blocks.reserve(excluded_parameter_blocks.size()); - for (int i = 0; i < excluded_parameter_blocks.size(); ++i) { - ParameterBlock* parameter_block = excluded_parameter_blocks[i]; + for (auto* parameter_block : excluded_parameter_blocks) { if (!parameter_block->IsConstant()) { variable_parameter_blocks.push_back(parameter_block); parameter_block->SetConstant(); @@ -796,8 +795,8 @@ bool ProblemImpl::Evaluate(const Problem::EvaluateOptions& evaluate_options, // Make the parameter blocks that were temporarily marked constant, // variable again. - for (int i = 0; i < variable_parameter_blocks.size(); ++i) { - variable_parameter_blocks[i]->SetVarying(); + for (auto* parameter_block : variable_parameter_blocks) { + parameter_block->SetVarying(); } if (status) { diff --git a/internal/ceres/problem_impl.h b/internal/ceres/problem_impl.h index 921bb4e8b..22073b674 100644 --- a/internal/ceres/problem_impl.h +++ b/internal/ceres/problem_impl.h @@ -69,10 +69,10 @@ class ResidualBlock; class CERES_NO_EXPORT ProblemImpl { public: - typedef std::map ParameterMap; - typedef std::unordered_set ResidualBlockSet; - typedef std::map CostFunctionRefCount; - typedef std::map LossFunctionRefCount; + using ParameterMap = std::map; + using ResidualBlockSet = std::unordered_set; + using CostFunctionRefCount = std::map; + using LossFunctionRefCount = std::map; ProblemImpl(); explicit ProblemImpl(const Problem::Options& options); diff --git a/internal/ceres/problem_test.cc b/internal/ceres/problem_test.cc index 9a730345d..641dad917 100644 --- a/internal/ceres/problem_test.cc +++ b/internal/ceres/problem_test.cc @@ -1293,8 +1293,7 @@ TEST_P(DynamicProblem, RemoveInvalidResidualBlockDies) { // Attempt to remove a cast pointer never added as a residual. int trash_memory = 1234; - ResidualBlock* invalid_residual = - reinterpret_cast(&trash_memory); + auto* invalid_residual = reinterpret_cast(&trash_memory); EXPECT_DEATH_IF_SUPPORTED(problem->RemoveResidualBlock(invalid_residual), "not found"); @@ -1363,40 +1362,40 @@ TEST_P(DynamicProblem, GetXXXBlocksForYYYBlock) { ResidualBlock* r_yzw = problem->AddResidualBlock(cost_yzw, nullptr, y, z, w); { - ResidualBlockId expected_residuals[] = {r_yzw, 0}; + ResidualBlockId expected_residuals[] = {r_yzw, nullptr}; ExpectProblemHasResidualBlocks(*problem, expected_residuals); } ResidualBlock* r_yz = problem->AddResidualBlock(cost_yz, nullptr, y, z); { - ResidualBlockId expected_residuals[] = {r_yzw, r_yz, 0}; + ResidualBlockId expected_residuals[] = {r_yzw, r_yz, nullptr}; ExpectProblemHasResidualBlocks(*problem, expected_residuals); } ResidualBlock* r_yw = problem->AddResidualBlock(cost_yw, nullptr, y, w); { - ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, 0}; + ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, nullptr}; ExpectProblemHasResidualBlocks(*problem, expected_residuals); } ResidualBlock* r_zw = problem->AddResidualBlock(cost_zw, nullptr, z, w); { - ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, r_zw, 0}; + ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, r_zw, nullptr}; ExpectProblemHasResidualBlocks(*problem, expected_residuals); } ResidualBlock* r_y = problem->AddResidualBlock(cost_y, nullptr, y); { - ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, r_zw, r_y, 0}; + ResidualBlock *expected_residuals[] = {r_yzw, r_yz, r_yw, r_zw, r_y, nullptr}; ExpectProblemHasResidualBlocks(*problem, expected_residuals); } ResidualBlock* r_z = problem->AddResidualBlock(cost_z, nullptr, z); { ResidualBlock *expected_residuals[] = { - r_yzw, r_yz, r_yw, r_zw, r_y, r_z, 0 + r_yzw, r_yz, r_yw, r_zw, r_y, r_z, nullptr }; ExpectProblemHasResidualBlocks(*problem, expected_residuals); } ResidualBlock* r_w = problem->AddResidualBlock(cost_w, nullptr, w); { ResidualBlock *expected_residuals[] = { - r_yzw, r_yz, r_yw, r_zw, r_y, r_z, r_w, 0 + r_yzw, r_yz, r_yw, r_zw, r_y, r_z, r_w, nullptr }; ExpectProblemHasResidualBlocks(*problem, expected_residuals); } diff --git a/internal/ceres/program.cc b/internal/ceres/program.cc index d9e6db248..66dc90b84 100644 --- a/internal/ceres/program.cc +++ b/internal/ceres/program.cc @@ -76,33 +76,32 @@ EvaluationCallback* Program::mutable_evaluation_callback() { } bool Program::StateVectorToParameterBlocks(const double* state) { - for (int i = 0; i < parameter_blocks_.size(); ++i) { - if (!parameter_blocks_[i]->IsConstant() && - !parameter_blocks_[i]->SetState(state)) { + for (auto* parameter_block : parameter_blocks_) { + if (!parameter_block->IsConstant() && !parameter_block->SetState(state)) { return false; } - state += parameter_blocks_[i]->Size(); + state += parameter_block->Size(); } return true; } void Program::ParameterBlocksToStateVector(double* state) const { - for (int i = 0; i < parameter_blocks_.size(); ++i) { - parameter_blocks_[i]->GetState(state); - state += parameter_blocks_[i]->Size(); + for (auto* parameter_block : parameter_blocks_) { + parameter_block->GetState(state); + state += parameter_block->Size(); } } void Program::CopyParameterBlockStateToUserState() { - for (int i = 0; i < parameter_blocks_.size(); ++i) { - parameter_blocks_[i]->GetState(parameter_blocks_[i]->mutable_user_state()); + for (auto* parameter_block : parameter_blocks_) { + parameter_block->GetState(parameter_block->mutable_user_state()); } } bool Program::SetParameterBlockStatePtrsToUserStatePtrs() { - for (int i = 0; i < parameter_blocks_.size(); ++i) { - if (!parameter_blocks_[i]->IsConstant() && - !parameter_blocks_[i]->SetState(parameter_blocks_[i]->user_state())) { + for (auto* parameter_block : parameter_blocks_) { + if (!parameter_block->IsConstant() && + !parameter_block->SetState(parameter_block->user_state())) { return false; } } @@ -112,13 +111,13 @@ bool Program::SetParameterBlockStatePtrsToUserStatePtrs() { bool Program::Plus(const double* state, const double* delta, double* state_plus_delta) const { - for (int i = 0; i < parameter_blocks_.size(); ++i) { - if (!parameter_blocks_[i]->Plus(state, delta, state_plus_delta)) { + for (auto* parameter_block : parameter_blocks_) { + if (!parameter_block->Plus(state, delta, state_plus_delta)) { return false; } - state += parameter_blocks_[i]->Size(); - delta += parameter_blocks_[i]->TangentSize(); - state_plus_delta += parameter_blocks_[i]->Size(); + state += parameter_block->Size(); + delta += parameter_block->TangentSize(); + state_plus_delta += parameter_block->Size(); } return true; } @@ -126,8 +125,7 @@ bool Program::Plus(const double* state, void Program::SetParameterOffsetsAndIndex() { // Set positions for all parameters appearing as arguments to residuals to one // past the end of the parameter block array. - for (int i = 0; i < residual_blocks_.size(); ++i) { - ResidualBlock* residual_block = residual_blocks_[i]; + for (auto* residual_block : residual_blocks_) { for (int j = 0; j < residual_block->NumParameterBlocks(); ++j) { residual_block->parameter_blocks()[j]->set_index(-1); } @@ -176,8 +174,7 @@ bool Program::IsValid() const { bool Program::ParameterBlocksAreFinite(std::string* message) const { CHECK(message != nullptr); - for (int i = 0; i < parameter_blocks_.size(); ++i) { - const ParameterBlock* parameter_block = parameter_blocks_[i]; + for (auto* parameter_block : parameter_blocks_) { const double* array = parameter_block->user_state(); const int size = parameter_block->Size(); const int invalid_index = FindInvalidValue(size, array); @@ -197,8 +194,7 @@ bool Program::ParameterBlocksAreFinite(std::string* message) const { } bool Program::IsBoundsConstrained() const { - for (int i = 0; i < parameter_blocks_.size(); ++i) { - const ParameterBlock* parameter_block = parameter_blocks_[i]; + for (auto* parameter_block : parameter_blocks_) { if (parameter_block->IsConstant()) { continue; } @@ -217,8 +213,7 @@ bool Program::IsBoundsConstrained() const { bool Program::IsFeasible(std::string* message) const { CHECK(message != nullptr); - for (int i = 0; i < parameter_blocks_.size(); ++i) { - const ParameterBlock* parameter_block = parameter_blocks_[i]; + for (auto* parameter_block : parameter_blocks_) { const double* parameters = parameter_block->user_state(); const int size = parameter_block->Size(); if (parameter_block->IsConstant()) { @@ -308,8 +303,8 @@ bool Program::RemoveFixedBlocks(std::vector* removed_parameter_blocks, // Mark all the parameters as unused. Abuse the index member of the // parameter blocks for the marking. - for (int i = 0; i < parameter_blocks_.size(); ++i) { - parameter_blocks_[i]->set_index(-1); + for (auto* parameter_block : parameter_blocks_) { + parameter_block->set_index(-1); } // Filter out residual that have all-constant parameters, and mark @@ -381,8 +376,7 @@ bool Program::RemoveFixedBlocks(std::vector* removed_parameter_blocks, // Filter out unused or fixed parameter blocks. int num_active_parameter_blocks = 0; removed_parameter_blocks->clear(); - for (int i = 0; i < parameter_blocks_.size(); ++i) { - ParameterBlock* parameter_block = parameter_blocks_[i]; + for (auto* parameter_block : parameter_blocks_) { if (parameter_block->index() == -1) { removed_parameter_blocks->push_back( parameter_block->mutable_user_state()); @@ -473,24 +467,24 @@ int Program::NumParameterBlocks() const { return parameter_blocks_.size(); } int Program::NumResiduals() const { int num_residuals = 0; - for (int i = 0; i < residual_blocks_.size(); ++i) { - num_residuals += residual_blocks_[i]->NumResiduals(); + for (auto* residual_block : residual_blocks_) { + num_residuals += residual_block->NumResiduals(); } return num_residuals; } int Program::NumParameters() const { int num_parameters = 0; - for (int i = 0; i < parameter_blocks_.size(); ++i) { - num_parameters += parameter_blocks_[i]->Size(); + for (auto* parameter_block : parameter_blocks_) { + num_parameters += parameter_block->Size(); } return num_parameters; } int Program::NumEffectiveParameters() const { int num_parameters = 0; - for (int i = 0; i < parameter_blocks_.size(); ++i) { - num_parameters += parameter_blocks_[i]->TangentSize(); + for (auto* parameter_block : parameter_blocks_) { + num_parameters += parameter_block->TangentSize(); } return num_parameters; } @@ -501,19 +495,18 @@ int Program::NumEffectiveParameters() const { int Program::MaxScratchDoublesNeededForEvaluate() const { // Compute the scratch space needed for evaluate. int max_scratch_bytes_for_evaluate = 0; - for (int i = 0; i < residual_blocks_.size(); ++i) { + for (auto* residual_block : residual_blocks_) { max_scratch_bytes_for_evaluate = std::max(max_scratch_bytes_for_evaluate, - residual_blocks_[i]->NumScratchDoublesForEvaluate()); + residual_block->NumScratchDoublesForEvaluate()); } return max_scratch_bytes_for_evaluate; } int Program::MaxDerivativesPerResidualBlock() const { int max_derivatives = 0; - for (int i = 0; i < residual_blocks_.size(); ++i) { + for (auto* residual_block : residual_blocks_) { int derivatives = 0; - ResidualBlock* residual_block = residual_blocks_[i]; int num_parameters = residual_block->NumParameterBlocks(); for (int j = 0; j < num_parameters; ++j) { derivatives += residual_block->NumResiduals() * @@ -526,18 +519,17 @@ int Program::MaxDerivativesPerResidualBlock() const { int Program::MaxParametersPerResidualBlock() const { int max_parameters = 0; - for (int i = 0; i < residual_blocks_.size(); ++i) { + for (auto* residual_block : residual_blocks_) { max_parameters = - std::max(max_parameters, residual_blocks_[i]->NumParameterBlocks()); + std::max(max_parameters, residual_block->NumParameterBlocks()); } return max_parameters; } int Program::MaxResidualsPerResidualBlock() const { int max_residuals = 0; - for (int i = 0; i < residual_blocks_.size(); ++i) { - max_residuals = - std::max(max_residuals, residual_blocks_[i]->NumResiduals()); + for (auto* residual_block : residual_blocks_) { + max_residuals = std::max(max_residuals, residual_block->NumResiduals()); } return max_residuals; } diff --git a/internal/ceres/random.h b/internal/ceres/random.h index 14f2ebdce..0495d6758 100644 --- a/internal/ceres/random.h +++ b/internal/ceres/random.h @@ -50,7 +50,7 @@ inline int Uniform(int n) { } inline double RandDouble() { - double r = static_cast(rand()); + auto r = static_cast(rand()); return r / RAND_MAX; } diff --git a/internal/ceres/reorder_program.cc b/internal/ceres/reorder_program.cc index 31d9ae0fe..488ced01d 100644 --- a/internal/ceres/reorder_program.cc +++ b/internal/ceres/reorder_program.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -88,8 +88,8 @@ static int MinParameterBlock(const ResidualBlock* residual_block, #if defined(CERES_USE_EIGEN_SPARSE) Eigen::SparseMatrix CreateBlockJacobian( const TripletSparseMatrix& block_jacobian_transpose) { - typedef Eigen::SparseMatrix SparseMatrix; - typedef Eigen::Triplet Triplet; + using SparseMatrix = Eigen::SparseMatrix; + using Triplet = Eigen::Triplet; const int* rows = block_jacobian_transpose.rows(); const int* cols = block_jacobian_transpose.cols(); @@ -97,7 +97,7 @@ Eigen::SparseMatrix CreateBlockJacobian( vector triplets; triplets.reserve(num_nonzeros); for (int i = 0; i < num_nonzeros; ++i) { - triplets.push_back(Triplet(cols[i], rows[i], 1)); + triplets.emplace_back(cols[i], rows[i], 1); } SparseMatrix block_jacobian(block_jacobian_transpose.num_cols(), @@ -127,9 +127,9 @@ void OrderingForSparseNormalCholeskyUsingSuiteSparse( ss.ApproximateMinimumDegreeOrdering(block_jacobian_transpose, &ordering[0]); } else { vector constraints; - for (int i = 0; i < parameter_blocks.size(); ++i) { + for (auto* parameter_block : parameter_blocks) { constraints.push_back(parameter_block_ordering.GroupId( - parameter_blocks[i]->mutable_user_state())); + parameter_block->mutable_user_state())); } // Renumber the entries of constraints to be contiguous integers @@ -188,7 +188,7 @@ void OrderingForSparseNormalCholeskyUsingEigenSparse( // things. The right thing to do here would be to get a compressed // row sparse matrix representation of the jacobian and go from // there. But that is a project for another day. - typedef Eigen::SparseMatrix SparseMatrix; + using SparseMatrix = Eigen::SparseMatrix; const SparseMatrix block_jacobian = CreateBlockJacobian(tsm_block_jacobian_transpose); @@ -314,8 +314,8 @@ bool LexicographicallyOrderResidualBlocks( << "to the developers."; } // Sanity check #2: No nullptr's left behind. - for (int i = 0; i < reordered_residual_blocks.size(); ++i) { - CHECK(reordered_residual_blocks[i] != nullptr) + for (auto* residual_block : reordered_residual_blocks) { + CHECK(residual_block != nullptr) << "Congratulations, you found a Ceres bug! Please report this error " << "to the developers."; } @@ -339,9 +339,9 @@ static void MaybeReorderSchurComplementColumnsUsingSuiteSparse( vector& parameter_blocks = *(program->mutable_parameter_blocks()); - for (int i = 0; i < parameter_blocks.size(); ++i) { + for (auto* parameter_block : parameter_blocks) { constraints.push_back(parameter_block_ordering.GroupId( - parameter_blocks[i]->mutable_user_state())); + parameter_block->mutable_user_state())); } // Renumber the entries of constraints to be contiguous integers as @@ -378,7 +378,7 @@ static void MaybeReorderSchurComplementColumnsUsingEigen( std::unique_ptr tsm_block_jacobian_transpose( program->CreateJacobianBlockSparsityTranspose()); - typedef Eigen::SparseMatrix SparseMatrix; + using SparseMatrix = Eigen::SparseMatrix; const SparseMatrix block_jacobian = CreateBlockJacobian(*tsm_block_jacobian_transpose); const int num_rows = block_jacobian.rows(); diff --git a/internal/ceres/rotation_test.cc b/internal/ceres/rotation_test.cc index 3184376ad..d97e38588 100644 --- a/internal/ceres/rotation_test.cc +++ b/internal/ceres/rotation_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -326,16 +326,16 @@ TEST(Rotation, AngleAxisToQuaterionAndBack) { // Make an axis by choosing three random numbers in [-1, 1) and // normalizing. double norm = 0; - for (int i = 0; i < 3; i++) { - axis_angle[i] = RandDouble() * 2 - 1; - norm += axis_angle[i] * axis_angle[i]; + for (double& coeff : axis_angle) { + coeff = RandDouble() * 2 - 1; + norm += coeff * coeff; } norm = sqrt(norm); // Angle in [-pi, pi). double theta = kPi * 2 * RandDouble() - kPi; - for (int i = 0; i < 3; i++) { - axis_angle[i] = axis_angle[i] * theta / norm; + for (double& coeff : axis_angle) { + coeff = coeff * theta / norm; } double quaternion[4]; @@ -358,14 +358,14 @@ TEST(Rotation, QuaterionToAngleAxisAndBack) { double quaternion[4]; // Choose four random numbers in [-1, 1) and normalize. double norm = 0; - for (int i = 0; i < 4; i++) { - quaternion[i] = RandDouble() * 2 - 1; - norm += quaternion[i] * quaternion[i]; + for (double& coeff : quaternion) { + coeff = RandDouble() * 2 - 1; + norm += coeff * coeff; } norm = sqrt(norm); - for (int i = 0; i < 4; i++) { - quaternion[i] = quaternion[i] / norm; + for (double& coeff : quaternion) { + coeff = coeff / norm; } double axis_angle[3]; @@ -435,9 +435,9 @@ TEST(Rotation, NearPiAngleAxisRoundTrip) { // Make an axis by choosing three random numbers in [-1, 1) and // normalizing. double norm = 0; - for (int i = 0; i < 3; i++) { - in_axis_angle[i] = RandDouble() * 2 - 1; - norm += in_axis_angle[i] * in_axis_angle[i]; + for (double& coeff : in_axis_angle) { + coeff = RandDouble() * 2 - 1; + norm += coeff * coeff; } norm = sqrt(norm); @@ -445,8 +445,8 @@ TEST(Rotation, NearPiAngleAxisRoundTrip) { const double kMaxSmallAngle = 1e-8; double theta = kPi - kMaxSmallAngle * RandDouble(); - for (int i = 0; i < 3; i++) { - in_axis_angle[i] *= (theta / norm); + for (double& coeff : in_axis_angle) { + coeff *= (theta / norm); } AngleAxisToRotationMatrix(in_axis_angle, matrix); RotationMatrixToAngleAxis(matrix, out_axis_angle); @@ -535,16 +535,16 @@ TEST(Rotation, AngleAxisToRotationMatrixAndBack) { // Make an axis by choosing three random numbers in [-1, 1) and // normalizing. double norm = 0; - for (int i = 0; i < 3; i++) { - axis_angle[i] = RandDouble() * 2 - 1; - norm += axis_angle[i] * axis_angle[i]; + for (double& i : axis_angle) { + i = RandDouble() * 2 - 1; + norm += i * i; } norm = sqrt(norm); // Angle in [-pi, pi). double theta = kPi * 2 * RandDouble() - kPi; - for (int i = 0; i < 3; i++) { - axis_angle[i] = axis_angle[i] * theta / norm; + for (double& i : axis_angle) { + i = i * theta / norm; } double matrix[9]; @@ -568,16 +568,16 @@ TEST(Rotation, AngleAxisToRotationMatrixAndBackNearZero) { // Make an axis by choosing three random numbers in [-1, 1) and // normalizing. double norm = 0; - for (int i = 0; i < 3; i++) { - axis_angle[i] = RandDouble() * 2 - 1; - norm += axis_angle[i] * axis_angle[i]; + for (double& i : axis_angle) { + i = RandDouble() * 2 - 1; + norm += i * i; } norm = sqrt(norm); // Tiny theta. double theta = 1e-16 * (kPi * 2 * RandDouble() - kPi); - for (int i = 0; i < 3; i++) { - axis_angle[i] = axis_angle[i] * theta / norm; + for (double& i : axis_angle) { + i = i * theta / norm; } double matrix[9]; @@ -648,8 +648,8 @@ TEST(EulerAnglesToRotationMatrix, IsOrthonormal) { srand(5); for (int trial = 0; trial < kNumTrials; ++trial) { double euler_angles_degrees[3]; - for (int i = 0; i < 3; ++i) { - euler_angles_degrees[i] = RandDouble() * 360.0 - 180.0; + for (double& euler_angles_degree : euler_angles_degrees) { + euler_angles_degree = RandDouble() * 360.0 - 180.0; } double rotation_matrix[9]; EulerAnglesToRotationMatrix(euler_angles_degrees, 3, rotation_matrix); @@ -660,8 +660,8 @@ TEST(EulerAnglesToRotationMatrix, IsOrthonormal) { // Tests using Jets for specific behavior involving auto differentiation // near singularity points. -typedef Jet J3; -typedef Jet J4; +using J3 = Jet; +using J4 = Jet; namespace { @@ -947,8 +947,8 @@ TEST(AngleAxis, RotatePointGivesSameAnswerAsRotationMatrix) { } const double inv_norm = theta / sqrt(norm2); - for (int k = 0; k < 3; ++k) { - angle_axis[k] *= inv_norm; + for (double& angle_axi : angle_axis) { + angle_axi *= inv_norm; } AngleAxisToRotationMatrix(angle_axis, R); @@ -990,8 +990,8 @@ TEST(AngleAxis, NearZeroRotatePointGivesSameAnswerAsRotationMatrix) { double theta = (2.0 * i * 0.0001 - 1.0) * 1e-16; const double inv_norm = theta / sqrt(norm2); - for (int k = 0; k < 3; ++k) { - angle_axis[k] *= inv_norm; + for (double& angle_axi : angle_axis) { + angle_axi *= inv_norm; } AngleAxisToRotationMatrix(angle_axis, R); diff --git a/internal/ceres/schur_complement_solver.cc b/internal/ceres/schur_complement_solver.cc index 3f8cfea91..bb442b428 100644 --- a/internal/ceres/schur_complement_solver.cc +++ b/internal/ceres/schur_complement_solver.cc @@ -210,8 +210,7 @@ LinearSolver::Summary DenseSchurComplementSolver::SolveReducedLinearSystem( summary.termination_type = LINEAR_SOLVER_SUCCESS; summary.message = "Success."; - BlockRandomAccessDenseMatrix* m = - down_cast(mutable_lhs()); + auto* m = down_cast(mutable_lhs()); const int num_rows = m->num_rows(); // The case where there are no f blocks, and the system is block @@ -294,8 +293,8 @@ void SparseSchurComplementSolver::InitStorage( CHECK_GE(row.cells.front().block_id, num_eliminate_blocks); for (int i = 0; i < row.cells.size(); ++i) { int r_block1_id = row.cells[i].block_id - num_eliminate_blocks; - for (int j = 0; j < row.cells.size(); ++j) { - int r_block2_id = row.cells[j].block_id - num_eliminate_blocks; + for (const auto& cell : row.cells) { + int r_block2_id = cell.block_id - num_eliminate_blocks; if (r_block1_id <= r_block2_id) { block_pairs.insert(make_pair(r_block1_id, r_block2_id)); } @@ -369,8 +368,7 @@ SparseSchurComplementSolver::SolveReducedLinearSystemUsingConjugateGradients( std::make_unique(blocks_); } - BlockRandomAccessSparseMatrix* sc = - down_cast(mutable_lhs()); + auto* sc = down_cast(mutable_lhs()); // Extract block diagonal from the Schur complement to construct the // schur_jacobi preconditioner. diff --git a/internal/ceres/schur_eliminator.h b/internal/ceres/schur_eliminator.h index f77acbdd1..94cd1ccc6 100644 --- a/internal/ceres/schur_eliminator.h +++ b/internal/ceres/schur_eliminator.h @@ -273,9 +273,9 @@ class CERES_NO_EXPORT SchurEliminator final : public SchurEliminatorBase { // buffer_layout[z1] = 0 // buffer_layout[z5] = y1 * z1 // buffer_layout[z2] = y1 * z1 + y1 * z5 - typedef std::map BufferLayoutType; + using BufferLayoutType = std::map; struct Chunk { - Chunk(int start) : size(0), start(start) {} + explicit Chunk(int start) : size(0), start(start) {} int size; int start; BufferLayoutType buffer_layout; diff --git a/internal/ceres/schur_eliminator_benchmark.cc b/internal/ceres/schur_eliminator_benchmark.cc index 9dac07dcd..4e19582d5 100644 --- a/internal/ceres/schur_eliminator_benchmark.cc +++ b/internal/ceres/schur_eliminator_benchmark.cc @@ -48,7 +48,7 @@ constexpr int kFBlockSize = 6; class BenchmarkData { public: explicit BenchmarkData(const int num_e_blocks) { - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; + auto* bs = new CompressedRowBlockStructure; bs->cols.resize(num_e_blocks + 1); int col_pos = 0; for (int i = 0; i < num_e_blocks; ++i) { diff --git a/internal/ceres/schur_eliminator_impl.h b/internal/ceres/schur_eliminator_impl.h index 32344f502..de3ba3e5d 100644 --- a/internal/ceres/schur_eliminator_impl.h +++ b/internal/ceres/schur_eliminator_impl.h @@ -524,7 +524,7 @@ void SchurEliminator:: // computation of the right-hand matrix product, but memory // references to the left hand side. const int e_block_size = inverse_ete.rows(); - BufferLayoutType::const_iterator it1 = buffer_layout.begin(); + auto it1 = buffer_layout.begin(); double* b1_transpose_inverse_ete = chunk_outer_product_buffer_.get() + thread_id * buffer_size_; @@ -541,7 +541,7 @@ void SchurEliminator:: b1_transpose_inverse_ete, 0, 0, block1_size, e_block_size); // clang-format on - BufferLayoutType::const_iterator it2 = it1; + auto it2 = it1; for (; it2 != buffer_layout.end(); ++it2) { const int block2 = it2->first - num_eliminate_blocks_; diff --git a/internal/ceres/schur_eliminator_test.cc b/internal/ceres/schur_eliminator_test.cc index f58647642..0b20d70de 100644 --- a/internal/ceres/schur_eliminator_test.cc +++ b/internal/ceres/schur_eliminator_test.cc @@ -228,7 +228,7 @@ TEST(SchurEliminatorForOneFBlock, MatchesSchurEliminator) { constexpr int kFBlockSize = 6; constexpr int num_e_blocks = 5; - CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; + auto* bs = new CompressedRowBlockStructure; bs->cols.resize(num_e_blocks + 1); int col_pos = 0; for (int i = 0; i < num_e_blocks; ++i) { diff --git a/internal/ceres/schur_jacobi_preconditioner.cc b/internal/ceres/schur_jacobi_preconditioner.cc index ffab66ff0..3ecec7282 100644 --- a/internal/ceres/schur_jacobi_preconditioner.cc +++ b/internal/ceres/schur_jacobi_preconditioner.cc @@ -43,9 +43,8 @@ namespace ceres { namespace internal { SchurJacobiPreconditioner::SchurJacobiPreconditioner( - const CompressedRowBlockStructure& bs, - const Preconditioner::Options& options) - : options_(options) { + const CompressedRowBlockStructure& bs, Preconditioner::Options options) + : options_(std::move(options)) { CHECK_GT(options_.elimination_groups.size(), 1); CHECK_GT(options_.elimination_groups[0], 0); const int num_blocks = bs.cols.size() - options_.elimination_groups[0]; diff --git a/internal/ceres/schur_jacobi_preconditioner.h b/internal/ceres/schur_jacobi_preconditioner.h index 59f271fbe..a43bc3388 100644 --- a/internal/ceres/schur_jacobi_preconditioner.h +++ b/internal/ceres/schur_jacobi_preconditioner.h @@ -84,7 +84,7 @@ class CERES_NO_EXPORT SchurJacobiPreconditioner // It has the same structural requirement as other Schur complement // based solvers. Please see schur_eliminator.h for more details. SchurJacobiPreconditioner(const CompressedRowBlockStructure& bs, - const Preconditioner::Options& options); + Preconditioner::Options options); SchurJacobiPreconditioner(const SchurJacobiPreconditioner&) = delete; void operator=(const SchurJacobiPreconditioner&) = delete; diff --git a/internal/ceres/scoped_thread_token.h b/internal/ceres/scoped_thread_token.h index a126412f3..533bfd5a3 100644 --- a/internal/ceres/scoped_thread_token.h +++ b/internal/ceres/scoped_thread_token.h @@ -41,19 +41,18 @@ namespace internal { // constructor and puts that token back with destruction. class CERES_NO_EXPORT ScopedThreadToken { public: - ScopedThreadToken(ThreadTokenProvider* provider) + explicit ScopedThreadToken(ThreadTokenProvider* provider) : provider_(provider), token_(provider->Acquire()) {} ~ScopedThreadToken() { provider_->Release(token_); } + ScopedThreadToken(ScopedThreadToken&) = delete; + ScopedThreadToken& operator=(ScopedThreadToken&) = delete; int token() const { return token_; } private: ThreadTokenProvider* provider_; int token_; - - ScopedThreadToken(ScopedThreadToken&); - ScopedThreadToken& operator=(ScopedThreadToken&); }; } // namespace internal diff --git a/internal/ceres/small_blas.h b/internal/ceres/small_blas.h index 856a2a24a..1cf41a5f1 100644 --- a/internal/ceres/small_blas.h +++ b/internal/ceres/small_blas.h @@ -210,7 +210,7 @@ CERES_GEMM_BEGIN(MatrixMatrixMultiplyNaive) { // Process the couple columns in remainder if present. if (NUM_COL_C & 2) { - int col = NUM_COL_C & (int)(~(span - 1)); + int col = NUM_COL_C & (~(span - 1)); const double* pa = &A[0]; for (int row = 0; row < NUM_ROW_C; ++row, pa += NUM_COL_A) { const double* pb = &B[col]; @@ -232,7 +232,7 @@ CERES_GEMM_BEGIN(MatrixMatrixMultiplyNaive) { } // Calculate the main part with multiples of 4. - int col_m = NUM_COL_C & (int)(~(span - 1)); + int col_m = NUM_COL_C & (~(span - 1)); for (int col = 0; col < col_m; col += span) { for (int row = 0; row < NUM_ROW_C; ++row) { const int index = (row + start_row_c) * col_stride_c + start_col_c + col; @@ -315,7 +315,7 @@ CERES_GEMM_BEGIN(MatrixTransposeMatrixMultiplyNaive) { // Process the couple columns in remainder if present. if (NUM_COL_C & 2) { - int col = NUM_COL_C & (int)(~(span - 1)); + int col = NUM_COL_C & (~(span - 1)); for (int row = 0; row < NUM_ROW_C; ++row) { const double* pa = &A[row]; const double* pb = &B[col]; @@ -339,7 +339,7 @@ CERES_GEMM_BEGIN(MatrixTransposeMatrixMultiplyNaive) { } // Process the main part with multiples of 4. - int col_m = NUM_COL_C & (int)(~(span - 1)); + int col_m = NUM_COL_C & (~(span - 1)); for (int col = 0; col < col_m; col += span) { for (int row = 0; row < NUM_ROW_C; ++row) { const int index = (row + start_row_c) * col_stride_c + start_col_c + col; @@ -435,7 +435,7 @@ inline void MatrixVectorMultiply(const double* A, // Process the couple rows in remainder if present. if (NUM_ROW_A & 2) { - int row = NUM_ROW_A & (int)(~(span - 1)); + int row = NUM_ROW_A & (~(span - 1)); const double* pa1 = &A[row * NUM_COL_A]; const double* pa2 = pa1 + NUM_COL_A; const double* pb = &b[0]; @@ -454,7 +454,7 @@ inline void MatrixVectorMultiply(const double* A, } // Calculate the main part with multiples of 4. - int row_m = NUM_ROW_A & (int)(~(span - 1)); + int row_m = NUM_ROW_A & (~(span - 1)); for (int row = 0; row < row_m; row += span) { // clang-format off MVM_mat4x1(NUM_COL_A, &A[row * NUM_COL_A], NUM_COL_A, @@ -522,7 +522,7 @@ inline void MatrixTransposeVectorMultiply(const double* A, // Process the couple columns in remainder if present. if (NUM_COL_A & 2) { - int row = NUM_COL_A & (int)(~(span - 1)); + int row = NUM_COL_A & (~(span - 1)); const double* pa = &A[row]; const double* pb = &b[0]; double tmp1 = 0.0, tmp2 = 0.0; @@ -543,7 +543,7 @@ inline void MatrixTransposeVectorMultiply(const double* A, } // Calculate the main part with multiples of 4. - int row_m = NUM_COL_A & (int)(~(span - 1)); + int row_m = NUM_COL_A & (~(span - 1)); for (int row = 0; row < row_m; row += span) { // clang-format off MTV_mat4x1(NUM_ROW_A, &A[row], NUM_COL_A, diff --git a/internal/ceres/solver_test.cc b/internal/ceres/solver_test.cc index 3ecde2083..7b34bf243 100644 --- a/internal/ceres/solver_test.cc +++ b/internal/ceres/solver_test.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2019 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -118,8 +118,8 @@ TEST(Solver, UpdateStateEveryIterationOptionNoEvaluationCallback) { num_iterations = summary.num_successful_steps + summary.num_unsuccessful_steps; EXPECT_GT(num_iterations, 1); - for (int i = 0; i < callback.x_values.size(); ++i) { - EXPECT_EQ(50.0, callback.x_values[i]); + for (double value : callback.x_values) { + EXPECT_EQ(50.0, value); } // Second: update_state_every_iteration=true, evaluation_callback=nullptr. diff --git a/internal/ceres/sparse_cholesky_test.cc b/internal/ceres/sparse_cholesky_test.cc index 363f9917b..9422bf58b 100644 --- a/internal/ceres/sparse_cholesky_test.cc +++ b/internal/ceres/sparse_cholesky_test.cc @@ -147,8 +147,8 @@ void SparseCholeskySolverUnitTest( << eigen_lhs; } -typedef ::testing::tuple - Param; +using Param = + ::testing::tuple; std::string ParamInfoToString(testing::TestParamInfo info) { Param param = info.param; @@ -267,8 +267,8 @@ using testing::_; using testing::Return; TEST(RefinedSparseCholesky, StorageType) { - MockSparseCholesky* mock_sparse_cholesky = new MockSparseCholesky; - MockIterativeRefiner* mock_iterative_refiner = new MockIterativeRefiner; + auto* mock_sparse_cholesky = new MockSparseCholesky; + auto* mock_iterative_refiner = new MockIterativeRefiner; EXPECT_CALL(*mock_sparse_cholesky, StorageType()) .Times(1) .WillRepeatedly(Return(CompressedRowSparseMatrix::UPPER_TRIANGULAR)); @@ -282,8 +282,8 @@ TEST(RefinedSparseCholesky, StorageType) { }; TEST(RefinedSparseCholesky, Factorize) { - MockSparseCholesky* mock_sparse_cholesky = new MockSparseCholesky; - MockIterativeRefiner* mock_iterative_refiner = new MockIterativeRefiner; + auto* mock_sparse_cholesky = new MockSparseCholesky; + auto* mock_iterative_refiner = new MockIterativeRefiner; EXPECT_CALL(*mock_sparse_cholesky, Factorize(_, _)) .Times(1) .WillRepeatedly(Return(LINEAR_SOLVER_SUCCESS)); @@ -299,8 +299,8 @@ TEST(RefinedSparseCholesky, Factorize) { }; TEST(RefinedSparseCholesky, FactorAndSolveWithUnsuccessfulFactorization) { - MockSparseCholesky* mock_sparse_cholesky = new MockSparseCholesky; - MockIterativeRefiner* mock_iterative_refiner = new MockIterativeRefiner; + auto* mock_sparse_cholesky = new MockSparseCholesky; + auto* mock_iterative_refiner = new MockIterativeRefiner; EXPECT_CALL(*mock_sparse_cholesky, Factorize(_, _)) .Times(1) .WillRepeatedly(Return(LINEAR_SOLVER_FAILURE)); @@ -320,7 +320,7 @@ TEST(RefinedSparseCholesky, FactorAndSolveWithUnsuccessfulFactorization) { }; TEST(RefinedSparseCholesky, FactorAndSolveWithSuccess) { - MockSparseCholesky* mock_sparse_cholesky = new MockSparseCholesky; + auto* mock_sparse_cholesky = new MockSparseCholesky; std::unique_ptr mock_iterative_refiner( new MockIterativeRefiner); EXPECT_CALL(*mock_sparse_cholesky, Factorize(_, _)) diff --git a/internal/ceres/subset_preconditioner.cc b/internal/ceres/subset_preconditioner.cc index b6b0c2e12..221530c0d 100644 --- a/internal/ceres/subset_preconditioner.cc +++ b/internal/ceres/subset_preconditioner.cc @@ -32,6 +32,7 @@ #include #include +#include #include "ceres/compressed_row_sparse_matrix.h" #include "ceres/inner_product_computer.h" @@ -42,9 +43,9 @@ namespace ceres { namespace internal { -SubsetPreconditioner::SubsetPreconditioner( - const Preconditioner::Options& options, const BlockSparseMatrix& A) - : options_(options), num_cols_(A.num_cols()) { +SubsetPreconditioner::SubsetPreconditioner(Preconditioner::Options options, + const BlockSparseMatrix& A) + : options_(std::move(options)), num_cols_(A.num_cols()) { CHECK_GE(options_.subset_preconditioner_start_row_block, 0) << "Congratulations, you found a bug in Ceres. Please report it."; @@ -66,7 +67,7 @@ void SubsetPreconditioner::RightMultiply(const double* x, double* y) const { bool SubsetPreconditioner::UpdateImpl(const BlockSparseMatrix& A, const double* D) { - BlockSparseMatrix* m = const_cast(&A); + auto* m = const_cast(&A); const CompressedRowBlockStructure* bs = m->block_structure(); // A = [P] diff --git a/internal/ceres/subset_preconditioner.h b/internal/ceres/subset_preconditioner.h index 7b6c31770..6d07995a1 100644 --- a/internal/ceres/subset_preconditioner.h +++ b/internal/ceres/subset_preconditioner.h @@ -71,7 +71,7 @@ class InnerProductComputer; class CERES_NO_EXPORT SubsetPreconditioner : public BlockSparseMatrixPreconditioner { public: - SubsetPreconditioner(const Preconditioner::Options& options, + SubsetPreconditioner(Preconditioner::Options options, const BlockSparseMatrix& A); ~SubsetPreconditioner() override; diff --git a/internal/ceres/subset_preconditioner_test.cc b/internal/ceres/subset_preconditioner_test.cc index 7d606f121..27e819bec 100644 --- a/internal/ceres/subset_preconditioner_test.cc +++ b/internal/ceres/subset_preconditioner_test.cc @@ -75,7 +75,7 @@ bool ComputeExpectedSolution(const CompressedRowSparseMatrix& lhs, dense_triangular_lhs, rhs, solution); } -typedef ::testing::tuple Param; +using Param = ::testing::tuple; std::string ParamInfoToString(testing::TestParamInfo info) { Param param = info.param; diff --git a/internal/ceres/suitesparse.h b/internal/ceres/suitesparse.h index 60b415c0b..3f62e7c7b 100644 --- a/internal/ceres/suitesparse.h +++ b/internal/ceres/suitesparse.h @@ -305,7 +305,7 @@ class CERES_NO_EXPORT SuiteSparseCholesky final : public SparseCholesky { std::string* message) final; private: - SuiteSparseCholesky(const OrderingType ordering_type); + explicit SuiteSparseCholesky(const OrderingType ordering_type); const OrderingType ordering_type_; SuiteSparse ss_; diff --git a/internal/ceres/system_test.cc b/internal/ceres/system_test.cc index 3f4450adb..c270def43 100644 --- a/internal/ceres/system_test.cc +++ b/internal/ceres/system_test.cc @@ -139,7 +139,7 @@ class PowellsFunction { double PowellsFunction::kResidualTolerance = 1e-8; -typedef SystemTest PowellTest; +using PowellTest = SystemTest; TEST_F(PowellTest, DenseQR) { PowellsFunction powells_function; diff --git a/internal/ceres/thread_pool.cc b/internal/ceres/thread_pool.cc index b50353769..57f01af54 100644 --- a/internal/ceres/thread_pool.cc +++ b/internal/ceres/thread_pool.cc @@ -83,7 +83,7 @@ void ThreadPool::Resize(int num_threads) { GetNumAllowedThreads(num_threads) - num_current_threads; for (int i = 0; i < create_num_threads; ++i) { - thread_pool_.push_back(std::thread(&ThreadPool::ThreadMainLoop, this)); + thread_pool_.emplace_back(&ThreadPool::ThreadMainLoop, this); } } diff --git a/internal/ceres/thread_token_provider.h b/internal/ceres/thread_token_provider.h index cd9f58f99..918c687eb 100644 --- a/internal/ceres/thread_token_provider.h +++ b/internal/ceres/thread_token_provider.h @@ -68,7 +68,7 @@ namespace internal { // class CERES_NO_EXPORT ThreadTokenProvider { public: - ThreadTokenProvider(int num_threads); + explicit ThreadTokenProvider(int num_threads); // Returns the first token from the queue. The acquired value must be // given back by Release(). diff --git a/internal/ceres/tiny_solver_autodiff_function_test.cc b/internal/ceres/tiny_solver_autodiff_function_test.cc index 2598188ee..36863dfe9 100644 --- a/internal/ceres/tiny_solver_autodiff_function_test.cc +++ b/internal/ceres/tiny_solver_autodiff_function_test.cc @@ -60,8 +60,8 @@ struct AutoDiffTestFunctor { static double const kTolerance = std::numeric_limits::epsilon() * 10; TEST(TinySolverAutoDiffFunction, SimpleFunction) { - typedef TinySolverAutoDiffFunction - AutoDiffTestFunction; + using AutoDiffTestFunction = + TinySolverAutoDiffFunction; AutoDiffTestFunctor autodiff_test_functor; AutoDiffTestFunction f(autodiff_test_functor); @@ -97,7 +97,7 @@ TEST(TinySolverAutoDiffFunction, SimpleFunction) { class DynamicResidualsFunctor { public: - typedef double Scalar; + using Scalar = double; enum { NUM_RESIDUALS = Eigen::Dynamic, NUM_PARAMETERS = 3, @@ -140,7 +140,7 @@ TEST(TinySolverAutoDiffFunction, ResidualsDynamicAutoDiff) { EXPECT_GT(residuals.squaredNorm() / 2.0, 1e-10); TinySolver solver; - solver.Solve(f, &x0); + solver.Solve(f_autodiff, &x0); EXPECT_NEAR(0.0, solver.summary.final_cost, 1e-10); } diff --git a/internal/ceres/tiny_solver_cost_function_adapter_test.cc b/internal/ceres/tiny_solver_cost_function_adapter_test.cc index ff8c070ef..c5d7a023d 100644 --- a/internal/ceres/tiny_solver_cost_function_adapter_test.cc +++ b/internal/ceres/tiny_solver_cost_function_adapter_test.cc @@ -68,8 +68,8 @@ class CostFunction2x3 : public SizedCostFunction<2, 3> { template void TestHelper() { std::unique_ptr cost_function(new CostFunction2x3); - typedef TinySolverCostFunctionAdapter - CostFunctionAdapter; + using CostFunctionAdapter = + TinySolverCostFunctionAdapter; CostFunctionAdapter cfa(*cost_function); EXPECT_EQ(CostFunctionAdapter::NUM_RESIDUALS, kNumResiduals); EXPECT_EQ(CostFunctionAdapter::NUM_PARAMETERS, kNumParameters); diff --git a/internal/ceres/tiny_solver_test.cc b/internal/ceres/tiny_solver_test.cc index 45c021f61..500ac0d6a 100644 --- a/internal/ceres/tiny_solver_test.cc +++ b/internal/ceres/tiny_solver_test.cc @@ -39,13 +39,13 @@ namespace ceres { -typedef Eigen::Matrix Vec2; -typedef Eigen::Matrix Vec3; -typedef Eigen::VectorXd VecX; +using Vec2 = Eigen::Matrix; +using Vec3 = Eigen::Matrix; +using VecX = Eigen::VectorXd; class ExampleStatic { public: - typedef double Scalar; + using Scalar = double; enum { // Can also be Eigen::Dynamic. NUM_RESIDUALS = 2, @@ -60,7 +60,7 @@ class ExampleStatic { class ExampleParametersDynamic { public: - typedef double Scalar; + using Scalar = double; enum { NUM_RESIDUALS = 2, NUM_PARAMETERS = Eigen::Dynamic, @@ -77,7 +77,7 @@ class ExampleParametersDynamic { class ExampleResidualsDynamic { public: - typedef double Scalar; + using Scalar = double; enum { NUM_RESIDUALS = Eigen::Dynamic, NUM_PARAMETERS = 3, @@ -94,7 +94,7 @@ class ExampleResidualsDynamic { class ExampleAllDynamic { public: - typedef double Scalar; + using Scalar = double; enum { NUM_RESIDUALS = Eigen::Dynamic, NUM_PARAMETERS = Eigen::Dynamic, diff --git a/internal/ceres/triplet_sparse_matrix.cc b/internal/ceres/triplet_sparse_matrix.cc index ad81fd2ce..bbb5f676a 100644 --- a/internal/ceres/triplet_sparse_matrix.cc +++ b/internal/ceres/triplet_sparse_matrix.cc @@ -109,8 +109,9 @@ bool TripletSparseMatrix::AllTripletsWithinBounds() const { for (int i = 0; i < num_nonzeros_; ++i) { // clang-format off if ((rows_[i] < 0) || (rows_[i] >= num_rows_) || - (cols_[i] < 0) || (cols_[i] >= num_cols_)) + (cols_[i] < 0) || (cols_[i] >= num_cols_)) { return false; + } // clang-format on } return true; diff --git a/internal/ceres/triplet_sparse_matrix.h b/internal/ceres/triplet_sparse_matrix.h index bcb452914..065c690db 100644 --- a/internal/ceres/triplet_sparse_matrix.h +++ b/internal/ceres/triplet_sparse_matrix.h @@ -57,7 +57,7 @@ class CERES_NO_EXPORT TripletSparseMatrix final : public SparseMatrix { const std::vector& cols, const std::vector& values); - explicit TripletSparseMatrix(const TripletSparseMatrix& orig); + TripletSparseMatrix(const TripletSparseMatrix& orig); TripletSparseMatrix& operator=(const TripletSparseMatrix& rhs); diff --git a/internal/ceres/trust_region_preprocessor.cc b/internal/ceres/trust_region_preprocessor.cc index 9892e1eff..edba47d88 100644 --- a/internal/ceres/trust_region_preprocessor.cc +++ b/internal/ceres/trust_region_preprocessor.cc @@ -60,9 +60,9 @@ std::shared_ptr CreateDefaultLinearSolverOrdering( std::shared_ptr ordering = std::make_shared(); const vector& parameter_blocks = program.parameter_blocks(); - for (int i = 0; i < parameter_blocks.size(); ++i) { + for (auto* parameter_block : parameter_blocks) { ordering->AddElementToGroup( - const_cast(parameter_blocks[i]->user_state()), 0); + const_cast(parameter_block->user_state()), 0); } return ordering; } diff --git a/internal/ceres/visibility.cc b/internal/ceres/visibility.cc index aa84a9f98..0867733a6 100644 --- a/internal/ceres/visibility.cc +++ b/internal/ceres/visibility.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -63,8 +63,8 @@ void ComputeVisibility(const CompressedRowBlockStructure& block_structure, visibility->resize(0); visibility->resize(block_structure.cols.size() - num_eliminate_blocks); - for (int i = 0; i < block_structure.rows.size(); ++i) { - const vector& cells = block_structure.rows[i].cells; + for (const auto& row : block_structure.rows) { + const vector& cells = row.cells; int block_id = cells[0].block_id; // If the first block is not an e_block, then skip this row block. if (block_id >= num_eliminate_blocks) { @@ -87,9 +87,9 @@ std::unique_ptr> CreateSchurComplementGraph( // set for each e_block/camera contains the set of e_blocks/points // visible to it, we find the maximum across all visibility sets. int num_points = 0; - for (int i = 0; i < visibility.size(); i++) { - if (visibility[i].size() > 0) { - num_points = max(num_points, (*visibility[i].rbegin()) + 1); + for (const auto& visible : visibility) { + if (visible.size() > 0) { + num_points = max(num_points, (*visible.rbegin()) + 1); } } @@ -101,7 +101,7 @@ std::unique_ptr> CreateSchurComplementGraph( vector> inverse_visibility(num_points); for (int i = 0; i < visibility.size(); i++) { const set& visibility_set = visibility[i]; - for (const int v : visibility_set) { + for (int v : visibility_set) { inverse_visibility[v].insert(i); } } @@ -112,10 +112,10 @@ std::unique_ptr> CreateSchurComplementGraph( // Count the number of points visible to each camera/f_block pair. for (const auto& inverse_visibility_set : inverse_visibility) { - for (set::const_iterator camera1 = inverse_visibility_set.begin(); + for (auto camera1 = inverse_visibility_set.begin(); camera1 != inverse_visibility_set.end(); ++camera1) { - set::const_iterator camera2 = camera1; + auto camera2 = camera1; for (++camera2; camera2 != inverse_visibility_set.end(); ++camera2) { ++(camera_pairs[make_pair(*camera1, *camera2)]); } diff --git a/internal/ceres/visibility_based_preconditioner.cc b/internal/ceres/visibility_based_preconditioner.cc index 9419e6b80..831a86630 100644 --- a/internal/ceres/visibility_based_preconditioner.cc +++ b/internal/ceres/visibility_based_preconditioner.cc @@ -1,5 +1,5 @@ // Ceres Solver - A fast non-linear least squares minimizer -// Copyright 2015 Google Inc. All rights reserved. +// Copyright 2022 Google Inc. All rights reserved. // http://ceres-solver.org/ // // Redistribution and use in source and binary forms, with or without @@ -70,9 +70,8 @@ static constexpr double kCanonicalViewsSimilarityPenaltyWeight = 0.0; static constexpr double kSingleLinkageMinSimilarity = 0.9; VisibilityBasedPreconditioner::VisibilityBasedPreconditioner( - const CompressedRowBlockStructure& bs, - const Preconditioner::Options& options) - : options_(options), num_blocks_(0), num_clusters_(0) { + const CompressedRowBlockStructure& bs, Preconditioner::Options options) + : options_(std::move(options)), num_blocks_(0), num_clusters_(0) { CHECK_GT(options_.elimination_groups.size(), 1); CHECK_GT(options_.elimination_groups[0], 0); CHECK(options_.type == CLUSTER_JACOBI || options_.type == CLUSTER_TRIDIAGONAL) @@ -283,14 +282,12 @@ void VisibilityBasedPreconditioner::ComputeBlockPairsInPreconditioner( } } - for (set::const_iterator block1 = f_blocks.begin(); - block1 != f_blocks.end(); - ++block1) { - set::const_iterator block2 = block1; + for (auto block1 = f_blocks.begin(); block1 != f_blocks.end(); ++block1) { + auto block2 = block1; ++block2; for (; block2 != f_blocks.end(); ++block2) { if (IsBlockPairInPreconditioner(*block1, *block2)) { - block_pairs_.insert(make_pair(*block1, *block2)); + block_pairs_.emplace(*block1, *block2); } } } @@ -302,8 +299,8 @@ void VisibilityBasedPreconditioner::ComputeBlockPairsInPreconditioner( CHECK_GE(row.cells.front().block_id, num_eliminate_blocks); for (int i = 0; i < row.cells.size(); ++i) { const int block1 = row.cells[i].block_id - num_eliminate_blocks; - for (int j = 0; j < row.cells.size(); ++j) { - const int block2 = row.cells[j].block_id - num_eliminate_blocks; + for (const auto& cell : row.cells) { + const int block2 = cell.block_id - num_eliminate_blocks; if (block1 <= block2) { if (IsBlockPairInPreconditioner(block1, block2)) { block_pairs_.insert(make_pair(block1, block2)); diff --git a/internal/ceres/visibility_based_preconditioner.h b/internal/ceres/visibility_based_preconditioner.h index 04d87ce38..8079dc3f3 100644 --- a/internal/ceres/visibility_based_preconditioner.h +++ b/internal/ceres/visibility_based_preconditioner.h @@ -134,7 +134,7 @@ class CERES_NO_EXPORT VisibilityBasedPreconditioner // It has the same structural requirement as other Schur complement // based solvers. Please see schur_eliminator.h for more details. VisibilityBasedPreconditioner(const CompressedRowBlockStructure& bs, - const Preconditioner::Options& options); + Preconditioner::Options options); VisibilityBasedPreconditioner(const VisibilityBasedPreconditioner&) = delete; void operator=(const VisibilityBasedPreconditioner&) = delete; diff --git a/internal/ceres/visibility_test.cc b/internal/ceres/visibility_test.cc index a1999635b..218bf6efd 100644 --- a/internal/ceres/visibility_test.cc +++ b/internal/ceres/visibility_test.cc @@ -60,50 +60,50 @@ TEST(VisibilityTest, SimpleMatrix) { // Row 1 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); - row.cells.push_back(Cell(5, 0)); + row.cells.emplace_back(0, 0); + row.cells.emplace_back(5, 0); } // Row 2 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 2; - row.cells.push_back(Cell(0, 1)); - row.cells.push_back(Cell(3, 1)); + row.cells.emplace_back(0, 1); + row.cells.emplace_back(3, 1); } // Row 3 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 4; - row.cells.push_back(Cell(1, 2)); - row.cells.push_back(Cell(2, 2)); + row.cells.emplace_back(1, 2); + row.cells.emplace_back(2, 2); } // Row 4 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 6; - row.cells.push_back(Cell(1, 3)); - row.cells.push_back(Cell(4, 3)); + row.cells.emplace_back(1, 3); + row.cells.emplace_back(4, 3); } bs.cols.resize(num_cols); vector> visibility; ComputeVisibility(bs, num_eliminate_blocks, &visibility); ASSERT_EQ(visibility.size(), num_cols - num_eliminate_blocks); - for (int i = 0; i < visibility.size(); ++i) { - ASSERT_EQ(visibility[i].size(), 1); + for (const auto& visible : visibility) { + ASSERT_EQ(visible.size(), 1); } std::unique_ptr> graph( @@ -139,46 +139,46 @@ TEST(VisibilityTest, NoEBlocks) { // Row 1 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 0; - row.cells.push_back(Cell(0, 0)); + row.cells.emplace_back(0, 0); } // Row 2 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 2; - row.cells.push_back(Cell(0, 1)); + row.cells.emplace_back(0, 1); } // Row 3 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 4; - row.cells.push_back(Cell(1, 2)); + row.cells.emplace_back(1, 2); } // Row 4 { - bs.rows.push_back(CompressedRow()); + bs.rows.emplace_back(); CompressedRow& row = bs.rows.back(); row.block.size = 2; row.block.position = 6; - row.cells.push_back(Cell(1, 3)); + row.cells.emplace_back(1, 3); } bs.cols.resize(num_cols); vector> visibility; ComputeVisibility(bs, num_eliminate_blocks, &visibility); ASSERT_EQ(visibility.size(), num_cols - num_eliminate_blocks); - for (int i = 0; i < visibility.size(); ++i) { - ASSERT_EQ(visibility[i].size(), 0); + for (const auto& visible : visibility) { + ASSERT_EQ(visible.size(), 0); } std::unique_ptr> graph(