Update bundled version of minilzo
[blender-staging.git] / intern / cycles / bvh / bvh_node.cpp
1 /*
2  * Adapted from code copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011, Blender Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "bvh.h"
19 #include "bvh_build.h"
20 #include "bvh_node.h"
21
22 #include "util_debug.h"
23 #include "util_vector.h"
24
25 CCL_NAMESPACE_BEGIN
26
27 /* BVH Node */
28
29 int BVHNode::getSubtreeSize(BVH_STAT stat) const
30 {
31         int cnt = 0;
32
33         switch(stat)
34         {
35                 case BVH_STAT_NODE_COUNT:
36                         cnt = 1;
37                         break;
38                 case BVH_STAT_LEAF_COUNT:
39                         cnt = is_leaf() ? 1 : 0;
40                         break;
41                 case BVH_STAT_INNER_COUNT:
42                         cnt = is_leaf() ? 0 : 1;
43                         break;
44                 case BVH_STAT_TRIANGLE_COUNT:
45                         cnt = is_leaf() ? reinterpret_cast<const LeafNode*>(this)->num_triangles() : 0;
46                         break;
47                 case BVH_STAT_CHILDNODE_COUNT:
48                         cnt = num_children();
49                         break;
50                 default:
51                         assert(0); /* unknown mode */
52         }
53
54         if(!is_leaf())
55                 for(int i = 0; i < num_children(); i++)
56                         cnt += get_child(i)->getSubtreeSize(stat);
57
58         return cnt;
59 }
60
61 void BVHNode::deleteSubtree()
62 {
63         for(int i = 0; i < num_children(); i++)
64                 if(get_child(i))
65                         get_child(i)->deleteSubtree();
66
67         delete this;
68 }
69
70 float BVHNode::computeSubtreeSAHCost(const BVHParams& p, float probability) const
71 {
72         float SAH = probability * p.cost(num_children(), num_triangles());
73
74         for(int i = 0; i < num_children(); i++) {
75                 BVHNode *child = get_child(i);
76                 SAH += child->computeSubtreeSAHCost(p, probability * child->m_bounds.safe_area()/m_bounds.safe_area());
77         }
78
79         return SAH;
80 }
81
82 uint BVHNode::update_visibility()
83 {
84         if(!is_leaf() && m_visibility == 0) {
85                 InnerNode *inner = (InnerNode*)this;
86                 BVHNode *child0 = inner->children[0];
87                 BVHNode *child1 = inner->children[1];
88
89                 m_visibility = child0->update_visibility()|child1->update_visibility();
90         }
91
92         return m_visibility;
93 }
94
95 /* Inner Node */
96
97 void InnerNode::print(int depth) const
98 {
99         for(int i = 0; i < depth; i++)
100                 printf("  ");
101         
102         printf("inner node %p\n", (void*)this);
103
104         if(children[0])
105                 children[0]->print(depth+1);
106         if(children[1])
107                 children[1]->print(depth+1);
108 }
109
110 void LeafNode::print(int depth) const
111 {
112         for(int i = 0; i < depth; i++)
113                 printf("  ");
114         
115         printf("leaf node %d to %d\n", m_lo, m_hi);
116 }
117
118 CCL_NAMESPACE_END
119