From 04a8eeebe8445df9a985d8488988115e79508349 Mon Sep 17 00:00:00 2001 From: William Moberg Date: Mon, 7 Dec 2015 16:25:01 +0100 Subject: [PATCH] Added test get methods and some comments. --- include/Engine/Core/OctTree.h | 11 +++++++++-- src/Engine/Core/OctTree.cpp | 12 ++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/Engine/Core/OctTree.h b/include/Engine/Core/OctTree.h index 2c54513e..6e80fa24 100644 --- a/include/Engine/Core/OctTree.h +++ b/include/Engine/Core/OctTree.h @@ -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& ContainingBoxes() { return m_ContainingBoxes; } + private: OctTree* m_Children[8]; std::vector 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; diff --git a/src/Engine/Core/OctTree.cpp b/src/Engine/Core/OctTree.cpp index af222039..f98720be 100644 --- a/src/Engine/Core/OctTree.cpp +++ b/src/Engine/Core/OctTree.cpp @@ -3,6 +3,7 @@ #include #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); } }