IDS peak IPL v1.18.0
Loading...
Searching...
No Matches
peak_ipl_range.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <type_traits>
9#include <cmath>
10#include <limits>
11#include <tuple>
12#include <algorithm>
13
18
19namespace peak
20{
21namespace ipl
22{
23
28template <typename T>
29class Range
30{
31 T m_min{};
32 T m_max{};
33 T m_inc{};
34
35 template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
36 static constexpr bool almostEqual(Q v1, Q v2)
37 {
38 return v1 == v2;
39 }
40
41 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
42 static constexpr Q almostEqualMaxHelper(Q v1, Q v2)
43 {
44 return std::max<Q>({ 1.0, std::fabs(v1), std::fabs(v2) });
45 }
46
47 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
48 static constexpr bool almostEqual(Q v1, Q v2)
49 {
50 return std::fabs(v1 - v2) <= std::numeric_limits<Q>::epsilon() * almostEqualMaxHelper(v1, v2);
51 }
52
53 template <typename Q = T, typename std::enable_if_t<std::is_integral<Q>::value, int> = 0>
54 constexpr bool validateInc(Q v) const
55 {
56 if (m_inc != 0 && (v % m_inc) != 0)
57 {
58 return false;
59 }
60 return true;
61 }
62
63 template <typename Q = T, typename std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
64 constexpr bool validateInc(Q v) const
65 {
66 if (!almostEqual<Q>(m_inc, 0) && !almostEqual<Q>(std::fmod(v, m_inc), 0))
67 {
68 return false;
69 }
70 return true;
71 }
72
73public:
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:79
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.
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_attribute_defines.h:20