normal_prior_test.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/normal_prior.h"
  31. #include <algorithm>
  32. #include <cstddef>
  33. #include <random>
  34. #include "ceres/internal/eigen.h"
  35. #include "gtest/gtest.h"
  36. namespace ceres {
  37. namespace internal {
  38. TEST(NormalPriorTest, ResidualAtRandomPosition) {
  39. std::mt19937 prng;
  40. std::uniform_real_distribution<double> distribution(-1.0, 1.0);
  41. auto randu = [&distribution, &prng] { return distribution(prng); };
  42. for (int num_rows = 1; num_rows < 5; ++num_rows) {
  43. for (int num_cols = 1; num_cols < 5; ++num_cols) {
  44. Vector b(num_cols);
  45. b.setRandom();
  46. Matrix A(num_rows, num_cols);
  47. A.setRandom();
  48. auto* x = new double[num_cols];
  49. std::generate_n(x, num_cols, randu);
  50. auto* jacobian = new double[num_rows * num_cols];
  51. Vector residuals(num_rows);
  52. NormalPrior prior(A, b);
  53. prior.Evaluate(&x, residuals.data(), &jacobian);
  54. // Compare the norm of the residual
  55. double residual_diff_norm =
  56. (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm();
  57. EXPECT_NEAR(residual_diff_norm, 0, 1e-10);
  58. // Compare the jacobians
  59. MatrixRef J(jacobian, num_rows, num_cols);
  60. double jacobian_diff_norm = (J - A).norm();
  61. EXPECT_NEAR(jacobian_diff_norm, 0.0, 1e-10);
  62. delete[] x;
  63. delete[] jacobian;
  64. }
  65. }
  66. }
  67. TEST(NormalPriorTest, ResidualAtRandomPositionNullJacobians) {
  68. std::mt19937 prng;
  69. std::uniform_real_distribution<double> distribution(-1.0, 1.0);
  70. auto randu = [&distribution, &prng] { return distribution(prng); };
  71. for (int num_rows = 1; num_rows < 5; ++num_rows) {
  72. for (int num_cols = 1; num_cols < 5; ++num_cols) {
  73. Vector b(num_cols);
  74. b.setRandom();
  75. Matrix A(num_rows, num_cols);
  76. A.setRandom();
  77. auto* x = new double[num_cols];
  78. std::generate_n(x, num_cols, randu);
  79. double* jacobians[1];
  80. jacobians[0] = nullptr;
  81. Vector residuals(num_rows);
  82. NormalPrior prior(A, b);
  83. prior.Evaluate(&x, residuals.data(), jacobians);
  84. // Compare the norm of the residual
  85. double residual_diff_norm =
  86. (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm();
  87. EXPECT_NEAR(residual_diff_norm, 0, 1e-10);
  88. prior.Evaluate(&x, residuals.data(), nullptr);
  89. // Compare the norm of the residual
  90. residual_diff_norm =
  91. (residuals - A * (VectorRef(x, num_cols) - b)).squaredNorm();
  92. EXPECT_NEAR(residual_diff_norm, 0, 1e-10);
  93. delete[] x;
  94. }
  95. }
  96. }
  97. } // namespace internal
  98. } // namespace ceres