]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Octree.hh
beginning CD implementation
[chaz/yoink] / src / Moof / Octree.hh
index 186961f00e7b3d28a55ea7e6bef9a69c7aceb945..88dae7ac6492e587d1bb133516a27b4343d98735 100644 (file)
@@ -39,6 +39,7 @@
 #include <Moof/Aabb.hh>
 #include <Moof/Drawable.hh>
 #include <Moof/Entity.hh>
+#include <Moof/Frustum.hh>
 #include <Moof/Log.hh>
 #include <Moof/Math.hh>
 #include <Moof/Sphere.hh>
 namespace Mf {
 
 
-class Frustum;
-
-
-struct OctreeNode;
-typedef stlplus::ntree<OctreeNode>::iterator OctreeNodeP;
+struct OctreeInsertable
+{
+       virtual ~OctreeInsertable() {}
 
-class Octree;
-typedef boost::shared_ptr<Octree> OctreeP;
+       virtual bool isInsideAabb(const Aabb& aabb) const = 0;
+       virtual int getOctant(const Aabb& aabb) const = 0;
+};
 
 
-struct OctreeNode : public Entity
+template <class T>
+class Octree : public Entity
 {
-       std::list<EntityP> objects;
+       typedef boost::shared_ptr<T> 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<InsertableP> objects;
+
+               Aabb aabb;
+               Sphere sphere;
+
+               Node(const Aabb& box) :
+                       aabb(box)
+               {
+                       sphere.point = aabb.getCenter();
+                       sphere.radius = (aabb.min - sphere.point).length();
+               }
+
+               void draw(Scalar alpha) const
+               {
+                       aabb.draw(alpha);
+               }
+
+               void drawIfVisible(Scalar alpha, const Frustum& frustum) const
+               {
+                       if (isVisible(frustum))
+                       {
+                               aabb.draw(alpha);
+                       }
+               }
+
+               void printSize()
+               {
+                       logDebug("size of node %d", objects.size());
+               }
+
+               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)
+                       {
+                               if ((*it)->isVisible(frustum)) insertables.push_back(*it);
+                       }
+               }
+
+
+               bool isVisible(const Frustum& frustum) const
+               {
+                       if (sphere.isVisible(frustum))
+                       {
+                               return aabb.isVisible(frustum);
+                       }
+
+                       return false;
+               }
+       };
+
+
+public:
 
-       OctreeNode(const Aabb& aabb)
+       typedef boost::shared_ptr<Octree> Ptr;
+       typedef typename stlplus::ntree<Node>::iterator NodeP;
+       
+private:
+
+
+       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");
+
+               if (entity->isInsideAabb(node->aabb))
+               {
+                       return insert_recurse(entity, node);
+               }
+               else
+               {
+                       node->objects.push_back(entity);
+                       return node;
+               }
        }
 
-       void draw(Scalar alpha) const
+       NodeP insert_recurse(InsertableP entity, NodeP node)
        {
-               std::list<EntityP>::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->aabb);
+               if (octantNum == -1)
                {
-                       (*it)->draw(alpha);
+                       node->objects.push_back(entity);
+                       return node;
                }
+               else
+               {
+                       if ((int)tree_.children(node) <= octantNum)
+                       {
+                               addChild(node, octantNum);
+                       }
+
+                       NodeP child = tree_.child(node, octantNum);
+                       ASSERT(child.valid() && "expected valid child node");
 
-               if (!objects.empty())
-                       aabb_.draw(); // temporary
+                       return insert_recurse(entity, child);
+               }
        }
 
-       void drawIfVisible(Scalar alpha, const Frustum& frustum) const
+       void addChild(NodeP node, int index)
        {
-               std::list<EntityP>::const_iterator it;
+               ASSERT(node.valid() && "invalid node passed");
 
-               for (it = objects.begin(); it != objects.end(); ++it)
+               Aabb octant;
+
+               for (int i = tree_.children(node); i <= index; ++i)
                {
-                       (*it)->drawIfVisible(alpha, frustum);
+                       node->aabb.getOctant(octant, i);
+                       tree_.append(node, octant);
                }
-
-               if (!objects.empty())
-                       aabb_.draw();
        }
 
 
-       bool isVisible(const Frustum& frustum) const
+       void getNearbyObjects(std::list<InsertableP>& insertables,
+                       const OctreeInsertable& entity, NodeP node) const
        {
-               if (sphere_.isVisible(frustum))
+               ASSERT(node.valid() && "invalid node passed");
+
+               node->printSize();
+
+               int octantNum = entity.getOctant(node->aabb);
+               if (octantNum != -1)
                {
-                       return aabb_.isVisible(frustum);
-               }
+                       node->getAll(insertables);
+
+                       if (octantNum < (int)tree_.children(node))
+                       {
+                               NodeP child = tree_.child(node, octantNum);
+                               ASSERT(child.valid() && "expected valid child node");
 
-               return false;
+                               getNearbyObjects(insertables, entity, child);
+                       }
+               }
+               else
+               {
+                       logDebug("getting all the rest...");
+                       getAll(insertables, node);
+               }
        }
 
 
-       static bool compareZOrder(EntityP a, EntityP b)
+       void getAll(std::list<InsertableP>& insertables, NodeP node) const
        {
-               return a->getSphere().point[2] < b->getSphere().point[2];
+               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");
+
+                       getAll(insertables, child);
+               }
        }
 
-       void sort()
+       void getIfVisible(std::list<InsertableP>& insertables,
+                       const Frustum& frustum, NodeP node) const
        {
-               //std::sort(objects.begin(), objects.end(), compareZOrder);
-               objects.sort(compareZOrder);
-       }
-};
+               ASSERT(node.valid() && "invalid node passed");
 
+               // try to cull by sphere
+               Frustum::Collision collision = frustum.contains(node->sphere);
+               if (collision == Frustum::OUTSIDE) return;
 
-class Octree
-{
-       OctreeNodeP insert(EntityP entity, OctreeNodeP node);
-       
-       void addChild(OctreeNodeP node, int index);
+               // try to cull by aabb
+               collision = frustum.contains(node->aabb);
+               if (collision == Frustum::OUTSIDE) return;
 
-       void draw(Scalar alpha, OctreeNodeP node);
-       void drawIfVisible(Scalar alpha, const Frustum& frustum, OctreeNodeP node);
 
-       stlplus::ntree<OctreeNode> tree_;
+               if (collision == Frustum::INSIDE)
+               {
+                       node->getAll(insertables);
+               }
+               else // collision == Frustum::INTERSECT
+               {
+                       node->getIfVisible(insertables, frustum);
+               }
+
+               if (tree_.children(node) > 0)
+               {
+                       if (collision == Frustum::INSIDE)
+                       {
+                               for (unsigned i = 0; i < tree_.children(node); ++i)
+                               {
+                                       NodeP child = tree_.child(node, i);
+                                       ASSERT(child.valid() && "expected valid child node");
+
+                                       getAll(insertables, child);
+                               }
+                       }
+                       else // collision == Frustum::INTERSECT
+                       {
+                               for (unsigned i = 0; i < tree_.children(node); ++i)
+                               {
+                                       NodeP child = tree_.child(node, i);
+                                       ASSERT(child.valid() && "expected valid child node");
+
+                                       getIfVisible(insertables, frustum, child);
+                               }
+                       }
+               }
+       }
+
+
+       mutable stlplus::ntree<Node> tree_;
+
 
 public:
 
-       void print(OctreeNodeP 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 OctreeP alloc(const OctreeNode& rootNode)
+       static Ptr alloc(const Node& rootNode)
        {
-               return OctreeP(new Octree(rootNode));
+               return Ptr(new Octree(rootNode));
        }
 
-       explicit Octree(const OctreeNode& rootNode)
+       explicit Octree(const Node& rootNode)
        {
                tree_.insert(rootNode);
        }
 
-       OctreeNodeP insert(EntityP entity)
+
+       NodeP insert(InsertableP entity)
        {
                return insert(entity, tree_.root());
        }
 
-       OctreeNodeP reinsert(EntityP entity, OctreeNodeP node);
+       void remove(InsertableP entity, NodeP node)
+       {
+               ASSERT(entity && "null entity passed");
+               ASSERT(node.valid() && "invalid node passed");
+
+               typename std::list<InsertableP>::iterator it;
+               it = std::find(node->objects.begin(), node->objects.end(), entity);
+
+               if (it != node->objects.end())
+               {
+                       node->objects.erase(it);
+               }
+       }
+
+
+       NodeP reinsert(InsertableP entity, NodeP node)
+       {
+               remove(entity, node);
+               return insert(entity);
+       }
+
 
-       void drawIfVisible(Scalar alpha, const Frustum& frustum)
+       void draw(Scalar alpha) const
        {
-               drawIfVisible(alpha, frustum, 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 sort();
+       void drawIfVisible(Scalar alpha, const Frustum& frustum) const
+       {
+               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.027234 seconds and 4 git commands to generate.