Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Range.h
Go to the documentation of this file.
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.LGPL **
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/CompatMacros.h>
00034 #include <ICLUtils/BasicTypes.h>
00035 #include <ICLUtils/ClippedCast.h>
00036 #include <ICLUtils/Macros.h>
00037 
00038 #include <algorithm>
00039 #include <limits>
00040 #include <iostream>
00041 #include <string>
00042 
00043 namespace icl{
00044   namespace utils{
00045     
00046    
00048     template<class Type> 
00049     struct Range{
00050   
00053       template<class T> struct MinMaxFunctor{
00054       MinMaxFunctor(Range<T> *r):r(r){}
00055         Range<T> *r;
00056         inline void operator()(const T &x){
00057           r->minVal = std::min(r->minVal,x);
00058           r->maxVal = std::max(r->maxVal,x);
00059         }
00060       };
00063       virtual ~Range(){}
00065       Range():minVal(Type(0)),maxVal(Type(0)){}
00066       
00068       Range(Type minVal, Type maxVal): minVal(minVal), maxVal(maxVal){}
00069   
00071       static inline Range<Type> limits(){
00072         return Range<Type>((std::numeric_limits<Type>::min)(),(std::numeric_limits<Type>::max)());
00073       }
00074 
00076           static inline Range<Type> inv_limits(){
00077         return Range<Type>((std::numeric_limits<Type>::max)(),(std::numeric_limits<Type>::min)());
00078       }
00079       
00081       static inline Range<Type> from(const Type *begin,const Type *end){
00082         Range<Type> r = Range<Type>::limits();
00083         std::swap(r.minVal,r.maxVal);
00084         std::for_each(begin,end,MinMaxFunctor<Type>(&r));
00085         return r;
00086       }
00087   
00089       Type minVal;
00090       
00092       Type maxVal;
00093       
00095       Type getLength() const { return maxVal - minVal; } 
00096       
00098       template<class dstType>
00099       const Range<dstType> castTo() const{
00100         return Range<dstType>(clipped_cast<Type,dstType>(minVal),clipped_cast<Type,dstType>(maxVal));
00101       }
00102       
00104       virtual bool contains(Type value) const { return value >= minVal && value <= maxVal; }
00105       
00106       // camparison operator
00107       bool operator==(const Range<Type> &other) const{
00108         return minVal==other.minVal && maxVal == other.maxVal;
00109       }
00110       
00111       // camparison operator
00112       bool operator!=(const Range<Type> &other) const{
00113         return minVal!=other.minVal || maxVal != other.maxVal;
00114       }
00115       
00117       void extend(const Type &t){
00118         if(t < minVal) minVal = t;
00119         if(t > maxVal) maxVal = t;
00120       }
00121       
00123       void extend(const Range &r){
00124         if(r.minVal < minVal) minVal = r.minVal;
00125         if(r.maxVal > maxVal) maxVal = r.maxVal;
00126       }
00127     };
00128     
00129   #define ICL_INSTANTIATE_DEPTH(D) typedef Range<icl##D> Range##D;
00130     ICL_INSTANTIATE_ALL_DEPTHS
00131   #undef ICL_INSTANTIATE_DEPTH
00132   
00134 
00136     template<class T> ICLUtils_API 
00137     std::ostream &operator<<(std::ostream &s, const Range <T> &range);
00138   
00140     template<class T> ICLUtils_API
00141     std::istream &operator>>(std::istream &s, Range <T> &range);
00142 
00143   } // namespace utils
00144 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines