Image Component Library (ICL)
Range.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/Range.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/BasicTypes.h>
35 #include <ICLUtils/ClippedCast.h>
36 #include <ICLUtils/Macros.h>
37 
38 #include <algorithm>
39 #include <limits>
40 #include <iostream>
41 #include <string>
42 
43 namespace icl{
44  namespace utils{
45 
46 
48  template<class Type>
49  struct Range{
50 
53  template<class T> struct MinMaxFunctor{
54  MinMaxFunctor(Range<T> *r):r(r){}
55  Range<T> *r;
56  inline void operator()(const T &x){
57  r->minVal = std::min(r->minVal,x);
58  r->maxVal = std::max(r->maxVal,x);
59  }
60  };
63  virtual ~Range(){}
65  Range():minVal(Type(0)),maxVal(Type(0)){}
66 
69 
71  static inline Range<Type> limits(){
72  return Range<Type>((std::numeric_limits<Type>::min)(),(std::numeric_limits<Type>::max)());
73  }
74 
76  static inline Range<Type> inv_limits(){
77  return Range<Type>((std::numeric_limits<Type>::max)(),(std::numeric_limits<Type>::min)());
78  }
79 
81  static inline Range<Type> from(const Type *begin,const Type *end){
83  std::swap(r.minVal,r.maxVal);
84  std::for_each(begin,end,MinMaxFunctor<Type>(&r));
85  return r;
86  }
87 
89  Type minVal;
90 
92  Type maxVal;
93 
95  Type getLength() const { return maxVal - minVal; }
96 
98  template<class dstType>
99  const Range<dstType> castTo() const{
100  return Range<dstType>(clipped_cast<Type,dstType>(minVal),clipped_cast<Type,dstType>(maxVal));
101  }
102 
104  virtual bool contains(Type value) const { return value >= minVal && value <= maxVal; }
105 
106  // camparison operator
107  bool operator==(const Range<Type> &other) const{
108  return minVal==other.minVal && maxVal == other.maxVal;
109  }
110 
111  // camparison operator
112  bool operator!=(const Range<Type> &other) const{
113  return minVal!=other.minVal || maxVal != other.maxVal;
114  }
115 
117  void extend(const Type &t){
118  if(t < minVal) minVal = t;
119  if(t > maxVal) maxVal = t;
120  }
121 
123  void extend(const Range &r){
124  if(r.minVal < minVal) minVal = r.minVal;
125  if(r.maxVal > maxVal) maxVal = r.maxVal;
126  }
127  };
128 
129  #define ICL_INSTANTIATE_DEPTH(D) typedef Range<icl##D> Range##D;
131  #undef ICL_INSTANTIATE_DEPTH
132 
134 
136  template<class T> ICLUtils_API
137  std::ostream &operator<<(std::ostream &s, const Range <T> &range);
138 
140  template<class T> ICLUtils_API
141  std::istream &operator>>(std::istream &s, Range <T> &range);
142 
143  } // namespace utils
144 }
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
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
Range(Type minVal, Type maxVal)
create a special Range
Definition: Range.h:68
bool operator==(const Range< Type > &other) const
Definition: Range.h:107
static Range< Type > inv_limits()
returns inverted limits range [numeric-limits.max,numeric-limits.min]
Definition: Range.h:76
Type minVal
minimum value of this range
Definition: Range.h:89
virtual ~Range()
Definition: Range.h:63
ICLUtils_API std::ostream & operator<<(std::ostream &s, const ConfigFile &cf)
Default ostream operator to put a ConfigFile into a stream.
static Range< Type > from(const Type *begin, const Type *end)
estimate range of given iterator range (using std::for_each)
Definition: Range.h:81
virtual bool contains(Type value) const
tests whether a given value is inside of this range
Definition: Range.h:104
ICLUtils_API std::istream & operator>>(std::istream &s, Point &p)
istream operator
Type getLength() const
return max-min
Definition: Range.h:95
void extend(const Type &t)
extends the range so that the given value is within this range
Definition: Range.h:117
static Range< Type > limits()
returns type range (using std::numeric_limits<Type>)
Definition: Range.h:71
Range()
create an empty range (min = max = 0)
Definition: Range.h:65
const Range< dstType > castTo() const
casts this range into another depth
Definition: Range.h:99
bool operator!=(const Range< Type > &other) const
Definition: Range.h:112
void extend(const Range &r)
extends the range so that the given range is within this range
Definition: Range.h:123