llvector3d.cpp 4.0 KB

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