Blender V5.0
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
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")
37 .supported_type({GeometryComponent::Type::PointCloud, GeometryComponent::Type::Mesh})
38 .description("Point cloud or mesh to merge points of");
39 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
40 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
41 b.add_input<decl::Menu>("Mode").static_items(mode_items).optional_label();
42 b.add_input<decl::Float>("Distance").default_value(0.001f).min(0.0f).subtype(PROP_DISTANCE);
43}
44
45static void node_init(bNodeTree * /*tree*/, bNode *node)
46{
47 /* Still used for forward compatibility. */
49}
50
52 const float merge_distance,
53 const Field<bool> &selection_field,
54 const AttributeFilter &attribute_filter)
55{
56 const bke::PointCloudFieldContext context{src_points};
57 FieldEvaluator evaluator{context, src_points.totpoint};
58 evaluator.add(selection_field);
59 evaluator.evaluate();
60
61 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
62 if (selection.is_empty()) {
63 return nullptr;
64 }
65
67 src_points, merge_distance, selection, attribute_filter);
68}
69
70static std::optional<Mesh *> mesh_merge_by_distance_connected(const Mesh &mesh,
71 const float merge_distance,
72 const Field<bool> &selection_field)
73{
74 Array<bool> selection(mesh.verts_num);
75 const bke::MeshFieldContext context{mesh, AttrDomain::Point};
76 FieldEvaluator evaluator{context, mesh.verts_num};
77 evaluator.add_with_destination(selection_field, selection.as_mutable_span());
78 evaluator.evaluate();
79
80 return geometry::mesh_merge_by_distance_connected(mesh, selection, merge_distance, false);
81}
82
83static std::optional<Mesh *> mesh_merge_by_distance_all(const Mesh &mesh,
84 const float merge_distance,
85 const Field<bool> &selection_field)
86{
87 const bke::MeshFieldContext context{mesh, AttrDomain::Point};
88 FieldEvaluator evaluator{context, mesh.verts_num};
89 evaluator.add(selection_field);
90 evaluator.evaluate();
91
92 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
93 if (selection.is_empty()) {
94 return std::nullopt;
95 }
96
97 return geometry::mesh_merge_by_distance_all(mesh, selection, merge_distance);
98}
99
101{
102 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
103 const auto mode = params.get_input<GeometryNodeMergeByDistanceMode>("Mode");
104 const Field<bool> selection = params.extract_input<Field<bool>>("Selection");
105 const float merge_distance = params.extract_input<float>("Distance");
106
107 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
108 if (const PointCloud *pointcloud = geometry_set.get_pointcloud()) {
110 *pointcloud, merge_distance, selection, params.get_attribute_filter("Geometry"));
111 if (result) {
112 geometry_set.replace_pointcloud(result);
113 }
114 }
115 if (const Mesh *mesh = geometry_set.get_mesh()) {
116 std::optional<Mesh *> result;
117 switch (mode) {
119 result = mesh_merge_by_distance_all(*mesh, merge_distance, selection);
120 break;
122 result = mesh_merge_by_distance_connected(*mesh, merge_distance, selection);
123 break;
124 default:
126 }
127 if (result) {
128 geometry_set.replace_mesh(*result);
129 }
130 }
131 });
132
133 params.set_output("Geometry", std::move(geometry_set));
134}
135
136static void node_register()
137{
138 static blender::bke::bNodeType ntype;
139
140 geo_node_type_base(&ntype, "GeometryNodeMergeByDistance", GEO_NODE_MERGE_BY_DISTANCE);
141 ntype.ui_name = "Merge by Distance";
142 ntype.ui_description = "Merge vertices or points within a given distance";
143 ntype.enum_name_legacy = "MERGE_BY_DISTANCE";
145 ntype.initfunc = node_init;
147 "NodeGeometryMergeByDistance",
150 ntype.declare = node_declare;
153}
155
156} // namespace blender::nodes::node_geo_merge_by_distance_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_MERGE_BY_DISTANCE
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
GeometryNodeMergeByDistanceMode
@ GEO_NODE_MERGE_BY_DISTANCE_MODE_ALL
@ GEO_NODE_MERGE_BY_DISTANCE_MODE_CONNECTED
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_DISTANCE
Definition RNA_types.hh:256
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:248
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_as_mask(int field_index)
Definition field.cc:804
int add_with_destination(GField field, GVMutableArray dst)
Definition field.cc:738
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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
std::optional< Mesh * > mesh_merge_by_distance_connected(const Mesh &mesh, Span< bool > selection, float merge_distance, bool only_loose_edges)
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_all(const Mesh &mesh, const IndexMask &selection, float merge_distance)
static std::optional< Mesh * > mesh_merge_by_distance_connected(const Mesh &mesh, const float merge_distance, const Field< bool > &selection_field)
static void node_declare(NodeDeclarationBuilder &b)
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_all(const Mesh &mesh, const float merge_distance, const Field< bool > &selection_field)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
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
int verts_num
void * storage
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: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
NodeDeclareFunction declare
Definition BKE_node.hh:362
#define N_(msgid)