base_funcs2.hh

Go to the documentation of this file.
00001 /*
00011  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00012  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00013  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00014  *****************************************************************************/
00015 
00016 #ifndef BASE_FUNCS2_HH
00017 #define BASE_FUNCS2_HH 1
00018 
00019 namespace base {
00020 
00024 
00028 
00031 INLINE bool FEQ( fp a, fp b ) { return ABS(a-b) < 0.001; }
00032 INLINE bool FNE( fp a, fp b ) { return ABS(a-b) > 0.001; }
00033 INLINE bool FEQ_FLOAT_LARGE( fp f ) { return ABS(f-FLOAT_LARGE) < 100.0; }
00034 INLINE bool FEQ_FLOAT_SMALL( fp f ) { return ABS(f-FLOAT_SMALL) < 100.0; }
00035 
00040 template<typename FP>
00041 bool
00042 FEQ( const FP a, const FP b )
00043 {
00044     const FP diff = a - b;
00045     const FP scale = 1000.0;
00046     const uint64 tolerance = 5;  // 5 = 1000.0 * 0.005
00047     return uint64(ABS(diff * scale)) <= tolerance;
00048 }
00049 
00051 template<typename FP>
00052 bool
00053 FEQ( const FP a, const FP b, const uint fracDigits )
00054 {
00055     const FP diff = a - b;
00056     const FP scale = pow( double(10.0), double(fracDigits) );
00057     const uint64 tolerance = uint64(scale * 0.005);
00058     return uint64(ABS(diff * scale)) <= tolerance;
00059 }
00060 
00064 INLINE int
00065 Clamp( int i, int limit )
00066 {
00067     if ( UX( i < 0 ) )
00068         return 0;
00069     else if ( UX( i > limit ) )
00070         return limit;
00071     else
00072         return i;
00073 }
00074 
00079 INLINE uint8
00080 ClampFF( uint i )
00081 {
00082     // gcc 4.x does inline an optimal x86 CMOV instruction.
00083     if ( EX(i < 0xff) )
00084         return uint8(i);
00085     else
00086         return 0xff;
00087 }
00088 
00089 INLINE uint8
00090 ClampFF( int i )
00091 {
00092     if ( i < 0 )
00093         return 0;
00094     else if ( i > 0xff )
00095         return 0xff;
00096     else
00097         return uint8(i);
00098 }
00099 
00100 INLINE uint8
00101 ClampFF( fp f )
00102 {
00103     if ( f >= 1.0 )
00104         return 0xff;
00105     else if ( f < 0.0 )
00106         return 0;
00107     else
00108         return uint8( f * 256.0 );
00109 }
00110 
00114 INLINE fp
00115 Clamp1( fp f )
00116 {
00117     if ( UX(f < 0.0) )
00118         return 0.0;
00119     else if ( UX(f > 1.0) )
00120         return 1.0;
00121     else
00122         return f;
00123 }
00124 
00125 INLINE fp
00126 Clamp( fp f, fp low = 0.0, fp high = 1.0 )
00127 {
00128     if ( UX( f < low ) )
00129         return low;
00130     else if ( UX( f > high ) )
00131         return high;
00132     else
00133         return f;
00134 }
00135 
00137 INLINE fp
00138 PrintableMegabyte( const long bytes )
00139 {
00140     // One fractional digit.
00141     return fp(long(bytes*10) / long(MEGABYTE)) / fp(10.0);
00142 }
00143 
00147 
00152 template<typename T>       T*       PTR(       vector<T>& v ) { return &v[0]; }
00153 template<typename T> const T* CONST_PTR( const vector<T>& v ) { return &v[0]; } 
00154 
00159 template<typename SEQCONT>
00160 void Expand( SEQCONT& seqcont, uint idx )
00161 {
00162 ASSERT2( idx < MAX_ELEMS );
00163 
00164     // Be careful, resize() can implode a container too.
00165     if ( UX( idx >= seqcont.size() ) )
00166         seqcont.resize( idx + 1 );  // one more than index
00167 }
00168 
00173 template<typename T>
00174 void
00175 Remove( vector<T>& container, T item )  // STL vector
00176 {
00177     // erase/remove idiom for STL vectors or deques.  erase() returns nothing.
00178     container.erase( std::remove( container.begin(), container.begin(), item ),
00179                      container.end() );
00180 }
00181 
00182 template<typename T>
00183 void
00184 Remove( deque<T>& container, T item )  // STL deque
00185 {
00186     container.erase( std::remove( container.begin(), container.begin(), item ),
00187                      container.end() );
00188 }
00189 
00190 template<typename T>
00191 void
00192 Remove( list<T>& container, T item )  // STL list
00193 {
00194     container.remove( item );
00195 }
00196 
00197 template<typename KEY>
00198 void
00199 Remove( set<KEY>& container, KEY item )  // STL set
00200 {
00201     container.erase( item );
00202 }
00203 
00204 template<typename KEY,typename VAL>
00205 void
00206 Remove( map<KEY,VAL>& container, VAL item )  // STL map
00207 {
00208     container.erase( item );
00209 }
00210 
00214 template<typename CONTAINER>
00215 bool
00216 IfDuplicate( const CONTAINER& c1, const CONTAINER& c2 )
00217 {
00218     for ( typename CONTAINER::const_iterator i1 = c1.begin(); i1 != c1.end(); ++i1 )
00219     {
00220         if ( std::find( c2.begin(), c2.end(), *i1 ) != c2.end() )
00221             return true;
00222     }
00223     return false;
00224 }
00225 
00226 } // namespace base
00227 
00228 #endif // BASE_FUNCS2_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:06 2007