conditioned_cost_function.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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: wjr@google.com (William Rucklidge)
  30. //
  31. // This file contains a cost function that can apply a transformation to
  32. // each residual value before they are square-summed.
  33. #ifndef CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_
  34. #define CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_
  35. #include <memory>
  36. #include <vector>
  37. #include "ceres/cost_function.h"
  38. #include "ceres/internal/disable_warnings.h"
  39. #include "ceres/types.h"
  40. namespace ceres {
  41. // This class allows you to apply different conditioning to the residual
  42. // values of a wrapped cost function. An example where this is useful is
  43. // where you have an existing cost function that produces N values, but you
  44. // want the total cost to be something other than just the sum of these
  45. // squared values - maybe you want to apply a different scaling to some
  46. // values, to change their contribution to the cost.
  47. //
  48. // Usage:
  49. //
  50. // // my_cost_function produces N residuals
  51. // CostFunction* my_cost_function = ...
  52. // CHECK_EQ(N, my_cost_function->num_residuals());
  53. // vector<CostFunction*> conditioners;
  54. //
  55. // // Make N 1x1 cost functions (1 parameter, 1 residual)
  56. // CostFunction* f_1 = ...
  57. // conditioners.push_back(f_1);
  58. // ...
  59. // CostFunction* f_N = ...
  60. // conditioners.push_back(f_N);
  61. // ConditionedCostFunction* ccf =
  62. // new ConditionedCostFunction(my_cost_function, conditioners);
  63. //
  64. // Now ccf's residual i (i=0..N-1) will be passed though the i'th conditioner.
  65. //
  66. // ccf_residual[i] = f_i(my_cost_function_residual[i])
  67. //
  68. // and the Jacobian will be affected appropriately.
  69. class CERES_EXPORT ConditionedCostFunction final : public CostFunction {
  70. public:
  71. // Builds a cost function based on a wrapped cost function, and a
  72. // per-residual conditioner. Takes ownership of all of the wrapped cost
  73. // functions, or not, depending on the ownership parameter. Conditioners
  74. // may be nullptr, in which case the corresponding residual is not modified.
  75. //
  76. // The conditioners can repeat.
  77. ConditionedCostFunction(CostFunction* wrapped_cost_function,
  78. const std::vector<CostFunction*>& conditioners,
  79. Ownership ownership);
  80. ~ConditionedCostFunction() override;
  81. bool Evaluate(double const* const* parameters,
  82. double* residuals,
  83. double** jacobians) const override;
  84. private:
  85. std::unique_ptr<CostFunction> wrapped_cost_function_;
  86. std::vector<CostFunction*> conditioners_;
  87. Ownership ownership_;
  88. };
  89. } // namespace ceres
  90. #include "ceres/internal/reenable_warnings.h"
  91. #endif // CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_