Angles.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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_ANGLES_H__
  21. #define __MATH_ANGLES_H__
  22. /*
  23. ===============================================================================
  24. Euler angles
  25. ===============================================================================
  26. */
  27. // angle indexes
  28. #define PITCH 0 // up / down
  29. #define YAW 1 // left / right
  30. #define ROLL 2 // fall over
  31. class idVec3;
  32. class idQuat;
  33. class idRotation;
  34. class idMat3;
  35. class idMat4;
  36. class idAngles {
  37. public:
  38. float pitch;
  39. float yaw;
  40. float roll;
  41. idAngles();
  42. idAngles( float pitch, float yaw, float roll );
  43. explicit idAngles( const idVec3 &v );
  44. void Set( float pitch, float yaw, float roll );
  45. idAngles & Zero();
  46. float operator[]( int index ) const;
  47. float & operator[]( int index );
  48. idAngles operator-() const; // negate angles, in general not the inverse rotation
  49. idAngles & operator=( const idAngles &a );
  50. idAngles operator+( const idAngles &a ) const;
  51. idAngles & operator+=( const idAngles &a );
  52. idAngles operator-( const idAngles &a ) const;
  53. idAngles & operator-=( const idAngles &a );
  54. idAngles operator*( const float a ) const;
  55. idAngles & operator*=( const float a );
  56. idAngles operator/( const float a ) const;
  57. idAngles & operator/=( const float a );
  58. friend idAngles operator*( const float a, const idAngles &b );
  59. bool Compare( const idAngles &a ) const; // exact compare, no epsilon
  60. bool Compare( const idAngles &a, const float epsilon ) const; // compare with epsilon
  61. bool operator==( const idAngles &a ) const; // exact compare, no epsilon
  62. bool operator!=( const idAngles &a ) const; // exact compare, no epsilon
  63. idAngles & Normalize360(); // normalizes 'this'
  64. idAngles & Normalize180(); // normalizes 'this'
  65. void Clamp( const idAngles &min, const idAngles &max );
  66. int GetDimension() const;
  67. void ToVectors( idVec3 *forward, idVec3 *right = NULL, idVec3 *up = NULL ) const;
  68. idVec3 ToForward() const;
  69. idQuat ToQuat() const;
  70. idRotation ToRotation() const;
  71. idMat3 ToMat3() const;
  72. idMat4 ToMat4() const;
  73. idVec3 ToAngularVelocity() const;
  74. const float * ToFloatPtr() const;
  75. float * ToFloatPtr();
  76. const char * ToString( int precision = 2 ) const;
  77. };
  78. extern idAngles ang_zero;
  79. ID_INLINE idAngles::idAngles() {
  80. }
  81. ID_INLINE idAngles::idAngles( float pitch, float yaw, float roll ) {
  82. this->pitch = pitch;
  83. this->yaw = yaw;
  84. this->roll = roll;
  85. }
  86. ID_INLINE idAngles::idAngles( const idVec3 &v ) {
  87. this->pitch = v[0];
  88. this->yaw = v[1];
  89. this->roll = v[2];
  90. }
  91. ID_INLINE void idAngles::Set( float pitch, float yaw, float roll ) {
  92. this->pitch = pitch;
  93. this->yaw = yaw;
  94. this->roll = roll;
  95. }
  96. ID_INLINE idAngles &idAngles::Zero() {
  97. pitch = yaw = roll = 0.0f;
  98. return *this;
  99. }
  100. ID_INLINE float idAngles::operator[]( int index ) const {
  101. assert( ( index >= 0 ) && ( index < 3 ) );
  102. return ( &pitch )[ index ];
  103. }
  104. ID_INLINE float &idAngles::operator[]( int index ) {
  105. assert( ( index >= 0 ) && ( index < 3 ) );
  106. return ( &pitch )[ index ];
  107. }
  108. ID_INLINE idAngles idAngles::operator-() const {
  109. return idAngles( -pitch, -yaw, -roll );
  110. }
  111. ID_INLINE idAngles &idAngles::operator=( const idAngles &a ) {
  112. pitch = a.pitch;
  113. yaw = a.yaw;
  114. roll = a.roll;
  115. return *this;
  116. }
  117. ID_INLINE idAngles idAngles::operator+( const idAngles &a ) const {
  118. return idAngles( pitch + a.pitch, yaw + a.yaw, roll + a.roll );
  119. }
  120. ID_INLINE idAngles& idAngles::operator+=( const idAngles &a ) {
  121. pitch += a.pitch;
  122. yaw += a.yaw;
  123. roll += a.roll;
  124. return *this;
  125. }
  126. ID_INLINE idAngles idAngles::operator-( const idAngles &a ) const {
  127. return idAngles( pitch - a.pitch, yaw - a.yaw, roll - a.roll );
  128. }
  129. ID_INLINE idAngles& idAngles::operator-=( const idAngles &a ) {
  130. pitch -= a.pitch;
  131. yaw -= a.yaw;
  132. roll -= a.roll;
  133. return *this;
  134. }
  135. ID_INLINE idAngles idAngles::operator*( const float a ) const {
  136. return idAngles( pitch * a, yaw * a, roll * a );
  137. }
  138. ID_INLINE idAngles& idAngles::operator*=( float a ) {
  139. pitch *= a;
  140. yaw *= a;
  141. roll *= a;
  142. return *this;
  143. }
  144. ID_INLINE idAngles idAngles::operator/( const float a ) const {
  145. float inva = 1.0f / a;
  146. return idAngles( pitch * inva, yaw * inva, roll * inva );
  147. }
  148. ID_INLINE idAngles& idAngles::operator/=( float a ) {
  149. float inva = 1.0f / a;
  150. pitch *= inva;
  151. yaw *= inva;
  152. roll *= inva;
  153. return *this;
  154. }
  155. ID_INLINE idAngles operator*( const float a, const idAngles &b ) {
  156. return idAngles( a * b.pitch, a * b.yaw, a * b.roll );
  157. }
  158. ID_INLINE bool idAngles::Compare( const idAngles &a ) const {
  159. return ( ( a.pitch == pitch ) && ( a.yaw == yaw ) && ( a.roll == roll ) );
  160. }
  161. ID_INLINE bool idAngles::Compare( const idAngles &a, const float epsilon ) const {
  162. if ( idMath::Fabs( pitch - a.pitch ) > epsilon ) {
  163. return false;
  164. }
  165. if ( idMath::Fabs( yaw - a.yaw ) > epsilon ) {
  166. return false;
  167. }
  168. if ( idMath::Fabs( roll - a.roll ) > epsilon ) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. ID_INLINE bool idAngles::operator==( const idAngles &a ) const {
  174. return Compare( a );
  175. }
  176. ID_INLINE bool idAngles::operator!=( const idAngles &a ) const {
  177. return !Compare( a );
  178. }
  179. ID_INLINE void idAngles::Clamp( const idAngles &min, const idAngles &max ) {
  180. if ( pitch < min.pitch ) {
  181. pitch = min.pitch;
  182. } else if ( pitch > max.pitch ) {
  183. pitch = max.pitch;
  184. }
  185. if ( yaw < min.yaw ) {
  186. yaw = min.yaw;
  187. } else if ( yaw > max.yaw ) {
  188. yaw = max.yaw;
  189. }
  190. if ( roll < min.roll ) {
  191. roll = min.roll;
  192. } else if ( roll > max.roll ) {
  193. roll = max.roll;
  194. }
  195. }
  196. ID_INLINE int idAngles::GetDimension() const {
  197. return 3;
  198. }
  199. ID_INLINE const float *idAngles::ToFloatPtr() const {
  200. return &pitch;
  201. }
  202. ID_INLINE float *idAngles::ToFloatPtr() {
  203. return &pitch;
  204. }
  205. #endif /* !__MATH_ANGLES_H__ */