screen_information_alg.cpp
Go to the documentation of this file.
00001 #include "screen_information_alg.h"
00002 
00003 ScreenInformationAlgorithm::ScreenInformationAlgorithm(void)
00004 {
00005   this->code = 0;
00006   
00007   //default own (robot/person) location (orange circle in map)
00008   ownX=1050;
00009   ownY=900;
00010 }
00011 
00012 ScreenInformationAlgorithm::~ScreenInformationAlgorithm(void)
00013 {
00014   cv::destroyWindow(WINDOW);
00015 }
00016 
00017 void ScreenInformationAlgorithm::config_update(Config& new_cfg, uint32_t level)
00018 {
00019   this->lock();
00020 
00021   // save the current configuration
00022   this->path =  new_cfg.path;
00023   this->config_=new_cfg;
00024   
00025   this->unlock();
00026 }
00027 
00028 //initialize: creates window and loads images from files
00029 void ScreenInformationAlgorithm::initialize()
00030 {
00031   std::string mapPath  = this->path + "/images/maps";
00032   std::string logoPath = this->path + "/images/logos";
00033 
00034   this->createWindow(WINDOW);
00035 
00036   cv::Mat blankImage(cv::Size(2048, 1536), CV_8UC3, CV_RGB(255, 255, 255));
00037   cv::Mat blankImage2(cv::Size(2048, 1536), CV_8UC3, CV_RGB(252, 235, 204));
00038   cv::Mat mapImage  = this->loadImage(mapPath  + "/map.jpg",CV_LOAD_IMAGE_COLOR);
00039   cv::Mat logoImage = this->loadImage(logoPath + "/iri2.png",CV_LOAD_IMAGE_COLOR);
00040   
00041   //Enlarge logo
00042   cv::Mat logoImageResize;
00043   cv::resize(logoImage, logoImageResize, cv::Size(), 3, 3, CV_INTER_LINEAR );
00044   logoImage=logoImageResize;
00045 
00046   // littleImage.copyTo(bigImage(cv::Rect(col0, row0, cols, rows)));
00047 
00048   //Put logo image in the center of the blank image
00049   cv::Mat logoBlankImage = blankImage.clone();
00050   logoImage.copyTo(logoBlankImage(cv::Rect(blankImage.cols/2-logoImage.cols/2, blankImage.rows/2-logoImage.rows/2, logoImage.cols, logoImage.rows)));
00051   
00052   
00053 //   cv::Mat mapImageResize;
00054 //   cv::resize(mapImage, mapImageResize, cv::Size(2048, 1536*mapImage.rows/2048), 0,0, CV_INTER_LINEAR);
00055 //   mapImage=mapImageResize;
00056   //cv::Mat mapBlankImage 
00057   
00058   this->images.push_back(blankImage);     //0
00059   this->images.push_back(blankImage);     //1
00060   this->images.push_back(blankImage2);    //2
00061   this->images.push_back(mapImage);       //3
00062   this->images.push_back(logoBlankImage); //4
00063   
00064   this->texts.push_back("Stand-by");
00065   this->texts.push_back("Calculating ambient noise...");
00066   this->texts.push_back("Waiting for a question...");
00067   this->texts.push_back("Recording sound...");
00068   this->texts.push_back("Adquiring sound...");
00069   this->texts.push_back("Processing sound...");
00070   this->texts.push_back("Processing your question...");
00071   
00072   this->colors.push_back(CV_RGB(240, 100,   0)); //orange  0
00073   this->colors.push_back(CV_RGB(  0, 240,   0)); //green   1
00074   this->colors.push_back(CV_RGB(  0,   0, 240)); //blue    2
00075   this->colors.push_back(CV_RGB(  0, 240, 240)); //cyan    3
00076   this->colors.push_back(CV_RGB(240,   0, 240)); //pink    4
00077   this->colors.push_back(CV_RGB(240, 240,   0)); //yellow  5
00078   this->colors.push_back(CV_RGB(  0,   0,   0)); //black   6
00079   this->colors.push_back(CV_RGB(240, 240, 240)); //white   7
00080   
00081   
00082 }
00083 
00084 //loadImage: returns cv image given path and mode
00085 cv::Mat ScreenInformationAlgorithm::loadImage(std::string path, int mode)
00086 {
00087   //Load cv image from file
00088   cv::Mat loadedImage = cv::imread(path, mode); 
00089   //mode
00090   // 0 CV_LOAD_IMAGE_GRAYSCALE 
00091   // 1 CV_LOAD_IMAGE_COLOR 
00092   //-1 CV_LOAD_IMAGE_UNCHANGED
00093   //   CV_LOAD_IMAGE_ANYDEPTH
00094   //   CV_LOAD_IMAGE_ANYCOLOR
00095   if(!loadedImage.data )
00096   {
00097     ROS_INFO("Could not open or find the image: %s", path.c_str());
00098     exit(1);
00099   }
00100   else
00101   {
00102     ROS_INFO("Loaded image: %s", path.c_str());
00103     return loadedImage;
00104   }
00105 }
00106 
00107 //createWindow: creates the window for display
00108 void ScreenInformationAlgorithm::createWindow(const std::string& window)
00109 {
00110   cv::namedWindow(window, CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO); // Create a window for display
00111   //cv::setWindowProperty(window, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); // Fullscreen mode
00112   cv::resizeWindow(window, 1024, 768); // Resize the window to desired resolution
00113 }
00114 
00115 //showImage: shows the image inside the window
00116 void ScreenInformationAlgorithm::showImage(const std::string& window, cv::Mat image)
00117 {
00118   //this->createWindow(window);
00119   cv::imshow(window, image); // Show our image inside the window.
00120   cv::waitKey(30);
00121 }
00122 
00123 //getImageIndex: depending on code, gives index of blank, blank2, or map image in images[]
00124 int ScreenInformationAlgorithm::getImageIndex(int code)
00125 {
00126   switch(code)
00127   {
00128     case 0:
00129     case 1:
00130     case 2:
00131     case 3:
00132     case 4:
00133     case 5:
00134     case 6:
00135       return 1; // satus image
00136       break;
00137     default:
00138       if(code<100) // normal answers
00139       {
00140         return 2; // text answer image        
00141       }
00142       else // location answers
00143       {
00144         return 3; // map answer image
00145       }
00146       break;
00147   }
00148 
00149 }
00150 
00151 //addText: adds text to image[0] depending on image index i and code
00152 void ScreenInformationAlgorithm::addText(int i, int code)
00153 {
00154   //Font and text 
00155   int    fontFace      = CV_FONT_HERSHEY_SIMPLEX;
00156   double fontScale     = 2.0; //22 height of CV_FONT_HERSHEY_SIMPLEX at scale 1
00157   float  fontShare     = 0.0; //no italic
00158   int    fontThickness = 2.0;
00159   int    fontLineType  = CV_AA;
00160  
00161   CvFont font;
00162   cv::Size textSize;
00163   cvInitFont(&font, fontFace , fontScale, fontScale, fontShare, fontThickness, fontLineType);
00164 
00165   //Adquisition status (show texts[code])
00166   if(code<10)
00167   {
00168     //ROS_INFO("ScreenInformationAlgorithm: this->code = %s", this->texts[code].c_str());
00169     putText(this->images[0], this->texts[code].c_str(), cv::Point(100, this->images[0].rows/2), fontFace, fontScale, colors[code], fontThickness, fontLineType);
00170   }
00171   //Giving answer (show this->answer)
00172   else if(code<100)
00173   {
00174     //Make answer text fit in the image (reducing fontScale until it does)
00175     cv::Size textSize = cv::getTextSize(this->texts[code].c_str(), fontFace, fontScale, fontThickness, 0);
00176     while((textSize.width > this->images[0].cols - 50) && (fontScale >1))
00177     {
00178       fontScale*=0.9;
00179       textSize = cv::getTextSize(this->texts[code].c_str(), fontFace, fontScale, fontThickness, 0);
00180     }
00181     
00182     //ROS_INFO("ScreenInformationAlgorithm: this->code = %s", this->answer.c_str());
00183     putText(this->images[0], this->answer.c_str(),        cv::Point(100, this->images[0].rows/2), fontFace, fontScale, colors[0], fontThickness, fontLineType);
00184   }
00185   //Giving location answer (show caption and locations)
00186   else
00187   {
00188     this->drawCircle(50,50,20,colors[0]);
00189     putText(this->images[0], "YOUR CURRENT LOCATION", cv::Point(100,  50+20), fontFace, fontScale, colors[0], fontThickness, fontLineType);
00190     this->drawCircle(50,100,20,colors[1]);
00191     putText(this->images[0], "DESTINATION",           cv::Point(100, 100+20), fontFace, fontScale, colors[1], fontThickness, fontLineType);
00192   }
00193 }
00194 
00195 //addOwnLocation: adds orange circle to map on input x y location
00196 void ScreenInformationAlgorithm::addOwnLocation(int x, int y)
00197 {
00198   this->drawCircle(x,y,30,colors[0]);
00199 }
00200 
00201 //addLocationWithCode: adds green circle to map on predefined x y location depending on code
00202 void ScreenInformationAlgorithm::addLocationWithCode(int code)
00203 {
00204   int x=0,y=0;
00205   
00206   switch(code)
00207   {
00208     case  100: x =  882; y = 1035; break; //biblioteca
00209     case  101: x =  666; y =  678; break; //plaza caminos
00210     case  102: x = 1420; y =  672; break; //plaza informatica
00211     case  103: x =  450; y = 1113; break; //nexus
00212     case  104: x = 1467; y = 1485; break; //nexus2
00213     case  105: x = 1350; y = 1074; break; //omega
00214     case  106: x = 1083; y =  618; break; //optica
00215     case  107: x = 1050; y = 1030; break; //polideportivo
00216     case  108: x = 2120; y = 1400; break; //rectorado
00217     case  109: x = 1044; y =  678; break; //plaza telecos
00218     case  110: x = 2150; y =  447; break; //vertex
00219     case 201: x =  575; y =  825; break; //a1...6
00220     case 202: x =  765; y =  825; break;
00221     case 203: x =  954; y =  825; break;
00222     case 204: x = 1140; y =  825; break;
00223     case 205: x = 1333; y =  825; break;
00224     case 206: x = 1518; y =  825; break;
00225     case 211: x =  507; y =  666; break; //b1...6
00226     case 212: x =  807; y =  666; break;
00227     case 213: x =  921; y =  666; break;
00228     case 214: x = 1185; y =  666; break;
00229     case 215: x = 1296; y =  666; break;
00230     case 216: x = 1560; y =  666; break;
00231     case 221: x =  441; y =  560; break; //c1...6
00232     case 222: x =  768; y =  560; break;
00233     case 223: x =  957; y =  560; break;
00234     case 224: x = 1149; y =  560; break;
00235     case 225: x = 1335; y =  560; break;
00236     case 226: x = 1521; y =  560; break;
00237     case 231: x =  420; y =  429; break; //d1...6
00238     case 232: x =  768; y =  429; break;
00239     case 233: x =  957; y =  429; break;
00240     case 234: x = 1149; y =  429; break;
00241     case 235: x = 1335; y =  429; break;
00242     case 236: x = 1521; y =  429; break;
00243     default:  x =    0; y =    0; break;
00244   }
00245   
00246   this->drawCircle(x,y,30,colors[1]);
00247 }
00248 
00249 //drawCircle: draws a circle on image[0] at input x y location and with input radius and color
00250 void ScreenInformationAlgorithm::drawCircle(int x, int y, int r, CvScalar color)
00251 {
00252   int img_x = x; //*this->image.cols;
00253   int img_y = y; //*this->image.rows;
00254   
00255   cv::circle(this->images[0], cv::Point(img_x, img_y), r, color, -1);
00256   cv::circle(this->images[0], cv::Point(img_x, img_y), r, CV_RGB(0,0,0), 2);
00257   
00258   if( ( img_x+r > this->images[0].cols ) || ( img_y+r > this->images[0].rows ) || (img_x-r < 0 ) || (img_y-r < 0) )
00259     ROS_INFO("Circle out of bounds");
00260 }
00261 
00262 // ScreenInformationAlgorithm Public API
00263 
00264 //iteration::performs an iteration
00265 void ScreenInformationAlgorithm::iteration()
00266 {
00267   if(this->lastCode != this->code)
00268   {
00269     int i=getImageIndex(this->code); //1 = blank, 2=blank2, 3=map
00270     this->images[0] = this->images[i].clone(); //0 = image to modify and display
00271     this->addText(i,this->code);
00272     if(i==3) //map
00273     {
00274      //orange location (arbitrary chosen)
00275      this->addOwnLocation(ownX, ownY);
00276      //green location depending on code
00277      this->addLocationWithCode(this->code); 
00278     }
00279     //display image
00280     this->showImage(WINDOW, this->images[0]);
00281   }  
00282 }
00283 
00284 void ScreenInformationAlgorithm::setCode(int newCode)
00285 {
00286   this->lastCode = this->code;
00287   this->code=newCode;
00288   //ROS_INFO("ScreenInformationAlgorithm: this->code set to = %i", this->code);
00289 }
00290 
00291 void ScreenInformationAlgorithm::setAnswer(std::string newAnswer)
00292 {
00293   this->answer=newAnswer;
00294 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


iri_screen_information
Author(s): fherrero
autogenerated on Mon Jan 14 2013 13:12:37