llbbox.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @file llbbox.h
  3. * @brief General purpose bounding box class
  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. #ifndef LL_BBOX_H
  33. #define LL_BBOX_H
  34. #include "llvector3.h"
  35. #include "llquaternion.h"
  36. // Note: "local space" for an LLBBox is defined relative to agent space in
  37. // terms of a translation followed by a rotation. There is no scale term since
  38. // the LLBBox's min and max are not necessarily symetrical and define their own
  39. // extents.
  40. class LLBBox
  41. {
  42. #if 0
  43. friend LLBBox operator*(const LLBBox& a, const LLMatrix4& b);
  44. #endif
  45. public:
  46. LLBBox() { mEmpty = true; }
  47. LLBBox(const LLVector3& pos_agent, const LLQuaternion& rot,
  48. const LLVector3& min_local, const LLVector3& max_local)
  49. : mMinLocal(min_local),
  50. mMaxLocal(max_local),
  51. mPosAgent(pos_agent),
  52. mRotation(rot),
  53. mEmpty(true)
  54. {
  55. }
  56. // Default copy constructor is OK.
  57. LL_INLINE const LLVector3& getPositionAgent() const { return mPosAgent; }
  58. LL_INLINE const LLQuaternion& getRotation() const { return mRotation; }
  59. LL_INLINE LLVector3 getMinAgent() const { return localToAgent(mMinLocal); }
  60. LL_INLINE const LLVector3& getMinLocal() const { return mMinLocal; }
  61. LL_INLINE void setMinLocal(const LLVector3& min) { mMinLocal = min; }
  62. LL_INLINE LLVector3 getMaxAgent() const { return localToAgent(mMaxLocal); }
  63. LL_INLINE const LLVector3& getMaxLocal() const { return mMaxLocal; }
  64. LL_INLINE void setMaxLocal(const LLVector3& max) { mMaxLocal = max; }
  65. LL_INLINE LLVector3 getCenterLocal() const { return (mMaxLocal - mMinLocal) * 0.5f + mMinLocal; }
  66. LL_INLINE LLVector3 getCenterAgent() const { return localToAgent(getCenterLocal()); }
  67. LL_INLINE LLVector3 getExtentLocal() const { return mMaxLocal - mMinLocal; }
  68. LL_INLINE bool containsPointLocal(const LLVector3& p) const
  69. {
  70. return !(p.mV[VX] < mMinLocal.mV[VX] || p.mV[VX] > mMaxLocal.mV[VX] ||
  71. p.mV[VY] < mMinLocal.mV[VY] || p.mV[VY] > mMaxLocal.mV[VY] ||
  72. p.mV[VZ] < mMinLocal.mV[VZ] || p.mV[VZ] > mMaxLocal.mV[VZ]);
  73. }
  74. LL_INLINE bool containsPointAgent(const LLVector3& p) const
  75. {
  76. return containsPointLocal(agentToLocal(p));
  77. }
  78. void addPointAgent(LLVector3 p);
  79. void addBBoxAgent(const LLBBox& b);
  80. void addPointLocal(const LLVector3& p);
  81. LL_INLINE void addBBoxLocal(const LLBBox& b)
  82. {
  83. addPointLocal(b.mMinLocal);
  84. addPointLocal(b.mMaxLocal);
  85. }
  86. LL_INLINE void expand(F32 delta)
  87. {
  88. mMinLocal.mV[VX] -= delta;
  89. mMinLocal.mV[VY] -= delta;
  90. mMinLocal.mV[VZ] -= delta;
  91. mMaxLocal.mV[VX] += delta;
  92. mMaxLocal.mV[VY] += delta;
  93. mMaxLocal.mV[VZ] += delta;
  94. }
  95. LLVector3 localToAgent(const LLVector3& v) const;
  96. LLVector3 agentToLocal(const LLVector3& v) const;
  97. // Changes rotation but not position
  98. LLVector3 localToAgentBasis(const LLVector3& v) const;
  99. LLVector3 agentToLocalBasis(const LLVector3& v) const;
  100. // Get the smallest possible axis aligned bbox that contains this bbox
  101. LLBBox getAxisAligned() const;
  102. private:
  103. LLQuaternion mRotation;
  104. LLVector3 mMinLocal;
  105. LLVector3 mMaxLocal;
  106. LLVector3 mPosAgent; // Position relative to Agent's Region
  107. bool mEmpty; // Nothing has been added to this bbox yet
  108. };
  109. #if 0 // Not used
  110. LL_INLINE LLBBox operator*(const LLBBox& a, const LLMatrix4& b);
  111. {
  112. return LLBBox(a.mMin * b, a.mMax * b);
  113. }
  114. #endif
  115. #endif // LL_BBOX_H