Blender V5.3
point_merge.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_array_utils.hh"
10#include "BLI_kdtree_new.hh"
11#include "BLI_offset_indices.hh"
12#include "BLI_task.hh"
13#include "BLI_vector_set.hh"
14
16
17#include "BKE_attribute_math.hh"
18#include "BKE_pointcloud.hh"
19
20#include "GEO_point_merge.hh"
21#include "GEO_randomize.hh"
22
23namespace blender::geometry {
24
26 const IndexMask &selection,
27 const Span<int> merge_ids,
28 const bke::AttributeFilter &attribute_filter)
29{
30 Array<int> point_groups(selection.size());
31 const int groups_num = array_utils::group_ids_to_indices(merge_ids, selection, point_groups);
32
33 Array<int> group_offset_data;
34 Array<int> all_group_indices;
36 point_groups, groups_num, group_offset_data, all_group_indices, selection);
37
38 IndexMaskMemory memory;
39 const IndexMask unselected = selection.complement(IndexMask(src_points.totpoint), memory);
40
41 PointCloud *dst_pointcloud = BKE_pointcloud_new_nomain(src_points.type,
42 unselected.size() + groups_num);
43 bke::MutableAttributeAccessor dst_attributes = dst_pointcloud->attributes_for_write();
44
45 src_points.attributes().foreach_attribute([&](const bke::AttributeIter &iter) {
46 if (attribute_filter.allow_skip(iter.name)) {
47 return;
48 }
49 bke::GAttributeReader src = iter.get();
50 const CommonVArrayInfo info = src.varray.common_info();
53 if (dst_attributes.add(iter.name, bke::AttrDomain::Point, iter.data_type, init)) {
54 return;
55 }
56 }
57 const GVArraySpan src_span(*src);
60 array_utils::gather(src_span, unselected, dst_attribute.span.take_front(unselected.size()));
61 if (iter.name == "id" && iter.data_type == bke::AttrType::Int32) {
62 const Span<int> src_typed = src_span.typed<int>();
63 MutableSpan<int> dst_typed = dst_attribute.span.typed<int>().take_back(groups_num);
64 threading::parallel_for(dst_typed.index_range(), 4096, [&](const IndexRange range) {
65 for (const int i : range) {
66 dst_typed[i] = src_typed[indices_by_group[i].first()];
67 }
68 });
69 }
70 else {
72 src_span, indices_by_group, dst_attribute.span.take_back(groups_num));
73 }
74 dst_attribute.finish();
75 });
76
77 debug_randomize_point_order(dst_pointcloud);
78
79 return dst_pointcloud;
80}
81
83 const float merge_distance,
84 const IndexMask &selection,
85 const bke::AttributeFilter &attribute_filter)
86{
87 KDTreeNew<float3> tree(src_points.positions(), selection);
88 Array<int> root_indices(src_points.totpoint, -1);
89 kdtree::calc_duplicates(tree, merge_distance, root_indices);
90 threading::parallel_for(root_indices.index_range(), 1024, [&](const IndexRange range) {
91 for (const int i : range) {
92 if (root_indices[i] == -1) {
93 root_indices[i] = i;
94 }
95 }
96 });
97 return merge_points(src_points, selection, root_indices.as_span(), attribute_filter);
98}
99
100} // namespace blender::geometry
General operations for point clouds.
IndexRange index_range() const
Definition BLI_array.hh:371
GMutableSpan take_front(const int64_t n) const
MutableSpan< T > typed() const
GPointer()=default
Span< T > typed() const
CommonVArrayInfo common_info() 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)
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
int group_ids_to_indices(Span< int > ids, const IndexMask &mask, MutableSpan< int > r_group_indices, Vector< int > *r_first_indices=nullptr)
void mix_groups(GSpan src, OffsetIndices< int > groups, Span< int > all_indices, std::optional< Span< float > > all_weights, GMutableSpan dst)
PointCloud * point_merge_by_distance(const PointCloud &src_points, const float merge_distance, const IndexMask &selection, const bke::AttributeFilter &attribute_filter)
PointCloud * merge_points(const PointCloud &src_points, const IndexMask &selection, Span< int > merge_ids, const bke::AttributeFilter &attribute_filter)
void debug_randomize_point_order(PointCloud *pointcloud)
Definition randomize.cc:258
int calc_duplicates(const KDTreeNew< T > &tree, typename T::base_type range, Span< int > visit_order, MutableSpan< int > duplicates)
Definition kdtree.cc:867
GroupedSpan< int > build_groups_from_indices(Span< int > indices, int groups_num, Array< int > &offset_data, Array< int > &index_data)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
bool allow_skip(const StringRef name) const