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