cgv
Loading...
Searching...
No Matches
interval.h
1#pragma once
2
3#include <cgv/type/traits/min.h>
4#include <cgv/type/traits/max.h>
5#include <iostream>
6#include <algorithm>
7#include <vector>
8
9namespace cgv {
10 namespace math {
11
25template <typename T>
27{
28public:
29 // the interval lower bound [x,_]
30 T lower_bound;
31 // the interval upper bound [_,x]
32 T upper_bound;
33
37 interval() : lower_bound(1), upper_bound(0) {}
43 interval(bool) : lower_bound(type::traits::min_fct<T>::get_value()), upper_bound(type::traits::max_fct<T>::get_value()) {}
45 interval(const interval<T>& I) : lower_bound(I.lower_bound), upper_bound(I.upper_bound) {}
47 interval(const T& lb, const T& ub) : lower_bound(std::min(lb, ub)), upper_bound(std::max(lb, ub)) {}
49 bool empty() const { return upper_bound < lower_bound; }
51 void clear() { lower_bound = T(1); upper_bound = T(0); }
53 bool contains(const T& v) const { return lower_bound <= v && v <= upper_bound; }
55 T center() const { return (lower_bound + upper_bound) / T(2); }
57 T size() const { return empty() ? T(0) : upper_bound - lower_bound; }
60 lower_bound = std::max(lower_bound, I.lower_bound);
61 upper_bound = std::min(upper_bound, I.upper_bound);
62 return *this;
63 }
65 interval<T> intersection(const interval<T>& I) const { interval J(*this); return J.intersect(I); }
67 interval<T>& extend(const T& v) {
68 if(empty()) {
69 lower_bound = v;
70 upper_bound = v;
71 } else {
72 lower_bound = std::min(lower_bound, v);
73 upper_bound = std::max(upper_bound, v);
74 }
75 return *this;
76 }
78 interval<T>& extend(const T vs[], size_t n) {
79 for(size_t i = 0; i < n; ++i)
80 extend(vs[i]);
81 return *this;
82 }
84 interval<T>& extend(const std::vector<T>& vs) {
85 for(size_t i = 0; i < vs.size(); ++i)
86 extend(vs[i]);
87 return *this;
88 }
90 interval<T>& extension(const T& v) { interval<T> I(*this); return I.extend(v); }
93 if (!I.empty()) {
94 extend(I.lower_bound);
95 extend(I.upper_bound);
96 }
97 return *this;
98 }
100 interval<T>& extension(const interval<T>& J) { interval<T> I(*this); return I.extend(J); }
102
105 interval<T>& operator *= (const T& s) { lower_bound *= s; upper_bound *= s; return *this; }
107 interval<T> operator * (const T& s) const { interval I(*this); return I *= s; }
109 interval<T>& operator /= (const T& s) { return *this *= T(1) / s; }
111 interval<T> operator / (const T& s) const { return *this * (T(1) / s); }
113 interval<T>& operator += (const T& s) { lower_bound += s; upper_bound += s; return *this; }
115 interval<T> operator + (const T& s) const { interval I(*this); return I += s; }
117 interval<T>& operator -= (const T& s) { lower_bound -= s; upper_bound -= s; return *this; }
119 interval<T> operator - (const T& s) const { interval I(*this); return I -= s; }
121
126 if (empty() || I.empty())
127 *this = interval<T>();
128 else
129 extend(lower_bound * I.lower_bound, lower_bound * I.upper_bound,
130 upper_bound * I.lower_bound, upper_bound * I.upper_bound);
131 return *this;
132 }
134 interval<T> operator * (const interval<T>& J) const { interval I(*this); return I *= J; }
140 if (empty() || I.empty())
141 *this = interval<T>();
142 else {
143 if (I.contains(T(0)))
144 *this = interval<T>(true);
145 else
146 extend(lower_bound / I.lower_bound, lower_bound/I.upper_bound,
147 upper_bound / I.lower_bound, upper_bound/I.upper_bound);
148 }
149 return *this;
150 }
152 interval<T> operator / (const interval<T>& J) const { interval I(*this); return I /= J; }
154 interval<T> operator - () const { if (empty()) return *this; else return interval<T>(-upper_bound, -lower_bound); }
157 if (empty() || I.empty())
158 *this = interval<T>();
159 else {
160 extend(lower_bound + I.lower_bound, lower_bound + I.upper_bound,
161 upper_bound + I.lower_bound, upper_bound + I.upper_bound);
162 }
163 return *this;
164 }
166 interval<T> operator + (const interval<T>& J) const { interval I(*this); return I += J; }
168 interval<T>& operator -= (const interval<T>& I) { return *this += -I; }
170 interval<T> operator - (const interval<T>& J) const { interval I(*this); return I -= J; }
172
176 bool operator == (const interval<T>& I) const {
177 return empty() && I.empty() ||
178 lower_bound == I.lower_bound && upper_bound == I.upper_bound;
179 }
181 bool operator != (const interval<T>& I) const {
182 return !(*this == I);
183 }
185 bool operator < (const interval<T>& I) const {
186 return !empty() && !I.empty() && upper_bound < I.lower_bound;
187 }
189 bool operator > (const interval<T>& I) const {
190 return !empty() && !I.empty() && lower_bound > I.upper_bound;
191 }
193
194};
195
196template <typename T> inline interval<T> operator + (const T& s, const interval<T>& I) { return I+s; }
197template <typename T> inline interval<T> operator - (const T& s, const interval<T>& I) { return -I+s; }
198template <typename T> inline interval<T> operator * (const T& s, const interval<T>& I) { return I*s; }
199template <typename T> inline interval<T> operator / (const T& s, const interval<T>& I) { return interval<T>(s,s)/I; }
200
201template <typename T>
202std::ostream& operator << (std::ostream& os, const interval<T>& I) {
203 return os << '[' << I.lower_bound << ',' << I.upper_bound << ']';
204}
205
206 }
207}
The interval template represents a closed interval of two numbers, i.e.
Definition interval.h:27
T center() const
return the center value, which is only valid if the interval is not empty
Definition interval.h:55
bool operator<(const interval< T > &I) const
only returns true if both intervals are not empty and the operator holds for all values in both inter...
Definition interval.h:185
T size() const
return the size of the interval
Definition interval.h:57
interval< T > & intersect(const interval< T > &I)
set interval to intersection with given interval and return reference to this interval
Definition interval.h:59
interval< T > & operator+=(const T &s)
right shift interval by adding scalar to both bounds
Definition interval.h:113
interval< T > intersection(const interval< T > &I) const
return intersection interval
Definition interval.h:65
void clear()
set to empty interval
Definition interval.h:51
interval< T > operator/(const T &s) const
return divided interval
Definition interval.h:111
interval< T > operator-() const
unary minus operator reflects interval at zero value
Definition interval.h:154
interval< T > & extend(const T &v)
extend interval such that it includes the given value
Definition interval.h:67
interval()
construct empty interval
Definition interval.h:37
interval< T > operator*(const T &s) const
return scaled interval
Definition interval.h:107
interval< T > & extension(const interval< T > &J)
return extension of interval so that it includes the given interval
Definition interval.h:100
bool operator!=(const interval< T > &I) const
check for inequality of two intervals
Definition interval.h:181
interval< T > & extend(const T vs[], size_t n)
extend interval such that it includes the n given values
Definition interval.h:78
interval(const T &lb, const T &ub)
Contruct interval from bounds, sort bounds if necessary. To construct empty interval call the standar...
Definition interval.h:47
interval< T > & operator/=(const T &s)
divide the interval
Definition interval.h:109
interval< T > & operator*=(const T &s)
scale the interval
Definition interval.h:105
bool empty() const
check if interval is empty
Definition interval.h:49
bool operator>(const interval< T > &I) const
only returns true if both intervals are not empty and the operator holds for all values in both inter...
Definition interval.h:189
interval(const interval< T > &I)
copy constructor
Definition interval.h:45
bool contains(const T &v) const
check if given value is contained in interval
Definition interval.h:53
interval< T > & extend(const interval< T > &I)
extend interval such that it includes the given interval
Definition interval.h:92
interval(bool)
Construct interval over all valid values of the value type, where the parameter is only a dummy to di...
Definition interval.h:43
interval< T > & extend(const std::vector< T > &vs)
extend interval such that it includes the given values
Definition interval.h:84
bool operator==(const interval< T > &I) const
check for equality of two intervals
Definition interval.h:176
interval< T > & extension(const T &v)
return extension of interval so that it includes the given value
Definition interval.h:90
interval< T > & operator-=(const T &s)
left shift interval by subtracting scalar from both bounds
Definition interval.h:117
interval< T > operator+(const T &s) const
return right shifted interval
Definition interval.h:115
the cgv namespace
Definition print.h:11