PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/v2math.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 435 lines | 313 code | 80 blank | 42 comment | 14 complexity | c87aeae4c8150f31619f0dfa5cbca6a9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v2math.h
  3. * @brief LLVector2 class header file.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_V2MATH_H
  27. #define LL_V2MATH_H
  28. #include "llmath.h"
  29. #include "v3math.h"
  30. class LLVector4;
  31. class LLMatrix3;
  32. class LLQuaternion;
  33. // Llvector2 = |x y z w|
  34. static const U32 LENGTHOFVECTOR2 = 2;
  35. class LLVector2
  36. {
  37. public:
  38. F32 mV[LENGTHOFVECTOR2];
  39. static LLVector2 zero;
  40. LLVector2(); // Initializes LLVector2 to (0, 0)
  41. LLVector2(F32 x, F32 y); // Initializes LLVector2 to (x. y)
  42. LLVector2(const F32 *vec); // Initializes LLVector2 to (vec[0]. vec[1])
  43. explicit LLVector2(const LLVector3 &vec); // Initializes LLVector2 to (vec[0]. vec[1])
  44. // Clears LLVector2 to (0, 0). DEPRECATED - prefer zeroVec.
  45. void clear();
  46. void setZero();
  47. void clearVec(); // deprecated
  48. void zeroVec(); // deprecated
  49. void set(F32 x, F32 y); // Sets LLVector2 to (x, y)
  50. void set(const LLVector2 &vec); // Sets LLVector2 to vec
  51. void set(const F32 *vec); // Sets LLVector2 to vec
  52. LLSD getValue() const;
  53. void setValue(LLSD& sd);
  54. void setVec(F32 x, F32 y); // deprecated
  55. void setVec(const LLVector2 &vec); // deprecated
  56. void setVec(const F32 *vec); // deprecated
  57. inline bool isFinite() const; // checks to see if all values of LLVector2 are finite
  58. F32 length() const; // Returns magnitude of LLVector2
  59. F32 lengthSquared() const; // Returns magnitude squared of LLVector2
  60. F32 normalize(); // Normalizes and returns the magnitude of LLVector2
  61. F32 magVec() const; // deprecated
  62. F32 magVecSquared() const; // deprecated
  63. F32 normVec(); // deprecated
  64. BOOL abs(); // sets all values to absolute value of original value (first octant), returns TRUE if changed
  65. const LLVector2& scaleVec(const LLVector2& vec); // scales per component by vec
  66. BOOL isNull(); // Returns TRUE if vector has a _very_small_ length
  67. BOOL isExactlyZero() const { return !mV[VX] && !mV[VY]; }
  68. F32 operator[](int idx) const { return mV[idx]; }
  69. F32 &operator[](int idx) { return mV[idx]; }
  70. friend bool operator<(const LLVector2 &a, const LLVector2 &b); // For sorting. x is "more significant" than y
  71. friend LLVector2 operator+(const LLVector2 &a, const LLVector2 &b); // Return vector a + b
  72. friend LLVector2 operator-(const LLVector2 &a, const LLVector2 &b); // Return vector a minus b
  73. friend F32 operator*(const LLVector2 &a, const LLVector2 &b); // Return a dot b
  74. friend LLVector2 operator%(const LLVector2 &a, const LLVector2 &b); // Return a cross b
  75. friend LLVector2 operator/(const LLVector2 &a, F32 k); // Return a divided by scaler k
  76. friend LLVector2 operator*(const LLVector2 &a, F32 k); // Return a times scaler k
  77. friend LLVector2 operator*(F32 k, const LLVector2 &a); // Return a times scaler k
  78. friend bool operator==(const LLVector2 &a, const LLVector2 &b); // Return a == b
  79. friend bool operator!=(const LLVector2 &a, const LLVector2 &b); // Return a != b
  80. friend const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b); // Return vector a + b
  81. friend const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b); // Return vector a minus b
  82. friend const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b); // Return a cross b
  83. friend const LLVector2& operator*=(LLVector2 &a, F32 k); // Return a times scaler k
  84. friend const LLVector2& operator/=(LLVector2 &a, F32 k); // Return a divided by scaler k
  85. friend LLVector2 operator-(const LLVector2 &a); // Return vector -a
  86. friend std::ostream& operator<<(std::ostream& s, const LLVector2 &a); // Stream a
  87. };
  88. // Non-member functions
  89. F32 angle_between(const LLVector2 &a, const LLVector2 &b); // Returns angle (radians) between a and b
  90. BOOL are_parallel(const LLVector2 &a, const LLVector2 &b, F32 epsilon=F_APPROXIMATELY_ZERO); // Returns TRUE if a and b are very close to parallel
  91. F32 dist_vec(const LLVector2 &a, const LLVector2 &b); // Returns distance between a and b
  92. F32 dist_vec_squared(const LLVector2 &a, const LLVector2 &b);// Returns distance squared between a and b
  93. F32 dist_vec_squared2D(const LLVector2 &a, const LLVector2 &b);// Returns distance squared between a and b ignoring Z component
  94. LLVector2 lerp(const LLVector2 &a, const LLVector2 &b, F32 u); // Returns a vector that is a linear interpolation between a and b
  95. // Constructors
  96. inline LLVector2::LLVector2(void)
  97. {
  98. mV[VX] = 0.f;
  99. mV[VY] = 0.f;
  100. }
  101. inline LLVector2::LLVector2(F32 x, F32 y)
  102. {
  103. mV[VX] = x;
  104. mV[VY] = y;
  105. }
  106. inline LLVector2::LLVector2(const F32 *vec)
  107. {
  108. mV[VX] = vec[VX];
  109. mV[VY] = vec[VY];
  110. }
  111. inline LLVector2::LLVector2(const LLVector3 &vec)
  112. {
  113. mV[VX] = vec.mV[VX];
  114. mV[VY] = vec.mV[VY];
  115. }
  116. // Clear and Assignment Functions
  117. inline void LLVector2::clear(void)
  118. {
  119. mV[VX] = 0.f;
  120. mV[VY] = 0.f;
  121. }
  122. inline void LLVector2::setZero(void)
  123. {
  124. mV[VX] = 0.f;
  125. mV[VY] = 0.f;
  126. }
  127. // deprecated
  128. inline void LLVector2::clearVec(void)
  129. {
  130. mV[VX] = 0.f;
  131. mV[VY] = 0.f;
  132. }
  133. // deprecated
  134. inline void LLVector2::zeroVec(void)
  135. {
  136. mV[VX] = 0.f;
  137. mV[VY] = 0.f;
  138. }
  139. inline void LLVector2::set(F32 x, F32 y)
  140. {
  141. mV[VX] = x;
  142. mV[VY] = y;
  143. }
  144. inline void LLVector2::set(const LLVector2 &vec)
  145. {
  146. mV[VX] = vec.mV[VX];
  147. mV[VY] = vec.mV[VY];
  148. }
  149. inline void LLVector2::set(const F32 *vec)
  150. {
  151. mV[VX] = vec[VX];
  152. mV[VY] = vec[VY];
  153. }
  154. // deprecated
  155. inline void LLVector2::setVec(F32 x, F32 y)
  156. {
  157. mV[VX] = x;
  158. mV[VY] = y;
  159. }
  160. // deprecated
  161. inline void LLVector2::setVec(const LLVector2 &vec)
  162. {
  163. mV[VX] = vec.mV[VX];
  164. mV[VY] = vec.mV[VY];
  165. }
  166. // deprecated
  167. inline void LLVector2::setVec(const F32 *vec)
  168. {
  169. mV[VX] = vec[VX];
  170. mV[VY] = vec[VY];
  171. }
  172. // LLVector2 Magnitude and Normalization Functions
  173. inline F32 LLVector2::length(void) const
  174. {
  175. return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
  176. }
  177. inline F32 LLVector2::lengthSquared(void) const
  178. {
  179. return mV[0]*mV[0] + mV[1]*mV[1];
  180. }
  181. inline F32 LLVector2::normalize(void)
  182. {
  183. F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
  184. F32 oomag;
  185. if (mag > FP_MAG_THRESHOLD)
  186. {
  187. oomag = 1.f/mag;
  188. mV[0] *= oomag;
  189. mV[1] *= oomag;
  190. }
  191. else
  192. {
  193. mV[0] = 0.f;
  194. mV[1] = 0.f;
  195. mag = 0;
  196. }
  197. return (mag);
  198. }
  199. // checker
  200. inline bool LLVector2::isFinite() const
  201. {
  202. return (llfinite(mV[VX]) && llfinite(mV[VY]));
  203. }
  204. // deprecated
  205. inline F32 LLVector2::magVec(void) const
  206. {
  207. return (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
  208. }
  209. // deprecated
  210. inline F32 LLVector2::magVecSquared(void) const
  211. {
  212. return mV[0]*mV[0] + mV[1]*mV[1];
  213. }
  214. // deprecated
  215. inline F32 LLVector2::normVec(void)
  216. {
  217. F32 mag = (F32) sqrt(mV[0]*mV[0] + mV[1]*mV[1]);
  218. F32 oomag;
  219. if (mag > FP_MAG_THRESHOLD)
  220. {
  221. oomag = 1.f/mag;
  222. mV[0] *= oomag;
  223. mV[1] *= oomag;
  224. }
  225. else
  226. {
  227. mV[0] = 0.f;
  228. mV[1] = 0.f;
  229. mag = 0;
  230. }
  231. return (mag);
  232. }
  233. inline const LLVector2& LLVector2::scaleVec(const LLVector2& vec)
  234. {
  235. mV[VX] *= vec.mV[VX];
  236. mV[VY] *= vec.mV[VY];
  237. return *this;
  238. }
  239. inline BOOL LLVector2::isNull()
  240. {
  241. if ( F_APPROXIMATELY_ZERO > mV[VX]*mV[VX] + mV[VY]*mV[VY] )
  242. {
  243. return TRUE;
  244. }
  245. return FALSE;
  246. }
  247. // LLVector2 Operators
  248. // For sorting. By convention, x is "more significant" than y.
  249. inline bool operator<(const LLVector2 &a, const LLVector2 &b)
  250. {
  251. if( a.mV[VX] == b.mV[VX] )
  252. {
  253. return a.mV[VY] < b.mV[VY];
  254. }
  255. else
  256. {
  257. return a.mV[VX] < b.mV[VX];
  258. }
  259. }
  260. inline LLVector2 operator+(const LLVector2 &a, const LLVector2 &b)
  261. {
  262. LLVector2 c(a);
  263. return c += b;
  264. }
  265. inline LLVector2 operator-(const LLVector2 &a, const LLVector2 &b)
  266. {
  267. LLVector2 c(a);
  268. return c -= b;
  269. }
  270. inline F32 operator*(const LLVector2 &a, const LLVector2 &b)
  271. {
  272. return (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1]);
  273. }
  274. inline LLVector2 operator%(const LLVector2 &a, const LLVector2 &b)
  275. {
  276. return LLVector2(a.mV[0]*b.mV[1] - b.mV[0]*a.mV[1], a.mV[1]*b.mV[0] - b.mV[1]*a.mV[0]);
  277. }
  278. inline LLVector2 operator/(const LLVector2 &a, F32 k)
  279. {
  280. F32 t = 1.f / k;
  281. return LLVector2( a.mV[0] * t, a.mV[1] * t );
  282. }
  283. inline LLVector2 operator*(const LLVector2 &a, F32 k)
  284. {
  285. return LLVector2( a.mV[0] * k, a.mV[1] * k );
  286. }
  287. inline LLVector2 operator*(F32 k, const LLVector2 &a)
  288. {
  289. return LLVector2( a.mV[0] * k, a.mV[1] * k );
  290. }
  291. inline bool operator==(const LLVector2 &a, const LLVector2 &b)
  292. {
  293. return ( (a.mV[0] == b.mV[0])
  294. &&(a.mV[1] == b.mV[1]));
  295. }
  296. inline bool operator!=(const LLVector2 &a, const LLVector2 &b)
  297. {
  298. return ( (a.mV[0] != b.mV[0])
  299. ||(a.mV[1] != b.mV[1]));
  300. }
  301. inline const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b)
  302. {
  303. a.mV[0] += b.mV[0];
  304. a.mV[1] += b.mV[1];
  305. return a;
  306. }
  307. inline const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b)
  308. {
  309. a.mV[0] -= b.mV[0];
  310. a.mV[1] -= b.mV[1];
  311. return a;
  312. }
  313. inline const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b)
  314. {
  315. LLVector2 ret(a.mV[0]*b.mV[1] - b.mV[0]*a.mV[1], a.mV[1]*b.mV[0] - b.mV[1]*a.mV[0]);
  316. a = ret;
  317. return a;
  318. }
  319. inline const LLVector2& operator*=(LLVector2 &a, F32 k)
  320. {
  321. a.mV[0] *= k;
  322. a.mV[1] *= k;
  323. return a;
  324. }
  325. inline const LLVector2& operator/=(LLVector2 &a, F32 k)
  326. {
  327. F32 t = 1.f / k;
  328. a.mV[0] *= t;
  329. a.mV[1] *= t;
  330. return a;
  331. }
  332. inline LLVector2 operator-(const LLVector2 &a)
  333. {
  334. return LLVector2( -a.mV[0], -a.mV[1] );
  335. }
  336. inline void update_min_max(LLVector2& min, LLVector2& max, const LLVector2& pos)
  337. {
  338. for (U32 i = 0; i < 2; i++)
  339. {
  340. if (min.mV[i] > pos.mV[i])
  341. {
  342. min.mV[i] = pos.mV[i];
  343. }
  344. if (max.mV[i] < pos.mV[i])
  345. {
  346. max.mV[i] = pos.mV[i];
  347. }
  348. }
  349. }
  350. inline std::ostream& operator<<(std::ostream& s, const LLVector2 &a)
  351. {
  352. s << "{ " << a.mV[VX] << ", " << a.mV[VY] << " }";
  353. return s;
  354. }
  355. #endif