Cleanup: BLI: Deduplicate bounds merging
Also declare variables and arguments consta
This commit is contained in:
@@ -18,61 +18,6 @@
|
||||
|
||||
namespace blender::bounds {
|
||||
|
||||
/**
|
||||
* Find the smallest and largest values element-wise in the span.
|
||||
*/
|
||||
template<typename T> [[nodiscard]] inline std::optional<Bounds<T>> min_max(Span<T> values)
|
||||
{
|
||||
if (values.is_empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const Bounds<T> init{values.first(), values.first()};
|
||||
return threading::parallel_reduce(
|
||||
values.index_range(),
|
||||
1024,
|
||||
init,
|
||||
[&](IndexRange range, const Bounds<T> &init) {
|
||||
Bounds<T> result = init;
|
||||
for (const int i : range) {
|
||||
math::min_max(values[i], result.min, result.max);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
[](const Bounds<T> &a, const Bounds<T> &b) {
|
||||
return Bounds<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>
|
||||
[[nodiscard]] inline std::optional<Bounds<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 Bounds<T> init{values.first(), values.first()};
|
||||
return threading::parallel_reduce(
|
||||
values.index_range(),
|
||||
1024,
|
||||
init,
|
||||
[&](IndexRange range, const Bounds<T> &init) {
|
||||
Bounds<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 Bounds<T> &a, const Bounds<T> &b) {
|
||||
return Bounds<T>{math::min(a.min, b.min), math::max(a.max, b.max)};
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T> [[nodiscard]] inline Bounds<T> merge(const Bounds<T> &a, const Bounds<T> &b)
|
||||
{
|
||||
return {math::min(a.min, b.min), math::max(a.max, b.max)};
|
||||
@@ -94,4 +39,55 @@ template<typename T>
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the smallest and largest values element-wise in the span.
|
||||
*/
|
||||
template<typename T> [[nodiscard]] inline std::optional<Bounds<T>> min_max(const Span<T> values)
|
||||
{
|
||||
if (values.is_empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const Bounds<T> init{values.first(), values.first()};
|
||||
return threading::parallel_reduce(
|
||||
values.index_range(),
|
||||
1024,
|
||||
init,
|
||||
[&](const IndexRange range, const Bounds<T> &init) {
|
||||
Bounds<T> result = init;
|
||||
for (const int i : range) {
|
||||
math::min_max(values[i], result.min, result.max);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
[](const Bounds<T> &a, const Bounds<T> &b) { return merge(a, b); });
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
[[nodiscard]] inline std::optional<Bounds<T>> min_max_with_radii(const Span<T> values,
|
||||
const Span<RadiusT> radii)
|
||||
{
|
||||
BLI_assert(values.size() == radii.size());
|
||||
if (values.is_empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const Bounds<T> init{values.first(), values.first()};
|
||||
return threading::parallel_reduce(
|
||||
values.index_range(),
|
||||
1024,
|
||||
init,
|
||||
[&](const IndexRange range, const Bounds<T> &init) {
|
||||
Bounds<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 Bounds<T> &a, const Bounds<T> &b) { return merge(a, b); });
|
||||
}
|
||||
|
||||
} // namespace blender::bounds
|
||||
|
||||
Reference in New Issue
Block a user