/indra/llmath/v3dmath.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 147 lines · 87 code · 24 blank · 36 comment · 18 complexity · 2aa9bf9ddd3a16d10a91392ffe7c7acc MD5 · raw file

  1. /**
  2. * @file v3dmath.cpp
  3. * @brief LLVector3d 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 <sstream> // gcc 2.95.2 doesn't support sstream
  28. #include "v3dmath.h"
  29. //#include "vmath.h"
  30. #include "v4math.h"
  31. #include "m4math.h"
  32. #include "m3math.h"
  33. #include "llquaternion.h"
  34. #include "llquantize.h"
  35. // LLVector3d
  36. // WARNING: Don't use these for global const definitions!
  37. // For example:
  38. // const LLQuaternion(0.5f * F_PI, LLVector3d::zero);
  39. // at the top of a *.cpp file might not give you what you think.
  40. const LLVector3d LLVector3d::zero(0,0,0);
  41. const LLVector3d LLVector3d::x_axis(1, 0, 0);
  42. const LLVector3d LLVector3d::y_axis(0, 1, 0);
  43. const LLVector3d LLVector3d::z_axis(0, 0, 1);
  44. const LLVector3d LLVector3d::x_axis_neg(-1, 0, 0);
  45. const LLVector3d LLVector3d::y_axis_neg(0, -1, 0);
  46. const LLVector3d LLVector3d::z_axis_neg(0, 0, -1);
  47. // Clamps each values to range (min,max).
  48. // Returns TRUE if data changed.
  49. BOOL LLVector3d::clamp(F64 min, F64 max)
  50. {
  51. BOOL ret = FALSE;
  52. if (mdV[0] < min) { mdV[0] = min; ret = TRUE; }
  53. if (mdV[1] < min) { mdV[1] = min; ret = TRUE; }
  54. if (mdV[2] < min) { mdV[2] = min; ret = TRUE; }
  55. if (mdV[0] > max) { mdV[0] = max; ret = TRUE; }
  56. if (mdV[1] > max) { mdV[1] = max; ret = TRUE; }
  57. if (mdV[2] > max) { mdV[2] = max; ret = TRUE; }
  58. return ret;
  59. }
  60. // Sets all values to absolute value of their original values
  61. // Returns TRUE if data changed
  62. BOOL LLVector3d::abs()
  63. {
  64. BOOL ret = FALSE;
  65. if (mdV[0] < 0.0) { mdV[0] = -mdV[0]; ret = TRUE; }
  66. if (mdV[1] < 0.0) { mdV[1] = -mdV[1]; ret = TRUE; }
  67. if (mdV[2] < 0.0) { mdV[2] = -mdV[2]; ret = TRUE; }
  68. return ret;
  69. }
  70. std::ostream& operator<<(std::ostream& s, const LLVector3d &a)
  71. {
  72. s << "{ " << a.mdV[VX] << ", " << a.mdV[VY] << ", " << a.mdV[VZ] << " }";
  73. return s;
  74. }
  75. const LLVector3d& LLVector3d::operator=(const LLVector4 &a)
  76. {
  77. mdV[0] = a.mV[0];
  78. mdV[1] = a.mV[1];
  79. mdV[2] = a.mV[2];
  80. return *this;
  81. }
  82. const LLVector3d& LLVector3d::rotVec(const LLMatrix3 &mat)
  83. {
  84. *this = *this * mat;
  85. return *this;
  86. }
  87. const LLVector3d& LLVector3d::rotVec(const LLQuaternion &q)
  88. {
  89. *this = *this * q;
  90. return *this;
  91. }
  92. const LLVector3d& LLVector3d::rotVec(F64 angle, const LLVector3d &vec)
  93. {
  94. if ( !vec.isExactlyZero() && angle )
  95. {
  96. *this = *this * LLMatrix3((F32)angle, vec);
  97. }
  98. return *this;
  99. }
  100. const LLVector3d& LLVector3d::rotVec(F64 angle, F64 x, F64 y, F64 z)
  101. {
  102. LLVector3d vec(x, y, z);
  103. if ( !vec.isExactlyZero() && angle )
  104. {
  105. *this = *this * LLMatrix3((F32)angle, vec);
  106. }
  107. return *this;
  108. }
  109. BOOL LLVector3d::parseVector3d(const std::string& buf, LLVector3d* value)
  110. {
  111. if( buf.empty() || value == NULL)
  112. {
  113. return FALSE;
  114. }
  115. LLVector3d v;
  116. S32 count = sscanf( buf.c_str(), "%lf %lf %lf", v.mdV + 0, v.mdV + 1, v.mdV + 2 );
  117. if( 3 == count )
  118. {
  119. value->setVec( v );
  120. return TRUE;
  121. }
  122. return FALSE;
  123. }