X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FOctree.hh;h=88dae7ac6492e587d1bb133516a27b4343d98735;hp=cdac62a29071f4eeaeb51c48c055e84db1a029dc;hb=64bd443538f57ad1bdff6c6b35953e72141129b2;hpb=bc2bc12125d6c223d2935557e01926fe21166e38 diff --git a/src/Moof/Octree.hh b/src/Moof/Octree.hh index cdac62a..88dae7a 100644 --- a/src/Moof/Octree.hh +++ b/src/Moof/Octree.hh @@ -48,13 +48,6 @@ namespace Mf { -//class Octree; -//typedef boost::shared_ptr OctreeP; - -//class Octree::Node; -//typedef stlplus::ntree::iterator OctreeNodeP; - - struct OctreeInsertable { virtual ~OctreeInsertable() {} @@ -85,30 +78,36 @@ class Octree : public Entity void draw(Scalar alpha) const { - typename std::list::const_iterator it; + aabb.draw(alpha); + } - for (it = objects.begin(); it != objects.end(); ++it) + void drawIfVisible(Scalar alpha, const Frustum& frustum) const + { + if (isVisible(frustum)) { - (*it)->draw(alpha); + aabb.draw(alpha); } + } - if (!objects.empty()) - aabb.draw(); // temporary + void printSize() + { + logDebug("size of node %d", objects.size()); } - void drawIfVisible(Scalar alpha, const Frustum& frustum) const + 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) { - (*it)->drawIfVisible(alpha, frustum); - } - - if (!objects.empty()) - { - //aabb.draw(); - //sphere.draw(); + if ((*it)->isVisible(frustum)) insertables.push_back(*it); } } @@ -170,7 +169,7 @@ private: NodeP child = tree_.child(node, octantNum); ASSERT(child.valid() && "expected valid child node"); - return insert(entity, child); + return insert_recurse(entity, child); } } @@ -188,22 +187,51 @@ private: } - void draw(Scalar alpha, NodeP node) const + void getNearbyObjects(std::list& insertables, + const OctreeInsertable& entity, NodeP node) const { ASSERT(node.valid() && "invalid node passed"); - node->draw(alpha); + node->printSize(); + + int octantNum = entity.getOctant(node->aabb); + if (octantNum != -1) + { + node->getAll(insertables); + + if (octantNum < (int)tree_.children(node)) + { + NodeP child = tree_.child(node, octantNum); + ASSERT(child.valid() && "expected valid child node"); + + getNearbyObjects(insertables, entity, child); + } + } + else + { + logDebug("getting all the rest..."); + getAll(insertables, node); + } + } + + + void getAll(std::list& insertables, NodeP node) const + { + ASSERT(node.valid() && "invalid node passed"); + + node->getAll(insertables); for (unsigned i = 0; i < tree_.children(node); ++i) { NodeP child = tree_.child(node, i); ASSERT(child.valid() && "expected valid child node"); - draw(alpha, child); + getAll(insertables, child); } } - void drawIfVisible(Scalar alpha, const Frustum& frustum, NodeP node) const + void getIfVisible(std::list& insertables, + const Frustum& frustum, NodeP node) const { ASSERT(node.valid() && "invalid node passed"); @@ -218,11 +246,11 @@ private: if (collision == Frustum::INSIDE) { - node->draw(alpha); + node->getAll(insertables); } else // collision == Frustum::INTERSECT { - node->drawIfVisible(alpha, frustum); + node->getIfVisible(insertables, frustum); } if (tree_.children(node) > 0) @@ -234,7 +262,7 @@ private: NodeP child = tree_.child(node, i); ASSERT(child.valid() && "expected valid child node"); - draw(alpha, child); + getAll(insertables, child); } } else // collision == Frustum::INTERSECT @@ -244,12 +272,13 @@ private: NodeP child = tree_.child(node, i); ASSERT(child.valid() && "expected valid child node"); - drawIfVisible(alpha, frustum, child); + getIfVisible(insertables, frustum, child); } } } } + mutable stlplus::ntree tree_; @@ -257,9 +286,9 @@ public: void print(NodeP node) { - //logDebug("-----"); - //logDebug("depth to node: %d", tree_.depth(node)); - //logDebug("size of node: %d", tree_.size(node)); + logInfo("-----"); + logInfo("depth to node: %d", tree_.depth(node)); + logInfo("size of node: %d", tree_.size(node)); } static Ptr alloc(const Node& rootNode) @@ -272,12 +301,13 @@ public: tree_.insert(rootNode); } + NodeP insert(InsertableP entity) { return insert(entity, tree_.root()); } - NodeP reinsert(InsertableP entity, NodeP node) + void remove(InsertableP entity, NodeP node) { ASSERT(entity && "null entity passed"); ASSERT(node.valid() && "invalid node passed"); @@ -289,18 +319,63 @@ public: { node->objects.erase(it); } + } + + NodeP reinsert(InsertableP entity, NodeP node) + { + remove(entity, node); return insert(entity); } + void draw(Scalar alpha) const { - draw(alpha, tree_.root()); + std::list objects; + getAll(objects); + + typename std::list::const_iterator it; + for (it = objects.begin(); it != objects.end(); ++it) + { + (*it)->draw(alpha); + } } void drawIfVisible(Scalar alpha, const Frustum& frustum) const { - drawIfVisible(alpha, frustum, 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); + } + } + + + void getAll(std::list& insertables) const + { + getAll(insertables, tree_.root()); + } + + void getIfVisible(std::list& insertables, + const Frustum& frustum) const + { + getIfVisible(insertables, frustum, tree_.root()); + } + + mutable const OctreeInsertable* savedObj; + + + void getNearbyObjects(std::list& insertables, + const OctreeInsertable& entity) const + { + logDebug("--- GETTING NEARBY"); + getNearbyObjects(insertables, entity, tree_.root()); + logDebug("---"); + savedObj = &entity; } };