Blender V4.3
bvh/node.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Adapted code from NVIDIA Corporation. */
7
8#ifndef __BVH_NODE_H__
9#define __BVH_NODE_H__
10
11#include "util/boundbox.h"
12#include "util/types.h"
13
15
30
31class BVHParams;
32
33class BVHNode {
34 public:
35 virtual ~BVHNode()
36 {
37 delete aligned_space;
38 }
39
40 virtual bool is_leaf() const = 0;
41 virtual int num_children() const = 0;
42 virtual BVHNode *get_child(int i) const = 0;
43 virtual int num_triangles() const
44 {
45 return 0;
46 }
47 virtual void print(int depth = 0) const = 0;
48
50 {
51 is_unaligned = true;
52 if (this->aligned_space == NULL) {
53 this->aligned_space = new Transform(aligned_space);
54 }
55 else {
56 *this->aligned_space = aligned_space;
57 }
58 }
59
61 {
62 if (aligned_space == NULL) {
63 return transform_identity();
64 }
65 return *aligned_space;
66 }
67
68 inline bool has_unaligned() const
69 {
70 if (is_leaf()) {
71 return false;
72 }
73 for (int i = 0; i < num_children(); ++i) {
74 if (get_child(i)->is_unaligned) {
75 return true;
76 }
77 }
78 return false;
79 }
80
81 // Subtree functions
83 float computeSubtreeSAHCost(const BVHParams &p, float probability = 1.0f) const;
84 void deleteSubtree();
85
87 void update_time();
88
89 /* Dump the content of the tree as a graphviz file. */
90 void dump_graph(const char *filename);
91
92 // Properties.
95
97
98 /* TODO(sergey): Can be stored as 3x3 matrix, but better to have some
99 * utilities and type defines in util_transform first.
100 */
102
104
105 protected:
106 explicit BVHNode(const BoundBox &bounds)
107 : bounds(bounds),
108 visibility(0),
111 time_from(0.0f),
112 time_to(1.0f)
113 {
114 }
115
116 explicit BVHNode(const BVHNode &other)
117 : bounds(other.bounds),
118 visibility(other.visibility),
121 time_from(other.time_from),
122 time_to(other.time_to)
123 {
124 if (other.aligned_space != NULL) {
125 assert(other.is_unaligned);
126 aligned_space = new Transform();
127 *aligned_space = *other.aligned_space;
128 }
129 else {
130 assert(!other.is_unaligned);
131 }
132 }
133};
134
135class InnerNode : public BVHNode {
136 public:
137 static constexpr int kNumMaxChildren = 8;
138
139 InnerNode(const BoundBox &bounds, BVHNode *child0, BVHNode *child1)
141 {
142 children[0] = child0;
143 children[1] = child1;
145
146 if (child0 && child1) {
147 visibility = child0->visibility | child1->visibility;
148 }
149 else {
150 /* Happens on build cancel. */
151 visibility = 0;
152 }
153 }
154
157 {
158 visibility = 0;
160 time_to = -FLT_MAX;
161 for (int i = 0; i < num_children; ++i) {
162 assert(children[i] != NULL);
164 this->children[i] = children[i];
167 }
169 }
170
171 /* NOTE: This function is only used during binary BVH builder, and it's
172 * supposed to be configured to have 2 children which will be filled-in in a
173 * bit. But this is important to have children reset to NULL. */
175 {
177 visibility = 0;
178 num_children_ = 2;
179 }
180
181 bool is_leaf() const
182 {
183 return false;
184 }
185 int num_children() const
186 {
187 return num_children_;
188 }
189 BVHNode *get_child(int i) const
190 {
191 assert(i >= 0 && i < num_children_);
192 return children[i];
193 }
194 void print(int depth) const;
195
198
199 protected:
201 {
202 for (int i = num_children_; i < kNumMaxChildren; ++i) {
203 children[i] = NULL;
204 }
205 }
206};
207
208class LeafNode : public BVHNode {
209 public:
211 : BVHNode(bounds), lo(lo), hi(hi)
212 {
213 this->bounds = bounds;
214 this->visibility = visibility;
215 }
216
217 LeafNode(const LeafNode &other) : BVHNode(other), lo(other.lo), hi(other.hi) {}
218
219 bool is_leaf() const
220 {
221 return true;
222 }
223 int num_children() const
224 {
225 return 0;
226 }
227 BVHNode *get_child(int) const
228 {
229 return NULL;
230 }
231 int num_triangles() const
232 {
233 return hi - lo;
234 }
235 void print(int depth) const;
236
237 int lo;
238 int hi;
239};
240
242
243#endif /* __BVH_NODE_H__ */
unsigned int uint
BVH_STAT
Definition bvh/node.h:16
@ BVH_STAT_TRIANGLE_COUNT
Definition bvh/node.h:20
@ BVH_STAT_NODE_COUNT
Definition bvh/node.h:17
@ BVH_STAT_CHILDNODE_COUNT
Definition bvh/node.h:21
@ BVH_STAT_ALIGNED_COUNT
Definition bvh/node.h:22
@ BVH_STAT_ALIGNED_INNER_COUNT
Definition bvh/node.h:24
@ BVH_STAT_ALIGNED_LEAF_COUNT
Definition bvh/node.h:26
@ BVH_STAT_DEPTH
Definition bvh/node.h:28
@ BVH_STAT_UNALIGNED_COUNT
Definition bvh/node.h:23
@ BVH_STAT_UNALIGNED_LEAF_COUNT
Definition bvh/node.h:27
@ BVH_STAT_UNALIGNED_INNER_COUNT
Definition bvh/node.h:25
@ BVH_STAT_INNER_COUNT
Definition bvh/node.h:18
@ BVH_STAT_LEAF_COUNT
Definition bvh/node.h:19
int num_children() const
Definition bvh/node.h:185
void reset_unused_children()
Definition bvh/node.h:200
bool is_leaf() const
Definition bvh/node.h:181
InnerNode(const BoundBox &bounds)
Definition bvh/node.h:174
InnerNode(const BoundBox &bounds, BVHNode *child0, BVHNode *child1)
Definition bvh/node.h:139
void print(int depth) const
Definition bvh/node.cpp:197
int num_children_
Definition bvh/node.h:196
BVHNode * get_child(int i) const
Definition bvh/node.h:189
static constexpr int kNumMaxChildren
Definition bvh/node.h:137
BVHNode * children[kNumMaxChildren]
Definition bvh/node.h:197
InnerNode(const BoundBox &bounds, BVHNode **children, const int num_children)
Definition bvh/node.h:155
LeafNode(const LeafNode &other)
Definition bvh/node.h:217
int num_triangles() const
Definition bvh/node.h:231
BVHNode * get_child(int) const
Definition bvh/node.h:227
int num_children() const
Definition bvh/node.h:223
void print(int depth) const
Definition bvh/node.cpp:213
bool is_leaf() const
Definition bvh/node.h:219
LeafNode(const BoundBox &bounds, uint visibility, int lo, int hi)
Definition bvh/node.h:210
#define CCL_NAMESPACE_END
#define NULL
#define min(a, b)
Definition sort.c:32
#define FLT_MAX
Definition stdcycles.h:14
Transform * aligned_space
Definition bvh/node.h:101
int getSubtreeSize(BVH_STAT stat=BVH_STAT_NODE_COUNT) const
Definition bvh/node.cpp:19
void update_time()
Definition bvh/node.cpp:134
uint visibility
Definition bvh/node.h:94
uint update_visibility()
Definition bvh/node.cpp:121
virtual int num_children() const =0
float time_to
Definition bvh/node.h:103
BVHNode(const BoundBox &bounds)
Definition bvh/node.h:106
float computeSubtreeSAHCost(const BVHParams &p, float probability=1.0f) const
Definition bvh/node.cpp:108
bool is_unaligned
Definition bvh/node.h:96
virtual ~BVHNode()
Definition bvh/node.h:35
virtual bool is_leaf() const =0
void deleteSubtree()
Definition bvh/node.cpp:97
float time_from
Definition bvh/node.h:103
void set_aligned_space(const Transform &aligned_space)
Definition bvh/node.h:49
bool has_unaligned() const
Definition bvh/node.h:68
virtual int num_triangles() const
Definition bvh/node.h:43
virtual void print(int depth=0) const =0
Transform get_aligned_space() const
Definition bvh/node.h:60
BVHNode(const BVHNode &other)
Definition bvh/node.h:116
virtual BVHNode * get_child(int i) const =0
BoundBox bounds
Definition bvh/node.h:93
void dump_graph(const char *filename)
Definition bvh/node.cpp:181
ccl_device_inline Transform transform_identity()
Definition transform.h:296
CCL_NAMESPACE_BEGIN struct Transform Transform
float max