Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SteppingRange.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/SteppingRange.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/Range.h>
00035 
00036 namespace icl{
00037   namespace utils{
00039 
00045     template<class Type> 
00046     struct SteppingRange : public Range<Type>{
00048       SteppingRange():Range<Type>(),stepping(0){}
00049       
00051       SteppingRange(Type minVal, Type maxVal, Type stepping): 
00052         Range<Type>(minVal,maxVal), stepping(stepping){}
00053   
00055       Type stepping;
00056       
00058       template<class dstType>
00059       const SteppingRange<dstType> castTo() const{
00060         return SteppingRange<dstType>(clipped_cast<Type,dstType>(Range<Type>::minVal),
00061                                       clipped_cast<Type,dstType>(Range<Type>::maxVal),
00062                                       clipped_cast<Type,dstType>(stepping));
00063       }
00064       
00066       virtual bool contains(Type value) const { 
00067         if(stepping == 0) return Range<Type>(Range<Type>::minVal,Range<Type>::maxVal).contains(value);
00068         Type offs = value - Range<Type>::minVal;
00069         int n = (int)(offs/stepping);
00070         double n2 = (double)offs/stepping;
00071         return Range<Type>::contains(value) && double(n) == n2;
00072       }
00073       
00075 
00079       Type nearest(Type value){
00080         if(value < Range<Type>::minVal) return Range<Type>::minVal;
00081         if(value > Range<Type>::maxVal) return Range<Type>::maxVal;
00082         if(stepping == 0) return value;
00083         Type offs = value - Range<Type>::minVal;
00084         int n = (int)(offs/stepping);
00085         Type k = n*stepping;
00086         if(value-k >= (double)(stepping)/2){
00087           return k+stepping;
00088         }else{
00089           return k;
00090         }
00091       }    
00092     };
00093     
00095 
00097     template<class T> ICLUtils_API
00098     std::ostream &operator<<(std::ostream &s, const SteppingRange <T> &range);
00099   
00101     template<class T> ICLUtils_API
00102     std::istream &operator>>(std::istream &s, SteppingRange <T> &range);
00103   
00104   
00105   #define ICL_INSTANTIATE_DEPTH(D) typedef SteppingRange<icl##D> SteppingRange##D;
00106     ICL_INSTANTIATE_ALL_DEPTHS
00107   #undef ICL_INSTANTIATE_DEPTH
00108 
00109   } // namespace utils
00110 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines