Blender V5.3
attribute_math.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 <algorithm>
10
11#include "BLI_array_utils.hh"
12#include "BLI_math_euler.hh"
13#include "BLI_math_matrix.hh"
15
16#include "PRF_profile.hh"
17
18#include "BKE_attribute_math.hh"
19
21
22template<>
23math::Quaternion mix2(const float factor, const math::Quaternion &a, const math::Quaternion &b)
24{
25 return math::interpolate(a, b, factor);
26}
27
28template<>
30 const math::Quaternion &v0,
31 const math::Quaternion &v1,
32 const math::Quaternion &v2)
33{
34 const float3 expmap_mixed = mix3(weights, v0.expmap(), v1.expmap(), v2.expmap());
35 return math::Quaternion::expmap(expmap_mixed);
36}
37
38template<>
40 const math::Quaternion &v0,
41 const math::Quaternion &v1,
42 const math::Quaternion &v2,
43 const math::Quaternion &v3)
44{
45 const float3 expmap_mixed = mix4(weights, v0.expmap(), v1.expmap(), v2.expmap(), v3.expmap());
46 return math::Quaternion::expmap(expmap_mixed);
47}
48
49template<> float4x4 mix2(const float factor, const float4x4 &a, const float4x4 &b)
50{
51 return math::interpolate(a, b, factor);
52}
53
54template<>
55float4x4 mix3(const float3 &weights, const float4x4 &v0, const float4x4 &v1, const float4x4 &v2)
56{
57 const float3 location = mix3(weights, v0.location(), v1.location(), v2.location());
58 const math::Quaternion rotation = mix3(
59 weights,
63 const float3 scale = mix3(weights, math::to_scale(v0), math::to_scale(v1), math::to_scale(v2));
64 return math::from_loc_rot_scale<float4x4>(location, rotation, scale);
65}
66
67template<>
68float4x4 mix4(const float4 &weights,
69 const float4x4 &v0,
70 const float4x4 &v1,
71 const float4x4 &v2,
72 const float4x4 &v3)
73{
74 const float3 location = mix4(
75 weights, v0.location(), v1.location(), v2.location(), v3.location());
76 const math::Quaternion rotation = mix4(weights,
81 const float3 scale = mix4(
83 return math::from_loc_rot_scale<float4x4>(location, rotation, scale);
84}
85
87 ColorGeometry4f default_color)
88 : ColorGeometry4fMixer(buffer, buffer.index_range(), default_color)
89{
90}
91
93 const IndexMask &mask,
94 const ColorGeometry4f default_color)
95 : buffer_(buffer), default_color_(default_color), total_weights_(buffer.size(), 0.0f)
96{
97 const ColorGeometry4f zero{0.0f, 0.0f, 0.0f, 0.0f};
98 index_mask::masked_fill(buffer_, zero, mask);
99}
100
102 const ColorGeometry4f &color,
103 const float weight)
104{
105 buffer_[index].r = color.r * weight;
106 buffer_[index].g = color.g * weight;
107 buffer_[index].b = color.b * weight;
108 buffer_[index].a = color.a * weight;
109 total_weights_[index] = weight;
110}
111
113 const ColorGeometry4f &color,
114 const float weight)
115{
116 ColorGeometry4f &output_color = buffer_[index];
117 output_color.r += color.r * weight;
118 output_color.g += color.g * weight;
119 output_color.b += color.b * weight;
120 output_color.a += color.a * weight;
121 total_weights_[index] += weight;
122}
123
125{
126 this->finalize(buffer_.index_range());
127}
128
130{
131 mask.foreach_index([&](const int64_t i) {
132 const float weight = total_weights_[i];
133 ColorGeometry4f &output_color = buffer_[i];
134 if (weight > 0.0f) {
135 const float weight_inv = 1.0f / weight;
136 output_color.r *= weight_inv;
137 output_color.g *= weight_inv;
138 output_color.b *= weight_inv;
139 output_color.a *= weight_inv;
140 }
141 else {
142 output_color = default_color_;
143 }
144 });
145}
146
148 const ColorGeometry4b default_color)
149 : ColorGeometry4bMixer(buffer, buffer.index_range(), default_color)
150{
151}
152
154 const IndexMask &mask,
155 const ColorGeometry4b default_color)
156 : buffer_(buffer),
157 default_color_(default_color),
158 total_weights_(buffer.size(), 0.0f),
159 accumulation_buffer_(buffer.size(), float4(0, 0, 0, 0))
160{
161 const ColorGeometry4b zero{0, 0, 0, 0};
162 index_mask::masked_fill(buffer_, zero, mask);
163}
164
165void ColorGeometry4bMixer::ColorGeometry4bMixer::set(int64_t index,
166 const ColorGeometry4b &color,
167 const float weight)
168{
169 accumulation_buffer_[index][0] = color.r * weight;
170 accumulation_buffer_[index][1] = color.g * weight;
171 accumulation_buffer_[index][2] = color.b * weight;
172 accumulation_buffer_[index][3] = color.a * weight;
173 total_weights_[index] = weight;
174}
175
177{
178 float4 &accum_value = accumulation_buffer_[index];
179 accum_value[0] += color.r * weight;
180 accum_value[1] += color.g * weight;
181 accum_value[2] += color.b * weight;
182 accum_value[3] += color.a * weight;
183 total_weights_[index] += weight;
184}
185
187{
188 this->finalize(buffer_.index_range());
189}
190
192{
193 PRF_scope_with_name("ColorGeometry4bMixer::finalize", ProfileCategory::Default);
194 mask.foreach_index([&](const int64_t i) {
195 const float weight = total_weights_[i];
196 const float4 &accum_value = accumulation_buffer_[i];
197 ColorGeometry4b &output_color = buffer_[i];
198 if (weight > 0.0f) {
199 const float weight_inv = 1.0f / weight;
200 output_color.r = accum_value[0] * weight_inv;
201 output_color.g = accum_value[1] * weight_inv;
202 output_color.b = accum_value[2] * weight_inv;
203 output_color.a = accum_value[3] * weight_inv;
204 }
205 else {
206 output_color = default_color_;
207 }
208 });
209}
210
212 : float4x4Mixer(buffer, buffer.index_range())
213{
214}
215
217 : buffer_(buffer),
218 total_weights_(buffer.size(), 0.0f),
219 location_buffer_(buffer.size(), float3(0)),
220 expmap_buffer_(buffer.size(), float3(0)),
221 scale_buffer_(buffer.size(), float3(0))
222{
223}
224
225void float4x4Mixer::float4x4Mixer::set(int64_t index, const float4x4 &value, const float weight)
226{
227 location_buffer_[index] = value.location() * weight;
228 expmap_buffer_[index] = math::to_quaternion(value).expmap() * weight;
229 scale_buffer_[index] = math::to_scale(value) * weight;
230 total_weights_[index] = weight;
231}
232
233void float4x4Mixer::mix_in(int64_t index, const float4x4 &value, float weight)
234{
235 float3 location;
236 math::Quaternion rotation;
238 math::to_loc_rot_scale_safe<true>(value, location, rotation, scale);
239
240 location_buffer_[index] += location * weight;
241 expmap_buffer_[index] += rotation.expmap() * weight;
242 scale_buffer_[index] += scale * weight;
243 total_weights_[index] += weight;
244}
245
247{
248 this->finalize(buffer_.index_range());
249}
250
252{
253 PRF_scope_with_name("float4x4Mixer::finalize", ProfileCategory::Default);
254 mask.foreach_index([&](const int64_t i) {
255 const float weight = total_weights_[i];
256 if (weight > 0.0f) {
257 const float weight_inv = math::rcp(weight);
259 location_buffer_[i] * weight_inv,
260 math::Quaternion::expmap(expmap_buffer_[i] * weight_inv),
261 scale_buffer_[i] * weight_inv);
262 }
263 else {
264 buffer_[i] = float4x4::identity();
265 }
266 });
267}
268
270{
271 float3 location_accum(0);
272 float3 expmap_accum(0);
273 float3 scale_accum(0);
274 for (const int i : indices) {
275 float3 location;
276 math::Quaternion rotation;
278 math::to_loc_rot_scale_safe<true>(src[i], location, rotation, scale);
279 location_accum += location;
280 expmap_accum += rotation.expmap();
281 scale_accum += scale;
282 }
283
284 const float weight_inv = math::safe_rcp(float(indices.size()));
285 return math::from_loc_rot_scale<float4x4>(location_accum * weight_inv,
286 math::Quaternion::expmap(expmap_accum * weight_inv),
287 scale_accum * weight_inv);
288}
289
290template<typename T>
291void mix_groups(const Span<T> src,
292 const OffsetIndices<int> groups,
293 const Span<int> all_indices,
294 const IndexMask &dst_mask,
295 MutableSpan<T> dst)
296{
297 PRF_scope_with_name("attribute_math::mix_groups", ProfileCategory::Default);
298 dst_mask.foreach_index([&](const int dst_i, const int group_i) {
299 dst[dst_i] = mix_indices(src, all_indices.slice(groups[group_i]));
300 });
301}
302
303float4x4 mix_indices(const Span<float4x4> src, const Span<int> indices, const Span<float> weights)
304{
305 float3 location_accum(0);
306 float3 expmap_accum(0);
307 float3 scale_accum(0);
308 float total_weight = 0.0f;
309 for (const int i : indices.index_range()) {
310 const float weight = weights[i];
311 float3 location;
312 math::Quaternion rotation;
314 math::to_loc_rot_scale_safe<true>(src[indices[i]], location, rotation, scale);
315 location_accum += location * weight;
316 expmap_accum += rotation.expmap() * weight;
317 scale_accum += scale * weight;
318 total_weight += weight;
319 }
320
321 const float weight_inv = math::safe_rcp(total_weight);
322 return math::from_loc_rot_scale<float4x4>(location_accum * weight_inv,
323 math::Quaternion::expmap(expmap_accum * weight_inv),
324 scale_accum * weight_inv);
325}
326
327template<typename T>
328void mix_groups(const Span<T> src,
329 const OffsetIndices<int> groups,
330 const Span<int> all_indices,
331 const Span<float> all_weights,
332 const IndexMask &dst_mask,
333 MutableSpan<T> dst)
334{
335 PRF_scope_with_name("attribute_math::mix_groups", ProfileCategory::Default);
336 dst_mask.foreach_index([&](const int dst_i, const int group_i) {
337 dst[dst_i] = mix_indices(
338 src, all_indices.slice(groups[group_i]), all_weights.slice(groups[group_i]));
339 });
340}
341
342void mix_groups(const GSpan src,
343 const OffsetIndices<int> groups,
344 const Span<int> all_indices,
345 const std::optional<Span<float>> all_weights,
346 GMutableSpan dst)
347{
348 BLI_assert(groups.size() == dst.size());
349 mix_groups(src, groups, all_indices, all_weights, IndexMask(dst.size()), dst);
350}
351
352void mix_groups(const GSpan src,
353 const OffsetIndices<int> groups,
354 const Span<int> all_indices,
355 const std::optional<Span<float>> all_weights,
356 const IndexMask &dst_mask,
357 GMutableSpan dst)
358{
359 BLI_assert(groups.size() == dst_mask.size());
360 BLI_assert(groups.total_size() == all_indices.size());
361 BLI_assert(!all_weights || groups.total_size() == all_weights->size());
362
363 to_static_type(src.type(), [&]<typename T>() {
364 if constexpr (!std::is_same_v<T, std::string>) {
365 threading::parallel_for(
366 groups.index_range(),
367 2048,
368 [&](const IndexRange range) {
369 const IndexMask dst_mask_slice = dst_mask.slice(range);
370 if (all_weights) {
371 mix_groups(src.typed<T>(),
372 groups.slice(range),
373 all_indices,
374 *all_weights,
375 dst_mask_slice,
376 dst.typed<T>());
377 }
378 else {
379 mix_groups(src.typed<T>(),
380 groups.slice(range),
381 all_indices,
382 dst_mask_slice,
383 dst.typed<T>());
384 }
385 },
386 threading::accumulated_task_sizes(
387 [&](const IndexRange range) { return groups[range].size(); }));
388 }
389 });
390}
391
392template<typename T>
393void shift_left(MutableSpan<T> data, int src_begin, int src_end, int dst_begin)
394{
395 if (ELEM(src_begin, dst_begin, src_end)) {
396 return;
397 }
398 std::move(data.data() + src_begin, data.data() + src_end, data.data() + dst_begin);
399}
400
401void shift_left(GMutableSpan data, int src_begin, int src_end, int dst_begin)
402{
403 to_static_type(data.type(), [&]<typename T>() {
404 shift_left(data.typed<T>(), src_begin, src_end, dst_begin);
405 });
406}
407
408template<typename T> void shift_right(MutableSpan<T> data, int src_begin, int src_end, int dst_end)
409{
410 if (ELEM(src_end, dst_end, src_begin)) {
411 return;
412 }
413 std::move_backward(data.data() + src_begin, data.data() + src_end, data.data() + dst_end);
414}
415
416void shift_right(GMutableSpan data, int src_begin, int src_end, int dst_begin)
417{
418 to_static_type(data.type(), [&]<typename T>() {
419 shift_right(data.typed<T>(), src_begin, src_end, dst_begin);
420 });
421}
422
423void gather(const GSpan src, const Span<int> map, GMutableSpan dst)
424{
425 gather(GVArray::from_span(src), map, IndexRange(dst.size()), dst);
426}
427
428void gather(const GVArray &src, const Span<int> map, GMutableSpan dst)
429{
430 gather(src, map, IndexRange(dst.size()), dst);
431}
432
433void gather(const GSpan src, const Span<int> map, const IndexMask &dst_mask, GMutableSpan dst)
434{
435 gather(GVArray::from_span(src), map, dst_mask, dst);
436}
437
438void gather(const GVArray &src, const Span<int> map, const IndexMask &dst_mask, GMutableSpan dst)
439{
440 to_static_type(src.type(), [&]<typename T>() {
441 array_utils::gather(src.typed<T>(), map, dst_mask, dst.typed<T>());
442 });
443}
444
446 const OffsetIndices<int> dst_offsets,
447 const IndexMask &selection,
448 const GSpan src,
449 GMutableSpan dst)
450{
451 to_static_type(src.type(), [&]<typename T>() {
452 array_utils::gather_group_to_group(
453 src_offsets, dst_offsets, selection, src.typed<T>(), dst.typed<T>());
454 });
455}
456
458 const OffsetIndices<int> dst_offsets,
459 const GSpan src,
460 GMutableSpan dst)
461{
462 to_static_type(src.type(), [&]<typename T>() {
463 Span<T> src_span = src.typed<T>();
464 MutableSpan<T> dst_span = dst.typed<T>();
465
466 threading::parallel_for(src_ranges.index_range(), 512, [&](const IndexRange range) {
467 for (const int i : range) {
468 dst_span.slice(dst_offsets[i]).copy_from(src_span.slice(src_ranges[i]));
469 }
470 });
471 });
472}
473
475 const IndexMask &src_selection,
476 const GSpan src,
477 GMutableSpan dst)
478{
479 to_static_type(src.type(), [&]<typename T>() {
480 array_utils::gather_to_groups(dst_offsets, src_selection, src.typed<T>(), dst.typed<T>());
481 });
482}
483
484} // namespace blender::bke::attribute_math
#define PRF_scope_with_name(ui_name, category)
#define ELEM(...)
ChannelStorageType r
ChannelStorageType g
ChannelStorageType b
ChannelStorageType a
const CPPType & type() const
static GVArray from_span(GSpan span)
constexpr IndexRange()=default
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
ColorGeometry4bMixer(MutableSpan< ColorGeometry4b > buffer, ColorGeometry4b default_color=ColorGeometry4b(0, 0, 0, 255))
void mix_in(int64_t index, const ColorGeometry4b &color, float weight=1.0f)
void mix_in(int64_t index, const ColorGeometry4f &color, float weight=1.0f)
ColorGeometry4fMixer(MutableSpan< ColorGeometry4f > buffer, ColorGeometry4f default_color=ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f))
void set(int64_t index, const ColorGeometry4f &color, float weight=1.0f)
void mix_in(int64_t index, const float4x4 &value, float weight=1.0f)
float4x4Mixer(MutableSpan< float4x4 > buffer)
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
float3x3 scale(float3x3 mat, float2 scale)
#define T
T mix_indices(const Span< T > src, const Span< int > indices)
void gather(GSpan src, Span< int > map, GMutableSpan dst)
void gather_to_groups(OffsetIndices< int > dst_offsets, const IndexMask &src_selection, GSpan src, GMutableSpan dst)
T mix3(const float3 &weights, const T &v0, const T &v1, const T &v2)
void gather_ranges_to_groups(Span< IndexRange > src_ranges, OffsetIndices< int > dst_offsets, GSpan src, GMutableSpan dst)
void shift_right(GMutableSpan data, int src_begin, int src_end, int dst_begin)
T mix4(const float4 &weights, const T &v0, const T &v1, const T &v2, const T &v3)
void gather_group_to_group(OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, GSpan src, GMutableSpan dst)
void shift_left(GMutableSpan data, int src_begin, int src_end, int dst_begin)
void mix_groups(GSpan src, OffsetIndices< int > groups, Span< int > all_indices, std::optional< Span< float > > all_weights, GMutableSpan dst)
void to_static_type(const CPPType &cpp_type, Fn &&fn)
T mix2(float factor, const T &a, const T &b)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
QuaternionBase< float > Quaternion
T safe_rcp(const T &a)
QuaternionBase< T > to_quaternion(const AxisAngleBase< T, AngleT > &axis_angle)
T interpolate(const T &a, const T &b, const FactorT &t)
T rcp(const T &a)
QuaternionBase< T > normalized_to_quaternion_safe(const MatBase< T, 3, 3 > &mat)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
VecBase< T, 3 > to_scale(const MatBase< T, NumCol, NumRow > &mat)
void to_loc_rot_scale_safe(const MatBase< T, 4, 4 > &mat, VecBase< T, 3 > &r_location, RotationT &r_rotation, VecBase< T, 3 > &r_scale)
MatT from_loc_rot_scale(const typename MatT::loc_type &location, const RotationT &rotation, const VecBase< typename MatT::base_type, ScaleDim > &scale)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
BMesh const char void * data
MatBase< float, 3, 3 > float3x3
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
VecBase< float, 3 > expmap() const
static QuaternionBase expmap(const VecBase< T, 3 > &expmap)