1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
/** \file
* \ingroup bli
*
* Generic algorithms for finding the largest and smallest elements in a span.
*/
#include <optional>
#include "BLI_math_vector.hh"
#include "BLI_task.hh"
namespace blender::bounds {
template<typename T> struct MinMaxResult {
T min;
T max;
};
/**
* Find the smallest and largest values element-wise in the span.
*/
template<typename T> static std::optional<MinMaxResult<T>> min_max(Span<T> values)
{
if (values.is_empty()) {
return std::nullopt;
}
const MinMaxResult<T> init{values.first(), values.first()};
return threading::parallel_reduce(
values.index_range(),
1024,
init,
[&](IndexRange range, const MinMaxResult<T> &init) {
MinMaxResult<T> result = init;
for (const int i : range) {
math::min_max(values[i], result.min, result.max);
}
return result;
},
[](const MinMaxResult<T> &a, const MinMaxResult<T> &b) {
return MinMaxResult<T>{math::min(a.min, b.min), math::max(a.max, b.max)};
});
}
/**
* Find the smallest and largest values element-wise in the span, adding the radius to each element
* first. The template type T is expected to have an addition operator implemented with RadiusT.
*/
template<typename T, typename RadiusT>
static std::optional<MinMaxResult<T>> min_max_with_radii(Span<T> values, Span<RadiusT> radii)
{
BLI_assert(values.size() == radii.size());
if (values.is_empty()) {
return std::nullopt;
}
const MinMaxResult<T> init{values.first(), values.first()};
return threading::parallel_reduce(
values.index_range(),
1024,
init,
[&](IndexRange range, const MinMaxResult<T> &init) {
MinMaxResult<T> result = init;
for (const int i : range) {
result.min = math::min(values[i] - radii[i], result.min);
result.max = math::max(values[i] + radii[i], result.max);
}
return result;
},
[](const MinMaxResult<T> &a, const MinMaxResult<T> &b) {
return MinMaxResult<T>{math::min(a.min, b.min), math::max(a.max, b.max)};
});
}
} // namespace blender::bounds
|