so2.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /// @file
  2. /// Special orthogonal group SO(2) - rotation in 2d.
  3. #ifndef SOPHUS_SO2_HPP
  4. #define SOPHUS_SO2_HPP
  5. #include <complex>
  6. #include <type_traits>
  7. // Include only the selective set of Eigen headers that we need.
  8. // This helps when using Sophus with unusual compilers, like nvcc.
  9. #include <eigen3/Eigen/LU>
  10. #include "rotation_matrix.hpp"
  11. #include "types.hpp"
  12. namespace Sophus {
  13. template <class Scalar_, int Options = 0>
  14. class SO2;
  15. using SO2d = SO2<double>;
  16. using SO2f = SO2<float>;
  17. } // namespace Sophus
  18. namespace Eigen {
  19. namespace internal {
  20. template <class Scalar_, int Options_>
  21. struct traits<Sophus::SO2<Scalar_, Options_>> {
  22. static constexpr int Options = Options_;
  23. using Scalar = Scalar_;
  24. using ComplexType = Sophus::Vector2<Scalar, Options>;
  25. };
  26. template <class Scalar_, int Options_>
  27. struct traits<Map<Sophus::SO2<Scalar_>, Options_>>
  28. : traits<Sophus::SO2<Scalar_, Options_>> {
  29. static constexpr int Options = Options_;
  30. using Scalar = Scalar_;
  31. using ComplexType = Map<Sophus::Vector2<Scalar>, Options>;
  32. };
  33. template <class Scalar_, int Options_>
  34. struct traits<Map<Sophus::SO2<Scalar_> const, Options_>>
  35. : traits<Sophus::SO2<Scalar_, Options_> const> {
  36. static constexpr int Options = Options_;
  37. using Scalar = Scalar_;
  38. using ComplexType = Map<Sophus::Vector2<Scalar> const, Options>;
  39. };
  40. } // namespace internal
  41. } // namespace Eigen
  42. namespace Sophus {
  43. /// SO2 base type - implements SO2 class but is storage agnostic.
  44. ///
  45. /// SO(2) is the group of rotations in 2d. As a matrix group, it is the set of
  46. /// matrices which are orthogonal such that ``R * R' = I`` (with ``R'`` being
  47. /// the transpose of ``R``) and have a positive determinant. In particular, the
  48. /// determinant is 1. Let ``theta`` be the rotation angle, the rotation matrix
  49. /// can be written in close form:
  50. ///
  51. /// | cos(theta) -sin(theta) |
  52. /// | sin(theta) cos(theta) |
  53. ///
  54. /// As a matter of fact, the first column of those matrices is isomorph to the
  55. /// set of unit complex numbers U(1). Thus, internally, SO2 is represented as
  56. /// complex number with length 1.
  57. ///
  58. /// SO(2) is a compact and commutative group. First it is compact since the set
  59. /// of rotation matrices is a closed and bounded set. Second it is commutative
  60. /// since ``R(alpha) * R(beta) = R(beta) * R(alpha)``, simply because ``alpha +
  61. /// beta = beta + alpha`` with ``alpha`` and ``beta`` being rotation angles
  62. /// (about the same axis).
  63. ///
  64. /// Class invariant: The 2-norm of ``unit_complex`` must be close to 1.
  65. /// Technically speaking, it must hold that:
  66. ///
  67. /// ``|unit_complex().squaredNorm() - 1| <= Constants::epsilon()``.
  68. template <class Derived>
  69. class SO2Base {
  70. public:
  71. static constexpr int Options = Eigen::internal::traits<Derived>::Options;
  72. using Scalar = typename Eigen::internal::traits<Derived>::Scalar;
  73. using ComplexT = typename Eigen::internal::traits<Derived>::ComplexType;
  74. using ComplexTemporaryType = Sophus::Vector2<Scalar, Options>;
  75. /// Degrees of freedom of manifold, number of dimensions in tangent space (one
  76. /// since we only have in-plane rotations).
  77. static int constexpr DoF = 1;
  78. /// Number of internal parameters used (complex numbers are a tuples).
  79. static int constexpr num_parameters = 2;
  80. /// Group transformations are 2x2 matrices.
  81. static int constexpr N = 2;
  82. using Transformation = Matrix<Scalar, N, N>;
  83. using Point = Vector2<Scalar>;
  84. using HomogeneousPoint = Vector3<Scalar>;
  85. using Line = ParametrizedLine2<Scalar>;
  86. using Tangent = Scalar;
  87. using Adjoint = Scalar;
  88. /// For binary operations the return type is determined with the
  89. /// ScalarBinaryOpTraits feature of Eigen. This allows mixing concrete and Map
  90. /// types, as well as other compatible scalar types such as Ceres::Jet and
  91. /// double scalars with SO2 operations.
  92. template <typename OtherDerived>
  93. using ReturnScalar = typename Eigen::ScalarBinaryOpTraits<
  94. Scalar, typename OtherDerived::Scalar>::ReturnType;
  95. template <typename OtherDerived>
  96. using SO2Product = SO2<ReturnScalar<OtherDerived>>;
  97. template <typename PointDerived>
  98. using PointProduct = Vector2<ReturnScalar<PointDerived>>;
  99. template <typename HPointDerived>
  100. using HomogeneousPointProduct = Vector3<ReturnScalar<HPointDerived>>;
  101. /// Adjoint transformation
  102. ///
  103. /// This function return the adjoint transformation ``Ad`` of the group
  104. /// element ``A`` such that for all ``x`` it holds that
  105. /// ``hat(Ad_A * x) = A * hat(x) A^{-1}``. See hat-operator below.
  106. ///
  107. /// It simply ``1``, since ``SO(2)`` is a commutative group.
  108. ///
  109. SOPHUS_FUNC Adjoint Adj() const { return Scalar(1); }
  110. /// Returns copy of instance casted to NewScalarType.
  111. ///
  112. template <class NewScalarType>
  113. SOPHUS_FUNC SO2<NewScalarType> cast() const {
  114. return SO2<NewScalarType>(unit_complex().template cast<NewScalarType>());
  115. }
  116. /// This provides unsafe read/write access to internal data. SO(2) is
  117. /// represented by a unit complex number (two parameters). When using direct
  118. /// write access, the user needs to take care of that the complex number stays
  119. /// normalized.
  120. ///
  121. SOPHUS_FUNC Scalar* data() { return unit_complex_nonconst().data(); }
  122. /// Const version of data() above.
  123. ///
  124. SOPHUS_FUNC Scalar const* data() const { return unit_complex().data(); }
  125. /// Returns group inverse.
  126. ///
  127. SOPHUS_FUNC SO2<Scalar> inverse() const {
  128. return SO2<Scalar>(unit_complex().x(), -unit_complex().y());
  129. }
  130. /// Logarithmic map
  131. ///
  132. /// Computes the logarithm, the inverse of the group exponential which maps
  133. /// element of the group (rotation matrices) to elements of the tangent space
  134. /// (rotation angles).
  135. ///
  136. /// To be specific, this function computes ``vee(logmat(.))`` with
  137. /// ``logmat(.)`` being the matrix logarithm and ``vee(.)`` the vee-operator
  138. /// of SO(2).
  139. ///
  140. SOPHUS_FUNC Scalar log() const {
  141. using std::atan2;
  142. return atan2(unit_complex().y(), unit_complex().x());
  143. }
  144. /// It re-normalizes ``unit_complex`` to unit length.
  145. ///
  146. /// Note: Because of the class invariant, there is typically no need to call
  147. /// this function directly.
  148. ///
  149. SOPHUS_FUNC void normalize() {
  150. using std::sqrt;
  151. Scalar length = sqrt(unit_complex().x() * unit_complex().x() +
  152. unit_complex().y() * unit_complex().y());
  153. SOPHUS_ENSURE(length >= Constants<Scalar>::epsilon(),
  154. "Complex number should not be close to zero!");
  155. unit_complex_nonconst().x() /= length;
  156. unit_complex_nonconst().y() /= length;
  157. }
  158. /// Returns 2x2 matrix representation of the instance.
  159. ///
  160. /// For SO(2), the matrix representation is an orthogonal matrix ``R`` with
  161. /// ``det(R)=1``, thus the so-called "rotation matrix".
  162. ///
  163. SOPHUS_FUNC Transformation matrix() const {
  164. Scalar const& real = unit_complex().x();
  165. Scalar const& imag = unit_complex().y();
  166. Transformation R;
  167. // clang-format off
  168. R <<
  169. real, -imag,
  170. imag, real;
  171. // clang-format on
  172. return R;
  173. }
  174. /// Assignment-like operator from OtherDerived.
  175. ///
  176. template <class OtherDerived>
  177. SOPHUS_FUNC SO2Base<Derived>& operator=(SO2Base<OtherDerived> const& other) {
  178. unit_complex_nonconst() = other.unit_complex();
  179. return *this;
  180. }
  181. /// Group multiplication, which is rotation concatenation.
  182. ///
  183. template <typename OtherDerived>
  184. SOPHUS_FUNC SO2Product<OtherDerived> operator*(
  185. SO2Base<OtherDerived> const& other) const {
  186. using ResultT = ReturnScalar<OtherDerived>;
  187. Scalar const lhs_real = unit_complex().x();
  188. Scalar const lhs_imag = unit_complex().y();
  189. typename OtherDerived::Scalar const& rhs_real = other.unit_complex().x();
  190. typename OtherDerived::Scalar const& rhs_imag = other.unit_complex().y();
  191. // complex multiplication
  192. ResultT const result_real = lhs_real * rhs_real - lhs_imag * rhs_imag;
  193. ResultT const result_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
  194. ResultT const squared_norm =
  195. result_real * result_real + result_imag * result_imag;
  196. // We can assume that the squared-norm is close to 1 since we deal with a
  197. // unit complex number. Due to numerical precision issues, there might
  198. // be a small drift after pose concatenation. Hence, we need to renormalizes
  199. // the complex number here.
  200. // Since squared-norm is close to 1, we do not need to calculate the costly
  201. // square-root, but can use an approximation around 1 (see
  202. // http://stackoverflow.com/a/12934750 for details).
  203. if (squared_norm != ResultT(1.0)) {
  204. ResultT const scale = ResultT(2.0) / (ResultT(1.0) + squared_norm);
  205. return SO2Product<OtherDerived>(result_real * scale, result_imag * scale);
  206. }
  207. return SO2Product<OtherDerived>(result_real, result_imag);
  208. }
  209. /// Group action on 2-points.
  210. ///
  211. /// This function rotates a 2 dimensional point ``p`` by the SO2 element
  212. /// ``bar_R_foo`` (= rotation matrix): ``p_bar = bar_R_foo * p_foo``.
  213. ///
  214. template <typename PointDerived,
  215. typename = typename std::enable_if<
  216. IsFixedSizeVector<PointDerived, 2>::value>::type>
  217. SOPHUS_FUNC PointProduct<PointDerived> operator*(
  218. Eigen::MatrixBase<PointDerived> const& p) const {
  219. Scalar const& real = unit_complex().x();
  220. Scalar const& imag = unit_complex().y();
  221. return PointProduct<PointDerived>(real * p[0] - imag * p[1],
  222. imag * p[0] + real * p[1]);
  223. }
  224. /// Group action on homogeneous 2-points.
  225. ///
  226. /// This function rotates a homogeneous 2 dimensional point ``p`` by the SO2
  227. /// element ``bar_R_foo`` (= rotation matrix): ``p_bar = bar_R_foo * p_foo``.
  228. ///
  229. template <typename HPointDerived,
  230. typename = typename std::enable_if<
  231. IsFixedSizeVector<HPointDerived, 3>::value>::type>
  232. SOPHUS_FUNC HomogeneousPointProduct<HPointDerived> operator*(
  233. Eigen::MatrixBase<HPointDerived> const& p) const {
  234. Scalar const& real = unit_complex().x();
  235. Scalar const& imag = unit_complex().y();
  236. return HomogeneousPointProduct<HPointDerived>(
  237. real * p[0] - imag * p[1], imag * p[0] + real * p[1], p[2]);
  238. }
  239. /// Group action on lines.
  240. ///
  241. /// This function rotates a parametrized line ``l(t) = o + t * d`` by the SO2
  242. /// element:
  243. ///
  244. /// Both direction ``d`` and origin ``o`` are rotated as a 2 dimensional point
  245. ///
  246. SOPHUS_FUNC Line operator*(Line const& l) const {
  247. return Line((*this) * l.origin(), (*this) * l.direction());
  248. }
  249. /// In-place group multiplication. This method is only valid if the return
  250. /// type of the multiplication is compatible with this SO2's Scalar type.
  251. ///
  252. template <typename OtherDerived,
  253. typename = typename std::enable_if<
  254. std::is_same<Scalar, ReturnScalar<OtherDerived>>::value>::type>
  255. SOPHUS_FUNC SO2Base<Derived> operator*=(SO2Base<OtherDerived> const& other) {
  256. *static_cast<Derived*>(this) = *this * other;
  257. return *this;
  258. }
  259. /// Returns derivative of this * SO2::exp(x) wrt. x at x=0.
  260. ///
  261. SOPHUS_FUNC Matrix<Scalar, num_parameters, DoF> Dx_this_mul_exp_x_at_0()
  262. const {
  263. return Matrix<Scalar, num_parameters, DoF>(-unit_complex()[1],
  264. unit_complex()[0]);
  265. }
  266. /// Returns internal parameters of SO(2).
  267. ///
  268. /// It returns (c[0], c[1]), with c being the unit complex number.
  269. ///
  270. SOPHUS_FUNC Sophus::Vector<Scalar, num_parameters> params() const {
  271. return unit_complex();
  272. }
  273. /// Takes in complex number / tuple and normalizes it.
  274. ///
  275. /// Precondition: The complex number must not be close to zero.
  276. ///
  277. SOPHUS_FUNC void setComplex(Point const& complex) {
  278. unit_complex_nonconst() = complex;
  279. normalize();
  280. }
  281. /// Accessor of unit quaternion.
  282. ///
  283. SOPHUS_FUNC
  284. ComplexT const& unit_complex() const {
  285. return static_cast<Derived const*>(this)->unit_complex();
  286. }
  287. private:
  288. /// Mutator of unit_complex is private to ensure class invariant. That is
  289. /// the complex number must stay close to unit length.
  290. ///
  291. SOPHUS_FUNC
  292. ComplexT& unit_complex_nonconst() {
  293. return static_cast<Derived*>(this)->unit_complex_nonconst();
  294. }
  295. };
  296. /// SO2 using default storage; derived from SO2Base.
  297. template <class Scalar_, int Options>
  298. class SO2 : public SO2Base<SO2<Scalar_, Options>> {
  299. public:
  300. using Base = SO2Base<SO2<Scalar_, Options>>;
  301. static int constexpr DoF = Base::DoF;
  302. static int constexpr num_parameters = Base::num_parameters;
  303. using Scalar = Scalar_;
  304. using Transformation = typename Base::Transformation;
  305. using Point = typename Base::Point;
  306. using HomogeneousPoint = typename Base::HomogeneousPoint;
  307. using Tangent = typename Base::Tangent;
  308. using Adjoint = typename Base::Adjoint;
  309. using ComplexMember = Vector2<Scalar, Options>;
  310. /// ``Base`` is friend so unit_complex_nonconst can be accessed from ``Base``.
  311. friend class SO2Base<SO2<Scalar, Options>>;
  312. using Base::operator=;
  313. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  314. /// Default constructor initializes unit complex number to identity rotation.
  315. ///
  316. SOPHUS_FUNC SO2() : unit_complex_(Scalar(1), Scalar(0)) {}
  317. /// Copy constructor
  318. ///
  319. SOPHUS_FUNC SO2(SO2 const& other) = default;
  320. /// Copy-like constructor from OtherDerived.
  321. ///
  322. template <class OtherDerived>
  323. SOPHUS_FUNC SO2(SO2Base<OtherDerived> const& other)
  324. : unit_complex_(other.unit_complex()) {}
  325. /// Constructor from rotation matrix
  326. ///
  327. /// Precondition: rotation matrix need to be orthogonal with determinant of 1.
  328. ///
  329. SOPHUS_FUNC explicit SO2(Transformation const& R)
  330. : unit_complex_(Scalar(0.5) * (R(0, 0) + R(1, 1)),
  331. Scalar(0.5) * (R(1, 0) - R(0, 1))) {
  332. SOPHUS_ENSURE(isOrthogonal(R), "R is not orthogonal:\n %", R);
  333. SOPHUS_ENSURE(R.determinant() > Scalar(0), "det(R) is not positive: %",
  334. R.determinant());
  335. }
  336. /// Constructor from pair of real and imaginary number.
  337. ///
  338. /// Precondition: The pair must not be close to zero.
  339. ///
  340. SOPHUS_FUNC SO2(Scalar const& real, Scalar const& imag)
  341. : unit_complex_(real, imag) {
  342. Base::normalize();
  343. }
  344. /// Constructor from 2-vector.
  345. ///
  346. /// Precondition: The vector must not be close to zero.
  347. ///
  348. template <class D>
  349. SOPHUS_FUNC explicit SO2(Eigen::MatrixBase<D> const& complex)
  350. : unit_complex_(complex) {
  351. static_assert(std::is_same<typename D::Scalar, Scalar>::value,
  352. "must be same Scalar type");
  353. Base::normalize();
  354. }
  355. /// Constructor from an rotation angle.
  356. ///
  357. SOPHUS_FUNC explicit SO2(Scalar theta) {
  358. unit_complex_nonconst() = SO2<Scalar>::exp(theta).unit_complex();
  359. }
  360. /// Accessor of unit complex number
  361. ///
  362. SOPHUS_FUNC ComplexMember const& unit_complex() const {
  363. return unit_complex_;
  364. }
  365. /// Group exponential
  366. ///
  367. /// This functions takes in an element of tangent space (= rotation angle
  368. /// ``theta``) and returns the corresponding element of the group SO(2).
  369. ///
  370. /// To be more specific, this function computes ``expmat(hat(omega))``
  371. /// with ``expmat(.)`` being the matrix exponential and ``hat(.)`` being the
  372. /// hat()-operator of SO(2).
  373. ///
  374. SOPHUS_FUNC static SO2<Scalar> exp(Tangent const& theta) {
  375. using std::cos;
  376. using std::sin;
  377. return SO2<Scalar>(cos(theta), sin(theta));
  378. }
  379. /// Returns derivative of exp(x) wrt. x.
  380. ///
  381. SOPHUS_FUNC static Sophus::Matrix<Scalar, num_parameters, DoF> Dx_exp_x(
  382. Tangent const& theta) {
  383. using std::cos;
  384. using std::sin;
  385. return Sophus::Matrix<Scalar, num_parameters, DoF>(-sin(theta), cos(theta));
  386. }
  387. /// Returns derivative of exp(x) wrt. x_i at x=0.
  388. ///
  389. SOPHUS_FUNC static Sophus::Matrix<Scalar, num_parameters, DoF>
  390. Dx_exp_x_at_0() {
  391. return Sophus::Matrix<Scalar, num_parameters, DoF>(Scalar(0), Scalar(1));
  392. }
  393. /// Returns derivative of exp(x).matrix() wrt. ``x_i at x=0``.
  394. ///
  395. SOPHUS_FUNC static Transformation Dxi_exp_x_matrix_at_0(int) {
  396. return generator();
  397. }
  398. /// Returns the infinitesimal generators of SO(2).
  399. ///
  400. /// The infinitesimal generators of SO(2) is:
  401. ///
  402. /// | 0 1 |
  403. /// | -1 0 |
  404. ///
  405. SOPHUS_FUNC static Transformation generator() { return hat(Scalar(1)); }
  406. /// hat-operator
  407. ///
  408. /// It takes in the scalar representation ``theta`` (= rotation angle) and
  409. /// returns the corresponding matrix representation of Lie algebra element.
  410. ///
  411. /// Formally, the hat()-operator of SO(2) is defined as
  412. ///
  413. /// ``hat(.): R^2 -> R^{2x2}, hat(theta) = theta * G``
  414. ///
  415. /// with ``G`` being the infinitesimal generator of SO(2).
  416. ///
  417. /// The corresponding inverse is the vee()-operator, see below.
  418. ///
  419. SOPHUS_FUNC static Transformation hat(Tangent const& theta) {
  420. Transformation Omega;
  421. // clang-format off
  422. Omega <<
  423. Scalar(0), -theta,
  424. theta, Scalar(0);
  425. // clang-format on
  426. return Omega;
  427. }
  428. /// Returns closed SO2 given arbitrary 2x2 matrix.
  429. ///
  430. template <class S = Scalar>
  431. static SOPHUS_FUNC enable_if_t<std::is_floating_point<S>::value, SO2>
  432. fitToSO2(Transformation const& R) {
  433. return SO2(makeRotationMatrix(R));
  434. }
  435. /// Lie bracket
  436. ///
  437. /// It returns the Lie bracket of SO(2). Since SO(2) is a commutative group,
  438. /// the Lie bracket is simple ``0``.
  439. ///
  440. SOPHUS_FUNC static Tangent lieBracket(Tangent const&, Tangent const&) {
  441. return Scalar(0);
  442. }
  443. /// Draw uniform sample from SO(2) manifold.
  444. ///
  445. template <class UniformRandomBitGenerator>
  446. static SO2 sampleUniform(UniformRandomBitGenerator& generator) {
  447. static_assert(IsUniformRandomBitGenerator<UniformRandomBitGenerator>::value,
  448. "generator must meet the UniformRandomBitGenerator concept");
  449. std::uniform_real_distribution<Scalar> uniform(-Constants<Scalar>::pi(),
  450. Constants<Scalar>::pi());
  451. return SO2(uniform(generator));
  452. }
  453. /// vee-operator
  454. ///
  455. /// It takes the 2x2-matrix representation ``Omega`` and maps it to the
  456. /// corresponding scalar representation of Lie algebra.
  457. ///
  458. /// This is the inverse of the hat()-operator, see above.
  459. ///
  460. /// Precondition: ``Omega`` must have the following structure:
  461. ///
  462. /// | 0 -a |
  463. /// | a 0 |
  464. ///
  465. SOPHUS_FUNC static Tangent vee(Transformation const& Omega) {
  466. using std::abs;
  467. return Omega(1, 0);
  468. }
  469. protected:
  470. /// Mutator of complex number is protected to ensure class invariant.
  471. ///
  472. SOPHUS_FUNC ComplexMember& unit_complex_nonconst() { return unit_complex_; }
  473. ComplexMember unit_complex_;
  474. };
  475. } // namespace Sophus
  476. namespace Eigen {
  477. /// Specialization of Eigen::Map for ``SO2``; derived from SO2Base.
  478. ///
  479. /// Allows us to wrap SO2 objects around POD array (e.g. external c style
  480. /// complex number / tuple).
  481. template <class Scalar_, int Options>
  482. class Map<Sophus::SO2<Scalar_>, Options>
  483. : public Sophus::SO2Base<Map<Sophus::SO2<Scalar_>, Options>> {
  484. public:
  485. using Base = Sophus::SO2Base<Map<Sophus::SO2<Scalar_>, Options>>;
  486. using Scalar = Scalar_;
  487. using Transformation = typename Base::Transformation;
  488. using Point = typename Base::Point;
  489. using HomogeneousPoint = typename Base::HomogeneousPoint;
  490. using Tangent = typename Base::Tangent;
  491. using Adjoint = typename Base::Adjoint;
  492. /// ``Base`` is friend so unit_complex_nonconst can be accessed from ``Base``.
  493. friend class Sophus::SO2Base<Map<Sophus::SO2<Scalar_>, Options>>;
  494. using Base::operator=;
  495. using Base::operator*=;
  496. using Base::operator*;
  497. SOPHUS_FUNC
  498. Map(Scalar* coeffs) : unit_complex_(coeffs) {}
  499. /// Accessor of unit complex number.
  500. ///
  501. SOPHUS_FUNC
  502. Map<Sophus::Vector2<Scalar>, Options> const& unit_complex() const {
  503. return unit_complex_;
  504. }
  505. protected:
  506. /// Mutator of unit_complex is protected to ensure class invariant.
  507. ///
  508. SOPHUS_FUNC
  509. Map<Sophus::Vector2<Scalar>, Options>& unit_complex_nonconst() {
  510. return unit_complex_;
  511. }
  512. Map<Matrix<Scalar, 2, 1>, Options> unit_complex_;
  513. };
  514. /// Specialization of Eigen::Map for ``SO2 const``; derived from SO2Base.
  515. ///
  516. /// Allows us to wrap SO2 objects around POD array (e.g. external c style
  517. /// complex number / tuple).
  518. template <class Scalar_, int Options>
  519. class Map<Sophus::SO2<Scalar_> const, Options>
  520. : public Sophus::SO2Base<Map<Sophus::SO2<Scalar_> const, Options>> {
  521. public:
  522. using Base = Sophus::SO2Base<Map<Sophus::SO2<Scalar_> const, Options>>;
  523. using Scalar = Scalar_;
  524. using Transformation = typename Base::Transformation;
  525. using Point = typename Base::Point;
  526. using HomogeneousPoint = typename Base::HomogeneousPoint;
  527. using Tangent = typename Base::Tangent;
  528. using Adjoint = typename Base::Adjoint;
  529. using Base::operator*=;
  530. using Base::operator*;
  531. SOPHUS_FUNC Map(Scalar const* coeffs) : unit_complex_(coeffs) {}
  532. /// Accessor of unit complex number.
  533. ///
  534. SOPHUS_FUNC Map<Sophus::Vector2<Scalar> const, Options> const& unit_complex()
  535. const {
  536. return unit_complex_;
  537. }
  538. protected:
  539. /// Mutator of unit_complex is protected to ensure class invariant.
  540. ///
  541. Map<Matrix<Scalar, 2, 1> const, Options> const unit_complex_;
  542. };
  543. } // namespace Eigen
  544. #endif // SOPHUS_SO2_HPP