Rotation.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __MATH_ROTATION_H__
  21. #define __MATH_ROTATION_H__
  22. /*
  23. ===============================================================================
  24. Describes a complete rotation in degrees about an abritray axis.
  25. A local rotation matrix is stored for fast rotation of multiple points.
  26. ===============================================================================
  27. */
  28. class idAngles;
  29. class idQuat;
  30. class idMat3;
  31. class idRotation {
  32. friend class idAngles;
  33. friend class idQuat;
  34. friend class idMat3;
  35. public:
  36. idRotation( void );
  37. idRotation( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle );
  38. void Set( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle );
  39. void SetOrigin( const idVec3 &rotationOrigin );
  40. void SetVec( const idVec3 &rotationVec ); // has to be normalized
  41. void SetVec( const float x, const float y, const float z ); // has to be normalized
  42. void SetAngle( const float rotationAngle );
  43. void Scale( const float s );
  44. void ReCalculateMatrix( void );
  45. const idVec3 & GetOrigin( void ) const;
  46. const idVec3 & GetVec( void ) const;
  47. float GetAngle( void ) const;
  48. idRotation operator-() const; // flips rotation
  49. idRotation operator*( const float s ) const; // scale rotation
  50. idRotation operator/( const float s ) const; // scale rotation
  51. idRotation & operator*=( const float s ); // scale rotation
  52. idRotation & operator/=( const float s ); // scale rotation
  53. idVec3 operator*( const idVec3 &v ) const; // rotate vector
  54. friend idRotation operator*( const float s, const idRotation &r ); // scale rotation
  55. friend idVec3 operator*( const idVec3 &v, const idRotation &r ); // rotate vector
  56. friend idVec3 & operator*=( idVec3 &v, const idRotation &r ); // rotate vector
  57. idAngles ToAngles( void ) const;
  58. idQuat ToQuat( void ) const;
  59. const idMat3 & ToMat3( void ) const;
  60. idMat4 ToMat4( void ) const;
  61. idVec3 ToAngularVelocity( void ) const;
  62. void RotatePoint( idVec3 &point ) const;
  63. void Normalize180( void );
  64. void Normalize360( void );
  65. private:
  66. idVec3 origin; // origin of rotation
  67. idVec3 vec; // normalized vector to rotate around
  68. float angle; // angle of rotation in degrees
  69. mutable idMat3 axis; // rotation axis
  70. mutable bool axisValid; // true if rotation axis is valid
  71. };
  72. ID_INLINE idRotation::idRotation( void ) {
  73. }
  74. ID_INLINE idRotation::idRotation( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle ) {
  75. origin = rotationOrigin;
  76. vec = rotationVec;
  77. angle = rotationAngle;
  78. axisValid = false;
  79. }
  80. ID_INLINE void idRotation::Set( const idVec3 &rotationOrigin, const idVec3 &rotationVec, const float rotationAngle ) {
  81. origin = rotationOrigin;
  82. vec = rotationVec;
  83. angle = rotationAngle;
  84. axisValid = false;
  85. }
  86. ID_INLINE void idRotation::SetOrigin( const idVec3 &rotationOrigin ) {
  87. origin = rotationOrigin;
  88. }
  89. ID_INLINE void idRotation::SetVec( const idVec3 &rotationVec ) {
  90. vec = rotationVec;
  91. axisValid = false;
  92. }
  93. ID_INLINE void idRotation::SetVec( float x, float y, float z ) {
  94. vec[0] = x;
  95. vec[1] = y;
  96. vec[2] = z;
  97. axisValid = false;
  98. }
  99. ID_INLINE void idRotation::SetAngle( const float rotationAngle ) {
  100. angle = rotationAngle;
  101. axisValid = false;
  102. }
  103. ID_INLINE void idRotation::Scale( const float s ) {
  104. angle *= s;
  105. axisValid = false;
  106. }
  107. ID_INLINE void idRotation::ReCalculateMatrix( void ) {
  108. axisValid = false;
  109. ToMat3();
  110. }
  111. ID_INLINE const idVec3 &idRotation::GetOrigin( void ) const {
  112. return origin;
  113. }
  114. ID_INLINE const idVec3 &idRotation::GetVec( void ) const {
  115. return vec;
  116. }
  117. ID_INLINE float idRotation::GetAngle( void ) const {
  118. return angle;
  119. }
  120. ID_INLINE idRotation idRotation::operator-() const {
  121. return idRotation( origin, vec, -angle );
  122. }
  123. ID_INLINE idRotation idRotation::operator*( const float s ) const {
  124. return idRotation( origin, vec, angle * s );
  125. }
  126. ID_INLINE idRotation idRotation::operator/( const float s ) const {
  127. assert( s != 0.0f );
  128. return idRotation( origin, vec, angle / s );
  129. }
  130. ID_INLINE idRotation &idRotation::operator*=( const float s ) {
  131. angle *= s;
  132. axisValid = false;
  133. return *this;
  134. }
  135. ID_INLINE idRotation &idRotation::operator/=( const float s ) {
  136. assert( s != 0.0f );
  137. angle /= s;
  138. axisValid = false;
  139. return *this;
  140. }
  141. ID_INLINE idVec3 idRotation::operator*( const idVec3 &v ) const {
  142. if ( !axisValid ) {
  143. ToMat3();
  144. }
  145. return ((v - origin) * axis + origin);
  146. }
  147. ID_INLINE idRotation operator*( const float s, const idRotation &r ) {
  148. return r * s;
  149. }
  150. ID_INLINE idVec3 operator*( const idVec3 &v, const idRotation &r ) {
  151. return r * v;
  152. }
  153. ID_INLINE idVec3 &operator*=( idVec3 &v, const idRotation &r ) {
  154. v = r * v;
  155. return v;
  156. }
  157. ID_INLINE void idRotation::RotatePoint( idVec3 &point ) const {
  158. if ( !axisValid ) {
  159. ToMat3();
  160. }
  161. point = ((point - origin) * axis + origin);
  162. }
  163. #endif /* !__MATH_ROTATION_H__ */