Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. #ifndef _VECTORZ_H
  2. #define _VECTORZ_H
  3.  
  4. #define CHECK_VALID( _v ) 0
  5. #define Assert( _exp ) ((void)0)
  6.  
  7. #include <Windows.h>
  8. #include <math.h>
  9.  
  10. // from the own leaked sdk
  11.  
  12. #define FORCEINLINE __forceinline
  13.  
  14. typedef float vec_t;
  15.  
  16. inline vec_t BitsToFloat(unsigned long i)
  17. {
  18. return *reinterpret_cast<vec_t*>(&i);
  19. }
  20.  
  21. #define FLOAT32_NAN_BITS (unsigned long)0x7FC00000
  22. #define FLOAT32_NAN BitsToFloat( FLOAT32_NAN_BITS )
  23.  
  24. #define VEC_T_NAN FLOAT32_NAN
  25.  
  26. class Vector
  27. {
  28. public:
  29. vec_t x, y, z;
  30.  
  31. Vector(void);
  32. Vector(vec_t X, vec_t Y, vec_t Z);
  33. explicit Vector(vec_t XYZ);
  34.  
  35. void Init(vec_t ix = 0.0f, vec_t iy = 0.0f, vec_t iz = 0.0f);
  36.  
  37. bool IsValid() const;
  38. void Invalidate();
  39.  
  40. vec_t operator[](int i) const;
  41. vec_t& operator[](int i);
  42.  
  43. vec_t* Base();
  44. vec_t const* Base() const;
  45.  
  46. inline void Zero();
  47.  
  48. bool operator==(const Vector& v) const;
  49. bool operator!=(const Vector& v) const;
  50.  
  51. FORCEINLINE Vector& operator+=(const Vector& v);
  52. FORCEINLINE Vector& operator-=(const Vector& v);
  53. FORCEINLINE Vector& operator*=(const Vector& v);
  54. FORCEINLINE Vector& operator*=(float s);
  55. FORCEINLINE Vector& operator/=(const Vector& v);
  56. FORCEINLINE Vector& operator/=(float s);
  57. FORCEINLINE Vector& operator+=(float fl);
  58. FORCEINLINE Vector& operator-=(float fl);
  59.  
  60. void Negate();
  61.  
  62. inline vec_t Length() const;
  63.  
  64. FORCEINLINE vec_t LengthSqr(void) const
  65. {
  66. return (x * x + y * y + z * z);
  67. }
  68.  
  69. bool IsZero(float tolerance = 0.01f) const
  70. {
  71. return (x > -tolerance && x < tolerance &&
  72. y > -tolerance && y < tolerance &&
  73. z > -tolerance && z < tolerance);
  74. }
  75.  
  76. vec_t NormalizeInPlace();
  77. Vector Normalized() const;
  78.  
  79. bool IsLengthGreaterThan(float val) const;
  80. bool IsLengthLessThan(float val) const;
  81.  
  82. FORCEINLINE bool WithinAABox(Vector const &boxmin, Vector const &boxmax);
  83.  
  84. vec_t DistTo(const Vector& vOther) const;
  85.  
  86. FORCEINLINE vec_t DistToSqr(const Vector& vOther) const
  87. {
  88. Vector delta;
  89.  
  90. delta.x = x - vOther.x;
  91. delta.y = y - vOther.y;
  92. delta.z = z - vOther.z;
  93.  
  94. return delta.LengthSqr();
  95. }
  96.  
  97. void CopyToArray(float* rgfl) const;
  98. void MulAdd(const Vector& a, const Vector& b, float scalar);
  99.  
  100. vec_t Dot(const Vector& vOther) const;
  101.  
  102. Vector& operator=(const Vector& vOther);
  103.  
  104. vec_t Length2D(void) const;
  105. vec_t Length2DSqr(void) const;
  106.  
  107. Vector operator-(void) const;
  108. Vector operator+(const Vector& v) const;
  109. Vector operator-(const Vector& v) const;
  110. Vector operator*(const Vector& v) const;
  111. Vector operator/(const Vector& v) const;
  112. Vector operator*(float fl) const;
  113. Vector operator/(float fl) const;
  114.  
  115. Vector Cross(const Vector& vOther) const;
  116.  
  117. Vector Min(const Vector& vOther) const;
  118. Vector Max(const Vector& vOther) const;
  119. };
  120.  
  121. FORCEINLINE Vector ReplicateToVector(float x)
  122. {
  123. return Vector(x, x, x);
  124. }
  125.  
  126. inline Vector::Vector(void)
  127. {
  128.  
  129. }
  130.  
  131.  
  132.  
  133. inline Vector::Vector(vec_t X, vec_t Y, vec_t Z)
  134. {
  135. x = X; y = Y; z = Z;
  136. }
  137.  
  138. inline Vector::Vector(vec_t XYZ)
  139. {
  140. x = y = z = XYZ;
  141. }
  142.  
  143. inline void Vector::Init(vec_t ix, vec_t iy, vec_t iz)
  144. {
  145. x = ix; y = iy; z = iz;
  146. }
  147.  
  148. inline void Vector::Zero()
  149. {
  150. x = y = z = 0.0f;
  151. }
  152.  
  153. inline void VectorClear(Vector& a)
  154. {
  155. a.x = a.y = a.z = 0.0f;
  156. }
  157.  
  158. inline Vector& Vector::operator=(const Vector& vOther)
  159. {
  160. x = vOther.x; y = vOther.y; z = vOther.z;
  161. return *this;
  162. }
  163.  
  164. inline vec_t& Vector::operator[](int i)
  165. {
  166. return ((vec_t*)this)[i];
  167. }
  168.  
  169. inline vec_t Vector::operator[](int i) const
  170. {
  171. return ((vec_t*)this)[i];
  172. }
  173.  
  174.  
  175.  
  176. inline vec_t* Vector::Base()
  177. {
  178. return (vec_t*)this;
  179. }
  180.  
  181. inline vec_t const* Vector::Base() const
  182. {
  183. return (vec_t const*)this;
  184. }
  185.  
  186. inline bool Vector::IsValid() const
  187. {
  188. return (x == x && y == y && z == z);
  189. }
  190.  
  191. inline void Vector::Invalidate()
  192. {
  193. x = y = z = VEC_T_NAN;
  194. }
  195.  
  196. inline bool Vector::operator==(const Vector& src) const
  197. {
  198. return (src.x == x) && (src.y == y) && (src.z == z);
  199. }
  200.  
  201. inline bool Vector::operator!=(const Vector& src) const
  202. {
  203. return (src.x != x) || (src.y != y) || (src.z != z);
  204. }
  205.  
  206. FORCEINLINE void VectorCopy(const Vector& src, Vector& dst)
  207. {
  208. dst.x = src.x;
  209. dst.y = src.y;
  210. dst.z = src.z;
  211. }
  212.  
  213. inline void Vector::CopyToArray(float* rgfl) const
  214. {
  215. rgfl[0] = x; rgfl[1] = y; rgfl[2] = z;
  216. }
  217.  
  218. inline void Vector::Negate()
  219. {
  220. x = -x; y = -y; z = -z;
  221. }
  222.  
  223. FORCEINLINE Vector& Vector::operator+=(const Vector& v)
  224. {
  225. x += v.x; y += v.y; z += v.z;
  226. return *this;
  227. }
  228.  
  229. FORCEINLINE Vector& Vector::operator-=(const Vector& v)
  230. {
  231. x -= v.x; y -= v.y; z -= v.z;
  232. return *this;
  233. }
  234.  
  235. FORCEINLINE Vector& Vector::operator*=(float fl)
  236. {
  237. x *= fl;
  238. y *= fl;
  239. z *= fl;
  240. return *this;
  241. }
  242.  
  243. FORCEINLINE Vector& Vector::operator*=(const Vector& v)
  244. {
  245. x *= v.x;
  246. y *= v.y;
  247. z *= v.z;
  248. return *this;
  249. }
  250.  
  251. FORCEINLINE Vector& Vector::operator+=(float fl)
  252. {
  253. x += fl;
  254. y += fl;
  255. z += fl;
  256. return *this;
  257. }
  258.  
  259. FORCEINLINE Vector& Vector::operator-=(float fl)
  260. {
  261. x -= fl;
  262. y -= fl;
  263. z -= fl;
  264. return *this;
  265. }
  266.  
  267. FORCEINLINE Vector& Vector::operator/=(float fl)
  268. {
  269. float oofl = 1.0f / fl;
  270. x *= oofl;
  271. y *= oofl;
  272. z *= oofl;
  273. return *this;
  274. }
  275.  
  276. FORCEINLINE Vector& Vector::operator/=(const Vector& v)
  277. {
  278. x /= v.x;
  279. y /= v.y;
  280. z /= v.z;
  281. return *this;
  282. }
  283.  
  284. FORCEINLINE void VectorAdd(const Vector& a, const Vector& b, Vector& c)
  285. {
  286. c.x = a.x + b.x;
  287. c.y = a.y + b.y;
  288. c.z = a.z + b.z;
  289. }
  290.  
  291. FORCEINLINE void VectorSubtract(const Vector& a, const Vector& b, Vector& c)
  292. {
  293. c.x = a.x - b.x;
  294. c.y = a.y - b.y;
  295. c.z = a.z - b.z;
  296. }
  297.  
  298. FORCEINLINE void VectorMultiply(const Vector& a, vec_t b, Vector& c)
  299. {
  300. c.x = a.x * b;
  301. c.y = a.y * b;
  302. c.z = a.z * b;
  303. }
  304.  
  305. FORCEINLINE void VectorMultiply(const Vector& a, const Vector& b, Vector& c)
  306. {
  307. c.x = a.x * b.x;
  308. c.y = a.y * b.y;
  309. c.z = a.z * b.z;
  310. }
  311.  
  312. inline void VectorScale(const Vector& in, vec_t scale, Vector& result)
  313. {
  314. VectorMultiply(in, scale, result);
  315. }
  316.  
  317. FORCEINLINE void VectorDivide(const Vector& a, vec_t b, Vector& c)
  318. {
  319. vec_t oob = 1.0f / b;
  320. c.x = a.x * oob;
  321. c.y = a.y * oob;
  322. c.z = a.z * oob;
  323. }
  324.  
  325. FORCEINLINE void VectorDivide(const Vector& a, const Vector& b, Vector& c)
  326. {
  327. c.x = a.x / b.x;
  328. c.y = a.y / b.y;
  329. c.z = a.z / b.z;
  330. }
  331.  
  332. inline void Vector::MulAdd(const Vector& a, const Vector& b, float scalar)
  333. {
  334. x = a.x + b.x * scalar;
  335. y = a.y + b.y * scalar;
  336. z = a.z + b.z * scalar;
  337. }
  338.  
  339. inline void VectorLerp(const Vector& src1, const Vector& src2, vec_t t, Vector& dest)
  340. {
  341. dest.x = src1.x + (src2.x - src1.x) * t;
  342. dest.y = src1.y + (src2.y - src1.y) * t;
  343. dest.z = src1.z + (src2.z - src1.z) * t;
  344. }
  345.  
  346. FORCEINLINE vec_t DotProduct(const Vector& a, const Vector& b)
  347. {
  348. return (a.x * b.x + a.y * b.y + a.z * b.z);
  349. }
  350.  
  351. inline vec_t Vector::Dot(const Vector& vOther) const
  352. {
  353. return DotProduct(*this, vOther);
  354. }
  355.  
  356. inline void CrossProduct(const Vector& a, const Vector& b, Vector& result)
  357. {
  358. result.x = a.y * b.z - a.z * b.y;
  359. result.y = a.z * b.x - a.x * b.z;
  360. result.z = a.x * b.y - a.y * b.x;
  361. }
  362.  
  363. //inline vec_t DotProductAbs(const Vector& v0, const Vector& v1)
  364. //{
  365. // return abs(v0.x * v1.x) + abs(v0.y * v1.y) + abs(v0.z * v1.z);
  366. //}
  367.  
  368. inline vec_t VectorLength(const Vector& v)
  369. {
  370. return (vec_t)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  371. }
  372.  
  373. inline vec_t Vector::Length(void) const
  374. {
  375. return VectorLength(*this);
  376. }
  377.  
  378. inline vec_t VectorNormalize(Vector& v)
  379. {
  380. vec_t l = v.Length();
  381.  
  382. if (l != 0.0f)
  383. {
  384. v /= l;
  385. }
  386. else
  387. {
  388. v.x = v.y = 0.0f; v.z = 1.0f;
  389. }
  390.  
  391. return l;
  392. }
  393.  
  394. FORCEINLINE float VectorNormalizer(float * v)
  395. {
  396. return VectorNormalize(*(reinterpret_cast<Vector *>(v)));
  397. }
  398. inline vec_t Vector::NormalizeInPlace()
  399. {
  400. return VectorNormalize(*this);
  401. }
  402.  
  403. bool Vector::WithinAABox(Vector const &boxmin, Vector const &boxmax)
  404. {
  405. return (
  406. (x >= boxmin.x) && (x <= boxmax.x) &&
  407. (y >= boxmin.y) && (y <= boxmax.y) &&
  408. (z >= boxmin.z) && (z <= boxmax.z)
  409. );
  410. }
  411.  
  412. inline vec_t Vector::DistTo(const Vector& vOther) const
  413. {
  414. Vector delta;
  415. VectorSubtract(*this, vOther, delta);
  416. return delta.Length();
  417. }
  418.  
  419. inline Vector Vector::Min(const Vector& vOther) const
  420. {
  421. return Vector(x < vOther.x ? x : vOther.x,
  422. y < vOther.y ? y : vOther.y,
  423. z < vOther.z ? z : vOther.z);
  424. }
  425.  
  426. inline Vector Vector::Max(const Vector& vOther) const
  427. {
  428. return Vector(x > vOther.x ? x : vOther.x,
  429. y > vOther.y ? y : vOther.y,
  430. z > vOther.z ? z : vOther.z);
  431. }
  432.  
  433. inline Vector Vector::operator-(void) const
  434. {
  435. return Vector(-x, -y, -z);
  436. }
  437.  
  438. inline Vector Vector::operator+(const Vector& v) const
  439. {
  440. Vector res;
  441. VectorAdd(*this, v, res);
  442. return res;
  443. }
  444.  
  445. inline Vector Vector::operator-(const Vector& v) const
  446. {
  447. Vector res;
  448. VectorSubtract(*this, v, res);
  449. return res;
  450. }
  451.  
  452. inline Vector Vector::operator*(float fl) const
  453. {
  454. Vector res;
  455. VectorMultiply(*this, fl, res);
  456. return res;
  457. }
  458.  
  459. inline Vector Vector::operator*(const Vector& v) const
  460. {
  461. Vector res;
  462. VectorMultiply(*this, v, res);
  463. return res;
  464. }
  465.  
  466. inline Vector Vector::operator/(float fl) const
  467. {
  468. Vector res;
  469. VectorDivide(*this, fl, res);
  470. return res;
  471. }
  472.  
  473. inline Vector Vector::operator/(const Vector& v) const
  474. {
  475. Vector res;
  476. VectorDivide(*this, v, res);
  477. return res;
  478. }
  479.  
  480. inline Vector operator*(float fl, const Vector& v)
  481. {
  482. return v * fl;
  483. }
  484.  
  485. inline Vector Vector::Cross(const Vector& vOther) const
  486. {
  487. Vector res;
  488. CrossProduct(*this, vOther, res);
  489. return res;
  490. }
  491.  
  492. inline vec_t Vector::Length2D(void) const
  493. {
  494. return (vec_t)::sqrtf(x * x + y * y);
  495. }
  496.  
  497. inline vec_t Vector::Length2DSqr(void) const
  498. {
  499. return (x * x + y * y);
  500. }
  501.  
  502. inline Vector CrossProduct(const Vector& a, const Vector& b)
  503. {
  504. return Vector(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  505. }
  506.  
  507. inline void VectorMin(const Vector& a, const Vector& b, Vector& result)
  508. {
  509. result.x = min(a.x, b.x);
  510. result.y = min(a.y, b.y);
  511. result.z = min(a.z, b.z);
  512. }
  513.  
  514. inline void VectorMax(const Vector& a, const Vector& b, Vector& result)
  515. {
  516. result.x = max(a.x, b.x);
  517. result.y = max(a.y, b.y);
  518. result.z = max(a.z, b.z);
  519. }
  520.  
  521.  
  522. class VectorAligned : public Vector
  523. {
  524. public:
  525. VectorAligned()
  526. {
  527. x = y = z = 0.0f;
  528. }
  529.  
  530. VectorAligned(const Vector& v)
  531. {
  532. x = v.x; y = v.y; z = v.z;
  533. }
  534.  
  535. float w;
  536. };
  537.  
  538.  
  539.  
  540. #endif // VECTOR_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement