Main Page   Class Hierarchy   Compound List   File List   Compound Members  

baMath.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2001-2002 David Pritchard <drpritch@alumni.uwaterloo.ca>
00003 // 
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later
00008 // version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 #include <freecloth/base/baMath.h>
00020 #include <freecloth/base/debug.h>
00021 #include <freecloth/base/algorithm>
00022 
00023 FREECLOTH_NAMESPACE_START
00024 
00025 ////////////////////////////////////////////////////////////////////////////////
00026 // CLASS BaMath
00027 
00028 const Float BaMath::TOLERANCE = 10e-4f;
00029 
00030 //------------------------------------------------------------------------------
00031 
00032 Int8 BaMath::ceilInt8( Float x )
00033 {
00034     return static_cast<Int8>( ceil( x ) );
00035 }
00036 
00037 //------------------------------------------------------------------------------
00038 
00039 Int8 BaMath::floorInt8( Float x )
00040 {
00041     return static_cast<Int8>( floor( x ) );
00042 }
00043 
00044 //------------------------------------------------------------------------------
00045 
00046 Int8 BaMath::roundInt8( Float x )
00047 {
00048     return static_cast<Int8>( round( x ) );
00049 }
00050 
00051 //------------------------------------------------------------------------------
00052 
00053 UInt8 BaMath::ceilUInt8( Float x )
00054 {
00055     return static_cast<UInt8>( ceil( x ) );
00056 }
00057 
00058 //------------------------------------------------------------------------------
00059 
00060 UInt8 BaMath::floorUInt8( Float x )
00061 {
00062     return static_cast<UInt8>( floor( x ) );
00063 }
00064 
00065 //------------------------------------------------------------------------------
00066 
00067 UInt8 BaMath::roundUInt8( Float x )
00068 {
00069     return static_cast<UInt8>( round( x ) );
00070 }
00071 
00072 //------------------------------------------------------------------------------
00073 
00074 Int16 BaMath::ceilInt16( Float x )
00075 {
00076     return static_cast<Int16>( ceil( x ) );
00077 }
00078 
00079 //------------------------------------------------------------------------------
00080 
00081 Int16 BaMath::floorInt16( Float x )
00082 {
00083     return static_cast<Int16>( floor( x ) );
00084 }
00085 
00086 //------------------------------------------------------------------------------
00087 
00088 Int16 BaMath::roundInt16( Float x )
00089 {
00090     return static_cast<Int16>( round( x ) );
00091 }
00092 
00093 //------------------------------------------------------------------------------
00094 
00095 UInt16 BaMath::ceilUInt16( Float x )
00096 {
00097     return static_cast<UInt16>( ceil( x ) );
00098 }
00099 
00100 //------------------------------------------------------------------------------
00101 
00102 UInt16 BaMath::floorUInt16( Float x )
00103 {
00104     return static_cast<UInt16>( floor( x ) );
00105 }
00106 
00107 //------------------------------------------------------------------------------
00108 
00109 UInt16 BaMath::roundUInt16( Float x )
00110 {
00111     return static_cast<UInt16>( round( x ) );
00112 }
00113 
00114 //------------------------------------------------------------------------------
00115 
00116 Int32 BaMath::ceilInt32( Float x )
00117 {
00118     return static_cast<Int32>( ceil( x ) );
00119 }
00120 
00121 //------------------------------------------------------------------------------
00122 
00123 Int32 BaMath::floorInt32( Float x )
00124 {
00125     return static_cast<Int32>( floor( x ) );
00126 }
00127 
00128 //------------------------------------------------------------------------------
00129 
00130 Int32 BaMath::roundInt32( Float x )
00131 {
00132     return static_cast<Int32>( round( x ) );
00133 }
00134 
00135 //------------------------------------------------------------------------------
00136 
00137 UInt32 BaMath::ceilUInt32( Float x )
00138 {
00139     return static_cast<UInt32>( ceil( x ) );
00140 }
00141 
00142 //------------------------------------------------------------------------------
00143 
00144 UInt32 BaMath::floorUInt32( Float x )
00145 {
00146     return static_cast<UInt32>( floor( x ) );
00147 }
00148 
00149 //------------------------------------------------------------------------------
00150 
00151 UInt32 BaMath::roundUInt32( Float x )
00152 {
00153     return static_cast<UInt32>( round( x ) );
00154 }
00155 
00156 //------------------------------------------------------------------------------
00157 
00158 UInt8 BaMath::roundTruncUInt8( Float x )
00159 {
00160     return static_cast<UInt8>(
00161         std::min(
00162             static_cast<Float>( 0xff ),
00163             std::max( static_cast<Float>( 0x00 ), round( x ) )
00164         )
00165     );
00166 }
00167 
00168 //------------------------------------------------------------------------------
00169 
00170 Int8 BaMath::roundTruncInt8( Float x )
00171 {
00172     return static_cast<Int8>(
00173         std::min(
00174             static_cast<Float>( 0x7f ),
00175             std::max( static_cast<Float>( -0x80 ), round( x ) )
00176         )
00177     );
00178 }
00179 
00180 //------------------------------------------------------------------------------
00181 
00182 UInt16 BaMath::roundTruncUInt16( Float x )
00183 {
00184     return static_cast<UInt16>(
00185         std::min(
00186             static_cast<Float>( 0xffff ),
00187             std::max( static_cast<Float>( 0x0000 ), round( x ) )
00188         )
00189     );
00190 }
00191 
00192 //------------------------------------------------------------------------------
00193 
00194 Int16 BaMath::roundTruncInt16( Float x )
00195 {
00196     return static_cast<Int16>(
00197         std::min(
00198             static_cast<Float>( 0x7fff ),
00199             std::max( static_cast<Float>( -0x8000 ), round( x ) )
00200         )
00201     );
00202 }
00203 
00204 //------------------------------------------------------------------------------
00205 
00206 Float BaMath::abs( Float x )
00207 {
00208     return static_cast<Float>( ::fabs( x ) );
00209 }
00210 
00211 //------------------------------------------------------------------------------
00212 
00213 bool BaMath::isPow2( UInt32 x )
00214 {
00215     return x == roundUpPow2( x );
00216 }
00217 
00218 //------------------------------------------------------------------------------
00219 
00220 UInt32 BaMath::roundUpPow2( UInt32 x )
00221 {
00222     for ( UInt32 i = 1 << 31; i > 0; i >>= 1 ) {
00223         if ( x > i ) {
00224             return i * 2;
00225         }
00226     }
00227     return 0;
00228 }
00229 
00230 //------------------------------------------------------------------------------
00231 
00232 Float BaMath::sinc( Float x )
00233 {
00234     if ( isEqual( x, 0 ) ) {
00235         return 1.f;
00236     }
00237     return sin( M_PI * x ) / ( M_PI * x );
00238 }
00239 
00240 //------------------------------------------------------------------------------
00241 
00242 UInt32 BaMath::randomUInt32()
00243 {
00244     // We only know how to handle two different cases of RAND_MAX
00245     if ( RAND_MAX == 0xffffffff ) {
00246         return rand();
00247     } else if ( RAND_MAX == 0x7fffffff ) {
00248         return rand() + rand();
00249     }
00250     DGFX_ASSERT( RAND_MAX == 0xffffffff );
00251     return 0;
00252 }
00253 
00254 //------------------------------------------------------------------------------
00255 
00256 UInt32 BaMath::randomUInt32( UInt32 nbVals )
00257 {
00258     return randomUInt32() % nbVals;
00259 }
00260 
00261 //------------------------------------------------------------------------------
00262 
00263 Float BaMath::randomFloat()
00264 {
00265     return rand() / Float( RAND_MAX );
00266 }
00267 
00268 //------------------------------------------------------------------------------
00269 
00270 bool BaMath::isEqual( Float a, Float b, Float tol )
00271 {
00272     return fabs( a - b ) < tol;
00273 }
00274 
00275 //------------------------------------------------------------------------------
00276 
00277 bool BaMath::isGreater( Float a, Float b, Float tol )
00278 {
00279     return a - b > tol;
00280 }
00281 
00282 //------------------------------------------------------------------------------
00283 
00284 bool BaMath::isLess( Float a, Float b, Float tol )
00285 {
00286     return b - a > tol;
00287 }
00288 
00289 FREECLOTH_NAMESPACE_END

Generated on Fri May 2 16:51:11 2003 for Freecloth by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002