rxso2.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 operator.
  171. ///
  172. SOPHUS_FUNC RxSO2Base& operator=(RxSO2Base const& other) = default;
  173. /// Assignment-like operator from OtherDerived.
  174. ///
  175. template <class OtherDerived>
  176. SOPHUS_FUNC RxSO2Base<Derived>& operator=(
  177. RxSO2Base<OtherDerived> const& other) {
  178. complex_nonconst() = other.complex();
  179. return *this;
  180. }
  181. /// Group multiplication, which is rotation concatenation and scale
  182. /// multiplication.
  183. ///
  184. /// Note: This function performs saturation for products close to zero in
  185. /// order to ensure the class invariant.
  186. ///
  187. template <typename OtherDerived>
  188. SOPHUS_FUNC RxSO2Product<OtherDerived> operator*(
  189. RxSO2Base<OtherDerived> const& other) const {
  190. using ResultT = ReturnScalar<OtherDerived>;
  191. Scalar lhs_real = complex().x();
  192. Scalar lhs_imag = complex().y();
  193. typename OtherDerived::Scalar const& rhs_real = other.complex().x();
  194. typename OtherDerived::Scalar const& rhs_imag = other.complex().y();
  195. /// complex multiplication
  196. typename RxSO2Product<OtherDerived>::ComplexType result_complex(
  197. lhs_real * rhs_real - lhs_imag * rhs_imag,
  198. lhs_real * rhs_imag + lhs_imag * rhs_real);
  199. const ResultT squared_scale = result_complex.squaredNorm();
  200. if (squared_scale <
  201. Constants<ResultT>::epsilon() * Constants<ResultT>::epsilon()) {
  202. /// Saturation to ensure class invariant.
  203. result_complex.normalize();
  204. result_complex *= Constants<ResultT>::epsilon();
  205. }
  206. return RxSO2Product<OtherDerived>(result_complex);
  207. }
  208. /// Group action on 2-points.
  209. ///
  210. /// This function rotates a 2 dimensional point ``p`` by the SO2 element
  211. /// ``bar_R_foo`` (= rotation matrix) and scales it by the scale factor ``s``:
  212. ///
  213. /// ``p_bar = s * (bar_R_foo * p_foo)``.
  214. ///
  215. template <typename PointDerived,
  216. typename = typename std::enable_if<
  217. IsFixedSizeVector<PointDerived, 2>::value>::type>
  218. SOPHUS_FUNC PointProduct<PointDerived> operator*(
  219. Eigen::MatrixBase<PointDerived> const& p) const {
  220. return matrix() * p;
  221. }
  222. /// Group action on homogeneous 2-points. See above for more details.
  223. ///
  224. template <typename HPointDerived,
  225. typename = typename std::enable_if<
  226. IsFixedSizeVector<HPointDerived, 3>::value>::type>
  227. SOPHUS_FUNC HomogeneousPointProduct<HPointDerived> operator*(
  228. Eigen::MatrixBase<HPointDerived> const& p) const {
  229. const auto rsp = *this * p.template head<2>();
  230. return HomogeneousPointProduct<HPointDerived>(rsp(0), rsp(1), p(2));
  231. }
  232. /// Group action on lines.
  233. ///
  234. /// This function rotates a parameterized line ``l(t) = o + t * d`` by the SO2
  235. /// element and scales it by the scale factor
  236. ///
  237. /// Origin ``o`` is rotated and scaled
  238. /// Direction ``d`` is rotated (preserving it's norm)
  239. ///
  240. SOPHUS_FUNC Line operator*(Line const& l) const {
  241. return Line((*this) * l.origin(), (*this) * l.direction() / scale());
  242. }
  243. /// In-place group multiplication. This method is only valid if the return
  244. /// type of the multiplication is compatible with this SO2's Scalar type.
  245. ///
  246. /// Note: This function performs saturation for products close to zero in
  247. /// order to ensure the class invariant.
  248. ///
  249. template <typename OtherDerived,
  250. typename = typename std::enable_if<
  251. std::is_same<Scalar, ReturnScalar<OtherDerived>>::value>::type>
  252. SOPHUS_FUNC RxSO2Base<Derived>& operator*=(
  253. RxSO2Base<OtherDerived> const& other) {
  254. *static_cast<Derived*>(this) = *this * other;
  255. return *this;
  256. }
  257. /// Returns internal parameters of RxSO(2).
  258. ///
  259. /// It returns (c[0], c[1]), with c being the complex number.
  260. ///
  261. SOPHUS_FUNC Sophus::Vector<Scalar, num_parameters> params() const {
  262. return complex();
  263. }
  264. /// Sets non-zero complex
  265. ///
  266. /// Precondition: ``z`` must not be close to zero.
  267. SOPHUS_FUNC void setComplex(Vector2<Scalar> const& z) {
  268. SOPHUS_ENSURE(z.squaredNorm() > Constants<Scalar>::epsilon() *
  269. Constants<Scalar>::epsilon(),
  270. "Scale factor must be greater-equal epsilon.");
  271. static_cast<Derived*>(this)->complex_nonconst() = z;
  272. }
  273. /// Accessor of complex.
  274. ///
  275. SOPHUS_FUNC ComplexType const& complex() const {
  276. return static_cast<Derived const*>(this)->complex();
  277. }
  278. /// Returns rotation matrix.
  279. ///
  280. SOPHUS_FUNC Transformation rotationMatrix() const {
  281. ComplexTemporaryType norm_quad = complex();
  282. norm_quad.normalize();
  283. return SO2<Scalar>(norm_quad).matrix();
  284. }
  285. /// Returns scale.
  286. ///
  287. SOPHUS_FUNC
  288. Scalar scale() const { return complex().norm(); }
  289. /// Setter of rotation angle, leaves scale as is.
  290. ///
  291. SOPHUS_FUNC void setAngle(Scalar const& theta) { setSO2(SO2<Scalar>(theta)); }
  292. /// Setter of complex using rotation matrix ``R``, leaves scale as is.
  293. ///
  294. /// Precondition: ``R`` must be orthogonal with determinant of one.
  295. ///
  296. SOPHUS_FUNC void setRotationMatrix(Transformation const& R) {
  297. setSO2(SO2<Scalar>(R));
  298. }
  299. /// Sets scale and leaves rotation as is.
  300. ///
  301. SOPHUS_FUNC void setScale(Scalar const& scale) {
  302. using std::sqrt;
  303. complex_nonconst().normalize();
  304. complex_nonconst() *= scale;
  305. }
  306. /// Setter of complex number using scaled rotation matrix ``sR``.
  307. ///
  308. /// Precondition: The 2x2 matrix must be "scaled orthogonal"
  309. /// and have a positive determinant.
  310. ///
  311. SOPHUS_FUNC void setScaledRotationMatrix(Transformation const& sR) {
  312. SOPHUS_ENSURE(isScaledOrthogonalAndPositive(sR),
  313. "sR must be scaled orthogonal:\n %", sR);
  314. complex_nonconst() = sR.col(0);
  315. }
  316. /// Setter of SO(2) rotations, leaves scale as is.
  317. ///
  318. SOPHUS_FUNC void setSO2(SO2<Scalar> const& so2) {
  319. using std::sqrt;
  320. Scalar saved_scale = scale();
  321. complex_nonconst() = so2.unit_complex();
  322. complex_nonconst() *= saved_scale;
  323. }
  324. SOPHUS_FUNC SO2<Scalar> so2() const { return SO2<Scalar>(complex()); }
  325. protected:
  326. /// Mutator of complex is private to ensure class invariant.
  327. ///
  328. SOPHUS_FUNC ComplexType& complex_nonconst() {
  329. return static_cast<Derived*>(this)->complex_nonconst();
  330. }
  331. };
  332. /// RxSO2 using storage; derived from RxSO2Base.
  333. template <class Scalar_, int Options>
  334. class RxSO2 : public RxSO2Base<RxSO2<Scalar_, Options>> {
  335. public:
  336. using Base = RxSO2Base<RxSO2<Scalar_, Options>>;
  337. using Scalar = Scalar_;
  338. using Transformation = typename Base::Transformation;
  339. using Point = typename Base::Point;
  340. using HomogeneousPoint = typename Base::HomogeneousPoint;
  341. using Tangent = typename Base::Tangent;
  342. using Adjoint = typename Base::Adjoint;
  343. using ComplexMember = Eigen::Matrix<Scalar, 2, 1, Options>;
  344. /// ``Base`` is friend so complex_nonconst can be accessed from ``Base``.
  345. friend class RxSO2Base<RxSO2<Scalar_, Options>>;
  346. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  347. /// Default constructor initializes complex number to identity rotation and
  348. /// scale to 1.
  349. ///
  350. SOPHUS_FUNC RxSO2() : complex_(Scalar(1), Scalar(0)) {}
  351. /// Copy constructor
  352. ///
  353. SOPHUS_FUNC RxSO2(RxSO2 const& other) = default;
  354. /// Copy-like constructor from OtherDerived.
  355. ///
  356. template <class OtherDerived>
  357. SOPHUS_FUNC RxSO2(RxSO2Base<OtherDerived> const& other)
  358. : complex_(other.complex()) {}
  359. /// Constructor from scaled rotation matrix
  360. ///
  361. /// Precondition: rotation matrix need to be scaled orthogonal with
  362. /// determinant of ``s^2``.
  363. ///
  364. SOPHUS_FUNC explicit RxSO2(Transformation const& sR) {
  365. this->setScaledRotationMatrix(sR);
  366. }
  367. /// Constructor from scale factor and rotation matrix ``R``.
  368. ///
  369. /// Precondition: Rotation matrix ``R`` must to be orthogonal with determinant
  370. /// of 1 and ``scale`` must to be close to zero.
  371. ///
  372. SOPHUS_FUNC RxSO2(Scalar const& scale, Transformation const& R)
  373. : RxSO2((scale * SO2<Scalar>(R).unit_complex()).eval()) {}
  374. /// Constructor from scale factor and SO2
  375. ///
  376. /// Precondition: ``scale`` must be close to zero.
  377. ///
  378. SOPHUS_FUNC RxSO2(Scalar const& scale, SO2<Scalar> const& so2)
  379. : RxSO2((scale * so2.unit_complex()).eval()) {}
  380. /// Constructor from complex number.
  381. ///
  382. /// Precondition: complex number must not be close to zero.
  383. ///
  384. SOPHUS_FUNC explicit RxSO2(Vector2<Scalar> const& z) : complex_(z) {
  385. SOPHUS_ENSURE(complex_.squaredNorm() >= Constants<Scalar>::epsilon() *
  386. Constants<Scalar>::epsilon(),
  387. "Scale factor must be greater-equal epsilon: % vs %",
  388. complex_.squaredNorm(),
  389. Constants<Scalar>::epsilon() * Constants<Scalar>::epsilon());
  390. }
  391. /// Constructor from complex number.
  392. ///
  393. /// Precondition: complex number must not be close to zero.
  394. ///
  395. SOPHUS_FUNC explicit RxSO2(Scalar const& real, Scalar const& imag)
  396. : RxSO2(Vector2<Scalar>(real, imag)) {}
  397. /// Accessor of complex.
  398. ///
  399. SOPHUS_FUNC ComplexMember const& complex() const { return complex_; }
  400. /// Returns derivative of exp(x).matrix() wrt. ``x_i at x=0``.
  401. ///
  402. SOPHUS_FUNC static Transformation Dxi_exp_x_matrix_at_0(int i) {
  403. return generator(i);
  404. }
  405. /// Group exponential
  406. ///
  407. /// This functions takes in an element of tangent space (= rotation angle
  408. /// plus logarithm of scale) and returns the corresponding element of the
  409. /// group RxSO2.
  410. ///
  411. /// To be more specific, this function computes ``expmat(hat(theta))``
  412. /// with ``expmat(.)`` being the matrix exponential and ``hat(.)`` being the
  413. /// hat()-operator of RSO2.
  414. ///
  415. SOPHUS_FUNC static RxSO2<Scalar> exp(Tangent const& a) {
  416. using std::exp;
  417. Scalar const theta = a[0];
  418. Scalar const sigma = a[1];
  419. Scalar s = exp(sigma);
  420. Vector2<Scalar> z = SO2<Scalar>::exp(theta).unit_complex();
  421. z *= s;
  422. return RxSO2<Scalar>(z);
  423. }
  424. /// Returns the ith infinitesimal generators of ``R+ x SO(2)``.
  425. ///
  426. /// The infinitesimal generators of RxSO2 are:
  427. ///
  428. /// ```
  429. /// | 0 -1 |
  430. /// G_0 = | 1 0 |
  431. ///
  432. /// | 1 0 |
  433. /// G_1 = | 0 1 |
  434. /// ```
  435. ///
  436. /// Precondition: ``i`` must be 0, or 1.
  437. ///
  438. SOPHUS_FUNC static Transformation generator(int i) {
  439. SOPHUS_ENSURE(i >= 0 && i <= 1, "i should be 0 or 1.");
  440. Tangent e;
  441. e.setZero();
  442. e[i] = Scalar(1);
  443. return hat(e);
  444. }
  445. /// hat-operator
  446. ///
  447. /// It takes in the 2-vector representation ``a`` (= rotation angle plus
  448. /// logarithm of scale) and returns the corresponding matrix representation
  449. /// of Lie algebra element.
  450. ///
  451. /// Formally, the hat()-operator of RxSO2 is defined as
  452. ///
  453. /// ``hat(.): R^2 -> R^{2x2}, hat(a) = sum_i a_i * G_i`` (for i=0,1,2)
  454. ///
  455. /// with ``G_i`` being the ith infinitesimal generator of RxSO2.
  456. ///
  457. /// The corresponding inverse is the vee()-operator, see below.
  458. ///
  459. SOPHUS_FUNC static Transformation hat(Tangent const& a) {
  460. Transformation A;
  461. // clang-format off
  462. A << a(1), -a(0),
  463. a(0), a(1);
  464. // clang-format on
  465. return A;
  466. }
  467. /// Lie bracket
  468. ///
  469. /// It computes the Lie bracket of RxSO(2). To be more specific, it computes
  470. ///
  471. /// ``[omega_1, omega_2]_rxso2 := vee([hat(omega_1), hat(omega_2)])``
  472. ///
  473. /// with ``[A,B] := AB-BA`` being the matrix commutator, ``hat(.)`` the
  474. /// hat()-operator and ``vee(.)`` the vee()-operator of RxSO2.
  475. ///
  476. SOPHUS_FUNC static Tangent lieBracket(Tangent const&, Tangent const&) {
  477. Vector2<Scalar> res;
  478. res.setZero();
  479. return res;
  480. }
  481. /// Draw uniform sample from RxSO(2) manifold.
  482. ///
  483. /// The scale factor is drawn uniformly in log2-space from [-1, 1],
  484. /// hence the scale is in [0.5, 2)].
  485. ///
  486. template <class UniformRandomBitGenerator>
  487. static RxSO2 sampleUniform(UniformRandomBitGenerator& generator) {
  488. std::uniform_real_distribution<Scalar> uniform(Scalar(-1), Scalar(1));
  489. using std::exp2;
  490. return RxSO2(exp2(uniform(generator)),
  491. SO2<Scalar>::sampleUniform(generator));
  492. }
  493. /// vee-operator
  494. ///
  495. /// It takes the 2x2-matrix representation ``Omega`` and maps it to the
  496. /// corresponding vector representation of Lie algebra.
  497. ///
  498. /// This is the inverse of the hat()-operator, see above.
  499. ///
  500. /// Precondition: ``Omega`` must have the following structure:
  501. ///
  502. /// | d -x |
  503. /// | x d |
  504. ///
  505. SOPHUS_FUNC static Tangent vee(Transformation const& Omega) {
  506. using std::abs;
  507. return Tangent(Omega(1, 0), Omega(0, 0));
  508. }
  509. protected:
  510. SOPHUS_FUNC ComplexMember& complex_nonconst() { return complex_; }
  511. ComplexMember complex_;
  512. };
  513. } // namespace Sophus
  514. namespace Eigen {
  515. /// Specialization of Eigen::Map for ``RxSO2``; derived from RxSO2Base.
  516. ///
  517. /// Allows us to wrap RxSO2 objects around POD array (e.g. external z style
  518. /// complex).
  519. template <class Scalar_, int Options>
  520. class Map<Sophus::RxSO2<Scalar_>, Options>
  521. : public Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_>, Options>> {
  522. using Base = Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_>, Options>>;
  523. public:
  524. using Scalar = Scalar_;
  525. using Transformation = typename Base::Transformation;
  526. using Point = typename Base::Point;
  527. using HomogeneousPoint = typename Base::HomogeneousPoint;
  528. using Tangent = typename Base::Tangent;
  529. using Adjoint = typename Base::Adjoint;
  530. /// ``Base`` is friend so complex_nonconst can be accessed from ``Base``.
  531. friend class Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_>, Options>>;
  532. // LCOV_EXCL_START
  533. SOPHUS_INHERIT_ASSIGNMENT_OPERATORS(Map);
  534. // LCOV_EXCL_STOP
  535. using Base::operator*=;
  536. using Base::operator*;
  537. SOPHUS_FUNC Map(Scalar* coeffs) : complex_(coeffs) {}
  538. /// Accessor of complex.
  539. ///
  540. SOPHUS_FUNC
  541. Map<Sophus::Vector2<Scalar>, Options> const& complex() const {
  542. return complex_;
  543. }
  544. protected:
  545. SOPHUS_FUNC Map<Sophus::Vector2<Scalar>, Options>& complex_nonconst() {
  546. return complex_;
  547. }
  548. Map<Sophus::Vector2<Scalar>, Options> complex_;
  549. };
  550. /// Specialization of Eigen::Map for ``RxSO2 const``; derived from RxSO2Base.
  551. ///
  552. /// Allows us to wrap RxSO2 objects around POD array (e.g. external z style
  553. /// complex).
  554. template <class Scalar_, int Options>
  555. class Map<Sophus::RxSO2<Scalar_> const, Options>
  556. : public Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_> const, Options>> {
  557. public:
  558. using Base = Sophus::RxSO2Base<Map<Sophus::RxSO2<Scalar_> const, Options>>;
  559. using Scalar = Scalar_;
  560. using Transformation = typename Base::Transformation;
  561. using Point = typename Base::Point;
  562. using HomogeneousPoint = typename Base::HomogeneousPoint;
  563. using Tangent = typename Base::Tangent;
  564. using Adjoint = typename Base::Adjoint;
  565. using Base::operator*=;
  566. using Base::operator*;
  567. SOPHUS_FUNC
  568. Map(Scalar const* coeffs) : complex_(coeffs) {}
  569. /// Accessor of complex.
  570. ///
  571. SOPHUS_FUNC
  572. Map<Sophus::Vector2<Scalar> const, Options> const& complex() const {
  573. return complex_;
  574. }
  575. protected:
  576. Map<Sophus::Vector2<Scalar> const, Options> const complex_;
  577. };
  578. } // namespace Eigen
  579. #endif /// SOPHUS_RXSO2_HPP