MorphoGraphX  2.0-1-227
Quaternion.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef QUATERNION_HPP
12 #define QUATERNION_HPP
13 
15 
16 #include <Config.hpp>
17 
18 #include <Geometry.hpp>
19 #include <Matrix.hpp>
20 #include <Vector.hpp>
21 
22 namespace mgx
23 {
29  struct Quaternion : public Point4d {
35  Quaternion() : Point4d(0, 0, 0, 1) {}
36 
40  Quaternion(const Point4d &other) : Point4d(other) {}
41 
45  Quaternion(double x, double y, double z, double w) : Point4d(x, y, z, w) {}
46 
50  Quaternion(const Quaternion &other) : Point4d(other) {}
51 
60  Quaternion(const Point3d &axis, double angle);
61 
66  Quaternion(const Point3d &from, const Point3d &to);
67 
71  Quaternion(const Matrix3d &m);
72 
76  Quaternion &operator=(const Quaternion &other);
77 
83  void setAxisAngle(const Point3d &axis, double angle);
84 
88  double &w() { return elems[3]; }
92  const double &w() const { return elems[3]; }
93 
98  {
99  for(size_t i = 0; i < 4; ++i)
100  elems[i] += other.elems[i];
101  return *this;
102  }
103 
107  Quaternion operator+(const Quaternion &other) const
108  {
109  Quaternion tmp(*this);
110  tmp += other;
111  return tmp;
112  }
113 
117  Quaternion operator*(const Quaternion &other) const;
121  Quaternion &operator*=(const Quaternion &other);
122 
127  {
128  for(size_t i = 0; i < 4; ++i)
129  elems[i] *= s;
130  return *this;
131  }
132 
137  {
138  double n = mgx::normsq(*this);
139  return Quaternion(-x() / n, -y() / n, -z() / n, w() / n);
140  }
141 
146  return Quaternion(-x(), -y(), -z(), w());
147  }
148 
153  {
154  for(size_t i = 0; i < 4; ++i)
155  elems[i] /= v;
156  return *this;
157  }
158 
162  Quaternion operator/(double v) const
163  {
164  Quaternion tmp(*this);
165  tmp /= v;
166  return tmp;
167  }
168 
175  void setMatrix(Matrix3d &m) const;
182  void setMatrix(Matrix4d &m) const;
183 
187  Point3d axis() const;
188 
192  double angle() const;
193 
197  Point3d rotate(const Point3d &v) const;
198 
205  Point3d inverseRotate(const Point3d &v) const;
206  };
207 
213  Quaternion slerp(const Quaternion &q1, const Quaternion &q2, double t);
214 
215  inline Quaternion operator*(double s, const Quaternion& q)
216  {
217  Quaternion tmp(q);
218  tmp *= s;
219  return tmp;
220  }
221 
222  inline Quaternion operator*(const Quaternion &q, double s)
223  {
224  Quaternion tmp(q);
225  tmp *= s;
226  return tmp;
227  }
228 }
229 
230 namespace std
231 {
237  mgx::Quaternion pow(const mgx::Quaternion &q, double p);
238 }
239 
240 #endif
mgx::Quaternion::conjugate
Quaternion conjugate() const
Return the conjugate of the current quaternion.
Definition: Quaternion.hpp:145
Vector.hpp
mgx::Quaternion::rotate
Point3d rotate(const Point3d &v) const
Apply the rotation contained in this quaternion on the vector.
mgx::Vector::z
CU_HOST_DEVICE T & z()
Short access to the third element.
Definition: Vector.hpp:775
mgx::normsq
CU_HOST_DEVICE T normsq(const Matrix< nRows, nCols, T > &mat)
Definition: Matrix.hpp:1123
Quaternion
mgx::Quaternion::axis
Point3d axis() const
Returns the axis of the rotation corresponding to this quaternion.
mgx::operator*
const VSMultOp< DistNhbdT, MatrixT, VectorT, ScalarT > operator*(DVector< DistNhbdT, MatrixT, VectorT, ScalarT > &p_vec, ScalarT p_a)
Definition: DistMatrix.hpp:159
mgx::Quaternion::Quaternion
Quaternion(const Quaternion &other)
Copy constructor.
Definition: Quaternion.hpp:50
n
#define n
Definition: Eigenvalues.hpp:36
mgx::Quaternion::Quaternion
Quaternion()
Default constructor.
Definition: Quaternion.hpp:35
mgx::Vector::x
CU_HOST_DEVICE T & x()
Short access to the first element.
Definition: Vector.hpp:757
mgx::Quaternion::w
const double & w() const
Accessing the real part of the quaternion.
Definition: Quaternion.hpp:92
mgx::Quaternion::w
double & w()
Accessing the real part of the quaternion.
Definition: Quaternion.hpp:88
Geometry.hpp
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::Quaternion::inverseRotate
Point3d inverseRotate(const Point3d &v) const
Apply the inverse of the rotation contained in this quaternion on the vector.
mgx::Quaternion::operator*=
Quaternion & operator*=(const Quaternion &other)
Quaternion in-place multiplication.
mgx::Quaternion::setMatrix
void setMatrix(Matrix3d &m) const
Fill the matrix as argument from the quaternion.
mgx::Vector::elems
T elems[dim]
Definition: Vector.hpp:50
mgx::Quaternion::Quaternion
Quaternion(double x, double y, double z, double w)
Creates a quaternion specified by its components.
Definition: Quaternion.hpp:45
mgx::Quaternion::setAxisAngle
void setAxisAngle(const Point3d &axis, double angle)
Set the quaternion to the described rotation.
mgx::Quaternion
Definition: Quaternion.hpp:29
mgx::Quaternion::operator/=
Quaternion & operator/=(double v)
Division of a quaternion by a real number.
Definition: Quaternion.hpp:152
mgx::Quaternion::inverse
Quaternion inverse() const
Return the quaternion corresponding to the inverse transform.
Definition: Quaternion.hpp:136
mgx::Quaternion::operator=
Quaternion & operator=(const Quaternion &other)
Assignment operator for quaternions.
mgx::Quaternion::operator*
Quaternion operator*(const Quaternion &other) const
Quaternion multiplication.
mgx::Quaternion::operator+=
Quaternion & operator+=(const Quaternion &other)
Quaternion in-place addition.
Definition: Quaternion.hpp:97
mgx::Quaternion::operator/
Quaternion operator/(double v) const
Division of a quaternion by a real number.
Definition: Quaternion.hpp:162
mgx::Vector< 4, double >
Matrix.hpp
mgx::Quaternion::angle
double angle() const
Returns the angle of the rotation corresponding to this quaternion.
mgx::Vector::i
CU_HOST_DEVICE T & i()
Short access to the first element.
Definition: Vector.hpp:866
mgx::Vector::y
CU_HOST_DEVICE T & y()
Short access to the second element.
Definition: Vector.hpp:766
mgx::Matrix
Definition: Matrix.hpp:39
mgx::Quaternion::operator*=
Quaternion & operator*=(double s)
In-place multiplication of a quaternion by a scalar.
Definition: Quaternion.hpp:126
mgx::Quaternion::Quaternion
Quaternion(const Point4d &other)
Creates a quaternion from a Point4d.
Definition: Quaternion.hpp:40
mgx::Quaternion::operator+
Quaternion operator+(const Quaternion &other) const
Quaternion addition.
Definition: Quaternion.hpp:107