40 b.use_custom_socket_order();
41 b.allow_any_socket_order();
43 b.add_default_layout();
46 .description(
"A point for each active voxel or tile in the grid");
47 b.add_output(data_type,
"Value"_ustr)
48 .anonymous_attribute_output()
49 .description(
"The grid's value at each voxel");
51 auto &panel =
b.add_panel(
"Voxel Index"_ustr).default_closed(
true);
53 "X coordinate of the voxel in index space, or the minimum X coordinate of a tile");
55 "Y coordinate of the voxel in index space, or the minimum Y coordinate of a tile");
57 "Z coordinate of the voxel in index space, or the minimum Z coordinate of a tile");
59 .anonymous_attribute_output()
60 .
description(
"The point represents a tile (multiple voxels) rather than a single voxel");
61 panel.add_output<
decl::Int>(
"Extent"_ustr)
62 .anonymous_attribute_output()
64 "The size of the tile or voxel. For individual voxels this is 1, for tiles this "
66 "the cubic size of the tile");
68 b.add_input(data_type,
"Grid"_ustr).hide_value().structure_type(StructureType::Grid);
80 switch (socket.
type) {
102 if (
ELEM(structure_type, StructureType::Grid, StructureType::Dynamic)) {
107 node.custom1 = *data_type;
124 node.custom1 = *data_type;
133template<
typename LeafNodeT>
134static void process_leaf_node(
const LeafNodeT &leaf_node,
144 using MaskT =
typename LeafNodeT::NodeMaskType;
146 r_is_tile.
fill(
false);
149 const MaskT &
mask = leaf_node.getValueMask();
151 for (
auto iter =
mask.beginOn(); iter; ++iter, ++iter_i) {
152 const int i_in_node = iter.pos();
153 const openvdb::Coord ijk = leaf_node.offsetToGlobalCoord(i_in_node);
155 float3(ijk.x(), ijk.y(), ijk.z()));
156 r_position[iter_i] = object_pos;
158 r_coord_x[iter_i] = ijk.x();
161 r_coord_y[iter_i] = ijk.y();
164 r_coord_z[iter_i] = ijk.z();
167 r_value[iter_i] = leaf_node.getValue(i_in_node);
172template<
typename InternalNodeT>
173static void process_internal_node(
const InternalNodeT &internal_node,
175 MutableSpan<float3> r_position,
176 MutableSpan<bool> r_is_tile,
177 MutableSpan<int> r_extent,
178 MutableSpan<int> r_coord_x,
179 MutableSpan<int> r_coord_y,
180 MutableSpan<int> r_coord_z,
181 MutableSpan<typename InternalNodeT::ValueType> r_value)
183 using MaskT =
typename InternalNodeT::NodeMaskType;
184 using UnionT =
typename InternalNodeT::UnionType;
186 r_is_tile.fill(
true);
187 r_extent.fill(InternalNodeT::ChildNodeType::DIM);
190 const UnionT *table = internal_node.getTable();
191 const MaskT &
mask = internal_node.getValueMask();
192 for (
auto iter =
mask.beginOn(); iter; ++iter, ++iter_i) {
193 const int i_in_node = iter.pos();
194 const openvdb::Coord ijk = internal_node.offsetToGlobalCoord(i_in_node);
196 float3(ijk.x(), ijk.y(), ijk.z()));
197 r_position[iter_i] = object_pos;
198 if (!r_coord_x.is_empty()) {
199 r_coord_x[iter_i] = ijk.x();
201 if (!r_coord_y.is_empty()) {
202 r_coord_y[iter_i] = ijk.y();
204 if (!r_coord_z.is_empty()) {
205 r_coord_z[iter_i] = ijk.z();
207 if (!r_value.is_empty()) {
208 r_value[iter_i] = table[i_in_node].getValue();
213template<
typename TreeT>
214static void process_tree(
const TreeT &tree,
216 Array<float3> &r_position,
217 std::optional<Array<bool>> &r_is_tile,
218 std::optional<Array<int>> &r_extent,
219 std::optional<Array<int>> &r_coord_x,
220 std::optional<Array<int>> &r_coord_y,
221 std::optional<Array<int>> &r_coord_z,
222 std::optional<GArray<>> &r_value)
224 using ValueT =
typename TreeT::ValueType;
225 using RootNodeT =
typename TreeT::RootNodeType;
226 using LeafNodeT =
typename TreeT::LeafNodeType;
228 openvdb::tree::NodeManager<const TreeT> node_manager(tree);
233 int current_offset = 0;
234 Map<const void *, IndexRange> slice_by_node;
235 node_manager.foreachTopDown(
236 [&]<
typename NodeT>(
const NodeT &
node) {
237 if constexpr (!std::is_same_v<NodeT, RootNodeT>) {
238 using MaskT =
typename NodeT::NodeMaskType;
239 const MaskT &value_mask =
node.getValueMask();
240 const int values_num = value_mask.countOn();
241 slice_by_node.add_new(&
node,
IndexRange(current_offset, values_num));
242 current_offset += values_num;
246 const int active_value_count = current_offset;
249 r_position.reinitialize(active_value_count);
250 if (r_is_tile.has_value()) {
251 r_is_tile->reinitialize(active_value_count);
253 if (r_extent.has_value()) {
254 r_extent->reinitialize(active_value_count);
256 if (r_coord_x.has_value()) {
257 r_coord_x->reinitialize(active_value_count);
259 if (r_coord_y.has_value()) {
260 r_coord_y->reinitialize(active_value_count);
262 if (r_coord_z.has_value()) {
263 r_coord_z->reinitialize(active_value_count);
265 if (r_value.has_value()) {
266 r_value->reinitialize(active_value_count);
270 node_manager.foreachTopDown([&]<
typename NodeT>(
const NodeT &
node) {
271 if constexpr (std::is_same_v<NodeT, RootNodeT>) {
275 const IndexRange slice = slice_by_node.lookup(&
node);
276 if (slice.is_empty()) {
279 const MutableSpan<float3> r_position_slice = r_position.as_mutable_span().slice(slice);
280 const MutableSpan<bool> r_is_tile_slice = r_is_tile.has_value() ?
281 r_is_tile->as_mutable_span().slice(slice) :
283 const MutableSpan<int> r_extent_slice = r_extent.has_value() ?
284 r_extent->as_mutable_span().slice(slice) :
286 const MutableSpan<int> r_coord_x_slice = r_coord_x.has_value() ?
287 r_coord_x->as_mutable_span().slice(slice) :
289 const MutableSpan<int> r_coord_y_slice = r_coord_y.has_value() ?
290 r_coord_y->as_mutable_span().slice(slice) :
292 const MutableSpan<int> r_coord_z_slice = r_coord_z.has_value() ?
293 r_coord_z->as_mutable_span().slice(slice) :
295 const MutableSpan<ValueT> r_value_slice =
296 r_value.has_value() ?
298 static_cast<ValueT *
>(r_value->as_mutable_span().slice(slice).data()),
300 MutableSpan<ValueT>();
301 if constexpr (std::is_same_v<NodeT, LeafNodeT>) {
302 process_leaf_node<LeafNodeT>(
node,
313 process_internal_node<NodeT>(
node,
332 const bke::GVolumeGrid grid = params.
extract_input<bke::GVolumeGrid>(
"Grid"_ustr);
344 bke::VolumeTreeAccessToken tree_token;
345 const openvdb::GridBase &grid_base = grid->grid(tree_token);
346 const openvdb::TreeBase &tree_base = grid_base.baseTree();
348 const float4x4 grid_transform = BKE_volume_transform_to_blender(grid_base.transform());
364 std::optional<Array<bool>> is_tile_array;
365 std::optional<Array<int>> extent_array;
366 std::optional<Array<int>> coord_x_array;
367 std::optional<Array<int>> coord_y_array;
368 std::optional<Array<int>> coord_z_array;
369 std::optional<GArray<>> value_array;
371 if (is_tile_id.has_value()) {
372 is_tile_array.emplace();
374 if (extent_id.has_value()) {
375 extent_array.emplace();
377 if (coord_x_id.has_value()) {
378 coord_x_array.emplace();
380 if (coord_y_id.has_value()) {
381 coord_y_array.emplace();
383 if (coord_z_id.has_value()) {
384 coord_z_array.emplace();
386 if (value_id.has_value()) {
387 value_array.emplace(*cpp_type);
390 bool valid_grid_type =
false;
392 using type_traits =
typename bke::VolumeGridTraits<ValueT>;
393 using TreeT =
typename type_traits::TreeType;
395 if constexpr (!std::is_same_v<TreeT, void>) {
396 valid_grid_type =
true;
397 const TreeT &tree =
static_cast<const TreeT &
>(tree_base);
398 process_tree<TreeT>(tree,
409 tree_base.activeLeafVoxelCount() + tree_base.activeTileCount());
413 if (!valid_grid_type) {
418 const int points_num = position_array.size();
426 position_attr->remove_user_and_delete_if_last();
427 if (coord_x_id.has_value()) {
429 attributes.
add<
int>(*coord_x_id,
432 coord_x_attr->remove_user_and_delete_if_last();
434 if (coord_y_id.has_value()) {
436 attributes.
add<
int>(*coord_y_id,
439 coord_y_attr->remove_user_and_delete_if_last();
441 if (coord_z_id.has_value()) {
443 attributes.
add<
int>(*coord_z_id,
446 coord_z_attr->remove_user_and_delete_if_last();
448 if (is_tile_id.has_value()) {
450 attributes.
add<
bool>(*is_tile_id,
453 is_tile_attr->remove_user_and_delete_if_last();
455 if (extent_id.has_value()) {
457 attributes.
add<
int>(*extent_id,
460 extent_attr->remove_user_and_delete_if_last();
462 if (value_id.has_value()) {
464 attributes.
add(*value_id,
468 value_attr->remove_user_and_delete_if_last();
489 "Node socket data type",
501 ntype.
ui_name =
"Grid to Points";
502 ntype.
ui_description =
"Generate a point cloud from a volume grid's active voxels";