types.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /// @file
  2. /// Common type aliases.
  3. #ifndef SOPHUS_TYPES_HPP
  4. #define SOPHUS_TYPES_HPP
  5. #include <type_traits>
  6. #include "common.hpp"
  7. namespace Sophus {
  8. template <class Scalar, int M, int Options = 0>
  9. using Vector = Eigen::Matrix<Scalar, M, 1, Options>;
  10. template <class Scalar, int Options = 0>
  11. using Vector2 = Vector<Scalar, 2, Options>;
  12. using Vector2f = Vector2<float>;
  13. using Vector2d = Vector2<double>;
  14. template <class Scalar, int Options = 0>
  15. using Vector3 = Vector<Scalar, 3, Options>;
  16. using Vector3f = Vector3<float>;
  17. using Vector3d = Vector3<double>;
  18. template <class Scalar>
  19. using Vector4 = Vector<Scalar, 4>;
  20. using Vector4f = Vector4<float>;
  21. using Vector4d = Vector4<double>;
  22. template <class Scalar>
  23. using Vector6 = Vector<Scalar, 6>;
  24. using Vector6f = Vector6<float>;
  25. using Vector6d = Vector6<double>;
  26. template <class Scalar>
  27. using Vector7 = Vector<Scalar, 7>;
  28. using Vector7f = Vector7<float>;
  29. using Vector7d = Vector7<double>;
  30. template <class Scalar, int M, int N>
  31. using Matrix = Eigen::Matrix<Scalar, M, N>;
  32. template <class Scalar>
  33. using Matrix2 = Matrix<Scalar, 2, 2>;
  34. using Matrix2f = Matrix2<float>;
  35. using Matrix2d = Matrix2<double>;
  36. template <class Scalar>
  37. using Matrix3 = Matrix<Scalar, 3, 3>;
  38. using Matrix3f = Matrix3<float>;
  39. using Matrix3d = Matrix3<double>;
  40. template <class Scalar>
  41. using Matrix4 = Matrix<Scalar, 4, 4>;
  42. using Matrix4f = Matrix4<float>;
  43. using Matrix4d = Matrix4<double>;
  44. template <class Scalar>
  45. using Matrix6 = Matrix<Scalar, 6, 6>;
  46. using Matrix6f = Matrix6<float>;
  47. using Matrix6d = Matrix6<double>;
  48. template <class Scalar>
  49. using Matrix7 = Matrix<Scalar, 7, 7>;
  50. using Matrix7f = Matrix7<float>;
  51. using Matrix7d = Matrix7<double>;
  52. template <class Scalar, int N, int Options = 0>
  53. using ParametrizedLine = Eigen::ParametrizedLine<Scalar, N, Options>;
  54. template <class Scalar, int Options = 0>
  55. using ParametrizedLine3 = ParametrizedLine<Scalar, 3, Options>;
  56. using ParametrizedLine3f = ParametrizedLine3<float>;
  57. using ParametrizedLine3d = ParametrizedLine3<double>;
  58. template <class Scalar, int Options = 0>
  59. using ParametrizedLine2 = ParametrizedLine<Scalar, 2, Options>;
  60. using ParametrizedLine2f = ParametrizedLine2<float>;
  61. using ParametrizedLine2d = ParametrizedLine2<double>;
  62. namespace details {
  63. template <class Scalar>
  64. class MaxMetric {
  65. public:
  66. static Scalar impl(Scalar s0, Scalar s1) {
  67. using std::abs;
  68. return abs(s0 - s1);
  69. }
  70. };
  71. template <class Scalar, int M, int N>
  72. class MaxMetric<Matrix<Scalar, M, N>> {
  73. public:
  74. static Scalar impl(Matrix<Scalar, M, N> const& p0,
  75. Matrix<Scalar, M, N> const& p1) {
  76. return (p0 - p1).template lpNorm<Eigen::Infinity>();
  77. }
  78. };
  79. template <class Scalar>
  80. class SetToZero {
  81. public:
  82. static void impl(Scalar& s) { s = Scalar(0); }
  83. };
  84. template <class Scalar, int M, int N>
  85. class SetToZero<Matrix<Scalar, M, N>> {
  86. public:
  87. static void impl(Matrix<Scalar, M, N>& v) { v.setZero(); }
  88. };
  89. template <class T1, class Scalar>
  90. class SetElementAt;
  91. template <class Scalar>
  92. class SetElementAt<Scalar, Scalar> {
  93. public:
  94. static void impl(Scalar& s, Scalar value, int at) {
  95. SOPHUS_ENSURE(at == 0, "is %", at);
  96. s = value;
  97. }
  98. };
  99. template <class Scalar, int N>
  100. class SetElementAt<Vector<Scalar, N>, Scalar> {
  101. public:
  102. static void impl(Vector<Scalar, N>& v, Scalar value, int at) {
  103. SOPHUS_ENSURE(at >= 0 && at < N, "is %", at);
  104. v[at] = value;
  105. }
  106. };
  107. template <class Scalar>
  108. class SquaredNorm {
  109. public:
  110. static Scalar impl(Scalar const& s) { return s * s; }
  111. };
  112. template <class Scalar, int N>
  113. class SquaredNorm<Matrix<Scalar, N, 1>> {
  114. public:
  115. static Scalar impl(Matrix<Scalar, N, 1> const& s) { return s.squaredNorm(); }
  116. };
  117. template <class Scalar>
  118. class Transpose {
  119. public:
  120. static Scalar impl(Scalar const& s) { return s; }
  121. };
  122. template <class Scalar, int M, int N>
  123. class Transpose<Matrix<Scalar, M, N>> {
  124. public:
  125. static Matrix<Scalar, M, N> impl(Matrix<Scalar, M, N> const& s) {
  126. return s.transpose();
  127. }
  128. };
  129. } // namespace details
  130. /// Returns maximum metric between two points ``p0`` and ``p1``, with ``p0, p1``
  131. /// being matrices or a scalars.
  132. ///
  133. template <class T>
  134. auto maxMetric(T const& p0, T const& p1)
  135. -> decltype(details::MaxMetric<T>::impl(p0, p1)) {
  136. return details::MaxMetric<T>::impl(p0, p1);
  137. }
  138. /// Sets point ``p`` to zero, with ``p`` being a matrix or a scalar.
  139. ///
  140. template <class T>
  141. void setToZero(T& p) {
  142. return details::SetToZero<T>::impl(p);
  143. }
  144. /// Sets ``i``th component of ``p`` to ``value``, with ``p`` being a
  145. /// matrix or a scalar. If ``p`` is a scalar, ``i`` must be ``0``.
  146. ///
  147. template <class T, class Scalar>
  148. void setElementAt(T& p, Scalar value, int i) {
  149. return details::SetElementAt<T, Scalar>::impl(p, value, i);
  150. }
  151. /// Returns the squared 2-norm of ``p``, with ``p`` being a vector or a scalar.
  152. ///
  153. template <class T>
  154. auto squaredNorm(T const& p) -> decltype(details::SquaredNorm<T>::impl(p)) {
  155. return details::SquaredNorm<T>::impl(p);
  156. }
  157. /// Returns ``p.transpose()`` if ``p`` is a matrix, and simply ``p`` if m is a
  158. /// scalar.
  159. ///
  160. template <class T>
  161. auto transpose(T const& p) -> decltype(details::Transpose<T>::impl(T())) {
  162. return details::Transpose<T>::impl(p);
  163. }
  164. template <class Scalar>
  165. struct IsFloatingPoint {
  166. static bool const value = std::is_floating_point<Scalar>::value;
  167. };
  168. template <class Scalar, int M, int N>
  169. struct IsFloatingPoint<Matrix<Scalar, M, N>> {
  170. static bool const value = std::is_floating_point<Scalar>::value;
  171. };
  172. template <class Scalar_>
  173. struct GetScalar {
  174. using Scalar = Scalar_;
  175. };
  176. template <class Scalar_, int M, int N>
  177. struct GetScalar<Matrix<Scalar_, M, N>> {
  178. using Scalar = Scalar_;
  179. };
  180. /// If the Vector type is of fixed size, then IsFixedSizeVector::value will be
  181. /// true.
  182. template <typename Vector, int NumDimensions,
  183. typename = typename std::enable_if<
  184. Vector::RowsAtCompileTime == NumDimensions &&
  185. Vector::ColsAtCompileTime == 1>::type>
  186. struct IsFixedSizeVector : std::true_type {};
  187. /// Planes in 3d are hyperplanes.
  188. template <class T>
  189. using Plane3 = Eigen::Hyperplane<T, 3>;
  190. using Plane3d = Plane3<double>;
  191. using Plane3f = Plane3<float>;
  192. /// Lines in 2d are hyperplanes.
  193. template <class T>
  194. using Line2 = Eigen::Hyperplane<T, 2>;
  195. using Line2d = Line2<double>;
  196. using Line2f = Line2<float>;
  197. } // namespace Sophus
  198. #endif // SOPHUS_TYPES_HPP