1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #pragma once19 20 #ifdef MATH_GRAPHICSENGINE_INTEROP21 #include "Time/Profiler.h"22 #else23 #define MGL_PROFILE(x)24 #endif25 #include "../Math/float2.h"26 #include "[AABB2D.h]"27 #include "../Math/MathTypes.h"28 29 #ifdef MATH_CONTAINERLIB_SUPPORT30 #include "Container/MaxHeap.h"31 #endif32 33 [MATH_BEGIN_NAMESPACE]34 35 36 37 static const int minQuadTreeNodeObjectCount = 16;38 39 40 41 42 static const float minQuadTreeQuadrantSize = 0.05f;43 44 45 inline float [MinX](const [float3] &pt) { return pt.[x]; }46 inline float [MaxX](const [float3] &pt) { return pt.[x]; }47 inline float [MinY](const [float3] &pt) { return pt.[y]; }48 inline float [MaxY](const [float3] &pt) { return pt.[y]; }49 50 51 52 53 54 55 template<typename T>56 class [QuadTree]57 {58 public:59 60 struct [Node]61 {62 63 [Node] *[parent];64 65 u32 [childIndex];66 67 #ifdef MATH_CONTAINERLIB_SUPPORT68 Array<T> [objects];69 #else70 std::vector<T> [objects];71 #endif72 73 [float2] [center];74 [float2] [radius];75 76 bool [IsLeaf]() const { return [childIndex] == 0xFFFFFFFF; }77 78 u32 [TopLeftChildIndex]() const { return [childIndex]; }79 u32 [TopRightChildIndex]() const { return [childIndex]+1; }80 u32 [BottomLeftChildIndex]() const { return [childIndex]+2; }81 u32 [BottomRightChildIndex]() const { return [childIndex]+3; }82 83 84 void [Remove](const T &object)85 {86 for(size_t i = 0; i < [objects].size(); ++i)87 if ([objects][i] == object)88 {89 [AssociateQuadTreeNode](object, 0); 90 std::swap([objects][i], [objects].back());91 [objects].pop_back();92 return;93 }94 }95 96 [AABB2D] [ComputeAABB]() const97 {98 return [AABB2D]([center] - [radius], [center] + [radius]);99 }100 101 float [DistanceSq](const [float2] &point) const102 {103 [float2] centered = point - [center];104 [float2] closestPoint = centered.[Clamp](-[radius], [radius]);105 return closestPoint.[DistanceSq](centered);106 107 108 }109 };110 111 112 struct [TraversalStackItem]113 {114 [Node] *[node];115 };116 117 [QuadTree]()118 :rootNodeIndex(-1),119 boundingAABB([float2](0,0), [float2](1,1))120 #ifdef QUADTREE_VERBOSE_LOGGING121 ,totalNumObjectsInTree(0)122 #endif123 {124 125 126 nodes.reserve(200000);127 }128 129 130 void [Clear](const [float2] &minXY = [float2](-1.f, -1.f), const [float2] &maxXY = [float2](1.f, 1.f));131 132 133 134 void [Add](const T &object);135 136 137 138 139 void [Remove](const T &object);140 141 142 143 [AABB2D] [BoundingAABB]() const { return boundingAABB; }144 145 146 Node *[Root]();147 const Node *[Root]() const;148 149 150 151 int [NumNodes]() const;152 153 154 155 int [NumLeaves]() const;156 157 158 159 int [NumInnerNodes]() const;160 161 162 163 int [NumObjects]() const;164 165 166 int [TreeHeight]() const;167 168 169 int [TreeHeight](const Node *node) const;170 171 172 173 174 175 176 177 178 template<typename Func>179 inline void [AABBQuery](const [AABB2D] &aabb, Func &callback);180 181 182 183 template<typename Func>184 inline void [CollidingPairsQuery](const [AABB2D] &aabb, Func &callback);185 186 #ifdef MATH_CONTAINERLIB_SUPPORT187 188 189 190 191 192 193 194 195 196 197 198 199 200 template<typename Func>201 inline void NearestNeighborNodes(const [float2] &point, Func &nodeCallback);202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 template<typename Func>219 inline void NearestNeighborObjects(const [float2] &targetPoint, Func &objectCallback);220 #endif221 222 223 void [DebugSanityCheckNode](Node *n);224 225 private:226 void [Add](const T &object, Node *n);227 228 229 int AllocateNodeGroup(Node *parent);230 231 void SplitLeaf(Node *leaf);232 233 234 235 236 std::vector<Node> nodes;237 238 239 240 int rootNodeIndex;241 [AABB2D] boundingAABB;242 243 void GrowRootTopLeft();244 void GrowRootTopRight();245 void GrowRootBottomLeft();246 void GrowRootBottomRight();247 248 void GrowImpl(int quadrantForRoot);249 250 #ifdef QUADTREE_VERBOSE_LOGGING251 int totalNumObjectsInTree;252 #endif253 };254 255 inline void [AssociateQuadTreeNode](const [float3] &, [QuadTree<float3>::Node] *) {}256 257 [MATH_END_NAMESPACE]258 259 #include "[QuadTree.inl]" Go back to previous page