Cafu Engine
Rotation.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_MATH_ROTATION_HPP_INCLUDED
8 #define CAFU_MATH_ROTATION_HPP_INCLUDED
9 
10 #include "Matrix3x3.hpp"
11 #include "Vector3.hpp"
12 
13 
14 namespace cf
15 {
16  namespace math
17  {
18  /// This class describes a rotation about an arbitrary origin and an arbitrary axis.
19  /// The mathematical background is simple, see for example the lecture notes of my computer graphics
20  /// course in winter semester 2001/2002, D:\\Uni\\Computergrafik\\Lecture02_AffineTransforms.pdf page 16.
21  template<class T> class RotationT
22  {
23  public:
24 
25  /// The default constructor. It creates a rotation that represents a "null" rotation.
27  : m_Origin(),
28  m_Axis(),
29  m_Angle(0),
30  m_IsRotMatValid(true), // Ok, because m_RotMat is inited as the identity matrix.
31  m_RotMat()
32  {
33  }
34 
35  /// The constructor for creating a rotation.
36  /// @param Origin The origin (center) of the rotation.
37  /// @param Axis The axis of the rotation.
38  /// @param Angle The Euler angle (in degrees) for the rotation.
39  RotationT(const Vector3T<T>& Origin, const Vector3T<T>& Axis, const T& Angle)
40  : m_Origin(Origin),
41  m_Axis(Axis),
42  m_Angle(Angle),
43  m_IsRotMatValid(false),
44  m_RotMat()
45  {
46  }
47 
48  /// Gets the origin of this rotation.
49  const Vector3T<T>& GetOrigin() const { return m_Origin; }
50 
51  /// Gets the rotation axis.
52  const Vector3T<T>& GetAxis() const { return m_Axis; }
53 
54  /// Gets the rotation angle.
55  T GetAngle() const { return m_Angle; }
56 
57  /// Gets the rotation matrix.
58  const Matrix3x3T<T>& GetRotMat() const;
59 
60  /// Rotates a vector with this rotation.
61  Vector3T<T> GetRotated(const Vector3T<T>& A) const { return GetRotMat()*(A-m_Origin) + m_Origin; }
62 
63  /// Same as GetRotated.
64  Vector3T<T> operator * (const Vector3T<T>& A) const { return GetRotated(A); }
65 
66 
67  private:
68 
69  Vector3T<T> m_Origin;
70  Vector3T<T> m_Axis;
71  T m_Angle;
72 
73  mutable bool m_IsRotMatValid; ///< Whether the m_RotMat member is valid (matches the m_Axis and m_Angle).
74  mutable Matrix3x3T<T> m_RotMat; ///< The 3x3 matrix that corresponds to the rotation about m_Axis about m_Angle degrees. Note that this always translates about the (0, 0, 0) origin, it does *not* take m_Origin into account!
75  };
76 
77 
78  /// Typedef for a RotationT of floats.
79  typedef RotationT<float> RotationfT;
80 
81  /// Typedef for a Rotation of doubles.
82  typedef RotationT<double> RotationdT;
83  }
84 }
85 
86 #endif
Vector3T< T > GetRotated(const Vector3T< T > &A) const
Rotates a vector with this rotation.
Definition: Rotation.hpp:61
const Vector3T< T > & GetAxis() const
Gets the rotation axis.
Definition: Rotation.hpp:52
Vector3T< T > operator*(const Vector3T< T > &A) const
Same as GetRotated.
Definition: Rotation.hpp:64
const Vector3T< T > & GetOrigin() const
Gets the origin of this rotation.
Definition: Rotation.hpp:49
This class represents a polymorphic 3-dimensional vector.
Definition: Misc.hpp:11
RotationT()
The default constructor. It creates a rotation that represents a "null" rotation. ...
Definition: Rotation.hpp:26
RotationT(const Vector3T< T > &Origin, const Vector3T< T > &Axis, const T &Angle)
The constructor for creating a rotation.
Definition: Rotation.hpp:39
const Matrix3x3T< T > & GetRotMat() const
Gets the rotation matrix.
Definition: Rotation.cpp:14
This class describes a rotation about an arbitrary origin and an arbitrary axis.
Definition: Rotation.hpp:21
T GetAngle() const
Gets the rotation angle.
Definition: Rotation.hpp:55
This class represents a generic 3x3 matrix.
Definition: Angles.hpp:17