Blender V5.0
BKE_attribute_math.hh
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#pragma once
10
11#include "BLI_array.hh"
12#include "BLI_color_types.hh"
13#include "BLI_cpp_type.hh"
14#include "BLI_generic_span.hh"
16#include "BLI_math_color.hh"
18#include "BLI_math_vector.h"
19#include "BLI_math_vector.hh"
20#include "BLI_offset_indices.hh"
21
22#include "BKE_attribute.hh"
23
24namespace blender {
25class GVArray;
26}
27
29
33template<typename Func>
34inline void convert_to_static_type(const CPPType &cpp_type, const Func &func)
35{
37 float2,
38 float3,
39 int,
40 int2,
41 bool,
42 int8_t,
43 short2,
47 float4x4>([&](auto type_tag) {
48 using T = typename decltype(type_tag)::type;
49 if constexpr (std::is_same_v<T, void>) {
50 /* It's expected that the given cpp type is one of the supported ones. */
52 }
53 else {
54 func(T());
55 }
56 });
57}
58
59template<typename Func>
60inline void convert_to_static_type(const bke::AttrType data_type, const Func &func)
61{
62 const CPPType &cpp_type = bke::attribute_type_to_cpp_type(data_type);
63 convert_to_static_type(cpp_type, func);
64}
65
66/* -------------------------------------------------------------------- */
71
72template<typename T> T mix2(float factor, const T &a, const T &b);
73
74template<> inline bool mix2(const float factor, const bool &a, const bool &b)
75{
76 return ((1.0f - factor) * a + factor * b) >= 0.5f;
77}
78
79template<> inline int8_t mix2(const float factor, const int8_t &a, const int8_t &b)
80{
81 return int8_t(std::round((1.0f - factor) * a + factor * b));
82}
83
84template<> inline int mix2(const float factor, const int &a, const int &b)
85{
86 return int(std::round((1.0f - factor) * a + factor * b));
87}
88
89template<> inline short2 mix2(const float factor, const short2 &a, const short2 &b)
90{
91 return math::interpolate(a, b, factor);
92}
93
94template<> inline int2 mix2(const float factor, const int2 &a, const int2 &b)
95{
96 return math::interpolate(a, b, factor);
97}
98
99template<> inline float mix2(const float factor, const float &a, const float &b)
100{
101 return (1.0f - factor) * a + factor * b;
102}
103
104template<> inline float2 mix2(const float factor, const float2 &a, const float2 &b)
105{
106 return math::interpolate(a, b, factor);
107}
108
109template<> inline float3 mix2(const float factor, const float3 &a, const float3 &b)
110{
111 return math::interpolate(a, b, factor);
112}
113
114template<>
115inline ColorGeometry4f mix2(const float factor, const ColorGeometry4f &a, const ColorGeometry4f &b)
116{
117 return math::interpolate(a, b, factor);
118}
119
120template<>
121inline ColorGeometry4b mix2(const float factor, const ColorGeometry4b &a, const ColorGeometry4b &b)
122{
123 return math::interpolate(a, b, factor);
124}
125
127
128/* -------------------------------------------------------------------- */
133
134template<typename T> T mix3(const float3 &weights, const T &v0, const T &v1, const T &v2);
135
136template<>
137inline int8_t mix3(const float3 &weights, const int8_t &v0, const int8_t &v1, const int8_t &v2)
138{
139 return int8_t(std::round(weights.x * v0 + weights.y * v1 + weights.z * v2));
140}
141
142template<> inline bool mix3(const float3 &weights, const bool &v0, const bool &v1, const bool &v2)
143{
144 return (weights.x * v0 + weights.y * v1 + weights.z * v2) >= 0.5f;
145}
146
147template<> inline int mix3(const float3 &weights, const int &v0, const int &v1, const int &v2)
148{
149 return int(std::round(weights.x * v0 + weights.y * v1 + weights.z * v2));
150}
151
152template<>
153inline short2 mix3(const float3 &weights, const short2 &v0, const short2 &v1, const short2 &v2)
154{
155 return short2(weights.x * float2(v0) + weights.y * float2(v1) + weights.z * float2(v2));
156}
157
158template<> inline int2 mix3(const float3 &weights, const int2 &v0, const int2 &v1, const int2 &v2)
159{
160 return int2(weights.x * float2(v0) + weights.y * float2(v1) + weights.z * float2(v2));
161}
162
163template<>
164inline float mix3(const float3 &weights, const float &v0, const float &v1, const float &v2)
165{
166 return weights.x * v0 + weights.y * v1 + weights.z * v2;
167}
168
169template<>
170inline float2 mix3(const float3 &weights, const float2 &v0, const float2 &v1, const float2 &v2)
171{
172 return weights.x * v0 + weights.y * v1 + weights.z * v2;
173}
174
175template<>
176inline float3 mix3(const float3 &weights, const float3 &v0, const float3 &v1, const float3 &v2)
177{
178 return weights.x * v0 + weights.y * v1 + weights.z * v2;
179}
180
181template<>
182inline ColorGeometry4f mix3(const float3 &weights,
183 const ColorGeometry4f &v0,
184 const ColorGeometry4f &v1,
185 const ColorGeometry4f &v2)
186{
188 interp_v4_v4v4v4(result, v0, v1, v2, weights);
189 return result;
190}
191
192template<>
193inline ColorGeometry4b mix3(const float3 &weights,
194 const ColorGeometry4b &v0,
195 const ColorGeometry4b &v1,
196 const ColorGeometry4b &v2)
197{
198 const float4 v0_f{&v0.r};
199 const float4 v1_f{&v1.r};
200 const float4 v2_f{&v2.r};
201 const float4 mixed = v0_f * weights[0] + v1_f * weights[1] + v2_f * weights[2];
202 return ColorGeometry4b{
203 uint8_t(mixed[0]), uint8_t(mixed[1]), uint8_t(mixed[2]), uint8_t(mixed[3])};
204}
205
207
208/* -------------------------------------------------------------------- */
212
213template<typename T>
214T mix4(const float4 &weights, const T &v0, const T &v1, const T &v2, const T &v3);
215
216template<>
217inline int8_t mix4(
218 const float4 &weights, const int8_t &v0, const int8_t &v1, const int8_t &v2, const int8_t &v3)
219{
220 return int8_t(std::round(weights.x * v0 + weights.y * v1 + weights.z * v2 + weights.w * v3));
221}
222
223template<>
224inline bool mix4(
225 const float4 &weights, const bool &v0, const bool &v1, const bool &v2, const bool &v3)
226{
227 return (weights.x * v0 + weights.y * v1 + weights.z * v2 + weights.w * v3) >= 0.5f;
228}
229
230template<>
231inline int mix4(const float4 &weights, const int &v0, const int &v1, const int &v2, const int &v3)
232{
233 return int(std::round(weights.x * v0 + weights.y * v1 + weights.z * v2 + weights.w * v3));
234}
235
236template<>
238 const float4 &weights, const short2 &v0, const short2 &v1, const short2 &v2, const short2 &v3)
239{
240 return short2(weights.x * float2(v0) + weights.y * float2(v1) + weights.z * float2(v2) +
241 weights.w * float2(v3));
242}
243
244template<>
245inline int2 mix4(
246 const float4 &weights, const int2 &v0, const int2 &v1, const int2 &v2, const int2 &v3)
247{
248 return int2(weights.x * float2(v0) + weights.y * float2(v1) + weights.z * float2(v2) +
249 weights.w * float2(v3));
250}
251
252template<>
253inline float mix4(
254 const float4 &weights, const float &v0, const float &v1, const float &v2, const float &v3)
255{
256 return weights.x * v0 + weights.y * v1 + weights.z * v2 + weights.w * v3;
257}
258
259template<>
261 const float4 &weights, const float2 &v0, const float2 &v1, const float2 &v2, const float2 &v3)
262{
263 return weights.x * v0 + weights.y * v1 + weights.z * v2 + weights.w * v3;
264}
265
266template<>
268 const float4 &weights, const float3 &v0, const float3 &v1, const float3 &v2, const float3 &v3)
269{
270 return weights.x * v0 + weights.y * v1 + weights.z * v2 + weights.w * v3;
271}
272
273template<>
274inline ColorGeometry4f mix4(const float4 &weights,
275 const ColorGeometry4f &v0,
276 const ColorGeometry4f &v1,
277 const ColorGeometry4f &v2,
278 const ColorGeometry4f &v3)
279{
281 interp_v4_v4v4v4v4(result, v0, v1, v2, v3, weights);
282 return result;
283}
284
285template<>
286inline ColorGeometry4b mix4(const float4 &weights,
287 const ColorGeometry4b &v0,
288 const ColorGeometry4b &v1,
289 const ColorGeometry4b &v2,
290 const ColorGeometry4b &v3)
291{
292 const float4 v0_f{&v0.r};
293 const float4 v1_f{&v1.r};
294 const float4 v2_f{&v2.r};
295 const float4 v3_f{&v3.r};
296 float4 mixed;
297 interp_v4_v4v4v4v4(mixed, v0_f, v1_f, v2_f, v3_f, weights);
298 return ColorGeometry4b{
299 uint8_t(mixed[0]), uint8_t(mixed[1]), uint8_t(mixed[2]), uint8_t(mixed[3])};
300}
301
303
304/* -------------------------------------------------------------------- */
311
312template<typename T> class SimpleMixer {
313 private:
314 MutableSpan<T> buffer_;
315 T default_value_;
316 Array<float> total_weights_;
317
318 public:
323 SimpleMixer(MutableSpan<T> buffer, T default_value = {})
324 : SimpleMixer(buffer, buffer.index_range(), default_value)
325 {
326 }
327
331 SimpleMixer(MutableSpan<T> buffer, const IndexMask &mask, T default_value = {})
332 : buffer_(buffer), default_value_(default_value), total_weights_(buffer.size(), 0.0f)
333 {
334 BLI_STATIC_ASSERT(std::is_trivial_v<T>, "");
335 mask.foreach_index([&](const int64_t i) { buffer_[i] = default_value_; });
336 }
337
341 void set(const int64_t index, const T &value, const float weight = 1.0f)
342 {
343 buffer_[index] = value * weight;
344 total_weights_[index] = weight;
345 }
346
350 void mix_in(const int64_t index, const T &value, const float weight = 1.0f)
351 {
352 buffer_[index] += value * weight;
353 total_weights_[index] += weight;
354 }
355
359 void finalize()
360 {
361 this->finalize(IndexMask(buffer_.size()));
362 }
363
365 {
366 mask.foreach_index([&](const int64_t i) {
367 const float weight = total_weights_[i];
368 if (weight > 0.0f) {
369 buffer_[i] *= 1.0f / weight;
370 }
371 else {
372 buffer_[i] = default_value_;
373 }
374 });
375 }
376};
377
387 private:
388 MutableSpan<bool> buffer_;
389
390 public:
395 : BooleanPropagationMixer(buffer, buffer.index_range())
396 {
397 }
398
402 BooleanPropagationMixer(MutableSpan<bool> buffer, const IndexMask &mask) : buffer_(buffer)
403 {
404 mask.foreach_index([&](const int64_t i) { buffer_[i] = false; });
405 }
406
410 void set(const int64_t index, const bool value, [[maybe_unused]] const float weight = 1.0f)
411 {
412 buffer_[index] = value;
413 }
414
418 void mix_in(const int64_t index, const bool value, [[maybe_unused]] const float weight = 1.0f)
419 {
420 buffer_[index] |= value;
421 }
422
426 void finalize() {}
427
428 void finalize(const IndexMask & /*mask*/) {}
429};
430
435template<typename T,
436 typename AccumulationT,
437 AccumulationT (*ValueToAccumulate)(const T &value),
438 T (*AccumulateToValue)(const AccumulationT &value)>
440 private:
441 struct Item {
442 /* Store both values together, because they are accessed together. */
443 AccumulationT value = AccumulationT(0);
444 float weight = 0.0f;
445 };
446
447 MutableSpan<T> buffer_;
448 T default_value_;
449 Array<Item> accumulation_buffer_;
450
451 public:
453 : SimpleMixerWithAccumulationType(buffer, buffer.index_range(), default_value)
454 {
455 }
456
461 const IndexMask &mask,
462 T default_value = {})
463 : buffer_(buffer), default_value_(default_value), accumulation_buffer_(buffer.size())
464 {
465 mask.foreach_index([&](const int64_t index) { buffer_[index] = default_value_; });
466 }
467
468 void set(const int64_t index, const T &value, const float weight = 1.0f)
469 {
470 const AccumulationT converted_value = ValueToAccumulate(value);
471 Item &item = accumulation_buffer_[index];
472 item.value = converted_value * weight;
473 item.weight = weight;
474 }
475
476 void mix_in(const int64_t index, const T &value, const float weight = 1.0f)
477 {
478 const AccumulationT converted_value = ValueToAccumulate(value);
479 Item &item = accumulation_buffer_[index];
480 item.value += converted_value * weight;
481 item.weight += weight;
482 }
483
484 void finalize()
485 {
486 this->finalize(buffer_.index_range());
487 }
488
490 {
491 mask.foreach_index([&](const int64_t i) {
492 const Item &item = accumulation_buffer_[i];
493 if (item.weight > 0.0f) {
494 const float weight_inv = 1.0f / item.weight;
495 const T converted_value = AccumulateToValue(item.value * weight_inv);
496 buffer_[i] = converted_value;
497 }
498 else {
499 buffer_[i] = default_value_;
500 }
501 });
502 }
503};
504
506 private:
508 ColorGeometry4f default_color_;
509 Array<float> total_weights_;
510
511 public:
513 ColorGeometry4f default_color = ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f));
518 const IndexMask &mask,
519 ColorGeometry4f default_color = ColorGeometry4f(0.0f, 0.0f, 0.0f, 1.0f));
520 void set(int64_t index, const ColorGeometry4f &color, float weight = 1.0f);
521 void mix_in(int64_t index, const ColorGeometry4f &color, float weight = 1.0f);
522 void finalize();
523 void finalize(const IndexMask &mask);
524};
525
527 private:
529 ColorGeometry4b default_color_;
530 Array<float> total_weights_;
531 Array<float4> accumulation_buffer_;
532
533 public:
535 ColorGeometry4b default_color = ColorGeometry4b(0, 0, 0, 255));
540 const IndexMask &mask,
541 ColorGeometry4b default_color = ColorGeometry4b(0, 0, 0, 255));
542 void set(int64_t index, const ColorGeometry4b &color, float weight = 1.0f);
543 void mix_in(int64_t index, const ColorGeometry4b &color, float weight = 1.0f);
544 void finalize();
545 void finalize(const IndexMask &mask);
546};
547
549 private:
550 MutableSpan<float4x4> buffer_;
551 Array<float> total_weights_;
552 Array<float3> location_buffer_;
553 Array<float3> expmap_buffer_;
554 Array<float3> scale_buffer_;
555
556 public:
562 void set(int64_t index, const float4x4 &value, float weight = 1.0f);
563 void mix_in(int64_t index, const float4x4 &value, float weight = 1.0f);
564 void finalize();
565 void finalize(const IndexMask &mask);
566};
567
568template<typename T> struct DefaultMixerStruct {
569 /* Use void by default. This can be checked for in `if constexpr` statements. */
570 using type = void;
571};
572template<> struct DefaultMixerStruct<float> {
574};
575template<> struct DefaultMixerStruct<float2> {
577};
578template<> struct DefaultMixerStruct<float3> {
580};
582 /* Use a special mixer for colors. ColorGeometry4f can't be added/multiplied, because this is not
583 * something one should usually do with colors. */
585};
591};
592template<> struct DefaultMixerStruct<int> {
593 static double int_to_double(const int &value)
594 {
595 return double(value);
596 }
597 static int double_to_int(const double &value)
598 {
599 return int(std::round(value));
600 }
601 /* Store interpolated ints in a double temporarily, so that weights are handled correctly. It
602 * uses double instead of float so that it is accurate for all 32 bit integers. */
604};
605template<> struct DefaultMixerStruct<short2> {
606 static float2 int_to_float(const short2 &value)
607 {
608 return float2(value);
609 }
610 static short2 float_to_int(const float2 &value)
611 {
612 return short2(math::round(value));
613 }
615};
616template<> struct DefaultMixerStruct<int2> {
617 static double2 int_to_double(const int2 &value)
618 {
619 return double2(value);
620 }
621 static int2 double_to_int(const double2 &value)
622 {
623 return int2(math::round(value));
624 }
625 /* Store interpolated ints in a double temporarily, so that weights are handled correctly. It
626 * uses double instead of float so that it is accurate for all 32 bit integers. */
628};
629template<> struct DefaultMixerStruct<bool> {
630 static float bool_to_float(const bool &value)
631 {
632 return value ? 1.0f : 0.0f;
633 }
634 static bool float_to_bool(const float &value)
635 {
636 return value >= 0.5f;
637 }
638 /* Store interpolated booleans in a float temporary.
639 * Otherwise information provided by weights is easily rounded away. */
641};
642
643template<> struct DefaultMixerStruct<int8_t> {
644 static float int8_t_to_float(const int8_t &value)
645 {
646 return float(value);
647 }
648 static int8_t float_to_int8_t(const float &value)
649 {
650 return int8_t(std::round(value));
651 }
652 /* Store interpolated 8 bit integers in a float temporarily to increase accuracy. */
654};
667
668template<typename T> struct DefaultPropagationMixerStruct {
669 /* Use void by default. This can be checked for in `if constexpr` statements. */
671};
672
673template<> struct DefaultPropagationMixerStruct<bool> {
675};
676
682template<typename T>
684
685/* Utility to get a good default mixer for a given type. This is `void` when there is no default
686 * mixer for the given type. */
687template<typename T> using DefaultMixer = typename DefaultMixerStruct<T>::type;
688
690
691/* -------------------------------------------------------------------- */
697
698void gather(GSpan src, Span<int> map, GMutableSpan dst);
699void gather(const GVArray &src, Span<int> map, GMutableSpan dst);
701 OffsetIndices<int> dst_offsets,
702 const IndexMask &selection,
703 GSpan src,
704 GMutableSpan dst);
705void gather_to_groups(OffsetIndices<int> dst_offsets,
706 const IndexMask &src_selection,
707 GSpan src,
708 GMutableSpan dst);
709
711 OffsetIndices<int> dst_offsets,
712 GSpan src,
713 GMutableSpan dst);
714
716
717} // namespace blender::bke::attribute_math
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:83
void interp_v4_v4v4v4v4(float p[4], const float v1[4], const float v2[4], const float v3[4], const float v4[4], const float w[4])
void interp_v4_v4v4v4(float p[4], const float v1[4], const float v2[4], const float v3[4], const float w[3])
ATTR_WARN_UNUSED_RESULT const BMVert * v2
long long int int64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void to_static_type_tag(const Fn &fn) const
ChannelStorageType r
BooleanPropagationMixer(MutableSpan< bool > buffer, const IndexMask &mask)
void set(const int64_t index, const bool value, const float weight=1.0f)
void mix_in(const int64_t index, const bool value, const float weight=1.0f)
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 set(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)
SimpleMixerWithAccumulationType(MutableSpan< T > buffer, const IndexMask &mask, T default_value={})
void mix_in(const int64_t index, const T &value, const float weight=1.0f)
SimpleMixerWithAccumulationType(MutableSpan< T > buffer, T default_value={})
void set(const int64_t index, const T &value, const float weight=1.0f)
SimpleMixer(MutableSpan< T > buffer, const IndexMask &mask, T default_value={})
void mix_in(const int64_t index, const T &value, const float weight=1.0f)
void set(const int64_t index, const T &value, const float weight=1.0f)
SimpleMixer(MutableSpan< T > buffer, T default_value={})
void mix_in(int64_t index, const float4x4 &value, float weight=1.0f)
void set(int64_t index, const float4x4 &value, float weight=1.0f)
float4x4Mixer(MutableSpan< float4x4 > buffer)
nullptr float
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define T
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)
void convert_to_static_type(const CPPType &cpp_type, const Func &func)
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)
typename DefaultMixerStruct< T >::type DefaultMixer
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)
typename DefaultPropagationMixerStruct< T >::type DefaultPropagationMixer
T mix2(float factor, const T &a, const T &b)
const CPPType & attribute_type_to_cpp_type(AttrType type)
QuaternionBase< float > Quaternion
T interpolate(const T &a, const T &b, const FactorT &t)
T round(const T &a)
VecBase< double, 2 > double2
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
blender::VecBase< int16_t, 2 > short2
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
SimpleMixerWithAccumulationType< bool, float, bool_to_float, float_to_bool > type
SimpleMixerWithAccumulationType< int2, double2, int_to_double, double_to_int > type
SimpleMixerWithAccumulationType< int8_t, float, int8_t_to_float, float_to_int8_t > type
SimpleMixerWithAccumulationType< int, double, int_to_double, double_to_int > type
SimpleMixerWithAccumulationType< math::Quaternion, float3, quat_to_expmap, expmap_to_quat > type
SimpleMixerWithAccumulationType< short2, float2, int_to_float, float_to_int > type
VecBase< float, 3 > expmap() const
static QuaternionBase expmap(const VecBase< T, 3 > &expmap)
i
Definition text_draw.cc:230