Blender V5.0
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#pragma once
9
10#include "util/boundbox.h"
11#include "util/types.h"
12#include "util/unique_ptr.h"
13
15
30
31class BVHParams;
32
33class BVHNode {
34 public:
35 virtual ~BVHNode() = default;
36
37 virtual bool is_leaf() const = 0;
38 virtual int num_children() const = 0;
39 virtual BVHNode *get_child(const int i) const = 0;
40 virtual int num_triangles() const
41 {
42 return 0;
43 }
44 virtual void print(const int depth = 0) const = 0;
45
47 {
48 is_unaligned = true;
49 if (this->aligned_space == nullptr) {
50 this->aligned_space = make_unique<Transform>(aligned_space);
51 }
52 else {
53 *this->aligned_space = aligned_space;
54 }
55 }
56
58 {
59 if (aligned_space == nullptr) {
60 return transform_identity();
61 }
62 return *aligned_space;
63 }
64
65 bool has_unaligned() const
66 {
67 if (is_leaf()) {
68 return false;
69 }
70 for (int i = 0; i < num_children(); ++i) {
71 if (get_child(i)->is_unaligned) {
72 return true;
73 }
74 }
75 return false;
76 }
77
78 // Subtree functions
80 float computeSubtreeSAHCost(const BVHParams &p, const float probability = 1.0f) const;
81
83 void update_time();
84
85 /* Dump the content of the tree as a graphviz file. */
86 void dump_graph(const char *filename);
87
88 // Properties.
91
92 bool is_unaligned = false;
93
94 /* TODO(sergey): Can be stored as 3x3 matrix, but better to have some
95 * utilities and type defines in util_transform first.
96 */
98
99 float time_from = 0.0f, time_to = 1.0f;
100
101 protected:
102 explicit BVHNode(const BoundBox &bounds) : bounds(bounds) {}
103
104 explicit BVHNode(const BVHNode &other)
105 : bounds(other.bounds),
106 visibility(other.visibility),
108
109 time_from(other.time_from),
110 time_to(other.time_to)
111 {
112 if (other.aligned_space != nullptr) {
113 assert(other.is_unaligned);
114 aligned_space = make_unique<Transform>(*other.aligned_space);
115 }
116 else {
117 assert(!other.is_unaligned);
118 }
119 }
120};
121
122class InnerNode : public BVHNode {
123 public:
124 static constexpr int kNumMaxChildren = 8;
125
128 {
129 if (child0 && child1) {
130 visibility = child0->visibility | child1->visibility;
131 }
132 else {
133 /* Happens on build cancel. */
134 visibility = 0;
135 }
136
137 children[0] = std::move(child0);
138 children[1] = std::move(child1);
139 }
140
141 /* NOTE: This function is only used during binary BVH builder, and it's
142 * supposed to be configured to have 2 children which will be filled-in in a
143 * bit. */
145 {
146 visibility = 0;
147 num_children_ = 2;
148 }
149
150 bool is_leaf() const override
151 {
152 return false;
153 }
154 int num_children() const override
155 {
156 return num_children_;
157 }
158 BVHNode *get_child(const int i) const override
159 {
160 assert(i >= 0 && i < num_children_);
161 return children[i].get();
162 }
163 void print(const int depth) const override;
164
167};
168
169class LeafNode : public BVHNode {
170 public:
171 LeafNode(const BoundBox &bounds, const uint visibility, const int lo, const int hi)
172 : BVHNode(bounds), lo(lo), hi(hi)
173 {
174 this->bounds = bounds;
175 this->visibility = visibility;
176 }
177
178 LeafNode(const LeafNode &other) = default;
179
180 bool is_leaf() const override
181 {
182 return true;
183 }
184 int num_children() const override
185 {
186 return 0;
187 }
188 BVHNode *get_child(int /*i*/) const override
189 {
190 return nullptr;
191 }
192 int num_triangles() const override
193 {
194 return hi - lo;
195 }
196 void print(const int depth) const override;
197
198 int lo;
199 int hi;
200};
201
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
void print(const int depth) const override
Definition bvh/node.cpp:186
InnerNode(const BoundBox &bounds)
Definition bvh/node.h:144
InnerNode(const BoundBox &bounds, unique_ptr< BVHNode > &&child0, unique_ptr< BVHNode > &&child1)
Definition bvh/node.h:126
bool is_leaf() const override
Definition bvh/node.h:150
int num_children_
Definition bvh/node.h:165
static constexpr int kNumMaxChildren
Definition bvh/node.h:124
int num_children() const override
Definition bvh/node.h:154
unique_ptr< BVHNode > children[kNumMaxChildren]
Definition bvh/node.h:166
BVHNode * get_child(const int i) const override
Definition bvh/node.h:158
bool is_leaf() const override
Definition bvh/node.h:180
int num_triangles() const override
Definition bvh/node.h:192
BVHNode * get_child(int) const override
Definition bvh/node.h:188
int num_children() const override
Definition bvh/node.h:184
void print(const int depth) const override
Definition bvh/node.cpp:202
LeafNode(const LeafNode &other)=default
LeafNode(const BoundBox &bounds, const uint visibility, const int lo, const int hi)
Definition bvh/node.h:171
#define CCL_NAMESPACE_END
#define assert(assertion)
float computeSubtreeSAHCost(const BVHParams &p, const float probability=1.0f) const
Definition bvh/node.cpp:95
int getSubtreeSize(BVH_STAT stat=BVH_STAT_NODE_COUNT) const
Definition bvh/node.cpp:17
void update_time()
Definition bvh/node.cpp:121
uint visibility
Definition bvh/node.h:90
uint update_visibility()
Definition bvh/node.cpp:108
virtual int num_children() const =0
unique_ptr< Transform > aligned_space
Definition bvh/node.h:97
virtual ~BVHNode()=default
float time_to
Definition bvh/node.h:99
BVHNode(const BoundBox &bounds)
Definition bvh/node.h:102
bool is_unaligned
Definition bvh/node.h:92
virtual bool is_leaf() const =0
float time_from
Definition bvh/node.h:99
void set_aligned_space(const Transform &aligned_space)
Definition bvh/node.h:46
bool has_unaligned() const
Definition bvh/node.h:65
virtual int num_triangles() const
Definition bvh/node.h:40
virtual BVHNode * get_child(const int i) const =0
virtual void print(const int depth=0) const =0
Transform get_aligned_space() const
Definition bvh/node.h:57
BVHNode(const BVHNode &other)
Definition bvh/node.h:104
BoundBox bounds
Definition bvh/node.h:89
void dump_graph(const char *filename)
Definition bvh/node.cpp:170
i
Definition text_draw.cc:230
ccl_device_inline Transform transform_identity()
Definition transform.h:322