floodfillgrid.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 6/16/00 11:51a $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "floodfillgrid.h"
  36. //#include "utils.h"
  37. #include "aabox.h"
  38. #include "colmath.h"
  39. #include "colmathaabox.h"
  40. ///////////////////////////////////////////////////////////////////////
  41. //
  42. // FloodfillGridClass
  43. //
  44. ///////////////////////////////////////////////////////////////////////
  45. FloodfillGridClass::FloodfillGridClass (void)
  46. : m_Grid (NULL),
  47. m_BoxExtent (0, 0, 0),
  48. m_CellSize (0, 0),
  49. m_CellsX (0),
  50. m_CellsY (0),
  51. m_WorldMin (0, 0),
  52. m_WorldMax (0, 0)
  53. {
  54. return ;
  55. }
  56. ///////////////////////////////////////////////////////////////////////
  57. //
  58. // ~FloodfillGridClass
  59. //
  60. ///////////////////////////////////////////////////////////////////////
  61. FloodfillGridClass::~FloodfillGridClass (void)
  62. {
  63. Reset ();
  64. return ;
  65. }
  66. ///////////////////////////////////////////////////////////////////////
  67. //
  68. // Reset
  69. //
  70. ///////////////////////////////////////////////////////////////////////
  71. void
  72. FloodfillGridClass::Reset (void)
  73. {
  74. if (m_Grid != NULL) {
  75. delete [] m_Grid;
  76. m_Grid = NULL;
  77. }
  78. m_CellsX = 0;
  79. m_CellsY = 0;
  80. m_CellSize.X = 0;
  81. m_CellSize.Y = 0;
  82. return ;
  83. }
  84. ///////////////////////////////////////////////////////////////////////
  85. //
  86. // Initialize
  87. //
  88. ///////////////////////////////////////////////////////////////////////
  89. void
  90. FloodfillGridClass::Initialize
  91. (
  92. const Vector3 &box_extents,
  93. const Vector3 &min_extents,
  94. const Vector3 &max_extents
  95. )
  96. {
  97. Reset ();
  98. m_BoxExtent = box_extents;
  99. m_WorldMin.X = min_extents.X;
  100. m_WorldMin.Y = min_extents.Y;
  101. m_WorldMax.X = max_extents.X;
  102. m_WorldMax.Y = max_extents.Y;
  103. //
  104. // Modify the min and max so they are multiples of the box extents
  105. //
  106. Vector3 box_size = box_extents * 2.0F;
  107. m_WorldMin.X = (int(m_WorldMin.X / box_size.X) * box_size.X) - box_extents.X;
  108. m_WorldMin.Y = (int(m_WorldMin.Y / box_size.Y) * box_size.Y) - box_extents.Y;
  109. m_WorldMax.X = (int(m_WorldMax.X / box_size.X) * box_size.X) + box_extents.X;
  110. m_WorldMax.Y = (int(m_WorldMax.Y / box_size.Y) * box_size.Y) + box_extents.Y;
  111. //
  112. // Calcuate the grid dimensions
  113. //
  114. m_CellSize.X = box_extents.X * 32;
  115. m_CellSize.Y = box_extents.Y * 32;
  116. m_CellsX = ((m_WorldMax.X - m_WorldMin.X) / m_CellSize.X) + 1;
  117. m_CellsY = ((m_WorldMax.Y - m_WorldMin.Y) / m_CellSize.Y) + 1;
  118. //
  119. // Allocate the grid
  120. //
  121. m_Grid = new FloodfillBoxClass *[m_CellsX * m_CellsY];
  122. ::memset (m_Grid, 0, sizeof (FloodfillBoxClass *) * m_CellsX * m_CellsY);
  123. return ;
  124. }
  125. ///////////////////////////////////////////////////////////////////////
  126. //
  127. // Collect_Boxes
  128. //
  129. ///////////////////////////////////////////////////////////////////////
  130. void
  131. FloodfillGridClass::Collect_Boxes (const AABoxClass &vol)
  132. {
  133. int min_cell_x = 0;
  134. int min_cell_y = 0;
  135. int max_cell_x = 0;
  136. int max_cell_y = 0;
  137. Point_To_Cell ((vol.Center - vol.Extent), &min_cell_x, &min_cell_y);
  138. Point_To_Cell ((vol.Center + vol.Extent), &max_cell_x, &max_cell_y);
  139. AABoxClass bounding_box;
  140. bounding_box.Extent = m_BoxExtent;
  141. m_CollectionList.Delete_All ();
  142. //
  143. // Loop over all the cells this volume touches
  144. //
  145. for (int cell_y = min_cell_y; cell_y <= max_cell_y; cell_y ++) {
  146. for (int cell_x = min_cell_x; cell_x <= max_cell_x; cell_x ++) {
  147. //
  148. // Loop over all the objects in this cell
  149. //
  150. FloodfillBoxClass *curr_box = m_Grid[cell_y * m_CellsX + cell_x];
  151. for (; curr_box != NULL; curr_box = curr_box->Get_Grid_Link ()) {
  152. bounding_box.Center = curr_box->Get_Position ();
  153. //
  154. // Does this box overlap the collection volume?
  155. //
  156. if (CollisionMath::Overlap_Test (vol, bounding_box) != CollisionMath::OUTSIDE) {
  157. m_CollectionList.Add (curr_box);
  158. }
  159. }
  160. }
  161. }
  162. return ;
  163. }
  164. ///////////////////////////////////////////////////////////////////////
  165. //
  166. // Find_Box
  167. //
  168. ///////////////////////////////////////////////////////////////////////
  169. FloodfillBoxClass *
  170. FloodfillGridClass::Find_Box (const Vector3 &pos)
  171. {
  172. int index = Get_Cell_Index (pos);
  173. FloodfillBoxClass *box = NULL;
  174. AABoxClass bounding_box;
  175. bounding_box.Extent = m_BoxExtent;
  176. //
  177. // Loop over all the objects in this cell (looking for one inside the given point)
  178. //
  179. for ( FloodfillBoxClass *curr_box = m_Grid[index];
  180. curr_box != NULL && box == NULL;
  181. curr_box = curr_box->Get_Grid_Link ())
  182. {
  183. bounding_box.Center = curr_box->Get_Position ();
  184. //
  185. // Is the point inside this box?
  186. //
  187. if ( (WWMath::Fabs(pos.X - bounding_box.Center.X) <= bounding_box.Extent.X) &&
  188. (WWMath::Fabs(pos.Y - bounding_box.Center.Y) <= bounding_box.Extent.Y) &&
  189. (WWMath::Fabs(pos.Z - bounding_box.Center.Z) <= bounding_box.Extent.Z))
  190. {
  191. box = curr_box;
  192. }
  193. }
  194. return box;
  195. }
  196. ///////////////////////////////////////////////////////////////////////
  197. //
  198. // Convert_To_AABox - converts a floodfill box into an AABox
  199. //
  200. ///////////////////////////////////////////////////////////////////////
  201. AABoxClass
  202. FloodfillGridClass::Convert_To_AABox(FloodfillBoxClass * floodbox)
  203. {
  204. return AABoxClass(floodbox->Get_Position(),m_BoxExtent);
  205. }
  206. ///////////////////////////////////////////////////////////////////////
  207. //
  208. // Compute_Box_Count - counts the number of boxes that overlap into
  209. // the specified AABox
  210. //
  211. ///////////////////////////////////////////////////////////////////////
  212. int
  213. FloodfillGridClass::Compute_Box_Count(const AABoxClass & vol)
  214. {
  215. int min_cell_x = 0;
  216. int min_cell_y = 0;
  217. int max_cell_x = 0;
  218. int max_cell_y = 0;
  219. Point_To_Cell ((vol.Center - vol.Extent), &min_cell_x, &min_cell_y);
  220. Point_To_Cell ((vol.Center + vol.Extent), &max_cell_x, &max_cell_y);
  221. AABoxClass bounding_box;
  222. bounding_box.Extent = m_BoxExtent;
  223. int count = 0;
  224. //
  225. // Loop over all the cells this volume touches
  226. //
  227. for (int cell_y = min_cell_y; cell_y <= max_cell_y; cell_y ++) {
  228. for (int cell_x = min_cell_x; cell_x <= max_cell_x; cell_x ++) {
  229. //
  230. // Loop over all the objects in this cell
  231. //
  232. FloodfillBoxClass *curr_box = m_Grid[cell_y * m_CellsX + cell_x];
  233. for (; curr_box != NULL; curr_box = curr_box->Get_Grid_Link ()) {
  234. bounding_box.Center = curr_box->Get_Position ();
  235. //
  236. // Does this box overlap the collection volume?
  237. //
  238. if (CollisionMath::Overlap_Test (vol, bounding_box) != CollisionMath::OUTSIDE) {
  239. count++;
  240. }
  241. }
  242. }
  243. }
  244. return count;
  245. }