00001 /*!@file Image/Range.H Manage min/max range information */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters@klab.caltech.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/Range.H $ 00035 // $Id: Range.H 14664 2011-03-31 23:21:21Z dberg $ 00036 // 00037 00038 #ifndef IMAGE_RANGE_H_DEFINED 00039 #define IMAGE_RANGE_H_DEFINED 00040 00041 #include <limits> // for std::numeric_limits 00042 #include <string> 00043 00044 //! A class for managing min/max range information. 00045 /*! This class is primarily intended to work with algorithms that 00046 manipulate the dynamic range of Image<T>'s. For example, it can help to 00047 specify how an image should have its current dynamic range remapped to 00048 a new range (see remapRange() in Image_MathOps.H). In addition, the 00049 Range<T> class provides an operator() that allows it to be used as a 00050 functor in generic algorithms for accumulating the min/max over a 00051 sequence of elements (as in rangeOf() in Image_MathOps.H). */ 00052 template <class T> 00053 class Range 00054 { 00055 private: 00056 T mini, maxi; 00057 00058 public: 00059 //! Default constructor. 00060 /*! We initialize the object's min element with type T's <b>max</b> 00061 value, and we initialize the object's max element with type T's 00062 <b>min</b> value. This seeming paradoxical situation is in fact "The 00063 Right Thing" if we want to accumulate the min/max over a range of 00064 elements. The starting condition of the Range object should make the 00065 weakest possible assumption about the values that will be observed; 00066 and this weakest assumption is precisely to start with the min being 00067 as large as possible, and the max being as small as possible. */ 00068 inline Range(); 00069 00070 //! Construct with given min and max values. 00071 inline Range(const T& mi, const T& ma); 00072 00073 //! Get the current min value. 00074 const T& min() const { return mini; } 00075 00076 //! Get the current max value. 00077 const T& max() const { return maxi; } 00078 00079 //! Get the range (i.e. the difference of max minus min). 00080 T range() const { return maxi - mini; } 00081 00082 //! Set the range's min value. 00083 void setMin(const T& mi) { mini = mi; } 00084 00085 //! Set the range's max value. 00086 void setMax(const T& ma) { maxi = ma; } 00087 00088 //! Update our min value if val is less than the current max. 00089 void mergeMin(const T& v); 00090 00091 //! Update our max value if val is greater than the current max. 00092 void mergeMax(const T& val); 00093 00094 //! Update our min or max value if val falls outside of the current range. 00095 void merge(const T& val); 00096 00097 //! This operator() allows straightforward use in generic algorithms. 00098 /*! In particular, this is useful for accumulating the min/max over a 00099 range of elements. For example, rangeOf() could be implemented like 00100 this: 00101 00102 \code 00103 Image<T> img; 00104 Range<T> rng = std::for_each(img.begin(), img.end(), Range<T>()); 00105 \endcode 00106 00107 We construct a default Range<T> object, which is copied and passed to 00108 for_each(), which in turn calls operator() on that range object for 00109 each element in the iterator sequence that is visited. 00110 */ 00111 void operator()(const T& v); 00112 00113 //! Merge other's min and max values into ours. 00114 void merge(const Range<T>& other); 00115 00116 //! See if two Range are the same. 00117 bool operator==(const Range& x) const throw(); 00118 00119 //! See if two Range are different. 00120 bool operator!=(const Range& x) const throw(); 00121 }; 00122 00123 //convert from a Range<T> to a string 00124 template<typename T> std::string convertToString(const Range<T>& val); 00125 00126 //convert from a string to a Range<T> 00127 template<typename T> void convertFromString(const std::string& str, Range<T>& val); 00128 00129 00130 // ###################################################################### 00131 template <class T> inline 00132 Range<T>::Range() : 00133 mini(std::numeric_limits<T>::max()), 00134 maxi(std::numeric_limits<T>::min()) 00135 { 00136 if (maxi > 0.0) maxi = -std::numeric_limits<T>::max(); 00137 } 00138 00139 // ###################################################################### 00140 template <class T> inline 00141 Range<T>::Range(const T& mi, const T& ma) : 00142 mini(mi), maxi(ma) 00143 {} 00144 00145 // ###################################################################### 00146 template <class T> inline 00147 void Range<T>::mergeMin(const T& v) { if (v < mini) mini = v; } 00148 00149 // ###################################################################### 00150 template <class T> inline 00151 void Range<T>::mergeMax(const T& v) { if (v > maxi) maxi = v; } 00152 00153 // ###################################################################### 00154 template <class T> inline 00155 void Range<T>::merge(const T& v) 00156 { 00157 mergeMin(v); 00158 mergeMax(v); 00159 } 00160 00161 // ###################################################################### 00162 template <class T> inline 00163 void Range<T>::operator()(const T& v) 00164 { 00165 merge(v); 00166 } 00167 00168 // ###################################################################### 00169 template <class T> inline 00170 void Range<T>::merge(const Range<T>& other) 00171 { 00172 mergeMin(other.min()); 00173 mergeMax(other.max()); 00174 } 00175 00176 // ###################################################################### 00177 template <class T> inline 00178 bool Range<T>::operator==(const Range<T>& rhs) const throw() 00179 { 00180 return ( (min() == rhs.min()) && (max() == rhs.max()) ); 00181 } 00182 00183 // ###################################################################### 00184 template <class T> inline 00185 bool Range<T>::operator!=(const Range<T>& rhs) const throw() 00186 { 00187 return !operator==(rhs); 00188 } 00189 00190 // ###################################################################### 00191 /* So things look consistent in everyone's emacs... */ 00192 /* Local Variables: */ 00193 /* mode: c++ */ 00194 /* indent-tabs-mode: nil */ 00195 /* End: */ 00196 00197 #endif // IMAGE_RANGE_H_DEFINED