X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FOctree.hh;h=13ece5bb4ecd9ac0fe75f7d3b838e9bacd0bc123;hp=e3e2225f03299636137b21aa3a54aeb38fd25b38;hb=71bd9dbaf1c1e3c55a9f63392a73865d8aeee7d4;hpb=660e768e64c2c30928c7f157d5ff34195a4347fa diff --git a/src/Moof/Octree.hh b/src/Moof/Octree.hh index e3e2225..13ece5b 100644 --- a/src/Moof/Octree.hh +++ b/src/Moof/Octree.hh @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -47,136 +48,320 @@ namespace Mf { -class Frustum; - - -struct OctreeNode; -typedef stlplus::ntree::iterator OctreeNodeP; +struct OctreeInsertable +{ + virtual ~OctreeInsertable() {} -class Octree; -typedef boost::shared_ptr OctreeP; + virtual int getOctant(const Aabb<3>& aabb) const = 0; +}; -struct OctreeNode : public Entity +template +class Octree : public Entity { - std::list objects; + typedef boost::shared_ptr InsertableP; - OctreeNode() + struct Node : public Entity { - aabb_.min = Vector3(-1.0, -1.0, -1.0); - aabb_.max = Vector3(1.0, 1.0, 1.0); - sphere_.init(Vector3(0.0, 0.0, 0.0), 1.41421); - } + std::list objects; + + Node(const Aabb<3>& aabb) + { + mAabb = aabb; + mSphere.point = mAabb.getCenter(); + mSphere.radius = (mAabb.min - mSphere.point).length(); + } + + void draw(Scalar alpha) const + { + mAabb.draw(alpha); + } + + void printSize() + { + logDebug("size of node %d", objects.size()); + } + + void getAll(std::list& insertables) const + { + insertables.insert(insertables.end(), objects.begin(), + objects.end()); + } + + void getIfVisible(std::list& insertables, + const Frustum& frustum) const + { + typename std::list::const_iterator it; + + for (it = objects.begin(); it != objects.end(); ++it) + { + if ((*it)->isVisible(frustum)) insertables.push_back(*it); + } + } + }; + + +public: + + typedef boost::shared_ptr Ptr; + typedef typename stlplus::ntree::iterator NodeP; + +private: + - OctreeNode(const Aabb& aabb) + NodeP insert(InsertableP entity, NodeP node) { - aabb_ = aabb; - sphere_.point = aabb.getCenter(); - sphere_.radius = (aabb.min - sphere_.point).length(); + ASSERT(node.valid() && "invalid node passed"); + ASSERT(entity && "null entity passed"); + + Aabb<3> entityAabb = entity->getAabb(); + Aabb<3> nodeAabb = node->getAabb(); + + if (!(entityAabb.max[0] < nodeAabb.max[0] && + entityAabb.min[0] > nodeAabb.min[0] && + entityAabb.max[1] < nodeAabb.max[1] && + entityAabb.min[1] > nodeAabb.min[1] && + entityAabb.max[2] < nodeAabb.max[2] && + entityAabb.min[2] > nodeAabb.min[2])) + { + node->objects.push_back(entity); + return node; + } + else + { + return insert_recurse(entity, node); + } } - void draw(Scalar alpha) const + NodeP insert_recurse(InsertableP entity, NodeP node) { - std::list::const_iterator it; + ASSERT(node.valid() && "invalid node passed"); + ASSERT(entity && "null entity passed"); - for (it = objects.begin(); it != objects.end(); ++it) + int octantNum = entity->getOctant(node->getAabb()); + if (octantNum == -1) { - (*it)->draw(alpha); + node->objects.push_back(entity); + return node; } + else + { + if ((int)mTree.children(node) <= octantNum) + { + addChild(node, octantNum); + } - if (!objects.empty()) - aabb_.draw(); // temporary + NodeP child = mTree.child(node, octantNum); + ASSERT(child.valid() && "expected valid child node"); + + return insert_recurse(entity, child); + } } - void drawIfVisible(Scalar alpha, const Frustum& frustum) const + void addChild(NodeP node, int index) { - std::list::const_iterator it; + ASSERT(node.valid() && "invalid node passed"); - for (it = objects.begin(); it != objects.end(); ++it) + Aabb<3> octant; + + for (int i = mTree.children(node); i <= index; ++i) { - (*it)->drawIfVisible(alpha, frustum); + node->getAabb().getOctant(octant, i); + mTree.append(node, octant); } + } + + + void getNearbyObjects(std::list& insertables, + const OctreeInsertable& entity, NodeP node) const + { + ASSERT(node.valid() && "invalid node passed"); - if (!objects.empty()) + node->printSize(); + + int octantNum = entity.getOctant(node->getAabb()); + if (octantNum != -1) + { + node->getAll(insertables); + + if (octantNum < (int)mTree.children(node)) + { + NodeP child = mTree.child(node, octantNum); + ASSERT(child.valid() && "expected valid child node"); + + getNearbyObjects(insertables, entity, child); + } + } + else { - aabb_.draw(); - //sphere_.draw(); + logDebug("getting all the rest..."); + getAll(insertables, node); } } - bool isVisible(const Frustum& frustum) const + void getAll(std::list& insertables, NodeP node) const { - if (sphere_.isVisible(frustum)) + ASSERT(node.valid() && "invalid node passed"); + + node->getAll(insertables); + + for (unsigned i = 0; i < mTree.children(node); ++i) + { + NodeP child = mTree.child(node, i); + ASSERT(child.valid() && "expected valid child node"); + + getAll(insertables, child); + } + } + + void getIfVisible(std::list& insertables, + const Frustum& frustum, NodeP node) const + { + ASSERT(node.valid() && "invalid node passed"); + + // try to cull by sphere + Frustum::Collision collision = frustum.contains(node->getSphere()); + if (collision == Frustum::OUTSIDE) return; + + // try to cull by aabb + collision = frustum.contains(node->getAabb()); + if (collision == Frustum::OUTSIDE) return; + + + if (collision == Frustum::INSIDE) + { + node->getAll(insertables); + } + else // collision == Frustum::INTERSECT { - return aabb_.isVisible(frustum); + node->getIfVisible(insertables, frustum); } - return false; + if (mTree.children(node) > 0) + { + if (collision == Frustum::INSIDE) + { + for (unsigned i = 0; i < mTree.children(node); ++i) + { + NodeP child = mTree.child(node, i); + ASSERT(child.valid() && "expected valid child node"); + + getAll(insertables, child); + } + } + else // collision == Frustum::INTERSECT + { + for (unsigned i = 0; i < mTree.children(node); ++i) + { + NodeP child = mTree.child(node, i); + ASSERT(child.valid() && "expected valid child node"); + + getIfVisible(insertables, frustum, child); + } + } + } } - static bool compareZOrder(EntityP a, EntityP b) + mutable stlplus::ntree mTree; + + +public: + + void print(NodeP node) { - return a->getSphere().point[2] < b->getSphere().point[2]; + logInfo("-----"); + logInfo("depth to node: %d", mTree.depth(node)); + logInfo("size of node: %d", mTree.size(node)); } - void sort() + static Ptr alloc(const Node& rootNode) { - //std::sort(objects.begin(), objects.end(), compareZOrder); - objects.sort(compareZOrder); + return Ptr(new Octree(rootNode)); } -}; + explicit Octree(const Node& rootNode) + { + mTree.insert(rootNode); + } -class Octree -{ - OctreeNodeP insert(EntityP entity, OctreeNodeP node); - - void addChild(OctreeNodeP node, int index); - void draw(Scalar alpha, OctreeNodeP node); - void drawIfVisible(Scalar alpha, const Frustum& frustum, OctreeNodeP node); + NodeP insert(InsertableP entity) + { + return insert(entity, mTree.root()); + } - stlplus::ntree tree_; + void remove(InsertableP entity, NodeP node) + { + ASSERT(entity && "null entity passed"); + ASSERT(node.valid() && "invalid node passed"); -public: + typename std::list::iterator it; + it = std::find(node->objects.begin(), node->objects.end(), entity); - void print(OctreeNodeP node) - { - //logDebug("-----"); - //logDebug("depth to node: %d", tree_.depth(node)); - //logDebug("size of node: %d", tree_.size(node)); + if (it != node->objects.end()) + { + node->objects.erase(it); + } } - static OctreeP alloc(const OctreeNode& rootNode) + + NodeP reinsert(InsertableP entity, NodeP node) { - return OctreeP(new Octree(rootNode)); + remove(entity, node); + return insert(entity); } - explicit Octree(const OctreeNode& rootNode) + + void draw(Scalar alpha) const { - tree_.insert(rootNode); + std::list objects; + getAll(objects); + + typename std::list::const_iterator it; + for (it = objects.begin(); it != objects.end(); ++it) + { + (*it)->draw(alpha); + } } - OctreeNodeP insert(EntityP entity) + void drawIfVisible(Scalar alpha, const Frustum& frustum) const { - return insert(entity, tree_.root()); + std::list objects; + getIfVisible(objects, frustum); + //getNearbyObjects(objects, *savedObj); + + typename std::list::const_iterator it; + for (it = objects.begin(); it != objects.end(); ++it) + { + (*it)->draw(alpha); + } } - OctreeNodeP reinsert(EntityP entity, OctreeNodeP node); - void draw(Scalar alpha) + void getAll(std::list& insertables) const { - draw(alpha, tree_.root()); + getAll(insertables, mTree.root()); } - void drawIfVisible(Scalar alpha, const Frustum& frustum) + void getIfVisible(std::list& insertables, + const Frustum& frustum) const { - drawIfVisible(alpha, frustum, tree_.root()); + getIfVisible(insertables, frustum, mTree.root()); } - void sort(); + mutable const OctreeInsertable* savedObj; + + + void getNearbyObjects(std::list& insertables, + const OctreeInsertable& entity) const + { + logDebug("--- GETTING NEARBY"); + getNearbyObjects(insertables, entity, mTree.root()); + logDebug("---"); + savedObj = &entity; + } };