geometry.hpp
Go to the documentation of this file.
1 
4 #ifndef GEOMETRY_HPP
5 #define GEOMETRY_HPP
6 
7 #include "se2.hpp"
8 #include "se3.hpp"
9 #include "so2.hpp"
10 #include "so3.hpp"
11 #include "types.hpp"
12 
13 namespace Sophus {
14 
18 template <class T>
19 Vector2<T> normalFromSO2(SO2<T> const& R_foo_line) {
20  return R_foo_line.matrix().col(1);
21 }
22 
28 template <class T>
30  SOPHUS_ENSURE(normal_foo.squaredNorm() > Constants<T>::epsilon(), "%",
31  normal_foo.transpose());
32  normal_foo.normalize();
33  return SO2<T>(normal_foo.y(), -normal_foo.x());
34 }
35 
40 template <class T>
41 Vector3<T> normalFromSO3(SO3<T> const& R_foo_plane) {
42  return R_foo_plane.matrix().col(2);
43 }
44 
57 template <class T>
59  Vector3<T> xDirHint_foo = Vector3<T>(T(1), T(0),
60  T(0)),
61  Vector3<T> yDirHint_foo = Vector3<T>(T(0), T(1),
62  T(0))) {
63  SOPHUS_ENSURE(xDirHint_foo.dot(yDirHint_foo) < Constants<T>::epsilon(),
64  "xDirHint (%) and yDirHint (%) must be perpendicular.",
65  xDirHint_foo.transpose(), yDirHint_foo.transpose());
66  using std::abs;
67  using std::sqrt;
68  T const xDirHint_foo_sqr_length = xDirHint_foo.squaredNorm();
69  T const yDirHint_foo_sqr_length = yDirHint_foo.squaredNorm();
70  T const normal_foo_sqr_length = normal_foo.squaredNorm();
71  SOPHUS_ENSURE(xDirHint_foo_sqr_length > Constants<T>::epsilon(), "%",
72  xDirHint_foo.transpose());
73  SOPHUS_ENSURE(yDirHint_foo_sqr_length > Constants<T>::epsilon(), "%",
74  yDirHint_foo.transpose());
75  SOPHUS_ENSURE(normal_foo_sqr_length > Constants<T>::epsilon(), "%",
76  normal_foo.transpose());
77 
78  Matrix3<T> basis_foo;
79  basis_foo.col(2) = normal_foo;
80 
81  if (abs(xDirHint_foo_sqr_length - T(1)) > Constants<T>::epsilon()) {
82  xDirHint_foo.normalize();
83  }
84  if (abs(yDirHint_foo_sqr_length - T(1)) > Constants<T>::epsilon()) {
85  yDirHint_foo.normalize();
86  }
87  if (abs(normal_foo_sqr_length - T(1)) > Constants<T>::epsilon()) {
88  basis_foo.col(2).normalize();
89  }
90 
91  T abs_x_dot_z = abs(basis_foo.col(2).dot(xDirHint_foo));
92  T abs_y_dot_z = abs(basis_foo.col(2).dot(yDirHint_foo));
93  if (abs_x_dot_z < abs_y_dot_z) {
94  // basis_foo.z and xDirHint are far from parallel.
95  basis_foo.col(1) = basis_foo.col(2).cross(xDirHint_foo).normalized();
96  basis_foo.col(0) = basis_foo.col(1).cross(basis_foo.col(2));
97  } else {
98  // basis_foo.z and yDirHint are far from parallel.
99  basis_foo.col(0) = yDirHint_foo.cross(basis_foo.col(2)).normalized();
100  basis_foo.col(1) = basis_foo.col(2).cross(basis_foo.col(0));
101  }
102  T det = basis_foo.determinant();
103  // sanity check
104  SOPHUS_ENSURE(abs(det - T(1)) < Constants<T>::epsilon(),
105  "Determinant of basis is not 1, but %. Basis is \n%\n", det,
106  basis_foo);
107  return basis_foo;
108 }
109 
115 template <class T>
116 SO3<T> SO3FromNormal(Vector3<T> const& normal_foo) {
117  return SO3<T>(rotationFromNormal(normal_foo));
118 }
119 
125 template <class T>
126 Line2<T> lineFromSE2(SE2<T> const& T_foo_line) {
127  return Line2<T>(normalFromSO2(T_foo_line.so2()), T_foo_line.translation());
128 }
129 
134 template <class T>
135 SE2<T> SE2FromLine(Line2<T> const& line_foo) {
136  T const d = line_foo.offset();
137  Vector2<T> const n = line_foo.normal();
138  SO2<T> const R_foo_plane = SO2FromNormal(n);
139  return SE2<T>(R_foo_plane, -d * n);
140 }
141 
147 template <class T>
148 Plane3<T> planeFromSE3(SE3<T> const& T_foo_plane) {
149  return Plane3<T>(normalFromSO3(T_foo_plane.so3()), T_foo_plane.translation());
150 }
151 
156 template <class T>
157 SE3<T> SE3FromPlane(Plane3<T> const& plane_foo) {
158  T const d = plane_foo.offset();
159  Vector3<T> const n = plane_foo.normal();
160  SO3<T> const R_foo_plane = SO3FromNormal(n);
161  return SE3<T>(R_foo_plane, -d * n);
162 }
163 
167 template <class T, int N>
168 Eigen::Hyperplane<T, N> makeHyperplaneUnique(
169  Eigen::Hyperplane<T, N> const& plane) {
170  if (plane.offset() >= 0) {
171  return plane;
172  }
173 
174  return Eigen::Hyperplane<T, N>(-plane.normal(), -plane.offset());
175 }
176 
177 } // namespace Sophus
178 
179 #endif // GEOMETRY_HPP
Sophus::normalFromSO2
Vector2< T > normalFromSO2(SO2< T > const &R_foo_line)
Definition: geometry.hpp:19
Sophus::SE3::so3
SOPHUS_FUNC SO3Member & so3()
Definition: se3.hpp:527
Sophus::SO2
SO2 using default storage; derived from SO2Base.
Definition: so2.hpp:19
Sophus::SO3
SO3 using default storage; derived from SO3Base.
Definition: so3.hpp:19
types.hpp
Sophus::SO2FromNormal
SO2< T > SO2FromNormal(Vector2< T > normal_foo)
Definition: geometry.hpp:29
Sophus::Vector3
Vector< Scalar, 3, Options > Vector3
Definition: types.hpp:21
se2.hpp
SOPHUS_ENSURE
#define SOPHUS_ENSURE(expr,...)
Definition: common.hpp:137
Sophus::lineFromSE2
Line2< T > lineFromSE2(SE2< T > const &T_foo_line)
Definition: geometry.hpp:126
Sophus::Vector2
Vector< Scalar, 2, Options > Vector2
Definition: types.hpp:16
Sophus::SE2::so2
SOPHUS_FUNC SO2Member & so2()
Definition: se2.hpp:460
Sophus::planeFromSE3
Plane3< T > planeFromSE3(SE3< T > const &T_foo_plane)
Definition: geometry.hpp:148
so3.hpp
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
Definition: average.hpp:17
Sophus::SE2
SE2 using default storage; derived from SE2Base.
Definition: se2.hpp:11
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::translation
SOPHUS_FUNC TranslationMember & translation()
Definition: se3.hpp:535
Sophus::SE3
SE3 using default storage; derived from SE3Base.
Definition: se3.hpp:11
Sophus::Plane3
Eigen::Hyperplane< T, 3 > Plane3
Planes in 3d are hyperplanes.
Definition: types.hpp:229
se3.hpp
Sophus::Line2
Eigen::Hyperplane< T, 2 > Line2
Lines in 2d are hyperplanes.
Definition: types.hpp:235
Sophus::Constants
Definition: common.hpp:146
Sophus::Matrix3
Matrix< Scalar, 3, 3 > Matrix3
Definition: types.hpp:49
Sophus::SE2::translation
SOPHUS_FUNC TranslationMember & translation()
Definition: se2.hpp:468
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
Sophus::SE3FromPlane
SE3< T > SE3FromPlane(Plane3< T > const &plane_foo)
Definition: geometry.hpp:157
so2.hpp


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