manifold_test_utils.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 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 <cmath>
  31. #include <limits>
  32. #include <memory>
  33. #include "ceres/dynamic_numeric_diff_cost_function.h"
  34. #include "ceres/internal/eigen.h"
  35. #include "ceres/manifold.h"
  36. #include "ceres/numeric_diff_options.h"
  37. #include "ceres/types.h"
  38. #include "gmock/gmock.h"
  39. #include "gtest/gtest.h"
  40. namespace ceres {
  41. // Matchers and macros to simplify testing of custom Manifold objects using the
  42. // gtest testing framework.
  43. //
  44. // Testing a Manifold has two parts.
  45. //
  46. // 1. Checking that Manifold::Plus() and Manifold::Minus() are correctly
  47. // defined. This requires per manifold tests.
  48. //
  49. // 2. The other methods of the manifold have mathematical properties that make
  50. // them compatible with Plus() and Minus(), as described in [1].
  51. //
  52. // To verify these general requirements for a custom Manifold, use the
  53. // EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD() macro from within a gtest test. Note
  54. // that additional domain-specific tests may also be prudent, e.g to verify the
  55. // behaviour of a Quaternion Manifold about pi.
  56. //
  57. // [1] "Integrating Generic Sensor Fusion Algorithms with Sound State
  58. // Representations through Encapsulation of Manifolds", C. Hertzberg,
  59. // R. Wagner, U. Frese and L. Schroder, https://arxiv.org/pdf/1107.1119.pdf
  60. // Verifies the general requirements for a custom Manifold are satisfied to
  61. // within the specified (numerical) tolerance.
  62. //
  63. // Example usage for a custom Manifold: ExampleManifold:
  64. //
  65. // TEST(ExampleManifold, ManifoldInvariantsHold) {
  66. // constexpr double kTolerance = 1.0e-9;
  67. // ExampleManifold manifold;
  68. // ceres::Vector x = ceres::Vector::Zero(manifold.AmbientSize());
  69. // ceres::Vector y = ceres::Vector::Zero(manifold.AmbientSize());
  70. // ceres::Vector delta = ceres::Vector::Zero(manifold.TangentSize());
  71. // EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, kTolerance);
  72. // }
  73. #define EXPECT_THAT_MANIFOLD_INVARIANTS_HOLD(manifold, x, delta, y, tolerance) \
  74. ::ceres::Vector zero_tangent = \
  75. ::ceres::Vector::Zero(manifold.TangentSize()); \
  76. EXPECT_THAT(manifold, ::ceres::XPlusZeroIsXAt(x, tolerance)); \
  77. EXPECT_THAT(manifold, ::ceres::XMinusXIsZeroAt(x, tolerance)); \
  78. EXPECT_THAT(manifold, ::ceres::MinusPlusIsIdentityAt(x, delta, tolerance)); \
  79. EXPECT_THAT(manifold, \
  80. ::ceres::MinusPlusIsIdentityAt(x, zero_tangent, tolerance)); \
  81. EXPECT_THAT(manifold, ::ceres::PlusMinusIsIdentityAt(x, x, tolerance)); \
  82. EXPECT_THAT(manifold, ::ceres::PlusMinusIsIdentityAt(x, y, tolerance)); \
  83. EXPECT_THAT(manifold, ::ceres::HasCorrectPlusJacobianAt(x, tolerance)); \
  84. EXPECT_THAT(manifold, ::ceres::HasCorrectMinusJacobianAt(x, tolerance)); \
  85. EXPECT_THAT(manifold, ::ceres::MinusPlusJacobianIsIdentityAt(x, tolerance)); \
  86. EXPECT_THAT(manifold, \
  87. ::ceres::HasCorrectRightMultiplyByPlusJacobianAt(x, tolerance));
  88. // Checks that the invariant Plus(x, 0) == x holds.
  89. MATCHER_P2(XPlusZeroIsXAt, x, tolerance, "") {
  90. const int ambient_size = arg.AmbientSize();
  91. const int tangent_size = arg.TangentSize();
  92. Vector actual = Vector::Zero(ambient_size);
  93. Vector zero = Vector::Zero(tangent_size);
  94. EXPECT_TRUE(arg.Plus(x.data(), zero.data(), actual.data()));
  95. const double n = (actual - x).norm();
  96. const double d = x.norm();
  97. const double diffnorm = (d == 0.0) ? n : (n / d);
  98. if (diffnorm > tolerance) {
  99. *result_listener << "\nexpected (x): " << x.transpose()
  100. << "\nactual: " << actual.transpose()
  101. << "\ndiffnorm: " << diffnorm;
  102. return false;
  103. }
  104. return true;
  105. }
  106. // Checks that the invariant Minus(x, x) == 0 holds.
  107. MATCHER_P2(XMinusXIsZeroAt, x, tolerance, "") {
  108. const int tangent_size = arg.TangentSize();
  109. Vector actual = Vector::Zero(tangent_size);
  110. EXPECT_TRUE(arg.Minus(x.data(), x.data(), actual.data()));
  111. const double diffnorm = actual.norm();
  112. if (diffnorm > tolerance) {
  113. *result_listener << "\nx: " << x.transpose() //
  114. << "\nexpected: 0 0 0"
  115. << "\nactual: " << actual.transpose()
  116. << "\ndiffnorm: " << diffnorm;
  117. return false;
  118. }
  119. return true;
  120. }
  121. // Helper struct to curry Plus(x, .) so that it can be numerically
  122. // differentiated.
  123. struct PlusFunctor {
  124. PlusFunctor(const Manifold& manifold, const double* x)
  125. : manifold(manifold), x(x) {}
  126. bool operator()(double const* const* parameters, double* x_plus_delta) const {
  127. return manifold.Plus(x, parameters[0], x_plus_delta);
  128. }
  129. const Manifold& manifold;
  130. const double* x;
  131. };
  132. // Checks that the output of PlusJacobian matches the one obtained by
  133. // numerically evaluating D_2 Plus(x,0).
  134. MATCHER_P2(HasCorrectPlusJacobianAt, x, tolerance, "") {
  135. const int ambient_size = arg.AmbientSize();
  136. const int tangent_size = arg.TangentSize();
  137. NumericDiffOptions options;
  138. options.ridders_relative_initial_step_size = 1e-4;
  139. DynamicNumericDiffCostFunction<PlusFunctor, RIDDERS> cost_function(
  140. new PlusFunctor(arg, x.data()), TAKE_OWNERSHIP, options);
  141. cost_function.AddParameterBlock(tangent_size);
  142. cost_function.SetNumResiduals(ambient_size);
  143. Vector zero = Vector::Zero(tangent_size);
  144. double* parameters[1] = {zero.data()};
  145. Vector x_plus_zero = Vector::Zero(ambient_size);
  146. Matrix expected = Matrix::Zero(ambient_size, tangent_size);
  147. double* jacobians[1] = {expected.data()};
  148. EXPECT_TRUE(
  149. cost_function.Evaluate(parameters, x_plus_zero.data(), jacobians));
  150. Matrix actual = Matrix::Random(ambient_size, tangent_size);
  151. EXPECT_TRUE(arg.PlusJacobian(x.data(), actual.data()));
  152. const double n = (actual - expected).norm();
  153. const double d = expected.norm();
  154. const double diffnorm = (d == 0.0) ? n : n / d;
  155. if (diffnorm > tolerance) {
  156. *result_listener << "\nx: " << x.transpose() << "\nexpected: \n"
  157. << expected << "\nactual:\n"
  158. << actual << "\ndiff:\n"
  159. << expected - actual << "\ndiffnorm : " << diffnorm;
  160. return false;
  161. }
  162. return true;
  163. }
  164. // Checks that the invariant Minus(Plus(x, delta), x) == delta holds.
  165. MATCHER_P3(MinusPlusIsIdentityAt, x, delta, tolerance, "") {
  166. const int ambient_size = arg.AmbientSize();
  167. const int tangent_size = arg.TangentSize();
  168. Vector x_plus_delta = Vector::Zero(ambient_size);
  169. EXPECT_TRUE(arg.Plus(x.data(), delta.data(), x_plus_delta.data()));
  170. Vector actual = Vector::Zero(tangent_size);
  171. EXPECT_TRUE(arg.Minus(x_plus_delta.data(), x.data(), actual.data()));
  172. const double n = (actual - delta).norm();
  173. const double d = delta.norm();
  174. const double diffnorm = (d == 0.0) ? n : (n / d);
  175. if (diffnorm > tolerance) {
  176. *result_listener << "\nx: " << x.transpose()
  177. << "\nexpected: " << delta.transpose()
  178. << "\nactual:" << actual.transpose()
  179. << "\ndiff:" << (delta - actual).transpose()
  180. << "\ndiffnorm: " << diffnorm;
  181. return false;
  182. }
  183. return true;
  184. }
  185. // Checks that the invariant Plus(Minus(y, x), x) == y holds.
  186. MATCHER_P3(PlusMinusIsIdentityAt, x, y, tolerance, "") {
  187. const int ambient_size = arg.AmbientSize();
  188. const int tangent_size = arg.TangentSize();
  189. Vector y_minus_x = Vector::Zero(tangent_size);
  190. EXPECT_TRUE(arg.Minus(y.data(), x.data(), y_minus_x.data()));
  191. Vector actual = Vector::Zero(ambient_size);
  192. EXPECT_TRUE(arg.Plus(x.data(), y_minus_x.data(), actual.data()));
  193. const double n = (actual - y).norm();
  194. const double d = y.norm();
  195. const double diffnorm = (d == 0.0) ? n : (n / d);
  196. if (diffnorm > tolerance) {
  197. *result_listener << "\nx: " << x.transpose()
  198. << "\nexpected: " << y.transpose()
  199. << "\nactual:" << actual.transpose()
  200. << "\ndiff:" << (y - actual).transpose()
  201. << "\ndiffnorm: " << diffnorm;
  202. return false;
  203. }
  204. return true;
  205. }
  206. // Helper struct to curry Minus(., x) so that it can be numerically
  207. // differentiated.
  208. struct MinusFunctor {
  209. MinusFunctor(const Manifold& manifold, const double* x)
  210. : manifold(manifold), x(x) {}
  211. bool operator()(double const* const* parameters, double* y_minus_x) const {
  212. return manifold.Minus(parameters[0], x, y_minus_x);
  213. }
  214. const Manifold& manifold;
  215. const double* x;
  216. };
  217. // Checks that the output of MinusJacobian matches the one obtained by
  218. // numerically evaluating D_1 Minus(x,x).
  219. MATCHER_P2(HasCorrectMinusJacobianAt, x, tolerance, "") {
  220. const int ambient_size = arg.AmbientSize();
  221. const int tangent_size = arg.TangentSize();
  222. Vector y = x;
  223. Vector y_minus_x = Vector::Zero(tangent_size);
  224. NumericDiffOptions options;
  225. options.ridders_relative_initial_step_size = 1e-4;
  226. DynamicNumericDiffCostFunction<MinusFunctor, RIDDERS> cost_function(
  227. new MinusFunctor(arg, x.data()), TAKE_OWNERSHIP, options);
  228. cost_function.AddParameterBlock(ambient_size);
  229. cost_function.SetNumResiduals(tangent_size);
  230. double* parameters[1] = {y.data()};
  231. Matrix expected = Matrix::Zero(tangent_size, ambient_size);
  232. double* jacobians[1] = {expected.data()};
  233. EXPECT_TRUE(cost_function.Evaluate(parameters, y_minus_x.data(), jacobians));
  234. Matrix actual = Matrix::Random(tangent_size, ambient_size);
  235. EXPECT_TRUE(arg.MinusJacobian(x.data(), actual.data()));
  236. const double n = (actual - expected).norm();
  237. const double d = expected.norm();
  238. const double diffnorm = (d == 0.0) ? n : (n / d);
  239. if (diffnorm > tolerance) {
  240. *result_listener << "\nx: " << x.transpose() << "\nexpected: \n"
  241. << expected << "\nactual:\n"
  242. << actual << "\ndiff:\n"
  243. << expected - actual << "\ndiffnorm: " << diffnorm;
  244. return false;
  245. }
  246. return true;
  247. }
  248. // Checks that D_delta Minus(Plus(x, delta), x) at delta = 0 is an identity
  249. // matrix.
  250. MATCHER_P2(MinusPlusJacobianIsIdentityAt, x, tolerance, "") {
  251. const int ambient_size = arg.AmbientSize();
  252. const int tangent_size = arg.TangentSize();
  253. Matrix plus_jacobian(ambient_size, tangent_size);
  254. EXPECT_TRUE(arg.PlusJacobian(x.data(), plus_jacobian.data()));
  255. Matrix minus_jacobian(tangent_size, ambient_size);
  256. EXPECT_TRUE(arg.MinusJacobian(x.data(), minus_jacobian.data()));
  257. const Matrix actual = minus_jacobian * plus_jacobian;
  258. const Matrix expected = Matrix::Identity(tangent_size, tangent_size);
  259. const double n = (actual - expected).norm();
  260. const double d = expected.norm();
  261. const double diffnorm = n / d;
  262. if (diffnorm > tolerance) {
  263. *result_listener << "\nx: " << x.transpose() << "\nexpected: \n"
  264. << expected << "\nactual:\n"
  265. << actual << "\ndiff:\n"
  266. << expected - actual << "\ndiffnorm: " << diffnorm;
  267. return false;
  268. }
  269. return true;
  270. }
  271. // Verify that the output of RightMultiplyByPlusJacobian is ambient_matrix *
  272. // plus_jacobian.
  273. MATCHER_P2(HasCorrectRightMultiplyByPlusJacobianAt, x, tolerance, "") {
  274. const int ambient_size = arg.AmbientSize();
  275. const int tangent_size = arg.TangentSize();
  276. constexpr int kMinNumRows = 0;
  277. constexpr int kMaxNumRows = 3;
  278. for (int num_rows = kMinNumRows; num_rows <= kMaxNumRows; ++num_rows) {
  279. Matrix plus_jacobian = Matrix::Random(ambient_size, tangent_size);
  280. EXPECT_TRUE(arg.PlusJacobian(x.data(), plus_jacobian.data()));
  281. Matrix ambient_matrix = Matrix::Random(num_rows, ambient_size);
  282. Matrix expected = ambient_matrix * plus_jacobian;
  283. Matrix actual = Matrix::Random(num_rows, tangent_size);
  284. EXPECT_TRUE(arg.RightMultiplyByPlusJacobian(
  285. x.data(), num_rows, ambient_matrix.data(), actual.data()));
  286. const double n = (actual - expected).norm();
  287. const double d = expected.norm();
  288. const double diffnorm = (d == 0.0) ? n : (n / d);
  289. if (diffnorm > tolerance) {
  290. *result_listener << "\nx: " << x.transpose() << "\nambient_matrix : \n"
  291. << ambient_matrix << "\nplus_jacobian : \n"
  292. << plus_jacobian << "\nexpected: \n"
  293. << expected << "\nactual:\n"
  294. << actual << "\ndiff:\n"
  295. << expected - actual << "\ndiffnorm : " << diffnorm;
  296. return false;
  297. }
  298. }
  299. return true;
  300. }
  301. } // namespace ceres