tiny_solver_cost_function_adapter.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2019 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: sameeragarwal@google.com (Sameer Agarwal)
  30. #ifndef CERES_PUBLIC_TINY_SOLVER_COST_FUNCTION_ADAPTER_H_
  31. #define CERES_PUBLIC_TINY_SOLVER_COST_FUNCTION_ADAPTER_H_
  32. #include "Eigen/Core"
  33. #include "ceres/cost_function.h"
  34. #include "glog/logging.h"
  35. namespace ceres {
  36. // An adapter class that lets users of TinySolver use
  37. // ceres::CostFunction objects that have exactly one parameter block.
  38. //
  39. // The adapter allows for the number of residuals and the size of the
  40. // parameter block to be specified at compile or run-time.
  41. //
  42. // WARNING: This object is not thread-safe.
  43. //
  44. // Example usage:
  45. //
  46. // CostFunction* cost_function = ...
  47. //
  48. // Number of residuals and parameter block size known at compile time:
  49. //
  50. // TinySolverCostFunctionAdapter<kNumResiduals, kNumParameters>
  51. // cost_function_adapter(*cost_function);
  52. //
  53. // Number of residuals known at compile time and the parameter block
  54. // size not known at compile time.
  55. //
  56. // TinySolverCostFunctionAdapter<kNumResiduals, Eigen::Dynamic>
  57. // cost_function_adapter(*cost_function);
  58. //
  59. // Number of residuals not known at compile time and the parameter
  60. // block size known at compile time.
  61. //
  62. // TinySolverCostFunctionAdapter<Eigen::Dynamic, kParameterBlockSize>
  63. // cost_function_adapter(*cost_function);
  64. //
  65. // Number of residuals not known at compile time and the parameter
  66. // block size not known at compile time.
  67. //
  68. // TinySolverCostFunctionAdapter cost_function_adapter(*cost_function);
  69. //
  70. template <int kNumResiduals = Eigen::Dynamic,
  71. int kNumParameters = Eigen::Dynamic>
  72. class TinySolverCostFunctionAdapter {
  73. public:
  74. using Scalar = double;
  75. enum ComponentSizeType {
  76. NUM_PARAMETERS = kNumParameters,
  77. NUM_RESIDUALS = kNumResiduals
  78. };
  79. // This struct needs to have an Eigen aligned operator new as it contains
  80. // fixed-size Eigen types.
  81. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  82. explicit TinySolverCostFunctionAdapter(const CostFunction& cost_function)
  83. : cost_function_(cost_function) {
  84. CHECK_EQ(cost_function_.parameter_block_sizes().size(), 1)
  85. << "Only CostFunctions with exactly one parameter blocks are allowed.";
  86. const int parameter_block_size = cost_function_.parameter_block_sizes()[0];
  87. if (NUM_PARAMETERS == Eigen::Dynamic || NUM_RESIDUALS == Eigen::Dynamic) {
  88. if (NUM_RESIDUALS != Eigen::Dynamic) {
  89. CHECK_EQ(cost_function_.num_residuals(), NUM_RESIDUALS);
  90. }
  91. if (NUM_PARAMETERS != Eigen::Dynamic) {
  92. CHECK_EQ(parameter_block_size, NUM_PARAMETERS);
  93. }
  94. row_major_jacobian_.resize(cost_function_.num_residuals(),
  95. parameter_block_size);
  96. }
  97. }
  98. bool operator()(const double* parameters,
  99. double* residuals,
  100. double* jacobian) const {
  101. if (!jacobian) {
  102. return cost_function_.Evaluate(&parameters, residuals, nullptr);
  103. }
  104. double* jacobians[1] = {row_major_jacobian_.data()};
  105. if (!cost_function_.Evaluate(&parameters, residuals, jacobians)) {
  106. return false;
  107. }
  108. // The Function object used by TinySolver takes its Jacobian in a
  109. // column-major layout, and the CostFunction objects use row-major
  110. // Jacobian matrices. So the following bit of code does the
  111. // conversion from row-major Jacobians to column-major Jacobians.
  112. Eigen::Map<Eigen::Matrix<double, NUM_RESIDUALS, NUM_PARAMETERS>>
  113. col_major_jacobian(jacobian, NumResiduals(), NumParameters());
  114. col_major_jacobian = row_major_jacobian_;
  115. return true;
  116. }
  117. int NumResiduals() const { return cost_function_.num_residuals(); }
  118. int NumParameters() const {
  119. return cost_function_.parameter_block_sizes()[0];
  120. }
  121. private:
  122. const CostFunction& cost_function_;
  123. mutable Eigen::Matrix<double, NUM_RESIDUALS, NUM_PARAMETERS, Eigen::RowMajor>
  124. row_major_jacobian_;
  125. };
  126. } // namespace ceres
  127. #endif // CERES_PUBLIC_TINY_SOLVER_COST_FUNCTION_ADAPTER_H_