X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FOctree.cc;h=300dbcdea7a9f2537d0499667374f22d777c2cb3;hp=50b38ac08f36ca4bb0af26e38ab3e6ac4e5a717e;hb=f72400af4fa3e7b54dab154b5a2b6503a6f9af18;hpb=bfa6212d09d8735d8fd5e2638188e4a99f21ada4 diff --git a/src/Moof/Octree.cc b/src/Moof/Octree.cc index 50b38ac..300dbcd 100644 --- a/src/Moof/Octree.cc +++ b/src/Moof/Octree.cc @@ -27,6 +27,7 @@ *******************************************************************************/ #include "Camera.hh" +#include "Log.hh" #include "Octree.hh" @@ -44,19 +45,30 @@ void Octree::sort() } -stlplus::ntree::iterator Octree::insert(stlplus::ntree::iterator node, - EntityPtr entity) +OctreeNodeP Octree::insert(EntityP entity, OctreeNodeP node) { - Plane::Halfspace halfspace; + ASSERT(node.valid() && "invalid node passed"); + ASSERT(entity && "null entity passed"); + int octantNum = -1; - if (!node.valid()) + 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])) { - std::cerr << "cannot insert into invalid node" << std::endl; - return stlplus::ntree::iterator(); + goto done; } - Plane xy = node->getAabb().getPlaneXY(); halfspace = xy.intersectsSphere(entity->getSphere()); if (halfspace == Plane::INTERSECT) { @@ -156,6 +168,8 @@ stlplus::ntree::iterator Octree::insert(stlplus::ntree:: } } +done: + if (octantNum == -1) { node->objects.push_front(entity); @@ -168,30 +182,19 @@ stlplus::ntree::iterator Octree::insert(stlplus::ntree:: addChild(node, octantNum); } - stlplus::ntree::iterator child = tree_.child(node, octantNum); + OctreeNodeP child = tree_.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::iterator(); - } + return insert(entity, child); } } -stlplus::ntree::iterator Octree::reinsert(EntityPtr entity, - stlplus::ntree::iterator node) +OctreeNodeP Octree::reinsert(EntityP entity, OctreeNodeP node) { - if (!node.valid()) - { - std::cerr << "cannot move entity from invalid node" << std::endl; - return stlplus::ntree::iterator(); - } + ASSERT(entity && "null entity passed"); + ASSERT(node.valid() && "invalid node passed"); - std::list::iterator it; + std::list::iterator it; it = std::find(node->objects.begin(), node->objects.end(), entity); if (it != node->objects.end()) @@ -203,15 +206,11 @@ stlplus::ntree::iterator Octree::reinsert(EntityPtr entity, } -void Octree::addChild(stlplus::ntree::iterator node, int index) +void Octree::addChild(OctreeNodeP 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 octant; for (int i = tree_.children(node); i <= index; ++i) { @@ -221,47 +220,31 @@ void Octree::addChild(stlplus::ntree::iterator node, int index) } -void Octree::draw(stlplus::ntree::iterator node, Scalar alpha) +void Octree::draw(Scalar alpha, OctreeNodeP node) { - if (!node.valid()) - { - std::cerr << "cannot draw null child node :-(" << std::endl; - return; - } + ASSERT(node.valid() && "invalid node passed"); node->draw(alpha); for (unsigned i = 0; i < tree_.children(node); ++i) { - stlplus::ntree::iterator child = tree_.child(node, i); - - if (child.valid()) - { - draw(child, alpha); - } - else - { - std::cerr << "node is not a leaf, but has an invalid child" << std::endl; - } + OctreeNodeP child = tree_.child(node, i); + ASSERT(child.valid() && "expected valid child node"); + draw(alpha, child); } } -void Octree::drawIfVisible(stlplus::ntree::iterator node, - Scalar alpha, const Camera& cam) +void Octree::drawIfVisible(Scalar alpha, const Camera& cam, OctreeNodeP node) { - //node.drawIfVisible(alpha, cam); - - if (!node.valid()) - { - std::cerr << "invalid child while drawing :-(" << std::endl; - return; - } + ASSERT(node.valid() && "invalid node passed"); + // try to cull by sphere Frustum::Collision collision = cam.getFrustum().containsSphere(node->getSphere()); if (collision == Frustum::OUTSIDE) return; + // try to cull by aabb collision = cam.getFrustum().containsAabb(node->getAabb()); if (collision == Frustum::OUTSIDE) return; @@ -281,33 +264,20 @@ void Octree::drawIfVisible(stlplus::ntree::iterator node, { for (unsigned i = 0; i < tree_.children(node); ++i) { - stlplus::ntree::iterator child = tree_.child(node, i); - - if (child.valid()) - { - draw(child, alpha); - } - else - { - std::cerr << "node is not a leaf, but has an invalid child" << std::endl; - } + OctreeNodeP child = tree_.child(node, i); + ASSERT(child.valid() && "expected valid child node"); + draw(alpha, child); } } else // collision == Frustum::INTERSECT { for (unsigned i = 0; i < tree_.children(node); ++i) { - stlplus::ntree::iterator child = tree_.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; - } + OctreeNodeP child = tree_.child(node, i); + ASSERT(child.valid() && "expected valid child node"); + + drawIfVisible(alpha, cam, child); } } }