searchShape.C

00001 #include <math.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include <string.h>
00006 #include <GL/gl.h>
00007 #undef APIENTRY // otherwise it gets redefined between gl.h and glut.h???
00008 #include <GL/glut.h>
00009 #include "BO/functionsShape.H"
00010 #include "BO/defineShape.H"
00011 
00012 #include "Component/ModelManager.H"
00013 #include "Psycho/EyeTrackerConfigurator.H"
00014 #include "Psycho/EyeTracker.H"
00015 #include "Psycho/PsychoOpts.H"
00016 #include "Component/EventLog.H"
00017 #include "Component/ComponentOpts.H"
00018 #include "Util/MathFunctions.H"
00019 #include "Util/Types.H"
00020 
00021 int type;
00022 int trial;
00023 int record = 1;
00024 int /*start = 0,*/ tracking = 0;
00025 int target[STIM*2];
00026 float color[16];
00027 float hor[16] = {0.25,0.25,-0.25,-0.25, //x location of stimuli
00028                  -0.25,0.25,0.75,0.75,
00029                  0.75,0.75,0.25,-0.25,
00030                  -0.75,-0.75,-0.75,-0.75};
00031 float ver[16] = {0.25,-0.25,-0.25,0.25, // y location of stimuli
00032                  0.75,0.75,0.75,0.25,
00033                  -0.25,-0.75,-0.75,-0.75,
00034                  -0.75,-0.25,0.25,0.75};
00035 
00036 //time_t timer;
00037 double Rec_ES[100],Rec_RT[100];
00038 int place[100][16];
00039 int calib[9] = {0,0,0,0,0,0,0,0,0};
00040 int state_calib=0,flag_state=0;
00041 int input,occlude=0;
00042 int target_BO,target_Shape,target_stim,target_type,target_top,target_location;
00043 char subj[128];
00044 /*int BO_L[2][2] = {{1,3},{0,2}};
00045   int BO_Sq[2][2] = {{0,2},{1,3}};*/
00046 //time_t sec, min;
00047 time_t start_sec,start_min,last_sec,last_min;
00048 clock_t SS,ES;
00049 
00050 ModelManager manager("SearchShape");
00051 
00052 nub::soft_ref<EyeTrackerConfigurator>
00053 etc(new EyeTrackerConfigurator(manager));
00054 
00055 nub::soft_ref<EventLog> el(new EventLog(manager));
00056 //manager.addSubComponent(el);
00057 
00058 nub::soft_ref<EyeTracker> et;
00059 //et->setEventLog(el);
00060 
00061 int Require_Result(void)
00062 {
00063   int i,j;
00064   FILE *fp1,*fp2;
00065   char File_Time[128],File_Loc[128];
00066 
00067   /* Make & Open record file. */
00068   sprintf(File_Time,"%s_RT.data",subj);
00069   sprintf(File_Loc,"%s_Loc.data",subj);
00070 
00071   /* Write Answer */
00072   fp1=fopen(File_Time,"w");
00073   fp2=fopen(File_Loc,"w");
00074 
00075   for(i=0;i<trial;i++){
00076     fprintf(fp1,"%d\t%d\t%6.3f\t%6.3f\n",i,target[i],Rec_RT[i],Rec_ES[i]);
00077   }
00078 
00079   for(i=0;i<trial;i++){
00080     for(j=0;j<16;j++){
00081       fprintf(fp2,"%d",place[i][j]);
00082       if(j==15)fprintf(fp2,"\n");
00083       else fprintf(fp2,"\t");
00084     }
00085   }
00086 
00087   fclose(fp1);
00088   fclose(fp2);
00089   return 0;
00090 }
00091 
00092 void idle(void)
00093 {
00094   //glutPostRedisplay();
00095   if(input == 1)input = 0;
00096   //printf("idle = %d\n",input);
00097 }
00098 
00099 void InputName(char *str)
00100 {
00101   printf("Subject Name : ");
00102   if (scanf("%s",str) != 1) LFATAL("scanf failed");
00103   printf("%s\n",subj);
00104 }
00105 
00106 void CheckFormat(int argc)
00107 {
00108   if(argc!=2 && argc !=3){
00109     printf("Error\n./StimuliBO FileName\n");
00110     exit(0);
00111   }
00112   if(argc == 3)record = 0;
00113 }
00114 
00115 void myReshape(GLsizei w, GLsizei h)
00116 {
00117   glClearColor(BGC, BGC, BGC, 1.0);
00118   glClear(GL_COLOR_BUFFER_BIT);
00119   glViewport(0, 0, w, h);
00120   glMatrixMode(GL_PROJECTION);
00121   glLoadIdentity();
00122 
00123   /*
00124     if(w <= h)
00125     glOrtho( -1.3, 1.3, -1.0 * (GLfloat)h/(GLfloat)w,   // 1.3 for 18deg if assigned to 15)
00126     1.0 * (GLfloat)h/(GLfloat)w, -10.0, 10.0);
00127     else
00128     glOrtho( -1.3 * (GLfloat)h/(GLfloat)w,
00129     1.3 * (GLfloat)h/(GLfloat)w, -1.0, 1.0, -10.0, 10.0); //original
00130 
00131     if(w <= h)
00132     glOrtho( -1.75, 1.75, -1.0 * (GLfloat)h/(GLfloat)w,   // 1.3 for 18deg if assigned to 15)
00133     1.0 * (GLfloat)h/(GLfloat)w, -10.0, 10.0);
00134     else
00135     glOrtho( -1.75 * (GLfloat)h/(GLfloat)w,
00136     1.75 * (GLfloat)h/(GLfloat)w, -1.0, 1.0, -10.0, 10.0);
00137   */
00138 
00139   if(w <= h)
00140     glOrtho( -3.25, 3.25, -1.0 * (GLfloat)h/(GLfloat)w,   // 1.3 for 18deg if assigned to 15)
00141              1.0 * (GLfloat)h/(GLfloat)w, -10.0, 10.0);
00142   else
00143     glOrtho( -3.25 * (GLfloat)h/(GLfloat)w,
00144              3.25 * (GLfloat)h/(GLfloat)w, -1.0, 1.0, -10.0, 10.0);
00145 
00146   glMatrixMode(GL_MODELVIEW);
00147   glLoadIdentity();
00148 
00149 }
00150 
00151 void Init(int stimuli[], int array[], float color[])
00152 {
00153   int i;
00154 
00155   for(i = 0 ;i < STIM; i++){
00156     stimuli[i]=0;
00157   }
00158 
00159   for(i = 0 ; i < 16; i++) array[i] = 0;
00160 
00161   for(i = 0 ; i < 16; i++){
00162     color[i] = rand()%2;
00163   }
00164 }
00165 
00166 void setTargetShape(int stimuli[],int array[],int target_trial)//target_stim is conbination of Target shapes
00167 {
00168   struct tm *t_st;
00169   time_t timer;
00170 
00171   time(&timer);
00172   t_st = localtime(&timer);
00173 
00174   target_location = (rand()+t_st->tm_sec)%16;
00175   //pattern = search(target_trial);
00176   if((target_trial)%2==0)target_BO = 1; //left
00177   else target_BO = 0; //right
00178 
00179   target_Shape = selection(target_trial);
00180   fragStimuliShape(stimuli,target_trial,target_Shape);
00181 
00182   array[target_location] = 1;
00183   place[trial][target_location]=target_trial;
00184 }
00185 
00186 /* Display Stimulus */
00187 void display(void)
00188 {
00189   //int target_BO,target_Shape,target_stim,target_type,target_top,target_location;
00190   int stimuli[STIM],array[16];
00191 
00192   //printf("checking et false\n");
00193   //usleep(50000);
00194   //et->track(false);
00195 
00196   /*if(tracking == 0){
00197     printf("before start\n");
00198     //et->track(true);
00199     et->track(true);
00200     tracking = 1;
00201     printf("after start\n");
00202     }*/
00203 
00204   if(flag_state == 0){
00205     if(tracking == 0){
00206       if(state_calib>=3 && state_calib <= 11){
00207         //printf("before start\n");
00208         //et->track(true);
00209         et->track(true);
00210         tracking = 1;
00211         //printf("after start\n");
00212       }
00213     }
00214     Display_Calibration(state_calib,calib);
00215     if(tracking == 1){
00216       usleep(50000);
00217       et->track(false);
00218       tracking = 0;
00219     }
00220   }else{
00221     Init(stimuli,array,color);
00222     //printf("trial = %d\n",trial);
00223     //printf("before start\n");
00224     //et->track(true);
00225     et->track(true);
00226     tracking = 1;
00227     //printf("after start\n");
00228     printf("target[%d] = %d\n",trial,target[trial]);
00229     target_stim = target[trial];
00230     target_type = target_stim%4;
00231     setTargetShape(stimuli,array,target[trial]);
00232 
00233     //array[target_location] = 1;
00234 
00235     Display_Shape(target_Shape,target_stim,target_location,stimuli,array,place,trial,hor,ver,color);
00236 
00237     SS = clock();
00238   }
00239 }
00240 
00241 /* For Format */
00242 void init(void)
00243 {
00244   glClearColor(BGC, BGC, BGC, 0.0);
00245 }
00246 
00247 /* Exit Process of Key board*/
00248 void keyboard(unsigned char key,int x,int y)
00249 {
00250   double RT;
00251   //printf("checking et\n");
00252   //et->track(true);
00253   if(flag_state == 0){
00254     switch (key){ //exit when 'q', 'Q' or "ESC"
00255     case 'q':
00256     case 'Q':
00257     case '\033':
00258       {
00259         //printf("before stop\n");
00260         usleep(50000);
00261         et->track(false);
00262         tracking = 0;
00263         //printf("after stop\n");
00264         //printf("before final\n");
00265         manager.stop();//printf("after final\n");
00266         exit(0);
00267       }
00268 
00269     case ' ':
00270       {
00271         usleep(50000);
00272         et->track(false);
00273         //usleep(50000);
00274         //et->track(false);
00275         tracking = 0;
00276         state_calib++;
00277         if(state_calib==13){
00278           //printf("calibration is over\n");
00279           glClearColor(BGC, BGC, BGC, 0);
00280           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00281           Display_Blank();
00282           glutSwapBuffers();
00283           msleep(F_SLEEP*5);
00284           flag_state = 1;
00285         }
00286       }
00287     default:
00288       break;
00289     }
00290   }else{
00291     switch (key){ //exit when 'q', 'Q' or "ESC"
00292     case 'q':
00293     case 'Q':
00294     case '\033':
00295       {
00296         //printf("before stop\n");
00297         usleep(50000);
00298         et->track(false);
00299         tracking = 0;
00300         //printf("after stop\n");
00301         //printf("before final\n");
00302         manager.stop();
00303         exit(0);
00304       }
00305 
00306     case ' ':
00307       {
00308         usleep(50000);
00309         et->track(false);
00310         //usleep(50000);
00311         //et->track(false);
00312         tracking = 0;
00313         ES=clock();
00314         RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00315         Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00316         Rec_RT[trial]=RT;
00317         printf("time = %6.3f sec\n",Rec_ES[trial]);
00318         printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00319         trial++;
00320         //printf("time = %6.3f sec\n",(double)ES/CLOCKS_PER_SEC);
00321         //printf("Reaction time R = %6.3f sec\n",RT);
00322         glClearColor(BGC, BGC, BGC, 0);
00323         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00324         glutSwapBuffers();
00325         msleep(1000);
00326       }
00327 
00328     default:
00329       break;
00330     }
00331 
00332     if(trial == STIM*2){
00333       //printf("before stop\n");
00334       usleep(50000);
00335       et->track(false);
00336       tracking = 0;
00337       // printf("after stop\n");
00338       // printf("before final\n");
00339       manager.stop();
00340       Require_Result();
00341       exit(0);
00342     }
00343   }
00344   /*printf("before stop\n");
00345   usleep(50000);
00346   et->track(false);
00347   //usleep(50000);
00348   //et->track(false);
00349   tracking = 0;
00350   printf("after stop\n\n");*/
00351   glutPostRedisplay();
00352 }
00353 
00354 void ourInit(void)
00355 {
00356 
00357 
00358   glClearColor(BGC,BGC,BGC,0.0);
00359   glMatrixMode(GL_PROJECTION);
00360   glLoadIdentity();
00361   // glOrtho(-1.2,1.2,-1.0,1.0,-1.0,1.0); //
00362 
00363 
00364   glEnable(GL_LINE_SMOOTH);
00365   glEnable(GL_POINT_SMOOTH);
00366   glEnable(GL_POLYGON_SMOOTH);
00367 
00368   glEnable(GL_BLEND);
00369   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00370 
00371   glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);//GL_DONT_CARE);
00372 
00373 
00374 }
00375 
00376 void myInit(int target[])
00377 {
00378   int i;
00379 
00380   for(i=0;i<STIM*2;i++){
00381     if(i<STIM)target[i]=i;
00382     else target[i]=i-STIM;
00383     //printf("%d\n",target[i]);
00384   }
00385 
00386   shuffle(target,STIM*2);
00387 }
00388 
00389 void new_mouse(int button, int state,int x, int y)
00390 {
00391   double RT;
00392 
00393   switch(button){
00394   case GLUT_RIGHT_BUTTON:
00395     if(state == GLUT_DOWN)
00396       {
00397         ES=clock();
00398         RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00399         Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00400         Rec_RT[trial]=RT;
00401         printf("time = %6.3f sec\n",Rec_ES[trial]);
00402         printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00403         trial++;
00404         //printf("time = %6.3f sec\n",(double)ES/CLOCKS_PER_SEC);
00405         //printf("Reaction time R = %6.3f sec\n",RT);
00406 
00407         glClearColor(BGC, BGC, BGC, 0);
00408         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00409         glutSwapBuffers();
00410         msleep(1000);
00411       }
00412     break;
00413 
00414   case GLUT_LEFT_BUTTON:
00415     if(state == GLUT_DOWN)
00416       {
00417         ES=clock();
00418         RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00419         Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00420         Rec_RT[trial]=RT;
00421         printf("time = %6.3f sec\n",Rec_ES[trial]);
00422         printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00423         trial++;
00424         //printf("time = %6.3f sec\n",(double)ES/CLOCKS_PER_SEC);
00425         //printf("Reaction time L = %6.3f sec\n",RT);
00426 
00427         glClearColor(BGC, BGC, BGC, 0);
00428         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00429         glutSwapBuffers();
00430         msleep(1000);
00431       }
00432     break;
00433 
00434   default:
00435     break;
00436   }
00437   if(trial == STIM*2){
00438     Require_Result();
00439     exit(0);
00440   }
00441   glutPostRedisplay();
00442 }
00443 
00444 
00445 int main(int argc, char *argv[])
00446 {
00447   const char *name = "New Stimulus";
00448 
00449   MYLOGVERB = LOG_INFO;  // suppress debug messages
00450 
00451   manager.addSubComponent(etc);
00452   manager.addSubComponent(el);
00453 
00454   manager.setOptionValString(&OPT_EventLogFileName, "searchShape.log");
00455   manager.setOptionValString(&OPT_EyeTrackerType, "ISCAN");
00456 
00457   // Parse command-line:
00458   if (manager.parseCommandLine(argc, argv, "<fileName>", 1, 1) == false)
00459     return(1);
00460 
00461   et = etc->getET();
00462   et->setEventLog(el);
00463 
00464   manager.start();
00465   el->pushEvent(std::string("===== Trial : Search Shape ====="));
00466 
00467 
00468   //CheckFormat(argc);
00469   //glutInit(&argc, argv);
00470   strcpy(subj,argv[1]);
00471   //InputType();
00472   myInit(target);
00473   trial = 0;
00474 
00475   glClearColor(BGC,BGC,BGC,0);
00476   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00477 
00478   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); //for movie
00479   glutInitWindowPosition(100,100);
00480   glutInitWindowSize(500,500);
00481 
00482 
00483   //InputOcclude(&occlude);
00484 
00485 
00486   //glutInitDisplayMode(GLUT_RGBA);
00487   glutCreateWindow(name);
00488 
00489   glutFullScreen();
00490 
00491   ourInit();
00492   glutReshapeFunc(myReshape);
00493   //glutKeyboardFunc(keyboard);
00494   glutDisplayFunc(display);
00495   glutKeyboardFunc(keyboard);
00496   glutIdleFunc(idle);
00497   glutMouseFunc(new_mouse);
00498   //glutKeyboardFunc(keyboard);
00499   //printf("trial = %d\n",trial);
00500   //glutDisplayFunc(display);
00501 
00502   glutMainLoop();
00503   return 0;
00504 }
Generated on Sun May 8 08:40:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3