Blender V4.5
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
16#include "UI_interface.hh"
17#include "UI_resources.hh"
18
20
21#include "node_geometry_util.hh"
22
24
26
28{
29 b.add_input<decl::Geometry>("Mesh").supported_type(GeometryComponent::Type::Mesh);
30 b.add_input<decl::Bool>("Selection").default_value(true).field_on_all().hide_value();
31 b.add_input<decl::Vector>("Position").implicit_field_on_all(NODE_DEFAULT_INPUT_POSITION_FIELD);
32 b.add_input<decl::Float>("Radius")
33 .default_value(0.05f)
34 .min(0.0f)
35 .subtype(PROP_DISTANCE)
36 .field_on_all();
37 b.add_output<decl::Geometry>("Points").propagate_all();
38}
39
40static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
41{
42 layout->prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
43}
44
51
52static void geometry_set_mesh_to_points(GeometrySet &geometry_set,
53 const Field<float3> &position_field,
54 const Field<float> &radius_field,
55 const Field<bool> &selection_field,
56 const AttrDomain domain,
57 const AttributeFilter &attribute_filter)
58{
59 const Mesh *mesh = geometry_set.get_mesh();
60 if (mesh == nullptr) {
61 geometry_set.remove_geometry_during_modify();
62 return;
63 }
64 const int domain_size = mesh->attributes().domain_size(domain);
65 if (domain_size == 0) {
66 geometry_set.remove_geometry_during_modify();
67 return;
68 }
69 const AttributeAccessor src_attributes = mesh->attributes();
70 const bke::MeshFieldContext field_context{*mesh, domain};
71 fn::FieldEvaluator evaluator{field_context, domain_size};
72 evaluator.set_selection(selection_field);
73 /* Evaluating directly into the point cloud doesn't work because we are not using the full
74 * "min_array_size" array but compressing the selected elements into the final array with no
75 * gaps. */
76 evaluator.add(position_field);
77 evaluator.add(radius_field);
78 evaluator.evaluate();
79 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
80 const VArray<float3> positions_eval = evaluator.get_evaluated<float3>(0);
81 const VArray<float> radii_eval = evaluator.get_evaluated<float>(1);
82
83 const bool share_arrays = selection.size() == domain_size;
84 const bool share_position = share_arrays && positions_eval.is_span() &&
85 positions_eval.get_internal_span().data() ==
86 mesh->vert_positions().data();
87
88 PointCloud *pointcloud;
89 if (share_position) {
90 /* Create an empty point cloud so the positions can be shared. */
92 const bke::AttributeReader src = src_attributes.lookup<float3>("position");
93 const bke::AttributeInitShared init(src.varray.get_internal_span().data(), *src.sharing_info);
94 pointcloud->attributes_for_write().add<float3>("position", AttrDomain::Point, init);
95 }
96 else {
97 pointcloud = BKE_pointcloud_new_nomain(selection.size());
98 array_utils::gather(positions_eval, selection, pointcloud->positions_for_write());
99 }
100
101 MutableAttributeAccessor dst_attributes = pointcloud->attributes_for_write();
103 "radius", AttrDomain::Point, CD_PROP_FLOAT);
104 array_utils::gather(evaluator.get_evaluated(1), selection, radius.span);
105 radius.finish();
106
108 geometry_set.gather_attributes_for_propagation({GeometryComponent::Type::Mesh},
109 GeometryComponent::Type::PointCloud,
110 false,
111 attribute_filter,
112 attributes);
113 attributes.remove("radius");
114 attributes.remove("position");
115
116 for (MapItem<StringRef, AttributeDomainAndType> entry : attributes.items()) {
117 const StringRef attribute_id = entry.key;
118 const eCustomDataType data_type = entry.value.data_type;
119 const bke::GAttributeReader src = src_attributes.lookup(attribute_id, domain, data_type);
120 if (!src) {
121 /* Domain interpolation can fail if the source domain is empty. */
122 continue;
123 }
124
125 if (share_arrays && src.domain == domain && src.sharing_info && src.varray.is_span()) {
127 *src.sharing_info);
128 dst_attributes.add(attribute_id, AttrDomain::Point, data_type, init);
129 }
130 else {
132 attribute_id, AttrDomain::Point, data_type);
133 array_utils::gather(src.varray, selection, dst.span);
134 dst.finish();
135 }
136 }
137
138 geometry_set.replace_pointcloud(pointcloud);
139 geometry_set.keep_only_during_modify({GeometryComponent::Type::PointCloud});
140}
141
143{
144 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
145 Field<float3> position = params.extract_input<Field<float3>>("Position");
146 Field<float> radius = params.extract_input<Field<float>>("Radius");
147 Field<bool> selection = params.extract_input<Field<bool>>("Selection");
148
149 /* Use another multi-function operation to make sure the input radius is greater than zero.
150 * TODO: Use mutable multi-function once that is supported. */
151 static auto max_zero_fn = mf::build::SI1_SO<float, float>(
152 __func__,
153 [](float value) { return std::max(0.0f, value); },
154 mf::build::exec_presets::AllSpanOrSingle());
155 const Field<float> positive_radius(FieldOperation::Create(max_zero_fn, {std::move(radius)}), 0);
156
157 const NodeGeometryMeshToPoints &storage = node_storage(params.node());
159
160 const NodeAttributeFilter &attribute_filter = params.get_attribute_filter("Points");
161
162 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
163 switch (mode) {
165 geometry_set_mesh_to_points(geometry_set,
166 position,
167 positive_radius,
168 selection,
169 AttrDomain::Point,
170 attribute_filter);
171 break;
173 geometry_set_mesh_to_points(geometry_set,
174 position,
175 positive_radius,
176 selection,
177 AttrDomain::Edge,
178 attribute_filter);
179 break;
181 geometry_set_mesh_to_points(geometry_set,
182 position,
183 positive_radius,
184 selection,
185 AttrDomain::Face,
186 attribute_filter);
187 break;
189 geometry_set_mesh_to_points(geometry_set,
190 position,
191 positive_radius,
192 selection,
193 AttrDomain::Corner,
194 attribute_filter);
195 break;
196 }
197 });
198
199 params.set_output("Points", std::move(geometry_set));
200}
201
202static void node_rna(StructRNA *srna)
203{
204 static EnumPropertyItem mode_items[] = {
206 "VERTICES",
207 0,
208 "Vertices",
209 "Create a point in the point cloud for each selected vertex"},
211 "EDGES",
212 0,
213 "Edges",
214 "Create a point in the point cloud for each selected edge"},
216 "FACES",
217 0,
218 "Faces",
219 "Create a point in the point cloud for each selected face"},
221 "CORNERS",
222 0,
223 "Corners",
224 "Create a point in the point cloud for each selected face corner"},
225 {0, nullptr, 0, nullptr, nullptr},
226 };
227
229 "mode",
230 "Mode",
231 "",
232 mode_items,
235 nullptr,
236 true);
237}
238
239static void node_register()
240{
241 static blender::bke::bNodeType ntype;
242
243 geo_node_type_base(&ntype, "GeometryNodeMeshToPoints", GEO_NODE_MESH_TO_POINTS);
244 ntype.ui_name = "Mesh to Points";
245 ntype.ui_description = "Generate a point cloud from a mesh's vertices";
246 ntype.enum_name_legacy = "MESH_TO_POINTS";
248 ntype.declare = node_declare;
250 ntype.initfunc = node_init;
253 ntype, "NodeGeometryMeshToPoints", node_free_standard_storage, node_copy_standard_storage);
255
256 node_rna(ntype.rna_ext.srna);
257}
259
260} // 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:1215
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_MESH_TO_POINTS
General operations for point clouds.
PointCloud * BKE_pointcloud_new_nomain(int totpoint)
@ CD_PROP_FLOAT
@ 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:244
#define UI_ITEM_NONE
BMesh const char void * data
void init()
AttributeSet attributes
const void * data() const
bool remove(const Key &key)
Definition BLI_map.hh:368
ItemIterator items() const &
Definition BLI_map.hh:902
Span< T > get_internal_span() const
GAttributeReader lookup(const StringRef attribute_id) const
bool add(const StringRef attribute_id, const AttrDomain domain, const eCustomDataType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, eCustomDataType 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 > Create(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:2748
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:5603
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
Definition RNA_types.hh:909
int verts_num
void * storage
const ImplicitSharingInfo * sharing_info
const ImplicitSharingInfo * sharing_info
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void keep_only_during_modify(Span< GeometryComponent::Type > component_types)
void gather_attributes_for_propagation(Span< GeometryComponent::Type > component_types, GeometryComponent::Type dst_component_type, bool include_instances, const AttributeFilter &attribute_filter, Map< StringRef, AttributeDomainAndType > &r_attributes) const
const Mesh * get_mesh() const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
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)
PointerRNA * ptr
Definition wm_files.cc:4227