Blender V5.3
node_geo_merge_by_distance.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 "DNA_mesh_types.h"
7
10#include "GEO_point_merge.hh"
11
12#include "node_geometry_util.hh"
13
15
17
20 "ALL",
21 0,
22 N_("All"),
23 N_("Merge all close selected points, whether or not they are connected")},
25 "CONNECTED",
26 0,
27 N_("Connected"),
28 N_("Only merge mesh vertices along existing edges. This method can be much faster")},
29 {0, nullptr, 0, nullptr, nullptr},
30};
31
33{
34 b.use_custom_socket_order();
35 b.allow_any_socket_order();
36 b.add_input<decl::Geometry>("Geometry"_ustr)
38 .description("Point cloud or mesh to merge points of");
39 b.add_output<decl::Geometry>("Geometry"_ustr).propagate_all_geometry().align_with_previous();
40 b.add_input<decl::Bool>("Selection"_ustr)
41 .default_value(true)
42 .hide_value()
43 .evaluated_geometry_field();
44 b.add_input<decl::Menu>("Mode"_ustr).static_items(mode_items).optional_label();
45 b.add_input<decl::Float>("Distance"_ustr).default_value(0.001f).min(0.0f).subtype(PROP_DISTANCE);
46}
47
48static void node_init(bNodeTree * /*tree*/, bNode *node)
49{
50 /* Still used for forward compatibility. */
51 node->storage = MEM_new<NodeGeometryMergeByDistance>(__func__);
52}
53
55 const float merge_distance,
56 const Field<bool> &selection_field,
57 const AttributeFilter &attribute_filter)
58{
59 const bke::PointCloudFieldContext context{src_points};
60 FieldEvaluator evaluator{context, src_points.totpoint};
61 evaluator.add(selection_field);
62 evaluator.evaluate();
63
64 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
65 if (selection.is_empty()) {
66 return nullptr;
67 }
68
70 src_points, merge_distance, selection, attribute_filter);
71}
72
73static std::optional<Mesh *> mesh_merge_by_distance_connected(
74 const Mesh &mesh,
75 const float merge_distance,
76 const Field<bool> &selection_field,
77 const AttributeFilter &attribute_filter)
78{
79 Array<bool> selection(mesh.verts_num);
80 const bke::MeshFieldContext context{mesh, AttrDomain::Point};
81 FieldEvaluator evaluator{context, mesh.verts_num};
82 evaluator.add_with_destination(selection_field, selection.as_mutable_span());
83 evaluator.evaluate();
84
86 mesh, selection, merge_distance, false, attribute_filter);
87}
88
89static std::optional<Mesh *> mesh_merge_by_distance_all(const Mesh &mesh,
90 const float merge_distance,
91 const Field<bool> &selection_field,
92 const AttributeFilter &attribute_filter)
93{
94 const bke::MeshFieldContext context{mesh, AttrDomain::Point};
95 FieldEvaluator evaluator{context, mesh.verts_num};
96 evaluator.add(selection_field);
97 evaluator.evaluate();
98
99 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
100 if (selection.is_empty()) {
101 return std::nullopt;
102 }
103
104 return geometry::mesh_merge_by_distance_all(mesh, selection, merge_distance, attribute_filter);
105}
106
108{
109 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
110 const auto mode = params.get_input<GeometryNodeMergeByDistanceMode>("Mode"_ustr);
111 const Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
112 const float merge_distance = params.extract_input<float>("Distance"_ustr);
113 const AttributeFilter &attribute_filter = params.get_attribute_filter("Geometry"_ustr);
114
115 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
116 if (const PointCloud *pointcloud = geometry_set.get_pointcloud()) {
119 *pointcloud, merge_distance, selection, attribute_filter);
120 if (result) {
121 geometry_set.replace_pointcloud(result);
122 }
123 }
124 }
125 if (const Mesh *mesh = geometry_set.get_mesh()) {
126 std::optional<Mesh *> result;
127 switch (mode) {
129 result = mesh_merge_by_distance_all(*mesh, merge_distance, selection, attribute_filter);
130 break;
133 *mesh, merge_distance, selection, attribute_filter);
134 break;
135 default:
137 }
138 if (result) {
139 geometry_set.replace_mesh(*result);
140 }
141 }
142 });
143
144 params.set_output("Geometry"_ustr, std::move(geometry_set));
145}
146
147static void node_register()
148{
149 static bke::bNodeType ntype;
150
151 geo_node_type_base(&ntype, "GeometryNodeMergeByDistance"_ustr, GEO_NODE_MERGE_BY_DISTANCE);
152 ntype.ui_name = "Merge by Distance";
153 ntype.ui_description = "Merge vertices or points within a given distance";
154 ntype.enum_name_legacy = "MERGE_BY_DISTANCE";
156 ntype.initfunc = node_init;
158 "NodeGeometryMergeByDistance",
161 ntype.declare = node_declare;
164}
166
167} // namespace blender::nodes::node_geo_merge_by_distance_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_MERGE_BY_DISTANCE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
int add_with_destination(GField field, GVMutableArray dst)
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
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
PointCloud * point_merge_by_distance(const PointCloud &src_points, const float merge_distance, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
std::optional< Mesh * > mesh_merge_by_distance_connected(const Mesh &mesh, Span< bool > selection, float merge_distance, bool only_loose_edges, const bke::AttributeFilter &attribute_filter=bke::AttributeFilter::default_filter())
std::optional< Mesh * > mesh_merge_by_distance_all(const Mesh &mesh, const IndexMask &selection, float merge_distance, const bke::AttributeFilter &attribute_filter=bke::AttributeFilter::default_filter())
static void node_declare(NodeDeclarationBuilder &b)
static std::optional< Mesh * > mesh_merge_by_distance_all(const Mesh &mesh, const float merge_distance, const Field< bool > &selection_field, const AttributeFilter &attribute_filter)
static PointCloud * pointcloud_merge_by_distance(const PointCloud &src_points, const float merge_distance, const Field< bool > &selection_field, const AttributeFilter &attribute_filter)
static void node_geo_exec(GeoNodeExecParams params)
static std::optional< Mesh * > mesh_merge_by_distance_connected(const Mesh &mesh, const float merge_distance, const Field< bool > &selection_field, const AttributeFilter &attribute_filter)
GeometryNodeMergeByDistanceMode
@ GEO_NODE_MERGE_BY_DISTANCE_MODE_ALL
@ GEO_NODE_MERGE_BY_DISTANCE_MODE_CONNECTED
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
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const PointCloud * get_pointcloud() const
const Mesh * get_mesh() 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
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)