#include <boost/shared_ptr.hpp>
+#include <Moof/Aabb.hh>
+#include <Moof/Drawable.hh>
+#include <Moof/Entity.hh>
#include <Moof/Math.hh>
+#include <Moof/Sphere.hh>
#include <Moof/Tree.hh>
namespace Mf {
-class Entity;
+struct OctreeNode : public Entity
+{
+ std::list<EntityPtr> objects;
+
+ OctreeNode()
+ {
+ 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);
+ }
+
+ OctreeNode(const Aabb& aabb)
+ {
+ aabb_ = aabb;
+ sphere_.point = aabb.getCenter();
+ sphere_.radius = (aabb.min - sphere_.point).length();
+ }
+
+ void draw(Scalar alpha) const
+ {
+ std::list<EntityPtr>::const_iterator it;
+
+ for (it = objects.begin(); it != objects.end(); ++it)
+ {
+ (*it)->draw(alpha);
+ }
+ if (!objects.empty())
+ aabb_.draw(); // temporary
+ }
+
+ void drawIfVisible(Scalar alpha, const Camera& cam) const
+ {
+ std::list<EntityPtr>::const_iterator it;
+
+ for (it = objects.begin(); it != objects.end(); ++it)
+ {
+ (*it)->drawIfVisible(alpha, cam);
+ }
+ if (!objects.empty())
+ aabb_.draw();
+ }
+
+ bool isVisible(const Camera& cam) const
+ {
+ if (sphere_.isVisible(cam))
+ {
+ return aabb_.isVisible(cam);
+ }
+
+ return false;
+ }
+};
-class Octree
+
+class Octree;
+typedef boost::shared_ptr<Octree> OctreePtr;
+
+class Octree : public Tree<OctreeNode>
{
+
+ Octree() {}
+
+ explicit Octree(const OctreeNode& initNode) :
+ Tree<OctreeNode>(initNode) {}
+
public:
- class Node
+ inline static OctreePtr createNewNode(const OctreeNode& item)
+ {
+ OctreePtr newNode = OctreePtr(new Octree(item));
+ init(newNode);
+ return newNode;
+ }
+
+
+ static Tree<OctreeNode>::WeakPtr add(Ptr node, EntityPtr entity)
{
- Aabb aabb_;
- Vector3 center_;
+ Plane::Halfspace halfspace;
+ int octantNum = -1;
+
+ Plane xy = node->node.getAabb().getPlaneXY();
+ halfspace = xy.intersectsSphere(entity->getSphere());
- std::list<boost::shared_ptr<Entity> > objects_;
+ if (halfspace == Plane::POSITIVE)
+ {
+ Plane xz = node->node.getAabb().getPlaneXZ();
+ halfspace = xz.intersectsSphere(entity->getSphere());
- public:
+ if (halfspace == Plane::POSITIVE)
+ {
+ Plane yz = node->node.getAabb().getPlaneYZ();
+ halfspace = yz.intersectsSphere(entity->getSphere());
- Node() :
- aabb_(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0),
- center_(0.0, 0.0, 0.0) {}
+ if (halfspace == Plane::POSITIVE)
+ {
+ octantNum = 2;
+ }
+ else if (halfspace == Plane::NEGATIVE)
+ {
+ octantNum = 3;
+ }
+ }
+ else if (halfspace == Plane::NEGATIVE)
+ {
+ Plane yz = node->node.getAabb().getPlaneYZ();
+ halfspace = yz.intersectsSphere(entity->getSphere());
- Node(const Aabb& aabb) :
- aabb_(aabb),
- center_(aabb.getCenter()) {}
- };
+ if (halfspace == Plane::POSITIVE)
+ {
+ octantNum = 1;
+ }
+ else if (halfspace == Plane::NEGATIVE)
+ {
+ octantNum = 0;
+ }
+ }
+ }
+ else if (halfspace == Plane::NEGATIVE)
+ {
+ Plane xz = node->node.getAabb().getPlaneXZ();
+ halfspace = xz.intersectsSphere(entity->getSphere());
- Octree() :
- root_(new Tree<Node>()) {}
+ if (halfspace == Plane::POSITIVE)
+ {
+ Plane yz = node->node.getAabb().getPlaneYZ();
+ halfspace = yz.intersectsSphere(entity->getSphere());
- Octree(const Aabb& aabb) :
- root_(new Tree<Node>(Node(aabb))) {}
+ if (halfspace == Plane::POSITIVE)
+ {
+ octantNum = 6;
+ }
+ else if (halfspace == Plane::NEGATIVE)
+ {
+ octantNum = 7;
+ }
+ }
+ else if (halfspace == Plane::NEGATIVE)
+ {
+ Plane yz = node->node.getAabb().getPlaneYZ();
+ halfspace = yz.intersectsSphere(entity->getSphere());
+ if (halfspace == Plane::POSITIVE)
+ {
+ octantNum = 5;
+ }
+ else if (halfspace == Plane::NEGATIVE)
+ {
+ octantNum = 4;
+ }
+ }
+ }
- Tree<Node>::WeakPtr add(EntityPtr object);
+ if (octantNum == -1)
+ {
+ node->node.objects.push_front(entity);
+ return node;
+ }
+ else
+ {
+ if (node->isLeaf())
+ {
+ addChildren(node);
+ }
+
+ Ptr child = node->getChild(octantNum);
+ if (child)
+ {
+ return add(child, entity);
+ }
+ else
+ {
+ std::cerr << "no child at index " << octantNum << std::endl;
+ return Ptr();
+ }
+ //return WeakPtr();
+ }
+ }
+
+ static void addChildren(Ptr node)
+ {
+ Aabb octant;
+
+ for (int i = 0; i < 8; ++i)
+ {
+ node->node.getAabb().getOctant(octant, i);
+ //OctreeNode octantNode(octant);
+
+ Ptr newChild = createNewNode(octant);
+ node->addChild(newChild);
+ }
+ }
+
+ void draw(Ptr node, Scalar alpha)
+ {
+ if (!node)
+ {
+ std::cerr << "null child :-(" << std::endl;
+ return;
+ }
+
+ node->node.draw(alpha);
+
+ if (!node->isLeaf())
+ {
+ Ptr firstChild = node->getFirstChild();
+ Ptr temp = firstChild;
+
+ if (!firstChild)
+ {
+ std::cerr << "node is not a leaf, but has no first child :-(" << std::endl;
+ return;
+ }
+
+ do
+ {
+ draw(temp, alpha);
+ temp = temp->getNextSibling();
+ }
+ while (temp && temp != firstChild);
+ }
+ }
+
+ void drawIfVisible(Ptr node, Scalar alpha, const Camera& cam)
+ {
+ //node.drawIfVisible(alpha, cam);
+
+ if (!node)
+ {
+ std::cerr << "null child :-(" << std::endl;
+ return;
+ }
+
+ Frustum::Collision collision =
+ cam.getFrustum().containsSphere(node->node.getSphere());
+ if (collision == Frustum::OUTSIDE) return;
+
+ collision = cam.getFrustum().containsAabb(node->node.getAabb());
+ if (collision == Frustum::OUTSIDE) return;
+
+
+ if (collision == Frustum::INSIDE)
+ {
+ node->node.draw(alpha);
+ }
+ else // collision == Frustum::INTERSECT
+ {
+ node->node.drawIfVisible(alpha, cam);
+ }
+
+ if (!node->isLeaf())
+ {
+ Ptr firstChild = node->getFirstChild();
+ Ptr temp = firstChild;
+
+ if (!firstChild)
+ {
+ std::cerr << "node is not a leaf, but has no first child :-(" << std::endl;
+ return;
+ }
+
+ if (collision == Frustum::INSIDE)
+ {
+ do
+ {
+ draw(temp, alpha);
+ temp = temp->getNextSibling();
+ }
+ while (temp && temp != firstChild);
+ }
+ else // collision == Frustum::INTERSECT
+ {
+ do
+ {
+ drawIfVisible(temp, alpha, cam);
+ temp = temp->getNextSibling();
+ }
+ while (temp && temp != firstChild);
+ }
+ }
+ }
+
+ void drawIfVisible(Scalar alpha, const Camera& cam)
+ {
+ drawIfVisible(getThis(), alpha, cam);
+ }
-private:
- Tree<Node>::Ptr root_;
};
+
} // namespace Mf
#endif // _MOOF_OCTREE_HH_