namespace Mf {
-//class Octree;
-//typedef boost::shared_ptr<Octree> OctreeP;
-
-//class Octree::Node;
-//typedef stlplus::ntree<Octree::Node>::iterator OctreeNodeP;
-
-
struct OctreeInsertable
{
virtual ~OctreeInsertable() {}
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)
{
- (*it)->drawIfVisible(alpha, frustum);
- }
-
- if (!objects.empty())
- {
- //aabb.draw();
- //sphere.draw();
+ if ((*it)->isVisible(frustum)) insertables.push_back(*it);
}
}
NodeP child = tree_.child(node, octantNum);
ASSERT(child.valid() && "expected valid child node");
- return insert(entity, child);
+ return insert_recurse(entity, child);
}
}
}
- 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");
- 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");
- 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");
if (collision == Frustum::INSIDE)
{
- node->draw(alpha);
+ node->getAll(insertables);
}
else // collision == Frustum::INTERSECT
{
- node->drawIfVisible(alpha, frustum);
+ node->getIfVisible(insertables, frustum);
}
if (tree_.children(node) > 0)
NodeP child = tree_.child(node, i);
ASSERT(child.valid() && "expected valid child node");
- draw(alpha, child);
+ getAll(insertables, child);
}
}
else // collision == Frustum::INTERSECT
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_;
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)
tree_.insert(rootNode);
}
+
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");
{
node->objects.erase(it);
}
+ }
+
+ NodeP reinsert(InsertableP entity, NodeP node)
+ {
+ remove(entity, node);
return insert(entity);
}
+
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
{
- 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;
}
};