math_angles.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #ifndef __MATH_ANGLES_H__
  19. #define __MATH_ANGLES_H__
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. #include "math_vector.h"
  23. class mat3_t;
  24. class quat_t;
  25. class idVec3_t;
  26. typedef idVec3_t &vec3_p;
  27. class angles_t {
  28. public:
  29. float pitch;
  30. float yaw;
  31. float roll;
  32. angles_t();
  33. angles_t( float pitch, float yaw, float roll );
  34. angles_t( const idVec3_t &vec );
  35. friend void toAngles( idVec3_t &src, angles_t &dst );
  36. friend void toAngles( quat_t &src, angles_t &dst );
  37. friend void toAngles( mat3_t &src, angles_t &dst );
  38. operator vec3_p();
  39. float operator[]( int index ) const;
  40. float& operator[]( int index );
  41. void set( float pitch, float yaw, float roll );
  42. void operator=( angles_t const &a );
  43. void operator=( idVec3_t const &a );
  44. friend angles_t operator+( const angles_t &a, const angles_t &b );
  45. angles_t &operator+=( angles_t const &a );
  46. angles_t &operator+=( idVec3_t const &a );
  47. friend angles_t operator-( angles_t &a, angles_t &b );
  48. angles_t &operator-=( angles_t &a );
  49. friend angles_t operator*( const angles_t &a, float b );
  50. friend angles_t operator*( float a, const angles_t &b );
  51. angles_t &operator*=( float a );
  52. friend int operator==( angles_t &a, angles_t &b );
  53. friend int operator!=( angles_t &a, angles_t &b );
  54. void toVectors( idVec3_t *forward, idVec3_t *right = NULL, idVec3_t *up = NULL );
  55. idVec3_t toForward( void );
  56. angles_t &Zero( void );
  57. angles_t &Normalize360( void );
  58. angles_t &Normalize180( void );
  59. };
  60. extern angles_t ang_zero;
  61. inline angles_t::angles_t() {}
  62. inline angles_t::angles_t( float pitch, float yaw, float roll ) {
  63. this->pitch = pitch;
  64. this->yaw = yaw;
  65. this->roll = roll;
  66. }
  67. inline angles_t::angles_t( const idVec3_t &vec ) {
  68. this->pitch = vec.x;
  69. this->yaw = vec.y;
  70. this->roll = vec.z;
  71. }
  72. inline float angles_t::operator[]( int index ) const {
  73. assert( ( index >= 0 ) && ( index < 3 ) );
  74. return ( &pitch )[ index ];
  75. }
  76. inline float& angles_t::operator[]( int index ) {
  77. assert( ( index >= 0 ) && ( index < 3 ) );
  78. return ( &pitch )[ index ];
  79. }
  80. inline angles_t::operator vec3_p( void ) {
  81. return *( idVec3_t * )&pitch;
  82. }
  83. inline void angles_t::set( float pitch, float yaw, float roll ) {
  84. this->pitch = pitch;
  85. this->yaw = yaw;
  86. this->roll = roll;
  87. }
  88. inline void angles_t::operator=( angles_t const &a ) {
  89. pitch = a.pitch;
  90. yaw = a.yaw;
  91. roll = a.roll;
  92. }
  93. inline void angles_t::operator=( idVec3_t const &a ) {
  94. pitch = a[ 0 ];
  95. yaw = a[ 1 ];
  96. roll = a[ 2 ];
  97. }
  98. inline angles_t operator+( const angles_t &a, const angles_t &b ) {
  99. return angles_t( a.pitch + b.pitch, a.yaw + b.yaw, a.roll + b.roll );
  100. }
  101. inline angles_t& angles_t::operator+=( angles_t const &a ) {
  102. pitch += a.pitch;
  103. yaw += a.yaw;
  104. roll += a.roll;
  105. return *this;
  106. }
  107. inline angles_t& angles_t::operator+=( idVec3_t const &a ) {
  108. pitch += a.x;
  109. yaw += a.y;
  110. roll += a.z;
  111. return *this;
  112. }
  113. inline angles_t operator-( angles_t &a, angles_t &b ) {
  114. return angles_t( a.pitch - b.pitch, a.yaw - b.yaw, a.roll - b.roll );
  115. }
  116. inline angles_t& angles_t::operator-=( angles_t &a ) {
  117. pitch -= a.pitch;
  118. yaw -= a.yaw;
  119. roll -= a.roll;
  120. return *this;
  121. }
  122. inline angles_t operator*( const angles_t &a, float b ) {
  123. return angles_t( a.pitch * b, a.yaw * b, a.roll * b );
  124. }
  125. inline angles_t operator*( float a, const angles_t &b ) {
  126. return angles_t( a * b.pitch, a * b.yaw, a * b.roll );
  127. }
  128. inline angles_t& angles_t::operator*=( float a ) {
  129. pitch *= a;
  130. yaw *= a;
  131. roll *= a;
  132. return *this;
  133. }
  134. inline int operator==( angles_t &a, angles_t &b ) {
  135. return ( ( a.pitch == b.pitch ) && ( a.yaw == b.yaw ) && ( a.roll == b.roll ) );
  136. }
  137. inline int operator!=( angles_t &a, angles_t &b ) {
  138. return ( ( a.pitch != b.pitch ) || ( a.yaw != b.yaw ) || ( a.roll != b.roll ) );
  139. }
  140. inline angles_t& angles_t::Zero( void ) {
  141. pitch = 0.0f;
  142. yaw = 0.0f;
  143. roll = 0.0f;
  144. return *this;
  145. }
  146. #endif /* !__MATH_ANGLES_H__ */