Blender V5.3
points_to_volume.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_color.hh"
10#include "BLI_math_base.hh"
11#include "BLI_math_rotation.hh"
12#include "BLI_string_utils.hh"
13#include "BLI_task.hh"
14#include "BLI_vector_set.hh"
15
16#include "BKE_attribute_math.hh"
17#include "BKE_volume.hh"
18#include "BKE_volume_grid.hh"
20#include "BKE_volume_openvdb.hh"
21
22#include "GEO_grid_samplers.hh"
24
25#include <type_traits>
26
27// #define DEBUG_TIME
28
29#ifdef DEBUG_TIME
30# include "BLI_timeit.hh"
31#endif
32
33#ifdef WITH_OPENVDB
34# include <openvdb/openvdb.h>
35# include <openvdb/points/PointConversion.h>
36# include <openvdb/points/PointTransfer.h>
37# include <openvdb/tools/LevelSetUtil.h>
38# include <openvdb/tools/Morphology.h>
39# include <openvdb/tools/ParticlesToLevelSet.h>
40# include <openvdb/tools/PointIndexGrid.h>
41#endif
42
43namespace blender::geometry {
44
46{
47 switch (rasterize_type) {
49 return CPPType::get<float>();
51 return CPPType::get<float3>();
52 }
54 return CPPType::get<float>();
55}
56
58{
59 switch (rasterize_type) {
61 return CPPType::get<float>();
63 return CPPType::get<float3>();
64 }
66 return CPPType::get<float>();
67}
68
69#ifdef WITH_OPENVDB
70
71/* Implements the interface required by #openvdb::tools::ParticlesToLevelSet. */
72class OpenVDBParticleList {
73 public:
74 using PosType = openvdb::Vec3R;
75
76 private:
77 Span<float3> positions_;
78 Span<float> radii_;
79 float voxel_size_inv_;
80
81 public:
82 OpenVDBParticleList(const Span<float3> positions,
83 const Span<float> radii,
84 const float voxel_size)
85 : positions_(positions), radii_(radii), voxel_size_inv_(math::rcp(voxel_size))
86 {
87 BLI_assert(voxel_size > 0.0f);
88 }
89
90 size_t size() const
91 {
92 return size_t(positions_.size());
93 }
94
95 void getPos(size_t n, openvdb::Vec3R &xyz) const
96 {
97 const float3 pos = positions_[n] * voxel_size_inv_;
98 xyz = &pos.x;
99 }
100
101 void getPosRad(size_t n, openvdb::Vec3R &xyz, openvdb::Real &radius) const
102 {
103 this->getPos(n, xyz);
104 radius = radii_[n] * voxel_size_inv_;
105 }
106};
107
108static openvdb::FloatGrid::Ptr points_to_sdf_grid_impl(const Span<float3> positions,
109 const Span<float> radii,
110 const float voxel_size)
111{
112 if (!BKE_volume_voxel_size_valid(float3(voxel_size))) {
113 return nullptr;
114 }
115
116 /* Create a new grid that will be filled. #ParticlesToLevelSet requires
117 * the background value to be positive */
118 openvdb::FloatGrid::Ptr new_grid = openvdb::FloatGrid::create(1.0f);
119
120 /* Create a narrow-band level set grid based on the positions and radii. */
121 openvdb::tools::ParticlesToLevelSet op{*new_grid};
122 /* Don't ignore particles based on their radius. */
123 op.setRmin(0.0f);
124 op.setRmax(std::numeric_limits<float>::max());
125 OpenVDBParticleList particles{positions, radii, voxel_size};
126 op.rasterizeSpheres(particles);
127 op.finalize();
128
129 new_grid->transform().postScale(voxel_size);
130 new_grid->setGridClass(openvdb::GRID_LEVEL_SET);
131
132 return new_grid;
133}
134
135bke::VolumeGrid<float> points_to_sdf_grid(const Span<float3> positions,
136 const Span<float> radii,
137 const float voxel_size)
138{
139 return bke::VolumeGrid<float>(points_to_sdf_grid_impl(positions, radii, voxel_size));
140}
141
142bke::VolumeGridData *fog_volume_grid_add_from_points(Volume *volume,
143 const StringRefNull name,
144 const Span<float3> positions,
145 const Span<float> radii,
146 const float voxel_size,
147 const float density)
148{
149 openvdb::FloatGrid::Ptr new_grid = points_to_sdf_grid_impl(positions, radii, voxel_size);
150 new_grid->setGridClass(openvdb::GRID_FOG_VOLUME);
151
152 /* Convert the level set to a fog volume. This also sets the background value to zero. Inside the
153 * fog there will be a density of 1. */
154 openvdb::tools::sdfToFogVolume(*new_grid);
155
156 /* Take the desired density into account. */
157 openvdb::tools::foreach(new_grid->beginValueOn(),
158 [&](const openvdb::FloatGrid::ValueOnIter &iter) {
159 iter.modifyValue([&](float &value) { value *= density; });
160 });
161
162 return BKE_volume_grid_add_vdb(*volume, name, std::move(new_grid));
163}
164
165bool is_point_attribute_grid_supported(const CPPType &cpp_type)
166{
167 return ELEM(cpp_type,
168 CPPType::get<bool>(),
169 CPPType::get<float>(),
170 CPPType::get<int>(),
171 CPPType::get<int64_t>(),
172 CPPType::get<float3>(),
173 CPPType::get<int3>(),
174 CPPType::get<float4x4>());
175}
176
177/* Remove characters that are invalid for OpenVDB attribute names. */
178static std::string sanitize_name_for_openvdb(const StringRef name)
179{
180 /* Based on openvdb::points::AttributeSet::Descriptor::validName. */
181 std::string result = name;
182 result.erase(std::remove_if(result.begin(),
183 result.end(),
184 [](const char c) {
185 return !(isalnum(c) || (c == '_') || (c == '|') || (c == ':'));
186 }),
187 result.end());
188 return result;
189}
190
191/* Find a unique attribute name based on a generic string that may contain invalid characters. */
192[[maybe_unused]] static std::string add_unique_vdb_attribute_name(
193 const StringRef name, VectorSet<std::string> &used_names)
194{
195 const std::string vdb_base_name = sanitize_name_for_openvdb(name);
196
197 std::string vdb_name = vdb_base_name;
198 int duplicates = 0;
199 while (!used_names.add(vdb_name)) {
200 ++duplicates;
201 vdb_name = vdb_base_name + "_" + std::to_string(duplicates);
202 }
203 return vdb_name;
204}
205
206static std::optional<StringRef> find_vdb_attribute_name(const PointAttributeNameMap &attribute_map,
207 const StringRef name)
208{
209 for (const std::pair<std::string, std::string> &name_pair : attribute_map) {
210 if (name_pair.first == name) {
211 return name_pair.second;
212 }
213 }
214 return std::nullopt;
215}
216
217/* Helper class providing a point data interface to OpenVDB. */
218template<typename T> class PointAttributeSpan {
219 private:
220 Span<T> data_;
221
222 public:
223 using type_traits = bke::VolumeGridTraits<T>;
224
225 using value_type = typename type_traits::PrimitiveType;
226 using PosType = value_type;
227
228 PointAttributeSpan(const Span<T> data) : data_(data) {}
229
230 size_t size() const
231 {
232 return data_.size();
233 }
234 void getPos(size_t n, PosType &xyz) const
235 {
236 xyz = type_traits::to_openvdb(data_[n]);
237 }
238 void get(value_type &value, size_t n) const
239 {
240 value = type_traits::to_openvdb(data_[n]);
241 }
242 void get(value_type &value, size_t n, openvdb::Index m) const
243 {
244 value = type_traits::to_openvdb(data_[n + m]);
245 }
246};
247
248MappedPointDataGrid points_to_point_data_grid(const Span<float3> positions,
249 const Span<PointDataGridAttributeInfo> attributes,
250 const float4x4 &transform)
251{
252# ifdef DEBUG_TIME
253 SCOPED_TIMER(__func__);
254# endif
255 const PointAttributeSpan positions_wrapper(positions);
256 /* Note: The createPointIndexGrid function is expecting a cell-centered transform while the input
257 * transform is corner-centered! The internal PointPartitioner can be configured, but that
258 * argument is not exposed. */
259 const float4x4 transform_cell_centered = transform * math::from_location<float4x4>(float3(0.5f));
260 const openvdb::math::Transform vdb_transform = BKE_volume_transform_to_openvdb(transform);
261 const openvdb::math::Transform vdb_transform_cell_centered = BKE_volume_transform_to_openvdb(
262 transform_cell_centered);
263
264 /* Create point index grid in advance so it can be used for all attribute grids. */
265 openvdb::tools::PointIndexGrid::Ptr point_index_grid =
266 openvdb::tools::createPointIndexGrid<openvdb::tools::PointIndexGrid>(
267 positions_wrapper, vdb_transform_cell_centered);
268
269 openvdb::points::PointDataGrid::Ptr point_data_grid;
270 VectorSet<std::string> used_names;
271 Vector<std::pair<std::string, std::string>> attribute_map;
272 /* OpenVDB holds a global attribute registry lock while running parallel tasks in
273 * #createPointDataGrid and #appendAttribute. Isolate to prevent a waiting thread from executing
274 * another point conversion that tries to acquire the same non-recursive lock. */
275 threading::isolate_task([&]() {
276 /* Convert the main positions array.
277 * Note: Use the corner-centered transform here so that position data in the points array is in
278 * the regular 0..1 sampling space. */
279 point_data_grid = openvdb::points::createPointDataGrid<openvdb::points::NullCodec,
280 openvdb::points::PointDataGrid>(
281 *point_index_grid, positions_wrapper, vdb_transform);
282
283 for (const PointDataGridAttributeInfo &info : attributes) {
284 const CPPType &cpp_type = info.data.type();
285 bke::attribute_math::to_static_type(cpp_type, [&]<typename ValueT>() {
286 using type_traits = typename bke::VolumeGridTraits<ValueT>;
287
288 if constexpr (!std::is_same_v<typename type_traits::PrimitiveType, void>) {
289 /* Note: some attributes could benefit from specialized codecs. The OpenVDB cookbook
290 * suggests to store e.g. radius attribute with a fixed-point codec. This is not
291 * supported here yet.
292 */
293 // openvdb::points::FixedPointCodec</*1-byte=*/false, openvdb::points::UnitRange>;
294 using Codec = openvdb::points::NullCodec;
295 using AttributeArray =
296 openvdb::points::TypedAttributeArray<typename type_traits::PrimitiveType, Codec>;
297 if (!AttributeArray::isRegistered()) {
298 AttributeArray::registerType();
299 }
300
301 const std::string vdb_name = add_unique_vdb_attribute_name(info.name, used_names);
302 attribute_map.append_as(info.name, vdb_name);
303
304 openvdb::NamePair point_data_attribute = AttributeArray::attributeType();
305 openvdb::points::appendAttribute(
306 point_data_grid->tree(), vdb_name, point_data_attribute);
307
308 const PointAttributeSpan data_wrapper(info.data.typed<ValueT>());
309 openvdb::points::populateAttribute(
310 point_data_grid->tree(), point_index_grid->tree(), vdb_name, data_wrapper);
311 }
312 });
313 }
314 });
315
316 return {bke::GVolumeGrid(std::move(point_data_grid)), std::move(attribute_map)};
317}
318
319MappedPointDataGrid points_to_point_data_grid(const VArray<float3> positions,
320 const bke::AttributeAccessor &attributes,
321 const bke::AttributeFilter &attribute_filter,
322 const float4x4 &transform)
323{
324 const VArraySpan<float3> positions_span = positions;
325
326 Vector<GVArraySpan> attribute_arrays;
327 Vector<PointDataGridAttributeInfo> attributes_info;
328 MappedPointDataGrid result;
329 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
330 if (attribute_filter.allow_skip(iter.name)) {
331 return;
332 }
333 const CPPType &cpp_type = bke::attribute_type_to_cpp_type(iter.data_type);
334 if (!is_point_attribute_grid_supported(cpp_type)) {
335 return;
336 }
337 const bke::GAttributeReader reader = iter.get(bke::AttrDomain::Point);
338 if (!reader) {
339 return;
340 }
341 attribute_arrays.append(*reader);
342 attributes_info.append({iter.name, attribute_arrays.last()});
343
344 result = points_to_point_data_grid(positions_span, attributes_info, transform);
345 });
346
347 return result;
348}
349
350namespace kernel_functions {
351
352/* Range of a kernel function in voxels.
353 *
354 * TODO currently this expands the search box uniformly in both directions.
355 * Some kernel functions (nearest point, quadratic) only affect an odd number of voxels.
356 * A smaller search box could be used by sorting the point data grid with a half-voxel offset.
357 * This is equivalent to the offset applied to sampling positions,
358 * see geometry::grid_sampling::sample_tree.
359 */
360inline int kernel_range(const KernelType kernel_type)
361{
362 using namespace geometry::grid_sampling;
363
364 switch (kernel_type) {
365 case KernelType::NearestPoint:
366 return std::max(NearestPointKernel::samples_left, NearestPointKernel::samples_right);
367 case KernelType::Linear:
368 return std::max(LinearKernel::samples_left, LinearKernel::samples_right);
369 case KernelType::Quadratic:
370 return std::max(QuadraticBSplineKernel::samples_left, QuadraticBSplineKernel::samples_right);
371 case KernelType::Cubic:
372 return std::max(CubicBSplineKernel::samples_left, CubicBSplineKernel::samples_right);
373 }
375 return 0;
376}
377
378/* Evaluate a kernel weight function in one dimension. */
379inline float kernel_eval_component(const KernelType kernel_type, const float t)
380{
381 switch (kernel_type) {
382 case KernelType::NearestPoint:
383 return geometry::grid_sampling::NearestPointKernel::weight(t);
384 case KernelType::Linear:
385 return geometry::grid_sampling::LinearKernel::weight(t);
386 case KernelType::Quadratic:
387 return geometry::grid_sampling::QuadraticBSplineKernel::weight(t);
388 case KernelType::Cubic:
389 return geometry::grid_sampling::CubicBSplineKernel::weight(t);
390 }
391 return 0.0f;
392}
393
394/* Evaluate a kernel weight function in three dimensions. */
395inline float kernel_eval(const KernelType kernel_type, const float3 &v)
396{
397 return kernel_eval_component(kernel_type, v.x) * kernel_eval_component(kernel_type, v.y) *
398 kernel_eval_component(kernel_type, v.z);
399}
400
401} // namespace kernel_functions
402
428
429/* TODO use multifunction evaluation for efficient kernel eval.
430 * Currently the kernel type is checked at runtime to determine the weights for each point.
431 * This could be optimized by making the kernel type a template argument, but that increases code
432 * generation. Using multi-function evaluation can avoid that but requires significant changes to
433 * the Transfer class and possible rewrite of the rasterization grid operator itself. */
434
435template<typename AttributeT, typename GridValueT>
436struct KernelTransferBase : public openvdb::points::TransformTransfer,
437 public openvdb::points::VolumeTransfer<
438 typename bke::VolumeGridTraits<GridValueT>::TreeType> {
439 using AttributeTraits = bke::VolumeGridTraits<AttributeT>;
440 using AttributeType = typename AttributeTraits::PrimitiveType;
441
442 using GridTraits = bke::VolumeGridTraits<GridValueT>;
443 using TreeType = typename GridTraits::TreeType;
444 using GridType = openvdb::Grid<TreeType>;
445 using GridValueType = typename GridTraits::PrimitiveType;
446 using NodeMaskType = typename TreeType::LeafNodeType::NodeMaskType;
447
448 static const int32_t LOG2DIM = TreeType::LeafNodeType::LOG2DIM;
449 static const int32_t DIM = TreeType::LeafNodeType::DIM;
450
451 KernelType kernel_type_;
452 int kernel_range_;
453
454 /* Point attribute name for input values. */
455 StringRef value_attribute_;
456 /* Point attribute handle for positions in the current leaf. */
457 std::optional<openvdb::points::AttributeHandle<openvdb::Vec3f>> position_handle_;
458 /* Point attribute handle for input values in the current leaf. */
459 std::optional<openvdb::points::AttributeHandle<AttributeType>> value_handle_;
460
461 KernelTransferBase(const KernelType kernel_type,
462 const openvdb::points::PointDataGrid &source,
463 const StringRef value_attribute,
464 GridType &dest)
465 : TransformTransfer(source.transform(), dest.transform()),
466 openvdb::points::VolumeTransfer<TreeType>(dest.tree()),
467 kernel_type_(kernel_type),
468 kernel_range_(kernel_functions::kernel_range(kernel_type)),
469 value_attribute_(value_attribute)
470 {
471 }
472
473 KernelTransferBase(const KernelTransferBase &other)
474 : TransformTransfer(other),
475 openvdb::points::VolumeTransfer<TreeType>(other),
476 kernel_type_(other.kernel_type_),
477 kernel_range_(other.kernel_range_),
478 value_attribute_(other.value_attribute_)
479 {
480 }
481
482 KernelType kernel_type() const
483 {
484 return kernel_type_;
485 }
486
487 /* Search range for point voxels around the target voxel. */
488 openvdb::Int32 range(const openvdb::Coord & /*leaf_origin*/, size_t /*leaf_idx*/) const
489 {
490 return kernel_range_;
491 }
492
493 AttributeType get_value(const openvdb::Index point_index)
494 {
495 return value_handle_->get(point_index);
496 }
497
498 bool startPointLeaf(const openvdb::points::PointDataTree::LeafNodeType &leaf)
499 {
500 position_handle_.emplace(
501 openvdb::points::AttributeHandle<openvdb::Vec3f>(leaf.constAttributeArray("P")));
502
503 BLI_assert(leaf.hasAttribute(value_attribute_));
504 value_handle_.emplace(openvdb::points::AttributeHandle<AttributeType>(
505 leaf.constAttributeArray(value_attribute_)));
506 return true;
507 }
508
517 template<typename ValueFn>
518 void add_points_to_voxels(const openvdb::Coord &ijk,
519 const IndexRange point_index_range,
520 const openvdb::CoordBBox &target_bounds,
521 ValueFn value_fn)
522 {
523 /* Left side includes the target voxel index, subtract 1. */
524 openvdb::CoordBBox intersect_box(ijk.offsetBy(-kernel_range_), ijk.offsetBy(kernel_range_));
525 intersect_box.intersect(target_bounds);
526 if (intersect_box.empty()) {
527 return;
528 }
529
530 auto *const data = this->template buffer<0>();
531 const auto &mask = *(this->template mask<0>());
532
533 for (const openvdb::Index point_index : point_index_range) {
534 const openvdb::Vec3d source_position = ijk.asVec3d() +
535 this->position_handle_->get(point_index);
536 const openvdb::Vec3d target_position = this->transformSourceToTarget(source_position);
537
538 const openvdb::Coord &a(intersect_box.min());
539 const openvdb::Coord &b(intersect_box.max());
540 for (openvdb::Coord c = a; c.x() <= b.x(); ++c.x()) {
541 const openvdb::Index i = ((c.x() & (DIM - 1u)) << 2 * LOG2DIM);
542 for (c.y() = a.y(); c.y() <= b.y(); ++c.y()) {
543 const openvdb::Index j = ((c.y() & (DIM - 1u)) << LOG2DIM);
544 for (c.z() = a.z(); c.z() <= b.z(); ++c.z()) {
545 BLI_assert(target_bounds.isInside(c));
546 const openvdb::Index offset = i + j + /*k*/ (c.z() & (DIM - 1u));
547 if (!mask.isOn(offset)) {
548 continue;
549 }
550
551 const float3 kernel_distance = float3(
552 openvdb::Vec3f(c.asVec3d() - target_position).asV());
553 data[offset] += value_fn(point_index, kernel_distance);
554 }
555 }
556 }
557 }
558 }
559
560 bool endPointLeaf(const openvdb::points::PointDataTree::LeafNodeType & /*leaf_node*/)
561 {
562 /* If endPointLeaf returns false for the given point leaf node
563 * then finalize is called and rasterization may be repeated for that point leaf. */
564 return true;
565 }
566
567 bool finalize(const openvdb::Coord & /*origin*/, size_t /*idx*/)
568 {
569 /* If finalize returns false for the given leaf origin
570 * then the rasterization is repeated for that leaf. */
571 return true;
572 }
573};
574
575template<typename AttributeT, typename GridValueT>
576struct ValueTransfer : public KernelTransferBase<AttributeT, GridValueT> {
577 using Base = KernelTransferBase<AttributeT, GridValueT>;
578 using AttributeType = typename Base::AttributeType;
579
580 using KernelTransferBase<AttributeT, GridValueT>::KernelTransferBase;
581
582 void rasterizePoints(const openvdb::Coord &ijk,
583 const openvdb::Index point_index_begin,
584 const openvdb::Index point_index_end,
585 const openvdb::CoordBBox &target_bounds)
586 {
587 this->add_points_to_voxels(
588 ijk,
589 IndexRange::from_begin_end(point_index_begin, point_index_end),
590 target_bounds,
591 [&](const openvdb::Index point_index, const float3 &kernel_distance) {
592 const AttributeType source_value = this->get_value(point_index);
593 const float weight = kernel_functions::kernel_eval(this->kernel_type(), kernel_distance);
594 return weight * source_value;
595 });
596 }
597};
598
599/* Construct a destination grid for a point data attribute.
600 * The grid transform is determined by the pre-constructed point data grid and the topology is
601 * dilated by the kernel size to ensure all point values are fully rasterized. */
602template<typename GridType>
603static typename GridType::Ptr prepare_destination_grid(
604 const openvdb::points::PointDataGrid &point_data_grid,
605 const float4x4 &transform,
606 const KernelType kernel_type)
607{
608 typename std::shared_ptr<GridType> dst_grid = GridType::create();
609 dst_grid->transform() = BKE_volume_transform_to_openvdb(transform);
610 {
611 /* Activate all voxels with particles in them. */
612# ifdef DEBUG_TIME
613 SCOPED_TIMER(" topologyUnion");
614# endif
615 dst_grid->tree().topologyUnion(point_data_grid.tree());
616 }
617 {
618# ifdef DEBUG_TIME
619 SCOPED_TIMER(" dilateActiveValues");
620# endif
621 /* Dilate to ensure all voxels within range of a particle are active. */
622 const int max_offset = kernel_functions::kernel_range(kernel_type);
623 openvdb::tools::dilateActiveValues(dst_grid->tree(),
624 max_offset,
625 openvdb::tools::NN_FACE_EDGE_VERTEX,
626 openvdb::tools::TilePolicy::PRESERVE_TILES,
627 false);
628 }
629 {
630# ifdef DEBUG_TIME
631 SCOPED_TIMER(" voxelizeActiveTiles");
632# endif
633 /* Voxelize all tiles since each voxel gets a different value. */
634 dst_grid->tree().voxelizeActiveTiles(true);
635 }
636 return dst_grid;
637}
638
639/* Rasterize a point attribute with a known static destination grid type. */
640template<typename AttributeT, typename GridValueT, typename TransferT>
641static bke::GVolumeGrid points_rasterize_with_static_type(
642 const openvdb::points::PointDataGrid &point_data_grid,
643 const StringRef value_attribute,
644 const PointRasterizeAttributeInfo & /*attribute_info*/,
645 const float4x4 &transform,
646 const KernelType kernel_type)
647{
648 using GridTraits = bke::VolumeGridTraits<GridValueT>;
649 using TreeType = typename GridTraits::TreeType;
650 using GridType = openvdb::Grid<TreeType>;
651
652 typename std::shared_ptr<GridType> dst_grid;
653
654 {
655# ifdef DEBUG_TIME
656 SCOPED_TIMER(" prepare_destination_grid");
657# endif
658 dst_grid = prepare_destination_grid<GridType>(point_data_grid, transform, kernel_type);
659 }
660
661 {
662# ifdef DEBUG_TIME
663 SCOPED_TIMER(" rasterize");
664# endif
665 BLI_assert(!value_attribute.is_empty());
666 TransferT transfer(kernel_type, point_data_grid, value_attribute, *dst_grid);
667 openvdb::points::rasterize(point_data_grid, transfer);
668 }
669
670 {
671# ifdef DEBUG_TIME
672 SCOPED_TIMER(" finalize_grid");
673# endif
674 dst_grid->setGridClass(openvdb::GridClass::GRID_FOG_VOLUME);
675 }
676
677 if (dst_grid) {
678 return bke::GVolumeGrid(std::move(dst_grid));
679 }
680 return {};
681}
682
683static bke::GVolumeGrid points_attribute_rasterize(
684 const openvdb::points::PointDataGrid &point_data_grid,
685 const KernelType kernel_type,
686 const StringRef value_attribute,
687 const PointRasterizeAttributeInfo &attribute_info,
688 const float4x4 &transform)
689{
690 bke::GVolumeGrid result;
691 /* Dispatch to the correct static grid type function. */
692 switch (attribute_info.type) {
693 case PointRasterizeType::Scalar:
694 result = points_rasterize_with_static_type<float, float, ValueTransfer<float, float>>(
695 point_data_grid, value_attribute, attribute_info, transform, kernel_type);
696 break;
697 case PointRasterizeType::Vector:
698 result = points_rasterize_with_static_type<float3, float3, ValueTransfer<float3, float3>>(
699 point_data_grid, value_attribute, attribute_info, transform, kernel_type);
700 break;
701 }
702
703 return result;
704}
705
706void points_rasterize(const MappedPointDataGrid &point_data_grid,
707 const KernelType kernel_type,
708 Span<PointRasterizeAttributeInfo> point_attributes,
709 const float4x4 &transform,
710 MutableSpan<bke::GVolumeGrid> r_attribute_grids)
711{
712# ifdef DEBUG_TIME
713 SCOPED_TIMER(__func__);
714# endif
715
716 BLI_assert(r_attribute_grids.size() == point_attributes.size());
717
718 if (!point_data_grid.grid || point_data_grid.grid->grid_type() != VOLUME_GRID_POINTS) {
719 return;
720 }
721 bke::VolumeTreeAccessToken access_token;
722 const openvdb::points::PointDataGrid::ConstPtr vdb_point_data_grid =
723 openvdb::GridBase::grid<openvdb::points::PointDataGrid>(
724 point_data_grid.grid->grid_ptr(access_token));
725 BLI_assert(vdb_point_data_grid);
726
727 for (const int i : point_attributes.index_range()) {
728 const PointRasterizeAttributeInfo &attribute = point_attributes[i];
729# ifdef DEBUG_TIME
730 SCOPED_TIMER(" attribute: " + attribute.name);
731# endif
732
733 const std::optional<std::string> vdb_attribute_name = find_vdb_attribute_name(
734 point_data_grid.attribute_map, attribute.name);
735 BLI_assert(vdb_attribute_name.has_value());
736 r_attribute_grids[i] = points_attribute_rasterize(
737 *vdb_point_data_grid, kernel_type, *vdb_attribute_name, attribute, transform);
738 }
739}
740
741#endif
742
743} // namespace blender::geometry
Volume data-block.
#define SCOPED_TIMER(name)
Definition BLI_timeit.hh:64
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define ELEM(...)
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
static const CPPType & get()
constexpr int64_t size() const
Definition BLI_span.hh:256
#define attribute(slot)
MatBase< 4, 4 > float4x4
VecBase< float, 3 > float3
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
int size(const AssetLibraryReference *library_reference)
const CPPType & points_rasterize_grid_type(const PointRasterizeType rasterize_type)
const CPPType & points_rasterize_attribute_type(const PointRasterizeType rasterize_type)
std::shared_ptr< const T > get(const GenericKey &key, FunctionRef< std::unique_ptr< T >()> compute_fn)
int get_value(int colorid)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMesh const char void * data
VecBase< float, 3 > float3
bool BKE_volume_voxel_size_valid(const float3 &voxel_size)
float x
Definition sky_math.h:136