Blender  V3.3
BLI_bounds.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
11 #include <optional>
12 
13 #include "BLI_math_vector.hh"
14 #include "BLI_task.hh"
15 
16 namespace blender::bounds {
17 
18 template<typename T> struct MinMaxResult {
19  T min;
20  T max;
21 };
22 
26 template<typename T> static std::optional<MinMaxResult<T>> min_max(Span<T> values)
27 {
28  if (values.is_empty()) {
29  return std::nullopt;
30  }
31  const MinMaxResult<T> init{values.first(), values.first()};
33  values.index_range(),
34  1024,
35  init,
36  [&](IndexRange range, const MinMaxResult<T> &init) {
37  MinMaxResult<T> result = init;
38  for (const int i : range) {
39  math::min_max(values[i], result.min, result.max);
40  }
41  return result;
42  },
43  [](const MinMaxResult<T> &a, const MinMaxResult<T> &b) {
44  return MinMaxResult<T>{math::min(a.min, b.min), math::max(a.max, b.max)};
45  });
46 }
47 
52 template<typename T, typename RadiusT>
53 static std::optional<MinMaxResult<T>> min_max_with_radii(Span<T> values, Span<RadiusT> radii)
54 {
55  BLI_assert(values.size() == radii.size());
56  if (values.is_empty()) {
57  return std::nullopt;
58  }
59  const MinMaxResult<T> init{values.first(), values.first()};
61  values.index_range(),
62  1024,
63  init,
64  [&](IndexRange range, const MinMaxResult<T> &init) {
65  MinMaxResult<T> result = init;
66  for (const int i : range) {
67  result.min = math::min(values[i] - radii[i], result.min);
68  result.max = math::max(values[i] + radii[i], result.max);
69  }
70  return result;
71  },
72  [](const MinMaxResult<T> &a, const MinMaxResult<T> &b) {
73  return MinMaxResult<T>{math::min(a.min, b.min), math::max(a.max, b.max)};
74  });
75 }
76 
77 } // namespace blender::bounds
#define BLI_assert(a)
Definition: BLI_assert.h:46
constexpr const T & first() const
Definition: BLI_span.hh:303
constexpr int64_t size() const
Definition: BLI_span.hh:240
constexpr IndexRange index_range() const
Definition: BLI_span.hh:401
constexpr bool is_empty() const
Definition: BLI_span.hh:248
#define T
static unsigned a[3]
Definition: RandGen.cpp:78
static std::optional< MinMaxResult< T > > min_max(Span< T > values)
Definition: BLI_bounds.hh:26
static std::optional< MinMaxResult< T > > min_max_with_radii(Span< T > values, Span< RadiusT > radii)
Definition: BLI_bounds.hh:53
T min(const T &a, const T &b)
T max(const T &a, const T &b)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition: BLI_task.hh:73
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
MutableSpan< float > radii
#define min(a, b)
Definition: sort.c:35
float max