map_location_alg.cpp
Go to the documentation of this file.
00001 #include "map_location_alg.h"
00002 
00003 MapLocationAlgorithm::MapLocationAlgorithm(void)
00004 {
00005   
00006  
00007   
00008   this->colorList.push_back(CV_RGB(240, 100,   0)); //orange  0
00009   this->colorList.push_back(CV_RGB(  0, 240,   0)); //green   1
00010   this->colorList.push_back(CV_RGB(  0,   0, 240)); //blue    2
00011   this->colorList.push_back(CV_RGB(  0, 240, 240)); //cyan    3
00012   this->colorList.push_back(CV_RGB(240,   0, 240)); //pink    4
00013   this->colorList.push_back(CV_RGB(240, 240,   0)); //yellow  5
00014   this->colorList.push_back(CV_RGB(  0,   0,   0)); //black   6
00015   this->colorList.push_back(CV_RGB(240, 240, 240)); //white   7
00016 
00017   this->textList.push_back("Your current location");
00018   this->textList.push_back("Asked destination"); 
00019   this->textList.push_back("Another helpful robot");
00020   this->textList.push_back("Transport vehicle");
00021   this->textList.push_back("Interesting place");
00022   this->textList.push_back("Extra information");
00023   
00024   this->initialize();
00025   t=0;
00026 }
00027 
00028 MapLocationAlgorithm::~MapLocationAlgorithm(void)
00029 {
00030   cv::destroyWindow(WINDOW);
00031 }
00032 
00033 void MapLocationAlgorithm::config_update(Config& new_cfg, uint32_t level)
00034 {
00035   this->lock();
00036 
00037   // save the current configuration
00038   this->path =  new_cfg.path;
00039   this->config_=new_cfg;
00040   
00041   this->unlock();
00042 }
00043 
00044 // MapLocationAlgorithm Public API
00045 void MapLocationAlgorithm::initialize()
00046 {
00047 
00048 }
00049 
00050 void MapLocationAlgorithm::mapIteration()
00051 {
00052   //ROS_INFO("MapLocationAlgorithm::mapIteration");
00053   this->initialize();
00054   this->image = this->mapImage.clone();
00055   if(this->locations.size()!=0)
00056   {
00057     this->loadCaption();
00058     this->loadLocations();
00059     t=t+10;
00060   }
00061   //this->showImage(); //deprecated
00062 }
00063 
00064 void MapLocationAlgorithm::logoIteration()
00065 {
00066   //ROS_INFO("MapLocationAlgorithm::logoIteration");
00067   cv::Mat img1 = this->logoImage;
00068   cv::Mat img2 = this->logo2Image;
00069   cv::Mat img3(cv::Size(img2.cols, img2.rows+img2.rows), CV_8UC3, CV_RGB(255, 255, 255));
00070 
00071   //img1.copyTo(img3(cv::Rect(0, 0        , img1.cols, img1.rows)));
00072   img2.copyTo(img3(cv::Rect(0, 0, img2.cols, img2.rows)));
00073   img2.copyTo(img3(cv::Rect(0, img2.rows, img2.cols, img2.rows)));
00074   
00075   this->image = img3;
00076 }
00077 
00078 cv::Mat MapLocationAlgorithm::loadImage(std::string imagePath, int mode)
00079 {
00080   //Load cv image from file
00081   cv::Mat loadedImage = cv::imread(imagePath, mode); 
00082     //mode
00083     // 0 CV_LOAD_IMAGE_GRAYSCALE 
00084     // 1 CV_LOAD_IMAGE_COLOR 
00085     //-1 CV_LOAD_IMAGE_UNCHANGED
00086   if(! loadedImage.data )
00087     ROS_INFO("Could not open or find the image: %s", imagePath.c_str());
00088   else
00089     ROS_INFO("Loaded image: %s", imagePath.c_str());
00090   return loadedImage;
00091 }
00092 
00093 void MapLocationAlgorithm::loadCaption()
00094 {
00095   int height = 400;
00096   int margin = 20;
00097   
00098   int width  = height*3;
00099   int startx = margin;
00100   int starty = this->image.rows - height - margin;
00101   
00102   //Background caption rectangle
00103   cv::Point pt1(startx, starty);
00104   cv::Point pt2(startx+width, starty+height);
00105   cv::rectangle(this->image, pt1, pt2, colorList[7], CV_FILLED, CV_AA, 0);
00106   cv::rectangle(this->image, pt1, pt2, colorList[6], 2,         CV_AA, 0);
00107 
00108   //Font and text 
00109   int    fontFace      = CV_FONT_HERSHEY_SIMPLEX;
00110   double fontScale     = height / (this->locations.size()*22); //22 height of CV_FONT_HERSHEY_SIMPLEX at scale 1
00111   float  fontShare     = 0.0; //no italic
00112   int    fontThickness = 3.0;
00113   int    fontLineType  = CV_AA;
00114   
00115   int blankSpace    = 100;
00116   int verticalSpace = 30;
00117   
00118   CvFont font;
00119   cv::Size textSize;
00120   this->initText(&textSize, &font, fontFace, fontScale, fontShare, fontThickness, fontLineType, currentText);
00121 
00122   //Make text fit in height
00123   while( signed(this->locations.size())*textSize.height + (signed(this->locations.size())+1)*verticalSpace > height)
00124   {
00125     this->initText(&textSize, &font, fontFace, fontScale, fontShare, fontThickness, fontLineType, currentText);
00126     fontScale*=0.9;
00127     if(fontScale<0.5)
00128       break;
00129   }
00130   //Make text fit in width
00131   while(textSize.width + blankSpace > width)
00132   {
00133     this->initText(&textSize, &font, fontFace, fontScale, fontShare, fontThickness, fontLineType, currentText);
00134     fontScale*=0.9;
00135     if(fontScale<0.5)
00136       break;
00137   }
00138   //Enlarge text to fill caption width and height
00139   while((( this->locations.size()*textSize.height*1.1 + (this->locations.size()+1)*verticalSpace) < height ) && ( ( textSize.width*1.1 + blankSpace) < width ))
00140   {
00141     this->initText(&textSize, &font, fontFace, fontScale, fontShare, fontThickness, fontLineType, currentText);
00142     fontScale*=1.1;
00143     if(fontScale>4)
00144       break;
00145   }
00146   //Draw text and circles
00147   IplImage dst_img = this->image;
00148   for(unsigned int i=0; i<this->locations.size(); i++)
00149   {
00150     cvPutText(&dst_img, this->currentText[i].c_str(), cv::Point(pt1.x + blankSpace, pt1.y + (i+1)*verticalSpace + (i+1)*textSize.height),   &font, currentColor[i]);
00151     this->drawCircle( startx + blankSpace/2, starty + (i+1)*verticalSpace + (i+(1./2.))*textSize.height      , 30, currentColor[i]);
00152   }
00153 }
00154 
00155 void MapLocationAlgorithm::loadLocations()
00156 {
00157   for(unsigned int i=0; i<locations.size(); i++)
00158   {
00159     int x  = locations[i][0];
00160     int y  = locations[i][1];
00161     int r  = locations[i][2];
00162     //int id = locations[i][3];
00163     this->drawCircle(x,y,r,currentColor[i]);
00164     //ROS_INFO("Drawing location: %i, %i, %i, %i", x, y, r, id);
00165   }
00166 }
00167 
00168 void MapLocationAlgorithm::initText(cv::Size * textSize, CvFont * font, int const fontFace, double const fontScale, float const fontShare, int const fontThickness, int const fontLineType, std::vector<std::string> const currentText)
00169 {
00170   textSize->width=0;
00171   textSize->height=0;
00172   cvInitFont(font, fontFace , fontScale, fontScale, fontShare, fontThickness, fontLineType);
00173   for(unsigned int i=0; i<locations.size(); i++)
00174   {
00175     cv::Size textSize2 = cv::getTextSize(currentText[i].c_str(), fontFace, fontScale, fontThickness, 0);
00176     textSize->width  = std::max(textSize->width, textSize2.width);
00177     textSize->height = std::max(textSize->height, textSize2.height);
00178   }
00179 }
00180 
00181 void MapLocationAlgorithm::drawCircle(int x, int y, int r, CvScalar color)
00182 {
00183   int img_x = x; //*this->image.cols;
00184   int img_y = y; //*this->image.rows;
00185   
00186   cv::circle(this->image, cv::Point(img_x, img_y), r, color, -1);
00187   cv::circle(this->image, cv::Point(img_x, img_y), r, CV_RGB(0,0,0), 2);
00188   
00189   if( ( img_x+r > this->image.cols ) || ( img_y+r > this->image.rows ) || (img_x-r < 0 ) || (img_y-r < 0) )
00190     ROS_INFO("Circle out of bounds");
00191 }
00192 
00193 void MapLocationAlgorithm::addLocation(float xpos, float ypos, int id)
00194 {
00195   int x = 1*xpos;
00196   int y = 1*ypos;
00197   int r = 30;
00198   bool added=false;
00199   
00200   if(id<0)
00201   {
00202     for(unsigned int i=0; i<locations.size(); i++)
00203     {
00204       if(locations[i][3]==abs(id))
00205       {
00206         //ROS_INFO("MapLocationAlgorithm::addLocation: Deleted location: %i, %i, %i, %i", locations[i][0], locations[i][1], locations[i][2], locations[i][3]);
00207         locations.erase(locations.begin()+i);
00208         currentColor.erase(currentColor.begin()+i);
00209         currentText.erase(currentText.begin()+i);        
00210         return;
00211       }
00212     }
00213     return;
00214   }
00215 
00216   for(unsigned int i=0; i<locations.size(); i++)
00217   {
00218     if(id==locations[i][3])
00219     {
00220       locations[i][0]=x;
00221       locations[i][1]=y;
00222       locations[i][2]=r;
00223       locations[i][3]=id;
00224       //ROS_INFO("MapLocationAlgorithm::addLocation: Updated location: %i, %i, %i, %i", x, y, r, id);
00225       added=true;
00226     }
00227   }
00228   if(!added)
00229   {
00230     std::vector<int> location;
00231     location.push_back(x);
00232     location.push_back(y);
00233     location.push_back(r);
00234     location.push_back(id);
00235     locations.push_back(location);
00236     ROS_INFO("MapLocationAlgorithm::addLocation: Added location: %i, %i, %i, %i", x, y, r, id);
00237     currentColor.push_back(colorList[id]);
00238     currentText.push_back(textList[id]);
00239   }
00240 }
00241 
00242 void MapLocationAlgorithm::addLocationWithCode(int code)
00243 {
00244   int x=0,y=0;
00245   
00246   switch(code)
00247   {
00248     case   0: x =  882; y = 1035; break; //biblioteca
00249     case   1: x =  666; y =  678; break; //plaza caminos
00250     case   2: x = 1420; y =  672; break; //plaza informatica
00251     case   3: x =  450; y = 1113; break; //nexus
00252     case   4: x = 1467; y = 1485; break; //nexus2
00253     case   5: x = 1350; y = 1074; break; //omega
00254     case   6: x = 1083; y =  618; break; //optica
00255     case   7: x = 1050; y = 1030; break; //polideportivo
00256     case   8: x = 2120; y = 1400; break; //rectorado
00257     case   9: x = 1044; y =  678; break; //plaza telecos
00258     case  10: x = 2150; y =  447; break; //vertex
00259     case 101: x =  575; y =  825; break; //a1...6
00260     case 102: x =  765; y =  825; break;
00261     case 103: x =  954; y =  825; break;
00262     case 104: x = 1140; y =  825; break;
00263     case 105: x = 1333; y =  825; break;
00264     case 106: x = 1518; y =  825; break;
00265     case 201: x =  507; y =  666; break; //b1...6
00266     case 202: x =  807; y =  666; break;
00267     case 203: x =  921; y =  666; break;
00268     case 204: x = 1185; y =  666; break;
00269     case 205: x = 1296; y =  666; break;
00270     case 206: x = 1560; y =  666; break;
00271     case 301: x =  441; y =  560; break; //c1...6
00272     case 302: x =  768; y =  560; break;
00273     case 303: x =  957; y =  560; break;
00274     case 304: x = 1149; y =  560; break;
00275     case 305: x = 1335; y =  560; break;
00276     case 306: x = 1521; y =  560; break;
00277     case 401: x =  420; y =  429; break; //d1...6
00278     case 402: x =  768; y =  429; break;
00279     case 403: x =  957; y =  429; break;
00280     case 404: x = 1149; y =  429; break;
00281     case 405: x = 1335; y =  429; break;
00282     case 406: x = 1521; y =  429; break;
00283     case  -1:
00284       addLocation(0  , 0,  -1);
00285       return;
00286       break;
00287     default:  x =    0; y =    0; break;
00288   }
00289   
00290   addLocation(x  , y,  1); //green
00291 }
00292 
00293 cv::Mat  MapLocationAlgorithm::getImage()
00294 {
00295   return this->image;
00296 }
00297 
00298 void MapLocationAlgorithm::showImage()
00299 {
00300   cv::namedWindow(WINDOW, CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO); // Create a window for display.
00301   cv::resizeWindow(WINDOW, 1024, 768);                             // Resize the window for display
00302   cv::imshow(WINDOW, this->image);                                 // Show our image inside it.
00303   cv::waitKey(0);
00304 }
00305 
00306 void MapLocationAlgorithm::loadFiles()
00307 {
00308   //this->path="./";
00309   
00310   this->mapPath=this->path + "/map.jpg"; //map image dimensions 2814x2023
00311   //this->mapPath="/home/fherrero/Pictures/map.jpg"; //map image dimensions 2814x2023
00312   this->mapImage = this->loadImage(this->mapPath, 1);
00313   //this->logoPath="/home/fherrero/Pictures/iri.png";
00314   //this->logoImage = this->loadImage(this->logoPath, 1);
00315   //this->logo2Path="/home/fherrero/Pictures/upc.jpg";
00316   //this->logo2Image = this->loadImage(this->logo2Path, 1);
00317 }
00318 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


iri_map_location
Author(s): fherrero
autogenerated on Tue Jan 15 2013 15:09:06