IDS peak IPL v1.17.0
Loading...
Searching...
No Matches
peak_ipl_range.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <type_traits>
14#include <cmath>
15#include <limits>
16#include <tuple>
17
22
23namespace peak
24{
25namespace ipl
26{
27
31template <typename T>
32class Range
33{
34 T m_min{};
35 T m_max{};
36 T m_inc{};
37
38 template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
39 static constexpr bool almostEqual(Q v1, Q v2)
40 {
41 return v1 == v2;
42 }
43
44 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
45 static constexpr Q almostEqualMaxHelper(Q v1, Q v2)
46 {
47 return std::max<Q>({ 1.0, std::fabs(v1), std::fabs(v2) });
48 }
49
50 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
51 static constexpr bool almostEqual(Q v1, Q v2)
52 {
53 return std::fabs(v1 - v2) <= std::numeric_limits<Q>::epsilon() * almostEqualMaxHelper(v1, v2);
54 }
55
56 template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
57 constexpr bool validateInc(Q v) const
58 {
59 if (m_inc != 0 && (v % m_inc) != 0)
60 {
61 return false;
62 }
63 return true;
64 }
65
66 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
67 constexpr bool validateInc(Q v) const
68 {
69 if (!almostEqual<Q>(m_inc, 0) && !almostEqual<Q>(std::fmod(v, m_inc), 0))
70 {
71 return false;
72 }
73 return true;
74 }
75
76public:
87 constexpr Range(T min, T max, T inc)
88 : m_min{ min }
89 , m_max{ max }
90 , m_inc{ inc }
91 {
92 if (min > max)
93 {
94 throw InvalidArgumentException("min > max is not allowed!");
95 }
96 if (max - min < inc)
97 {
98 throw InvalidArgumentException("inc > max - min is not allowed!");
99 }
100 if (inc < 0)
101 {
102 throw InvalidArgumentException("inc < 0 is not allowed!");
103 }
104 if (almostEqual(min, max))
105 {
106 throw InvalidArgumentException("min == max is not allowed!");
107 }
108 }
109
115 PEAK_IPL_NO_DISCARD constexpr T Minimum() const
116 {
117 return m_min;
118 }
119
125 PEAK_IPL_NO_DISCARD constexpr T Maximum() const
126 {
127 return m_max;
128 }
129
135 PEAK_IPL_NO_DISCARD constexpr T Increment() const
136 {
137 return m_inc;
138 }
139
145 PEAK_IPL_NO_DISCARD constexpr std::tuple<T, T, T> GetRange() const
146 {
147 return std::make_tuple(Minimum(), Maximum(), Increment());
148 }
149
157 PEAK_IPL_NO_DISCARD constexpr bool InRange(T value) const
158 {
159 if (value < m_min || value > m_max)
160 {
161 return false;
162 }
163
164 return validateInc<T>(value);
165 }
166};
167
168} /* namespace ipl */
169} /* namespace peak */
The exception thrown when passing an invalid parameter to a function.
Definition peak_ipl_exception.hpp:80
constexpr T Minimum() const
Get the minimum value for the range.
Definition peak_ipl_range.hpp:115
constexpr std::tuple< T, T, T > GetRange() const
Get the range as tuple.
Definition peak_ipl_range.hpp:145
constexpr Range(T min, T max, T inc)
Constructor for the Range.
Definition peak_ipl_range.hpp:87
constexpr T Increment() const
Get the increment value for the range.
Definition peak_ipl_range.hpp:135
constexpr bool InRange(T value) const
Check if the value is in range.
Definition peak_ipl_range.hpp:157
constexpr T Maximum() const
Get the maximum value for the range.
Definition peak_ipl_range.hpp:125
The "peak::ipl" namespace contains the whole image processing library.
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_attribute_defines.h:25