PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/v3math.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 371 lines | 274 code | 55 blank | 42 comment | 32 complexity | 71d918abae4c1362b3061570daa6b3fd MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v3math.cpp
  3. * @brief LLVector3 class implementation.
  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. #include "linden_common.h"
  27. #include "v3math.h"
  28. //#include "vmath.h"
  29. #include "v2math.h"
  30. #include "v4math.h"
  31. #include "m4math.h"
  32. #include "m3math.h"
  33. #include "llquaternion.h"
  34. #include "llquantize.h"
  35. #include "v3dmath.h"
  36. // LLVector3
  37. // WARNING: Don't use these for global const definitions!
  38. // For example:
  39. // const LLQuaternion(0.5f * F_PI, LLVector3::zero);
  40. // at the top of a *.cpp file might not give you what you think.
  41. const LLVector3 LLVector3::zero(0,0,0);
  42. const LLVector3 LLVector3::x_axis(1.f, 0, 0);
  43. const LLVector3 LLVector3::y_axis(0, 1.f, 0);
  44. const LLVector3 LLVector3::z_axis(0, 0, 1.f);
  45. const LLVector3 LLVector3::x_axis_neg(-1.f, 0, 0);
  46. const LLVector3 LLVector3::y_axis_neg(0, -1.f, 0);
  47. const LLVector3 LLVector3::z_axis_neg(0, 0, -1.f);
  48. const LLVector3 LLVector3::all_one(1.f,1.f,1.f);
  49. // Clamps each values to range (min,max).
  50. // Returns TRUE if data changed.
  51. BOOL LLVector3::clamp(F32 min, F32 max)
  52. {
  53. BOOL ret = FALSE;
  54. if (mV[0] < min) { mV[0] = min; ret = TRUE; }
  55. if (mV[1] < min) { mV[1] = min; ret = TRUE; }
  56. if (mV[2] < min) { mV[2] = min; ret = TRUE; }
  57. if (mV[0] > max) { mV[0] = max; ret = TRUE; }
  58. if (mV[1] > max) { mV[1] = max; ret = TRUE; }
  59. if (mV[2] > max) { mV[2] = max; ret = TRUE; }
  60. return ret;
  61. }
  62. // Clamps length to an upper limit.
  63. // Returns TRUE if the data changed
  64. BOOL LLVector3::clampLength( F32 length_limit )
  65. {
  66. BOOL changed = FALSE;
  67. F32 len = length();
  68. if (llfinite(len))
  69. {
  70. if ( len > length_limit)
  71. {
  72. normalize();
  73. if (length_limit < 0.f)
  74. {
  75. length_limit = 0.f;
  76. }
  77. mV[0] *= length_limit;
  78. mV[1] *= length_limit;
  79. mV[2] *= length_limit;
  80. changed = TRUE;
  81. }
  82. }
  83. else
  84. { // this vector may still be salvagable
  85. F32 max_abs_component = 0.f;
  86. for (S32 i = 0; i < 3; ++i)
  87. {
  88. F32 abs_component = fabs(mV[i]);
  89. if (llfinite(abs_component))
  90. {
  91. if (abs_component > max_abs_component)
  92. {
  93. max_abs_component = abs_component;
  94. }
  95. }
  96. else
  97. {
  98. // no it can't be salvaged --> clear it
  99. clear();
  100. changed = TRUE;
  101. break;
  102. }
  103. }
  104. if (!changed)
  105. {
  106. // yes it can be salvaged -->
  107. // bring the components down before we normalize
  108. mV[0] /= max_abs_component;
  109. mV[1] /= max_abs_component;
  110. mV[2] /= max_abs_component;
  111. normalize();
  112. if (length_limit < 0.f)
  113. {
  114. length_limit = 0.f;
  115. }
  116. mV[0] *= length_limit;
  117. mV[1] *= length_limit;
  118. mV[2] *= length_limit;
  119. }
  120. }
  121. return changed;
  122. }
  123. BOOL LLVector3::clamp(const LLVector3 &min_vec, const LLVector3 &max_vec)
  124. {
  125. BOOL ret = FALSE;
  126. if (mV[0] < min_vec[0]) { mV[0] = min_vec[0]; ret = TRUE; }
  127. if (mV[1] < min_vec[1]) { mV[1] = min_vec[1]; ret = TRUE; }
  128. if (mV[2] < min_vec[2]) { mV[2] = min_vec[2]; ret = TRUE; }
  129. if (mV[0] > max_vec[0]) { mV[0] = max_vec[0]; ret = TRUE; }
  130. if (mV[1] > max_vec[1]) { mV[1] = max_vec[1]; ret = TRUE; }
  131. if (mV[2] > max_vec[2]) { mV[2] = max_vec[2]; ret = TRUE; }
  132. return ret;
  133. }
  134. // Sets all values to absolute value of their original values
  135. // Returns TRUE if data changed
  136. BOOL LLVector3::abs()
  137. {
  138. BOOL ret = FALSE;
  139. if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = TRUE; }
  140. if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = TRUE; }
  141. if (mV[2] < 0.f) { mV[2] = -mV[2]; ret = TRUE; }
  142. return ret;
  143. }
  144. // Quatizations
  145. void LLVector3::quantize16(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz)
  146. {
  147. F32 x = mV[VX];
  148. F32 y = mV[VY];
  149. F32 z = mV[VZ];
  150. x = U16_to_F32(F32_to_U16(x, lowerxy, upperxy), lowerxy, upperxy);
  151. y = U16_to_F32(F32_to_U16(y, lowerxy, upperxy), lowerxy, upperxy);
  152. z = U16_to_F32(F32_to_U16(z, lowerz, upperz), lowerz, upperz);
  153. mV[VX] = x;
  154. mV[VY] = y;
  155. mV[VZ] = z;
  156. }
  157. void LLVector3::quantize8(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz)
  158. {
  159. mV[VX] = U8_to_F32(F32_to_U8(mV[VX], lowerxy, upperxy), lowerxy, upperxy);;
  160. mV[VY] = U8_to_F32(F32_to_U8(mV[VY], lowerxy, upperxy), lowerxy, upperxy);
  161. mV[VZ] = U8_to_F32(F32_to_U8(mV[VZ], lowerz, upperz), lowerz, upperz);
  162. }
  163. void LLVector3::snap(S32 sig_digits)
  164. {
  165. mV[VX] = snap_to_sig_figs(mV[VX], sig_digits);
  166. mV[VY] = snap_to_sig_figs(mV[VY], sig_digits);
  167. mV[VZ] = snap_to_sig_figs(mV[VZ], sig_digits);
  168. }
  169. const LLVector3& LLVector3::rotVec(const LLMatrix3 &mat)
  170. {
  171. *this = *this * mat;
  172. return *this;
  173. }
  174. const LLVector3& LLVector3::rotVec(const LLQuaternion &q)
  175. {
  176. *this = *this * q;
  177. return *this;
  178. }
  179. const LLVector3& LLVector3::transVec(const LLMatrix4& mat)
  180. {
  181. setVec(
  182. mV[VX] * mat.mMatrix[VX][VX] +
  183. mV[VY] * mat.mMatrix[VX][VY] +
  184. mV[VZ] * mat.mMatrix[VX][VZ] +
  185. mat.mMatrix[VX][VW],
  186. mV[VX] * mat.mMatrix[VY][VX] +
  187. mV[VY] * mat.mMatrix[VY][VY] +
  188. mV[VZ] * mat.mMatrix[VY][VZ] +
  189. mat.mMatrix[VY][VW],
  190. mV[VX] * mat.mMatrix[VZ][VX] +
  191. mV[VY] * mat.mMatrix[VZ][VY] +
  192. mV[VZ] * mat.mMatrix[VZ][VZ] +
  193. mat.mMatrix[VZ][VW]);
  194. return *this;
  195. }
  196. const LLVector3& LLVector3::rotVec(F32 angle, const LLVector3 &vec)
  197. {
  198. if ( !vec.isExactlyZero() && angle )
  199. {
  200. *this = *this * LLQuaternion(angle, vec);
  201. }
  202. return *this;
  203. }
  204. const LLVector3& LLVector3::rotVec(F32 angle, F32 x, F32 y, F32 z)
  205. {
  206. LLVector3 vec(x, y, z);
  207. if ( !vec.isExactlyZero() && angle )
  208. {
  209. *this = *this * LLQuaternion(angle, vec);
  210. }
  211. return *this;
  212. }
  213. const LLVector3& LLVector3::scaleVec(const LLVector3& vec)
  214. {
  215. mV[VX] *= vec.mV[VX];
  216. mV[VY] *= vec.mV[VY];
  217. mV[VZ] *= vec.mV[VZ];
  218. return *this;
  219. }
  220. LLVector3 LLVector3::scaledVec(const LLVector3& vec) const
  221. {
  222. LLVector3 ret = LLVector3(*this);
  223. ret.scaleVec(vec);
  224. return ret;
  225. }
  226. const LLVector3& LLVector3::set(const LLVector3d &vec)
  227. {
  228. mV[0] = (F32)vec.mdV[0];
  229. mV[1] = (F32)vec.mdV[1];
  230. mV[2] = (F32)vec.mdV[2];
  231. return (*this);
  232. }
  233. const LLVector3& LLVector3::set(const LLVector4 &vec)
  234. {
  235. mV[0] = vec.mV[0];
  236. mV[1] = vec.mV[1];
  237. mV[2] = vec.mV[2];
  238. return (*this);
  239. }
  240. const LLVector3& LLVector3::setVec(const LLVector3d &vec)
  241. {
  242. mV[0] = (F32)vec.mdV[0];
  243. mV[1] = (F32)vec.mdV[1];
  244. mV[2] = (F32)vec.mdV[2];
  245. return (*this);
  246. }
  247. const LLVector3& LLVector3::setVec(const LLVector4 &vec)
  248. {
  249. mV[0] = vec.mV[0];
  250. mV[1] = vec.mV[1];
  251. mV[2] = vec.mV[2];
  252. return (*this);
  253. }
  254. LLVector3::LLVector3(const LLVector2 &vec)
  255. {
  256. mV[VX] = (F32)vec.mV[VX];
  257. mV[VY] = (F32)vec.mV[VY];
  258. mV[VZ] = 0;
  259. }
  260. LLVector3::LLVector3(const LLVector3d &vec)
  261. {
  262. mV[VX] = (F32)vec.mdV[VX];
  263. mV[VY] = (F32)vec.mdV[VY];
  264. mV[VZ] = (F32)vec.mdV[VZ];
  265. }
  266. LLVector3::LLVector3(const LLVector4 &vec)
  267. {
  268. mV[VX] = (F32)vec.mV[VX];
  269. mV[VY] = (F32)vec.mV[VY];
  270. mV[VZ] = (F32)vec.mV[VZ];
  271. }
  272. LLVector3::LLVector3(const LLSD& sd)
  273. {
  274. setValue(sd);
  275. }
  276. LLSD LLVector3::getValue() const
  277. {
  278. LLSD ret;
  279. ret[0] = mV[0];
  280. ret[1] = mV[1];
  281. ret[2] = mV[2];
  282. return ret;
  283. }
  284. void LLVector3::setValue(const LLSD& sd)
  285. {
  286. mV[0] = (F32) sd[0].asReal();
  287. mV[1] = (F32) sd[1].asReal();
  288. mV[2] = (F32) sd[2].asReal();
  289. }
  290. const LLVector3& operator*=(LLVector3 &a, const LLQuaternion &rot)
  291. {
  292. const F32 rw = - rot.mQ[VX] * a.mV[VX] - rot.mQ[VY] * a.mV[VY] - rot.mQ[VZ] * a.mV[VZ];
  293. const F32 rx = rot.mQ[VW] * a.mV[VX] + rot.mQ[VY] * a.mV[VZ] - rot.mQ[VZ] * a.mV[VY];
  294. const F32 ry = rot.mQ[VW] * a.mV[VY] + rot.mQ[VZ] * a.mV[VX] - rot.mQ[VX] * a.mV[VZ];
  295. const F32 rz = rot.mQ[VW] * a.mV[VZ] + rot.mQ[VX] * a.mV[VY] - rot.mQ[VY] * a.mV[VX];
  296. a.mV[VX] = - rw * rot.mQ[VX] + rx * rot.mQ[VW] - ry * rot.mQ[VZ] + rz * rot.mQ[VY];
  297. a.mV[VY] = - rw * rot.mQ[VY] + ry * rot.mQ[VW] - rz * rot.mQ[VX] + rx * rot.mQ[VZ];
  298. a.mV[VZ] = - rw * rot.mQ[VZ] + rz * rot.mQ[VW] - rx * rot.mQ[VY] + ry * rot.mQ[VX];
  299. return a;
  300. }
  301. // static
  302. BOOL LLVector3::parseVector3(const std::string& buf, LLVector3* value)
  303. {
  304. if( buf.empty() || value == NULL)
  305. {
  306. return FALSE;
  307. }
  308. LLVector3 v;
  309. S32 count = sscanf( buf.c_str(), "%f %f %f", v.mV + 0, v.mV + 1, v.mV + 2 );
  310. if( 3 == count )
  311. {
  312. value->setVec( v );
  313. return TRUE;
  314. }
  315. return FALSE;
  316. }