IceOBB.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains OBB-related code. (oriented bounding box)
  4. * \file IceOBB.h
  5. * \author Pierre Terdiman
  6. * \date January, 13, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICEOBB_H__
  12. #define __ICEOBB_H__
  13. // Forward declarations
  14. class LSS;
  15. class ICEMATHS_API OBB
  16. {
  17. public:
  18. //! Constructor
  19. inline_ OBB() {}
  20. //! Constructor
  21. inline_ OBB(const Point& center, const Point& extents, const Matrix3x3& rot) : mCenter(center), mExtents(extents), mRot(rot) {}
  22. //! Destructor
  23. inline_ ~OBB() {}
  24. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. /**
  26. * Setups an empty OBB.
  27. */
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. void SetEmpty()
  30. {
  31. mCenter.Zero();
  32. mExtents.Set(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);
  33. mRot.Identity();
  34. }
  35. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. /**
  37. * Tests if a point is contained within the OBB.
  38. * \param p [in] the world point to test
  39. * \return true if inside the OBB
  40. */
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. bool ContainsPoint(const Point& p) const;
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. /**
  45. * Builds an OBB from an AABB and a world transform.
  46. * \param aabb [in] the aabb
  47. * \param mat [in] the world transform
  48. */
  49. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. void Create(const AABB& aabb, const Matrix4x4& mat);
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. /**
  53. * Recomputes the OBB after an arbitrary transform by a 4x4 matrix.
  54. * \param mtx [in] the transform matrix
  55. * \param obb [out] the transformed OBB
  56. */
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. inline_ void Rotate(const Matrix4x4& mtx, OBB& obb) const
  59. {
  60. // The extents remain constant
  61. obb.mExtents = mExtents;
  62. // The center gets x-formed
  63. obb.mCenter = mCenter * mtx;
  64. // Combine rotations
  65. obb.mRot = mRot * Matrix3x3(mtx);
  66. }
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. /**
  69. * Checks the OBB is valid.
  70. * \return true if the box is valid
  71. */
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. inline_ BOOL IsValid() const
  74. {
  75. // Consistency condition for (Center, Extents) boxes: Extents >= 0.0f
  76. if(mExtents.x < 0.0f) return FALSE;
  77. if(mExtents.y < 0.0f) return FALSE;
  78. if(mExtents.z < 0.0f) return FALSE;
  79. return TRUE;
  80. }
  81. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. /**
  83. * Computes the obb planes.
  84. * \param planes [out] 6 box planes
  85. * \return true if success
  86. */
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. bool ComputePlanes(Plane* planes) const;
  89. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. /**
  91. * Computes the obb points.
  92. * \param pts [out] 8 box points
  93. * \return true if success
  94. */
  95. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  96. bool ComputePoints(Point* pts) const;
  97. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  98. /**
  99. * Computes vertex normals.
  100. * \param pts [out] 8 box points
  101. * \return true if success
  102. */
  103. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. bool ComputeVertexNormals(Point* pts) const;
  105. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. /**
  107. * Returns edges.
  108. * \return 24 indices (12 edges) indexing the list returned by ComputePoints()
  109. */
  110. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  111. const udword* GetEdges() const;
  112. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. /**
  114. * Returns local edge normals.
  115. * \return edge normals in local space
  116. */
  117. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. const Point* GetLocalEdgeNormals() const;
  119. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  120. /**
  121. * Returns world edge normal
  122. * \param edge_index [in] 0 <= edge index < 12
  123. * \param world_normal [out] edge normal in world space
  124. */
  125. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  126. void ComputeWorldEdgeNormal(udword edge_index, Point& world_normal) const;
  127. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. /**
  129. * Computes an LSS surrounding the OBB.
  130. * \param lss [out] the LSS
  131. */
  132. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. void ComputeLSS(LSS& lss) const;
  134. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. /**
  136. * Checks the OBB is inside another OBB.
  137. * \param box [in] the other OBB
  138. * \return TRUE if we're inside the other box
  139. */
  140. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. BOOL IsInside(const OBB& box) const;
  142. inline_ const Point& GetCenter() const { return mCenter; }
  143. inline_ const Point& GetExtents() const { return mExtents; }
  144. inline_ const Matrix3x3& GetRot() const { return mRot; }
  145. inline_ void GetRotatedExtents(Matrix3x3& extents) const
  146. {
  147. extents = mRot;
  148. extents.Scale(mExtents);
  149. }
  150. Point mCenter; //!< B for Box
  151. Point mExtents; //!< B for Bounding
  152. Matrix3x3 mRot; //!< O for Oriented
  153. // Orientation is stored in row-major format,
  154. // i.e. rows = eigen vectors of the covariance matrix
  155. };
  156. #endif // __ICEOBB_H__