diff --git a/examples/nist.cc b/examples/nist.cc index 394458614..a3430caa6 100644 --- a/examples/nist.cc +++ b/examples/nist.cc @@ -553,7 +553,7 @@ CostFunction* CreateCostFunction(const Matrix& predictor, model, ceres::TAKE_OWNERSHIP, num_observations, options); } else { LOG(ERROR) << "Invalid numeric diff method specified"; - return 0; + return nullptr; } } else { cost_function = diff --git a/examples/sampled_function/README.md b/examples/sampled_function/README.md index ef1af43a5..5fde4151e 100644 --- a/examples/sampled_function/README.md +++ b/examples/sampled_function/README.md @@ -32,7 +32,7 @@ interpolation schemes by using a `SizedCostFunction` and defining the ```c++ bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const { - if (jacobians == NULL || jacobians[0] == NULL) + if (jacobians == nullptr || jacobians[0] == nullptr) interpolator_.Evaluate(parameters[0][0], residuals); else interpolator_.Evaluate(parameters[0][0], residuals, jacobians[0]); diff --git a/examples/sampled_function/sampled_function.cc b/examples/sampled_function/sampled_function.cc index e96018dde..ee7e5edde 100644 --- a/examples/sampled_function/sampled_function.cc +++ b/examples/sampled_function/sampled_function.cc @@ -82,7 +82,7 @@ int main(int argc, char** argv) { double x = 1.0; Problem problem; CostFunction* cost_function = InterpolatedCostFunctor::Create(interpolator); - problem.AddResidualBlock(cost_function, NULL, &x); + problem.AddResidualBlock(cost_function, nullptr, &x); Solver::Options options; options.minimizer_progress_to_stdout = true; diff --git a/internal/ceres/accelerate_sparse.cc b/internal/ceres/accelerate_sparse.cc index d2b642bf5..6cb9382f5 100644 --- a/internal/ceres/accelerate_sparse.cc +++ b/internal/ceres/accelerate_sparse.cc @@ -196,17 +196,17 @@ template LinearSolverTerminationType AppleAccelerateCholesky::Factorize( CompressedRowSparseMatrix* lhs, std::string* message) { CHECK_EQ(lhs->storage_type(), StorageType()); - if (lhs == NULL) { - *message = "Failure: Input lhs is NULL."; + if (lhs == nullptr) { + *message = "Failure: Input lhs is nullptr."; return LINEAR_SOLVER_FATAL_ERROR; } typename SparseTypesTrait::SparseMatrix as_lhs = as_.CreateSparseMatrixTransposeView(lhs); if (!symbolic_factor_) { - symbolic_factor_.reset( - new typename SparseTypesTrait::SymbolicFactorization( - as_.AnalyzeCholesky(&as_lhs))); + symbolic_factor_ = std::make_unique< + typename SparseTypesTrait::SymbolicFactorization>( + as_.AnalyzeCholesky(&as_lhs)); if (symbolic_factor_->status != SparseStatusOK) { *message = StringPrintf( "Apple Accelerate Failure : Symbolic factorisation failed: %s", @@ -217,9 +217,9 @@ LinearSolverTerminationType AppleAccelerateCholesky::Factorize( } if (!numeric_factor_) { - numeric_factor_.reset( - new typename SparseTypesTrait::NumericFactorization( - as_.Cholesky(&as_lhs, symbolic_factor_.get()))); + numeric_factor_ = std::make_unique< + typename SparseTypesTrait::NumericFactorization>( + as_.Cholesky(&as_lhs, symbolic_factor_.get())); } else { // Recycle memory from previous numeric factorization. as_.Cholesky(&as_lhs, numeric_factor_.get()); @@ -265,7 +265,7 @@ template void AppleAccelerateCholesky::FreeSymbolicFactorization() { if (symbolic_factor_) { SparseCleanup(*symbolic_factor_); - symbolic_factor_.reset(); + symbolic_factor_ = nullptr; } } @@ -273,7 +273,7 @@ template void AppleAccelerateCholesky::FreeNumericFactorization() { if (numeric_factor_) { SparseCleanup(*numeric_factor_); - numeric_factor_.reset(); + numeric_factor_ = nullptr; } } diff --git a/internal/ceres/array_utils.cc b/internal/ceres/array_utils.cc index 6bffd840f..113d41c92 100644 --- a/internal/ceres/array_utils.cc +++ b/internal/ceres/array_utils.cc @@ -44,7 +44,7 @@ namespace internal { using std::string; bool IsArrayValid(const int size, const double* x) { - if (x != NULL) { + if (x != nullptr) { for (int i = 0; i < size; ++i) { if (!std::isfinite(x[i]) || (x[i] == kImpossibleValue)) { return false; @@ -55,7 +55,7 @@ bool IsArrayValid(const int size, const double* x) { } int FindInvalidValue(const int size, const double* x) { - if (x == NULL) { + if (x == nullptr) { return size; } @@ -69,7 +69,7 @@ int FindInvalidValue(const int size, const double* x) { } void InvalidateArray(const int size, double* x) { - if (x != NULL) { + if (x != nullptr) { for (int i = 0; i < size; ++i) { x[i] = kImpossibleValue; } @@ -78,7 +78,7 @@ void InvalidateArray(const int size, double* x) { void AppendArrayToString(const int size, const double* x, string* result) { for (int i = 0; i < size; ++i) { - if (x == NULL) { + if (x == nullptr) { StringAppendF(result, "Not Computed "); } else { if (x[i] == kImpossibleValue) { diff --git a/internal/ceres/array_utils.h b/internal/ceres/array_utils.h index 68feca5e7..5264ee64b 100644 --- a/internal/ceres/array_utils.h +++ b/internal/ceres/array_utils.h @@ -64,7 +64,7 @@ CERES_EXPORT_INTERNAL bool IsArrayValid(int size, const double* x); CERES_EXPORT_INTERNAL int FindInvalidValue(const int size, const double* x); // Utility routine to print an array of doubles to a string. If the -// array pointer is NULL, it is treated as an array of zeros. +// array pointer is nullptr, it is treated as an array of zeros. CERES_EXPORT_INTERNAL void AppendArrayToString(const int size, const double* x, std::string* result); diff --git a/internal/ceres/array_utils_test.cc b/internal/ceres/array_utils_test.cc index 6c0ea8432..b011520a1 100644 --- a/internal/ceres/array_utils_test.cc +++ b/internal/ceres/array_utils_test.cc @@ -53,7 +53,7 @@ TEST(ArrayUtils, IsArrayValid) { EXPECT_FALSE(IsArrayValid(3, x)); x[1] = std::numeric_limits::signaling_NaN(); EXPECT_FALSE(IsArrayValid(3, x)); - EXPECT_TRUE(IsArrayValid(1, NULL)); + EXPECT_TRUE(IsArrayValid(1, nullptr)); InvalidateArray(3, x); EXPECT_FALSE(IsArrayValid(3, x)); } @@ -70,7 +70,7 @@ TEST(ArrayUtils, FindInvalidIndex) { EXPECT_EQ(FindInvalidValue(3, x), 1); x[1] = std::numeric_limits::signaling_NaN(); EXPECT_EQ(FindInvalidValue(3, x), 1); - EXPECT_EQ(FindInvalidValue(1, NULL), 1); + EXPECT_EQ(FindInvalidValue(1, nullptr), 1); InvalidateArray(3, x); EXPECT_EQ(FindInvalidValue(3, x), 0); } diff --git a/internal/ceres/block_evaluate_preparer.cc b/internal/ceres/block_evaluate_preparer.cc index 7db96d94e..56c97b60c 100644 --- a/internal/ceres/block_evaluate_preparer.cc +++ b/internal/ceres/block_evaluate_preparer.cc @@ -53,7 +53,7 @@ void BlockEvaluatePreparer::Prepare(const ResidualBlock* residual_block, SparseMatrix* jacobian, double** jacobians) { // If the overall jacobian is not available, use the scratch space. - if (jacobian == NULL) { + if (jacobian == nullptr) { scratch_evaluate_preparer_.Prepare( residual_block, residual_block_index, jacobian, jacobians); return; @@ -73,7 +73,7 @@ void BlockEvaluatePreparer::Prepare(const ResidualBlock* residual_block, // parameters. Instead, bump the pointer for active parameters only. jacobian_block_offset++; } else { - jacobians[j] = NULL; + jacobians[j] = nullptr; } } } diff --git a/internal/ceres/block_jacobi_preconditioner.cc b/internal/ceres/block_jacobi_preconditioner.cc index 7459310d9..da8fc9482 100644 --- a/internal/ceres/block_jacobi_preconditioner.cc +++ b/internal/ceres/block_jacobi_preconditioner.cc @@ -47,7 +47,7 @@ BlockJacobiPreconditioner::BlockJacobiPreconditioner( blocks[i] = bs->cols[i].size; } - m_.reset(new BlockRandomAccessDiagonalMatrix(blocks)); + m_ = std::make_unique(blocks); } BlockJacobiPreconditioner::~BlockJacobiPreconditioner() = default; @@ -74,7 +74,7 @@ bool BlockJacobiPreconditioner::UpdateImpl(const BlockSparseMatrix& A, } } - if (D != NULL) { + if (D != nullptr) { // Add the diagonal. int position = 0; for (int i = 0; i < bs->cols.size(); ++i) { diff --git a/internal/ceres/block_jacobi_preconditioner_test.cc b/internal/ceres/block_jacobi_preconditioner_test.cc index cc582c620..6d124f1aa 100644 --- a/internal/ceres/block_jacobi_preconditioner_test.cc +++ b/internal/ceres/block_jacobi_preconditioner_test.cc @@ -45,12 +45,12 @@ namespace internal { class BlockJacobiPreconditionerTest : public ::testing::Test { protected: void SetUpFromProblemId(int problem_id) { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(problem_id)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(problem_id); CHECK(problem != nullptr); A.reset(down_cast(problem->A.release())); - D.reset(problem->D.release()); + D = std::move(problem->D); Matrix dense_a; A->ToDenseMatrix(&dense_a); diff --git a/internal/ceres/block_jacobian_writer.cc b/internal/ceres/block_jacobian_writer.cc index e589c3446..a5020e650 100644 --- a/internal/ceres/block_jacobian_writer.cc +++ b/internal/ceres/block_jacobian_writer.cc @@ -30,6 +30,8 @@ #include "ceres/block_jacobian_writer.h" +#include + #include "ceres/block_evaluate_preparer.h" #include "ceres/block_sparse_matrix.h" #include "ceres/internal/eigen.h" @@ -148,7 +150,7 @@ BlockEvaluatePreparer* BlockJacobianWriter::CreateEvaluatePreparers( return preparers; } -SparseMatrix* BlockJacobianWriter::CreateJacobian() const { +std::unique_ptr BlockJacobianWriter::CreateJacobian() const { CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; const vector& parameter_blocks = @@ -201,12 +203,10 @@ SparseMatrix* BlockJacobianWriter::CreateJacobian() const { } } - sort(row->cells.begin(), row->cells.end(), CellLessThan); + std::sort(row->cells.begin(), row->cells.end(), CellLessThan); } - BlockSparseMatrix* jacobian = new BlockSparseMatrix(bs); - CHECK(jacobian != nullptr); - return jacobian; + return std::make_unique(bs); } } // namespace internal diff --git a/internal/ceres/block_jacobian_writer.h b/internal/ceres/block_jacobian_writer.h index 8054d7b33..b9c918bde 100644 --- a/internal/ceres/block_jacobian_writer.h +++ b/internal/ceres/block_jacobian_writer.h @@ -61,7 +61,7 @@ class BlockJacobianWriter { // This makes the final Write() a nop. BlockEvaluatePreparer* CreateEvaluatePreparers(int num_threads); - SparseMatrix* CreateJacobian() const; + std::unique_ptr CreateJacobian() const; void Write(int /* residual_id */, int /* residual_offset */, diff --git a/internal/ceres/block_random_access_dense_matrix.cc b/internal/ceres/block_random_access_dense_matrix.cc index f2ac0b81f..ed172de1d 100644 --- a/internal/ceres/block_random_access_dense_matrix.cc +++ b/internal/ceres/block_random_access_dense_matrix.cc @@ -48,9 +48,9 @@ BlockRandomAccessDenseMatrix::BlockRandomAccessDenseMatrix( num_rows_ += blocks[i]; } - values_.reset(new double[num_rows_ * num_rows_]); + values_ = std::make_unique(num_rows_ * num_rows_); - cell_infos_.reset(new CellInfo[num_blocks * num_blocks]); + cell_infos_ = std::make_unique(num_blocks * num_blocks); for (int i = 0; i < num_blocks * num_blocks; ++i) { cell_infos_[i].values = values_.get(); } diff --git a/internal/ceres/block_random_access_dense_matrix.h b/internal/ceres/block_random_access_dense_matrix.h index 21007de01..aef252e48 100644 --- a/internal/ceres/block_random_access_dense_matrix.h +++ b/internal/ceres/block_random_access_dense_matrix.h @@ -46,7 +46,7 @@ namespace internal { // num_rows x num_cols. // // This class is NOT thread safe. Since all n^2 cells are stored, -// GetCell never returns NULL for any (row_block_id, col_block_id) +// GetCell never returns nullptr for any (row_block_id, col_block_id) // pair. // // ReturnCell is a nop. diff --git a/internal/ceres/block_random_access_dense_matrix_test.cc b/internal/ceres/block_random_access_dense_matrix_test.cc index 0736d568c..c85c38876 100644 --- a/internal/ceres/block_random_access_dense_matrix_test.cc +++ b/internal/ceres/block_random_access_dense_matrix_test.cc @@ -58,7 +58,7 @@ TEST(BlockRandomAccessDenseMatrix, GetCell) { int col_stride; CellInfo* cell = m.GetCell(i, j, &row, &col, &row_stride, &col_stride); - EXPECT_TRUE(cell != NULL); + EXPECT_TRUE(cell != nullptr); EXPECT_EQ(row, row_idx); EXPECT_EQ(col, col_idx); EXPECT_EQ(row_stride, 3 + 4 + 5); diff --git a/internal/ceres/block_random_access_diagonal_matrix.cc b/internal/ceres/block_random_access_diagonal_matrix.cc index 08f6d7f17..1b021557e 100644 --- a/internal/ceres/block_random_access_diagonal_matrix.cc +++ b/internal/ceres/block_random_access_diagonal_matrix.cc @@ -31,6 +31,7 @@ #include "ceres/block_random_access_diagonal_matrix.h" #include +#include #include #include #include @@ -66,7 +67,8 @@ BlockRandomAccessDiagonalMatrix::BlockRandomAccessDiagonalMatrix( VLOG(1) << "Matrix Size [" << num_cols << "," << num_cols << "] " << num_nonzeros; - tsm_.reset(new TripletSparseMatrix(num_cols, num_cols, num_nonzeros)); + tsm_ = + std::make_unique(num_cols, num_cols, num_nonzeros); tsm_->set_num_nonzeros(num_nonzeros); int* rows = tsm_->mutable_rows(); int* cols = tsm_->mutable_cols(); @@ -99,7 +101,7 @@ CellInfo* BlockRandomAccessDiagonalMatrix::GetCell(int row_block_id, int* row_stride, int* col_stride) { if (row_block_id != col_block_id) { - return NULL; + return nullptr; } const int stride = blocks_[row_block_id]; diff --git a/internal/ceres/block_random_access_diagonal_matrix_test.cc b/internal/ceres/block_random_access_diagonal_matrix_test.cc index afd95ee3e..42a309f46 100644 --- a/internal/ceres/block_random_access_diagonal_matrix_test.cc +++ b/internal/ceres/block_random_access_diagonal_matrix_test.cc @@ -52,7 +52,7 @@ class BlockRandomAccessDiagonalMatrixTest : public ::testing::Test { const int num_rows = 3 + 4 + 5; num_nonzeros_ = 3 * 3 + 4 * 4 + 5 * 5; - m_.reset(new BlockRandomAccessDiagonalMatrix(blocks)); + m_ = std::make_unique(blocks); EXPECT_EQ(m_->num_rows(), num_rows); EXPECT_EQ(m_->num_cols(), num_rows); @@ -71,11 +71,11 @@ class BlockRandomAccessDiagonalMatrixTest : public ::testing::Test { row_block_id, col_block_id, &row, &col, &row_stride, &col_stride); // Off diagonal entries are not present. if (i != j) { - EXPECT_TRUE(cell == NULL); + EXPECT_TRUE(cell == nullptr); continue; } - EXPECT_TRUE(cell != NULL); + EXPECT_TRUE(cell != nullptr); EXPECT_EQ(row, 0); EXPECT_EQ(col, 0); EXPECT_EQ(row_stride, blocks[row_block_id]); diff --git a/internal/ceres/block_random_access_matrix.h b/internal/ceres/block_random_access_matrix.h index f190622ea..7f01763ca 100644 --- a/internal/ceres/block_random_access_matrix.h +++ b/internal/ceres/block_random_access_matrix.h @@ -62,7 +62,7 @@ namespace internal { // // There is no requirement that all cells be present, i.e. the matrix // itself can be block sparse. When a cell is not present, the GetCell -// method will return a NULL pointer. +// method will return a nullptr pointer. // // There is no requirement about how the cells are stored beyond that // form a dense submatrix of a larger dense matrix. Like everywhere @@ -77,7 +77,7 @@ namespace internal { // &row, &col, // &row_stride, &col_stride); // -// if (cell != NULL) { +// if (cell != nullptr) { // MatrixRef m(cell->values, row_stride, col_stride); // std::lock_guard l(&cell->m); // m.block(row, col, row_block_size, col_block_size) = ... @@ -99,7 +99,7 @@ class CERES_EXPORT_INTERNAL BlockRandomAccessMatrix { // If the cell (row_block_id, col_block_id) is present, then return // a CellInfo with a pointer to the dense matrix containing it, - // otherwise return NULL. The dense matrix containing this cell has + // otherwise return nullptr. The dense matrix containing this cell has // size row_stride, col_stride and the cell is located at position // (row, col) within this matrix. // diff --git a/internal/ceres/block_random_access_sparse_matrix.cc b/internal/ceres/block_random_access_sparse_matrix.cc index c28b7cef3..1b2f5e7b3 100644 --- a/internal/ceres/block_random_access_sparse_matrix.cc +++ b/internal/ceres/block_random_access_sparse_matrix.cc @@ -76,7 +76,8 @@ BlockRandomAccessSparseMatrix::BlockRandomAccessSparseMatrix( VLOG(1) << "Matrix Size [" << num_cols << "," << num_cols << "] " << num_nonzeros; - tsm_.reset(new TripletSparseMatrix(num_cols, num_cols, num_nonzeros)); + tsm_ = + std::make_unique(num_cols, num_cols, num_nonzeros); tsm_->set_num_nonzeros(num_nonzeros); int* rows = tsm_->mutable_rows(); int* cols = tsm_->mutable_cols(); @@ -129,7 +130,7 @@ CellInfo* BlockRandomAccessSparseMatrix::GetCell(int row_block_id, const LayoutType::iterator it = layout_.find(IntPairToLong(row_block_id, col_block_id)); if (it == layout_.end()) { - return NULL; + return nullptr; } // Each cell is stored contiguously as its own little dense matrix. diff --git a/internal/ceres/block_random_access_sparse_matrix_test.cc b/internal/ceres/block_random_access_sparse_matrix_test.cc index 557b67871..7224b65f9 100644 --- a/internal/ceres/block_random_access_sparse_matrix_test.cc +++ b/internal/ceres/block_random_access_sparse_matrix_test.cc @@ -80,7 +80,7 @@ TEST(BlockRandomAccessSparseMatrix, GetCell) { int col_stride; CellInfo* cell = m.GetCell( row_block_id, col_block_id, &row, &col, &row_stride, &col_stride); - EXPECT_TRUE(cell != NULL); + EXPECT_TRUE(cell != nullptr); EXPECT_EQ(row, 0); EXPECT_EQ(col, 0); EXPECT_EQ(row_stride, blocks[row_block_id]); @@ -142,7 +142,7 @@ class BlockRandomAccessSparseMatrixTest : public ::testing::Test { blocks.push_back(1); set> block_pairs; block_pairs.insert(make_pair(0, 0)); - m_.reset(new BlockRandomAccessSparseMatrix(blocks, block_pairs)); + m_ = std::make_unique(blocks, block_pairs); } void CheckIntPairToLong(int a, int b) { diff --git a/internal/ceres/block_sparse_matrix.cc b/internal/ceres/block_sparse_matrix.cc index 4db409dca..e06bd2fad 100644 --- a/internal/ceres/block_sparse_matrix.cc +++ b/internal/ceres/block_sparse_matrix.cc @@ -80,7 +80,7 @@ BlockSparseMatrix::BlockSparseMatrix( CHECK_GE(num_nonzeros_, 0); VLOG(2) << "Allocating values array with " << num_nonzeros_ * sizeof(double) << " bytes."; // NOLINT - values_.reset(new double[num_nonzeros_]); + values_ = std::make_unique(num_nonzeros_); max_num_nonzeros_ = num_nonzeros_; CHECK(values_ != nullptr); } @@ -248,7 +248,7 @@ void BlockSparseMatrix::ToTextFile(FILE* file) const { } } -BlockSparseMatrix* BlockSparseMatrix::CreateDiagonalMatrix( +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(); @@ -265,7 +265,7 @@ BlockSparseMatrix* BlockSparseMatrix::CreateDiagonalMatrix( } // Create the BlockSparseMatrix with the given block structure. - BlockSparseMatrix* matrix = new BlockSparseMatrix(bs); + auto matrix = std::make_unique(bs); matrix->SetZero(); // Fill the values array of the block sparse matrix. @@ -308,9 +308,10 @@ void BlockSparseMatrix::AppendRows(const BlockSparseMatrix& m) { } if (num_nonzeros_ > max_num_nonzeros_) { - double* new_values = new double[num_nonzeros_]; - std::copy(values_.get(), values_.get() + old_num_nonzeros, new_values); - values_.reset(new_values); + std::unique_ptr new_values = + std::make_unique(num_nonzeros_); + std::copy_n(values_.get(), old_num_nonzeros, new_values.get()); + values_ = std::move(new_values); max_num_nonzeros_ = num_nonzeros_; } @@ -337,7 +338,7 @@ void BlockSparseMatrix::DeleteRowBlocks(const int delta_row_blocks) { block_structure_->rows.resize(num_row_blocks - delta_row_blocks); } -BlockSparseMatrix* BlockSparseMatrix::CreateRandomMatrix( +std::unique_ptr BlockSparseMatrix::CreateRandomMatrix( const BlockSparseMatrix::RandomMatrixOptions& options) { CHECK_GT(options.num_row_blocks, 0); CHECK_GT(options.min_row_block_size, 0); @@ -395,7 +396,7 @@ BlockSparseMatrix* BlockSparseMatrix::CreateRandomMatrix( } } - BlockSparseMatrix* matrix = new BlockSparseMatrix(bs); + auto matrix = std::make_unique(bs); double* values = matrix->mutable_values(); for (int i = 0; i < matrix->num_nonzeros(); ++i) { values[i] = RandNormal(); diff --git a/internal/ceres/block_sparse_matrix.h b/internal/ceres/block_sparse_matrix.h index ecf62637a..07075468b 100644 --- a/internal/ceres/block_sparse_matrix.h +++ b/internal/ceres/block_sparse_matrix.h @@ -97,7 +97,7 @@ class CERES_EXPORT_INTERNAL BlockSparseMatrix : public SparseMatrix { // Delete the bottom delta_rows_blocks. void DeleteRowBlocks(int delta_row_blocks); - static BlockSparseMatrix* CreateDiagonalMatrix( + static std::unique_ptr CreateDiagonalMatrix( const double* diagonal, const std::vector& column_blocks); struct RandomMatrixOptions { @@ -122,9 +122,7 @@ class CERES_EXPORT_INTERNAL BlockSparseMatrix : public SparseMatrix { // Create a random BlockSparseMatrix whose entries are normally // distributed and whose structure is determined by // RandomMatrixOptions. - // - // Caller owns the result. - static BlockSparseMatrix* CreateRandomMatrix( + static std::unique_ptr CreateRandomMatrix( const RandomMatrixOptions& options); private: diff --git a/internal/ceres/block_sparse_matrix_test.cc b/internal/ceres/block_sparse_matrix_test.cc index 02d3fb123..747cdeff5 100644 --- a/internal/ceres/block_sparse_matrix_test.cc +++ b/internal/ceres/block_sparse_matrix_test.cc @@ -46,12 +46,12 @@ namespace internal { class BlockSparseMatrixTest : public ::testing::Test { protected: void SetUp() final { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(2)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(2); CHECK(problem != nullptr); A_.reset(down_cast(problem->A.release())); - problem.reset(CreateLinearLeastSquaresProblemFromId(1)); + problem = CreateLinearLeastSquaresProblemFromId(1); CHECK(problem != nullptr); B_.reset(down_cast(problem->A.release())); @@ -110,15 +110,15 @@ TEST_F(BlockSparseMatrixTest, ToDenseMatrixTest) { } TEST_F(BlockSparseMatrixTest, AppendRows) { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(2)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(2); std::unique_ptr m( down_cast(problem->A.release())); A_->AppendRows(*m); EXPECT_EQ(A_->num_rows(), 2 * m->num_rows()); EXPECT_EQ(A_->num_cols(), m->num_cols()); - problem.reset(CreateLinearLeastSquaresProblemFromId(1)); + problem = CreateLinearLeastSquaresProblemFromId(1); std::unique_ptr m2( down_cast(problem->A.release())); B_->AppendRows(*m2); diff --git a/internal/ceres/bundle_adjustment_test_util.h b/internal/ceres/bundle_adjustment_test_util.h index 074931fac..8af935d0e 100644 --- a/internal/ceres/bundle_adjustment_test_util.h +++ b/internal/ceres/bundle_adjustment_test_util.h @@ -149,10 +149,11 @@ class BundleAdjustmentProblem { // point_index()[i] respectively. double* camera = cameras + 9 * camera_index_[i]; double* point = points + 3 * point_index()[i]; - problem_.AddResidualBlock(cost_function, NULL, camera, point); + problem_.AddResidualBlock(cost_function, nullptr, camera, point); } - options_.linear_solver_ordering.reset(new ParameterBlockOrdering); + options_.linear_solver_ordering = + std::make_shared(); // The points come before the cameras. for (int i = 0; i < num_points_; ++i) { diff --git a/internal/ceres/c_api.cc b/internal/ceres/c_api.cc index a604e5610..ddb8a98c6 100644 --- a/internal/ceres/c_api.cc +++ b/internal/ceres/c_api.cc @@ -148,24 +148,25 @@ ceres_residual_block_id_t* ceres_problem_add_residual_block( double** parameters) { Problem* ceres_problem = reinterpret_cast(problem); - ceres::CostFunction* callback_cost_function = - new CallbackCostFunction(cost_function, - cost_function_data, - num_residuals, - num_parameter_blocks, - parameter_block_sizes); + auto callback_cost_function = + std::make_unique(cost_function, + cost_function_data, + num_residuals, + num_parameter_blocks, + parameter_block_sizes); - ceres::LossFunction* callback_loss_function = NULL; - if (loss_function != NULL) { - callback_loss_function = - new CallbackLossFunction(loss_function, loss_function_data); + std::unique_ptr callback_loss_function; + if (loss_function != nullptr) { + callback_loss_function = std::make_unique( + loss_function, loss_function_data); } std::vector parameter_blocks(parameters, parameters + num_parameter_blocks); return reinterpret_cast( - ceres_problem->AddResidualBlock( - callback_cost_function, callback_loss_function, parameter_blocks)); + ceres_problem->AddResidualBlock(callback_cost_function.release(), + callback_loss_function.release(), + parameter_blocks)); } void ceres_solve(ceres_problem_t* c_problem) { diff --git a/internal/ceres/c_api_test.cc b/internal/ceres/c_api_test.cc index 043f6ab26..247311635 100644 --- a/internal/ceres/c_api_test.cc +++ b/internal/ceres/c_api_test.cc @@ -121,13 +121,13 @@ static int exponential_residual(void* user_data, double c = parameters[1][0]; residuals[0] = y - exp(m * x + c); - if (jacobians == NULL) { + if (jacobians == nullptr) { return 1; } - if (jacobians[0] != NULL) { + if (jacobians[0] != nullptr) { jacobians[0][0] = -x * exp(m * x + c); // dr/dm } - if (jacobians[1] != NULL) { + if (jacobians[1] != nullptr) { jacobians[1][0] = -exp(m * x + c); // dr/dc } return 1; @@ -148,8 +148,8 @@ TEST(C_API, SimpleEndToEndTest) { problem, exponential_residual, // Cost function &data[2 * i], // Points to the (x,y) measurement - NULL, // Loss function - NULL, // Loss function user data + nullptr, // Loss function + nullptr, // Loss function user data 1, // Number of residuals 2, // Number of parameter blocks parameter_sizes, diff --git a/internal/ceres/callbacks.cc b/internal/ceres/callbacks.cc index 70e2409d1..7a4381c29 100644 --- a/internal/ceres/callbacks.cc +++ b/internal/ceres/callbacks.cc @@ -30,6 +30,7 @@ #include "ceres/callbacks.h" +#include #include // NO LINT #include "ceres/program.h" @@ -69,9 +70,7 @@ GradientProblemSolverStateUpdatingCallback:: CallbackReturnType GradientProblemSolverStateUpdatingCallback::operator()( const IterationSummary& summary) { if (summary.step_is_successful) { - std::copy(internal_parameters_, - internal_parameters_ + num_parameters_, - user_parameters_); + std::copy_n(internal_parameters_, num_parameters_, user_parameters_); } return SOLVER_CONTINUE; } diff --git a/internal/ceres/canonical_views_clustering.cc b/internal/ceres/canonical_views_clustering.cc index 740ab8edb..51cd4cb30 100644 --- a/internal/ceres/canonical_views_clustering.cc +++ b/internal/ceres/canonical_views_clustering.cc @@ -85,11 +85,11 @@ void ComputeCanonicalViewsClustering( const WeightedGraph& graph, vector* centers, IntMap* membership) { - time_t start_time = time(NULL); + time_t start_time = time(nullptr); CanonicalViewsClustering cv; cv.ComputeClustering(options, graph, centers, membership); VLOG(2) << "Canonical views clustering time (secs): " - << time(NULL) - start_time; + << time(nullptr) - start_time; } // Implementation of CanonicalViewsClustering @@ -107,7 +107,7 @@ void CanonicalViewsClustering::ComputeClustering( IntSet valid_views; FindValidViews(&valid_views); - while (valid_views.size() > 0) { + while (!valid_views.empty()) { // Find the next best canonical view. double best_difference = -std::numeric_limits::max(); int best_view = 0; diff --git a/internal/ceres/casts.h b/internal/ceres/casts.h index d13707131..21445c8f5 100644 --- a/internal/ceres/casts.h +++ b/internal/ceres/casts.h @@ -32,7 +32,6 @@ #define CERES_INTERNAL_CASTS_H_ #include -#include // For NULL. namespace ceres { @@ -86,6 +85,7 @@ inline To implicit_cast(typename identity_::type const& f) { // if (dynamic_cast(foo)) HandleASubclass2Object(foo); // You should design the code some other way not to need this. +// TODO(sameeragarwal): Modernize this. template // use like this: down_cast(foo); inline To down_cast(From* f) { // so we only accept pointers // Ensures that To is a sub-type of From *. This test is here only @@ -95,11 +95,11 @@ inline To down_cast(From* f) { // so we only accept pointers // TODO(csilvers): This should use COMPILE_ASSERT. if (false) { - implicit_cast(NULL); + implicit_cast(nullptr); } // uses RTTI in dbg and fastbuild. asserts are disabled in opt builds. - assert(f == NULL || dynamic_cast(f) != NULL); // NOLINT + assert(f == nullptr || dynamic_cast(f) != nullptr); // NOLINT return static_cast(f); } diff --git a/internal/ceres/cgnr_linear_operator.h b/internal/ceres/cgnr_linear_operator.h index a0d9e85ac..569ecdc87 100644 --- a/internal/ceres/cgnr_linear_operator.h +++ b/internal/ceres/cgnr_linear_operator.h @@ -94,7 +94,7 @@ class CgnrLinearOperator : public LinearOperator { A_.LeftMultiply(z_.get(), y); // y = y + DtDx - if (D_ != NULL) { + if (D_ != nullptr) { int n = A_.num_cols(); VectorRef(y, n).array() += ConstVectorRef(D_, n).array().square() * ConstVectorRef(x, n).array(); diff --git a/internal/ceres/cgnr_solver.cc b/internal/ceres/cgnr_solver.cc index 2f38d3038..e89305f89 100644 --- a/internal/ceres/cgnr_solver.cc +++ b/internal/ceres/cgnr_solver.cc @@ -30,6 +30,8 @@ #include "ceres/cgnr_solver.h" +#include + #include "ceres/block_jacobi_preconditioner.h" #include "ceres/cgnr_linear_operator.h" #include "ceres/conjugate_gradients_solver.h" @@ -70,7 +72,7 @@ LinearSolver::Summary CgnrSolver::SolveImpl( if (!preconditioner_) { if (options_.preconditioner_type == JACOBI) { - preconditioner_.reset(new BlockJacobiPreconditioner(*A)); + preconditioner_ = std::make_unique(*A); } else if (options_.preconditioner_type == SUBSET) { Preconditioner::Options preconditioner_options; preconditioner_options.type = SUBSET; @@ -81,8 +83,8 @@ LinearSolver::Summary CgnrSolver::SolveImpl( preconditioner_options.use_postordering = options_.use_postordering; preconditioner_options.num_threads = options_.num_threads; preconditioner_options.context = options_.context; - preconditioner_.reset( - new SubsetPreconditioner(preconditioner_options, *A)); + preconditioner_ = + std::make_unique(preconditioner_options, *A); } } diff --git a/internal/ceres/compressed_row_jacobian_writer.cc b/internal/ceres/compressed_row_jacobian_writer.cc index c6a300198..25269400a 100644 --- a/internal/ceres/compressed_row_jacobian_writer.cc +++ b/internal/ceres/compressed_row_jacobian_writer.cc @@ -30,7 +30,9 @@ #include "ceres/compressed_row_jacobian_writer.h" +#include #include +#include #include #include @@ -81,10 +83,12 @@ void CompressedRowJacobianWriter::GetOrderedParameterBlocks( make_pair(parameter_block->index(), j)); } } - sort(evaluated_jacobian_blocks->begin(), evaluated_jacobian_blocks->end()); + std::sort(evaluated_jacobian_blocks->begin(), + evaluated_jacobian_blocks->end()); } -SparseMatrix* CompressedRowJacobianWriter::CreateJacobian() const { +std::unique_ptr CompressedRowJacobianWriter::CreateJacobian() + const { const vector& residual_blocks = program_->residual_blocks(); int total_num_residuals = program_->NumResiduals(); @@ -108,10 +112,11 @@ SparseMatrix* CompressedRowJacobianWriter::CreateJacobian() const { // Allocate more space than needed to store the jacobian so that when the LM // algorithm adds the diagonal, no reallocation is necessary. This reduces // peak memory usage significantly. - CompressedRowSparseMatrix* jacobian = new CompressedRowSparseMatrix( - total_num_residuals, - total_num_effective_parameters, - num_jacobian_nonzeros + total_num_effective_parameters); + std::unique_ptr jacobian = + std::make_unique( + total_num_residuals, + total_num_effective_parameters, + num_jacobian_nonzeros + total_num_effective_parameters); // At this stage, the CompressedRowSparseMatrix is an invalid state. But this // seems to be the only way to construct it without doing a memory copy. @@ -183,7 +188,7 @@ SparseMatrix* CompressedRowJacobianWriter::CreateJacobian() const { } CHECK_EQ(num_jacobian_nonzeros, rows[total_num_residuals]); - PopulateJacobianRowAndColumnBlockVectors(program_, jacobian); + PopulateJacobianRowAndColumnBlockVectors(program_, jacobian.get()); return jacobian; } diff --git a/internal/ceres/compressed_row_jacobian_writer.h b/internal/ceres/compressed_row_jacobian_writer.h index b1251ca5c..2765a5835 100644 --- a/internal/ceres/compressed_row_jacobian_writer.h +++ b/internal/ceres/compressed_row_jacobian_writer.h @@ -93,7 +93,7 @@ class CompressedRowJacobianWriter { return ScratchEvaluatePreparer::Create(*program_, num_threads); } - SparseMatrix* CreateJacobian() const; + std::unique_ptr CreateJacobian() const; void Write(int residual_id, int residual_offset, diff --git a/internal/ceres/compressed_row_sparse_matrix.cc b/internal/ceres/compressed_row_sparse_matrix.cc index b9e2715a6..766e41379 100644 --- a/internal/ceres/compressed_row_sparse_matrix.cc +++ b/internal/ceres/compressed_row_sparse_matrix.cc @@ -104,7 +104,7 @@ void TransposeForCompressedRowSparseStructure(const int num_rows, const int c = cols[idx]; const int transpose_idx = transpose_rows[c]++; transpose_cols[transpose_idx] = r; - if (values != NULL && transpose_values != NULL) { + if (values != nullptr && transpose_values != nullptr) { transpose_values[transpose_idx] = values[idx]; } } @@ -174,18 +174,20 @@ CompressedRowSparseMatrix::CompressedRowSparseMatrix(int num_rows, cols_.size() * sizeof(double); // NOLINT } -CompressedRowSparseMatrix* CompressedRowSparseMatrix::FromTripletSparseMatrix( +std::unique_ptr +CompressedRowSparseMatrix::FromTripletSparseMatrix( const TripletSparseMatrix& input) { return CompressedRowSparseMatrix::FromTripletSparseMatrix(input, false); } -CompressedRowSparseMatrix* +std::unique_ptr CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed( const TripletSparseMatrix& input) { return CompressedRowSparseMatrix::FromTripletSparseMatrix(input, true); } -CompressedRowSparseMatrix* CompressedRowSparseMatrix::FromTripletSparseMatrix( +std::unique_ptr +CompressedRowSparseMatrix::FromTripletSparseMatrix( const TripletSparseMatrix& input, bool transpose) { int num_rows = input.num_rows(); int num_cols = input.num_cols(); @@ -214,8 +216,9 @@ CompressedRowSparseMatrix* CompressedRowSparseMatrix::FromTripletSparseMatrix( input.num_nonzeros() * sizeof(int) + // NOLINT input.num_nonzeros() * sizeof(double)); // NOLINT - CompressedRowSparseMatrix* output = - new CompressedRowSparseMatrix(num_rows, num_cols, input.num_nonzeros()); + std::unique_ptr output = + std::make_unique( + num_rows, num_cols, input.num_nonzeros()); if (num_rows == 0) { // No data to copy. @@ -533,7 +536,8 @@ void CompressedRowSparseMatrix::SetMaxNumNonZeros(int num_nonzeros) { values_.resize(num_nonzeros); } -CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( +std::unique_ptr +CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( const double* diagonal, const vector& blocks) { int num_rows = 0; int num_nonzeros = 0; @@ -542,8 +546,9 @@ CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( num_nonzeros += blocks[i] * blocks[i]; } - CompressedRowSparseMatrix* matrix = - new CompressedRowSparseMatrix(num_rows, num_rows, num_nonzeros); + std::unique_ptr matrix = + std::make_unique( + num_rows, num_rows, num_nonzeros); int* rows = matrix->mutable_rows(); int* cols = matrix->mutable_cols(); @@ -573,9 +578,11 @@ CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( return matrix; } -CompressedRowSparseMatrix* CompressedRowSparseMatrix::Transpose() const { - CompressedRowSparseMatrix* transpose = - new CompressedRowSparseMatrix(num_cols_, num_rows_, num_nonzeros()); +std::unique_ptr +CompressedRowSparseMatrix::Transpose() const { + std::unique_ptr transpose = + std::make_unique( + num_cols_, num_rows_, num_nonzeros()); switch (storage_type_) { case UNSYMMETRIC: @@ -612,7 +619,8 @@ CompressedRowSparseMatrix* CompressedRowSparseMatrix::Transpose() const { return transpose; } -CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateRandomMatrix( +std::unique_ptr +CompressedRowSparseMatrix::CreateRandomMatrix( CompressedRowSparseMatrix::RandomMatrixOptions options) { CHECK_GT(options.num_row_blocks, 0); CHECK_GT(options.min_row_block_size, 0); @@ -714,7 +722,7 @@ CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateRandomMatrix( const int num_rows = std::accumulate(row_blocks.begin(), row_blocks.end(), 0); const int num_cols = std::accumulate(col_blocks.begin(), col_blocks.end(), 0); const bool kDoNotTranspose = false; - CompressedRowSparseMatrix* matrix = + std::unique_ptr matrix = CompressedRowSparseMatrix::FromTripletSparseMatrix( TripletSparseMatrix( num_rows, num_cols, tsm_rows, tsm_cols, tsm_values), diff --git a/internal/ceres/compressed_row_sparse_matrix.h b/internal/ceres/compressed_row_sparse_matrix.h index a92fc9100..0805ee78f 100644 --- a/internal/ceres/compressed_row_sparse_matrix.h +++ b/internal/ceres/compressed_row_sparse_matrix.h @@ -63,9 +63,7 @@ class CERES_EXPORT_INTERNAL CompressedRowSparseMatrix : public SparseMatrix { // entries. // // The storage type of the matrix is set to UNSYMMETRIC. - // - // Caller owns the result. - static CompressedRowSparseMatrix* FromTripletSparseMatrix( + static std::unique_ptr FromTripletSparseMatrix( const TripletSparseMatrix& input); // Create a matrix with the same content as the TripletSparseMatrix @@ -73,10 +71,8 @@ class CERES_EXPORT_INTERNAL CompressedRowSparseMatrix : public SparseMatrix { // entries. // // The storage type of the matrix is set to UNSYMMETRIC. - // - // Caller owns the result. - static CompressedRowSparseMatrix* FromTripletSparseMatrixTransposed( - const TripletSparseMatrix& input); + static std::unique_ptr + FromTripletSparseMatrixTransposed(const TripletSparseMatrix& input); // Use this constructor only if you know what you are doing. This // creates a "blank" matrix with the appropriate amount of memory @@ -124,7 +120,7 @@ class CERES_EXPORT_INTERNAL CompressedRowSparseMatrix : public SparseMatrix { void ToCRSMatrix(CRSMatrix* matrix) const; - CompressedRowSparseMatrix* Transpose() const; + std::unique_ptr Transpose() const; // Destructive array resizing method. void SetMaxNumNonZeros(int num_nonzeros); @@ -154,9 +150,7 @@ class CERES_EXPORT_INTERNAL CompressedRowSparseMatrix : public SparseMatrix { // Create a block diagonal CompressedRowSparseMatrix with the given // block structure. The individual blocks are assumed to be laid out // contiguously in the diagonal array, one block at a time. - // - // Caller owns the result. - static CompressedRowSparseMatrix* CreateBlockDiagonalMatrix( + static std::unique_ptr CreateBlockDiagonalMatrix( const double* diagonal, const std::vector& blocks); // Options struct to control the generation of random block sparse @@ -198,13 +192,11 @@ class CERES_EXPORT_INTERNAL CompressedRowSparseMatrix : public SparseMatrix { // Create a random CompressedRowSparseMatrix whose entries are // normally distributed and whose structure is determined by // RandomMatrixOptions. - // - // Caller owns the result. - static CompressedRowSparseMatrix* CreateRandomMatrix( + static std::unique_ptr CreateRandomMatrix( RandomMatrixOptions options); private: - static CompressedRowSparseMatrix* FromTripletSparseMatrix( + static std::unique_ptr FromTripletSparseMatrix( const TripletSparseMatrix& input, bool transpose); int num_rows_; diff --git a/internal/ceres/compressed_row_sparse_matrix_test.cc b/internal/ceres/compressed_row_sparse_matrix_test.cc index 91f3ba424..3a2768cdc 100644 --- a/internal/ceres/compressed_row_sparse_matrix_test.cc +++ b/internal/ceres/compressed_row_sparse_matrix_test.cc @@ -30,8 +30,10 @@ #include "ceres/compressed_row_sparse_matrix.h" +#include #include #include +#include #include "Eigen/SparseCore" #include "ceres/casts.h" @@ -72,13 +74,13 @@ static void CompareMatrices(const SparseMatrix* a, const SparseMatrix* b) { class CompressedRowSparseMatrixTest : public ::testing::Test { protected: void SetUp() final { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(1)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(1); CHECK(problem != nullptr); tsm.reset(down_cast(problem->A.release())); - crsm.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm)); + crsm = CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm); num_rows = tsm->num_rows(); num_cols = tsm->num_cols(); @@ -132,8 +134,8 @@ TEST_F(CompressedRowSparseMatrixTest, AppendRows) { tsm_appendage.Resize(i, num_cols); tsm->AppendRows(tsm_appendage); - std::unique_ptr crsm_appendage( - CompressedRowSparseMatrix::FromTripletSparseMatrix(tsm_appendage)); + std::unique_ptr crsm_appendage = + CompressedRowSparseMatrix::FromTripletSparseMatrix(tsm_appendage); crsm->AppendRows(*crsm_appendage); CompareMatrices(tsm.get(), crsm.get()); @@ -143,7 +145,8 @@ TEST_F(CompressedRowSparseMatrixTest, AppendRows) { TEST_F(CompressedRowSparseMatrixTest, AppendAndDeleteBlockDiagonalMatrix) { int num_diagonal_rows = crsm->num_cols(); - std::unique_ptr diagonal(new double[num_diagonal_rows]); + std::unique_ptr diagonal = + std::make_unique(num_diagonal_rows); for (int i = 0; i < num_diagonal_rows; ++i) { diagonal[i] = i; } @@ -156,9 +159,9 @@ TEST_F(CompressedRowSparseMatrixTest, AppendAndDeleteBlockDiagonalMatrix) { const vector pre_row_blocks = crsm->row_blocks(); const vector pre_col_blocks = crsm->col_blocks(); - std::unique_ptr appendage( + std::unique_ptr appendage = CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( - diagonal.get(), row_and_column_blocks)); + diagonal.get(), row_and_column_blocks); crsm->AppendRows(*appendage); @@ -220,9 +223,9 @@ TEST(CompressedRowSparseMatrix, CreateBlockDiagonalMatrix) { diagonal(i) = i + 1; } - std::unique_ptr matrix( + std::unique_ptr matrix = CompressedRowSparseMatrix::CreateBlockDiagonalMatrix(diagonal.data(), - blocks)); + blocks); EXPECT_EQ(matrix->num_rows(), 5); EXPECT_EQ(matrix->num_cols(), 5); @@ -305,7 +308,7 @@ TEST(CompressedRowSparseMatrix, Transpose) { std::copy(values, values + 17, cols); - std::unique_ptr transpose(matrix.Transpose()); + std::unique_ptr transpose = matrix.Transpose(); ASSERT_EQ(transpose->row_blocks().size(), matrix.col_blocks().size()); for (int i = 0; i < transpose->row_blocks().size(); ++i) { @@ -333,10 +336,10 @@ TEST(CompressedRowSparseMatrix, FromTripletSparseMatrix) { const int kNumTrials = 10; for (int i = 0; i < kNumTrials; ++i) { - std::unique_ptr tsm( - TripletSparseMatrix::CreateRandomMatrix(options)); - std::unique_ptr crsm( - CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm)); + std::unique_ptr tsm = + TripletSparseMatrix::CreateRandomMatrix(options); + std::unique_ptr crsm = + CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm); Matrix expected; tsm->ToDenseMatrix(&expected); @@ -359,10 +362,10 @@ TEST(CompressedRowSparseMatrix, FromTripletSparseMatrixTransposed) { const int kNumTrials = 10; for (int i = 0; i < kNumTrials; ++i) { - std::unique_ptr tsm( - TripletSparseMatrix::CreateRandomMatrix(options)); - std::unique_ptr crsm( - CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm)); + std::unique_ptr tsm = + TripletSparseMatrix::CreateRandomMatrix(options); + std::unique_ptr crsm = + CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm); Matrix tmp; tsm->ToDenseMatrix(&tmp); @@ -416,8 +419,8 @@ TEST_P(RightMultiplyTest, _) { options.max_row_block_size = kMaxBlockSize; options.block_density = std::max(0.5, RandDouble()); options.storage_type = ::testing::get<0>(param); - std::unique_ptr matrix( - CompressedRowSparseMatrix::CreateRandomMatrix(options)); + std::unique_ptr matrix = + CompressedRowSparseMatrix::CreateRandomMatrix(options); const int num_rows = matrix->num_rows(); const int num_cols = matrix->num_cols(); @@ -485,8 +488,8 @@ TEST_P(LeftMultiplyTest, _) { options.max_row_block_size = kMaxBlockSize; options.block_density = std::max(0.5, RandDouble()); options.storage_type = ::testing::get<0>(param); - std::unique_ptr matrix( - CompressedRowSparseMatrix::CreateRandomMatrix(options)); + std::unique_ptr matrix = + CompressedRowSparseMatrix::CreateRandomMatrix(options); const int num_rows = matrix->num_rows(); const int num_cols = matrix->num_cols(); @@ -554,8 +557,8 @@ TEST_P(SquaredColumnNormTest, _) { options.max_row_block_size = kMaxBlockSize; options.block_density = std::max(0.5, RandDouble()); options.storage_type = ::testing::get<0>(param); - std::unique_ptr matrix( - CompressedRowSparseMatrix::CreateRandomMatrix(options)); + std::unique_ptr matrix = + CompressedRowSparseMatrix::CreateRandomMatrix(options); const int num_cols = matrix->num_cols(); Vector actual(num_cols); diff --git a/internal/ceres/conditioned_cost_function.cc b/internal/ceres/conditioned_cost_function.cc index fb4c52af0..a9013a23d 100644 --- a/internal/ceres/conditioned_cost_function.cc +++ b/internal/ceres/conditioned_cost_function.cc @@ -98,7 +98,7 @@ bool ConditionedCostFunction::Evaluate(double const* const* parameters, double** conditioner_derivative_pointer2 = &conditioner_derivative_pointer; if (!jacobians) { - conditioner_derivative_pointer2 = NULL; + conditioner_derivative_pointer2 = nullptr; } double unconditioned_residual = residuals[r]; diff --git a/internal/ceres/conjugate_gradients_solver.cc b/internal/ceres/conjugate_gradients_solver.cc index 3019628a1..436a5b1ce 100644 --- a/internal/ceres/conjugate_gradients_solver.cc +++ b/internal/ceres/conjugate_gradients_solver.cc @@ -112,7 +112,7 @@ LinearSolver::Summary ConjugateGradientsSolver::Solve( for (summary.num_iterations = 1;; ++summary.num_iterations) { // Apply preconditioner - if (per_solve_options.preconditioner != NULL) { + if (per_solve_options.preconditioner != nullptr) { z.setZero(); per_solve_options.preconditioner->RightMultiply(r.data(), z.data()); } else { diff --git a/internal/ceres/coordinate_descent_minimizer.cc b/internal/ceres/coordinate_descent_minimizer.cc index 3c566c73f..86ab83885 100644 --- a/internal/ceres/coordinate_descent_minimizer.cc +++ b/internal/ceres/coordinate_descent_minimizer.cc @@ -135,8 +135,10 @@ void CoordinateDescentMinimizer::Minimize(const Minimizer::Options& options, parameter_block->SetConstant(); } - std::unique_ptr linear_solvers( - new LinearSolver*[options.num_threads]); + std::vector> linear_solvers( + options.num_threads); + // std::unique_ptr linear_solvers( + // new LinearSolver*[options.num_threads]); LinearSolver::Options linear_solver_options; linear_solver_options.type = DENSE_QR; @@ -188,7 +190,7 @@ void CoordinateDescentMinimizer::Minimize(const Minimizer::Options& options, // we are fine. Solver::Summary inner_summary; Solve(&inner_program, - linear_solvers[thread_id], + linear_solvers[thread_id].get(), parameters + parameter_block->state_offset(), &inner_summary); @@ -204,9 +206,9 @@ void CoordinateDescentMinimizer::Minimize(const Minimizer::Options& options, parameter_blocks_[i]->SetVarying(); } - for (int i = 0; i < options.num_threads; ++i) { - delete linear_solvers[i]; - } + // for (int i = 0; i < options.num_threads; ++i) { + // delete linear_solvers[i]; + //} } // Solve the optimization problem for one parameter block. @@ -221,17 +223,16 @@ void CoordinateDescentMinimizer::Solve(Program* program, string error; Minimizer::Options minimizer_options; - minimizer_options.evaluator.reset( - Evaluator::Create(evaluator_options_, program, &error)); + minimizer_options.evaluator = + Evaluator::Create(evaluator_options_, program, &error); CHECK(minimizer_options.evaluator != nullptr); - minimizer_options.jacobian.reset( - minimizer_options.evaluator->CreateJacobian()); + minimizer_options.jacobian = minimizer_options.evaluator->CreateJacobian(); CHECK(minimizer_options.jacobian != nullptr); TrustRegionStrategy::Options trs_options; trs_options.linear_solver = linear_solver; - minimizer_options.trust_region_strategy.reset( - TrustRegionStrategy::Create(trs_options)); + minimizer_options.trust_region_strategy = + TrustRegionStrategy::Create(trs_options); CHECK(minimizer_options.trust_region_strategy != nullptr); minimizer_options.is_silent = true; @@ -263,12 +264,12 @@ bool CoordinateDescentMinimizer::IsOrderingValid( // of independent sets of decreasing size and invert it. This // seems to work better in practice, i.e., Cameras before // points. -ParameterBlockOrdering* CoordinateDescentMinimizer::CreateOrdering( - const Program& program) { - std::unique_ptr ordering(new ParameterBlockOrdering); +std::shared_ptr +CoordinateDescentMinimizer::CreateOrdering(const Program& program) { + auto ordering = std::make_shared(); ComputeRecursiveIndependentSetOrdering(program, ordering.get()); ordering->Reverse(); - return ordering.release(); + return ordering; } } // namespace internal diff --git a/internal/ceres/coordinate_descent_minimizer.h b/internal/ceres/coordinate_descent_minimizer.h index 90ba2cf19..d46e6a85a 100644 --- a/internal/ceres/coordinate_descent_minimizer.h +++ b/internal/ceres/coordinate_descent_minimizer.h @@ -81,7 +81,8 @@ class CoordinateDescentMinimizer : public Minimizer { // of independent sets of decreasing size and invert it. This // seems to work better in practice, i.e., Cameras before // points. - static ParameterBlockOrdering* CreateOrdering(const Program& program); + static std::shared_ptr CreateOrdering( + const Program& program); private: void Solve(Program* program, diff --git a/internal/ceres/corrector.cc b/internal/ceres/corrector.cc index 6a79a06a5..bf3ba9c57 100644 --- a/internal/ceres/corrector.cc +++ b/internal/ceres/corrector.cc @@ -111,7 +111,7 @@ Corrector::Corrector(const double sq_norm, const double rho[3]) { } void Corrector::CorrectResiduals(const int num_rows, double* residuals) { - DCHECK(residuals != NULL); + DCHECK(residuals != nullptr); // Equation 11 in BANS. VectorRef(residuals, num_rows) *= residual_scaling_; } @@ -120,8 +120,8 @@ void Corrector::CorrectJacobian(const int num_rows, const int num_cols, double* residuals, double* jacobian) { - DCHECK(residuals != NULL); - DCHECK(jacobian != NULL); + DCHECK(residuals != nullptr); + DCHECK(jacobian != nullptr); // The common case (rho[2] <= 0). if (alpha_sq_norm_ == 0.0) { diff --git a/internal/ceres/cost_function_to_functor_test.cc b/internal/ceres/cost_function_to_functor_test.cc index 11f47e330..80efd6699 100644 --- a/internal/ceres/cost_function_to_functor_test.cc +++ b/internal/ceres/cost_function_to_functor_test.cc @@ -92,9 +92,9 @@ static void ExpectCostFunctionsAreEqual( } EXPECT_TRUE( - cost_function.Evaluate(parameter_blocks.get(), residuals.get(), NULL)); + cost_function.Evaluate(parameter_blocks.get(), residuals.get(), nullptr)); EXPECT_TRUE(actual_cost_function.Evaluate( - parameter_blocks.get(), actual_residuals.get(), NULL)); + parameter_blocks.get(), actual_residuals.get(), nullptr)); for (int i = 0; i < num_residuals; ++i) { EXPECT_NEAR(residuals[i], actual_residuals[i], kTolerance) << "residual id: " << i; diff --git a/internal/ceres/covariance.cc b/internal/ceres/covariance.cc index ead3287db..d63dd3789 100644 --- a/internal/ceres/covariance.cc +++ b/internal/ceres/covariance.cc @@ -39,12 +39,11 @@ namespace ceres { -using std::make_pair; using std::pair; using std::vector; Covariance::Covariance(const Covariance::Options& options) { - impl_.reset(new internal::CovarianceImpl(options)); + impl_ = std::make_unique(options); } Covariance::~Covariance() = default; diff --git a/internal/ceres/covariance_impl.cc b/internal/ceres/covariance_impl.cc index 8c003840e..507560922 100644 --- a/internal/ceres/covariance_impl.cc +++ b/internal/ceres/covariance_impl.cc @@ -83,7 +83,7 @@ CovarianceImpl::~CovarianceImpl() = default; template void CheckForDuplicates(std::vector blocks) { - sort(blocks.begin(), blocks.end()); + std::sort(blocks.begin(), blocks.end()); typename std::vector::iterator it = std::adjacent_find(blocks.begin(), blocks.end()); if (it != blocks.end()) { @@ -117,7 +117,7 @@ bool CovarianceImpl::Compute(const CovarianceBlocks& covariance_blocks, covariance_blocks); problem_ = problem; parameter_block_to_row_index_.clear(); - covariance_matrix_.reset(nullptr); + covariance_matrix_ = nullptr; is_valid_ = (ComputeCovarianceSparsity(covariance_blocks, problem) && ComputeCovarianceValues()); is_computed_ = true; @@ -448,9 +448,9 @@ bool CovarianceImpl::ComputeCovarianceSparsity( } } - if (covariance_blocks.size() == 0) { + if (covariance_blocks.empty()) { VLOG(2) << "No non-zero covariance blocks found"; - covariance_matrix_.reset(nullptr); + covariance_matrix_ = nullptr; return true; } @@ -460,8 +460,8 @@ bool CovarianceImpl::ComputeCovarianceSparsity( std::sort(covariance_blocks.begin(), covariance_blocks.end()); // Fill the sparsity pattern of the covariance matrix. - covariance_matrix_.reset( - new CompressedRowSparseMatrix(num_rows, num_rows, num_nonzeros)); + covariance_matrix_ = std::make_unique( + num_rows, num_rows, num_nonzeros); int* rows = covariance_matrix_->mutable_rows(); int* cols = covariance_matrix_->mutable_cols(); diff --git a/internal/ceres/covariance_test.cc b/internal/ceres/covariance_test.cc index e39b2aec2..b117a6428 100644 --- a/internal/ceres/covariance_test.cc +++ b/internal/ceres/covariance_test.cc @@ -72,12 +72,12 @@ class UnaryCostFunction : public CostFunction { residuals[i] = 1; } - if (jacobians == NULL) { + if (jacobians == nullptr) { return true; } - if (jacobians[0] != NULL) { - copy(jacobian_.begin(), jacobian_.end(), jacobians[0]); + if (jacobians[0] != nullptr) { + std::copy(jacobian_.begin(), jacobian_.end(), jacobians[0]); } return true; @@ -110,16 +110,16 @@ class BinaryCostFunction : public CostFunction { residuals[i] = 2; } - if (jacobians == NULL) { + if (jacobians == nullptr) { return true; } - if (jacobians[0] != NULL) { - copy(jacobian1_.begin(), jacobian1_.end(), jacobians[0]); + if (jacobians[0] != nullptr) { + std::copy(jacobian1_.begin(), jacobian1_.end(), jacobians[0]); } - if (jacobians[1] != NULL) { - copy(jacobian2_.begin(), jacobian2_.end(), jacobians[1]); + if (jacobians[1] != nullptr) { + std::copy(jacobian2_.begin(), jacobian2_.end(), jacobians[1]); } return true; @@ -143,13 +143,13 @@ TEST(CovarianceImpl, ComputeCovarianceSparsity) { // Add in random order Vector junk_jacobian = Vector::Zero(10); problem.AddResidualBlock( - new UnaryCostFunction(1, 1, junk_jacobian.data()), NULL, block1); + new UnaryCostFunction(1, 1, junk_jacobian.data()), nullptr, block1); problem.AddResidualBlock( - new UnaryCostFunction(1, 4, junk_jacobian.data()), NULL, block4); + new UnaryCostFunction(1, 4, junk_jacobian.data()), nullptr, block4); problem.AddResidualBlock( - new UnaryCostFunction(1, 3, junk_jacobian.data()), NULL, block3); + new UnaryCostFunction(1, 3, junk_jacobian.data()), nullptr, block3); problem.AddResidualBlock( - new UnaryCostFunction(1, 2, junk_jacobian.data()), NULL, block2); + new UnaryCostFunction(1, 2, junk_jacobian.data()), nullptr, block2); // Sparsity pattern // @@ -229,13 +229,13 @@ TEST(CovarianceImpl, ComputeCovarianceSparsityWithConstantParameterBlock) { // Add in random order Vector junk_jacobian = Vector::Zero(10); problem.AddResidualBlock( - new UnaryCostFunction(1, 1, junk_jacobian.data()), NULL, block1); + new UnaryCostFunction(1, 1, junk_jacobian.data()), nullptr, block1); problem.AddResidualBlock( - new UnaryCostFunction(1, 4, junk_jacobian.data()), NULL, block4); + new UnaryCostFunction(1, 4, junk_jacobian.data()), nullptr, block4); problem.AddResidualBlock( - new UnaryCostFunction(1, 3, junk_jacobian.data()), NULL, block3); + new UnaryCostFunction(1, 3, junk_jacobian.data()), nullptr, block3); problem.AddResidualBlock( - new UnaryCostFunction(1, 2, junk_jacobian.data()), NULL, block2); + new UnaryCostFunction(1, 2, junk_jacobian.data()), nullptr, block2); problem.SetParameterBlockConstant(block3); // Sparsity pattern @@ -310,12 +310,12 @@ TEST(CovarianceImpl, ComputeCovarianceSparsityWithFreeParameterBlock) { // Add in random order Vector junk_jacobian = Vector::Zero(10); problem.AddResidualBlock( - new UnaryCostFunction(1, 1, junk_jacobian.data()), NULL, block1); + new UnaryCostFunction(1, 1, junk_jacobian.data()), nullptr, block1); problem.AddResidualBlock( - new UnaryCostFunction(1, 4, junk_jacobian.data()), NULL, block4); + new UnaryCostFunction(1, 4, junk_jacobian.data()), nullptr, block4); problem.AddParameterBlock(block3, 3); problem.AddResidualBlock( - new UnaryCostFunction(1, 2, junk_jacobian.data()), NULL, block2); + new UnaryCostFunction(1, 2, junk_jacobian.data()), nullptr, block2); // Sparsity pattern // @@ -450,32 +450,34 @@ class CovarianceTest : public ::testing::Test { { double jacobian[] = {1.0, 0.0, 0.0, 1.0}; - problem_.AddResidualBlock(new UnaryCostFunction(2, 2, jacobian), NULL, x); + problem_.AddResidualBlock( + new UnaryCostFunction(2, 2, jacobian), nullptr, x); } { double jacobian[] = {2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 2.0}; - problem_.AddResidualBlock(new UnaryCostFunction(3, 3, jacobian), NULL, y); + problem_.AddResidualBlock( + new UnaryCostFunction(3, 3, jacobian), nullptr, y); } { double jacobian = 5.0; problem_.AddResidualBlock( - new UnaryCostFunction(1, 1, &jacobian), NULL, z); + new UnaryCostFunction(1, 1, &jacobian), nullptr, z); } { double jacobian1[] = {1.0, 2.0, 3.0}; double jacobian2[] = {-5.0, -6.0}; problem_.AddResidualBlock( - new BinaryCostFunction(1, 3, 2, jacobian1, jacobian2), NULL, y, x); + new BinaryCostFunction(1, 3, 2, jacobian1, jacobian2), nullptr, y, x); } { double jacobian1[] = {2.0}; double jacobian2[] = {3.0, -2.0}; problem_.AddResidualBlock( - new BinaryCostFunction(1, 1, 2, jacobian1, jacobian2), NULL, z, x); + new BinaryCostFunction(1, 1, 2, jacobian1, jacobian2), nullptr, z, x); } all_covariance_blocks_.push_back(make_pair(x, x)); @@ -1368,32 +1370,34 @@ class RankDeficientCovarianceTest : public CovarianceTest { { double jacobian[] = {1.0, 0.0, 0.0, 1.0}; - problem_.AddResidualBlock(new UnaryCostFunction(2, 2, jacobian), NULL, x); + problem_.AddResidualBlock( + new UnaryCostFunction(2, 2, jacobian), nullptr, x); } { double jacobian[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; - problem_.AddResidualBlock(new UnaryCostFunction(3, 3, jacobian), NULL, y); + problem_.AddResidualBlock( + new UnaryCostFunction(3, 3, jacobian), nullptr, y); } { double jacobian = 5.0; problem_.AddResidualBlock( - new UnaryCostFunction(1, 1, &jacobian), NULL, z); + new UnaryCostFunction(1, 1, &jacobian), nullptr, z); } { double jacobian1[] = {0.0, 0.0, 0.0}; double jacobian2[] = {-5.0, -6.0}; problem_.AddResidualBlock( - new BinaryCostFunction(1, 3, 2, jacobian1, jacobian2), NULL, y, x); + new BinaryCostFunction(1, 3, 2, jacobian1, jacobian2), nullptr, y, x); } { double jacobian1[] = {2.0}; double jacobian2[] = {3.0, -2.0}; problem_.AddResidualBlock( - new BinaryCostFunction(1, 1, 2, jacobian1, jacobian2), NULL, z, x); + new BinaryCostFunction(1, 1, 2, jacobian1, jacobian2), nullptr, z, x); } all_covariance_blocks_.push_back(make_pair(x, x)); @@ -1602,8 +1606,8 @@ class LargeScaleCovarianceTest : public ::testing::Test { void SetUp() final { num_parameter_blocks_ = 2000; parameter_block_size_ = 5; - parameters_.reset( - new double[parameter_block_size_ * num_parameter_blocks_]); + parameters_ = std::make_unique(parameter_block_size_ * + num_parameter_blocks_); Matrix jacobian(parameter_block_size_, parameter_block_size_); for (int i = 0; i < num_parameter_blocks_; ++i) { @@ -1614,7 +1618,7 @@ class LargeScaleCovarianceTest : public ::testing::Test { problem_.AddResidualBlock( new UnaryCostFunction( parameter_block_size_, parameter_block_size_, jacobian.data()), - NULL, + nullptr, block_i); for (int j = i; j < num_parameter_blocks_; ++j) { double* block_j = parameters_.get() + j * parameter_block_size_; diff --git a/internal/ceres/cubic_interpolation_test.cc b/internal/ceres/cubic_interpolation_test.cc index 3907d228c..03b1aae54 100644 --- a/internal/ceres/cubic_interpolation_test.cc +++ b/internal/ceres/cubic_interpolation_test.cc @@ -226,7 +226,7 @@ class CubicInterpolatorTest : public ::testing::Test { const double b, const double c, const double d) { - values_.reset(new double[kDataDimension * kNumSamples]); + values_ = std::make_unique(kDataDimension * kNumSamples); for (int x = 0; x < kNumSamples; ++x) { for (int dim = 0; dim < kDataDimension; ++dim) { @@ -335,7 +335,7 @@ class BiCubicInterpolatorTest : public ::testing::Test { template void RunPolynomialInterpolationTest(const Eigen::Matrix3d& coeff) { - values_.reset(new double[kNumRows * kNumCols * kDataDimension]); + values_ = std::make_unique(kNumRows * kNumCols * kDataDimension); coeff_ = coeff; double* v = values_.get(); for (int r = 0; r < kNumRows; ++r) { diff --git a/internal/ceres/cxsparse.cc b/internal/ceres/cxsparse.cc index 0167f9886..7800e8b0b 100644 --- a/internal/ceres/cxsparse.cc +++ b/internal/ceres/cxsparse.cc @@ -47,7 +47,7 @@ namespace internal { using std::vector; -CXSparse::CXSparse() : scratch_(NULL), scratch_size_(0) {} +CXSparse::CXSparse() : scratch_(nullptr), scratch_size_(0) {} CXSparse::~CXSparse() { if (scratch_size_ > 0) { @@ -116,7 +116,7 @@ cs_dis* CXSparse::BlockAnalyzeCholesky(cs_di* A, block_matrix.nzmax = block_rows.size(); block_matrix.p = &block_cols[0]; block_matrix.i = &block_rows[0]; - block_matrix.x = NULL; + block_matrix.x = nullptr; int* ordering = cs_amd(1, &block_matrix); vector block_ordering(num_row_blocks, -1); @@ -146,7 +146,7 @@ cs_dis* CXSparse::BlockAnalyzeCholesky(cs_di* A, if (symbolic_factor->lnz < 0) { cs_sfree(symbolic_factor); - symbolic_factor = NULL; + symbolic_factor = nullptr; } return symbolic_factor; @@ -206,8 +206,8 @@ CompressedRowSparseMatrix::StorageType CXSparseCholesky::StorageType() const { CXSparseCholesky::CXSparseCholesky(const OrderingType ordering_type) : ordering_type_(ordering_type), - symbolic_factor_(NULL), - numeric_factor_(NULL) {} + symbolic_factor_(nullptr), + numeric_factor_(nullptr) {} CXSparseCholesky::~CXSparseCholesky() { FreeSymbolicFactorization(); @@ -217,14 +217,14 @@ CXSparseCholesky::~CXSparseCholesky() { LinearSolverTerminationType CXSparseCholesky::Factorize( CompressedRowSparseMatrix* lhs, std::string* message) { CHECK_EQ(lhs->storage_type(), StorageType()); - if (lhs == NULL) { - *message = "Failure: Input lhs is NULL."; + if (lhs == nullptr) { + *message = "Failure: Input lhs is nullptr."; return LINEAR_SOLVER_FATAL_ERROR; } cs_di cs_lhs = cs_.CreateSparseMatrixTransposeView(lhs); - if (symbolic_factor_ == NULL) { + if (symbolic_factor_ == nullptr) { if (ordering_type_ == NATURAL) { symbolic_factor_ = cs_.AnalyzeCholeskyWithNaturalOrdering(&cs_lhs); } else { @@ -236,7 +236,7 @@ LinearSolverTerminationType CXSparseCholesky::Factorize( } } - if (symbolic_factor_ == NULL) { + if (symbolic_factor_ == nullptr) { *message = "CXSparse Failure : Symbolic factorization failed."; return LINEAR_SOLVER_FATAL_ERROR; } @@ -244,7 +244,7 @@ LinearSolverTerminationType CXSparseCholesky::Factorize( FreeNumericFactorization(); numeric_factor_ = cs_.Cholesky(&cs_lhs, symbolic_factor_); - if (numeric_factor_ == NULL) { + if (numeric_factor_ == nullptr) { *message = "CXSparse Failure : Numeric factorization failed."; return LINEAR_SOLVER_FAILURE; } @@ -255,7 +255,7 @@ LinearSolverTerminationType CXSparseCholesky::Factorize( LinearSolverTerminationType CXSparseCholesky::Solve(const double* rhs, double* solution, std::string* message) { - CHECK(numeric_factor_ != NULL) + CHECK(numeric_factor_ != nullptr) << "Solve called without a call to Factorize first."; const int num_cols = numeric_factor_->L->n; memcpy(solution, rhs, num_cols * sizeof(*solution)); @@ -264,16 +264,16 @@ LinearSolverTerminationType CXSparseCholesky::Solve(const double* rhs, } void CXSparseCholesky::FreeSymbolicFactorization() { - if (symbolic_factor_ != NULL) { + if (symbolic_factor_ != nullptr) { cs_.Free(symbolic_factor_); - symbolic_factor_ = NULL; + symbolic_factor_ = nullptr; } } void CXSparseCholesky::FreeNumericFactorization() { - if (numeric_factor_ != NULL) { + if (numeric_factor_ != nullptr) { cs_.Free(numeric_factor_); - numeric_factor_ = NULL; + numeric_factor_ = nullptr; } } diff --git a/internal/ceres/cxsparse.h b/internal/ceres/cxsparse.h index d1d14ecdd..63dddb598 100644 --- a/internal/ceres/cxsparse.h +++ b/internal/ceres/cxsparse.h @@ -80,7 +80,7 @@ class CXSparse { cs_di CreateSparseMatrixTransposeView(CompressedRowSparseMatrix* A); // Creates a new matrix from a triplet form. Deallocate the returned matrix - // with Free. May return NULL if the compression or allocation fails. + // with Free. May return nullptr if the compression or allocation fails. cs_di* CreateSparseMatrix(TripletSparseMatrix* A); // B = A' @@ -122,7 +122,7 @@ class CXSparse { const std::vector& col_blocks); // Compute an fill-reducing approximate minimum degree ordering of - // the matrix A. ordering should be non-NULL and should point to + // the matrix A. ordering should be non-nullptr and should point to // enough memory to hold the ordering for the rows of A. void ApproximateMinimumDegreeOrdering(cs_di* A, int* ordering); diff --git a/internal/ceres/dense_jacobian_writer.h b/internal/ceres/dense_jacobian_writer.h index efecf35d0..0d8681926 100644 --- a/internal/ceres/dense_jacobian_writer.h +++ b/internal/ceres/dense_jacobian_writer.h @@ -58,9 +58,9 @@ class DenseJacobianWriter { return ScratchEvaluatePreparer::Create(*program_, num_threads); } - SparseMatrix* CreateJacobian() const { - return new DenseSparseMatrix(program_->NumResiduals(), - program_->NumEffectiveParameters()); + std::unique_ptr CreateJacobian() const { + return std::make_unique( + program_->NumResiduals(), program_->NumEffectiveParameters()); } void Write(int residual_id, diff --git a/internal/ceres/dense_linear_solver_test.cc b/internal/ceres/dense_linear_solver_test.cc index 4dff5af4b..8110d8d4c 100644 --- a/internal/ceres/dense_linear_solver_test.cc +++ b/internal/ceres/dense_linear_solver_test.cc @@ -62,8 +62,8 @@ TEST_P(DenseLinearSolverTest, _) { Param param = GetParam(); const bool regularized = testing::get<2>(param); - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(testing::get<3>(param))); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(testing::get<3>(param)); DenseSparseMatrix lhs(*down_cast(problem->A.get())); const int num_cols = lhs.num_cols(); diff --git a/internal/ceres/dense_normal_cholesky_solver.cc b/internal/ceres/dense_normal_cholesky_solver.cc index b208d589d..2d3521c6a 100644 --- a/internal/ceres/dense_normal_cholesky_solver.cc +++ b/internal/ceres/dense_normal_cholesky_solver.cc @@ -72,7 +72,7 @@ LinearSolver::Summary DenseNormalCholeskySolver::SolveImpl( // rhs = A'b Vector rhs = A->matrix().transpose() * ConstVectorRef(b, num_rows); - if (per_solve_options.D != NULL) { + if (per_solve_options.D != nullptr) { ConstVectorRef D(per_solve_options.D, num_cols); lhs += D.array().square().matrix().asDiagonal(); } diff --git a/internal/ceres/dense_sparse_matrix_test.cc b/internal/ceres/dense_sparse_matrix_test.cc index 2fa7216ca..1bfd6c61d 100644 --- a/internal/ceres/dense_sparse_matrix_test.cc +++ b/internal/ceres/dense_sparse_matrix_test.cc @@ -70,13 +70,13 @@ static void CompareMatrices(const SparseMatrix* a, const SparseMatrix* b) { class DenseSparseMatrixTest : public ::testing::Test { protected: void SetUp() final { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(1)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(1); CHECK(problem != nullptr); tsm.reset(down_cast(problem->A.release())); - dsm.reset(new DenseSparseMatrix(*tsm)); + dsm = std::make_unique(*tsm); num_rows = tsm->num_rows(); num_cols = tsm->num_cols(); diff --git a/internal/ceres/dogleg_strategy.cc b/internal/ceres/dogleg_strategy.cc index 03ae22f7e..65f7ccd84 100644 --- a/internal/ceres/dogleg_strategy.cc +++ b/internal/ceres/dogleg_strategy.cc @@ -480,7 +480,7 @@ bool DoglegStrategy::FindMinimumOnTrustRegionBoundary(Vector2d* minimum) const { // Find the real parts y_i of its roots (not only the real roots). Vector roots_real; - if (!FindPolynomialRoots(polynomial, &roots_real, NULL)) { + if (!FindPolynomialRoots(polynomial, &roots_real, nullptr)) { // Failed to find the roots of the polynomial, i.e. the candidate // solutions of the constrained problem. Report this back to the caller. return false; diff --git a/internal/ceres/dogleg_strategy_test.cc b/internal/ceres/dogleg_strategy_test.cc index 0c20f2574..cd5fe0291 100644 --- a/internal/ceres/dogleg_strategy_test.cc +++ b/internal/ceres/dogleg_strategy_test.cc @@ -79,7 +79,7 @@ class DoglegStrategyFixtureEllipse : public Fixture { Matrix sqrtD = Ddiag.array().sqrt().matrix().asDiagonal(); Matrix jacobian = sqrtD * basis; - jacobian_.reset(new DenseSparseMatrix(jacobian)); + jacobian_ = std::make_unique(jacobian); Vector minimum(6); minimum << 1.0, 1.0, 1.0, 1.0, 1.0, 1.0; @@ -107,7 +107,7 @@ class DoglegStrategyFixtureValley : public Fixture { Ddiag << 1.0, 2.0, 4.0, 8.0, 16.0, 32.0; Matrix jacobian = Ddiag.asDiagonal(); - jacobian_.reset(new DenseSparseMatrix(jacobian)); + jacobian_ = std::make_unique(jacobian); Vector minimum(6); minimum << 0.0, 0.0, 1.0, 0.0, 0.0, 0.0; diff --git a/internal/ceres/dynamic_autodiff_cost_function_test.cc b/internal/ceres/dynamic_autodiff_cost_function_test.cc index 55d3fe129..7da81a95e 100644 --- a/internal/ceres/dynamic_autodiff_cost_function_test.cc +++ b/internal/ceres/dynamic_autodiff_cost_function_test.cc @@ -89,7 +89,7 @@ TEST(DynamicAutodiffCostFunctionTest, TestResiduals) { parameter_blocks[0] = ¶m_block_0[0]; parameter_blocks[1] = ¶m_block_1[0]; EXPECT_TRUE( - cost_function.Evaluate(¶meter_blocks[0], residuals.data(), NULL)); + cost_function.Evaluate(¶meter_blocks[0], residuals.data(), nullptr)); for (int r = 0; r < 10; ++r) { EXPECT_EQ(1.0 * r, residuals.at(r * 2)); EXPECT_EQ(-1.0 * r, residuals.at(r * 2 + 1)); @@ -189,7 +189,7 @@ TEST(DynamicAutodiffCostFunctionTest, JacobianWithFirstParameterBlockConstant) { jacobian_vect[0].resize(21 * 10, -100000); jacobian_vect[1].resize(21 * 5, -100000); vector jacobian; - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect[1].data()); // Test jacobian computation. @@ -240,7 +240,7 @@ TEST(DynamicAutodiffCostFunctionTest, jacobian_vect[1].resize(21 * 5, -100000); vector jacobian; jacobian.push_back(jacobian_vect[0].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); // Test jacobian computation. EXPECT_TRUE(cost_function.Evaluate( @@ -329,15 +329,14 @@ class ThreeParameterCostFunctorTest : public ::testing::Test { // Prepare the cost function. typedef DynamicAutoDiffCostFunction DynamicMyThreeParameterCostFunction; - DynamicMyThreeParameterCostFunction* cost_function = - new DynamicMyThreeParameterCostFunction( - new MyThreeParameterCostFunctor()); + auto cost_function = std::make_unique( + new MyThreeParameterCostFunctor()); cost_function->AddParameterBlock(1); cost_function->AddParameterBlock(2); cost_function->AddParameterBlock(3); cost_function->SetNumResiduals(7); - cost_function_.reset(cost_function); + cost_function_ = std::move(cost_function); // Setup jacobian data. jacobian_vect_.resize(3); @@ -430,7 +429,7 @@ class ThreeParameterCostFunctorTest : public ::testing::Test { TEST_F(ThreeParameterCostFunctorTest, TestThreeParameterResiduals) { vector residuals(7, -100000); EXPECT_TRUE(cost_function_->Evaluate( - parameter_blocks_.data(), residuals.data(), NULL)); + parameter_blocks_.data(), residuals.data(), nullptr)); for (int i = 0; i < 7; ++i) { EXPECT_EQ(expected_residuals_[i], residuals[i]); } @@ -469,9 +468,9 @@ TEST_F(ThreeParameterCostFunctorTest, vector residuals(7, -100000); vector jacobian; - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect_[1].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); EXPECT_TRUE(cost_function_->Evaluate( parameter_blocks_.data(), residuals.data(), jacobian.data())); @@ -491,7 +490,7 @@ TEST_F(ThreeParameterCostFunctorTest, vector jacobian; jacobian.push_back(jacobian_vect_[0].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect_[2].data()); EXPECT_TRUE(cost_function_->Evaluate( @@ -562,14 +561,14 @@ class SixParameterCostFunctorTest : public ::testing::Test { // Prepare the cost function. typedef DynamicAutoDiffCostFunction DynamicMySixParameterCostFunction; - DynamicMySixParameterCostFunction* cost_function = - new DynamicMySixParameterCostFunction(new MySixParameterCostFunctor()); + auto cost_function = std::make_unique( + new MySixParameterCostFunctor()); for (int i = 0; i < 6; ++i) { cost_function->AddParameterBlock(1); } cost_function->SetNumResiduals(7); - cost_function_.reset(cost_function); + cost_function_ = std::move(cost_function); // Setup jacobian data. jacobian_vect_.resize(6); @@ -669,7 +668,7 @@ class SixParameterCostFunctorTest : public ::testing::Test { TEST_F(SixParameterCostFunctorTest, TestSixParameterResiduals) { vector residuals(7, -100000); EXPECT_TRUE(cost_function_->Evaluate( - parameter_blocks_.data(), residuals.data(), NULL)); + parameter_blocks_.data(), residuals.data(), nullptr)); for (int i = 0; i < 7; ++i) { EXPECT_EQ(expected_residuals_[i], residuals[i]); } @@ -706,10 +705,10 @@ TEST_F(SixParameterCostFunctorTest, TestSixParameterJacobianVVCVVC) { vector jacobian; jacobian.push_back(jacobian_vect_[0].data()); jacobian.push_back(jacobian_vect_[1].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect_[3].data()); jacobian.push_back(jacobian_vect_[4].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); EXPECT_TRUE(cost_function_->Evaluate( parameter_blocks_.data(), residuals.data(), jacobian.data())); @@ -735,10 +734,10 @@ TEST_F(SixParameterCostFunctorTest, TestSixParameterJacobianVCCVCV) { vector jacobian; jacobian.push_back(jacobian_vect_[0].data()); - jacobian.push_back(NULL); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect_[3].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect_[5].data()); EXPECT_TRUE(cost_function_->Evaluate( diff --git a/internal/ceres/dynamic_compressed_row_jacobian_writer.cc b/internal/ceres/dynamic_compressed_row_jacobian_writer.cc index 9f709a3b6..f6bb3b384 100644 --- a/internal/ceres/dynamic_compressed_row_jacobian_writer.cc +++ b/internal/ceres/dynamic_compressed_row_jacobian_writer.cc @@ -48,12 +48,12 @@ DynamicCompressedRowJacobianWriter::CreateEvaluatePreparers(int num_threads) { return ScratchEvaluatePreparer::Create(*program_, num_threads); } -SparseMatrix* DynamicCompressedRowJacobianWriter::CreateJacobian() const { - DynamicCompressedRowSparseMatrix* jacobian = - new DynamicCompressedRowSparseMatrix(program_->NumResiduals(), - program_->NumEffectiveParameters(), - 0 /* max_num_nonzeros */); - return jacobian; +std::unique_ptr +DynamicCompressedRowJacobianWriter::CreateJacobian() const { + return std::make_unique( + program_->NumResiduals(), + program_->NumEffectiveParameters(), + 0 /* max_num_nonzeros */); } void DynamicCompressedRowJacobianWriter::Write(int residual_id, diff --git a/internal/ceres/dynamic_compressed_row_jacobian_writer.h b/internal/ceres/dynamic_compressed_row_jacobian_writer.h index ef8fa25d7..b9858ba47 100644 --- a/internal/ceres/dynamic_compressed_row_jacobian_writer.h +++ b/internal/ceres/dynamic_compressed_row_jacobian_writer.h @@ -60,7 +60,7 @@ class DynamicCompressedRowJacobianWriter { // Return a `DynamicCompressedRowSparseMatrix` which is filled by // `Write`. Note that `Finalize` must be called to make the // `CompressedRowSparseMatrix` interface valid. - SparseMatrix* CreateJacobian() const; + std::unique_ptr CreateJacobian() const; // Write only the non-zero jacobian entries for a residual block // (specified by `residual_id`) into `base_jacobian`, starting at the row diff --git a/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc b/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc index 95dc807b8..3b673895a 100644 --- a/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc +++ b/internal/ceres/dynamic_compressed_row_sparse_matrix_test.cc @@ -61,7 +61,8 @@ class DynamicCompressedRowSparseMatrixTest : public ::testing::Test { InitialiseDenseReference(); InitialiseSparseMatrixReferences(); - dcrsm.reset(new DynamicCompressedRowSparseMatrix(num_rows, num_cols, 0)); + dcrsm = std::make_unique( + num_rows, num_cols, 0); } void Finalize() { dcrsm->Finalize(num_additional_elements); } @@ -93,8 +94,8 @@ class DynamicCompressedRowSparseMatrixTest : public ::testing::Test { } ASSERT_EQ(values.size(), expected_num_nonzeros); - tsm.reset( - new TripletSparseMatrix(num_rows, num_cols, expected_num_nonzeros)); + tsm = std::make_unique( + num_rows, num_cols, expected_num_nonzeros); copy(rows.begin(), rows.end(), tsm->mutable_rows()); copy(cols.begin(), cols.end(), tsm->mutable_cols()); copy(values.begin(), values.end(), tsm->mutable_values()); @@ -104,7 +105,7 @@ class DynamicCompressedRowSparseMatrixTest : public ::testing::Test { tsm->ToDenseMatrix(&dense_from_tsm); ASSERT_TRUE((dense.array() == dense_from_tsm.array()).all()); - crsm.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm)); + crsm = CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm); Matrix dense_from_crsm; crsm->ToDenseMatrix(&dense_from_crsm); ASSERT_TRUE((dense.array() == dense_from_crsm.array()).all()); diff --git a/internal/ceres/dynamic_numeric_diff_cost_function_test.cc b/internal/ceres/dynamic_numeric_diff_cost_function_test.cc index 0150f5ea8..ad3f479b5 100644 --- a/internal/ceres/dynamic_numeric_diff_cost_function_test.cc +++ b/internal/ceres/dynamic_numeric_diff_cost_function_test.cc @@ -89,7 +89,7 @@ TEST(DynamicNumericdiffCostFunctionTest, TestResiduals) { parameter_blocks[0] = ¶m_block_0[0]; parameter_blocks[1] = ¶m_block_1[0]; EXPECT_TRUE( - cost_function.Evaluate(¶meter_blocks[0], residuals.data(), NULL)); + cost_function.Evaluate(¶meter_blocks[0], residuals.data(), nullptr)); for (int r = 0; r < 10; ++r) { EXPECT_EQ(1.0 * r, residuals.at(r * 2)); EXPECT_EQ(-1.0 * r, residuals.at(r * 2 + 1)); @@ -190,7 +190,7 @@ TEST(DynamicNumericdiffCostFunctionTest, jacobian_vect[0].resize(21 * 10, -100000); jacobian_vect[1].resize(21 * 5, -100000); vector jacobian; - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect[1].data()); // Test jacobian computation. @@ -241,7 +241,7 @@ TEST(DynamicNumericdiffCostFunctionTest, jacobian_vect[1].resize(21 * 5, -100000); vector jacobian; jacobian.push_back(jacobian_vect[0].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); // Test jacobian computation. EXPECT_TRUE(cost_function.Evaluate( @@ -330,15 +330,14 @@ class ThreeParameterCostFunctorTest : public ::testing::Test { // Prepare the cost function. typedef DynamicNumericDiffCostFunction DynamicMyThreeParameterCostFunction; - DynamicMyThreeParameterCostFunction* cost_function = - new DynamicMyThreeParameterCostFunction( - new MyThreeParameterCostFunctor()); + auto cost_function = std::make_unique( + new MyThreeParameterCostFunctor()); cost_function->AddParameterBlock(1); cost_function->AddParameterBlock(2); cost_function->AddParameterBlock(3); cost_function->SetNumResiduals(7); - cost_function_.reset(cost_function); + cost_function_ = std::move(cost_function); // Setup jacobian data. jacobian_vect_.resize(3); @@ -431,7 +430,7 @@ class ThreeParameterCostFunctorTest : public ::testing::Test { TEST_F(ThreeParameterCostFunctorTest, TestThreeParameterResiduals) { vector residuals(7, -100000); EXPECT_TRUE(cost_function_->Evaluate( - parameter_blocks_.data(), residuals.data(), NULL)); + parameter_blocks_.data(), residuals.data(), nullptr)); for (int i = 0; i < 7; ++i) { EXPECT_EQ(expected_residuals_[i], residuals[i]); } @@ -470,9 +469,9 @@ TEST_F(ThreeParameterCostFunctorTest, vector residuals(7, -100000); vector jacobian; - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect_[1].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); EXPECT_TRUE(cost_function_->Evaluate( parameter_blocks_.data(), residuals.data(), jacobian.data())); @@ -492,7 +491,7 @@ TEST_F(ThreeParameterCostFunctorTest, vector jacobian; jacobian.push_back(jacobian_vect_[0].data()); - jacobian.push_back(NULL); + jacobian.push_back(nullptr); jacobian.push_back(jacobian_vect_[2].data()); EXPECT_TRUE(cost_function_->Evaluate( diff --git a/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc b/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc index 87cc904ce..d101aef6d 100644 --- a/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc +++ b/internal/ceres/dynamic_sparse_normal_cholesky_solver.cc @@ -71,11 +71,11 @@ LinearSolver::Summary DynamicSparseNormalCholeskySolver::SolveImpl( // it before returning the matrix to the user. std::unique_ptr regularizer; if (!A->col_blocks().empty()) { - regularizer.reset(CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( - per_solve_options.D, A->col_blocks())); + regularizer = CompressedRowSparseMatrix::CreateBlockDiagonalMatrix( + per_solve_options.D, A->col_blocks()); } else { - regularizer.reset( - new CompressedRowSparseMatrix(per_solve_options.D, num_cols)); + regularizer = std::make_unique( + per_solve_options.D, num_cols); } A->AppendRows(*regularizer); } diff --git a/internal/ceres/dynamic_sparse_normal_cholesky_solver_test.cc b/internal/ceres/dynamic_sparse_normal_cholesky_solver_test.cc index 8bf609ec8..f915d3232 100644 --- a/internal/ceres/dynamic_sparse_normal_cholesky_solver_test.cc +++ b/internal/ceres/dynamic_sparse_normal_cholesky_solver_test.cc @@ -50,19 +50,19 @@ namespace internal { class DynamicSparseNormalCholeskySolverTest : public ::testing::Test { protected: void SetUp() final { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(1)); - A_.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix( - *down_cast(problem->A.get()))); - b_.reset(problem->b.release()); - D_.reset(problem->D.release()); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(1); + A_ = CompressedRowSparseMatrix::FromTripletSparseMatrix( + *down_cast(problem->A.get())); + b_ = std::move(problem->b); + D_ = std::move(problem->D); } void TestSolver(const LinearSolver::Options& options, double* D) { Matrix dense_A; A_->ToDenseMatrix(&dense_A); Matrix lhs = dense_A.transpose() * dense_A; - if (D != NULL) { + if (D != nullptr) { lhs += (ConstVectorRef(D, A_->num_cols()).array() * ConstVectorRef(D, A_->num_cols()).array()) .matrix() @@ -100,7 +100,7 @@ class DynamicSparseNormalCholeskySolverTest : public ::testing::Test { sparse_linear_algebra_library_type; ContextImpl context; options.context = &context; - TestSolver(options, NULL); + TestSolver(options, nullptr); TestSolver(options, D_.get()); } diff --git a/internal/ceres/dynamic_sparsity_test.cc b/internal/ceres/dynamic_sparsity_test.cc index 12e62ef21..6c7b24ccd 100644 --- a/internal/ceres/dynamic_sparsity_test.cc +++ b/internal/ceres/dynamic_sparsity_test.cc @@ -307,16 +307,16 @@ class PointToLineSegmentContourCostFunction : public CostFunction { residuals[0] = y_[0] - ((1.0 - u) * x[1 + i0][0] + u * x[1 + i1][0]); residuals[1] = y_[1] - ((1.0 - u) * x[1 + i0][1] + u * x[1 + i1][1]); - if (jacobians == NULL) { + if (jacobians == nullptr) { return true; } - if (jacobians[0] != NULL) { + if (jacobians[0] != nullptr) { jacobians[0][0] = x[1 + i0][0] - x[1 + i1][0]; jacobians[0][1] = x[1 + i0][1] - x[1 + i1][1]; } for (int i = 0; i < num_segments_; ++i) { - if (jacobians[i + 1] != NULL) { + if (jacobians[i + 1] != nullptr) { MatrixRef(jacobians[i + 1], 2, 2).setZero(); if (i == i0) { jacobians[i + 1][0] = -(1.0 - u); @@ -403,7 +403,7 @@ TEST(DynamicSparsity, StaticAndDynamicSparsityProduceSameSolution) { // For each data point add a residual which measures its distance to its // corresponding position on the line segment contour. std::vector parameter_blocks(1 + num_segments); - parameter_blocks[0] = NULL; + parameter_blocks[0] = nullptr; for (int i = 0; i < num_segments; ++i) { parameter_blocks[i + 1] = X.data() + 2 * i; } @@ -411,7 +411,7 @@ TEST(DynamicSparsity, StaticAndDynamicSparsityProduceSameSolution) { parameter_blocks[0] = &t[i]; problem.AddResidualBlock( PointToLineSegmentContourCostFunction::Create(num_segments, kY.row(i)), - NULL, + nullptr, parameter_blocks); } @@ -419,7 +419,7 @@ TEST(DynamicSparsity, StaticAndDynamicSparsityProduceSameSolution) { for (int i = 0; i < num_segments; ++i) { problem.AddResidualBlock( EuclideanDistanceFunctor::Create(sqrt(regularization_weight)), - NULL, + nullptr, X.data() + 2 * i, X.data() + 2 * ((i + 1) % num_segments)); } diff --git a/internal/ceres/eigensparse.cc b/internal/ceres/eigensparse.cc index 1c47a023e..ba8e05a6b 100644 --- a/internal/ceres/eigensparse.cc +++ b/internal/ceres/eigensparse.cc @@ -109,7 +109,7 @@ class EigenSparseCholeskyTemplate : public SparseCholesky { std::string* message) final { CHECK_EQ(lhs->storage_type(), StorageType()); - typename Solver::Scalar* values_ptr = NULL; + typename Solver::Scalar* values_ptr = nullptr; if (std::is_same::value) { values_ptr = reinterpret_cast(lhs->mutable_values()); @@ -141,8 +141,6 @@ class EigenSparseCholeskyTemplate : public SparseCholesky { std::unique_ptr EigenSparseCholesky::Create( const OrderingType ordering_type) { - std::unique_ptr sparse_cholesky; - typedef Eigen::SimplicialLDLT, Eigen::Upper, Eigen::AMDOrdering> @@ -151,20 +149,18 @@ std::unique_ptr EigenSparseCholesky::Create( Eigen::Upper, Eigen::NaturalOrdering> WithNaturalOrdering; + if (ordering_type == AMD) { - sparse_cholesky.reset(new EigenSparseCholeskyTemplate()); + return std::make_unique>(); } else { - sparse_cholesky.reset( - new EigenSparseCholeskyTemplate()); + return std::make_unique>(); } - return sparse_cholesky; } EigenSparseCholesky::~EigenSparseCholesky() = default; std::unique_ptr FloatEigenSparseCholesky::Create( const OrderingType ordering_type) { - std::unique_ptr sparse_cholesky; typedef Eigen::SimplicialLDLT, Eigen::Upper, Eigen::AMDOrdering> @@ -174,12 +170,10 @@ std::unique_ptr FloatEigenSparseCholesky::Create( Eigen::NaturalOrdering> WithNaturalOrdering; if (ordering_type == AMD) { - sparse_cholesky.reset(new EigenSparseCholeskyTemplate()); + return std::make_unique>(); } else { - sparse_cholesky.reset( - new EigenSparseCholeskyTemplate()); + return std::make_unique>(); } - return sparse_cholesky; } FloatEigenSparseCholesky::~FloatEigenSparseCholesky() = default; diff --git a/internal/ceres/evaluation_callback_test.cc b/internal/ceres/evaluation_callback_test.cc index f81f3bd48..84fff8aa5 100644 --- a/internal/ceres/evaluation_callback_test.cc +++ b/internal/ceres/evaluation_callback_test.cc @@ -132,7 +132,7 @@ struct WigglyBowlCostFunctionAndEvaluationCallback : SizedCostFunction<2, 2>, double y = (*parameters)[1]; residuals[0] = y - a * sin(x); residuals[1] = x; - if (jacobians != NULL) { + if (jacobians != nullptr) { (*jacobians)[2 * 0 + 0] = -a * cos(x); // df1/dx (*jacobians)[2 * 0 + 1] = 1.0; // df1/dy (*jacobians)[2 * 1 + 0] = 1.0; // df2/dx @@ -157,7 +157,7 @@ struct WigglyBowlCostFunctionAndEvaluationCallback : SizedCostFunction<2, 2>, EXPECT_EQ(prepare_parameter_hash, incoming_parameter_hash); // Check: jacobians are requested if they were in PrepareForEvaluation(). - EXPECT_EQ(prepare_requested_jacobians, jacobians != NULL); + EXPECT_EQ(prepare_requested_jacobians, jacobians != nullptr); evaluate_num_calls++; evaluate_last_parameter_hash = incoming_parameter_hash; @@ -196,7 +196,7 @@ TEST(EvaluationCallback, WithTrustRegionMinimizer) { problem_options.evaluation_callback = &cost_function; problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP; Problem problem(problem_options); - problem.AddResidualBlock(&cost_function, NULL, parameters); + problem.AddResidualBlock(&cost_function, nullptr, parameters); Solver::Options options; options.linear_solver_type = DENSE_QR; @@ -322,7 +322,7 @@ static void WithLineSearchMinimizerImpl( problem_options.evaluation_callback = &cost_function; problem_options.cost_function_ownership = DO_NOT_TAKE_OWNERSHIP; Problem problem(problem_options); - problem.AddResidualBlock(&cost_function, NULL, parameters); + problem.AddResidualBlock(&cost_function, nullptr, parameters); Solver::Options options; options.linear_solver_type = DENSE_QR; diff --git a/internal/ceres/evaluator.cc b/internal/ceres/evaluator.cc index 4d69f962c..f8d1121bc 100644 --- a/internal/ceres/evaluator.cc +++ b/internal/ceres/evaluator.cc @@ -50,36 +50,40 @@ namespace internal { Evaluator::~Evaluator() = default; -Evaluator* Evaluator::Create(const Evaluator::Options& options, - Program* program, - std::string* error) { - CHECK(options.context != NULL); +std::unique_ptr Evaluator::Create(const Evaluator::Options& options, + Program* program, + std::string* error) { + CHECK(options.context != nullptr); switch (options.linear_solver_type) { case DENSE_QR: case DENSE_NORMAL_CHOLESKY: - return new ProgramEvaluator( + return std::make_unique< + ProgramEvaluator>( options, program); case DENSE_SCHUR: case SPARSE_SCHUR: case ITERATIVE_SCHUR: case CGNR: - return new ProgramEvaluator( + return std::make_unique< + ProgramEvaluator>( options, program); case SPARSE_NORMAL_CHOLESKY: if (options.dynamic_sparsity) { - return new ProgramEvaluator( - options, program); + return std::make_unique< + ProgramEvaluator>(options, + program); } else { - return new ProgramEvaluator( + return std::make_unique< + ProgramEvaluator>( options, program); } default: *error = "Invalid Linear Solver Type. Unable to create evaluator."; - return NULL; + return nullptr; } } diff --git a/internal/ceres/evaluator.h b/internal/ceres/evaluator.h index 5c9cd56e6..e642551c3 100644 --- a/internal/ceres/evaluator.h +++ b/internal/ceres/evaluator.h @@ -68,9 +68,9 @@ class CERES_EXPORT_INTERNAL Evaluator { EvaluationCallback* evaluation_callback = nullptr; }; - static Evaluator* Create(const Options& options, - Program* program, - std::string* error); + static std::unique_ptr Create(const Options& options, + Program* program, + std::string* error); // Build and return a sparse matrix for storing and working with the Jacobian // of the objective function. The jacobian has dimensions @@ -88,7 +88,7 @@ class CERES_EXPORT_INTERNAL Evaluator { // the jacobian for use with CHOLMOD, where as BlockOptimizationProblem // creates a BlockSparseMatrix representation of the jacobian for use in the // Schur complement based methods. - virtual SparseMatrix* CreateJacobian() const = 0; + virtual std::unique_ptr CreateJacobian() const = 0; // Options struct to control Evaluator::Evaluate; struct EvaluateOptions { @@ -102,10 +102,10 @@ class CERES_EXPORT_INTERNAL Evaluator { // Evaluate the cost function for the given state. Returns the cost, // residuals, and jacobian in the corresponding arguments. Both residuals and - // jacobian are optional; to avoid computing them, pass NULL. + // jacobian are optional; to avoid computing them, pass nullptr. // - // If non-NULL, the Jacobian must have a suitable sparsity pattern; only the - // values array of the jacobian is modified. + // If non-nullptr, the Jacobian must have a suitable sparsity pattern; only + // the values array of the jacobian is modified. // // state is an array of size NumParameters(), cost is a pointer to a single // double, and residuals is an array of doubles of size NumResiduals(). diff --git a/internal/ceres/evaluator_test.cc b/internal/ceres/evaluator_test.cc index 7aaf1b0ff..f5703f702 100644 --- a/internal/ceres/evaluator_test.cc +++ b/internal/ceres/evaluator_test.cc @@ -117,7 +117,7 @@ struct EvaluatorTestOptions { }; struct EvaluatorTest : public ::testing::TestWithParam { - Evaluator* CreateEvaluator(Program* program) { + std::unique_ptr CreateEvaluator(Program* program) { // This program is straight from the ProblemImpl, and so has no index/offset // yet; compute it here as required by the evaluator implementations. program->SetParameterOffsetsAndIndex(); @@ -152,8 +152,8 @@ struct EvaluatorTest : public ::testing::TestWithParam { const double* expected_residuals, const double* expected_gradient, const double* expected_jacobian) { - std::unique_ptr evaluator( - CreateEvaluator(problem->mutable_program())); + std::unique_ptr evaluator = + CreateEvaluator(problem->mutable_program()); int num_residuals = expected_num_rows; int num_parameters = expected_num_cols; @@ -608,8 +608,8 @@ TEST_P(EvaluatorTest, EvaluatorAbortsForResidualsThatFailToEvaluate) { // The values are ignored. double state[9]; - std::unique_ptr evaluator( - CreateEvaluator(problem.mutable_program())); + std::unique_ptr evaluator = + CreateEvaluator(problem.mutable_program()); std::unique_ptr jacobian(evaluator->CreateJacobian()); double cost; EXPECT_FALSE(evaluator->Evaluate(state, &cost, nullptr, nullptr, nullptr)); diff --git a/internal/ceres/evaluator_test_utils.cc b/internal/ceres/evaluator_test_utils.cc index 25801db9f..f3384bc8d 100644 --- a/internal/ceres/evaluator_test_utils.cc +++ b/internal/ceres/evaluator_test_utils.cc @@ -49,7 +49,7 @@ void CompareEvaluations(int expected_num_rows, const double* actual_jacobian) { EXPECT_EQ(expected_cost, actual_cost); - if (expected_residuals != NULL) { + if (expected_residuals != nullptr) { ConstVectorRef expected_residuals_vector(expected_residuals, expected_num_rows); ConstVectorRef actual_residuals_vector(actual_residuals, expected_num_rows); @@ -61,7 +61,7 @@ void CompareEvaluations(int expected_num_rows, << expected_residuals_vector; } - if (expected_gradient != NULL) { + if (expected_gradient != nullptr) { ConstVectorRef expected_gradient_vector(expected_gradient, expected_num_cols); ConstVectorRef actual_gradient_vector(actual_gradient, expected_num_cols); @@ -74,7 +74,7 @@ void CompareEvaluations(int expected_num_rows, << expected_gradient_vector.transpose(); } - if (expected_jacobian != NULL) { + if (expected_jacobian != nullptr) { ConstMatrixRef expected_jacobian_matrix( expected_jacobian, expected_num_rows, expected_num_cols); ConstMatrixRef actual_jacobian_matrix( diff --git a/internal/ceres/generate_bundle_adjustment_tests.py b/internal/ceres/generate_bundle_adjustment_tests.py index 7b0caa307..86b4fcb32 100644 --- a/internal/ceres/generate_bundle_adjustment_tests.py +++ b/internal/ceres/generate_bundle_adjustment_tests.py @@ -137,7 +137,7 @@ TEST_F(BundleAdjustmentTest, options->sparse_linear_algebra_library_type = %(sparse_backend)s; options->preconditioner_type = %(preconditioner)s; if (%(ordering)s) { - options->linear_solver_ordering.reset(); + options->linear_solver_ordering = nullptr; } Problem* problem = bundle_adjustment_problem.mutable_problem(); RunSolverForConfigAndExpectResidualsMatch(*options, problem); diff --git a/internal/ceres/gradient_checker.cc b/internal/ceres/gradient_checker.cc index c0dcd471f..fd302f099 100644 --- a/internal/ceres/gradient_checker.cc +++ b/internal/ceres/gradient_checker.cc @@ -134,18 +134,17 @@ GradientChecker::GradientChecker( manifolds_[i] = new internal::ManifoldAdapter(local_param); } - DynamicNumericDiffCostFunction* - finite_diff_cost_function = - new DynamicNumericDiffCostFunction( - function, DO_NOT_TAKE_OWNERSHIP, options); - finite_diff_cost_function_.reset(finite_diff_cost_function); - + auto finite_diff_cost_function = + std::make_unique>( + function, DO_NOT_TAKE_OWNERSHIP, options); const vector& parameter_block_sizes = function->parameter_block_sizes(); for (int32_t parameter_block_size : parameter_block_sizes) { finite_diff_cost_function->AddParameterBlock(parameter_block_size); } finite_diff_cost_function->SetNumResiduals(function->num_residuals()); + + finite_diff_cost_function_ = std::move(finite_diff_cost_function); } GradientChecker::GradientChecker(const CostFunction* function, @@ -158,12 +157,10 @@ GradientChecker::GradientChecker(const CostFunction* function, } else { manifolds_.resize(function->parameter_block_sizes().size(), nullptr); } - DynamicNumericDiffCostFunction* - finite_diff_cost_function = - new DynamicNumericDiffCostFunction( - function, DO_NOT_TAKE_OWNERSHIP, options); - finite_diff_cost_function_.reset(finite_diff_cost_function); + auto finite_diff_cost_function = + std::make_unique>( + function, DO_NOT_TAKE_OWNERSHIP, options); const vector& parameter_block_sizes = function->parameter_block_sizes(); const int num_parameter_blocks = parameter_block_sizes.size(); @@ -171,6 +168,8 @@ GradientChecker::GradientChecker(const CostFunction* function, finite_diff_cost_function->AddParameterBlock(parameter_block_sizes[i]); } finite_diff_cost_function->SetNumResiduals(function->num_residuals()); + + finite_diff_cost_function_ = std::move(finite_diff_cost_function); } GradientChecker::~GradientChecker() { diff --git a/internal/ceres/gradient_checking_cost_function.cc b/internal/ceres/gradient_checking_cost_function.cc index f786b83af..bbe68b834 100644 --- a/internal/ceres/gradient_checking_cost_function.cc +++ b/internal/ceres/gradient_checking_cost_function.cc @@ -87,7 +87,7 @@ class GradientCheckingCostFunction : public CostFunction { double** jacobians) const final { if (!jacobians) { // Nothing to check in this case; just forward. - return function_->Evaluate(parameters, residuals, NULL); + return function_->Evaluate(parameters, residuals, nullptr); } GradientChecker::ProbeResults results; @@ -107,7 +107,7 @@ class GradientCheckingCostFunction : public CostFunction { // Copy the original jacobian blocks into the jacobians array. const vector& block_sizes = function_->parameter_block_sizes(); for (int k = 0; k < block_sizes.size(); k++) { - if (jacobians[k] != NULL) { + if (jacobians[k] != nullptr) { MatrixRef(jacobians[k], results.jacobians[k].rows(), results.jacobians[k].cols()) = results.jacobians[k]; @@ -169,7 +169,7 @@ CostFunction* CreateGradientCheckingCostFunction( callback); } -ProblemImpl* CreateGradientCheckingProblemImpl( +std::unique_ptr CreateGradientCheckingProblemImpl( ProblemImpl* problem_impl, double relative_step_size, double relative_precision, @@ -190,8 +190,8 @@ ProblemImpl* CreateGradientCheckingProblemImpl( NumericDiffOptions numeric_diff_options; numeric_diff_options.relative_step_size = relative_step_size; - ProblemImpl* gradient_checking_problem_impl = - new ProblemImpl(gradient_checking_problem_options); + auto gradient_checking_problem_impl = + std::make_unique(gradient_checking_problem_options); Program* program = problem_impl->mutable_program(); diff --git a/internal/ceres/gradient_checking_cost_function.h b/internal/ceres/gradient_checking_cost_function.h index 46cf4dde9..b21d113cd 100644 --- a/internal/ceres/gradient_checking_cost_function.h +++ b/internal/ceres/gradient_checking_cost_function.h @@ -92,8 +92,6 @@ CERES_EXPORT_INTERNAL CostFunction* CreateGradientCheckingCostFunction( // iteration, the respective cost function will notify the // GradientCheckingIterationCallback. // -// The caller owns the returned ProblemImpl object. -// // Note: This is quite inefficient and is intended only for debugging. // // relative_step_size and relative_precision are parameters to control @@ -102,11 +100,11 @@ CERES_EXPORT_INTERNAL CostFunction* CreateGradientCheckingCostFunction( // jacobians obtained by numerically differentiating them. See the // documentation of 'numeric_derivative_relative_step_size' in solver.h for a // better explanation. -CERES_EXPORT_INTERNAL ProblemImpl* CreateGradientCheckingProblemImpl( - ProblemImpl* problem_impl, - double relative_step_size, - double relative_precision, - GradientCheckingIterationCallback* callback); +CERES_EXPORT_INTERNAL std::unique_ptr +CreateGradientCheckingProblemImpl(ProblemImpl* problem_impl, + double relative_step_size, + double relative_precision, + GradientCheckingIterationCallback* callback); } // namespace internal } // namespace ceres diff --git a/internal/ceres/gradient_checking_cost_function_test.cc b/internal/ceres/gradient_checking_cost_function_test.cc index 264a85856..5630c328a 100644 --- a/internal/ceres/gradient_checking_cost_function_test.cc +++ b/internal/ceres/gradient_checking_cost_function_test.cc @@ -366,8 +366,8 @@ TEST(GradientCheckingProblemImpl, // clang-format on GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_problem_impl( - CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback)); + std::unique_ptr gradient_checking_problem_impl = + CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback); // The dimensions of the two problems match. EXPECT_EQ(problem_impl.NumParameterBlocks(), @@ -444,8 +444,8 @@ TEST(GradientCheckingProblemImpl, ProblemDimensionsMatch) { // clang-format on GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_problem_impl( - CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback)); + std::unique_ptr gradient_checking_problem_impl = + CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback); // The dimensions of the two problems match. EXPECT_EQ(problem_impl.NumParameterBlocks(), @@ -505,8 +505,8 @@ TEST(GradientCheckingProblemImpl, ConstrainedProblemBoundsArePropagated) { problem_impl.SetParameterUpperBound(x, 1, 2.5); GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_problem_impl( - CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback)); + std::unique_ptr gradient_checking_problem_impl = + CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback); // The dimensions of the two problems match. EXPECT_EQ(problem_impl.NumParameterBlocks(), diff --git a/internal/ceres/gradient_problem.cc b/internal/ceres/gradient_problem.cc index 3ef8e4f6e..93b18f567 100644 --- a/internal/ceres/gradient_problem.cc +++ b/internal/ceres/gradient_problem.cc @@ -53,9 +53,10 @@ GradientProblem::GradientProblem(FirstOrderFunction* function, scratch_(new double[function_->NumParameters()]) { CHECK(function != nullptr); if (parameterization != nullptr) { - manifold_.reset(new internal::ManifoldAdapter(parameterization_.get())); + manifold_ = + std::make_unique(parameterization_.get()); } else { - manifold_.reset(new EuclideanManifold(function_->NumParameters())); + manifold_ = std::make_unique(function_->NumParameters()); } CHECK_EQ(function_->NumParameters(), manifold_->AmbientSize()); } diff --git a/internal/ceres/gradient_problem_evaluator.h b/internal/ceres/gradient_problem_evaluator.h index a510b25e8..f0aba948f 100644 --- a/internal/ceres/gradient_problem_evaluator.h +++ b/internal/ceres/gradient_problem_evaluator.h @@ -32,12 +32,14 @@ #define CERES_INTERNAL_GRADIENT_PROBLEM_EVALUATOR_H_ #include +#include #include #include "ceres/evaluator.h" #include "ceres/execution_summary.h" #include "ceres/gradient_problem.h" #include "ceres/internal/port.h" +#include "ceres/sparse_matrix.h" #include "ceres/wall_time.h" namespace ceres { @@ -47,15 +49,18 @@ class GradientProblemEvaluator : public Evaluator { public: explicit GradientProblemEvaluator(const GradientProblem& problem) : problem_(problem) {} + ~GradientProblemEvaluator() override = default; - SparseMatrix* CreateJacobian() const final { return nullptr; } + + std::unique_ptr CreateJacobian() const final { return nullptr; } + bool Evaluate(const EvaluateOptions& evaluate_options, const double* state, double* cost, double* residuals, double* gradient, SparseMatrix* jacobian) final { - CHECK(jacobian == NULL); + CHECK(jacobian == nullptr); ScopedExecutionTimer total_timer("Evaluator::Total", &execution_summary_); // The reason we use Residual and Jacobian here even when we are // only computing the cost and gradient has to do with the fact @@ -65,7 +70,7 @@ class GradientProblemEvaluator : public Evaluator { // to be consistent across the code base for the time accounting // to work. ScopedExecutionTimer call_type_timer( - gradient == NULL ? "Evaluator::Residual" : "Evaluator::Jacobian", + gradient == nullptr ? "Evaluator::Residual" : "Evaluator::Jacobian", &execution_summary_); return problem_.Evaluate(state, cost, gradient); } diff --git a/internal/ceres/gradient_problem_solver.cc b/internal/ceres/gradient_problem_solver.cc index f75427460..582895da9 100644 --- a/internal/ceres/gradient_problem_solver.cc +++ b/internal/ceres/gradient_problem_solver.cc @@ -135,21 +135,22 @@ void GradientProblemSolver::Solve(const GradientProblemSolver::Options& options, // now. Minimizer::Options minimizer_options = Minimizer::Options(GradientProblemSolverOptionsToSolverOptions(options)); - minimizer_options.evaluator.reset(new GradientProblemEvaluator(problem)); + minimizer_options.evaluator = + std::make_unique(problem); std::unique_ptr logging_callback; if (options.logging_type != SILENT) { - logging_callback.reset( - new LoggingCallback(LINE_SEARCH, options.minimizer_progress_to_stdout)); + logging_callback = std::make_unique( + LINE_SEARCH, options.minimizer_progress_to_stdout); minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(), logging_callback.get()); } std::unique_ptr state_updating_callback; if (options.update_state_every_iteration) { - state_updating_callback.reset( - new GradientProblemSolverStateUpdatingCallback( - problem.NumParameters(), solution.data(), parameters_ptr)); + state_updating_callback = + std::make_unique( + problem.NumParameters(), solution.data(), parameters_ptr); minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(), state_updating_callback.get()); } diff --git a/internal/ceres/gradient_problem_solver_test.cc b/internal/ceres/gradient_problem_solver_test.cc index e3b408551..5e33b3163 100644 --- a/internal/ceres/gradient_problem_solver_test.cc +++ b/internal/ceres/gradient_problem_solver_test.cc @@ -48,7 +48,7 @@ class Rosenbrock : public ceres::FirstOrderFunction { const double y = parameters[1]; cost[0] = (1.0 - x) * (1.0 - x) + 100.0 * (y - x * x) * (y - x * x); - if (gradient != NULL) { + if (gradient != nullptr) { gradient[0] = -2.0 * (1.0 - x) - 200.0 * (y - x * x) * 2.0 * x; gradient[1] = 200.0 * (y - x * x); } @@ -79,7 +79,7 @@ class QuadraticFunction : public ceres::FirstOrderFunction { double* gradient) const final { const double x = parameters[0]; *cost = 0.5 * (5.0 - x) * (5.0 - x); - if (gradient != NULL) { + if (gradient != nullptr) { gradient[0] = x - 5.0; } diff --git a/internal/ceres/implicit_schur_complement.cc b/internal/ceres/implicit_schur_complement.cc index bb2da1742..4e712b95b 100644 --- a/internal/ceres/implicit_schur_complement.cc +++ b/internal/ceres/implicit_schur_complement.cc @@ -43,7 +43,7 @@ namespace internal { ImplicitSchurComplement::ImplicitSchurComplement( const LinearSolver::Options& options) - : options_(options), D_(NULL), b_(NULL) {} + : options_(options), D_(nullptr), b_(nullptr) {} ImplicitSchurComplement::~ImplicitSchurComplement() = default; @@ -52,8 +52,8 @@ void ImplicitSchurComplement::Init(const BlockSparseMatrix& A, const double* b) { // Since initialization is reasonably heavy, perhaps we can save on // constructing a new object everytime. - if (A_ == NULL) { - A_.reset(PartitionedMatrixViewBase::Create(options_, A)); + if (A_ == nullptr) { + A_ = PartitionedMatrixViewBase::Create(options_, A); } D_ = D; @@ -61,10 +61,10 @@ void ImplicitSchurComplement::Init(const BlockSparseMatrix& A, // Initialize temporary storage and compute the block diagonals of // E'E and F'E. - if (block_diagonal_EtE_inverse_ == NULL) { - block_diagonal_EtE_inverse_.reset(A_->CreateBlockDiagonalEtE()); + if (block_diagonal_EtE_inverse_ == nullptr) { + block_diagonal_EtE_inverse_ = A_->CreateBlockDiagonalEtE(); if (options_.preconditioner_type == JACOBI) { - block_diagonal_FtF_inverse_.reset(A_->CreateBlockDiagonalFtF()); + block_diagonal_FtF_inverse_ = A_->CreateBlockDiagonalFtF(); } rhs_.resize(A_->num_cols_f()); rhs_.setZero(); @@ -84,7 +84,7 @@ void ImplicitSchurComplement::Init(const BlockSparseMatrix& A, // the block diagonals and invert them. AddDiagonalAndInvert(D_, block_diagonal_EtE_inverse_.get()); if (options_.preconditioner_type == JACOBI) { - AddDiagonalAndInvert((D_ == NULL) ? NULL : D_ + A_->num_cols_e(), + AddDiagonalAndInvert((D_ == nullptr) ? nullptr : D_ + A_->num_cols_e(), block_diagonal_FtF_inverse_.get()); } @@ -118,7 +118,7 @@ void ImplicitSchurComplement::RightMultiply(const double* x, double* y) const { A_->RightMultiplyE(tmp_e_cols_2_.data(), tmp_rows_.data()); // y5 = D * x - if (D_ != NULL) { + if (D_ != nullptr) { ConstVectorRef Dref(D_ + A_->num_cols_e(), num_cols()); VectorRef(y, num_cols()) = (Dref.array().square() * ConstVectorRef(x, num_cols()).array()) @@ -146,7 +146,7 @@ void ImplicitSchurComplement::AddDiagonalAndInvert( row_block_size, row_block_size); - if (D != NULL) { + if (D != nullptr) { ConstVectorRef d(D + row_block_pos, row_block_size); m += d.array().square().matrix().asDiagonal(); } diff --git a/internal/ceres/implicit_schur_complement_test.cc b/internal/ceres/implicit_schur_complement_test.cc index b6d886fb6..b4f618cda 100644 --- a/internal/ceres/implicit_schur_complement_test.cc +++ b/internal/ceres/implicit_schur_complement_test.cc @@ -57,13 +57,13 @@ const double kEpsilon = 1e-14; class ImplicitSchurComplementTest : public ::testing::Test { protected: void SetUp() final { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(2)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(2); CHECK(problem != nullptr); A_.reset(down_cast(problem->A.release())); - b_.reset(problem->b.release()); - D_.reset(problem->D.release()); + b_ = std::move(problem->b); + D_ = std::move(problem->D); num_cols_ = A_->num_cols(); num_rows_ = A_->num_rows(); @@ -90,8 +90,8 @@ class ImplicitSchurComplementTest : public ::testing::Test { ContextImpl context; options.context = &context; - std::unique_ptr eliminator( - SchurEliminatorBase::Create(options)); + std::unique_ptr eliminator = + SchurEliminatorBase::Create(options); CHECK(eliminator != nullptr); const bool kFullRankETE = true; eliminator->Init(num_eliminate_blocks_, kFullRankETE, bs); @@ -202,7 +202,7 @@ class ImplicitSchurComplementTest : public ::testing::Test { // We do this with and without regularization to check that the // support for the LM diagonal is correct. TEST_F(ImplicitSchurComplementTest, SchurMatrixValuesTest) { - EXPECT_TRUE(TestImplicitSchurComplement(NULL)); + EXPECT_TRUE(TestImplicitSchurComplement(nullptr)); EXPECT_TRUE(TestImplicitSchurComplement(D_.get())); } diff --git a/internal/ceres/inner_product_computer.cc b/internal/ceres/inner_product_computer.cc index ef38b7b3a..1a3a1a18f 100644 --- a/internal/ceres/inner_product_computer.cc +++ b/internal/ceres/inner_product_computer.cc @@ -44,11 +44,12 @@ namespace internal { // or the lower triangular part of the product. // // num_nonzeros is the number of non-zeros in the result matrix. -CompressedRowSparseMatrix* InnerProductComputer::CreateResultMatrix( +std::unique_ptr +InnerProductComputer::CreateResultMatrix( const CompressedRowSparseMatrix::StorageType storage_type, const int num_nonzeros) { - CompressedRowSparseMatrix* matrix = - new CompressedRowSparseMatrix(m_.num_cols(), m_.num_cols(), num_nonzeros); + auto matrix = std::make_unique( + m_.num_cols(), m_.num_cols(), num_nonzeros); matrix->set_storage_type(storage_type); const CompressedRowBlockStructure* bs = m_.block_structure(); @@ -116,14 +117,14 @@ InnerProductComputer::InnerProductComputer(const BlockSparseMatrix& m, // // product_storage_type controls the form of the output matrix. It // can be LOWER_TRIANGULAR or UPPER_TRIANGULAR. -InnerProductComputer* InnerProductComputer::Create( +std::unique_ptr InnerProductComputer::Create( const BlockSparseMatrix& m, CompressedRowSparseMatrix::StorageType product_storage_type) { return InnerProductComputer::Create( m, 0, m.block_structure()->rows.size(), product_storage_type); } -InnerProductComputer* InnerProductComputer::Create( +std::unique_ptr InnerProductComputer::Create( const BlockSparseMatrix& m, const int start_row_block, const int end_row_block, @@ -132,8 +133,8 @@ InnerProductComputer* InnerProductComputer::Create( product_storage_type == CompressedRowSparseMatrix::UPPER_TRIANGULAR); CHECK_GT(m.num_nonzeros(), 0) << "Congratulations, you found a bug in Ceres. Please report it."; - InnerProductComputer* inner_product_computer = - new InnerProductComputer(m, start_row_block, end_row_block); + std::unique_ptr inner_product_computer( + new InnerProductComputer(m, start_row_block, end_row_block)); inner_product_computer->Init(product_storage_type); return inner_product_computer; } @@ -183,7 +184,7 @@ void InnerProductComputer::ComputeOffsetsAndCreateResultMatrix( std::vector row_block_nnz; const int num_nonzeros = ComputeNonzeros(product_terms, &row_block_nnz); - result_.reset(CreateResultMatrix(product_storage_type, num_nonzeros)); + result_ = CreateResultMatrix(product_storage_type, num_nonzeros); // Populate the row non-zero counts in the result matrix. int* crsm_rows = result_->mutable_rows(); diff --git a/internal/ceres/inner_product_computer.h b/internal/ceres/inner_product_computer.h index 04ec1d183..abf81be35 100644 --- a/internal/ceres/inner_product_computer.h +++ b/internal/ceres/inner_product_computer.h @@ -74,7 +74,7 @@ class CERES_EXPORT_INTERNAL InnerProductComputer { // // The user must ensure that the matrix m is valid for the life time // of this object. - static InnerProductComputer* Create( + static std::unique_ptr Create( const BlockSparseMatrix& m, CompressedRowSparseMatrix::StorageType storage_type); @@ -83,7 +83,7 @@ class CERES_EXPORT_INTERNAL InnerProductComputer { // // a = m(start_row_block : end_row_block, :); // result = a' * a; - static InnerProductComputer* Create( + static std::unique_ptr Create( const BlockSparseMatrix& m, int start_row_block, int end_row_block, @@ -127,7 +127,7 @@ class CERES_EXPORT_INTERNAL InnerProductComputer { void Init(CompressedRowSparseMatrix::StorageType storage_type); - CompressedRowSparseMatrix* CreateResultMatrix( + std::unique_ptr CreateResultMatrix( const CompressedRowSparseMatrix::StorageType storage_type, int num_nonzeros); diff --git a/internal/ceres/inner_product_computer_test.cc b/internal/ceres/inner_product_computer_test.cc index f65c8a3f9..b672f8caa 100644 --- a/internal/ceres/inner_product_computer_test.cc +++ b/internal/ceres/inner_product_computer_test.cc @@ -128,11 +128,11 @@ TEST(InnerProductComputer, NormalOperation) { std::unique_ptr inner_product_computer; - inner_product_computer.reset(InnerProductComputer::Create( - *random_matrix, CompressedRowSparseMatrix::LOWER_TRIANGULAR)); + inner_product_computer = InnerProductComputer::Create( + *random_matrix, CompressedRowSparseMatrix::LOWER_TRIANGULAR); COMPUTE_AND_COMPARE; - inner_product_computer.reset(InnerProductComputer::Create( - *random_matrix, CompressedRowSparseMatrix::UPPER_TRIANGULAR)); + inner_product_computer = InnerProductComputer::Create( + *random_matrix, CompressedRowSparseMatrix::UPPER_TRIANGULAR); COMPUTE_AND_COMPARE; } } @@ -202,17 +202,17 @@ TEST(InnerProductComputer, SubMatrix) { eigen_random_matrix.transpose() * eigen_random_matrix; std::unique_ptr inner_product_computer; - inner_product_computer.reset(InnerProductComputer::Create( + inner_product_computer = InnerProductComputer::Create( *random_matrix, start_row_block, end_row_block, - CompressedRowSparseMatrix::LOWER_TRIANGULAR)); + CompressedRowSparseMatrix::LOWER_TRIANGULAR); COMPUTE_AND_COMPARE; - inner_product_computer.reset(InnerProductComputer::Create( + inner_product_computer = InnerProductComputer::Create( *random_matrix, start_row_block, end_row_block, - CompressedRowSparseMatrix::UPPER_TRIANGULAR)); + CompressedRowSparseMatrix::UPPER_TRIANGULAR); COMPUTE_AND_COMPARE; } } diff --git a/internal/ceres/is_close.h b/internal/ceres/is_close.h index b781a4493..82dc7e9d2 100644 --- a/internal/ceres/is_close.h +++ b/internal/ceres/is_close.h @@ -39,7 +39,7 @@ namespace ceres { namespace internal { // Returns true if x and y have a relative (unsigned) difference less than // relative_precision and false otherwise. Stores the relative and absolute -// difference in relative/absolute_error if non-NULL. If one of the two values +// difference in relative/absolute_error if non-nullptr. If one of the two values // is exactly zero, the absolute difference will be compared, and relative_error // will be set to the absolute difference. CERES_EXPORT_INTERNAL bool IsClose(double x, diff --git a/internal/ceres/iterative_schur_complement_solver.cc b/internal/ceres/iterative_schur_complement_solver.cc index e6340df34..6ff84b5fc 100644 --- a/internal/ceres/iterative_schur_complement_solver.cc +++ b/internal/ceres/iterative_schur_complement_solver.cc @@ -69,13 +69,13 @@ LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( CHECK(A->block_structure() != nullptr); const int num_eliminate_blocks = options_.elimination_groups[0]; // Initialize a ImplicitSchurComplement object. - if (schur_complement_ == NULL) { + if (schur_complement_ == nullptr) { DetectStructure(*(A->block_structure()), num_eliminate_blocks, &options_.row_block_size, &options_.e_block_size, &options_.f_block_size); - schur_complement_.reset(new ImplicitSchurComplement(options_)); + schur_complement_ = std::make_unique(options_); } schur_complement_->Init(*A, per_solve_options.D, b); @@ -86,7 +86,7 @@ LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( LinearSolver::Summary summary; summary.num_iterations = 0; summary.termination_type = LINEAR_SOLVER_SUCCESS; - schur_complement_->BackSubstitute(NULL, x); + schur_complement_->BackSubstitute(nullptr, x); return summary; } @@ -104,7 +104,7 @@ LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( cg_per_solve_options.q_tolerance = per_solve_options.q_tolerance; CreatePreconditioner(A); - if (preconditioner_.get() != NULL) { + if (preconditioner_.get() != nullptr) { if (!preconditioner_->Update(*A, per_solve_options.D)) { LinearSolver::Summary summary; summary.num_iterations = 0; @@ -134,7 +134,7 @@ LinearSolver::Summary IterativeSchurComplementSolver::SolveImpl( void IterativeSchurComplementSolver::CreatePreconditioner( BlockSparseMatrix* A) { if (options_.preconditioner_type == IDENTITY || - preconditioner_.get() != NULL) { + preconditioner_.get() != nullptr) { return; } @@ -149,22 +149,22 @@ void IterativeSchurComplementSolver::CreatePreconditioner( preconditioner_options.e_block_size = options_.e_block_size; preconditioner_options.f_block_size = options_.f_block_size; preconditioner_options.elimination_groups = options_.elimination_groups; - CHECK(options_.context != NULL); + CHECK(options_.context != nullptr); preconditioner_options.context = options_.context; switch (options_.preconditioner_type) { case JACOBI: - preconditioner_.reset(new SparseMatrixPreconditionerWrapper( - schur_complement_->block_diagonal_FtF_inverse())); + preconditioner_ = std::make_unique( + schur_complement_->block_diagonal_FtF_inverse()); break; case SCHUR_JACOBI: - preconditioner_.reset(new SchurJacobiPreconditioner( - *A->block_structure(), preconditioner_options)); + preconditioner_ = std::make_unique( + *A->block_structure(), preconditioner_options); break; case CLUSTER_JACOBI: case CLUSTER_TRIDIAGONAL: - preconditioner_.reset(new VisibilityBasedPreconditioner( - *A->block_structure(), preconditioner_options)); + preconditioner_ = std::make_unique( + *A->block_structure(), preconditioner_options); break; default: LOG(FATAL) << "Unknown Preconditioner Type"; diff --git a/internal/ceres/iterative_schur_complement_solver_test.cc b/internal/ceres/iterative_schur_complement_solver_test.cc index fdd65c7f6..80e388a66 100644 --- a/internal/ceres/iterative_schur_complement_solver_test.cc +++ b/internal/ceres/iterative_schur_complement_solver_test.cc @@ -61,13 +61,13 @@ const double kEpsilon = 1e-14; class IterativeSchurComplementSolverTest : public ::testing::Test { protected: void SetUpProblem(int problem_id) { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(problem_id)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(problem_id); CHECK(problem != nullptr); A_.reset(down_cast(problem->A.release())); - b_.reset(problem->b.release()); - D_.reset(problem->D.release()); + b_ = std::move(problem->b); + D_ = std::move(problem->D); num_cols_ = A_->num_cols(); num_rows_ = A_->num_rows(); @@ -121,13 +121,13 @@ class IterativeSchurComplementSolverTest : public ::testing::Test { TEST_F(IterativeSchurComplementSolverTest, NormalProblem) { SetUpProblem(2); - EXPECT_TRUE(TestSolver(NULL)); + EXPECT_TRUE(TestSolver(nullptr)); EXPECT_TRUE(TestSolver(D_.get())); } TEST_F(IterativeSchurComplementSolverTest, ProblemWithNoFBlocks) { SetUpProblem(3); - EXPECT_TRUE(TestSolver(NULL)); + EXPECT_TRUE(TestSolver(nullptr)); EXPECT_TRUE(TestSolver(D_.get())); } diff --git a/internal/ceres/levenberg_marquardt_strategy_test.cc b/internal/ceres/levenberg_marquardt_strategy_test.cc index c05dad163..dfb730260 100644 --- a/internal/ceres/levenberg_marquardt_strategy_test.cc +++ b/internal/ceres/levenberg_marquardt_strategy_test.cc @@ -87,7 +87,7 @@ TEST(LevenbergMarquardtStrategy, AcceptRejectStepRadiusScaling) { // We need a non-null pointer here, so anything should do. std::unique_ptr linear_solver( - new RegularizationCheckingLinearSolver(0, NULL)); + new RegularizationCheckingLinearSolver(0, nullptr)); options.linear_solver = linear_solver.get(); LevenbergMarquardtStrategy lms(options); diff --git a/internal/ceres/line_search.cc b/internal/ceres/line_search.cc index 7e871a20a..0141015a5 100644 --- a/internal/ceres/line_search.cc +++ b/internal/ceres/line_search.cc @@ -68,24 +68,21 @@ ostream& operator<<(ostream& os, const FunctionSample& sample) { LineSearch::LineSearch(const LineSearch::Options& options) : options_(options) {} -LineSearch* LineSearch::Create(const LineSearchType line_search_type, - const LineSearch::Options& options, - string* error) { - LineSearch* line_search = NULL; +std::unique_ptr LineSearch::Create( + const LineSearchType line_search_type, + const LineSearch::Options& options, + string* error) { switch (line_search_type) { case ceres::ARMIJO: - line_search = new ArmijoLineSearch(options); - break; + return std::make_unique(options); case ceres::WOLFE: - line_search = new WolfeLineSearch(options); - break; + return std::make_unique(options); default: *error = string("Invalid line search algorithm type: ") + LineSearchTypeToString(line_search_type) + string(", unable to create line search."); - return NULL; } - return line_search; + return nullptr; } LineSearchFunction::LineSearchFunction(Evaluator* evaluator) @@ -119,13 +116,13 @@ void LineSearchFunction::Evaluate(const double x, } output->vector_x_is_valid = true; - double* gradient = NULL; + double* gradient = nullptr; if (evaluate_gradient) { output->vector_gradient.resize(direction_.rows(), 1); gradient = output->vector_gradient.data(); } const bool eval_status = evaluator_->Evaluate( - output->vector_x.data(), &(output->value), NULL, gradient, NULL); + output->vector_x.data(), &(output->value), nullptr, gradient, nullptr); if (!eval_status || !std::isfinite(output->value)) { return; diff --git a/internal/ceres/line_search.h b/internal/ceres/line_search.h index 0af488438..7d1a85269 100644 --- a/internal/ceres/line_search.h +++ b/internal/ceres/line_search.h @@ -163,9 +163,10 @@ class LineSearch { explicit LineSearch(const LineSearch::Options& options); virtual ~LineSearch() = default; - static LineSearch* Create(const LineSearchType line_search_type, - const LineSearch::Options& options, - std::string* error); + static std::unique_ptr Create( + const LineSearchType line_search_type, + const LineSearch::Options& options, + std::string* error); // Perform the line search. // diff --git a/internal/ceres/line_search_direction.cc b/internal/ceres/line_search_direction.cc index 2b707fb44..4d6e641f5 100644 --- a/internal/ceres/line_search_direction.cc +++ b/internal/ceres/line_search_direction.cc @@ -338,32 +338,32 @@ class BFGS : public LineSearchDirection { bool is_positive_definite_; }; -LineSearchDirection* LineSearchDirection::Create( +std::unique_ptr LineSearchDirection::Create( const LineSearchDirection::Options& options) { if (options.type == STEEPEST_DESCENT) { - return new SteepestDescent; + return std::make_unique(); } if (options.type == NONLINEAR_CONJUGATE_GRADIENT) { - return new NonlinearConjugateGradient( + return std::make_unique( options.nonlinear_conjugate_gradient_type, options.function_tolerance); } if (options.type == ceres::LBFGS) { - return new ceres::internal::LBFGS( + return std::make_unique( options.num_parameters, options.max_lbfgs_rank, options.use_approximate_eigenvalue_bfgs_scaling); } if (options.type == ceres::BFGS) { - return new ceres::internal::BFGS( + return std::make_unique( options.num_parameters, options.use_approximate_eigenvalue_bfgs_scaling); } LOG(ERROR) << "Unknown line search direction type: " << options.type; - return NULL; + return nullptr; } } // namespace internal diff --git a/internal/ceres/line_search_direction.h b/internal/ceres/line_search_direction.h index e8b11393b..29127c17b 100644 --- a/internal/ceres/line_search_direction.h +++ b/internal/ceres/line_search_direction.h @@ -57,7 +57,7 @@ class LineSearchDirection { bool use_approximate_eigenvalue_bfgs_scaling; }; - static LineSearchDirection* Create(const Options& options); + static std::unique_ptr Create(const Options& options); virtual ~LineSearchDirection() = default; virtual bool NextDirection(const LineSearchMinimizer::State& previous, diff --git a/internal/ceres/line_search_minimizer.cc b/internal/ceres/line_search_minimizer.cc index ea1c5072a..6768d4b19 100644 --- a/internal/ceres/line_search_minimizer.cc +++ b/internal/ceres/line_search_minimizer.cc @@ -171,8 +171,8 @@ void LineSearchMinimizer::Minimize(const Minimizer::Options& options, line_search_direction_options.max_lbfgs_rank = options.max_lbfgs_rank; line_search_direction_options.use_approximate_eigenvalue_bfgs_scaling = options.use_approximate_eigenvalue_bfgs_scaling; - std::unique_ptr line_search_direction( - LineSearchDirection::Create(line_search_direction_options)); + std::unique_ptr line_search_direction = + LineSearchDirection::Create(line_search_direction_options); LineSearchFunction line_search_function(evaluator); @@ -280,8 +280,8 @@ void LineSearchMinimizer::Minimize(const Minimizer::Options& options, << options.max_num_line_search_direction_restarts << " [max]."; } - line_search_direction.reset( - LineSearchDirection::Create(line_search_direction_options)); + line_search_direction = + LineSearchDirection::Create(line_search_direction_options); current_state.search_direction = -current_state.gradient; } diff --git a/internal/ceres/line_search_minimizer_test.cc b/internal/ceres/line_search_minimizer_test.cc index 2ef27b941..76a4a4799 100644 --- a/internal/ceres/line_search_minimizer_test.cc +++ b/internal/ceres/line_search_minimizer_test.cc @@ -44,7 +44,7 @@ class QuadraticFirstOrderFunction : public ceres::FirstOrderFunction { double* cost, double* gradient) const final { cost[0] = parameters[0] * parameters[0]; - if (gradient != NULL) { + if (gradient != nullptr) { gradient[0] = 2.0 * parameters[0]; } return true; diff --git a/internal/ceres/line_search_preprocessor.cc b/internal/ceres/line_search_preprocessor.cc index 465d9a41a..02a1a8c1c 100644 --- a/internal/ceres/line_search_preprocessor.cc +++ b/internal/ceres/line_search_preprocessor.cc @@ -63,9 +63,9 @@ bool SetupEvaluator(PreprocessedProblem* pp) { pp->evaluator_options.context = pp->problem->context(); pp->evaluator_options.evaluation_callback = pp->reduced_program->mutable_evaluation_callback(); - pp->evaluator.reset(Evaluator::Create( - pp->evaluator_options, pp->reduced_program.get(), &pp->error)); - return (pp->evaluator.get() != NULL); + pp->evaluator = Evaluator::Create( + pp->evaluator_options, pp->reduced_program.get(), &pp->error); + return (pp->evaluator.get() != nullptr); } } // namespace @@ -85,10 +85,10 @@ bool LineSearchPreprocessor::Preprocess(const Solver::Options& options, return false; } - pp->reduced_program.reset(program->CreateReducedProgram( - &pp->removed_parameter_blocks, &pp->fixed_cost, &pp->error)); + pp->reduced_program = program->CreateReducedProgram( + &pp->removed_parameter_blocks, &pp->fixed_cost, &pp->error); - if (pp->reduced_program.get() == NULL) { + if (pp->reduced_program.get() == nullptr) { return false; } diff --git a/internal/ceres/line_search_preprocessor_test.cc b/internal/ceres/line_search_preprocessor_test.cc index b64946c9d..90cca10d2 100644 --- a/internal/ceres/line_search_preprocessor_test.cc +++ b/internal/ceres/line_search_preprocessor_test.cc @@ -85,7 +85,7 @@ class FailingCostFunction : public SizedCostFunction<1, 1> { TEST(LineSearchPreprocessor, RemoveParameterBlocksFailed) { ProblemImpl problem; double x = 3.0; - problem.AddResidualBlock(new FailingCostFunction, NULL, &x); + problem.AddResidualBlock(new FailingCostFunction, nullptr, &x); problem.SetParameterBlockConstant(&x); Solver::Options options; options.minimizer_type = LINE_SEARCH; @@ -121,8 +121,8 @@ TEST(LineSearchPreprocessor, NormalOperation) { double x = 1.0; double y = 1.0; double z = 1.0; - problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, NULL, &x, &y); - problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, NULL, &y, &z); + problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, nullptr, &x, &y); + problem.AddResidualBlock(new DummyCostFunction<1, 1, 1>, nullptr, &y, &z); Solver::Options options; options.minimizer_type = LINE_SEARCH; @@ -131,7 +131,7 @@ TEST(LineSearchPreprocessor, NormalOperation) { PreprocessedProblem pp; EXPECT_TRUE(preprocessor.Preprocess(options, &problem, &pp)); EXPECT_EQ(pp.evaluator_options.linear_solver_type, CGNR); - EXPECT_TRUE(pp.evaluator.get() != NULL); + EXPECT_TRUE(pp.evaluator.get() != nullptr); } } // namespace internal diff --git a/internal/ceres/linear_least_squares_problems.cc b/internal/ceres/linear_least_squares_problems.cc index 73787d692..4b37e00ef 100644 --- a/internal/ceres/linear_least_squares_problems.cc +++ b/internal/ceres/linear_least_squares_problems.cc @@ -49,7 +49,8 @@ namespace internal { using std::string; -LinearLeastSquaresProblem* CreateLinearLeastSquaresProblemFromId(int id) { +std::unique_ptr +CreateLinearLeastSquaresProblemFromId(int id) { switch (id) { case 0: return LinearLeastSquaresProblem0(); @@ -64,7 +65,7 @@ LinearLeastSquaresProblem* CreateLinearLeastSquaresProblemFromId(int id) { default: LOG(FATAL) << "Unknown problem id requested " << id; } - return NULL; + return nullptr; } /* @@ -85,15 +86,16 @@ D = [1 x_D = [1.78448275; 2.82327586;] */ -LinearLeastSquaresProblem* LinearLeastSquaresProblem0() { - LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem; +std::unique_ptr LinearLeastSquaresProblem0() { + std::unique_ptr problem = + std::make_unique(); - TripletSparseMatrix* A = new TripletSparseMatrix(3, 2, 6); - problem->b.reset(new double[3]); - problem->D.reset(new double[2]); + auto A = std::make_unique(3, 2, 6); + problem->b = std::make_unique(3); + problem->D = std::make_unique(2); - problem->x.reset(new double[2]); - problem->x_D.reset(new double[2]); + problem->x = std::make_unique(2); + problem->x_D = std::make_unique(2); int* Ai = A->mutable_rows(); int* Aj = A->mutable_cols(); @@ -115,7 +117,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem0() { Ax[4] = 6; Ax[5] = -10; A->set_num_nonzeros(6); - problem->A.reset(A); + problem->A = std::move(A); problem->b[0] = 8; problem->b[1] = 18; @@ -181,15 +183,16 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem0() { // BlockSparseMatrix version of this problem. // TripletSparseMatrix version. -LinearLeastSquaresProblem* LinearLeastSquaresProblem1() { +std::unique_ptr LinearLeastSquaresProblem1() { int num_rows = 6; int num_cols = 5; - LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem; - TripletSparseMatrix* A = - new TripletSparseMatrix(num_rows, num_cols, num_rows * num_cols); - problem->b.reset(new double[num_rows]); - problem->D.reset(new double[num_cols]); + std::unique_ptr problem = + std::make_unique(); + auto A = std::make_unique( + num_rows, num_cols, num_rows * num_cols); + problem->b = std::make_unique(num_rows); + problem->D = std::make_unique(num_cols); problem->num_eliminate_blocks = 2; int* rows = A->mutable_rows(); @@ -271,7 +274,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem1() { A->set_num_nonzeros(nnz); CHECK(A->IsValid()); - problem->A.reset(A); + problem->A = std::move(A); for (int i = 0; i < num_cols; ++i) { problem->D.get()[i] = 1; @@ -285,18 +288,20 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem1() { } // BlockSparseMatrix version -LinearLeastSquaresProblem* LinearLeastSquaresProblem2() { +std::unique_ptr LinearLeastSquaresProblem2() { int num_rows = 6; int num_cols = 5; - LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem; + std::unique_ptr problem = + std::make_unique(); - problem->b.reset(new double[num_rows]); - problem->D.reset(new double[num_cols]); + problem->b = std::make_unique(num_rows); + problem->D = std::make_unique(num_cols); problem->num_eliminate_blocks = 2; CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; - std::unique_ptr values(new double[num_rows * num_cols]); + std::unique_ptr values = + std::make_unique(num_rows * num_cols); for (int c = 0; c < num_cols; ++c) { bs->cols.push_back(Block()); @@ -386,7 +391,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem2() { row.cells.push_back(Cell(4, 12)); } - BlockSparseMatrix* A = new BlockSparseMatrix(bs); + auto A = std::make_unique(bs); memcpy(A->mutable_values(), values.get(), nnz * sizeof(*A->values())); for (int i = 0; i < num_cols; ++i) { @@ -397,7 +402,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem2() { problem->b.get()[i] = i; } - problem->A.reset(A); + problem->A = std::move(A); return problem; } @@ -418,18 +423,20 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem2() { 5] */ // BlockSparseMatrix version -LinearLeastSquaresProblem* LinearLeastSquaresProblem3() { +std::unique_ptr LinearLeastSquaresProblem3() { int num_rows = 5; int num_cols = 2; - LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem; + std::unique_ptr problem = + std::make_unique(); - problem->b.reset(new double[num_rows]); - problem->D.reset(new double[num_cols]); + problem->b = std::make_unique(num_rows); + problem->D = std::make_unique(num_cols); problem->num_eliminate_blocks = 2; CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; - std::unique_ptr values(new double[num_rows * num_cols]); + std::unique_ptr values = + std::make_unique(num_rows * num_cols); for (int c = 0; c < num_cols; ++c) { bs->cols.push_back(Block()); @@ -489,7 +496,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem3() { row.cells.push_back(Cell(1, 4)); } - BlockSparseMatrix* A = new BlockSparseMatrix(bs); + auto A = std::make_unique(bs); memcpy(A->mutable_values(), values.get(), nnz * sizeof(*A->values())); for (int i = 0; i < num_cols; ++i) { @@ -500,7 +507,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem3() { problem->b.get()[i] = i; } - problem->A.reset(A); + problem->A = std::move(A); return problem; } @@ -525,18 +532,20 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem3() { // // NOTE: This problem is too small and rank deficient to be solved without // the diagonal regularization. -LinearLeastSquaresProblem* LinearLeastSquaresProblem4() { +std::unique_ptr LinearLeastSquaresProblem4() { int num_rows = 3; int num_cols = 7; - LinearLeastSquaresProblem* problem = new LinearLeastSquaresProblem; + std::unique_ptr problem = + std::make_unique(); - problem->b.reset(new double[num_rows]); - problem->D.reset(new double[num_cols]); + problem->b = std::make_unique(num_rows); + problem->D = std::make_unique(num_cols); problem->num_eliminate_blocks = 1; CompressedRowBlockStructure* bs = new CompressedRowBlockStructure; - std::unique_ptr values(new double[num_rows * num_cols]); + std::unique_ptr values = + std::make_unique(num_rows * num_cols); // Column block structure bs->cols.push_back(Block()); @@ -590,7 +599,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem4() { values[nnz++] = 1; } - BlockSparseMatrix* A = new BlockSparseMatrix(bs); + auto A = std::make_unique(bs); memcpy(A->mutable_values(), values.get(), nnz * sizeof(*A->values())); for (int i = 0; i < num_cols; ++i) { @@ -601,7 +610,7 @@ LinearLeastSquaresProblem* LinearLeastSquaresProblem4() { problem->b.get()[i] = i; } - problem->A.reset(A); + problem->A = std::move(A); return problem; } @@ -616,15 +625,15 @@ bool DumpLinearLeastSquaresProblemToConsole(const SparseMatrix* A, A->ToDenseMatrix(&AA); LOG(INFO) << "A^T: \n" << AA.transpose(); - if (D != NULL) { + if (D != nullptr) { LOG(INFO) << "A's appended diagonal:\n" << ConstVectorRef(D, A->num_cols()); } - if (b != NULL) { + if (b != nullptr) { LOG(INFO) << "b: \n" << ConstVectorRef(b, A->num_rows()); } - if (x != NULL) { + if (x != nullptr) { LOG(INFO) << "x: \n" << ConstVectorRef(x, A->num_cols()); } return true; @@ -673,21 +682,21 @@ bool DumpLinearLeastSquaresProblemToTextFile(const string& filename_base, A->num_cols()); } - if (D != NULL) { + if (D != nullptr) { string filename = filename_base + "_D.txt"; WriteArrayToFileOrDie(filename, D, A->num_cols()); StringAppendF( &matlab_script, "lsqp.D = load('%s', '-ascii');\n", filename.c_str()); } - if (b != NULL) { + if (b != nullptr) { string filename = filename_base + "_b.txt"; WriteArrayToFileOrDie(filename, b, A->num_rows()); StringAppendF( &matlab_script, "lsqp.b = load('%s', '-ascii');\n", filename.c_str()); } - if (x != NULL) { + if (x != nullptr) { string filename = filename_base + "_x.txt"; WriteArrayToFileOrDie(filename, x, A->num_cols()); StringAppendF( diff --git a/internal/ceres/linear_least_squares_problems.h b/internal/ceres/linear_least_squares_problems.h index cddaa9fd4..2120cc290 100644 --- a/internal/ceres/linear_least_squares_problems.h +++ b/internal/ceres/linear_least_squares_problems.h @@ -60,14 +60,14 @@ struct CERES_EXPORT_INTERNAL LinearLeastSquaresProblem { }; // Factories for linear least squares problem. -CERES_EXPORT_INTERNAL LinearLeastSquaresProblem* +CERES_EXPORT_INTERNAL std::unique_ptr CreateLinearLeastSquaresProblemFromId(int id); -LinearLeastSquaresProblem* LinearLeastSquaresProblem0(); -LinearLeastSquaresProblem* LinearLeastSquaresProblem1(); -LinearLeastSquaresProblem* LinearLeastSquaresProblem2(); -LinearLeastSquaresProblem* LinearLeastSquaresProblem3(); -LinearLeastSquaresProblem* LinearLeastSquaresProblem4(); +std::unique_ptr LinearLeastSquaresProblem0(); +std::unique_ptr LinearLeastSquaresProblem1(); +std::unique_ptr LinearLeastSquaresProblem2(); +std::unique_ptr LinearLeastSquaresProblem3(); +std::unique_ptr LinearLeastSquaresProblem4(); // Write the linear least squares problem to disk. The exact format // depends on dump_format_type. diff --git a/internal/ceres/linear_solver.cc b/internal/ceres/linear_solver.cc index befaa1442..cf358bdb0 100644 --- a/internal/ceres/linear_solver.cc +++ b/internal/ceres/linear_solver.cc @@ -69,50 +69,51 @@ LinearSolverType LinearSolver::LinearSolverForZeroEBlocks( return linear_solver_type; } -LinearSolver* LinearSolver::Create(const LinearSolver::Options& options) { - CHECK(options.context != NULL); +std::unique_ptr LinearSolver::Create( + const LinearSolver::Options& options) { + CHECK(options.context != nullptr); switch (options.type) { case CGNR: - return new CgnrSolver(options); + return std::make_unique(options); case SPARSE_NORMAL_CHOLESKY: #if defined(CERES_NO_SPARSE) - return NULL; + return nullptr; #else if (options.dynamic_sparsity) { - return new DynamicSparseNormalCholeskySolver(options); + return std::make_unique(options); } - return new SparseNormalCholeskySolver(options); + return std::make_unique(options); #endif case SPARSE_SCHUR: #if defined(CERES_NO_SPARSE) - return NULL; + return nullptr; #else - return new SparseSchurComplementSolver(options); + return std::make_unique(options); #endif case DENSE_SCHUR: - return new DenseSchurComplementSolver(options); + return std::make_unique(options); case ITERATIVE_SCHUR: if (options.use_explicit_schur_complement) { - return new SparseSchurComplementSolver(options); + return std::make_unique(options); } else { - return new IterativeSchurComplementSolver(options); + return std::make_unique(options); } case DENSE_QR: - return new DenseQRSolver(options); + return std::make_unique(options); case DENSE_NORMAL_CHOLESKY: - return new DenseNormalCholeskySolver(options); + return std::make_unique(options); default: LOG(FATAL) << "Unknown linear solver type :" << options.type; - return NULL; // MSVC doesn't understand that LOG(FATAL) never returns. + return nullptr; // MSVC doesn't understand that LOG(FATAL) never returns. } } diff --git a/internal/ceres/linear_solver.h b/internal/ceres/linear_solver.h index f4616cdce..ea44f3b9b 100644 --- a/internal/ceres/linear_solver.h +++ b/internal/ceres/linear_solver.h @@ -288,7 +288,7 @@ class CERES_EXPORT_INTERNAL LinearSolver { } // Factory - static LinearSolver* Create(const Options& options); + static std::unique_ptr Create(const Options& options); }; // This templated subclass of LinearSolver serves as a base class for diff --git a/internal/ceres/local_parameterization_test.cc b/internal/ceres/local_parameterization_test.cc index 2321ce7bd..cb2ad7462 100644 --- a/internal/ceres/local_parameterization_test.cc +++ b/internal/ceres/local_parameterization_test.cc @@ -283,7 +283,7 @@ void QuaternionParameterizationTestHelper(const double* x, double jacobian_ref[12]; double zero_delta[kLocalSize] = {0.0, 0.0, 0.0}; const double* parameters[2] = {x, zero_delta}; - double* jacobian_array[2] = {NULL, jacobian_ref}; + double* jacobian_array[2] = {nullptr, jacobian_ref}; // Autodiff jacobian at delta_x = 0. internal::AutoDifferentiate constant_parameters1; constant_parameters1.push_back(2); - param1_.reset( - new SubsetParameterization(global_size1, constant_parameters1)); + param1_ = std::make_unique(global_size1, + constant_parameters1); const int global_size2 = 3; std::vector constant_parameters2; constant_parameters2.push_back(0); constant_parameters2.push_back(1); - param2_.reset( - new SubsetParameterization(global_size2, constant_parameters2)); + param2_ = std::make_unique(global_size2, + constant_parameters2); const int global_size3 = 4; std::vector constant_parameters3; constant_parameters3.push_back(1); - param3_.reset( - new SubsetParameterization(global_size3, constant_parameters3)); + param3_ = std::make_unique(global_size3, + constant_parameters3); const int global_size4 = 2; std::vector constant_parameters4; constant_parameters4.push_back(1); - param4_.reset( - new SubsetParameterization(global_size4, constant_parameters4)); + param4_ = std::make_unique(global_size4, + constant_parameters4); } std::unique_ptr param1_; diff --git a/internal/ceres/loss_function.cc b/internal/ceres/loss_function.cc index 353f29a5b..f4ea0f7d1 100644 --- a/internal/ceres/loss_function.cc +++ b/internal/ceres/loss_function.cc @@ -161,7 +161,7 @@ void ComposedLoss::Evaluate(double s, double rho[3]) const { } void ScaledLoss::Evaluate(double s, double rho[3]) const { - if (rho_.get() == NULL) { + if (rho_.get() == nullptr) { rho[0] = a_ * s; rho[1] = a_; rho[2] = 0.0; diff --git a/internal/ceres/loss_function_test.cc b/internal/ceres/loss_function_test.cc index 638c0c9e8..e5de95f0b 100644 --- a/internal/ceres/loss_function_test.cc +++ b/internal/ceres/loss_function_test.cc @@ -195,7 +195,7 @@ TEST(LossFunction, ScaledLoss) { // construction with the call to AssertLossFunctionIsValid() because Apple's // GCC is unable to eliminate the copy of ScaledLoss, which is not copyable. { - ScaledLoss scaled_loss(NULL, 6, TAKE_OWNERSHIP); + ScaledLoss scaled_loss(nullptr, 6, TAKE_OWNERSHIP); AssertLossFunctionIsValid(scaled_loss, 0.323); } { @@ -265,17 +265,17 @@ TEST(LossFunction, LossFunctionWrapper) { EXPECT_NEAR(rho[i], rho_gold[i], 1e-12); } - // Set to NULL + // Set to nullptr TrivialLoss loss_function4; - loss_function_wrapper.Reset(NULL, TAKE_OWNERSHIP); + loss_function_wrapper.Reset(nullptr, TAKE_OWNERSHIP); loss_function_wrapper.Evaluate(s, rho); loss_function4.Evaluate(s, rho_gold); for (int i = 0; i < 3; ++i) { EXPECT_NEAR(rho[i], rho_gold[i], 1e-12); } - // Set to NULL, not taking ownership - loss_function_wrapper.Reset(NULL, DO_NOT_TAKE_OWNERSHIP); + // Set to nullptr, not taking ownership + loss_function_wrapper.Reset(nullptr, DO_NOT_TAKE_OWNERSHIP); loss_function_wrapper.Evaluate(s, rho); loss_function4.Evaluate(s, rho_gold); for (int i = 0; i < 3; ++i) { diff --git a/internal/ceres/minimizer.cc b/internal/ceres/minimizer.cc index 8e27c7bda..85629682e 100644 --- a/internal/ceres/minimizer.cc +++ b/internal/ceres/minimizer.cc @@ -48,7 +48,7 @@ Minimizer* Minimizer::Create(MinimizerType minimizer_type) { } LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type; - return NULL; + return nullptr; } Minimizer::~Minimizer() = default; diff --git a/internal/ceres/normal_prior.cc b/internal/ceres/normal_prior.cc index 4a62132db..473d05ceb 100644 --- a/internal/ceres/normal_prior.cc +++ b/internal/ceres/normal_prior.cc @@ -56,7 +56,7 @@ bool NormalPrior::Evaluate(double const* const* parameters, // r = A_ * (p - b_); // The extra eval is to get around a bug in the eigen library. r = A_ * (p - b_).eval(); - if ((jacobians != NULL) && (jacobians[0] != NULL)) { + if ((jacobians != nullptr) && (jacobians[0] != nullptr)) { MatrixRef(jacobians[0], num_residuals(), parameter_block_sizes()[0]) = A_; } return true; diff --git a/internal/ceres/normal_prior_test.cc b/internal/ceres/normal_prior_test.cc index 518c18e4d..39a7eb6b1 100644 --- a/internal/ceres/normal_prior_test.cc +++ b/internal/ceres/normal_prior_test.cc @@ -106,7 +106,7 @@ TEST(NormalPriorTest, ResidualAtRandomPositionNullJacobians) { for (int i = 0; i < num_cols; ++i) x[i] = 2 * RandDouble() - 1; double* jacobians[1]; - jacobians[0] = NULL; + jacobians[0] = nullptr; Vector residuals(num_rows); @@ -118,7 +118,7 @@ TEST(NormalPriorTest, ResidualAtRandomPositionNullJacobians) { (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm(); EXPECT_NEAR(residual_diff_norm, 0, 1e-10); - prior.Evaluate(&x, residuals.data(), NULL); + prior.Evaluate(&x, residuals.data(), nullptr); // Compare the norm of the residual residual_diff_norm = (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm(); diff --git a/internal/ceres/numeric_diff_cost_function_test.cc b/internal/ceres/numeric_diff_cost_function_test.cc index a5f7a1553..b9a0beb04 100644 --- a/internal/ceres/numeric_diff_cost_function_test.cc +++ b/internal/ceres/numeric_diff_cost_function_test.cc @@ -49,100 +49,102 @@ namespace ceres { namespace internal { TEST(NumericDiffCostFunction, EasyCaseFunctorCentralDifferences) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction(new EasyFunctor)); + auto cost_function = + std::make_unique>(new EasyFunctor); EasyFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); } TEST(NumericDiffCostFunction, EasyCaseFunctorForwardDifferences) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction(new EasyFunctor)); + auto cost_function = + std::make_unique>(new EasyFunctor); EasyFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); } TEST(NumericDiffCostFunction, EasyCaseFunctorRidders) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction(new EasyFunctor)); + auto cost_function = + std::make_unique>(new EasyFunctor); EasyFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, RIDDERS); } TEST(NumericDiffCostFunction, EasyCaseCostFunctionCentralDifferences) { - std::unique_ptr cost_function; - cost_function.reset( - new NumericDiffCostFunction(new EasyCostFunction, TAKE_OWNERSHIP)); + auto cost_function = + std::make_unique>(new EasyCostFunction, + TAKE_OWNERSHIP); EasyFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); } TEST(NumericDiffCostFunction, EasyCaseCostFunctionForwardDifferences) { - std::unique_ptr cost_function; - cost_function.reset( - new NumericDiffCostFunction(new EasyCostFunction, TAKE_OWNERSHIP)); + auto cost_function = + std::make_unique>(new EasyCostFunction, + TAKE_OWNERSHIP); EasyFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); } TEST(NumericDiffCostFunction, EasyCaseCostFunctionRidders) { - std::unique_ptr cost_function; - cost_function.reset( - new NumericDiffCostFunction(new EasyCostFunction, TAKE_OWNERSHIP)); + auto cost_function = + std::make_unique>(new EasyCostFunction, + TAKE_OWNERSHIP); + EasyFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, RIDDERS); } TEST(NumericDiffCostFunction, TranscendentalCaseFunctorCentralDifferences) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction(new TranscendentalFunctor)); + auto cost_function = + std::make_unique>(new TranscendentalFunctor); TranscendentalFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); } TEST(NumericDiffCostFunction, TranscendentalCaseFunctorForwardDifferences) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction(new TranscendentalFunctor)); + auto cost_function = + std::make_unique>(new TranscendentalFunctor); + TranscendentalFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); } @@ -153,43 +155,43 @@ TEST(NumericDiffCostFunction, TranscendentalCaseFunctorRidders) { // Using a smaller initial step size to overcome oscillatory function // behavior. options.ridders_relative_initial_step_size = 1e-3; + auto cost_function = + std::make_unique>( + new TranscendentalFunctor, TAKE_OWNERSHIP, 2, options); - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction( - new TranscendentalFunctor, TAKE_OWNERSHIP, 2, options)); TranscendentalFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, RIDDERS); } TEST(NumericDiffCostFunction, TranscendentalCaseCostFunctionCentralDifferences) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction( - new TranscendentalCostFunction, TAKE_OWNERSHIP)); + auto cost_function = + std::make_unique>( + new TranscendentalCostFunction, TAKE_OWNERSHIP); TranscendentalFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); } TEST(NumericDiffCostFunction, TranscendentalCaseCostFunctionForwardDifferences) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction( - new TranscendentalCostFunction, TAKE_OWNERSHIP)); + auto cost_function = + std::make_unique>( + new TranscendentalCostFunction, TAKE_OWNERSHIP); TranscendentalFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, FORWARD); } @@ -201,14 +203,14 @@ TEST(NumericDiffCostFunction, TranscendentalCaseCostFunctionRidders) { // behavior. options.ridders_relative_initial_step_size = 1e-3; - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction( - new TranscendentalCostFunction, TAKE_OWNERSHIP, 2, options)); + auto cost_function = + std::make_unique>( + new TranscendentalCostFunction, TAKE_OWNERSHIP, 2, options); TranscendentalFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, RIDDERS); } @@ -230,121 +232,120 @@ class SizeTestingCostFunction : public SizedCostFunction { // templates are instantiated for various shapes of the Jacobian // matrix. TEST(NumericDiffCostFunction, EigenRowMajorColMajorTest) { - std::unique_ptr cost_function; - cost_function.reset( - new NumericDiffCostFunction, CENTRAL, 1, 1>( - new SizeTestingCostFunction<1, 1>, ceres::TAKE_OWNERSHIP)); + std::unique_ptr cost_function = std::make_unique< + NumericDiffCostFunction, CENTRAL, 1, 1>>( + new SizeTestingCostFunction<1, 1>, ceres::TAKE_OWNERSHIP); - cost_function.reset( - new NumericDiffCostFunction, CENTRAL, 2, 1>( - new SizeTestingCostFunction<2, 1>, ceres::TAKE_OWNERSHIP)); + cost_function = std::make_unique< + NumericDiffCostFunction, CENTRAL, 2, 1>>( + new SizeTestingCostFunction<2, 1>, ceres::TAKE_OWNERSHIP); - cost_function.reset( - new NumericDiffCostFunction, CENTRAL, 1, 2>( - new SizeTestingCostFunction<1, 2>, ceres::TAKE_OWNERSHIP)); + cost_function = std::make_unique< + NumericDiffCostFunction, CENTRAL, 1, 2>>( + new SizeTestingCostFunction<1, 2>, ceres::TAKE_OWNERSHIP); - cost_function.reset( - new NumericDiffCostFunction, CENTRAL, 2, 2>( - new SizeTestingCostFunction<2, 2>, ceres::TAKE_OWNERSHIP)); + cost_function = std::make_unique< + NumericDiffCostFunction, CENTRAL, 2, 2>>( + new SizeTestingCostFunction<2, 2>, ceres::TAKE_OWNERSHIP); - cost_function.reset( - new NumericDiffCostFunction( - new EasyFunctor, TAKE_OWNERSHIP, 1)); + cost_function = std::make_unique< + NumericDiffCostFunction>( + new EasyFunctor, TAKE_OWNERSHIP, 1); - cost_function.reset( - new NumericDiffCostFunction( - new EasyFunctor, TAKE_OWNERSHIP, 2)); + cost_function = std::make_unique< + NumericDiffCostFunction>( + new EasyFunctor, TAKE_OWNERSHIP, 2); - cost_function.reset( - new NumericDiffCostFunction( - new EasyFunctor, TAKE_OWNERSHIP, 1)); + cost_function = std::make_unique< + NumericDiffCostFunction>( + new EasyFunctor, TAKE_OWNERSHIP, 1); - cost_function.reset( - new NumericDiffCostFunction( - new EasyFunctor, TAKE_OWNERSHIP, 2)); + cost_function = std::make_unique< + NumericDiffCostFunction>( + new EasyFunctor, TAKE_OWNERSHIP, 2); - cost_function.reset( - new NumericDiffCostFunction( - new EasyFunctor, TAKE_OWNERSHIP, 1)); + cost_function = std::make_unique< + NumericDiffCostFunction>( + new EasyFunctor, TAKE_OWNERSHIP, 1); - cost_function.reset( - new NumericDiffCostFunction( - new EasyFunctor, TAKE_OWNERSHIP, 2)); + cost_function = std::make_unique< + NumericDiffCostFunction>( + new EasyFunctor, TAKE_OWNERSHIP, 2); } TEST(NumericDiffCostFunction, EasyCaseFunctorCentralDifferencesAndDynamicNumResiduals) { - std::unique_ptr cost_function; - cost_function.reset( - new NumericDiffCostFunction(new EasyFunctor, TAKE_OWNERSHIP, 3)); + auto cost_function = + std::make_unique>( + new EasyFunctor, TAKE_OWNERSHIP, 3); EasyFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function, CENTRAL); } TEST(NumericDiffCostFunction, ExponentialFunctorRidders) { - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction(new ExponentialFunctor)); + auto cost_function = + std::make_unique>(new ExponentialFunctor); ExponentialFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function); } TEST(NumericDiffCostFunction, ExponentialCostFunctionRidders) { - std::unique_ptr cost_function; - cost_function.reset( - new NumericDiffCostFunction(new ExponentialCostFunction)); + auto cost_function = + std::make_unique>(new ExponentialCostFunction); ExponentialFunctor functor; functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function); } TEST(NumericDiffCostFunction, RandomizedFunctorRidders) { - std::unique_ptr cost_function; NumericDiffOptions options; // Larger initial step size is chosen to produce robust results in the // presence of random noise. options.ridders_relative_initial_step_size = 10.0; - cost_function.reset(new NumericDiffCostFunction( - new RandomizedFunctor(kNoiseFactor, kRandomSeed), - TAKE_OWNERSHIP, - 1, - options)); + auto cost_function = + std::make_unique>( + new RandomizedFunctor(kNoiseFactor, kRandomSeed), + TAKE_OWNERSHIP, + 1, + options); RandomizedFunctor functor(kNoiseFactor, kRandomSeed); functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function); } TEST(NumericDiffCostFunction, RandomizedCostFunctionRidders) { - std::unique_ptr cost_function; NumericDiffOptions options; // Larger initial step size is chosen to produce robust results in the // presence of random noise. options.ridders_relative_initial_step_size = 10.0; - cost_function.reset(new NumericDiffCostFunction( - new RandomizedCostFunction(kNoiseFactor, kRandomSeed), - TAKE_OWNERSHIP, - 1, - options)); + auto cost_function = + std::make_unique>( + new RandomizedCostFunction(kNoiseFactor, kRandomSeed), + TAKE_OWNERSHIP, + 1, + options); + RandomizedFunctor functor(kNoiseFactor, kRandomSeed); functor.ExpectCostFunctionEvaluationIsNearlyCorrect(*cost_function); } @@ -363,15 +364,15 @@ TEST(NumericDiffCostFunction, PartiallyFilledResidualShouldFailEvaluation) { double* parameters[] = {¶meter}; double* jacobians[] = {jacobian}; - std::unique_ptr cost_function( - new NumericDiffCostFunction( - new OnlyFillsOneOutputFunctor)); + auto cost_function = std::make_unique< + NumericDiffCostFunction>( + new OnlyFillsOneOutputFunctor); InvalidateArray(2, jacobian); InvalidateArray(2, residuals); EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, jacobians)); EXPECT_FALSE(IsArrayValid(2, residuals)); InvalidateArray(2, residuals); - EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, NULL)); + EXPECT_TRUE(cost_function->Evaluate(parameters, residuals, nullptr)); // We are only testing residuals here, because the Jacobians are // computed using finite differencing from the residuals, so unless // we introduce a validation step after every evaluation of @@ -385,12 +386,9 @@ TEST(NumericDiffCostFunction, ParameterBlockConstant) { constexpr int kX1 = 5; constexpr int kX2 = 5; - std::unique_ptr cost_function; - cost_function.reset(new NumericDiffCostFunction(new EasyFunctor)); + auto cost_function = std::make_unique< + NumericDiffCostFunction>( + new EasyFunctor); // Prepare the parameters and residuals. std::array x1{1e-64, 2.0, 3.0, 4.0, 5.0}; diff --git a/internal/ceres/parallel_for_cxx.cc b/internal/ceres/parallel_for_cxx.cc index 4da40c01e..a697d8039 100644 --- a/internal/ceres/parallel_for_cxx.cc +++ b/internal/ceres/parallel_for_cxx.cc @@ -125,7 +125,7 @@ void ParallelFor(ContextImpl* context, int num_threads, const std::function& function) { CHECK_GT(num_threads, 0); - CHECK(context != NULL); + CHECK(context != nullptr); if (end <= start) { return; } @@ -167,7 +167,7 @@ void ParallelFor(ContextImpl* context, int num_threads, const std::function& function) { CHECK_GT(num_threads, 0); - CHECK(context != NULL); + CHECK(context != nullptr); if (end <= start) { return; } diff --git a/internal/ceres/parallel_for_nothreads.cc b/internal/ceres/parallel_for_nothreads.cc index d036569fc..79314450f 100644 --- a/internal/ceres/parallel_for_nothreads.cc +++ b/internal/ceres/parallel_for_nothreads.cc @@ -47,7 +47,7 @@ void ParallelFor(ContextImpl* context, int num_threads, const std::function& function) { CHECK_GT(num_threads, 0); - CHECK(context != NULL); + CHECK(context != nullptr); if (end <= start) { return; } @@ -62,7 +62,7 @@ void ParallelFor(ContextImpl* context, int num_threads, const std::function& function) { CHECK_GT(num_threads, 0); - CHECK(context != NULL); + CHECK(context != nullptr); if (end <= start) { return; } diff --git a/internal/ceres/parallel_for_openmp.cc b/internal/ceres/parallel_for_openmp.cc index eb9d90581..882f244f3 100644 --- a/internal/ceres/parallel_for_openmp.cc +++ b/internal/ceres/parallel_for_openmp.cc @@ -50,7 +50,7 @@ void ParallelFor(ContextImpl* context, int num_threads, const std::function& function) { CHECK_GT(num_threads, 0); - CHECK(context != NULL); + CHECK(context != nullptr); if (end <= start) { return; } @@ -69,7 +69,7 @@ void ParallelFor(ContextImpl* context, int end, int num_threads, const std::function& function) { - CHECK(context != NULL); + CHECK(context != nullptr); ThreadTokenProvider thread_token_provider(num_threads); ParallelFor(context, start, end, num_threads, [&](int i) { diff --git a/internal/ceres/parameter_block.h b/internal/ceres/parameter_block.h index 824745c9f..304b76986 100644 --- a/internal/ceres/parameter_block.h +++ b/internal/ceres/parameter_block.h @@ -166,7 +166,7 @@ class ParameterBlock { if (new_manifold == nullptr) { manifold_ = nullptr; - plus_jacobian_.reset(nullptr); + plus_jacobian_ = nullptr; return; } @@ -180,8 +180,8 @@ class ParameterBlock { << "non-negative dimensional tangent space."; manifold_ = new_manifold; - plus_jacobian_.reset( - new double[manifold_->AmbientSize() * manifold_->TangentSize()]); + plus_jacobian_ = std::make_unique(manifold_->AmbientSize() * + manifold_->TangentSize()); CHECK(UpdatePlusJacobian()) << "Manifold::PlusJacobian computation failed for x: " << ConstVectorRef(state_, Size()).transpose(); @@ -195,7 +195,7 @@ class ParameterBlock { } if (!upper_bounds_) { - upper_bounds_.reset(new double[size_]); + upper_bounds_ = std::make_unique(size_); std::fill(upper_bounds_.get(), upper_bounds_.get() + size_, std::numeric_limits::max()); @@ -212,7 +212,7 @@ class ParameterBlock { } if (!lower_bounds_) { - lower_bounds_.reset(new double[size_]); + lower_bounds_ = std::make_unique(size_); std::fill(lower_bounds_.get(), lower_bounds_.get() + size_, -std::numeric_limits::max()); @@ -269,7 +269,7 @@ class ParameterBlock { CHECK(residual_blocks_.get() == nullptr) << "Ceres bug: There is already a residual block collection " << "for parameter block: " << ToString(); - residual_blocks_.reset(new ResidualBlockSet); + residual_blocks_ = std::make_unique(); } void AddResidualBlock(ResidualBlock* residual_block) { diff --git a/internal/ceres/parameter_block_ordering.cc b/internal/ceres/parameter_block_ordering.cc index 9899c2438..d16e6dd4f 100644 --- a/internal/ceres/parameter_block_ordering.cc +++ b/internal/ceres/parameter_block_ordering.cc @@ -163,7 +163,7 @@ void OrderingToGroupSizes(const ParameterBlockOrdering* ordering, vector* group_sizes) { CHECK(group_sizes != nullptr); group_sizes->clear(); - if (ordering == NULL) { + if (ordering == nullptr) { return; } diff --git a/internal/ceres/parameter_block_ordering_test.cc b/internal/ceres/parameter_block_ordering_test.cc index 10788930d..fd248398f 100644 --- a/internal/ceres/parameter_block_ordering_test.cc +++ b/internal/ceres/parameter_block_ordering_test.cc @@ -71,12 +71,12 @@ class SchurOrderingTest : public ::testing::Test { problem_.AddParameterBlock(z_, 5); problem_.AddParameterBlock(w_, 6); - problem_.AddResidualBlock(new DummyCostFunction<2, 3>, NULL, x_); - problem_.AddResidualBlock(new DummyCostFunction<6, 5, 4>, NULL, z_, y_); - problem_.AddResidualBlock(new DummyCostFunction<3, 3, 5>, NULL, x_, z_); - problem_.AddResidualBlock(new DummyCostFunction<7, 5, 3>, NULL, z_, x_); + problem_.AddResidualBlock(new DummyCostFunction<2, 3>, nullptr, x_); + problem_.AddResidualBlock(new DummyCostFunction<6, 5, 4>, nullptr, z_, y_); + problem_.AddResidualBlock(new DummyCostFunction<3, 3, 5>, nullptr, x_, z_); + problem_.AddResidualBlock(new DummyCostFunction<7, 5, 3>, nullptr, z_, x_); problem_.AddResidualBlock( - new DummyCostFunction<1, 5, 3, 6>, NULL, z_, x_, w_); + new DummyCostFunction<1, 5, 3, 6>, nullptr, z_, x_, w_); } ProblemImpl problem_; diff --git a/internal/ceres/partitioned_matrix_view.cc b/internal/ceres/partitioned_matrix_view.cc index b67bc905d..595b7016c 100644 --- a/internal/ceres/partitioned_matrix_view.cc +++ b/internal/ceres/partitioned_matrix_view.cc @@ -45,137 +45,137 @@ namespace ceres { namespace internal { -PartitionedMatrixViewBase* PartitionedMatrixViewBase::Create( +std::unique_ptr PartitionedMatrixViewBase::Create( const LinearSolver::Options& options, const BlockSparseMatrix& matrix) { #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION if ((options.row_block_size == 2) && (options.e_block_size == 2) && (options.f_block_size == 2)) { - return new PartitionedMatrixView<2, 2, 2>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 2) && (options.f_block_size == 3)) { - return new PartitionedMatrixView<2, 2, 3>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 2) && (options.f_block_size == 4)) { - return new PartitionedMatrixView<2, 2, 4>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 2)) { - return new PartitionedMatrixView<2, 2, Eigen::Dynamic>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 3)) { - return new PartitionedMatrixView<2, 3, 3>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 4)) { - return new PartitionedMatrixView<2, 3, 4>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 6)) { - return new PartitionedMatrixView<2, 3, 6>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 9)) { - return new PartitionedMatrixView<2, 3, 9>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 3)) { - return new PartitionedMatrixView<2, 3, Eigen::Dynamic>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 3)) { - return new PartitionedMatrixView<2, 4, 3>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 4)) { - return new PartitionedMatrixView<2, 4, 4>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 6)) { - return new PartitionedMatrixView<2, 4, 6>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 8)) { - return new PartitionedMatrixView<2, 4, 8>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 9)) { - return new PartitionedMatrixView<2, 4, 9>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 2) && (options.e_block_size == 4)) { - return new PartitionedMatrixView<2, 4, Eigen::Dynamic>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if (options.row_block_size == 2) { - return new PartitionedMatrixView<2, Eigen::Dynamic, Eigen::Dynamic>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 3) && (options.e_block_size == 3) && (options.f_block_size == 3)) { - return new PartitionedMatrixView<3, 3, 3>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 4) && (options.e_block_size == 4) && (options.f_block_size == 2)) { - return new PartitionedMatrixView<4, 4, 2>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 4) && (options.e_block_size == 4) && (options.f_block_size == 3)) { - return new PartitionedMatrixView<4, 4, 3>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 4) && (options.e_block_size == 4) && (options.f_block_size == 4)) { - return new PartitionedMatrixView<4, 4, 4>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } if ((options.row_block_size == 4) && (options.e_block_size == 4)) { - return new PartitionedMatrixView<4, 4, Eigen::Dynamic>(matrix, - options.elimination_groups[0]); + return std::make_unique>( + matrix, options.elimination_groups[0]); } #endif VLOG(1) << "Template specializations not found for <" << options.row_block_size << "," << options.e_block_size << "," << options.f_block_size << ">"; - return new PartitionedMatrixView( + return std::make_unique>( matrix, options.elimination_groups[0]); }; diff --git a/internal/ceres/partitioned_matrix_view.h b/internal/ceres/partitioned_matrix_view.h index 47610500b..36cf0d47c 100644 --- a/internal/ceres/partitioned_matrix_view.h +++ b/internal/ceres/partitioned_matrix_view.h @@ -77,11 +77,11 @@ class CERES_EXPORT_INTERNAL PartitionedMatrixViewBase { virtual void RightMultiplyF(const double* x, double* y) const = 0; // Create and return the block diagonal of the matrix E'E. - virtual BlockSparseMatrix* CreateBlockDiagonalEtE() const = 0; + virtual std::unique_ptr CreateBlockDiagonalEtE() const = 0; // Create and return the block diagonal of the matrix F'F. Caller // owns the result. - virtual BlockSparseMatrix* CreateBlockDiagonalFtF() const = 0; + virtual std::unique_ptr CreateBlockDiagonalFtF() const = 0; // Compute the block diagonal of the matrix E'E and store it in // block_diagonal. The matrix block_diagonal is expected to have a @@ -108,8 +108,8 @@ class CERES_EXPORT_INTERNAL PartitionedMatrixViewBase { virtual int num_cols() const = 0; // clang-format on - static PartitionedMatrixViewBase* Create(const LinearSolver::Options& options, - const BlockSparseMatrix& matrix); + static std::unique_ptr Create( + const LinearSolver::Options& options, const BlockSparseMatrix& matrix); }; template CreateBlockDiagonalEtE() const final; + std::unique_ptr CreateBlockDiagonalFtF() const final; void UpdateBlockDiagonalEtE(BlockSparseMatrix* block_diagonal) const final; void UpdateBlockDiagonalFtF(BlockSparseMatrix* block_diagonal) const final; // clang-format off @@ -140,8 +140,8 @@ class PartitionedMatrixView : public PartitionedMatrixViewBase { // clang-format on private: - BlockSparseMatrix* CreateBlockDiagonalMatrixLayout(int start_col_block, - int end_col_block) const; + std::unique_ptr CreateBlockDiagonalMatrixLayout( + int start_col_block, int end_col_block) const; const BlockSparseMatrix& matrix_; int num_row_blocks_e_; diff --git a/internal/ceres/partitioned_matrix_view_impl.h b/internal/ceres/partitioned_matrix_view_impl.h index 22594ef6e..4dd8d7035 100644 --- a/internal/ceres/partitioned_matrix_view_impl.h +++ b/internal/ceres/partitioned_matrix_view_impl.h @@ -235,7 +235,7 @@ void PartitionedMatrixView:: // and return a BlockSparseMatrix with the this block structure. The // caller owns the result. template -BlockSparseMatrix* +std::unique_ptr PartitionedMatrixView:: CreateBlockDiagonalMatrixLayout(int start_col_block, int end_col_block) const { @@ -270,28 +270,27 @@ PartitionedMatrixView:: // Build a BlockSparseMatrix with the just computed block // structure. - return new BlockSparseMatrix(block_diagonal_structure); + return std::make_unique(block_diagonal_structure); } template -BlockSparseMatrix* PartitionedMatrixView::CreateBlockDiagonalEtE() - const { - BlockSparseMatrix* block_diagonal = +std::unique_ptr +PartitionedMatrixView:: + CreateBlockDiagonalEtE() const { + std::unique_ptr block_diagonal = CreateBlockDiagonalMatrixLayout(0, num_col_blocks_e_); - UpdateBlockDiagonalEtE(block_diagonal); + UpdateBlockDiagonalEtE(block_diagonal.get()); return block_diagonal; } template -BlockSparseMatrix* PartitionedMatrixView::CreateBlockDiagonalFtF() - const { - BlockSparseMatrix* block_diagonal = CreateBlockDiagonalMatrixLayout( - num_col_blocks_e_, num_col_blocks_e_ + num_col_blocks_f_); - UpdateBlockDiagonalFtF(block_diagonal); +std::unique_ptr +PartitionedMatrixView:: + CreateBlockDiagonalFtF() const { + std::unique_ptr block_diagonal = + CreateBlockDiagonalMatrixLayout(num_col_blocks_e_, + num_col_blocks_e_ + num_col_blocks_f_); + UpdateBlockDiagonalFtF(block_diagonal.get()); return block_diagonal; } diff --git a/internal/ceres/partitioned_matrix_view_template.py b/internal/ceres/partitioned_matrix_view_template.py index 05a25bf83..9ab56cf87 100644 --- a/internal/ceres/partitioned_matrix_view_template.py +++ b/internal/ceres/partitioned_matrix_view_template.py @@ -128,21 +128,21 @@ FACTORY_FILE_HEADER = """ namespace ceres { namespace internal { -PartitionedMatrixViewBase* PartitionedMatrixViewBase::Create( +std::unique_ptr PartitionedMatrixViewBase::Create( const LinearSolver::Options& options, const BlockSparseMatrix& matrix) { #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION """ -FACTORY = """ return new PartitionedMatrixView<%s, %s, %s>(matrix, - options.elimination_groups[0]);""" +FACTORY = """ return std::make_unique>( + matrix, options.elimination_groups[0]);""" FACTORY_FOOTER = """ #endif VLOG(1) << "Template specializations not found for <" << options.row_block_size << "," << options.e_block_size << "," << options.f_block_size << ">"; - return new PartitionedMatrixView( + return std::make_unique>( matrix, options.elimination_groups[0]); }; diff --git a/internal/ceres/partitioned_matrix_view_test.cc b/internal/ceres/partitioned_matrix_view_test.cc index b66d0b892..d01dd3b9c 100644 --- a/internal/ceres/partitioned_matrix_view_test.cc +++ b/internal/ceres/partitioned_matrix_view_test.cc @@ -51,18 +51,18 @@ class PartitionedMatrixViewTest : public ::testing::Test { protected: void SetUp() final { srand(5); - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(2)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(2); CHECK(problem != nullptr); - A_.reset(problem->A.release()); + A_ = std::move(problem->A); num_cols_ = A_->num_cols(); num_rows_ = A_->num_rows(); num_eliminate_blocks_ = problem->num_eliminate_blocks; LinearSolver::Options options; options.elimination_groups.push_back(num_eliminate_blocks_); - pmv_.reset(PartitionedMatrixViewBase::Create( - options, *down_cast(A_.get()))); + pmv_ = PartitionedMatrixViewBase::Create( + options, *down_cast(A_.get())); } int num_rows_; diff --git a/internal/ceres/polynomial.cc b/internal/ceres/polynomial.cc index 20812f4de..feb12227d 100644 --- a/internal/ceres/polynomial.cc +++ b/internal/ceres/polynomial.cc @@ -128,12 +128,12 @@ void FindLinearPolynomialRoots(const Vector& polynomial, Vector* real, Vector* imaginary) { CHECK_EQ(polynomial.size(), 2); - if (real != NULL) { + if (real != nullptr) { real->resize(1); (*real)(0) = -polynomial(1) / polynomial(0); } - if (imaginary != NULL) { + if (imaginary != nullptr) { imaginary->setZero(1); } } @@ -147,16 +147,16 @@ void FindQuadraticPolynomialRoots(const Vector& polynomial, const double c = polynomial(2); const double D = b * b - 4 * a * c; const double sqrt_D = sqrt(fabs(D)); - if (real != NULL) { + if (real != nullptr) { real->setZero(2); } - if (imaginary != NULL) { + if (imaginary != nullptr) { imaginary->setZero(2); } // Real roots. if (D >= 0) { - if (real != NULL) { + if (real != nullptr) { // Stable quadratic roots according to BKP Horn. // http://people.csail.mit.edu/bkph/articles/Quadratics.pdf if (b >= 0) { @@ -171,11 +171,11 @@ void FindQuadraticPolynomialRoots(const Vector& polynomial, } // Use the normal quadratic formula for the complex case. - if (real != NULL) { + if (real != nullptr) { (*real)(0) = -b / (2.0 * a); (*real)(1) = -b / (2.0 * a); } - if (imaginary != NULL) { + if (imaginary != nullptr) { (*imaginary)(0) = sqrt_D / (2.0 * a); (*imaginary)(1) = -sqrt_D / (2.0 * a); } @@ -240,14 +240,14 @@ bool FindPolynomialRoots(const Vector& polynomial_in, } // Output roots - if (real != NULL) { + if (real != nullptr) { *real = solver.eigenvalues().real(); } else { - LOG(WARNING) << "NULL pointer passed as real argument to " + LOG(WARNING) << "nullptr pointer passed as real argument to " << "FindPolynomialRoots. Real parts of the roots will not " << "be returned."; } - if (imaginary != NULL) { + if (imaginary != nullptr) { *imaginary = solver.eigenvalues().imag(); } return true; @@ -304,7 +304,7 @@ void MinimizePolynomial(const Vector& polynomial, const Vector derivative = DifferentiatePolynomial(polynomial); Vector roots_real; - if (!FindPolynomialRoots(derivative, &roots_real, NULL)) { + if (!FindPolynomialRoots(derivative, &roots_real, nullptr)) { LOG(WARNING) << "Unable to find the critical points of " << "the interpolating polynomial."; return; diff --git a/internal/ceres/polynomial.h b/internal/ceres/polynomial.h index 20071f2c6..3d43284cf 100644 --- a/internal/ceres/polynomial.h +++ b/internal/ceres/polynomial.h @@ -64,8 +64,8 @@ inline double EvaluatePolynomial(const Vector& polynomial, double x) { // Failure indicates that the polynomial is invalid (of size 0) or // that the eigenvalues of the companion matrix could not be computed. // On failure, a more detailed message will be written to LOG(ERROR). -// If real is not NULL, the real parts of the roots will be returned in it. -// Likewise, if imaginary is not NULL, imaginary parts will be returned in it. +// If real is not nullptr, the real parts of the roots will be returned in it. +// Likewise, if imaginary is not nullptr, imaginary parts will be returned in it. CERES_EXPORT_INTERNAL bool FindPolynomialRoots(const Vector& polynomial, Vector* real, Vector* imaginary); diff --git a/internal/ceres/polynomial_test.cc b/internal/ceres/polynomial_test.cc index 0ff73eab1..c45557e4a 100644 --- a/internal/ceres/polynomial_test.cc +++ b/internal/ceres/polynomial_test.cc @@ -88,8 +88,8 @@ Vector SortVector(const Vector& in) { } // Run a test with the polynomial defined by the N real roots in roots_real. -// If use_real is false, NULL is passed as the real argument to -// FindPolynomialRoots. If use_imaginary is false, NULL is passed as the +// If use_real is false, nullptr is passed as the real argument to +// FindPolynomialRoots. If use_imaginary is false, nullptr is passed as the // imaginary argument to FindPolynomialRoots. template void RunPolynomialTestRealRoots(const double (&real_roots)[N], @@ -102,8 +102,8 @@ void RunPolynomialTestRealRoots(const double (&real_roots)[N], for (int i = 0; i < N; ++i) { poly = AddRealRoot(poly, real_roots[i]); } - Vector* const real_ptr = use_real ? &real : NULL; - Vector* const imaginary_ptr = use_imaginary ? &imaginary : NULL; + Vector* const real_ptr = use_real ? &real : nullptr; + Vector* const imaginary_ptr = use_imaginary ? &imaginary : nullptr; bool success = FindPolynomialRoots(poly, real_ptr, imaginary_ptr); EXPECT_EQ(success, true); diff --git a/internal/ceres/preconditioner.h b/internal/ceres/preconditioner.h index 6a6bc615d..e0eb88d64 100644 --- a/internal/ceres/preconditioner.h +++ b/internal/ceres/preconditioner.h @@ -126,7 +126,7 @@ class CERES_EXPORT_INTERNAL Preconditioner : public LinearOperator { // for some vector b. It is important that the matrix A have the // same block structure as the one used to construct this object. // - // D can be NULL, in which case its interpreted as a diagonal matrix + // D can be nullptr, in which case its interpreted as a diagonal matrix // of size zero. virtual bool Update(const LinearOperator& A, const double* D) = 0; diff --git a/internal/ceres/preprocessor.cc b/internal/ceres/preprocessor.cc index 097ac6480..1fcd74a8d 100644 --- a/internal/ceres/preprocessor.cc +++ b/internal/ceres/preprocessor.cc @@ -51,7 +51,7 @@ Preprocessor* Preprocessor::Create(MinimizerType minimizer_type) { } LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type; - return NULL; + return nullptr; } Preprocessor::~Preprocessor() = default; @@ -85,15 +85,15 @@ void SetupCommonMinimizerOptions(PreprocessedProblem* pp) { minimizer_options.evaluator = pp->evaluator; if (options.logging_type != SILENT) { - pp->logging_callback.reset(new LoggingCallback( - options.minimizer_type, options.minimizer_progress_to_stdout)); + pp->logging_callback = std::make_unique( + options.minimizer_type, options.minimizer_progress_to_stdout); minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(), pp->logging_callback.get()); } if (options.update_state_every_iteration) { - pp->state_updating_callback.reset( - new StateUpdatingCallback(program, reduced_parameters)); + pp->state_updating_callback = + std::make_unique(program, reduced_parameters); // This must get pushed to the front of the callbacks so that it // is run before any of the user callbacks. minimizer_options.callbacks.insert(minimizer_options.callbacks.begin(), diff --git a/internal/ceres/problem_impl.cc b/internal/ceres/problem_impl.cc index a49cd5e7d..f256aab6d 100644 --- a/internal/ceres/problem_impl.cc +++ b/internal/ceres/problem_impl.cc @@ -763,8 +763,8 @@ bool ProblemImpl::Evaluate(const Problem::EvaluateOptions& evaluate_options, std::unique_ptr tmp_jacobian; if (jacobian != nullptr) { - tmp_jacobian.reset( - down_cast(evaluator->CreateJacobian())); + tmp_jacobian.reset(down_cast( + evaluator->CreateJacobian().release())); } // Point the state pointers to the user state pointers. This is diff --git a/internal/ceres/problem_test.cc b/internal/ceres/problem_test.cc index 483ae071b..1987e1354 100644 --- a/internal/ceres/problem_test.cc +++ b/internal/ceres/problem_test.cc @@ -429,7 +429,7 @@ struct DynamicProblem : public ::testing::TestWithParam { DynamicProblem() { Problem::Options options; options.enable_fast_removal = GetParam(); - problem.reset(new ProblemImpl(options)); + problem = std::make_unique(options); } ParameterBlock* GetParameterBlock(int block) { diff --git a/internal/ceres/program.cc b/internal/ceres/program.cc index 516d58d97..adfff0b00 100644 --- a/internal/ceres/program.cc +++ b/internal/ceres/program.cc @@ -279,7 +279,7 @@ bool Program::IsFeasible(std::string* message) const { return true; } -Program* Program::CreateReducedProgram( +std::unique_ptr Program::CreateReducedProgram( std::vector* removed_parameter_blocks, double* fixed_cost, std::string* error) const { @@ -287,14 +287,14 @@ Program* Program::CreateReducedProgram( CHECK(fixed_cost != nullptr); CHECK(error != nullptr); - std::unique_ptr reduced_program(new Program(*this)); + std::unique_ptr reduced_program = std::make_unique(*this); if (!reduced_program->RemoveFixedBlocks( removed_parameter_blocks, fixed_cost, error)) { return nullptr; } reduced_program->SetParameterOffsetsAndIndex(); - return reduced_program.release(); + return reduced_program; } bool Program::RemoveFixedBlocks(std::vector* removed_parameter_blocks, @@ -305,8 +305,8 @@ bool Program::RemoveFixedBlocks(std::vector* removed_parameter_blocks, CHECK(error != nullptr); std::unique_ptr residual_block_evaluate_scratch; - residual_block_evaluate_scratch.reset( - new double[MaxScratchDoublesNeededForEvaluate()]); + residual_block_evaluate_scratch = + std::make_unique(MaxScratchDoublesNeededForEvaluate()); *fixed_cost = 0.0; bool need_to_call_prepare_for_evaluation = evaluation_callback_ != nullptr; diff --git a/internal/ceres/program.h b/internal/ceres/program.h index 343cf8e6c..ffd5db73a 100644 --- a/internal/ceres/program.h +++ b/internal/ceres/program.h @@ -146,12 +146,13 @@ class CERES_EXPORT_INTERNAL Program { // fixed_cost will be equal to the sum of the costs of the residual // blocks that were removed. // - // If there was a problem, then the function will return a NULL + // If there was a problem, then the function will return a nullptr // pointer and error will contain a human readable description of // the problem. - Program* CreateReducedProgram(std::vector* removed_parameter_blocks, - double* fixed_cost, - std::string* error) const; + std::unique_ptr CreateReducedProgram( + std::vector* removed_parameter_blocks, + double* fixed_cost, + std::string* error) const; // See problem.h for what these do. int NumParameterBlocks() const; diff --git a/internal/ceres/program_evaluator.h b/internal/ceres/program_evaluator.h index 4ab557fc5..2ab52da38 100644 --- a/internal/ceres/program_evaluator.h +++ b/internal/ceres/program_evaluator.h @@ -59,7 +59,7 @@ // class JacobianWriter { // // Create a jacobian that this writer can write. Same as // // Evaluator::CreateJacobian. -// SparseMatrix* CreateJacobian() const; +// std::unique_ptr CreateJacobian() const; // // // Create num_threads evaluate preparers. Caller owns result which must // // be freed with delete[]. Resulting preparers are valid while *this is. @@ -127,12 +127,12 @@ class ProgramEvaluator : public Evaluator { #endif // CERES_NO_THREADS BuildResidualLayout(*program, &residual_layout_); - evaluate_scratch_.reset( - CreateEvaluatorScratch(*program, options.num_threads)); + evaluate_scratch_ = + std::move(CreateEvaluatorScratch(*program, options.num_threads)); } // Implementation of Evaluator interface. - SparseMatrix* CreateJacobian() const final { + std::unique_ptr CreateJacobian() const final { return jacobian_writer_.CreateJacobian(); } @@ -309,13 +309,14 @@ class ProgramEvaluator : public Evaluator { int max_scratch_doubles_needed_for_evaluate, int max_residuals_per_residual_block, int num_parameters) { - residual_block_evaluate_scratch.reset( - new double[max_scratch_doubles_needed_for_evaluate]); - gradient.reset(new double[num_parameters]); + residual_block_evaluate_scratch = + std::make_unique(max_scratch_doubles_needed_for_evaluate); + gradient = std::make_unique(num_parameters); VectorRef(gradient.get(), num_parameters).setZero(); - residual_block_residuals.reset( - new double[max_residuals_per_residual_block]); - jacobian_block_ptrs.reset(new double*[max_parameters_per_residual_block]); + residual_block_residuals = + std::make_unique(max_residuals_per_residual_block); + jacobian_block_ptrs = + std::make_unique(max_parameters_per_residual_block); } double cost; @@ -341,8 +342,8 @@ class ProgramEvaluator : public Evaluator { } // Create scratch space for each thread evaluating the program. - static EvaluateScratch* CreateEvaluatorScratch(const Program& program, - int num_threads) { + static std::unique_ptr CreateEvaluatorScratch( + const Program& program, int num_threads) { int max_parameters_per_residual_block = program.MaxParametersPerResidualBlock(); int max_scratch_doubles_needed_for_evaluate = @@ -351,7 +352,7 @@ class ProgramEvaluator : public Evaluator { program.MaxResidualsPerResidualBlock(); int num_parameters = program.NumEffectiveParameters(); - EvaluateScratch* evaluate_scratch = new EvaluateScratch[num_threads]; + auto evaluate_scratch = std::make_unique(num_threads); for (int i = 0; i < num_threads; i++) { evaluate_scratch[i].Init(max_parameters_per_residual_block, max_scratch_doubles_needed_for_evaluate, diff --git a/internal/ceres/reorder_program.cc b/internal/ceres/reorder_program.cc index eefb0852d..2d93487e7 100644 --- a/internal/ceres/reorder_program.cc +++ b/internal/ceres/reorder_program.cc @@ -291,7 +291,7 @@ bool LexicographicallyOrderResidualBlocks( // filling is finished, the offset pointerts should have shifted down one // entry (this is verified below). vector reordered_residual_blocks( - (*residual_blocks).size(), static_cast(NULL)); + (*residual_blocks).size(), static_cast(nullptr)); for (int i = 0; i < residual_blocks->size(); ++i) { int bucket = min_position_per_residual[i]; @@ -299,7 +299,7 @@ bool LexicographicallyOrderResidualBlocks( offsets[bucket]--; // Sanity. - CHECK(reordered_residual_blocks[offsets[bucket]] == NULL) + CHECK(reordered_residual_blocks[offsets[bucket]] == nullptr) << "Congratulations, you found a Ceres bug! Please report this error " << "to the developers."; @@ -313,9 +313,9 @@ bool LexicographicallyOrderResidualBlocks( << "Congratulations, you found a Ceres bug! Please report this error " << "to the developers."; } - // Sanity check #2: No NULL's left behind. + // Sanity check #2: No nullptr's left behind. for (int i = 0; i < reordered_residual_blocks.size(); ++i) { - CHECK(reordered_residual_blocks[i] != NULL) + CHECK(reordered_residual_blocks[i] != nullptr) << "Congratulations, you found a Ceres bug! Please report this error " << "to the developers."; } @@ -441,7 +441,7 @@ bool ReorderProgramForSchurTypeLinearSolver( if (parameter_block_ordering->NumGroups() == 1) { // If the user supplied an parameter_block_ordering with just one - // group, it is equivalent to the user supplying NULL as an + // group, it is equivalent to the user supplying nullptr as an // parameter_block_ordering. Ceres is completely free to choose the // parameter block ordering as it sees fit. For Schur type solvers, // this means that the user wishes for Ceres to identify the diff --git a/internal/ceres/reorder_program_test.cc b/internal/ceres/reorder_program_test.cc index 37a07cb6c..427334e13 100644 --- a/internal/ceres/reorder_program_test.cc +++ b/internal/ceres/reorder_program_test.cc @@ -78,14 +78,14 @@ TEST(_, ReorderResidualBlockNormalFunction) { problem.AddResidualBlock(new BinaryCostFunction(), nullptr, &x, &y); problem.AddResidualBlock(new UnaryCostFunction(), nullptr, &y); - ParameterBlockOrdering* linear_solver_ordering = new ParameterBlockOrdering; + auto linear_solver_ordering = std::make_shared(); linear_solver_ordering->AddElementToGroup(&x, 0); linear_solver_ordering->AddElementToGroup(&y, 0); linear_solver_ordering->AddElementToGroup(&z, 1); Solver::Options options; options.linear_solver_type = DENSE_SCHUR; - options.linear_solver_ordering.reset(linear_solver_ordering); + options.linear_solver_ordering = linear_solver_ordering; const vector& residual_blocks = problem.program().residual_blocks(); diff --git a/internal/ceres/residual_block.h b/internal/ceres/residual_block.h index 888a44a68..80c8dd620 100644 --- a/internal/ceres/residual_block.h +++ b/internal/ceres/residual_block.h @@ -77,9 +77,9 @@ class CERES_EXPORT_INTERNAL ResidualBlock { // Evaluates the residual term, storing the scalar cost in *cost, the residual // components in *residuals, and the jacobians between the parameters and - // residuals in jacobians[i], in row-major order. If residuals is NULL, the - // residuals are not computed. If jacobians is NULL, no jacobians are - // computed. If jacobians[i] is NULL, then the jacobian for that parameter is + // residuals in jacobians[i], in row-major order. If residuals is nullptr, the + // residuals are not computed. If jacobians is nullptr, no jacobians are + // computed. If jacobians[i] is nullptr, then the jacobian for that parameter is // not computed. // // cost must not be null. diff --git a/internal/ceres/residual_block_test.cc b/internal/ceres/residual_block_test.cc index d5870582b..f01d842ce 100644 --- a/internal/ceres/residual_block_test.cc +++ b/internal/ceres/residual_block_test.cc @@ -64,7 +64,7 @@ class TernaryCostFunction : public CostFunction { } if (jacobians) { for (int k = 0; k < 3; ++k) { - if (jacobians[k] != NULL) { + if (jacobians[k] != nullptr) { MatrixRef jacobian( jacobians[k], num_residuals(), parameter_block_sizes()[k]); jacobian.setConstant(k); @@ -96,11 +96,11 @@ TEST(ResidualBlock, EvaluteWithNoLossFunctionOrManifolds) { TernaryCostFunction cost_function(3, 2, 3, 4); // Create the object under tests. - ResidualBlock residual_block(&cost_function, NULL, parameters, -1); + ResidualBlock residual_block(&cost_function, nullptr, parameters, -1); // Verify getters. EXPECT_EQ(&cost_function, residual_block.cost_function()); - EXPECT_EQ(NULL, residual_block.loss_function()); + EXPECT_EQ(nullptr, residual_block.loss_function()); EXPECT_EQ(parameters[0], residual_block.parameter_blocks()[0]); EXPECT_EQ(parameters[1], residual_block.parameter_blocks()[1]); EXPECT_EQ(parameters[2], residual_block.parameter_blocks()[2]); @@ -108,12 +108,12 @@ TEST(ResidualBlock, EvaluteWithNoLossFunctionOrManifolds) { // Verify cost-only evaluation. double cost; - residual_block.Evaluate(true, &cost, NULL, NULL, scratch); + residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch); EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); // Verify cost and residual evaluation. double residuals[3]; - residual_block.Evaluate(true, &cost, residuals, NULL, scratch); + residual_block.Evaluate(true, &cost, residuals, nullptr, scratch); EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); EXPECT_EQ(0.0, residuals[0]); EXPECT_EQ(1.0, residuals[1]); @@ -151,7 +151,7 @@ TEST(ResidualBlock, EvaluteWithNoLossFunctionOrManifolds) { jacobian_ry.setConstant(-1.0); jacobian_rz.setConstant(-1.0); - jacobian_ptrs[1] = NULL; // Don't compute the jacobian for y. + jacobian_ptrs[1] = nullptr; // Don't compute the jacobian for y. residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch); EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); @@ -187,7 +187,7 @@ class LocallyParameterizedCostFunction : public SizedCostFunction<3, 2, 3, 4> { // 0 1 2 3 4 ... // 0 1 2 3 4 ... // - if (jacobians[k] != NULL) { + if (jacobians[k] != nullptr) { MatrixRef jacobian( jacobians[k], num_residuals(), parameter_block_sizes()[k]); for (int j = 0; j < k + 2; ++j) { @@ -233,11 +233,11 @@ TEST(ResidualBlock, EvaluteWithManifolds) { LocallyParameterizedCostFunction cost_function; // Create the object under tests. - ResidualBlock residual_block(&cost_function, NULL, parameters, -1); + ResidualBlock residual_block(&cost_function, nullptr, parameters, -1); // Verify getters. EXPECT_EQ(&cost_function, residual_block.cost_function()); - EXPECT_EQ(NULL, residual_block.loss_function()); + EXPECT_EQ(nullptr, residual_block.loss_function()); EXPECT_EQ(parameters[0], residual_block.parameter_blocks()[0]); EXPECT_EQ(parameters[1], residual_block.parameter_blocks()[1]); EXPECT_EQ(parameters[2], residual_block.parameter_blocks()[2]); @@ -245,12 +245,12 @@ TEST(ResidualBlock, EvaluteWithManifolds) { // Verify cost-only evaluation. double cost; - residual_block.Evaluate(true, &cost, NULL, NULL, scratch); + residual_block.Evaluate(true, &cost, nullptr, nullptr, scratch); EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); // Verify cost and residual evaluation. double residuals[3]; - residual_block.Evaluate(true, &cost, residuals, NULL, scratch); + residual_block.Evaluate(true, &cost, residuals, nullptr, scratch); EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); EXPECT_EQ(0.0, residuals[0]); EXPECT_EQ(1.0, residuals[1]); @@ -311,7 +311,7 @@ TEST(ResidualBlock, EvaluteWithManifolds) { jacobian_ry.setConstant(-1.0); jacobian_rz.setConstant(-1.0); - jacobian_ptrs[1] = NULL; // Don't compute the jacobian for y. + jacobian_ptrs[1] = nullptr; // Don't compute the jacobian for y. residual_block.Evaluate(true, &cost, residuals, jacobian_ptrs, scratch); EXPECT_EQ(0.5 * (0 * 0 + 1 * 1 + 2 * 2), cost); diff --git a/internal/ceres/residual_block_utils.cc b/internal/ceres/residual_block_utils.cc index d5b3fa1fa..17cf619ea 100644 --- a/internal/ceres/residual_block_utils.cc +++ b/internal/ceres/residual_block_utils.cc @@ -56,7 +56,7 @@ void InvalidateEvaluation(const ResidualBlock& block, InvalidateArray(1, cost); InvalidateArray(num_residuals, residuals); - if (jacobians != NULL) { + if (jacobians != nullptr) { for (int i = 0; i < num_parameter_blocks; ++i) { const int parameter_block_size = block.parameter_blocks()[i]->Size(); InvalidateArray(num_residuals * parameter_block_size, jacobians[i]); @@ -104,9 +104,9 @@ string EvaluationToString(const ResidualBlock& block, StringAppendF(&result, "| "); for (int k = 0; k < num_residuals; ++k) { AppendArrayToString(1, - (jacobians != NULL && jacobians[i] != NULL) + (jacobians != nullptr && jacobians[i] != nullptr) ? jacobians[i] + k * parameter_block_size + j - : NULL, + : nullptr, &result); } StringAppendF(&result, "\n"); @@ -129,7 +129,7 @@ bool IsEvaluationValid(const ResidualBlock& block, return false; } - if (jacobians != NULL) { + if (jacobians != nullptr) { for (int i = 0; i < num_parameter_blocks; ++i) { const int parameter_block_size = block.parameter_blocks()[i]->Size(); if (!IsArrayValid(num_residuals * parameter_block_size, jacobians[i])) { diff --git a/internal/ceres/residual_block_utils.h b/internal/ceres/residual_block_utils.h index 41ae81abc..1ffff8e4a 100644 --- a/internal/ceres/residual_block_utils.h +++ b/internal/ceres/residual_block_utils.h @@ -52,7 +52,7 @@ namespace internal { class ResidualBlock; -// Invalidate cost, resdual and jacobian arrays (if not NULL). +// Invalidate cost, resdual and jacobian arrays (if not nullptr). void InvalidateEvaluation(const ResidualBlock& block, double* cost, double* residuals, diff --git a/internal/ceres/residual_block_utils_test.cc b/internal/ceres/residual_block_utils_test.cc index 331f5ab47..2254619d9 100644 --- a/internal/ceres/residual_block_utils_test.cc +++ b/internal/ceres/residual_block_utils_test.cc @@ -51,7 +51,7 @@ static void CheckEvaluation(const CostFunction& cost_function, bool is_good) { std::vector parameter_blocks; parameter_blocks.push_back(¶meter_block); - ResidualBlock residual_block(&cost_function, NULL, parameter_blocks, -1); + ResidualBlock residual_block(&cost_function, nullptr, parameter_blocks, -1); std::unique_ptr scratch( new double[residual_block.NumScratchDoublesForEvaluate()]); @@ -74,7 +74,7 @@ class GoodCostFunction : public SizedCostFunction<1, 1> { double* residuals, double** jacobians) const final { residuals[0] = 1; - if (jacobians != NULL && jacobians[0] != NULL) { + if (jacobians != nullptr && jacobians[0] != nullptr) { jacobians[0][0] = 0.0; } return true; @@ -90,7 +90,7 @@ class NoResidualUpdateCostFunction : public SizedCostFunction<1, 1> { double** jacobians) const final { // Forget to update the residuals. // residuals[0] = 1; - if (jacobians != NULL && jacobians[0] != NULL) { + if (jacobians != nullptr && jacobians[0] != nullptr) { jacobians[0][0] = 0.0; } return true; @@ -103,7 +103,7 @@ class NoJacobianUpdateCostFunction : public SizedCostFunction<1, 1> { double* residuals, double** jacobians) const final { residuals[0] = 1; - if (jacobians != NULL && jacobians[0] != NULL) { + if (jacobians != nullptr && jacobians[0] != nullptr) { // Forget to update the jacobians. // jacobians[0][0] = 0.0; } @@ -117,7 +117,7 @@ class BadResidualCostFunction : public SizedCostFunction<1, 1> { double* residuals, double** jacobians) const final { residuals[0] = std::numeric_limits::infinity(); - if (jacobians != NULL && jacobians[0] != NULL) { + if (jacobians != nullptr && jacobians[0] != nullptr) { jacobians[0][0] = 0.0; } return true; @@ -130,7 +130,7 @@ class BadJacobianCostFunction : public SizedCostFunction<1, 1> { double* residuals, double** jacobians) const final { residuals[0] = 1.0; - if (jacobians != NULL && jacobians[0] != NULL) { + if (jacobians != nullptr && jacobians[0] != nullptr) { jacobians[0][0] = std::numeric_limits::quiet_NaN(); } return true; diff --git a/internal/ceres/rotation_test.cc b/internal/ceres/rotation_test.cc index e28185c6d..951d7caba 100644 --- a/internal/ceres/rotation_test.cc +++ b/internal/ceres/rotation_test.cc @@ -687,7 +687,7 @@ J4 MakeJ4(double a, double v0, double v1, double v2, double v3) { bool IsClose(double x, double y) { EXPECT_FALSE(isnan(x)); EXPECT_FALSE(isnan(y)); - return internal::IsClose(x, y, kTolerance, NULL, NULL); + return internal::IsClose(x, y, kTolerance, nullptr, nullptr); } } // namespace diff --git a/internal/ceres/schur_complement_solver.cc b/internal/ceres/schur_complement_solver.cc index 57419c2bf..15c16293d 100644 --- a/internal/ceres/schur_complement_solver.cc +++ b/internal/ceres/schur_complement_solver.cc @@ -150,9 +150,9 @@ LinearSolver::Summary SchurComplementSolver::SolveImpl( // mechanism that does not cause binary bloat. if (options_.row_block_size == 2 && options_.e_block_size == 3 && options_.f_block_size == 6 && num_f_blocks == 1) { - eliminator_.reset(new SchurEliminatorForOneFBlock<2, 3, 6>); + eliminator_ = std::make_unique>(); } else { - eliminator_.reset(SchurEliminatorBase::Create(options_)); + eliminator_ = SchurEliminatorBase::Create(options_); } CHECK(eliminator_); @@ -202,8 +202,8 @@ void DenseSchurComplementSolver::InitStorage( blocks[j] = bs->cols[i].size; } - set_lhs(new BlockRandomAccessDenseMatrix(blocks)); - set_rhs(new double[lhs()->num_rows()]); + set_lhs(std::make_unique(blocks)); + set_rhs(std::make_unique(lhs()->num_rows())); } // Solve the system Sx = r, assuming that the matrix S is stored in a @@ -309,8 +309,9 @@ void SparseSchurComplementSolver::InitStorage( } } - set_lhs(new BlockRandomAccessSparseMatrix(blocks_, block_pairs)); - set_rhs(new double[lhs()->num_rows()]); + set_lhs( + std::make_unique(blocks_, block_pairs)); + set_rhs(std::make_unique(lhs()->num_rows())); } LinearSolver::Summary SparseSchurComplementSolver::SolveReducedLinearSystem( @@ -335,11 +336,10 @@ LinearSolver::Summary SparseSchurComplementSolver::SolveReducedLinearSystem( const CompressedRowSparseMatrix::StorageType storage_type = sparse_cholesky_->StorageType(); if (storage_type == CompressedRowSparseMatrix::UPPER_TRIANGULAR) { - lhs.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm)); + lhs = CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm); lhs->set_storage_type(CompressedRowSparseMatrix::UPPER_TRIANGULAR); } else { - lhs.reset( - CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm)); + lhs = CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm); lhs->set_storage_type(CompressedRowSparseMatrix::LOWER_TRIANGULAR); } @@ -371,7 +371,8 @@ SparseSchurComplementSolver::SolveReducedLinearSystemUsingConjugateGradients( CHECK_EQ(options().preconditioner_type, SCHUR_JACOBI); if (preconditioner_.get() == nullptr) { - preconditioner_.reset(new BlockRandomAccessDiagonalMatrix(blocks_)); + preconditioner_ = + std::make_unique(blocks_); } BlockRandomAccessSparseMatrix* sc = @@ -401,10 +402,11 @@ SparseSchurComplementSolver::SolveReducedLinearSystemUsingConjugateGradients( VectorRef(solution, num_rows).setZero(); - std::unique_ptr lhs_adapter( - new BlockRandomAccessSparseMatrixAdapter(*sc)); - std::unique_ptr preconditioner_adapter( - new BlockRandomAccessDiagonalMatrixAdapter(*preconditioner_)); + std::unique_ptr lhs_adapter = + std::make_unique(*sc); + std::unique_ptr preconditioner_adapter = + std::make_unique( + *preconditioner_); LinearSolver::Options cg_options; cg_options.min_num_iterations = options().min_num_iterations; diff --git a/internal/ceres/schur_complement_solver.h b/internal/ceres/schur_complement_solver.h index b7cb1d9f0..3d343bf71 100644 --- a/internal/ceres/schur_complement_solver.h +++ b/internal/ceres/schur_complement_solver.h @@ -125,11 +125,13 @@ class CERES_EXPORT_INTERNAL SchurComplementSolver protected: const LinearSolver::Options& options() const { return options_; } - void set_lhs(BlockRandomAccessMatrix* lhs) { lhs_.reset(lhs); } + void set_lhs(std::unique_ptr lhs) { + lhs_ = std::move(lhs); + } const BlockRandomAccessMatrix* lhs() const { return lhs_.get(); } BlockRandomAccessMatrix* mutable_lhs() { return lhs_.get(); } - void set_rhs(double* rhs) { rhs_.reset(rhs); } + void set_rhs(std::unique_ptr rhs) { rhs_ = std::move(rhs); } const double* rhs() const { return rhs_.get(); } private: diff --git a/internal/ceres/schur_complement_solver_test.cc b/internal/ceres/schur_complement_solver_test.cc index 550733e52..e943ed1e2 100644 --- a/internal/ceres/schur_complement_solver_test.cc +++ b/internal/ceres/schur_complement_solver_test.cc @@ -51,13 +51,13 @@ namespace internal { class SchurComplementSolverTest : public ::testing::Test { protected: void SetUpFromProblemId(int problem_id) { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(problem_id)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(problem_id); CHECK(problem != nullptr); A.reset(down_cast(problem->A.release())); - b.reset(problem->b.release()); - D.reset(problem->D.release()); + b = std::move(problem->b); + D = std::move(problem->D); num_cols = A->num_cols(); num_rows = A->num_rows(); diff --git a/internal/ceres/schur_eliminator.cc b/internal/ceres/schur_eliminator.cc index 613ae9558..bcffbe6db 100644 --- a/internal/ceres/schur_eliminator.cc +++ b/internal/ceres/schur_eliminator.cc @@ -45,115 +45,116 @@ namespace ceres { namespace internal { -SchurEliminatorBase* SchurEliminatorBase::Create( +std::unique_ptr SchurEliminatorBase::Create( const LinearSolver::Options& options) { #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION if ((options.row_block_size == 2) && (options.e_block_size == 2) && (options.f_block_size == 2)) { - return new SchurEliminator<2, 2, 2>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 2) && (options.f_block_size == 3)) { - return new SchurEliminator<2, 2, 3>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 2) && (options.f_block_size == 4)) { - return new SchurEliminator<2, 2, 4>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 2)) { - return new SchurEliminator<2, 2, Eigen::Dynamic>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 3)) { - return new SchurEliminator<2, 3, 3>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 4)) { - return new SchurEliminator<2, 3, 4>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 6)) { - return new SchurEliminator<2, 3, 6>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 3) && (options.f_block_size == 9)) { - return new SchurEliminator<2, 3, 9>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 3)) { - return new SchurEliminator<2, 3, Eigen::Dynamic>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 3)) { - return new SchurEliminator<2, 4, 3>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 4)) { - return new SchurEliminator<2, 4, 4>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 6)) { - return new SchurEliminator<2, 4, 6>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 8)) { - return new SchurEliminator<2, 4, 8>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 4) && (options.f_block_size == 9)) { - return new SchurEliminator<2, 4, 9>(options); + return std::make_unique>(options); } if ((options.row_block_size == 2) && (options.e_block_size == 4)) { - return new SchurEliminator<2, 4, Eigen::Dynamic>(options); + return std::make_unique>(options); } if (options.row_block_size == 2) { - return new SchurEliminator<2, Eigen::Dynamic, Eigen::Dynamic>(options); + return std::make_unique>(options); } if ((options.row_block_size == 3) && (options.e_block_size == 3) && (options.f_block_size == 3)) { - return new SchurEliminator<3, 3, 3>(options); + return std::make_unique>(options); } if ((options.row_block_size == 4) && (options.e_block_size == 4) && (options.f_block_size == 2)) { - return new SchurEliminator<4, 4, 2>(options); + return std::make_unique>(options); } if ((options.row_block_size == 4) && (options.e_block_size == 4) && (options.f_block_size == 3)) { - return new SchurEliminator<4, 4, 3>(options); + return std::make_unique>(options); } if ((options.row_block_size == 4) && (options.e_block_size == 4) && (options.f_block_size == 4)) { - return new SchurEliminator<4, 4, 4>(options); + return std::make_unique>(options); } if ((options.row_block_size == 4) && (options.e_block_size == 4)) { - return new SchurEliminator<4, 4, Eigen::Dynamic>(options); + return std::make_unique>(options); } #endif VLOG(1) << "Template specializations not found for <" << options.row_block_size << "," << options.e_block_size << "," << options.f_block_size << ">"; - return new SchurEliminator( - options); + return std::make_unique>(options); } } // namespace internal diff --git a/internal/ceres/schur_eliminator.h b/internal/ceres/schur_eliminator.h index a9eada5e9..cf7a3e49e 100644 --- a/internal/ceres/schur_eliminator.h +++ b/internal/ceres/schur_eliminator.h @@ -209,7 +209,8 @@ class CERES_EXPORT_INTERNAL SchurEliminatorBase { const double* z, double* y) = 0; // Factory - static SchurEliminatorBase* Create(const LinearSolver::Options& options); + static std::unique_ptr Create( + const LinearSolver::Options& options); }; // Templated implementation of the SchurEliminatorBase interface. The @@ -483,7 +484,7 @@ class SchurEliminatorForOneFBlock : public SchurEliminatorBase { Eigen::Matrix f_t_b; // Add the square of the diagonal to e_t_e. - if (D != NULL) { + if (D != nullptr) { const typename EigenTypes::ConstVectorRef diag( D + bs->cols[e_block_id].position, kEBlockSize); e_t_e = diag.array().square().matrix().asDiagonal(); diff --git a/internal/ceres/schur_eliminator_benchmark.cc b/internal/ceres/schur_eliminator_benchmark.cc index 73bdfd9dd..76ccfaad1 100644 --- a/internal/ceres/schur_eliminator_benchmark.cc +++ b/internal/ceres/schur_eliminator_benchmark.cc @@ -88,7 +88,7 @@ class BenchmarkData { } } - matrix_.reset(new BlockSparseMatrix(bs)); + matrix_ = std::make_unique(bs); double* values = matrix_->mutable_values(); for (int i = 0; i < matrix_->num_nonzeros(); ++i) { values[i] = RandNormal(); @@ -98,7 +98,7 @@ class BenchmarkData { b_.setRandom(); std::vector blocks(1, kFBlockSize); - lhs_.reset(new BlockRandomAccessDenseMatrix(blocks)); + lhs_ = std::make_unique(blocks); diagonal_.resize(matrix_->num_cols()); diagonal_.setOnes(); rhs_.resize(kFBlockSize); @@ -192,7 +192,8 @@ static void BM_SchurEliminatorForOneFBlockEliminate(benchmark::State& state) { } } -static void BM_SchurEliminatorForOneFBlockBackSubstitute(benchmark::State& state) { +static void BM_SchurEliminatorForOneFBlockBackSubstitute( + benchmark::State& state) { const int num_e_blocks = state.range(0); BenchmarkData data(num_e_blocks); SchurEliminatorForOneFBlock<2, 3, 6> eliminator; diff --git a/internal/ceres/schur_eliminator_impl.h b/internal/ceres/schur_eliminator_impl.h index 9b2ff0ec2..271d4cbb2 100644 --- a/internal/ceres/schur_eliminator_impl.h +++ b/internal/ceres/schur_eliminator_impl.h @@ -159,12 +159,13 @@ void SchurEliminator::Init( uneliminated_row_begins_ = chunk.start + chunk.size; - buffer_.reset(new double[buffer_size_ * num_threads_]); + buffer_ = std::make_unique(buffer_size_ * num_threads_); // chunk_outer_product_buffer_ only needs to store e_block_size * // f_block_size, which is always less than buffer_size_, so we just // allocate buffer_size_ per thread. - chunk_outer_product_buffer_.reset(new double[buffer_size_ * num_threads_]); + chunk_outer_product_buffer_ = + std::make_unique(buffer_size_ * num_threads_); STLDeleteElements(&rhs_locks_); rhs_locks_.resize(num_col_blocks - num_eliminate_blocks_); @@ -191,7 +192,7 @@ void SchurEliminator::Eliminate( const int num_col_blocks = bs->cols.size(); // Add the diagonal to the schur complement. - if (D != NULL) { + if (D != nullptr) { ParallelFor(context_, num_eliminate_blocks_, num_col_blocks, @@ -201,7 +202,7 @@ void SchurEliminator::Eliminate( int r, c, row_stride, col_stride; CellInfo* cell_info = lhs->GetCell( block_id, block_id, &r, &c, &row_stride, &col_stride); - if (cell_info != NULL) { + if (cell_info != nullptr) { const int block_size = bs->cols[i].size; typename EigenTypes::ConstVectorRef diag( D + bs->cols[i].position, block_size); @@ -243,7 +244,7 @@ void SchurEliminator::Eliminate( typename EigenTypes::Matrix ete(e_block_size, e_block_size); - if (D != NULL) { + if (D != nullptr) { const typename EigenTypes::ConstVectorRef diag( D + bs->cols[e_block_id].position, e_block_size); ete = diag.array().square().matrix().asDiagonal(); @@ -325,7 +326,7 @@ void SchurEliminator::BackSubstitute( typename EigenTypes::Matrix ete(e_block_size, e_block_size); - if (D != NULL) { + if (D != nullptr) { const typename EigenTypes::ConstVectorRef diag( D + bs->cols[e_block_id].position, e_block_size); ete = diag.array().square().matrix().asDiagonal(); @@ -547,7 +548,7 @@ void SchurEliminator:: int r, c, row_stride, col_stride; CellInfo* cell_info = lhs->GetCell(block1, block2, &r, &c, &row_stride, &col_stride); - if (cell_info != NULL) { + if (cell_info != nullptr) { const int block2_size = bs->cols[it2->first].size; std::lock_guard l(cell_info->m); // clang-format off @@ -625,7 +626,7 @@ void SchurEliminator:: int r, c, row_stride, col_stride; CellInfo* cell_info = lhs->GetCell(block1, block1, &r, &c, &row_stride, &col_stride); - if (cell_info != NULL) { + if (cell_info != nullptr) { std::lock_guard l(cell_info->m); // This multiply currently ignores the fact that this is a // symmetric outer product. @@ -645,7 +646,7 @@ void SchurEliminator:: int r, c, row_stride, col_stride; CellInfo* cell_info = lhs->GetCell(block1, block2, &r, &c, &row_stride, &col_stride); - if (cell_info != NULL) { + if (cell_info != nullptr) { const int block2_size = bs->cols[row.cells[j].block_id].size; std::lock_guard l(cell_info->m); // clang-format off @@ -680,7 +681,7 @@ void SchurEliminator:: int r, c, row_stride, col_stride; CellInfo* cell_info = lhs->GetCell(block1, block1, &r, &c, &row_stride, &col_stride); - if (cell_info != NULL) { + if (cell_info != nullptr) { std::lock_guard l(cell_info->m); // block += b1.transpose() * b1; // clang-format off @@ -700,7 +701,7 @@ void SchurEliminator:: int r, c, row_stride, col_stride; CellInfo* cell_info = lhs->GetCell(block1, block2, &r, &c, &row_stride, &col_stride); - if (cell_info != NULL) { + if (cell_info != nullptr) { // block += b1.transpose() * b2; std::lock_guard l(cell_info->m); // clang-format off diff --git a/internal/ceres/schur_eliminator_template.py b/internal/ceres/schur_eliminator_template.py index 50515956e..a6a8d56d4 100644 --- a/internal/ceres/schur_eliminator_template.py +++ b/internal/ceres/schur_eliminator_template.py @@ -130,20 +130,21 @@ FACTORY_FILE_HEADER = """ namespace ceres { namespace internal { -SchurEliminatorBase* SchurEliminatorBase::Create( +std::unique_ptr SchurEliminatorBase::Create( const LinearSolver::Options& options) { #ifndef CERES_RESTRICT_SCHUR_SPECIALIZATION """ -FACTORY = """ return new SchurEliminator<%s, %s, %s>(options);""" +FACTORY = """ return std::make_unique>(options);""" FACTORY_FOOTER = """ #endif VLOG(1) << "Template specializations not found for <" << options.row_block_size << "," << options.e_block_size << "," << options.f_block_size << ">"; - return new SchurEliminator( - options); + return std::make_unique>(options); } } // namespace internal diff --git a/internal/ceres/schur_eliminator_test.cc b/internal/ceres/schur_eliminator_test.cc index 6383ced86..f58647642 100644 --- a/internal/ceres/schur_eliminator_test.cc +++ b/internal/ceres/schur_eliminator_test.cc @@ -57,16 +57,16 @@ namespace internal { class SchurEliminatorTest : public ::testing::Test { protected: void SetUpFromId(int id) { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(id)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(id); CHECK(problem != nullptr); SetupHelper(problem.get()); } void SetupHelper(LinearLeastSquaresProblem* problem) { A.reset(down_cast(problem->A.release())); - b.reset(problem->b.release()); - D.reset(problem->D.release()); + b = std::move(problem->b); + D = std::move(problem->D); num_eliminate_blocks = problem->num_eliminate_blocks; num_eliminate_cols = 0; @@ -150,8 +150,8 @@ class SchurEliminatorTest : public ::testing::Test { &options.f_block_size); } - std::unique_ptr eliminator; - eliminator.reset(SchurEliminatorBase::Create(options)); + std::unique_ptr eliminator = + SchurEliminatorBase::Create(options); const bool kFullRankETE = true; eliminator->Init(num_eliminate_blocks, kFullRankETE, A->block_structure()); eliminator->Eliminate( diff --git a/internal/ceres/schur_jacobi_preconditioner.cc b/internal/ceres/schur_jacobi_preconditioner.cc index 283cd4b0a..ffab66ff0 100644 --- a/internal/ceres/schur_jacobi_preconditioner.cc +++ b/internal/ceres/schur_jacobi_preconditioner.cc @@ -51,14 +51,14 @@ SchurJacobiPreconditioner::SchurJacobiPreconditioner( const int num_blocks = bs.cols.size() - options_.elimination_groups[0]; CHECK_GT(num_blocks, 0) << "Jacobian should have at least 1 f_block for " << "SCHUR_JACOBI preconditioner."; - CHECK(options_.context != NULL); + CHECK(options_.context != nullptr); std::vector blocks(num_blocks); for (int i = 0; i < num_blocks; ++i) { blocks[i] = bs.cols[i + options_.elimination_groups[0]].size; } - m_.reset(new BlockRandomAccessDiagonalMatrix(blocks)); + m_ = std::make_unique(blocks); InitEliminator(bs); } @@ -74,7 +74,7 @@ void SchurJacobiPreconditioner::InitEliminator( eliminator_options.f_block_size = options_.f_block_size; eliminator_options.row_block_size = options_.row_block_size; eliminator_options.context = options_.context; - eliminator_.reset(SchurEliminatorBase::Create(eliminator_options)); + eliminator_ = SchurEliminatorBase::Create(eliminator_options); const bool kFullRankETE = true; eliminator_->Init( eliminator_options.elimination_groups[0], kFullRankETE, &bs); diff --git a/internal/ceres/schur_jacobi_preconditioner.h b/internal/ceres/schur_jacobi_preconditioner.h index 73339887e..81f584b40 100644 --- a/internal/ceres/schur_jacobi_preconditioner.h +++ b/internal/ceres/schur_jacobi_preconditioner.h @@ -69,7 +69,7 @@ class SchurEliminatorBase; // options.elimination_groups.push_back(num_cameras); // SchurJacobiPreconditioner preconditioner( // *A.block_structure(), options); -// preconditioner.Update(A, NULL); +// preconditioner.Update(A, nullptr); // preconditioner.RightMultiply(x, y); // class SchurJacobiPreconditioner : public BlockSparseMatrixPreconditioner { diff --git a/internal/ceres/scratch_evaluate_preparer.cc b/internal/ceres/scratch_evaluate_preparer.cc index d8fee30c9..a59694ee0 100644 --- a/internal/ceres/scratch_evaluate_preparer.cc +++ b/internal/ceres/scratch_evaluate_preparer.cc @@ -49,7 +49,8 @@ ScratchEvaluatePreparer* ScratchEvaluatePreparer::Create(const Program& program, } void ScratchEvaluatePreparer::Init(int max_derivatives_per_residual_block) { - jacobian_scratch_.reset(new double[max_derivatives_per_residual_block]); + jacobian_scratch_ = + std::make_unique(max_derivatives_per_residual_block); } // Point the jacobian blocks into the scratch area of this evaluate preparer. @@ -64,7 +65,7 @@ void ScratchEvaluatePreparer::Prepare(const ResidualBlock* residual_block, const ParameterBlock* parameter_block = residual_block->parameter_blocks()[j]; if (parameter_block->IsConstant()) { - jacobians[j] = NULL; + jacobians[j] = nullptr; } else { jacobians[j] = jacobian_block_cursor; jacobian_block_cursor += num_residuals * parameter_block->TangentSize(); diff --git a/internal/ceres/solver.cc b/internal/ceres/solver.cc index 491f0662d..1d2420c23 100644 --- a/internal/ceres/solver.cc +++ b/internal/ceres/solver.cc @@ -367,7 +367,7 @@ void PostSolveSummarize(const internal::PreprocessedProblem& pp, &(summary->inner_iteration_ordering_used)); // clang-format off - summary->inner_iterations_used = pp.inner_iteration_minimizer.get() != NULL; // NOLINT + summary->inner_iterations_used = pp.inner_iteration_minimizer.get() != nullptr; // NOLINT summary->linear_solver_type_used = pp.linear_solver_options.type; summary->num_threads_used = pp.options.num_threads; summary->preconditioner_type_used = pp.options.preconditioner_type; @@ -375,7 +375,7 @@ void PostSolveSummarize(const internal::PreprocessedProblem& pp, internal::SetSummaryFinalCost(summary); - if (pp.reduced_program.get() != NULL) { + if (pp.reduced_program.get() != nullptr) { SummarizeReducedProgram(*pp.reduced_program, summary); } @@ -385,7 +385,7 @@ void PostSolveSummarize(const internal::PreprocessedProblem& pp, // case if the preprocessor failed, or if the reduced problem did // not contain any parameter blocks. Thus, only extract the // evaluator statistics if one exists. - if (pp.evaluator.get() != NULL) { + if (pp.evaluator.get() != nullptr) { const map& evaluator_statistics = pp.evaluator->Statistics(); { @@ -407,7 +407,7 @@ void PostSolveSummarize(const internal::PreprocessedProblem& pp, // Again, like the evaluator, there may or may not be a linear // solver from which we can extract run time statistics. In // particular the line search solver does not use a linear solver. - if (pp.linear_solver.get() != NULL) { + if (pp.linear_solver.get() != nullptr) { const map& linear_solver_statistics = pp.linear_solver->Statistics(); const CallStatistics& call_stats = FindWithDefault( @@ -518,11 +518,11 @@ void Solver::Solve(const Solver::Options& options, Solver::Options modified_options = options; if (options.check_gradients) { modified_options.callbacks.push_back(&gradient_checking_callback); - gradient_checking_problem.reset(CreateGradientCheckingProblemImpl( + gradient_checking_problem = CreateGradientCheckingProblemImpl( problem_impl, options.gradient_check_numeric_derivative_relative_step_size, options.gradient_check_relative_precision, - &gradient_checking_callback)); + &gradient_checking_callback); problem_impl = gradient_checking_problem.get(); program = problem_impl->mutable_program(); } diff --git a/internal/ceres/sparse_normal_cholesky_solver.cc b/internal/ceres/sparse_normal_cholesky_solver.cc index 1ffce8f1e..2e52ae6d9 100644 --- a/internal/ceres/sparse_normal_cholesky_solver.cc +++ b/internal/ceres/sparse_normal_cholesky_solver.cc @@ -75,21 +75,21 @@ LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl( A->LeftMultiply(b, rhs_.data()); event_logger.AddEvent("Compute RHS"); - if (per_solve_options.D != NULL) { + if (per_solve_options.D != nullptr) { // Temporarily append a diagonal block to the A matrix, but undo // it before returning the matrix to the user. - std::unique_ptr regularizer; - regularizer.reset(BlockSparseMatrix::CreateDiagonalMatrix( - per_solve_options.D, A->block_structure()->cols)); + std::unique_ptr regularizer = + BlockSparseMatrix::CreateDiagonalMatrix(per_solve_options.D, + A->block_structure()->cols); event_logger.AddEvent("Diagonal"); A->AppendRows(*regularizer); event_logger.AddEvent("Append"); } event_logger.AddEvent("Append Rows"); - if (inner_product_computer_.get() == NULL) { - inner_product_computer_.reset( - InnerProductComputer::Create(*A, sparse_cholesky_->StorageType())); + if (inner_product_computer_.get() == nullptr) { + inner_product_computer_ = + InnerProductComputer::Create(*A, sparse_cholesky_->StorageType()); event_logger.AddEvent("InnerProductComputer::Create"); } @@ -97,7 +97,7 @@ LinearSolver::Summary SparseNormalCholeskySolver::SolveImpl( inner_product_computer_->Compute(); event_logger.AddEvent("InnerProductComputer::Compute"); - if (per_solve_options.D != NULL) { + if (per_solve_options.D != nullptr) { A->DeleteRowBlocks(A->block_structure()->cols.size()); } diff --git a/internal/ceres/sparse_normal_cholesky_solver_test.cc b/internal/ceres/sparse_normal_cholesky_solver_test.cc index 8acb98e2c..2b00dcd0d 100644 --- a/internal/ceres/sparse_normal_cholesky_solver_test.cc +++ b/internal/ceres/sparse_normal_cholesky_solver_test.cc @@ -54,20 +54,20 @@ namespace internal { class SparseNormalCholeskySolverTest : public ::testing::Test { protected: void SetUp() final { - std::unique_ptr problem( - CreateLinearLeastSquaresProblemFromId(2)); + std::unique_ptr problem = + CreateLinearLeastSquaresProblemFromId(2); CHECK(problem != nullptr); A_.reset(down_cast(problem->A.release())); - b_.reset(problem->b.release()); - D_.reset(problem->D.release()); + b_ = std::move(problem->b); + D_ = std::move(problem->D); } void TestSolver(const LinearSolver::Options& options, double* D) { Matrix dense_A; A_->ToDenseMatrix(&dense_A); Matrix lhs = dense_A.transpose() * dense_A; - if (D != NULL) { + if (D != nullptr) { lhs += (ConstVectorRef(D, A_->num_cols()).array() * ConstVectorRef(D, A_->num_cols()).array()) .matrix() @@ -97,7 +97,7 @@ class SparseNormalCholeskySolverTest : public ::testing::Test { } void TestSolver(const LinearSolver::Options& options) { - TestSolver(options, NULL); + TestSolver(options, nullptr); TestSolver(options, D_.get()); } diff --git a/internal/ceres/stl_util.h b/internal/ceres/stl_util.h index d3411b733..2af2518f8 100644 --- a/internal/ceres/stl_util.h +++ b/internal/ceres/stl_util.h @@ -73,7 +73,7 @@ void STLDeleteUniqueContainerPointers(ForwardIterator begin, // hash_set, or any other STL container which defines sensible begin(), end(), // and clear() methods. // -// If container is NULL, this function is a no-op. +// If container is nullptr, this function is a no-op. // // As an alternative to calling STLDeleteElements() directly, consider // ElementDeleter (defined below), which ensures that your container's elements diff --git a/internal/ceres/stringprintf.cc b/internal/ceres/stringprintf.cc index b0e2acce8..def40fe9f 100644 --- a/internal/ceres/stringprintf.cc +++ b/internal/ceres/stringprintf.cc @@ -66,7 +66,7 @@ void StringAppendV(string* dst, const char* format, va_list ap) { // Error or MSVC running out of space. MSVC 8.0 and higher // can be asked about space needed with the special idiom below: va_copy(backup_ap, ap); - result = vsnprintf(NULL, 0, format, backup_ap); + result = vsnprintf(nullptr, 0, format, backup_ap); va_end(backup_ap); #endif diff --git a/internal/ceres/subset_preconditioner.cc b/internal/ceres/subset_preconditioner.cc index e5146ce3c..b6b0c2e12 100644 --- a/internal/ceres/subset_preconditioner.cc +++ b/internal/ceres/subset_preconditioner.cc @@ -73,7 +73,7 @@ bool SubsetPreconditioner::UpdateImpl(const BlockSparseMatrix& A, // [Q] // Now add D to A if needed. - if (D != NULL) { + if (D != nullptr) { // A = [P] // [Q] // [D] @@ -82,19 +82,19 @@ bool SubsetPreconditioner::UpdateImpl(const BlockSparseMatrix& A, m->AppendRows(*regularizer); } - if (inner_product_computer_.get() == NULL) { - inner_product_computer_.reset(InnerProductComputer::Create( + if (inner_product_computer_ == nullptr) { + inner_product_computer_ = InnerProductComputer::Create( *m, options_.subset_preconditioner_start_row_block, bs->rows.size(), - sparse_cholesky_->StorageType())); + sparse_cholesky_->StorageType()); } // Compute inner_product = [Q'*Q + D'*D] inner_product_computer_->Compute(); // Unappend D if needed. - if (D != NULL) { + if (D != nullptr) { // A = [P] // [Q] m->DeleteRowBlocks(bs->cols.size()); diff --git a/internal/ceres/subset_preconditioner_test.cc b/internal/ceres/subset_preconditioner_test.cc index 202110bc2..7d606f121 100644 --- a/internal/ceres/subset_preconditioner_test.cc +++ b/internal/ceres/subset_preconditioner_test.cc @@ -99,28 +99,28 @@ class SubsetPreconditionerTest : public ::testing::TestWithParam { options.max_row_block_size = 4; options.block_density = 0.9; - m_.reset(BlockSparseMatrix::CreateRandomMatrix(options)); + m_ = BlockSparseMatrix::CreateRandomMatrix(options); start_row_block_ = m_->block_structure()->rows.size(); // Ensure that the bottom part of the matrix has the same column // block structure. options.col_blocks = m_->block_structure()->cols; - b_.reset(BlockSparseMatrix::CreateRandomMatrix(options)); + b_ = BlockSparseMatrix::CreateRandomMatrix(options); m_->AppendRows(*b_); // Create a Identity block diagonal matrix with the same column // block structure. diagonal_ = Vector::Ones(m_->num_cols()); - block_diagonal_.reset(BlockSparseMatrix::CreateDiagonalMatrix( - diagonal_.data(), b_->block_structure()->cols)); + block_diagonal_ = BlockSparseMatrix::CreateDiagonalMatrix( + diagonal_.data(), b_->block_structure()->cols); // Unconditionally add the block diagonal to the matrix b_, // because either it is either part of b_ to make it full rank, or // we pass the same diagonal matrix later as the parameter D. In // either case the preconditioner matrix is b_' b + D'D. b_->AppendRows(*block_diagonal_); - inner_product_computer_.reset(InnerProductComputer::Create( - *b_, CompressedRowSparseMatrix::UPPER_TRIANGULAR)); + inner_product_computer_ = InnerProductComputer::Create( + *b_, CompressedRowSparseMatrix::UPPER_TRIANGULAR); inner_product_computer_->Compute(); } @@ -138,7 +138,7 @@ TEST_P(SubsetPreconditionerTest, foo) { Preconditioner::Options options; options.subset_preconditioner_start_row_block = start_row_block_; options.sparse_linear_algebra_library_type = ::testing::get<0>(param); - preconditioner_.reset(new SubsetPreconditioner(options, *m_)); + preconditioner_ = std::make_unique(options, *m_); const bool with_diagonal = ::testing::get<1>(param); if (!with_diagonal) { @@ -146,7 +146,7 @@ TEST_P(SubsetPreconditionerTest, foo) { } EXPECT_TRUE( - preconditioner_->Update(*m_, with_diagonal ? diagonal_.data() : NULL)); + preconditioner_->Update(*m_, with_diagonal ? diagonal_.data() : nullptr)); // Repeatedly apply the preconditioner to random vectors and check // that the preconditioned value is the same as one obtained by diff --git a/internal/ceres/suitesparse.cc b/internal/ceres/suitesparse.cc index 0d6f6bdfb..ce8ac6593 100644 --- a/internal/ceres/suitesparse.cc +++ b/internal/ceres/suitesparse.cc @@ -368,7 +368,7 @@ SuiteSparseCholesky::~SuiteSparseCholesky() { LinearSolverTerminationType SuiteSparseCholesky::Factorize( CompressedRowSparseMatrix* lhs, string* message) { if (lhs == nullptr) { - *message = "Failure: Input lhs is NULL."; + *message = "Failure: Input lhs is nullptr."; return LINEAR_SOLVER_FATAL_ERROR; } diff --git a/internal/ceres/suitesparse.h b/internal/ceres/suitesparse.h index a98c94686..8802774d8 100644 --- a/internal/ceres/suitesparse.h +++ b/internal/ceres/suitesparse.h @@ -106,7 +106,7 @@ class SuiteSparse { cholmod_dense CreateDenseVectorView(const double* x, int size); // Given a vector x, build a cholmod_dense vector of size out_size - // with the first in_size entries copied from x. If x is NULL, then + // with the first in_size entries copied from x. If x is nullptr, then // an all zeros vector is returned. Caller owns the result. cholmod_dense* CreateDenseVector(const double* x, int in_size, int out_size); @@ -123,7 +123,7 @@ class SuiteSparse { // Create and return a matrix m = A * A'. Caller owns the // result. The matrix A is not modified. cholmod_sparse* AATranspose(cholmod_sparse* A) { - cholmod_sparse* m = cholmod_aat(A, NULL, A->nrow, 1, &cc_); + cholmod_sparse* m = cholmod_aat(A, nullptr, A->nrow, 1, &cc_); m->stype = 1; // Pay attention to the upper triangular part. return m; } @@ -196,7 +196,7 @@ class SuiteSparse { // Given a Cholesky factorization of a matrix A = LL^T, solve the // linear system Ax = b, and return the result. If the Solve fails - // NULL is returned. Caller owns the result. + // nullptr is returned. Caller owns the result. // // message contains an explanation of the failures if any. cholmod_dense* Solve(cholmod_factor* L, diff --git a/internal/ceres/system_test.cc b/internal/ceres/system_test.cc index 11b050433..3f4450adb 100644 --- a/internal/ceres/system_test.cc +++ b/internal/ceres/system_test.cc @@ -70,13 +70,13 @@ class PowellsFunction { x_[3] = 1.0; problem_.AddResidualBlock( - new AutoDiffCostFunction(new F1), NULL, &x_[0], &x_[1]); + new AutoDiffCostFunction(new F1), nullptr, &x_[0], &x_[1]); problem_.AddResidualBlock( - new AutoDiffCostFunction(new F2), NULL, &x_[2], &x_[3]); + new AutoDiffCostFunction(new F2), nullptr, &x_[2], &x_[3]); problem_.AddResidualBlock( - new AutoDiffCostFunction(new F3), NULL, &x_[1], &x_[2]); + new AutoDiffCostFunction(new F3), nullptr, &x_[1], &x_[2]); problem_.AddResidualBlock( - new AutoDiffCostFunction(new F4), NULL, &x_[0], &x_[3]); + new AutoDiffCostFunction(new F4), nullptr, &x_[0], &x_[3]); // Settings for the reference solution. options_.linear_solver_type = ceres::DENSE_QR; diff --git a/internal/ceres/tiny_solver_cost_function_adapter_test.cc b/internal/ceres/tiny_solver_cost_function_adapter_test.cc index 6f5719326..ff8c070ef 100644 --- a/internal/ceres/tiny_solver_cost_function_adapter_test.cc +++ b/internal/ceres/tiny_solver_cost_function_adapter_test.cc @@ -85,8 +85,8 @@ void TestHelper() { double* parameters[1] = {xyz}; // Check that residual only evaluation works. - cost_function->Evaluate(parameters, expected_residuals.data(), NULL); - cfa(xyz, actual_residuals.data(), NULL); + cost_function->Evaluate(parameters, expected_residuals.data(), nullptr); + cfa(xyz, actual_residuals.data(), nullptr); EXPECT_NEAR( (expected_residuals - actual_residuals).norm() / actual_residuals.norm(), 0.0, diff --git a/internal/ceres/tiny_solver_test.cc b/internal/ceres/tiny_solver_test.cc index 2e70694ee..45c021f61 100644 --- a/internal/ceres/tiny_solver_test.cc +++ b/internal/ceres/tiny_solver_test.cc @@ -115,7 +115,7 @@ template void TestHelper(const Function& f, const Vector& x0) { Vector x = x0; Vec2 residuals; - f(x.data(), residuals.data(), NULL); + f(x.data(), residuals.data(), nullptr); EXPECT_GT(residuals.squaredNorm() / 2.0, 1e-10); TinySolver solver; diff --git a/internal/ceres/triplet_sparse_matrix.cc b/internal/ceres/triplet_sparse_matrix.cc index e4f7f9bd7..c409a59cf 100644 --- a/internal/ceres/triplet_sparse_matrix.cc +++ b/internal/ceres/triplet_sparse_matrix.cc @@ -123,9 +123,12 @@ void TripletSparseMatrix::Reserve(int new_max_num_nonzeros) { // Nothing to do if we have enough space already. if (new_max_num_nonzeros <= max_num_nonzeros_) return; - int* new_rows = new int[new_max_num_nonzeros]; - int* new_cols = new int[new_max_num_nonzeros]; - double* new_values = new double[new_max_num_nonzeros]; + std::unique_ptr new_rows = + std::make_unique(new_max_num_nonzeros); + std::unique_ptr new_cols = + std::make_unique(new_max_num_nonzeros); + std::unique_ptr new_values = + std::make_unique(new_max_num_nonzeros); for (int i = 0; i < num_nonzeros_; ++i) { new_rows[i] = rows_[i]; @@ -133,10 +136,9 @@ void TripletSparseMatrix::Reserve(int new_max_num_nonzeros) { new_values[i] = values_[i]; } - rows_.reset(new_rows); - cols_.reset(new_cols); - values_.reset(new_values); - + rows_ = std::move(new_rows); + cols_ = std::move(new_cols); + values_ = std::move(new_values); max_num_nonzeros_ = new_max_num_nonzeros; } @@ -152,9 +154,9 @@ void TripletSparseMatrix::set_num_nonzeros(int num_nonzeros) { } void TripletSparseMatrix::AllocateMemory() { - rows_.reset(new int[max_num_nonzeros_]); - cols_.reset(new int[max_num_nonzeros_]); - values_.reset(new double[max_num_nonzeros_]); + rows_ = std::make_unique(max_num_nonzeros_); + cols_ = std::make_unique(max_num_nonzeros_); + values_ = std::make_unique(max_num_nonzeros_); } void TripletSparseMatrix::CopyData(const TripletSparseMatrix& orig) { @@ -252,10 +254,11 @@ void TripletSparseMatrix::Resize(int new_num_rows, int new_num_cols) { num_nonzeros_ -= dropped_terms; } -TripletSparseMatrix* TripletSparseMatrix::CreateSparseDiagonalMatrix( - const double* values, int num_rows) { - TripletSparseMatrix* m = - new TripletSparseMatrix(num_rows, num_rows, num_rows); +std::unique_ptr +TripletSparseMatrix::CreateSparseDiagonalMatrix(const double* values, + int num_rows) { + std::unique_ptr m = + std::make_unique(num_rows, num_rows, num_rows); for (int i = 0; i < num_rows; ++i) { m->mutable_rows()[i] = i; m->mutable_cols()[i] = i; @@ -272,7 +275,7 @@ void TripletSparseMatrix::ToTextFile(FILE* file) const { } } -TripletSparseMatrix* TripletSparseMatrix::CreateRandomMatrix( +std::unique_ptr TripletSparseMatrix::CreateRandomMatrix( const TripletSparseMatrix::RandomMatrixOptions& options) { CHECK_GT(options.num_rows, 0); CHECK_GT(options.num_cols, 0); @@ -297,7 +300,7 @@ TripletSparseMatrix* TripletSparseMatrix::CreateRandomMatrix( } } - return new TripletSparseMatrix( + return std::make_unique( options.num_rows, options.num_cols, rows, cols, values); } diff --git a/internal/ceres/triplet_sparse_matrix.h b/internal/ceres/triplet_sparse_matrix.h index daa464353..ba426c433 100644 --- a/internal/ceres/triplet_sparse_matrix.h +++ b/internal/ceres/triplet_sparse_matrix.h @@ -115,8 +115,8 @@ class CERES_EXPORT_INTERNAL TripletSparseMatrix : public SparseMatrix { // Build a sparse diagonal matrix of size num_rows x num_rows from // the array values. Entries of the values array are copied into the // sparse matrix. - static TripletSparseMatrix* CreateSparseDiagonalMatrix(const double* values, - int num_rows); + static std::unique_ptr CreateSparseDiagonalMatrix( + const double* values, int num_rows); // Options struct to control the generation of random // TripletSparseMatrix objects. @@ -132,9 +132,7 @@ class CERES_EXPORT_INTERNAL TripletSparseMatrix : public SparseMatrix { // Create a random CompressedRowSparseMatrix whose entries are // normally distributed and whose structure is determined by // RandomMatrixOptions. - // - // Caller owns the result. - static TripletSparseMatrix* CreateRandomMatrix( + static std::unique_ptr CreateRandomMatrix( const TripletSparseMatrix::RandomMatrixOptions& options); private: diff --git a/internal/ceres/trust_region_minimizer.cc b/internal/ceres/trust_region_minimizer.cc index 5e292bb61..f8e6b2978 100644 --- a/internal/ceres/trust_region_minimizer.cc +++ b/internal/ceres/trust_region_minimizer.cc @@ -75,11 +75,11 @@ void TrustRegionMinimizer::Minimize(const Minimizer::Options& options, // Create the TrustRegionStepEvaluator. The construction needs to be // delayed to this point because we need the cost for the starting // point to initialize the step evaluator. - step_evaluator_.reset(new TrustRegionStepEvaluator( + step_evaluator_ = std::make_unique( x_cost_, options_.use_nonmonotonic_steps ? options_.max_consecutive_nonmonotonic_steps - : 0)); + : 0); while (FinalizeIterationAndCheckIfMinimizerCanContinue()) { iteration_start_time_in_secs_ = WallTimeInSeconds(); diff --git a/internal/ceres/trust_region_minimizer_test.cc b/internal/ceres/trust_region_minimizer_test.cc index 7f2c931da..d42aab02a 100644 --- a/internal/ceres/trust_region_minimizer_test.cc +++ b/internal/ceres/trust_region_minimizer_test.cc @@ -79,10 +79,10 @@ class PowellEvaluator2 : public Evaluator { ~PowellEvaluator2() override = default; // Implementation of Evaluator interface. - SparseMatrix* CreateJacobian() const final { + std::unique_ptr CreateJacobian() const final { CHECK(col1 || col2 || col3 || col4); - DenseSparseMatrix* dense_jacobian = - new DenseSparseMatrix(NumResiduals(), NumEffectiveParameters()); + auto dense_jacobian = std::make_unique( + NumResiduals(), NumEffectiveParameters()); dense_jacobian->SetZero(); return dense_jacobian; } @@ -119,14 +119,14 @@ class PowellEvaluator2 : public Evaluator { VLOG(1) << "Cost: " << *cost; - if (residuals != NULL) { + if (residuals != nullptr) { residuals[0] = f1; residuals[1] = f2; residuals[2] = f3; residuals[3] = f4; } - if (jacobian != NULL) { + if (jacobian != nullptr) { DenseSparseMatrix* dense_jacobian; dense_jacobian = down_cast(jacobian); dense_jacobian->SetZero(); @@ -176,7 +176,7 @@ class PowellEvaluator2 : public Evaluator { VLOG(1) << "\n" << jacobian_matrix; } - if (gradient != NULL) { + if (gradient != nullptr) { int column_index = 0; if (col1) { gradient[column_index++] = f1 + f4 * sqrt(10.0) * 2.0 * (x1 - x4); @@ -240,10 +240,9 @@ void IsTrustRegionSolveSuccessful(TrustRegionStrategyType strategy_type) { minimizer_options.gradient_tolerance = 1e-26; minimizer_options.function_tolerance = 1e-26; minimizer_options.parameter_tolerance = 1e-26; - minimizer_options.evaluator.reset( - new PowellEvaluator2); - minimizer_options.jacobian.reset( - minimizer_options.evaluator->CreateJacobian()); + minimizer_options.evaluator = + std::make_unique>(); + minimizer_options.jacobian = minimizer_options.evaluator->CreateJacobian(); TrustRegionStrategy::Options trust_region_strategy_options; trust_region_strategy_options.trust_region_strategy_type = strategy_type; @@ -252,8 +251,8 @@ void IsTrustRegionSolveSuccessful(TrustRegionStrategyType strategy_type) { trust_region_strategy_options.max_radius = 1e20; trust_region_strategy_options.min_lm_diagonal = 1e-6; trust_region_strategy_options.max_lm_diagonal = 1e32; - minimizer_options.trust_region_strategy.reset( - TrustRegionStrategy::Create(trust_region_strategy_options)); + minimizer_options.trust_region_strategy = + TrustRegionStrategy::Create(trust_region_strategy_options); TrustRegionMinimizer minimizer; Solver::Summary summary; @@ -343,12 +342,12 @@ class CurveCostFunction : public CostFunction { residuals[0] -= sqrt(length); } - if (jacobians == NULL) { + if (jacobians == nullptr) { return true; } for (int i = 0; i < num_vertices_; ++i) { - if (jacobians[i] != NULL) { + if (jacobians[i] != nullptr) { int prev = (num_vertices_ + i - 1) % num_vertices_; int next = (i + 1) % num_vertices_; @@ -398,7 +397,7 @@ TEST(TrustRegionMinimizer, JacobiScalingTest) { } Problem problem; - problem.AddResidualBlock(new CurveCostFunction(N, 10.), NULL, y); + problem.AddResidualBlock(new CurveCostFunction(N, 10.), nullptr, y); Solver::Options options; options.linear_solver_type = ceres::DENSE_QR; Solver::Summary summary; @@ -425,7 +424,7 @@ struct ExpCostFunctor { TEST(TrustRegionMinimizer, GradientToleranceConvergenceUpdatesStep) { double x = 5; Problem problem; - problem.AddResidualBlock(ExpCostFunctor::Create(), NULL, &x); + problem.AddResidualBlock(ExpCostFunctor::Create(), nullptr, &x); problem.SetParameterLowerBound(&x, 0, 3.0); Solver::Options options; Solver::Summary summary; diff --git a/internal/ceres/trust_region_preprocessor.cc b/internal/ceres/trust_region_preprocessor.cc index fc7636957..a6a8b9f0f 100644 --- a/internal/ceres/trust_region_preprocessor.cc +++ b/internal/ceres/trust_region_preprocessor.cc @@ -55,9 +55,10 @@ using std::vector; namespace { -ParameterBlockOrdering* CreateDefaultLinearSolverOrdering( +std::shared_ptr CreateDefaultLinearSolverOrdering( const Program& program) { - ParameterBlockOrdering* ordering = new ParameterBlockOrdering; + std::shared_ptr ordering = + std::make_shared(); const vector& parameter_blocks = program.parameter_blocks(); for (int i = 0; i < parameter_blocks.size(); ++i) { ordering->AddElementToGroup( @@ -160,8 +161,8 @@ bool SetupLinearSolver(PreprocessedProblem* pp) { // assume that they are giving all the freedom to us in choosing // the best possible ordering. This intent can be indicated by // putting all the parameter blocks in the same elimination group. - options.linear_solver_ordering.reset( - CreateDefaultLinearSolverOrdering(*pp->reduced_program)); + options.linear_solver_ordering = + CreateDefaultLinearSolverOrdering(*pp->reduced_program); } else { // If the user supplied an ordering, then check if the first // elimination group is still non-empty after the reduced problem @@ -247,7 +248,7 @@ bool SetupLinearSolver(PreprocessedProblem* pp) { } } - pp->linear_solver.reset(LinearSolver::Create(pp->linear_solver_options)); + pp->linear_solver = LinearSolver::Create(pp->linear_solver_options); return (pp->linear_solver != nullptr); } @@ -269,8 +270,8 @@ bool SetupEvaluator(PreprocessedProblem* pp) { pp->evaluator_options.context = pp->problem->context(); pp->evaluator_options.evaluation_callback = pp->reduced_program->mutable_evaluation_callback(); - pp->evaluator.reset(Evaluator::Create( - pp->evaluator_options, pp->reduced_program.get(), &pp->error)); + pp->evaluator = Evaluator::Create( + pp->evaluator_options, pp->reduced_program.get(), &pp->error); return (pp->evaluator != nullptr); } @@ -316,12 +317,12 @@ bool SetupInnerIterationMinimizer(PreprocessedProblem* pp) { } } else { // The user did not supply an ordering, so create one. - options.inner_iteration_ordering.reset( - CoordinateDescentMinimizer::CreateOrdering(*pp->reduced_program)); + options.inner_iteration_ordering = + CoordinateDescentMinimizer::CreateOrdering(*pp->reduced_program); } - pp->inner_iteration_minimizer.reset( - new CoordinateDescentMinimizer(pp->problem->context())); + pp->inner_iteration_minimizer = + std::make_unique(pp->problem->context()); return pp->inner_iteration_minimizer->Init(*pp->reduced_program, pp->problem->parameter_map(), *options.inner_iteration_ordering, @@ -335,7 +336,7 @@ void SetupMinimizerOptions(PreprocessedProblem* pp) { SetupCommonMinimizerOptions(pp); pp->minimizer_options.is_constrained = pp->reduced_program->IsBoundsConstrained(); - pp->minimizer_options.jacobian.reset(pp->evaluator->CreateJacobian()); + pp->minimizer_options.jacobian = pp->evaluator->CreateJacobian(); pp->minimizer_options.inner_iteration_minimizer = pp->inner_iteration_minimizer; @@ -348,8 +349,8 @@ void SetupMinimizerOptions(PreprocessedProblem* pp) { strategy_options.trust_region_strategy_type = options.trust_region_strategy_type; strategy_options.dogleg_type = options.dogleg_type; - pp->minimizer_options.trust_region_strategy.reset( - TrustRegionStrategy::Create(strategy_options)); + pp->minimizer_options.trust_region_strategy = + TrustRegionStrategy::Create(strategy_options); CHECK(pp->minimizer_options.trust_region_strategy != nullptr); } @@ -370,10 +371,10 @@ bool TrustRegionPreprocessor::Preprocess(const Solver::Options& options, return false; } - pp->reduced_program.reset(program->CreateReducedProgram( - &pp->removed_parameter_blocks, &pp->fixed_cost, &pp->error)); + pp->reduced_program = program->CreateReducedProgram( + &pp->removed_parameter_blocks, &pp->fixed_cost, &pp->error); - if (pp->reduced_program.get() == NULL) { + if (pp->reduced_program.get() == nullptr) { return false; } diff --git a/internal/ceres/trust_region_preprocessor_test.cc b/internal/ceres/trust_region_preprocessor_test.cc index ee93df359..fbec58992 100644 --- a/internal/ceres/trust_region_preprocessor_test.cc +++ b/internal/ceres/trust_region_preprocessor_test.cc @@ -225,7 +225,7 @@ TEST_F(LinearSolverAndEvaluatorCreationTest, MinimizerIsAwareOfBounds) { TEST_F(LinearSolverAndEvaluatorCreationTest, SchurTypeSolverWithBadOrdering) { Solver::Options options; options.linear_solver_type = DENSE_SCHUR; - options.linear_solver_ordering.reset(new ParameterBlockOrdering); + options.linear_solver_ordering = std::make_shared(); options.linear_solver_ordering->AddElementToGroup(&x_, 0); options.linear_solver_ordering->AddElementToGroup(&y_, 0); options.linear_solver_ordering->AddElementToGroup(&z_, 1); @@ -238,7 +238,7 @@ TEST_F(LinearSolverAndEvaluatorCreationTest, SchurTypeSolverWithBadOrdering) { TEST_F(LinearSolverAndEvaluatorCreationTest, SchurTypeSolverWithGoodOrdering) { Solver::Options options; options.linear_solver_type = DENSE_SCHUR; - options.linear_solver_ordering.reset(new ParameterBlockOrdering); + options.linear_solver_ordering = std::make_shared(); options.linear_solver_ordering->AddElementToGroup(&x_, 0); options.linear_solver_ordering->AddElementToGroup(&z_, 0); options.linear_solver_ordering->AddElementToGroup(&y_, 1); @@ -260,7 +260,7 @@ TEST_F(LinearSolverAndEvaluatorCreationTest, Solver::Options options; options.linear_solver_type = DENSE_SCHUR; - options.linear_solver_ordering.reset(new ParameterBlockOrdering); + options.linear_solver_ordering = std::make_shared(); options.linear_solver_ordering->AddElementToGroup(&x_, 0); options.linear_solver_ordering->AddElementToGroup(&z_, 0); options.linear_solver_ordering->AddElementToGroup(&y_, 1); @@ -281,7 +281,7 @@ TEST_F(LinearSolverAndEvaluatorCreationTest, Solver::Options options; options.linear_solver_type = DENSE_SCHUR; - options.linear_solver_ordering.reset(new ParameterBlockOrdering); + options.linear_solver_ordering = std::make_shared(); options.linear_solver_ordering->AddElementToGroup(&x_, 0); options.linear_solver_ordering->AddElementToGroup(&z_, 0); options.linear_solver_ordering->AddElementToGroup(&y_, 1); @@ -328,7 +328,7 @@ TEST_F(LinearSolverAndEvaluatorCreationTest, TEST_F(LinearSolverAndEvaluatorCreationTest, InvalidInnerIterationsOrdering) { Solver::Options options; options.use_inner_iterations = true; - options.inner_iteration_ordering.reset(new ParameterBlockOrdering); + options.inner_iteration_ordering = std::make_shared(); options.inner_iteration_ordering->AddElementToGroup(&x_, 0); options.inner_iteration_ordering->AddElementToGroup(&z_, 0); options.inner_iteration_ordering->AddElementToGroup(&y_, 0); @@ -341,7 +341,7 @@ TEST_F(LinearSolverAndEvaluatorCreationTest, InvalidInnerIterationsOrdering) { TEST_F(LinearSolverAndEvaluatorCreationTest, ValidInnerIterationsOrdering) { Solver::Options options; options.use_inner_iterations = true; - options.inner_iteration_ordering.reset(new ParameterBlockOrdering); + options.inner_iteration_ordering = std::make_shared(); options.inner_iteration_ordering->AddElementToGroup(&x_, 0); options.inner_iteration_ordering->AddElementToGroup(&z_, 0); options.inner_iteration_ordering->AddElementToGroup(&y_, 1); diff --git a/internal/ceres/trust_region_strategy.cc b/internal/ceres/trust_region_strategy.cc index 235bf9861..c6961a14c 100644 --- a/internal/ceres/trust_region_strategy.cc +++ b/internal/ceres/trust_region_strategy.cc @@ -40,12 +40,13 @@ namespace internal { TrustRegionStrategy::~TrustRegionStrategy() = default; -TrustRegionStrategy* TrustRegionStrategy::Create(const Options& options) { +std::unique_ptr TrustRegionStrategy::Create( + const Options& options) { switch (options.trust_region_strategy_type) { case LEVENBERG_MARQUARDT: - return new LevenbergMarquardtStrategy(options); + return std::make_unique(options); case DOGLEG: - return new DoglegStrategy(options); + return std::make_unique(options); default: LOG(FATAL) << "Unknown trust region strategy: " << options.trust_region_strategy_type; @@ -53,7 +54,7 @@ TrustRegionStrategy* TrustRegionStrategy::Create(const Options& options) { LOG(FATAL) << "Unknown trust region strategy: " << options.trust_region_strategy_type; - return NULL; + return nullptr; } } // namespace internal diff --git a/internal/ceres/trust_region_strategy.h b/internal/ceres/trust_region_strategy.h index 176f73a48..d520917b4 100644 --- a/internal/ceres/trust_region_strategy.h +++ b/internal/ceres/trust_region_strategy.h @@ -75,7 +75,7 @@ class CERES_EXPORT_INTERNAL TrustRegionStrategy { }; // Factory. - static TrustRegionStrategy* Create(const Options& options); + static std::unique_ptr Create(const Options& options); virtual ~TrustRegionStrategy(); diff --git a/internal/ceres/visibility.cc b/internal/ceres/visibility.cc index 82bf6f170..06f31ec27 100644 --- a/internal/ceres/visibility.cc +++ b/internal/ceres/visibility.cc @@ -81,7 +81,7 @@ void ComputeVisibility(const CompressedRowBlockStructure& block_structure, WeightedGraph* CreateSchurComplementGraph( const vector>& visibility) { - const time_t start_time = time(NULL); + const time_t start_time = time(nullptr); // Compute the number of e_blocks/point blocks. Since the visibility // set for each e_block/camera contains the set of e_blocks/points // visible to it, we find the maximum across all visibility sets. @@ -146,7 +146,7 @@ WeightedGraph* CreateSchurComplementGraph( graph->AddEdge(camera1, camera2, weight); } - VLOG(2) << "Schur complement graph time: " << (time(NULL) - start_time); + VLOG(2) << "Schur complement graph time: " << (time(nullptr) - start_time); return graph; } diff --git a/internal/ceres/visibility_based_preconditioner.cc b/internal/ceres/visibility_based_preconditioner.cc index 566dcfa50..3e3604f74 100644 --- a/internal/ceres/visibility_based_preconditioner.cc +++ b/internal/ceres/visibility_based_preconditioner.cc @@ -80,7 +80,7 @@ VisibilityBasedPreconditioner::VisibilityBasedPreconditioner( num_blocks_ = bs.cols.size() - options_.elimination_groups[0]; CHECK_GT(num_blocks_, 0) << "Jacobian should have at least 1 f_block for " << "visibility based preconditioning."; - CHECK(options_.context != NULL); + CHECK(options_.context != nullptr); // Vector of camera block sizes block_size_.resize(num_blocks_); @@ -88,7 +88,7 @@ VisibilityBasedPreconditioner::VisibilityBasedPreconditioner( block_size_[i] = bs.cols[i + options_.elimination_groups[0]].size; } - const time_t start_time = time(NULL); + const time_t start_time = time(nullptr); switch (options_.type) { case CLUSTER_JACOBI: ComputeClusterJacobiSparsity(bs); @@ -99,11 +99,11 @@ VisibilityBasedPreconditioner::VisibilityBasedPreconditioner( default: LOG(FATAL) << "Unknown preconditioner type"; } - const time_t structure_time = time(NULL); + const time_t structure_time = time(nullptr); InitStorage(bs); - const time_t storage_time = time(NULL); + const time_t storage_time = time(nullptr); InitEliminator(bs); - const time_t eliminator_time = time(NULL); + const time_t eliminator_time = time(nullptr); LinearSolver::Options sparse_cholesky_options; sparse_cholesky_options.sparse_linear_algebra_library_type = @@ -118,7 +118,7 @@ VisibilityBasedPreconditioner::VisibilityBasedPreconditioner( sparse_cholesky_options.use_postordering = true; sparse_cholesky_ = SparseCholesky::Create(sparse_cholesky_options); - const time_t init_time = time(NULL); + const time_t init_time = time(nullptr); VLOG(2) << "init time: " << init_time - start_time << " structure time: " << structure_time - start_time << " storage time:" << storage_time - structure_time @@ -175,7 +175,8 @@ void VisibilityBasedPreconditioner::ComputeClusterTridiagonalSparsity( void VisibilityBasedPreconditioner::InitStorage( const CompressedRowBlockStructure& bs) { ComputeBlockPairsInPreconditioner(bs); - m_.reset(new BlockRandomAccessSparseMatrix(block_size_, block_pairs_)); + m_ = std::make_unique(block_size_, + block_pairs_); } // Call the canonical views algorithm and cluster the cameras based on @@ -328,7 +329,7 @@ void VisibilityBasedPreconditioner::InitEliminator( eliminator_options.f_block_size = options_.f_block_size; eliminator_options.row_block_size = options_.row_block_size; eliminator_options.context = options_.context; - eliminator_.reset(SchurEliminatorBase::Create(eliminator_options)); + eliminator_ = SchurEliminatorBase::Create(eliminator_options); const bool kFullRankETE = true; eliminator_->Init( eliminator_options.elimination_groups[0], kFullRankETE, &bs); @@ -337,7 +338,7 @@ void VisibilityBasedPreconditioner::InitEliminator( // Update the values of the preconditioner matrix and factorize it. bool VisibilityBasedPreconditioner::UpdateImpl(const BlockSparseMatrix& A, const double* D) { - const time_t start_time = time(NULL); + const time_t start_time = time(nullptr); const int num_rows = m_->num_rows(); CHECK_GT(num_rows, 0); @@ -375,7 +376,7 @@ bool VisibilityBasedPreconditioner::UpdateImpl(const BlockSparseMatrix& A, status = Factorize(); } - VLOG(2) << "Compute time: " << time(NULL) - start_time; + VLOG(2) << "Compute time: " << time(nullptr) - start_time; return (status == LINEAR_SOLVER_SUCCESS); } @@ -395,7 +396,7 @@ void VisibilityBasedPreconditioner::ScaleOffDiagonalCells() { int r, c, row_stride, col_stride; CellInfo* cell_info = m_->GetCell(block1, block2, &r, &c, &row_stride, &col_stride); - CHECK(cell_info != NULL) + CHECK(cell_info != nullptr) << "Cell missing for block pair (" << block1 << "," << block2 << ")" << " cluster pair (" << cluster_membership_[block1] << " " << cluster_membership_[block2] << ")"; @@ -420,11 +421,10 @@ LinearSolverTerminationType VisibilityBasedPreconditioner::Factorize() { const CompressedRowSparseMatrix::StorageType storage_type = sparse_cholesky_->StorageType(); if (storage_type == CompressedRowSparseMatrix::UPPER_TRIANGULAR) { - lhs.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm)); + lhs = CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm); lhs->set_storage_type(CompressedRowSparseMatrix::UPPER_TRIANGULAR); } else { - lhs.reset( - CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm)); + lhs = CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(*tsm); lhs->set_storage_type(CompressedRowSparseMatrix::LOWER_TRIANGULAR); } diff --git a/internal/ceres/visibility_based_preconditioner.h b/internal/ceres/visibility_based_preconditioner.h index 7146e3fe2..0da42a23f 100644 --- a/internal/ceres/visibility_based_preconditioner.h +++ b/internal/ceres/visibility_based_preconditioner.h @@ -122,7 +122,7 @@ class SchurEliminatorBase; // options.elimination_groups.push_back(num_cameras); // VisibilityBasedPreconditioner preconditioner( // *A.block_structure(), options); -// preconditioner.Update(A, NULL); +// preconditioner.Update(A, nullptr); // preconditioner.RightMultiply(x, y); class VisibilityBasedPreconditioner : public BlockSparseMatrixPreconditioner { public: diff --git a/internal/ceres/visibility_based_preconditioner_test.cc b/internal/ceres/visibility_based_preconditioner_test.cc index 10aa619c9..8fcae507d 100644 --- a/internal/ceres/visibility_based_preconditioner_test.cc +++ b/internal/ceres/visibility_based_preconditioner_test.cc @@ -67,8 +67,8 @@ namespace internal { // void SetUp() { // string input_file = TestFileAbsolutePath("problem-6-1384-000.lsqp"); -// std::unique_ptr problem( -// CHECK_NOTNULL(CreateLinearLeastSquaresProblemFromFile(input_file))); +// std::unique_ptr problem = +// CreateLinearLeastSquaresProblemFromFile(input_file)); // A_.reset(down_cast(problem->A.release())); // b_.reset(problem->b.release()); // D_.reset(problem->D.release()); @@ -96,7 +96,8 @@ namespace internal { // // conditioned. // VectorRef(D_.get(), num_cols_).setConstant(10.0); -// schur_complement_.reset(new BlockRandomAccessDenseMatrix(blocks)); +// schur_complement_ = +// std::make_unique(blocks); // Vector rhs(schur_complement_->num_rows()); // std::unique_ptr eliminator; @@ -104,7 +105,7 @@ namespace internal { // eliminator_options.elimination_groups = options_.elimination_groups; // eliminator_options.num_threads = options_.num_threads; -// eliminator.reset(SchurEliminatorBase::Create(eliminator_options)); +// eliminator = SchurEliminatorBase::Create(eliminator_options); // eliminator->Init(num_eliminate_blocks_, bs); // eliminator->Eliminate(A_.get(), b_.get(), D_.get(), // schur_complement_.get(), rhs.data()); @@ -242,8 +243,9 @@ namespace internal { // TEST_F(VisibilityBasedPreconditionerTest, OneClusterClusterJacobi) { // options_.type = CLUSTER_JACOBI; -// preconditioner_.reset( -// new VisibilityBasedPreconditioner(*A_->block_structure(), options_)); +// preconditioner_ = +// std::make_unique( +// *A_->block_structure(), options_); // // Override the clustering to be a single clustering containing all // // the cameras. @@ -287,8 +289,9 @@ namespace internal { // TEST_F(VisibilityBasedPreconditionerTest, ClusterJacobi) { // options_.type = CLUSTER_JACOBI; -// preconditioner_.reset( -// new VisibilityBasedPreconditioner(*A_->block_structure(), options_)); +// preconditioner_ = +// std::make_unique(*A_->block_structure(), +// options_); // // Override the clustering to be equal number of cameras. // vector& cluster_membership = *get_mutable_cluster_membership(); @@ -312,8 +315,9 @@ namespace internal { // TEST_F(VisibilityBasedPreconditionerTest, ClusterTridiagonal) { // options_.type = CLUSTER_TRIDIAGONAL; -// preconditioner_.reset( -// new VisibilityBasedPreconditioner(*A_->block_structure(), options_)); +// preconditioner_ = +// std::make_unique(*A_->block_structure(), +// options_); // static const int kNumClusters = 3; // // Override the clustering to be 3 clusters. diff --git a/internal/ceres/wall_time.cc b/internal/ceres/wall_time.cc index 716392741..bd920acde 100644 --- a/internal/ceres/wall_time.cc +++ b/internal/ceres/wall_time.cc @@ -58,7 +58,7 @@ double WallTimeInSeconds() { static_cast(frequency.QuadPart); #else timeval time_val; - gettimeofday(&time_val, NULL); + gettimeofday(&time_val, nullptr); return (time_val.tv_sec + time_val.tv_usec * 1e-6); #endif #endif