problem_impl.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: keir@google.com (Keir Mierle)
  30. //
  31. // This is the implementation of the public Problem API. The pointer to
  32. // implementation (PIMPL) idiom makes it possible for Ceres internal code to
  33. // refer to the private data members without needing to exposing it to the
  34. // world. An alternative to PIMPL is to have a factory which returns instances
  35. // of a virtual base class; while that approach would work, it requires clients
  36. // to always put a Problem object into a scoped pointer; this needlessly muddies
  37. // client code for little benefit. Therefore, the PIMPL comprise was chosen.
  38. #ifndef CERES_PUBLIC_PROBLEM_IMPL_H_
  39. #define CERES_PUBLIC_PROBLEM_IMPL_H_
  40. #include <array>
  41. #include <map>
  42. #include <memory>
  43. #include <unordered_map>
  44. #include <unordered_set>
  45. #include <vector>
  46. #include "ceres/context_impl.h"
  47. #include "ceres/internal/disable_warnings.h"
  48. #include "ceres/internal/export.h"
  49. #include "ceres/internal/port.h"
  50. #include "ceres/manifold.h"
  51. #include "ceres/problem.h"
  52. #include "ceres/types.h"
  53. namespace ceres {
  54. class CostFunction;
  55. class EvaluationCallback;
  56. class LossFunction;
  57. struct CRSMatrix;
  58. namespace internal {
  59. class Program;
  60. class ResidualBlock;
  61. class CERES_NO_EXPORT ProblemImpl {
  62. public:
  63. using ParameterMap = std::map<double*, ParameterBlock*>;
  64. using ResidualBlockSet = std::unordered_set<ResidualBlock*>;
  65. using CostFunctionRefCount = std::map<CostFunction*, int>;
  66. using LossFunctionRefCount = std::map<LossFunction*, int>;
  67. ProblemImpl();
  68. explicit ProblemImpl(const Problem::Options& options);
  69. ProblemImpl(const ProblemImpl&) = delete;
  70. void operator=(const ProblemImpl&) = delete;
  71. ~ProblemImpl();
  72. // See the public problem.h file for description of these methods.
  73. ResidualBlockId AddResidualBlock(CostFunction* cost_function,
  74. LossFunction* loss_function,
  75. double* const* const parameter_blocks,
  76. int num_parameter_blocks);
  77. template <typename... Ts>
  78. ResidualBlockId AddResidualBlock(CostFunction* cost_function,
  79. LossFunction* loss_function,
  80. double* x0,
  81. Ts*... xs) {
  82. const std::array<double*, sizeof...(Ts) + 1> parameter_blocks{{x0, xs...}};
  83. return AddResidualBlock(cost_function,
  84. loss_function,
  85. parameter_blocks.data(),
  86. static_cast<int>(parameter_blocks.size()));
  87. }
  88. void AddParameterBlock(double* values, int size);
  89. void AddParameterBlock(double* values, int size, Manifold* manifold);
  90. void RemoveResidualBlock(ResidualBlock* residual_block);
  91. void RemoveParameterBlock(const double* values);
  92. void SetParameterBlockConstant(const double* values);
  93. void SetParameterBlockVariable(double* values);
  94. bool IsParameterBlockConstant(const double* values) const;
  95. void SetManifold(double* values, Manifold* manifold);
  96. const Manifold* GetManifold(const double* values) const;
  97. bool HasManifold(const double* values) const;
  98. void SetParameterLowerBound(double* values, int index, double lower_bound);
  99. void SetParameterUpperBound(double* values, int index, double upper_bound);
  100. double GetParameterLowerBound(const double* values, int index) const;
  101. double GetParameterUpperBound(const double* values, int index) const;
  102. bool Evaluate(const Problem::EvaluateOptions& options,
  103. double* cost,
  104. std::vector<double>* residuals,
  105. std::vector<double>* gradient,
  106. CRSMatrix* jacobian);
  107. bool EvaluateResidualBlock(ResidualBlock* residual_block,
  108. bool apply_loss_function,
  109. bool new_point,
  110. double* cost,
  111. double* residuals,
  112. double** jacobians) const;
  113. int NumParameterBlocks() const;
  114. int NumParameters() const;
  115. int NumResidualBlocks() const;
  116. int NumResiduals() const;
  117. int ParameterBlockSize(const double* values) const;
  118. int ParameterBlockTangentSize(const double* values) const;
  119. bool HasParameterBlock(const double* values) const;
  120. void GetParameterBlocks(std::vector<double*>* parameter_blocks) const;
  121. void GetResidualBlocks(std::vector<ResidualBlockId>* residual_blocks) const;
  122. void GetParameterBlocksForResidualBlock(
  123. const ResidualBlockId residual_block,
  124. std::vector<double*>* parameter_blocks) const;
  125. const CostFunction* GetCostFunctionForResidualBlock(
  126. const ResidualBlockId residual_block) const;
  127. const LossFunction* GetLossFunctionForResidualBlock(
  128. const ResidualBlockId residual_block) const;
  129. void GetResidualBlocksForParameterBlock(
  130. const double* values,
  131. std::vector<ResidualBlockId>* residual_blocks) const;
  132. const Program& program() const { return *program_; }
  133. Program* mutable_program() { return program_.get(); }
  134. const ParameterMap& parameter_map() const { return parameter_block_map_; }
  135. const ResidualBlockSet& residual_block_set() const {
  136. CHECK(options_.enable_fast_removal)
  137. << "Fast removal not enabled, residual_block_set is not maintained.";
  138. return residual_block_set_;
  139. }
  140. const Problem::Options& options() const { return options_; }
  141. ContextImpl* context() { return context_impl_; }
  142. private:
  143. ParameterBlock* InternalAddParameterBlock(double* values, int size);
  144. void InternalSetManifold(double* values,
  145. ParameterBlock* parameter_block,
  146. Manifold* manifold);
  147. void InternalRemoveResidualBlock(ResidualBlock* residual_block);
  148. // Delete the arguments in question. These differ from the Remove* functions
  149. // in that they do not clean up references to the block to delete; they
  150. // merely delete them.
  151. template <typename Block>
  152. void DeleteBlockInVector(std::vector<Block*>* mutable_blocks,
  153. Block* block_to_remove);
  154. void DeleteBlock(ResidualBlock* residual_block);
  155. void DeleteBlock(ParameterBlock* parameter_block);
  156. const Problem::Options options_;
  157. bool context_impl_owned_;
  158. ContextImpl* context_impl_;
  159. // The mapping from user pointers to parameter blocks.
  160. ParameterMap parameter_block_map_;
  161. // Iff enable_fast_removal is enabled, contains the current residual blocks.
  162. ResidualBlockSet residual_block_set_;
  163. // The actual parameter and residual blocks.
  164. std::unique_ptr<internal::Program> program_;
  165. // TODO(sameeragarwal): Unify the shared object handling across object types.
  166. // Right now we are using vectors for Manifold objects and reference counting
  167. // for CostFunctions and LossFunctions. Ideally this should be done uniformly.
  168. // When removing parameter blocks, manifolds have ambiguous
  169. // ownership. Instead of scanning the entire problem to see if the
  170. // manifold is shared with other parameter blocks, buffer
  171. // them until destruction.
  172. std::vector<Manifold*> manifolds_to_delete_;
  173. // For each cost function and loss function in the problem, a count
  174. // of the number of residual blocks that refer to them. When the
  175. // count goes to zero and the problem owns these objects, they are
  176. // destroyed.
  177. CostFunctionRefCount cost_function_ref_count_;
  178. LossFunctionRefCount loss_function_ref_count_;
  179. };
  180. } // namespace internal
  181. } // namespace ceres
  182. #include "ceres/internal/reenable_warnings.h"
  183. #endif // CERES_PUBLIC_PROBLEM_IMPL_H_