dynamic_numeric_diff_cost_function.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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: mierle@gmail.com (Keir Mierle)
  30. // sameeragarwal@google.com (Sameer Agarwal)
  31. // thadh@gmail.com (Thad Hughes)
  32. // tbennun@gmail.com (Tal Ben-Nun)
  33. #ifndef CERES_PUBLIC_DYNAMIC_NUMERIC_DIFF_COST_FUNCTION_H_
  34. #define CERES_PUBLIC_DYNAMIC_NUMERIC_DIFF_COST_FUNCTION_H_
  35. #include <cmath>
  36. #include <memory>
  37. #include <numeric>
  38. #include <vector>
  39. #include "ceres/dynamic_cost_function.h"
  40. #include "ceres/internal/eigen.h"
  41. #include "ceres/internal/numeric_diff.h"
  42. #include "ceres/internal/parameter_dims.h"
  43. #include "ceres/numeric_diff_options.h"
  44. #include "ceres/types.h"
  45. #include "glog/logging.h"
  46. namespace ceres {
  47. // This numeric diff implementation differs from the one found in
  48. // numeric_diff_cost_function.h by supporting numericdiff on cost
  49. // functions with variable numbers of parameters with variable
  50. // sizes. With the other implementation, all the sizes (both the
  51. // number of parameter blocks and the size of each block) must be
  52. // fixed at compile time.
  53. //
  54. // The functor API differs slightly from the API for fixed size
  55. // numeric diff; the expected interface for the cost functors is:
  56. //
  57. // struct MyCostFunctor {
  58. // bool operator()(double const*
  59. // const* parameters,
  60. // double* residuals) const {
  61. // // Use parameters[i] to access the i'th parameter block.
  62. // }
  63. // }
  64. //
  65. // Since the sizing of the parameters is done at runtime, you must
  66. // also specify the sizes after creating the
  67. // DynamicNumericDiffCostFunction. For example:
  68. //
  69. // DynamicAutoDiffCostFunction<MyCostFunctor, CENTRAL> cost_function(
  70. // new MyCostFunctor());
  71. // cost_function.AddParameterBlock(5);
  72. // cost_function.AddParameterBlock(10);
  73. // cost_function.SetNumResiduals(21);
  74. template <typename CostFunctor, NumericDiffMethodType kMethod = CENTRAL>
  75. class DynamicNumericDiffCostFunction final : public DynamicCostFunction {
  76. public:
  77. explicit DynamicNumericDiffCostFunction(
  78. const CostFunctor* functor,
  79. Ownership ownership = TAKE_OWNERSHIP,
  80. const NumericDiffOptions& options = NumericDiffOptions())
  81. : functor_(functor), ownership_(ownership), options_(options) {}
  82. DynamicNumericDiffCostFunction(DynamicNumericDiffCostFunction&& other)
  83. : functor_(std::move(other.functor_)), ownership_(other.ownership_) {}
  84. ~DynamicNumericDiffCostFunction() override {
  85. if (ownership_ != TAKE_OWNERSHIP) {
  86. functor_.release();
  87. }
  88. }
  89. bool Evaluate(double const* const* parameters,
  90. double* residuals,
  91. double** jacobians) const override {
  92. using internal::NumericDiff;
  93. CHECK_GT(num_residuals(), 0)
  94. << "You must call DynamicNumericDiffCostFunction::SetNumResiduals() "
  95. << "before DynamicNumericDiffCostFunction::Evaluate().";
  96. const std::vector<int32_t>& block_sizes = parameter_block_sizes();
  97. CHECK(!block_sizes.empty())
  98. << "You must call DynamicNumericDiffCostFunction::AddParameterBlock() "
  99. << "before DynamicNumericDiffCostFunction::Evaluate().";
  100. const bool status =
  101. internal::VariadicEvaluate<internal::DynamicParameterDims>(
  102. *functor_.get(), parameters, residuals);
  103. if (jacobians == nullptr || !status) {
  104. return status;
  105. }
  106. // Create local space for a copy of the parameters which will get mutated.
  107. int parameters_size = accumulate(block_sizes.begin(), block_sizes.end(), 0);
  108. std::vector<double> parameters_copy(parameters_size);
  109. std::vector<double*> parameters_references_copy(block_sizes.size());
  110. parameters_references_copy[0] = &parameters_copy[0];
  111. for (size_t block = 1; block < block_sizes.size(); ++block) {
  112. parameters_references_copy[block] =
  113. parameters_references_copy[block - 1] + block_sizes[block - 1];
  114. }
  115. // Copy the parameters into the local temp space.
  116. for (size_t block = 0; block < block_sizes.size(); ++block) {
  117. memcpy(parameters_references_copy[block],
  118. parameters[block],
  119. block_sizes[block] * sizeof(*parameters[block]));
  120. }
  121. for (size_t block = 0; block < block_sizes.size(); ++block) {
  122. if (jacobians[block] != nullptr &&
  123. !NumericDiff<CostFunctor,
  124. kMethod,
  125. ceres::DYNAMIC,
  126. internal::DynamicParameterDims,
  127. ceres::DYNAMIC,
  128. ceres::DYNAMIC>::
  129. EvaluateJacobianForParameterBlock(functor_.get(),
  130. residuals,
  131. options_,
  132. this->num_residuals(),
  133. block,
  134. block_sizes[block],
  135. &parameters_references_copy[0],
  136. jacobians[block])) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. private:
  143. std::unique_ptr<const CostFunctor> functor_;
  144. Ownership ownership_;
  145. NumericDiffOptions options_;
  146. };
  147. } // namespace ceres
  148. #endif // CERES_PUBLIC_DYNAMIC_AUTODIFF_COST_FUNCTION_H_