rxso2.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /// @file
  2. /// Direct product R X SO(2) - rotation and scaling in 2d.
  3. #ifndef SOPHUS_RXSO2_HPP
  4. #define SOPHUS_RXSO2_HPP
  5. #include "so2.hpp"
  6. namespace Sophus {
  7. template <class Scalar_, int Options = 0>
  8. class RxSO2;
  9. using RxSO2d = RxSO2<double>;
  10. using RxSO2f = RxSO2<float>;
  11. } // namespace Sophus
  12. namespace Eigen {
  13. namespace internal {
  14. template <class Scalar_, int Options_>
  15. struct traits<Sophus::RxSO2<Scalar_, Options_>> {
  16. static constexpr int Options = Options_;
  17. using Scalar = Scalar_;
  18. using ComplexType = Sophus::Vector2<Scalar, Options>;
  19. };
  20. template <class Scalar_, int Options_>
  21. struct traits<Map<Sophus::RxSO2<Scalar_>, Options_>>
  22. : traits<Sophus::RxSO2<Scalar_, Options_>> {
  23. static constexpr int Options = Options_;
  24. using Scalar = Scalar_;
  25. using ComplexType = Map<Sophus::Vector2<Scalar>, Options>;
  26. };
  27. template <class Scalar_, int Options_>
  28. struct traits<Map<Sophus::RxSO2<Scalar_> const, Options_>>
  29. : traits<Sophus::RxSO2<Scalar_, Options_> const> {
  30. static constexpr int Options = Options_;
  31. using Scalar = Scalar_;
  32. using ComplexType = Map<Sophus::Vector2<Scalar> const, Options>;
  33. };
  34. } // namespace internal
  35. } // namespace Eigen
  36. namespace Sophus {
  37. /// RxSO2 base type - implements RxSO2 class but is storage agnostic
  38. ///
  39. /// This class implements the group ``R+ x SO(2)``, the direct product of the
  40. /// group of positive scalar 2x2 matrices (= isomorph to the positive
  41. /// real numbers) and the two-dimensional special orthogonal group SO(2).
  42. /// Geometrically, it is the group of rotation and scaling in two dimensions.
  43. /// As a matrix groups, R+ x SO(2) consists of matrices of the form ``s * R``
  44. /// where ``R`` is an orthogonal matrix with ``det(R) = 1`` and ``s > 0``
  45. /// being a positive real number. In particular, it has the following form:
  46. ///
  47. /// | s * cos(theta) s * -sin(theta) |
  48. /// | s * sin(theta) s * cos(theta) |
  49. ///
  50. /// where ``theta`` being the rotation angle. Internally, it is represented by
  51. /// the first column of the rotation matrix, or in other words by a non-zero
  52. /// complex number.
  53. ///
  54. /// R+ x SO(2) is not compact, but a commutative group. First it is not compact
  55. /// since the scale factor is not bound. Second it is commutative since
  56. /// ``sR(alpha, s1) * sR(beta, s2) = sR(beta, s2) * sR(alpha, s1)``, simply
  57. /// because ``alpha + beta = beta + alpha`` and ``s1 * s2 = s2 * s1`` with
  58. /// ``alpha`` and ``beta`` being rotation angles and ``s1``, ``s2`` being scale
  59. /// factors.
  60. ///
  61. /// This class has the explicit class invariant that the scale ``s`` is not
  62. /// too close to zero. Strictly speaking, it must hold that:
  63. ///
  64. /// ``complex().norm() >= Constants::epsilon()``.
  65. ///
  66. /// In order to obey this condition, group multiplication is implemented with
  67. /// saturation such that a product always has a scale which is equal or greater
  68. /// this threshold.
  69. template <class Derived>
  70. class RxSO2Base {
  71. public:
  72. static constexpr int Options = Eigen::internal::traits<Derived>::Options;
  73. using Scalar = typename Eigen::internal::traits<Derived>::Scalar;
  74. using ComplexType = typename Eigen::internal::traits<Derived>::ComplexType;
  75. using ComplexTemporaryType = Sophus::Vector2<Scalar, Options>;
  76. /// Degrees of freedom of manifold, number of dimensions in tangent space
  77. /// (one for rotation and one for scaling).
  78. static int constexpr DoF = 2;
  79. /// Number of internal parameters used (complex number is a tuple).
  80. static int constexpr num_parameters = 2;
  81. /// Group transformations are 2x2 matrices.
  82. static int constexpr N = 2;
  83. using Transformation = Matrix<Scalar, N, N>;
  84. using Point = Vector2<Scalar>;
  85. using HomogeneousPoint = Vector3<Scalar>;
  86. using Line = ParametrizedLine2<Scalar>;
  87. using Tangent = Vector<Scalar, DoF>;
  88. using Adjoint = Matrix<Scalar, DoF, DoF>;
  89. /// For binary operations the return type is determined with the
  90. /// ScalarBinaryOpTraits feature of Eigen. This allows mixing concrete and Map
  91. /// types, as well as other compatible scalar types such as Ceres::Jet and
  92. /// double scalars with RxSO2 operations.
  93. template <typename OtherDerived>
  94. using ReturnScalar = typename Eigen::ScalarBinaryOpTraits<
  95. Scalar, typename OtherDerived::Scalar>::ReturnType;
  96. template <typename OtherDerived>
  97. using RxSO2Product = RxSO2<ReturnScalar<OtherDerived>>;
  98. template <typename PointDerived>
  99. using PointProduct = Vector2<ReturnScalar<PointDerived>>;
  100. template <typename HPointDerived>
  101. using HomogeneousPointProduct = Vector3<ReturnScalar<HPointDerived>>;
  102. /// Adjoint transformation
  103. ///
  104. /// This function return the adjoint transformation ``Ad`` of the group
  105. /// element ``A`` such that for all ``x`` it holds that
  106. /// ``hat(Ad_A * x) = A * hat(x) A^{-1}``. See hat-operator below.
  107. ///
  108. /// For RxSO(2), it simply returns the identity matrix.
  109. ///
  110. SOPHUS_FUNC Adjoint Adj() const { return Adjoint::Identity(); }
  111. /// Returns rotation angle.
  112. ///
  113. SOPHUS_FUNC Scalar angle() const { return SO2<Scalar>(complex()).log(); }
  114. /// Returns copy of instance casted to NewScalarType.
  115. ///
  116. template <class NewScalarType>
  117. SOPHUS_FUNC RxSO2<NewScalarType> cast() const {
  118. return RxSO2<NewScalarType>(complex().template cast<NewScalarType>());
  119. }
  120. /// This provides unsafe read/write access to internal data. RxSO(2) is
  121. /// represented by a complex number (two parameters). When using direct
  122. /// write access, the user needs to take care of that the complex number is
  123. /// not set close to zero.
  124. ///
  125. /// Note: The first parameter represents the real part, while the
  126. /// second parameter represent the imaginary part.
  127. ///
  128. SOPHUS_FUNC Scalar* data() { return complex_nonconst().data(); }
  129. /// Const version of data() above.
  130. ///
  131. SOPHUS_FUNC Scalar const* data() const { return complex().data(); }
  132. /// Returns group inverse.
  133. ///
  134. SOPHUS_FUNC RxSO2<Scalar> inverse() const {
  135. Scalar squared_scale = complex().squaredNorm();
  136. return RxSO2<Scalar>(complex().x() / squared_scale,
  137. -complex().y() / squared_scale);
  138. }
  139. /// Logarithmic map
  140. ///
  141. /// Computes the logarithm, the inverse of the group exponential which maps
  142. /// element of the group (scaled rotation matrices) to elements of the tangent
  143. /// space (rotation-vector plus logarithm of scale factor).
  144. ///
  145. /// To be specific, this function computes ``vee(logmat(.))`` with
  146. /// ``logmat(.)`` being the matrix logarithm and ``vee(.)`` the vee-operator
  147. /// of RxSO2.
  148. ///
  149. SOPHUS_FUNC Tangent log() const {
  150. using std::log;
  151. Tangent theta_sigma;
  152. theta_sigma[1] = log(scale());
  153. theta_sigma[0] = SO2<Scalar>(complex()).log();
  154. return theta_sigma;
  155. }
  156. /// Returns 2x2 matrix representation of the instance.
  157. ///
  158. /// For RxSO2, the matrix representation is an scaled orthogonal matrix ``sR``
  159. /// with ``det(R)=s^2``, thus a scaled rotation matrix ``R`` with scale
  160. /// ``s``.
  161. ///
  162. SOPHUS_FUNC Transformation matrix() const {
  163. Transformation sR;
  164. // clang-format off
  165. sR << complex()[0], -complex()[1],
  166. complex()[1], complex()[0];
  167. // clang-format on
  168. return sR;
  169. }
  170. /// Assignment-like operator from OtherDerived.
  171. ///
  172. template <class OtherDerived>
  173. SOPHUS_FUNC RxSO2Base<Derived>& operator=(
  174. RxSO2Base<OtherDerived> const& other) {
  175. complex_nonconst() = other.complex();
  176. return *this;
  177. }
  178. /// Group multiplication, which is rotation concatenation and scale
  179. /// multiplication.
  180. ///
  181. /// Note: This function performs saturation for products close to zero in
  182. /// order to ensure the class invariant.
  183. ///
  184. template <typename OtherDerived>
  185. SOPHUS_FUNC RxSO2Product<OtherDerived> operator*(
  186. RxSO2Base<OtherDerived> const& other) const {
  187. using ResultT = ReturnScalar<OtherDerived>;
  188. Scalar lhs_real = complex().x();
  189. Scalar lhs_imag = complex().y();
  190. typename OtherDerived::Scalar const& rhs_real = other.complex().x();
  191. typename OtherDerived::Scalar const& rhs_imag = other.complex().y();
  192. /// complex multiplication
  193. typename RxSO2Product<OtherDerived>::ComplexType result_complex(
  194. lhs_real * rhs_real - lhs_imag * rhs_imag,
  195. lhs_real * rhs_imag + lhs_imag * rhs_real);
  196. const ResultT squared_scale = result_complex.squaredNorm();
  197. if (squared_scale <
  198. Constants<ResultT>::epsilon() * Constants<ResultT>::epsilon()) {
  199. /// Saturation to ensure class invariant.
  200. result_complex.normalize();
  201. result_complex *= Constants<ResultT>::epsilon();
  202. }
  203. return RxSO2Product<OtherDerived>(result_complex);
  204. }
  205. /// Group action on 2-points.
  206. ///
  207. /// This function rotates a 2 dimensional point ``p`` by the SO2 element
  208. /// ``bar_R_foo`` (= rotation matrix) and scales it by the scale factor ``s``:
  209. ///
  210. /// ``p_bar = s * (bar_R_foo * p_foo)``.
  211. ///
  212. template <typename PointDerived,
  213. typename = typename std::enable_if<
  214. IsFixedSizeVector<PointDerived, 2>::value>::type>
  215. SOPHUS_FUNC PointProduct<PointDerived> operator*(
  216. Eigen::MatrixBase<PointDerived> const& p) const {
  217. return matrix() * p;
  218. }
  219. /// Group action on homogeneous 2-points. See above for more details.
  220. ///
  221. template <typename HPointDerived,
  222. typename = typename std::enable_if<
  223. IsFixedSizeVector<HPointDerived, 3>::value>::type>
  224. SOPHUS_FUNC HomogeneousPointProduct<HPointDerived> operator*(
  225. Eigen::MatrixBase<HPointDerived> const& p) const {
  226. const auto rsp = *this * p.template head<2>();
  227. return HomogeneousPointProduct<HPointDerived>(rsp(0), rsp(1), p(2));
  228. }
  229. /// Group action on lines.
  230. ///
  231. /// This function rotates a parameterized line ``l(t) = o + t * d`` by the SO2
  232. /// element and scales it by the scale factor
  233. ///
  234. /// Origin ``o`` is rotated and scaled
  235. /// Direction ``d`` is rotated (preserving it's norm)
  236. ///
  237. SOPHUS_FUNC Line operator*(Line const& l) const {
  238. return Line((*this) * l.origin(), (*this) * l.direction() / scale());
  239. }
  240. /// In-place group multiplication. This method is only valid if the return
  241. /// type of the multiplication is compatible with this SO2's Scalar type.
  242. ///
  243. /// Note: This function performs saturation for products close to zero in
  244. /// order to ensure the class invariant.
  245. ///
  246. template <typename OtherDerived,
  247. typename = typename std::enable_if<
  248. std::is_same<Scalar, ReturnScalar<OtherDerived>>::value>::type>
  249. SOPHUS_FUNC RxSO2Base<Derived>& operator*=(
  250. RxSO2Base<OtherDerived> const& other) {
  251. *static_cast<Derived*>(this) = *this * other;
  252. return *this;
  253. }
  254. /// Returns internal parameters of RxSO(2).
  255. ///
  256. /// It returns (c[0], c[1]), with c being the complex number.
  257. ///
  258. SOPHUS_FUNC Sophus::Vector<Scalar, num_parameters> params() const {
  259. return complex();
  260. }
  261. /// Sets non-zero complex
  262. ///
  263. /// Precondition: ``z`` must not be close to zero.
  264. SOPHUS_FUNC void setComplex(Vector2<Scalar> const& z) {
  265. SOPHUS_ENSURE(z.squaredNorm() > Constants<Scalar>::epsilon() *
  266. Constants<Scalar>::epsilon(),
  267. "Scale factor must be greater-equal epsilon.");
  268. static_cast<Derived*>(this)->complex_nonconst() = z;
  269. }
  270. /// Accessor of complex.
  271. ///
  272. SOPHUS_FUNC ComplexType const& complex() const {
  273. return static_cast<Derived const*>(this)->complex();
  274. }
  275. /// Returns rotation matrix.
  276. ///
  277. SOPHUS_FUNC Transformation rotationMatrix() const {
  278. ComplexTemporaryType norm_quad = complex();
  279. norm_quad.normalize();
  280. return SO2<Scalar>(norm_quad).matrix();
  281. }
  282. /// Returns scale.
  283. ///
  284. SOPHUS_FUNC
  285. Scalar scale() const { return complex().norm(); }
  286. /// Setter of rotation angle, leaves scale as is.
  287. ///
  288. SOPHUS_FUNC void setAngle(Scalar const& theta) { setSO2(SO2<Scalar>(theta)); }
  289. /// Setter of complex using rotation matrix ``R``, leaves scale as is.
  290. ///
  291. /// Precondition: ``R`` must be orthogonal with determinant of one.
  292. ///
  293. SOPHUS_FUNC void setRotationMatrix(Transformation const& R) {
  294. setSO2(SO2<Scalar>(R));
  295. }
  296. /// Sets scale and leaves rotation as is.
  297. ///
  298. SOPHUS_FUNC void setScale(Scalar const& scale) {
  299. using std::sqrt;
  300. complex_nonconst().normalize();
  301. complex_nonconst() *= scale;
  302. }
  303. /// Setter of complex number using scaled rotation matrix ``sR``.
  304. ///
  305. /// Precondition: The 2x2 matrix must be "scaled orthogonal"
  306. /// and have a positive determinant.
  307. ///
  308. SOPHUS_FUNC void setScaledRotationMatrix(Transformation const& sR) {
  309. SOPHUS_ENSURE(isScaledOrthogonalAndPositive(sR),
  310. "sR must be scaled orthogonal:\n %", sR);
  311. complex_nonconst() = sR.col(0);
  312. }
  313. /// Setter of SO(2) rotations, leaves scale as is.
  314. ///
  315. SOPHUS_FUNC void setSO2(SO2<Scalar> const& so2) {
  316. using std::sqrt;
  317. Scalar saved_scale = scale();
  318. complex_nonconst() = so2.unit_complex();
  319. complex_nonconst() *= saved_scale;
  320. }
  321. SOPHUS_FUNC SO2<Scalar> so2() const { return SO2<Scalar>(complex()); }
  322. private:
  323. /// Mutator of complex is private to ensure class invariant.
  324. ///
  325. SOPHUS_FUNC ComplexType& complex_nonconst() {
  326. return static_cast<Derived*>(this)->complex_nonconst();
  327. }
  328. };
  329. /// RxSO2 using storage; derived from RxSO2Base.
  330. template <class Scalar_, int Options>
  331. class RxSO2 : public RxSO2Base<RxSO2<Scalar_, Options>> {
  332. public:
  333. using Base = RxSO2Base<RxSO2<Scalar_, Options>>;
  334. using Scalar = Scalar_;
  335. using Transformation = typename Base::Transformation;
  336. using Point = typename Base::Point;
  337. using HomogeneousPoint = typename Base::HomogeneousPoint;
  338. using Tangent = typename Base::Tangent;
  339. using Adjoint = typename Base::Adjoint;
  340. using ComplexMember = Eigen::Matrix<Scalar, 2, 1, Options>;
  341. /// ``Base`` is friend so complex_nonconst can be accessed from ``Base``.
  342. friend class RxSO2Base<RxSO2<Scalar_, Options>>;
  343. using Base::operator=;
  344. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  345. /// Default constructor initializes complex number to identity rotation and
  346. /// scale to 1.
  347. ///
  348. SOPHUS_FUNC RxSO2() : complex_(Scalar(1), Scalar(0)) {}
  349. /// Copy constructor
  350. ///
  351. SOPHUS_FUNC RxSO2(RxSO2 const& other) = default;
  352. /// Copy-like constructor from OtherDerived.
  353. ///
  354. template <class OtherDerived>
  355. SOPHUS_FUNC RxSO2(RxSO2Base<OtherDerived> const& other)
  356. : complex_(other.complex()) {}
  357. /// Constructor from scaled rotation matrix
  358. ///
  359. /// Precondition: rotation matrix need to be scaled orthogonal with
  360. /// determinant of ``s^2``.
  361. ///
  362. SOPHUS_FUNC explicit RxSO2(Transformation const& sR) {
  363. this->setScaledRotationMatrix(sR);
  364. }
  365. /// Constructor from scale factor and rotation matrix ``R``.
  366. ///
  367. /// Precondition: Rotation matrix ``R`` must to be orthogonal with determinant
  368. /// of 1 and ``scale`` must to be close to zero.
  369. ///
  370. SOPHUS_FUNC RxSO2(Scalar const& scale, Transformation const& R)
  371. : RxSO2((scale * SO2<Scalar>(R).unit_complex()).eval()) {}
  372. /// Constructor from scale factor and SO2
  373. ///
  374. /// Precondition: ``scale`` must be close to zero.
  375. ///
  376. SOPHUS_FUNC RxSO2(Scalar const& scale, SO2<Scalar> const& so2)
  377. : RxSO2((scale * so2.unit_complex()).eval()) {}
  378. /// Constructor from complex number.
  379. ///
  380. /// Precondition: complex number must not be close to zero.
  381. ///
  382. SOPHUS_FUNC explicit RxSO2(Vector2<Scalar> const& z) : complex_(z) {
  383. SOPHUS_ENSURE(complex_.squaredNorm() >= Constants<Scalar>::epsilon() *
  384. Constants<Scalar>::epsilon(),
  385. "Scale factor must be greater-equal epsilon: % vs %",
  386. complex_.squaredNorm(),
  387. Constants<Scalar>::epsilon() * Constants<Scalar>::epsilon());
  388. }
  389. /// Constructor from complex number.
  390. ///
  391. /// Precondition: complex number must not be close to zero.
  392. ///
  393. SOPHUS_FUNC explicit RxSO2(Scalar const& real, Scalar const& imag)
  394. : RxSO2(Vector2<Scalar>(real, imag)) {}
  395. /// Accessor of complex.
  396. ///
  397. SOPHUS_FUNC ComplexMember const& complex() const { return complex_; }
  398. /// Returns derivative of exp(x).matrix() wrt. ``x_i at x=0``.
  399. ///
  400. SOPHUS_FUNC static Transformation Dxi_exp_x_matrix_at_0(int i) {
  401. return generator(i);
  402. }
  403. /// Group exponential
  404. ///
  405. /// This functions takes in an element of tangent space (= rotation angle
  406. /// plus logarithm of scale) and returns the corresponding element of the
  407. /// group RxSO2.
  408. ///
  409. /// To be more specific, this function computes ``expmat(hat(theta))``
  410. /// with ``expmat(.)`` being the matrix exponential and ``hat(.)`` being the
  411. /// hat()-operator of RSO2.
  412. ///
  413. SOPHUS_FUNC static RxSO2<Scalar> exp(Tangent const& a) {
  414. using std::exp;
  415. Scalar const theta = a[0];
  416. Scalar const sigma = a[1];
  417. Scalar s = exp(sigma);
  418. Vector2<Scalar> z = SO2<Scalar>::exp(theta).unit_complex();
  419. z *= s;
  420. return RxSO2<Scalar>(z);
  421. }
  422. /// Returns the ith infinitesimal generators of ``R+ x SO(2)``.
  423. ///
  424. /// The infinitesimal generators of RxSO2 are:
  425. ///
  426. /// ```
  427. /// | 0 -1 |
  428. /// G_0 = | 1 0 |
  429. ///
  430. /// | 1 0 |
  431. /// G_1 = | 0 1 |
  432. /// ```
  433. ///
  434. /// Precondition: ``i`` must be 0, or 1.
  435. ///
  436. SOPHUS_FUNC static Transformation generator(int i) {
  437. SOPHUS_ENSURE(i >= 0 && i <= 1, "i should be 0 or 1.");
  438. Tangent e;
  439. e.setZero();
  440. e[i] = Scalar(1);
  441. return hat(e);
  442. }
  443. /// hat-operator
  444. ///
  445. /// It takes in the 2-vector representation ``a`` (= rotation angle plus
  446. /// logarithm of scale) and returns the corresponding matrix representation
  447. /// of Lie algebra element.
  448. ///
  449. /// Formally, the hat()-operator of RxSO2 is defined as
  450. ///
  451. /// ``hat(.): R^2 -> R^{2x2}, hat(a) = sum_i a_i * G_i`` (for i=0,1,2)
  452. ///
  453. /// with ``G_i`` being the ith infinitesimal generator of RxSO2.
  454. ///
  455. /// The corresponding inverse is the vee()-operator, see below.
  456. ///
  457. SOPHUS_FUNC static Transformation hat(Tangent const& a) {
  458. Transformation A;
  459. // clang-format off
  460. A << a(1), -a(0),
  461. a(0), a(1);
  462. // clang-format on
  463. return A;
  464. }
  465. /// Lie bracket
  466. ///
  467. /// It computes the Lie bracket of RxSO(2). To be more specific, it computes
  468. ///
  469. /// ``[omega_1, omega_2]_rxso2 := vee([hat(omega_1), hat(omega_2)])``
  470. ///
  471. /// with ``[A,B] := AB-BA`` being the matrix commutator, ``hat(.)`` the
  472. /// hat()-operator and ``vee(.)`` the vee()-operator of RxSO2.
  473. ///
  474. SOPHUS_FUNC static Tangent lieBracket(Tangent const&, Tangent const&) {
  475. Vector2<Scalar> res;
  476. res.setZero();
  477. return res;
  478. }
  479. /// Draw uniform sample from RxSO(2) manifold.
  480. ///
  481. /// The scale factor is drawn uniformly in log2-space from [-1, 1],
  482. /// hence the scale is in [0.5, 2)].
  483. ///
  484. template <class UniformRandomBitGenerator>
  485. static RxSO2 sampleUniform(UniformRandomBitGenerator& generator) {
  486. std::uniform_real_distribution<Scalar> uniform(Scalar(-1), Scalar(1));
  487. using std::exp2;
  488. return RxSO2(exp2(uniform(generator)),
  489. SO2<Scalar>::sampleUniform(generator));
  490. }
  491. /// vee-operator
  492. ///
  493. /// It takes the 2x2-matrix representation ``Omega`` and maps it to the
  494. /// corresponding vector representation of Lie algebra.
  495. ///
  496. /// This is the inverse of the hat()-operator, see above.
  497. ///
  498. /// Precondition: ``Omega`` must have the following structure:
  499. ///
  500. /// | d -x |
  501. /// | x d |
  502. ///
  503. SOPHUS_FUNC static Tangent vee(Transformation const& Omega) {
  504. using std::abs;
  505. return Tangent(Omega(1, 0), Omega(0, 0));
  506. }
  507. protected:
  508. SOPHUS_FUNC ComplexMember& complex_nonconst() { return complex_; }
  509. ComplexMember complex_;
  510. };
  511. } // namespace Sophus
  512. namespace Eigen {
  513. /// Specialization of Eigen::Map for ``RxSO2``; derived from RxSO2Base.
  514. ///
  515. /// Allows us to wrap RxSO2 objects around POD array (e.g. external z style
  516. /// complex).
  517. template <class Scalar_, int Options>
  518. class Map<Sophus::RxSO2<Scalar_>, Options>
  519. : public Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_>, Options>> {
  520. using Base = Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_>, Options>>;
  521. public:
  522. using Scalar = Scalar_;
  523. using Transformation = typename Base::Transformation;
  524. using Point = typename Base::Point;
  525. using HomogeneousPoint = typename Base::HomogeneousPoint;
  526. using Tangent = typename Base::Tangent;
  527. using Adjoint = typename Base::Adjoint;
  528. /// ``Base`` is friend so complex_nonconst can be accessed from ``Base``.
  529. friend class Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_>, Options>>;
  530. using Base::operator=;
  531. using Base::operator*=;
  532. using Base::operator*;
  533. SOPHUS_FUNC Map(Scalar* coeffs) : complex_(coeffs) {}
  534. /// Accessor of complex.
  535. ///
  536. SOPHUS_FUNC
  537. Map<Sophus::Vector2<Scalar>, Options> const& complex() const {
  538. return complex_;
  539. }
  540. protected:
  541. SOPHUS_FUNC Map<Sophus::Vector2<Scalar>, Options>& complex_nonconst() {
  542. return complex_;
  543. }
  544. Map<Sophus::Vector2<Scalar>, Options> complex_;
  545. };
  546. /// Specialization of Eigen::Map for ``RxSO2 const``; derived from RxSO2Base.
  547. ///
  548. /// Allows us to wrap RxSO2 objects around POD array (e.g. external z style
  549. /// complex).
  550. template <class Scalar_, int Options>
  551. class Map<Sophus::RxSO2<Scalar_> const, Options>
  552. : public Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_> const, Options>> {
  553. public:
  554. using Base = Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_> const, Options>>;
  555. using Scalar = Scalar_;
  556. using Transformation = typename Base::Transformation;
  557. using Point = typename Base::Point;
  558. using HomogeneousPoint = typename Base::HomogeneousPoint;
  559. using Tangent = typename Base::Tangent;
  560. using Adjoint = typename Base::Adjoint;
  561. using Base::operator*=;
  562. using Base::operator*;
  563. SOPHUS_FUNC
  564. Map(Scalar const* coeffs) : complex_(coeffs) {}
  565. /// Accessor of complex.
  566. ///
  567. SOPHUS_FUNC
  568. Map<Sophus::Vector2<Scalar> const, Options> const& complex() const {
  569. return complex_;
  570. }
  571. protected:
  572. Map<Sophus::Vector2<Scalar> const, Options> const complex_;
  573. };
  574. } // namespace Eigen
  575. #endif /// SOPHUS_RXSO2_HPP