IDS peak IPL v1.18.0
Loading...
Searching...
No Matches
peak_ipl_interval.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
10#include <type_traits>
11#include <cmath>
12#include <limits>
13#include <algorithm>
14
19
20namespace peak
21{
22namespace ipl
23{
24
31template <typename T>
33{
34public:
43 constexpr Interval(T start, T end)
44 : m_start{ start }
45 , m_end{ end }
46 {
47 if (start > end)
48 {
49 throw InvalidArgumentException("start > end is not allowed!");
50 }
51 if (almostEqual(start, end))
52 {
53 throw InvalidArgumentException("start == end is not allowed!");
54 }
55 }
56
64 PEAK_IPL_NO_DISCARD constexpr T Start() const
65 {
66 return m_start;
67 }
68
76 PEAK_IPL_NO_DISCARD constexpr T End() const
77 {
78 return m_end;
79 }
80
90 PEAK_IPL_NO_DISCARD constexpr bool InInterval(T value) const
91 {
92 return value >= m_start && value <= m_end;
93 }
94
95private:
96 T m_start{};
97 T m_end{};
98
99 template <typename Q = T, std::enable_if_t<std::is_integral<Q>::value, int> = 0>
100 static constexpr bool almostEqual(Q v1, Q v2)
101 {
102 return v1 == v2;
103 }
104
105 template <typename Q = T, std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
106 static constexpr Q almostEqualMaxHelper(Q v1, Q v2)
107 {
108 return std::max<Q>({ 1.0, std::fabs(v1), std::fabs(v2) });
109 }
110
111 template <typename Q = T, std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
112 static constexpr bool almostEqual(Q v1, Q v2)
113 {
114 return std::fabs(v1 - v2) <= std::numeric_limits<Q>::epsilon() * almostEqualMaxHelper(v1, v2);
115 }
116};
117
118} /* namespace ipl */
119} /* namespace peak */
constexpr T End() const
Get the end value (inclusive) for the interval.
Definition peak_ipl_interval.hpp:76
constexpr Interval(T start, T end)
Constructor for the interval.
Definition peak_ipl_interval.hpp:43
constexpr T Start() const
Get the start value (inclusive) for the interval.
Definition peak_ipl_interval.hpp:64
constexpr bool InInterval(T value) const
Check if the value is in the interval.
Definition peak_ipl_interval.hpp:90
The exception thrown when passing an invalid parameter to a function.
Definition peak_ipl_exception.hpp:79
The "peak::ipl" namespace contains the whole image processing library.
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_attribute_defines.h:20