3#include <cgv/type/traits/min.h>
4#include <cgv/type/traits/max.h>
43 interval(
bool) : lower_bound(type::traits::min_fct<T>::get_value()), upper_bound(type::traits::max_fct<T>::get_value()) {}
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);
72 lower_bound = std::min(lower_bound, v);
73 upper_bound = std::max(upper_bound, v);
79 for(
size_t i = 0; i < n; ++i)
85 for(
size_t i = 0; i < vs.size(); ++i)
129 extend(lower_bound * I.lower_bound, lower_bound * I.upper_bound,
130 upper_bound * I.lower_bound, upper_bound * I.upper_bound);
146 extend(lower_bound / I.lower_bound, lower_bound/I.upper_bound,
147 upper_bound / I.lower_bound, upper_bound/I.upper_bound);
160 extend(lower_bound + I.lower_bound, lower_bound + I.upper_bound,
161 upper_bound + I.lower_bound, upper_bound + I.upper_bound);
178 lower_bound == I.lower_bound && upper_bound == I.upper_bound;
182 return !(*
this == I);
186 return !
empty() && !I.
empty() && upper_bound < I.lower_bound;
190 return !
empty() && !I.
empty() && lower_bound > I.upper_bound;
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; }
202std::ostream& operator << (std::ostream& os,
const interval<T>& I) {
203 return os <<
'[' << I.lower_bound <<
',' << I.upper_bound <<
']';
The interval template represents a closed interval of two numbers, i.e.
T center() const
return the center value, which is only valid if the interval is not empty
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...
T size() const
return the size of the interval
interval< T > & intersect(const interval< T > &I)
set interval to intersection with given interval and return reference to this interval
interval< T > & operator+=(const T &s)
right shift interval by adding scalar to both bounds
interval< T > intersection(const interval< T > &I) const
return intersection interval
void clear()
set to empty interval
interval< T > operator/(const T &s) const
return divided interval
interval< T > operator-() const
unary minus operator reflects interval at zero value
interval< T > & extend(const T &v)
extend interval such that it includes the given value
interval()
construct empty interval
interval< T > operator*(const T &s) const
return scaled interval
interval< T > & extension(const interval< T > &J)
return extension of interval so that it includes the given interval
bool operator!=(const interval< T > &I) const
check for inequality of two intervals
interval< T > & extend(const T vs[], size_t n)
extend interval such that it includes the n given values
interval(const T &lb, const T &ub)
Contruct interval from bounds, sort bounds if necessary. To construct empty interval call the standar...
interval< T > & operator/=(const T &s)
divide the interval
interval< T > & operator*=(const T &s)
scale the interval
bool empty() const
check if interval is empty
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...
interval(const interval< T > &I)
copy constructor
bool contains(const T &v) const
check if given value is contained in interval
interval< T > & extend(const interval< T > &I)
extend interval such that it includes the given interval
interval(bool)
Construct interval over all valid values of the value type, where the parameter is only a dummy to di...
interval< T > & extend(const std::vector< T > &vs)
extend interval such that it includes the given values
bool operator==(const interval< T > &I) const
check for equality of two intervals
interval< T > & extension(const T &v)
return extension of interval so that it includes the given value
interval< T > & operator-=(const T &s)
left shift interval by subtracting scalar from both bounds
interval< T > operator+(const T &s) const
return right shifted interval