!XT (Code) Update copyright headers in Code/Sandbox.
[CRYENGINE.git] / Code / Sandbox / EditorQt / IndirectLighting / Quadtree / QuadtreeHelper.h
blobb8427ec9741292e9eab138ce2ae8316b7e493c5a
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 /*
4 quadtree helper functions and structures
5 */
6 #ifndef QUADTREE_HELPER_H
7 #define QUADTREE_HELPER_H
9 #pragma once
11 #include <CryMath/Cry_Math.h>
12 #include <vector>
13 #include <set>
14 #include <limits> //numeric_limits
15 #include <memory.h> //memcpy
16 #include <algorithm> //sort
18 namespace
20 //!< LOKI stuff to be able to partial template specialize global functions
21 template<typename T>
22 struct SType2Type
24 typedef T OriginalType;
27 //!< retrieves for a certain type the minimum radius of distance (below resulting in no position changes for subdivision)
28 template<class TUnitType>
29 const TUnitType GetMinSubdivDist(SType2Type<TUnitType> );
32 //!< namespace for all quadtree related types
33 namespace NQT
35 //!< fast static vector class
36 //!< provides necessary vector-like interface but is allocated on heap
37 //!< used to avoid reallocations or static data (unusable for MT)
38 template<class T>
39 class CTravVec
41 public:
42 CTravVec();
43 void push_back(const T& rAdd);
44 const uint32 size() const;
45 T& operator[](const uint32 cIndex);
46 const T& operator[](const uint32 cIndex) const;
47 const bool empty() const;
49 private:
50 static const uint32 scMaxArrayCount = 4096;
52 uint8 m_Mem[sizeof(T) * scMaxArrayCount];
53 uint32 m_Count;
56 //!< quadtree child indices
57 //!< A denotes added, S denotes subtracted and always related to XY
58 typedef enum {SS = 0, AS = 1, SA = 2, AA = 3} TEChildIndex;
59 typedef enum {SS_E = 0, AS_E = 1, SA_E = 2, AA_E = 3, NP_E = 4} TEChildIndexExt; //extended version with a denotion of a non pos
61 //!< InsertLeaf ret value
62 typedef enum
64 SUCCESS = 0,
65 POS_EQUAL_TO_EXISTING_LEAF,
66 NOT_ENOUGH_SUBDIV_LEVELS,
67 TOO_MANY_LEAVES,
68 LEAVE_OUTSIDE_QUADTREE,
69 INSERTION_RESULT_COUNT
70 } EInsertionResult;
72 //!< PropagateLeavesToNewNodeFallback ret value
73 //!< we need to know whether it has not been inserted in the cell where the pos actually lies in or just in shared radius cell
74 typedef enum
76 POS_SUCCESS = 0,
77 POS_FAILED,
78 NON_POS_FAILED,
79 } EPosInsertionResult;
81 //!< saves a square 32 bit TGA texture
82 const bool SaveTGA32(const char* cpFilename, const uint32* cpBuffer, const uint32 cSize);
84 //!< round function for a particular type, preserves the value if float or double is invoked (partial template specification)
85 template<typename T>
86 const T Round(const double cInput, SType2Type<T> );
88 //!< type independent 2D vector
89 template<class TCompType>
90 struct SVec2
92 TCompType x, y;
94 SVec2(const TCompType cX = 0, const TCompType cY = 0);
95 template<class TCastCompType>
96 const float GetDistSq(const SVec2<TCastCompType>& crPos) const;
97 template<class TCastCompType>
98 const bool operator!=(const SVec2<TCastCompType>& crPos) const;
99 const float GetDist(const SVec2<TCompType>& crPos) const;
100 const bool operator==(const SVec2<TCompType>& crPos) const;
101 const float Dot(const SVec2<TCompType>& crPos) const;
102 template<class TCastCompType>
103 operator SVec2<TCastCompType>() const;
104 SVec2<TCompType> operator*(const TCompType& crScale) const;
106 void SwapEndianess();
109 template<class TCompType>
110 const SVec2<TCompType> operator-(const SVec2<TCompType>& crPos0, const SVec2<TCompType>& crPos1);
112 template<class TCompType>
113 const SVec2<TCompType> operator+(const SVec2<TCompType>& crPos0, const SVec2<TCompType>& crPos1);
115 template<class TPosType> //!< types the center type according to the position type to use float always but in the case of pos double's
116 struct SCenterType
118 typedef float Type;
121 template<>
122 struct SCenterType<double>
124 typedef double Type;
127 //!< matrix for 2d orientation of quadtree
128 struct S2DMatrix
130 float m00, m01, m10, m11;
132 S2DMatrix();
133 S2DMatrix(const S2DMatrix& crCopyFrom);
134 S2DMatrix(const float cM00, const float cM01, const float cM10, const float cM11);
135 template<class TCompType>
136 const SVec2<TCompType> RotateVec2(const SVec2<TCompType>& crPos) const;
137 template<class TCompType>
138 const SVec2<TCompType> RotateVec2Inv(const SVec2<TCompType>& crPos) const;
140 void SwapEndianess();
143 //!< holds the child indices a leaf will go to, can be multiple if using radius
144 template<bool TUseRadius, uint32 TMaxCellElems, class TIndexType>
145 struct SChildIndices
147 TEChildIndex indices[TUseRadius ? 4 : 1]; //!< static array with child indices, if no radius is used, it can only go to one quad cell index
148 uint32 count; //!< current number of stored indices (always reset and started from index 0)
149 //!< initialized to false, indicates whether this leaf has already been reinserted again, important for cloning if to insert a leaf multiple times as for TUseRadius
150 bool reinserted;
152 SChildIndices(); //!< ctor setting count to 0 for radius usage, to 1 otherwise
153 void ResetForRecursion(); //!< resets for next usage, leaves reinserted unchanged however
154 void CompleteReset(); //!< completely resets it, used to be able to use a static array rather then lots of reallocation
157 //!< selects an array size according to the radius usage
158 template<bool TAllCells, class Type>
159 struct SArraySelector
161 Type elems[TAllCells ? 4 : 1]; //!< 4 elems (one for each cell if requested, otherwise 1)
164 //!< encapsulates the container type for content retrieval
165 template<class TContentSort, class TIndexType>
166 class CSearchContainer
168 public:
169 //!< constructor reserving some memory too
170 explicit CSearchContainer
172 std::vector<typename TContentSort::TRetrievalType>& rContentVec,
173 const TIndexType cExpectedElements,
174 const bool
176 explicit CSearchContainer
178 std::vector<typename TContentSort::TRetrievalType>& rContentVec,
179 const float cRadius,
180 const bool cSort
182 //!< fills the vector with all gathered elements in sorted order
183 //!< performs binary search
184 void FillVector(const TIndexType&);
185 void FillVector(const float);
186 //!< adds an element
187 void Insert(const TContentSort& crElementToAdd, const float cCompareValue);
188 //!< returns true if contents is to be added or not, compares the squared distance but returns always true in case of the number retrieval and not yet enough stored elements
189 const bool CheckInsertCriteria(const float cDistSq) const;
191 private:
192 std::vector<TContentSort> m_ElemContainer; //!< container type chosen according to TUseVector
193 std::vector<float> m_ElemDist[2]; //!< container record for all current distances to compute threshold for new insertion, sorted by bucket sort, 2nd used for copying to not use swap
194 float m_CompareValue; //!< max compare value from last element
195 uint32 m_ElemCount; //!< elem count to retrieve, template arg for FillVector takes care of radius too
196 std::vector<typename TContentSort::TRetrievalType>& m_rContentVec; //reference to vector to be filled
197 bool m_Sort; //!< true if to sort elements, only effective in radius case
199 const float GetNewCompareValue() const; //!< returns new compare value for count based content search
200 void RecordCompareValue(const float cNewCompareValue); //!< records new compare value and resorts the contents
203 //!< performs ray - cell square intersection and stores for each child index the hit result
204 //!< also returns true if ray hits the cell
205 //!< done in aligned space after all positions have been rotated
206 template<class TPosType>
207 const bool RayCellIntersection
209 const float cSideLength, // cell side length
210 const SVec2<TPosType>& crRayPos, // ray origin
211 const SVec2<TPosType>& crRayDir, // ray direction
212 const SVec2<TPosType>& crCenterPos, // cell center pos
213 const float cRayLength, //ray length
214 bool childHits[4], //hit result for children
215 const SVec2<TPosType> cChildCenterPos[4] // children cell center pos
218 //!< helper function for RayCellIntersection
219 //!< adjusts the childHits vec with the result for the behind ray and beyond ray length
220 template<class TPosType>
221 void CheckChildCenters
223 const float cSideLength, // cell side length
224 const SVec2<TPosType>& crRayPos, // ray origin
225 const SVec2<TPosType>& crRayDir, // ray direction
226 const SVec2<TPosType>& crCenterPos, // cell center pos
227 const float cRayLength, //ray length
228 bool childHits[4], //hit result for children
229 const SVec2<TPosType> cChildCenterPos[4] // children cell center pos
232 //!< performs ray - circle intersection test and checks the ray length
233 //!< returns true if intersection
234 template<class TPosType, class TLeafPosType>
235 const bool RayLeafIntersection
237 const float cRadius, // leaf radius
238 const SVec2<TPosType>& crRayPos, // ray origin
239 const SVec2<TPosType>& crRayDir, // ray direction
240 const SVec2<TLeafPosType>& crLeafPos, // leaf pos
241 const float cRayLength //ray length
243 }//NQT
245 #endif//QUADTREE_HELPER