VTK
vtkQuaternion.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkQuaternion.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
33 #ifndef vtkQuaternion_h
34 #define vtkQuaternion_h
35 
36 #include "vtkTuple.h"
37 
38 template<typename T> class vtkQuaternion : public vtkTuple<T, 4>
39 {
40 public:
44  vtkQuaternion();
45 
49  explicit vtkQuaternion(const T& scalar) : vtkTuple<T, 4>(scalar) {}
50 
56  explicit vtkQuaternion(const T* init) : vtkTuple<T, 4>(init) {}
57 
61  vtkQuaternion(const T& w, const T& x, const T& y, const T& z);
62 
66  T SquaredNorm() const;
67 
71  T Norm() const;
72 
76  void ToIdentity();
77 
82  static vtkQuaternion<T> Identity();
83 
88  T Normalize();
89 
94 
98  void Conjugate();
99 
104 
110  void Invert();
111 
115  vtkQuaternion<T> Inverse() const;
116 
122  void ToUnitLog();
123 
129  vtkQuaternion<T> UnitLog() const;
130 
136  void ToUnitExp();
137 
143  vtkQuaternion<T> UnitExp() const;
144 
150 
156 
158 
161  void Set(const T& w, const T& x, const T& y, const T& z);
162  void Set(T quat[4]);
163  void Get(T quat[4]) const;
165 
167 
170  void SetW(const T& w);
171  const T& GetW() const;
173 
175 
178  void SetX(const T& x);
179  const T& GetX() const;
181 
183 
186  void SetY(const T& y);
187  const T& GetY() const;
189 
191 
194  void SetZ(const T& z);
195  const T& GetZ() const;
197 
199 
203  T GetRotationAngleAndAxis(T axis[3]) const;
204  void SetRotationAngleAndAxis(T angle, T axis[3]);
206  const T& angle, const T& x, const T& y, const T& z);
208 
212  template<typename CastTo> vtkQuaternion<CastTo> Cast() const;
213 
219  void ToMatrix3x3(T A[3][3]) const;
220 
227  void FromMatrix3x3(const T A[3][3]);
228 
235  vtkQuaternion<T> Slerp(T t, const vtkQuaternion<T>& q) const;
236 
243  const vtkQuaternion<T>& q2) const;
244 
249 
254 
259 
263  vtkQuaternion<T> operator*(const T& scalar) const;
264 
268  void operator*=(const T& scalar) const;
269 
274 
278  vtkQuaternion<T> operator/(const T& scalar) const;
279 
281 
284  void operator/=(const T& scalar);
285 };
287 
292 #define vtkQuaternionIdentity(quaternionType, type) \
293 quaternionType Identity() const \
294 { \
295  return quaternionType(vtkQuaternion<type>::Identity().GetData()); \
296 }
297 #define vtkQuaternionNormalized(quaternionType, type) \
298 quaternionType Normalized() const \
299 { \
300  return quaternionType(vtkQuaternion<type>::Normalized().GetData()); \
301 }
302 #define vtkQuaternionConjugated(quaternionType, type) \
303 quaternionType Conjugated() const \
304 { \
305  return quaternionType(vtkQuaternion<type>::Conjugated().GetData()); \
306 }
307 #define vtkQuaternionInverse(quaternionType, type) \
308 quaternionType Inverse() const \
309 { \
310  return quaternionType(vtkQuaternion<type>::Inverse().GetData()); \
311 }
312 #define vtkQuaternionUnitLog(quaternionType, type) \
313 quaternionType UnitLog() const \
314 { \
315  return quaternionType( \
316  vtkQuaternion<type>::UnitLog().GetData()); \
317 }
318 #define vtkQuaternionUnitExp(quaternionType, type) \
319 quaternionType UnitExp() const \
320 { \
321  return quaternionType( \
322  vtkQuaternion<type>::UnitExp().GetData()); \
323 }
324 #define vtkQuaternionNormalizedWithAngleInDegrees(quaternionType, type) \
325 quaternionType NormalizedWithAngleInDegrees() const \
326 { \
327  return quaternionType( \
328  vtkQuaternion<type>::NormalizedWithAngleInDegrees().GetData()); \
329 }
330 #define vtkQuaternionSlerp(quaternionType, type) \
331 quaternionType Slerp(type t, const quaternionType& q) const \
332 { \
333  return quaternionType( \
334  vtkQuaternion<type>::Slerp(t, q).GetData()); \
335 }
336 #define vtkQuaternionInnerPoint(quaternionType, type) \
337 quaternionType InnerPoint(const quaternionType& q1, \
338  const quaternionType& q2) const \
339 { \
340  return quaternionType( \
341  vtkQuaternion<type>::InnerPoint(q1, q2).GetData()); \
342 }
343 #define vtkQuaternionOperatorPlus(quaternionType, type) \
344 inline quaternionType operator+(const quaternionType& q) const \
345 { \
346  return quaternionType( ( \
347  static_cast< vtkQuaternion<type> > (*this) + \
348  static_cast< vtkQuaternion<type> > (q)).GetData()); \
349 }
350 #define vtkQuaternionOperatorMinus(quaternionType, type) \
351 inline quaternionType operator-(const quaternionType& q) const \
352 { \
353  return quaternionType( ( \
354  static_cast< vtkQuaternion<type> > (*this) - \
355  static_cast< vtkQuaternion<type> > (q)).GetData()); \
356 }
357 #define vtkQuaternionOperatorMultiply(quaternionType, type) \
358 inline quaternionType operator*(const quaternionType& q) const \
359 { \
360  return quaternionType( ( \
361  static_cast< vtkQuaternion<type> > (*this) * \
362  static_cast< vtkQuaternion<type> > (q)).GetData()); \
363 }
364 #define vtkQuaternionOperatorMultiplyScalar(quaternionType, type) \
365 inline quaternionType operator*(const type& scalar) const \
366 { \
367  return quaternionType( ( \
368  static_cast< vtkQuaternion<type> > (*this) * \
369  scalar).GetData()); \
370 }
371 #define vtkQuaternionOperatorDivide(quaternionType, type) \
372 inline quaternionType operator/(const quaternionType& q) const \
373 { \
374  return quaternionType( ( \
375  static_cast< vtkQuaternion<type> > (*this) / \
376  static_cast< vtkQuaternion<type> > (q)).GetData()); \
377 }
378 #define vtkQuaternionOperatorDivideScalar(quaternionType, type) \
379 inline quaternionType operator/(const type& scalar) const \
380 { \
381  return quaternionType( ( \
382  static_cast< vtkQuaternion<type> > (*this) / \
383  scalar).GetData()); \
384 }
385 
386 #define vtkQuaternionOperatorMacro(quaternionType, type) \
387 vtkQuaternionIdentity(quaternionType, type) \
388 vtkQuaternionNormalized(quaternionType, type) \
389 vtkQuaternionConjugated(quaternionType, type) \
390 vtkQuaternionInverse(quaternionType, type) \
391 vtkQuaternionUnitLog(quaternionType, type) \
392 vtkQuaternionUnitExp(quaternionType, type) \
393 vtkQuaternionNormalizedWithAngleInDegrees(quaternionType, type) \
394 vtkQuaternionSlerp(quaternionType, type) \
395 vtkQuaternionInnerPoint(quaternionType, type) \
396 vtkQuaternionOperatorPlus(quaternionType, type) \
397 vtkQuaternionOperatorMinus(quaternionType, type) \
398 vtkQuaternionOperatorMultiply(quaternionType, type) \
399 vtkQuaternionOperatorMultiplyScalar(quaternionType, type) \
400 vtkQuaternionOperatorDivide(quaternionType, type) \
401 vtkQuaternionOperatorDivideScalar(quaternionType, type)
402 
403 // .NAME vtkQuaternionf - Float quaternion type.
404 //
405 // .SECTION Description
406 // This class is uses vtkQuaternion with float type data.
407 // For futher description, see the templated class vtkQuaternion.
408 // @sa vtkQuaterniond vtkQuaternion
409 class vtkQuaternionf : public vtkQuaternion<float>
410 {
411 public:
413  explicit vtkQuaternionf(float w, float x, float y, float z)
414  : vtkQuaternion<float>(w, x, y, z) {}
415  explicit vtkQuaternionf(float scalar) : vtkQuaternion<float>(scalar) {}
416  explicit vtkQuaternionf(const float *init) : vtkQuaternion<float>(init) {}
418 };
419 
420 // .NAME vtkQuaterniond - Double quaternion type.
421 //
422 // .SECTION Description
423 // This class is uses vtkQuaternion with double type data.
424 // For futher description, seethe templated class vtkQuaternion.
425 // @sa vtkQuaternionf vtkQuaternion
426 class vtkQuaterniond : public vtkQuaternion<double>
427 {
428 public:
430  explicit vtkQuaterniond(double w, double x, double y, double z)
431  : vtkQuaternion<double>(w, x, y, z) {}
432  explicit vtkQuaterniond(double scalar) : vtkQuaternion<double>(scalar) {}
433  explicit vtkQuaterniond(const double *init) : vtkQuaternion<double>(init) {}
435 };
436 
437 #include "vtkQuaternion.txx"
438 
439 #endif // vtkQuaternion_h
440 // VTK-HeaderTest-Exclude: vtkQuaternion.h
vtkQuaternion< T > UnitLog() const
Return the unit log version of this quaternion.
vtkQuaternionf(float scalar)
void SetZ(const T &z)
Set/Get the y component of the quaternion, i.e.
void ToMatrix3x3(T A[3][3]) const
Convert a quaternion to a 3x3 rotation matrix.
vtkQuaternion< T > operator/(const vtkQuaternion< T > &q) const
Performs division of quaternions of the same type.
const T & GetZ() const
Set/Get the y component of the quaternion, i.e.
vtkQuaterniond(double w, double x, double y, double z)
T SquaredNorm() const
Get the squared norm of the quaternion.
void NormalizeWithAngleInDegrees()
Normalize a quaternion in place and transform it to so its angle is in degrees and its axis normalize...
void Invert()
Invert the quaternion in place.
void operator/=(const T &scalar)
Performs in place division of the quaternions by a scalar value.
vtkQuaternion< T > operator-(const vtkQuaternion< T > &q) const
Performs subtraction of quaternions of the same basic type.
vtkQuaternion< T > Normalized() const
Return the normalized form of this quaternion.
vtkQuaternion< T > NormalizedWithAngleInDegrees() const
Returns a quaternion normalized and transformed so its angle is in degrees and its axis normalized...
templated base type for storage of quaternions.
Definition: vtkQuaternion.h:38
vtkQuaternion< T > UnitExp() const
Return the unit exponential version of this quaternion.
void SetRotationAngleAndAxis(T angle, T axis[3])
Set/Get the angle (in radians) and the axis corresponding to the axis-angle rotation of this quaterni...
T GetRotationAngleAndAxis(T axis[3]) const
Set/Get the angle (in radians) and the axis corresponding to the axis-angle rotation of this quaterni...
vtkQuaterniond(double scalar)
void Get(T quat[4]) const
Set/Get the w, x, y and z components of the quaternion.
vtkQuaternionOperatorMacro(vtkQuaterniond, double)
const T & GetY() const
Set/Get the y component of the quaternion, i.e.
void SetY(const T &y)
Set/Get the y component of the quaternion, i.e.
vtkQuaternion< T > Slerp(T t, const vtkQuaternion< T > &q) const
Interpolate quaternions using spherical linear interpolation between this quaternion and q1 to produc...
void Conjugate()
Conjugate the quaternion in place.
vtkQuaternion(const T *init)
Initalize the quaternion's elements with the elements of the supplied array.
Definition: vtkQuaternion.h:56
vtkQuaternionf(const float *init)
templated base type for containers of constant size.
Definition: vtkTuple.h:35
void FromMatrix3x3(const T A[3][3])
Convert a 3x3 matrix into a quaternion.
vtkQuaternion< CastTo > Cast() const
Cast the quaternion to the specified type and return the result.
const T & GetW() const
Set/Get the w component of the quaternion, i.e.
const T & GetX() const
Set/Get the x component of the quaternion, i.e.
vtkQuaternion< T > InnerPoint(const vtkQuaternion< T > &q1, const vtkQuaternion< T > &q2) const
Interpolates between quaternions, using spherical quadrangle interpolation.
vtkQuaternionf(float w, float x, float y, float z)
void ToUnitExp()
Convert this quaternion to a unit exponential quaternion.
void SetW(const T &w)
Set/Get the w component of the quaternion, i.e.
vtkQuaternion< T > operator+(const vtkQuaternion< T > &q) const
Performs addition of quaternion of the same basic type.
vtkQuaternion< T > Inverse() const
Return the inverted form of this quaternion.
#define vtkQuaternionOperatorMacro(quaternionType, type)
vtkQuaterniond(const double *init)
T Normalize()
Normalize the quaternion in place.
vtkQuaternion(const T &scalar)
Initialize all of the quaternion's elements with the supplied scalar.
Definition: vtkQuaternion.h:49
void ToIdentity()
Set the quaternion to identity in place.
void Set(const T &w, const T &x, const T &y, const T &z)
Set/Get the w, x, y and z components of the quaternion.
void ToUnitLog()
Convert this quaternion to a unit log quaternion.
T Norm() const
Get the norm of the quaternion, i.e.
void operator*=(const T &scalar) const
Performs in place multiplication of the quaternions by a scalar value.
void SetX(const T &x)
Set/Get the x component of the quaternion, i.e.
vtkQuaternion< T > operator*(const vtkQuaternion< T > &q) const
Performs multiplication of quaternion of the same basic type.
static vtkQuaternion< T > Identity()
Return the identity quaternion.
vtkQuaternion()
Default constructor.
vtkQuaternion< T > Conjugated() const
Return the conjugate form of this quaternion.