Blender V5.3
node_geo_grid_to_points.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_math_matrix.hh"
8
10#include "BKE_pointcloud.hh"
11#include "BKE_volume_grid.hh"
12#include "BKE_volume_openvdb.hh"
13
14#include "NOD_rna_define.hh"
15#include "NOD_socket.hh"
17
18#include "UI_interface.hh"
20#include "UI_resources.hh"
21
22#include "RNA_enum_types.hh"
23
24#include "GEO_randomize.hh"
25
26#ifdef WITH_OPENVDB
27# include <openvdb/tree/NodeManager.h>
28#endif
29
31
33{
34 const bNode *node = b.node_or_null();
35 if (!node) {
36 return;
37 }
38
39 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
40 b.use_custom_socket_order();
41 b.allow_any_socket_order();
42
43 b.add_default_layout();
44
45 b.add_output<decl::Geometry>("Points"_ustr)
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");
50
51 auto &panel = b.add_panel("Voxel Index"_ustr).default_closed(true);
52 panel.add_output<decl::Int>("X"_ustr).anonymous_attribute_output().description(
53 "X coordinate of the voxel in index space, or the minimum X coordinate of a tile");
54 panel.add_output<decl::Int>("Y"_ustr).anonymous_attribute_output().description(
55 "Y coordinate of the voxel in index space, or the minimum Y coordinate of a tile");
56 panel.add_output<decl::Int>("Z"_ustr).anonymous_attribute_output().description(
57 "Z coordinate of the voxel in index space, or the minimum Z coordinate of a tile");
58 panel.add_output<decl::Bool>("Is Tile"_ustr)
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 "
65 "represents "
66 "the cubic size of the tile");
67
68 b.add_input(data_type, "Grid"_ustr).hide_value().structure_type(StructureType::Grid);
69}
70
71static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
72{
73 layout.use_property_split_set(true);
74 layout.use_property_decorate_set(false);
75 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
76}
77
78static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
79{
80 switch (socket.type) {
81 case SOCK_FLOAT:
82 return SOCK_FLOAT;
83 case SOCK_BOOLEAN:
84 return SOCK_BOOLEAN;
85 case SOCK_INT:
86 return SOCK_INT;
87 case SOCK_VECTOR:
88 case SOCK_RGBA:
89 return SOCK_VECTOR;
90 default:
91 return std::nullopt;
92 }
93}
94
96{
97 const bNodeSocket &other_socket = params.other_socket();
98 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
99 const eNodeSocketDatatype other_type = other_socket.type;
100
101 if (params.in_out() == SOCK_IN) {
102 if (ELEM(structure_type, StructureType::Grid, StructureType::Dynamic)) {
103 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
104 if (data_type) {
105 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
106 bNode &node = params.add_node("GeometryNodeGridToPoints"_ustr);
107 node.custom1 = *data_type;
108 params.update_and_connect_available_socket(node, "Grid"_ustr);
109 });
110 }
111 }
112 }
113 else {
114 if (params.node_tree().typeinfo->validate_link(SOCK_GEOMETRY, other_type)) {
115 params.add_item(IFACE_("Points"), [](LinkSearchOpParams &params) {
116 bNode &node = params.add_node("GeometryNodeGridToPoints"_ustr);
117 params.update_and_connect_available_socket(node, "Points"_ustr);
118 });
119 }
120 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
121 if (data_type) {
122 params.add_item(IFACE_("Value"), [data_type](LinkSearchOpParams &params) {
123 bNode &node = params.add_node("GeometryNodeGridToPoints"_ustr);
124 node.custom1 = *data_type;
125 params.update_and_connect_available_socket(node, "Value"_ustr);
126 });
127 }
128 }
129}
130
131#ifdef WITH_OPENVDB
132
133template<typename LeafNodeT>
134static void process_leaf_node(const LeafNodeT &leaf_node,
135 const float4x4 &grid_transform,
136 MutableSpan<float3> r_position,
137 MutableSpan<bool> r_is_tile,
138 MutableSpan<int> r_extent,
139 MutableSpan<int> r_coord_x,
140 MutableSpan<int> r_coord_y,
141 MutableSpan<int> r_coord_z,
143{
144 using MaskT = typename LeafNodeT::NodeMaskType;
145
146 r_is_tile.fill(false);
147 r_extent.fill(1);
148
149 const MaskT &mask = leaf_node.getValueMask();
150 int iter_i = 0;
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);
154 const float3 object_pos = math::transform_point(grid_transform,
155 float3(ijk.x(), ijk.y(), ijk.z()));
156 r_position[iter_i] = object_pos;
157 if (!r_coord_x.is_empty()) {
158 r_coord_x[iter_i] = ijk.x();
159 }
160 if (!r_coord_y.is_empty()) {
161 r_coord_y[iter_i] = ijk.y();
162 }
163 if (!r_coord_z.is_empty()) {
164 r_coord_z[iter_i] = ijk.z();
165 }
166 if (!r_value.is_empty()) {
167 r_value[iter_i] = leaf_node.getValue(i_in_node);
168 }
169 }
170}
171
172template<typename InternalNodeT>
173static void process_internal_node(const InternalNodeT &internal_node,
174 const float4x4 &grid_transform,
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)
182{
183 using MaskT = typename InternalNodeT::NodeMaskType;
184 using UnionT = typename InternalNodeT::UnionType;
185
186 r_is_tile.fill(true);
187 r_extent.fill(InternalNodeT::ChildNodeType::DIM);
188
189 int iter_i = 0;
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);
195 const float3 object_pos = math::transform_point(grid_transform,
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();
200 }
201 if (!r_coord_y.is_empty()) {
202 r_coord_y[iter_i] = ijk.y();
203 }
204 if (!r_coord_z.is_empty()) {
205 r_coord_z[iter_i] = ijk.z();
206 }
207 if (!r_value.is_empty()) {
208 r_value[iter_i] = table[i_in_node].getValue();
209 }
210 }
211}
212
213template<typename TreeT>
214static void process_tree(const TreeT &tree,
215 const float4x4 &grid_transform,
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)
223{
224 using ValueT = typename TreeT::ValueType;
225 using RootNodeT = typename TreeT::RootNodeType;
226 using LeafNodeT = typename TreeT::LeafNodeType;
227
228 openvdb::tree::NodeManager<const TreeT> node_manager(tree);
229
230 /* Iterate over all nodes sequentially to figure out how many points need to be created. Also
231 * compute an #IndexRange for each node indicating where the points for that node will be put in
232 * the output. */
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;
243 }
244 },
245 false);
246 const int active_value_count = current_offset;
247
248 /* Initialize all the required output arrays. */
249 r_position.reinitialize(active_value_count);
250 if (r_is_tile.has_value()) {
251 r_is_tile->reinitialize(active_value_count);
252 }
253 if (r_extent.has_value()) {
254 r_extent->reinitialize(active_value_count);
255 }
256 if (r_coord_x.has_value()) {
257 r_coord_x->reinitialize(active_value_count);
258 }
259 if (r_coord_y.has_value()) {
260 r_coord_y->reinitialize(active_value_count);
261 }
262 if (r_coord_z.has_value()) {
263 r_coord_z->reinitialize(active_value_count);
264 }
265 if (r_value.has_value()) {
266 r_value->reinitialize(active_value_count);
267 }
268
269 /* Iterate over all grid nodes in parallel to compute all the required point attributes. */
270 node_manager.foreachTopDown([&]<typename NodeT>(const NodeT &node) {
271 if constexpr (std::is_same_v<NodeT, RootNodeT>) {
272 /* Ignore. */
273 }
274 else {
275 const IndexRange slice = slice_by_node.lookup(&node);
276 if (slice.is_empty()) {
277 return;
278 }
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) :
282 MutableSpan<bool>();
283 const MutableSpan<int> r_extent_slice = r_extent.has_value() ?
284 r_extent->as_mutable_span().slice(slice) :
285 MutableSpan<int>();
286 const MutableSpan<int> r_coord_x_slice = r_coord_x.has_value() ?
287 r_coord_x->as_mutable_span().slice(slice) :
288 MutableSpan<int>();
289 const MutableSpan<int> r_coord_y_slice = r_coord_y.has_value() ?
290 r_coord_y->as_mutable_span().slice(slice) :
291 MutableSpan<int>();
292 const MutableSpan<int> r_coord_z_slice = r_coord_z.has_value() ?
293 r_coord_z->as_mutable_span().slice(slice) :
294 MutableSpan<int>();
295 const MutableSpan<ValueT> r_value_slice =
296 r_value.has_value() ?
298 static_cast<ValueT *>(r_value->as_mutable_span().slice(slice).data()),
299 slice.size()) :
300 MutableSpan<ValueT>();
301 if constexpr (std::is_same_v<NodeT, LeafNodeT>) {
302 process_leaf_node<LeafNodeT>(node,
303 grid_transform,
304 r_position_slice,
305 r_is_tile_slice,
306 r_extent_slice,
307 r_coord_x_slice,
308 r_coord_y_slice,
309 r_coord_z_slice,
310 r_value_slice);
311 }
312 else {
313 process_internal_node<NodeT>(node,
314 grid_transform,
315 r_position_slice,
316 r_is_tile_slice,
317 r_extent_slice,
318 r_coord_x_slice,
319 r_coord_y_slice,
320 r_coord_z_slice,
321 r_value_slice);
322 }
323 }
324 });
325}
326
327#endif
328
330{
331#ifdef WITH_OPENVDB
332 const bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
333 if (!grid) {
335 return;
336 }
337 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.node().custom1);
338 const CPPType *cpp_type = bke::socket_type_to_geo_nodes_base_cpp_type(socket_type);
339 if (!cpp_type) {
341 return;
342 }
343
344 bke::VolumeTreeAccessToken tree_token;
345 const openvdb::GridBase &grid_base = grid->grid(tree_token);
346 const openvdb::TreeBase &tree_base = grid_base.baseTree();
347
348 const float4x4 grid_transform = BKE_volume_transform_to_blender(grid_base.transform());
349
350 std::optional<std::string> coord_x_id = params.get_output_anonymous_attribute_id_if_needed(
351 "X"_ustr);
352 std::optional<std::string> coord_y_id = params.get_output_anonymous_attribute_id_if_needed(
353 "Y"_ustr);
354 std::optional<std::string> coord_z_id = params.get_output_anonymous_attribute_id_if_needed(
355 "Z"_ustr);
356 std::optional<std::string> is_tile_id = params.get_output_anonymous_attribute_id_if_needed(
357 "Is Tile"_ustr);
358 std::optional<std::string> extent_id = params.get_output_anonymous_attribute_id_if_needed(
359 "Extent"_ustr);
360 std::optional<std::string> value_id = params.get_output_anonymous_attribute_id_if_needed(
361 "Value"_ustr);
362
363 Array<float3> position_array;
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;
370
371 if (is_tile_id.has_value()) {
372 is_tile_array.emplace();
373 }
374 if (extent_id.has_value()) {
375 extent_array.emplace();
376 }
377 if (coord_x_id.has_value()) {
378 coord_x_array.emplace();
379 }
380 if (coord_y_id.has_value()) {
381 coord_y_array.emplace();
382 }
383 if (coord_z_id.has_value()) {
384 coord_z_array.emplace();
385 }
386 if (value_id.has_value()) {
387 value_array.emplace(*cpp_type);
388 }
389
390 bool valid_grid_type = false;
391 bke::attribute_math::to_static_type(*cpp_type, [&]<typename ValueT>() {
392 using type_traits = typename bke::VolumeGridTraits<ValueT>;
393 using TreeT = typename type_traits::TreeType;
394
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,
399 grid_transform,
400 position_array,
401 is_tile_array,
402 extent_array,
403 coord_x_array,
404 coord_y_array,
405 coord_z_array,
406 value_array);
407
408 BLI_assert(position_array.size() ==
409 tree_base.activeLeafVoxelCount() + tree_base.activeTileCount());
410 }
411 });
412
413 if (!valid_grid_type) {
415 return;
416 }
417
418 const int points_num = position_array.size();
420 MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
421
422 auto *position_attr = new ImplicitSharedValue<Array<float3>>(std::move(position_array));
423 attributes.add<float3>("position",
424 AttrDomain::Point,
425 bke::AttributeInitShared(position_attr->data.data(), *position_attr));
426 position_attr->remove_user_and_delete_if_last();
427 if (coord_x_id.has_value()) {
428 auto *coord_x_attr = new ImplicitSharedValue<Array<int>>(std::move(*coord_x_array));
429 attributes.add<int>(*coord_x_id,
430 AttrDomain::Point,
431 bke::AttributeInitShared(coord_x_attr->data.data(), *coord_x_attr));
432 coord_x_attr->remove_user_and_delete_if_last();
433 }
434 if (coord_y_id.has_value()) {
435 auto *coord_y_attr = new ImplicitSharedValue<Array<int>>(std::move(*coord_y_array));
436 attributes.add<int>(*coord_y_id,
437 AttrDomain::Point,
438 bke::AttributeInitShared(coord_y_attr->data.data(), *coord_y_attr));
439 coord_y_attr->remove_user_and_delete_if_last();
440 }
441 if (coord_z_id.has_value()) {
442 auto *coord_z_attr = new ImplicitSharedValue<Array<int>>(std::move(*coord_z_array));
443 attributes.add<int>(*coord_z_id,
444 AttrDomain::Point,
445 bke::AttributeInitShared(coord_z_attr->data.data(), *coord_z_attr));
446 coord_z_attr->remove_user_and_delete_if_last();
447 }
448 if (is_tile_id.has_value()) {
449 auto *is_tile_attr = new ImplicitSharedValue<Array<bool>>(std::move(*is_tile_array));
450 attributes.add<bool>(*is_tile_id,
451 AttrDomain::Point,
452 bke::AttributeInitShared(is_tile_attr->data.data(), *is_tile_attr));
453 is_tile_attr->remove_user_and_delete_if_last();
454 }
455 if (extent_id.has_value()) {
456 auto *extent_attr = new ImplicitSharedValue<Array<int>>(std::move(*extent_array));
457 attributes.add<int>(*extent_id,
458 AttrDomain::Point,
459 bke::AttributeInitShared(extent_attr->data.data(), *extent_attr));
460 extent_attr->remove_user_and_delete_if_last();
461 }
462 if (value_id.has_value()) {
463 auto *value_attr = new ImplicitSharedValue<GArray<>>(std::move(*value_array));
464 attributes.add(*value_id,
465 AttrDomain::Point,
467 bke::AttributeInitShared(value_attr->data.data(), *value_attr));
468 value_attr->remove_user_and_delete_if_last();
469 }
470
472 params.set_output("Points"_ustr, GeometrySet::from_pointcloud(pointcloud));
473
474#else
476#endif
477}
478
479static void node_init(bNodeTree * /*tree*/, bNode *node)
480{
481 node->custom1 = SOCK_FLOAT;
482}
483
484static void node_rna(StructRNA *srna)
485{
487 "data_type",
488 "Data Type",
489 "Node socket data type",
494}
495
496static void node_register()
497{
498 static bke::bNodeType ntype;
499
500 geo_node_type_base(&ntype, "GeometryNodeGridToPoints"_ustr);
501 ntype.ui_name = "Grid to Points";
502 ntype.ui_description = "Generate a point cloud from a volume grid's active voxels";
504 ntype.initfunc = node_init;
508 ntype.declare = node_declare;
510
511 node_rna(ntype.rna_ext.srna);
512}
514
515} // namespace blender::nodes::node_geo_grid_to_points_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
General operations for point clouds.
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define ELEM(...)
constexpr IndexRange()=default
constexpr MutableSpan()=default
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
nodes::StructureType inferred_structure_type
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
std::optional< std::string > get_output_anonymous_attribute_id_if_needed(const UString output_identifier, const bool force_create=false)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void to_static_type(const CPPType &cpp_type, Fn &&fn)
PointCloud * pointcloud_new_no_attributes(PointCloudType type, int totpoint)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
Definition node.cc:5969
AttrType cpp_type_to_attribute_type(const CPPType &type)
void debug_randomize_point_order(PointCloud *pointcloud)
Definition randomize.cc:258
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static void node_geo_exec(GeoNodeExecParams params)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
const EnumPropertyItem * grid_socket_type_items_filter_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MatBase< float, 4, 4 > float4x4
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void mask(PageMask &srt, TileMaps &tilemaps, const uint3 global_id, const uint local_tile_index)
eNodeSocketDatatype type
bke::bNodeSocketRuntime * runtime
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)