+
+
+ 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_recurse(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 getNearbyObjects(std::list<InsertableP>& insertables,
+ const OctreeInsertable& entity, NodeP node) const
+ {
+ ASSERT(node.valid() && "invalid node passed");
+
+ 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");
+
+ getAll(insertables, child);
+ }
+ }
+
+ 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->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->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(NodeP node)
+ {
+ logInfo("-----");
+ logInfo("depth to node: %d", tree_.depth(node));
+ logInfo("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());
+ }
+
+ 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, 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;
+ }