llvector3d.h 11 KB

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