tibi_dabo_questions_alg.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_questions_alg.h"
00002 
00003 TibiDaboQuestionsAlgorithm::TibiDaboQuestionsAlgorithm(void)
00004 {
00005   this->showingAnswer_=false;
00006 }
00007 
00008 TibiDaboQuestionsAlgorithm::~TibiDaboQuestionsAlgorithm(void)
00009 {
00010   cv::destroyWindow(WINDOW);
00011 }
00012 
00013 void TibiDaboQuestionsAlgorithm::config_update(Config& new_cfg, uint32_t level)
00014 {
00015   this->lock();
00016 
00017   // save the current configuration
00018   this->config_=new_cfg;
00019   
00020   this->unlock();
00021 }
00022 
00023 cv::Mat TibiDaboQuestionsAlgorithm::loadImage(std::string path, int read_mode)
00024 {
00025   //Load cv image from file
00026   cv::Mat loadedImage = cv::imread(path, read_mode); 
00027   //read_mode
00028   // 0 CV_LOAD_IMAGE_GRAYSCALE 
00029   // 1 CV_LOAD_IMAGE_COLOR 
00030   //-1 CV_LOAD_IMAGE_UNCHANGED
00031   //   CV_LOAD_IMAGE_ANYDEPTH
00032   //   CV_LOAD_IMAGE_ANYCOLOR
00033   if(!loadedImage.data )
00034   {
00035     ROS_INFO("TibiDaboQuestionsAlgorithm::loadImage: Could not open or find the image: %s", path.c_str());
00036     exit(1);
00037   }
00038   else
00039   {
00040     ROS_INFO("TibiDaboQuestionsAlgorithm::loadImage: Loaded image: %s", path.c_str());
00041     return loadedImage;
00042   }
00043 }
00044 
00045 void TibiDaboQuestionsAlgorithm::createWindow(const std::string& window)
00046 {
00047   cv::namedWindow(window, CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO);
00048   cv::resizeWindow(window, 1024, 768); // Resize the window to desired resolution
00049   //cv::setWindowProperty(window, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); // Fullscreen mode
00050 }
00051 
00052 void TibiDaboQuestionsAlgorithm::showImage(const std::string& window, cv::Mat image)
00053 {
00054   this->createWindow(WINDOW);
00055   cv::imshow(window, image); // Show our image inside the window.
00056   cv::waitKey(30);
00057 }
00058 
00059 void TibiDaboQuestionsAlgorithm::drawCircle(int x, int y, int r, CvScalar color)
00060 {
00061   int img_x = x; //*this->image.cols;
00062   int img_y = y; //*this->image.rows;
00063   
00064   cv::circle(this->image, cv::Point(img_x, img_y), r, color, -1);
00065   cv::circle(this->image, cv::Point(img_x, img_y), r, CV_RGB(0,0,0), 2);
00066   
00067   if( ( img_x+r > this->image.cols ) || ( img_y+r > this->image.rows ) || (img_x-r < 0 ) || (img_y-r < 0) )
00068     ROS_INFO("TibiDaboQuestionsAlgorithm::drawCircle: Circle out of bounds");
00069 }
00070 
00071 void TibiDaboQuestionsAlgorithm::drawAskedLocation(int code)
00072 {
00073   int x=0,y=0;
00074   
00075   switch(code)
00076   {
00077     case  100: x =  882; y = 1035; break; //biblioteca
00078     case  101: x =  666; y =  678; break; //plaza caminos
00079     case  102: x = 1420; y =  672; break; //plaza informatica
00080     case  103: x =  450; y = 1113; break; //nexus
00081     case  104: x = 1467; y = 1485; break; //nexus2
00082     case  105: x = 1350; y = 1074; break; //omega
00083     case  106: x = 1083; y =  618; break; //optica
00084     case  107: x = 1050; y = 1030; break; //polideportivo
00085     case  108: x = 2120; y = 1400; break; //rectorado
00086     case  109: x = 1044; y =  678; break; //plaza telecos
00087     case  110: x = 2150; y =  447; break; //vertex
00088     case 201: x =  575; y =  825; break; //a1...6
00089     case 202: x =  765; y =  825; break;
00090     case 203: x =  954; y =  825; break;
00091     case 204: x = 1140; y =  825; break;
00092     case 205: x = 1333; y =  825; break;
00093     case 206: x = 1518; y =  825; break;
00094     case 211: x =  507; y =  666; break; //b1...6
00095     case 212: x =  807; y =  666; break;
00096     case 213: x =  921; y =  666; break;
00097     case 214: x = 1185; y =  666; break;
00098     case 215: x = 1296; y =  666; break;
00099     case 216: x = 1560; y =  666; break;
00100     case 221: x =  441; y =  560; break; //c1...6
00101     case 222: x =  768; y =  560; break;
00102     case 223: x =  957; y =  560; break;
00103     case 224: x = 1149; y =  560; break;
00104     case 225: x = 1335; y =  560; break;
00105     case 226: x = 1521; y =  560; break;
00106     case 231: x =  420; y =  429; break; //d1...6
00107     case 232: x =  768; y =  429; break;
00108     case 233: x =  957; y =  429; break;
00109     case 234: x = 1149; y =  429; break;
00110     case 235: x = 1335; y =  429; break;
00111     case 236: x = 1521; y =  429; break;
00112     default:  x =    0; y =    0; break;
00113   }
00114   
00115   this->drawCircle(x,y,30,this->colors[1]);
00116 }
00117 
00118 // TibiDaboQuestionsAlgorithm Public API
00119 void TibiDaboQuestionsAlgorithm::initialize()
00120 {
00121   this->createWindow(WINDOW);
00122   
00123   cv::Mat backgroundBlank(cv::Size(2048, 1536), CV_8UC3, CV_RGB(255, 255, 255));
00124   cv::Mat backgroundBlank2(cv::Size(2048, 1536), CV_8UC3, CV_RGB(252, 235, 204));
00125   cv::Mat backgroundBlack(cv::Size(2048, 1536), CV_8UC3, CV_RGB(0, 0, 0));
00126 
00127   cv::Mat mapImage  = this->loadImage(this->path_ + "/data" + "/map.jpg",CV_LOAD_IMAGE_COLOR);
00128 
00129   cv::Mat logoImage = this->loadImage(this->path_ + "/data" + "/iri.png",CV_LOAD_IMAGE_COLOR);
00130   cv::Mat logoImageResize;
00131   cv::resize(logoImage, logoImageResize, cv::Size(), 2, 2, CV_INTER_LINEAR );
00132   logoImage=logoImageResize;
00133   cv::Mat backgroundLogo = backgroundBlank.clone();
00134   logoImage.copyTo(backgroundLogo(cv::Rect(backgroundBlank.cols/2-logoImage.cols/2, backgroundBlank.rows/2-logoImage.rows/2, logoImage.cols, logoImage.rows)));
00135 
00136   this->images.push_back(backgroundBlank);  // 0
00137   this->images.push_back(backgroundBlank2); // 1
00138   this->images.push_back(mapImage);         // 2
00139   this->images.push_back(backgroundLogo);   // 3
00140   this->images.push_back(backgroundBlack);  // 4
00141   
00142   
00143   this->fontFace      = CV_FONT_HERSHEY_SIMPLEX;
00144   this->fontScale     = 4.0; //22 height of CV_FONT_HERSHEY_SIMPLEX at scale 1
00145   this->fontShare     = 0.0; //no italic
00146   this->fontThickness = 2.0;
00147   this->fontLineType  = CV_AA;
00148   
00149   cvInitFont(&(this->font), this->fontFace , this->fontScale, this->fontScale, this->fontShare, this->fontThickness, this->fontLineType);
00150 
00151   this->texts.push_back("En espera");
00152   this->texts.push_back("Calculando ruido ambiente...");
00153   this->texts.push_back("Pregunta!");
00154   this->texts.push_back("Grabando...");
00155   this->texts.push_back("Adquiriendo sonido...");
00156   this->texts.push_back("Procesando sonido...");
00157   this->texts.push_back("Calculando respuesta...");
00158   
00159   this->colors.push_back(CV_RGB(240, 100,   0)); //orange  0
00160   this->colors.push_back(CV_RGB(  0, 240,   0)); //green   1
00161   this->colors.push_back(CV_RGB(  0,   0, 240)); //blue    2
00162   this->colors.push_back(CV_RGB(  0, 240, 240)); //cyan    3
00163   this->colors.push_back(CV_RGB(240,   0, 240)); //pink    4
00164   this->colors.push_back(CV_RGB(240, 240,   0)); //yellow  5
00165   this->colors.push_back(CV_RGB(  0,   0,   0)); //black   6
00166   this->colors.push_back(CV_RGB(240, 240, 240)); //white   7
00167   this->colors.push_back(CV_RGB(255,   0,   0)); //red     8
00168 }
00169 
00170 void TibiDaboQuestionsAlgorithm::iteration(int newStatus, std::string newAnswer)
00171 {
00172   //ROS_INFO("TibiDaboQuestionsAlgorithm::iteration: status, answer: %i, %s", newStatus, newAnswer.c_str());
00173   this->status_ = newStatus;
00174   this->answer_ = newAnswer;
00175   this->answer_[this->answer_.size()-1]=' ';
00176   int currentColor;
00177   if(this->status_==2)
00178   {
00179     currentColor = 1;
00180     this->fontThickness = 4;
00181     fontScale = 2.5;
00182   }
00183   else if(this->status_==3)
00184   {
00185     currentColor = 8;
00186     this->fontThickness = 4;
00187     fontScale = 2.5;
00188   }
00189   else
00190   {
00191     currentColor = 6;
00192     this->fontThickness = 2;
00193     fontScale = 2;
00194   }
00195   
00196   switch(this->status_)
00197   {
00198     case -1: 
00199       this->image = this->images[3].clone();
00200       break;
00201     case 0:
00202     case 1:
00203     case 2:
00204     case 3:
00205     case 4:
00206     case 5:
00207     case 6:
00208       //Status texts
00209       //Load background image
00210       if(this->status_==2 || this->status_==3)
00211         this->image = this->images[4].clone();
00212       else
00213         this->image = this->images[0].clone();
00214       //Show status text
00215       putText(this->image, this->texts[this->status_].c_str(), cv::Point(100, this->image.rows/2), fontFace, 2*fontScale, this->colors[currentColor], fontThickness, fontLineType);
00216       break;
00217     default:
00218       this->showingAnswer_=true;
00219       if(this->status_ < 100)
00220       {
00221         //Normal answer
00222         //Load background image
00223         this->image = this->images[1].clone();
00224         //Make answer text fit in the image (reducing fontScale until it does)
00225         cv::Size textSize = cv::getTextSize(this->answer_.c_str(), this->fontFace, this->fontScale, this->fontThickness, 0);
00226         while((textSize.width > this->image.cols - 50) && (fontScale >1.2))
00227         {
00228           fontScale-=0.1;
00229           ROS_INFO("fontScale %f", fontScale);
00230           textSize = cv::getTextSize(this->answer_.c_str(), this->fontFace, this->fontScale, this->fontThickness, 0);
00231         }
00232         if(fontScale > 1.2)
00233         {
00234           //Show answer text
00235           putText(this->image, this->answer_.c_str(), cv::Point(100, this->image.rows/2), this->fontFace, this->fontScale, this->colors[0], this->fontThickness, this->fontLineType);
00236           this->fontScale=2.0;
00237           break;
00238         }
00239         else
00240         {
00241           //Show answer text split
00242           std::string answer1 = this->answer_.substr(0,this->answer_.size()/2);
00243           std::string answer2 = this->answer_.substr(this->answer_.size()/2,this->answer_.size()-1);
00244           ROS_INFO("split answer: %s", answer1.c_str());
00245           ROS_INFO("split answer: %s", answer2.c_str());
00246           putText(this->image, answer1.c_str(), cv::Point(100, this->image.rows/2-50), this->fontFace, this->fontScale, this->colors[0], this->fontThickness, this->fontLineType);
00247           putText(this->image, answer2.c_str(), cv::Point(100, this->image.rows/2+50), this->fontFace, this->fontScale, this->colors[0], this->fontThickness, this->fontLineType);
00248           this->fontScale=2.0;
00249           break;
00250         }
00251       }
00252       else
00253       {
00254         // Location answer
00255         // Load map image
00256         this->image = images[2].clone();
00257         //Draw own location
00258         int ownX = 1050;
00259         int ownY = 900;
00260         this->drawCircle(ownX,ownY,30,colors[0]);
00261         //Draw asked location
00262         this->drawAskedLocation(this->status_);
00263         //Draw locations caption
00264         this->drawCircle(50,50,20,this->colors[0]);
00265         putText(this->image, "YOUR CURRENT LOCATION", cv::Point(100,  50+20), fontFace, fontScale, this->colors[0], fontThickness, fontLineType);
00266         this->drawCircle(50,100,20,this->colors[1]);
00267         putText(this->image, "DESTINATION",           cv::Point(100, 100+20), fontFace, fontScale, this->colors[1], fontThickness, fontLineType);
00268         break;        
00269       }
00270   }
00271   //display image
00272   this->showImage(WINDOW, this->image);
00273 }


tibi_dabo_questions
Author(s): fherrero
autogenerated on Fri Dec 6 2013 21:16:32