Added test get methods and some comments.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#ifndef OctTree_h__
|
||||
#define OctTree_h__
|
||||
|
||||
#include "Core/Collision.h"
|
||||
#include "Core/AABB.h"
|
||||
|
||||
struct Ray;
|
||||
|
||||
class OctTree
|
||||
{
|
||||
@@ -19,13 +21,18 @@ public:
|
||||
//Returns true if the ray collides with something in the tree. Result is written to [data].
|
||||
bool RayCollides(const Ray& ray, Output& data) const;
|
||||
|
||||
//Test Getters.
|
||||
OctTree** Children() { return m_Children; }
|
||||
const AABB& Box() { return m_Box; }
|
||||
const std::vector<AABB>& ContainingBoxes() { return m_ContainingBoxes; }
|
||||
|
||||
private:
|
||||
OctTree* m_Children[8];
|
||||
std::vector<AABB> m_ContainingBoxes;
|
||||
//TODO: Do derived class from AABB with a bool Tested, falsify at
|
||||
//start of Collision test, set on check, don't check if set already. Solves duplicate boxes in tree.
|
||||
AABB m_Box;
|
||||
|
||||
|
||||
bool rayCollides(const Ray& ray, Output& data) const;
|
||||
inline bool hasChildren() const;
|
||||
int childIndexContainingPoint(const glm::vec3& point) const;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <bitset>
|
||||
|
||||
#include "Core/OctTree.h"
|
||||
#include "Core/Collision.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -37,7 +38,7 @@ OctTree::OctTree(const AABB& octTreeBounds, int subDivisions)
|
||||
glm::vec3 minPos, maxPos;
|
||||
const glm::vec3& parentMin = m_Box.MinCorner();
|
||||
const glm::vec3& parentMax = m_Box.MaxCorner();
|
||||
const glm::vec3& parentCenter = m_Box.MaxCorner();
|
||||
const glm::vec3& parentCenter = m_Box.Center();
|
||||
std::bitset<3> bits(i);
|
||||
//If child is 4,5,6,7.
|
||||
if (bits.test(2)) {
|
||||
@@ -128,6 +129,8 @@ void OctTree::AddBox(const AABB& box)
|
||||
if (hasChildren()) {
|
||||
int minInd = childIndexContainingPoint(box.MinCorner());
|
||||
int maxInd = childIndexContainingPoint(box.MaxCorner());
|
||||
//Because of the predictable ordering of the child indices,
|
||||
//the number of bits set when xor:ing the indices will determine the number of children containing the box.
|
||||
std::bitset<3> bits(minInd ^ maxInd);
|
||||
switch (bits.count()) {
|
||||
case 0: //Box contained completely in one child.
|
||||
@@ -138,9 +141,14 @@ void OctTree::AddBox(const AABB& box)
|
||||
m_Children[maxInd]->AddBox(box);
|
||||
break;
|
||||
case 2: //Four children.
|
||||
//Bit-hax to calculate the right 4 cildren containing the box.
|
||||
//This works because of the childrens index determine what part of
|
||||
//the dimensions they are responsible for (which octant).
|
||||
bits.flip();
|
||||
//At this point the bits necessarily have exactly one bit set.
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
if ((bits & std::bitset<3>(c)).any()) {
|
||||
//If the child index have the same bit set as the bits, add box to it.
|
||||
if (bits.to_ulong() & c) {
|
||||
m_Children[c]->AddBox(box);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user