#ifndef _MOOF_OCTREE_HH_
#define _MOOF_OCTREE_HH_
+#include <algorithm>
#include <list>
#include <boost/shared_ptr.hpp>
+#include <stlplus/ntree.hpp>
+
+#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/Tree.hh>
+#include <Moof/Sphere.hh>
namespace Mf {
-class Entity;
+//class Octree;
+//typedef boost::shared_ptr<Octree> OctreeP;
+
+//class Octree::Node;
+//typedef stlplus::ntree<Octree::Node>::iterator OctreeNodeP;
-class Octree
+struct OctreeInsertable
{
-public:
+ virtual ~OctreeInsertable() {}
+
+ virtual bool isInsideAabb(const Aabb& aabb) const = 0;
+ virtual int getOctant(const Aabb& aabb) const = 0;
+};
- class Node
+
+template <class T>
+class Octree : public Entity
+{
+ typedef boost::shared_ptr<T> InsertableP;
+
+ struct Node : public Entity
{
- Aabb aabb_;
- Vector3 center_;
+ std::list<InsertableP> objects;
- std::list<boost::shared_ptr<Entity> > objects_;
+ Aabb aabb;
+ Sphere sphere;
- public:
+ Node(const Aabb& box) :
+ aabb(box)
+ {
+ sphere.point = aabb.getCenter();
+ sphere.radius = (aabb.min - sphere.point).length();
+ }
- Node() :
- aabb_(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0),
- center_(0.0, 0.0, 0.0) {}
+ void draw(Scalar alpha) const
+ {
+ typename std::list<InsertableP>::const_iterator it;
- Node(const Aabb& aabb) :
- aabb_(aabb),
- center_(aabb.getCenter()) {}
- };
+ for (it = objects.begin(); it != objects.end(); ++it)
+ {
+ (*it)->draw(alpha);
+ }
- Octree() :
- root_(new Tree<Node>()) {}
+ if (!objects.empty())
+ aabb.draw(); // temporary
+ }
- Octree(const Aabb& aabb) :
- root_(new Tree<Node>(Node(aabb))) {}
+ void drawIfVisible(Scalar alpha, const Frustum& frustum) const
+ {
+ typename std::list<InsertableP>::const_iterator it;
+ for (it = objects.begin(); it != objects.end(); ++it)
+ {
+ (*it)->drawIfVisible(alpha, frustum);
+ }
- Tree<Node>::WeakPtr add(EntityPtr object);
+ if (!objects.empty())
+ {
+ //aabb.draw();
+ //sphere.draw();
+ }
+ }
+
+ bool isVisible(const Frustum& frustum) const
+ {
+ if (sphere.isVisible(frustum))
+ {
+ return aabb.isVisible(frustum);
+ }
+
+ return false;
+ }
+ };
+
+
+public:
+
+ typedef boost::shared_ptr<Octree> Ptr;
+ typedef typename stlplus::ntree<Node>::iterator NodeP;
+
private:
- Tree<Node>::Ptr root_;
+
+
+ NodeP insert(InsertableP entity, NodeP node)
+ {
+ 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;
+ }
+ }
+
+ NodeP insert_recurse(InsertableP entity, NodeP node)
+ {
+ ASSERT(node.valid() && "invalid node passed");
+ ASSERT(entity && "null entity passed");
+
+ int octantNum = entity->getOctant(node->aabb);
+ if (octantNum == -1)
+ {
+ 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");
+
+ return insert(entity, child);
+ }
+ }
+
+ void addChild(NodeP node, int index)
+ {
+ ASSERT(node.valid() && "invalid node passed");
+
+ Aabb octant;
+
+ for (int i = tree_.children(node); i <= index; ++i)
+ {
+ node->aabb.getOctant(octant, i);
+ tree_.append(node, octant);
+ }
+ }
+
+
+ void draw(Scalar alpha, NodeP node) const
+ {
+ ASSERT(node.valid() && "invalid node passed");
+
+ node->draw(alpha);
+
+ 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);
+ }
+ }
+
+ void drawIfVisible(Scalar alpha, const Frustum& frustum, NodeP node) const
+ {
+ ASSERT(node.valid() && "invalid node passed");
+
+ // try to cull by sphere
+ Frustum::Collision collision = frustum.contains(node->sphere);
+ if (collision == Frustum::OUTSIDE) return;
+
+ // try to cull by aabb
+ collision = frustum.contains(node->aabb);
+ if (collision == Frustum::OUTSIDE) return;
+
+
+ if (collision == Frustum::INSIDE)
+ {
+ node->draw(alpha);
+ }
+ else // collision == Frustum::INTERSECT
+ {
+ node->drawIfVisible(alpha, 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");
+
+ draw(alpha, 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");
+
+ drawIfVisible(alpha, frustum, child);
+ }
+ }
+ }
+ }
+
+ mutable stlplus::ntree<Node> tree_;
+
+
+public:
+
+ void print(NodeP node)
+ {
+ //logDebug("-----");
+ //logDebug("depth to node: %d", tree_.depth(node));
+ //logDebug("size of node: %d", tree_.size(node));
+ }
+
+ static Ptr alloc(const Node& rootNode)
+ {
+ return Ptr(new Octree(rootNode));
+ }
+
+ explicit Octree(const Node& rootNode)
+ {
+ tree_.insert(rootNode);
+ }
+
+ NodeP insert(InsertableP entity)
+ {
+ return insert(entity, tree_.root());
+ }
+
+ NodeP reinsert(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);
+ }
+
+ return insert(entity);
+ }
+
+ void draw(Scalar alpha) const
+ {
+ draw(alpha, tree_.root());
+ }
+
+ void drawIfVisible(Scalar alpha, const Frustum& frustum) const
+ {
+ drawIfVisible(alpha, frustum, tree_.root());
+ }
};