Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Quadtree.cpp
Go to the documentation of this file.
1
2
3#include "Headers/Quadtree.h"
7
8namespace Divide {
9
11 : _root(std::make_unique<QuadtreeNode>(this))
12{
13}
14
16{
17}
18
21 _root->toggleBoundingBoxes();
22}
23
24void Quadtree::drawBBox(GFXDevice& context) const {
25 if (!_drawBBoxes) {
26 return;
27 }
28
29 _root->drawBBox(context);
30
31 IM::BoxDescriptor descriptor;
32 descriptor.min = _root->getBoundingBox().getMin();
33 descriptor.max = _root->getBoundingBox().getMax();
34 descriptor.colour = UColour4(0, 64, 255, 255);
35 context.debugDrawBox(-1, descriptor);
36}
37
38QuadtreeNode* Quadtree::findLeaf(const vec2<F32> pos) const noexcept
39{
40 assert(_root);
41
42 QuadtreeNode* node = _root.get();
43 while (!node->isALeaf()) {
44 U32 i;
45 for (i = 0; i < 4; i++) {
46 QuadtreeNode* child = &node->getChild(i);
47 const BoundingBox& bb = child->getBoundingBox();
48 if (bb.containsPoint(vec3<F32>(pos.x, bb.getCenter().y, pos.y))) {
49 node = child;
50 break;
51 }
52 }
53
54 if (i >= 4) {
55 return nullptr;
56 }
57 }
58
59 return node;
60}
61
62void Quadtree::build(const BoundingBox& terrainBBox,
63 const vec2<U16> HMSize,
64 Terrain* const terrain) {
65
66 _targetChunkDimension = std::max(HMSize.maxComponent() / 8u, 8u);
67
68 _root->setBoundingBox(terrainBBox);
69 _root->build(0, vec2<U16>(0u), HMSize, _targetChunkDimension, terrain, _chunkCount);
70}
71
73 assert(_root);
74 BoundingBox rootBB = _root->getBoundingBox();
75 if (!_root->computeBoundingBox(rootBB)) {
77 }
78
79 return _root->getBoundingBox();
80}
81
82} //namespace Divide
#define DIVIDE_UNEXPECTED_CALL()
vec3< F32 > getCenter() const noexcept
bool containsPoint(const vec3< F32 > &point) const noexcept
Definition: BoundingBox.inl:37
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
void debugDrawBox(const I64 ID, IM::BoxDescriptor descriptor) noexcept
Definition: GFXDevice.cpp:2877
bool _drawBBoxes
Definition: Quadtree.h:78
void build(const BoundingBox &terrainBBox, const vec2< U16 > HMSize, Terrain *terrain)
Definition: Quadtree.cpp:62
void toggleBoundingBoxes()
Definition: Quadtree.cpp:19
QuadtreeNode_uptr _root
Definition: Quadtree.h:75
QuadtreeNode * findLeaf(vec2< F32 > pos) const noexcept
Definition: Quadtree.cpp:38
const BoundingBox & computeBoundingBox() const
Definition: Quadtree.cpp:72
void drawBBox(GFXDevice &context) const
Definition: Quadtree.cpp:24
QuadtreeNode & getChild(const ChildPosition pos) const noexcept
Definition: QuadtreeNode.h:89
bool isALeaf() const noexcept
Definition: QuadtreeNode.h:82
const BoundingBox & getBoundingBox() const noexcept
Definition: QuadtreeNode.h:85
T maxComponent() const noexcept
get the largest value of X or Y
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
vec4< U8 > UColour4
Definition: MathHelper.h:72
uint32_t U32