-/*******************************************************************************
-
- Copyright (c) 2009, Charles McGarvey
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-*******************************************************************************/
+/*] Copyright (c) 2009-2010, Charles McGarvey [**************************
+**] All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
#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/Sphere.hh>
namespace Mf {
-struct OctreeNode : public Entity
+struct OctreeInsertable
{
- std::list<EntityPtr> objects;
+ virtual ~OctreeInsertable() {}
- 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);
- }
+ virtual int getOctant(const Aabb<3>& aabb) const = 0;
+};
- OctreeNode(const Aabb& aabb)
- {
- aabb_ = aabb;
- sphere_.point = aabb.getCenter();
- sphere_.radius = (aabb.min - sphere_.point).length();
- }
- void draw(Scalar alpha) const
+template <class T>
+class Octree : public Entity
+{
+ typedef boost::shared_ptr<T> InsertableP;
+
+ struct Node : public Entity
{
- std::list<EntityPtr>::const_iterator it;
+ std::list<InsertableP> objects;
- for (it = objects.begin(); it != objects.end(); ++it)
+ Node(const Aabb<3>& aabb)
{
- (*it)->draw(alpha);
+ mAabb = aabb;
+ mSphere.point = mAabb.getCenter();
+ mSphere.radius = (mAabb.min - mSphere.point).length();
}
- 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)
+ void draw(Scalar alpha) const
{
- (*it)->drawIfVisible(alpha, cam);
+ mAabb.draw(alpha);
}
- if (!objects.empty())
- aabb_.draw();
- }
-
- bool isVisible(const Camera& cam) const
- {
- if (sphere_.isVisible(cam))
+ void printSize()
{
- return aabb_.isVisible(cam);
+ logInfo << "size of node " << objects.size() << std::endl;
}
- return false;
- }
-};
-
+ void getAll(std::list<InsertableP>& insertables) const
+ {
+ insertables.insert(insertables.end(), objects.begin(),
+ objects.end());
+ }
-class Octree;
-typedef boost::shared_ptr<Octree> OctreePtr;
+ void getIfVisible(std::list<InsertableP>& insertables,
+ const Frustum& frustum) const
+ {
+ typename std::list<InsertableP>::const_iterator it;
-class Octree
-{
+ for (it = objects.begin(); it != objects.end(); ++it)
+ {
+ if ((*it)->isVisible(frustum)) insertables.push_back(*it);
+ }
+ }
+ };
- stlplus::ntree<OctreeNode> root_;
public:
- explicit Octree(const OctreeNode& rootNode)
- {
- root_.insert(rootNode);
- }
+ typedef boost::shared_ptr<Octree> Ptr;
+ typedef typename stlplus::ntree<Node>::iterator NodeP;
+
+private:
- void insert(EntityPtr entity)
- {
- insert(root_.root(), entity);
- }
- void insert(stlplus::ntree<OctreeNode>::iterator node, EntityPtr entity)
+ NodeP insert(InsertableP entity, NodeP node)
{
- Plane::Halfspace halfspace;
- int octantNum = -1;
-
- if (!node.valid())
+ ASSERT(node.valid() && "invalid node passed");
+ ASSERT(entity && "null entity passed");
+
+ Aabb<3> entityAabb = entity->getAabb();
+ Aabb<3> nodeAabb = node->getAabb();
+
+ if (!(entityAabb.max[0] < nodeAabb.max[0] &&
+ entityAabb.min[0] > nodeAabb.min[0] &&
+ entityAabb.max[1] < nodeAabb.max[1] &&
+ entityAabb.min[1] > nodeAabb.min[1] &&
+ entityAabb.max[2] < nodeAabb.max[2] &&
+ entityAabb.min[2] > nodeAabb.min[2]))
{
- std::cerr << "cannot insert into invalid node" << std::endl;
- return;
+ node->objects.push_back(entity);
+ return node;
}
-
- Plane xy = node->getAabb().getPlaneXY();
- halfspace = xy.intersectsSphere(entity->getSphere());
-
- if (halfspace == Plane::POSITIVE)
+ else
{
- Plane xz = node->getAabb().getPlaneXZ();
- halfspace = xz.intersectsSphere(entity->getSphere());
-
- if (halfspace == Plane::POSITIVE)
- {
- Plane yz = node->getAabb().getPlaneYZ();
- halfspace = yz.intersectsSphere(entity->getSphere());
-
- if (halfspace == Plane::POSITIVE)
- {
- octantNum = 2;
- }
- else if (halfspace == Plane::NEGATIVE)
- {
- octantNum = 3;
- }
- }
- else if (halfspace == Plane::NEGATIVE)
- {
- Plane yz = node->getAabb().getPlaneYZ();
- halfspace = yz.intersectsSphere(entity->getSphere());
-
- if (halfspace == Plane::POSITIVE)
- {
- octantNum = 1;
- }
- else if (halfspace == Plane::NEGATIVE)
- {
- octantNum = 0;
- }
- }
+ return insert_recurse(entity, node);
}
- else if (halfspace == Plane::NEGATIVE)
- {
- Plane xz = node->getAabb().getPlaneXZ();
- halfspace = xz.intersectsSphere(entity->getSphere());
-
- if (halfspace == Plane::POSITIVE)
- {
- Plane yz = node->getAabb().getPlaneYZ();
- halfspace = yz.intersectsSphere(entity->getSphere());
-
- if (halfspace == Plane::POSITIVE)
- {
- octantNum = 6;
- }
- else if (halfspace == Plane::NEGATIVE)
- {
- octantNum = 7;
- }
- }
- else if (halfspace == Plane::NEGATIVE)
- {
- Plane yz = node->getAabb().getPlaneYZ();
- halfspace = yz.intersectsSphere(entity->getSphere());
+ }
- if (halfspace == Plane::POSITIVE)
- {
- octantNum = 5;
- }
- else if (halfspace == Plane::NEGATIVE)
- {
- octantNum = 4;
- }
- }
- }
+ NodeP insert_recurse(InsertableP entity, NodeP node)
+ {
+ ASSERT(node.valid() && "invalid node passed");
+ ASSERT(entity && "null entity passed");
+ int octantNum = entity->getOctant(node->getAabb());
if (octantNum == -1)
{
- node->objects.push_front(entity);
- //return node;
+ node->objects.push_back(entity);
+ return node;
}
else
{
- if (root_.children(node) == 0)
+ if ((int)mTree.children(node) <= octantNum)
{
- addChildren(node);
+ addChild(node, octantNum);
}
- stlplus::ntree<OctreeNode>::iterator child = root_.child(node, octantNum);
+ NodeP child = mTree.child(node, octantNum);
+ ASSERT(child.valid() && "expected valid child node");
- if (child.valid())
- {
- return insert(child, entity);
- }
- else
- {
- std::cerr << "expected but found no child at index " << octantNum << std::endl;
- //return stlplus::ntree<OctreeNode>::iterator();
- }
- //return WeakPtr();
+ return insert_recurse(entity, child);
}
}
- void addChildren(stlplus::ntree<OctreeNode>::iterator node)
+ void addChild(NodeP node, int index)
{
- Aabb octant;
+ ASSERT(node.valid() && "invalid node passed");
- if (!node.valid())
- {
- std::cerr << "cannot add children to invalid node" << std::endl;
- return;
- }
+ Aabb<3> octant;
- for (int i = 0; i < 8; ++i)
+ for (int i = mTree.children(node); i <= index; ++i)
{
node->getAabb().getOctant(octant, i);
- //OctreeNode octantNode(octant);
-
- root_.append(node, octant);
+ mTree.append(node, octant);
}
}
- void draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha)
+
+ void getNearbyObjects(std::list<InsertableP>& insertables,
+ const OctreeInsertable& entity, NodeP node) const
{
- if (!node.valid())
- {
- std::cerr << "cannot draw null child node :-(" << std::endl;
- return;
- }
+ ASSERT(node.valid() && "invalid node passed");
- node->draw(alpha);
+ node->printSize();
- for (unsigned i = 0; i < root_.children(node); ++i)
+ int octantNum = entity.getOctant(node->getAabb());
+ if (octantNum != -1)
{
- stlplus::ntree<OctreeNode>::iterator child = root_.child(node, i);
+ node->getAll(insertables);
- if (child.valid())
- {
- draw(child, alpha);
- }
- else
+ if (octantNum < (int)mTree.children(node))
{
- std::cerr << "node is not a leaf, but has an invalid child" << std::endl;
- }
+ NodeP child = mTree.child(node, octantNum);
+ ASSERT(child.valid() && "expected valid child node");
+ getNearbyObjects(insertables, entity, child);
+ }
+ }
+ else
+ {
+ logInfo("getting all the rest...");
+ getAll(insertables, node);
}
}
- void drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
- Scalar alpha, const Camera& cam)
+
+ void getAll(std::list<InsertableP>& insertables, NodeP node) const
{
- //node.drawIfVisible(alpha, cam);
-
- if (!node.valid())
+ ASSERT(node.valid() && "invalid node passed");
+
+ node->getAll(insertables);
+
+ for (unsigned i = 0; i < mTree.children(node); ++i)
{
- std::cerr << "invalid child while drawing :-(" << std::endl;
- return;
+ NodeP child = mTree.child(node, i);
+ ASSERT(child.valid() && "expected valid child node");
+
+ getAll(insertables, child);
}
+ }
- Frustum::Collision collision =
- cam.getFrustum().containsSphere(node->getSphere());
+ void getIfVisible(std::list<InsertableP>& insertables,
+ const Frustum& frustum, NodeP node) const
+ {
+ ASSERT(node.valid() && "invalid node passed");
+
+ // try to cull by sphere
+ Frustum::Collision collision = frustum.contains(node->getSphere());
if (collision == Frustum::OUTSIDE) return;
- collision = cam.getFrustum().containsAabb(node->getAabb());
+ // try to cull by aabb
+ collision = frustum.contains(node->getAabb());
if (collision == Frustum::OUTSIDE) return;
if (collision == Frustum::INSIDE)
{
- node->draw(alpha);
+ node->getAll(insertables);
}
else // collision == Frustum::INTERSECT
{
- node->drawIfVisible(alpha, cam);
+ node->getIfVisible(insertables, frustum);
}
- if (root_.children(node) > 0)
+ if (mTree.children(node) > 0)
{
if (collision == Frustum::INSIDE)
{
- for (unsigned i = 0; i < root_.children(node); ++i)
+ for (unsigned i = 0; i < mTree.children(node); ++i)
{
- stlplus::ntree<OctreeNode>::iterator child = root_.child(node, i);
-
- if (child.valid())
- {
- draw(child, alpha);
- }
- else
- {
- std::cerr << "node is not a leaf, but has an invalid child" << std::endl;
- }
+ NodeP child = mTree.child(node, i);
+ ASSERT(child.valid() && "expected valid child node");
+ getAll(insertables, child);
}
}
else // collision == Frustum::INTERSECT
{
- for (unsigned i = 0; i < root_.children(node); ++i)
+ for (unsigned i = 0; i < mTree.children(node); ++i)
{
- stlplus::ntree<OctreeNode>::iterator child = root_.child(node, i);
-
- if (child.valid())
- {
- drawIfVisible(child, alpha, cam);
- }
- else
- {
- std::cerr << "node is not a leaf, but has an invalid child" << std::endl;
- }
+ NodeP child = mTree.child(node, i);
+ ASSERT(child.valid() && "expected valid child node");
+
+ getIfVisible(insertables, frustum, child);
}
}
}
}
- void drawIfVisible(Scalar alpha, const Camera& cam)
+
+ mutable stlplus::ntree<Node> mTree;
+
+
+public:
+
+ void print(NodeP node)
{
- drawIfVisible(root_.root(), alpha, cam);
+ logInfo("-----");
+ logInfo << "depth to node: " << mTree.depth(node) << std::endl;
+ logInfo << "size of node: " << mTree.size(node) << std::endl;
}
-};
+ static Ptr alloc(const Node& rootNode)
+ {
+ return Ptr(new Octree(rootNode));
+ }
+
+ explicit Octree(const Node& rootNode)
+ {
+ mTree.insert(rootNode);
+ }
+
+
+ NodeP insert(InsertableP entity)
+ {
+ return insert(entity, mTree.root());
+ }
+
+ 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 draw(Scalar alpha) const
+ {
+ 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
+ {
+ 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, mTree.root());
+ }
+
+ void getIfVisible(std::list<InsertableP>& insertables,
+ const Frustum& frustum) const
+ {
+ getIfVisible(insertables, frustum, mTree.root());
+ }
+
+ mutable const OctreeInsertable* savedObj;
+
+
+ void getNearbyObjects(std::list<InsertableP>& insertables,
+ const OctreeInsertable& entity) const
+ {
+ logInfo("--- GETTING NEARBY");
+ getNearbyObjects(insertables, entity, mTree.root());
+ logInfo("---");
+ savedObj = &entity;
+ }
+};
} // namespace Mf
#endif // _MOOF_OCTREE_HH_
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
-