PageRenderTime 265ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/llbbox.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 178 lines | 119 code | 23 blank | 36 comment | 4 complexity | e399dc9bcd543ae3015b30278c616696 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llbbox.cpp
  3. * @brief General purpose bounding box class (Not axis aligned)
  4. *
  5. * $LicenseInfo:firstyear=2001&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. // self include
  28. #include "llbbox.h"
  29. // library includes
  30. #include "m4math.h"
  31. void LLBBox::addPointLocal(const LLVector3& p)
  32. {
  33. if (mEmpty)
  34. {
  35. mMinLocal = p;
  36. mMaxLocal = p;
  37. mEmpty = FALSE;
  38. }
  39. else
  40. {
  41. mMinLocal.mV[VX] = llmin( p.mV[VX], mMinLocal.mV[VX] );
  42. mMinLocal.mV[VY] = llmin( p.mV[VY], mMinLocal.mV[VY] );
  43. mMinLocal.mV[VZ] = llmin( p.mV[VZ], mMinLocal.mV[VZ] );
  44. mMaxLocal.mV[VX] = llmax( p.mV[VX], mMaxLocal.mV[VX] );
  45. mMaxLocal.mV[VY] = llmax( p.mV[VY], mMaxLocal.mV[VY] );
  46. mMaxLocal.mV[VZ] = llmax( p.mV[VZ], mMaxLocal.mV[VZ] );
  47. }
  48. }
  49. void LLBBox::addPointAgent( LLVector3 p)
  50. {
  51. p -= mPosAgent;
  52. p.rotVec( ~mRotation );
  53. addPointLocal( p );
  54. }
  55. void LLBBox::addBBoxAgent(const LLBBox& b)
  56. {
  57. if (mEmpty)
  58. {
  59. mPosAgent = b.mPosAgent;
  60. mRotation = b.mRotation;
  61. mMinLocal.clearVec();
  62. mMaxLocal.clearVec();
  63. }
  64. LLVector3 vertex[8];
  65. vertex[0].setVec( b.mMinLocal.mV[VX], b.mMinLocal.mV[VY], b.mMinLocal.mV[VZ] );
  66. vertex[1].setVec( b.mMinLocal.mV[VX], b.mMinLocal.mV[VY], b.mMaxLocal.mV[VZ] );
  67. vertex[2].setVec( b.mMinLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMinLocal.mV[VZ] );
  68. vertex[3].setVec( b.mMinLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMaxLocal.mV[VZ] );
  69. vertex[4].setVec( b.mMaxLocal.mV[VX], b.mMinLocal.mV[VY], b.mMinLocal.mV[VZ] );
  70. vertex[5].setVec( b.mMaxLocal.mV[VX], b.mMinLocal.mV[VY], b.mMaxLocal.mV[VZ] );
  71. vertex[6].setVec( b.mMaxLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMinLocal.mV[VZ] );
  72. vertex[7].setVec( b.mMaxLocal.mV[VX], b.mMaxLocal.mV[VY], b.mMaxLocal.mV[VZ] );
  73. LLMatrix4 m( b.mRotation );
  74. m.translate( b.mPosAgent );
  75. m.translate( -mPosAgent );
  76. m.rotate( ~mRotation );
  77. for( S32 i=0; i<8; i++ )
  78. {
  79. addPointLocal( vertex[i] * m );
  80. }
  81. }
  82. LLBBox LLBBox::getAxisAligned() const
  83. {
  84. // no rotation = axis aligned rotation
  85. LLBBox aligned(mPosAgent, LLQuaternion(), LLVector3(), LLVector3());
  86. // add the center point so that it's not empty
  87. aligned.addPointAgent(mPosAgent);
  88. // add our BBox
  89. aligned.addBBoxAgent(*this);
  90. return aligned;
  91. }
  92. void LLBBox::expand( F32 delta )
  93. {
  94. mMinLocal.mV[VX] -= delta;
  95. mMinLocal.mV[VY] -= delta;
  96. mMinLocal.mV[VZ] -= delta;
  97. mMaxLocal.mV[VX] += delta;
  98. mMaxLocal.mV[VY] += delta;
  99. mMaxLocal.mV[VZ] += delta;
  100. }
  101. LLVector3 LLBBox::localToAgent(const LLVector3& v) const
  102. {
  103. LLMatrix4 m( mRotation );
  104. m.translate( mPosAgent );
  105. return v * m;
  106. }
  107. LLVector3 LLBBox::agentToLocal(const LLVector3& v) const
  108. {
  109. LLMatrix4 m;
  110. m.translate( -mPosAgent );
  111. m.rotate( ~mRotation ); // inverse rotation
  112. return v * m;
  113. }
  114. LLVector3 LLBBox::localToAgentBasis(const LLVector3& v) const
  115. {
  116. LLMatrix4 m( mRotation );
  117. return v * m;
  118. }
  119. LLVector3 LLBBox::agentToLocalBasis(const LLVector3& v) const
  120. {
  121. LLMatrix4 m( ~mRotation ); // inverse rotation
  122. return v * m;
  123. }
  124. BOOL LLBBox::containsPointLocal(const LLVector3& p) const
  125. {
  126. if ( (p.mV[VX] < mMinLocal.mV[VX])
  127. ||(p.mV[VX] > mMaxLocal.mV[VX])
  128. ||(p.mV[VY] < mMinLocal.mV[VY])
  129. ||(p.mV[VY] > mMaxLocal.mV[VY])
  130. ||(p.mV[VZ] < mMinLocal.mV[VZ])
  131. ||(p.mV[VZ] > mMaxLocal.mV[VZ]))
  132. {
  133. return FALSE;
  134. }
  135. return TRUE;
  136. }
  137. BOOL LLBBox::containsPointAgent(const LLVector3& p) const
  138. {
  139. LLVector3 point_local = agentToLocal(p);
  140. return containsPointLocal(point_local);
  141. }
  142. LLVector3 LLBBox::getMinAgent() const
  143. {
  144. return localToAgent(mMinLocal);
  145. }
  146. LLVector3 LLBBox::getMaxAgent() const
  147. {
  148. return localToAgent(mMaxLocal);
  149. }
  150. /*
  151. LLBBox operator*(const LLBBox &a, const LLMatrix4 &b)
  152. {
  153. return LLBBox( a.mMin * b, a.mMax * b );
  154. }
  155. */