Blender V2.61 - r43446

bvh_node.cpp

Go to the documentation of this file.
00001 /*
00002  * Adapted from code copyright 2009-2010 NVIDIA Corporation
00003  * Modifications Copyright 2011, Blender Foundation.
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "bvh.h"
00019 #include "bvh_build.h"
00020 #include "bvh_node.h"
00021 
00022 #include "util_debug.h"
00023 #include "util_vector.h"
00024 
00025 CCL_NAMESPACE_BEGIN
00026 
00027 int BVHNode::getSubtreeSize(BVH_STAT stat) const
00028 {
00029     int cnt = 0;
00030 
00031     switch(stat)
00032     {
00033         case BVH_STAT_NODE_COUNT:
00034             cnt = 1;
00035             break;
00036         case BVH_STAT_LEAF_COUNT:
00037             cnt = is_leaf() ? 1 : 0;
00038             break;
00039         case BVH_STAT_INNER_COUNT:
00040             cnt = is_leaf() ? 0 : 1;
00041             break;
00042         case BVH_STAT_TRIANGLE_COUNT:
00043             cnt = is_leaf() ? reinterpret_cast<const LeafNode*>(this)->num_triangles() : 0;
00044             break;
00045         case BVH_STAT_CHILDNODE_COUNT:
00046             cnt = num_children();
00047             break;
00048         default:
00049             assert(0); /* unknown mode */
00050     }
00051 
00052     if(!is_leaf())
00053         for(int i=0;i<num_children();i++)
00054             cnt += get_child(i)->getSubtreeSize(stat);
00055 
00056     return cnt;
00057 }
00058 
00059 void BVHNode::deleteSubtree()
00060 {
00061     for(int i=0;i<num_children();i++)
00062         get_child(i)->deleteSubtree();
00063 
00064     delete this;
00065 }
00066 
00067 float BVHNode::computeSubtreeSAHCost(const BVHParams& p, float probability) const
00068 {
00069     float SAH = probability * p.cost(num_children(), num_triangles());
00070 
00071     for(int i=0;i<num_children();i++) {
00072         BVHNode *child = get_child(i);
00073         SAH += child->computeSubtreeSAHCost(p, probability * child->m_bounds.area()/m_bounds.area());
00074     }
00075 
00076     return SAH;
00077 }
00078 
00079 void InnerNode::print(int depth) const
00080 {
00081     for(int i = 0; i < depth; i++)
00082         printf("  ");
00083     
00084     printf("inner node %p\n", (void*)this);
00085 
00086     if(children[0])
00087         children[0]->print(depth+1);
00088     if(children[1])
00089         children[1]->print(depth+1);
00090 }
00091 
00092 void LeafNode::print(int depth) const
00093 {
00094     for(int i = 0; i < depth; i++)
00095         printf("  ");
00096     
00097     printf("leaf node %d to %d\n", m_lo, m_hi);
00098 }
00099 
00100 CCL_NAMESPACE_END
00101