parameter_block_test.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2021 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: keir@google.com (Keir Mierle)
  30. #include "ceres/parameter_block.h"
  31. #include "ceres/internal/eigen.h"
  32. #include "gtest/gtest.h"
  33. namespace ceres {
  34. namespace internal {
  35. TEST(ParameterBlock, SetManifoldDiesOnSizeMismatch) {
  36. double x[3] = {1.0, 2.0, 3.0};
  37. ParameterBlock parameter_block(x, 3, -1);
  38. std::vector<int> indices;
  39. indices.push_back(1);
  40. SubsetManifold subset_wrong_size(4, indices);
  41. EXPECT_DEATH_IF_SUPPORTED(parameter_block.SetManifold(&subset_wrong_size),
  42. "ambient");
  43. }
  44. TEST(ParameterBlock, SetManifoldWithSameExistingManifold) {
  45. double x[3] = {1.0, 2.0, 3.0};
  46. ParameterBlock parameter_block(x, 3, -1);
  47. std::vector<int> indices;
  48. indices.push_back(1);
  49. SubsetManifold subset(3, indices);
  50. parameter_block.SetManifold(&subset);
  51. parameter_block.SetManifold(&subset);
  52. }
  53. TEST(ParameterBlock, SetManifoldAllowsResettingToNull) {
  54. double x[3] = {1.0, 2.0, 3.0};
  55. ParameterBlock parameter_block(x, 3, -1);
  56. std::vector<int> indices;
  57. indices.push_back(1);
  58. SubsetManifold subset(3, indices);
  59. parameter_block.SetManifold(&subset);
  60. EXPECT_EQ(parameter_block.manifold(), &subset);
  61. parameter_block.SetManifold(nullptr);
  62. EXPECT_EQ(parameter_block.manifold(), nullptr);
  63. EXPECT_EQ(parameter_block.PlusJacobian(), nullptr);
  64. }
  65. TEST(ParameterBlock, SetManifoldAllowsResettingToDifferentManifold) {
  66. double x[3] = {1.0, 2.0, 3.0};
  67. ParameterBlock parameter_block(x, 3, -1);
  68. std::vector<int> indices;
  69. indices.push_back(1);
  70. SubsetManifold subset(3, indices);
  71. parameter_block.SetManifold(&subset);
  72. EXPECT_EQ(parameter_block.manifold(), &subset);
  73. SubsetManifold subset_different(3, indices);
  74. parameter_block.SetManifold(&subset_different);
  75. EXPECT_EQ(parameter_block.manifold(), &subset_different);
  76. }
  77. TEST(ParameterBlock, SetManifoldAndNormalOperation) {
  78. double x[3] = {1.0, 2.0, 3.0};
  79. ParameterBlock parameter_block(x, 3, -1);
  80. std::vector<int> indices;
  81. indices.push_back(1);
  82. SubsetManifold subset(3, indices);
  83. parameter_block.SetManifold(&subset);
  84. // Ensure the manifold plus jacobian result is correctly computed.
  85. ConstMatrixRef manifold_jacobian(parameter_block.PlusJacobian(), 3, 2);
  86. ASSERT_EQ(1.0, manifold_jacobian(0, 0));
  87. ASSERT_EQ(0.0, manifold_jacobian(0, 1));
  88. ASSERT_EQ(0.0, manifold_jacobian(1, 0));
  89. ASSERT_EQ(0.0, manifold_jacobian(1, 1));
  90. ASSERT_EQ(0.0, manifold_jacobian(2, 0));
  91. ASSERT_EQ(1.0, manifold_jacobian(2, 1));
  92. // Check that updating works as expected.
  93. double x_plus_delta[3];
  94. double delta[2] = {0.5, 0.3};
  95. parameter_block.Plus(x, delta, x_plus_delta);
  96. ASSERT_EQ(1.5, x_plus_delta[0]);
  97. ASSERT_EQ(2.0, x_plus_delta[1]);
  98. ASSERT_EQ(3.3, x_plus_delta[2]);
  99. }
  100. struct TestManifold : public Manifold {
  101. public:
  102. bool Plus(const double* x,
  103. const double* delta,
  104. double* x_plus_delta) const final {
  105. LOG(FATAL) << "Shouldn't get called.";
  106. return true;
  107. }
  108. bool PlusJacobian(const double* x, double* jacobian) const final {
  109. jacobian[0] = *x * 2;
  110. return true;
  111. }
  112. bool Minus(const double* y, const double* x, double* y_minus_x) const final {
  113. LOG(FATAL) << "Shouldn't get called";
  114. return true;
  115. }
  116. bool MinusJacobian(const double* x, double* jacobian) const final {
  117. jacobian[0] = *x * 2;
  118. return true;
  119. }
  120. int AmbientSize() const final { return 1; }
  121. int TangentSize() const final { return 1; }
  122. };
  123. TEST(ParameterBlock, SetStateUpdatesPlusJacobian) {
  124. TestManifold test_manifold;
  125. double x[1] = {1.0};
  126. ParameterBlock parameter_block(x, 1, -1, &test_manifold);
  127. EXPECT_EQ(2.0, *parameter_block.PlusJacobian());
  128. x[0] = 5.5;
  129. parameter_block.SetState(x);
  130. EXPECT_EQ(11.0, *parameter_block.PlusJacobian());
  131. }
  132. TEST(ParameterBlock, PlusWithNoManifold) {
  133. double x[2] = {1.0, 2.0};
  134. ParameterBlock parameter_block(x, 2, -1);
  135. double delta[2] = {0.2, 0.3};
  136. double x_plus_delta[2];
  137. parameter_block.Plus(x, delta, x_plus_delta);
  138. EXPECT_EQ(1.2, x_plus_delta[0]);
  139. EXPECT_EQ(2.3, x_plus_delta[1]);
  140. }
  141. // Stops computing the plus_jacobian after the first time.
  142. class BadManifold : public Manifold {
  143. public:
  144. BadManifold() = default;
  145. bool Plus(const double* x,
  146. const double* delta,
  147. double* x_plus_delta) const final {
  148. *x_plus_delta = *x + *delta;
  149. return true;
  150. }
  151. bool PlusJacobian(const double* x, double* jacobian) const final {
  152. if (calls_ == 0) {
  153. jacobian[0] = 0;
  154. }
  155. ++calls_;
  156. return true;
  157. }
  158. bool Minus(const double* y, const double* x, double* y_minus_x) const final {
  159. LOG(FATAL) << "Shouldn't get called";
  160. return true;
  161. }
  162. bool MinusJacobian(const double* x, double* jacobian) const final {
  163. jacobian[0] = *x * 2;
  164. return true;
  165. }
  166. int AmbientSize() const final { return 1; }
  167. int TangentSize() const final { return 1; }
  168. private:
  169. mutable int calls_{0};
  170. };
  171. TEST(ParameterBlock, DetectBadManifold) {
  172. double x = 1;
  173. BadManifold bad_manifold;
  174. ParameterBlock parameter_block(&x, 1, -1, &bad_manifold);
  175. double y = 2;
  176. EXPECT_FALSE(parameter_block.SetState(&y));
  177. }
  178. TEST(ParameterBlock, DefaultBounds) {
  179. double x[2];
  180. ParameterBlock parameter_block(x, 2, -1, nullptr);
  181. EXPECT_EQ(parameter_block.UpperBoundForParameter(0),
  182. std::numeric_limits<double>::max());
  183. EXPECT_EQ(parameter_block.UpperBoundForParameter(1),
  184. std::numeric_limits<double>::max());
  185. EXPECT_EQ(parameter_block.LowerBoundForParameter(0),
  186. -std::numeric_limits<double>::max());
  187. EXPECT_EQ(parameter_block.LowerBoundForParameter(1),
  188. -std::numeric_limits<double>::max());
  189. }
  190. TEST(ParameterBlock, SetBounds) {
  191. double x[2];
  192. ParameterBlock parameter_block(x, 2, -1, nullptr);
  193. parameter_block.SetLowerBound(0, 1);
  194. parameter_block.SetUpperBound(1, 1);
  195. EXPECT_EQ(parameter_block.LowerBoundForParameter(0), 1.0);
  196. EXPECT_EQ(parameter_block.LowerBoundForParameter(1),
  197. -std::numeric_limits<double>::max());
  198. EXPECT_EQ(parameter_block.UpperBoundForParameter(0),
  199. std::numeric_limits<double>::max());
  200. EXPECT_EQ(parameter_block.UpperBoundForParameter(1), 1.0);
  201. }
  202. TEST(ParameterBlock, PlusWithBoundsConstraints) {
  203. double x[] = {1.0, 0.0};
  204. double delta[] = {2.0, -10.0};
  205. ParameterBlock parameter_block(x, 2, -1, nullptr);
  206. parameter_block.SetUpperBound(0, 2.0);
  207. parameter_block.SetLowerBound(1, -1.0);
  208. double x_plus_delta[2];
  209. parameter_block.Plus(x, delta, x_plus_delta);
  210. EXPECT_EQ(x_plus_delta[0], 2.0);
  211. EXPECT_EQ(x_plus_delta[1], -1.0);
  212. }
  213. TEST(ParameterBlock, ResetManifoldToNull) {
  214. double x[3] = {1.0, 2.0, 3.0};
  215. ParameterBlock parameter_block(x, 3, -1);
  216. std::vector<int> indices;
  217. indices.push_back(1);
  218. SubsetManifold subset(3, indices);
  219. parameter_block.SetManifold(&subset);
  220. EXPECT_EQ(parameter_block.manifold(), &subset);
  221. parameter_block.SetManifold(nullptr);
  222. EXPECT_EQ(parameter_block.manifold(), nullptr);
  223. }
  224. TEST(ParameterBlock, ResetManifoldToNotNull) {
  225. double x[3] = {1.0, 2.0, 3.0};
  226. ParameterBlock parameter_block(x, 3, -1);
  227. std::vector<int> indices;
  228. indices.push_back(1);
  229. SubsetManifold subset(3, indices);
  230. parameter_block.SetManifold(&subset);
  231. EXPECT_EQ(parameter_block.manifold(), &subset);
  232. SubsetManifold subset_different(3, indices);
  233. parameter_block.SetManifold(&subset_different);
  234. EXPECT_EQ(parameter_block.manifold(), &subset_different);
  235. }
  236. TEST(ParameterBlock, SetNullManifold) {
  237. double x[3] = {1.0, 2.0, 3.0};
  238. ParameterBlock parameter_block(x, 3, -1);
  239. EXPECT_EQ(parameter_block.manifold(), nullptr);
  240. parameter_block.SetManifold(nullptr);
  241. EXPECT_EQ(parameter_block.manifold(), nullptr);
  242. }
  243. } // namespace internal
  244. } // namespace ceres