math_quaternion.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_QUATERNION_H__
  19. #define __MATH_QUATERNION_H__
  20. #include <assert.h>
  21. #include <math.h>
  22. class idVec3_t;
  23. class angles_t;
  24. class mat3_t;
  25. class quat_t {
  26. public:
  27. float x;
  28. float y;
  29. float z;
  30. float w;
  31. quat_t();
  32. quat_t( float x, float y, float z, float w );
  33. friend void toQuat( idVec3_t &src, quat_t &dst );
  34. friend void toQuat( angles_t &src, quat_t &dst );
  35. friend void toQuat( mat3_t &src, quat_t &dst );
  36. float *vec4( void );
  37. float operator[]( int index ) const;
  38. float &operator[]( int index );
  39. void set( float x, float y, float z, float w );
  40. void operator=( quat_t a );
  41. friend quat_t operator+( quat_t a, quat_t b );
  42. quat_t &operator+=( quat_t a );
  43. friend quat_t operator-( quat_t a, quat_t b );
  44. quat_t &operator-=( quat_t a );
  45. friend quat_t operator*( quat_t a, float b );
  46. friend quat_t operator*( float a, quat_t b );
  47. quat_t &operator*=( float a );
  48. friend int operator==( quat_t a, quat_t b );
  49. friend int operator!=( quat_t a, quat_t b );
  50. float Length( void );
  51. quat_t &Normalize( void );
  52. quat_t operator-();
  53. };
  54. inline quat_t::quat_t() {
  55. }
  56. inline quat_t::quat_t( float x, float y, float z, float w ) {
  57. this->x = x;
  58. this->y = y;
  59. this->z = z;
  60. this->w = w;
  61. }
  62. inline float *quat_t::vec4( void ) {
  63. return &x;
  64. }
  65. inline float quat_t::operator[]( int index ) const {
  66. assert( ( index >= 0 ) && ( index < 4 ) );
  67. return ( &x )[ index ];
  68. }
  69. inline float& quat_t::operator[]( int index ) {
  70. assert( ( index >= 0 ) && ( index < 4 ) );
  71. return ( &x )[ index ];
  72. }
  73. inline void quat_t::set( float x, float y, float z, float w ) {
  74. this->x = x;
  75. this->y = y;
  76. this->z = z;
  77. this->w = w;
  78. }
  79. inline void quat_t::operator=( quat_t a ) {
  80. x = a.x;
  81. y = a.y;
  82. z = a.z;
  83. w = a.w;
  84. }
  85. inline quat_t operator+( quat_t a, quat_t b ) {
  86. return quat_t( a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w );
  87. }
  88. inline quat_t& quat_t::operator+=( quat_t a ) {
  89. x += a.x;
  90. y += a.y;
  91. z += a.z;
  92. w += a.w;
  93. return *this;
  94. }
  95. inline quat_t operator-( quat_t a, quat_t b ) {
  96. return quat_t( a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w );
  97. }
  98. inline quat_t& quat_t::operator-=( quat_t a ) {
  99. x -= a.x;
  100. y -= a.y;
  101. z -= a.z;
  102. w -= a.w;
  103. return *this;
  104. }
  105. inline quat_t operator*( quat_t a, float b ) {
  106. return quat_t( a.x * b, a.y * b, a.z * b, a.w * b );
  107. }
  108. inline quat_t operator*( float a, quat_t b ) {
  109. return b * a;
  110. }
  111. inline quat_t& quat_t::operator*=( float a ) {
  112. x *= a;
  113. y *= a;
  114. z *= a;
  115. w *= a;
  116. return *this;
  117. }
  118. inline int operator==( quat_t a, quat_t b ) {
  119. return ( ( a.x == b.x ) && ( a.y == b.y ) && ( a.z == b.z ) && ( a.w == b.w ) );
  120. }
  121. inline int operator!=( quat_t a, quat_t b ) {
  122. return ( ( a.x != b.x ) || ( a.y != b.y ) || ( a.z != b.z ) && ( a.w != b.w ) );
  123. }
  124. inline float quat_t::Length( void ) {
  125. float length;
  126. length = x * x + y * y + z * z + w * w;
  127. return ( float )sqrt( length );
  128. }
  129. inline quat_t& quat_t::Normalize( void ) {
  130. float length;
  131. float ilength;
  132. length = this->Length();
  133. if ( length ) {
  134. ilength = 1 / length;
  135. x *= ilength;
  136. y *= ilength;
  137. z *= ilength;
  138. w *= ilength;
  139. }
  140. return *this;
  141. }
  142. inline quat_t quat_t::operator-() {
  143. return quat_t( -x, -y, -z, -w );
  144. }
  145. #endif /* !__MATH_QUATERNION_H__ */