#ifndef RANGE_H #define RANGE_H #include #include #include #include #include #include #include namespace icl{ /// class representing a range defined by min and max value \ingroup TYPES template struct Range{ /** \cond */ template struct MinMaxFunctor{ MinMaxFunctor(Range *r):r(r){} Range *r; inline void operator()(const T &x){ r->minVal = std::min(r->minVal,x); r->maxVal = std::max(r->maxVal,x); } }; /** \endcond */ virtual ~Range(){} /// create an empty range (min = max = 0) Range():minVal(Type(0)),maxVal(Type(0)){} /// create a special Range Range(Type minVal, Type maxVal): minVal(minVal), maxVal(maxVal){} /// returns type range (using std::numeric_limits) static inline Range limits(){ return Range(std::numeric_limits::min(),std::numeric_limits::max()); } /// estimate range of given iterator range (using std::for_each) static inline Range from(const Type *begin,const Type *end){ Range r = Range::limits(); std::swap(r.minVal,r.maxVal); std::for_each(begin,end,MinMaxFunctor(&r)); return r; } /// minimum value of this range Type minVal; /// maximum value of this range Type maxVal; /// return max-min Type getLength() const { return maxVal - minVal; } /// casts this range into another depth template const Range castTo() const{ return Range(clipped_cast(minVal),clipped_cast(maxVal)); } /// tests whether a given value is inside of this range virtual bool contains(Type value) const { return value >= minVal && value <= maxVal; } // camparison operator bool operator==(const Range &other) const{ return minVal==other.minVal && maxVal == other.maxVal; } // camparison operator bool operator!=(const Range &other) const{ return minVal!=other.minVal || maxVal != other.maxVal; } }; #define ICL_INSTANTIATE_DEPTH(D) typedef Range Range##D; ICL_INSTANTIATE_ALL_DEPTHS #undef ICL_INSTANTIATE_DEPTH /// puts a string representation [min,max] of given range into the given stream /** Available for all icl-Types (icl8u,icl16s, icl32s, icl32f and icl64f and for unsigned int */ template std::ostream &operator<<(std::ostream &s, const Range &range); /// parses a range argument into a std::string template std::istream &operator>>(std::istream &s, Range &range); } #endif