Blender V5.3
node_geo_mesh_to_points.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#include "BKE_attribute.h"
6#include "BLI_array_utils.hh"
7
8#include "DNA_mesh_types.h"
10
11#include "BKE_attribute_math.hh"
12#include "BKE_pointcloud.hh"
13
14#include "NOD_rna_define.hh"
15
17#include "UI_resources.hh"
18
20
22
23#include "node_geometry_util.hh"
24
26
28
30{
31 b.add_input<decl::Geometry>("Mesh"_ustr)
32 .supported_type(GeometryComponent::Type::Mesh)
33 .description("Mesh whose elements are converted to points");
34 b.add_input<decl::Bool>("Selection"_ustr)
35 .default_value(true)
36 .evaluated_geometry_field()
37 .hide_value();
38 b.add_input<decl::Vector>("Position"_ustr)
39 .evaluated_geometry_field()
40 .default_input_type(NODE_DEFAULT_INPUT_POSITION_FIELD);
41 b.add_input<decl::Float>("Radius"_ustr)
42 .default_value(0.05f)
43 .min(0.0f)
44 .subtype(PROP_DISTANCE)
45 .evaluated_geometry_field();
46 b.add_output<decl::Geometry>("Points"_ustr).propagate_all_geometry();
47}
48
49static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
50{
51 layout.prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
52}
53
54static void node_init(bNodeTree * /*tree*/, bNode *node)
55{
56 NodeGeometryMeshToPoints *data = MEM_new<NodeGeometryMeshToPoints>(__func__);
58 node->storage = data;
59}
60
61static void geometry_set_mesh_to_points(GeometrySet &geometry_set,
62 const Field<float3> &position_field,
63 const Field<float> &radius_field,
64 const Field<bool> &selection_field,
65 const AttrDomain domain,
66 const AttributeFilter &attribute_filter)
67{
68 const Mesh *mesh = geometry_set.get_mesh();
69 if (mesh == nullptr) {
71 return;
72 }
73 const int domain_size = mesh->attributes().domain_size(domain);
74 if (domain_size == 0) {
76 return;
77 }
78 const AttributeAccessor src_attributes = mesh->attributes();
79 const bke::MeshFieldContext field_context{*mesh, domain};
80 fn::FieldEvaluator evaluator{field_context, domain_size};
81 evaluator.set_selection(selection_field);
82 /* Evaluating directly into the point cloud doesn't work because we are not using the full
83 * "min_array_size" array but compressing the selected elements into the final array with no
84 * gaps. */
85 evaluator.add(position_field);
86 evaluator.add(radius_field);
87 evaluator.evaluate();
88 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
89 if (selection.is_empty()) {
91 return;
92 }
93 const VArray<float3> positions_eval = evaluator.get_evaluated<float3>(0);
94 const VArray<float> radii_eval = evaluator.get_evaluated<float>(1);
95
96 const bool share_arrays = selection.size() == domain_size;
97 const bool share_position = share_arrays && positions_eval.is_span() &&
98 positions_eval.get_internal_span().data() ==
99 mesh->vert_positions().data();
100
101 PointCloud *pointcloud;
102 if (share_position) {
103 /* Create an empty point cloud so the positions can be shared. */
105 const bke::AttributeReader src = src_attributes.lookup<float3>("position");
106 const bke::AttributeInitShared init(src.varray.get_internal_span().data(), *src.sharing_info);
107 pointcloud->attributes_for_write().add<float3>("position", AttrDomain::Point, init);
108 }
109 else {
110 pointcloud = BKE_pointcloud_new_nomain(PointCloudType::Points, selection.size());
111 array_utils::gather(positions_eval, selection, pointcloud->positions_for_write());
112 }
113
114 MutableAttributeAccessor dst_attributes = pointcloud->attributes_for_write();
115 {
116 const VArray<float> radii = evaluator.get_evaluated<float>(1);
117 if (const std::optional<float> radius = radii.get_if_single()) {
118 dst_attributes.add<float>("radius", AttrDomain::Point, bke::AttributeInitValue(*radius));
119 }
120 else {
121 SpanAttributeWriter attr = dst_attributes.lookup_or_add_for_write_only_span<float>(
122 "radius", AttrDomain::Point);
123 array_utils::gather(radii, selection, attr.span);
124 attr.finish();
125 }
126 }
127
128 src_attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
129 const StringRef src_name = iter.name;
130 if (iter.is_builtin && !dst_attributes.is_builtin(src_name)) {
131 return;
132 }
133 if (ELEM(src_name, "position", "radius", ".select_edge", ".select_poly")) {
134 return;
135 }
136 if (attribute_filter.allow_skip(src_name)) {
137 return;
138 }
139 const bke::AttrType data_type = iter.data_type;
140 const bke::GAttributeReader src = iter.get(domain);
141 if (!src) {
142 /* Domain interpolation can fail if the source domain is empty. */
143 return;
144 }
145 const StringRef dst_name = src_name == ".select_vert" ? ".selection" : src_name;
146 const CommonVArrayInfo info = src.varray.common_info();
148 const CPPType &type = src.varray.type();
149 const bke::AttributeInitValue init(GPointer(type, info.data));
150 dst_attributes.add(dst_name, AttrDomain::Point, data_type, init);
151 return;
152 }
153
154 if (share_arrays && src.domain == domain && src.sharing_info &&
156 {
158 dst_attributes.add(dst_name, AttrDomain::Point, data_type, init);
159 }
160 else {
162 dst_name, AttrDomain::Point, data_type);
163 array_utils::gather(src.varray, selection, dst.span);
164 dst.finish();
165 }
166 });
167
168 geometry_set.replace_pointcloud(pointcloud);
170}
171
173{
174 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh"_ustr);
175 Field<float3> position = params.extract_input<Field<float3>>("Position"_ustr);
176 Field<float> radius = params.extract_input<Field<float>>("Radius"_ustr);
177 Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
178
179 static const auto &max_zero_fn = fn::multi_function::registry::lookup("max(float, float)"_ustr);
180 const Field<float> positive_radius(
181 FieldOperation::from(max_zero_fn, {std::move(radius), fn::Field<float>(0.0f)}), 0);
182
183 const NodeGeometryMeshToPoints &storage = node_storage(params.node());
185
186 const NodeAttributeFilter &attribute_filter = params.get_attribute_filter("Points"_ustr);
187
188 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
189 switch (mode) {
191 geometry_set_mesh_to_points(geometry_set,
192 position,
193 positive_radius,
194 selection,
195 AttrDomain::Point,
196 attribute_filter);
197 break;
199 geometry_set_mesh_to_points(geometry_set,
200 position,
201 positive_radius,
202 selection,
203 AttrDomain::Edge,
204 attribute_filter);
205 break;
207 geometry_set_mesh_to_points(geometry_set,
208 position,
209 positive_radius,
210 selection,
211 AttrDomain::Face,
212 attribute_filter);
213 break;
215 geometry_set_mesh_to_points(geometry_set,
216 position,
217 positive_radius,
218 selection,
219 AttrDomain::Corner,
220 attribute_filter);
221 break;
222 }
223 });
224
225 params.set_output("Points"_ustr, std::move(geometry_set));
226}
227
228static void node_rna(StructRNA *srna)
229{
230 static EnumPropertyItem mode_items[] = {
232 "VERTICES",
233 0,
234 "Vertices",
235 "Create a point in the point cloud for each selected vertex"},
237 "EDGES",
238 0,
239 "Edges",
240 "Create a point in the point cloud for each selected edge"},
242 "FACES",
243 0,
244 "Faces",
245 "Create a point in the point cloud for each selected face"},
247 "CORNERS",
248 0,
249 "Corners",
250 "Create a point in the point cloud for each selected face corner"},
251 {0, nullptr, 0, nullptr, nullptr},
252 };
253
255 "mode",
256 "Mode",
257 "",
258 mode_items,
261 nullptr,
262 true);
263}
264
265static void node_register()
266{
267 static bke::bNodeType ntype;
268
269 geo_node_type_base(&ntype, "GeometryNodeMeshToPoints"_ustr, GEO_NODE_MESH_TO_POINTS);
270 ntype.ui_name = "Mesh to Points";
271 ntype.ui_description = "Generate a point cloud from a mesh's vertices";
272 ntype.enum_name_legacy = "MESH_TO_POINTS";
274 ntype.declare = node_declare;
276 ntype.initfunc = node_init;
279 ntype, "NodeGeometryMeshToPoints", node_free_standard_storage, node_copy_standard_storage);
281
282 node_rna(ntype.rna_ext.srna);
283}
285
286} // namespace blender::nodes::node_geo_mesh_to_points_cc
Generic geometry attributes built on CustomData.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_MESH_TO_POINTS
General operations for point clouds.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
#define ELEM(...)
GPointer()=default
CommonVArrayInfo common_info() const
std::optional< T > get_if_single() const
Span< T > get_internal_span() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
GAttributeReader lookup(const StringRef name) const
bool is_builtin(const StringRef name) const
GAttributeReader get() const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
void set_selection(Field< bool > selection)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_selection_as_mask() const
const GVArray & get_evaluated(const int field_index) const
static FieldOperationPtr from(std::shared_ptr< const mf::MultiFunction > fn, Vector< GField > inputs)
Definition field.cc:252
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
#define storage(slot, qualifiers)
#define position
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
PointCloud * pointcloud_new_no_attributes(PointCloudType type, int totpoint)
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
const MultiFunction & lookup(UString id)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_init(bNodeTree *, bNode *node)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void geometry_set_mesh_to_points(GeometrySet &geometry_set, const Field< float3 > &position_field, const Field< float > &radius_field, const Field< bool > &selection_field, const AttrDomain domain, const AttributeFilter &attribute_filter)
static void node_geo_exec(GeoNodeExecParams 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)
BMesh const char void * data
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GeometryNodeMeshToPointsMode
@ GEO_NODE_MESH_TO_POINTS_FACES
@ GEO_NODE_MESH_TO_POINTS_EDGES
@ GEO_NODE_MESH_TO_POINTS_CORNERS
@ GEO_NODE_MESH_TO_POINTS_VERTICES
#define min(a, b)
Definition sort.cc:38
bool allow_skip(const StringRef name) const
const ImplicitSharingInfo * sharing_info
const ImplicitSharingInfo * sharing_info
void keep_only(Span< GeometryComponent::Type > component_types)
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const Mesh * get_mesh() const
Defines a node type.
Definition BKE_node.hh:265
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
const char * enum_name_legacy
Definition BKE_node.hh:274
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)