Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bvh_node.cpp « bvh « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8294690da7d2e437a5c9e4f6d858d52601870a9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Adapted from code copyright 2009-2010 NVIDIA Corporation
 * Modifications Copyright 2011, Blender Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "bvh.h"
#include "bvh_build.h"
#include "bvh_node.h"

#include "util_debug.h"
#include "util_vector.h"

CCL_NAMESPACE_BEGIN

/* BVH Node */

int BVHNode::getSubtreeSize(BVH_STAT stat) const
{
	int cnt = 0;

	switch(stat)
	{
		case BVH_STAT_NODE_COUNT:
			cnt = 1;
			break;
		case BVH_STAT_LEAF_COUNT:
			cnt = is_leaf() ? 1 : 0;
			break;
		case BVH_STAT_INNER_COUNT:
			cnt = is_leaf() ? 0 : 1;
			break;
		case BVH_STAT_TRIANGLE_COUNT:
			cnt = is_leaf() ? reinterpret_cast<const LeafNode*>(this)->num_triangles() : 0;
			break;
		case BVH_STAT_CHILDNODE_COUNT:
			cnt = num_children();
			break;
		case BVH_STAT_QNODE_COUNT:
			cnt = 1;
			for(int i = 0; i < num_children(); i++) {
				BVHNode *node = get_child(i);
				if(node->is_leaf()) {
					cnt += 1;
				}
				else {
					for(int j = 0; j < node->num_children(); j++) {
						cnt += node->get_child(j)->getSubtreeSize(stat);
					}
				}
			}
			return cnt;
		default:
			assert(0); /* unknown mode */
	}

	if(!is_leaf())
		for(int i = 0; i < num_children(); i++)
			cnt += get_child(i)->getSubtreeSize(stat);

	return cnt;
}

void BVHNode::deleteSubtree()
{
	for(int i = 0; i < num_children(); i++)
		if(get_child(i))
			get_child(i)->deleteSubtree();

	delete this;
}

float BVHNode::computeSubtreeSAHCost(const BVHParams& p, float probability) const
{
	float SAH = probability * p.cost(num_children(), num_triangles());

	for(int i = 0; i < num_children(); i++) {
		BVHNode *child = get_child(i);
		SAH += child->computeSubtreeSAHCost(p, probability * child->m_bounds.safe_area()/m_bounds.safe_area());
	}

	return SAH;
}

uint BVHNode::update_visibility()
{
	if(!is_leaf() && m_visibility == 0) {
		InnerNode *inner = (InnerNode*)this;
		BVHNode *child0 = inner->children[0];
		BVHNode *child1 = inner->children[1];

		m_visibility = child0->update_visibility()|child1->update_visibility();
	}

	return m_visibility;
}

/* Inner Node */

void InnerNode::print(int depth) const
{
	for(int i = 0; i < depth; i++)
		printf("  ");
	
	printf("inner node %p\n", (void*)this);

	if(children[0])
		children[0]->print(depth+1);
	if(children[1])
		children[1]->print(depth+1);
}

void LeafNode::print(int depth) const
{
	for(int i = 0; i < depth; i++)
		printf("  ");
	
	printf("leaf node %d to %d\n", m_lo, m_hi);
}

CCL_NAMESPACE_END