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.

345 lines
11 KiB

  1. //--------------------------------------------------------------------------------------------------
  2. /**
  3. @file qhMath.h
  4. @author Dirk Gregorius
  5. @version 0.1
  6. @date 30/11/2011
  7. Copyright(C) 2011 by D. Gregorius. All rights reserved.
  8. */
  9. //--------------------------------------------------------------------------------------------------
  10. #pragma once
  11. #include "qhTypes.h"
  12. #include "qhArray.h"
  13. #include <math.h>
  14. #include <limits>
  15. class qhVector3;
  16. class qhMatrix3;
  17. class qhQuaternion;
  18. class qhTransform;
  19. class qhPlane;
  20. class qhBounds3;
  21. #ifdef max
  22. #undef max
  23. #endif
  24. #ifdef min
  25. #undef min
  26. #endif
  27. #pragma warning( push )
  28. #pragma warning( disable : 4201 ) // nameless structs/unions
  29. //--------------------------------------------------------------------------------------------------
  30. // qhConstants
  31. //--------------------------------------------------------------------------------------------------
  32. #define QH_PI qhReal( 3.14159265358 )
  33. #define QH_2PI qhReal( 6.28318530717 )
  34. #define QH_SQRT2 qhReal( 1.41421356237 )
  35. #define QH_SQRT3 qhReal( 1.73205080757 )
  36. #define QH_DEG2RAD ( QH_PI / qhReal( 180 ) )
  37. #define QH_RAD2DEG ( qhReal( 180 ) / QH_PI )
  38. //--------------------------------------------------------------------------------------------------
  39. // qhMath
  40. //--------------------------------------------------------------------------------------------------
  41. qhReal qhSin( qhReal Rad );
  42. qhReal qhCos( qhReal Rad );
  43. qhReal qhTan( qhReal Rad );
  44. qhReal qhArcSin( qhReal X );
  45. qhReal qhArcCos( qhReal X );
  46. qhReal qhArcTan( qhReal X );
  47. qhReal qhArcTan2( qhReal Y, qhReal X );
  48. qhReal qhAbs( qhReal X );
  49. qhReal qhSqrt( qhReal X );
  50. template< typename T > T qhMin( T X, T Y );
  51. template< typename T > T qhMax( T X, T Y );
  52. template< typename T > T qhClamp( T X, T Min, T Max );
  53. //--------------------------------------------------------------------------------------------------
  54. // qhVector3
  55. //--------------------------------------------------------------------------------------------------
  56. class qhVector3
  57. {
  58. public:
  59. // Attributes
  60. qhReal X, Y, Z;
  61. // Construction
  62. qhVector3( void );
  63. qhVector3( qhReal X, qhReal Y, qhReal Z );
  64. qhVector3( const qhReal* V );
  65. // Conversion
  66. operator qhReal*( void );
  67. operator const qhReal*( void ) const;
  68. // Assignment
  69. qhVector3& operator*=( const qhVector3& V );
  70. qhVector3& operator+=( const qhVector3& V );
  71. qhVector3& operator-=( const qhVector3& V );
  72. qhVector3& operator*=( qhReal S );
  73. qhVector3& operator/=( qhReal S );
  74. // Unary operators
  75. qhVector3 operator+( void ) const;
  76. qhVector3 operator-( void ) const;
  77. };
  78. // Binary operators
  79. qhVector3 operator*( const qhMatrix3& M, const qhVector3& V );
  80. qhVector3 operator*( const qhQuaternion& Q, const qhVector3& V );
  81. qhVector3 operator*( const qhTransform& T, const qhVector3& V );
  82. qhVector3 operator*( const qhVector3& V1, const qhVector3& V2 );
  83. qhVector3 operator+( const qhVector3& V1, const qhVector3& V2 );
  84. qhVector3 operator-( const qhVector3& V1, const qhVector3& V2 );
  85. qhVector3 operator*( qhReal S, const qhVector3& V );
  86. qhVector3 operator*( const qhVector3& V, qhReal S );
  87. qhVector3 operator/( const qhVector3& V, qhReal S );
  88. bool operator==( const qhVector3& V1, const qhVector3& V2 );
  89. bool operator!=( const qhVector3& V1, const qhVector3& V2 );
  90. // Standard vector operations
  91. void qhStore( qhReal Dst[ 3 ], const qhVector3& V );
  92. qhVector3 qhMul( const qhMatrix3& M, const qhVector3& V );
  93. qhVector3 qhTMul( const qhMatrix3& M, const qhVector3& V );
  94. qhVector3 qhMul( const qhQuaternion& Q, const qhVector3& V );
  95. qhVector3 qhTMul( const qhQuaternion& Q, const qhVector3& V );
  96. qhVector3 qhMul( const qhTransform& T, const qhVector3& V );
  97. qhVector3 qhTMul( const qhTransform& T, const qhVector3& V );
  98. qhVector3 qhMul( const qhVector3& V1, const qhVector3& V2 );
  99. qhVector3 qhAdd( const qhVector3& V1, const qhVector3& V2 );
  100. qhVector3 qhSub( const qhVector3& V1, const qhVector3& V2 );
  101. qhVector3 qhCross( const qhVector3& V1, const qhVector3& V2 );
  102. qhVector3 qhScale( const qhVector3& V, qhReal S );
  103. qhVector3 qhNormalize( const qhVector3& V );
  104. qhVector3 qhNegate( const qhVector3& V );
  105. qhVector3 qhAbs( const qhVector3& V );
  106. qhVector3 qhMin( const qhVector3& V1, const qhVector3& V2 );
  107. qhVector3 qhMax( const qhVector3& V1, const qhVector3& V2 );
  108. qhVector3 qhClamp( const qhVector3& V, const qhVector3& Min, const qhVector3& Max );
  109. qhReal qhDot( const qhVector3& V1, const qhVector3& V2 );
  110. qhReal qhLength( const qhVector3& V );
  111. qhReal qhLengthSq( const qhVector3& V );
  112. qhReal qhDistance( const qhVector3& V1, const qhVector3& V2 );
  113. qhReal qhDistanceSq( const qhVector3& V1, const qhVector3& V2 );
  114. qhReal qhDet( const qhVector3& V1, const qhVector3& V2, const qhVector3& V3 );
  115. int qhMinElement( const qhVector3& V );
  116. int qhMaxElement( const qhVector3& V );
  117. // Constants
  118. QH_GLOBAL_CONSTANT const qhVector3 QH_VEC3_ZERO = qhVector3( 0, 0, 0 );
  119. QH_GLOBAL_CONSTANT const qhVector3 QH_VEC3_AXIS_X = qhVector3( 1, 0, 0 );
  120. QH_GLOBAL_CONSTANT const qhVector3 QH_VEC3_AXIS_Y = qhVector3( 0, 1, 0 );
  121. QH_GLOBAL_CONSTANT const qhVector3 QH_VEC3_AXIS_Z = qhVector3( 0, 0, 1 );
  122. //--------------------------------------------------------------------------------------------------
  123. // qhMatrix3
  124. //--------------------------------------------------------------------------------------------------
  125. class qhMatrix3
  126. {
  127. public:
  128. // Attributes
  129. qhVector3 C1, C2, C3;
  130. // Construction
  131. qhMatrix3( void );
  132. qhMatrix3( qhReal A11, qhReal A22, qhReal A33 );
  133. qhMatrix3( const qhVector3& C1, const qhVector3& C2, const qhVector3& C3 );
  134. // Assignment
  135. qhMatrix3& operator*=( const qhMatrix3& M );
  136. qhMatrix3& operator+=( const qhMatrix3& M );
  137. qhMatrix3& operator-=( const qhMatrix3& M );
  138. qhMatrix3& operator*=( qhReal F );
  139. qhMatrix3& operator/=( qhReal F );
  140. // Unary operators
  141. qhMatrix3 operator+( void ) const;
  142. qhMatrix3 operator-( void ) const;
  143. };
  144. // Binary arithmetic operators
  145. qhMatrix3 operator*( const qhMatrix3& M1, const qhMatrix3& M2 );
  146. qhMatrix3 operator+( const qhMatrix3& M1, const qhMatrix3& M2 );
  147. qhMatrix3 operator-( const qhMatrix3& M1, const qhMatrix3& M2 );
  148. qhMatrix3 operator*( qhReal F, const qhMatrix3& M );
  149. qhMatrix3 operator*( const qhMatrix3& M, qhReal F );
  150. qhMatrix3 operator/( const qhMatrix3& M, qhReal F );
  151. // Comparison operators
  152. bool operator==( const qhMatrix3& M1, const qhMatrix3& M2 );
  153. bool operator!=( const qhMatrix3& M1, const qhMatrix3& M2 );
  154. // Standard matrix operations
  155. qhMatrix3 qhMul( const qhMatrix3& M1, const qhMatrix3& M2 );
  156. qhMatrix3 qhTMul( const qhMatrix3& M1, const qhMatrix3& M2 );
  157. qhMatrix3 qhTranspose( const qhMatrix3& M );
  158. qhMatrix3 qhAdjoint( const qhMatrix3& M );
  159. qhMatrix3 qhInvert( const qhMatrix3& M );
  160. qhMatrix3 qhInvertT( const qhMatrix3& M );
  161. qhMatrix3 qhConvert( const qhQuaternion& Q );
  162. qhMatrix3 qhSkew( const qhVector3& V );
  163. qhReal qhTrace( const qhMatrix3& M );
  164. qhReal qhDet( const qhMatrix3& M );
  165. // Constants
  166. QH_GLOBAL_CONSTANT const qhMatrix3 QH_MAT3_ZERO = qhMatrix3( qhVector3( 0, 0, 0 ), qhVector3( 0, 0, 0 ), qhVector3( 0, 0, 0 ) );
  167. QH_GLOBAL_CONSTANT const qhMatrix3 QH_MAT3_IDENTITY = qhMatrix3( qhVector3( 1, 0, 0 ), qhVector3( 0, 1, 0 ), qhVector3( 0, 0, 1 ) );
  168. //--------------------------------------------------------------------------------------------------
  169. // qhQuaternion
  170. //--------------------------------------------------------------------------------------------------
  171. class qhQuaternion
  172. {
  173. public:
  174. // Attributes
  175. qhReal X, Y, Z, W;
  176. // Construction
  177. qhQuaternion( void );
  178. qhQuaternion( qhReal X, qhReal Y, qhReal Z, qhReal W );
  179. qhQuaternion( const qhVector3& V, qhReal S );
  180. qhQuaternion( const qhReal* Q );
  181. qhVector3 V( void ) const { return qhVector3( X, Y, Z ); }
  182. qhReal S( void ) const { return W; }
  183. };
  184. qhQuaternion operator*( const qhQuaternion& Q1, const qhQuaternion& Q2 );
  185. qhQuaternion qhRotation( const qhVector3& V1, const qhVector3& V2 );
  186. qhQuaternion qhRotationX( qhReal Rad );
  187. qhQuaternion qhRotationY( qhReal Rad );
  188. qhQuaternion qhRotationZ( qhReal Rad );
  189. qhQuaternion qhConjugate( const qhQuaternion& Q );
  190. qhQuaternion qhNormalize( const qhQuaternion& Q );
  191. qhReal qhDot( const qhQuaternion& Q1, const qhQuaternion& Q2 );
  192. qhReal qhLength( const qhQuaternion& Q );
  193. qhReal qhLengthSq( const qhQuaternion& Q );
  194. // Constants
  195. QH_GLOBAL_CONSTANT const qhQuaternion QH_QUAT_ZERO = qhQuaternion( 0, 0, 0, 0 );
  196. QH_GLOBAL_CONSTANT const qhQuaternion QH_QUAT_IDENTITY = qhQuaternion( 0, 0, 0, 1 );
  197. //--------------------------------------------------------------------------------------------------
  198. // qhTranform
  199. //--------------------------------------------------------------------------------------------------
  200. class qhTransform
  201. {
  202. public:
  203. // Attributes
  204. qhMatrix3 Rotation;
  205. qhVector3 Translation;
  206. };
  207. //--------------------------------------------------------------------------------------------------
  208. // qhPlane
  209. //--------------------------------------------------------------------------------------------------
  210. class qhPlane
  211. {
  212. public:
  213. // Attributes
  214. qhVector3 Normal;
  215. qhReal Offset;
  216. // Construction
  217. qhPlane( void );
  218. qhPlane( const qhVector3& Normal, qhReal Offset );
  219. qhPlane( const qhVector3& Normal, const qhVector3& Point );
  220. qhPlane( const qhVector3& Point1, const qhVector3& Point2, const qhVector3& Point3 );
  221. void Negate( void );
  222. void Normalize( void );
  223. void Translate( const qhVector3& Translation );
  224. qhReal Distance( const qhVector3& Point ) const;
  225. };
  226. // Standard plane operations
  227. void qhStore( qhReal Dst[ 4 ], const qhPlane& Plane );
  228. //--------------------------------------------------------------------------------------------------
  229. // qhPlane
  230. //--------------------------------------------------------------------------------------------------
  231. class qhBounds3
  232. {
  233. public:
  234. // Attributes
  235. qhVector3 Min, Max;
  236. // Construction
  237. qhBounds3( void );
  238. qhBounds3( const qhVector3& Min, const qhVector3& Max );
  239. // Assignment
  240. qhBounds3& operator+=( const qhVector3& Point );
  241. qhBounds3& operator+=( const qhBounds3& Bounds );
  242. // Standard bounds operations
  243. qhVector3 GetCenter( void ) const;
  244. qhVector3 GetExtent( void ) const;
  245. qhReal GetVolume( void ) const;
  246. };
  247. // Binary arithmetic operators
  248. qhBounds3 operator+( const qhBounds3& Bounds1, const qhBounds3& Bounds2 );
  249. // Comparison operators
  250. bool operator==( const qhBounds3& Bounds1, const qhBounds3& Bounds2 );
  251. bool operator!=( const qhBounds3& Bounds1, const qhBounds3& Bounds2 );
  252. QH_GLOBAL_CONSTANT const qhBounds3 QH_BOUNDS3_EMPTY = qhBounds3( qhVector3( QH_REAL_MAX, QH_REAL_MAX, QH_REAL_MAX ), qhVector3( -QH_REAL_MAX, -QH_REAL_MAX, -QH_REAL_MAX ) );
  253. QH_GLOBAL_CONSTANT const qhBounds3 QH_BOUNDS3_INFINITE = qhBounds3( qhVector3( -QH_REAL_MAX, -QH_REAL_MAX, -QH_REAL_MAX ), qhVector3( QH_REAL_MAX, QH_REAL_MAX, QH_REAL_MAX ) );
  254. //--------------------------------------------------------------------------------------------------
  255. // qhBox
  256. //--------------------------------------------------------------------------------------------------
  257. class qhBox
  258. {
  259. public:
  260. qhVector3 Center;
  261. qhQuaternion Orientation;
  262. qhVector3 Extent;
  263. void GetVertices( qhVector3 Vertices[ 8 ] ) const;
  264. };
  265. qhBox qhBestFit( const qhArray< qhVector3 >& Vertices, qhReal Threshold = qhReal( 1 ) );
  266. qhBox qhBestFit( int VertexCount, const void* VertexBase, int VertexStride = 3 * sizeof( qhReal ), qhReal Threshold = qhReal( 1 ) );
  267. #include "qhMath.inl"
  268. #pragma warning( pop )