Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

BasicMath.h

Go to the documentation of this file.
00001 00002 00003 00004 00005 00006 00007 00008 00009 #ifndef _ZFXMATH_INCLUDE_BASICMATH_H_ 00010 #define _ZFXMATH_INCLUDE_BASICMATH_H_ 00011 00012 #include <assert.h> 00013 00019 namespace ZFXMath 00020 { 00021 #undef DELTA 00022 #undef E 00023 #undef LOG2_E 00024 #undef LOG10_E 00025 #undef LOGE_2 00026 #undef LOGE_10 00027 #undef PI 00028 #undef SQRT_2 00029 00035 const double EPSILON = 0.00001; 00036 00042 const double E = 2.71828182845904523536; 00043 00049 const double LOG2_E = 1.44269504088896340736; 00050 00056 const double LOG10_E = 0.434294481903251827651; 00057 00063 const double LOGE_2 = 0.693147180559945309417; 00064 00070 const double LOGE_10 = 2.30258509299404568402; 00071 00077 const double PI = 3.14159265358979323846; 00078 00084 const double SQRT_2 = 1.41421356237309504880; 00085 00091 template<class T> inline T RadToDeg(T& rad) 00092 { 00093 // rad * 180° / pi 00094 return rad * 57.295779513082320876798154814105; 00095 } 00096 00102 template<class T> inline T DegToRad(T& degree) 00103 { 00104 // degree * pi / 180° 00105 return degree * 0.017453292519943295769236907684886; 00106 } 00107 00113 template<class T> inline T Sin(const T& rad) 00114 { 00115 return ::sin(rad); 00116 } 00117 00123 template<class T> inline T Cos(const T& rad) 00124 { 00125 return ::cos(rad); 00126 } 00127 00133 template<class T> inline void SinCos( const T& rad, T& retSin, T& retCos ) 00134 { 00135 retSin = Sin( rad ); 00136 retCos = Cos( rad ); 00137 } 00138 00144 template<class T> inline T Tan(const T& rad) 00145 { 00146 return ::tan(rad); 00147 } 00148 00154 template<class T> inline T ASin(const T& value) 00155 { 00156 return ::asin(value); 00157 } 00158 00164 template<class T> inline T ACos(const T& value) 00165 { 00166 return ::acos(value); 00167 } 00168 00174 template<class T> inline T ATan(const T& value) 00175 { 00176 return ::atan(value); 00177 } 00178 00184 template<class T> inline T Sqrt(const T& value) 00185 { 00186 return ::sqrt(value); 00187 } 00188 00194 template<class T> inline T Pow2(const T& base) 00195 { 00196 return base*base; 00197 } 00198 00204 template<class T> inline T Pow(const T& base, const T& exp) 00205 { 00206 return ::pow(base,exp); 00207 } 00208 00214 template<class T> inline T Mod(const T& value1, const T& value2) 00215 { 00216 return ::fmod(value1,value2); 00217 //return ( value1 - RoundDown(value1 / value2) * value2 ); 00218 } 00219 00225 template<class T> inline T Abs(const T& value) 00226 { 00227 return ((value < 0) ? -value : value); 00228 } 00229 00235 template<class T> inline T LogE(T& num) 00236 { 00237 return ::log(num); 00238 } 00239 00245 template<class T> inline T Log10(T& num) 00246 { 00247 return ::log10(num); 00248 } 00249 00255 template<class T> inline T Log(T& base, T& num) 00256 { 00257 return ( ::log(num) / ::log(base) ); 00258 } 00259 00265 template<class T> inline int Round(T& value) 00266 { 00267 return (int)(d<0 ? d-0.5 : d+0.5); 00268 } 00269 00279 template<class T> inline T Round(T& value, int digits) 00280 { 00281 assert(digits >= -20 && digits <= 20); 00282 T v[] = { -1e20, -1e19, -1e18, -1e17, -1e16, -1e15, -1e14, -1e13, -1e12, 00283 -1e11, -1e10, -1e9, -1e8, -1e7, -1e6, -1e5, -1e4, -1e3, -1e2, 00284 -10, 1, 10, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 00285 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20 }; 00286 return RoundDown(value / v[digits+20] + 0.5) * v[digits+20]; 00287 } 00288 00294 template<class T> inline int RoundUp(T& value) 00295 { 00296 //if(value - RoundDown(value) == 0) 00297 // return (int)value; 00298 //else return (int)(value+1); 00299 return (int)::ceil(value); 00300 } 00301 00307 template<class T> inline T RoundDown(T& value) 00308 { 00309 return (int)value; 00310 } 00311 00317 template <class T> inline const bool IsEven(const T& value) 00318 { 00319 return (!(value&1)); 00320 } 00321 00327 template<class T> inline const T& Min(const T& value1, const T& value2) 00328 { 00329 return ( (value1 < value2) ? value1 : value2 ); 00330 } 00331 00337 template<class T> inline const T& Max(const T& value1, const T& value2) 00338 { 00339 return ( (value1 > value2) ? value1 : value2 ); 00340 } 00341 00347 template<class T> inline const T& Clamp(const T& value, const T& min, const T& max) 00348 { 00349 return ( (value < min) ? min : ( (value > max) ? max : value) ); 00350 } 00351 00357 template<class T> inline bool NearTo( const T& value, const T& nearto ) 00358 { 00359 return Abs( nearto - value ) <= EPSILON; 00360 } 00361 00367 template<class T> inline T Sign(const T& value) 00368 { 00369 return (T)((t < 0) ? (-1) : (t > 0 ? 1 : 0)); 00370 } 00371 00377 template<class T1, class T2> inline T1 Interpolate( const T1& v1, const T1& v2, const T2& lerp ) 00378 { 00379 return v1 + ( v2 - v1 ) * lerp; 00380 } 00381 00387 template<class T> 00388 T Fac( T value ) 00389 { 00390 assert( value >= 0 ); 00391 T res = 1; 00392 while( value > 1 ) 00393 { 00394 res *= value; 00395 value--; 00396 } 00397 return res; 00398 } 00399 00400 00406 template<class T> 00407 T Frac( T value ) 00408 { 00409 return value - RoundDown( value ); 00410 } 00411 00417 double Noise( unsigned int x ) 00418 { 00419 x = ( x << 13 ) ^ x; 00420 return ( 1.0 - ( ( x * ( x * x * 15731 + 789221 ) + 1376312589) & 0x7FFFFFFF ) / 1073741824.0 ); 00421 } 00422 00423 00429 double Noise( unsigned int x, unsigned int y ) 00430 { 00431 unsigned int n = x + y * 57; 00432 n = ( n << 13 ) ^ n; 00433 unsigned int result = ( ( n * ( n * n * 15731 + 789221 ) + 1376312589 ) & 0x7FFFFFFF); 00434 00435 return ( 1.0 - double(result) / 1073741824.0 ); 00436 } 00437 00443 double SmoothNoise( unsigned int x ) 00444 { 00445 double sides = ( Noise( x - 1 ) + Noise( x + 1 ) ) / 4.0; 00446 double center = Noise( x ) / 2.0; 00447 return sides + center; 00448 } 00449 00455 double SmoothNoise( unsigned int x, unsigned int y ) 00456 { 00457 double corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16.0; 00458 double sides = ( Noise(x-1, y) +Noise(x+1, y) +Noise(x, y-1) +Noise(x, y+1) ) / 8.0; 00459 double center = Noise(x, y) / 4.0; 00460 return corners + sides + center; 00461 } 00462 00468 double InterpolatedNoise( double x, double y ) 00469 { 00470 int integer_X = int(x); 00471 double fractional_X = x - integer_X; 00472 00473 int integer_Y = int(y); 00474 double fractional_Y = y - integer_Y; 00475 00476 double v1 = Noise( integer_X, integer_Y ); 00477 double v2 = Noise( integer_X + 1, integer_Y); 00478 double v3 = Noise( integer_X, integer_Y + 1 ); 00479 double v4 = Noise( integer_X + 1, integer_Y + 1 ); 00480 00481 double i1 = Interpolate( v1, v2, fractional_X ); 00482 double i2 = Interpolate( v3, v4, fractional_X ); 00483 00484 return Interpolate( i1, i2, fractional_Y ); 00485 } 00486 00492 double SmoothInterpolatedNoise( double x, double y ) 00493 { 00494 int integer_X = int(x); 00495 double fractional_X = x - integer_X; 00496 00497 int integer_Y = int(y); 00498 double fractional_Y = y - integer_Y; 00499 00500 double v1 = SmoothNoise( integer_X, integer_Y ); 00501 double v2 = SmoothNoise( integer_X + 1, integer_Y); 00502 double v3 = SmoothNoise( integer_X, integer_Y + 1 ); 00503 double v4 = SmoothNoise( integer_X + 1, integer_Y + 1 ); 00504 00505 double i1 = Interpolate( v1, v2, fractional_X ); 00506 double i2 = Interpolate( v3, v4, fractional_X ); 00507 00508 return Interpolate( i1, i2, fractional_Y ); 00509 } 00510 00511 00512 00513 struct ARGB 00514 { 00515 union 00516 { 00517 unsigned long argb; 00518 struct 00519 { 00520 #ifdef LITTLE_ENDIAN 00521 unsigned char B : 8; 00522 unsigned char G : 8; 00523 unsigned char R : 8; 00524 unsigned char A : 8; 00525 #else 00526 unsigned char A : 8; 00527 unsigned char R : 8; 00528 unsigned char G : 8; 00529 unsigned char B : 8; 00530 #endif 00531 }; 00532 }; 00533 00534 ARGB() {} 00535 00536 template<class T> 00537 ARGB( const T& r, const T& g, const T& b, const T& a ) 00538 { 00539 R = (unsigned char) ( r * 255.0 ); 00540 G = (unsigned char) ( g * 255.0 ); 00541 B = (unsigned char) ( b * 255.0 ); 00542 A = (unsigned char) ( a * 255.0 ); 00543 } 00544 00545 ARGB( unsigned char r, unsigned char g, unsigned char b, unsigned char a ) 00546 { 00547 R = r; 00548 G = g; 00549 B = b; 00550 A = a; 00551 } 00552 00553 operator unsigned long () { return argb; } 00554 operator const unsigned long () const { return argb; } 00555 00556 }; 00557 00558 00559 } 00560 00561 #endif //_ZFXMATH_INCLUDE_BASICMATH_H_ 00562 00563

Generated on Thu Nov 25 04:02:58 2004 for ZFX-Math Library by doxygen 1.3.8