Horizon
bbox_accumulator.hpp
1 #pragma once
2 #include "common/common.hpp"
3 #include <optional>
4 
5 namespace horizon {
6 template <typename T> class BBoxAccumulator {
7  using TCoord = Coord<T>;
8  using TBB = std::pair<TCoord, TCoord>;
9 
10 public:
11  void accumulate(const TCoord &c)
12  {
13  if (bbox) {
14  bbox->first = TCoord::min(bbox->first, c);
15  bbox->second = TCoord::max(bbox->second, c);
16  }
17  else {
18  bbox.emplace(c, c);
19  }
20  }
21 
22  void accumulate(const TBB &bb)
23  {
24  accumulate(bb.first);
25  accumulate(bb.second);
26  }
27 
28  const auto &get() const
29  {
30  return bbox.value();
31  }
32 
33  const TBB get_or_0() const
34  {
35  if (bbox)
36  return bbox.value();
37  else
38  return {};
39  }
40 
41  const TBB get_or(const TBB &other) const
42  {
43  if (bbox)
44  return bbox.value();
45  else
46  return other;
47  }
48 
49 private:
50  std::optional<TBB> bbox;
51 };
52 } // namespace horizon
Definition: bbox_accumulator.hpp:6
Your typical coordinate class.
Definition: common.hpp:78
static Coord< T > max(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:156
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:148