llvector4.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**
  2. * @file llvector4.h
  3. * @brief LLVector4 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_V4MATH_H
  33. #define LL_V4MATH_H
  34. #include "llvector2.h"
  35. class LLMatrix3;
  36. class LLMatrix4;
  37. class LLQuaternion;
  38. // LLVector4 = |x y z w|
  39. constexpr U32 LENGTHOFVECTOR4 = 4;
  40. class LLVector4
  41. {
  42. public:
  43. // Initializes LLVector4 to (0, 0, 0, 1):
  44. LL_INLINE LLVector4() noexcept { mV[VX] = mV[VY] = mV[VZ] = 0.f; mV[VW] = 1.f; }
  45. // Initializes LLVector4 to (x. y, z, 1):
  46. LL_INLINE LLVector4(F32 x, F32 y, F32 z) noexcept
  47. {
  48. mV[VX] = x;
  49. mV[VY] = y;
  50. mV[VZ] = z;
  51. mV[VW] = 1.f;
  52. }
  53. LL_INLINE LLVector4(F32 x, F32 y, F32 z, F32 w) noexcept
  54. {
  55. mV[VX] = x;
  56. mV[VY] = y;
  57. mV[VZ] = z;
  58. mV[VW] = w;
  59. }
  60. LL_INLINE explicit LLVector4(const F32* vec) noexcept
  61. {
  62. mV[VX] = vec[VX];
  63. mV[VY] = vec[VY];
  64. mV[VZ] = vec[VZ];
  65. mV[VW] = vec[VW];
  66. }
  67. LL_INLINE explicit LLVector4(const F64* vec) noexcept
  68. {
  69. mV[VX] = (F32)vec[VX];
  70. mV[VY] = (F32)vec[VY];
  71. mV[VZ] = (F32)vec[VZ];
  72. mV[VW] = (F32)vec[VW];
  73. }
  74. LL_INLINE explicit LLVector4(const LLVector2& vec) noexcept
  75. {
  76. mV[VX] = vec[VX];
  77. mV[VY] = vec[VY];
  78. mV[VZ] = mV[VW] = 0.f;
  79. }
  80. LL_INLINE explicit LLVector4(const LLVector2& vec, F32 z, F32 w) noexcept
  81. {
  82. mV[VX] = vec[VX];
  83. mV[VY] = vec[VY];
  84. mV[VZ] = z;
  85. mV[VW] = w;
  86. }
  87. // Initializes LLVector4 to (vec, 1):
  88. LL_INLINE explicit LLVector4(const LLVector3& vec) noexcept
  89. {
  90. mV[VX] = vec.mV[VX];
  91. mV[VY] = vec.mV[VY];
  92. mV[VZ] = vec.mV[VZ];
  93. mV[VW] = 1.f;
  94. }
  95. // Initializes LLVector4 to (vec, w):
  96. LL_INLINE explicit LLVector4(const LLVector3& vec, F32 w) noexcept
  97. {
  98. mV[VX] = vec.mV[VX];
  99. mV[VY] = vec.mV[VY];
  100. mV[VZ] = vec.mV[VZ];
  101. mV[VW] = w;
  102. }
  103. LL_INLINE explicit LLVector4(const LLSD& sd)
  104. {
  105. mV[0] = sd[0].asReal();
  106. mV[1] = sd[1].asReal();
  107. mV[2] = sd[2].asReal();
  108. mV[3] = sd[3].asReal();
  109. }
  110. // Allow the use of the default C++11 move constructor and assignation
  111. LLVector4(LLVector4&& other) noexcept = default;
  112. LLVector4& operator=(LLVector4&& other) noexcept = default;
  113. LLVector4(const LLVector4& other) = default;
  114. LLVector4& operator=(const LLVector4& other) = default;
  115. LL_INLINE void setValue(const LLSD& sd)
  116. {
  117. mV[0] = sd[0].asReal();
  118. mV[1] = sd[1].asReal();
  119. mV[2] = sd[2].asReal();
  120. mV[3] = sd[3].asReal();
  121. }
  122. LL_INLINE LLSD getValue() const
  123. {
  124. LLSD ret;
  125. ret[0] = mV[0];
  126. ret[1] = mV[1];
  127. ret[2] = mV[2];
  128. ret[3] = mV[3];
  129. return ret;
  130. }
  131. // Checks to see if all values of LLVector3 are finite
  132. LL_INLINE bool isFinite() const
  133. {
  134. return llfinite(mV[VX]) && llfinite(mV[VY]) && llfinite(mV[VZ]) &&
  135. llfinite(mV[VW]);
  136. }
  137. // Clears LLVector4 to (0, 0, 0, 1)
  138. LL_INLINE void clear() { mV[VX] = mV[VY] = mV[VZ] = 0.f; mV[VW] = 1.f; }
  139. // Clears LLVector4 to (0, 0, 0, 0)
  140. LL_INLINE void setZero() { mV[VX] = mV[VY] = mV[VZ] = mV[VW] = 0.f; }
  141. // Sets LLVector4 to (x, y, z, 1)
  142. LL_INLINE void set(F32 x, F32 y, F32 z)
  143. {
  144. mV[VX] = x;
  145. mV[VY] = y;
  146. mV[VZ] = z;
  147. mV[VW] = 1.f;
  148. }
  149. LL_INLINE void set(F32 x, F32 y, F32 z, F32 w)
  150. {
  151. mV[VX] = x;
  152. mV[VY] = y;
  153. mV[VZ] = z;
  154. mV[VW] = w;
  155. }
  156. LL_INLINE void set(const LLVector4& vec)
  157. {
  158. mV[VX] = vec.mV[VX];
  159. mV[VY] = vec.mV[VY];
  160. mV[VZ] = vec.mV[VZ];
  161. mV[VW] = vec.mV[VW];
  162. }
  163. LL_INLINE void set(const LLVector3& vec, F32 w = 1.f)
  164. {
  165. mV[VX] = vec.mV[VX];
  166. mV[VY] = vec.mV[VY];
  167. mV[VZ] = vec.mV[VZ];
  168. mV[VW] = w;
  169. }
  170. LL_INLINE void set(const F32* vec)
  171. {
  172. mV[VX] = vec[VX];
  173. mV[VY] = vec[VY];
  174. mV[VZ] = vec[VZ];
  175. mV[VW] = vec[VW];
  176. }
  177. // Returns magnitude of LLVector4
  178. LL_INLINE F32 length() const
  179. {
  180. return sqrtf(mV[VX] * mV[VX] + mV[VY] * mV[VY] + mV[VZ] * mV[VZ]);
  181. }
  182. // Returns magnitude squared of LLVector4
  183. LL_INLINE F32 lengthSquared() const
  184. {
  185. return mV[VX] * mV[VX] + mV[VY] * mV[VY] + mV[VZ] * mV[VZ];
  186. }
  187. // Normalizes and returns the magnitude
  188. LL_INLINE F32 normalize()
  189. {
  190. F32 mag = sqrtf(mV[VX] * mV[VX] + mV[VY] * mV[VY] + mV[VZ] * mV[VZ]);
  191. if (mag > FP_MAG_THRESHOLD)
  192. {
  193. F32 oomag = 1.f / mag;
  194. mV[VX] *= oomag;
  195. mV[VY] *= oomag;
  196. mV[VZ] *= oomag;
  197. }
  198. else
  199. {
  200. mV[0] = mV[1] = mV[2] = mag = 0.f;
  201. }
  202. return mag;
  203. }
  204. // Sets all values to absolute value of their original values
  205. // Returns true if data changed
  206. bool abs();
  207. LL_INLINE bool isExactlyClear() const { return mV[VW] == 1.f && !mV[VX] && !mV[VY] && !mV[VZ]; }
  208. LL_INLINE bool isExactlyZero() const { return !mV[VW] && !mV[VX] && !mV[VY] && !mV[VZ]; }
  209. // Rotates about vec by angle radians
  210. const LLVector4& rotVec(F32 angle, const LLVector4& vec);
  211. // Rotates about x,y,z by angle radians
  212. const LLVector4& rotVec(F32 angle, F32 x, F32 y, F32 z);
  213. // Rotates by MAT4 mat
  214. const LLVector4& rotVec(const LLMatrix4& mat);
  215. // Rotates by QUAT q
  216. const LLVector4& rotVec(const LLQuaternion& q);
  217. // Scales component-wise by vec
  218. const LLVector4& scaleVec(const LLVector4& vec);
  219. LL_INLINE F32 operator[](int idx) const { return mV[idx]; }
  220. LL_INLINE F32& operator[](int idx) { return mV[idx]; }
  221. friend std::ostream& operator<<(std::ostream& s, const LLVector4& a); // Prints a
  222. friend LLVector4 operator+(const LLVector4& a, const LLVector4& b); // Returns vector a + b
  223. friend LLVector4 operator-(const LLVector4& a, const LLVector4& b); // Returns vector a minus b
  224. friend F32 operator*(const LLVector4& a, const LLVector4& b); // Returns a dot b
  225. friend LLVector4 operator%(const LLVector4& a, const LLVector4& b); // Returns a cross b
  226. friend LLVector4 operator/(const LLVector4& a, F32 k); // Returns a divided by scaler k
  227. friend LLVector4 operator*(const LLVector4& a, F32 k); // Returns a times scaler k
  228. friend LLVector4 operator*(F32 k, const LLVector4& a); // Returns a times scaler k
  229. friend bool operator==(const LLVector4& a, const LLVector4& b); // Returns a == b
  230. friend bool operator!=(const LLVector4& a, const LLVector4& b); // Returns a != b
  231. friend const LLVector4& operator+=(LLVector4& a, const LLVector4& b); // Returns vector a + b
  232. friend const LLVector4& operator-=(LLVector4& a, const LLVector4& b); // Returns vector a minus b
  233. friend const LLVector4& operator%=(LLVector4& a, const LLVector4& b); // Returns a cross b
  234. friend const LLVector4& operator*=(LLVector4& a, F32 k); // Returns a times scaler k
  235. friend const LLVector4& operator/=(LLVector4& a, F32 k); // Returns a divided by scaler k
  236. friend LLVector4 operator-(const LLVector4& a); // Returns vector -a
  237. public:
  238. F32 mV[LENGTHOFVECTOR4];
  239. };
  240. // Non-member functions
  241. // Returns distance between a and b
  242. LL_INLINE F32 dist_vec(const LLVector4& a, const LLVector4& b)
  243. {
  244. LLVector4 vec = a - b;
  245. return vec.length();
  246. }
  247. // Returns distance squared between a and b
  248. LL_INLINE F32 dist_vec_squared(const LLVector4& a, const LLVector4& b)
  249. {
  250. LLVector4 vec = a - b;
  251. return vec.lengthSquared();
  252. }
  253. // Returns a vector that is a linear interpolation between a and b
  254. LL_INLINE LLVector4 lerp(const LLVector4& a, const LLVector4& b, F32 u)
  255. {
  256. return LLVector4(a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
  257. a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u,
  258. a.mV[VZ] + (b.mV[VZ] - a.mV[VZ]) * u,
  259. a.mV[VW] + (b.mV[VW] - a.mV[VW]) * u);
  260. }
  261. // Returns angle (radians) between a and b
  262. F32 angle_between(const LLVector4& a, const LLVector4& b);
  263. // Returns true if a and b are very close to parallel
  264. bool are_parallel(const LLVector4& a, const LLVector4& b,
  265. F32 epsilon = F_APPROXIMATELY_ZERO);
  266. LLVector3 vec4to3(const LLVector4& vec);
  267. LLVector4 vec3to4(const LLVector3& vec);
  268. // LLVector4 Operators
  269. LL_INLINE LLVector4 operator+(const LLVector4& a, const LLVector4& b)
  270. {
  271. LLVector4 c(a);
  272. return c += b;
  273. }
  274. LL_INLINE LLVector4 operator-(const LLVector4& a, const LLVector4& b)
  275. {
  276. LLVector4 c(a);
  277. return c -= b;
  278. }
  279. LL_INLINE F32 operator*(const LLVector4& a, const LLVector4& b)
  280. {
  281. return a.mV[VX] * b.mV[VX] + a.mV[VY] * b.mV[VY] + a.mV[VZ] * b.mV[VZ];
  282. }
  283. LL_INLINE LLVector4 operator%(const LLVector4& a, const LLVector4& b)
  284. {
  285. return LLVector4(a.mV[VY] * b.mV[VZ] - b.mV[VY] * a.mV[VZ],
  286. a.mV[VZ] * b.mV[VX] - b.mV[VZ] * a.mV[VX],
  287. a.mV[VX] * b.mV[VY] - b.mV[VX] * a.mV[VY]);
  288. }
  289. LL_INLINE LLVector4 operator/(const LLVector4& a, F32 k)
  290. {
  291. F32 t = 1.f / k;
  292. return LLVector4(a.mV[VX] * t, a.mV[VY] * t, a.mV[VZ] * t);
  293. }
  294. LL_INLINE LLVector4 operator*(const LLVector4& a, F32 k)
  295. {
  296. return LLVector4(a.mV[VX] * k, a.mV[VY] * k, a.mV[VZ] * k);
  297. }
  298. LL_INLINE LLVector4 operator*(F32 k, const LLVector4& a)
  299. {
  300. return LLVector4(a.mV[VX] * k, a.mV[VY] * k, a.mV[VZ] * k);
  301. }
  302. LL_INLINE bool operator==(const LLVector4& a, const LLVector4& b)
  303. {
  304. return a.mV[VX] == b.mV[VX] && a.mV[VY] == b.mV[VY] &&
  305. a.mV[VZ] == b.mV[VZ];
  306. }
  307. LL_INLINE bool operator!=(const LLVector4& a, const LLVector4& b)
  308. {
  309. return a.mV[VX] != b.mV[VX] || a.mV[VY] != b.mV[VY] ||
  310. a.mV[VZ] != b.mV[VZ] || a.mV[VW] != b.mV[VW];
  311. }
  312. LL_INLINE const LLVector4& operator+=(LLVector4& a, const LLVector4& b)
  313. {
  314. a.mV[VX] += b.mV[VX];
  315. a.mV[VY] += b.mV[VY];
  316. a.mV[VZ] += b.mV[VZ];
  317. return a;
  318. }
  319. LL_INLINE const LLVector4& operator-=(LLVector4& a, const LLVector4& b)
  320. {
  321. a.mV[VX] -= b.mV[VX];
  322. a.mV[VY] -= b.mV[VY];
  323. a.mV[VZ] -= b.mV[VZ];
  324. return a;
  325. }
  326. LL_INLINE const LLVector4& operator%=(LLVector4& a, const LLVector4& b)
  327. {
  328. LLVector4 ret(a.mV[VY] * b.mV[VZ] - b.mV[VY] * a.mV[VZ],
  329. a.mV[VZ] * b.mV[VX] - b.mV[VZ] * a.mV[VX],
  330. a.mV[VX] * b.mV[VY] - b.mV[VX] * a.mV[VY]);
  331. a = ret;
  332. return a;
  333. }
  334. LL_INLINE const LLVector4& operator*=(LLVector4& a, F32 k)
  335. {
  336. a.mV[VX] *= k;
  337. a.mV[VY] *= k;
  338. a.mV[VZ] *= k;
  339. return a;
  340. }
  341. LL_INLINE const LLVector4& operator/=(LLVector4& a, F32 k)
  342. {
  343. F32 t = 1.f / k;
  344. a.mV[VX] *= t;
  345. a.mV[VY] *= t;
  346. a.mV[VZ] *= t;
  347. return a;
  348. }
  349. LL_INLINE LLVector4 operator-(const LLVector4& a)
  350. {
  351. return LLVector4(-a.mV[VX], -a.mV[VY], -a.mV[VZ]);
  352. }
  353. LL_INLINE const LLVector4 srgbVector4(const LLVector4& a)
  354. {
  355. return LLVector4(linearToSRGB(a.mV[0]), linearToSRGB(a.mV[1]),
  356. linearToSRGB(a.mV[2]), a.mV[3]);
  357. }
  358. #endif // LL_V4MATH_H