residual_block.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 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. // keir@google.com (Keir Mierle)
  31. //
  32. // Purpose : Class and struct definitions for parameter and residual blocks.
  33. #ifndef CERES_INTERNAL_RESIDUAL_BLOCK_H_
  34. #define CERES_INTERNAL_RESIDUAL_BLOCK_H_
  35. #include <cstdint>
  36. #include <memory>
  37. #include <string>
  38. #include <vector>
  39. #include "ceres/cost_function.h"
  40. #include "ceres/internal/disable_warnings.h"
  41. #include "ceres/internal/export.h"
  42. #include "ceres/stringprintf.h"
  43. #include "ceres/types.h"
  44. namespace ceres {
  45. class LossFunction;
  46. namespace internal {
  47. class ParameterBlock;
  48. // A term in the least squares problem. The mathematical form of each term in
  49. // the overall least-squares cost function is:
  50. //
  51. // 1
  52. // --- loss_function( || cost_function(block1, block2, ...) ||^2 ),
  53. // 2
  54. //
  55. // Storing the cost function and the loss function separately permits optimizing
  56. // the problem with standard non-linear least techniques, without requiring a
  57. // more general non-linear solver.
  58. //
  59. // The residual block stores pointers to but does not own the cost functions,
  60. // loss functions, and parameter blocks.
  61. class CERES_NO_EXPORT ResidualBlock {
  62. public:
  63. // Construct the residual block with the given cost/loss functions. Loss may
  64. // be null. The index is the index of the residual block in the Program's
  65. // residual_blocks array.
  66. ResidualBlock(const CostFunction* cost_function,
  67. const LossFunction* loss_function,
  68. const std::vector<ParameterBlock*>& parameter_blocks,
  69. int index);
  70. // Evaluates the residual term, storing the scalar cost in *cost, the residual
  71. // components in *residuals, and the jacobians between the parameters and
  72. // residuals in jacobians[i], in row-major order. If residuals is nullptr, the
  73. // residuals are not computed. If jacobians is nullptr, no jacobians are
  74. // computed. If jacobians[i] is nullptr, then the jacobian for that parameter
  75. // is not computed.
  76. //
  77. // cost must not be null.
  78. //
  79. // Evaluate needs scratch space which must be supplied by the caller via
  80. // scratch. The array should have at least NumScratchDoublesForEvaluate()
  81. // space available.
  82. //
  83. // The return value indicates the success or failure. If the function returns
  84. // false, the caller should expect the output memory locations to have
  85. // been modified.
  86. //
  87. // The returned cost and jacobians have had robustification and manifold
  88. // projection applied already; for example, the jacobian for a 4-dimensional
  89. // quaternion parameter using the "Quaternion" manifold is num_residuals by 3
  90. // instead of num_residuals by 4.
  91. //
  92. // apply_loss_function as the name implies allows the user to switch
  93. // the application of the loss function on and off.
  94. bool Evaluate(bool apply_loss_function,
  95. double* cost,
  96. double* residuals,
  97. double** jacobians,
  98. double* scratch) const;
  99. const CostFunction* cost_function() const { return cost_function_; }
  100. const LossFunction* loss_function() const { return loss_function_; }
  101. // Access the parameter blocks for this residual. The array has size
  102. // NumParameterBlocks().
  103. ParameterBlock* const* parameter_blocks() const {
  104. return parameter_blocks_.get();
  105. }
  106. // Number of variable blocks that this residual term depends on.
  107. int NumParameterBlocks() const {
  108. return cost_function_->parameter_block_sizes().size();
  109. }
  110. // The size of the residual vector returned by this residual function.
  111. int NumResiduals() const { return cost_function_->num_residuals(); }
  112. // The minimum amount of scratch space needed to pass to Evaluate().
  113. int NumScratchDoublesForEvaluate() const;
  114. // This residual block's index in an array.
  115. int index() const { return index_; }
  116. void set_index(int index) { index_ = index; }
  117. std::string ToString() const {
  118. return StringPrintf("{residual block; index=%d}", index_);
  119. }
  120. private:
  121. const CostFunction* cost_function_;
  122. const LossFunction* loss_function_;
  123. std::unique_ptr<ParameterBlock*[]> parameter_blocks_;
  124. // The index of the residual, typically in a Program. This is only to permit
  125. // switching from a ResidualBlock* to an index in the Program's array, needed
  126. // to do efficient removals.
  127. int32_t index_;
  128. };
  129. } // namespace internal
  130. } // namespace ceres
  131. #include "ceres/internal/reenable_warnings.h"
  132. #endif // CERES_INTERNAL_RESIDUAL_BLOCK_H_