llvector2.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @file llvector2.h
  3. * @brief LLVector2 class header file.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_V2MATH_H
  33. #define LL_V2MATH_H
  34. #include "llmath.h"
  35. #include "llvector3.h"
  36. class LLVector4;
  37. class LLMatrix3;
  38. class LLQuaternion;
  39. // LLVector2 = |x y z w|
  40. constexpr U32 LENGTHOFVECTOR2 = 2;
  41. class LLVector2
  42. {
  43. public:
  44. LL_INLINE LLVector2() noexcept
  45. {
  46. mV[VX] = mV[VY] = 0.f;
  47. }
  48. LL_INLINE LLVector2(F32 x, F32 y) noexcept
  49. {
  50. mV[VX] = x;
  51. mV[VY] = y;
  52. }
  53. LL_INLINE LLVector2(const F32* vec) noexcept
  54. {
  55. mV[VX] = vec[VX];
  56. mV[VY] = vec[VY];
  57. }
  58. LL_INLINE explicit LLVector2(const LLVector3& vec) noexcept
  59. {
  60. mV[VX] = vec.mV[VX];
  61. mV[VY] = vec.mV[VY];
  62. }
  63. LL_INLINE explicit LLVector2(const LLSD& sd) noexcept
  64. {
  65. mV[0] = (F32)sd[0].asReal();
  66. mV[1] = (F32)sd[1].asReal();
  67. }
  68. // Allow the use of the default C++11 move constructor and assignation
  69. LLVector2(LLVector2&& other) noexcept = default;
  70. LLVector2& operator=(LLVector2&& other) noexcept = default;
  71. LLVector2(const LLVector2& other) = default;
  72. LLVector2& operator=(const LLVector2& other) = default;
  73. // Clears LLVector2 to (0, 0).
  74. LL_INLINE void clear() { mV[VX] = mV[VY] = 0.f; }
  75. LL_INLINE void setZero() { mV[VX] = mV[VY] = 0.f; }
  76. LL_INLINE void set(F32 x, F32 y)
  77. {
  78. mV[VX] = x;
  79. mV[VY] = y;
  80. }
  81. LL_INLINE void set(const LLVector2& vec)
  82. {
  83. mV[VX] = vec.mV[VX];
  84. mV[VY] = vec.mV[VY];
  85. }
  86. LL_INLINE void set(const F32* vec)
  87. {
  88. mV[VX] = vec[VX];
  89. mV[VY] = vec[VY];
  90. }
  91. LL_INLINE LLSD getValue() const
  92. {
  93. LLSD ret;
  94. ret[0] = mV[0];
  95. ret[1] = mV[1];
  96. return ret;
  97. }
  98. LL_INLINE void setValue(const LLSD& sd)
  99. {
  100. mV[0] = (F32)sd[0].asReal();
  101. mV[1] = (F32)sd[1].asReal();
  102. }
  103. // Checks to see if all values of LLVector2 are finite
  104. LL_INLINE bool isFinite() const { return llfinite(mV[VX]) && llfinite(mV[VY]); }
  105. // Returns magnitude of LLVector2
  106. LL_INLINE F32 length() const { return sqrtf(mV[0] * mV[0] + mV[1] * mV[1]); }
  107. // Returns magnitude squared of LLVector2
  108. LL_INLINE F32 lengthSquared() const { return mV[0] * mV[0] + mV[1] * mV[1]; }
  109. // Normalizes and returns the magnitude
  110. LL_INLINE F32 normalize()
  111. {
  112. F32 mag = sqrtf(mV[0] * mV[0] + mV[1] * mV[1]);
  113. if (mag > FP_MAG_THRESHOLD)
  114. {
  115. F32 oomag = 1.f / mag;
  116. mV[0] *= oomag;
  117. mV[1] *= oomag;
  118. }
  119. else
  120. {
  121. mV[0] = mV[1] = mag = 0.f;
  122. }
  123. return mag;
  124. }
  125. // scales per component by vec
  126. LL_INLINE const LLVector2& scaleVec(const LLVector2& vec)
  127. {
  128. mV[VX] *= vec.mV[VX];
  129. mV[VY] *= vec.mV[VY];
  130. return *this;
  131. }
  132. // Sets all values to absolute value of original value (first octant),
  133. // returns true if changed.
  134. bool abs();
  135. // Returns true if vector has a _very_small_ length
  136. LL_INLINE bool isNull()
  137. {
  138. return F_APPROXIMATELY_ZERO > mV[VX] * mV[VX] + mV[VY] * mV[VY];
  139. }
  140. LL_INLINE bool isExactlyZero() const { return !mV[VX] && !mV[VY]; }
  141. LL_INLINE F32 operator[](int idx) const { return mV[idx]; }
  142. LL_INLINE F32& operator[](int idx) { return mV[idx]; }
  143. friend bool operator<(const LLVector2& a, const LLVector2& b); // For sorting. x is "more significant" than y
  144. friend LLVector2 operator+(const LLVector2& a, const LLVector2& b); // Returns vector a + b
  145. friend LLVector2 operator-(const LLVector2& a, const LLVector2& b); // Returns vector a minus b
  146. friend F32 operator*(const LLVector2& a, const LLVector2& b); // Returns a dot b
  147. friend LLVector2 operator%(const LLVector2& a, const LLVector2& b); // Returns a cross b
  148. friend LLVector2 operator/(const LLVector2& a, F32 k); // Returns a divided by scaler k
  149. friend LLVector2 operator*(const LLVector2& a, F32 k); // Returns a times scaler k
  150. friend LLVector2 operator*(F32 k, const LLVector2& a); // Returns a times scaler k
  151. friend bool operator==(const LLVector2& a, const LLVector2& b); // Returns a == b
  152. friend bool operator!=(const LLVector2& a, const LLVector2& b); // Returns a != b
  153. friend const LLVector2& operator+=(LLVector2& a, const LLVector2& b); // Returns vector a + b
  154. friend const LLVector2& operator-=(LLVector2& a, const LLVector2& b); // Returns vector a minus b
  155. friend const LLVector2& operator%=(LLVector2& a, const LLVector2& b); // Returns a cross b
  156. friend const LLVector2& operator*=(LLVector2& a, F32 k); // Returns a times scaler k
  157. friend const LLVector2& operator/=(LLVector2& a, F32 k); // Returns a divided by scaler k
  158. friend LLVector2 operator-(const LLVector2& a); // Returns vector -a
  159. friend std::ostream& operator<<(std::ostream& s, const LLVector2& a); // Stream a
  160. public:
  161. F32 mV[LENGTHOFVECTOR2];
  162. static LLVector2 zero;
  163. };
  164. // Non-member functions
  165. LL_INLINE void update_min_max(LLVector2& min, LLVector2& max,
  166. const LLVector2& pos)
  167. {
  168. for (U32 i = 0; i < 2; ++i)
  169. {
  170. if (min.mV[i] > pos.mV[i])
  171. {
  172. min.mV[i] = pos.mV[i];
  173. }
  174. if (max.mV[i] < pos.mV[i])
  175. {
  176. max.mV[i] = pos.mV[i];
  177. }
  178. }
  179. }
  180. // Returns angle (radians) between a and b
  181. F32 angle_between(const LLVector2& a, const LLVector2& b);
  182. // Returns true if a and b are very close to parallel
  183. bool are_parallel(const LLVector2& a, const LLVector2& b,
  184. F32 epsilon = F_APPROXIMATELY_ZERO);
  185. // Returns distance between a and b
  186. F32 dist_vec(const LLVector2& a, const LLVector2& b);
  187. // Returns distance squared between a and b
  188. F32 dist_vec_squared(const LLVector2& a, const LLVector2& b);
  189. // Returns distance squared between a and b ignoring Z component
  190. F32 dist_vec_squared2D(const LLVector2& a, const LLVector2& b);
  191. // Returns a vector that is a linear interpolation between a and b
  192. LLVector2 lerp(const LLVector2& a, const LLVector2& b, F32 u);
  193. // LLVector2 Operators
  194. // For sorting. By convention, x is "more significant" than y.
  195. LL_INLINE bool operator<(const LLVector2& a, const LLVector2& b)
  196. {
  197. if (a.mV[VX] == b.mV[VX])
  198. {
  199. return a.mV[VY] < b.mV[VY];
  200. }
  201. else
  202. {
  203. return a.mV[VX] < b.mV[VX];
  204. }
  205. }
  206. LL_INLINE LLVector2 operator+(const LLVector2& a, const LLVector2& b)
  207. {
  208. LLVector2 c(a);
  209. return c += b;
  210. }
  211. LL_INLINE LLVector2 operator-(const LLVector2& a, const LLVector2& b)
  212. {
  213. LLVector2 c(a);
  214. return c -= b;
  215. }
  216. LL_INLINE F32 operator*(const LLVector2& a, const LLVector2& b)
  217. {
  218. return (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1]);
  219. }
  220. LL_INLINE LLVector2 operator%(const LLVector2& a, const LLVector2& b)
  221. {
  222. return LLVector2(a.mV[0] * b.mV[1] - b.mV[0] * a.mV[1],
  223. a.mV[1] * b.mV[0] - b.mV[1] * a.mV[0]);
  224. }
  225. LL_INLINE LLVector2 operator/(const LLVector2& a, F32 k)
  226. {
  227. F32 t = 1.f / k;
  228. return LLVector2(a.mV[0] * t, a.mV[1] * t);
  229. }
  230. LL_INLINE LLVector2 operator*(const LLVector2& a, F32 k)
  231. {
  232. return LLVector2(a.mV[0] * k, a.mV[1] * k);
  233. }
  234. LL_INLINE LLVector2 operator*(F32 k, const LLVector2& a)
  235. {
  236. return LLVector2(a.mV[0] * k, a.mV[1] * k);
  237. }
  238. LL_INLINE bool operator==(const LLVector2& a, const LLVector2& b)
  239. {
  240. return a.mV[0] == b.mV[0] && a.mV[1] == b.mV[1];
  241. }
  242. LL_INLINE bool operator!=(const LLVector2& a, const LLVector2& b)
  243. {
  244. return a.mV[0] != b.mV[0] || a.mV[1] != b.mV[1];
  245. }
  246. LL_INLINE const LLVector2& operator+=(LLVector2& a, const LLVector2& b)
  247. {
  248. a.mV[0] += b.mV[0];
  249. a.mV[1] += b.mV[1];
  250. return a;
  251. }
  252. LL_INLINE const LLVector2& operator-=(LLVector2& a, const LLVector2& b)
  253. {
  254. a.mV[0] -= b.mV[0];
  255. a.mV[1] -= b.mV[1];
  256. return a;
  257. }
  258. LL_INLINE const LLVector2& operator%=(LLVector2& a, const LLVector2& b)
  259. {
  260. LLVector2 ret(a.mV[0] * b.mV[1] - b.mV[0] * a.mV[1],
  261. a.mV[1] * b.mV[0] - b.mV[1] * a.mV[0]);
  262. a = ret;
  263. return a;
  264. }
  265. LL_INLINE const LLVector2& operator*=(LLVector2& a, F32 k)
  266. {
  267. a.mV[0] *= k;
  268. a.mV[1] *= k;
  269. return a;
  270. }
  271. LL_INLINE const LLVector2& operator/=(LLVector2& a, F32 k)
  272. {
  273. F32 t = 1.f / k;
  274. a.mV[0] *= t;
  275. a.mV[1] *= t;
  276. return a;
  277. }
  278. LL_INLINE LLVector2 operator-(const LLVector2& a)
  279. {
  280. return LLVector2(-a.mV[0], -a.mV[1]);
  281. }
  282. LL_INLINE std::ostream& operator<<(std::ostream& s, const LLVector2& a)
  283. {
  284. s << "{ " << a.mV[VX] << ", " << a.mV[VY] << " }";
  285. return s;
  286. }
  287. #endif // LL_V2MATH_H