smooth
A C++ library for Lie theory
Loading...
Searching...
No Matches
se3.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/se3.hpp"
10#include "lie_group_base.hpp"
11#include "so3.hpp"
12
13SMOOTH_BEGIN_NAMESPACE
14
15template<typename Scalar>
16class SE2;
17
62template<typename _Derived>
63class SE3Base : public LieGroupBase<_Derived>
64{
66
67protected:
68 SE3Base() = default;
69
70public:
71 SMOOTH_INHERIT_TYPEDEFS;
72
77 requires is_mutable
78 {
79 return Map<SO3<Scalar>>(static_cast<_Derived &>(*this).data() + 3);
80 }
81
85 Map<const SO3<Scalar>> so3() const { return Map<const SO3<Scalar>>(static_cast<const _Derived &>(*this).data() + 3); }
86
90 Eigen::Map<Eigen::Vector3<Scalar>> r3()
91 requires is_mutable
92 {
93 return Eigen::Map<Eigen::Vector3<Scalar>>(static_cast<_Derived &>(*this).data());
94 }
95
99 Eigen::Map<const Eigen::Vector3<Scalar>> r3() const
100 {
101 return Eigen::Map<const Eigen::Vector3<Scalar>>(static_cast<const _Derived &>(*this).data());
102 }
103
107 Eigen::Transform<Scalar, 3, Eigen::Isometry> isometry() const
108 {
109 return Eigen::Translation<Scalar, 3>(r3()) * so3().quat();
110 }
111
115 template<typename EigenDerived>
116 Eigen::Vector3<Scalar> operator*(const Eigen::MatrixBase<EigenDerived> & v) const
117 {
118 return so3() * v + r3();
119 }
120
128 template<typename EigenDerived>
129 Eigen::Matrix<Scalar, 3, 6> dr_action(const Eigen::MatrixBase<EigenDerived> & v) const
130 {
131 Eigen::Matrix<Scalar, 3, 6> ret;
132 ret.template leftCols<3>() = so3().matrix();
133 ret.template rightCols<3>() = so3().dr_action(v);
134 return ret;
135 }
136
142 SE2<Scalar> project_se2() const { return SE2<Scalar>(so3().project_so2(), r3().template head<2>()); }
143};
144
145// \cond
146template<typename _Scalar>
147class SE3;
148// \endcond
149
150// \cond
151template<typename _Scalar>
152struct liebase_info<SE3<_Scalar>>
153{
154 static constexpr bool is_mutable = true;
155
156 using Impl = SE3Impl<_Scalar>;
157 using Scalar = _Scalar;
158
159 template<typename NewScalar>
161};
162// \endcond
163
169template<typename _Scalar>
170class SE3 : public SE3Base<SE3<_Scalar>>
171{
172 using Base = SE3Base<SE3<_Scalar>>;
173
174 SMOOTH_GROUP_API(SE3);
175
176public:
183 template<typename SO3Derived, typename T3Derived>
184 SE3(const SO3Base<SO3Derived> & so3, const Eigen::MatrixBase<T3Derived> & r3)
185 {
186 Base::so3() = static_cast<const SO3Derived &>(so3);
187 Base::r3() = static_cast<const T3Derived &>(r3);
188 }
189
193 explicit SE3(const Eigen::Transform<Scalar, 3, Eigen::Isometry> & t)
194 {
195 Base::so3() = smooth::SO3<Scalar>(Eigen::Quaternion<Scalar>(t.rotation()));
196 Base::r3() = t.translation();
197 }
198};
199
200// \cond
201template<typename _Scalar>
202struct liebase_info<Map<SE3<_Scalar>>> : public liebase_info<SE3<_Scalar>>
203{};
204// \endcond
205
211template<typename _Scalar>
212class Map<SE3<_Scalar>> : public SE3Base<Map<SE3<_Scalar>>>
213{
215
216 SMOOTH_MAP_API();
217};
218
219// \cond
220template<typename _Scalar>
221struct liebase_info<Map<const SE3<_Scalar>>> : public liebase_info<SE3<_Scalar>>
222{
223 static constexpr bool is_mutable = false;
224};
225// \endcond
226
232template<typename _Scalar>
233class Map<const SE3<_Scalar>> : public SE3Base<Map<const SE3<_Scalar>>>
234{
236
237 SMOOTH_CONST_MAP_API();
238};
239
240using SE3f = SE3<float>;
241using SE3d = SE3<double>;
242
243SMOOTH_END_NAMESPACE
244
245// Std format
246#if __has_include(<format>)
247#include <format>
248#include <string>
249
250template<class Scalar>
251struct std::formatter<smooth::SE3<Scalar>>
252{
253 std::string m_format;
254
255 constexpr auto parse(std::format_parse_context & ctx)
256 {
257 m_format = "{:";
258 for (auto it = ctx.begin(); it != ctx.end(); ++it) {
259 char c = *it;
260 m_format += c;
261 if (c == '}') return it;
262 }
263 return ctx.end();
264 }
265
266 auto format(const smooth::SE3<Scalar> & obj, std::format_context & ctx) const
267 {
268 const auto fmtSting = std::format("r3: [{0}, {0}, {0}], so3: [{0}, {0}, {0}, {0}]", m_format);
269 return std::vformat_to(
270 ctx.out(),
271 fmtSting,
272 std::make_format_args(
273 obj.r3().x(),
274 obj.r3().y(),
275 obj.r3().z(),
276 obj.so3().quat().w(),
277 obj.so3().quat().x(),
278 obj.so3().quat().y(),
279 obj.so3().quat().z()));
280 }
281};
282
283#endif
Base class for Lie group types.
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.
Storage implementation of SE2 Lie group.
Definition se2.hpp:174
Base class for SE3 Lie group types.
Definition se3.hpp:64
Eigen::Transform< Scalar, 3, Eigen::Isometry > isometry() const
Return as 3D Eigen transform.
Definition se3.hpp:107
Eigen::Map< Eigen::Vector3< Scalar > > r3()
Access R3 part.
Definition se3.hpp:90
Eigen::Vector3< Scalar > operator*(const Eigen::MatrixBase< EigenDerived > &v) const
Tranformation action on 3D vector.
Definition se3.hpp:116
Map< SO3< Scalar > > so3()
Access SO(3) part.
Definition se3.hpp:76
Eigen::Matrix< Scalar, 3, 6 > dr_action(const Eigen::MatrixBase< EigenDerived > &v) const
Jacobian of rotation action w.r.t. group.
Definition se3.hpp:129
Eigen::Map< const Eigen::Vector3< Scalar > > r3() const
Const access R3 part.
Definition se3.hpp:99
SE2< Scalar > project_se2() const
Project to SE2.
Definition se3.hpp:142
Map< const SO3< Scalar > > so3() const
Const access SO(3) part.
Definition se3.hpp:85
Storage implementation of SE3 Lie group.
Definition se3.hpp:171
SE3(const SO3Base< SO3Derived > &so3, const Eigen::MatrixBase< T3Derived > &r3)
Construct from SO3 and translation.
Definition se3.hpp:184
SE3(const Eigen::Transform< Scalar, 3, Eigen::Isometry > &t)
Construct from Eigen transform.
Definition se3.hpp:193
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.