MorphoGraphX
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 namespace util {
29 struct Quaternion : public Point4f {
36  : Point4f(0, 0, 0, 1)
37  {
38  }
39 
43  Quaternion(float x, float y, float z, float w)
44  : Point4f(x, y, z, w)
45  {
46  }
47 
51  Quaternion(const Quaternion& other)
52  : Point4f(other)
53  {
54  }
55 
64  Quaternion(const Point3f& axis, float angle);
65 
70  Quaternion(const Point3f& from, const Point3f& to);
71 
75  Quaternion(const Matrix3f& m);
76 
80  Quaternion& operator=(const Quaternion& other);
81 
87  void setAxisAngle(const Point3f& axis, float angle);
88 
92  float& w() {
93  return elems[3];
94  }
98  const float& w() const {
99  return elems[3];
100  }
101 
106  {
107  for(size_t i = 0; i < 4; ++i)
108  elems[i] += other.elems[i];
109  return *this;
110  }
111 
115  Quaternion operator+(const Quaternion& other) const
116  {
117  Quaternion tmp(*this);
118  tmp += other;
119  return tmp;
120  }
121 
125  Quaternion operator*(const Quaternion& other) const;
129  Quaternion& operator*=(const Quaternion& other);
130 
135  {
136  for(size_t i = 0; i < 4; ++i)
137  elems[i] *= s;
138  return *this;
139  }
140 
145  {
146  float n = mgx::util::normsq(*this);
147  return Quaternion(-x() / n, -y() / n, -z() / n, w() / n);
148  }
149 
154  return Quaternion(-x(), -y(), -z(), w());
155  }
156 
161  {
162  for(size_t i = 0; i < 4; ++i)
163  elems[i] /= v;
164  return *this;
165  }
166 
170  Quaternion operator/(float v) const
171  {
172  Quaternion tmp(*this);
173  tmp /= v;
174  return tmp;
175  }
176 
183  void setMatrix(Matrix3f& m) const;
190  void setMatrix(Matrix4f& m) const;
191 
195  Point3f axis() const;
196 
200  float angle() const;
201 
205  Point3f rotate(const Point3f& v) const;
206 
213  Point3f inverseRotate(const Point3f& v) const;
214 };
215 
221 Quaternion slerp(const Quaternion& q1, const Quaternion& q2, float t);
222 
223 inline Quaternion operator*(float s, const Quaternion& q)
224 {
225  Quaternion tmp(q);
226  tmp *= s;
227  return tmp;
228 }
229 
230 inline Quaternion operator*(const Quaternion& q, float s)
231 {
232  Quaternion tmp(q);
233  tmp *= s;
234  return tmp;
235 }
236 } // namespace util
237 } // namespace mgx
238 
239 namespace std {
245 mgx::util::Quaternion pow(const mgx::util::Quaternion& q, float p);
246 } // namespace std
247 
248 #endif // QUATERNION_HPP
Quaternion operator+(const Quaternion &other) const
Quaternion addition.
Definition: Quaternion.hpp:115
Quaternion & operator/=(float v)
Division of a quaternion by a real number.
Definition: Quaternion.hpp:160
Quaternion inverse() const
Return the quaternion corresponding to the inverse transform.
Definition: Quaternion.hpp:144
Point3f axis() const
Returns the axis of the rotation corresponding to this quaternion.
CU_HOST_DEVICE float & y()
Short access to the second element.
Definition: Vector.hpp:706
Quaternion operator*(const Quaternion &other) const
Quaternion multiplication.
CU_HOST_DEVICE float & t()
Short access to the fourth element.
Definition: Vector.hpp:724
STL namespace.
CU_HOST_DEVICE float & i()
Short access to the first element.
Definition: Vector.hpp:806
float & w()
Accessing the real part of the quaternion.
Definition: Quaternion.hpp:92
Quaternion(float x, float y, float z, float w)
Creates a quaternion specified by its components.
Definition: Quaternion.hpp:43
CU_HOST_DEVICE float & x()
Short access to the first element.
Definition: Vector.hpp:697
Quaternion conjugate() const
Return the conjugate of the current quaternion.
Definition: Quaternion.hpp:153
Point3f inverseRotate(const Point3f &v) const
Apply the inverse of the rotation contained in this quaternion on the vector.
const float & w() const
Accessing the real part of the quaternion.
Definition: Quaternion.hpp:98
Common definitions and utilities for all geometry algorithms This file is shared by cuda...
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Quaternion & operator+=(const Quaternion &other)
Quaternion in-place addition.
Definition: Quaternion.hpp:105
float angle() const
Returns the angle of the rotation corresponding to this quaternion.
Quaternion & operator*=(float s)
In-place multiplication of a quaternion by a scalar.
Definition: Quaternion.hpp:134
Quaternion & operator=(const Quaternion &other)
Assignment operator for quaternions.
Implements the quaternion operations.
Definition: Quaternion.hpp:29
CU_HOST_DEVICE float & z()
Short access to the third element.
Definition: Vector.hpp:715
Point3f rotate(const Point3f &v) const
Apply the rotation contained in this quaternion on the vector.
Class representing a fixed-size matrix.
Definition: Matrix.hpp:44
void setMatrix(Matrix3f &m) const
Fill the matrix as argument from the quaternion.
void setAxisAngle(const Point3f &axis, float angle)
Set the quaternion to the described rotation.
Defines the Matrix class template This file is shared by cuda, do not include headers that nvcc can&#39;t...
Quaternion(const Quaternion &other)
Copy constructor.
Definition: Quaternion.hpp:51
Quaternion()
Default constructor.
Definition: Quaternion.hpp:35
Quaternion operator/(float v) const
Division of a quaternion by a real number.
Definition: Quaternion.hpp:170
Quaternion & operator*=(const Quaternion &other)
Quaternion in-place multiplication.
Defines the Vector class template This file is shared by cuda, do not include headers that nvcc can&#39;t...