#ifndef ICL_STEPPING_RANGE_H #define ICL_STEPPING_RANGE_H #include "iclRange.h" namespace icl{ /// class representing a range with defined stepping \ingroup TYPES /** A Stepping range is a well defined interval I = [minVal,maxVal] with an also well defined stepping e.g. S. the SteppingRange contains all values {minVal, minVal+S, minVal+2*S,...} that are less then or equal to the maxVal **/ template struct SteppingRange : public Range{ /// create an empty range (min = max = 0) SteppingRange():Range(),stepping(0){} /// create a special Rage SteppingRange(Type minVal, Type maxVal, Type stepping): Range(minVal,maxVal), stepping(stepping){} /// internal stepping parameter Type stepping; /// casts this range into another depth template const SteppingRange castTo() const{ return SteppingRange(clipped_cast(Range::minVal), clipped_cast(Range::maxVal), clipped_cast(stepping)); } /// tests whether a given value is inside of this range virtual bool contains(Type value) const { if(stepping == 0) return Range(Range::minVal,Range::maxVal).contains(value); Type offs = value - Range::minVal; int n = (int)(offs/stepping); double n2 = (double)offs/stepping; return Range::contains(value) && double(n) == n2; } /// returns the nearest value to the given one /** -1st: value is clipped to the underlying range -2nd: if value is inside the range, the nearest implicit step is returned **/ Type nearest(Type value){ if(value < Range::minVal) return Range::minVal; if(value > Range::maxVal) return Range::maxVal; if(stepping == 0) return value; Type offs = value - Range::minVal; int n = (int)(offs/stepping); Type k = n*stepping; if(value-k >= (double)(stepping)/2){ return k+stepping; }else{ return k; } } }; #define ICL_INSTANTIATE_DEPTH(D) typedef SteppingRange SteppingRange##D; ICL_INSTANTIATE_ALL_DEPTHS #undef ICL_INSTANTIATE_DEPTH } #endif