Blender V5.3
node_geo_sdf_grid_boolean.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#ifdef WITH_OPENVDB
6# include <openvdb/openvdb.h>
7# include <openvdb/tools/Composite.h>
8#endif
9
10#include "BKE_volume_grid.hh"
11
13
14#include "NOD_rna_define.hh"
15
17#include "UI_resources.hh"
18
19#include "node_geometry_util.hh"
20
22
23enum class Operation {
25 Union = 1,
26 Difference = 2,
27};
28
30{
31 const bNode *node = b.node_or_null();
32
33 auto &first_grid =
34 b.add_input<decl::Float>("Grid 1"_ustr).hide_value().structure_type(StructureType::Grid);
35
36 if (node) {
37 static const auto make_available = [](bNode &node) {
39 };
40 switch (Operation(node->custom1)) {
43 b.add_input<decl::Float>("Grid"_ustr, "Grid 2"_ustr)
44 .hide_value()
45 .multi_input()
46 .make_available(make_available)
47 .structure_type(StructureType::Grid);
48 break;
50 b.add_input<decl::Float>("Grid 2"_ustr)
51 .hide_value()
52 .multi_input()
53 .make_available(make_available)
54 .structure_type(StructureType::Grid);
55 break;
56 }
57 }
58
59 b.add_output<decl::Float>("Grid"_ustr).hide_value().structure_type(StructureType::Grid);
60
61 if (node) {
62 switch (Operation(node->custom1)) {
65 first_grid.available(false);
66 break;
68 first_grid.available(true);
69 break;
70 }
71 }
72}
73
74static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
75{
76 layout.prop(ptr, "operation", UI_ITEM_NONE, "", ICON_NONE);
77}
78
79static void node_init(bNodeTree * /*tree*/, bNode *node)
80{
82}
83
84#ifdef WITH_OPENVDB
85
86static std::optional<std::string> validate_sdf_grid(const openvdb::FloatGrid &grid)
87{
88 if (grid.background() <= 0.0f) {
89 return "SDF background value must be greater than zero";
90 }
91 return std::nullopt;
92}
93
94/* The first valid operand is used to define the output grid. */
95struct ResultData {
96 bke::VolumeGrid<float> operand;
97 bke::VolumeTreeAccessToken tree_token;
98 openvdb::FloatGrid &grid;
99};
100
101#endif
102
104{
105#ifdef WITH_OPENVDB
106 const Operation operation = Operation(params.node().custom1);
107
109 params.extract_input<GeoNodesMultiInput<bke::VolumeGrid<float>>>("Grid 2"_ustr).values;
110 if (operands.is_empty()) {
112 return;
113 }
114
115 std::optional<ResultData> result;
116 if (operation == Operation::Difference) {
117 bke::VolumeGrid<float> primary = params.extract_input<bke::VolumeGrid<float>>("Grid 1"_ustr);
118 if (!primary) {
120 return;
121 }
122
123 bke::VolumeTreeAccessToken tree_token;
124 openvdb::FloatGrid &grid = primary.grid_for_write(tree_token);
125 if (std::optional error = validate_sdf_grid(grid)) {
126 params.error_message_add(NodeWarningType::Warning, TIP_("Grid: " + (*error)));
128 return;
129 }
130
131 result.emplace(ResultData{std::move(primary), std::move(tree_token), grid});
132 }
133
134 for (bke::VolumeGrid<float> &operand : operands) {
135 if (!operand) {
136 continue;
137 }
138
139 bke::VolumeTreeAccessToken tree_token;
140 std::shared_ptr<openvdb::FloatGrid> resampled_storage;
141 openvdb::FloatGrid &grid =
142 result ? geometry::resample_sdf_grid_if_necessary(
143 operand, tree_token, result->grid.transform(), resampled_storage) :
144 operand.grid_for_write(tree_token);
145 if (std::optional error = validate_sdf_grid(grid)) {
146 params.error_message_add(NodeWarningType::Warning, TIP_("Grid 2: " + (*error)));
147 continue;
148 }
149
150 if (result) {
151 try {
152 switch (operation) {
154 openvdb::tools::csgIntersection(result->grid, grid);
155 break;
156 case Operation::Union:
157 openvdb::tools::csgUnion(result->grid, grid);
158 break;
160 openvdb::tools::csgDifference(result->grid, grid);
161 break;
162 }
163 }
164 catch (const openvdb::Exception & /*ex*/) {
165 params.error_message_add(NodeWarningType::Error, TIP_("OpenVDB error"));
167 return;
168 }
169 result->operand->tag_tree_modified();
170 }
171 else {
172 /* Resampling only starts with the 2nd valid operand. */
173 BLI_assert(!resampled_storage);
174 result.emplace(ResultData{std::move(operand), std::move(tree_token), grid});
175 }
176 }
177
178 if (result) {
179 params.set_output("Grid"_ustr, std::move(result->operand));
180 }
181 else {
183 }
184#else
186#endif
187}
188
189static void node_rna(StructRNA *srna)
190{
191 static const EnumPropertyItem operation_items[] = {
193 "INTERSECT",
194 0,
195 "Intersect",
196 "Keep the part of the grids that is common between all operands"},
197 {int(Operation::Union), "UNION", 0, "Union", "Combine grids in an additive way"},
199 "DIFFERENCE",
200 0,
201 "Difference",
202 "Combine grids in a subtractive way"},
203 {0, nullptr, 0, nullptr, nullptr},
204 };
205
207 "operation",
208 "Operation",
209 "",
210 operation_items,
213}
214
215static void node_register()
216{
217 static bke::bNodeType ntype;
218 geo_node_type_base(&ntype, "GeometryNodeSDFGridBoolean"_ustr, GEO_NODE_SDF_GRID_BOOLEAN);
219 ntype.ui_name = "SDF Grid Boolean";
220 ntype.ui_description = "Cut, subtract, or join multiple SDF volume grid inputs";
221 ntype.enum_name_legacy = "SDF_GRID_BOOLEAN";
223 ntype.declare = node_declare;
224 ntype.initfunc = node_init;
228 node_rna(ntype.rna_ext.srna);
229}
231
232} // namespace blender::nodes::node_geo_sdf_grid_boolean_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SDF_GRID_BOOLEAN
#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
bool is_empty() const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void make_available(bNode &node) const
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
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)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed short int16_t
Definition BKE_text.h:77
@ Union
Definition types.hh:109
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
const char * enum_name_legacy
Definition BKE_node.hh:274
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)