test_rxso2.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include <iostream>
  2. #include <sophus/rxso2.hpp>
  3. #include "tests.hpp"
  4. // Explicit instantiate all class templates so that all member methods
  5. // get compiled and for code coverage analysis.
  6. namespace Eigen {
  7. template class Map<Sophus::RxSO2<double>>;
  8. template class Map<Sophus::RxSO2<double> const>;
  9. } // namespace Eigen
  10. namespace Sophus {
  11. template class RxSO2<double, Eigen::AutoAlign>;
  12. template class RxSO2<float, Eigen::DontAlign>;
  13. #if SOPHUS_CERES
  14. template class RxSO2<ceres::Jet<double, 3>>;
  15. #endif
  16. template <class Scalar>
  17. class Tests {
  18. public:
  19. using SO2Type = SO2<Scalar>;
  20. using RxSO2Type = RxSO2<Scalar>;
  21. using RotationMatrixType = typename SO2<Scalar>::Transformation;
  22. using Point = typename RxSO2<Scalar>::Point;
  23. using Tangent = typename RxSO2<Scalar>::Tangent;
  24. Scalar const kPi = Constants<Scalar>::pi();
  25. Tests() {
  26. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(0.2, 1.)));
  27. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(0.2, 1.1)));
  28. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(0., 1.1)));
  29. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(0.00001, 0.)));
  30. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(0.00001, 0.00001)));
  31. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(kPi, 0.9)));
  32. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(0.2, 0)) *
  33. RxSO2Type::exp(Tangent(kPi, 0.0)) *
  34. RxSO2Type::exp(Tangent(-0.2, 0)));
  35. rxso2_vec_.push_back(RxSO2Type::exp(Tangent(0.3, 0)) *
  36. RxSO2Type::exp(Tangent(kPi, 0.001)) *
  37. RxSO2Type::exp(Tangent(-0.3, 0)));
  38. Tangent tmp;
  39. tmp << Scalar(0), Scalar(0);
  40. tangent_vec_.push_back(tmp);
  41. tmp << Scalar(1), Scalar(0);
  42. tangent_vec_.push_back(tmp);
  43. tmp << Scalar(1), Scalar(0.1);
  44. tangent_vec_.push_back(tmp);
  45. tmp << Scalar(0), Scalar(0.1);
  46. tangent_vec_.push_back(tmp);
  47. tmp << Scalar(0), Scalar(-0.1);
  48. tangent_vec_.push_back(tmp);
  49. tmp << Scalar(-1), Scalar(-0.1);
  50. tangent_vec_.push_back(tmp);
  51. tmp << Scalar(20), Scalar(2);
  52. tangent_vec_.push_back(tmp);
  53. point_vec_.push_back(Point(Scalar(1), Scalar(4)));
  54. point_vec_.push_back(Point(Scalar(1), Scalar(-3)));
  55. }
  56. template <class S = Scalar>
  57. enable_if_t<std::is_floating_point<S>::value, bool> testFit() {
  58. bool passed = true;
  59. for (int i = 0; i < 10; ++i) {
  60. Matrix2<Scalar> M = Matrix2<Scalar>::Random();
  61. for (Scalar scale : {Scalar(0.01), Scalar(0.99), Scalar(1), Scalar(10)}) {
  62. Matrix2<Scalar> R = makeRotationMatrix(M);
  63. Matrix2<Scalar> sR = scale * R;
  64. SOPHUS_TEST(passed, isScaledOrthogonalAndPositive(sR),
  65. "isScaledOrthogonalAndPositive(sR): % *\n%", scale, R);
  66. Matrix2<Scalar> sR_cols_swapped;
  67. sR_cols_swapped << sR.col(1), sR.col(0);
  68. SOPHUS_TEST(passed, !isScaledOrthogonalAndPositive(sR_cols_swapped),
  69. "isScaledOrthogonalAndPositive(-sR): % *\n%", scale, R);
  70. }
  71. }
  72. return passed;
  73. }
  74. template <class S = Scalar>
  75. enable_if_t<!std::is_floating_point<S>::value, bool> testFit() {
  76. return true;
  77. }
  78. void runAll() {
  79. bool passed = testLieProperties();
  80. passed &= testSaturation();
  81. passed &= testRawDataAcces();
  82. passed &= testConstructors();
  83. passed &= testFit();
  84. processTestResult(passed);
  85. }
  86. private:
  87. bool testLieProperties() {
  88. LieGroupTests<RxSO2Type> tests(rxso2_vec_, tangent_vec_, point_vec_);
  89. return tests.doAllTestsPass();
  90. }
  91. bool testSaturation() {
  92. bool passed = true;
  93. RxSO2Type small1(Scalar(1.1) * Constants<Scalar>::epsilon(), SO2Type());
  94. RxSO2Type small2(Scalar(1.1) * Constants<Scalar>::epsilon(),
  95. SO2Type::exp(Constants<Scalar>::pi()));
  96. RxSO2Type saturated_product = small1 * small2;
  97. SOPHUS_TEST_APPROX(passed, saturated_product.scale(),
  98. Constants<Scalar>::epsilon(),
  99. Constants<Scalar>::epsilon());
  100. SOPHUS_TEST_APPROX(passed, saturated_product.so2().matrix(),
  101. (small1.so2() * small2.so2()).matrix(),
  102. Constants<Scalar>::epsilon());
  103. return passed;
  104. }
  105. bool testRawDataAcces() {
  106. bool passed = true;
  107. Eigen::Matrix<Scalar, 2, 1> raw = {0, 1};
  108. Eigen::Map<RxSO2Type const> map_of_const_rxso2(raw.data());
  109. SOPHUS_TEST_APPROX(passed, map_of_const_rxso2.complex().eval(), raw,
  110. Constants<Scalar>::epsilon());
  111. SOPHUS_TEST_EQUAL(passed, map_of_const_rxso2.complex().data(), raw.data());
  112. Eigen::Map<RxSO2Type const> const_shallow_copy = map_of_const_rxso2;
  113. SOPHUS_TEST_EQUAL(passed, const_shallow_copy.complex().eval(),
  114. map_of_const_rxso2.complex().eval());
  115. Eigen::Matrix<Scalar, 2, 1> raw2 = {1, 0};
  116. Eigen::Map<RxSO2Type> map_of_rxso2(raw2.data());
  117. SOPHUS_TEST_APPROX(passed, map_of_rxso2.complex().eval(), raw2,
  118. Constants<Scalar>::epsilon());
  119. SOPHUS_TEST_EQUAL(passed, map_of_rxso2.complex().data(), raw2.data());
  120. Eigen::Map<RxSO2Type> shallow_copy = map_of_rxso2;
  121. SOPHUS_TEST_EQUAL(passed, shallow_copy.complex().eval(),
  122. map_of_rxso2.complex().eval());
  123. RxSO2Type const const_so2(raw2);
  124. for (int i = 0; i < 2; ++i) {
  125. SOPHUS_TEST_EQUAL(passed, const_so2.data()[i], raw2.data()[i]);
  126. }
  127. RxSO2Type so2(raw2);
  128. for (int i = 0; i < 2; ++i) {
  129. so2.data()[i] = raw[i];
  130. }
  131. for (int i = 0; i < 2; ++i) {
  132. SOPHUS_TEST_EQUAL(passed, so2.data()[i], raw.data()[i]);
  133. }
  134. // regression: test that rotationMatrix API doesn't change underlying value
  135. // for non-const-map and compiles at all for const-map
  136. Eigen::Matrix<Scalar, 2, 1> raw3 = {Scalar(2), Scalar(0)};
  137. Eigen::Map<RxSO2Type> map_of_rxso2_3(raw3.data());
  138. Eigen::Map<const RxSO2Type> const_map_of_rxso2_3(raw3.data());
  139. RxSO2Type rxso2_copy3 = map_of_rxso2_3;
  140. const RotationMatrixType r_ref = map_of_rxso2_3.so2().matrix();
  141. const RotationMatrixType r = map_of_rxso2_3.rotationMatrix();
  142. SOPHUS_TEST_APPROX(passed, r_ref, r, Constants<Scalar>::epsilon());
  143. SOPHUS_TEST_APPROX(passed, map_of_rxso2_3.complex().eval(),
  144. rxso2_copy3.complex().eval(),
  145. Constants<Scalar>::epsilon());
  146. const RotationMatrixType r_const = const_map_of_rxso2_3.rotationMatrix();
  147. SOPHUS_TEST_APPROX(passed, r_ref, r_const, Constants<Scalar>::epsilon());
  148. SOPHUS_TEST_APPROX(passed, const_map_of_rxso2_3.complex().eval(),
  149. rxso2_copy3.complex().eval(),
  150. Constants<Scalar>::epsilon());
  151. Eigen::Matrix<Scalar, 2, 1> data1, data2;
  152. data1 << Scalar(.1), Scalar(.2);
  153. data2 << Scalar(.5), Scalar(.4);
  154. Eigen::Map<RxSO2Type> map1(data1.data()), map2(data2.data());
  155. // map -> map assignment
  156. map2 = map1;
  157. SOPHUS_TEST_EQUAL(passed, map1.matrix(), map2.matrix());
  158. // map -> type assignment
  159. RxSO2Type copy;
  160. copy = map1;
  161. SOPHUS_TEST_EQUAL(passed, map1.matrix(), copy.matrix());
  162. // type -> map assignment
  163. copy = RxSO2Type::exp(Tangent(Scalar(0.2), Scalar(0.5)));
  164. map1 = copy;
  165. SOPHUS_TEST_EQUAL(passed, map1.matrix(), copy.matrix());
  166. return passed;
  167. }
  168. bool testConstructors() {
  169. bool passed = true;
  170. RxSO2Type rxso2;
  171. Scalar scale(1.2);
  172. rxso2.setScale(scale);
  173. SOPHUS_TEST_APPROX(passed, scale, rxso2.scale(),
  174. Constants<Scalar>::epsilon(), "setScale");
  175. Scalar angle(0.2);
  176. rxso2.setAngle(angle);
  177. SOPHUS_TEST_APPROX(passed, angle, rxso2.angle(),
  178. Constants<Scalar>::epsilon(), "setAngle");
  179. SOPHUS_TEST_APPROX(passed, scale, rxso2.scale(),
  180. Constants<Scalar>::epsilon(),
  181. "setAngle leaves scale as is");
  182. auto so2 = rxso2_vec_[0].so2();
  183. rxso2.setSO2(so2);
  184. SOPHUS_TEST_APPROX(passed, scale, rxso2.scale(),
  185. Constants<Scalar>::epsilon(), "setSO2");
  186. SOPHUS_TEST_APPROX(passed, RxSO2Type(scale, so2).matrix(), rxso2.matrix(),
  187. Constants<Scalar>::epsilon(), "RxSO2(scale, SO2)");
  188. SOPHUS_TEST_APPROX(passed, RxSO2Type(scale, so2.matrix()).matrix(),
  189. rxso2.matrix(), Constants<Scalar>::epsilon(),
  190. "RxSO2(scale, SO2)");
  191. Matrix2<Scalar> R = SO2<Scalar>::exp(Scalar(0.2)).matrix();
  192. Matrix2<Scalar> sR = R * Scalar(1.3);
  193. SOPHUS_TEST_APPROX(passed, RxSO2Type(sR).matrix(), sR,
  194. Constants<Scalar>::epsilon(), "RxSO2(sR)");
  195. rxso2.setScaledRotationMatrix(sR);
  196. SOPHUS_TEST_APPROX(passed, sR, rxso2.matrix(), Constants<Scalar>::epsilon(),
  197. "setScaleRotationMatrix");
  198. rxso2.setScale(scale);
  199. rxso2.setRotationMatrix(R);
  200. SOPHUS_TEST_APPROX(passed, R, rxso2.rotationMatrix(),
  201. Constants<Scalar>::epsilon(), "setRotationMatrix");
  202. SOPHUS_TEST_APPROX(passed, scale, rxso2.scale(),
  203. Constants<Scalar>::epsilon(), "setScale");
  204. return passed;
  205. }
  206. std::vector<RxSO2Type, Eigen::aligned_allocator<RxSO2Type>> rxso2_vec_;
  207. std::vector<Tangent, Eigen::aligned_allocator<Tangent>> tangent_vec_;
  208. std::vector<Point, Eigen::aligned_allocator<Point>> point_vec_;
  209. };
  210. int test_rxso2() {
  211. using std::cerr;
  212. using std::endl;
  213. cerr << "Test RxSO2" << endl << endl;
  214. cerr << "Double tests: " << endl;
  215. Tests<double>().runAll();
  216. cerr << "Float tests: " << endl;
  217. Tests<float>().runAll();
  218. #if SOPHUS_CERES
  219. cerr << "ceres::Jet<double, 3> tests: " << endl;
  220. Tests<ceres::Jet<double, 3>>().runAll();
  221. #endif
  222. return 0;
  223. }
  224. } // namespace Sophus
  225. int main() { return Sophus::test_rxso2(); }