16# include <openvdb/tools/FindActiveValues.h>
25 return x + resolution_ * (
y +
z * resolution_);
30 const blocked_range3d<int>
range(
31 index_min.
x, index_max.
x, 32, index_min.
y, index_max.
y, 32, index_min.
z, index_max.
z, 32);
35 auto reduction_func = [&](
const blocked_range3d<int> &r, Extrema<float>
init) -> Extrema<float> {
36 for (
int z = r.cols().begin();
z < r.cols().end(); ++
z) {
37 for (
int y = r.rows().begin();
y < r.rows().end(); ++
y) {
38 for (
int x = r.pages().begin();
x < r.pages().end(); ++
x) {
46 auto join_func = [](Extrema<float> a, Extrema<float>
b) -> Extrema<float> {
55 return (p - bbox_min) * position_to_index_scale_;
58int3 Octree::position_to_floor_index(
const float3 p)
const
64int3 Octree::position_to_ceil_index(
const float3 p)
const
66 if (
any_zero(position_to_index_scale_)) {
81 return index_to_position_scale_;
84bool Octree::should_split(std::shared_ptr<OctreeNode> &
node)
const
86 const int3 index_min = position_to_floor_index(
node->bbox.min);
87 const int3 index_max = position_to_ceil_index(
node->bbox.max);
88 node->sigma = get_extrema(index_min, index_max);
98 return (
node->sigma.range() *
len(bbox_size) * scale_ > 1.442f &&
104static bool vdb_voxel_intersect(
const float3 p_min,
106 openvdb::BoolGrid::ConstPtr &grid,
107 const openvdb::tools::FindActiveValues<openvdb::BoolTree> &find)
114 const openvdb::math::CoordBBox coord_bbox(
115 openvdb::Coord::floor(grid->worldToIndex({p_min.x, p_min.y, p_min.z})),
116 openvdb::Coord::ceil(grid->worldToIndex({p_max.x, p_max.y, p_max.z})));
120 return find.anyActiveValues(coord_bbox,
true);
130 openvdb::BoolGrid::ConstPtr &interior_mask,
132 const int resolution)
134 const int object_id =
object->get_device_index();
135 const uint shader_id = shader->
id;
142 const float3 padded_size = voxel_size +
pad * 2.0f;
144 const blocked_range3d<int> range(0, resolution, 8, 0, resolution, 8, 0, resolution, 8);
145 parallel_for(range, [&](
const blocked_range3d<int> &r) {
148 const auto find = openvdb::tools::FindActiveValues(interior_mask->tree());
151 for (
int z = r.cols().begin();
z < r.cols().end(); ++
z) {
152 for (
int y = r.rows().begin();
y < r.rows().end(); ++
y) {
153 for (
int x = r.pages().begin();
x < r.pages().end(); ++
x) {
159 if (!vdb_voxel_intersect(p, p + voxel_size, interior_mask, find)) {
167 in.object = object_id;
171 d_input_data[offset * 2] =
in;
173 in.object = shader_id;
175 in.u = padded_size.
y;
176 in.v = padded_size.
z;
177 d_input_data[offset * 2 + 1] =
in;
187 const int num_channels,
188 const int resolution,
191 const float *d_output_data = d_output.
data();
192 const blocked_range3d<int> range(0, resolution, 32, 0, resolution, 32, 0, resolution, 32);
194 parallel_for(range, [&](
const blocked_range3d<int> &r) {
195 for (
int z = r.cols().begin();
z < r.cols().end(); ++
z) {
196 for (
int y = r.rows().begin();
y < r.rows().end(); ++
y) {
197 for (
int x = r.pages().begin();
x < r.pages().end(); ++
x) {
199 sigmas[
index].min = d_output_data[
index * num_channels + 0];
200 sigmas[
index].max = d_output_data[
index * num_channels + 1];
207void Octree::evaluate_volume_density(
Device *device,
210 openvdb::BoolGrid::ConstPtr &interior_mask,
220 index_to_position_scale_ = root_->bbox.size() /
float(resolution_);
225 const int size = resolution_ * resolution_ * resolution_;
226 sigmas_.resize(size);
227 parallel_for(0, size, [&](
int i) { sigmas_[i] = {0.0f, 0.0f}; });
230 const int num_channels = 2;
233 const int num_inputs =
size * 2;
236 ShaderEval shader_eval(device, progress);
241 [&](device_vector<KernelShaderEvalInput> &d_input) {
249 [&](device_vector<float> &d_output) {
254float Octree::volume_scale(
const Object *
object)
const
256 const Geometry *geom =
object->get_geometry();
258 const Volume *volume =
static_cast<const Volume *
>(geom);
259 if (volume->get_object_space()) {
287std::shared_ptr<OctreeInternalNode> Octree::make_internal(std::shared_ptr<OctreeNode> &
node)
290 auto internal = std::make_shared<OctreeInternalNode>(*
node);
293 const float3 center = internal->bbox.center();
294 for (
int i = 0; i < 8; i++) {
296 const BoundBox bbox(
mix(internal->bbox.min, center, t),
mix(center, internal->bbox.max, t));
297 internal->children_[i] = std::make_shared<OctreeNode>(bbox, internal->depth + 1);
303void Octree::recursive_build(std::shared_ptr<OctreeNode> &octree_node)
305 if (!should_split(octree_node)) {
310 auto internal = make_internal(octree_node);
312 for (
auto &child : internal->children_) {
313 task_pool_.push([&] { recursive_build(child); });
316 octree_node = internal;
320 const int current_index,
321 const std::shared_ptr<OctreeNode> &
node,
322 int &child_index)
const
327 if (
auto internal_ptr = std::dynamic_pointer_cast<OctreeInternalNode>(
node)) {
332 for (
int i = 0; i < 8; i++) {
344 is_flattened_ = flattened;
349 return is_flattened_;
355 openvdb::BoolGrid::ConstPtr &interior_mask,
360 const char *name =
object->get_asset_name().c_str();
364 evaluate_volume_density(device, progress, interior_mask,
object, shader);
366 evaluate_volume_density(device, progress,
object, shader);
374 scale_ = volume_scale(
object);
375 recursive_build(root_);
377 task_pool_.wait_work();
386 root_ = std::make_shared<OctreeNode>(bbox, 0);
388 is_flattened_ =
false;
423 str +=
"(" + mid_x +
"," + mid_y +
"," + min_z +
"), "
424 "(" + mid_x +
"," + mid_y +
"," + max_z +
"), "
425 "(" + mid_x +
"," + max_y +
"," + max_z +
"), "
426 "(" + mid_x +
"," + max_y +
"," + min_z +
"), "
427 "(" + mid_x +
"," + min_y +
"," + min_z +
"), "
428 "(" + mid_x +
"," + min_y +
"," + max_z +
"), ";
429 str +=
"(" + min_x +
"," + mid_y +
"," + mid_z +
"), "
430 "(" + max_x +
"," + mid_y +
"," + mid_z +
"), "
431 "(" + max_x +
"," + mid_y +
"," + max_z +
"), "
432 "(" + min_x +
"," + mid_y +
"," + max_z +
"), "
433 "(" + min_x +
"," + mid_y +
"," + min_z +
"), "
434 "(" + max_x +
"," + mid_y +
"," + min_z +
"), ";
435 str +=
"(" + mid_x +
"," + min_y +
"," + mid_z +
"), "
436 "(" + mid_x +
"," + max_y +
"," + mid_z +
"), "
437 "(" + max_x +
"," + max_y +
"," + mid_z +
"), "
438 "(" + max_x +
"," + min_y +
"," + mid_z +
"), "
439 "(" + min_x +
"," + min_y +
"," + mid_z +
"), "
440 "(" + min_x +
"," + max_y +
"," + mid_z +
"), ";
442 for (
const auto &child : internal->children_) {
443 child->visualize(
str);
449 std::string
str =
"vertices = [";
450 root_->visualize(
str);
452 "]\nr = range(len(vertices))\n"
453 "edges = [(i, i+1 if i%6<5 else i-4) for i in r]\n"
454 "mesh = bpy.data.meshes.new('Octree')\n"
455 "mesh.from_pydata(vertices, edges, [])\n"
457 "obj = bpy.data.objects.new('" +
460 "octree.objects.link(obj)\n"
461 "bpy.context.view_layer.objects.active = obj\n"
462 "bpy.ops.object.mode_set(mode='EDIT')\n";
465 const float3 center = root_->bbox.center();
466 const float3 size = root_->bbox.size() * 0.5f;
467 file <<
"bpy.ops.mesh.primitive_cube_add(location = " << center <<
", scale = " << size <<
")\n";
468 file <<
"bpy.ops.mesh.delete(type='ONLY_FACE')\n"
469 "bpy.ops.object.mode_set(mode='OBJECT')\n"
470 "obj.select_set(True)\n";
int pad[32 - sizeof(int)]
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
__forceinline float3 center() const
void visualize(std::ofstream &file, const std::string object_name) const
int get_num_nodes() const
float3 voxel_size() const
int flatten_index(int x, int y, int z) const
void flatten(KernelOctreeNode *, const int, const std::shared_ptr< OctreeNode > &, int &) const
void set_flattened(const bool=true)
float3 index_to_position(int x, int y, int z) const
std::shared_ptr< OctreeNode > get_root() const
void build(Device *, Progress &, const Object *, const Shader *)
bool is_flattened() const
Octree(const BoundBox &bbox)
void set_substatus(const string &substatus_)
static bool is_homogeneous_volume(const Object *, const Shader *)
static void fill_shader_input(device_vector< KernelShaderEvalInput > &d_input, const Octree *octree, const Object *object, const Shader *shader, const int resolution)
static void read_shader_output(const device_vector< float > &d_output, const Octree *octree, const int num_channels, const int resolution, vector< Extrema< float > > &sigmas)
#define VOLUME_OCTREE_MAX_DEPTH
#define CCL_NAMESPACE_END
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
VecBase< float, 3 > float3
float safe_divide(float a, float b)
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
ccl_device_inline int power_of_2(const int a)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
ccl_device_inline float3 one_float3()
ccl_device_inline bool any_zero(const float3 a)
int size(const AssetLibraryReference *library_reference)
const std::any * find(const AnyDictionary &dictionary, const std::string &key)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
@ SHADER_EVAL_VOLUME_DENSITY
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
string to_string(const char *str)
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
void visualize(std::string &str) const