Blender V5.3
node_geo_grid_advect.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 "NOD_rna_define.hh"
7
8#include "RNA_enum_types.hh"
9
10#include "node_geometry_util.hh"
11
13#include "UI_resources.hh"
14
15#include "BKE_volume_grid.hh"
16#include "BKE_volume_openvdb.hh"
17
18#ifdef WITH_OPENVDB
19# include "openvdb/tools/VolumeAdvect.h"
20#endif
21
23
32
33enum class LimiterType : int8_t {
34 None = 0,
35 Clamp = 1,
36 Revert = 2,
37};
38
41 "SEMI",
42 0,
43 N_("Semi-Lagrangian"),
44 N_("1st order semi-Lagrangian integration. Fast but least accurate, suitable for simple "
45 "advection")},
47 "MID",
48 0,
49 N_("Midpoint"),
50 N_("2nd order midpoint integration. Good balance between speed and accuracy for most cases")},
52 "RK3",
53 0,
54 N_("Runge-Kutta 3"),
55 N_("3rd order Runge-Kutta integration. Higher accuracy at moderate computational cost")},
57 "RK4",
58 0,
59 N_("Runge-Kutta 4"),
60 N_("4th order Runge-Kutta integration. Highest accuracy single-step method but slower")},
62 "MAC",
63 0,
64 N_("MacCormack"),
65 N_("MacCormack scheme with implicit diffusion control. Reduces numerical dissipation while "
66 "maintaining stability")},
68 "BFECC",
69 0,
70 N_("BFECC"),
71 N_("Back and Forth Error Compensation and Correction. Advanced scheme that minimizes "
72 "dissipation and diffusion")},
73 {0, nullptr, 0, nullptr, nullptr},
74};
75
78 "NONE",
79 0,
80 N_("None"),
81 N_("No limiting applied. Fastest but may produce artifacts in high-order schemes")},
83 "CLAMP",
84 0,
85 N_("Clamp"),
86 N_("Clamp values to the range of the original neighborhood. Prevents overshooting and "
87 "undershooting")},
89 "REVERT",
90 0,
91 N_("Revert"),
92 N_("Revert to 1st order integration when clamping would be applied. More conservative than "
93 "clamping")},
94 {0, nullptr, 0, nullptr, nullptr},
95};
96
98{
99 b.use_custom_socket_order();
100 b.allow_any_socket_order();
101 b.add_default_layout();
102
103 const bNode *node = b.node_or_null();
104 if (!node) {
105 return;
106 }
107
108 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
109 b.add_input(data_type, "Grid"_ustr)
110 .hide_value()
111 .structure_type(StructureType::Grid)
112 .is_default_link_socket();
113 b.add_output(data_type, "Grid"_ustr).structure_type(StructureType::Grid).align_with_previous();
114 b.add_input<decl::Vector>("Velocity"_ustr).hide_value().structure_type(StructureType::Grid);
115 b.add_input<decl::Float>("Time Step"_ustr)
116 .subtype(PROP_TIME_ABSOLUTE)
117 .default_value(1.0f)
118 .description("Time step for advection in seconds");
119 b.add_input<decl::Menu>("Integration Scheme"_ustr)
120 .static_items(integration_scheme_items)
122 .optional_label()
123 .description("Numerical integration method for advection");
124 b.add_input<decl::Menu>("Limiter"_ustr)
125 .static_items(limiter_type_items)
127 .optional_label()
128 .description("Limiting strategy to prevent numerical artifacts");
129}
130
131static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
132{
133 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
134}
135
136static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
137{
138 switch (socket.type) {
139 case SOCK_FLOAT:
140 return SOCK_FLOAT;
141 case SOCK_INT:
142 return SOCK_INT;
143 case SOCK_VECTOR:
144 case SOCK_RGBA:
145 return SOCK_VECTOR;
146 default:
147 return std::nullopt;
148 }
149}
150
152{
153 const std::optional<eNodeSocketDatatype> data_type = node_type_for_socket_type(
154 params.other_socket());
155 if (!data_type) {
156 return;
157 }
158 if (params.in_out() == SOCK_IN) {
159 if (params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_VECTOR)) {
160 params.add_item(IFACE_("Velocity"), [](LinkSearchOpParams &params) {
161 bNode &node = params.add_node("GeometryNodeGridAdvect"_ustr);
162 params.update_and_connect_available_socket(node, "Velocity"_ustr);
163 });
164 }
165 if (params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_FLOAT)) {
166 params.add_item(IFACE_("Time Step"), [](LinkSearchOpParams &params) {
167 bNode &node = params.add_node("GeometryNodeGridAdvect"_ustr);
168 params.update_and_connect_available_socket(node, "Time Step"_ustr);
169 });
170 }
171 }
172 params.add_item(IFACE_("Grid"), [data_type](LinkSearchOpParams &params) {
173 bNode &node = params.add_node("GeometryNodeGridAdvect"_ustr);
174 node.custom1 = *data_type;
175 params.update_and_connect_available_socket(node, "Grid"_ustr);
176 });
177}
178
179#ifdef WITH_OPENVDB
180static openvdb::tools::Scheme::SemiLagrangian to_openvdb_scheme(const IntegrationScheme scheme)
181{
182 switch (scheme) {
184 return openvdb::tools::Scheme::SEMI;
186 return openvdb::tools::Scheme::MID;
188 return openvdb::tools::Scheme::RK3;
190 return openvdb::tools::Scheme::RK4;
192 return openvdb::tools::Scheme::MAC;
194 return openvdb::tools::Scheme::BFECC;
195 }
196 return openvdb::tools::Scheme::SEMI;
197}
198
199static openvdb::tools::Scheme::Limiter to_openvdb_limiter(const LimiterType limiter)
200{
201 switch (limiter) {
203 return openvdb::tools::Scheme::NO_LIMITER;
205 return openvdb::tools::Scheme::CLAMP;
207 return openvdb::tools::Scheme::REVERT;
208 }
209 return openvdb::tools::Scheme::NO_LIMITER;
210}
211
212template<typename GridType, typename SamplerType = openvdb::tools::Sampler<1>>
213static typename GridType::Ptr advect_grid(const GridType &grid,
214 const openvdb::Vec3SGrid &velocity_grid,
215 const float time_step,
216 const IntegrationScheme scheme,
217 const LimiterType limiter)
218{
219 openvdb::tools::VolumeAdvection<openvdb::Vec3SGrid, false> advection(velocity_grid);
220
221 advection.setIntegrator(to_openvdb_scheme(scheme));
222 advection.setLimiter(to_openvdb_limiter(limiter));
223 return advection.template advect<GridType, SamplerType>(grid, time_step);
224}
225#endif
226
228{
229#ifdef WITH_OPENVDB
230 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
231 if (!grid) {
233 return;
234 }
235
237 "Velocity"_ustr);
238 if (!velocity_grid) {
239 params.set_output("Grid"_ustr, std::move(grid));
240 return;
241 }
242
243 const float time_step = params.extract_input<float>("Time Step"_ustr);
245 "Integration Scheme"_ustr);
246 const LimiterType limiter = params.extract_input<LimiterType>("Limiter"_ustr);
247
248 bke::VolumeTreeAccessToken tree_token;
249 bke::VolumeTreeAccessToken velocity_token;
250 const openvdb::GridBase &grid_base = grid->grid(tree_token);
251 const openvdb::Vec3SGrid &velocity_vdb_grid = velocity_grid.grid(velocity_token);
252
253 /* OpenVDB's advection requires uniform voxel scale on the grid being advected
254 * but not for the velocity grid being sampled. */
255 if (!grid_base.hasUniformVoxels()) {
256 params.error_message_add(
258 TIP_("The input grid must have a uniform voxel scale to be advected."));
259 params.set_output("Grid"_ustr, std::move(grid));
260 return;
261 }
262
263 const VolumeGridType grid_type = grid->grid_type();
264
265 BKE_volume_grid_type_to_static_type(
266 grid_type, [&]<std::derived_from<openvdb::GridBase> GridType>() {
267 if constexpr (std::is_same_v<GridType, openvdb::FloatGrid> ||
268 std::is_same_v<GridType, openvdb::Int32Grid> ||
269 std::is_same_v<GridType, openvdb::Vec3fGrid>)
270 {
271 typename GridType::Ptr result = advect_grid(
272 static_cast<const GridType &>(grid->grid(tree_token)),
273 velocity_vdb_grid,
274 time_step,
275 scheme,
276 limiter);
277 params.set_output("Grid"_ustr, bke::GVolumeGrid(std::move(result)));
278 }
279 else {
280 params.error_message_add(NodeWarningType::Error, "Unsupported grid type for advection");
282 }
283 });
284#else
286#endif
287}
288
289static void node_init(bNodeTree * /*tree*/, bNode *node)
290{
291 node->custom1 = SOCK_FLOAT;
292}
293
294/* Only float, int, and vector grids are supported for advection */
296 PointerRNA * /*ptr*/,
297 PropertyRNA * /*prop*/,
298 bool *r_free)
299{
300 *r_free = true;
302 [](const EnumPropertyItem &item) -> bool {
304 return ELEM(type, SOCK_FLOAT, SOCK_INT, SOCK_VECTOR);
305 });
306}
307
308static void node_rna(StructRNA *srna)
309{
311 "data_type",
312 "Data Type",
313 "Node socket data type",
318}
319
321 const bNode &node,
322 const bNodeSocket &output_socket)
323{
324 return node.input_by_identifier(output_socket.identifier_ustr());
325}
326
327static void node_register()
328{
329 static bke::bNodeType ntype;
330 geo_node_type_base(&ntype, "GeometryNodeGridAdvect"_ustr);
331 ntype.ui_name = "Advect Grid";
332 ntype.ui_description =
333 "Move grid values through a velocity field using numerical integration. Supports multiple "
334 "integration schemes for different accuracy and performance trade-offs";
336 ntype.declare = node_declare;
338 ntype.initfunc = node_init;
343 node_rna(ntype.rna_ext.srna);
344}
346
347} // namespace blender::nodes::node_geo_grid_advect_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define ELEM(...)
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
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 update_and_connect_available_socket(bNode &new_node, UString socket_name)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &output_socket)
static const EnumPropertyItem * advect_grid_socket_type_filter(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
static const EnumPropertyItem integration_scheme_items[]
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_init(bNodeTree *, bNode *node)
static const EnumPropertyItem limiter_type_items[]
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &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)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
@ PROP_TIME_ABSOLUTE
Definition RNA_types.hh:311
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::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
NodeInternallyLinkedInputFunction internally_linked_input
Definition BKE_node.hh:415
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)