Blender V5.3
mix_geometries.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BKE_attribute.hh"
12#include "BKE_attribute_math.hh"
13#include "BKE_curves.hh"
14#include "BKE_instances.hh"
15
16#include "DNA_mesh_types.h"
18
21
22namespace blender::geometry {
23
25{
26 if (!a || !b) {
27 return false;
28 }
29 return a == b;
30}
31
32template<typename T>
34 const VArray<T> &b,
35 const Span<int> index_map,
36 const float factor)
37{
38 threading::parallel_for(a.index_range(), 1024, [&](const IndexRange range) {
39 devirtualize_varray(b, [&](const auto b) {
40 for (const int i : range) {
41 if (index_map[i] != -1) {
42 a[i] = bke::attribute_math::mix2(factor, a[i], b[index_map[i]]);
43 }
44 }
45 });
46 });
47}
48
50 const GVArray &b,
51 const Span<int> index_map,
52 const float factor)
53{
54 bke::attribute_math::to_static_type(a.type(), [&]<typename T>() {
55 if constexpr (!std::is_same_v<T, std::string>) {
56 mix_with_indices(a.typed<T>(), b.typed<T>(), index_map, factor);
57 }
58 });
59}
60
61template<typename T> static void mix(MutableSpan<T> a, const VArray<T> &b, const float factor)
62{
63 threading::parallel_for(a.index_range(), 1024, [&](const IndexRange range) {
64 devirtualize_varray(b, [&](const auto b) {
65 for (const int i : range) {
66 a[i] = bke::attribute_math::mix2(factor, a[i], b[i]);
67 }
68 });
69 });
70}
71
72static void mix(GMutableSpan a, const GVArray &b, const float factor)
73{
74 bke::attribute_math::to_static_type(a.type(), [&]<typename T>() {
75 if constexpr (!std::is_same_v<T, std::string>) {
76 mix(a.typed<T>(), b.typed<T>(), factor);
77 }
78 });
79}
80
82 const bke::AttributeAccessor b_attributes,
83 const Span<int> index_map,
84 const bke::AttrDomain mix_domain,
85 const float factor,
86 const Set<std::string> &names_to_skip = {})
87{
88 Set<StringRefNull> names = attributes_a.all_names();
89 names.remove("id");
90 for (const StringRef name : names_to_skip) {
91 names.remove_as(name);
92 }
93
94 for (const StringRef name : names) {
95 const bke::GAttributeReader attribute_a = attributes_a.lookup(name);
96 const bke::AttrDomain domain = attribute_a.domain;
97 if (domain != mix_domain) {
98 continue;
99 }
100 const bke::AttrType type = bke::cpp_type_to_attribute_type(attribute_a.varray.type());
101 if (ELEM(type, bke::AttrType::String, bke::AttrType::Bool)) {
102 /* String attributes can't be mixed, and there's no point in mixing boolean attributes. */
103 continue;
104 }
105 const bke::GAttributeReader attribute_b = b_attributes.lookup(name, attribute_a.domain, type);
106 if (sharing_info_equal(attribute_a.sharing_info, attribute_b.sharing_info)) {
107 continue;
108 }
109 if (!index_map.is_empty()) {
110 bke::GSpanAttributeWriter dst = attributes_a.lookup_for_write_span(name);
111 /* If there's an ID attribute, use its values to mix with potentially changed indices. */
112 mix_with_indices(dst.span, *attribute_b, index_map, factor);
113 dst.finish();
114 }
115 else if (attributes_a.domain_size(domain) == b_attributes.domain_size(domain)) {
116 bke::GSpanAttributeWriter dst = attributes_a.lookup_for_write_span(name);
117 /* With no ID attribute to find matching elements, we can only support mixing when the domain
118 * size (topology) is the same. Other options like mixing just the start of arrays might work
119 * too, but give bad results too. */
120 mix(dst.span, attribute_b.varray, factor);
121 dst.finish();
122 }
123 }
124}
125
127 const bke::AttributeAccessor b_attributes,
128 const bke::AttrDomain id_domain)
129{
130 const bke::GAttributeReader ids_a = attributes_a.lookup("id");
131 const bke::GAttributeReader ids_b = b_attributes.lookup("id");
132 if (!ids_a || !ids_b) {
133 return {};
134 }
135 if (!ids_a.varray.type().is<int>() || !ids_b.varray.type().is<int>()) {
136 return {};
137 }
138 if (ids_a.domain != id_domain || ids_b.domain != id_domain) {
139 return {};
140 }
141 if (sharing_info_equal(ids_a.sharing_info, ids_b.sharing_info)) {
142 return {};
143 }
144
145 const VArraySpan ids_span_a(ids_a.varray.typed<int>());
146 const VArraySpan ids_span_b(ids_b.varray.typed<int>());
147
148 /* Use #int instead of the default #int64_t for internal indices. */
149 const VectorSet<int,
150 16,
156 id_map_b(ids_span_b);
157 Array<int> index_map(ids_span_a.size());
158 threading::parallel_for(ids_span_a.index_range(), 1024, [&](const IndexRange range) {
159 for (const int i : range) {
160 index_map[i] = id_map_b.index_of_try(ids_span_a[i]);
161 }
162 });
163 return index_map;
164}
165
168 const float factor)
169{
170 BLI_assert(a.get().type() == b.get().type());
171 if (a.is_single() && b.is_single()) {
172 GMutablePointer a_ptr = a.get();
173 const GPointer b_ptr = b.get();
174 if (!a_ptr || !b_ptr) {
175 return;
176 }
177 if (a_ptr.is_type<bke::GeometrySet>()) {
178 mix_geometries(*a_ptr.get<bke::GeometrySet>(), *b_ptr.get<bke::GeometrySet>(), factor);
179 }
180 else if (a_ptr.is_type<nodes::BundlePtr>()) {
181 nodes::BundlePtr &a_bundle_ptr = *a_ptr.get<nodes::BundlePtr>();
182 const nodes::BundlePtr &b_bundle_ptr = *b_ptr.get<nodes::BundlePtr>();
183 if (!a_bundle_ptr || !b_bundle_ptr) {
184 return;
185 }
186 mix_bundles(a_bundle_ptr.ensure_mutable_inplace(), *b_bundle_ptr, factor);
187 }
188 else {
189 mix(GMutableSpan(a_ptr.type(), a_ptr.get(), 1),
190 GVArray::from_single_ref(*b_ptr.type(), 1, b_ptr.get()),
191 factor);
192 }
193 }
194 else if (a.is_list() && b.is_list()) {
195 const nodes::GListPtr &a_list_ref = *a.get_if<nodes::GListPtr>();
196 const nodes::GListPtr b_list = *b.get_if<nodes::GListPtr>();
197 if (!a_list_ref || !b_list) {
198 return;
199 }
200 if (a_list_ref->cpp_type() != b_list->cpp_type()) {
201 /* Lists with the same socket type can still have different CPPTypes, e.g. for fields and
202 * grids and single values. For now just don't try to support those combinations. */
203 return;
204 }
205 nodes::GListPtr a_list_ptr = a.extract<nodes::GListPtr>();
206 nodes::GList &a_list = a_list_ptr.get_for_write();
207 std::variant<GMutableSpan, GMutablePointer> a_values = a_list.values_for_write();
208 if (auto *a_span = std::get_if<GMutableSpan>(&a_values)) {
209 const GVArray b_varray = b_list->varray();
210 mix(*a_span, b_varray.slice(IndexRange(a_span->size())), factor);
211 }
212 else if (auto *a_pointer = std::get_if<GMutablePointer>(&a_values)) {
213 const GVArray b_varray = b_list->varray();
214 mix(GMutableSpan(*a_pointer->type(), a_pointer->get(), 1),
215 b_varray.slice(IndexRange(1)),
216 factor);
217 }
218 /* Ideally the API would not require extracting the list and storing it again. */
219 a = bke::SocketValueVariant::from(std::move(a_list_ptr));
220 }
221}
222
225 const float factor)
226{
227 if (a.is_list()) {
228 if (const nodes::GListPtr &a_list = *a.get_if<nodes::GListPtr>()) {
229 if (a_list->cpp_type().is<std::string>()) {
230 return;
231 }
232 }
233 }
234 else if (a.get().is_type<std::string>()) {
235 return;
236 }
237 bke::SocketValueVariant b_converted = b;
238 b_converted.ensure_type(*a.get().type());
239 mix_socket_values_same_type(a, b_converted, factor);
240}
241
244 const float factor)
245{
246 auto *a_socket_value = std::get_if<nodes::BundleItemSocketValue>(&a.value);
247 if (!a_socket_value) {
248 return;
249 }
250 const auto *b_socket_value = std::get_if<nodes::BundleItemSocketValue>(&b.value);
251 if (!b_socket_value) {
252 return;
253 }
254 mix_socket_values(a_socket_value->value, b_socket_value->value, factor);
255}
256
257void mix_bundles(nodes::Bundle &a, const nodes::Bundle &b, const float factor)
258{
259 for (const auto &[name, a_value] : a.items()) {
260 const nodes::BundleItemValue *b_value = b.lookup(name);
261 if (!b_value) {
262 continue;
263 }
264 mix_bundle_items(a_value, *b_value, factor);
265 }
266}
267
268void mix_geometries(bke::GeometrySet &a, const bke::GeometrySet &b, const float factor)
269{
270 if (Mesh *mesh_a = a.get_mesh_for_write()) {
271 if (const Mesh *mesh_b = b.get_mesh()) {
273 mesh_a->attributes(), mesh_b->attributes(), bke::AttrDomain::Point);
274 mix_attributes(mesh_a->attributes_for_write(),
275 mesh_b->attributes(),
276 vert_map,
278 factor,
279 {});
280 }
281 }
282 if (PointCloud *points_a = a.get_pointcloud_for_write()) {
283 if (const PointCloud *points_b = b.get_pointcloud()) {
284 const Array<int> index_map = create_id_index_map(
285 points_a->attributes(), points_b->attributes(), bke::AttrDomain::Point);
286 mix_attributes(points_a->attributes_for_write(),
287 points_b->attributes(),
288 index_map,
290 factor);
291 }
292 }
293 if (Curves *curves_a = a.get_curves_for_write()) {
294 if (const Curves *curves_b = b.get_curves()) {
295 bke::MutableAttributeAccessor a = curves_a->geometry.wrap().attributes_for_write();
296 const bke::AttributeAccessor b = curves_b->geometry.wrap().attributes();
299 a,
300 b,
301 index_map,
303 factor,
304 {"curve_type", "nurbs_order", "knots_mode", "handle_type_left", "handle_type_right"});
305 }
306 }
307 if (bke::Instances *instances_a = a.get_instances_for_write()) {
308 if (const bke::Instances *instances_b = b.get_instances()) {
309 const Array<int> index_map = create_id_index_map(
310 instances_a->attributes(), instances_b->attributes(), bke::AttrDomain::Instance);
311 mix_attributes(instances_a->attributes_for_write(),
312 instances_b->attributes(),
313 index_map,
315 factor,
316 {".reference_index"});
317 }
318 }
319 if (a.has_bundle() && b.has_bundle()) {
320 mix_bundles(a.bundle_for_write(), *b.bundle(), factor);
321 }
322}
323
324} // namespace blender::geometry
Low-level operations for curves.
#define ELEM(...)
bool is() const
const CPPType * type() const
const CPPType & type() const
const CPPType * type() const
const void * get() const
GVArray slice(IndexRange slice) const
static GVArray from_single_ref(const CPPType &type, int64_t size, const void *value)
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
bool remove_as(const ForwardKey &key)
Definition BLI_set.hh:408
bool remove(const Key &key)
Definition BLI_set.hh:404
constexpr bool is_empty() const
Definition BLI_span.hh:264
Set< StringRefNull > all_names() const
GAttributeReader lookup(const StringRef name) const
int domain_size(const AttrDomain domain) const
GSpanAttributeWriter lookup_for_write_span(StringRef name)
static SocketValueVariant from(T &&value)
constexpr IndexRange index_range() const
constexpr int64_t size() const
BundleItemMap::ItemIterator items() const
const CPPType & cpp_type() const
std::variant< GMutableSpan, GMutablePointer > values_for_write()
#define T
void to_static_type(const CPPType &cpp_type, Fn &&fn)
void mix_geometries(bke::GeometrySet &a, const bke::GeometrySet &b, float factor)
static void mix_attributes(bke::MutableAttributeAccessor attributes_a, const bke::AttributeAccessor b_attributes, const Span< int > index_map, const bke::AttrDomain mix_domain, const float factor, const Set< std::string > &names_to_skip={})
void mix_with_indices(MutableSpan< T > a, const VArray< T > &b, const Span< int > index_map, const float factor)
static Array< int > create_id_index_map(const bke::AttributeAccessor attributes_a, const bke::AttributeAccessor b_attributes, const bke::AttrDomain id_domain)
void mix_socket_values(bke::SocketValueVariant &a, const bke::SocketValueVariant &b, const float factor)
static bool sharing_info_equal(const ImplicitSharingInfo *a, const ImplicitSharingInfo *b)
static void mix(MutableSpan< T > a, const VArray< T > &b, const float factor)
void mix_bundles(nodes::Bundle &a, const nodes::Bundle &b, float factor)
static void mix_socket_values_same_type(bke::SocketValueVariant &a, const bke::SocketValueVariant &b, const float factor)
static void mix_bundle_items(nodes::BundleItemValue &a, const nodes::BundleItemValue &b, const float factor)
ImplicitSharingPtr< Bundle > BundlePtr
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
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PythonProbingStrategy<> DefaultProbingStrategy
#define mix
const ImplicitSharingInfo * sharing_info
PointCloud * get_pointcloud_for_write()
Instances * get_instances_for_write()
nodes::Bundle & bundle_for_write()
std::variant< BundleItemSocketValue, BundleItemInternalValue > value