Eigen2Support/Geometry/Quaternion.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) 2008 Gael Guennebaud <g.gael@free.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 // no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
11 
12 namespace Eigen {
13 
14 template<typename Other,
15  int OtherRows=Other::RowsAtCompileTime,
16  int OtherCols=Other::ColsAtCompileTime>
18 
41 template<typename _Scalar> struct ei_traits<Quaternion<_Scalar> >
42 {
43  typedef _Scalar Scalar;
44 };
45 
46 template<typename _Scalar>
47 class Quaternion : public RotationBase<Quaternion<_Scalar>,3>
48 {
50 
51 public:
53 
54  using Base::operator*;
55 
57  typedef _Scalar Scalar;
58 
60  typedef Matrix<Scalar, 4, 1> Coefficients;
62  typedef Matrix<Scalar,3,1> Vector3;
64  typedef Matrix<Scalar,3,3> Matrix3;
66  typedef AngleAxis<Scalar> AngleAxisType;
67 
69  inline Scalar x() const { return m_coeffs.coeff(0); }
71  inline Scalar y() const { return m_coeffs.coeff(1); }
73  inline Scalar z() const { return m_coeffs.coeff(2); }
75  inline Scalar w() const { return m_coeffs.coeff(3); }
76 
78  inline Scalar& x() { return m_coeffs.coeffRef(0); }
80  inline Scalar& y() { return m_coeffs.coeffRef(1); }
82  inline Scalar& z() { return m_coeffs.coeffRef(2); }
84  inline Scalar& w() { return m_coeffs.coeffRef(3); }
85 
87  inline const Block<const Coefficients,3,1> vec() const { return m_coeffs.template start<3>(); }
88 
90  inline Block<Coefficients,3,1> vec() { return m_coeffs.template start<3>(); }
91 
93  inline const Coefficients& coeffs() const { return m_coeffs; }
94 
96  inline Coefficients& coeffs() { return m_coeffs; }
97 
99  inline Quaternion() {}
100 
108  inline Quaternion(Scalar w, Scalar x, Scalar y, Scalar z)
109  { m_coeffs << x, y, z, w; }
110 
112  inline Quaternion(const Quaternion& other) { m_coeffs = other.m_coeffs; }
113 
115  explicit inline Quaternion(const AngleAxisType& aa) { *this = aa; }
116 
122  template<typename Derived>
123  explicit inline Quaternion(const MatrixBase<Derived>& other) { *this = other; }
124 
125  Quaternion& operator=(const Quaternion& other);
126  Quaternion& operator=(const AngleAxisType& aa);
127  template<typename Derived>
128  Quaternion& operator=(const MatrixBase<Derived>& m);
129 
133  static inline Quaternion Identity() { return Quaternion(1, 0, 0, 0); }
134 
137  inline Quaternion& setIdentity() { m_coeffs << 0, 0, 0, 1; return *this; }
138 
142  inline Scalar squaredNorm() const { return m_coeffs.squaredNorm(); }
143 
147  inline Scalar norm() const { return m_coeffs.norm(); }
148 
151  inline void normalize() { m_coeffs.normalize(); }
154  inline Quaternion normalized() const { return Quaternion(m_coeffs.normalized()); }
155 
161  inline Scalar eigen2_dot(const Quaternion& other) const { return m_coeffs.eigen2_dot(other.m_coeffs); }
162 
163  inline Scalar angularDistance(const Quaternion& other) const;
164 
165  Matrix3 toRotationMatrix(void) const;
166 
167  template<typename Derived1, typename Derived2>
168  Quaternion& setFromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
169 
170  inline Quaternion operator* (const Quaternion& q) const;
171  inline Quaternion& operator*= (const Quaternion& q);
172 
173  Quaternion inverse(void) const;
174  Quaternion conjugate(void) const;
175 
176  Quaternion slerp(Scalar t, const Quaternion& other) const;
177 
178  template<typename Derived>
179  Vector3 operator* (const MatrixBase<Derived>& vec) const;
180 
186  template<typename NewScalarType>
188  { return typename internal::cast_return_type<Quaternion,Quaternion<NewScalarType> >::type(*this); }
189 
191  template<typename OtherScalarType>
192  inline explicit Quaternion(const Quaternion<OtherScalarType>& other)
193  { m_coeffs = other.coeffs().template cast<Scalar>(); }
194 
199  bool isApprox(const Quaternion& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
200  { return m_coeffs.isApprox(other.m_coeffs, prec); }
201 
202 protected:
204 };
205 
212 
213 // Generic Quaternion * Quaternion product
214 template<typename Scalar> inline Quaternion<Scalar>
216 {
217  return Quaternion<Scalar>
218  (
219  a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(),
220  a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(),
221  a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(),
222  a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x()
223  );
224 }
225 
227 template <typename Scalar>
229 {
230  return ei_quaternion_product(*this,other);
231 }
232 
234 template <typename Scalar>
236 {
237  return (*this = *this * other);
238 }
239 
247 template <typename Scalar>
248 template<typename Derived>
249 inline typename Quaternion<Scalar>::Vector3
251 {
252  // Note that this algorithm comes from the optimization by hand
253  // of the conversion to a Matrix followed by a Matrix/Vector product.
254  // It appears to be much faster than the common algorithm found
255  // in the litterature (30 versus 39 flops). It also requires two
256  // Vector3 as temporaries.
257  Vector3 uv;
258  uv = 2 * this->vec().cross(v);
259  return v + this->w() * uv + this->vec().cross(uv);
260 }
261 
262 template<typename Scalar>
264 {
265  m_coeffs = other.m_coeffs;
266  return *this;
267 }
268 
271 template<typename Scalar>
273 {
274  Scalar ha = Scalar(0.5)*aa.angle(); // Scalar(0.5) to suppress precision loss warnings
275  this->w() = ei_cos(ha);
276  this->vec() = ei_sin(ha) * aa.axis();
277  return *this;
278 }
279 
285 template<typename Scalar>
286 template<typename Derived>
288 {
289  ei_quaternion_assign_impl<Derived>::run(*this, xpr.derived());
290  return *this;
291 }
292 
294 template<typename Scalar>
295 inline typename Quaternion<Scalar>::Matrix3
297 {
298  // NOTE if inlined, then gcc 4.2 and 4.4 get rid of the temporary (not gcc 4.3 !!)
299  // if not inlined then the cost of the return by value is huge ~ +35%,
300  // however, not inlining this function is an order of magnitude slower, so
301  // it has to be inlined, and so the return by value is not an issue
302  Matrix3 res;
303 
304  const Scalar tx = Scalar(2)*this->x();
305  const Scalar ty = Scalar(2)*this->y();
306  const Scalar tz = Scalar(2)*this->z();
307  const Scalar twx = tx*this->w();
308  const Scalar twy = ty*this->w();
309  const Scalar twz = tz*this->w();
310  const Scalar txx = tx*this->x();
311  const Scalar txy = ty*this->x();
312  const Scalar txz = tz*this->x();
313  const Scalar tyy = ty*this->y();
314  const Scalar tyz = tz*this->y();
315  const Scalar tzz = tz*this->z();
316 
317  res.coeffRef(0,0) = Scalar(1)-(tyy+tzz);
318  res.coeffRef(0,1) = txy-twz;
319  res.coeffRef(0,2) = txz+twy;
320  res.coeffRef(1,0) = txy+twz;
321  res.coeffRef(1,1) = Scalar(1)-(txx+tzz);
322  res.coeffRef(1,2) = tyz-twx;
323  res.coeffRef(2,0) = txz-twy;
324  res.coeffRef(2,1) = tyz+twx;
325  res.coeffRef(2,2) = Scalar(1)-(txx+tyy);
326 
327  return res;
328 }
329 
336 template<typename Scalar>
337 template<typename Derived1, typename Derived2>
339 {
340  Vector3 v0 = a.normalized();
341  Vector3 v1 = b.normalized();
342  Scalar c = v0.eigen2_dot(v1);
343 
344  // if dot == 1, vectors are the same
345  if (ei_isApprox(c,Scalar(1)))
346  {
347  // set to identity
348  this->w() = 1; this->vec().setZero();
349  return *this;
350  }
351  // if dot == -1, vectors are opposites
352  if (ei_isApprox(c,Scalar(-1)))
353  {
354  this->vec() = v0.unitOrthogonal();
355  this->w() = 0;
356  return *this;
357  }
358 
359  Vector3 axis = v0.cross(v1);
360  Scalar s = ei_sqrt((Scalar(1)+c)*Scalar(2));
361  Scalar invs = Scalar(1)/s;
362  this->vec() = axis * invs;
363  this->w() = s * Scalar(0.5);
364 
365  return *this;
366 }
367 
374 template <typename Scalar>
376 {
377  // FIXME should this function be called multiplicativeInverse and conjugate() be called inverse() or opposite() ??
378  Scalar n2 = this->squaredNorm();
379  if (n2 > 0)
380  return Quaternion(conjugate().coeffs() / n2);
381  else
382  {
383  // return an invalid result to flag the error
384  return Quaternion(Coefficients::Zero());
385  }
386 }
387 
394 template <typename Scalar>
396 {
397  return Quaternion(this->w(),-this->x(),-this->y(),-this->z());
398 }
399 
403 template <typename Scalar>
405 {
406  double d = ei_abs(this->eigen2_dot(other));
407  if (d>=1.0)
408  return 0;
409  return Scalar(2) * std::acos(d);
410 }
411 
415 template <typename Scalar>
417 {
418  static const Scalar one = Scalar(1) - machine_epsilon<Scalar>();
419  Scalar d = this->eigen2_dot(other);
420  Scalar absD = ei_abs(d);
421 
422  Scalar scale0;
423  Scalar scale1;
424 
425  if (absD>=one)
426  {
427  scale0 = Scalar(1) - t;
428  scale1 = t;
429  }
430  else
431  {
432  // theta is the angle between the 2 quaternions
433  Scalar theta = std::acos(absD);
434  Scalar sinTheta = ei_sin(theta);
435 
436  scale0 = ei_sin( ( Scalar(1) - t ) * theta) / sinTheta;
437  scale1 = ei_sin( ( t * theta) ) / sinTheta;
438  if (d<0)
439  scale1 = -scale1;
440  }
441 
442  return Quaternion<Scalar>(scale0 * coeffs() + scale1 * other.coeffs());
443 }
444 
445 // set from a rotation matrix
446 template<typename Other>
447 struct ei_quaternion_assign_impl<Other,3,3>
448 {
449  typedef typename Other::Scalar Scalar;
450  static inline void run(Quaternion<Scalar>& q, const Other& mat)
451  {
452  // This algorithm comes from "Quaternion Calculus and Fast Animation",
453  // Ken Shoemake, 1987 SIGGRAPH course notes
454  Scalar t = mat.trace();
455  if (t > 0)
456  {
457  t = ei_sqrt(t + Scalar(1.0));
458  q.w() = Scalar(0.5)*t;
459  t = Scalar(0.5)/t;
460  q.x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
461  q.y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
462  q.z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
463  }
464  else
465  {
466  int i = 0;
467  if (mat.coeff(1,1) > mat.coeff(0,0))
468  i = 1;
469  if (mat.coeff(2,2) > mat.coeff(i,i))
470  i = 2;
471  int j = (i+1)%3;
472  int k = (j+1)%3;
473 
474  t = ei_sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
475  q.coeffs().coeffRef(i) = Scalar(0.5) * t;
476  t = Scalar(0.5)/t;
477  q.w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
478  q.coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
479  q.coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
480  }
481  }
482 };
483 
484 // set from a vector of coefficients assumed to be a quaternion
485 template<typename Other>
486 struct ei_quaternion_assign_impl<Other,4,1>
487 {
488  typedef typename Other::Scalar Scalar;
489  static inline void run(Quaternion<Scalar>& q, const Other& vec)
490  {
491  q.coeffs() = vec;
492  }
493 };
494 
495 } // end namespace Eigen
Quaternion conjugate(void) const
d
Quaternion(const Quaternion &other)
static void run(Quaternion< Scalar > &q, const Other &vec)
static Matrix< Scalar, 2, 2 > toRotationMatrix(const Scalar &s)
internal::cast_return_type< Quaternion, Quaternion< NewScalarType > >::type cast() const
Quaternion(Scalar w, Scalar x, Scalar y, Scalar z)
XmlRpcServer s
Derived & setZero()
Definition: LDLT.h:16
Quaternion & operator*=(const Quaternion &q)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
const internal::permut_matrix_product_retval< PermutationDerived, Derived, OnTheRight > operator*(const MatrixBase< Derived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Block< Coefficients, 3, 1 > vec()
TFSIMD_FORCE_INLINE Quaternion slerp(const Quaternion &q1, const Quaternion &q2, const tfScalar &t)
Quaternion & operator=(const Quaternion &other)
const Vector3 & axis() const
const CwiseUnaryOp< internal::scalar_inverse_op< Scalar >, const Derived > inverse() const
#define Quaternion
Definition: All.h:29
Quaternion(const MatrixBase< Derived > &other)
EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
const PlainObject normalized() const
Definition: Dot.h:139
Common base class for compact rotation representations.
T ei_cos(const T &x)
TFSIMD_FORCE_INLINE const tfScalar & x() const
ConjugateReturnType conjugate() const
T ei_sin(const T &x)
void axis(float size)
TFSIMD_FORCE_INLINE const tfScalar & z() const
T ei_sqrt(const T &x)
TFSIMD_FORCE_INLINE const tfScalar & w() const
bool ei_isApprox(const Scalar &x, const Scalar &y, typename NumTraits< Scalar >::Real precision=NumTraits< Scalar >::dummy_precision())
Scalar angularDistance(const Quaternion &other) const
Quaternion slerp(Scalar t, const Quaternion &other) const
cross_product_return_type< OtherDerived >::type cross(const MatrixBase< OtherDerived > &other) const
NumTraits< T >::Real ei_abs(const T &x)
Expression of a fixed-size or dynamic-size block.
Definition: Core/Block.h:102
The quaternion class used to represent 3D orientations and rotations.
const Coefficients & coeffs() const
Quaternion(const AngleAxisType &aa)
Quaternion(const Quaternion< OtherScalarType > &other)
Scalar eigen2_dot(const Quaternion &other) const
Quaternion< double > Quaterniond
static void run(Quaternion< Scalar > &q, const Other &mat)
#define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Quaternion operator*(const Quaternion &q) const
const CwiseUnaryOp< internal::scalar_acos_op< Scalar >, const Derived > acos() const
Quaternion< float > Quaternionf
const Block< const Coefficients, 3, 1 > vec() const
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
bool isApprox(const Quaternion &other, typename NumTraits< Scalar >::Real prec=precision< Scalar >()) const
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
RotationBase< Quaternion< _Scalar >, 3 > Base
Quaternion< Scalar > ei_quaternion_product(const Quaternion< Scalar > &a, const Quaternion< Scalar > &b)
Quaternion & setFromTwoVectors(const MatrixBase< Derived1 > &a, const MatrixBase< Derived2 > &b)


tuw_aruco
Author(s): Lukas Pfeifhofer
autogenerated on Mon Jun 10 2019 15:40:56