Mathematical functions
[General purpose utilitary functions]


Functions

static short swaps (short value)
 Swap the bytes of a short value.
TL_DLL double TeAdjustToPrecision (double val, int precision, bool reduce=false)
 Adjust a number to a given precision.
TL_DLL bool TeCheckFileExistence (const std::string &filename)
TL_DLL bool TeCompareDouble (double a, double b, int precision)
 Compares two doubles.
TL_DLL bool TeCompareFiles (const std::string &inputFileName1, const std::string &inputFileName2)
TL_DLL bool TeCopyFile (const std::string &inputFileName, const std::string &outputFileName)
TL_DLL unsigned long int TeCreateHashFromString (unsigned char const *inputString, const unsigned int &inputStringSize)
double TeCubicRoot (double x)
 Cubic root from x.
bool TeFPEquals (double d1, double d2, double precision)
TL_DLL bool TeGetDirFullFilesNames (const std::string &path, const bool &recursive, std::vector< std::string > &filesnames)
 Get the full names of all files inside the given directory.
TL_DLL unsigned long int TeGetFileSize (const std::string &filename)
TL_DLL unsigned long int TeGetFreePhysicalMemory ()
 Returns the amount of free physical memory (bytes).
TL_DLL unsigned int TeGetPhysProcNumber ()
 Returns the number of physical processors.
TL_DLL bool TeGetTempFileName (std::string &filename)
 Generates a temporary unique file name.
TL_DLL unsigned long int TeGetTotalPhysicalMemory ()
 Returns the amount of total physical memory (bytes).
TL_DLL unsigned long int TeGetTotalVirtualMemory ()
 Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physical + swapped).
TL_DLL unsigned long int TeGetUsedVirtualMemory ()
 Returns the amount of used virtual memory (bytes) for the current process (physical + swapped).
TL_DLL bool TeReplaceTextFileSubString (const std::string &inputFileName, const std::string &outputFileName, const std::string &oldSubString, const std::string &newSubString)
int TeRound (double val)
 Rounds a double to int.
TL_DLL double TeRoundD (double val, int precision=8)
 Rounds a double value to a given number of decimal digits.
int TeRoundRasterIndex (double val)
 Rounds a double raster element index to an integer.


Function Documentation

static short swaps ( short  value  )  [inline, static]

Definition at line 235 of file TeUtils.h.

Referenced by TeDecoderMemoryMap::getElement(), and TeDecoderMemoryMap::setElement().

00236         {
00237                 short svalue = ((value & 0x00ff) << 8) | ((value >> 8) & 0x00ff);
00238                 return svalue;
00239         }

TL_DLL double TeAdjustToPrecision ( double  val,
int  precision,
bool  reduce = false 
)

Definition at line 553 of file TeUtils.cpp.

00554 {
00555         double p = pow(10.0, (double)-precision);
00556         
00557         if (reduce)
00558                 return (val - p);
00559 
00560         return (val + p);
00561 }

TL_DLL bool TeCheckFileExistence ( const std::string filename  ) 

Check the file existence.

Parameters:
filename The file name.
Returns:
true if the file exists, false if not.

Definition at line 993 of file TeUtils.cpp.

00994 {
00995   FILE* fileptr = fopen( filename.c_str(), "r" );
00996   
00997   if( fileptr == 0 ) {
00998     return false;
00999   } else {
01000     fclose( fileptr );
01001     return true;
01002   }
01003 }

TL_DLL bool TeCompareDouble ( double  a,
double  b,
int  precision 
)

Definition at line 517 of file TeUtils.cpp.

References TeNAME_LENGTH.

00518 {
00519         char bufa [ TeNAME_LENGTH ];
00520         char bufb [ TeNAME_LENGTH ];
00521         if (precision == 0)
00522         {
00523                 sprintf ( bufa, "%f", a );
00524                 sprintf ( bufb, "%f", b );
00525         }
00526         else
00527         {
00528                 sprintf ( bufa, "%.*f", precision, a );
00529                 sprintf ( bufb, "%.*f", precision, b );
00530         }
00531 
00532         string A = bufa;
00533         string B = bufb;
00534         return (A == B);
00535 }

TL_DLL bool TeCompareFiles ( const std::string inputFileName1,
const std::string inputFileName2 
)

Compare two files.

Parameters:
inputFileName1 The input full file 1 name.
inputFileName2 The input full file 2 name.
Returns:
true if the two files are identical, false if not.

Definition at line 1124 of file TeUtils.cpp.

01126 {
01127   std::ifstream ifs1(inputFileName1.c_str(),std::ios::in | std::ios::binary |
01128     std::ios_base::ate );
01129   std::ifstream ifs2(inputFileName2.c_str(),std::ios::in | std::ios::binary |
01130     std::ios_base::ate );
01131   
01132   if( ifs1.is_open() && ifs2.is_open() )
01133   {
01134     if( ifs1.tellg() == ifs2.tellg() )
01135     {  
01136       ifs1.seekg( 0, ios_base::beg );
01137       ifs2.seekg( 0, ios_base::beg );
01138       
01139       char data1 = 0;
01140       char data2 = 0;
01141       
01142       while( ! ifs1.eof() )
01143       {
01144         if( ifs1.read( &data1, 1 ).bad() )
01145           return false;
01146         if( ifs2.read( &data2, 1 ).bad() )
01147           return false;        
01148         if( data1 != data2 )
01149           return false;  
01150       }
01151       
01152       return true;
01153     }
01154     else
01155     {
01156       return false;
01157     }    
01158   }
01159   else
01160   {
01161     return false;
01162   }
01163 }

TL_DLL bool TeCopyFile ( const std::string inputFileName,
const std::string outputFileName 
)

Creates a copy from the given input file name.

Parameters:
inputFileName The input full file name.
outputFileName The output full file name.
Returns:
true if OK, false if not.

Definition at line 1101 of file TeUtils.cpp.

01103 {
01104   std::ifstream ifs(inputFileName.c_str(),std::ios::in | std::ios::binary);
01105   if( ifs.is_open() )
01106   {
01107     std::ofstream ofs(outputFileName.c_str(),std::ios::out | std::ios::binary);
01108     if( ofs.is_open() )
01109     {
01110       ofs << ifs.rdbuf();
01111       return ! ofs.bad();
01112     }
01113     else
01114     {
01115       return false;
01116     }
01117   }
01118   else
01119   {
01120     return false;
01121   }
01122 }

TL_DLL unsigned long int TeCreateHashFromString ( unsigned char const *  inputString,
const unsigned int &  inputStringSize 
)

Creates a hash number from an input string. This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. It was found to do well in scrambling bits, causing better distribution of the keys and fewer splits. It also happens to be a good general hashing function with good distribution. the actual function is hash(i) = hash(i - 1) * 65599 + str[i]

Parameters:
inputStringFileName The input full file name.
inputStringSize Input string size.
Returns:
The generated hash value.

Definition at line 1207 of file TeUtils.cpp.

01209 {
01210   unsigned long int hash = 0;
01211 
01212   for( register unsigned int idx = 0 ; idx < inputStringSize ; ++idx )
01213   {  
01214     hash = ((unsigned long int)inputString[ idx ]) + (hash << 6) + 
01215       (hash << 16) - hash;
01216   }
01217   
01218   return hash;
01219 }

double TeCubicRoot ( double  x  )  [inline]

Parameters:
x X.
Returns:
The cubic root from x.

Definition at line 212 of file TeUtils.h.

Referenced by TePDIRgbPaletteFunctions::createLSBPalette().

00213         {
00214                 if( x < 0 ) {
00215                 return ( -1. ) * pow(  ( -1. ) * x, ( 1. / 3. ) );
00216                 } else {
00217                         return pow(  x, ( 1. / 3. ) );
00218                 }
00219         };

bool TeFPEquals ( double  d1,
double  d2,
double  precision 
) [inline]

Comparassion of two floating points, considering a given precision

Definition at line 222 of file TeUtils.h.

Referenced by TeProjection::operator==(), and TeDatum::operator==().

00223         {
00224                 double eps1 = fabs(d1), 
00225                         eps2 = fabs(d2), 
00226                         eps;
00227                 eps = (eps1 > eps2) ? eps1 : eps2;
00228                 if (eps == 0.0)
00229                         return true; //both numbers are 0.0
00230                 eps *= precision;
00231                 return (fabs(d1 - d2) < eps);
00232         }

TL_DLL bool TeGetDirFullFilesNames ( const std::string path,
const bool recursive,
std::vector< std::string > &  filesnames 
)

TL_DLL unsigned long int TeGetFileSize ( const std::string filename  ) 

The file size (bytes).

Note:
Throws an exception if file not found.
Parameters:
filename The file name.
Returns:
The file size (bytes).

Definition at line 975 of file TeUtils.cpp.

References CANNOT_OPEN_FILE, and fseek().

00976 {
00977   FILE* fileptr = fopen( filename.c_str(), "r" );
00978   
00979   if( fileptr == 0 ) {
00980     throw TeException( CANNOT_OPEN_FILE, "File not found", false );
00981   }
00982   
00983   fseek( fileptr, 0, SEEK_END );
00984   
00985   unsigned long int filesize = ( unsigned long int ) ftell( fileptr );
00986   
00987   fclose( fileptr );
00988   
00989   return filesize;
00990 }

TL_DLL unsigned long int TeGetFreePhysicalMemory (  ) 

Returns:
The amount of free physical memory (bytes).

Definition at line 805 of file TeUtils.cpp.

References NULL, and TEAGN_LOG_AND_THROW.

00806 {
00807   unsigned long int freemem = 0;
00808 
00809   #if defined __unix__  || TePLATFORM == TePLATFORMCODE_APPLE
00810     #if defined( __FreeBSD__ ) || TePLATFORM == TePLATFORMCODE_APPLE
00811       /* BSD workaround */
00812       
00813       unsigned int usermem;
00814       size_t usermem_len = sizeof( usermem );
00815       int mib[2] = { CTL_HW, HW_USERMEM };
00816       
00817       if( sysctl( mib, 2, &usermem, &usermem_len, NULL, 0 ) 
00818         == 0 ) {
00819         
00820         freemem = (unsigned long int)usermem;
00821       } else {
00822         TEAGN_LOG_AND_THROW( "TeGetFreePhysicalMemory error" );
00823       }
00824     #else
00825       /* Other linux stuff */
00826       
00827       freemem = (unsigned long int) sysconf( _SC_PAGESIZE ) *
00828         (unsigned long int) sysconf( _SC_AVPHYS_PAGES );
00829     #endif
00830   #elif defined WIN32
00831       LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
00832       GlobalMemoryStatus( status_buffer );
00833       freemem = (unsigned long int) status_buffer->dwAvailPhys;
00834       delete status_buffer;
00835   #else
00836       #error "Unsuported plataform for physical memory checking"
00837   #endif
00838 
00839   return freemem;
00840 }

TL_DLL unsigned int TeGetPhysProcNumber (  ) 

Returns:
The number of physical processors.

Definition at line 943 of file TeUtils.cpp.

References TePLATFORMCODE_AIX, TePLATFORMCODE_APPLE, and TePLATFORMCODE_LINUX.

00944 {
00945   unsigned int procnmb = 0;
00946   
00947   #if TePLATFORM == TePLATFORMCODE_MSWINDOWS
00948     SYSTEM_INFO siSysInfo;
00949     GetSystemInfo(&siSysInfo);
00950     procnmb = (unsigned int)siSysInfo.dwNumberOfProcessors;
00951   #elif TePLATFORM == TePLATFORMCODE_LINUX || TePLATFORM == TePLATFORMCODE_AIX || TePLATFORM == TePLATFORMCODE_APPLE
00952     procnmb = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN );
00953   #else
00954     #error "ERROR: Unsupported platform"
00955   #endif    
00956 
00957   return procnmb;
00958 }

TL_DLL bool TeGetTempFileName ( std::string filename  ) 

Parameters:
filename The generated file name.
Returns:
true if ok, false errors.

Definition at line 961 of file TeUtils.cpp.

00962 {
00963   char* name = tempnam( 0, 0 );
00964   
00965   if( name == 0 ) {
00966     filename.clear();
00967     return false;
00968   } else {
00969     filename = std::string( name );
00970     return true;
00971   }
00972 }  

TL_DLL unsigned long int TeGetTotalPhysicalMemory (  ) 

Returns:
The amount of total physical memory (bytes).

Definition at line 843 of file TeUtils.cpp.

References NULL, and TEAGN_LOG_AND_THROW.

00844 {
00845   unsigned long int totalmem = 0;
00846 
00847   #if defined __unix__ || TePLATFORM == TePLATFORMCODE_APPLE
00848     #if defined( __FreeBSD__ ) || TePLATFORM == TePLATFORMCODE_APPLE
00849       /* BSD workaround */
00850       
00851       unsigned int physmem;
00852       size_t physmem_len = sizeof( physmem );
00853       int mib[2] = { CTL_HW, HW_PHYSMEM };
00854       
00855       if( sysctl( mib, 2, &physmem, &physmem_len, NULL, 0 ) 
00856         == 0 ) {
00857         
00858         totalmem = (unsigned long int)physmem; 
00859       } else {
00860         TEAGN_LOG_AND_THROW( "TeGetTotalPhysicalMemory error" );
00861       }
00862     #else
00863       /* Other linux stuff */  
00864   
00865       totalmem = (unsigned long int) sysconf( _SC_PAGESIZE ) *
00866         (unsigned long int) sysconf( _SC_PHYS_PAGES );
00867     #endif
00868   #elif defined WIN32
00869     LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
00870     GlobalMemoryStatus( status_buffer );
00871     totalmem = (unsigned long int) status_buffer->dwTotalPhys;
00872     delete status_buffer;
00873   #else
00874     #error "Unsuported plataform for physical memory checking"
00875   #endif
00876 
00877   return totalmem;
00878 }

TL_DLL unsigned long int TeGetTotalVirtualMemory (  ) 

Returns:
The amount of total virtual memory (bytes).

Definition at line 920 of file TeUtils.cpp.

00921 {
00922   unsigned long int totalmem = 0;
00923 
00924   #if defined __unix__ || TePLATFORM == TePLATFORMCODE_APPLE
00925     struct rlimit info;
00926      
00927     if( getrlimit( RLIMIT_AS, &info ) == 0 )
00928     {
00929       totalmem = (unsigned long int)info.rlim_max;
00930     };
00931   #elif defined WIN32
00932     LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
00933     GlobalMemoryStatus( status_buffer );
00934     totalmem = (unsigned long int) status_buffer->dwTotalVirtual;
00935     delete status_buffer;
00936   #else
00937     #error "Unsuported plataform for virtual memory checking"
00938   #endif
00939 
00940   return totalmem;
00941 }

TL_DLL unsigned long int TeGetUsedVirtualMemory (  ) 

Returns:
The amount of free virtual memory (bytes).

Definition at line 880 of file TeUtils.cpp.

00881 {
00882   unsigned long int usedmem = 0;
00883   
00884   #if TePLATFORM == TePLATFORMCODE_LINUX
00885     #if defined( __FreeBSD__ )
00886       struct rusage rusageinfo;
00887       getrusage( RUSAGE_SELF, &rusageinfo );
00888       usedmem = (unsigned long int)( 1024 * rusageinfo.ru_maxrss );
00889     #else
00890       std::string pid, comm, state, ppid, pgrp, session, tty_nr, 
00891         tpgid, flags, minflt, cminflt, majflt, cmajflt,
00892         utime, stime, cutime, cstime, priority, nice,
00893         stringO, itrealvalue, starttime;
00894     
00895       std::ifstream stat_stream("/proc/self/stat",std::ios_base::in); 
00896       
00897       stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr 
00898         >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt 
00899         >> utime >> stime >> cutime >> cstime >> priority >> nice 
00900         >> stringO >> itrealvalue >> starttime >> usedmem;    
00901     #endif
00902   #elif TePLATFORM == TePLATFORMCODE_AIX || TePLATFORM == TePLATFORMCODE_APPLE
00903     struct rusage rusageinfo;
00904     getrusage( RUSAGE_SELF, &rusageinfo );
00905     usedmem = (unsigned long int)( 1024 * rusageinfo.ru_maxrss );
00906   #elif TePLATFORM == TePLATFORMCODE_MSWINDOWS
00907     LPMEMORYSTATUS status_buffer = new MEMORYSTATUS;
00908     GlobalMemoryStatus( status_buffer );
00909     usedmem = (unsigned long int)( status_buffer->dwTotalVirtual -
00910       status_buffer->dwAvailVirtual );
00911     delete status_buffer;
00912   #else
00913       #error "Unsuported plataform for virtual memory checking"
00914   #endif
00915 
00916   return usedmem;
00917 }

TL_DLL bool TeReplaceTextFileSubString ( const std::string inputFileName,
const std::string outputFileName,
const std::string oldSubString,
const std::string newSubString 
)

Creates a copy from the given input text file name replacing all ocurrences of

Parameters:
inputFileName The input full file name.
inputFileName The output full file name.
oldSubString The sub-string to be replaced from the input file.
newSubString The new sub-string.
Returns:
true if OK, false if not.

Definition at line 1165 of file TeUtils.cpp.

01168 {
01169   std::ifstream ifs(inputFileName.c_str(),std::ios::in);
01170   if( ifs.is_open() )
01171   {
01172     std::ofstream ofs(outputFileName.c_str(),std::ios::out);
01173     
01174     if( ofs.is_open() )
01175     {
01176       std::string lineStr;
01177       std::string::size_type findPos = 0;
01178       
01179       do
01180       {
01181         getline( ifs, lineStr );
01182         
01183         while( ( findPos = lineStr.find( oldSubString, 0 ) ) < 
01184           std::string::npos )
01185         {
01186           lineStr.erase( findPos, oldSubString.size() );
01187           lineStr.insert( findPos, newSubString );
01188         }
01189         
01190         ofs << lineStr << std::endl;
01191       }
01192       while( ! ifs.eof() );
01193       
01194       return ! ofs.bad();
01195     }
01196     else
01197     {
01198       return false;
01199     }
01200   }
01201   else
01202   {
01203     return false;
01204   }
01205 }

int TeRound ( double  val  )  [inline]

Examples:
TePDICorrelationMatching_test.cpp, TePDIMIMatching_test.cpp, TePDIMMIOMatching_test.cpp, and TePDIOFMatching_test.cpp.

Definition at line 169 of file TeUtils.h.

Referenced by TeQwtPlotZoomer::adjustRect(), TeRaster::begin(), TePDIInterpolator::bicubicInterpolation(), TePDIMMIOMatching::bicubicResampleMatrix(), TePDIInterpolator::bilinearInterpolation(), TeDecoderDatabase::blockIndexPos(), TeQtGLWidget::changeQuality(), TePDIHistogram::Discretize(), TeQtGLWidget::draw2DPixmaps(), TeQtColorBar::drawColorBar(), TePDIParallelSegmenter::flushBlock(), generateColorBarMap(), TePDIMMIOMatching::generateCorrelationFeatures(), TePDIOFMatching::generateCorrWindows(), getColors(), TeQtTextEdit::getRect(), TeQtMultiTextEdit::getTextIndex(), hsv2Rgb(), HSVtoRGB(), TePDIHistogram::IsDiscrete(), TePDIOFMatching::loadImage(), TePDIMIMatching::loadImage(), TePDICorrelationMatching::loadImage(), TePDIOFMatching::matrix2Tiff(), TePDIMIMatching::matrix2Tiff(), TePDICorrelationMatching::matrix2Tiff(), TePDIInterpolator::nNInterpolation(), TePDIHistogram::operator=(), TeQtGLWidget::paint2D(), TeQtCanvas::plotGraphicScale(), TePDIOFMatching::raster2Tiff(), TePDIMIMatching::raster2Tiff(), raster2Tiff(), TePDICorrelationMatching::raster2Tiff(), TePDIContrast::RemapLevels(), TePDIUtils::resampleRasterByRes(), TePDIHistogram::reset(), rgb2Hsv(), rotateRasterClockWize(), TeSAM::TeGridIndex::setResolution(), TeQtGLWidget::setXOffset(), TeQtGLWidget::setXSize(), TeQtGLWidget::setYOffset(), TeQtGLWidget::setYSize(), TeQtGLWidget::setZOffset(), TeQtGLWidget::setZSize(), TePDIUtils::TeCopyRasterPixels(), TeCreateCells(), TeRasterRemap::TeInterpolateIn(), and TePDIJointHistogram::updateFloat().

00170         {       
00171                 if (val>=0)
00172                         return (int)(val+.5);
00173                 else
00174                         return (int)(val-.5);
00175         }

TL_DLL double TeRoundD ( double  val,
int  precision = 8 
)

Definition at line 508 of file TeUtils.cpp.

References TeNAME_LENGTH.

00509 {
00510         char name [ TeNAME_LENGTH ];
00511         sprintf ( name, "%.*f", precision, val);
00512         return atof(name);
00513 
00514 }

int TeRoundRasterIndex ( double  val  )  [inline]

Definition at line 197 of file TeUtils.h.

Referenced by applyStrategic(), TeRasterRemap::copy(), TeRasterRemap::resample(), TePixelBBInterPoly::strateg(), TePixelBoxInPoly::strateg(), TeBuildMultiResolutionPyramid(), TeGeoOpAssignByLocationCollect(), TeMask(), TeMemoryZonal(), TePolygonSetMemoryZonal(), TeRasterClipping(), and TeDatabase::zonal().

00198         {       
00199                 int ind = (int) val;
00200                 if (val < (ind-0.5))
00201                         ind -= 1;
00202                 else if (val >= (ind+0.5))
00203                         ind += 1;
00204                 return ind;
00205         }


Generated on Sun Jul 29 04:04:41 2012 for TerraLib - Development Source by  doxygen 1.5.3