Blender V4.5
volume_grid_fields.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
7
8#ifdef WITH_OPENVDB
9
10# include <openvdb/math/Transform.h>
11
12namespace blender::bke {
13
14VoxelFieldContext::VoxelFieldContext(const openvdb::math::Transform &transform,
15 const Span<openvdb::Coord> voxels)
16 : transform_(transform), voxels_(voxels)
17{
18}
19
20GVArray VoxelFieldContext::get_varray_for_input(const fn::FieldInput &field_input,
21 const IndexMask & /*mask*/,
22 ResourceScope & /*scope*/) const
23{
24 const bke::AttributeFieldInput *attribute_field_input =
25 dynamic_cast<const bke::AttributeFieldInput *>(&field_input);
26 if (!attribute_field_input) {
27 return {};
28 }
29 if (attribute_field_input->attribute_name() != "position") {
30 return {};
31 }
32
33 /* Support retrieving voxel positions. */
34 Array<float3> positions(voxels_.size());
35 threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
36 for (const int64_t i : range) {
37 const openvdb::Coord &voxel = voxels_[i];
38 const openvdb::Vec3d position = transform_.indexToWorld(voxel);
39 positions[i] = float3(position.x(), position.y(), position.z());
40 }
41 });
42 return VArray<float3>::ForContainer(std::move(positions));
43}
44
45TilesFieldContext::TilesFieldContext(const openvdb::math::Transform &transform,
47 : transform_(transform), tiles_(tiles)
48{
49}
50
51GVArray TilesFieldContext::get_varray_for_input(const fn::FieldInput &field_input,
52 const IndexMask & /*mask*/,
53 ResourceScope & /*scope*/) const
54{
55 const bke::AttributeFieldInput *attribute_field_input =
56 dynamic_cast<const bke::AttributeFieldInput *>(&field_input);
57 if (attribute_field_input == nullptr) {
58 return {};
59 }
60 if (attribute_field_input->attribute_name() != "position") {
61 return {};
62 }
63
64 /* Support retrieving tile positions. */
65 Array<float3> positions(tiles_.size());
66 threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
67 for (const int64_t i : range) {
68 const openvdb::CoordBBox &tile = tiles_[i];
69 const openvdb::Vec3d position = transform_.indexToWorld(tile.getCenter());
70 positions[i] = float3(position.x(), position.y(), position.z());
71 }
72 });
73 return VArray<float3>::ForContainer(std::move(positions));
74}
75
76} // namespace blender::bke
77
78#endif
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static VArray ForContainer(ContainerT container)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile * tiles