LieAlgebra_so3.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2013 CEA LIST (DIASI/LSI) <xde-support@saxifrage.cea.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_LGSM_LIE_ALGEBRA_so3_H
11 #define EIGEN_LGSM_LIE_ALGEBRA_so3_H
12 
13 /***************************************************************************
14 * Function used in exp derivatives
15 ***************************************************************************/
16 
17 template<typename Scalar> inline Scalar ei_lie_algebra_so3_derivative_f2(Scalar n, Scalar n2, Scalar cos_n, Scalar precision){
18  if(n2 < precision)
19  return (Scalar(0.5) + (Scalar(-1.) + n2 / Scalar(30.)) * n2 / Scalar(24.) );
20  else
21  return (Scalar(1.0) - cos_n)/n2;
22 }
23 
24 template<typename Scalar> EIGEN_STRONG_INLINE Scalar ei_lie_algebra_so3_derivative_df2(Scalar n, Scalar n2, Scalar cos_n, Scalar sin_n, Scalar precision){ // 1.e-2
25  if ( n2 < precision )
26  return ( (Scalar(-1.)+(Scalar(1.)/Scalar(15.)+(Scalar(-1.)/Scalar(560.)+n2/Scalar(37800.))*n2)*n2)/Scalar(12.) );
27  else
28  return ( Scalar(2.)*( Scalar(0.5)*(n2-Scalar(2.)+Scalar(2.)*cos_n )/(n2*n2) ) - (n - std::sin(n))/(n2*n) );
29 }
30 
31 template<typename Scalar> inline Scalar ei_lie_algebra_so3_derivative_f3(Scalar n, Scalar n2, Scalar sin_n, Scalar precision){
32  if(n2 < precision)
33  return (Scalar(20.)+(Scalar(-1.)+n2/Scalar(42.))*n2)/Scalar(120.);
34  else
35  return (n - sin_n)/(n2*n);
36 }
37 
38 template<typename Scalar> EIGEN_STRONG_INLINE Scalar ei_lie_algebra_so3_derivative_df3(Scalar n, Scalar n2, Scalar cos_n, Scalar sin_n, Scalar precision){ // 1.e-2
39  if (n2 < precision)
40  return (-Scalar(21.)+(Scalar(1.)+(-Scalar(1.)/Scalar(48.)+Scalar(1.)/Scalar(3960.)*n2)*n2)*n2)/Scalar(1260.) ;
41  else
42  return Scalar(3)*( (n*n2-Scalar(6)*n+Scalar(6)*sin_n)/(Scalar(6)*n*n2*n2) ) - ( Scalar(0.5)*(n2-Scalar(2)+Scalar(2)*cos_n )/(n2*n2) );
43 }
44 
45 /***********************************************************************************
46 * Definition/implementation of LieAlgebraBase<Matrix<Scalar, 3, 1>, Derived>
47 ************************************************************************************/
48 
59 template<class Derived>
60 class LieAlgebraBase<Matrix<typename internal::traits<Derived>::Scalar, 3, 1>, Derived> :
61  public MatrixBase<Derived>
62 {
63 protected:
65  typedef MatrixBase<Derived> Base;
66 public:
67  // inherit MatrixBase interface
68  EIGEN_DENSE_PUBLIC_INTERFACE(LieAlgebraBase)
69  // inherit operator= [XXX] inheriting Base::opertor= through warning C4522 with visual studio : multiple assignment operators specified
70  //EIGEN_INHERIT_ASSIGNMENT_OPERATORS(LieAlgebraBase)
71  // accessor needed for MatrixBase inheritance
73 
75  typedef Matrix<Scalar, 3, 1> BaseType;
77  typedef typename internal::traits<Derived>::Coefficients Coefficients;
79  typedef LieAlgebra<BaseType> PlainObject;
81  typedef LieAlgebraDual<BaseType> AlgebraDual;
83  typedef typename internal::traits<Derived>::Group Group;
85  typedef Matrix<Scalar, 3, 3> Matrix3;
86 
88  EIGEN_STRONG_INLINE LieAlgebraBase& operator=(const LieAlgebraBase& other);
90  template<class OtherDerived> EIGEN_STRONG_INLINE Derived& operator=(const LieAlgebraBase<BaseType, OtherDerived>& other);
91 
92  template<class OtherDerived> EIGEN_STRONG_INLINE Derived& operator=(const MatrixBase<OtherDerived>& other) {
93  this->get() = other;
94  return derived();
95  }
96 
97 
99  template<class OtherDerived> inline PlainObject bracket(const LieAlgebraBase<BaseType, OtherDerived>& a) const;
100 // template<class OtherDerived> inline PlainObject adjoint(const LieAlgebraBase<BaseType, OtherDerived>& a) const;
101 
104  inline Group exp(Scalar precision = 1.e-5) const;
108  inline Matrix<Scalar, 3, 3> dexp(Scalar precision = 1.e-6, Scalar precision2 = 1.e-2) const;
112  template<class OtherDerived> Matrix<Scalar, 3, 3> inline d2exp(const MatrixBase<OtherDerived>& v, Scalar precision = 1.e-6, Scalar precision2 = 1.e-2) const;
113 
115  inline Coefficients& get() {return this->derived().get(); }
117  inline const Coefficients& get() const {return this->derived().get(); }
118 };
119 
120 /***************************************************************
121  * Implementation of LieAlgebraBase<Array<Scalar, 3, 1> > methods
122  ***************************************************************/
123 
124 // assignation
125 
126 template<class Derived>
129 {
130  this->get() = other.get();
131  return *this;
132 }
133 
134 template<class Derived>
135 template<class OtherDerived>
136 inline Derived&
138 {
139  this->get() = other.get();
140  return derived();
141 }
142 
143 // bracket
144 template<class Derived>
145 template<class OtherDerived>
147  LieAlgebraBase<Matrix<typename internal::traits<Derived>::Scalar, 3, 1>, Derived>::bracket(const LieAlgebraBase<Matrix<Scalar, 3, 1>, OtherDerived>& ang) const
148 {
149  return this->cross(ang);
150 }
151 
152 // adjoint
153 /*template<class Derived, typename _Scalar>
154 template<class OtherDerived>
155 typename LieAlgebraBase<Matrix<_Scalar, 3, 1>, Derived >::PlainObject LieAlgebraBase<Matrix<_Scalar, 3, 1>, Derived >::adjoint(const LieAlgebraBase<Matrix<Scalar, 3, 1>, OtherDerived>& a) const {
156  return this->bracket(ang);
157 }*/
158 
159 // exp
160 template<class Derived>
162  LieAlgebraBase<Matrix<typename internal::traits<Derived>::Scalar,3, 1>, Derived >::exp(const Scalar precision) const
163 {
164  const Scalar n2 = this->squaredNorm();
165 
166  if (n2 < precision)
167  {
168  const Scalar w = Scalar(1.0) + (Scalar(-1.0) + n2 / Scalar(48.0)) * n2 / Scalar(8.0); // Series expansion of cos(n/2) O(4)
169  return Group(w, (Scalar(1.0) + (Scalar(-1.0) + Scalar(0.0125) * n2) * n2 / Scalar(24.0)) / Scalar(2.0) * *this); // Series expansion of sin(n/2)/n O(4)
170  }
171  else
172  {
173  const Scalar n = std::sqrt(n2);
174  const Scalar w = std::cos(n * Scalar(0.5));
175  return Group(w, std::sin(n * Scalar(0.5)) / n * *this);
176  }
177 }
178 
179 //dexp
180 template<class Derived>
182  LieAlgebraBase<Matrix<typename internal::traits<Derived>::Scalar, 3, 1>, Derived >::dexp(Scalar precision,
183  Scalar precision2) const
184 {
185  Scalar n2 = this->squaredNorm();
186  Scalar n = std::sqrt(n2);
187  Scalar sin_n = std::sin(n);
188  Scalar cos_n = std::cos(n);
189 
190  Scalar f2 = ei_lie_algebra_so3_derivative_f2(n, n2, cos_n, precision);
191  Scalar f3 = ei_lie_algebra_so3_derivative_f3(n, n2, sin_n, precision2);
192 
193  Matrix<Scalar, 3, 3> m;
194 
195  m(0,0) = Scalar(1.0) - f3 * (n2 - this->coeff(0)*this->coeff(0));
196  m(1,1) = Scalar(1.0) - f3 * (n2 - this->coeff(1)*this->coeff(1));
197  m(2,2) = Scalar(1.0) - f3 * (n2 - this->coeff(2)*this->coeff(2));
198 
199  m(1, 0) = f3 * this->coeff(0) * this->coeff(1) - f2 * this->coeff(2);
200  m(0, 1) = m(1,0) + 2 * f2 * this->coeff(2);
201 
202  m(2, 0) = f3 * this->coeff(0) * this->coeff(2) + f2 * this->coeff(1);
203  m(0, 2) = m(2,0) - 2 * f2 * this->coeff(1);
204 
205  m(2, 1) = f3 * this->coeff(1) * this->coeff(2) - f2 * this->coeff(0);
206  m(1, 2) = m(2,1) + 2 * f2 * this->coeff(0);
207 
208  return m;
209 }
210 
211 //d2exp
212 template<class Derived>
213 template<class OtherDerived>
215  LieAlgebraBase<Matrix<typename internal::traits<Derived>::Scalar,3, 1>, Derived >::d2exp(const MatrixBase<OtherDerived>& vec,
216  Scalar precision,
217  Scalar precision2) const
218 {
219  Scalar n2 = this->squaredNorm();
220  Scalar n = std::sqrt(n2);
221  Scalar cos_n = std::cos(n);
222  Scalar sin_n = std::sin(n);
223 
224  Scalar f2 = ei_lie_algebra_so3_derivative_f2(n, n2, cos_n, precision);
225  Scalar f3 = ei_lie_algebra_so3_derivative_f3(n, n2, sin_n, precision2);
226  Scalar df2 = ei_lie_algebra_so3_derivative_df2(n, n2, cos_n, sin_n, precision2);
227  Scalar df3 = ei_lie_algebra_so3_derivative_df3(n, n2, cos_n, sin_n, precision2);
228 
229  Scalar vw = this->dot(vec);
230 
231  // computing w*v^T may be faster
232 
233  Matrix<Scalar, 3, 3> m;
234 
235  m(0,0) = - 2 * f3 * (this->coeff(2) * vec[2] + this->coeff(1) * vec[1]) - df3 * vw * (n2 - this->coeff(0)*this->coeff(0));
236  m(1,1) = - 2 * f3 * (this->coeff(2) * vec[2] + this->coeff(0) * vec[0]) - df3 * vw * (n2 - this->coeff(1)*this->coeff(1));
237  m(2,2) = - 2 * f3 * (this->coeff(1) * vec[1] + this->coeff(0) * vec[0]) - df3 * vw * (n2 - this->coeff(2)*this->coeff(2));
238 
239  m(1, 0) = -vec[2] * f2 + f3 * (this->coeff(0)*vec[1] + this->coeff(1)*vec[0]) + vw * (df3 * this->coeff(0) * this->coeff(1) - df2 * this->coeff(2));
240  m(0, 1) = m(1,0) + 2 * vec[2] * f2 + 2 * vw * df2 * this->coeff(2);
241 
242  m(2, 0) = vec[1] * f2 + f3 * (this->coeff(0)*vec[2] + this->coeff(2)*vec[0]) + vw * (df3 * this->coeff(0) * this->coeff(2) + df2 * this->coeff(1));
243  m(0, 2) = m(2,0) - 2 * vec[1] * f2 - 2 * vw * df2 * this->coeff(1);
244 
245  m(2, 1) = -vec[0] * f2 + f3 * (this->coeff(2)*vec[1] + this->coeff(1)*vec[2]) + vw * (df3 * this->coeff(1) * this->coeff(2) - df2 * this->coeff(0));
246  m(1, 2) = m(2,1) + 2 * vec[0] * f2 + 2 * vw * df2 * this->coeff(0);
247 
248  return m;
249 }
250 
251 
252 /***********************************************************************************
253 * Definition/implementation of LieAlgebraDualBase<Matrix<Scalar, 3, 1>, Derived>
254 ************************************************************************************/
255 
256 namespace internal {
257  template<typename Scalar>
258  struct traits<LieAlgebraDual<Matrix<Scalar, 3, 1> > > : public traits<Matrix<Scalar, 3, 1> >
259  {
260  typedef Matrix<Scalar, 3, 1> Coefficients;
262  };
263 
264  template<typename Scalar, int MapOptions>
265  struct traits<Map<LieAlgebraDual<Matrix<Scalar, 3, 1> >, MapOptions> > : public traits<Map<Matrix<Scalar, 3, 1>, MapOptions> >
266  {
267  typedef Map<Matrix<Scalar, 3, 1>, MapOptions> Coefficients;
269  };
270 
271  template<typename Scalar, int MapOptions>
272  struct traits<Map<const LieAlgebraDual<Matrix<Scalar, 3, 1> >, MapOptions> > : public traits<Map<const Matrix<Scalar, 3, 1>, MapOptions> >
273  {
274  typedef Map<const Matrix<Scalar, 3, 1>, MapOptions> Coefficients;
276  };
277 
278 }
279 
280 // no function to implement
281 
282 /***************************************************************************
283  * Definition/implementation of LieAlgebra<Matrix<Scalar, 3, 1> >
284  ***************************************************************************/
285 
296 namespace internal {
297  template<typename Scalar>
298  struct traits<LieAlgebra<Matrix<Scalar, 3, 1> > >
299  : public traits<LieAlgebraBase<Matrix<Scalar, 3, 1>, LieAlgebra<Matrix<Scalar, 3, 1> > > >
300  {
301  typedef Matrix<Scalar, 3, 1> Coefficients;
303  };
304 
305  template<typename Scalar, int Options>
306  struct traits<Map<LieAlgebra<Matrix<Scalar, 3, 1> >, Options> >
307  : public traits<LieAlgebraBase<Matrix<Scalar, 3, 1>, LieAlgebra<Matrix<Scalar, 3, 1> > > >
308  {
309  typedef Map<Matrix<Scalar, 3, 1>, Options> Coefficients;
311  };
312 
313  template<typename Scalar, int Options>
314  struct traits<Map<const LieAlgebra<Matrix<Scalar, 3, 1> >, Options> >
315  : public traits<LieAlgebraBase<Matrix<Scalar, 3, 1>, LieAlgebra<Matrix<Scalar, 3, 1> > > >
316  {
317  typedef Map<const Matrix<Scalar, 3, 1>, Options> Coefficients;
319  };
320 
321 }
322 
323 template<typename _Scalar>
324 class LieAlgebra<Matrix<_Scalar, 3, 1> > :
325  public LieAlgebraBase<Matrix<_Scalar, 3, 1>, LieAlgebra<Matrix<_Scalar, 3, 1> > >
326 {
327 protected:
330 public:
331  // inherit MatrixBase operator
332  EIGEN_DENSE_PUBLIC_INTERFACE(LieAlgebra)
333  // inherit assignement operator
334  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(LieAlgebra)
335 
337  typedef typename internal::traits<LieAlgebra<Matrix<Scalar, 3, 1> > >::Coefficients Coefficients;
338 
340  inline LieAlgebra() : m_coeffs() {}
342  inline LieAlgebra(const LieAlgebra& a) : m_coeffs(a.get() ) {} // maybe supernumerary
344  template<class Derived>
347  inline LieAlgebra(const Coefficients& a) : m_coeffs(a) {}
349  inline LieAlgebra(Scalar x, Scalar y, Scalar z) : m_coeffs(x, y, z) {}
350 
352  inline Coefficients& get() { return m_coeffs; }
354  inline const Coefficients& get() const { return m_coeffs; }
355 
356 protected:
359 };
360 
361 
362 /*****************************************************************************************
363 * Definition/implementation of Map<LieAlgebra<Matrix<Scalar, 3, 1> > >
364 ******************************************************************************************/
365 
366 // no new constructor needed
367 
368 /*****************************************************************************************
369 * Definition/implementation of LieAlgebraDual<Matrix<Scalar, 3, 1> >
370 ******************************************************************************************/
371 
372 template<typename _Scalar>
373 class LieAlgebraDual<Matrix<_Scalar, 3, 1> > :
374  public LieAlgebraDualBase<Matrix<_Scalar, 3, 1>, LieAlgebraDual<Matrix<_Scalar, 3, 1> > >
375 {
376 protected:
379 public:
380  // inherit MatrixBase operator
381  EIGEN_DENSE_PUBLIC_INTERFACE(LieAlgebraDual)
382  // inherit assignement operator
383  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(LieAlgebraDual)
384 
386  typedef typename internal::traits<LieAlgebraDual<Matrix<Scalar, 3, 1> > >::Coefficients Coefficients;
387 
389  inline LieAlgebraDual() : m_coeffs() {}
391  inline LieAlgebraDual(const LieAlgebraDual& a) : m_coeffs(a.get() ) {}
393  inline LieAlgebraDual(const Coefficients& a) : m_coeffs(a) {}
395  inline LieAlgebraDual(Scalar x, Scalar y, Scalar z) : m_coeffs(x, y, z) {}
396 
398  inline Coefficients& get() { return m_coeffs; }
400  inline const Coefficients& get() const { return m_coeffs; }
401 
402 protected:
405 };
406 
407 /*****************************************************************************************
408 * Definition/implementation of Map<LieAlgebraDual<Matrix<Scalar, 3, 1> > >
409 ******************************************************************************************/
410 
411 // no new constructor needed
412 
413 #endif
LieAlgebraBase< Matrix< _Scalar, 3, 1 >, LieAlgebra< Matrix< _Scalar, 3, 1 > > > Base
Class describing an element of a Lie Algebra.
Definition: LieAlgebra.h:168
Coefficients & get()
LieAlgebraDualBase< Matrix< _Scalar, 3, 1 >, LieAlgebraDual< Matrix< _Scalar, 3, 1 > > > Base
Scalar ei_lie_algebra_so3_derivative_f3(Scalar n, Scalar n2, Scalar sin_n, Scalar precision)
internal::traits< LieAlgebraDual< Matrix< Scalar, 3, 1 > > >::Coefficients Coefficients
const Derived & derived() const
Definition: LieAlgebra.h:68
LieAlgebra(Scalar x, Scalar y, Scalar z)
LieAlgebraDual(Scalar x, Scalar y, Scalar z)
Derived::LieGroup Group
Definition: LieAlgebra.h:55
Scalar ei_lie_algebra_so3_derivative_f2(Scalar n, Scalar n2, Scalar cos_n, Scalar precision)
Group exp(Scalar precision=1.e-6) const
internal::traits< LieAlgebra< Matrix< Scalar, 3, 1 > > >::Coefficients Coefficients
#define LIE_INHERIT_MATRIX_BASE(r, c)
Definition: Macros.h:13
Base class for all Lie Algebra class.
Definition: Displacement.h:139
EIGEN_STRONG_INLINE Scalar ei_lie_algebra_so3_derivative_df2(Scalar n, Scalar n2, Scalar cos_n, Scalar sin_n, Scalar precision)
Class describing an element of a Lie Group.
Definition: LieGroup.h:117
Class describing an element of a Lie algebra dual.
Definition: LieAlgebra.h:216
EIGEN_STRONG_INLINE Scalar ei_lie_algebra_so3_derivative_df3(Scalar n, Scalar n2, Scalar cos_n, Scalar sin_n, Scalar precision)
PlainObject bracket(const LieAlgebraBase< BaseType, OtherDerived > &a) const
LieAlgebra(const LieAlgebraBase< typename Base::BaseType, Derived > &a)


lgsm
Author(s): Roberto Martín-Martín
autogenerated on Mon Jun 10 2019 14:05:58