test_geometry.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include <sophus/geometry.hpp>
4 #include <sophus/test_macros.hpp>
5 #include "tests.hpp"
6 
7 namespace Sophus {
8 
9 namespace {
10 
11 template <class T>
12 bool test2dGeometry() {
13  bool passed = true;
14  T const eps = Constants<T>::epsilon();
15 
16  for (int i = 0; i < 20; ++i) {
17  // Roundtrip test:
18  Vector2<T> normal_foo = Vector2<T>::Random().normalized();
19  Sophus::SO2<T> R_foo_plane = SO2FromNormal(normal_foo);
20  Vector2<T> resultNormal_foo = normalFromSO2(R_foo_plane);
21  SOPHUS_TEST_APPROX(passed, normal_foo, resultNormal_foo, eps);
22  }
23 
24  for (int i = 0; i < 20; ++i) {
25  // Roundtrip test:
26  Line2<T> line_foo = makeHyperplaneUnique(
27  Line2<T>(Vector2<T>::Random().normalized(), Vector2<T>::Random()));
28  Sophus::SE2<T> T_foo_plane = SE2FromLine(line_foo);
29  Line2<T> resultPlane_foo = lineFromSE2(T_foo_plane);
30  SOPHUS_TEST_APPROX(passed, line_foo.normal().eval(),
31  resultPlane_foo.normal().eval(), eps);
32  SOPHUS_TEST_APPROX(passed, line_foo.offset(), resultPlane_foo.offset(),
33  eps);
34  }
35 
36  std::vector<SE2<T>, Eigen::aligned_allocator<SE2<T>>> Ts_foo_line =
37  getTestSE2s<T>();
38 
39  for (SE2<T> const& T_foo_line : Ts_foo_line) {
40  Line2<T> line_foo = lineFromSE2(T_foo_line);
41  SE2<T> T2_foo_line = SE2FromLine(line_foo);
42  Line2<T> line2_foo = lineFromSE2(T2_foo_line);
43  SOPHUS_TEST_APPROX(passed, line_foo.normal().eval(),
44  line2_foo.normal().eval(), eps);
45  SOPHUS_TEST_APPROX(passed, line_foo.offset(), line2_foo.offset(), eps);
46  }
47 
48  return passed;
49 }
50 
51 template <class T>
52 bool test3dGeometry() {
53  bool passed = true;
54  T const eps = Constants<T>::epsilon();
55 
56  Vector3<T> normal_foo = Vector3<T>(1, 2, 0).normalized();
57  Matrix3<T> R_foo_plane = rotationFromNormal(normal_foo);
58  SOPHUS_TEST_APPROX(passed, normal_foo, R_foo_plane.col(2).eval(), eps);
59  // Just testing that the function normalizes the input normal and hint
60  // direction correctly:
61  Matrix3<T> R2_foo_plane = rotationFromNormal((T(0.9) * normal_foo).eval());
62  SOPHUS_TEST_APPROX(passed, normal_foo, R2_foo_plane.col(2).eval(), eps);
63  Matrix3<T> R3_foo_plane =
64  rotationFromNormal(normal_foo, Vector3<T>(T(0.9), T(0), T(0)),
65  Vector3<T>(T(0), T(1.1), T(0)));
66  SOPHUS_TEST_APPROX(passed, normal_foo, R3_foo_plane.col(2).eval(), eps);
67 
68  normal_foo = Vector3<T>(1, 0, 0);
69  R_foo_plane = rotationFromNormal(normal_foo);
70  SOPHUS_TEST_APPROX(passed, normal_foo, R_foo_plane.col(2).eval(), eps);
71  SOPHUS_TEST_APPROX(passed, Vector3<T>(0, 1, 0), R_foo_plane.col(1).eval(),
72  eps);
73 
74  normal_foo = Vector3<T>(0, 1, 0);
75  R_foo_plane = rotationFromNormal(normal_foo);
76  SOPHUS_TEST_APPROX(passed, normal_foo, R_foo_plane.col(2).eval(), eps);
77  SOPHUS_TEST_APPROX(passed, Vector3<T>(1, 0, 0), R_foo_plane.col(0).eval(),
78  eps);
79 
80  for (int i = 0; i < 20; ++i) {
81  // Roundtrip test:
82  Vector3<T> normal_foo = Vector3<T>::Random().normalized();
83  Sophus::SO3<T> R_foo_plane = SO3FromNormal(normal_foo);
84  Vector3<T> resultNormal_foo = normalFromSO3(R_foo_plane);
85  SOPHUS_TEST_APPROX(passed, normal_foo, resultNormal_foo, eps);
86  }
87 
88  for (int i = 0; i < 20; ++i) {
89  // Roundtrip test:
90  Plane3<T> plane_foo = makeHyperplaneUnique(
91  Plane3<T>(Vector3<T>::Random().normalized(), Vector3<T>::Random()));
92  Sophus::SE3<T> T_foo_plane = SE3FromPlane(plane_foo);
93  Plane3<T> resultPlane_foo = planeFromSE3(T_foo_plane);
94  SOPHUS_TEST_APPROX(passed, plane_foo.normal().eval(),
95  resultPlane_foo.normal().eval(), eps);
96  SOPHUS_TEST_APPROX(passed, plane_foo.offset(), resultPlane_foo.offset(),
97  eps);
98  }
99 
100  std::vector<SE3<T>, Eigen::aligned_allocator<SE3<T>>> Ts_foo_plane =
101  getTestSE3s<T>();
102 
103  for (SE3<T> const& T_foo_plane : Ts_foo_plane) {
104  Plane3<T> plane_foo = planeFromSE3(T_foo_plane);
105  SE3<T> T2_foo_plane = SE3FromPlane(plane_foo);
106  Plane3<T> plane2_foo = planeFromSE3(T2_foo_plane);
107  SOPHUS_TEST_APPROX(passed, plane_foo.normal().eval(),
108  plane2_foo.normal().eval(), eps);
109  SOPHUS_TEST_APPROX(passed, plane_foo.offset(), plane2_foo.offset(), eps);
110  }
111 
112  return passed;
113 }
114 
115 void runAll() {
116  std::cerr << "Geometry (Lines/Planes) tests:" << std::endl;
117  std::cerr << "Double tests: " << std::endl;
118  bool passed = test2dGeometry<double>();
119  passed = test3dGeometry<double>();
120  processTestResult(passed);
121  std::cerr << "Float tests: " << std::endl;
122  passed = test2dGeometry<float>();
123  passed = test3dGeometry<float>();
124  processTestResult(passed);
125 }
126 
127 } // namespace
128 } // namespace Sophus
129 
130 int main() { Sophus::runAll(); }
Sophus::normalFromSO2
Vector2< T > normalFromSO2(SO2< T > const &R_foo_line)
Definition: geometry.hpp:19
Sophus::SO2
SO2 using default storage; derived from SO2Base.
Definition: so2.hpp:19
SOPHUS_TEST_APPROX
#define SOPHUS_TEST_APPROX(passed, left, right, thr,...)
Definition: test_macros.hpp:96
Sophus::SO3
SO3 using default storage; derived from SO3Base.
Definition: so3.hpp:19
test_macros.hpp
Sophus::SO2FromNormal
SO2< T > SO2FromNormal(Vector2< T > normal_foo)
Definition: geometry.hpp:29
Sophus::lineFromSE2
Line2< T > lineFromSE2(SE2< T > const &T_foo_line)
Definition: geometry.hpp:126
Sophus::planeFromSE3
Plane3< T > planeFromSE3(SE3< T > const &T_foo_plane)
Definition: geometry.hpp:148
Sophus::SO3FromNormal
SO3< T > SO3FromNormal(Vector3< T > const &normal_foo)
Definition: geometry.hpp:116
Sophus::SE2FromLine
SE2< T > SE2FromLine(Line2< T > const &line_foo)
Definition: geometry.hpp:135
Sophus::Constants::epsilon
static SOPHUS_FUNC Scalar epsilon()
Definition: common.hpp:147
Sophus
Definition: average.hpp:17
Sophus::SE2
SE2 using default storage; derived from SE2Base.
Definition: se2.hpp:11
Sophus::processTestResult
void processTestResult(bool passed)
Definition: test_macros.hpp:38
Sophus::normalFromSO3
Vector3< T > normalFromSO3(SO3< T > const &R_foo_plane)
Definition: geometry.hpp:41
Sophus::makeHyperplaneUnique
Eigen::Hyperplane< T, N > makeHyperplaneUnique(Eigen::Hyperplane< T, N > const &plane)
Definition: geometry.hpp:168
Sophus::SE3
SE3 using default storage; derived from SE3Base.
Definition: se3.hpp:11
geometry.hpp
main
int main()
Definition: test_geometry.cpp:130
Sophus::rotationFromNormal
Matrix3< T > rotationFromNormal(Vector3< T > const &normal_foo, Vector3< T > xDirHint_foo=Vector3< T >(T(1), T(0), T(0)), Vector3< T > yDirHint_foo=Vector3< T >(T(0), T(1), T(0)))
Definition: geometry.hpp:58
tests.hpp
Sophus::SE3FromPlane
SE3< T > SE3FromPlane(Plane3< T > const &plane_foo)
Definition: geometry.hpp:157


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