Blender V4.5
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
11#include "NOD_rna_define.hh"
12
13#include "UI_interface.hh"
14#include "UI_resources.hh"
15
16#include "node_geometry_util.hh"
17
19
21
23{
24 b.use_custom_socket_order();
25 b.allow_any_socket_order();
26 b.add_default_layout();
27 b.add_input<decl::Geometry>("Geometry")
28 .supported_type({GeometryComponent::Type::PointCloud, GeometryComponent::Type::Mesh});
29 b.add_output<decl::Geometry>("Geometry").propagate_all().align_with_previous();
30 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
31 b.add_input<decl::Float>("Distance").default_value(0.001f).min(0.0f).subtype(PROP_DISTANCE);
32}
33
34static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
35{
36 uiLayoutSetPropSep(layout, true);
37 uiLayoutSetPropDecorate(layout, false);
38 layout->prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
39}
40
47
49 const float merge_distance,
50 const Field<bool> &selection_field,
51 const AttributeFilter &attribute_filter)
52{
53 const bke::PointCloudFieldContext context{src_points};
54 FieldEvaluator evaluator{context, src_points.totpoint};
55 evaluator.add(selection_field);
56 evaluator.evaluate();
57
58 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
59 if (selection.is_empty()) {
60 return nullptr;
61 }
62
64 src_points, merge_distance, selection, attribute_filter);
65}
66
67static std::optional<Mesh *> mesh_merge_by_distance_connected(const Mesh &mesh,
68 const float merge_distance,
69 const Field<bool> &selection_field)
70{
71 Array<bool> selection(mesh.verts_num);
72 const bke::MeshFieldContext context{mesh, AttrDomain::Point};
73 FieldEvaluator evaluator{context, mesh.verts_num};
74 evaluator.add_with_destination(selection_field, selection.as_mutable_span());
75 evaluator.evaluate();
76
77 return geometry::mesh_merge_by_distance_connected(mesh, selection, merge_distance, false);
78}
79
80static std::optional<Mesh *> mesh_merge_by_distance_all(const Mesh &mesh,
81 const float merge_distance,
82 const Field<bool> &selection_field)
83{
84 const bke::MeshFieldContext context{mesh, AttrDomain::Point};
85 FieldEvaluator evaluator{context, mesh.verts_num};
86 evaluator.add(selection_field);
87 evaluator.evaluate();
88
89 const IndexMask selection = evaluator.get_evaluated_as_mask(0);
90 if (selection.is_empty()) {
91 return std::nullopt;
92 }
93
94 return geometry::mesh_merge_by_distance_all(mesh, selection, merge_distance);
95}
96
98{
99 const NodeGeometryMergeByDistance &storage = node_storage(params.node());
101
102 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
103
104 const Field<bool> selection = params.extract_input<Field<bool>>("Selection");
105 const float merge_distance = params.extract_input<float>("Distance");
106
107 geometry_set.modify_geometry_sets([&](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_rna(StructRNA *srna)
137{
138 static EnumPropertyItem mode_items[] = {
140 "ALL",
141 0,
142 "All",
143 "Merge all close selected points, whether or not they are connected"},
145 "CONNECTED",
146 0,
147 "Connected",
148 "Only merge mesh vertices along existing edges. This method can be much faster"},
149 {0, nullptr, 0, nullptr, nullptr},
150 };
151
153 "mode",
154 "Mode",
155 "",
156 mode_items,
159}
160
161static void node_register()
162{
163 static blender::bke::bNodeType ntype;
164
165 geo_node_type_base(&ntype, "GeometryNodeMergeByDistance", GEO_NODE_MERGE_BY_DISTANCE);
166 ntype.ui_name = "Merge by Distance";
167 ntype.ui_description = "Merge vertices or points within a given distance";
168 ntype.enum_name_legacy = "MERGE_BY_DISTANCE";
170 ntype.initfunc = node_init;
172 "NodeGeometryMergeByDistance",
175 ntype.declare = node_declare;
179
180 node_rna(ntype.rna_ext.srna);
181}
183
184} // namespace blender::nodes::node_geo_merge_by_distance_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#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)
#define NOD_storage_enum_accessors(member)
@ PROP_DISTANCE
Definition RNA_types.hh:244
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
BMesh const char void * data
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:237
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:2748
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
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)
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 void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
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)
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
#define min(a, b)
Definition sort.cc:36
StructRNA * srna
Definition RNA_types.hh:909
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 modify_geometry_sets(ForeachSubGeometryCallback callback)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
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:4226