Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATUtil.h Source File

CATUtil.h

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATUtil.h
00003 /// \brief Defines basic utility functions
00004 /// \ingroup CAT
00005 ///
00006 /// Copyright (c) 2003-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-23 06:02:38 -0600 (Wed, 23 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //---------------------------------------------------------------------------
00014 #ifndef CATUtil_H_
00015 #define CATUtil_H_
00016 
00017 // limits.h allows us to assert on floats that are too large to round
00018 // into integer types
00019 #include <limits.h>
00020 #include <math.h>
00021 
00022 enum CATVALUE_TYPE
00023 {
00024     CATVALUE_LINEAR,
00025     CATVALUE_DB
00026 };
00027 
00028 const CATFloat32 kCATPI       = (CATFloat32)(3.1415926535897932384626433832795);
00029 const CATFloat32 kCATPI_2     = kCATPI/2;
00030 const CATFloat32 kCAT2_PI     = kCATPI*2;
00031 const CATFloat32 kCATDEGTORAD = (CATFloat32)(kCATPI / 180.0);
00032 const CATFloat32 kCATRADTODEG = (CATFloat32)(180.0 / kCATPI);
00033 const CATFloat32 kCATINV2PI   = (CATFloat32)(1.0 / ( 8 * 0.78539816339744830961566084581988));
00034 
00035 /// CATMin returns the minimum of two values. Types must be the same.
00036 template<class T>
00037 inline T const& CATMin(T const& a, T const& b)
00038 {
00039    // return minimum
00040    return a < b ? a : b;
00041 }
00042 
00043 /// CATMax returns the maximum of two values. Types must be the same.
00044 template<class T>
00045 inline T const& CATMax(T const& a, T const& b)
00046 {
00047    // return maximum
00048    return a > b ? a : b;
00049 }
00050 
00051 /// CATSwap swaps to values of the same type
00052 template<class T>
00053 inline void CATSwap(T& a, T& b)
00054 {
00055    T c = a;
00056    a = b;
00057    b = c;
00058 }
00059 
00060 /// CATAbs finds the absolute value
00061 template<class T>
00062 inline T CATAbs(T const& a)
00063 {
00064    if (a < 0)
00065       return -a;
00066    return a;
00067 }
00068 
00069 /// CATAbsDiff returns the absolute difference between values
00070 template<class T>
00071 inline T CATAbsDiff(T const& a, T const& b)
00072 {
00073    T c = a - b;
00074    if (c < 0)
00075       return -c;
00076    return c;
00077 }
00078 
00079 /// Round a floating point to the nearest integer
00080 /// Assumption: float must be within valid integer range.
00081 /// \param floatVal - floating point value to round. 
00082 ///        Must be within integer range.
00083 /// \return CATInt32 - floatVal rounded to nearest integer
00084 inline CATInt32 CATRound(CATFloat32 floatVal)
00085 {
00086    CATASSERT( (floatVal >= INT_MIN) && (floatVal <= INT_MAX),
00087              "Value is too large to round to an integer.");
00088 
00089    if (floatVal < 0) 
00090    {
00091       return (CATInt32)(floatVal - 0.5f);
00092    }
00093    else
00094    {
00095       return (CATInt32)(floatVal + 0.5f);
00096    }   
00097 }  
00098 
00099 /// Round a double-precision floating point to the nearest integer
00100 /// Assumption: float must be within valid integer range.
00101 /// \param dblVal - double-precision floating point value to round. 
00102 ///        Must be within integer range.
00103 /// \return CATInt32 - dblVal rounded to nearest integer
00104 inline CATInt32 CATRound(CATFloat64 dblVal)
00105 {
00106    CATASSERT( (dblVal >= INT_MIN) && (dblVal <= INT_MAX),
00107              "CATRound returns an integer, so the passed in value " \
00108              "to round must be within range for an int. It isn't.");
00109 
00110    if (dblVal < 0) 
00111    {
00112       return (CATInt32)(dblVal - 0.5);
00113    }
00114    else
00115    {
00116       return (CATInt32)(dblVal + 0.5);
00117    }   
00118 }  
00119 
00120 inline CATFloat32 CATConstrainAngle( CATFloat32 angle)
00121 {
00122    int k = (int)(angle * kCATINV2PI);
00123    return angle - ((CATFloat32)k * kCAT2_PI);   
00124 }
00125 
00126 inline CATFloat64 CATConstrainAngle( CATFloat64 angle)
00127 {
00128    int k = (int)(angle * kCATINV2PI);
00129    return angle - ((CATFloat32)k * kCAT2_PI);
00130 }
00131 
00132 /// CATModFloat() performs 'retVal = modFloat % modBase', keeping the floating point 
00133 /// fraction in retVal.  e.g.  CATModFloat( 7.5, 3 )  would return 1.5.
00134 inline CATFloat32 CATModFloat (CATFloat32 modFloat, CATUInt32 modBase)
00135 {
00136     CATUInt32 tmpVal = (CATUInt32)modFloat;
00137     CATFloat32 diff   = modFloat - tmpVal;
00138     tmpVal = tmpVal % modBase;
00139     CATFloat32 result = (CATFloat32)tmpVal + diff;
00140     CATASSERT(result < modBase, "Huh... that shouldn't happen...");
00141     return result;
00142 }
00143 
00144 inline CATFloat64 CATModFloat (CATFloat64 modFloat, CATUInt32 modBase)
00145 {
00146     CATUInt32 tmpVal = (CATUInt32)modFloat;
00147     CATFloat64 diff   = modFloat - tmpVal;
00148     tmpVal = tmpVal % modBase;
00149     
00150     CATFloat64 result = (CATFloat64)tmpVal + diff;
00151     CATASSERT(result < modBase, "Huh... that shouldn't happen...");
00152     return result;
00153 }
00154 
00155 // Find the greatest common denominator with Euclid's method
00156 inline CATUInt32 CATGreatestCommonDenominator( CATUInt32 a, CATUInt32 b)
00157 {
00158     if (b == 0)
00159     {
00160         CATASSERT(false,"Greatest common denominator doesn't work with 0...");
00161         return 0;
00162     }
00163 
00164     while (a > 0)
00165     {
00166         if (a < b) CATSwap(a,b);
00167         a = a - b;
00168     }
00169 
00170     return b;
00171 }
00172 
00173 inline CATInt32 CATGreatestCommonDenominator( CATInt32 a, CATInt32 b)
00174 {
00175     if (b == 0)
00176     {
00177         CATASSERT(false,"Greatest common denominator doesn't work with 0...");
00178         return 0;
00179     }
00180 
00181     while (a > 0)
00182     {
00183         if (a < b) CATSwap(a,b);
00184         a = a - b;
00185     }
00186 
00187     return b;
00188 }
00189 
00190 // Find the lowest common multiple
00191 inline CATUInt32 CATLowestCommonMultiple( CATUInt32 a, CATUInt32 b)
00192 {
00193     if (b == 0)
00194     {
00195         CATASSERT(false,"Lowest common multiple doesn't like 0.");
00196         return 0;
00197     }
00198 
00199     CATUInt32 gcd = CATGreatestCommonDenominator(a,b);  
00200     return (a*b) / gcd;
00201 }
00202 
00203 inline CATInt32 CATLowestCommonMultiple( CATInt32 a, CATInt32 b)
00204 {
00205     if (b == 0)
00206     {
00207         CATASSERT(false,"Lowest common multiple doesn't like 0.");
00208         return 0;
00209     }
00210 
00211     CATInt32 gcd = CATGreatestCommonDenominator(a,b);
00212     return (a*b) / gcd;
00213 }
00214 
00215 /// Swap endian of value if and only if we're on a little-endian machine
00216 inline CATInt32 CATSwapLittleEndian( CATInt32 a )
00217 {
00218 #ifdef CAT_LITTLE_ENDIAN
00219        a =    ( ((a & 0xFF) << 24)    | 
00220                 ((a & 0xff00) << 8)  | 
00221                 ((a & 0xff0000) >> 8) |
00222                 ((a & 0xff000000) >> 24));
00223 #endif
00224 
00225     return a;
00226 }
00227 
00228 inline CATUInt32 CATSwapLittleEndian( CATUInt32 a )
00229 {
00230 #ifdef CAT_LITTLE_ENDIAN
00231        a =    ( ((a & 0xFF) << 24)    | 
00232                 ((a & 0xff00) << 8)  | 
00233                 ((a & 0xff0000) >> 8) |
00234                 ((a & 0xff000000) >> 24));
00235 #endif
00236 
00237     return a;
00238 }
00239 
00240 
00241 inline CATUInt16 CATSwapLittleEndian( CATUInt16 a )
00242 {
00243 #ifdef CAT_LITTLE_ENDIAN
00244     a =    ( ((a & 0xFF) << 8)    | 
00245                 ((a & 0xff00) >> 8));
00246 #endif
00247 
00248     return a;
00249 }
00250 
00251 #endif // CATUtil_H_
00252 
00253 

Generated on Mon Feb 11 04:09:49 2008 for Game Accessibility Suite by doxygen 1.5.4