MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Quaternion.hpp
Go to the documentation of this file.
1 #ifndef QUATERNION_HPP
2 #define QUATERNION_HPP
3 
5 
6 #include <Config.hpp>
7 
8 #include <Geometry.hpp>
9 #include <Matrix.hpp>
10 #include <Vector.hpp>
11 
12 namespace mgx {
13 namespace util {
19 struct Quaternion : public Point4f {
26  : Point4f(0, 0, 0, 1)
27  {
28  }
29 
33  Quaternion(float x, float y, float z, float w)
34  : Point4f(x, y, z, w)
35  {
36  }
37 
41  Quaternion(const Quaternion& other)
42  : Point4f(other)
43  {
44  }
45 
54  Quaternion(const Point3f& axis, float angle);
55 
60  Quaternion(const Point3f& from, const Point3f& to);
61 
65  Quaternion(const Matrix3f& m);
66 
70  Quaternion& operator=(const Quaternion& other);
71 
77  void setAxisAngle(const Point3f& axis, float angle);
78 
82  float& w() {
83  return elems[3];
84  }
88  const float& w() const {
89  return elems[3];
90  }
91 
96  {
97  for(size_t i = 0; i < 4; ++i)
98  elems[i] += other.elems[i];
99  return *this;
100  }
101 
105  Quaternion operator+(const Quaternion& other) const
106  {
107  Quaternion tmp(*this);
108  tmp += other;
109  return tmp;
110  }
111 
115  Quaternion operator*(const Quaternion& other) const;
119  Quaternion& operator*=(const Quaternion& other);
120 
125  {
126  for(size_t i = 0; i < 4; ++i)
127  elems[i] *= s;
128  return *this;
129  }
130 
135  {
136  float n = mgx::util::normsq(*this);
137  return Quaternion(-x() / n, -y() / n, -z() / n, w() / n);
138  }
139 
144  return Quaternion(-x(), -y(), -z(), w());
145  }
146 
151  {
152  for(size_t i = 0; i < 4; ++i)
153  elems[i] /= v;
154  return *this;
155  }
156 
160  Quaternion operator/(float v) const
161  {
162  Quaternion tmp(*this);
163  tmp /= v;
164  return tmp;
165  }
166 
173  void setMatrix(Matrix3f& m) const;
180  void setMatrix(Matrix4f& m) const;
181 
185  Point3f axis() const;
186 
190  float angle() const;
191 
195  Point3f rotate(const Point3f& v) const;
196 
203  Point3f inverseRotate(const Point3f& v) const;
204 };
205 
211 Quaternion slerp(const Quaternion& q1, const Quaternion& q2, float t);
212 
213 inline Quaternion operator*(float s, const Quaternion& q)
214 {
215  Quaternion tmp(q);
216  tmp *= s;
217  return tmp;
218 }
219 
220 inline Quaternion operator*(const Quaternion& q, float s)
221 {
222  Quaternion tmp(q);
223  tmp *= s;
224  return tmp;
225 }
226 } // namespace util
227 } // namespace mgx
228 
229 namespace std {
235 mgx::util::Quaternion pow(const mgx::util::Quaternion& q, float p);
236 } // namespace std
237 
238 #endif // QUATERNION_HPP
Quaternion operator+(const Quaternion &other) const
Quaternion addition.
Definition: Quaternion.hpp:105
Quaternion & operator/=(float v)
Division of a quaternion by a real number.
Definition: Quaternion.hpp:150
Quaternion inverse() const
Return the quaternion corresponding to the inverse transform.
Definition: Quaternion.hpp:134
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:696
Quaternion operator*(const Quaternion &other) const
Quaternion multiplication.
CU_HOST_DEVICE float & i()
Short access to the first element.
Definition: Vector.hpp:796
float & w()
Accessing the real part of the quaternion.
Definition: Quaternion.hpp:82
Quaternion(float x, float y, float z, float w)
Creates a quaternion specified by its components.
Definition: Quaternion.hpp:33
CU_HOST_DEVICE float & x()
Short access to the first element.
Definition: Vector.hpp:687
Quaternion conjugate() const
Return the conjugate of the current quaternion.
Definition: Quaternion.hpp:143
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:88
Common definitions and utilities for all geometry algorithms This file is shared by cuda...
Quaternion & operator+=(const Quaternion &other)
Quaternion in-place addition.
Definition: Quaternion.hpp:95
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:124
Quaternion & operator=(const Quaternion &other)
Assignment operator for quaternions.
Implements the quaternion operations.
Definition: Quaternion.hpp:19
CU_HOST_DEVICE float & z()
Short access to the third element.
Definition: Vector.hpp:705
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:34
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't...
Quaternion(const Quaternion &other)
Copy constructor.
Definition: Quaternion.hpp:41
Quaternion()
Default constructor.
Definition: Quaternion.hpp:25
Quaternion operator/(float v) const
Division of a quaternion by a real number.
Definition: Quaternion.hpp:160
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't...