test_so2.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include <sophus/so2.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::SO2<double>>;
10 template class Map<Sophus::SO2<double> const>;
11 } // namespace Eigen
12 
13 namespace Sophus {
14 
15 template class SO2<double, Eigen::AutoAlign>;
16 template class SO2<float, Eigen::DontAlign>;
17 #if SOPHUS_CERES
18 template class SO2<ceres::Jet<double, 3>>;
19 #endif
20 
21 template <class Scalar>
22 class Tests {
23  public:
25  using Point = typename SO2<Scalar>::Point;
26  using Tangent = typename SO2<Scalar>::Tangent;
28 
29  Tests() {
30  so2_vec_.push_back(SO2Type::exp(Scalar(0.0)));
31  so2_vec_.push_back(SO2Type::exp(Scalar(0.2)));
32  so2_vec_.push_back(SO2Type::exp(Scalar(10.)));
33  so2_vec_.push_back(SO2Type::exp(Scalar(0.00001)));
34  so2_vec_.push_back(SO2Type::exp(kPi));
35  so2_vec_.push_back(SO2Type::exp(Scalar(0.2)) * SO2Type::exp(kPi) *
36  SO2Type::exp(Scalar(-0.2)));
37  so2_vec_.push_back(SO2Type::exp(Scalar(-0.3)) * SO2Type::exp(kPi) *
38  SO2Type::exp(Scalar(0.3)));
39 
40  tangent_vec_.push_back(Tangent(Scalar(0)));
41  tangent_vec_.push_back(Tangent(Scalar(1)));
42  tangent_vec_.push_back(Tangent(Scalar(kPi / 2.)));
43  tangent_vec_.push_back(Tangent(Scalar(-1)));
44  tangent_vec_.push_back(Tangent(Scalar(20)));
45  tangent_vec_.push_back(Tangent(Scalar(kPi / 2. + 0.0001)));
46 
47  point_vec_.push_back(Point(Scalar(1), Scalar(2)));
48  point_vec_.push_back(Point(Scalar(1), Scalar(-3)));
49  }
50 
51  void runAll() {
52  bool passed = testLieProperties();
53  passed &= testUnity();
54  passed &= testRawDataAcces();
55  passed &= testConstructors();
56  passed &= testFit();
57  processTestResult(passed);
58  }
59 
60  private:
63  return tests.doAllTestsPass();
64  }
65 
66  bool testUnity() {
67  bool passed = true;
68  // Test that the complex number magnitude stays close to one.
69  SO2Type current_q;
70  for (std::size_t i = 0; i < 1000; ++i) {
71  for (SO2Type const& q : so2_vec_) {
72  current_q *= q;
73  }
74  }
75  SOPHUS_TEST_APPROX(passed, current_q.unit_complex().norm(), Scalar(1),
76  Constants<Scalar>::epsilon(), "Magnitude drift");
77  return passed;
78  }
79 
81  bool passed = true;
82  Vector2<Scalar> raw = {0, 1};
83  Eigen::Map<SO2Type const> map_of_const_so2(raw.data());
84  SOPHUS_TEST_APPROX(passed, map_of_const_so2.unit_complex().eval(), raw,
86  SOPHUS_TEST_EQUAL(passed, map_of_const_so2.unit_complex().data(),
87  raw.data());
88  Eigen::Map<SO2Type const> const_shallow_copy = map_of_const_so2;
89  SOPHUS_TEST_EQUAL(passed, const_shallow_copy.unit_complex().eval(),
90  map_of_const_so2.unit_complex().eval());
91 
92  Vector2<Scalar> raw2 = {1, 0};
93  Eigen::Map<SO2Type> map_of_so2(raw.data());
94  map_of_so2.setComplex(raw2);
95  SOPHUS_TEST_APPROX(passed, map_of_so2.unit_complex().eval(), raw2,
97  SOPHUS_TEST_EQUAL(passed, map_of_so2.unit_complex().data(), raw.data());
98  SOPHUS_TEST_NEQ(passed, map_of_so2.unit_complex().data(), raw2.data());
99  Eigen::Map<SO2Type> shallow_copy = map_of_so2;
100  SOPHUS_TEST_EQUAL(passed, shallow_copy.unit_complex().eval(),
101  map_of_so2.unit_complex().eval());
102 
103  SO2Type const const_so2(raw2);
104  for (int i = 0; i < 2; ++i) {
105  SOPHUS_TEST_EQUAL(passed, const_so2.data()[i], raw2.data()[i]);
106  }
107 
108  SO2Type so2(raw2);
109  for (int i = 0; i < 2; ++i) {
110  so2.data()[i] = raw[i];
111  }
112 
113  for (int i = 0; i < 2; ++i) {
114  SOPHUS_TEST_EQUAL(passed, so2.data()[i], raw.data()[i]);
115  }
116  return passed;
117  }
118 
120  bool passed = true;
121  Matrix2<Scalar> R = so2_vec_.front().matrix();
122  SO2Type so2(R);
123  SOPHUS_TEST_APPROX(passed, R, so2.matrix(), Constants<Scalar>::epsilon());
124 
125  return passed;
126  }
127 
128  template <class S = Scalar>
130  bool passed = true;
131 
132  for (int i = 0; i < 100; ++i) {
134  SO2Type so2 = SO2Type::fitToSO2(R);
135  SO2Type so2_2 = SO2Type::fitToSO2(so2.matrix());
136 
137  SOPHUS_TEST_APPROX(passed, so2.matrix(), so2_2.matrix(),
139  }
140  return passed;
141  }
142 
143  template <class S = Scalar>
145  return true;
146  }
147 
148  std::vector<SO2Type, Eigen::aligned_allocator<SO2Type>> so2_vec_;
149  std::vector<Tangent, Eigen::aligned_allocator<Tangent>> tangent_vec_;
150  std::vector<Point, Eigen::aligned_allocator<Point>> point_vec_;
151 };
152 
153 int test_so2() {
154  using std::cerr;
155  using std::endl;
156 
157  cerr << "Test SO2" << endl << endl;
158  cerr << "Double tests: " << endl;
159  Tests<double>().runAll();
160  cerr << "Float tests: " << endl;
161  Tests<float>().runAll();
162 
163 #if SOPHUS_CERES
164  cerr << "ceres::Jet<double, 3> tests: " << endl;
165  Tests<ceres::Jet<double, 3>>().runAll();
166 #endif
167 
168  return 0;
169 }
170 } // namespace Sophus
171 
172 int main() { return Sophus::test_so2(); }
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::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
Sophus::Vector2
Vector< Scalar, 2, Options > Vector2
Definition: types.hpp:16
Sophus::LieGroupTests
Definition: tests.hpp:21
Sophus::Tests::Tangent
typename RxSO2< Scalar >::Tangent Tangent
Definition: test_rxso2.cpp:27
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::processTestResult
void processTestResult(bool passed)
Definition: test_macros.hpp:38
Sophus::Tests::testConstructors
bool testConstructors()
Definition: test_rxso2.cpp:154
Sophus::Tests::Tests
Tests()
Definition: test_so2.cpp:29
Sophus::SO2::fitToSO2
static SOPHUS_FUNC enable_if_t< std::is_floating_point< S >::value, SO2 > fitToSO2(Transformation const &R)
Definition: so2.hpp:485
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
Sophus::Tests::testUnity
bool testUnity()
Definition: test_so2.cpp:66
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_so2.cpp:144
SOPHUS_TEST_NEQ
#define SOPHUS_TEST_NEQ(passed, left, right,...)
Definition: test_macros.hpp:81
Sophus::Constants
Definition: common.hpp:146
Sophus::SO2::Tangent
typename Base::Tangent Tangent
Definition: so2.hpp:344
Sophus::Tests::testRawDataAcces
bool testRawDataAcces()
Definition: test_rxso2.cpp:118
Sophus::Tests::so2_vec_
std::vector< SO2Type, Eigen::aligned_allocator< SO2Type > > so2_vec_
Definition: test_so2.cpp:148
tests.hpp
Sophus::Tests::runAll
void runAll()
Definition: test_so2.cpp:51
Sophus::test_so2
int test_so2()
Definition: test_so2.cpp:153
main
int main()
Definition: test_so2.cpp:172
Sophus::SO2::Point
typename Base::Point Point
Definition: so2.hpp:342
so2.hpp
Sophus::SO2::unit_complex
SOPHUS_FUNC ComplexMember const & unit_complex() const
Definition: so2.hpp:408


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