Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include "JWmath.h"
  4.  
  5. #define default_point 0
  6.  
  7. struct JWVector
  8. {
  9. public:
  10. // Vector's X copmponent.
  11. float X;
  12.  
  13. // Vector's Y copmponent.
  14. float Y;
  15.  
  16. // Vector's Z copmponent.
  17. float Z;
  18.  
  19. public:
  20. static const JWVector ZeroVector;
  21. static const JWVector OneVector;
  22. static const JWVector UpVector;
  23. static const JWVector DownVector;
  24. static const JWVector LeftVector;
  25. static const JWVector RightVector;
  26. static const JWVector ForwardVector;
  27. static const JWVector BackVector;
  28.  
  29. public:
  30. JWVector() { X = 0.0f; Y = 0.0f; Z = 0.0f; }
  31. JWVector(const float &InX, const float &InY) : X(InX), Y(InY), Z(default_point) {}
  32. JWVector(const float &inX, const float &inY, const float &inZ) : X(inX), Y(inY), Z(inZ) {}
  33.  
  34. inline static float DotProduct(const JWVector &a, const JWVector &b);
  35. inline float operator|(const JWVector& b) const;
  36. inline static JWVector CrossProduct(const JWVector &a, const JWVector &b);
  37. inline JWVector operator^(const JWVector& b) const;
  38.  
  39. inline JWVector operator+(const JWVector& b) const;
  40. inline JWVector operator+= (const JWVector& b);
  41. inline JWVector operator+(const float& bias) const;
  42.  
  43. inline JWVector operator-(const JWVector& b) const;
  44. inline JWVector operator-=(const JWVector& b);
  45. inline JWVector operator-() const;
  46. inline JWVector operator-(const float& bias) const;
  47.  
  48. inline JWVector operator*(const JWVector& b) const;
  49. inline JWVector operator*=(const JWVector& b);
  50. inline JWVector operator*(const float& Scale) const;
  51. inline JWVector operator*=(const float& Scale);
  52.  
  53. inline JWVector operator/(const JWVector& b) const;
  54. inline JWVector operator/=(const JWVector& b);
  55. inline JWVector operator/(const float& Scale) const;
  56. inline JWVector operator/=(const float& Scale);
  57.  
  58. inline static float Dist(const JWVector &a, const JWVector &b);
  59. inline static float Distance(const JWVector &a, const JWVector &b) { return Dist(a, b); }
  60. inline static float DistSquared(const JWVector&a, const JWVector&b);
  61.  
  62. inline float SizeSquared() const;
  63. inline float Size() const;
  64.  
  65. inline bool Equals(const JWVector& b) const;
  66. inline bool IsZero() const;
  67. inline bool operator==(const JWVector& b) const;
  68. inline bool operator!=(const JWVector& b) const;
  69. inline float& operator[](const int &index);
  70. inline float operator[](const int &index) const;
  71.  
  72. inline bool Normalize(float Tolerance = SMALL_NUMBER);
  73. inline bool IsNormalized() const;
  74. inline JWVector GetUnsafeNormal() const;
  75. inline JWVector GetSafeNormal(float Tolerance = SMALL_NUMBER) const;
  76. inline JWVector Reciprocal() const;
  77. inline bool AllComponentEqual(float Tolerance = KINDA_SMALL_NUMBER) const;
  78.  
  79. };
  80.  
  81. const JWVector JWVector::ZeroVector(0.f, 0.f, 0.f);
  82. const JWVector JWVector::OneVector(1.f, 1.f, 1.f);
  83. const JWVector JWVector::UpVector(0.f, 0.f, 1.f);
  84. const JWVector JWVector::DownVector(0.f, 0.f, -1.f);
  85. const JWVector JWVector::LeftVector(0.f, -1.f, 0.f);
  86. const JWVector JWVector::RightVector(0.f, 1.f, 0.f);
  87. const JWVector JWVector::ForwardVector(1.f, 0.f, 0.f);
  88. const JWVector JWVector::BackVector(-1.f, 0.f, 0.f);
  89.  
  90. inline float JWVector::operator|(const JWVector & b) const
  91. {
  92. return
  93. (X * b.X) +
  94. (Y * b.Y) +
  95. (Z * b.Z);
  96. }
  97.  
  98. inline JWVector JWVector::operator^(const JWVector & b) const
  99. {
  100. return JWVector(
  101. (Y * b.Z) - (Z * b.Y),
  102. (Z * b.X) - (X * b.Z),
  103. (X * b.Y) - (Y * b.X)
  104. );
  105. }
  106.  
  107. inline float JWVector::DotProduct(const JWVector & a, const JWVector & b)
  108. {
  109. return a | b;
  110. }
  111.  
  112. inline JWVector JWVector::CrossProduct(const JWVector & a, const JWVector & b)
  113. {
  114. return a ^ b;
  115. }
  116.  
  117. inline float JWVector::Dist(const JWVector & a, const JWVector & b)
  118. {
  119. return Math::Sqrt(JWVector::DistSquared(a, b));
  120. }
  121.  
  122. inline float JWVector::DistSquared(const JWVector & a, const JWVector & b)
  123. {
  124. return Math::Square(b.X - a.X) + Math::Square(b.Y - a.Y) + Math::Square(b.Z - a.Z);
  125. }
  126.  
  127. inline JWVector JWVector::operator+(const JWVector & b) const
  128. {
  129. return JWVector(X + b.X, Y + b.Y, Z + b.Z);
  130. }
  131.  
  132. inline JWVector JWVector::operator+(const float &bias) const
  133. {
  134. return JWVector(X + bias, Y + bias, Z + bias);
  135. }
  136.  
  137. inline JWVector JWVector::operator-(const JWVector & b) const
  138. {
  139. return JWVector(X - b.X, Y - b.Y, Z - b.Z);
  140. }
  141.  
  142. inline JWVector JWVector::operator-(const float &bias) const
  143. {
  144. return JWVector(X - bias, Y - bias, Z - bias);
  145. }
  146.  
  147. inline JWVector JWVector::operator-() const
  148. {
  149. return JWVector(-X, -Y, -Z);
  150. }
  151.  
  152. inline JWVector JWVector::operator*(const JWVector & V) const
  153. {
  154. return JWVector(X * V.X, Y * V.Y, Z * V.Z);
  155. }
  156.  
  157. inline JWVector JWVector::operator*(const float &Scale) const
  158. {
  159. return JWVector(X * Scale, Y * Scale, Z * Scale);
  160. }
  161.  
  162.  
  163. inline JWVector JWVector::operator/(const JWVector & V) const
  164. {
  165. return JWVector(X / V.X, Y / V.Y, Z / V.Z);
  166. }
  167.  
  168. inline JWVector JWVector::operator/(const float & Scale) const
  169. {
  170. const float RScale = 1.f / Scale;
  171. return JWVector(X * RScale, Y * RScale, Z * RScale);
  172. }
  173.  
  174. inline bool JWVector::operator==(const JWVector & b) const
  175. {
  176. return X == b.X && Y == b.Y && Z == b.Z;
  177. }
  178.  
  179. inline bool JWVector::operator!=(const JWVector & b) const
  180. {
  181. return X != b.X || Y != b.Y || Z != b.Z;
  182. }
  183.  
  184. inline JWVector JWVector::operator+=(const JWVector & b)
  185. {
  186. X += b.X; Y += b.Y; Z += b.Z;
  187. return *this;
  188. }
  189.  
  190. inline JWVector JWVector::operator-=(const JWVector & b)
  191. {
  192. X -= b.X; Y -= b.Y; Z -= b.Z;
  193. return *this;
  194. }
  195.  
  196. inline JWVector JWVector::operator*=(const JWVector & b)
  197. {
  198. X *= b.X; Y *= b.Y; Z *= b.Z;
  199. return *this;
  200. }
  201.  
  202.  
  203. inline JWVector JWVector::operator*=(const float & Scale)
  204. {
  205. X *= Scale; Y *= Scale; Z *= Scale;
  206. return *this;
  207. }
  208.  
  209. inline JWVector JWVector::operator/=(const JWVector& b)
  210. {
  211. X /= b.X; Y /= b.Y; Z /= b.Z;
  212. return *this;
  213. }
  214.  
  215. inline JWVector JWVector::operator/=(const float & Scale)
  216. {
  217. const float RScale = 1.f / Scale;
  218. X *= RScale; Y *= RScale; Z *= RScale;
  219. return *this;
  220. }
  221.  
  222. inline bool JWVector::Equals(const JWVector & b) const
  223. {
  224. return X == b.X && Y == b.Y && Z == b.Z;
  225. }
  226.  
  227. inline bool JWVector::AllComponentEqual(float Tolerance) const
  228. {
  229. return Math::Abs(X - Y) <= Tolerance && Math::Abs(X - Z) <= Tolerance && Math::Abs(Y - Z) <= Tolerance;
  230. }
  231.  
  232. inline bool JWVector::IsZero() const
  233. {
  234. return X == 0.f && Y == 0.f && Z == 0.f;
  235. }
  236.  
  237. inline float JWVector::Size() const
  238. {
  239. return Math::Sqrt(SizeSquared());
  240. }
  241.  
  242. inline float JWVector::SizeSquared() const
  243. {
  244. return (X * X) + (Y * Y) + (Z * Z);
  245. }
  246.  
  247. inline JWVector JWVector::GetUnsafeNormal() const
  248. {
  249. const float Scale = Math::InvSqrt((X*X) + (Y*Y) + (Z*Z));
  250. return JWVector(X*Scale, Y*Scale, Z*Scale);
  251. }
  252.  
  253. inline JWVector JWVector::GetSafeNormal(float Tolerance) const
  254. {
  255. const float SquareSum = (X*X) + (Y*Y) + (Z*Z);
  256.  
  257. if (SquareSum == 1.f)
  258. {
  259. return *this;
  260. }
  261. else if (SquareSum < Tolerance)
  262. {
  263. return JWVector::ZeroVector;
  264. }
  265. const float Scale = Math::InvSqrt(SquareSum);
  266. return JWVector(X*Scale, Y*Scale, Z*Scale);
  267. }
  268.  
  269. inline JWVector JWVector::Reciprocal() const
  270. {
  271. JWVector RecVector;
  272.  
  273. if (X != 0.f)
  274. RecVector.X = 1.f / X;
  275. else
  276. RecVector.X = BIG_NUMBER;
  277.  
  278. if (Y != 0.f)
  279. RecVector.Y = 1.f / Y;
  280. else
  281. RecVector.Y = BIG_NUMBER;
  282.  
  283. if (Z != 0.f)
  284. RecVector.Z = 1.f / Z;
  285. else
  286. RecVector.Z = BIG_NUMBER;
  287.  
  288. return RecVector;
  289. }
  290.  
  291. bool JWVector::Normalize(float Tolerance)
  292. {
  293. const float SquareSum = X * X + Y * Y + Z * Z;
  294. if (SquareSum > Tolerance)
  295. {
  296. const float Scale = Math::InvSqrt(SquareSum);
  297. X *= Scale; Y *= Scale; Z *= Scale;
  298. return true;
  299. }
  300. return false;
  301. }
  302.  
  303. bool JWVector::IsNormalized() const
  304. {
  305. return (Math::Abs(1.f - SizeSquared()) < THRESH_VECTOR_NORMALIZED);
  306. }
  307.  
  308. float& JWVector::operator[](const int & Index)
  309. {
  310. if(Index >= 0 && Index < 3)
  311. throw std::out_of_range("Array");
  312.  
  313. else {
  314. if (Index == 0) return X;
  315.  
  316. else if (Index == 1) return Y;
  317.  
  318. else return Z;
  319. }
  320. }
  321.  
  322. float JWVector::operator[](const int & Index)const
  323. {
  324. if (Index < 0 || Index >= 3)
  325. throw std::out_of_range("Array");
  326.  
  327. else
  328. if (Index == 0) return X;
  329.  
  330. else if (Index == 1) return Y;
  331.  
  332. else return Z;
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement