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