Image Component Library (ICL)
SteppingRange.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLUtils/src/ICLUtils/SteppingRange.h **
10 ** Module : ICLUtils **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLUtils/Range.h>
35 
36 namespace icl{
37  namespace utils{
39 
45  template<class Type>
46  struct SteppingRange : public Range<Type>{
48  SteppingRange():Range<Type>(),stepping(0){}
49 
51  SteppingRange(Type minVal, Type maxVal, Type stepping):
53 
55  Type stepping;
56 
58  template<class dstType>
60  return SteppingRange<dstType>(clipped_cast<Type,dstType>(Range<Type>::minVal),
61  clipped_cast<Type,dstType>(Range<Type>::maxVal),
62  clipped_cast<Type,dstType>(stepping));
63  }
64 
66  virtual bool contains(Type value) const {
68  Type offs = value - Range<Type>::minVal;
69  int n = (int)(offs/stepping);
70  double n2 = (double)offs/stepping;
71  return Range<Type>::contains(value) && double(n) == n2;
72  }
73 
75 
79  Type nearest(Type value){
80  if(value < Range<Type>::minVal) return Range<Type>::minVal;
81  if(value > Range<Type>::maxVal) return Range<Type>::maxVal;
82  if(stepping == 0) return value;
83  Type offs = value - Range<Type>::minVal;
84  int n = (int)(offs/stepping);
85  Type k = n*stepping;
86  if(value-k >= (double)(stepping)/2){
87  return k+stepping;
88  }else{
89  return k;
90  }
91  }
92  };
93 
95 
97  template<class T> ICLUtils_API
98  std::ostream &operator<<(std::ostream &s, const SteppingRange <T> &range);
99 
101  template<class T> ICLUtils_API
102  std::istream &operator>>(std::istream &s, SteppingRange <T> &range);
103 
104 
105  #define ICL_INSTANTIATE_DEPTH(D) typedef SteppingRange<icl##D> SteppingRange##D;
107  #undef ICL_INSTANTIATE_DEPTH
108 
109  } // namespace utils
110 }
class representing a range defined by min and max value
Definition: Range.h:49
Type maxVal
maximum value of this range
Definition: Range.h:92
const SteppingRange< dstType > castTo() const
casts this range into another depth
Definition: SteppingRange.h:59
undocument this line if you encounter any issues!
Definition: Any.h:37
#define ICL_INSTANTIATE_ALL_DEPTHS
Definition: Macros.h:175
#define ICLUtils_API
this macros are important for creating dll's
Definition: CompatMacros.h:171
class representing a range with defined stepping
Definition: SteppingRange.h:46
Type minVal
minimum value of this range
Definition: Range.h:89
SteppingRange(Type minVal, Type maxVal, Type stepping)
create a special Rage
Definition: SteppingRange.h:51
ICLUtils_API std::ostream & operator<<(std::ostream &s, const ConfigFile &cf)
Default ostream operator to put a ConfigFile into a stream.
virtual bool contains(Type value) const
tests whether a given value is inside of this range
Definition: Range.h:104
Type stepping
internal stepping parameter
Definition: SteppingRange.h:55
ICLUtils_API std::istream & operator>>(std::istream &s, Point &p)
istream operator
Type nearest(Type value)
returns the nearest value to the given one
Definition: SteppingRange.h:79
SteppingRange()
create an empty range (min = max = 0)
Definition: SteppingRange.h:48
virtual bool contains(Type value) const
tests whether a given value is inside of this range
Definition: SteppingRange.h:66