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


sophus
Author(s): Hauke Strasdat
autogenerated on Wed Mar 2 2022 01:01:48