range.h

Go to the documentation of this file.
00001 /**
00002  * \file range.h <mi32/range.h>
00003  * \brief Definitions for RANGE template
00004  *
00005  * \if NODOC
00006  * $Id: range.h_v 1.10 2004/03/31 18:46:21 mju Exp $
00007  *
00008  * $Log: range.h_v $
00009  * Revision 1.10  2004/03/31 18:46:21  mju
00010  * Add getSize.
00011  *
00012  * Revision 1.9  2004/02/18 20:17:15  scowan
00013  * Added limit method.
00014  *
00015  * Revision 1.8  2003/09/15 13:49:56  fileserver!dwilliss
00016  * Doxygen
00017  *
00018  * Revision 1.7  2003/04/09 19:49:19  dwilliss
00019  * Don't use _T in templates.  Mac's ctype.h defines a global _T. Thanks Apple!
00020  *
00021  * Revision 1.6  2001/10/26 21:46:38  mju
00022  * Add Set, SetMinimum and SetMaximum methods.
00023  *
00024  * Revision 1.5  2001/07/18 19:38:01  mju
00025  * Use 'minval' and 'maxval' instead of 'min' and 'max' as those are #defined as macros by Windows headers.
00026  *
00027  * Revision 1.4  2000/07/07 13:39:57  mju
00028  * Don't use limits.h.
00029  *
00030  * Revision 1.3  2000/06/28 20:27:25  mju
00031  * Use limits.h for Unix.
00032  *
00033  * Revision 1.2  2000/06/26 20:16:49  mju
00034  * Add ctor from separate min/max values.
00035  *
00036  * Revision 1.1  2000/06/26 18:06:09  mju
00037  * Initial revision
00038  *
00039  * \endif
00040 **/
00041 
00042 #ifndef  INC_MI32_RANGE_H
00043 #define  INC_MI32_RANGE_H
00044 
00045 #ifndef  INC_MI32_STDDEFNS_H
00046 #include <mi32/stddefns.h>
00047 #endif
00048 
00049 #ifndef  INC_LIMITS
00050 #include <limits>
00051 #define  INC_LIMITS
00052 #endif
00053 
00054 //! Template for storing numeric range.
00055 //! Designed to work with signed integer and floating-point data types, unsigned types are not supported.
00056 template <class _CT> class RANGE {
00057    public:
00058 
00059       //! Default constructor, initialize to invalid (empty) range.
00060       RANGE (
00061          ) {
00062          SetInvalid();
00063          }
00064 
00065       //! Construct range with specified minimum and maximum values.
00066       RANGE (
00067          _CT minval,
00068          _CT maxval
00069          ): m_min(minval), m_max(maxval)
00070          { }
00071 
00072       //! Construct range to include only specified value.
00073       explicit RANGE (
00074          _CT value
00075          ) : m_min(value), m_max(value)
00076          { }
00077 
00078       //! Equality operator.
00079       bool operator== (
00080          const RANGE& rhs
00081          ) const {
00082          return (m_min == rhs.m_min && m_max == rhs.m_max);
00083          }
00084 
00085       //! Inequality operator.
00086       bool operator!= (
00087          const RANGE& rhs
00088          ) const {
00089          return (!(*this == rhs));
00090          }
00091 
00092       //! Determine if range contains specified value.
00093       bool Contains (
00094          _CT value
00095          ) const {
00096          return (value >= m_min && value <= m_max);
00097          }
00098 
00099       //! Determine if range containes specified range.
00100       bool Contains (
00101          const RANGE& rhs
00102          ) const {
00103          return (rhs.m_min >= m_min && rhs.m_max <= m_max);
00104          }
00105 
00106       //! Expand range by specified amount.
00107       void Expand (
00108          _CT value
00109          ) {
00110          m_min -= value;
00111          m_max += value;
00112          return;
00113          }
00114 
00115       //! Extend range to include specified value.
00116       void Extend (
00117          _CT value
00118          ) {
00119          if (value < m_min) m_min = value;
00120          if (value > m_max) m_max = value;
00121          return;
00122          }
00123 
00124       //! Extend range to include specified range.
00125       void Extend (
00126          _CT minval,
00127          _CT maxval
00128          ) {
00129          if (minval < m_min) m_min = minval;
00130          if (maxval > m_max) m_max = maxval;
00131          return;
00132          }
00133 
00134       //! Extend range to include specified range.
00135       void Extend (
00136          const RANGE& rhs
00137          ) {
00138          if (rhs.m_min < m_min) m_min = rhs.m_min;
00139          if (rhs.m_max > m_max) m_max = rhs.m_max;
00140          return;
00141          }
00142 
00143       //! Get center of range.
00144       _CT GetCenter (
00145          ) const {
00146          return ((m_min + m_max) / 2);
00147          }
00148 
00149       //! Get maximum value in range.
00150       _CT GetMaximum (
00151          ) const {
00152          return (m_max);
00153          }
00154 
00155       //! Get minimum value in range.
00156       _CT GetMinimum (
00157          ) const {
00158          return (m_min);
00159          }
00160 
00161       //! Get 'size' of range.
00162       _CT GetSize (
00163          ) const { return (m_max - m_min); }
00164 
00165       //! Intersect range with another range.
00166       void Intersect (
00167          const RANGE& rhs
00168          ) {
00169          if (rhs.IsValid()) {
00170             if (rhs.m_min > m_min) m_min = rhs.m_min;
00171             if (rhs.m_max < m_max) m_max = rhs.m_max;
00172             }
00173          else {
00174             *this = rhs;
00175             }
00176          return;
00177          }
00178 
00179       //! Determine if range is valid.
00180       bool IsValid (
00181          ) const {
00182          return (m_min <= m_max);
00183          }
00184          
00185       //! Limit the value based on the current range
00186       //! Can be used to replace the bound() macro
00187       //! @return Limit'ed value based on the range
00188       _CT Limit (
00189          _CT value
00190          ) const {
00191          return ((value < m_min) ? m_min : ((value > m_max) ? m_max : value));
00192          }
00193 
00194       //! Determine if range overlaps another range.
00195       bool Overlaps (
00196          const RANGE& rhs
00197          ) const {
00198          return (!(rhs.m_max < m_min || rhs.m_max > m_min));
00199          }
00200 
00201       //! Set range to be "invalid" (empty).
00202       void SetInvalid (
00203          ) {
00204          m_min = std::numeric_limits<_CT>::max();
00205          m_max = -m_min;
00206          return;
00207          }
00208 
00209       //! Set minimum and maximum values.
00210       void Set (
00211          _CT minval,
00212          _CT maxval
00213          ) { m_min = minval; m_max = maxval; }
00214 
00215       //! Set maximum value.
00216       void SetMaximum (
00217          _CT val
00218          ) { m_max = val; }
00219 
00220       //! Set minimum value.
00221       void SetMinimum (
00222          _CT val
00223          ) { m_min = val; }
00224 
00225    private:
00226       #ifndef GENERATING_DOXYGEN_OUTPUT
00227       _CT m_min;                             //!< Minimum value in range
00228       _CT m_max;                             //!< Maximum value in range
00229       #endif // GENERATING_DOXYGEN_OUTPUT
00230 
00231    };
00232 
00233 typedef RANGE<INT16> INT16_RANGE;
00234 typedef RANGE<INT32> INT32_RANGE;
00235 typedef RANGE<float> FLOAT_RANGE;
00236 typedef RANGE<double> DOUBLE_RANGE;
00237 
00238 #endif   //!< INC_MI32_RANGE_H

Generated on Wed May 31 15:27:03 2006 for TNTsdk by  doxygen 1.3.8-20040913