floodfillgrid.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/floodfillgrid.h $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 6/16/00 11:51a $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef __FLOODFILLGRID_H
  39. #define __FLOODFILLGRID_H
  40. #include "vector2.h"
  41. #include "vector2i.h"
  42. #include "vector3.h"
  43. #include "floodfillbox.h"
  44. #include "wwdebug.h"
  45. ///////////////////////////////////////////////////////////////////////
  46. // Forward declarations
  47. ///////////////////////////////////////////////////////////////////////
  48. class AABoxClass;
  49. ///////////////////////////////////////////////////////////////////////
  50. //
  51. // FloodfillGridClass
  52. //
  53. ///////////////////////////////////////////////////////////////////////
  54. class FloodfillGridClass
  55. {
  56. public:
  57. ////////////////////////////////////////////////////////////////////
  58. // Public constructors/destructors
  59. ////////////////////////////////////////////////////////////////////
  60. FloodfillGridClass (void);
  61. virtual ~FloodfillGridClass (void);
  62. ////////////////////////////////////////////////////////////////////
  63. // Public methods
  64. ////////////////////////////////////////////////////////////////////
  65. //
  66. // Initialization methods
  67. //
  68. void Initialize (const Vector3 &box_extents, const Vector3 &min_extents, const Vector3 &max_extents);
  69. //
  70. // Insertion/removal methods
  71. //
  72. void Add_Box (FloodfillBoxClass *box);
  73. bool Remove_Box (FloodfillBoxClass *box);
  74. void Reset (void);
  75. //
  76. // Collection methods
  77. //
  78. void Collect_Boxes (const AABoxClass &vol);
  79. BODY_BOX_LIST & Get_Collection_List (void);
  80. FloodfillBoxClass * Find_Box (const Vector3 &pos);
  81. int Compute_Box_Count(const AABoxClass & vol);
  82. //
  83. // Accessors
  84. //
  85. Vector3 Get_Floodfill_Box_Extents(void);
  86. AABoxClass Convert_To_AABox(FloodfillBoxClass * floodbox);
  87. protected:
  88. ////////////////////////////////////////////////////////////////////
  89. // Protected methods
  90. ////////////////////////////////////////////////////////////////////
  91. int Get_Cell_Index (const Vector3 &pos);
  92. void Point_To_Cell (const Vector3 &pos, int *cell_x, int *cell_y);
  93. private:
  94. ////////////////////////////////////////////////////////////////////
  95. // Private member data
  96. ////////////////////////////////////////////////////////////////////
  97. FloodfillBoxClass ** m_Grid;
  98. Vector3 m_BoxExtent;
  99. Vector2 m_CellSize;
  100. Vector2 m_WorldMin;
  101. Vector2 m_WorldMax;
  102. int m_CellsX;
  103. int m_CellsY;
  104. BODY_BOX_LIST m_CollectionList;
  105. };
  106. ////////////////////////////////////////////////////////////////////
  107. // Get_Collection_List
  108. ////////////////////////////////////////////////////////////////////
  109. inline BODY_BOX_LIST &
  110. FloodfillGridClass::Get_Collection_List (void)
  111. {
  112. return m_CollectionList;
  113. }
  114. ////////////////////////////////////////////////////////////////////
  115. // Get_Floodfill_Box_Size
  116. ////////////////////////////////////////////////////////////////////
  117. inline Vector3 FloodfillGridClass::Get_Floodfill_Box_Extents(void)
  118. {
  119. return m_BoxExtent;
  120. }
  121. ////////////////////////////////////////////////////////////////////
  122. // Point_To_Cell
  123. ////////////////////////////////////////////////////////////////////
  124. inline void
  125. FloodfillGridClass::Point_To_Cell (const Vector3 &pos, int *cell_x, int *cell_y)
  126. {
  127. //
  128. // Convert from 'world-coords' to 'grid-coords'
  129. //
  130. float cell_x_pos = pos.X - m_WorldMin.X;
  131. float cell_y_pos = pos.Y - m_WorldMin.Y;
  132. //
  133. // Calculate cell indicies from the coordinates
  134. //
  135. (*cell_x) = int(cell_x_pos / m_CellSize.X);
  136. (*cell_y) = int(cell_y_pos / m_CellSize.Y);
  137. //
  138. // Clamp the cells to the bounds
  139. //
  140. (*cell_x) = max ((*cell_x), 0);
  141. (*cell_y) = max ((*cell_y), 0);
  142. (*cell_x) = min ((*cell_x), m_CellsX - 1);
  143. (*cell_y) = min ((*cell_y), m_CellsY - 1);
  144. return;
  145. }
  146. ////////////////////////////////////////////////////////////////////
  147. // Get_Cell_Index
  148. ////////////////////////////////////////////////////////////////////
  149. inline int
  150. FloodfillGridClass::Get_Cell_Index (const Vector3 &pos)
  151. {
  152. int cell_x = 0;
  153. int cell_y = 0;
  154. Point_To_Cell (pos, &cell_x, &cell_y);
  155. /*int min_x = 0;
  156. int min_y = 0;
  157. Point_To_Cell (pos - (m_BoxExtent-Vector3 (0.075F, 0.075F, 0.075F)), &min_x, &min_y);
  158. int max_x = 0;
  159. int max_y = 0;
  160. Point_To_Cell (pos + (m_BoxExtent-Vector3 (0.075F, 0.075F, 0.075F)), &max_x, &max_y);
  161. WWASSERT (cell_x == min_x && cell_x == max_x);
  162. WWASSERT (cell_y == min_y && cell_y == max_y);*/
  163. return (cell_y * m_CellsX) + cell_x;
  164. }
  165. ////////////////////////////////////////////////////////////////////
  166. // Add_Box
  167. ////////////////////////////////////////////////////////////////////
  168. inline void
  169. FloodfillGridClass::Add_Box (FloodfillBoxClass *box)
  170. {
  171. //
  172. // Calculate what cell this box should live in
  173. //
  174. int index = Get_Cell_Index (box->Get_Position ());
  175. WWASSERT (index >= 0 && index < m_CellsX * m_CellsY);
  176. //
  177. // Link the box into the system at the given index
  178. //
  179. FloodfillBoxClass *cell_contents = m_Grid[index];
  180. m_Grid[index] = box;
  181. box->Set_Grid_Link (cell_contents);
  182. return ;
  183. }
  184. ////////////////////////////////////////////////////////////////////
  185. // Remove_Box
  186. ////////////////////////////////////////////////////////////////////
  187. inline bool
  188. FloodfillGridClass::Remove_Box (FloodfillBoxClass *box)
  189. {
  190. //
  191. // Calculate what cell this box lives in
  192. //
  193. int index = Get_Cell_Index (box->Get_Position ());
  194. WWASSERT (index >= 0 && index < m_CellsX * m_CellsY);
  195. //
  196. // Attempt to find the box in this grid
  197. //
  198. bool found = false;
  199. FloodfillBoxClass *curr_box = NULL;
  200. FloodfillBoxClass *prev_box = NULL;
  201. for ( curr_box = m_Grid[index];
  202. curr_box != NULL && !found;
  203. curr_box = curr_box->Get_Grid_Link ())
  204. {
  205. //
  206. // Is this the box we are looking for?
  207. //
  208. if (box == curr_box) {
  209. //
  210. // Unlink this box from the grid
  211. //
  212. if (prev_box != NULL) {
  213. prev_box->Set_Grid_Link (box->Get_Grid_Link ());
  214. } else {
  215. m_Grid[index] = box->Get_Grid_Link ();
  216. }
  217. box->Set_Grid_Link (NULL);
  218. found = true;
  219. }
  220. prev_box = curr_box;
  221. }
  222. return found;
  223. }
  224. #endif //__FLOODFILLGRID_H