llbbox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file llbbox.cpp
  3. * @brief General purpose bounding box class (Not axis aligned)
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, 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 "llbbox.h"
  34. #include "llmatrix4.h"
  35. void LLBBox::addPointLocal(const LLVector3& p)
  36. {
  37. if (mEmpty)
  38. {
  39. mMinLocal = p;
  40. mMaxLocal = p;
  41. mEmpty = false;
  42. }
  43. else
  44. {
  45. mMinLocal.mV[VX] = llmin(p.mV[VX], mMinLocal.mV[VX]);
  46. mMinLocal.mV[VY] = llmin(p.mV[VY], mMinLocal.mV[VY]);
  47. mMinLocal.mV[VZ] = llmin(p.mV[VZ], mMinLocal.mV[VZ]);
  48. mMaxLocal.mV[VX] = llmax(p.mV[VX], mMaxLocal.mV[VX]);
  49. mMaxLocal.mV[VY] = llmax(p.mV[VY], mMaxLocal.mV[VY]);
  50. mMaxLocal.mV[VZ] = llmax(p.mV[VZ], mMaxLocal.mV[VZ]);
  51. }
  52. }
  53. void LLBBox::addPointAgent(LLVector3 p)
  54. {
  55. p -= mPosAgent;
  56. p.rotVec(~mRotation);
  57. addPointLocal(p);
  58. }
  59. void LLBBox::addBBoxAgent(const LLBBox& b)
  60. {
  61. if (mEmpty)
  62. {
  63. mPosAgent = b.mPosAgent;
  64. mRotation = b.mRotation;
  65. mMinLocal.clear();
  66. mMaxLocal.clear();
  67. }
  68. LLVector3 vertex[8];
  69. vertex[0].set(b.mMinLocal.mV[VX], b.mMinLocal.mV[VY], b.mMinLocal.mV[VZ]);
  70. vertex[1].set(b.mMinLocal.mV[VX], b.mMinLocal.mV[VY], b.mMaxLocal.mV[VZ]);
  71. vertex[2].set(b.mMinLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMinLocal.mV[VZ]);
  72. vertex[3].set(b.mMinLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMaxLocal.mV[VZ]);
  73. vertex[4].set(b.mMaxLocal.mV[VX], b.mMinLocal.mV[VY], b.mMinLocal.mV[VZ]);
  74. vertex[5].set(b.mMaxLocal.mV[VX], b.mMinLocal.mV[VY], b.mMaxLocal.mV[VZ]);
  75. vertex[6].set(b.mMaxLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMinLocal.mV[VZ]);
  76. vertex[7].set(b.mMaxLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMaxLocal.mV[VZ]);
  77. LLMatrix4 m(b.mRotation);
  78. m.translate(b.mPosAgent);
  79. m.translate(-mPosAgent);
  80. m.rotate(~mRotation);
  81. for(S32 i = 0; i < 8; ++i)
  82. {
  83. addPointLocal(vertex[i] * m);
  84. }
  85. }
  86. LLBBox LLBBox::getAxisAligned() const
  87. {
  88. // No rotation = axis aligned rotation
  89. LLBBox aligned(mPosAgent, LLQuaternion(), LLVector3(), LLVector3());
  90. // Add the center point so that it's not empty
  91. aligned.addPointAgent(mPosAgent);
  92. // Add our BBox
  93. aligned.addBBoxAgent(*this);
  94. return aligned;
  95. }
  96. LLVector3 LLBBox::localToAgent(const LLVector3& v) const
  97. {
  98. LLMatrix4 m(mRotation);
  99. m.translate(mPosAgent);
  100. return v * m;
  101. }
  102. LLVector3 LLBBox::agentToLocal(const LLVector3& v) const
  103. {
  104. LLMatrix4 m;
  105. m.translate(-mPosAgent);
  106. m.rotate(~mRotation); // inverse rotation
  107. return v * m;
  108. }
  109. LLVector3 LLBBox::localToAgentBasis(const LLVector3& v) const
  110. {
  111. return v * LLMatrix4(mRotation);
  112. }
  113. LLVector3 LLBBox::agentToLocalBasis(const LLVector3& v) const
  114. {
  115. return v * LLMatrix4(~mRotation); // Inverse rotation
  116. }