Blender V5.3
node_geo_grid_topology_boolean.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
5#include "BKE_volume_grid.hh"
7
8#include "NOD_rna_define.hh"
9#include "NOD_socket.hh"
11
13#include "UI_resources.hh"
14
15#include "RNA_enum_types.hh"
16#include "RNA_prototypes.hh"
17
18#include "node_geometry_util.hh"
19
20#ifdef WITH_OPENVDB
21# include <openvdb/tools/Prune.h>
22#endif
23
25
26enum class Operation {
28 Union = 1,
29 Difference = 2,
30};
31
34 "INTERSECT",
35 0,
36 "Intersect",
37 "Keep voxels and tiles that are active in all grids"},
38 {int(Operation::Union),
39 "UNION",
40 0,
41 "Union",
42 "Add voxels or tiles that are active in any grid"},
44 "DIFFERENCE",
45 0,
46 "Difference",
47 "Keep active voxels and tiles of the primary grid that are not active in secondary grids"},
48 {0, nullptr, 0, nullptr, nullptr},
49};
50
52{
53 b.use_custom_socket_order();
54 b.allow_any_socket_order();
55
56 const bNode *node = b.node_or_null();
57 if (!node) {
58 return;
59 }
60 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
61
62 b.add_default_layout();
63
64 b.add_input<decl::Menu>("Operation"_ustr)
65 .default_value(Operation::Intersect)
66 .static_items(operation_items)
68
69 b.add_input(data_type, "Grid 1"_ustr).hide_value().structure_type(StructureType::Grid);
70 b.add_output(data_type, "Grid"_ustr)
71 .hide_value()
72 .structure_type(StructureType::Grid)
73 .align_with_previous();
74
75 b.add_input(data_type, "Grid 2"_ustr)
76 .hide_value()
77 .multi_input()
78 .structure_type(StructureType::Grid);
79}
80
81static void node_init(bNodeTree * /*tree*/, bNode *node)
82{
83 node->custom1 = SOCK_FLOAT;
84}
85
86static void node_rna(StructRNA *srna)
87{
89 "data_type",
90 "Data Type",
91 "Node socket data type",
96}
97
98static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
99{
100 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
101}
102
104{
105#ifdef WITH_OPENVDB
106 const eNodeSocketDatatype data_type = eNodeSocketDatatype(params.node().custom1);
107 const Operation operation = params.extract_input<Operation>("Operation"_ustr);
109 if (auto grid = params.extract_input<bke::GVolumeGrid>("Grid 1"_ustr)) {
110 operands.append(std::move(grid));
111 }
112 const auto grids = params.extract_input<GeoNodesMultiInput<bke::GVolumeGrid>>("Grid 2"_ustr);
113 for (const bke::GVolumeGrid &grid : grids.values) {
114 if (grid) {
115 operands.append(grid);
116 }
117 }
118
119 if (operands.is_empty()) {
121 return;
122 }
123
124 const std::optional<VolumeGridType> grid_type = bke::socket_type_to_grid_type(data_type);
125 if (!grid_type) {
127 return;
128 }
129
130 BKE_volume_grid_type_to_static_type(
131 *grid_type, [&]<std::derived_from<openvdb::GridBase> GridType>() {
132 if constexpr (std::is_same_v<GridType, openvdb::BoolGrid> ||
133 std::is_same_v<GridType, openvdb::FloatGrid> ||
134 std::is_same_v<GridType, openvdb::Int32Grid> ||
135 std::is_same_v<GridType, openvdb::Vec3fGrid>)
136 {
137 bke::VolumeTreeAccessToken result_token;
138 GridType &result_grid = static_cast<GridType &>(
139 operands.first().get_for_write().grid_for_write(result_token));
140 const openvdb::math::Transform &transform = result_grid.transform();
141
142 for (bke::GVolumeGrid &volume_grid : operands.as_mutable_span().drop_front(1)) {
143 bke::VolumeTreeAccessToken operand_token;
144 const GridType &operand_grid = static_cast<const GridType &>(
145 volume_grid.get().grid(operand_token));
146
147 if (operand_grid.transform() != transform) {
149 TIP_("Mismatched grid operand transforms"));
150 }
151
152 try {
153 switch (operation) {
155 result_grid.tree().topologyIntersection(operand_grid.tree());
156 openvdb::tools::pruneInactive(result_grid.tree());
157 break;
158 case Operation::Union:
159 result_grid.tree().topologyUnion(operand_grid.tree());
160 break;
162 result_grid.tree().topologyDifference(operand_grid.tree());
163 openvdb::tools::pruneInactive(result_grid.tree());
164 break;
165 }
166 }
167 catch (const openvdb::ValueError & /*ex*/) {
168 /* May happen if a grid is empty. */
170 return;
171 }
172 }
173 operands.first()->tag_tree_modified();
174 }
175 });
176
177 params.set_output("Grid"_ustr, std::move(operands.first()));
178#else
180#endif
181}
182
183static void node_register()
184{
185 static blender::bke::bNodeType ntype;
186
187 geo_node_type_base(&ntype, "GeometryNodeGridTopologyBoolean"_ustr);
188 ntype.ui_name = "Grid Topology Boolean";
189 ntype.ui_description = "Combine the topology of multiple grids";
191 ntype.declare = node_declare;
192 ntype.initfunc = node_init;
197
198 node_rna(ntype.rna_ext.srna);
199}
201
202} // namespace blender::nodes::node_geo_grid_topology_boolean
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
DBVT_INLINE bool Intersect(const btDbvtAabbMm &a, const btDbvtAabbMm &b)
Definition btDbvt.h:621
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
std::optional< VolumeGridType > socket_type_to_grid_type(eNodeSocketDatatype type)
Definition node.cc:6080
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
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)
@ Union
Definition types.hh:109
static constexpr int _180
Definition BKE_node.hh:247
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
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)