Blender V5.3
join_geometries.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
8
9#include "BLI_array_utils.hh"
10
13
14#include "BKE_instances.hh"
15
16namespace blender::geometry {
17
18using bke::AttributeDomainAndType;
19using bke::GeometryComponent;
20using bke::GeometrySet;
21
23 const Span<const GeometryComponent *> components, const Span<StringRef> ignored_attributes)
24{
26
27 for (const GeometryComponent *component : components) {
28 component->attributes()->foreach_attribute([&](const bke::AttributeIter &iter) {
29 if (ignored_attributes.contains(iter.name)) {
30 return;
31 }
32 if (iter.data_type == bke::AttrType::String) {
33 return;
34 }
35 info.add(iter.name, AttributeDomainAndType{iter.domain, iter.data_type});
36 });
37 }
38
39 return info;
40}
41
42static void fill_new_attribute(const Span<const GeometryComponent *> src_components,
43 const StringRef name,
44 const bke::AttrType data_type,
45 const bke::AttrDomain domain,
46 GMutableSpan dst_span)
47{
48 const CPPType &cpp_type = bke::attribute_type_to_cpp_type(data_type);
49
50 int offset = 0;
51 for (const GeometryComponent *component : src_components) {
52 const int domain_num = component->attribute_domain_size(domain);
53 if (domain_num == 0) {
54 continue;
55 }
56 GVArray read_attribute = *component->attributes()->lookup_or_default(
57 name, domain, data_type, nullptr);
58
59 GVArraySpan src_span{read_attribute};
60 const void *src_buffer = src_span.data();
61 void *dst_buffer = dst_span[offset];
62 cpp_type.copy_construct_n(src_buffer, dst_buffer, domain_num);
63
64 offset += domain_num;
65 }
66}
67
69 const StringRef name,
70 const bke::AttrDomain domain,
71 const bke::AttrType data_type,
72 bke::MutableAttributeAccessor dst_attributes)
73{
74 const auto get_single_value = [&](const GeometryComponent &component) {
75 const bke::AttributeAccessor attributes = *component.attributes();
76 const GVArray src = *attributes.lookup_or_default(name, domain, data_type);
77 const CommonVArrayInfo info = src.common_info();
79 return GPointer();
80 }
81 return GPointer(src.type(), info.data);
82 };
83 const GPointer first_value = get_single_value(*src_components.first());
84 if (!first_value) {
85 return false;
86 }
87 const bool all_equal = threading::parallel_reduce(
88 src_components.index_range().drop_front(1),
89 64,
90 true,
91 [&](const IndexRange range, bool value) {
92 if (!value) {
93 return false;
94 }
95 for (const int i : range) {
96 const GPointer value = get_single_value(*src_components[i]);
97 if (!value) {
98 return false;
99 }
100 if (!value.type()->is_equal(value.get(), first_value.get())) {
101 return false;
102 }
103 }
104 return true;
105 },
106 std::logical_and<bool>());
107 if (!all_equal) {
108 return false;
109 }
110 return dst_attributes.add(name, domain, data_type, bke::AttributeInitValue(first_value));
111}
112
113static void join_attributes(const Span<const GeometryComponent *> src_components,
115 const Span<StringRef> ignored_attributes)
116{
118 ignored_attributes);
119 bke::MutableAttributeAccessor dst_attributes = *result.attributes_for_write();
120
121 for (const int i : info.names.index_range()) {
122 const StringRef name = info.names[i];
123 const AttributeDomainAndType &meta_data = info.kinds[i];
124
126 src_components, name, meta_data.domain, meta_data.data_type, dst_attributes))
127 {
128 continue;
129 }
130
131 bke::GSpanAttributeWriter write_attribute = dst_attributes.lookup_or_add_for_write_only_span(
132 name, meta_data.domain, meta_data.data_type);
133 if (!write_attribute) {
134 continue;
135 }
137 src_components, name, meta_data.data_type, meta_data.domain, write_attribute.span);
138 write_attribute.finish();
139 }
140}
141
142static void join_instances(const Span<const GeometryComponent *> src_components,
143 const bool allow_merging_instance_references,
145{
146 Array<int> offsets_data(src_components.size() + 1);
147 for (const int i : src_components.index_range()) {
148 const auto &src_component = static_cast<const bke::InstancesComponent &>(*src_components[i]);
149 offsets_data[i] = src_component.get()->instances_num();
150 }
152
153 auto dst_instances = std::make_unique<bke::Instances>(offsets.total_size());
154
155 MutableSpan<int> all_handles = dst_instances->reference_handles_for_write();
156
157 Map<std::reference_wrapper<const bke::InstanceReference>, int> new_handle_by_src_reference_cache;
158
159 for (const int i : src_components.index_range()) {
160 const auto &src_component = static_cast<const bke::InstancesComponent &>(*src_components[i]);
161 const bke::Instances &src_instances = *src_component.get();
162
163 const Span<bke::InstanceReference> src_references = src_instances.references();
164 Array<int> handle_map(src_references.size());
165 for (const int src_handle : src_references.index_range()) {
166 const bke::InstanceReference &src_reference = src_references[src_handle];
167 if (allow_merging_instance_references) {
168 handle_map[src_handle] = new_handle_by_src_reference_cache.lookup_or_add_cb(
169 src_reference, [&]() { return dst_instances->add_new_reference(src_reference); });
170 }
171 else {
172 handle_map[src_handle] = dst_instances->add_new_reference(src_reference);
173 }
174 }
175
176 const IndexRange dst_range = offsets[i];
177
178 const Span<int> src_handles = src_instances.reference_handles();
179 array_utils::gather(handle_map.as_span(), src_handles, all_handles.slice(dst_range));
180 }
181
182 result.replace_instances(dst_instances.release());
183 auto &dst_component = result.get_component_for_write<bke::InstancesComponent>();
184 join_attributes(src_components, dst_component, {".reference_index"});
185}
186
187static void join_volumes(const Span<const GeometryComponent *> /*src_components*/,
188 GeometrySet & /*result*/)
189{
190 /* Not yet supported. Joining volume grids with the same name requires resampling of at least one
191 * of the grids. The cell size of the resulting volume has to be determined somehow. */
192}
193
194static void join_component_type(const bke::GeometryComponent::Type component_type,
195 const Span<GeometrySet> src_geometry_sets,
196 const bke::AttributeFilter &attribute_filter,
197 const bool allow_merging_instance_references,
199{
201 for (const GeometrySet &geometry_set : src_geometry_sets) {
202 const GeometryComponent *component = geometry_set.get_component(component_type);
203 if (component != nullptr && !component->is_empty()) {
204 components.append(component);
205 }
206 }
207
208 if (components.is_empty()) {
209 return;
210 }
211 if (components.size() == 1) {
212 result.add(*components.first());
213 return;
214 }
215
216 switch (component_type) {
217 case bke::GeometryComponent::Type::Instance:
218 join_instances(components, allow_merging_instance_references, result);
219 return;
220 case bke::GeometryComponent::Type::Volume:
221 join_volumes(components, result);
222 return;
223 default:
224 break;
225 }
226
227 auto instances = std::make_unique<bke::Instances>(components.size());
228 instances->transforms_for_write().fill(float4x4::identity());
229 MutableSpan<int> handles = instances->reference_handles_for_write();
230 Map<const GeometryComponent *, int> handle_by_component;
231 for (const int i : components.index_range()) {
232 const GeometryComponent *component = components[i];
233 handles[i] = handle_by_component.lookup_or_add_cb(component, [&]() {
234 GeometrySet tmp_geo;
235 tmp_geo.add(*components[i]);
236 return instances->add_new_reference(bke::InstanceReference{tmp_geo});
237 });
238 }
239
241 options.keep_original_ids = true;
242 options.realize_instance_attributes = false;
243 options.attribute_filter = attribute_filter;
244 GeometrySet joined_components =
246 result.add(joined_components.get_component_for_write(component_type));
247}
248
250 const Span<GeometrySet> geometries,
251 const bke::AttributeFilter &attribute_filter,
252 const std::optional<Span<GeometryComponent::Type>> &component_types_to_join,
253 const bool allow_merging_instance_references)
254{
256 result.set_name(geometries.is_empty() ? "" : geometries[0].name());
257 for (const GeometrySet &geometry_set : geometries) {
258 result.merge_bundle_from(geometry_set);
259 }
260 static const Array<GeometryComponent::Type> supported_types(
261 {GeometryComponent::Type::Mesh,
262 GeometryComponent::Type::PointCloud,
263 GeometryComponent::Type::Instance,
264 GeometryComponent::Type::Volume,
265 GeometryComponent::Type::Curve,
266 GeometryComponent::Type::GreasePencil,
267 GeometryComponent::Type::Edit});
268
269 const Span<GeometryComponent::Type> types_to_join = component_types_to_join.has_value() ?
270 *component_types_to_join :
272 supported_types);
273
274 for (const GeometryComponent::Type type : types_to_join) {
276 type, geometries, attribute_filter, allow_merging_instance_references, result);
277 }
278
279 return result;
280}
281
282} // namespace blender::geometry
Span< T > as_span() const
Definition BLI_array.hh:254
void copy_construct_n(const void *src, void *dst, int64_t n) const
bool is_equal(const void *a, const void *b) const
const CPPType * type() const
GPointer()=default
const void * get() const
const void * data() const
CommonVArrayInfo common_info() const
constexpr IndexRange drop_front(int64_t n) const
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr bool contains(const T &value) const
Definition BLI_span.hh:281
int64_t size() const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
const T & first() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
virtual bool is_empty() const
Span< int > reference_handles() const
Definition instances.cc:222
Span< InstanceReference > references() const
Definition instances.cc:290
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
CCL_NAMESPACE_BEGIN struct Options options
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
const CPPType & attribute_type_to_cpp_type(AttrType type)
static void join_instances(const Span< const GeometryComponent * > src_components, const bool allow_merging_instance_references, GeometrySet &result)
bke::GeometrySet join_geometries(Span< bke::GeometrySet > geometries, const bke::AttributeFilter &attribute_filter, const std::optional< Span< bke::GeometryComponent::Type > > &component_types_to_join=std::nullopt, bool allow_merging_instance_references=true)
static bool try_join_single_value_attribute(const Span< const GeometryComponent * > src_components, const StringRef name, const bke::AttrDomain domain, const bke::AttrType data_type, bke::MutableAttributeAccessor dst_attributes)
static void join_volumes(const Span< const GeometryComponent * >, GeometrySet &)
static GeometrySet::GatheredAttributes get_final_attribute_info(const Span< const GeometryComponent * > components, const Span< StringRef > ignored_attributes)
RealizeInstancesResult realize_instances(bke::GeometrySet geometry_set, const RealizeInstancesOptions &options)
static void join_component_type(const bke::GeometryComponent::Type component_type, const Span< GeometrySet > src_geometry_sets, const bke::AttributeFilter &attribute_filter, const bool allow_merging_instance_references, GeometrySet &result)
static void fill_new_attribute(const Span< const GeometryComponent * > src_components, const StringRef name, const bke::AttrType data_type, const bke::AttrDomain domain, GMutableSpan dst_span)
static void join_attributes(const Span< const GeometryComponent * > src_components, GeometryComponent &result, const Span< StringRef > ignored_attributes)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
void add(const GeometryComponent &component)
void add(const StringRef name, const AttributeDomainAndType &kind)
static GeometrySet from_instances(std::unique_ptr< Instances > instances)