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/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.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/Range.h> 00034 00035 namespace icl{ 00036 namespace utils{ 00038 00044 template<class Type> 00045 struct SteppingRange : public Range<Type>{ 00047 SteppingRange():Range<Type>(),stepping(0){} 00048 00050 SteppingRange(Type minVal, Type maxVal, Type stepping): 00051 Range<Type>(minVal,maxVal), stepping(stepping){} 00052 00054 Type stepping; 00055 00057 template<class dstType> 00058 const SteppingRange<dstType> castTo() const{ 00059 return SteppingRange<dstType>(clipped_cast<Type,dstType>(Range<Type>::minVal), 00060 clipped_cast<Type,dstType>(Range<Type>::maxVal), 00061 clipped_cast<Type,dstType>(stepping)); 00062 } 00063 00065 virtual bool contains(Type value) const { 00066 if(stepping == 0) return Range<Type>(Range<Type>::minVal,Range<Type>::maxVal).contains(value); 00067 Type offs = value - Range<Type>::minVal; 00068 int n = (int)(offs/stepping); 00069 double n2 = (double)offs/stepping; 00070 return Range<Type>::contains(value) && double(n) == n2; 00071 } 00072 00074 00078 Type nearest(Type value){ 00079 if(value < Range<Type>::minVal) return Range<Type>::minVal; 00080 if(value > Range<Type>::maxVal) return Range<Type>::maxVal; 00081 if(stepping == 0) return value; 00082 Type offs = value - Range<Type>::minVal; 00083 int n = (int)(offs/stepping); 00084 Type k = n*stepping; 00085 if(value-k >= (double)(stepping)/2){ 00086 return k+stepping; 00087 }else{ 00088 return k; 00089 } 00090 } 00091 }; 00092 00094 00096 template<class T> 00097 std::ostream &operator<<(std::ostream &s, const SteppingRange <T> &range); 00098 00100 template<class T> 00101 std::istream &operator>>(std::istream &s, SteppingRange <T> &range); 00102 00103 00104 #define ICL_INSTANTIATE_DEPTH(D) typedef SteppingRange<icl##D> SteppingRange##D; 00105 ICL_INSTANTIATE_ALL_DEPTHS 00106 #undef ICL_INSTANTIATE_DEPTH 00107 00108 00109 } // namespace utils 00110 }