Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.8 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =====//
  2. //
  3. // Dme version of an axis aligned bounding box
  4. //
  5. //===========================================================================//
  6. // Valve includes
  7. #include "datamodel/dmelementfactoryhelper.h"
  8. #include "mdlobjects/dmebbox.h"
  9. #include "mathlib/mathlib.h"
  10. #include "tier2/renderutils.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. //-----------------------------------------------------------------------------
  14. // Expose this class to the scene database
  15. //-----------------------------------------------------------------------------
  16. IMPLEMENT_ELEMENT_FACTORY( DmeBBox, CDmeBBox );
  17. //-----------------------------------------------------------------------------
  18. // Purpose:
  19. //-----------------------------------------------------------------------------
  20. void CDmeBBox::OnConstruction()
  21. {
  22. Vector vMin;
  23. Vector vMax;
  24. ClearBounds( vMin, vMax );
  25. m_vMinBounds.InitAndSet( this, "minBounds", vMin );
  26. m_vMaxBounds.InitAndSet( this, "maxBounds", vMax );
  27. }
  28. //-----------------------------------------------------------------------------
  29. //
  30. //-----------------------------------------------------------------------------
  31. void CDmeBBox::OnDestruction()
  32. {
  33. }
  34. //-----------------------------------------------------------------------------
  35. //
  36. //-----------------------------------------------------------------------------
  37. void CDmeBBox::Clear()
  38. {
  39. Vector vMin;
  40. Vector vMax;
  41. ClearBounds( vMin, vMax );
  42. m_vMinBounds = vMin;
  43. m_vMaxBounds = vMax;
  44. }
  45. //-----------------------------------------------------------------------------
  46. //
  47. //-----------------------------------------------------------------------------
  48. bool CDmeBBox::Empty() const
  49. {
  50. const Vector &vMin = m_vMinBounds.Get();
  51. const Vector &vMax = m_vMaxBounds.Get();
  52. return vMin.x > vMax.x || vMin.y > vMax.y || vMin.z > vMax.z;
  53. }
  54. //-----------------------------------------------------------------------------
  55. //
  56. //-----------------------------------------------------------------------------
  57. void CDmeBBox::TransformUsing( const matrix3x4_t &mMatrix )
  58. {
  59. Vector vMin;
  60. Vector vMax;
  61. TransformAABB( mMatrix, m_vMinBounds, m_vMaxBounds, vMin, vMax );
  62. m_vMinBounds = vMin;
  63. m_vMaxBounds = vMax;
  64. }
  65. //-----------------------------------------------------------------------------
  66. //
  67. //-----------------------------------------------------------------------------
  68. void CDmeBBox::Expand( const Vector &vPoint )
  69. {
  70. Vector vMin = m_vMinBounds;
  71. Vector vMax = m_vMaxBounds;
  72. AddPointToBounds( vPoint, vMin, vMax );
  73. m_vMinBounds = vMin;
  74. m_vMaxBounds = vMax;
  75. }
  76. //-----------------------------------------------------------------------------
  77. //
  78. //-----------------------------------------------------------------------------
  79. void CDmeBBox::Expand( const CDmeBBox &bbox )
  80. {
  81. Expand( bbox.m_vMinBounds );
  82. Expand( bbox.m_vMaxBounds );
  83. }
  84. //-----------------------------------------------------------------------------
  85. //
  86. //-----------------------------------------------------------------------------
  87. bool CDmeBBox::Contains( const Vector &vPoint ) const
  88. {
  89. const Vector &vMin = m_vMinBounds.Get();
  90. const Vector &vMax = m_vMaxBounds.Get();
  91. return
  92. vPoint.x >= vMin.x && vPoint.x <= vMax.x &&
  93. vPoint.y >= vMin.y && vPoint.y <= vMax.y &&
  94. vPoint.z >= vMin.z && vPoint.z <= vMax.z;
  95. }
  96. //-----------------------------------------------------------------------------
  97. //
  98. //-----------------------------------------------------------------------------
  99. bool CDmeBBox::Intersects( const CDmeBBox &bbox ) const
  100. {
  101. return QuickBoxIntersectTest( m_vMinBounds, m_vMaxBounds, bbox.m_vMinBounds, bbox.m_vMaxBounds );
  102. }
  103. //-----------------------------------------------------------------------------
  104. //
  105. //-----------------------------------------------------------------------------
  106. float CDmeBBox::Width() const
  107. {
  108. const float flWidth = m_vMaxBounds.Get().x - m_vMinBounds.Get().x;
  109. return flWidth > 0.0f ? flWidth : 0.0f;
  110. }
  111. //-----------------------------------------------------------------------------
  112. //
  113. //-----------------------------------------------------------------------------
  114. float CDmeBBox::Height() const
  115. {
  116. const float flHeight = m_vMaxBounds.Get().y - m_vMinBounds.Get().y;
  117. return flHeight > 0.0f ? flHeight : 0.0f;
  118. }
  119. //-----------------------------------------------------------------------------
  120. //
  121. //-----------------------------------------------------------------------------
  122. float CDmeBBox::Depth() const
  123. {
  124. const float flDepth = m_vMaxBounds.Get().z - m_vMinBounds.Get().z;
  125. return flDepth > 0.0f ? flDepth : 0.0f;
  126. }
  127. //-----------------------------------------------------------------------------
  128. //
  129. //-----------------------------------------------------------------------------
  130. Vector CDmeBBox::Center() const
  131. {
  132. const Vector &vMin = m_vMinBounds.Get();
  133. const Vector &vMax = m_vMaxBounds.Get();
  134. return Vector(
  135. ( vMax.x + vMin.x ) / 2.0f,
  136. ( vMax.y + vMin.y ) / 2.0f,
  137. ( vMax.z + vMin.z ) / 2.0f );
  138. }
  139. //-----------------------------------------------------------------------------
  140. //
  141. //-----------------------------------------------------------------------------
  142. const Vector &CDmeBBox::Min() const
  143. {
  144. return m_vMinBounds.Get();
  145. }
  146. //-----------------------------------------------------------------------------
  147. //
  148. //-----------------------------------------------------------------------------
  149. const Vector &CDmeBBox::Max() const
  150. {
  151. return m_vMaxBounds.Get();
  152. }
  153. //-----------------------------------------------------------------------------
  154. //
  155. //-----------------------------------------------------------------------------
  156. void CDmeBBox::Draw( const matrix3x4_t &shapeToWorld, CDmeDrawSettings *pDrawSettings /*= NULL */ )
  157. {
  158. static const Color cRenderColor( 0, 192, 0 );
  159. Vector vOrigin;
  160. QAngle angles;
  161. MatrixAngles( shapeToWorld, angles, vOrigin );
  162. RenderBox( vOrigin, angles, m_vMinBounds, m_vMaxBounds, cRenderColor, true );
  163. }