Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
aabtreecull.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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/***********************************************************************************************
20 *** 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 ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWMath *
24 * *
25 * $Archive:: /Commando/Code/wwmath/aabtreecull.h $*
26 * *
27 * Author:: Greg Hjelstrom *
28 * *
29 * $Modtime:: 7/24/01 10:00a $*
30 * *
31 * $Revision:: 15 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37
38#if defined(_MSC_VER)
39#pragma once
40#endif
41
42#ifndef AABTREECULL_H
43#define AABTREECULL_H
44
45#include "cullsys.h"
46#include "aaplane.h"
47#include "wwmath.h"
48#include "mempool.h"
49#include "simplevec.h"
50#include <math.h>
51#include <float.h>
52
54class ChunkLoadClass;
55class ChunkSaveClass;
56class SphereClass;
57
63{
64public:
65
67 virtual ~AABTreeCullSystemClass(void);
68
69 /*
70 ** Re-partition the tree. Two methods can be used to accomplish this. The
71 ** first re-partitions the tree based on the objects contained within, the second
72 ** re-partitions the tree based solely on a set of input "seed" boxes. Each seed
73 ** box will become a leaf; then the objects will be re-inserted in the new tree.
74 */
75 void Re_Partition(void);
76 void Re_Partition(const AABoxClass & bounds,SimpleDynVecClass<AABoxClass> & boxes);
77
78 /*
79 ** Update_Bounding_Boxes. This function causes all bounding boxes in the tree to update themselves.
80 ** If any box is found to not bound the objects it is supposed to contain, the box is updated
81 ** Note that this is normally not necessary, the reason this function existsis due to the fact
82 ** that the renegade level editor tries to do everything possible to not discard the precalculated
83 ** visibilty data for a level. In some cases, we want to load geometry that has been edited back
84 ** into the same AABTree without re-partitioning.
85 */
86 void Update_Bounding_Boxes(void);
87
88 /*
89 ** Re-insert an object into the tree
90 */
91 virtual void Update_Culling(CullableClass * obj);
92
93 /*
94 ** Statistics about the AAB-Tree
95 */
96 int Partition_Node_Count(void) const;
97 int Partition_Tree_Depth(void) const;
98 int Object_Count(void) const;
99
100 /*
101 ** Collect objects which overlap the given primitive
102 */
103 virtual void Collect_Objects(const Vector3 & point);
104 virtual void Collect_Objects(const AABoxClass & box);
105 virtual void Collect_Objects(const OBBoxClass & box);
106 virtual void Collect_Objects(const FrustumClass & frustum);
107 virtual void Collect_Objects(const SphereClass & sphere);
108
109 /*
110 ** Load and Save a description of this AAB-Tree and its contents
111 */
112 virtual void Load(ChunkLoadClass & cload);
113 virtual void Save(ChunkSaveClass & csave);
114
115 /*
116 ** Save an objects linkage, load the linkage and re-link the object
117 */
120
121 /*
122 ** Bounding box of the entire tree
123 */
124 const AABoxClass & Get_Bounding_Box(void);
125 void Get_Node_Bounds(int node_id,AABoxClass * set_bounds);
126
127 /*
128 ** Statistics
129 */
137
138 void Reset_Statistics(void);
139 const StatsStruct & Get_Statistics(void);
140
141protected:
142
143 /*
144 ** Internal stat tracking
145 */
146#ifdef WWDEBUG
147 void NODE_ACCEPTED(void) { Stats.NodesAccepted ++; }
149 void NODE_REJECTED(void) { Stats.NodesRejected ++; }
150#else
151 void NODE_ACCEPTED(void) { }
153 void NODE_REJECTED(void) { }
154#endif
155
156 /*
157 ** Internal functions
158 */
159 void Add_Object_Internal(CullableClass * obj,int node_index = -1);
161
162 void Re_Index_Nodes(void);
164
166 void Partition_Tree_Depth_Recursive(AABTreeNodeClass * node,int cur_depth,int & max_depth) const;
169
171 void Collect_Objects_Recursive(AABTreeNodeClass * node,const Vector3 & point);
175 void Collect_Objects_Recursive(AABTreeNodeClass * node,const FrustumClass & frustum,int planes_passed);
176 void Collect_Objects_Recursive(AABTreeNodeClass * node,const SphereClass & sphere);
177
179
180 void Load_Nodes(AABTreeNodeClass * node,ChunkLoadClass & cload);
181 void Save_Nodes(AABTreeNodeClass * node,ChunkSaveClass & csave);
182
183 virtual void Load_Node_Contents(AABTreeNodeClass * /*node*/,ChunkLoadClass & /*cload*/) { }
184 virtual void Save_Node_Contents(AABTreeNodeClass * /*node*/,ChunkSaveClass & /*csave*/) { }
185
186 AABTreeNodeClass * RootNode; // root of the AAB-Tree
187 int ObjectCount; // number of objects in the system
188
189 int NodeCount; // number of nodes
190 AABTreeNodeClass ** IndexedNodes; // index access to the nodes
191
193
194 friend class AABTreeIterator;
195};
196
197
204{
205public:
207
208 void Reset(void);
209 bool Enter_Parent(void);
210 bool Enter_Sibling(void);
211 bool Has_Front_Child(void);
212 bool Enter_Front_Child(void);
213 bool Has_Back_Child(void);
214 bool Enter_Back_Child(void);
215
216 int Get_Current_Node_Index(void);
217 void Get_Current_Box(AABoxClass * set_box);
218
219private:
220
221 void validate(void);
222
224 int CurNodeIndex;
225
226};
227
228
235{
236public:
237
238 virtual void Add_Object(T * obj,int node_index=-1) { Add_Object_Internal(obj,node_index); }
239 virtual void Remove_Object(T * obj) { Remove_Object_Internal(obj); }
240
245};
246
247
248
249
257class AABTreeNodeClass : public AutoPoolClass<AABTreeNodeClass,256>
258{
259
260public:
261
262 AABTreeNodeClass(void);
263 ~AABTreeNodeClass(void);
264
265 void Add_Object(CullableClass * obj,bool update_bounds = true);
266 void Remove_Object(CullableClass * obj);
267 int Object_Count(void);
268 CullableClass * Peek_Object(int index);
269
270 uint32 Index; // Index of this node
271 AABoxClass Box; // Bounding box of the node
272 AABTreeNodeClass * Parent; // parent of this node
273 AABTreeNodeClass * Front; // front node
274 AABTreeNodeClass * Back; // back node
275 CullableClass * Object; // objects in this node
276 uint32 UserData; // 32bit field for the user, initialized to 0
277
278 /*
279 ** Construction support:
280 */
282 {
283 SplitChoiceStruct(void) : Cost(FLT_MAX),FrontCount(0),BackCount(0),Plane(AAPlaneClass::XNORMAL,0.0f)
284 {
285 FrontBox.Init_Empty();
286 BackBox.Init_Empty();
287 }
288
289 float Cost;
295 };
296
297 void Compute_Bounding_Box(void);
299 float Compute_Volume(void);
300 void Transfer_Objects(AABTreeNodeClass * dummy_node);
301
302 /*
303 ** Partition the tree based on the objects contained.
304 */
305 void Partition(void);
306 void Split_Objects( const SplitChoiceStruct & sc,
307 AABTreeNodeClass * front,
308 AABTreeNodeClass * back);
309
310 /*
311 ** Partition the tree based on a set of input "seed" boxes.
312 */
313 void Partition(const AABoxClass & bounds,SimpleDynVecClass<AABoxClass> & boxes);
314 void Split_Boxes( const SplitChoiceStruct & sc,
318
319 /*
320 ** Functions used by both partitioning algorithms
321 */
325};
326
327
328/*
329** AABTreeLinkClass
330** This structure is used to link objects into an AAB-Tree culling system.
331*/
332class AABTreeLinkClass : public CullLinkClass, public AutoPoolClass<AABTreeLinkClass,256>
333{
334public:
336
337 AABTreeNodeClass * Node; // partition node containing this object
338 CullableClass * NextObject; // next object in the node
339};
340
341
342
343
344#endif // AABTREECULL_H
#define NULL
Definition BaseType.h:92
unsigned long uint32
Definition bittype.h:46
int Partition_Tree_Depth(void) const
void Update_Bounding_Boxes_Recursive(AABTreeNodeClass *node)
void Update_Bounding_Boxes(void)
void Partition_Tree_Depth_Recursive(AABTreeNodeClass *node, int cur_depth, int &max_depth) const
virtual void Load_Node_Contents(AABTreeNodeClass *, ChunkLoadClass &)
void Load_Object_Linkage(ChunkLoadClass &cload, CullableClass *obj)
void Add_Object_Internal(CullableClass *obj, int node_index=-1)
void Remove_Object_Internal(CullableClass *obj)
const AABoxClass & Get_Bounding_Box(void)
void Re_Index_Nodes_Recursive(AABTreeNodeClass *node, int &counter)
AABTreeNodeClass ** IndexedNodes
void Save_Object_Linkage(ChunkSaveClass &csave, CullableClass *obj)
void Add_Loaded_Object(AABTreeNodeClass *node, CullableClass *obj)
virtual void Update_Culling(CullableClass *obj)
int Object_Count(void) const
virtual ~AABTreeCullSystemClass(void)
friend class AABTreeIterator
int Partition_Node_Count_Recursive(AABTreeNodeClass *node) const
void Collect_Objects_Recursive(AABTreeNodeClass *node)
const StatsStruct & Get_Statistics(void)
virtual void Save_Node_Contents(AABTreeNodeClass *, ChunkSaveClass &)
int Partition_Node_Count(void) const
void NODE_TRIVIALLY_ACCEPTED(void)
void Get_Node_Bounds(int node_id, AABoxClass *set_bounds)
virtual void Collect_Objects(const Vector3 &point)
virtual void Load(ChunkLoadClass &cload)
void Add_Object_Recursive(AABTreeNodeClass *node, CullableClass *obj)
void Save_Nodes(AABTreeNodeClass *node, ChunkSaveClass &csave)
AABTreeNodeClass * RootNode
virtual void Save(ChunkSaveClass &csave)
void Collect_Objects_Recursive(AABTreeNodeClass *node, const FrustumClass &frustum)
void Load_Nodes(AABTreeNodeClass *node, ChunkLoadClass &cload)
bool Enter_Back_Child(void)
void Get_Current_Box(AABoxClass *set_box)
bool Has_Front_Child(void)
bool Has_Back_Child(void)
AABTreeIterator(AABTreeCullSystemClass *tree)
bool Enter_Sibling(void)
bool Enter_Front_Child(void)
int Get_Current_Node_Index(void)
bool Enter_Parent(void)
void Add_Object(CullableClass *obj, bool update_bounds=true)
void Transfer_Objects(AABTreeNodeClass *dummy_node)
void Compute_Score(SplitChoiceStruct *sc, SimpleDynVecClass< AABoxClass > &boxes)
AABTreeNodeClass * Parent
AABTreeNodeClass * Back
void Split_Boxes(const SplitChoiceStruct &sc, SimpleDynVecClass< AABoxClass > &boxes, SimpleDynVecClass< AABoxClass > &frontboxes, SimpleDynVecClass< AABoxClass > &backboxes)
CullableClass * Peek_Object(int index)
float Compute_Volume(void)
AABTreeNodeClass * Front
void Select_Splitting_Plane_Brute_Force(SplitChoiceStruct *sc, SimpleDynVecClass< AABoxClass > &boxes)
void Select_Splitting_Plane(SplitChoiceStruct *sc, SimpleDynVecClass< AABoxClass > &boxes)
void Split_Objects(const SplitChoiceStruct &sc, AABTreeNodeClass *front, AABTreeNodeClass *back)
void Remove_Object(CullableClass *obj)
CullableClass * Object
void Compute_Bounding_Box(void)
void Compute_Local_Bounding_Box(void)
CullableClass * Get_Next_Collected_Object_Internal(CullableClass *obj)
Definition cullsys.cpp:119
CullableClass * Get_First_Collected_Object_Internal(void)
Definition cullsys.cpp:114
friend class CullableClass
Definition cullsys.h:197
CullableClass * Peek_First_Collected_Object_Internal(void)
Definition cullsys.cpp:127
CullSystemClass(void)
Definition cullsys.cpp:103
CullableClass * Peek_Next_Collected_Object_Internal(CullableClass *obj)
Definition cullsys.cpp:132
T * Peek_Next_Collected_Object(T *obj)
T * Get_Next_Collected_Object(T *obj)
virtual void Add_Object(T *obj, int node_index=-1)
virtual void Remove_Object(T *obj)
int counter
Definition patch.cpp:410