test-BeoSubDecoder.C

Go to the documentation of this file.
00001 /*! @file BeoSub/test-BeoSubDecoder.C [put description here] */
00002 
00003 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/BeoSub/test-BeoSubDecoder.C $
00004 // $Id: test-BeoSubDecoder.C 14295 2010-12-02 20:02:32Z itti $
00005 
00006 // compile with: -lm -lglut -lGLU -lGL -lX11 -lXext
00007 
00008 
00009 #include <cmath>
00010 #ifdef HAVE_GL_GLUT_H
00011 #include <GL/gl.h>
00012 #undef APIENTRY // otherwise it gets redefined between gl.h and glut.h???
00013 #include <GL/glut.h>
00014 #include <GL/glu.h>
00015 #endif
00016 #include <cstdio>
00017 #include <cstring>
00018 #include <cstdlib>
00019 
00020 #include "Image/Image.H"
00021 #include "Image/ImageSet.H"
00022 #include "Component/ModelManager.H"
00023 #include "BeoSub/BeoSubTaskDecoder.H"
00024 
00025 #ifdef HAVE_GL_GLUT_H
00026 
00027 float fangle=0.0,deltaAngle = 0.0,ratio, dtemp;
00028 float x=0.0f,y=5.0f,z=2.0f;     // init postion of the camera
00029 float lx=0.0f,ly=0.0f,lz=-0.1f;
00030 bool shape = true;
00031 int sw=320,sh=240;
00032 int deltaMove = 0,h=300,w=300;
00033 int deltaDepth = 0;
00034 int counter=0;
00035 void* font=GLUT_BITMAP_8_BY_13;
00036 int bitmapHeight=13;
00037 int frame,atime,timebase=0;
00038 int outframe,outtime,outtimebase=0, decode = 0;
00039 char s[30],s2[30];
00040 int mainWindow, subWindow4;
00041 
00042 float avgFR =0.0F;
00043 byte *image = new byte[sw*sh*3];
00044 
00045 ImageSet< PixRGB<byte> > inStream;
00046 
00047 ModelManager mgr("BeoSubTaskDecoder Tester");
00048 // Instantiate our various ModelComponents:
00049 nub::soft_ref<BeoSubTaskDecoder> test(new BeoSubTaskDecoder(mgr));
00050 
00051 void initWindow();
00052 
00053 void changeSize2(int w1, int h1)
00054 {
00055   // Prevent a divide by zero, when window is too short
00056   // (you cant make a window of zero width).
00057   ratio = 1.0f * w1 / h1;
00058   // Reset the coordinate system before modifying
00059   glMatrixMode(GL_PROJECTION);
00060   glLoadIdentity();
00061 
00062   // Set the viewport to be the entire window
00063   glViewport(0, 0, w1, h1);
00064 
00065   // Set the clipping volume
00066   gluPerspective(45,ratio,0.1,1000);
00067   glMatrixMode(GL_MODELVIEW);
00068 }
00069 
00070 void changeSize(int w1,int h1) {
00071   if(h1 == 0)
00072     h1 = 1;
00073 
00074   w = w1;
00075   h = h1;
00076 
00077   glutSetWindow(subWindow4);
00078   glutPositionWindow(0,0);
00079   glutReshapeWindow(w, h);
00080   changeSize2(w, h);
00081 
00082 }
00083 
00084 // rotate alone y axis
00085 void drawRecRotate(GLfloat de, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat d, GLfloat h)
00086 {
00087   // rotation matrix
00088   // cos(de)  0  -sin(de)
00089   // 0          1  0
00090   // sin(de)  0  cos(de)
00091   // newx = cos(de) * x - sin(de) * z
00092   // newz = sin(de) * x + cos(de) * z
00093   // down
00094   GLfloat tempx=x,tempz=z;
00095   x=z=0;
00096   glBegin(GL_TRIANGLES);
00097   glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz);
00098   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz);
00099   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y,  sin(de)*x+cos(de)*(z+d)+tempz);
00100   glEnd();
00101   glBegin(GL_TRIANGLES);
00102   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y,  sin(de)*x+cos(de)*(z+d)+tempz);
00103   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz);
00104   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00105   glEnd();
00106   // up
00107   glBegin(GL_TRIANGLES);
00108   glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz);
00109   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz);
00110   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz);
00111   glEnd();
00112   glBegin(GL_TRIANGLES);
00113   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz);
00114   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00115   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz);
00116   glEnd();
00117   // left
00118   glBegin(GL_TRIANGLES);
00119   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y, sin(de)*x+cos(de)*(z+d)+tempz);
00120   glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz);
00121   glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz);
00122   glEnd();
00123   glBegin(GL_TRIANGLES);
00124   glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz);
00125   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y,  sin(de)*x+cos(de)*(z+d)+tempz);
00126   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h, sin(de)*x+cos(de)*(z+d)+tempz);
00127   glEnd();
00128   // right
00129   glBegin(GL_TRIANGLES);
00130   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00131   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz);
00132   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00133   glEnd();
00134   glBegin(GL_TRIANGLES);
00135   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz);
00136   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00137   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y,  sin(de)*(x+w)+cos(de)*z+tempz);
00138   glEnd();
00139   // front
00140   glBegin(GL_TRIANGLES);
00141   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00142   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h,  sin(de)*x+cos(de)*(z+d)+tempz);
00143   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y, sin(de)*x+cos(de)*(z+d)+tempz);
00144   glEnd();
00145   glBegin(GL_TRIANGLES);
00146   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00147   glVertex3f( cos(de)*(x+w)-sin(de)*(z+d)+tempx, y+h, sin(de)*(x+w)+cos(de)*(z+d)+tempz);
00148   glVertex3f( cos(de)*x-sin(de)*(z+d)+tempx, y+h,  sin(de)*x+cos(de)*(z+d)+tempz);
00149   glEnd();
00150   // back
00151   glBegin(GL_TRIANGLES);
00152   glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz);
00153   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h,  sin(de)*(x+w)+cos(de)*z+tempz);
00154   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y, sin(de)*(x+w)+cos(de)*z+tempz);
00155   glEnd();
00156   glBegin(GL_TRIANGLES);
00157   glVertex3f( cos(de)*x-sin(de)*z+tempx, y, sin(de)*x+cos(de)*z+tempz);
00158   glVertex3f( cos(de)*x-sin(de)*z+tempx, y+h, sin(de)*x+cos(de)*z+tempz);
00159   glVertex3f( cos(de)*(x+w)-sin(de)*z+tempx, y+h, sin(de)*(x+w)+cos(de)*z+tempz);
00160   glEnd();
00161 }
00162 void setOrthographicProjection() {
00163 
00164   // switch to projection mode
00165   glMatrixMode(GL_PROJECTION);
00166   // save previous matrix which contains the
00167   //settings for the perspective projection
00168   glPushMatrix();
00169   // reset matrix
00170   glLoadIdentity();
00171   // set a 2D orthographic projection
00172   gluOrtho2D(0, w, 0, h/2);
00173   // invert the y axis, down is positive
00174   glScalef(1, -1, 1);
00175   // mover the origin from the bottom left corner
00176   // to the upper left corner
00177   glTranslatef(0, -h/2, 0);
00178   glMatrixMode(GL_MODELVIEW);
00179 }
00180 
00181 void resetPerspectiveProjection() {
00182   // set the current matrix to GL_PROJECTION
00183   glMatrixMode(GL_PROJECTION);
00184   // restore previous settings
00185   glPopMatrix();
00186   // get back to GL_MODELVIEW matrix
00187   glMatrixMode(GL_MODELVIEW);
00188 }
00189 
00190 void renderBitmapString(float x, float y, void *font,char *string)
00191 {
00192   char *c;
00193   // set position to start drawing fonts
00194   glRasterPos2f(x, y);
00195   // loop all the characters in the string
00196   for (c=string; *c != '\0'; c++) {
00197     glutBitmapCharacter(font, *c);
00198   }
00199 }
00200 void initScene() {
00201   glEnable(GL_DEPTH_TEST);
00202   glEnable(GL_CULL_FACE);
00203   // add fog effect
00204   /*
00205   glEnable(GL_FOG);
00206   {
00207     GLfloat fogColor[4] = {0.35,0.5,0.5,1.0};
00208 
00209     glFogi(GL_FOG_MODE, GL_EXP);
00210     glFogfv(GL_FOG_COLOR, fogColor);
00211     glFogf(GL_FOG_DENSITY, 0.5);
00212     glHint(GL_FOG_HINT, GL_DONT_CARE);
00213     glFogf(GL_FOG_START, 0.0);
00214     glFogf(GL_FOG_END, 10.0);
00215   }
00216   glClearColor(0.35,0.5,0.5,1.0);
00217   */
00218 }
00219 
00220 void orientMe(float ang) {
00221   lx = sin(ang);
00222   lz = -cos(ang);
00223 }
00224 
00225 
00226 void moveMeFlat(int i) {
00227   x = x + i*(lx)*0.01;
00228   z = z + i*(lz)*0.01;
00229 }
00230 
00231 void moveMeVer(int i)
00232 {
00233   y = y + i*0.01;
00234 }
00235 
00236 void renderScene2(int currentWindow) {
00237 
00238   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00239 
00240   // Draw ground
00241   glColor3f(0.0f, 0.5f, 0.5f);
00242   glBegin(GL_QUADS);
00243   glVertex3f(-200.0f, 0.0f, -200.0f);
00244   glVertex3f(-200.0f, 0.0f,  200.0f);
00245   glVertex3f( 200.0f, 0.0f,  200.0f);
00246   glVertex3f( 200.0f, 0.0f, -200.0f);
00247   glEnd();
00248   glBegin(GL_QUADS);
00249   glVertex3f(-200.0f, 0.0f, -200.0f);
00250   glVertex3f( 200.0f, 0.0f, -200.0f);
00251   glVertex3f( 200.0f, 0.0f,  200.0f);
00252   glVertex3f(-200.0f, 0.0f,  200.0f);
00253   glEnd();
00254   // Draw water surface
00255 
00256   glColor3f(0.0f, 0.0f, 0.9f);
00257   glBegin(GL_QUADS);
00258   glVertex3f(-200.0f, 16.5f, -200.0f);
00259   glVertex3f(-200.0f, 16.5f,  200.0f);
00260   glVertex3f( 200.0f, 16.5f,  200.0f);
00261   glVertex3f( 200.0f, 16.5f, -200.0f);
00262   glEnd();
00263   glBegin(GL_QUADS);
00264   glVertex3f(-200.0f, 16.5f, -200.0f);
00265   glVertex3f( 200.0f, 16.5f, -200.0f);
00266   glVertex3f( 200.0f, 16.5f,  200.0f);
00267   glVertex3f(-200.0f, 16.5f,  200.0f);
00268   glEnd();
00269 
00270   outframe++;
00271   outtime=glutGet(GLUT_ELAPSED_TIME);
00272 
00273   if (outtime - outtimebase > 1000)       // per second
00274     {
00275       sprintf(s2,"FPS:%4.2f",outframe*1000.0/(outtime-outtimebase));
00276       outtimebase = outtime;
00277       outframe = 0;
00278     }
00279 
00280   // frequency = 5 Hz with red light
00281   if(decode == 0)
00282     {
00283       if(outtime/100 % 2 == 0)
00284         glColor3f(1.0f,0.0f,0.0f);
00285       else    // whiet
00286         glColor3f(1.0f,1.0f,1.0f);
00287     }
00288   // frequency = 2 Hz with red light
00289   else if(decode == 1)
00290     {
00291       if(outtime/100 % 5 == 0)
00292         glColor3f(1.0f,0.0f,0.0f);
00293       else    // whiet
00294         glColor3f(1.0f,1.0f,1.0f);
00295     }
00296   // fequency = 5 Hz with green light
00297   else if(decode == 2)
00298     {
00299       if(outtime/100 % 2 == 0)
00300         glColor3f(0.0f,1.0f,0.0f);
00301       else    // whiet
00302         glColor3f(1.0f,1.0f,1.0f);
00303     }
00304   // frequency = 2 Hz with green light
00305   else if(decode == 3)
00306     {
00307       if(outtime/100 % 5 == 0)
00308         glColor3f(0.0f,1.0f,0.0f);
00309       else    // whiet
00310         glColor3f(1.0f,1.0f,1.0f);
00311     }
00312   glTranslatef(0, 5, 0);
00313   if(shape)
00314     glutSolidSphere(0.1f,20,20);
00315   else
00316     drawRecRotate(0.0f, -0.15f,-0.15f,-0.25f,0.3f,0.3f,0.3f);
00317   //      glColor3f(0.0f,0.0f,0.0f);
00318   //      drawRecRotate(0.0f, -0.15f,-0.15f,-0.25f,0.3f,0.3f,0.3f);
00319   // show the text
00320 
00321   if (currentWindow == subWindow4)
00322     {
00323       glReadPixels(0,0,320,240 ,GL_RGB,GL_UNSIGNED_BYTE,image);
00324       Image< PixRGB<byte> > im((PixRGB<byte> *)image, 320, 240);
00325 
00326       renderBitmapString(30,15,(void *)font,s);
00327       frame++;
00328       atime=glutGet(GLUT_ELAPSED_TIME);
00329 
00330       inStream.push_back(im);
00331 
00332       if (frame%100 == 0)
00333         {
00334           avgFR = ((float)frame/(float)(atime/1000));
00335           sprintf(s2,"FPS:%4.2F",avgFR);
00336           printf("FPS:%4.2F", avgFR);
00337 
00338           //Use decoder
00339           test->setupDecoder("Red", true);
00340           test->runDecoder(inStream, avgFR);
00341           float hertz = test->calculateHz();
00342           printf("\n\nFinal hertz calculated is: %f\n\n", hertz);
00343           inStream.clear();
00344         }
00345 
00346       glColor3f(0.0,1.0,1.0);
00347       setOrthographicProjection();
00348       glPushMatrix();
00349       glLoadIdentity();
00350       sprintf(s,"Position:%.1f,%.1f Depth:%.1f ",x,z,(16-y));
00351       renderBitmapString(30,15,(void *)font,s);
00352       //              sprintf(s,"F3-Change the signal, F4-Change the shape of the light");
00353       //              renderBitmapString(30,25,(void *)font,s);
00354       renderBitmapString(30,35,(void *)font,s2);
00355       glPopMatrix();
00356       resetPerspectiveProjection();
00357     }
00358 
00359   //===========================================
00360   // test the glReadPixels()
00361   // unsigned char *image;
00362   // assign 3 images get from cameras to image[]
00363 
00364   glutSwapBuffers();
00365 }
00366 void renderScene() {
00367   glutSetWindow(mainWindow);
00368   glClear(GL_COLOR_BUFFER_BIT);
00369   glutSwapBuffers();
00370 }
00371 
00372 void renderScenesw4() {
00373   glutSetWindow(subWindow4);
00374   glLoadIdentity();
00375 
00376   gluLookAt(x, y, z,
00377             x + lx,y + ly,z + lz,
00378             0.0f,1.0f,0.0f);
00379 
00380   renderScene2(subWindow4);
00381 }
00382 
00383 //=================================
00384 // control the submarine for high level command
00385 void advance(int steps)
00386 {
00387   deltaMove = steps;
00388 }
00389 /*
00390   void Turn(string s, double angle)
00391   {
00392   if(s=="LEFT")
00393   deltaAngle = -0.05f * angle;
00394   else
00395   deltaAngle = 0.05f * angle;
00396   }
00397 */
00398 //=================================
00399 void renderSceneAll() {
00400   if (deltaMove)
00401     moveMeFlat(deltaMove);
00402   if (deltaAngle) {
00403     fangle += deltaAngle;
00404     orientMe(fangle);
00405   }
00406   if (deltaDepth)
00407     moveMeVer(deltaDepth);
00408   renderScenesw4();
00409 }
00410 
00411 void processNormalKeys(unsigned char key, int x, int y) {
00412 
00413   if (key == 27)
00414     exit(0);
00415 }
00416 
00417 void pressKey(int key, int x, int y) {
00418 
00419   switch (key) {
00420   case GLUT_KEY_LEFT : deltaAngle = -0.01f;break;
00421   case GLUT_KEY_RIGHT : deltaAngle = 0.01f;break;
00422   case GLUT_KEY_UP : deltaMove = 1;break;
00423   case GLUT_KEY_DOWN : deltaMove = -1;break;
00424   case GLUT_KEY_F1 : deltaDepth = 1;break;
00425   case GLUT_KEY_F2 : deltaDepth = -1;break;
00426   case GLUT_KEY_F3 :
00427     decode++;
00428     if(decode>=4)
00429       decode = 0;
00430     break;
00431   case GLUT_KEY_F4 :
00432     shape = !shape;
00433     break;
00434   }
00435 }
00436 
00437 void releaseKey(int key, int x, int y) {
00438 
00439   switch (key) {
00440   case GLUT_KEY_LEFT :
00441     if (deltaAngle < 0.0f)
00442       deltaAngle = 0.0f;
00443     break;
00444   case GLUT_KEY_RIGHT :
00445     if (deltaAngle > 0.0f)
00446       deltaAngle = 0.0f;
00447     break;
00448   case GLUT_KEY_UP :
00449     if (deltaMove > 0)
00450       deltaMove = 0;
00451     break;
00452   case GLUT_KEY_DOWN :
00453     if (deltaMove < 0)
00454       deltaMove = 0;
00455     break;
00456   case GLUT_KEY_F1:
00457     if(deltaDepth > 0)
00458       deltaDepth =0;
00459     break;
00460   case GLUT_KEY_F2:
00461     if(deltaDepth < 0)
00462       deltaDepth =0;
00463     break;
00464   }
00465 }
00466 
00467 #endif // HAVE_GL_GLUT_H
00468 
00469 int main(int argc, char **argv)
00470 {
00471 #ifndef HAVE_GL_GLUT_H
00472 
00473   LFATAL("<GL/glut.h> must be installed to use this program");
00474 
00475 #else
00476 
00477   mgr.addSubComponent(test);
00478   mgr.start();
00479 
00480   glutInit(&argc, argv);
00481   glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
00482   glutInitWindowPosition(100,100);
00483   glutInitWindowSize(w,h);
00484   mainWindow = glutCreateWindow("Sub");
00485   glutIgnoreKeyRepeat(1);
00486   glutKeyboardFunc(processNormalKeys);
00487   glutSpecialFunc(pressKey);
00488   glutSpecialUpFunc(releaseKey);
00489   glutReshapeFunc(changeSize);
00490   glutDisplayFunc(renderScene);
00491   glutIdleFunc(renderSceneAll);
00492   subWindow4 = glutCreateSubWindow(mainWindow, 0,0,
00493                                    w,h);
00494   glutDisplayFunc(renderScenesw4);
00495   initScene();
00496   glutMainLoop();
00497 
00498   return(0);
00499 
00500 #endif // HAVE_GL_GLUT_H
00501 }
Generated on Sun May 8 08:04:33 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3