Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
4.0 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // A simple 3D integer vector class.
  4. //
  5. //===============================================================================
  6. #ifndef INTVECTOR3D_H
  7. #define INTVECTOR3D_H
  8. #if defined( COMPILER_MSVC )
  9. #pragma once
  10. #endif
  11. //-----------------------------------------------------------------------------
  12. // A simple, 3-component, 32-bit integer vector.
  13. //
  14. // Use when SIMD versions aren't appropriate (e.g. for branch-heavy code,
  15. // when readability/ease-of-use trump performance).
  16. //-----------------------------------------------------------------------------
  17. class IntVector3D
  18. {
  19. public:
  20. int x, y, z;
  21. IntVector3D() { }
  22. IntVector3D( int nX, int nY, int nZ ) : x( nX ), y( nY ), z( nZ ) { }
  23. explicit IntVector3D( int nReplicate ) : x( nReplicate ), y( nReplicate ), z( nReplicate ) { }
  24. explicit IntVector3D( Vector v, float flEpsilon ) : x( v.x + flEpsilon ), y( v.y + flEpsilon ), z( v.z + flEpsilon ) { }
  25. Vector ToVector() const { return Vector( x, y, z ); }
  26. IntVector3D operator+( const IntVector3D &rhs ) const
  27. {
  28. return IntVector3D( x + rhs.x, y + rhs.y, z + rhs.z );
  29. }
  30. IntVector3D operator-( const IntVector3D &rhs ) const
  31. {
  32. return IntVector3D( x - rhs.x, y - rhs.y, z - rhs.z );
  33. }
  34. IntVector3D operator-() const
  35. {
  36. return IntVector3D( -x, -y, -z );
  37. }
  38. IntVector3D operator*( int n ) const
  39. {
  40. return IntVector3D( n * x, n * y, n * z );
  41. }
  42. IntVector3D operator*( const IntVector3D &rhs ) const
  43. {
  44. return IntVector3D( x * rhs.x, y * rhs.y, z * rhs.z );
  45. }
  46. IntVector3D operator/( int n ) const
  47. {
  48. return IntVector3D( x / n, y / n, z / n );
  49. }
  50. IntVector3D operator%( int n ) const
  51. {
  52. return IntVector3D( x % n, y % n, z % n );
  53. }
  54. IntVector3D& operator+=( const IntVector3D &rhs )
  55. {
  56. x += rhs.x;
  57. y += rhs.y;
  58. z += rhs.z;
  59. return *this;
  60. }
  61. IntVector3D& operator-=( const IntVector3D &rhs )
  62. {
  63. x -= rhs.x;
  64. y -= rhs.y;
  65. z -= rhs.z;
  66. return *this;
  67. }
  68. IntVector3D& operator*=( int n )
  69. {
  70. x *= n;
  71. y *= n;
  72. z *= n;
  73. return *this;
  74. }
  75. IntVector3D& operator/=( int n )
  76. {
  77. x /= n;
  78. y /= n;
  79. z /= n;
  80. return *this;
  81. }
  82. IntVector3D& operator%=( int n )
  83. {
  84. x %= n;
  85. y %= n;
  86. z %= n;
  87. return *this;
  88. }
  89. bool operator==( const IntVector3D &rhs ) const
  90. {
  91. return x == rhs.x && y == rhs.y && z == rhs.z;
  92. }
  93. bool operator!=( const IntVector3D &rhs ) const
  94. {
  95. return x != rhs.x || y != rhs.y || z != rhs.z;
  96. }
  97. const int& operator[]( const int i ) const
  98. {
  99. Assert( i >= 0 && i < 3 );
  100. return ( ( int * )this )[i];
  101. }
  102. int& operator[]( const int i )
  103. {
  104. Assert( i >= 0 && i < 3 );
  105. return ( ( int * )this )[i];
  106. }
  107. int Dot( const IntVector3D &rhs ) const
  108. {
  109. return x * rhs.x + y * rhs.y + z * rhs.z;
  110. }
  111. int LengthSqr() const
  112. {
  113. return x * x + y * y + z * z;
  114. }
  115. bool StrictlyGreater( const IntVector3D &rhs ) const
  116. {
  117. return x > rhs.x && y > rhs.y && z > rhs.z;
  118. }
  119. bool StrictlyGreaterOrEqual( const IntVector3D &rhs ) const
  120. {
  121. return x >= rhs.x && y >= rhs.y && z >= rhs.z;
  122. }
  123. bool StrictlyLess( const IntVector3D &rhs ) const
  124. {
  125. return x < rhs.x && y < rhs.y && z < rhs.z;
  126. }
  127. bool StrictlyLessOrEqual( const IntVector3D &rhs ) const
  128. {
  129. return x <= rhs.x && y <= rhs.y && z <= rhs.z;
  130. }
  131. bool AnyGreater( const IntVector3D &rhs ) const
  132. {
  133. return x > rhs.x || y > rhs.y || z > rhs.z;
  134. }
  135. bool AnyGreaterOrEqual( const IntVector3D &rhs ) const
  136. {
  137. return x >= rhs.x || y >= rhs.y || z >= rhs.z;
  138. }
  139. bool AnyLess( const IntVector3D &rhs ) const
  140. {
  141. return x < rhs.x || y < rhs.y || z < rhs.z;
  142. }
  143. bool AnyLessOrEqual( const IntVector3D &rhs ) const
  144. {
  145. return x <= rhs.x || y <= rhs.y || z <= rhs.z;
  146. }
  147. };
  148. inline IntVector3D Max( const IntVector3D &lhs, const IntVector3D &rhs )
  149. {
  150. return IntVector3D( MAX( lhs.x, rhs.x ), MAX( lhs.y, rhs.y ), MAX( lhs.z, rhs.z ) );
  151. }
  152. inline IntVector3D Min( const IntVector3D &rhs, const IntVector3D &lhs )
  153. {
  154. return IntVector3D( MIN( lhs.x, rhs.x ), MIN( lhs.y, rhs.y ), MIN( lhs.z, rhs.z ) );
  155. }
  156. static const IntVector3D INT_VECTOR3_ORIGIN( 0, 0, 0 );
  157. #endif // INTVECTOR3D_H