Blender V5.3
node_geo_grid_dilate_erode.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
5#include "BKE_volume_grid.hh"
7
8#include "NOD_rna_define.hh"
10
11#include "RNA_enum_types.hh"
12
14#include "UI_resources.hh"
15
16#include "node_geometry_util.hh"
17
18#ifdef WITH_OPENVDB
19# include "openvdb/tools/Morphology.h"
20#endif
21
23
24enum class Connectivity : int8_t {
25 Face = 0,
28};
29
30enum class TilePolicy : int8_t {
31 Ignore = 0,
32 Expand = 1,
34};
35
37{
38 b.use_custom_socket_order();
39 b.allow_any_socket_order();
40 b.add_default_layout();
41 const bNode *node = b.node_or_null();
42 if (!node) {
43 return;
44 }
45 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
46 b.add_input(data_type, "Grid"_ustr).hide_value().structure_type(StructureType::Grid);
47 b.add_output(data_type, "Grid"_ustr).structure_type(StructureType::Grid).align_with_previous();
48
49 static EnumPropertyItem connectivity_items[] = {
51 "FACE",
52 0,
53 N_("Face"),
54 N_("6-connectivity: affect voxels connected by faces only")},
56 "FACE_EDGE",
57 0,
58 N_("Edge"),
59 N_("18-connectivity: affect voxels connected by faces or edges only")},
61 "FACE_EDGE_VERTEX",
62 0,
63 N_("Vertex"),
64 N_("26-connectivity: affect voxels connected by faces, edges, or vertices")},
65 {0, nullptr, 0, nullptr, nullptr},
66 };
67
68 static EnumPropertyItem tile_policy_items[] = {
70 "IGNORE",
71 0,
72 N_("Ignore"),
73 N_("Ignore active tiles; they are neither dilated/eroded nor contribute to the operation")},
75 "EXPAND",
76 0,
77 N_("Expand"),
78 N_("Voxelize active tiles, apply operation, and leave in voxelized state")},
80 "PRESERVE",
81 0,
82 N_("Preserve"),
83 N_("Keep tiles unchanged when possible, only voxelizing if necessary. More memory "
84 "efficient")},
85 {0, nullptr, 0, nullptr, nullptr},
86 };
87
88 b.add_input<decl::Menu>("Connectivity"_ustr)
89 .static_items(connectivity_items)
91 .structure_type(StructureType::Single)
92 .optional_label();
93
94 b.add_input<decl::Menu>("Tiles"_ustr)
95 .static_items(tile_policy_items)
97 .structure_type(StructureType::Single)
98 .optional_label();
99
100 b.add_input<decl::Int>("Steps"_ustr)
101 .default_value(1)
102 .min(-100)
103 .max(100)
104 .structure_type(StructureType::Single)
105 .description("Number of times to dilate or erode the active voxels");
106}
107
108static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
109{
110 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
111}
112
113static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
114{
115 switch (socket.type) {
116 case SOCK_FLOAT:
117 return SOCK_FLOAT;
118 case SOCK_BOOLEAN:
119 return SOCK_BOOLEAN;
120 case SOCK_INT:
121 return SOCK_INT;
122 case SOCK_VECTOR:
123 case SOCK_RGBA:
124 return SOCK_VECTOR;
125 default:
126 return std::nullopt;
127 }
128}
129
131{
132 const bNodeSocket &other_socket = params.other_socket();
133 const StructureType structure_type = other_socket.runtime->inferred_structure_type;
134 const bool supports_grid = ELEM(structure_type, StructureType::Grid, StructureType::Dynamic);
135
136 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(other_socket);
137 if (!data_type) {
138 return;
139 }
140
141 if (params.in_out() == SOCK_IN &&
142 ELEM(structure_type, StructureType::Dynamic, StructureType::Single) &&
144 {
145 params.add_item(IFACE_("Steps"), [](LinkSearchOpParams &params) {
146 bNode &node = params.add_node("GeometryNodeGridDilateAndErode"_ustr);
147 params.update_and_connect_available_socket(node, "Steps"_ustr);
148 });
149 }
150 if (params.node_tree().typeinfo->validate_link(params.other_socket().type, data_type.value())) {
151 if (params.in_out() == SOCK_IN && supports_grid) {
152 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
153 bNode &node = params.add_node("GeometryNodeGridDilateAndErode"_ustr);
154 node.custom1 = *data_type;
155 params.update_and_connect_available_socket(node, "Grid"_ustr);
156 });
157 }
158 else if (params.in_out() == SOCK_OUT) {
159 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
160 bNode &node = params.add_node("GeometryNodeGridDilateAndErode"_ustr);
161 node.custom1 = *data_type;
162 params.update_and_connect_available_socket(node, "Grid"_ustr);
163 });
164 }
165 }
166}
167
168#ifdef WITH_OPENVDB
169static openvdb::tools::NearestNeighbors connectivity_to_openvdb(const Connectivity connectivity)
170{
171 switch (connectivity) {
173 return openvdb::tools::NN_FACE;
175 return openvdb::tools::NN_FACE_EDGE;
177 return openvdb::tools::NN_FACE_EDGE_VERTEX;
178 }
180 return openvdb::tools::NN_FACE;
181}
182
183static openvdb::tools::TilePolicy tile_policy_to_openvdb(const TilePolicy policy)
184{
185 switch (policy) {
187 return openvdb::tools::IGNORE_TILES;
189 return openvdb::tools::EXPAND_TILES;
191 return openvdb::tools::PRESERVE_TILES;
192 }
194 return openvdb::tools::PRESERVE_TILES;
195}
196#endif
197
199{
200#ifdef WITH_OPENVDB
201 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
202 if (!grid) {
204 return;
205 }
206
207 const Connectivity connectivity = params.extract_input<Connectivity>("Connectivity"_ustr);
208 const TilePolicy tile_policy = params.extract_input<TilePolicy>("Tiles"_ustr);
209 const int steps = params.extract_input<int>("Steps"_ustr);
210 if (steps == 0) {
211 params.set_output("Grid"_ustr, std::move(grid));
212 return;
213 }
214
215 const openvdb::tools::NearestNeighbors nn = connectivity_to_openvdb(connectivity);
216 const openvdb::tools::TilePolicy policy = tile_policy_to_openvdb(tile_policy);
217
218 bke::VolumeTreeAccessToken tree_token;
219 openvdb::GridBase &grid_base = grid.get_for_write().grid_for_write(tree_token);
220 bke::volume_grid::to_typed_grid(grid_base, [&](auto &grid) {
221 if (steps > 0) {
222 openvdb::tools::dilateActiveValues(grid.tree(), steps, nn, policy);
223 }
224 else {
225 openvdb::tools::erodeActiveValues(grid.tree(), -steps, nn, policy);
226 }
227 });
228
229 params.set_output("Grid"_ustr, std::move(grid));
230#else
232#endif
233}
234
235static void node_init(bNodeTree * /*tree*/, bNode *node)
236{
237 node->custom1 = SOCK_FLOAT;
238}
239
240static void node_rna(StructRNA *srna)
241{
243 "data_type",
244 "Data Type",
245 "Node socket data type",
250}
251
252static void node_register()
253{
254 static blender::bke::bNodeType ntype;
255
256 geo_node_type_base(&ntype, "GeometryNodeGridDilateAndErode"_ustr);
257 ntype.ui_name = "Grid Dilate & Erode";
258 ntype.ui_description =
259 "Dilate or erode the active regions of a grid. This changes which voxels are "
260 "active but does not change their values.";
262 ntype.initfunc = node_init;
266 ntype.declare = node_declare;
268
269 node_rna(ntype.rna_ext.srna);
270}
272
273} // namespace blender::nodes::node_geo_grid_dilate_erode_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define ELEM(...)
nodes::StructureType inferred_structure_type
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
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 node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
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)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
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
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)
#define N_(msgid)