Blender V5.3
node_geo_points_to_vertices.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
8
9#include "BKE_mesh.hh"
10
12
13#include "node_geometry_util.hh"
14
16
18{
19 b.add_input<decl::Geometry>("Points"_ustr)
21 .description("Points that are converted to vertices in a mesh");
22 b.add_input<decl::Bool>("Selection"_ustr)
23 .default_value(true)
24 .evaluated_geometry_field()
25 .hide_value();
26 b.add_output<decl::Geometry>("Mesh"_ustr).propagate_all_geometry();
27}
28
29/* One improvement would be to move the attribute arrays directly to the mesh when possible. */
31 Field<bool> &selection_field,
32 const AttributeFilter &attribute_filter)
33{
34 const PointCloud *points = geometry_set.get_pointcloud();
35 if (points == nullptr) {
37 return;
38 }
39 if (points->totpoint == 0) {
41 return;
42 }
43
44 const bke::PointCloudFieldContext field_context{*points};
45 fn::FieldEvaluator selection_evaluator{field_context, points->totpoint};
46 selection_evaluator.add(selection_field);
47 selection_evaluator.evaluate();
48 const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0);
49
50 Mesh *mesh = bke::mesh_new_no_attributes(selection.size(), 0, 0, 0);
51
52 const AttributeAccessor src_attributes = points->attributes();
53 MutableAttributeAccessor dst_attributes = mesh->attributes_for_write();
54
55 src_attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
56 const StringRef src_name = iter.name;
57 if (iter.is_builtin && !dst_attributes.is_builtin(src_name)) {
58 return;
59 }
60 if (attribute_filter.allow_skip(src_name)) {
61 return;
62 }
63 const StringRef dst_name = src_name == ".selection" ? ".select_vert" : src_name;
64 const bke::AttrType data_type = iter.data_type;
65 const GAttributeReader src = iter.get();
66 const CommonVArrayInfo info = src.varray.common_info();
68 const CPPType &type = src.varray.type();
69 const bke::AttributeInitValue init(GPointer(type, info.data));
70 dst_attributes.add(dst_name, AttrDomain::Point, data_type, init);
71 return;
72 }
73
74 if (selection.size() == points->totpoint && src.sharing_info &&
76 {
78 dst_attributes.add(dst_name, AttrDomain::Point, data_type, init);
79 }
80 else {
82 dst_name, AttrDomain::Point, data_type);
83 array_utils::gather(src.varray, selection, dst.span);
84 dst.finish();
85 }
86 });
87
88 mesh->tag_loose_edges_none();
89 mesh->tag_overlapping_none();
90
91 geometry_set.replace_mesh(mesh);
93}
94
96{
97 GeometrySet geometry_set = params.extract_input<GeometrySet>("Points"_ustr);
98 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
99
100 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
102 geometry_set, selection_field, params.get_attribute_filter("Mesh"_ustr));
103 });
104
105 params.set_output("Mesh"_ustr, std::move(geometry_set));
106}
107
108static void node_register()
109{
110 static bke::bNodeType ntype;
111
112 geo_node_type_base(&ntype, "GeometryNodePointsToVertices"_ustr, GEO_NODE_POINTS_TO_VERTICES);
113 ntype.ui_name = "Points to Vertices";
114 ntype.ui_description = "Generate a mesh vertex for each point cloud point";
115 ntype.enum_name_legacy = "POINTS_TO_VERTICES";
117 ntype.declare = node_declare;
120}
122
123} // namespace blender::nodes::node_geo_points_to_vertices_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_POINTS_TO_VERTICES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
GPointer()=default
CommonVArrayInfo common_info() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) 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)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
Mesh * mesh_new_no_attributes(int verts_num, int edges_num, int faces_num, int corners_num)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void geometry_set_points_to_vertices(GeometrySet &geometry_set, Field< bool > &selection_field, const AttributeFilter &attribute_filter)
static void node_declare(NodeDeclarationBuilder &b)
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bool allow_skip(const StringRef name) const
const ImplicitSharingInfo * sharing_info
void keep_only(Span< GeometryComponent::Type > component_types)
const PointCloud * get_pointcloud() const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393