Blender V5.0
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 "BLI_array_utils.hh"
6
7#include "DNA_mesh_types.h"
9
10#include "BKE_attribute_math.hh"
11#include "BKE_customdata.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")
32 .supported_type(GeometryComponent::Type::Mesh)
33 .description("Mesh whose elements are converted to points");
34 b.add_input<decl::Bool>("Selection").default_value(true).field_on_all().hide_value();
35 b.add_input<decl::Vector>("Position").implicit_field_on_all(NODE_DEFAULT_INPUT_POSITION_FIELD);
36 b.add_input<decl::Float>("Radius")
37 .default_value(0.05f)
38 .min(0.0f)
39 .subtype(PROP_DISTANCE)
40 .field_on_all();
41 b.add_output<decl::Geometry>("Points").propagate_all();
42}
43
44static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
45{
46 layout->prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
47}
48
55
56static void geometry_set_mesh_to_points(GeometrySet &geometry_set,
57 const Field<float3> &position_field,
58 const Field<float> &radius_field,
59 const Field<bool> &selection_field,
60 const AttrDomain domain,
61 const AttributeFilter &attribute_filter)
62{
63 const Mesh *mesh = geometry_set.get_mesh();
64 if (mesh == nullptr) {
65 geometry_set.keep_only({GeometryComponent::Type::Edit});
66 return;
67 }
68 const int domain_size = mesh->attributes().domain_size(domain);
69 if (domain_size == 0) {
70 geometry_set.keep_only({GeometryComponent::Type::Edit});
71 return;
72 }
73 const AttributeAccessor src_attributes = mesh->attributes();
74 const bke::MeshFieldContext field_context{*mesh, domain};
75 fn::FieldEvaluator evaluator{field_context, domain_size};
76 evaluator.set_selection(selection_field);
77 /* Evaluating directly into the point cloud doesn't work because we are not using the full
78 * "min_array_size" array but compressing the selected elements into the final array with no
79 * gaps. */
80 evaluator.add(position_field);
81 evaluator.add(radius_field);
82 evaluator.evaluate();
83 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
84 const VArray<float3> positions_eval = evaluator.get_evaluated<float3>(0);
85 const VArray<float> radii_eval = evaluator.get_evaluated<float>(1);
86
87 const bool share_arrays = selection.size() == domain_size;
88 const bool share_position = share_arrays && positions_eval.is_span() &&
89 positions_eval.get_internal_span().data() ==
90 mesh->vert_positions().data();
91
92 PointCloud *pointcloud;
93 if (share_position) {
94 /* Create an empty point cloud so the positions can be shared. */
96 const bke::AttributeReader src = src_attributes.lookup<float3>("position");
97 const bke::AttributeInitShared init(src.varray.get_internal_span().data(), *src.sharing_info);
98 pointcloud->attributes_for_write().add<float3>("position", AttrDomain::Point, init);
99 }
100 else {
101 pointcloud = BKE_pointcloud_new_nomain(selection.size());
102 array_utils::gather(positions_eval, selection, pointcloud->positions_for_write());
103 }
104
105 MutableAttributeAccessor dst_attributes = pointcloud->attributes_for_write();
106 SpanAttributeWriter radius = dst_attributes.lookup_or_add_for_write_only_span<float>(
107 "radius", AttrDomain::Point);
108 array_utils::gather(evaluator.get_evaluated(1), selection, radius.span);
109 radius.finish();
110
112 geometry_set.gather_attributes_for_propagation({GeometryComponent::Type::Mesh},
113 GeometryComponent::Type::PointCloud,
114 false,
115 attribute_filter,
116 attributes);
117
118 for (const int i : attributes.names.index_range()) {
119 if (ELEM(attributes.names[i], "position", "radius")) {
120 continue;
121 }
122 const StringRef attribute_id = attributes.names[i];
123 const bke::AttrType data_type = attributes.kinds[i].data_type;
124 const bke::GAttributeReader src = src_attributes.lookup(attribute_id, domain, data_type);
125 if (!src) {
126 /* Domain interpolation can fail if the source domain is empty. */
127 continue;
128 }
129
130 if (share_arrays && src.domain == domain && src.sharing_info && src.varray.is_span()) {
132 *src.sharing_info);
133 dst_attributes.add(attribute_id, AttrDomain::Point, data_type, init);
134 }
135 else {
137 attribute_id, AttrDomain::Point, data_type);
138 array_utils::gather(src.varray, selection, dst.span);
139 dst.finish();
140 }
141 }
142
143 geometry_set.replace_pointcloud(pointcloud);
144 geometry_set.keep_only({GeometryComponent::Type::PointCloud, GeometryComponent::Type::Edit});
145}
146
148{
149 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
150 Field<float3> position = params.extract_input<Field<float3>>("Position");
151 Field<float> radius = params.extract_input<Field<float>>("Radius");
152 Field<bool> selection = params.extract_input<Field<bool>>("Selection");
153
154 /* Use another multi-function operation to make sure the input radius is greater than zero.
155 * TODO: Use mutable multi-function once that is supported. */
156 static auto max_zero_fn = mf::build::SI1_SO<float, float>(
157 __func__,
158 [](float value) { return std::max(0.0f, value); },
159 mf::build::exec_presets::AllSpanOrSingle());
160 const Field<float> positive_radius(FieldOperation::from(max_zero_fn, {std::move(radius)}), 0);
161
162 const NodeGeometryMeshToPoints &storage = node_storage(params.node());
164
165 const NodeAttributeFilter &attribute_filter = params.get_attribute_filter("Points");
166
167 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
168 switch (mode) {
170 geometry_set_mesh_to_points(geometry_set,
171 position,
172 positive_radius,
173 selection,
174 AttrDomain::Point,
175 attribute_filter);
176 break;
178 geometry_set_mesh_to_points(geometry_set,
179 position,
180 positive_radius,
181 selection,
182 AttrDomain::Edge,
183 attribute_filter);
184 break;
186 geometry_set_mesh_to_points(geometry_set,
187 position,
188 positive_radius,
189 selection,
190 AttrDomain::Face,
191 attribute_filter);
192 break;
194 geometry_set_mesh_to_points(geometry_set,
195 position,
196 positive_radius,
197 selection,
198 AttrDomain::Corner,
199 attribute_filter);
200 break;
201 }
202 });
203
204 params.set_output("Points", std::move(geometry_set));
205}
206
207static void node_rna(StructRNA *srna)
208{
209 static EnumPropertyItem mode_items[] = {
211 "VERTICES",
212 0,
213 "Vertices",
214 "Create a point in the point cloud for each selected vertex"},
216 "EDGES",
217 0,
218 "Edges",
219 "Create a point in the point cloud for each selected edge"},
221 "FACES",
222 0,
223 "Faces",
224 "Create a point in the point cloud for each selected face"},
226 "CORNERS",
227 0,
228 "Corners",
229 "Create a point in the point cloud for each selected face corner"},
230 {0, nullptr, 0, nullptr, nullptr},
231 };
232
234 "mode",
235 "Mode",
236 "",
237 mode_items,
240 nullptr,
241 true);
242}
243
244static void node_register()
245{
246 static blender::bke::bNodeType ntype;
247
248 geo_node_type_base(&ntype, "GeometryNodeMeshToPoints", GEO_NODE_MESH_TO_POINTS);
249 ntype.ui_name = "Mesh to Points";
250 ntype.ui_description = "Generate a point cloud from a mesh's vertices";
251 ntype.enum_name_legacy = "MESH_TO_POINTS";
253 ntype.declare = node_declare;
255 ntype.initfunc = node_init;
258 ntype, "NodeGeometryMeshToPoints", node_free_standard_storage, node_copy_standard_storage);
260
261 node_rna(ntype.rna_ext.srna);
262}
264
265} // namespace blender::nodes::node_geo_mesh_to_points_cc
CustomData interface, see also DNA_customdata_types.h.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_MESH_TO_POINTS
General operations for point clouds.
PointCloud * BKE_pointcloud_new_nomain(int totpoint)
#define ELEM(...)
@ NODE_DEFAULT_INPUT_POSITION_FIELD
GeometryNodeMeshToPointsMode
@ GEO_NODE_MESH_TO_POINTS_FACES
@ GEO_NODE_MESH_TO_POINTS_VERTICES
@ GEO_NODE_MESH_TO_POINTS_CORNERS
@ GEO_NODE_MESH_TO_POINTS_EDGES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
@ PROP_DISTANCE
Definition RNA_types.hh:256
#define UI_ITEM_NONE
BMesh const char void * data
void init()
AttributeSet attributes
const void * data() const
Span< T > get_internal_span() const
GAttributeReader lookup(const StringRef attribute_id) const
bool add(const StringRef attribute_id, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, AttrType data_type)
void set_selection(Field< bool > selection)
Definition FN_field.hh:383
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_selection_as_mask() const
Definition field.cc:817
const GVArray & get_evaluated(const int field_index) const
Definition FN_field.hh:448
static std::shared_ptr< FieldOperation > from(std::shared_ptr< const mf::MultiFunction > function, Vector< GField > inputs={})
Definition FN_field.hh:242
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, int64_t grain_size=4096)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
PointCloud * pointcloud_new_no_attributes(int totpoint)
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:5414
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_init(bNodeTree *, bNode *node)
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)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
static void init(bNodeTree *, bNode *node)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
#define min(a, b)
Definition sort.cc:36
StructRNA * srna
int verts_num
void * storage
const ImplicitSharingInfo * sharing_info
const ImplicitSharingInfo * sharing_info
Vector< AttributeDomainAndType, 16 > kinds
void keep_only(Span< GeometryComponent::Type > component_types)
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void gather_attributes_for_propagation(Span< GeometryComponent::Type > component_types, GeometryComponent::Type dst_component_type, bool include_instances, const AttributeFilter &attribute_filter, GatheredAttributes &r_attributes) const
const Mesh * get_mesh() const
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
i
Definition text_draw.cc:230
PointerRNA * ptr
Definition wm_files.cc:4238