llvector4.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file llvector4.cpp
  3. * @brief LLVector4 class implementation.
  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. #include "linden_common.h"
  33. #include "llvector3.h"
  34. #include "llvector4.h"
  35. #include "llmatrix4.h"
  36. #include "llmatrix3.h"
  37. #include "llquaternion.h"
  38. // LLVector4
  39. // Axis-Angle rotations
  40. #if 0
  41. const LLVector4& LLVector4::rotVec(F32 angle, const LLVector4& vec)
  42. {
  43. if (angle && !vec.isExactlyZero())
  44. {
  45. *this = *this * LLMatrix4(angle, vec);
  46. }
  47. return *this;
  48. }
  49. const LLVector4& LLVector4::rotVec(F32 angle, F32 x, F32 y, F32 z)
  50. {
  51. LLVector3 vec(x, y, z);
  52. if (angle && !vec.isExactlyZero())
  53. {
  54. *this = *this * LLMatrix4(angle, vec);
  55. }
  56. return *this;
  57. }
  58. #endif
  59. const LLVector4& LLVector4::rotVec(const LLMatrix4& mat)
  60. {
  61. *this = *this * mat;
  62. return *this;
  63. }
  64. const LLVector4& LLVector4::rotVec(const LLQuaternion& q)
  65. {
  66. *this = *this * q;
  67. return *this;
  68. }
  69. const LLVector4& LLVector4::scaleVec(const LLVector4& vec)
  70. {
  71. mV[VX] *= vec.mV[VX];
  72. mV[VY] *= vec.mV[VY];
  73. mV[VZ] *= vec.mV[VZ];
  74. mV[VW] *= vec.mV[VW];
  75. return *this;
  76. }
  77. // Sets all values to absolute value of their original values. Returns true if
  78. // data changed.
  79. bool LLVector4::abs()
  80. {
  81. bool ret = false;
  82. if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = true; }
  83. if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = true; }
  84. if (mV[2] < 0.f) { mV[2] = -mV[2]; ret = true; }
  85. if (mV[3] < 0.f) { mV[3] = -mV[3]; ret = true; }
  86. return ret;
  87. }
  88. std::ostream& operator<<(std::ostream& s, const LLVector4& a)
  89. {
  90. s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ]
  91. << ", " << a.mV[VW] << " }";
  92. return s;
  93. }
  94. // Non-member functions
  95. F32 angle_between(const LLVector4& a, const LLVector4& b)
  96. {
  97. LLVector4 an = a;
  98. LLVector4 bn = b;
  99. an.normalize();
  100. bn.normalize();
  101. F32 cosine = an * bn;
  102. F32 angle = cosine >= 1.f ? 0.f
  103. : (cosine <= -1.f ? F_PI : acosf(cosine));
  104. return angle;
  105. }
  106. bool are_parallel(const LLVector4& a, const LLVector4& b, F32 epsilon)
  107. {
  108. LLVector4 an = a;
  109. LLVector4 bn = b;
  110. an.normalize();
  111. bn.normalize();
  112. F32 dot = an * bn;
  113. return 1.f - fabsf(dot) < epsilon;
  114. }
  115. LLVector3 vec4to3(const LLVector4& vec)
  116. {
  117. return LLVector3(vec.mV[VX], vec.mV[VY], vec.mV[VZ]);
  118. }
  119. LLVector4 vec3to4(const LLVector3 &vec)
  120. {
  121. return LLVector4(vec.mV[VX], vec.mV[VY], vec.mV[VZ]);
  122. }