PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/v4math.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 142 lines | 69 code | 22 blank | 51 comment | 5 complexity | d39650cf663e46ccf0d587cda1de0d2a MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v4math.cpp
  3. * @brief LLVector4 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 "vmath.h"
  28. #include "v3math.h"
  29. #include "v4math.h"
  30. #include "m4math.h"
  31. #include "m3math.h"
  32. #include "llquaternion.h"
  33. // LLVector4
  34. // Axis-Angle rotations
  35. /*
  36. const LLVector4& LLVector4::rotVec(F32 angle, const LLVector4 &vec)
  37. {
  38. if ( !vec.isExactlyZero() && angle )
  39. {
  40. *this = *this * LLMatrix4(angle, vec);
  41. }
  42. return *this;
  43. }
  44. const LLVector4& LLVector4::rotVec(F32 angle, F32 x, F32 y, F32 z)
  45. {
  46. LLVector3 vec(x, y, z);
  47. if ( !vec.isExactlyZero() && angle )
  48. {
  49. *this = *this * LLMatrix4(angle, vec);
  50. }
  51. return *this;
  52. }
  53. */
  54. const LLVector4& LLVector4::rotVec(const LLMatrix4 &mat)
  55. {
  56. *this = *this * mat;
  57. return *this;
  58. }
  59. const LLVector4& LLVector4::rotVec(const LLQuaternion &q)
  60. {
  61. *this = *this * q;
  62. return *this;
  63. }
  64. const LLVector4& LLVector4::scaleVec(const LLVector4& vec)
  65. {
  66. mV[VX] *= vec.mV[VX];
  67. mV[VY] *= vec.mV[VY];
  68. mV[VZ] *= vec.mV[VZ];
  69. mV[VW] *= vec.mV[VW];
  70. return *this;
  71. }
  72. // Sets all values to absolute value of their original values
  73. // Returns TRUE if data changed
  74. BOOL LLVector4::abs()
  75. {
  76. BOOL ret = FALSE;
  77. if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = TRUE; }
  78. if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = TRUE; }
  79. if (mV[2] < 0.f) { mV[2] = -mV[2]; ret = TRUE; }
  80. if (mV[3] < 0.f) { mV[3] = -mV[3]; ret = TRUE; }
  81. return ret;
  82. }
  83. std::ostream& operator<<(std::ostream& s, const LLVector4 &a)
  84. {
  85. s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << ", " << a.mV[VW] << " }";
  86. return s;
  87. }
  88. // Non-member functions
  89. F32 angle_between( const LLVector4& a, const LLVector4& b )
  90. {
  91. LLVector4 an = a;
  92. LLVector4 bn = b;
  93. an.normalize();
  94. bn.normalize();
  95. F32 cosine = an * bn;
  96. F32 angle = (cosine >= 1.0f) ? 0.0f :
  97. (cosine <= -1.0f) ? F_PI :
  98. acos(cosine);
  99. return angle;
  100. }
  101. BOOL are_parallel(const LLVector4 &a, const LLVector4 &b, F32 epsilon)
  102. {
  103. LLVector4 an = a;
  104. LLVector4 bn = b;
  105. an.normalize();
  106. bn.normalize();
  107. F32 dot = an * bn;
  108. if ( (1.0f - fabs(dot)) < epsilon)
  109. return TRUE;
  110. return FALSE;
  111. }
  112. LLVector3 vec4to3(const LLVector4 &vec)
  113. {
  114. return LLVector3( vec.mV[VX], vec.mV[VY], vec.mV[VZ] );
  115. }
  116. LLVector4 vec3to4(const LLVector3 &vec)
  117. {
  118. return LLVector4(vec.mV[VX], vec.mV[VY], vec.mV[VZ]);
  119. }