/Server/Src/ServerEngine/XMath.h

https://github.com/ylmbtm/GameProject3 · C Header · 514 lines · 406 code · 86 blank · 22 comment · 32 complexity · eea137b5bd1272bbb88c509df30d5ed3 MD5 · raw file

  1. #ifndef __X_MATH_H_
  2. #define __X_MATH_H_
  3. #include "CommonConvert.h"
  4. #define PI 3.1415926f
  5. #define TWO_PI 6.2831852f
  6. #define DEG_TO_RAD 0.0174533f
  7. #define RAD_TO_DEG 57.2957805f
  8. // float Q_rsqrt( float number )
  9. // {
  10. // long i;
  11. // float x2, y;
  12. // const float threehalfs = 1.5F;
  13. //
  14. // x2 = number * 0.5F;
  15. // y = number;
  16. // i = * ( long* ) &y; // evil floating point bit level hacking
  17. // i = 0x5f3759df - ( i >> 1 ); // what the fuck?
  18. // y = * ( float* ) &i;
  19. // y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  20. // // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
  21. // return y;
  22. // }
  23. class Vector2D
  24. {
  25. public:
  26. float m_x;
  27. float m_y;
  28. Vector2D(float _x = 0.0f, float _y = 0.0f )
  29. {
  30. m_x = _x;
  31. m_y = _y;
  32. }
  33. Vector2D& operator *=(float v)
  34. {
  35. m_x *= v;
  36. m_y *= v;
  37. return *this;
  38. }
  39. Vector2D& operator /=(float v)
  40. {
  41. m_x /= v;
  42. m_y /= v;
  43. return *this;
  44. }
  45. inline Vector2D& operator -=(float v)
  46. {
  47. m_x -= v;
  48. m_y -= v;
  49. return *this;
  50. }
  51. inline Vector2D& operator +=(float v)
  52. {
  53. m_x += v;
  54. m_y += v;
  55. return *this;
  56. }
  57. inline Vector2D& operator +=(const Vector2D& v)
  58. {
  59. m_x += v.m_x;
  60. m_y += v.m_y;
  61. return *this;
  62. }
  63. inline Vector2D& operator -=(const Vector2D& v)
  64. {
  65. m_x -= v.m_x;
  66. m_y -= v.m_y;
  67. return *this;
  68. }
  69. inline bool operator ==(Vector2D& pt)
  70. {
  71. if(pt.m_x == m_x && pt.m_y == m_y)
  72. {
  73. return true;
  74. }
  75. return false;
  76. }
  77. inline Vector2D operator - (const Vector2D& rkVector) const
  78. {
  79. return Vector2D(
  80. m_x - rkVector.m_x,
  81. m_y - rkVector.m_y);
  82. }
  83. float Length() const
  84. {
  85. return sqrtf(m_x * m_x + m_y * m_y);
  86. }
  87. float SquaredLength() const
  88. {
  89. return m_x * m_x + m_y * m_y;
  90. }
  91. float Distance(Vector2D pos)
  92. {
  93. return (*this - pos).Length();
  94. }
  95. float Normalized()
  96. {
  97. float fLength = sqrtf(m_x * m_x + m_y * m_y);
  98. if (fLength > 1e-08)
  99. {
  100. float fInvLength = 1.0f / fLength;
  101. m_x *= fInvLength;
  102. m_y *= fInvLength;
  103. }
  104. return fLength;
  105. }
  106. FLOAT DistanceToSegment(Vector2D pt1, Vector2D pt2)
  107. {
  108. FLOAT cross = (pt2.m_x - pt1.m_x) * (m_x - pt1.m_x) + (pt2.m_y - pt1.m_y) * (m_y - pt1.m_y);
  109. if (cross <= 0)
  110. {
  111. return sqrtf((m_x - pt1.m_x) * (m_x - pt1.m_x) + (m_y - pt1.m_y) * (m_y - pt1.m_y));
  112. }
  113. FLOAT d2 = (pt2.m_x - pt1.m_x) * (pt2.m_x - pt1.m_x) + (pt2.m_y - pt1.m_y) * (pt2.m_y - pt1.m_y);
  114. if (cross >= d2)
  115. {
  116. return sqrtf((m_x - pt2.m_x) * (m_x - pt2.m_x) + (m_y - pt2.m_y) * (m_y - pt2.m_y));
  117. }
  118. FLOAT r = cross / d2;
  119. FLOAT px = pt1.m_x + (pt2.m_x - pt1.m_x) * r;
  120. FLOAT py = pt1.m_y + (pt2.m_y - pt1.m_y) * r;
  121. return sqrtf((m_x - px) * (m_x - px) + (py - pt1.m_y) * (py - pt1.m_y));
  122. }
  123. //范围: 0~~π (0~~180)
  124. float AngleBetween(const Vector2D dest)
  125. {
  126. return acos((m_x * dest.m_x + m_y * dest.m_y) / Length() / dest.Length());
  127. }
  128. //范围: 0~~2π (0~~360)
  129. float ToRadiansAngle()
  130. {
  131. float fAngle = acos(m_x / Length());
  132. if (m_y < 0.0f)
  133. {
  134. if (m_x < 0.0f)
  135. {
  136. fAngle += PI / 2;
  137. }
  138. else
  139. {
  140. fAngle += PI;
  141. }
  142. }
  143. return fAngle;
  144. }
  145. //范围: 0~~360
  146. float ToDegreesAngle()
  147. {
  148. return ToRadiansAngle() * RAD_TO_DEG;
  149. }
  150. Vector2D Rotate(Vector2D A, FLOAT radianAngle)
  151. {
  152. return Vector2D(A.m_x * cos(radianAngle) - A.m_y * sin(radianAngle), A.m_x * sin(radianAngle) + A.m_y * cos(radianAngle));
  153. }
  154. void Rotate(FLOAT radianAngle)
  155. {
  156. float tmx = 0, tmy = 0;
  157. tmx = m_x * cos(radianAngle) - m_y * sin(radianAngle);
  158. tmy = m_x * sin(radianAngle) + m_y * cos(radianAngle);
  159. m_x = tmx;
  160. m_y = tmy;
  161. }
  162. BOOL FromString(std::string str)
  163. {
  164. m_x = CommonConvert::StringToFloat(str.substr(0, str.find_first_of(',')).c_str());
  165. m_y = CommonConvert::StringToFloat(str.substr(str.find_last_of(',')).c_str());
  166. return TRUE;
  167. }
  168. };
  169. typedef Vector2D CPoint2D;
  170. class Rect2D
  171. {
  172. public:
  173. FLOAT m_Left, m_Top, m_Bottom, m_Right;
  174. Rect2D(float _left, float _top, float _right, float _bottom)
  175. {
  176. m_Left = _left;
  177. m_Top = _top;
  178. m_Bottom = _bottom;
  179. m_Right = _right;
  180. };
  181. Rect2D()
  182. {
  183. m_Left = 0;
  184. m_Top = 0;
  185. m_Bottom = 0;
  186. m_Right = 0;
  187. };
  188. bool PtInRect(CPoint2D pt)
  189. {
  190. if(pt.m_x >= m_Left && pt.m_y >= m_Top && pt.m_x <= m_Right && pt.m_y <= m_Bottom)
  191. {
  192. return true;
  193. }
  194. return false;
  195. };
  196. };
  197. class Vector3D
  198. {
  199. public:
  200. Vector3D(): m_x(0), m_y(0), m_z(0) {}
  201. Vector3D(float x1, float y1, float z1): m_x(x1), m_y(y1), m_z(z1) {}
  202. Vector3D(const Vector3D& v): m_x(v.m_x), m_y(v.m_y), m_z(v.m_z) {}
  203. ~Vector3D() {};
  204. Vector3D& operator=(const Vector3D& v)
  205. {
  206. m_x = v.m_x;
  207. m_y = v.m_y;
  208. m_z = v.m_z;
  209. return *this;
  210. }
  211. Vector3D operator+(const Vector3D& v)
  212. {
  213. return Vector3D(m_x + v.m_x,
  214. m_y + v.m_y,
  215. m_z + v.m_z);
  216. }
  217. Vector3D operator-(const Vector3D& v)
  218. {
  219. return Vector3D(m_x - v.m_x,
  220. m_y - v.m_y,
  221. m_z - v.m_z);
  222. }
  223. Vector3D operator * (const Vector3D& rhs) const
  224. {
  225. return Vector3D(
  226. m_x * rhs.m_x,
  227. m_y * rhs.m_y,
  228. m_z * rhs.m_z);
  229. }
  230. Vector3D operator / (const Vector3D& rhs) const
  231. {
  232. return Vector3D(
  233. m_x / rhs.m_x,
  234. m_y / rhs.m_y,
  235. m_z / rhs.m_z);
  236. }
  237. Vector3D operator*(const Vector3D& v)
  238. {
  239. return Vector3D(m_x * v.m_x,
  240. m_y * v.m_y,
  241. m_z * v.m_z);
  242. }
  243. Vector3D operator+(float f)
  244. {
  245. return Vector3D(m_x + f,
  246. m_y + f,
  247. m_z + f);
  248. }
  249. Vector3D operator-(float f)
  250. {
  251. return Vector3D(m_x - f,
  252. m_y - f,
  253. m_z - f);
  254. }
  255. Vector3D operator*(float f)
  256. {
  257. return Vector3D(m_x * f,
  258. m_y * f,
  259. m_z * f);
  260. }
  261. Vector3D& operator += (const Vector3D& v)
  262. {
  263. m_x += v.m_x;
  264. m_y += v.m_y;
  265. m_z += v.m_z;
  266. return *this;
  267. }
  268. Vector3D& operator -= (const Vector3D& v)
  269. {
  270. m_x -= v.m_x;
  271. m_y -= v.m_y;
  272. m_z -= v.m_z;
  273. return *this;
  274. }
  275. Vector3D& operator *= (const Vector3D& v)
  276. {
  277. m_x *= v.m_x;
  278. m_y *= v.m_y;
  279. m_z *= v.m_z;
  280. return *this;
  281. }
  282. Vector3D& operator /= (const Vector3D& rhs)
  283. {
  284. m_x /= rhs.m_x;
  285. m_y /= rhs.m_y;
  286. m_z /= rhs.m_z;
  287. return *this;
  288. }
  289. Vector3D& operator += (float f)
  290. {
  291. m_x += f;
  292. m_y += f;
  293. m_z += f;
  294. return *this;
  295. }
  296. Vector3D& operator -= (float f)
  297. {
  298. m_x -= f;
  299. m_y -= f;
  300. m_z -= f;
  301. return *this;
  302. }
  303. Vector3D& operator *= (float f)
  304. {
  305. m_x *= f;
  306. m_y *= f;
  307. m_z *= f;
  308. return *this;
  309. }
  310. bool operator == (const Vector3D& rkVector) const
  311. {
  312. return (m_x == rkVector.m_x && m_y == rkVector.m_y && m_z == rkVector.m_z);
  313. }
  314. bool operator != (const Vector3D& rkVector) const
  315. {
  316. return (m_x != rkVector.m_x || m_y != rkVector.m_y || m_z != rkVector.m_z);
  317. }
  318. //点集
  319. float DotProduct(const Vector3D& v)
  320. {
  321. return m_x * v.m_x +
  322. m_y * v.m_y +
  323. m_z * v.m_z;
  324. }
  325. float Length()
  326. {
  327. return sqrtf(m_x * m_x + m_y * m_y + m_z * m_z);
  328. }
  329. void Reset()
  330. {
  331. m_x = 0.0f;
  332. m_y = 0.0f;
  333. m_z = 0.0f;
  334. }
  335. float SquaredLength() const
  336. {
  337. return m_x * m_x + m_y * m_y + m_z * m_z;
  338. }
  339. float Normalize()
  340. {
  341. float fLength = Length();
  342. if (fLength > 1e-08)
  343. {
  344. float fInvLength = 1.0f / fLength;
  345. m_x *= fInvLength;
  346. m_y *= fInvLength;
  347. m_z *= fInvLength;
  348. }
  349. return fLength;
  350. }
  351. Vector3D CrossProduct(const Vector3D& v)
  352. {
  353. return Vector3D(m_y * v.m_z - m_z * v.m_y,
  354. m_z * v.m_x - m_x * v.m_z,
  355. m_x * v.m_y - m_y * v.m_x);
  356. }
  357. float AngleBetween(Vector3D dest)
  358. {
  359. float lenProduct = Length();
  360. lenProduct *= dest.Length();
  361. if (lenProduct < 1e-6f)
  362. {
  363. lenProduct = 1e-6f;
  364. }
  365. float f = DotProduct(dest) / lenProduct;
  366. if (f > 1.0f)
  367. {
  368. f = 1.0f;
  369. }
  370. else if (f < -1.0f)
  371. {
  372. f = -1.0f;
  373. }
  374. return acosf(f);
  375. }
  376. float Distance2D(Vector3D pos)
  377. {
  378. float dx = m_x - pos.m_x;
  379. float dz = m_z - pos.m_z;
  380. return sqrtf(dx * dx + dz * dz);
  381. }
  382. //范围: 0~~π (0~~180)
  383. float AngleBetween2D(Vector3D& dest)
  384. {
  385. return acosf((m_x * dest.m_x + m_z * dest.m_z) / sqrtf(m_x * m_x + m_z * m_z) / sqrtf(dest.m_x * dest.m_x + dest.m_z * dest.m_z));
  386. }
  387. Vector2D Rotate(Vector2D A, FLOAT radianAngle)
  388. {
  389. return Vector2D(A.m_x * cos(radianAngle) - A.m_y * sin(radianAngle), A.m_x * sin(radianAngle) + A.m_y * cos(radianAngle));
  390. }
  391. //范围: 0~~2π
  392. float ToRadiansAngle()
  393. {
  394. float fAngle = acos(m_x / Length());
  395. if (m_z < 0.0f)
  396. {
  397. if (m_x < 0.0f)
  398. {
  399. fAngle += PI;
  400. }
  401. else
  402. {
  403. fAngle += PI / 2 ;
  404. }
  405. }
  406. return fAngle;
  407. }
  408. //范围: 0~~360
  409. float ToDegreesAngle()
  410. {
  411. return Vector3D::RadiansToDegrees(ToRadiansAngle());
  412. }
  413. BOOL FromString(const char* pStr)
  414. {
  415. return CommonConvert::StringToPos((CHAR*)pStr, m_x, m_y, m_z);
  416. }
  417. static inline float DegreesToRadians(float degrees)
  418. {
  419. return degrees * DEG_TO_RAD;
  420. }
  421. static inline float RadiansToDegrees(float radians)
  422. {
  423. return radians * RAD_TO_DEG;
  424. }
  425. float m_x, m_y, m_z;
  426. };
  427. typedef Vector3D CPoint3D;
  428. #endif //_MAP_H_