00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CATUtil_H_
00015 #define CATUtil_H_
00016
00017
00018
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
00036 template<class T>
00037 inline T const& CATMin(T const& a, T const& b)
00038 {
00039
00040 return a < b ? a : b;
00041 }
00042
00043
00044 template<class T>
00045 inline T const& CATMax(T const& a, T const& b)
00046 {
00047
00048 return a > b ? a : b;
00049 }
00050
00051
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
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
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
00080
00081
00082
00083
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
00100
00101
00102
00103
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
00133
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
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
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
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