fixed stack overflow of entities outside octree
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 2 Sep 2009 15:50:59 +0000 (09:50 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Wed, 2 Sep 2009 15:50:59 +0000 (09:50 -0600)
src/Moof/Octree.cc
src/Moof/Octree.hh

index 22b4eb1dd58219b460fa010df0014d9844c808bc..300dbcdea7a9f2537d0499667374f22d777c2cb3 100644 (file)
@@ -45,16 +45,30 @@ void Octree::sort()
 }
 
 
-stlplus::ntree<OctreeNode>::iterator Octree::insert(stlplus::ntree<OctreeNode>::iterator node,
-               EntityP entity)
+OctreeNodeP Octree::insert(EntityP entity, OctreeNodeP node)
 {
        ASSERT(node.valid() && "invalid node passed");
        ASSERT(entity && "null entity passed");
 
-       Plane::Halfspace halfspace;
        int octantNum = -1;
 
+       Plane::Halfspace halfspace;
+
+       // TODO this method needs a lot of work
        Plane xy = node->getAabb().getPlaneXY();
+
+
+       // make sure the entity is fully inside the volume
+       if (!(entity->getAabb().max[0] < node->getAabb().max[0] &&
+                       entity->getAabb().min[0] > node->getAabb().min[0] &&
+                       entity->getAabb().max[1] < node->getAabb().max[1] &&
+                       entity->getAabb().min[1] > node->getAabb().min[1] &&
+                       entity->getAabb().max[2] < node->getAabb().max[2] &&
+                       entity->getAabb().min[2] > node->getAabb().min[2]))
+       {
+               goto done;
+       }
+
        halfspace = xy.intersectsSphere(entity->getSphere());
        if (halfspace == Plane::INTERSECT)
        {
@@ -154,6 +168,8 @@ stlplus::ntree<OctreeNode>::iterator Octree::insert(stlplus::ntree<OctreeNode>::
                }
        }
 
+done:
+
        if (octantNum == -1)
        {
                node->objects.push_front(entity);
@@ -166,15 +182,14 @@ stlplus::ntree<OctreeNode>::iterator Octree::insert(stlplus::ntree<OctreeNode>::
                        addChild(node, octantNum);
                }
 
-               stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, octantNum);
+               OctreeNodeP child = tree_.child(node, octantNum);
                ASSERT(child.valid() && "expected valid child node");
 
-               return insert(child, entity);
+               return insert(entity, child);
        }
 }
 
-stlplus::ntree<OctreeNode>::iterator Octree::reinsert(EntityP entity,
-               stlplus::ntree<OctreeNode>::iterator node)
+OctreeNodeP Octree::reinsert(EntityP entity, OctreeNodeP node)
 {
        ASSERT(entity && "null entity passed");
        ASSERT(node.valid() && "invalid node passed");
@@ -191,7 +206,7 @@ stlplus::ntree<OctreeNode>::iterator Octree::reinsert(EntityP entity,
 }
 
 
-void Octree::addChild(stlplus::ntree<OctreeNode>::iterator node, int index)
+void Octree::addChild(OctreeNodeP node, int index)
 {
        ASSERT(node.valid() && "invalid node passed");
 
@@ -205,7 +220,7 @@ void Octree::addChild(stlplus::ntree<OctreeNode>::iterator node, int index)
 }
 
 
-void Octree::draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha)
+void Octree::draw(Scalar alpha, OctreeNodeP node)
 {
        ASSERT(node.valid() && "invalid node passed");
 
@@ -213,15 +228,14 @@ void Octree::draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha)
 
        for (unsigned i = 0; i < tree_.children(node); ++i)
        {
-               stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, i);
+               OctreeNodeP child = tree_.child(node, i);
                ASSERT(child.valid() && "expected valid child node");
 
-               draw(child, alpha);
+               draw(alpha, child);
        }
 }
 
-void Octree::drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
-               Scalar alpha, const Camera& cam)
+void Octree::drawIfVisible(Scalar alpha, const Camera& cam, OctreeNodeP node)
 {
        ASSERT(node.valid() && "invalid node passed");
 
@@ -250,20 +264,20 @@ void Octree::drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
                {
                        for (unsigned i = 0; i < tree_.children(node); ++i)
                        {
-                               stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, i);
+                               OctreeNodeP child = tree_.child(node, i);
                                ASSERT(child.valid() && "expected valid child node");
 
-                               draw(child, alpha);
+                               draw(alpha, child);
                        }
                }
                else // collision == Frustum::INTERSECT
                {
                        for (unsigned i = 0; i < tree_.children(node); ++i)
                        {
-                               stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, i);
+                               OctreeNodeP child = tree_.child(node, i);
                                ASSERT(child.valid() && "expected valid child node");
 
-                               drawIfVisible(child, alpha, cam);
+                               drawIfVisible(alpha, cam, child);
                        }
                }
        }
index 774eece11c56cb34236a82d2b23ce6f501765351..4dc0c5c7e460e801594585200e47d3a70bc34064 100644 (file)
@@ -49,6 +49,13 @@ namespace Mf {
 class Camera;
 
 
+struct OctreeNode;
+typedef stlplus::ntree<OctreeNode>::iterator OctreeNodeP;
+
+class Octree;
+typedef boost::shared_ptr<Octree> OctreeP;
+
+
 struct OctreeNode : public Entity
 {
        std::list<EntityP> objects;
@@ -118,19 +125,14 @@ struct OctreeNode : public Entity
 };
 
 
-class Octree;
-typedef boost::shared_ptr<Octree> OctreeP;
-
 class Octree
 {
-       stlplus::ntree<OctreeNode>::iterator
-               insert(stlplus::ntree<OctreeNode>::iterator node, EntityP entity);
+       OctreeNodeP insert(EntityP entity, OctreeNodeP node);
        
-       void addChild(stlplus::ntree<OctreeNode>::iterator node, int index);
+       void addChild(OctreeNodeP node, int index);
 
-       void draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha);
-       void drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
-                       Scalar alpha, const Camera& cam);
+       void draw(Scalar alpha, OctreeNodeP node);
+       void drawIfVisible(Scalar alpha, const Camera& cam, OctreeNodeP node);
 
        stlplus::ntree<OctreeNode> tree_;
 
@@ -146,17 +148,16 @@ public:
                tree_.insert(rootNode);
        }
 
-       stlplus::ntree<OctreeNode>::iterator insert(EntityP entity)
+       OctreeNodeP insert(EntityP entity)
        {
-               return insert(tree_.root(), entity);
+               return insert(entity, tree_.root());
        }
 
-       stlplus::ntree<OctreeNode>::iterator reinsert(EntityP entity,
-                       stlplus::ntree<OctreeNode>::iterator node);
+       OctreeNodeP reinsert(EntityP entity, OctreeNodeP node);
 
        void drawIfVisible(Scalar alpha, const Camera& cam)
        {
-               drawIfVisible(tree_.root(), alpha, cam);
+               drawIfVisible(alpha, cam, tree_.root());
        }
 
        void sort();
This page took 0.025364 seconds and 4 git commands to generate.