Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLUtils/src/ICLUtils/Range.h ** 00010 ** Module : ICLUtils ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLUtils/BasicTypes.h> 00034 #include <ICLUtils/ClippedCast.h> 00035 #include <ICLUtils/Macros.h> 00036 00037 #include <algorithm> 00038 #include <limits> 00039 #include <iostream> 00040 #include <string> 00041 00042 namespace icl{ 00043 namespace utils{ 00044 00045 00047 template<class Type> 00048 struct Range{ 00049 00052 template<class T> struct MinMaxFunctor{ 00053 MinMaxFunctor(Range<T> *r):r(r){} 00054 Range<T> *r; 00055 inline void operator()(const T &x){ 00056 r->minVal = std::min(r->minVal,x); 00057 r->maxVal = std::max(r->maxVal,x); 00058 } 00059 }; 00062 virtual ~Range(){} 00064 Range():minVal(Type(0)),maxVal(Type(0)){} 00065 00067 Range(Type minVal, Type maxVal): minVal(minVal), maxVal(maxVal){} 00068 00070 static inline Range<Type> limits(){ 00071 return Range<Type>(std::numeric_limits<Type>::min(),std::numeric_limits<Type>::max()); 00072 } 00073 00075 static inline Range<Type> inv_limits(){ 00076 return Range<Type>(std::numeric_limits<Type>::max(),std::numeric_limits<Type>::min()); 00077 } 00078 00080 static inline Range<Type> from(const Type *begin,const Type *end){ 00081 Range<Type> r = Range<Type>::limits(); 00082 std::swap(r.minVal,r.maxVal); 00083 std::for_each(begin,end,MinMaxFunctor<Type>(&r)); 00084 return r; 00085 } 00086 00088 Type minVal; 00089 00091 Type maxVal; 00092 00094 Type getLength() const { return maxVal - minVal; } 00095 00097 template<class dstType> 00098 const Range<dstType> castTo() const{ 00099 return Range<dstType>(clipped_cast<Type,dstType>(minVal),clipped_cast<Type,dstType>(maxVal)); 00100 } 00101 00103 virtual bool contains(Type value) const { return value >= minVal && value <= maxVal; } 00104 00105 // camparison operator 00106 bool operator==(const Range<Type> &other) const{ 00107 return minVal==other.minVal && maxVal == other.maxVal; 00108 } 00109 00110 // camparison operator 00111 bool operator!=(const Range<Type> &other) const{ 00112 return minVal!=other.minVal || maxVal != other.maxVal; 00113 } 00114 00116 void extend(const Type &t){ 00117 if(t < minVal) minVal = t; 00118 if(t > maxVal) maxVal = t; 00119 } 00120 00122 void extend(const Range &r){ 00123 if(r.minVal < minVal) minVal = r.minVal; 00124 if(r.maxVal > maxVal) maxVal = r.maxVal; 00125 } 00126 }; 00127 00128 #define ICL_INSTANTIATE_DEPTH(D) typedef Range<icl##D> Range##D; 00129 ICL_INSTANTIATE_ALL_DEPTHS 00130 #undef ICL_INSTANTIATE_DEPTH 00131 00133 00135 template<class T> 00136 std::ostream &operator<<(std::ostream &s, const Range <T> &range); 00137 00139 template<class T> 00140 std::istream &operator>>(std::istream &s, Range <T> &range); 00141 00142 00143 } // namespace utils 00144 }