]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Octree.hh
beginning CD implementation
[chaz/yoink] / src / Moof / Octree.hh
index cdac62a29071f4eeaeb51c48c055e84db1a029dc..88dae7ac6492e587d1bb133516a27b4343d98735 100644 (file)
 namespace Mf {
 
 
 namespace Mf {
 
 
-//class Octree;
-//typedef boost::shared_ptr<Octree> OctreeP;
-
-//class Octree::Node;
-//typedef stlplus::ntree<Octree::Node>::iterator OctreeNodeP;
-
-
 struct OctreeInsertable
 {
        virtual ~OctreeInsertable() {}
 struct OctreeInsertable
 {
        virtual ~OctreeInsertable() {}
@@ -85,30 +78,36 @@ class Octree : public Entity
 
                void draw(Scalar alpha) const
                {
 
                void draw(Scalar alpha) const
                {
-                       typename std::list<InsertableP>::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<InsertableP>& insertables) const
+               {
+                       insertables.insert(insertables.end(), objects.begin(),
+                                       objects.end());
+               }
+
+               void getIfVisible(std::list<InsertableP>& insertables,
+                               const Frustum& frustum) const
                {
                        typename std::list<InsertableP>::const_iterator it;
 
                        for (it = objects.begin(); it != objects.end(); ++it)
                        {
                {
                        typename std::list<InsertableP>::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");
 
                        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<InsertableP>& insertables,
+                       const OctreeInsertable& entity, NodeP node) const
        {
                ASSERT(node.valid() && "invalid node passed");
 
        {
                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<InsertableP>& 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");
 
 
                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<InsertableP>& insertables,
+                       const Frustum& frustum, NodeP node) const
        {
                ASSERT(node.valid() && "invalid node passed");
 
        {
                ASSERT(node.valid() && "invalid node passed");
 
@@ -218,11 +246,11 @@ private:
 
                if (collision == Frustum::INSIDE)
                {
 
                if (collision == Frustum::INSIDE)
                {
-                       node->draw(alpha);
+                       node->getAll(insertables);
                }
                else // collision == Frustum::INTERSECT
                {
                }
                else // collision == Frustum::INTERSECT
                {
-                       node->drawIfVisible(alpha, frustum);
+                       node->getIfVisible(insertables, frustum);
                }
 
                if (tree_.children(node) > 0)
                }
 
                if (tree_.children(node) > 0)
@@ -234,7 +262,7 @@ private:
                                        NodeP child = tree_.child(node, i);
                                        ASSERT(child.valid() && "expected valid child node");
 
                                        NodeP child = tree_.child(node, i);
                                        ASSERT(child.valid() && "expected valid child node");
 
-                                       draw(alpha, child);
+                                       getAll(insertables, child);
                                }
                        }
                        else // collision == Frustum::INTERSECT
                                }
                        }
                        else // collision == Frustum::INTERSECT
@@ -244,12 +272,13 @@ private:
                                        NodeP child = tree_.child(node, i);
                                        ASSERT(child.valid() && "expected valid child node");
 
                                        NodeP child = tree_.child(node, i);
                                        ASSERT(child.valid() && "expected valid child node");
 
-                                       drawIfVisible(alpha, frustum, child);
+                                       getIfVisible(insertables, frustum, child);
                                }
                        }
                }
        }
 
                                }
                        }
                }
        }
 
+
        mutable stlplus::ntree<Node> tree_;
 
 
        mutable stlplus::ntree<Node> tree_;
 
 
@@ -257,9 +286,9 @@ public:
 
        void print(NodeP node)
        {
 
        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)
        }
 
        static Ptr alloc(const Node& rootNode)
@@ -272,12 +301,13 @@ public:
                tree_.insert(rootNode);
        }
 
                tree_.insert(rootNode);
        }
 
+
        NodeP insert(InsertableP entity)
        {
                return insert(entity, tree_.root());
        }
 
        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");
        {
                ASSERT(entity && "null entity passed");
                ASSERT(node.valid() && "invalid node passed");
@@ -289,18 +319,63 @@ public:
                {
                        node->objects.erase(it);
                }
                {
                        node->objects.erase(it);
                }
+       }
 
 
+
+       NodeP reinsert(InsertableP entity, NodeP node)
+       {
+               remove(entity, node);
                return insert(entity);
        }
 
                return insert(entity);
        }
 
+
        void draw(Scalar alpha) const
        {
        void draw(Scalar alpha) const
        {
-               draw(alpha, tree_.root());
+               std::list<InsertableP> objects;
+               getAll(objects);
+
+               typename std::list<InsertableP>::const_iterator it;
+               for (it = objects.begin(); it != objects.end(); ++it)
+               {
+                       (*it)->draw(alpha);
+               }
        }
 
        void drawIfVisible(Scalar alpha, const Frustum& frustum) const
        {
        }
 
        void drawIfVisible(Scalar alpha, const Frustum& frustum) const
        {
-               drawIfVisible(alpha, frustum, tree_.root());
+               std::list<InsertableP> objects;
+               //getIfVisible(objects, frustum);
+               getNearbyObjects(objects, *savedObj);
+
+               typename std::list<InsertableP>::const_iterator it;
+               for (it = objects.begin(); it != objects.end(); ++it)
+               {
+                       (*it)->draw(alpha);
+               }
+       }
+
+
+       void getAll(std::list<InsertableP>& insertables) const
+       {
+               getAll(insertables, tree_.root());
+       }
+
+       void getIfVisible(std::list<InsertableP>& insertables,
+                       const Frustum& frustum) const
+       {
+               getIfVisible(insertables, frustum, tree_.root());
+       }
+
+       mutable const OctreeInsertable* savedObj;
+
+
+       void getNearbyObjects(std::list<InsertableP>& insertables,
+                       const OctreeInsertable& entity) const
+       {
+               logDebug("--- GETTING NEARBY");
+               getNearbyObjects(insertables, entity, tree_.root());
+               logDebug("---");
+               savedObj = &entity;
        }
 };
 
        }
 };
 
This page took 0.028963 seconds and 4 git commands to generate.