Blender V5.3
node_geo_points_to_volume.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 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/LevelSetUtil.h>
8# include <openvdb/tools/ParticlesToLevelSet.h>
9#endif
10
11#include "BLI_bounds.hh"
12
13#include "node_geometry_util.hh"
14
17
18#include "BKE_lib_id.hh"
19#include "BKE_volume.hh"
20
22
23#ifdef WITH_OPENVDB
24
25static void gather_point_data_from_component(Field<float> radius_field,
26 const GeometryComponent &component,
27 Vector<float3> &r_positions,
28 Vector<float> &r_radii)
29{
30 if (component.is_empty()) {
31 return;
32 }
33 const VArray<float3> positions = *component.attributes()->lookup<float3>("position");
34 if (!positions) {
35 return;
36 }
37
38 const bke::GeometryFieldContext field_context{component, AttrDomain::Point};
39 const int domain_num = component.attribute_domain_size(AttrDomain::Point);
40
41 r_positions.resize(r_positions.size() + domain_num);
42 positions.materialize(r_positions.as_mutable_span().take_back(domain_num));
43
44 r_radii.resize(r_radii.size() + domain_num);
45 fn::FieldEvaluator evaluator{field_context, domain_num};
46 evaluator.add_with_destination(radius_field, r_radii.as_mutable_span().take_back(domain_num));
47 evaluator.evaluate();
48}
49
50static float compute_voxel_size_from_amount(const float voxel_amount,
51 Span<float3> positions,
52 const float radius)
53{
54 if (positions.is_empty()) {
55 return 0.0f;
56 }
57
58 if (voxel_amount <= 1) {
59 return 0.0f;
60 }
61
62 const Bounds<float3> bounds = *bounds::min_max(positions);
63
64 /* The voxel size adapts to the final size of the volume. */
65 const float diagonal = math::distance(bounds.min, bounds.max);
66 const float extended_diagonal = diagonal + 2.0f * radius;
67 const float voxel_size = extended_diagonal / voxel_amount;
68 return voxel_size;
69}
70
75static void initialize_volume_component_from_points(GeoNodeExecParams &params,
76 GeometrySet &r_geometry_set)
77{
78 Vector<float3> positions;
79 Vector<float> radii;
80 Field<float> radius_field = params.get_input<Field<float>>("Radius"_ustr);
81
85 {
86 if (r_geometry_set.has(type)) {
87 gather_point_data_from_component(
88 radius_field, *r_geometry_set.get_component(type), positions, radii);
89 }
90 }
91
92 if (positions.is_empty()) {
93 return;
94 }
95
96 const auto resolution_mode = params.get_input<GeometryNodePointsToVolumeResolutionMode>(
97 "Resolution Mode"_ustr);
98
99 float voxel_size = 0.0f;
100 if (resolution_mode == GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_SIZE) {
101 voxel_size = params.get_input<float>("Voxel Size"_ustr);
102 }
103 else if (resolution_mode == GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_AMOUNT) {
104 const float voxel_amount = params.get_input<float>("Voxel Amount"_ustr);
105 const float max_radius = *std::max_element(radii.begin(), radii.end());
106 voxel_size = compute_voxel_size_from_amount(voxel_amount, positions, max_radius);
107 }
108 else {
109 BLI_assert_msg(0, "Unknown volume resolution mode");
110 }
111
112 if (!BKE_volume_voxel_size_valid(float3(voxel_size))) {
113 return;
114 }
115
116 Volume *volume = BKE_id_new_nomain<Volume>(nullptr);
117
118 const float density = params.get_input<float>("Density"_ustr);
119 geometry::fog_volume_grid_add_from_points(
120 volume, "density", positions, radii, voxel_size, density);
121
123 r_geometry_set.replace_volume(volume);
124}
125
126#endif /* WITH_OPENVDB */
127
129
132 "VOXEL_AMOUNT",
133 0,
135 N_("Specify the approximate number of voxels along the diagonal")},
137 "VOXEL_SIZE",
138 0,
139
141 N_("Specify the voxel side length")},
142 {0, nullptr, 0, nullptr, nullptr},
143};
144
146{
147 b.add_input<decl::Geometry>("Points"_ustr)
148 .is_default_link_socket()
149 .description("Points which are converted to a volume");
150 b.add_input<decl::Float>("Density"_ustr).default_value(1.0f).min(0.0f);
151 b.add_input<decl::Menu>("Resolution Mode"_ustr)
152 .static_items(resolution_mode_items)
154 .description("How the voxel size is specified")
155 .translation_context(BLT_I18NCONTEXT_COUNTABLE);
156 b.add_input<decl::Float>("Voxel Size"_ustr)
157 .default_value(0.3f)
158 .min(0.01f)
161 b.add_input<decl::Float>("Voxel Amount"_ustr)
162 .default_value(64.0f)
163 .min(0.0f)
165 b.add_input<decl::Float>("Radius"_ustr)
166 .default_value(0.5f)
167 .min(0.0f)
169 .evaluated_geometry_field();
170 b.add_output<decl::Geometry>("Volume"_ustr).translation_context(BLT_I18NCONTEXT_ID_ID);
171}
172
173static void node_init(bNodeTree * /*tree*/, bNode *node)
174{
175 /* Still used for forward compatibility. */
176 node->storage = MEM_new<NodeGeometryPointsToVolume>(__func__);
177}
178
180{
181#ifdef WITH_OPENVDB
182 GeometrySet geometry_set = params.extract_input<GeometrySet>("Points"_ustr);
183 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
184 initialize_volume_component_from_points(params, geometry_set);
185 });
186 params.set_output("Volume"_ustr, std::move(geometry_set));
187#else
189#endif
190}
191
192static void node_register()
193{
194 static bke::bNodeType ntype;
195
196 geo_node_type_base(&ntype, "GeometryNodePointsToVolume"_ustr, GEO_NODE_POINTS_TO_VOLUME);
197 ntype.ui_name = "Points to Volume";
198 ntype.ui_description = "Generate a fog volume sphere around every point";
199 ntype.enum_name_legacy = "POINTS_TO_VOLUME";
202 ntype, "NodeGeometryPointsToVolume", node_free_standard_storage, node_copy_standard_storage);
204 ntype.initfunc = node_init;
205 ntype.declare = node_declare;
208}
210
211} // namespace blender::nodes::node_geo_points_to_volume_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_POINTS_TO_VOLUME
Volume data-block.
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_ID
#define BLT_I18NCONTEXT_COUNTABLE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_msg(a, msg)
constexpr bool is_empty() const
Definition BLI_span.hh:264
void materialize(MutableSpan< T > r_span) const
int64_t size() const
bool is_empty() const
void resize(const int64_t new_size)
MutableSpan< T > as_mutable_span()
int attribute_domain_size(AttrDomain domain) const
virtual std::optional< AttributeAccessor > attributes() const
virtual bool is_empty() const
int add_with_destination(GField field, GVMutableArray dst)
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
std::optional< Bounds< T > > min_max(const std::optional< Bounds< T > > &a, const T &b)
Definition BLI_bounds.hh:56
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
T distance(const T &a, const T &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
@ PROP_DISTANCE
Definition RNA_types.hh:313
GeometryNodePointsToVolumeResolutionMode
@ GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_AMOUNT
@ GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_SIZE
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bool BKE_volume_voxel_size_valid(const float3 &voxel_size)
void replace_volume(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void keep_only(Span< GeometryComponent::Type > component_types)
bool has(const GeometryComponent::Type component_type) const
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
static constexpr int _180
Definition BKE_node.hh:247
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
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
#define N_(msgid)