00001
00002 #ifndef __scalar_hpp__
00003 #define __scalar_hpp__
00004
00005 #include <math.h>
00006
00007 #include <crbn/basic/basic.h>
00008
00009 inline float sqr( float f )
00010 {
00011 return f * f;
00012 }
00013
00014 inline float radians( float x )
00015 {
00016 return 0.0174532925199432957692369076848f * x;
00017 }
00018
00019 inline float degrees( float x )
00020 {
00021 return 57.295779513082320876798154814114f * x;
00022 }
00023
00024 inline float sign( float x )
00025 {
00026 return (x>0.0f)?1.0f:((x<0.0f)?-1.0f:0.0f);
00027 }
00028
00029 inline int32 sign( int32 x )
00030 {
00031 return (x>0)?1:((x<0)?-1:0);
00032 }
00033
00034 inline float floor( float x )
00035 {
00036 return (float)( (int)x - (x < 0.0f) );
00037 }
00038
00039 inline float ceil( float x )
00040 {
00041 return (int)x + ( (x > 0.0f) && (x != (int)x) );
00042 }
00043
00044 inline float fract( float x )
00045 {
00046 return x - floor( x );
00047 }
00048
00049 inline float min( float x, float y )
00050 {
00051 return (x<y)?x:y;
00052 }
00053
00054 inline int32 min( int32 x, int32 y )
00055 {
00056 return (x<y)?x:y;
00057 }
00058
00059 inline float max( float x, float y )
00060 {
00061 return (x>y)?x:y;
00062 }
00063
00064 inline int32 max( int32 x, int32 y )
00065 {
00066 return (x>y)?x:y;
00067 }
00068
00069 inline float clamp( float x, float minVal, float maxVal )
00070 {
00071 return (x>maxVal)?maxVal:((x<minVal)?minVal:x);
00072 }
00073
00074 inline float step( float a, float x )
00075 {
00076 return (float)( x >= a );
00077 }
00078
00079 inline float pulse( float a, float b, float x )
00080 {
00081 return step( a ,x ) - step( b, x );
00082 }
00083
00084 inline float lerp( float t, float a, float b )
00085 {
00086 return a + t * (b - a);
00087 }
00088
00089 inline float boxstep( float a, float b, float x )
00090 {
00091 return clamp( ( x - a ) / ( b - a ), 0.0f, 1.0f );
00092 }
00093
00094 inline float smoothstep( float a, float b, float x )
00095 {
00096 if( x < a )
00097 return 0.0f;
00098 if( x > b )
00099 return 1.0f;
00100
00101 x = ( x - a ) / ( b - a );
00102 return ( x * x * ( 3.0f - 2.0f * x ) );
00103 }
00104
00105 inline float mod( float a, float b )
00106 {
00107 int32 n = (int32)( a / b );
00108
00109 a -= n * b;
00110 if( a < 0 )
00111 a += b;
00112
00113 return a;
00114 }
00115
00116 #endif // __scalar_hpp__