Blender V5.0
node_geo_points_to_sdf_grid.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#include "BKE_volume.hh"
6#include "BKE_volume_grid.hh"
7
9
10#include "node_geometry_util.hh"
11
13
15{
16 b.add_input<decl::Geometry>("Points").description(
17 "Points whose volume is converted to a signed distance field grid");
18 b.add_input<decl::Float>("Radius")
19 .default_value(0.5f)
20 .min(0.0f)
22 .field_on_all();
23 b.add_input<decl::Float>("Voxel Size").default_value(0.3f).min(0.01f).subtype(PROP_DISTANCE);
24 b.add_output<decl::Float>("SDF Grid").structure_type(StructureType::Grid);
25}
26
27#ifdef WITH_OPENVDB
28
29static void gather_positions_from_component(const GeometryComponent &component,
30 Vector<float3> &r_positions)
31{
32 if (component.is_empty()) {
33 return;
34 }
35 const VArray<float3> positions = *component.attributes()->lookup<float3>("position");
36 r_positions.resize(r_positions.size() + positions.size());
37 positions.materialize(r_positions.as_mutable_span().take_back(positions.size()));
38}
39
40static void gather_radii_from_component(const GeometryComponent &component,
41 const Field<float> radius_field,
42 Vector<float> &r_radii)
43{
44 if (component.is_empty()) {
45 return;
46 }
47
48 const bke::GeometryFieldContext field_context{component, AttrDomain::Point};
49 const int domain_num = component.attribute_domain_size(AttrDomain::Point);
50
51 r_radii.resize(r_radii.size() + domain_num);
52 fn::FieldEvaluator evaluator{field_context, domain_num};
53 evaluator.add_with_destination(radius_field, r_radii.as_mutable_span().take_back(domain_num));
54 evaluator.evaluate();
55}
56
61static bke::VolumeGrid<float> points_to_grid(const GeometrySet &geometry_set,
62 const Field<float> &radius_field,
63 const float voxel_size)
64{
65 if (!BKE_volume_voxel_size_valid(float3(voxel_size))) {
66 return {};
67 }
68
69 Vector<float3> positions;
70 Vector<float> radii;
71 for (const GeometryComponent::Type type : {GeometryComponent::Type::Mesh,
72 GeometryComponent::Type::PointCloud,
73 GeometryComponent::Type::Curve})
74 {
75 if (const GeometryComponent *component = geometry_set.get_component(type)) {
76 gather_positions_from_component(*component, positions);
77 gather_radii_from_component(*component, radius_field, radii);
78 }
79 }
80
81 if (positions.is_empty()) {
82 return {};
83 }
84
85 return geometry::points_to_sdf_grid(positions, radii, voxel_size);
86}
87
88#endif /* WITH_OPENVDB */
89
91{
92#ifdef WITH_OPENVDB
93 bke::VolumeGrid<float> grid = points_to_grid(params.extract_input<GeometrySet>("Points"),
94 params.extract_input<Field<float>>("Radius"),
95 params.extract_input<float>("Voxel Size"));
96 if (grid) {
97 params.set_output("SDF Grid", std::move(grid));
98 }
99
100 params.set_default_remaining_outputs();
101#else
103#endif
104}
105
106static void node_register()
107{
108 static blender::bke::bNodeType ntype;
109
110 geo_node_type_base(&ntype, "GeometryNodePointsToSDFGrid", GEO_NODE_POINTS_TO_SDF_GRID);
111 ntype.ui_name = "Points to SDF Grid";
112 ntype.ui_description = "Create a signed distance volume grid from points";
113 ntype.enum_name_legacy = "POINTS_TO_SDF_GRID";
115 ntype.declare = node_declare;
118}
120
121} // namespace blender::nodes::node_geo_points_to_sdf_grid_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_POINTS_TO_SDF_GRID
Volume data-block.
bool BKE_volume_voxel_size_valid(const blender::float3 &voxel_size)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_DISTANCE
Definition RNA_types.hh:256
int64_t size() const
bool is_empty() const
void resize(const int64_t new_size)
MutableSpan< T > as_mutable_span()
void materialize(MutableSpan< T > r_span) const
int64_t size() const
void resize(const int64_t new_size)
MutableSpan< T > as_mutable_span()
virtual std::optional< AttributeAccessor > attributes() const
virtual bool is_empty() const
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void node_declare(NodeDeclarationBuilder &b)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
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
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362