conditioned_cost_function.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: wjr@google.com (William Rucklidge)
  30. //
  31. // This file contains the implementation of the conditioned cost function.
  32. #include "ceres/conditioned_cost_function.h"
  33. #include <cstddef>
  34. #include "ceres/internal/eigen.h"
  35. #include "ceres/stl_util.h"
  36. #include "ceres/types.h"
  37. #include "glog/logging.h"
  38. namespace ceres {
  39. // This cost function has the same dimensions (parameters, residuals) as
  40. // the one it's wrapping.
  41. ConditionedCostFunction::ConditionedCostFunction(
  42. CostFunction* wrapped_cost_function,
  43. const std::vector<CostFunction*>& conditioners,
  44. Ownership ownership)
  45. : wrapped_cost_function_(wrapped_cost_function),
  46. conditioners_(conditioners),
  47. ownership_(ownership) {
  48. // Set up our dimensions.
  49. set_num_residuals(wrapped_cost_function_->num_residuals());
  50. *mutable_parameter_block_sizes() =
  51. wrapped_cost_function_->parameter_block_sizes();
  52. // Sanity-check the conditioners' dimensions.
  53. CHECK_EQ(wrapped_cost_function_->num_residuals(), conditioners_.size());
  54. for (int i = 0; i < wrapped_cost_function_->num_residuals(); i++) {
  55. if (conditioners[i]) {
  56. CHECK_EQ(1, conditioners[i]->num_residuals());
  57. CHECK_EQ(1, conditioners[i]->parameter_block_sizes().size());
  58. CHECK_EQ(1, conditioners[i]->parameter_block_sizes()[0]);
  59. }
  60. }
  61. }
  62. ConditionedCostFunction::~ConditionedCostFunction() {
  63. if (ownership_ == TAKE_OWNERSHIP) {
  64. STLDeleteUniqueContainerPointers(conditioners_.begin(),
  65. conditioners_.end());
  66. } else {
  67. wrapped_cost_function_.release();
  68. }
  69. }
  70. bool ConditionedCostFunction::Evaluate(double const* const* parameters,
  71. double* residuals,
  72. double** jacobians) const {
  73. bool success =
  74. wrapped_cost_function_->Evaluate(parameters, residuals, jacobians);
  75. if (!success) {
  76. return false;
  77. }
  78. for (int r = 0; r < wrapped_cost_function_->num_residuals(); r++) {
  79. // On output, we want to have
  80. // residuals[r] = conditioners[r](wrapped_residuals[r])
  81. // For parameter block i, column c,
  82. // jacobians[i][r*parameter_block_size_[i] + c] =
  83. // = d residual[r] / d parameters[i][c]
  84. // = conditioners[r]'(wrapped_residuals[r]) *
  85. // d wrapped_residuals[r] / d parameters[i][c]
  86. if (conditioners_[r]) {
  87. double conditioner_derivative;
  88. double* conditioner_derivative_pointer = &conditioner_derivative;
  89. double** conditioner_derivative_pointer2 =
  90. &conditioner_derivative_pointer;
  91. if (!jacobians) {
  92. conditioner_derivative_pointer2 = nullptr;
  93. }
  94. double unconditioned_residual = residuals[r];
  95. double* parameter_pointer = &unconditioned_residual;
  96. success = conditioners_[r]->Evaluate(
  97. &parameter_pointer, &residuals[r], conditioner_derivative_pointer2);
  98. if (!success) {
  99. return false;
  100. }
  101. if (jacobians) {
  102. for (int i = 0;
  103. i < wrapped_cost_function_->parameter_block_sizes().size();
  104. i++) {
  105. if (jacobians[i]) {
  106. int parameter_block_size =
  107. wrapped_cost_function_->parameter_block_sizes()[i];
  108. VectorRef jacobian_row(jacobians[i] + r * parameter_block_size,
  109. parameter_block_size,
  110. 1);
  111. jacobian_row *= conditioner_derivative;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return true;
  118. }
  119. } // namespace ceres