Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
AABTreeCull.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef TT_INCLUDE_AABTREECULL_H
13#define TT_INCLUDE_AABTREECULL_H
14#include "cullsystemclass.h"
15#include "aaplaneclass.h"
16#include "AABoxClass.h"
17#include "wwmath.h"
18#include "engine_vector.h"
19#include <math.h>
20#include <float.h>
21
22class AABTreeNodeClass;
23class ChunkLoadClass;
24class ChunkSaveClass;
25class SphereClass;
26class AABTreeCullSystemClass : public CullSystemClass
27{
28public:
29 AABTreeCullSystemClass(void);
30 virtual ~AABTreeCullSystemClass(void);
31
32 void Re_Partition(void);
33 void Re_Partition(const AABoxClass & bounds,SimpleDynVecClass<AABoxClass> & boxes);
34 void Update_Bounding_Boxes(void);
35 virtual void Update_Culling(CullableClass * obj);
36 int Partition_Node_Count(void) const;
37 int Partition_Tree_Depth(void) const;
38 int Object_Count(void) const;
39 virtual void Collect_Objects(const Vector3 & point);
40 virtual void Collect_Objects(const AABoxClass & box);
41 virtual void Collect_Objects(const OBBoxClass & box);
42 virtual void Collect_Objects(const FrustumClass & frustum);
43 virtual void Collect_Objects(const SphereClass & sphere);
44 virtual void Load(ChunkLoadClass & cload);
45 virtual void Save(ChunkSaveClass & csave);
46 void Load_Object_Linkage(ChunkLoadClass & cload, CullableClass * obj);
47 void Save_Object_Linkage(ChunkSaveClass & csave, CullableClass * obj);
48 const AABoxClass & Get_Bounding_Box(void);
49 void Get_Node_Bounds(int node_id,AABoxClass * set_bounds);
50
51 struct StatsStruct
52 {
53 int NodeCount;
54 int NodesAccepted;
55 int NodesTriviallyAccepted;
56 int NodesRejected;
57 };
58
59 void Reset_Statistics(void);
60 const StatsStruct & Get_Statistics(void);
61
62protected:
63
64 void NODE_ACCEPTED(void) { }
65 void NODE_TRIVIALLY_ACCEPTED(void) { }
66 void NODE_REJECTED(void) { }
67
68 void Add_Object_Internal(CullableClass * obj, int node_index = -1);
69 void Remove_Object_Internal(CullableClass * obj);
70 void Re_Index_Nodes(void);
71 void Re_Index_Nodes_Recursive(AABTreeNodeClass * node, int & counter);
72 int Partition_Node_Count_Recursive(AABTreeNodeClass * node) const;
73 void Partition_Tree_Depth_Recursive(AABTreeNodeClass * node, int cur_depth, int & max_depth) const;
74 void Add_Object_Recursive(AABTreeNodeClass * node, CullableClass * obj);
75 void Add_Loaded_Object(AABTreeNodeClass * node, CullableClass * obj);
76 void Collect_Branch(AABTreeNodeClass * node);
77 void Collect_Objects_Recursive(AABTreeNodeClass * node, const FrustumClass & frustum, int planes_passed);
78 void Update_Bounding_Boxes_Recursive(AABTreeNodeClass * node);
79 void Load_Nodes(AABTreeNodeClass * node, ChunkLoadClass & cload);
80 void Save_Nodes(AABTreeNodeClass * node, ChunkSaveClass & csave);
81 virtual void Load_Node_Contents(AABTreeNodeClass * node, ChunkLoadClass & cload) { }
82 virtual void Save_Node_Contents(AABTreeNodeClass * node, ChunkSaveClass & csave) { }
83
84 AABTreeNodeClass * RootNode;
85 int ObjectCount;
86 int NodeCount;
87 AABTreeNodeClass ** IndexedNodes;
88 StatsStruct Stats;
89
90 friend class AABTreeIterator;
91};
92class AABTreeIterator
93{
94public:
95 AABTreeIterator(AABTreeCullSystemClass * tree);
96 void Reset(void);
97 bool Enter_Parent(void);
98 bool Enter_Sibling(void);
99 bool Has_Front_Child(void);
100 bool Enter_Front_Child(void);
101 bool Has_Back_Child(void);
102 bool Enter_Back_Child(void);
103 int Get_Current_Node_Index(void);
104 void Get_Current_Box(AABoxClass * set_box);
105
106private:
107 void validate(void);
108
109 AABTreeCullSystemClass * Tree;
110 int CurNodeIndex;
111};
112
113template <class T> class TypedAABTreeCullSystemClass : public AABTreeCullSystemClass
114{
115public:
116 virtual void Add_Object(T * obj, int node_index=-1) { Add_Object_Internal(obj,node_index); }
117 virtual void Remove_Object(T * obj) { Remove_Object_Internal(obj); }
118 T * Get_First_Collected_Object(void) { return (T*)Get_First_Collected_Object_Internal(); }
119 T * Get_Next_Collected_Object(T * obj) { return (T*)Get_Next_Collected_Object_Internal(obj); }
120 T * Peek_First_Collected_Object(void) { return (T*)Peek_First_Collected_Object_Internal(); }
121 T * Peek_Next_Collected_Object(T * obj) { return (T*)Peek_Next_Collected_Object_Internal(obj); }
122};
123
124class AABTreeNodeClass : public AutoPoolClass<AABTreeNodeClass, 256>
125{
126public:
127 AABTreeNodeClass(void);
128 ~AABTreeNodeClass(void);
129
130 void Add_Object(CullableClass * obj,bool update_bounds = true);
131 void Remove_Object(CullableClass * obj);
132 int Object_Count(void);
133 CullableClass * Peek_Object(int index);
134
135 uint32 Index;
136 AABoxClass Box;
137 AABTreeNodeClass * Parent;
138 AABTreeNodeClass * Front;
139 AABTreeNodeClass * Back;
140 CullableClass * Object;
141 uint32 UserData;
142
143 struct SplitChoiceStruct
144 {
145 SplitChoiceStruct(void) : Cost(FLT_MAX),FrontCount(0),BackCount(0),Plane(AAPlaneClass::XNORMAL,0.0f)
146 {
147 FrontBox.Init_Empty();
148 BackBox.Init_Empty();
149 }
150
151 float Cost;
152 int FrontCount;
153 int BackCount;
154 MinMaxAABoxClass FrontBox;
155 MinMaxAABoxClass BackBox;
156 AAPlaneClass Plane;
157 };
158
159 void Compute_Bounding_Box(void);
160 void Compute_Local_Bounding_Box(void);
161 float Compute_Volume(void);
162 void Transfer_Objects(AABTreeNodeClass * dummy_node);
163 void Partition(void);
164 void Split_Objects(const SplitChoiceStruct & sc, AABTreeNodeClass * front, AABTreeNodeClass * back);
165 void Partition(const AABoxClass & bounds, SimpleDynVecClass<AABoxClass> & boxes);
166 void Split_Boxes(const SplitChoiceStruct & sc, SimpleDynVecClass<AABoxClass> & boxes, SimpleDynVecClass<AABoxClass> & frontboxes, SimpleDynVecClass<AABoxClass> & backboxes);
167 void Select_Splitting_Plane(SplitChoiceStruct * sc,SimpleDynVecClass<AABoxClass> & boxes);
168 void Select_Splitting_Plane_Brute_Force(SplitChoiceStruct * sc, SimpleDynVecClass<AABoxClass> & boxes);
169 void Compute_Score(SplitChoiceStruct * sc, SimpleDynVecClass<AABoxClass> & boxes);
170};
171
172class AABTreeLinkClass : public CullLinkClass, public AutoPoolClass<AABTreeLinkClass,256>
173{
174public:
175 AABTreeLinkClass(AABTreeCullSystemClass * system) : CullLinkClass(system),Node(NULL), NextObject(NULL) { }
176 AABTreeNodeClass * Node;
177 CullableClass * NextObject;
178};
179#endif