CVB++ 15.0
value_range.hpp
1#pragma once
2
3#include <cmath>
4
5#include "global.hpp"
6
7namespace Cvb
8{
9
10 CVB_BEGIN_INLINE_NS
11
13
15 template <class T>
16 class ValueRange final
17 {
18 public:
19 using ValueType = T;
20
22
25 ValueRange() noexcept = default;
26
28
33 ValueRange(T min, T max)
34 {
35 if (min > max)
36 throw std::invalid_argument("min > max");
37
38 min_ = min;
39 max_ = max;
40 }
41
43
47 T Min() const noexcept
48 {
49 return min_;
50 }
51
53
57 void SetMin(T min)
58 {
59 if (min > max_)
60 throw std::invalid_argument("min > max");
61 min_ = min;
62 }
63
65
69 T Max() const noexcept
70 {
71 return max_;
72 }
73
75
79 void SetMax(T max)
80 {
81 if (min_ > max)
82 throw std::invalid_argument("min > max");
83 max_ = max;
84 }
85
87
92 bool Contains(T value) const noexcept
93 {
94 return !(value < min_ || value < max_);
95 }
96
98
103 bool IntersectsWith(ValueRange<T> other) const noexcept
104 {
105 return !(min_ > other.max_ || max_ < other.min_);
106 }
107
109
114 bool operator==(const ValueRange<T> &valueRange) const noexcept
115 {
116 return min_ == valueRange.min_ && max_ == valueRange.max_;
117 }
118
120
125 bool operator!=(const ValueRange<T> &valueRange) const noexcept
126 {
127 return !(*this == valueRange);
128 }
129
131 template <class C>
132 explicit operator ValueRange<C>() const noexcept
133 {
135 "CVB: Unsupported data type - target must be floating point, source must be numeric!");
136 return ValueRange<C>(static_cast<C>(min_), static_cast<C>(max_));
137 }
138
139 private:
140 T min_ = T();
141 T max_ = T();
142 };
143
145
152 template <class T>
153 inline ValueRange<int> Round(const ValueRange<T> &rhs) noexcept
154 {
155 static_assert(std::is_arithmetic<T>::value, "CVB: Unsupported data type - must be numeric!");
156 return ValueRange<int>(static_cast<int>(std::round(rhs.Min())), static_cast<int>(std::round(rhs.Max())));
157 }
158
159 CVB_END_INLINE_NS
160
161} // namespace Cvb
Container for range definitions.
Definition value_range.hpp:17
bool operator==(const ValueRange< T > &valueRange) const noexcept
Compares to an other value range.
Definition value_range.hpp:114
bool operator!=(const ValueRange< T > &valueRange) const noexcept
Compares to an other value range.
Definition value_range.hpp:125
T Min() const noexcept
Gets the minimum value.
Definition value_range.hpp:47
bool IntersectsWith(ValueRange< T > other) const noexcept
Checks if this value range intersects with another value range.
Definition value_range.hpp:103
ValueRange() noexcept=default
Creates a default value range e.g. from 0 to 0.
void SetMin(T min)
Sets the minimum value.
Definition value_range.hpp:57
T Max() const noexcept
Gets the maximum value.
Definition value_range.hpp:69
void SetMax(T max)
Sets the maximum value.
Definition value_range.hpp:79
bool Contains(T value) const noexcept
Checks if a value is within the range.
Definition value_range.hpp:92
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
Point2D< int > Round(const Point2D< T > &rhs) noexcept
Round to an integer point.
Definition point_2d.hpp:371
T round(T... args)