IDS peak IPL  v1.7.0
peak_ipl_range.hpp
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <type_traits>
14 #include <cmath>
15 #include <limits>
16 #include <tuple>
17 
21 namespace peak
22 {
23 namespace ipl
24 {
25 
29 template <typename T>
30 class Range
31 {
32  T m_min{};
33  T m_max{};
34  T m_inc{};
35 
36  template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
37  static constexpr bool almostEqual(Q v1, Q v2)
38  {
39  return v1 == v2;
40  }
41 
42  template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
43  static constexpr Q almostEqualMaxHelper(Q v1, Q v2)
44  {
45  return std::max<Q>({ 1.0, std::fabs(v1), std::fabs(v2) });
46  }
47 
48  template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
49  static constexpr bool almostEqual(Q v1, Q v2)
50  {
51  return std::fabs(v1 - v2) <= std::numeric_limits<Q>::epsilon() * almostEqualMaxHelper(v1, v2);
52  }
53 
54  template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
55  constexpr bool validateInc(Q v) const
56  {
57  if (m_inc != 0 && (v % m_inc) != 0)
58  {
59  return false;
60  }
61  return true;
62  }
63 
64  template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
65  constexpr bool validateInc(Q v) const
66  {
67  if (!almostEqual<Q>(m_inc, 0) && !almostEqual<Q>(std::fmod(v, m_inc), 0))
68  {
69  return false;
70  }
71  return true;
72  }
73 
74 public:
75  constexpr Range(T min, T max, T inc)
76  : m_min{ min }
77  , m_max{ max }
78  , m_inc{ inc }
79  {
80  if (min > max)
81  {
82  throw InvalidArgumentException("min > max is not allowed!");
83  }
84  if (max - min < inc)
85  {
86  throw InvalidArgumentException("inc > max - min is not allowed!");
87  }
88  if (inc < 0)
89  {
90  throw InvalidArgumentException("inc < 0 is not allowed!");
91  }
92  if (almostEqual(min, max))
93  {
94  throw InvalidArgumentException("min == max is not allowed!");
95  }
96  };
97 
98  constexpr T Minimum() const
99  {
100  return m_min;
101  }
102  constexpr T Maximum() const
103  {
104  return m_max;
105  }
106  constexpr T Increment() const
107  {
108  return m_inc;
109  }
110 
111  constexpr std::tuple<T, T, T> GetRange() const
112  {
113  return std::make_tuple(Minimum(), Maximum(), Increment());
114  }
115 
116  constexpr bool InRange(T value) const
117  {
118  if (value < m_min || value > m_max)
119  {
120  return false;
121  }
122 
123  return validateInc<T>(value);
124  }
125 };
126 
127 } /* namespace ipl */
128 } /* namespace peak */
constexpr T Increment() const
Definition: peak_ipl_range.hpp:106
constexpr T Maximum() const
Definition: peak_ipl_range.hpp:102
The "peak::ipl" namespace contains the whole image processing library.
Definition: peak_ipl_binning.hpp:22
The exception thrown when passing an invalid parameter to a function.
Definition: peak_ipl_exception.hpp:58
constexpr Range(T min, T max, T inc)
Definition: peak_ipl_range.hpp:75
Range.
Definition: peak_ipl_range.hpp:30
constexpr T Minimum() const
Definition: peak_ipl_range.hpp:98
constexpr std::tuple< T, T, T > GetRange() const
Definition: peak_ipl_range.hpp:111
constexpr bool InRange(T value) const
Definition: peak_ipl_range.hpp:116