tiny_solver_cost_function_adapter_test.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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: sameeragarwal@google.com (Sameer Agarwal)
  30. #include "ceres/tiny_solver_cost_function_adapter.h"
  31. #include <algorithm>
  32. #include <cmath>
  33. #include <memory>
  34. #include "ceres/cost_function.h"
  35. #include "ceres/sized_cost_function.h"
  36. #include "gtest/gtest.h"
  37. namespace ceres {
  38. class CostFunction2x3 : public SizedCostFunction<2, 3> {
  39. bool Evaluate(double const* const* parameters,
  40. double* residuals,
  41. double** jacobians) const final {
  42. double x = parameters[0][0];
  43. double y = parameters[0][1];
  44. double z = parameters[0][2];
  45. residuals[0] = x + 2 * y + 4 * z;
  46. residuals[1] = y * z;
  47. if (jacobians && jacobians[0]) {
  48. jacobians[0][0] = 1;
  49. jacobians[0][1] = 2;
  50. jacobians[0][2] = 4;
  51. jacobians[0][3 + 0] = 0;
  52. jacobians[0][3 + 1] = z;
  53. jacobians[0][3 + 2] = y;
  54. }
  55. return true;
  56. }
  57. };
  58. template <int kNumResiduals, int kNumParameters>
  59. void TestHelper() {
  60. std::unique_ptr<CostFunction> cost_function(new CostFunction2x3);
  61. using CostFunctionAdapter =
  62. TinySolverCostFunctionAdapter<kNumResiduals, kNumParameters>;
  63. CostFunctionAdapter cfa(*cost_function);
  64. EXPECT_EQ(CostFunctionAdapter::NUM_RESIDUALS, kNumResiduals);
  65. EXPECT_EQ(CostFunctionAdapter::NUM_PARAMETERS, kNumParameters);
  66. EXPECT_EQ(cfa.NumResiduals(), 2);
  67. EXPECT_EQ(cfa.NumParameters(), 3);
  68. Eigen::Matrix<double, 2, 1> actual_residuals, expected_residuals;
  69. Eigen::Matrix<double, 2, 3, Eigen::ColMajor> actual_jacobian;
  70. Eigen::Matrix<double, 2, 3, Eigen::RowMajor> expected_jacobian;
  71. double xyz[3] = {1.0, -1.0, 2.0};
  72. double* parameters[1] = {xyz};
  73. // Check that residual only evaluation works.
  74. cost_function->Evaluate(parameters, expected_residuals.data(), nullptr);
  75. cfa(xyz, actual_residuals.data(), nullptr);
  76. EXPECT_NEAR(
  77. (expected_residuals - actual_residuals).norm() / actual_residuals.norm(),
  78. 0.0,
  79. std::numeric_limits<double>::epsilon())
  80. << "\nExpected residuals: " << expected_residuals.transpose()
  81. << "\nActual residuals: " << actual_residuals.transpose();
  82. // Check that residual and jacobian evaluation works.
  83. double* jacobians[1] = {expected_jacobian.data()};
  84. cost_function->Evaluate(parameters, expected_residuals.data(), jacobians);
  85. cfa(xyz, actual_residuals.data(), actual_jacobian.data());
  86. EXPECT_NEAR(
  87. (expected_residuals - actual_residuals).norm() / actual_residuals.norm(),
  88. 0.0,
  89. std::numeric_limits<double>::epsilon())
  90. << "\nExpected residuals: " << expected_residuals.transpose()
  91. << "\nActual residuals: " << actual_residuals.transpose();
  92. EXPECT_NEAR(
  93. (expected_jacobian - actual_jacobian).norm() / actual_jacobian.norm(),
  94. 0.0,
  95. std::numeric_limits<double>::epsilon())
  96. << "\nExpected jacobian: " << expected_jacobian.transpose()
  97. << "\nActual jacobian: " << actual_jacobian.transpose();
  98. }
  99. TEST(TinySolverCostFunctionAdapter, StaticResidualsStaticParameterBlock) {
  100. TestHelper<2, 3>();
  101. }
  102. TEST(TinySolverCostFunctionAdapter, DynamicResidualsStaticParameterBlock) {
  103. TestHelper<Eigen::Dynamic, 3>();
  104. }
  105. TEST(TinySolverCostFunctionAdapter, StaticResidualsDynamicParameterBlock) {
  106. TestHelper<2, Eigen::Dynamic>();
  107. }
  108. TEST(TinySolverCostFunctionAdapter, DynamicResidualsDynamicParameterBlock) {
  109. TestHelper<Eigen::Dynamic, Eigen::Dynamic>();
  110. }
  111. } // namespace ceres