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.

145 lines
5.6 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //
  8. // The BSP tree leaf data system
  9. //
  10. //=============================================================================//
  11. #include "tier0/platform.h"
  12. #if !defined( BSPTREEDATA )
  13. #define BSPTREEDATA
  14. #ifdef _WIN32
  15. #pragma once
  16. #endif
  17. //-----------------------------------------------------------------------------
  18. // Forward declarations
  19. //-----------------------------------------------------------------------------
  20. class Vector;
  21. struct Ray_t;
  22. //-----------------------------------------------------------------------------
  23. // Handle to an renderable in the client leaf system
  24. //-----------------------------------------------------------------------------
  25. typedef unsigned short BSPTreeDataHandle_t;
  26. enum
  27. {
  28. TREEDATA_INVALID_HANDLE = (BSPTreeDataHandle_t)~0
  29. };
  30. //-----------------------------------------------------------------------------
  31. // Interface needed by tree data to do its job
  32. //
  33. // Note that anything that has convex spatial regions with leaves identified
  34. // by indices can implement the ISpatialQuery. All you have to do is to implement
  35. // a class that can answer the 5 questions in the Query interface about the
  36. // spatial subdivision. For example, a K-D tree or a BSP tree could implement
  37. // this interface
  38. //
  39. //-----------------------------------------------------------------------------
  40. abstract_class ISpatialLeafEnumerator
  41. {
  42. public:
  43. // call back with a leaf and a context
  44. // The context is completely user defined; it's passed into the enumeration
  45. // function of ISpatialQuery.
  46. // This gets called by the enumeration methods with each leaf
  47. // that passes the test; return true to continue enumerating,
  48. // false to stop
  49. virtual bool EnumerateLeaf( int leaf, intp context ) = 0;
  50. };
  51. abstract_class ISpatialQuery
  52. {
  53. public:
  54. // Returns the number of leaves
  55. virtual int LeafCount() const = 0;
  56. // Enumerates the leaves along a ray, box, etc.
  57. virtual bool EnumerateLeavesAtPoint( const Vector& pt, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  58. virtual bool EnumerateLeavesInBox( const Vector& mins, const Vector& maxs, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  59. virtual bool EnumerateLeavesInSphere( const Vector& center, float radius, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  60. virtual bool EnumerateLeavesAlongRay( Ray_t const& ray, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  61. virtual bool EnumerateLeavesInSphereWithFlagSet( const Vector& center, float radius, ISpatialLeafEnumerator* pEnum, intp context, int nFlagsCheck ) = 0;
  62. virtual int ListLeavesInBox( const Vector& mins, const Vector& maxs, unsigned short *pList, int listMax ) = 0;
  63. // Used to determine which leaves passed in (specified by leafcount, pLeafs, and nLeafStride )
  64. // are within radius flRadius of vecCenter and have the flag set.
  65. // The result is placed in the pLeafsInSphere array, which specifies _indices_ into the original pLeafs array
  66. // The number of leaves found within the sphere is the return value.
  67. // The caller is expected to have allocated at least nLeafCount pLeafsInSphere to place the results into
  68. virtual int ListLeavesInSphereWithFlagSet( int *pLeafsInSphere, const Vector& vecCenter, float flRadius, int nLeafCount, const uint16 *pLeafs, int nLeafStride = sizeof(uint16), int nFlagsCheck = 0xFFFFFFFF ) = 0;
  69. };
  70. //-----------------------------------------------------------------------------
  71. // Data associated with leaves.
  72. //
  73. // This is a parasitic class that attaches data to the leaves specified by the
  74. // ISpatialQuery sent in to the initialization function. It can't exist without
  75. // a spatial partition of some sort to hold onto.
  76. //-----------------------------------------------------------------------------
  77. abstract_class IBSPTreeDataEnumerator
  78. {
  79. public:
  80. // call back with a userId and a context
  81. virtual bool FASTCALL EnumerateElement( int userId, intp context ) = 0;
  82. };
  83. abstract_class IBSPTreeData
  84. {
  85. public:
  86. // Add a virtual destructor so that the derived class destructors will
  87. // be called.
  88. virtual ~IBSPTreeData() {}
  89. // Initializes, shuts down
  90. virtual void Init( ISpatialQuery* pBSPTree ) = 0;
  91. virtual void Shutdown() = 0;
  92. // Adds and removes data from the leaf lists
  93. virtual BSPTreeDataHandle_t Insert( int userId, const Vector& mins, const Vector& maxs ) = 0;
  94. virtual void Remove( BSPTreeDataHandle_t handle ) = 0;
  95. // Call this when a element moves
  96. virtual void ElementMoved( BSPTreeDataHandle_t handle, const Vector& mins, const Vector& maxs ) = 0;
  97. // Enumerate elements in a particular leaf
  98. virtual bool EnumerateElementsInLeaf( int leaf, IBSPTreeDataEnumerator* pEnum, intp context ) = 0;
  99. // Is the element in any leaves at all?
  100. virtual bool IsElementInTree( BSPTreeDataHandle_t handle ) const = 0;
  101. // NOTE: These methods call through to the functions in the attached
  102. // ISpatialQuery
  103. // For convenience, enumerates the leaves along a ray, box, etc.
  104. virtual bool EnumerateLeavesAtPoint( const Vector& pt, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  105. virtual bool EnumerateLeavesInBox( const Vector& mins, const Vector& maxs, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  106. virtual bool EnumerateLeavesInSphere( const Vector& center, float radius, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  107. virtual bool EnumerateLeavesAlongRay( Ray_t const& ray, ISpatialLeafEnumerator* pEnum, intp context ) = 0;
  108. };
  109. //-----------------------------------------------------------------------------
  110. // Class factory
  111. //-----------------------------------------------------------------------------
  112. IBSPTreeData* CreateBSPTreeData();
  113. void DestroyBSPTreeData( IBSPTreeData* pTreeData );
  114. #endif // BSPTREEDATA