/source/game/ai/vec3.h

https://github.com/Warsow/qfusion · C Header · 157 lines · 139 code · 18 blank · 0 comment · 5 complexity · f6182a166e4d42e904e4de8931fe79d7 MD5 · raw file

  1. #ifndef WSW_046c64e4_0cf8_4378_bbe0_4b2b3394c273_H
  2. #define WSW_046c64e4_0cf8_4378_bbe0_4b2b3394c273_H
  3. #include "../../gameshared/q_math.h"
  4. class Vec3
  5. {
  6. vec3_t vec;
  7. public:
  8. explicit Vec3( const vec3_t that ) {
  9. VectorCopy( that, Data() );
  10. }
  11. Vec3( const Vec3 &that ) {
  12. VectorCopy( that.Data(), Data() );
  13. }
  14. Vec3( vec_t x, vec_t y, vec_t z ) {
  15. VectorSet( vec, x, y, z );
  16. }
  17. Vec3 &operator=( const Vec3 &that ) {
  18. VectorCopy( that.Data(), Data() );
  19. return *this;
  20. }
  21. float Length() const { return (float)VectorLength( vec ); }
  22. float LengthFast() const { return VectorLengthFast( vec ); }
  23. float SquaredLength() const { return VectorLengthSquared( vec ); }
  24. float DistanceTo( const Vec3 &that ) const { return DistanceTo( that.Data() ); }
  25. float DistanceTo( const vec3_t that ) const { return (float)Distance( vec, that ); }
  26. float FastDistanceTo( const Vec3 &that ) const { return FastDistanceTo( that.Data() ); }
  27. float FastDistanceTo( const vec3_t that ) const { return DistanceFast( vec, that ); }
  28. float SquareDistanceTo( const Vec3 &that ) const { return SquareDistanceTo( that.Data() ); }
  29. float SquareDistanceTo( const vec3_t that ) const { return DistanceSquared( vec, that ); }
  30. float Distance2DTo( const Vec3 &that ) const { return Distance2DTo( that.vec ); }
  31. float Distance2DTo( const vec3_t that ) const { return std::sqrt( SquareDistance2DTo( that ) ); }
  32. float FastDistance2DTo( const Vec3 &that ) const { return FastDistance2DTo( that.vec ); }
  33. float FastDistance2DTo( const vec3_t that ) const { return Q_Sqrt( SquareDistance2DTo( that ) ); }
  34. float SquareDistance2DTo( const Vec3 &that ) const { return SquareDistanceTo( that.vec ); }
  35. float SquareDistance2DTo( const vec3_t that ) const {
  36. float dx = vec[0] - that[0];
  37. float dy = vec[1] - that[1];
  38. return dx * dx + dy * dy;
  39. }
  40. float Normalize() {
  41. float squareLength = VectorLengthSquared( vec );
  42. if( squareLength > 0 ) {
  43. float invLength = 1.0f / sqrtf( squareLength );
  44. VectorScale( vec, invLength, vec );
  45. return 1.0f / invLength;
  46. }
  47. return 0.0f;
  48. }
  49. float NormalizeFast() {
  50. float squareLength = VectorLengthSquared( vec );
  51. if( squareLength > 0 ) {
  52. float invLength = Q_RSqrt( squareLength );
  53. VectorScale( vec, invLength, vec );
  54. return squareLength * invLength;
  55. }
  56. return 0.0f;
  57. }
  58. float *Data() { return vec; }
  59. const float *Data() const { return vec; }
  60. void Set( const Vec3 &that ) { Set( that.Data() ); }
  61. void Set( const vec3_t that ) { Set( that[0], that[1], that[2] ); }
  62. void Set( vec_t x, vec_t y, vec_t z ) {
  63. VectorSet( this->vec, x, y, z );
  64. }
  65. void CopyTo( Vec3 &that ) const { that.Set( *this ); }
  66. void CopyTo( vec3_t that ) const { VectorCopy( this->vec, that ); }
  67. void CopyTo( vec_t *x, vec_t *y, vec_t *z ) const {
  68. if( x ) {
  69. *x = X();
  70. }
  71. if( y ) {
  72. *y = Y();
  73. }
  74. if( z ) {
  75. *z = Z();
  76. }
  77. }
  78. vec_t &X() { return vec[0]; }
  79. vec_t &Y() { return vec[1]; }
  80. vec_t &Z() { return vec[2]; }
  81. vec_t X() const { return vec[0]; }
  82. vec_t Y() const { return vec[1]; }
  83. vec_t Z() const { return vec[2]; }
  84. void operator+=( const Vec3 &that ) {
  85. VectorAdd( vec, that.vec, vec );
  86. }
  87. void operator+=( const vec3_t that ) {
  88. VectorAdd( vec, that, vec );
  89. }
  90. void operator-=( const Vec3 &that ) {
  91. VectorSubtract( vec, that.vec, vec );
  92. }
  93. void operator-=( const vec3_t that ) {
  94. VectorSubtract( vec, that, vec );
  95. }
  96. void operator*=( float scale ) {
  97. VectorScale( vec, scale, vec );
  98. }
  99. Vec3 operator*( float scale ) const {
  100. return Vec3( scale * X(), scale * Y(), scale * Z() );
  101. }
  102. Vec3 operator+( const Vec3 &that ) const {
  103. return Vec3( X() + that.X(), Y() + that.Y(), Z() + that.Z() );
  104. }
  105. Vec3 operator+( const vec3_t that ) const {
  106. return Vec3( X() + that[0], Y() + that[1], Z() + that[2] );
  107. }
  108. Vec3 operator-( const Vec3 &that ) const {
  109. return Vec3( X() - that.X(), Y() - that.Y(), Z() - that.Z() );
  110. }
  111. Vec3 operator-( const vec3_t that ) const {
  112. return Vec3( X() - that[0], Y() - that[1], Z() - that[2] );
  113. }
  114. Vec3 operator-() const {
  115. return Vec3( -X(), -Y(), -Z() );
  116. }
  117. vec_t Dot( const Vec3 &that ) const {
  118. return DotProduct( vec, that.vec );
  119. }
  120. float Dot( const vec3_t that ) const {
  121. return DotProduct( vec, that );
  122. }
  123. inline Vec3 Cross( const Vec3 &that ) const {
  124. return Vec3(
  125. Y() * that.Z() - Z() * that.Y(),
  126. Z() * that.X() - X() * that.Z(),
  127. X() * that.Y() - Y() * that.X() );
  128. }
  129. inline Vec3 Cross( const vec3_t that ) const {
  130. return Vec3(
  131. Y() * that[2] - Z() * that[1],
  132. Z() * that[0] - X() * that[2],
  133. X() * that[2] - Y() * that[1] );
  134. }
  135. };
  136. inline Vec3 operator *( float scale, const Vec3 &v ) {
  137. return v * scale;
  138. }
  139. #endif