test_sim2.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <iostream>
  2. #include <unsupported/Eigen/MatrixFunctions>
  3. #include <sophus/sim2.hpp>
  4. #include "tests.hpp"
  5. // Explicit instantiate all class templates so that all member methods
  6. // get compiled and for code coverage analysis.
  7. namespace Eigen {
  8. template class Map<Sophus::Sim2<double>>;
  9. template class Map<Sophus::Sim2<double> const>;
  10. } // namespace Eigen
  11. namespace Sophus {
  12. template class Sim2<double, Eigen::AutoAlign>;
  13. template class Sim2<float, Eigen::DontAlign>;
  14. #if SOPHUS_CERES
  15. template class Sim2<ceres::Jet<double, 3>>;
  16. #endif
  17. template <class Scalar>
  18. class Tests {
  19. public:
  20. using Sim2Type = Sim2<Scalar>;
  21. using RxSO2Type = RxSO2<Scalar>;
  22. using Point = typename Sim2<Scalar>::Point;
  23. using Vector2Type = Vector2<Scalar>;
  24. using Tangent = typename Sim2<Scalar>::Tangent;
  25. Scalar const kPi = Constants<Scalar>::pi();
  26. Tests() {
  27. sim2_vec_.push_back(
  28. Sim2Type(RxSO2Type::exp(Vector2Type(0.2, 1.)), Point(0, 0)));
  29. sim2_vec_.push_back(
  30. Sim2Type(RxSO2Type::exp(Vector2Type(0.2, 1.1)), Point(10, 0)));
  31. sim2_vec_.push_back(
  32. Sim2Type(RxSO2Type::exp(Vector2Type(0., 0.)), Point(0, 10)));
  33. sim2_vec_.push_back(
  34. Sim2Type(RxSO2Type::exp(Vector2Type(0.00001, 0.)), Point(0, 0)));
  35. sim2_vec_.push_back(
  36. Sim2Type(RxSO2Type::exp(Vector2Type(0.00001, 0.0000001)),
  37. Point(1, -1.00000001)));
  38. sim2_vec_.push_back(
  39. Sim2Type(RxSO2Type::exp(Vector2Type(0., 0.)), Point(0.01, 0)));
  40. sim2_vec_.push_back(
  41. Sim2Type(RxSO2Type::exp(Vector2Type(kPi, 0.9)), Point(4, 0)));
  42. sim2_vec_.push_back(
  43. Sim2Type(RxSO2Type::exp(Vector2Type(0.2, 0)), Point(0, 0)) *
  44. Sim2Type(RxSO2Type::exp(Vector2Type(kPi, 0)), Point(0, 0)) *
  45. Sim2Type(RxSO2Type::exp(Vector2Type(-0.2, 0)), Point(0, 0)));
  46. sim2_vec_.push_back(
  47. Sim2Type(RxSO2Type::exp(Vector2Type(0.3, 0)), Point(2, -7)) *
  48. Sim2Type(RxSO2Type::exp(Vector2Type(kPi, 0)), Point(0, 0)) *
  49. Sim2Type(RxSO2Type::exp(Vector2Type(-0.3, 0)), Point(0, 6)));
  50. Tangent tmp;
  51. tmp << Scalar(0), Scalar(0), Scalar(0), Scalar(0);
  52. tangent_vec_.push_back(tmp);
  53. tmp << Scalar(1), Scalar(0), Scalar(0), Scalar(0);
  54. tangent_vec_.push_back(tmp);
  55. tmp << Scalar(0), Scalar(1), Scalar(0), Scalar(0.1);
  56. tangent_vec_.push_back(tmp);
  57. tmp << Scalar(-1), Scalar(1), Scalar(1), Scalar(-0.1);
  58. tangent_vec_.push_back(tmp);
  59. tmp << Scalar(20), Scalar(-1), Scalar(0), Scalar(-0.1);
  60. tangent_vec_.push_back(tmp);
  61. tmp << Scalar(30), Scalar(5), Scalar(-1), Scalar(1.5);
  62. tangent_vec_.push_back(tmp);
  63. point_vec_.push_back(Point(Scalar(1), Scalar(4)));
  64. point_vec_.push_back(Point(Scalar(1), Scalar(-3)));
  65. }
  66. void runAll() {
  67. bool passed = testLieProperties();
  68. passed &= testRawDataAcces();
  69. passed &= testConstructors();
  70. processTestResult(passed);
  71. }
  72. private:
  73. bool testLieProperties() {
  74. LieGroupTests<Sim2Type> tests(sim2_vec_, tangent_vec_, point_vec_);
  75. return tests.doAllTestsPass();
  76. }
  77. bool testRawDataAcces() {
  78. bool passed = true;
  79. Eigen::Matrix<Scalar, 4, 1> raw;
  80. raw << Scalar(0), Scalar(1), Scalar(3), Scalar(2);
  81. Eigen::Map<Sim2Type const> map_of_const_sim2(raw.data());
  82. SOPHUS_TEST_APPROX(passed, map_of_const_sim2.complex().eval(),
  83. raw.template head<2>().eval(),
  84. Constants<Scalar>::epsilon());
  85. SOPHUS_TEST_APPROX(passed, map_of_const_sim2.translation().eval(),
  86. raw.template tail<2>().eval(),
  87. Constants<Scalar>::epsilon());
  88. SOPHUS_TEST_EQUAL(passed, map_of_const_sim2.complex().data(), raw.data());
  89. SOPHUS_TEST_EQUAL(passed, map_of_const_sim2.translation().data(),
  90. raw.data() + 2);
  91. Eigen::Map<Sim2Type const> const_shallow_copy = map_of_const_sim2;
  92. SOPHUS_TEST_EQUAL(passed, const_shallow_copy.complex().eval(),
  93. map_of_const_sim2.complex().eval());
  94. SOPHUS_TEST_EQUAL(passed, const_shallow_copy.translation().eval(),
  95. map_of_const_sim2.translation().eval());
  96. Eigen::Matrix<Scalar, 4, 1> raw2;
  97. raw2 << Scalar(1), Scalar(0), Scalar(2), Scalar(1);
  98. Eigen::Map<Sim2Type> map_of_sim2(raw.data());
  99. Vector2<Scalar> z;
  100. z = raw2.template head<2>();
  101. map_of_sim2.setComplex(z);
  102. map_of_sim2.translation() = raw2.template tail<2>();
  103. SOPHUS_TEST_APPROX(passed, map_of_sim2.complex().eval(),
  104. raw2.template head<2>().eval(),
  105. Constants<Scalar>::epsilon());
  106. SOPHUS_TEST_APPROX(passed, map_of_sim2.translation().eval(),
  107. raw2.template tail<2>().eval(),
  108. Constants<Scalar>::epsilon());
  109. SOPHUS_TEST_EQUAL(passed, map_of_sim2.complex().data(), raw.data());
  110. SOPHUS_TEST_EQUAL(passed, map_of_sim2.translation().data(), raw.data() + 2);
  111. SOPHUS_TEST_NEQ(passed, map_of_sim2.complex().data(), z.data());
  112. Eigen::Map<Sim2Type> shallow_copy = map_of_sim2;
  113. SOPHUS_TEST_EQUAL(passed, shallow_copy.complex().eval(),
  114. map_of_sim2.complex().eval());
  115. SOPHUS_TEST_EQUAL(passed, shallow_copy.translation().eval(),
  116. map_of_sim2.translation().eval());
  117. Eigen::Map<Sim2Type> const const_map_of_sim3 = map_of_sim2;
  118. SOPHUS_TEST_EQUAL(passed, const_map_of_sim3.complex().eval(),
  119. map_of_sim2.complex().eval());
  120. SOPHUS_TEST_EQUAL(passed, const_map_of_sim3.translation().eval(),
  121. map_of_sim2.translation().eval());
  122. Sim2Type const const_sim2(z, raw2.template tail<2>().eval());
  123. for (int i = 0; i < 4; ++i) {
  124. SOPHUS_TEST_EQUAL(passed, const_sim2.data()[i], raw2.data()[i]);
  125. }
  126. Sim2Type se3(z, raw2.template tail<2>().eval());
  127. for (int i = 0; i < 4; ++i) {
  128. SOPHUS_TEST_EQUAL(passed, se3.data()[i], raw2.data()[i]);
  129. }
  130. for (int i = 0; i < 4; ++i) {
  131. SOPHUS_TEST_EQUAL(passed, se3.data()[i], raw.data()[i]);
  132. }
  133. Eigen::Matrix<Scalar, 4, 1> data1, data2;
  134. data1 << Scalar(0), Scalar(2), Scalar(1), Scalar(2);
  135. data2 << Scalar(2), Scalar(0), Scalar(2), Scalar(1);
  136. Eigen::Map<Sim2Type> map1(data1.data()), map2(data2.data());
  137. // map -> map assignment
  138. map2 = map1;
  139. SOPHUS_TEST_EQUAL(passed, map1.matrix(), map2.matrix());
  140. // map -> type assignment
  141. Sim2Type copy;
  142. copy = map1;
  143. SOPHUS_TEST_EQUAL(passed, map1.matrix(), copy.matrix());
  144. // type -> map assignment
  145. copy = Sim2Type(RxSO2Type::exp(Vector2Type(-1, 1)),
  146. Point(Scalar(10), Scalar(0)));
  147. map1 = copy;
  148. SOPHUS_TEST_EQUAL(passed, map1.matrix(), copy.matrix());
  149. return passed;
  150. }
  151. bool testConstructors() {
  152. bool passed = true;
  153. Eigen::Matrix<Scalar, 3, 3> I = Eigen::Matrix<Scalar, 3, 3>::Identity();
  154. SOPHUS_TEST_EQUAL(passed, Sim2Type().matrix(), I);
  155. Sim2Type sim2 = sim2_vec_.front();
  156. Point translation = sim2.translation();
  157. RxSO2Type rxso2 = sim2.rxso2();
  158. SOPHUS_TEST_APPROX(passed, Sim2Type(rxso2, translation).matrix(),
  159. sim2.matrix(), Constants<Scalar>::epsilon());
  160. SOPHUS_TEST_APPROX(passed, Sim2Type(rxso2.complex(), translation).matrix(),
  161. sim2.matrix(), Constants<Scalar>::epsilon());
  162. SOPHUS_TEST_APPROX(passed, Sim2Type(sim2.matrix()).matrix(), sim2.matrix(),
  163. Constants<Scalar>::epsilon());
  164. Scalar scale(1.2);
  165. sim2.setScale(scale);
  166. SOPHUS_TEST_APPROX(passed, scale, sim2.scale(),
  167. Constants<Scalar>::epsilon(), "setScale");
  168. sim2.setComplex(sim2_vec_[0].rxso2().complex());
  169. SOPHUS_TEST_APPROX(passed, sim2_vec_[0].rxso2().complex(),
  170. sim2_vec_[0].rxso2().complex(),
  171. Constants<Scalar>::epsilon(), "setComplex");
  172. return passed;
  173. }
  174. std::vector<Sim2Type, Eigen::aligned_allocator<Sim2Type>> sim2_vec_;
  175. std::vector<Tangent, Eigen::aligned_allocator<Tangent>> tangent_vec_;
  176. std::vector<Point, Eigen::aligned_allocator<Point>> point_vec_;
  177. };
  178. int test_sim3() {
  179. using std::cerr;
  180. using std::endl;
  181. cerr << "Test Sim2" << endl << endl;
  182. cerr << "Double tests: " << endl;
  183. Tests<double>().runAll();
  184. cerr << "Float tests: " << endl;
  185. Tests<float>().runAll();
  186. #if SOPHUS_CERES
  187. cerr << "ceres::Jet<double, 3> tests: " << endl;
  188. Tests<ceres::Jet<double, 3>>().runAll();
  189. #endif
  190. return 0;
  191. }
  192. } // namespace Sophus
  193. int main() { return Sophus::test_sim3(); }