Blender V5.3
node_geo_sdf_grid_mean.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"
6
8
9#ifdef WITH_OPENVDB
10# include "openvdb/tools/LevelSetFilter.h"
11#endif
12
14
16{
17 b.use_custom_socket_order();
18 b.allow_any_socket_order();
19 b.add_input<decl::Float>("Grid"_ustr).hide_value().structure_type(StructureType::Grid);
20 b.add_output<decl::Float>("Grid"_ustr).structure_type(StructureType::Grid).align_with_previous();
21 b.add_input<decl::Int>("Width"_ustr)
22 .default_value(1)
23 .min(0)
24 .description("Filter kernel radius in voxels");
25 b.add_input<decl::Int>("Iterations"_ustr)
26 .default_value(1)
27 .min(0)
28 .description("Number of iterations to apply the filter");
29}
30
32{
33#ifdef WITH_OPENVDB
34 auto grid = params.extract_input<bke::VolumeGrid<float>>("Grid"_ustr);
35 if (!grid) {
37 return;
38 }
39
40 const int iterations = params.extract_input<int>("Iterations"_ustr);
41 const int width = params.extract_input<int>("Width"_ustr);
42 if (iterations <= 0 || width <= 0) {
43 params.set_output("Grid"_ustr, std::move(grid));
44 return;
45 }
46
47 bke::VolumeTreeAccessToken tree_token;
48 openvdb::FloatGrid &vdb_grid = grid.grid_for_write(tree_token);
49
50 try {
51 openvdb::tools::LevelSetFilter<openvdb::FloatGrid> filter(vdb_grid);
52 for (int i = 0; i < iterations; i++) {
53 filter.mean(width);
54 }
55 }
56 catch (const openvdb::RuntimeError & /*e*/) {
58 return;
59 }
60
61 params.set_output("Grid"_ustr, std::move(grid));
62#else
64#endif
65}
66
67static void node_register()
68{
69 static bke::bNodeType ntype;
70 geo_node_type_base(&ntype, "GeometryNodeSDFGridMean"_ustr);
71 ntype.ui_name = "SDF Grid Mean";
72 ntype.ui_description =
73 "Apply mean (box) filter smoothing to a signed distance field. Fast separable averaging "
74 "filter for general smoothing of the distance field";
76 ntype.declare = node_declare;
79}
81
82} // namespace blender::nodes::node_geo_sdf_grid_mean_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define NOD_REGISTER_NODE(REGISTER_FUNC)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
void node_geo_sdf_grid_error_not_levelset(GeoNodeExecParams &params)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
NodeDeclareFunction declare
Definition BKE_node.hh:393