diff --git a/internal/ceres/block_jacobian_writer.cc b/internal/ceres/block_jacobian_writer.cc index a5020e650..b8cb3c7a5 100644 --- a/internal/ceres/block_jacobian_writer.cc +++ b/internal/ceres/block_jacobian_writer.cc @@ -138,12 +138,12 @@ BlockJacobianWriter::BlockJacobianWriter(const Evaluator::Options& options, // Create evaluate prepareres that point directly into the final jacobian. This // makes the final Write() a nop. -BlockEvaluatePreparer* BlockJacobianWriter::CreateEvaluatePreparers( - int num_threads) { +std::unique_ptr +BlockJacobianWriter::CreateEvaluatePreparers(int num_threads) { int max_derivatives_per_residual_block = program_->MaxDerivativesPerResidualBlock(); - BlockEvaluatePreparer* preparers = new BlockEvaluatePreparer[num_threads]; + auto preparers = std::make_unique(num_threads); for (int i = 0; i < num_threads; i++) { preparers[i].Init(&jacobian_layout_[0], max_derivatives_per_residual_block); } diff --git a/internal/ceres/block_jacobian_writer.h b/internal/ceres/block_jacobian_writer.h index b9c918bde..2bf584c92 100644 --- a/internal/ceres/block_jacobian_writer.h +++ b/internal/ceres/block_jacobian_writer.h @@ -59,7 +59,8 @@ class BlockJacobianWriter { // Create evaluate prepareres that point directly into the final jacobian. // This makes the final Write() a nop. - BlockEvaluatePreparer* CreateEvaluatePreparers(int num_threads); + std::unique_ptr CreateEvaluatePreparers( + int num_threads); std::unique_ptr CreateJacobian() const; diff --git a/internal/ceres/compressed_row_jacobian_writer.h b/internal/ceres/compressed_row_jacobian_writer.h index 2765a5835..a73d0c6f3 100644 --- a/internal/ceres/compressed_row_jacobian_writer.h +++ b/internal/ceres/compressed_row_jacobian_writer.h @@ -33,6 +33,7 @@ #ifndef CERES_INTERNAL_COMPRESSED_ROW_JACOBIAN_WRITER_H_ #define CERES_INTERNAL_COMPRESSED_ROW_JACOBIAN_WRITER_H_ +#include #include #include @@ -89,7 +90,8 @@ class CompressedRowJacobianWriter { // assumed by the cost functions, use scratch space to store the // jacobians temporarily then copy them over to the larger jacobian // in the Write() function. - ScratchEvaluatePreparer* CreateEvaluatePreparers(int num_threads) { + std::unique_ptr CreateEvaluatePreparers( + int num_threads) { return ScratchEvaluatePreparer::Create(*program_, num_threads); } diff --git a/internal/ceres/dense_jacobian_writer.h b/internal/ceres/dense_jacobian_writer.h index 0d8681926..26f171543 100644 --- a/internal/ceres/dense_jacobian_writer.h +++ b/internal/ceres/dense_jacobian_writer.h @@ -33,6 +33,8 @@ #ifndef CERES_INTERNAL_DENSE_JACOBIAN_WRITER_H_ #define CERES_INTERNAL_DENSE_JACOBIAN_WRITER_H_ +#include + #include "ceres/casts.h" #include "ceres/dense_sparse_matrix.h" #include "ceres/internal/eigen.h" @@ -54,7 +56,8 @@ class DenseJacobianWriter { // Since the dense matrix has different layout than that assumed by the cost // functions, use scratch space to store the jacobians temporarily then copy // them over to the larger jacobian later. - ScratchEvaluatePreparer* CreateEvaluatePreparers(int num_threads) { + std::unique_ptr CreateEvaluatePreparers( + int num_threads) { return ScratchEvaluatePreparer::Create(*program_, num_threads); } diff --git a/internal/ceres/dynamic_compressed_row_jacobian_writer.cc b/internal/ceres/dynamic_compressed_row_jacobian_writer.cc index f6bb3b384..b261926be 100644 --- a/internal/ceres/dynamic_compressed_row_jacobian_writer.cc +++ b/internal/ceres/dynamic_compressed_row_jacobian_writer.cc @@ -43,7 +43,7 @@ namespace internal { using std::pair; using std::vector; -ScratchEvaluatePreparer* +std::unique_ptr DynamicCompressedRowJacobianWriter::CreateEvaluatePreparers(int num_threads) { return ScratchEvaluatePreparer::Create(*program_, num_threads); } diff --git a/internal/ceres/dynamic_compressed_row_jacobian_writer.h b/internal/ceres/dynamic_compressed_row_jacobian_writer.h index b9858ba47..9a93b2b3c 100644 --- a/internal/ceres/dynamic_compressed_row_jacobian_writer.h +++ b/internal/ceres/dynamic_compressed_row_jacobian_writer.h @@ -55,7 +55,8 @@ class DynamicCompressedRowJacobianWriter { // the cost functions. The scratch space is therefore used to store // the jacobians (including zeros) temporarily before only the non-zero // entries are copied over to the larger jacobian in `Write`. - ScratchEvaluatePreparer* CreateEvaluatePreparers(int num_threads); + std::unique_ptr CreateEvaluatePreparers( + int num_threads); // Return a `DynamicCompressedRowSparseMatrix` which is filled by // `Write`. Note that `Finalize` must be called to make the diff --git a/internal/ceres/gradient_checking_cost_function.cc b/internal/ceres/gradient_checking_cost_function.cc index 1bb9ec9af..fdd378e90 100644 --- a/internal/ceres/gradient_checking_cost_function.cc +++ b/internal/ceres/gradient_checking_cost_function.cc @@ -80,7 +80,6 @@ class GradientCheckingCostFunction : public CostFunction { set_num_residuals(function->num_residuals()); } - bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const final { @@ -143,6 +142,7 @@ CallbackReturnType GradientCheckingIterationCallback::operator()( } return SOLVER_CONTINUE; } + void GradientCheckingIterationCallback::SetGradientErrorDetected( std::string& error_log) { std::lock_guard l(mutex_); @@ -150,7 +150,7 @@ void GradientCheckingIterationCallback::SetGradientErrorDetected( error_log_ += "\n" + error_log; } -CostFunction* CreateGradientCheckingCostFunction( +std::unique_ptr CreateGradientCheckingCostFunction( const CostFunction* cost_function, const std::vector* manifolds, double relative_step_size, @@ -160,12 +160,12 @@ CostFunction* CreateGradientCheckingCostFunction( NumericDiffOptions numeric_diff_options; numeric_diff_options.relative_step_size = relative_step_size; - return new GradientCheckingCostFunction(cost_function, - manifolds, - numeric_diff_options, - relative_precision, - extra_info, - callback); + return std::make_unique(cost_function, + manifolds, + numeric_diff_options, + relative_precision, + extra_info, + callback); } std::unique_ptr CreateGradientCheckingProblemImpl( diff --git a/internal/ceres/gradient_checking_cost_function.h b/internal/ceres/gradient_checking_cost_function.h index b21d113cd..e514ae6bc 100644 --- a/internal/ceres/gradient_checking_cost_function.h +++ b/internal/ceres/gradient_checking_cost_function.h @@ -73,7 +73,8 @@ class CERES_EXPORT_INTERNAL GradientCheckingIterationCallback // with finite differences. This API is only intended for unit tests that intend // to check the functionality of the GradientCheckingCostFunction // implementation directly. -CERES_EXPORT_INTERNAL CostFunction* CreateGradientCheckingCostFunction( +CERES_EXPORT_INTERNAL std::unique_ptr +CreateGradientCheckingCostFunction( const CostFunction* cost_function, const std::vector* manifolds, double relative_step_size, diff --git a/internal/ceres/gradient_checking_cost_function_test.cc b/internal/ceres/gradient_checking_cost_function_test.cc index 5e6b64c62..9994efa99 100644 --- a/internal/ceres/gradient_checking_cost_function_test.cc +++ b/internal/ceres/gradient_checking_cost_function_test.cc @@ -164,13 +164,13 @@ TEST(GradientCheckingCostFunction, ResidualsAndJacobiansArePreservedTest) { TestTerm<-1, -1> term(arity, dim); GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_cost_function( + auto gradient_checking_cost_function = CreateGradientCheckingCostFunction(&term, nullptr, kRelativeStepSize, kRelativePrecision, "Ignored.", - &callback)); + &callback); term.Evaluate(¶meters[0], &original_residual, &original_jacobians[0]); gradient_checking_cost_function->Evaluate( @@ -220,13 +220,13 @@ TEST(GradientCheckingCostFunction, SmokeTest) { { TestTerm<1, 2> term(arity, dim); GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_cost_function( + auto gradient_checking_cost_function = CreateGradientCheckingCostFunction(&term, nullptr, kRelativeStepSize, kRelativePrecision, "Fuzzy banana", - &callback)); + &callback); EXPECT_TRUE(gradient_checking_cost_function->Evaluate( ¶meters[0], &residual, &jacobians[0])); EXPECT_TRUE(callback.gradient_error_detected()); @@ -240,13 +240,13 @@ TEST(GradientCheckingCostFunction, SmokeTest) { { TestTerm<-1, -1> term(arity, dim); GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_cost_function( + auto gradient_checking_cost_function = CreateGradientCheckingCostFunction(&term, nullptr, kRelativeStepSize, kRelativePrecision, "Fuzzy banana", - &callback)); + &callback); EXPECT_TRUE(gradient_checking_cost_function->Evaluate( ¶meters[0], &residual, &jacobians[0])); EXPECT_FALSE(callback.gradient_error_detected()); @@ -365,7 +365,7 @@ TEST(GradientCheckingProblemImpl, // clang-format on GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_problem_impl = + auto gradient_checking_problem_impl = CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback); // The dimensions of the two problems match. @@ -443,7 +443,7 @@ TEST(GradientCheckingProblemImpl, ProblemDimensionsMatch) { // clang-format on GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_problem_impl = + auto gradient_checking_problem_impl = CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback); // The dimensions of the two problems match. @@ -504,7 +504,7 @@ TEST(GradientCheckingProblemImpl, ConstrainedProblemBoundsArePropagated) { problem_impl.SetParameterUpperBound(x, 1, 2.5); GradientCheckingIterationCallback callback; - std::unique_ptr gradient_checking_problem_impl = + auto gradient_checking_problem_impl = CreateGradientCheckingProblemImpl(&problem_impl, 1.0, 1.0, &callback); // The dimensions of the two problems match. diff --git a/internal/ceres/graph_algorithms.h b/internal/ceres/graph_algorithms.h index 7d63b337f..82a20d831 100644 --- a/internal/ceres/graph_algorithms.h +++ b/internal/ceres/graph_algorithms.h @@ -257,11 +257,11 @@ Vertex FindConnectedComponent(const Vertex& vertex, // spanning forest, or a collection of linear paths that span the // graph G. template -WeightedGraph* Degree2MaximumSpanningForest( +std::unique_ptr> Degree2MaximumSpanningForest( const WeightedGraph& graph) { // Array of edges sorted in decreasing order of their weights. std::vector>> weighted_edges; - WeightedGraph* forest = new WeightedGraph(); + auto forest = std::make_unique>(); // Disjoint-set to keep track of the connected components in the // maximum spanning tree. diff --git a/internal/ceres/minimizer.cc b/internal/ceres/minimizer.cc index 85629682e..a3c77cba0 100644 --- a/internal/ceres/minimizer.cc +++ b/internal/ceres/minimizer.cc @@ -38,13 +38,13 @@ namespace ceres { namespace internal { -Minimizer* Minimizer::Create(MinimizerType minimizer_type) { +std::unique_ptr Minimizer::Create(MinimizerType minimizer_type) { if (minimizer_type == TRUST_REGION) { - return new TrustRegionMinimizer; + return std::make_unique(); } if (minimizer_type == LINE_SEARCH) { - return new LineSearchMinimizer; + return std::make_unique(); } LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type; diff --git a/internal/ceres/minimizer.h b/internal/ceres/minimizer.h index 246550de7..326b325cf 100644 --- a/internal/ceres/minimizer.h +++ b/internal/ceres/minimizer.h @@ -178,7 +178,7 @@ class CERES_EXPORT_INTERNAL Minimizer { std::shared_ptr inner_iteration_minimizer; }; - static Minimizer* Create(MinimizerType minimizer_type); + static std::unique_ptr Create(MinimizerType minimizer_type); static bool RunCallbacks(const Options& options, const IterationSummary& iteration_summary, Solver::Summary* summary); diff --git a/internal/ceres/parameter_block_ordering.cc b/internal/ceres/parameter_block_ordering.cc index d16e6dd4f..50a30c9d0 100644 --- a/internal/ceres/parameter_block_ordering.cc +++ b/internal/ceres/parameter_block_ordering.cc @@ -54,7 +54,7 @@ int ComputeStableSchurOrdering(const Program& program, CHECK(ordering != nullptr); ordering->clear(); EventLogger event_logger("ComputeStableSchurOrdering"); - std::unique_ptr> graph(CreateHessianGraph(program)); + auto graph = CreateHessianGraph(program); event_logger.AddEvent("CreateHessianGraph"); const vector& parameter_blocks = program.parameter_blocks(); @@ -86,7 +86,7 @@ int ComputeSchurOrdering(const Program& program, CHECK(ordering != nullptr); ordering->clear(); - std::unique_ptr> graph(CreateHessianGraph(program)); + auto graph = CreateHessianGraph(program); int independent_set_size = IndependentSetOrdering(*graph, ordering); const vector& parameter_blocks = program.parameter_blocks(); @@ -106,7 +106,7 @@ void ComputeRecursiveIndependentSetOrdering(const Program& program, CHECK(ordering != nullptr); ordering->Clear(); const vector parameter_blocks = program.parameter_blocks(); - std::unique_ptr> graph(CreateHessianGraph(program)); + auto graph = CreateHessianGraph(program); int num_covered = 0; int round = 0; @@ -124,8 +124,9 @@ void ComputeRecursiveIndependentSetOrdering(const Program& program, } } -Graph* CreateHessianGraph(const Program& program) { - Graph* graph = new Graph; +std::unique_ptr> CreateHessianGraph( + const Program& program) { + auto graph = std::make_unique>(); CHECK(graph != nullptr); const vector& parameter_blocks = program.parameter_blocks(); for (int i = 0; i < parameter_blocks.size(); ++i) { diff --git a/internal/ceres/parameter_block_ordering.h b/internal/ceres/parameter_block_ordering.h index 82ab75dc6..d9b321004 100644 --- a/internal/ceres/parameter_block_ordering.h +++ b/internal/ceres/parameter_block_ordering.h @@ -31,6 +31,7 @@ #ifndef CERES_INTERNAL_PARAMETER_BLOCK_ORDERING_H_ #define CERES_INTERNAL_PARAMETER_BLOCK_ORDERING_H_ +#include #include #include "ceres/graph.h" @@ -78,8 +79,8 @@ CERES_EXPORT_INTERNAL void ComputeRecursiveIndependentSetOrdering( // vertex corresponds to a parameter block in the Problem except for // parameter blocks that are marked constant. An edge connects two // parameter blocks, if they co-occur in a residual block. -CERES_EXPORT_INTERNAL Graph* CreateHessianGraph( - const Program& program); +CERES_EXPORT_INTERNAL std::unique_ptr> +CreateHessianGraph(const Program& program); // Iterate over each of the groups in order of their priority and fill // summary with their sizes. diff --git a/internal/ceres/parameter_block_ordering_test.cc b/internal/ceres/parameter_block_ordering_test.cc index fd248398f..a64f81bfa 100644 --- a/internal/ceres/parameter_block_ordering_test.cc +++ b/internal/ceres/parameter_block_ordering_test.cc @@ -46,9 +46,6 @@ namespace ceres { namespace internal { -using std::vector; - -typedef Graph HessianGraph; typedef std::unordered_set VertexSet; template @@ -85,8 +82,9 @@ class SchurOrderingTest : public ::testing::Test { TEST_F(SchurOrderingTest, NoFixed) { const Program& program = problem_.program(); - const vector& parameter_blocks = program.parameter_blocks(); - std::unique_ptr graph(CreateHessianGraph(program)); + const std::vector& parameter_blocks = + program.parameter_blocks(); + auto graph = CreateHessianGraph(program); const VertexSet& vertices = graph->vertices(); EXPECT_EQ(vertices.size(), 4); @@ -131,7 +129,7 @@ TEST_F(SchurOrderingTest, AllFixed) { problem_.SetParameterBlockConstant(w_); const Program& program = problem_.program(); - std::unique_ptr graph(CreateHessianGraph(program)); + auto graph = CreateHessianGraph(program); EXPECT_EQ(graph->vertices().size(), 0); } @@ -139,8 +137,9 @@ TEST_F(SchurOrderingTest, OneFixed) { problem_.SetParameterBlockConstant(x_); const Program& program = problem_.program(); - const vector& parameter_blocks = program.parameter_blocks(); - std::unique_ptr graph(CreateHessianGraph(program)); + const std::vector& parameter_blocks = + program.parameter_blocks(); + auto graph = CreateHessianGraph(program); const VertexSet& vertices = graph->vertices(); @@ -171,7 +170,7 @@ TEST_F(SchurOrderingTest, OneFixed) { } // The constant parameter block is at the end. - vector ordering; + std::vector ordering; ComputeSchurOrdering(program, &ordering); EXPECT_EQ(ordering.back(), parameter_blocks[0]); } diff --git a/internal/ceres/preprocessor.cc b/internal/ceres/preprocessor.cc index 1fcd74a8d..b9dc0669b 100644 --- a/internal/ceres/preprocessor.cc +++ b/internal/ceres/preprocessor.cc @@ -41,13 +41,14 @@ namespace ceres { namespace internal { -Preprocessor* Preprocessor::Create(MinimizerType minimizer_type) { +std::unique_ptr Preprocessor::Create( + MinimizerType minimizer_type) { if (minimizer_type == TRUST_REGION) { - return new TrustRegionPreprocessor; + return std::make_unique(); } if (minimizer_type == LINE_SEARCH) { - return new LineSearchPreprocessor; + return std::make_unique(); } LOG(FATAL) << "Unknown minimizer_type: " << minimizer_type; diff --git a/internal/ceres/preprocessor.h b/internal/ceres/preprocessor.h index ec56c6e43..7dc74ccb9 100644 --- a/internal/ceres/preprocessor.h +++ b/internal/ceres/preprocessor.h @@ -70,7 +70,7 @@ struct PreprocessedProblem; class CERES_EXPORT_INTERNAL Preprocessor { public: // Factory. - static Preprocessor* Create(MinimizerType minimizer_type); + static std::unique_ptr Create(MinimizerType minimizer_type); virtual ~Preprocessor(); virtual bool Preprocess(const Solver::Options& options, ProblemImpl* problem, diff --git a/internal/ceres/program_evaluator.h b/internal/ceres/program_evaluator.h index 2ab52da38..c009719c1 100644 --- a/internal/ceres/program_evaluator.h +++ b/internal/ceres/program_evaluator.h @@ -61,9 +61,11 @@ // // Evaluator::CreateJacobian. // 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. -// EvaluatePreparer* CreateEvaluatePreparers(int num_threads); +// // Create num_threads evaluate preparers.Resulting preparers are valid +// // while *this is. +// +// std::unique_ptr CreateEvaluatePreparers( +// int num_threads); // // // Write the block jacobians from a residual block evaluation to the // // larger sparse jacobian. @@ -115,8 +117,8 @@ class ProgramEvaluator : public Evaluator { : options_(options), program_(program), jacobian_writer_(options, program), - evaluate_preparers_( - jacobian_writer_.CreateEvaluatePreparers(options.num_threads)) { + evaluate_preparers_(std::move( + jacobian_writer_.CreateEvaluatePreparers(options.num_threads))) { #ifdef CERES_NO_THREADS if (options_.num_threads > 1) { LOG(WARNING) << "No threading support is compiled into this binary; " diff --git a/internal/ceres/scratch_evaluate_preparer.cc b/internal/ceres/scratch_evaluate_preparer.cc index a59694ee0..f7589f60c 100644 --- a/internal/ceres/scratch_evaluate_preparer.cc +++ b/internal/ceres/scratch_evaluate_preparer.cc @@ -37,9 +37,9 @@ namespace ceres { namespace internal { -ScratchEvaluatePreparer* ScratchEvaluatePreparer::Create(const Program& program, - int num_threads) { - ScratchEvaluatePreparer* preparers = new ScratchEvaluatePreparer[num_threads]; +std::unique_ptr ScratchEvaluatePreparer::Create( + const Program& program, int num_threads) { + auto preparers = std::make_unique(num_threads); int max_derivatives_per_residual_block = program.MaxDerivativesPerResidualBlock(); for (int i = 0; i < num_threads; i++) { diff --git a/internal/ceres/scratch_evaluate_preparer.h b/internal/ceres/scratch_evaluate_preparer.h index 2d2745d62..d0ecc3d7b 100644 --- a/internal/ceres/scratch_evaluate_preparer.h +++ b/internal/ceres/scratch_evaluate_preparer.h @@ -47,8 +47,8 @@ class SparseMatrix; class ScratchEvaluatePreparer { public: // Create num_threads ScratchEvaluatePreparers. - static ScratchEvaluatePreparer* Create(const Program& program, - int num_threads); + static std::unique_ptr Create( + const Program& program, int num_threads); // EvaluatePreparer interface void Init(int max_derivatives_per_residual_block); diff --git a/internal/ceres/solver.cc b/internal/ceres/solver.cc index 1d2420c23..6561f18c5 100644 --- a/internal/ceres/solver.cc +++ b/internal/ceres/solver.cc @@ -436,8 +436,7 @@ void Minimize(internal::PreprocessedProblem* pp, Solver::Summary* summary) { } const Vector original_reduced_parameters = pp->reduced_parameters; - std::unique_ptr minimizer( - Minimizer::Create(pp->options.minimizer_type)); + auto minimizer = Minimizer::Create(pp->options.minimizer_type); minimizer->Minimize( pp->minimizer_options, pp->reduced_parameters.data(), summary); @@ -534,8 +533,7 @@ void Solver::Solve(const Solver::Options& options, // The main thread also does work so we only need to launch num_threads - 1. problem_impl->context()->EnsureMinimumThreads(options.num_threads - 1); - std::unique_ptr preprocessor( - Preprocessor::Create(modified_options.minimizer_type)); + auto preprocessor = Preprocessor::Create(modified_options.minimizer_type); PreprocessedProblem pp; const bool status = diff --git a/internal/ceres/sparse_cholesky_test.cc b/internal/ceres/sparse_cholesky_test.cc index 2ef24e321..363f9917b 100644 --- a/internal/ceres/sparse_cholesky_test.cc +++ b/internal/ceres/sparse_cholesky_test.cc @@ -51,10 +51,11 @@ namespace internal { namespace { -BlockSparseMatrix* CreateRandomFullRankMatrix(const int num_col_blocks, - const int min_col_block_size, - const int max_col_block_size, - const double block_density) { +std::unique_ptr CreateRandomFullRankMatrix( + const int num_col_blocks, + const int min_col_block_size, + const int max_col_block_size, + const double block_density) { // Create a random matrix BlockSparseMatrix::RandomMatrixOptions options; options.num_col_blocks = num_col_blocks; @@ -65,16 +66,14 @@ BlockSparseMatrix* CreateRandomFullRankMatrix(const int num_col_blocks, options.min_row_block_size = 1; options.max_row_block_size = max_col_block_size; options.block_density = block_density; - std::unique_ptr random_matrix( - BlockSparseMatrix::CreateRandomMatrix(options)); + auto random_matrix = BlockSparseMatrix::CreateRandomMatrix(options); // Add a diagonal block sparse matrix to make it full rank. Vector diagonal = Vector::Ones(random_matrix->num_cols()); - std::unique_ptr block_diagonal( - BlockSparseMatrix::CreateDiagonalMatrix( - diagonal.data(), random_matrix->block_structure()->cols)); + auto block_diagonal = BlockSparseMatrix::CreateDiagonalMatrix( + diagonal.data(), random_matrix->block_structure()->cols); random_matrix->AppendRows(*block_diagonal); - return random_matrix.release(); + return random_matrix; } static bool ComputeExpectedSolution(const CompressedRowSparseMatrix& lhs, @@ -115,15 +114,13 @@ void SparseCholeskySolverUnitTest( sparse_cholesky_options.sparse_linear_algebra_library_type = sparse_linear_algebra_library_type; sparse_cholesky_options.use_postordering = (ordering_type == AMD); - std::unique_ptr sparse_cholesky = - SparseCholesky::Create(sparse_cholesky_options); + auto sparse_cholesky = SparseCholesky::Create(sparse_cholesky_options); const CompressedRowSparseMatrix::StorageType storage_type = sparse_cholesky->StorageType(); - std::unique_ptr m(CreateRandomFullRankMatrix( - num_blocks, min_block_size, max_block_size, block_density)); - std::unique_ptr inner_product_computer( - InnerProductComputer::Create(*m, storage_type)); + auto m = CreateRandomFullRankMatrix( + num_blocks, min_block_size, max_block_size, block_density); + auto inner_product_computer = InnerProductComputer::Create(*m, storage_type); inner_product_computer->Compute(); CompressedRowSparseMatrix* lhs = inner_product_computer->mutable_result(); diff --git a/internal/ceres/visibility.cc b/internal/ceres/visibility.cc index 06f31ec27..54de638b5 100644 --- a/internal/ceres/visibility.cc +++ b/internal/ceres/visibility.cc @@ -79,7 +79,7 @@ void ComputeVisibility(const CompressedRowBlockStructure& block_structure, } } -WeightedGraph* CreateSchurComplementGraph( +std::unique_ptr> CreateSchurComplementGraph( const vector>& visibility) { const time_t start_time = time(nullptr); // Compute the number of e_blocks/point blocks. Since the visibility @@ -121,7 +121,7 @@ WeightedGraph* CreateSchurComplementGraph( } } - WeightedGraph* graph = new WeightedGraph; + auto graph = std::make_unique>(); // Add vertices and initialize the pairs for self edges so that self // edges are guaranteed. This is needed for the Canonical views diff --git a/internal/ceres/visibility.h b/internal/ceres/visibility.h index 68c6723fa..e86bb8833 100644 --- a/internal/ceres/visibility.h +++ b/internal/ceres/visibility.h @@ -35,6 +35,7 @@ #ifndef CERES_INTERNAL_VISIBILITY_H_ #define CERES_INTERNAL_VISIBILITY_H_ +#include #include #include @@ -72,8 +73,8 @@ CERES_EXPORT_INTERNAL void ComputeVisibility( // // Caller acquires ownership of the returned WeightedGraph pointer // (heap-allocated). -CERES_EXPORT_INTERNAL WeightedGraph* CreateSchurComplementGraph( - const std::vector>& visibility); +CERES_EXPORT_INTERNAL std::unique_ptr> +CreateSchurComplementGraph(const std::vector>& visibility); } // namespace internal } // namespace ceres diff --git a/internal/ceres/visibility_based_preconditioner.cc b/internal/ceres/visibility_based_preconditioner.cc index 3e3604f74..9419e6b80 100644 --- a/internal/ceres/visibility_based_preconditioner.cc +++ b/internal/ceres/visibility_based_preconditioner.cc @@ -162,11 +162,9 @@ void VisibilityBasedPreconditioner::ComputeClusterTridiagonalSparsity( // maximum spanning forest of this graph. vector> cluster_visibility; ComputeClusterVisibility(visibility, &cluster_visibility); - std::unique_ptr> cluster_graph( - CreateClusterGraph(cluster_visibility)); + auto cluster_graph = CreateClusterGraph(cluster_visibility); CHECK(cluster_graph != nullptr); - std::unique_ptr> forest( - Degree2MaximumSpanningForest(*cluster_graph)); + auto forest = Degree2MaximumSpanningForest(*cluster_graph); CHECK(forest != nullptr); ForestToClusterPairs(*forest, &cluster_pairs_); } @@ -187,8 +185,7 @@ void VisibilityBasedPreconditioner::InitStorage( // memberships for each camera block. void VisibilityBasedPreconditioner::ClusterCameras( const vector>& visibility) { - std::unique_ptr> schur_complement_graph( - CreateSchurComplementGraph(visibility)); + auto schur_complement_graph = CreateSchurComplementGraph(visibility); CHECK(schur_complement_graph != nullptr); std::unordered_map membership; @@ -503,9 +500,10 @@ void VisibilityBasedPreconditioner::ComputeClusterVisibility( // Construct a graph whose vertices are the clusters, and the edge // weights are the number of 3D points visible to cameras in both the // vertices. -WeightedGraph* VisibilityBasedPreconditioner::CreateClusterGraph( +std::unique_ptr> +VisibilityBasedPreconditioner::CreateClusterGraph( const vector>& cluster_visibility) const { - WeightedGraph* cluster_graph = new WeightedGraph; + auto cluster_graph = std::make_unique>(); for (int i = 0; i < num_clusters_; ++i) { cluster_graph->AddVertex(i); diff --git a/internal/ceres/visibility_based_preconditioner.h b/internal/ceres/visibility_based_preconditioner.h index 0da42a23f..1150d3345 100644 --- a/internal/ceres/visibility_based_preconditioner.h +++ b/internal/ceres/visibility_based_preconditioner.h @@ -160,7 +160,7 @@ class VisibilityBasedPreconditioner : public BlockSparseMatrixPreconditioner { void ComputeClusterVisibility( const std::vector>& visibility, std::vector>* cluster_visibility) const; - WeightedGraph* CreateClusterGraph( + std::unique_ptr> CreateClusterGraph( const std::vector>& visibility) const; void ForestToClusterPairs( const WeightedGraph& forest,