GlobalUtil.cpp
Go to the documentation of this file.
00001 
00002 //      File:           GlobalUtil.cpp
00003 //      Author:         Changchang Wu
00004 //      Description : Global Utility class for SiftGPU
00005 //
00006 //
00007 //
00008 //      Copyright (c) 2007 University of North Carolina at Chapel Hill
00009 //      All Rights Reserved
00010 //
00011 //      Permission to use, copy, modify and distribute this software and its
00012 //      documentation for educational, research and non-profit purposes, without
00013 //      fee, and without a written agreement is hereby granted, provided that the
00014 //      above copyright notice and the following paragraph appear in all copies.
00015 //      
00016 //      The University of North Carolina at Chapel Hill make no representations
00017 //      about the suitability of this software for any purpose. It is provided
00018 //      'as is' without express or implied warranty. 
00019 //
00020 //      Please send BUG REPORTS to ccwu@cs.unc.edu
00021 //
00023 #include <string.h>
00024 #include <iostream>
00025 using std::cout;
00026 
00027 #include "GL/glew.h"
00028 #include "GlobalUtil.h"
00029 
00030 //for windows, the default timing uses timeGetTime, you can define TIMING_BY_CLOCK to use clock()
00031 //for other os, the default timing uses gettimeofday, you can define TIMING_BY_CLOCK to use clock()
00032 
00033 
00034 #if defined(_WIN32)
00035         #if defined(TIMING_BY_CLOCK)
00036                 #include <time.h>
00037         #else
00038             #define WIN32_LEAN_AND_MEAN
00039             #include <windows.h>
00040                 #include <mmsystem.h>
00041         #endif
00042 #else
00043         #if defined(TIMING_BY_CLOCK)
00044                 #include <time.h>
00045         #else
00046                 #include <sys/time.h>
00047         #endif
00048 #include <stdio.h>
00049 #endif
00050 
00051 #include "LiteWindow.h"
00052 
00053 //
00054 int GlobalParam::		_verbose =  1;   
00055 int     GlobalParam::       _timingS = 1;  //print out information of each step
00056 int     GlobalParam::       _timingO = 0;  //print out information of each octave
00057 int     GlobalParam::       _timingL = 0;       //print out information of each level
00058 GLuint GlobalParam::	_texTarget = GL_TEXTURE_RECTANGLE_ARB; //only this one is supported
00059 GLuint GlobalParam::	_iTexFormat =GL_RGBA32F_ARB;       //or GL_RGBA16F_ARB
00060 int     GlobalParam::		_debug = 0;              //enable debug code?
00061 int     GlobalParam::		_usePackedTex = 1;//packed implementation
00062 int GlobalParam::		_BetaFilter = 0;
00063 int     GlobalParam::		_UseCUDA = 0;
00064 int GlobalParam::       _UseOpenCL = 0;
00065 int GlobalParam::		_MaxFilterWidth = -1;        //maximum filter width, use when GPU is not good enough
00066 float GlobalParam::     _FilterWidthFactor      = 4.0f; //the filter size will be _FilterWidthFactor*sigma*2+1
00067 float GlobalParam::     _DescriptorWindowFactor = 3.0f; //descriptor sampling window factor
00068 int GlobalParam::		_SubpixelLocalization = 1; //sub-pixel and sub-scale localization    
00069 int     GlobalParam::       _MaxOrientation = 2;        //whether we find multiple orientations for each feature 
00070 int     GlobalParam::       _OrientationPack2 = 0;  //use one float to store two orientations
00071 float GlobalParam::		_MaxFeaturePercent = 0.005f;//at most 0.005 of all pixels
00072 int     GlobalParam::		_MaxLevelFeatureNum = 4096; //maximum number of features of a level
00073 int GlobalParam::		_FeatureTexBlock = 4; //feature texture storagte alignment
00074 int     GlobalParam::		_NarrowFeatureTex = 0; 
00075 
00076 //if _ForceTightPyramid is not 0, pyramid will be reallocated to fit the size of input images.
00077 //otherwise, pyramid can be reused for smaller input images. 
00078 int GlobalParam::		_ForceTightPyramid = 0;
00079 
00080 //use gpu or cpu to generate feature list ...gpu is a little bit faster
00081 int GlobalParam::		_ListGenGPU =        1;      
00082 int     GlobalParam::       _ListGenSkipGPU = 6;  //how many levels are skipped on gpu
00083 int GlobalParam::		_PreProcessOnCPU = 1; //convert rgb 2 intensity on gpu, down sample on GPU
00084 
00085 //hardware parameter,   automatically retrieved
00086 int GlobalParam::		_texMaxDim = 3200;   //Maximum working size for SiftGPU, 3200 for packed
00087 int     GlobalParam::		_texMaxDimGL = 4096;        //GPU texture limit
00088 int GlobalParam::       _texMinDim = 16; //
00089 int     GlobalParam::		_IsNvidia = 0;                           //GPU vendor
00090 
00091 //you can't change the following 2 values
00092 //all other versions of code are now dropped
00093 int GlobalParam::       _DescriptorPPR = 8;
00094 int     GlobalParam::		_DescriptorPPT = 16;
00095 
00096 //whether orientation/descriptor is supported by hardware
00097 int GlobalParam::		_SupportNVFloat = 0;
00098 int GlobalParam::       _SupportTextureRG = 0;
00099 int     GlobalParam::		_UseDynamicIndexing = 0; 
00100 int GlobalParam::		_FullSupported = 1;
00101 
00102 //when SiftGPUEX is not used, display VBO generation is skipped
00103 int GlobalParam::		_UseSiftGPUEX = 0;
00104 int GlobalParam::		_InitPyramidWidth=0;
00105 int GlobalParam::		_InitPyramidHeight=0;
00106 int     GlobalParam::		_octave_min_default=0;
00107 int     GlobalParam::		_octave_num_default=-1;
00108 
00109 
00111 int     GlobalParam::		_GoodOpenGL = -1;      //indicates OpenGl initialization status
00112 int     GlobalParam::		_FixedOrientation = 0; //upright
00113 int     GlobalParam::		_LoweOrigin = 0;       //(0, 0) to be at the top-left corner.
00114 int     GlobalParam::       _NormalizedSIFT = 1;   //normalize descriptor
00115 int GlobalParam::       _BinarySIFT = 0;       //saving binary format
00116 int     GlobalParam::		_ExitAfterSIFT = 0;    //exif after saving result
00117 int     GlobalParam::		_KeepExtremumSign = 0; // if 1, scales of dog-minimum will be multiplied by -1
00119 int GlobalParam::       _KeyPointListForceLevel0 = 0;
00120 int     GlobalParam::		_ProcessOBO = 0;
00121 int GlobalParam::       _TruncateMethod = 0;
00122 int     GlobalParam::		_PreciseBorder = 1;
00123 
00124 // parameter changing for better matching with Lowe's SIFT
00125 float GlobalParam::		_OrientationWindowFactor = 2.0f;   // 1.0(-v292), 2(v293-), 
00126 float GlobalParam::		_OrientationGaussianFactor = 1.5f; // 4.5(-v292), 1.5(v293-)
00127 float GlobalParam::     _MulitiOrientationThreshold = 0.8f;
00129 int GlobalParam::       _FeatureCountThreshold = -1;
00130 
00132 int     GlobalParam::			_WindowInitX = -1;
00133 int GlobalParam::			_WindowInitY = -1;
00134 int GlobalParam::           _DeviceIndex = 0; 
00135 const char * GlobalParam::	_WindowDisplay = NULL;
00136 
00137 
00138 
00141 ClockTimer GlobalUtil::	_globalTimer;
00142 
00143 
00144 #ifdef _DEBUG
00145 void GlobalUtil::CheckErrorsGL(const char* location)
00146 {
00147         GLuint errnum;
00148         const char *errstr;
00149         while (errnum = glGetError()) 
00150         {
00151                 errstr = (const char *)(gluErrorString(errnum));
00152                 if(errstr) {
00153                         std::cerr << errstr; 
00154                 }
00155                 else {
00156                         std::cerr  << "Error " << errnum;
00157                 }
00158                 
00159                 if(location) std::cerr  << " at " << location;          
00160                 std::cerr  << "\n";
00161         }
00162         return;
00163 }
00164 
00165 #endif
00166 
00167 void GlobalUtil::CleanupOpenGL()
00168 {
00169         glActiveTexture(GL_TEXTURE0);
00170 }
00171 
00172 void GlobalUtil::SetDeviceParam(int argc, char** argv)
00173 {
00174     if(GlobalParam::_GoodOpenGL!= -1) return;
00175 
00176     #define CHAR1_TO_INT(x)         ((x >= 'A' && x <= 'Z') ? x + 32 : x)
00177     #define CHAR2_TO_INT(str, i)    (str[i] ? CHAR1_TO_INT(str[i]) + (CHAR1_TO_INT(str[i+1]) << 8) : 0)  
00178     #define CHAR3_TO_INT(str, i)    (str[i] ? CHAR1_TO_INT(str[i]) + (CHAR2_TO_INT(str, i + 1) << 8) : 0)
00179     #define STRING_TO_INT(str)      (CHAR1_TO_INT(str[0]) +  (CHAR3_TO_INT(str, 1) << 8))
00180 
00181         char* arg, * opt;
00182         for(int i = 0; i< argc; i++)
00183         {
00184                 arg = argv[i];
00185                 if(arg == NULL || arg[0] != '-')continue;
00186                 opt = arg+1;
00187  
00189         switch( STRING_TO_INT(opt))
00190         {
00191         case 'w' + ('i' << 8) + ('n' << 16) + ('p' << 24): 
00192             if(_GoodOpenGL != 2 && i + 1 < argc)
00193             {
00194                 int x =0, y=0;
00195                 if(sscanf(argv[++i], "%dx%d", &x, &y) == 2)
00196                 {
00197                     GlobalParam::_WindowInitX = x;
00198                     GlobalParam::_WindowInitY = y;
00199                 }  
00200             }
00201             break;  
00202         case 'd' + ('i' << 8) + ('s' << 16) + ('p' << 24):
00203             if(_GoodOpenGL != 2 && i + 1 < argc)
00204             {
00205                 GlobalParam::_WindowDisplay = argv[++i];
00206             }
00207             break;   
00208         case 'c' + ('u' << 8) + ('d' << 16) + ('a' << 24):
00209             if(i + 1 < argc)
00210             {
00211                int device =  0; 
00212                scanf(argv[++i], "%d", &device) ;
00213                GlobalParam::_DeviceIndex = device;
00214             }
00215             break;  
00216         default:
00217             break;
00218         }               
00219     }
00220 }
00221 
00222 void GlobalUtil::SetTextureParameter()
00223 {
00224 
00225         glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
00226         glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
00227         glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
00228         glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
00229         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00230 }
00231 
00232 //if image need to be up sampled ..use this one
00233 
00234 void GlobalUtil::SetTextureParameterUS()
00235 {
00236 
00237         glTexParameteri (_texTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
00238         glTexParameteri (_texTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
00239         glTexParameteri(_texTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
00240         glTexParameteri(_texTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
00241         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00242 }
00243 
00244 
00245 void GlobalUtil::FitViewPort(int width, int height)
00246 {
00247         GLint port[4];
00248         glGetIntegerv(GL_VIEWPORT, port);
00249         if(port[2] !=width || port[3] !=height)
00250         {
00251                 glViewport(0, 0, width, height);      
00252                 glMatrixMode(GL_PROJECTION);    
00253                 glLoadIdentity();               
00254                 glOrtho(0, width, 0, height,  0, 1);            
00255                 glMatrixMode(GL_MODELVIEW);     
00256                 glLoadIdentity();  
00257         }
00258 }
00259 
00260 
00261 bool GlobalUtil::CheckFramebufferStatus() {
00262     GLenum status;
00263     status=(GLenum)glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
00264     switch(status) {
00265         case GL_FRAMEBUFFER_COMPLETE_EXT:
00266             return true;
00267         case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
00268             std::cerr<<("Framebuffer incomplete,incomplete attachment\n");
00269             return false;
00270         case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
00271             std::cerr<<("Unsupported framebuffer format\n");
00272             return false;
00273         case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
00274             std::cerr<<("Framebuffer incomplete,missing attachment\n");
00275             return false;
00276         case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
00277             std::cerr<<("Framebuffer incomplete,attached images must have same dimensions\n");
00278             return false;
00279         case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
00280              std::cerr<<("Framebuffer incomplete,attached images must have same format\n");
00281             return false;
00282         case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
00283             std::cerr<<("Framebuffer incomplete,missing draw buffer\n");
00284             return false;
00285         case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
00286             std::cerr<<("Framebuffer incomplete,missing read buffer\n");
00287             return false;
00288     }
00289         return false;
00290 }
00291 
00292 
00293 int ClockTimer::ClockMS()
00294 {
00295 #if defined(TIMING_BY_CLOCK)
00296         return clock() * 1000 / CLOCKS_PER_SEC;
00297 #elif defined(_WIN32)
00298         static int    started = 0;
00299         static int      tstart;
00300         if(started == 0)
00301         {
00302                 tstart = timeGetTime();
00303                 started = 1;
00304                 return 0;
00305         }else
00306         {
00307                 return timeGetTime() - tstart;
00308         }
00309 #else
00310         static int    started = 0;
00311         static struct timeval tstart;
00312         if(started == 0) 
00313         {
00314                 gettimeofday(&tstart, NULL);
00315                 started = 1;
00316                 return 0;
00317         }else
00318         {       
00319                 struct timeval now;
00320                 gettimeofday(&now, NULL) ;
00321                 return (now.tv_usec - tstart.tv_usec + (now.tv_sec - tstart.tv_sec) * 1000000)/1000;
00322         }
00323 #endif
00324 }
00325 
00326 double ClockTimer::CLOCK()
00327 {
00328         return ClockMS() * 0.001;
00329 }
00330 
00331 void ClockTimer::InitHighResolution()
00332 {
00333 #if defined(_WIN32) 
00334         timeBeginPeriod(1);
00335 #endif
00336 }
00337 
00338 void ClockTimer::StartTimer(const char* event, int verb)
00339 {       
00340         strcpy(_current_event, event);
00341         _time_start = ClockMS();
00342         if(verb && GlobalUtil::_verbose)
00343         {
00344                 std::cout<<"\n["<<_current_event<<"]:\tbegin ...\n";
00345         }
00346 } 
00347 
00348 void ClockTimer::StopTimer(int verb)
00349 {
00350         _time_stop = ClockMS();
00351         if(verb && GlobalUtil::_verbose)
00352         {
00353                 std::cout<<"["<<_current_event<<"]:\t"<<GetElapsedTime()<<"\n";
00354         }
00355 }
00356 
00357 float ClockTimer::GetElapsedTime()
00358 {
00359         return (_time_stop - _time_start)  * 0.001f;
00360 }
00361 
00362 void GlobalUtil::SetGLParam()
00363 {
00364     if(GlobalUtil::_UseCUDA) return;
00365     else if(GlobalUtil::_UseOpenCL) return;
00366         glEnable(GlobalUtil::_texTarget);
00367         glActiveTexture(GL_TEXTURE0);
00368 }
00369 
00370 void GlobalUtil::InitGLParam(int NotTargetGL)
00371 {
00372     //IF the OpenGL context passed the check
00373     if(GlobalUtil::_GoodOpenGL == 2) return;
00374     //IF the OpenGl context failed the check
00375     if(GlobalUtil::_GoodOpenGL == 0) return; 
00376     //IF se use CUDA or OpenCL
00377     if(NotTargetGL && !GlobalUtil::_UseSiftGPUEX)
00378     {
00379         GlobalUtil::_GoodOpenGL = 1;
00380     }else
00381     {
00382         //first time in this function
00383         glewInit();
00384 
00385             GlobalUtil::_GoodOpenGL = 2;
00386 
00387             const char * vendor = (const char * )glGetString(GL_VENDOR);
00388             if(vendor)
00389             {
00390                     GlobalUtil::_IsNvidia  = (strstr(vendor, "NVIDIA") !=NULL ? 1 : 0);
00391                     if(GlobalUtil::_verbose) std::cout << "[GPU VENDOR]:\t" << vendor << "\n";
00392             }
00393             if(GlobalUtil::_IsNvidia == 0 )GlobalUtil::_UseCUDA = 0;
00394 
00395             if (glewGetExtension("GL_ARB_fragment_shader")    != GL_TRUE ||
00396                     glewGetExtension("GL_ARB_shader_objects")       != GL_TRUE ||
00397                     glewGetExtension("GL_ARB_shading_language_100") != GL_TRUE)
00398             {
00399                     std::cerr << "Shader not supported by your hardware!\n";
00400                     GlobalUtil::_GoodOpenGL = 0;
00401             }
00402 
00403             if (glewGetExtension("GL_EXT_framebuffer_object") != GL_TRUE) 
00404             {
00405                     std::cerr << "Framebuffer object not supported!\n";
00406                     GlobalUtil::_GoodOpenGL = 0;
00407             }
00408 
00409             if(glewGetExtension("GL_ARB_texture_rectangle")==GL_TRUE)
00410             {
00411                 GLint value;
00412                     GlobalUtil::_texTarget =  GL_TEXTURE_RECTANGLE_ARB;
00413                     glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_EXT, &value);
00414                     GlobalUtil::_texMaxDimGL = value; 
00415                     if(GlobalUtil::_verbose) std::cout << "TEXTURE:\t" << GlobalUtil::_texMaxDimGL << "\n";
00416 
00417                     if(GlobalUtil::_texMaxDim == 0 || GlobalUtil::_texMaxDim > GlobalUtil::_texMaxDimGL)
00418                     {
00419                             GlobalUtil::_texMaxDim = GlobalUtil::_texMaxDimGL; 
00420                     }
00421                     glEnable(GlobalUtil::_texTarget);
00422             }else
00423             {
00424                     std::cerr << "GL_ARB_texture_rectangle not supported!\n";
00425                     GlobalUtil::_GoodOpenGL = 0;
00426             }
00427 
00428             GlobalUtil::_SupportNVFloat = glewGetExtension("GL_NV_float_buffer");
00429             GlobalUtil::_SupportTextureRG = glewGetExtension("GL_ARB_texture_rg");
00430 
00431 
00432             glShadeModel(GL_FLAT);
00433             glPolygonMode(GL_FRONT, GL_FILL);
00434 
00435             GlobalUtil::SetTextureParameter();
00436     }
00437 }
00438 
00439 int GlobalUtil::CreateWindowEZ(LiteWindow* window)
00440 {
00441         if(window == NULL) return 0;
00442     if(!window->IsValid())window->Create(_WindowInitX, _WindowInitY, _WindowDisplay);
00443     if(window->IsValid()) 
00444     {
00445         window->MakeCurrent();
00446         return 1;
00447     }
00448     else  
00449     {
00450         std::cerr << "Unable to create OpenGL Context!\n";
00451                 std::cerr << "For nVidia cards, you can try change to CUDA mode in this case\n";
00452         return 0;
00453     }
00454 }
00455 
00456 int GlobalUtil::CreateWindowEZ()
00457 {
00458         static LiteWindow window;
00459     return CreateWindowEZ(&window);
00460 }
00461 
00462 int CreateLiteWindow(LiteWindow* window)
00463 {
00464     return GlobalUtil::CreateWindowEZ(window);
00465 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


rgbd_registration
Author(s): Ross Kidson
autogenerated on Thu May 23 2013 15:36:53