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));
00009 this->colorList.push_back(CV_RGB( 0, 240, 0));
00010 this->colorList.push_back(CV_RGB( 0, 0, 240));
00011 this->colorList.push_back(CV_RGB( 0, 240, 240));
00012 this->colorList.push_back(CV_RGB(240, 0, 240));
00013 this->colorList.push_back(CV_RGB(240, 240, 0));
00014 this->colorList.push_back(CV_RGB( 0, 0, 0));
00015 this->colorList.push_back(CV_RGB(240, 240, 240));
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
00038 this->path = new_cfg.path;
00039 this->config_=new_cfg;
00040
00041 this->unlock();
00042 }
00043
00044
00045 void MapLocationAlgorithm::initialize()
00046 {
00047
00048 }
00049
00050 void MapLocationAlgorithm::mapIteration()
00051 {
00052
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
00062 }
00063
00064 void MapLocationAlgorithm::logoIteration()
00065 {
00066
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
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
00081 cv::Mat loadedImage = cv::imread(imagePath, mode);
00082
00083
00084
00085
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
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
00109 int fontFace = CV_FONT_HERSHEY_SIMPLEX;
00110 double fontScale = height / (this->locations.size()*22);
00111 float fontShare = 0.0;
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
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
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
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
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
00163 this->drawCircle(x,y,r,currentColor[i]);
00164
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;
00184 int img_y = y;
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
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
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;
00249 case 1: x = 666; y = 678; break;
00250 case 2: x = 1420; y = 672; break;
00251 case 3: x = 450; y = 1113; break;
00252 case 4: x = 1467; y = 1485; break;
00253 case 5: x = 1350; y = 1074; break;
00254 case 6: x = 1083; y = 618; break;
00255 case 7: x = 1050; y = 1030; break;
00256 case 8: x = 2120; y = 1400; break;
00257 case 9: x = 1044; y = 678; break;
00258 case 10: x = 2150; y = 447; break;
00259 case 101: x = 575; y = 825; break;
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;
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;
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;
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);
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);
00301 cv::resizeWindow(WINDOW, 1024, 768);
00302 cv::imshow(WINDOW, this->image);
00303 cv::waitKey(0);
00304 }
00305
00306 void MapLocationAlgorithm::loadFiles()
00307 {
00308
00309
00310 this->mapPath=this->path + "/map.jpg";
00311
00312 this->mapImage = this->loadImage(this->mapPath, 1);
00313
00314
00315
00316
00317 }
00318