conditioned_cost_function_test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // Tests for the conditioned cost function.
  32. #include "ceres/conditioned_cost_function.h"
  33. #include "ceres/internal/eigen.h"
  34. #include "ceres/normal_prior.h"
  35. #include "ceres/types.h"
  36. #include "gtest/gtest.h"
  37. namespace ceres {
  38. namespace internal {
  39. // The size of the cost functions we build.
  40. static constexpr int kTestCostFunctionSize = 3;
  41. // A simple cost function: return ax + b.
  42. class LinearCostFunction : public CostFunction {
  43. public:
  44. LinearCostFunction(double a, double b) : a_(a), b_(b) {
  45. set_num_residuals(1);
  46. mutable_parameter_block_sizes()->push_back(1);
  47. }
  48. bool Evaluate(double const* const* parameters,
  49. double* residuals,
  50. double** jacobians) const final {
  51. *residuals = **parameters * a_ + b_;
  52. if (jacobians && *jacobians) {
  53. **jacobians = a_;
  54. }
  55. return true;
  56. }
  57. private:
  58. const double a_, b_;
  59. };
  60. // Tests that ConditionedCostFunction does what it's supposed to.
  61. TEST(ConditionedCostFunction, NormalOperation) {
  62. double v1[kTestCostFunctionSize], v2[kTestCostFunctionSize],
  63. jac[kTestCostFunctionSize * kTestCostFunctionSize],
  64. result[kTestCostFunctionSize];
  65. for (int i = 0; i < kTestCostFunctionSize; i++) {
  66. v1[i] = i;
  67. v2[i] = i * 10;
  68. // Seed a few garbage values in the Jacobian matrix, to make sure that
  69. // they're overwritten.
  70. jac[i * 2] = i * i;
  71. result[i] = i * i * i;
  72. }
  73. // Make a cost function that computes x - v2
  74. VectorRef v2_vector(v2, kTestCostFunctionSize, 1);
  75. Matrix identity(kTestCostFunctionSize, kTestCostFunctionSize);
  76. identity.setIdentity();
  77. auto* difference_cost_function = new NormalPrior(identity, v2_vector);
  78. std::vector<CostFunction*> conditioners;
  79. for (int i = 0; i < kTestCostFunctionSize; i++) {
  80. conditioners.push_back(new LinearCostFunction(i + 2, i * 7));
  81. }
  82. ConditionedCostFunction conditioned_cost_function(
  83. difference_cost_function, conditioners, TAKE_OWNERSHIP);
  84. EXPECT_EQ(difference_cost_function->num_residuals(),
  85. conditioned_cost_function.num_residuals());
  86. EXPECT_EQ(difference_cost_function->parameter_block_sizes(),
  87. conditioned_cost_function.parameter_block_sizes());
  88. double* parameters[1];
  89. parameters[0] = v1;
  90. double* jacs[1];
  91. jacs[0] = jac;
  92. conditioned_cost_function.Evaluate(parameters, result, jacs);
  93. for (int i = 0; i < kTestCostFunctionSize; i++) {
  94. EXPECT_DOUBLE_EQ((i + 2) * (v1[i] - v2[i]) + i * 7, result[i]);
  95. }
  96. for (int i = 0; i < kTestCostFunctionSize; i++) {
  97. for (int j = 0; j < kTestCostFunctionSize; j++) {
  98. double actual = jac[i * kTestCostFunctionSize + j];
  99. if (i != j) {
  100. EXPECT_DOUBLE_EQ(0, actual);
  101. } else {
  102. EXPECT_DOUBLE_EQ(i + 2, actual);
  103. }
  104. }
  105. }
  106. }
  107. TEST(ConditionedCostFunction, SharedConditionersDoNotTriggerDoubleFree) {
  108. // Make a cost function that computes x - v2
  109. double v2[kTestCostFunctionSize];
  110. VectorRef v2_vector(v2, kTestCostFunctionSize, 1);
  111. Matrix identity =
  112. Matrix::Identity(kTestCostFunctionSize, kTestCostFunctionSize);
  113. auto* difference_cost_function = new NormalPrior(identity, v2_vector);
  114. CostFunction* conditioner = new LinearCostFunction(2, 7);
  115. std::vector<CostFunction*> conditioners;
  116. for (int i = 0; i < kTestCostFunctionSize; i++) {
  117. conditioners.push_back(conditioner);
  118. }
  119. ConditionedCostFunction conditioned_cost_function(
  120. difference_cost_function, conditioners, TAKE_OWNERSHIP);
  121. }
  122. } // namespace internal
  123. } // namespace ceres