65d5df01158ec79334102771a6357cc69207530e
[blender.git] / intern / cycles / bvh / bvh_node.h
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 #ifndef __BVH_NODE_H__
19 #define __BVH_NODE_H__
20
21 #include "util/util_boundbox.h"
22 #include "util/util_types.h"
23
24 CCL_NAMESPACE_BEGIN
25
26 enum BVH_STAT {
27         BVH_STAT_NODE_COUNT,
28         BVH_STAT_INNER_COUNT,
29         BVH_STAT_LEAF_COUNT,
30         BVH_STAT_TRIANGLE_COUNT,
31         BVH_STAT_CHILDNODE_COUNT,
32         BVH_STAT_QNODE_COUNT,
33         BVH_STAT_ALIGNED_COUNT,
34         BVH_STAT_UNALIGNED_COUNT,
35         BVH_STAT_ALIGNED_INNER_COUNT,
36         BVH_STAT_UNALIGNED_INNER_COUNT,
37         BVH_STAT_ALIGNED_INNER_QNODE_COUNT,
38         BVH_STAT_UNALIGNED_INNER_QNODE_COUNT,
39         BVH_STAT_ALIGNED_LEAF_COUNT,
40         BVH_STAT_UNALIGNED_LEAF_COUNT,
41         BVH_STAT_DEPTH,
42         BVH_STAT_ONODE_COUNT,
43         BVH_STAT_UNALIGNED_INNER_ONODE_COUNT,
44 };
45
46 class BVHParams;
47
48 class BVHNode
49 {
50 public:
51         BVHNode() : is_unaligned(false),
52                     aligned_space(NULL),
53                     time_from(0.0f),
54                     time_to(1.0f)
55         {
56         }
57
58         virtual ~BVHNode()
59         {
60                 delete aligned_space;
61         }
62
63         virtual bool is_leaf() const = 0;
64         virtual int num_children() const = 0;
65         virtual BVHNode *get_child(int i) const = 0;
66         virtual int num_triangles() const { return 0; }
67         virtual void print(int depth = 0) const = 0;
68
69         inline void set_aligned_space(const Transform& aligned_space)
70         {
71                 is_unaligned = true;
72                 if(this->aligned_space == NULL) {
73                         this->aligned_space = new Transform(aligned_space);
74                 }
75                 else {
76                         *this->aligned_space = aligned_space;
77                 }
78         }
79
80         inline Transform get_aligned_space() const
81         {
82                 if(aligned_space == NULL) {
83                         return transform_identity();
84                 }
85                 return *aligned_space;
86         }
87
88         // Subtree functions
89         int getSubtreeSize(BVH_STAT stat=BVH_STAT_NODE_COUNT) const;
90         float computeSubtreeSAHCost(const BVHParams& p, float probability = 1.0f) const;
91         void deleteSubtree();
92
93         uint update_visibility();
94         void update_time();
95
96         // Properties.
97         BoundBox bounds;
98         uint visibility;
99
100         bool is_unaligned;
101
102         /* TODO(sergey): Can be stored as 3x3 matrix, but better to have some
103          * utilities and type defines in util_transform first.
104          */
105         Transform *aligned_space;
106
107         float time_from, time_to;
108 };
109
110 class InnerNode : public BVHNode
111 {
112 public:
113         InnerNode(const BoundBox& bounds,
114                   BVHNode* child0,
115                   BVHNode* child1)
116         {
117                 this->bounds = bounds;
118                 children[0] = child0;
119                 children[1] = child1;
120
121                 if(child0 && child1)
122                         visibility = child0->visibility|child1->visibility;
123                 else
124                         visibility = 0; /* happens on build cancel */
125         }
126
127         explicit InnerNode(const BoundBox& bounds)
128         {
129                 this->bounds = bounds;
130                 visibility = 0;
131                 children[0] = NULL;
132                 children[1] = NULL;
133         }
134
135         bool is_leaf() const { return false; }
136         int num_children() const { return 2; }
137         BVHNode *get_child(int i) const{ assert(i>=0 && i<2); return children[i]; }
138         void print(int depth) const;
139
140         BVHNode *children[2];
141 };
142
143 class LeafNode : public BVHNode
144 {
145 public:
146         LeafNode(const BoundBox& bounds, uint visibility, int lo, int hi)
147         : lo(lo),
148           hi(hi)
149         {
150                 this->bounds = bounds;
151                 this->visibility = visibility;
152         }
153
154         LeafNode(const LeafNode& s)
155         : BVHNode()
156         {
157                 *this = s;
158         }
159
160         bool is_leaf() const { return true; }
161         int num_children() const { return 0; }
162         BVHNode *get_child(int) const { return NULL; }
163         int num_triangles() const { return hi - lo; }
164         void print(int depth) const;
165
166         int lo;
167         int hi;
168 };
169
170 CCL_NAMESPACE_END
171
172 #endif  /* __BVH_NODE_H__ */