103 baseNodeSize(0.0f, 0.0f, 0.0f)
124template<
class TYPE>
void
127 #if NEBULA_BOUNDSCHECKS
131 this->treeDepth = depth;
132 this->boundingBox = box;
134 int baseDimension = 1 << (this->treeDepth - 1);
135 this->baseNodeSize.
set(this->boundingBox.size().x / baseDimension,
136 this->boundingBox.size().y,
137 this->boundingBox.size().z / baseDimension);
139 SizeT numNodes = this->GetNumNodes(this->treeDepth);
140 this->nodeArray.SetSize(numNodes);
141 this->nodeArray[0].Setup(
this, 0, 0, 0);
144 #if NEBULA_BOUNDSCHECKS
146 int num = this->nodeArray.Size();
147 for (i = 0; i < num; i++)
149 n_assert(this->nodeArray[i].Level() >= 0);
158template<
class TYPE>
uchar
161 return this->treeDepth;
171 return this->boundingBox;
178template<
class TYPE>
SizeT
182 return 0x55555555 & ((1 << level * 2) - 1);
189template<
class TYPE>
SizeT
192 return this->nodeArray.Size();
200template<
class TYPE>
IndexT
203 #if NEBULA_BOUNDSCHECKS
204 n_assert((col >= 0) && (col < (1 << level)));
205 n_assert((row >= 0) && (row < (1 << level)));
207 return this->GetNumNodes(level) + (row << level) + col;
231 for (i = 0; i < 4; i++)
250template<
class TYPE>
void
253 #if NEBULA_BOUNDSCHECKS
261 this->level = _level;
266 float levelFactor = float(1 << (tree->
treeDepth - 1 - this->level));
274 center.
set(treeCenter.
x + (((this->col + 0.5f) * levelFactor * baseSize.
x) - (treeSize.
x * 0.5f)),
276 treeCenter.
z + (((this->row + 0.5f) * levelFactor * baseSize.
z) - (treeSize.
z * 0.5f)));
278 extent.
set(levelFactor * baseSize.
x * 0.5f,
280 levelFactor * baseSize.
z * 0.5f );
282 this->box.set(center, extent);
285 uchar childLevel = this->level + 1;
289 for (i = 0; i < 4; i++)
291 ushort childCol = 2 * this->col + (i & 1);
292 ushort childRow = 2 * this->row + ((i & 2) >> 1);
294 this->children[i] = &(tree->
nodeArray[childIndex]);
295 this->children[i]->Setup(tree, childLevel, childCol, childRow);
308 if (this->box.contains(checkBox))
311 if (this->children[0] != 0)
314 for (i = 0; i < 4; i++)
364template<
class TYPE>
char
373template<
class TYPE>
ushort
382template<
class TYPE>
ushort
391template<
class TYPE>
void
400template<
class TYPE>
const TYPE&
403 return this->element;
412 return this->children[i];
Nebula's bounding box class.
Definition bbox.h:24
vec3 size() const
get size of box
Definition bbox.h:164
vector extents() const
get extents of box
Definition bbox.h:155
point center() const
get center of box
Definition bbox.h:146
void set(const mat4 &m)
set from mat4
Definition bbox.h:124
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
node in quad tree
Definition quadtree.h:51
Node * children[4]
Definition quadtree.h:79
void SetElement(const TYPE &elm)
set data element associated with node
Definition quadtree.h:392
TYPE element
Definition quadtree.h:84
Node()
constructor
Definition quadtree.h:225
ushort Column() const
get the node's column
Definition quadtree.h:374
ushort row
Definition quadtree.h:82
const TYPE & GetElement() const
get data element
Definition quadtree.h:401
Math::bbox box
Definition quadtree.h:83
ushort col
Definition quadtree.h:81
ushort Row() const
get the node's row
Definition quadtree.h:383
char level
Definition quadtree.h:80
Node * GetChildAt(IndexT i)
get child at index
Definition quadtree.h:410
~Node()
destructor
Definition quadtree.h:241
Node * FindContainmentNode(const Math::bbox &box)
recursively find the smallest child node which contains the bounding box
Definition quadtree.h:306
const Math::bbox & GetBoundingBox() const
compute the node's bounding box
Definition quadtree.h:356
char Level() const
get the node's level
Definition quadtree.h:365
void Setup(QuadTree< TYPE > *tree, uchar _level, ushort _col, ushort _row)
recursively initialize the node
Definition quadtree.h:251
Node * FindContainmentNode(const Math::bbox &box)
recursively find the smallest child node which contains the bounding box
Definition quadtree.h:216
Math::bbox boundingBox
Definition quadtree.h:91
Util::FixedArray< Node > nodeArray
Definition quadtree.h:93
SizeT GetNumNodes(uchar level) const
compute number of nodes in a level, including its children
Definition quadtree.h:179
SizeT GetNumNodesInTree() const
get overall number of nodes in the tree
Definition quadtree.h:190
uchar GetDepth() const
get the tree depth
Definition quadtree.h:159
uchar treeDepth
Definition quadtree.h:90
const Math::bbox & GetBoundingBox() const
get the top level bounding box
Definition quadtree.h:169
QuadTree()
constructor
Definition quadtree.h:101
~QuadTree()
destructor
Definition quadtree.h:113
Node & NodeByIndex(IndexT i)
read/write access to node
Definition quadtree.h:347
Math::vector baseNodeSize
Definition quadtree.h:92
const Node & GetNodeByIndex(IndexT i) const
get pointer to node by index
Definition quadtree.h:338
void Setup(const Math::bbox &box, uchar depth)
initialize quad tree
Definition quadtree.h:125
IndexT GetNodeIndex(uchar level, ushort col, ushort row) const
compute linear chunk index from level, col and row
Definition quadtree.h:201
#define n_assert(exp)
Definition debug.h:50
A pinned array is an array which manages its own virtual memory.
Definition String.cs:6
A vector is a 3D direction in space.
Definition vector.h:22
float z
Definition vector.h:85
float x
Definition vector.h:85
float y
Definition vector.h:85
void set(scalar x, scalar y, scalar z)
set content
Definition vector.h:353
unsigned char uchar
Definition types.h:33
int SizeT
Definition types.h:49
unsigned short ushort
Definition types.h:32
int IndexT
Definition types.h:48