aabtreecull.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. ** Command & Conquer Generals(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 : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/aabtreecull.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 7/24/01 10:00a $*
  29. * *
  30. * $Revision:: 15 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef AABTREECULL_H
  39. #define AABTREECULL_H
  40. #include "cullsys.h"
  41. #include "aaplane.h"
  42. #include "wwmath.h"
  43. #include "mempool.h"
  44. #include "simplevec.h"
  45. #include <math.h>
  46. #include <float.h>
  47. class AABTreeNodeClass;
  48. class ChunkLoadClass;
  49. class ChunkSaveClass;
  50. class SphereClass;
  51. /**
  52. ** AABTreeCullSystemClass
  53. ** Derived culling system that uses an Axis-Aligned Bounding Box Tree
  54. */
  55. class AABTreeCullSystemClass : public CullSystemClass
  56. {
  57. public:
  58. AABTreeCullSystemClass(void);
  59. virtual ~AABTreeCullSystemClass(void);
  60. /*
  61. ** Re-partition the tree. Two methods can be used to accomplish this. The
  62. ** first re-partitions the tree based on the objects contained within, the second
  63. ** re-partitions the tree based solely on a set of input "seed" boxes. Each seed
  64. ** box will become a leaf; then the objects will be re-inserted in the new tree.
  65. */
  66. void Re_Partition(void);
  67. void Re_Partition(const AABoxClass & bounds,SimpleDynVecClass<AABoxClass> & boxes);
  68. /*
  69. ** Update_Bounding_Boxes. This function causes all bounding boxes in the tree to update themselves.
  70. ** If any box is found to not bound the objects it is supposed to contain, the box is updated
  71. ** Note that this is normally not necessary, the reason this function existsis due to the fact
  72. ** that the renegade level editor tries to do everything possible to not discard the precalculated
  73. ** visibilty data for a level. In some cases, we want to load geometry that has been edited back
  74. ** into the same AABTree without re-partitioning.
  75. */
  76. void Update_Bounding_Boxes(void);
  77. /*
  78. ** Re-insert an object into the tree
  79. */
  80. virtual void Update_Culling(CullableClass * obj);
  81. /*
  82. ** Statistics about the AAB-Tree
  83. */
  84. int Partition_Node_Count(void) const;
  85. int Partition_Tree_Depth(void) const;
  86. int Object_Count(void) const;
  87. /*
  88. ** Collect objects which overlap the given primitive
  89. */
  90. virtual void Collect_Objects(const Vector3 & point);
  91. virtual void Collect_Objects(const AABoxClass & box);
  92. virtual void Collect_Objects(const OBBoxClass & box);
  93. virtual void Collect_Objects(const FrustumClass & frustum);
  94. virtual void Collect_Objects(const SphereClass & sphere);
  95. /*
  96. ** Load and Save a description of this AAB-Tree and its contents
  97. */
  98. virtual void Load(ChunkLoadClass & cload);
  99. virtual void Save(ChunkSaveClass & csave);
  100. /*
  101. ** Save an objects linkage, load the linkage and re-link the object
  102. */
  103. void Load_Object_Linkage(ChunkLoadClass & cload,CullableClass * obj);
  104. void Save_Object_Linkage(ChunkSaveClass & csave,CullableClass * obj);
  105. /*
  106. ** Bounding box of the entire tree
  107. */
  108. const AABoxClass & Get_Bounding_Box(void);
  109. void Get_Node_Bounds(int node_id,AABoxClass * set_bounds);
  110. /*
  111. ** Statistics
  112. */
  113. struct StatsStruct
  114. {
  115. int NodeCount;
  116. int NodesAccepted;
  117. int NodesTriviallyAccepted;
  118. int NodesRejected;
  119. };
  120. void Reset_Statistics(void);
  121. const StatsStruct & Get_Statistics(void);
  122. protected:
  123. /*
  124. ** Internal stat tracking
  125. */
  126. #ifdef WWDEBUG
  127. void NODE_ACCEPTED(void) { Stats.NodesAccepted ++; }
  128. void NODE_TRIVIALLY_ACCEPTED(void) { Stats.NodesTriviallyAccepted ++; }
  129. void NODE_REJECTED(void) { Stats.NodesRejected ++; }
  130. #else
  131. void NODE_ACCEPTED(void) { }
  132. void NODE_TRIVIALLY_ACCEPTED(void) { }
  133. void NODE_REJECTED(void) { }
  134. #endif
  135. /*
  136. ** Internal functions
  137. */
  138. void Add_Object_Internal(CullableClass * obj,int node_index = -1);
  139. void Remove_Object_Internal(CullableClass * obj);
  140. void Re_Index_Nodes(void);
  141. void Re_Index_Nodes_Recursive(AABTreeNodeClass * node,int & counter);
  142. int Partition_Node_Count_Recursive(AABTreeNodeClass * node) const;
  143. void Partition_Tree_Depth_Recursive(AABTreeNodeClass * node,int cur_depth,int & max_depth) const;
  144. void Add_Object_Recursive(AABTreeNodeClass * node,CullableClass * obj);
  145. void Add_Loaded_Object(AABTreeNodeClass * node,CullableClass * obj);
  146. void Collect_Objects_Recursive(AABTreeNodeClass * node);
  147. void Collect_Objects_Recursive(AABTreeNodeClass * node,const Vector3 & point);
  148. void Collect_Objects_Recursive(AABTreeNodeClass * node,const AABoxClass & box);
  149. void Collect_Objects_Recursive(AABTreeNodeClass * node,const OBBoxClass & box);
  150. void Collect_Objects_Recursive(AABTreeNodeClass * node,const FrustumClass & frustum);
  151. void Collect_Objects_Recursive(AABTreeNodeClass * node,const FrustumClass & frustum,int planes_passed);
  152. void Collect_Objects_Recursive(AABTreeNodeClass * node,const SphereClass & sphere);
  153. void Update_Bounding_Boxes_Recursive(AABTreeNodeClass * node);
  154. void Load_Nodes(AABTreeNodeClass * node,ChunkLoadClass & cload);
  155. void Save_Nodes(AABTreeNodeClass * node,ChunkSaveClass & csave);
  156. virtual void Load_Node_Contents(AABTreeNodeClass * /*node*/,ChunkLoadClass & /*cload*/) { }
  157. virtual void Save_Node_Contents(AABTreeNodeClass * /*node*/,ChunkSaveClass & /*csave*/) { }
  158. AABTreeNodeClass * RootNode; // root of the AAB-Tree
  159. int ObjectCount; // number of objects in the system
  160. int NodeCount; // number of nodes
  161. AABTreeNodeClass ** IndexedNodes; // index access to the nodes
  162. StatsStruct Stats;
  163. friend class AABTreeIterator;
  164. };
  165. /**
  166. ** AABTreeIterator
  167. ** This iterator allows the user to walk a tree. It can return the index of the current
  168. ** node and the bounds of the current node.
  169. */
  170. class AABTreeIterator
  171. {
  172. public:
  173. AABTreeIterator(AABTreeCullSystemClass * tree);
  174. void Reset(void);
  175. bool Enter_Parent(void);
  176. bool Enter_Sibling(void);
  177. bool Has_Front_Child(void);
  178. bool Enter_Front_Child(void);
  179. bool Has_Back_Child(void);
  180. bool Enter_Back_Child(void);
  181. int Get_Current_Node_Index(void);
  182. void Get_Current_Box(AABoxClass * set_box);
  183. private:
  184. void validate(void);
  185. AABTreeCullSystemClass * Tree;
  186. int CurNodeIndex;
  187. };
  188. /**
  189. ** TypedAABTreeCullSystemClass
  190. ** This template adds type-safety to an AABTree. It allows you to create trees
  191. ** containing a particular type of object (the class must be derived from CullableClass though)
  192. */
  193. template <class T> class TypedAABTreeCullSystemClass : public AABTreeCullSystemClass
  194. {
  195. public:
  196. virtual void Add_Object(T * obj,int node_index=-1) { Add_Object_Internal(obj,node_index); }
  197. virtual void Remove_Object(T * obj) { Remove_Object_Internal(obj); }
  198. T * Get_First_Collected_Object(void) { return (T*)Get_First_Collected_Object_Internal(); }
  199. T * Get_Next_Collected_Object(T * obj) { return (T*)Get_Next_Collected_Object_Internal(obj); }
  200. T * Peek_First_Collected_Object(void) { return (T*)Peek_First_Collected_Object_Internal(); }
  201. T * Peek_Next_Collected_Object(T * obj) { return (T*)Peek_Next_Collected_Object_Internal(obj); }
  202. };
  203. /**
  204. ** AABTreeNodeClass - the aab-tree is built out of these objects
  205. ** CullableClass's can be linked into any of these nodes. Whenever the
  206. ** tree is re-built, all objects will end up in the leaf nodes. Between
  207. ** re-builds, as objects are added, they will be placed as deep into the
  208. ** tree as possible.
  209. */
  210. class AABTreeNodeClass : public AutoPoolClass<AABTreeNodeClass,256>
  211. {
  212. public:
  213. AABTreeNodeClass(void);
  214. ~AABTreeNodeClass(void);
  215. void Add_Object(CullableClass * obj,bool update_bounds = true);
  216. void Remove_Object(CullableClass * obj);
  217. int Object_Count(void);
  218. CullableClass * Peek_Object(int index);
  219. uint32 Index; // Index of this node
  220. AABoxClass Box; // Bounding box of the node
  221. AABTreeNodeClass * Parent; // parent of this node
  222. AABTreeNodeClass * Front; // front node
  223. AABTreeNodeClass * Back; // back node
  224. CullableClass * Object; // objects in this node
  225. uint32 UserData; // 32bit field for the user, initialized to 0
  226. /*
  227. ** Construction support:
  228. */
  229. struct SplitChoiceStruct
  230. {
  231. SplitChoiceStruct(void) : Cost(FLT_MAX),FrontCount(0),BackCount(0),Plane(AAPlaneClass::XNORMAL,0.0f)
  232. {
  233. FrontBox.Init_Empty();
  234. BackBox.Init_Empty();
  235. }
  236. float Cost;
  237. int FrontCount;
  238. int BackCount;
  239. MinMaxAABoxClass FrontBox;
  240. MinMaxAABoxClass BackBox;
  241. AAPlaneClass Plane;
  242. };
  243. void Compute_Bounding_Box(void);
  244. void Compute_Local_Bounding_Box(void);
  245. float Compute_Volume(void);
  246. void Transfer_Objects(AABTreeNodeClass * dummy_node);
  247. /*
  248. ** Partition the tree based on the objects contained.
  249. */
  250. void Partition(void);
  251. void Split_Objects( const SplitChoiceStruct & sc,
  252. AABTreeNodeClass * front,
  253. AABTreeNodeClass * back);
  254. /*
  255. ** Partition the tree based on a set of input "seed" boxes.
  256. */
  257. void Partition(const AABoxClass & bounds,SimpleDynVecClass<AABoxClass> & boxes);
  258. void Split_Boxes( const SplitChoiceStruct & sc,
  259. SimpleDynVecClass<AABoxClass> & boxes,
  260. SimpleDynVecClass<AABoxClass> & frontboxes,
  261. SimpleDynVecClass<AABoxClass> & backboxes);
  262. /*
  263. ** Functions used by both partitioning algorithms
  264. */
  265. void Select_Splitting_Plane(SplitChoiceStruct * sc,SimpleDynVecClass<AABoxClass> & boxes);
  266. void Select_Splitting_Plane_Brute_Force(SplitChoiceStruct * sc,SimpleDynVecClass<AABoxClass> & boxes);
  267. void Compute_Score(SplitChoiceStruct * sc,SimpleDynVecClass<AABoxClass> & boxes);
  268. };
  269. /*
  270. ** AABTreeLinkClass
  271. ** This structure is used to link objects into an AAB-Tree culling system.
  272. */
  273. class AABTreeLinkClass : public CullLinkClass, public AutoPoolClass<AABTreeLinkClass,256>
  274. {
  275. public:
  276. AABTreeLinkClass(AABTreeCullSystemClass * system) : CullLinkClass(system),Node(NULL), NextObject(NULL) { }
  277. AABTreeNodeClass * Node; // partition node containing this object
  278. CullableClass * NextObject; // next object in the node
  279. };
  280. #endif // AABTREECULL_H