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 =========================================================================*/
36 #ifndef vtkQuaternion_h
37 #define vtkQuaternion_h
38 
39 #include "vtkTuple.h"
40 
41 template<typename T> class vtkQuaternion : public vtkTuple<T, 4>
42 {
43 public:
47  vtkQuaternion();
48 
52  explicit vtkQuaternion(const T& scalar) : vtkTuple<T, 4>(scalar) {}
53 
59  explicit vtkQuaternion(const T* init) : vtkTuple<T, 4>(init) {}
60 
64  vtkQuaternion(const T& w, const T& x, const T& y, const T& z);
65 
69  T SquaredNorm() const;
70 
74  T Norm() const;
75 
79  void ToIdentity();
80 
85  static vtkQuaternion<T> Identity();
86 
91  T Normalize();
92 
97 
101  void Conjugate();
102 
107 
113  void Invert();
114 
118  vtkQuaternion<T> Inverse() const;
119 
125  void ToUnitLog();
126 
132  vtkQuaternion<T> UnitLog() const;
133 
139  void ToUnitExp();
140 
146  vtkQuaternion<T> UnitExp() const;
147 
153 
159 
161 
164  void Set(const T& w, const T& x, const T& y, const T& z);
165  void Set(T quat[4]);
166  void Get(T quat[4]) const;
168 
170 
173  void SetW(const T& w);
174  const T& GetW() const;
176 
178 
181  void SetX(const T& x);
182  const T& GetX() const;
184 
186 
189  void SetY(const T& y);
190  const T& GetY() const;
192 
194 
197  void SetZ(const T& z);
198  const T& GetZ() const;
200 
202 
206  T GetRotationAngleAndAxis(T axis[3]) const;
207  void SetRotationAngleAndAxis(T angle, T axis[3]);
209  const T& angle, const T& x, const T& y, const T& z);
211 
215  template<typename CastTo> vtkQuaternion<CastTo> Cast() const;
216 
222  void ToMatrix3x3(T A[3][3]) const;
223 
230  void FromMatrix3x3(const T A[3][3]);
231 
238  vtkQuaternion<T> Slerp(T t, const vtkQuaternion<T>& q) const;
239 
246  const vtkQuaternion<T>& q2) const;
247 
252 
257 
262 
266  vtkQuaternion<T> operator*(const T& scalar) const;
267 
271  void operator*=(const T& scalar) const;
272 
277 
281  vtkQuaternion<T> operator/(const T& scalar) const;
282 
284 
287  void operator/=(const T& scalar);
288 };
290 
295 #define vtkQuaternionIdentity(quaternionType, type) \
296 quaternionType Identity() const \
297 { \
298  return quaternionType(vtkQuaternion<type>::Identity().GetData()); \
299 }
300 #define vtkQuaternionNormalized(quaternionType, type) \
301 quaternionType Normalized() const \
302 { \
303  return quaternionType(vtkQuaternion<type>::Normalized().GetData()); \
304 }
305 #define vtkQuaternionConjugated(quaternionType, type) \
306 quaternionType Conjugated() const \
307 { \
308  return quaternionType(vtkQuaternion<type>::Conjugated().GetData()); \
309 }
310 #define vtkQuaternionInverse(quaternionType, type) \
311 quaternionType Inverse() const \
312 { \
313  return quaternionType(vtkQuaternion<type>::Inverse().GetData()); \
314 }
315 #define vtkQuaternionUnitLog(quaternionType, type) \
316 quaternionType UnitLog() const \
317 { \
318  return quaternionType( \
319  vtkQuaternion<type>::UnitLog().GetData()); \
320 }
321 #define vtkQuaternionUnitExp(quaternionType, type) \
322 quaternionType UnitExp() const \
323 { \
324  return quaternionType( \
325  vtkQuaternion<type>::UnitExp().GetData()); \
326 }
327 #define vtkQuaternionNormalizedWithAngleInDegrees(quaternionType, type) \
328 quaternionType NormalizedWithAngleInDegrees() const \
329 { \
330  return quaternionType( \
331  vtkQuaternion<type>::NormalizedWithAngleInDegrees().GetData()); \
332 }
333 #define vtkQuaternionSlerp(quaternionType, type) \
334 quaternionType Slerp(type t, const quaternionType& q) const \
335 { \
336  return quaternionType( \
337  vtkQuaternion<type>::Slerp(t, q).GetData()); \
338 }
339 #define vtkQuaternionInnerPoint(quaternionType, type) \
340 quaternionType InnerPoint(const quaternionType& q1, \
341  const quaternionType& q2) const \
342 { \
343  return quaternionType( \
344  vtkQuaternion<type>::InnerPoint(q1, q2).GetData()); \
345 }
346 #define vtkQuaternionOperatorPlus(quaternionType, type) \
347 inline quaternionType operator+(const quaternionType& q) const \
348 { \
349  return quaternionType( ( \
350  static_cast< vtkQuaternion<type> > (*this) + \
351  static_cast< vtkQuaternion<type> > (q)).GetData()); \
352 }
353 #define vtkQuaternionOperatorMinus(quaternionType, type) \
354 inline quaternionType operator-(const quaternionType& q) const \
355 { \
356  return quaternionType( ( \
357  static_cast< vtkQuaternion<type> > (*this) - \
358  static_cast< vtkQuaternion<type> > (q)).GetData()); \
359 }
360 #define vtkQuaternionOperatorMultiply(quaternionType, type) \
361 inline quaternionType operator*(const quaternionType& q) const \
362 { \
363  return quaternionType( ( \
364  static_cast< vtkQuaternion<type> > (*this) * \
365  static_cast< vtkQuaternion<type> > (q)).GetData()); \
366 }
367 #define vtkQuaternionOperatorMultiplyScalar(quaternionType, type) \
368 inline quaternionType operator*(const type& scalar) const \
369 { \
370  return quaternionType( ( \
371  static_cast< vtkQuaternion<type> > (*this) * \
372  scalar).GetData()); \
373 }
374 #define vtkQuaternionOperatorDivide(quaternionType, type) \
375 inline quaternionType operator/(const quaternionType& q) const \
376 { \
377  return quaternionType( ( \
378  static_cast< vtkQuaternion<type> > (*this) / \
379  static_cast< vtkQuaternion<type> > (q)).GetData()); \
380 }
381 #define vtkQuaternionOperatorDivideScalar(quaternionType, type) \
382 inline quaternionType operator/(const type& scalar) const \
383 { \
384  return quaternionType( ( \
385  static_cast< vtkQuaternion<type> > (*this) / \
386  scalar).GetData()); \
387 }
388 
389 #define vtkQuaternionOperatorMacro(quaternionType, type) \
390 vtkQuaternionIdentity(quaternionType, type) \
391 vtkQuaternionNormalized(quaternionType, type) \
392 vtkQuaternionConjugated(quaternionType, type) \
393 vtkQuaternionInverse(quaternionType, type) \
394 vtkQuaternionUnitLog(quaternionType, type) \
395 vtkQuaternionUnitExp(quaternionType, type) \
396 vtkQuaternionNormalizedWithAngleInDegrees(quaternionType, type) \
397 vtkQuaternionSlerp(quaternionType, type) \
398 vtkQuaternionInnerPoint(quaternionType, type) \
399 vtkQuaternionOperatorPlus(quaternionType, type) \
400 vtkQuaternionOperatorMinus(quaternionType, type) \
401 vtkQuaternionOperatorMultiply(quaternionType, type) \
402 vtkQuaternionOperatorMultiplyScalar(quaternionType, type) \
403 vtkQuaternionOperatorDivide(quaternionType, type) \
404 vtkQuaternionOperatorDivideScalar(quaternionType, type)
405 
406 // .NAME vtkQuaternionf - Float quaternion type.
407 //
408 // .SECTION Description
409 // This class is uses vtkQuaternion with float type data.
410 // For further description, see the templated class vtkQuaternion.
411 // @sa vtkQuaterniond vtkQuaternion
412 class vtkQuaternionf : public vtkQuaternion<float>
413 {
414 public:
416  explicit vtkQuaternionf(float w, float x, float y, float z)
417  : vtkQuaternion<float>(w, x, y, z) {}
418  explicit vtkQuaternionf(float scalar) : vtkQuaternion<float>(scalar) {}
419  explicit vtkQuaternionf(const float *init) : vtkQuaternion<float>(init) {}
421 };
422 
423 // .NAME vtkQuaterniond - Double quaternion type.
424 //
425 // .SECTION Description
426 // This class is uses vtkQuaternion with double type data.
427 // For further description, seethe templated class vtkQuaternion.
428 // @sa vtkQuaternionf vtkQuaternion
429 class vtkQuaterniond : public vtkQuaternion<double>
430 {
431 public:
433  explicit vtkQuaterniond(double w, double x, double y, double z)
434  : vtkQuaternion<double>(w, x, y, z) {}
435  explicit vtkQuaterniond(double scalar) : vtkQuaternion<double>(scalar) {}
436  explicit vtkQuaterniond(const double *init) : vtkQuaternion<double>(init) {}
438 };
439 
440 #include "vtkQuaternion.txx"
441 
442 #endif // vtkQuaternion_h
443 // 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:41
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.
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)
Initialize the quaternion&#39;s elements with the elements of the supplied array.
Definition: vtkQuaternion.h:59
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&#39;s elements with the supplied scalar.
Definition: vtkQuaternion.h:52
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.