smooth
A C++ library for Lie theory
Loading...
Searching...
No Matches
se2.hpp
1// Copyright (C) 2021-2022 Petter Nilsson. MIT License.
2
3#pragma once
4
5#include <Eigen/Core>
6#include <Eigen/Geometry>
7
8#include "detail/macro.hpp"
9#include "detail/se2.hpp"
10#include "lie_group_base.hpp"
11#include "so2.hpp"
12
13SMOOTH_BEGIN_NAMESPACE
14
15// \cond
16template<typename Scalar>
17class SE3;
18// \endcond
19
62template<typename _Derived>
63class SE2Base : public LieGroupBase<_Derived>
64{
66
67protected:
68 SE2Base() = default;
69
70public:
71 SMOOTH_INHERIT_TYPEDEFS;
72
77 requires is_mutable
78 {
79 return Map<SO2<Scalar>>(static_cast<_Derived &>(*this).data() + 2);
80 }
81
85 Map<const SO2<Scalar>> so2() const { return Map<const SO2<Scalar>>(static_cast<const _Derived &>(*this).data() + 2); }
86
90 Eigen::Map<Eigen::Vector2<Scalar>> r2()
91 requires is_mutable
92 {
93 return Eigen::Map<Eigen::Vector2<Scalar>>(static_cast<_Derived &>(*this).data());
94 }
95
99 Eigen::Map<const Eigen::Vector2<Scalar>> r2() const
100 {
101 return Eigen::Map<const Eigen::Vector2<Scalar>>(static_cast<const _Derived &>(*this).data());
102 }
103
107 Eigen::Transform<Scalar, 2, Eigen::Isometry> isometry() const
108 {
109 return Eigen::Translation<Scalar, 2>(r2()) * Eigen::Rotation2D<Scalar>(so2().angle());
110 }
111
115 template<typename EigenDerived>
116 Eigen::Vector2<Scalar> operator*(const Eigen::MatrixBase<EigenDerived> & v) const
117 {
118 return so2() * v + r2();
119 }
120
128 template<typename EigenDerived>
129 Eigen::Matrix<Scalar, 2, 3> dr_action(const Eigen::MatrixBase<EigenDerived> & v) const
130 {
131 Eigen::Matrix<Scalar, 2, 3> ret;
132 ret.template leftCols<2>() = so2().matrix();
133 ret.template rightCols<1>() = so2().dr_action(v);
134 return ret;
135 }
136
143 {
144 return SE3<Scalar>(so2().lift_so3(), Eigen::Vector3<Scalar>(r2().x(), r2().y(), Scalar(0)));
145 }
146};
147
148// \cond
149template<typename _Scalar>
150class SE2;
151// \endcond
152
153// \cond
154template<typename _Scalar>
155struct liebase_info<SE2<_Scalar>>
156{
157 static constexpr bool is_mutable = true;
158
159 using Impl = SE2Impl<_Scalar>;
160 using Scalar = _Scalar;
161
162 template<typename NewScalar>
164};
165// \endcond
166
172template<typename _Scalar>
173class SE2 : public SE2Base<SE2<_Scalar>>
174{
175 using Base = SE2Base<SE2<_Scalar>>;
176
177 SMOOTH_GROUP_API(SE2);
178
179public:
186 template<typename SO2Derived, typename T2Derived>
187 SE2(const SO2Base<SO2Derived> & so2, const Eigen::MatrixBase<T2Derived> & r2)
188 {
189 Base::so2() = static_cast<const SO2Derived &>(so2);
190 Base::r2() = static_cast<const T2Derived &>(r2);
191 }
192
196 explicit SE2(const Eigen::Transform<Scalar, 2, Eigen::Isometry> & t)
197 {
198 Eigen::Matrix2<Scalar> rotmat = t.rotation();
199 coeffs().x() = t.translation().x();
200 coeffs().y() = t.translation().y();
201 coeffs().z() = rotmat(1, 0); // sin(angle)
202 coeffs().w() = rotmat(0, 0); // cos(angle)
203 }
204};
205
206// MAP TYPE TRAITS
207
208// \cond
209template<typename _Scalar>
210struct liebase_info<Map<SE2<_Scalar>>> : public liebase_info<SE2<_Scalar>>
211{};
212// \endcond
213
219template<typename _Scalar>
220class Map<SE2<_Scalar>> : public SE2Base<Map<SE2<_Scalar>>>
221{
223
224 SMOOTH_MAP_API();
225};
226
227// \cond
228template<typename _Scalar>
229struct liebase_info<Map<const SE2<_Scalar>>> : public liebase_info<SE2<_Scalar>>
230{
231 static constexpr bool is_mutable = false;
232};
233// \endcond
234
240template<typename _Scalar>
241class Map<const SE2<_Scalar>> : public SE2Base<Map<const SE2<_Scalar>>>
242{
244
245 SMOOTH_CONST_MAP_API();
246};
247
248using SE2f = SE2<float>;
249using SE2d = SE2<double>;
250
251SMOOTH_END_NAMESPACE
252
253#if __has_include(<format>)
254#include <format>
255#include <string>
256
257template<class Scalar>
258struct std::formatter<smooth::SE2<Scalar>>
259{
260 std::string m_format;
261
262 constexpr auto parse(std::format_parse_context & ctx)
263 {
264 m_format = "{:";
265 for (auto it = ctx.begin(); it != ctx.end(); ++it) {
266 char c = *it;
267 m_format += c;
268 if (c == '}') return it;
269 }
270 return ctx.end();
271 }
272
273 auto format(const smooth::SE2<Scalar> & obj, std::format_context & ctx) const
274 {
275 const auto fmtSting = std::format("r2: [{0}, {0}], so2: {0}", m_format);
276 return std::vformat_to(ctx.out(), fmtSting, std::make_format_args(obj.r2().x(), obj.r2().y(), obj.so2().angle()));
277 }
278};
279
280#endif
Base class for Lie group types.
typename traits::Scalar Scalar
Scalar type.
auto & coeffs() const
Access underlying storages.
static constexpr bool is_mutable
True if underlying storage supports modification.
Eigen::Matrix< Scalar, Dim, Dim > Matrix
Lie group matrix type.
Memory mapping of internal Lie group types.
Base class for SE2 Lie group types.
Definition se2.hpp:64
Map< const SO2< Scalar > > so2() const
Const access SO(2) part.
Definition se2.hpp:85
Eigen::Vector2< Scalar > operator*(const Eigen::MatrixBase< EigenDerived > &v) const
Tranformation action on 2D vector.
Definition se2.hpp:116
Eigen::Map< const Eigen::Vector2< Scalar > > r2() const
Const access R2 part.
Definition se2.hpp:99
Map< SO2< Scalar > > so2()
Access SO(2) part.
Definition se2.hpp:76
Eigen::Transform< Scalar, 2, Eigen::Isometry > isometry() const
Return as 2D Eigen transform.
Definition se2.hpp:107
Eigen::Map< Eigen::Vector2< Scalar > > r2()
Access R2 part.
Definition se2.hpp:90
SE3< Scalar > lift_se3() const
Lift to SE3.
Definition se2.hpp:142
Eigen::Matrix< Scalar, 2, 3 > dr_action(const Eigen::MatrixBase< EigenDerived > &v) const
Jacobian of rotation action w.r.t. group.
Definition se2.hpp:129
Storage implementation of SE2 Lie group.
Definition se2.hpp:174
SE2(const SO2Base< SO2Derived > &so2, const Eigen::MatrixBase< T2Derived > &r2)
Construct from SO2 and R2.
Definition se2.hpp:187
SE2(const Eigen::Transform< Scalar, 2, Eigen::Isometry > &t)
Construct from Eigen transform.
Definition se2.hpp:196
Storage implementation of SE3 Lie group.
Definition se3.hpp:171
typename traits::man< M >::Scalar Scalar
Manifold scalar type.
Definition manifold.hpp:88
typename traits::man< M >::PlainObject PlainObject
Manifold default type.
Definition manifold.hpp:94
Type trait that maps a type to Lie group operations.