IDS peak IPL v1.17.1
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#include <algorithm>
18
23
24namespace peak
25{
26namespace ipl
27{
28
33template <typename T>
34class Range
35{
36 T m_min{};
37 T m_max{};
38 T m_inc{};
39
40 template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
41 static constexpr bool almostEqual(Q v1, Q v2)
42 {
43 return v1 == v2;
44 }
45
46 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
47 static constexpr Q almostEqualMaxHelper(Q v1, Q v2)
48 {
49 return std::max<Q>({ 1.0, std::fabs(v1), std::fabs(v2) });
50 }
51
52 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
53 static constexpr bool almostEqual(Q v1, Q v2)
54 {
55 return std::fabs(v1 - v2) <= std::numeric_limits<Q>::epsilon() * almostEqualMaxHelper(v1, v2);
56 }
57
58 template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
59 constexpr bool validateInc(Q v) const
60 {
61 if (m_inc != 0 && (v % m_inc) != 0)
62 {
63 return false;
64 }
65 return true;
66 }
67
68 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
69 constexpr bool validateInc(Q v) const
70 {
71 if (!almostEqual<Q>(m_inc, 0) && !almostEqual<Q>(std::fmod(v, m_inc), 0))
72 {
73 return false;
74 }
75 return true;
76 }
77
78public:
89 constexpr Range(T min, T max, T inc)
90 : m_min{ min }
91 , m_max{ max }
92 , m_inc{ inc }
93 {
94 if (min > max)
95 {
96 throw InvalidArgumentException("min > max is not allowed!");
97 }
98 if (max - min < inc)
99 {
100 throw InvalidArgumentException("inc > max - min is not allowed!");
101 }
102 if (inc < 0)
103 {
104 throw InvalidArgumentException("inc < 0 is not allowed!");
105 }
106 if (almostEqual(min, max))
107 {
108 throw InvalidArgumentException("min == max is not allowed!");
109 }
110 }
111
117 PEAK_IPL_NO_DISCARD constexpr T Minimum() const
118 {
119 return m_min;
120 }
121
127 PEAK_IPL_NO_DISCARD constexpr T Maximum() const
128 {
129 return m_max;
130 }
131
137 PEAK_IPL_NO_DISCARD constexpr T Increment() const
138 {
139 return m_inc;
140 }
141
147 PEAK_IPL_NO_DISCARD constexpr std::tuple<T, T, T> GetRange() const
148 {
149 return std::make_tuple(Minimum(), Maximum(), Increment());
150 }
151
159 PEAK_IPL_NO_DISCARD constexpr bool InRange(T value) const
160 {
161 if (value < m_min || value > m_max)
162 {
163 return false;
164 }
165
166 return validateInc<T>(value);
167 }
168};
169
170} /* namespace ipl */
171} /* namespace peak */
The exception thrown when passing an invalid parameter to a function.
Definition peak_ipl_exception.hpp:84
constexpr T Minimum() const
Get the minimum value for the range.
Definition peak_ipl_range.hpp:117
constexpr std::tuple< T, T, T > GetRange() const
Get the range as tuple.
Definition peak_ipl_range.hpp:147
constexpr Range(T min, T max, T inc)
Constructor for the Range.
Definition peak_ipl_range.hpp:89
constexpr T Increment() const
Get the increment value for the range.
Definition peak_ipl_range.hpp:137
constexpr bool InRange(T value) const
Check if the value is in range.
Definition peak_ipl_range.hpp:159
constexpr T Maximum() const
Get the maximum value for the range.
Definition peak_ipl_range.hpp:127
The "peak::ipl" namespace contains the whole image processing library.
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_attribute_defines.h:25