IDS peak IPL v1.17.1
Loading...
Searching...
No Matches
peak_ipl_interval.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
15#include <type_traits>
16#include <cmath>
17#include <limits>
18#include <algorithm>
19
24
25namespace peak
26{
27namespace ipl
28{
29
36template <typename T>
38{
39public:
48 constexpr Interval(T start, T end)
49 : m_start{ start }
50 , m_end{ end }
51 {
52 if (start > end)
53 {
54 throw InvalidArgumentException("start > end is not allowed!");
55 }
56 if (almostEqual(start, end))
57 {
58 throw InvalidArgumentException("start == end is not allowed!");
59 }
60 }
61
69 PEAK_IPL_NO_DISCARD constexpr T Start() const
70 {
71 return m_start;
72 }
73
81 PEAK_IPL_NO_DISCARD constexpr T End() const
82 {
83 return m_end;
84 }
85
95 PEAK_IPL_NO_DISCARD constexpr bool InInterval(T value) const
96 {
97 return value >= m_start && value <= m_end;
98 }
99
100private:
101 T m_start{};
102 T m_end{};
103
104 template <typename Q = T, std::enable_if_t<std::is_integral<Q>::value, int> = 0>
105 static constexpr bool almostEqual(Q v1, Q v2)
106 {
107 return v1 == v2;
108 }
109
110 template <typename Q = T, std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
111 static constexpr Q almostEqualMaxHelper(Q v1, Q v2)
112 {
113 return std::max<Q>({ 1.0, std::fabs(v1), std::fabs(v2) });
114 }
115
116 template <typename Q = T, std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
117 static constexpr bool almostEqual(Q v1, Q v2)
118 {
119 return std::fabs(v1 - v2) <= std::numeric_limits<Q>::epsilon() * almostEqualMaxHelper(v1, v2);
120 }
121};
122
123} /* namespace ipl */
124} /* namespace peak */
constexpr T End() const
Get the end value (inclusive) for the interval.
Definition peak_ipl_interval.hpp:81
constexpr Interval(T start, T end)
Constructor for the interval.
Definition peak_ipl_interval.hpp:48
constexpr T Start() const
Get the start value (inclusive) for the interval.
Definition peak_ipl_interval.hpp:69
constexpr bool InInterval(T value) const
Check if the value is in the interval.
Definition peak_ipl_interval.hpp:95
The exception thrown when passing an invalid parameter to a function.
Definition peak_ipl_exception.hpp:84
The "peak::ipl" namespace contains the whole image processing library.
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_attribute_defines.h:25