From 5ecb1c3f1dfde6e8ed4b493eafef7b43dad19e72 Mon Sep 17 00:00:00 2001 From: Sameer Agarwal Date: Tue, 1 Apr 2014 09:20:35 -0700 Subject: [PATCH] Add Problem::IsParameterBlockPresent. This allows the user to query the Problem to see if a parameter block is already present or not. Change-Id: If786f6c008cc644f3398597901d718d12a6d865d --- include/ceres/problem.h | 3 +++ internal/ceres/problem.cc | 4 ++++ internal/ceres/problem_impl.cc | 5 +++++ internal/ceres/problem_impl.h | 3 +++ internal/ceres/problem_test.cc | 2 ++ 5 files changed, 17 insertions(+) diff --git a/include/ceres/problem.h b/include/ceres/problem.h index bed792a..b8759eb 100644 --- a/include/ceres/problem.h +++ b/include/ceres/problem.h @@ -349,6 +349,9 @@ class Problem { // block, then ParameterBlockLocalSize = ParameterBlockSize. int ParameterBlockLocalSize(const double* values) const; + // Is the given parameter block present in this problem or not? + bool HasParameterBlock(const double* values) const; + // Fills the passed parameter_blocks vector with pointers to the // parameter blocks currently in the problem. After this call, // parameter_block.size() == NumParameterBlocks. diff --git a/internal/ceres/problem.cc b/internal/ceres/problem.cc index 9bdc1ef..674694d 100644 --- a/internal/ceres/problem.cc +++ b/internal/ceres/problem.cc @@ -231,6 +231,10 @@ int Problem::ParameterBlockLocalSize(const double* parameter_block) const { return problem_impl_->ParameterBlockLocalSize(parameter_block); }; +bool Problem::HasParameterBlock(const double* values) const { + return problem_impl_->HasParameterBlock(values); +} + void Problem::GetParameterBlocks(vector* parameter_blocks) const { problem_impl_->GetParameterBlocks(parameter_blocks); } diff --git a/internal/ceres/problem_impl.cc b/internal/ceres/problem_impl.cc index d9fb2af..7c86efb 100644 --- a/internal/ceres/problem_impl.cc +++ b/internal/ceres/problem_impl.cc @@ -791,6 +791,11 @@ int ProblemImpl::ParameterBlockLocalSize(const double* parameter_block) const { parameter_block_map_, const_cast(parameter_block))->LocalSize(); }; +bool ProblemImpl::HasParameterBlock(const double* parameter_block) const { + return (parameter_block_map_.find(const_cast(parameter_block)) != + parameter_block_map_.end()); +} + void ProblemImpl::GetParameterBlocks(vector* parameter_blocks) const { CHECK_NOTNULL(parameter_blocks); parameter_blocks->resize(0); diff --git a/internal/ceres/problem_impl.h b/internal/ceres/problem_impl.h index e846c03..7b5547b 100644 --- a/internal/ceres/problem_impl.h +++ b/internal/ceres/problem_impl.h @@ -147,6 +147,9 @@ class ProblemImpl { int ParameterBlockSize(const double* parameter_block) const; int ParameterBlockLocalSize(const double* parameter_block) const; + + bool HasParameterBlock(const double* parameter_block) const; + void GetParameterBlocks(vector* parameter_blocks) const; void GetResidualBlocks(vector* residual_blocks) const; diff --git a/internal/ceres/problem_test.cc b/internal/ceres/problem_test.cc index eb75e3a..db082ec 100644 --- a/internal/ceres/problem_test.cc +++ b/internal/ceres/problem_test.cc @@ -556,7 +556,9 @@ TEST(Problem, ParameterBlockQueryTest) { EXPECT_TRUE(parameter_blocks[0] == x || parameter_blocks[0] == y); EXPECT_TRUE(parameter_blocks[1] == x || parameter_blocks[1] == y); + EXPECT_TRUE(problem.HasParameterBlock(x)); problem.RemoveParameterBlock(x); + EXPECT_FALSE(problem.HasParameterBlock(x)); problem.GetParameterBlocks(¶meter_blocks); EXPECT_EQ(parameter_blocks.size(), 1); EXPECT_TRUE(parameter_blocks[0] == y);