IDS peak IPL v1.17.0
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
35template <typename T>
37{
38public:
47 constexpr Interval(T start, T end)
48 : m_start{ start }
49 , m_end{ end }
50 {
51 if (start > end)
52 {
53 throw InvalidArgumentException("start > end is not allowed!");
54 }
55 if (almostEqual(start, end))
56 {
57 throw InvalidArgumentException("start == end is not allowed!");
58 }
59 }
60
68 PEAK_IPL_NO_DISCARD constexpr T Start() const
69 {
70 return m_start;
71 }
72
80 PEAK_IPL_NO_DISCARD constexpr T End() const
81 {
82 return m_end;
83 }
84
94 PEAK_IPL_NO_DISCARD constexpr bool InInterval(T value) const
95 {
96 return value >= m_start && value <= m_end;
97 }
98
99private:
100 T m_start{};
101 T m_end{};
102
103 template <typename Q = T, std::enable_if_t<std::is_integral<Q>::value, int> = 0>
104 static constexpr bool almostEqual(Q v1, Q v2)
105 {
106 return v1 == v2;
107 }
108
109 template <typename Q = T, std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
110 static constexpr Q almostEqualMaxHelper(Q v1, Q v2)
111 {
112 return std::max<Q>({ 1.0, std::fabs(v1), std::fabs(v2) });
113 }
114
115 template <typename Q = T, std::enable_if_t<std::is_floating_point<Q>::value, int> = 0>
116 static constexpr bool almostEqual(Q v1, Q v2)
117 {
118 return std::fabs(v1 - v2) <= std::numeric_limits<Q>::epsilon() * almostEqualMaxHelper(v1, v2);
119 }
120};
121
122} /* namespace ipl */
123} /* namespace peak */
constexpr T End() const
Get the end value (inclusive) for the interval.
Definition peak_ipl_interval.hpp:80
constexpr Interval(T start, T end)
Constructor for the interval.
Definition peak_ipl_interval.hpp:47
constexpr T Start() const
Get the start value (inclusive) for the interval.
Definition peak_ipl_interval.hpp:68
constexpr bool InInterval(T value) const
Check if the value is in the interval.
Definition peak_ipl_interval.hpp:94
The exception thrown when passing an invalid parameter to a function.
Definition peak_ipl_exception.hpp:80
The "peak::ipl" namespace contains the whole image processing library.
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_attribute_defines.h:25