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