SceneNode bounds calculation
[laserbrain_demo] / src / snode.cc
index a1d41ae..eee5fce 100644 (file)
@@ -11,6 +11,7 @@ SceneNode::SceneNode()
        scene = 0;
        parent = 0;
        name = 0;
+       local_bvol_valid = false;
 }
 
 SceneNode::SceneNode(Object *obj)
@@ -19,6 +20,7 @@ SceneNode::SceneNode(Object *obj)
        scene = 0;
        parent = 0;
        name = 0;
+       local_bvol_valid = false;
        add_object(obj);
 }
 
@@ -95,6 +97,8 @@ void SceneNode::add_object(Object *obj)
 
        this->obj.push_back(obj);
        obj->node = this;
+
+       local_bvol_valid = false;
 }
 
 bool SceneNode::remove_object(Object *o)
@@ -109,6 +113,8 @@ bool SceneNode::remove_object(Object *o)
                return false;
        }
        obj.erase(it);
+
+       local_bvol_valid = false;
        return true;
 }
 
@@ -276,3 +282,55 @@ bool SceneNode::intersect(const Ray &ray, HitPoint *hit) const
        }
        return false;
 }
+
+const AABox &SceneNode::calc_local_bounds()
+{
+       local_bvol = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX));
+
+       // calculate the axis-aligned bounding box of all objects in this node
+       int nobj = obj.size();
+       for(int i=0; i<nobj; i++) {
+               AABox tmp = obj[i]->get_aabox();
+               calc_bounding_aabox(&local_bvol, &local_bvol, &tmp);
+       }
+
+       local_bvol_valid = true;
+       return local_bvol;
+}
+
+const AABox &SceneNode::get_local_bounds() const
+{
+       if(!local_bvol_valid) {
+               ((SceneNode*)this)->calc_local_bounds();
+       }
+       return local_bvol;
+}
+
+AABox SceneNode::get_node_bounds() const
+{
+       get_local_bounds();     // validate local_bvol
+
+       // calculate the transformed local_bvol
+       Box node_bbox = Box(local_bvol, xform);
+
+       // then calculate the axis-aligned bounding box
+       AABox aabox;
+       calc_bounding_aabox(&aabox, &node_bbox);
+       return aabox;
+}
+
+AABox SceneNode::get_bounds() const
+{
+       AABox sub_aabb = AABox(Vec3(FLT_MAX, FLT_MAX, FLT_MAX), Vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX));
+
+       // calculate the bounding box of all children
+       int nchild = children.size();
+       for(int i=0; i<nchild; i++) {
+               AABox tmp = children[i]->get_bounds();
+               calc_bounding_aabox(&sub_aabb, &sub_aabb, &tmp);
+       }
+
+       AABox aabb;
+       calc_bounding_aabox(&aabb, &local_bvol, &sub_aabb);
+       return aabb;
+}