IDS peak IPL v1.11.0
Loading...
Searching...
No Matches
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
21namespace peak
22{
23namespace ipl
24{
25
29template <typename T>
30class 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
74public:
84 constexpr Range(T min, T max, T inc)
85 : m_min{ min }
86 , m_max{ max }
87 , m_inc{ inc }
88 {
89 if (min > max)
90 {
91 throw InvalidArgumentException("min > max is not allowed!");
92 }
93 if (max - min < inc)
94 {
95 throw InvalidArgumentException("inc > max - min is not allowed!");
96 }
97 if (inc < 0)
98 {
99 throw InvalidArgumentException("inc < 0 is not allowed!");
100 }
101 if (almostEqual(min, max))
102 {
103 throw InvalidArgumentException("min == max is not allowed!");
104 }
105 };
106
112 PEAK_IPL_NO_DISCARD constexpr T Minimum() const
113 {
114 return m_min;
115 }
116
122 PEAK_IPL_NO_DISCARD constexpr T Maximum() const
123 {
124 return m_max;
125 }
126
132 PEAK_IPL_NO_DISCARD constexpr T Increment() const
133 {
134 return m_inc;
135 }
136
142 PEAK_IPL_NO_DISCARD constexpr std::tuple<T, T, T> GetRange() const
143 {
144 return std::make_tuple(Minimum(), Maximum(), Increment());
145 }
146
154 PEAK_IPL_NO_DISCARD constexpr bool InRange(T value) const
155 {
156 if (value < m_min || value > m_max)
157 {
158 return false;
159 }
160
161 return validateInc<T>(value);
162 }
163};
164
165} /* namespace ipl */
166} /* namespace peak */
The exception thrown when passing an invalid parameter to a function.
Definition peak_ipl_exception.hpp:59
Range support and convenience class.
Definition peak_ipl_range.hpp:31
constexpr T Minimum() const
Get the minimum value for the range.
Definition peak_ipl_range.hpp:112
constexpr std::tuple< T, T, T > GetRange() const
Get the range as tuple.
Definition peak_ipl_range.hpp:142
constexpr Range(T min, T max, T inc)
Constructor for the Range.
Definition peak_ipl_range.hpp:84
constexpr T Increment() const
Get the increment value for the range.
Definition peak_ipl_range.hpp:132
constexpr bool InRange(T value) const
Check if the value is in range.
Definition peak_ipl_range.hpp:154
constexpr T Maximum() const
Get the maximum value for the range.
Definition peak_ipl_range.hpp:122
The "peak::ipl" namespace contains the whole image processing library.
Definition peak_ipl_binning.hpp:23
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_backend.h:26