Grid3D.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/Tools/LevelEdit/Grid3D.h $Modtime:: $*
  25. * *
  26. * $Revision:: 2 $*
  27. * *
  28. *---------------------------------------------------------------------------------------------*
  29. * Functions: *
  30. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  31. #if defined(_MSC_VER)
  32. #pragma once
  33. #endif
  34. #ifndef __GRID_3D_H
  35. #define __GRID_3D_H
  36. #include "Vector3.H"
  37. #include "WWDebug.H"
  38. //////////////////////////////////////////////////////////////////////////
  39. //
  40. // Grid3DClass
  41. //
  42. //////////////////////////////////////////////////////////////////////////
  43. template<class T>
  44. class Grid3DClass
  45. {
  46. public:
  47. ////////////////////////////////////////////////////////////////////
  48. // Public constructors/destructors
  49. ////////////////////////////////////////////////////////////////////
  50. Grid3DClass (void)
  51. : m_GridSize (0, 0, 0),
  52. m_Grid (NULL) { }
  53. Grid3DClass (const Vector3 &grid_size, const T &inital_value)
  54. : m_GridSize (0, 0, 0),
  55. m_Grid (NULL) { Create_Grid (grid_size, inital_value); }
  56. virtual ~Grid3DClass (void) { Free_Grid (); }
  57. ////////////////////////////////////////////////////////////////////
  58. // Public methods
  59. ////////////////////////////////////////////////////////////////////
  60. // Cell manipulation
  61. T & Get_At (int x, int y, int z);
  62. void Set_At (int x, int y, int z, const T &value);
  63. // Allocation routines
  64. void Create_Grid (const Vector3 &grid_size, const T &initial_value);
  65. void Free_Grid (void);
  66. // Information methods
  67. Vector3 Get_Grid_Size (void) const { return m_GridSize; }
  68. int Get_Cells_X (void) const { return m_GridSize.X; }
  69. int Get_Cells_Y (void) const { return m_GridSize.Y; }
  70. int Get_Cells_Z (void) const { return m_GridSize.Z; }
  71. // 'Flat' methods, for enumerating every cell
  72. T & Get_At_Flat (int index) { return m_Grid[index]; }
  73. int Get_Flat_Size (void) const { return (m_GridSize.X * m_GridSize.Y * m_GridSize.Z); }
  74. protected:
  75. ////////////////////////////////////////////////////////////////////
  76. // Protected methods
  77. ////////////////////////////////////////////////////////////////////
  78. virtual int Cell_Coord_To_Index (int x, int y, int z);
  79. private:
  80. ////////////////////////////////////////////////////////////////////
  81. // Private member data
  82. ////////////////////////////////////////////////////////////////////
  83. T * m_Grid;
  84. Vector3 m_GridSize;
  85. };
  86. //////////////////////////////////////////////////////////////////////////
  87. //
  88. // Create_Grid
  89. //
  90. //////////////////////////////////////////////////////////////////////////
  91. template<class T>
  92. inline void
  93. Grid3DClass<T>::Create_Grid (const Vector3 &grid_size, const T &inital_value)
  94. {
  95. // Start fresh
  96. Free_Grid ();
  97. // Store the grid's dimensions
  98. m_GridSize = grid_size;
  99. //
  100. // Allocate a flat array we can index into like a 3-D grid
  101. //
  102. int flat_size = m_GridSize.X * m_GridSize.Y * m_GridSize.Z;
  103. WWASSERT ((flat_size * sizeof (T)) < 16000000L);
  104. m_Grid = new T[flat_size];
  105. //
  106. // Initialize the grid cells
  107. //
  108. for (int index = 0; index < flat_size; index ++) {
  109. m_Grid[index] = inital_value;
  110. }
  111. return ;
  112. }
  113. //////////////////////////////////////////////////////////////////////////
  114. //
  115. // Free_Grid
  116. //
  117. //////////////////////////////////////////////////////////////////////////
  118. template<class T>
  119. inline void
  120. Grid3DClass<T>::Free_Grid (void)
  121. {
  122. if (m_Grid != NULL) {
  123. delete m_Grid;
  124. m_Grid = NULL;
  125. }
  126. m_GridSize.Set (0, 0, 0);
  127. return ;
  128. }
  129. //////////////////////////////////////////////////////////////////////////
  130. //
  131. // Cell_Coord_To_Index
  132. //
  133. //////////////////////////////////////////////////////////////////////////
  134. template<class T>
  135. inline int
  136. Grid3DClass<T>::Cell_Coord_To_Index (int x, int y, int z)
  137. {
  138. return (x + (y * m_GridSize.X) + (z * m_GridSize.X * m_GridSize.Y));
  139. }
  140. //////////////////////////////////////////////////////////////////////////
  141. //
  142. // Get_At
  143. //
  144. //////////////////////////////////////////////////////////////////////////
  145. template<class T>
  146. inline T &
  147. Grid3DClass<T>::Get_At (int x, int y, int z)
  148. {
  149. WWASSERT (m_Grid != NULL);
  150. WWASSERT ((x < m_GridSize.X) && (y < m_GridSize.Y) && (z < m_GridSize.Z));
  151. return m_Grid[Cell_Coord_To_Index (x, y, z)];
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. //
  155. // Set_At
  156. //
  157. //////////////////////////////////////////////////////////////////////////
  158. template<class T>
  159. inline void
  160. Grid3DClass<T>::Set_At (int x, int y, int z, const T &value)
  161. {
  162. WWASSERT (m_Grid != NULL);
  163. WWASSERT ((x < m_GridSize.X) && (y < m_GridSize.Y) && (z < m_GridSize.Z));
  164. m_Grid[Cell_Coord_To_Index (x, y, z)] = value;
  165. return ;
  166. }
  167. #endif //__GRID_3D_H