Blender V5.0
node_geo_sdf_grid_median.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").hide_value().structure_type(StructureType::Grid);
20 b.add_output<decl::Float>("Grid").structure_type(StructureType::Grid).align_with_previous();
21 b.add_input<decl::Int>("Width").default_value(1).min(0).description(
22 "Filter kernel radius in voxels");
23 b.add_input<decl::Int>("Iterations")
24 .default_value(1)
25 .min(0)
26 .description("Number of iterations to apply the filter");
27}
28
30{
31#ifdef WITH_OPENVDB
32 auto grid = params.extract_input<bke::VolumeGrid<float>>("Grid");
33 if (!grid) {
34 params.set_default_remaining_outputs();
35 return;
36 }
37
38 const int iterations = params.extract_input<int>("Iterations");
39 const int width = params.extract_input<int>("Width");
40 if (iterations <= 0 || width <= 0) {
41 params.set_output("Grid", std::move(grid));
42 return;
43 }
44
45 bke::VolumeTreeAccessToken tree_token;
46 openvdb::FloatGrid &vdb_grid = grid.grid_for_write(tree_token);
47
48 try {
49 openvdb::tools::LevelSetFilter<openvdb::FloatGrid> filter(vdb_grid);
50 for (int i = 0; i < iterations; i++) {
51 filter.median(width);
52 }
53 }
54 catch (const openvdb::RuntimeError & /*e*/) {
56 return;
57 }
58
59 params.set_output("Grid", std::move(grid));
60#else
62#endif
63}
64
65static void node_register()
66{
67 static blender::bke::bNodeType ntype;
68 geo_node_type_base(&ntype, "GeometryNodeSDFGridMedian");
69 ntype.ui_name = "SDF Grid Median";
70 ntype.ui_description =
71 "Apply median filter to a signed distance field. Reduces noise while preserving sharp "
72 "features and edges in the distance field";
74 ntype.declare = node_declare;
77}
79
80} // namespace blender::nodes::node_geo_sdf_grid_median_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define filter
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230