Turi Create  4.0
extrema.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 
7 #ifndef __TC_VIS_EXTREMA
8 #define __TC_VIS_EXTREMA
9 
10 #include <core/data/sframe/gl_sframe.hpp>
11 
12 #include <ostream>
13 
14 namespace turi {
15 namespace visualization {
16 
17 template<typename T>
18 class extrema {
19  private:
20  T m_max = std::numeric_limits<T>::min();
21  T m_min = std::numeric_limits<T>::max();
22 
23  public:
24  void update(const extrema<T>& value) {
25  this->update(value.get_min());
26  this->update(value.get_max());
27  }
28 
29  void update(const T& value) {
30  if (value < m_min) {
31  m_min = value;
32  }
33  if (value > m_max) {
34  m_max = value;
35  }
36  }
37 
38  T get_max() const {
39  // if you are getting values, you should've put at least one value in first.
40  // hitting this assertion means the extrema is probably initialized to [FLOAT_MAX, FLOAT_MIN].
41  DASSERT_GE(m_max, m_min);
42  return m_max;
43  }
44 
45  T get_min() const {
46  // if you are getting values, you should've put at least one value in first.
47  // hitting this assertion means the extrema is probably initialized to [FLOAT_MAX, FLOAT_MIN].
48  DASSERT_GE(m_max, m_min);
49  return m_min;
50  }
51 
52  bool operator==(const extrema<T>& other) const {
53  return m_max == other.m_max && m_min == other.m_min;
54  }
55 
56  friend std::ostream& operator<<(std::ostream& stream, const extrema<T>& ex) {
57  stream << "[" << ex.m_min << ", " << ex.m_max << "]";
58  return stream;
59  }
60 };
61 
62 template<typename T>
63 struct bounding_box {
64  extrema<T> x;
65  extrema<T> y;
66  void update(const bounding_box<T>& value) {
67  this->x.update(value.x);
68  this->y.update(value.y);
69  }
70 
71  bool operator==(const bounding_box<T>& other) const {
72  return x == other.x && y == other.y;
73  }
74 
75  friend std::ostream& operator<<(std::ostream& stream, const bounding_box<T>& bb) {
76  stream << "[" << bb.x << ", " << bb.y << "]";
77  return stream;
78  }
79 };
80 
81 }}
82 
83 #endif