00001 #include "screen_information_alg.h"
00002
00003 ScreenInformationAlgorithm::ScreenInformationAlgorithm(void)
00004 {
00005 this->code = 0;
00006
00007
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
00022 this->path = new_cfg.path;
00023 this->config_=new_cfg;
00024
00025 this->unlock();
00026 }
00027
00028
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
00042 cv::Mat logoImageResize;
00043 cv::resize(logoImage, logoImageResize, cv::Size(), 3, 3, CV_INTER_LINEAR );
00044 logoImage=logoImageResize;
00045
00046
00047
00048
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
00054
00055
00056
00057
00058 this->images.push_back(blankImage);
00059 this->images.push_back(blankImage);
00060 this->images.push_back(blankImage2);
00061 this->images.push_back(mapImage);
00062 this->images.push_back(logoBlankImage);
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));
00073 this->colors.push_back(CV_RGB( 0, 240, 0));
00074 this->colors.push_back(CV_RGB( 0, 0, 240));
00075 this->colors.push_back(CV_RGB( 0, 240, 240));
00076 this->colors.push_back(CV_RGB(240, 0, 240));
00077 this->colors.push_back(CV_RGB(240, 240, 0));
00078 this->colors.push_back(CV_RGB( 0, 0, 0));
00079 this->colors.push_back(CV_RGB(240, 240, 240));
00080
00081
00082 }
00083
00084
00085 cv::Mat ScreenInformationAlgorithm::loadImage(std::string path, int mode)
00086 {
00087
00088 cv::Mat loadedImage = cv::imread(path, mode);
00089
00090
00091
00092
00093
00094
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
00108 void ScreenInformationAlgorithm::createWindow(const std::string& window)
00109 {
00110 cv::namedWindow(window, CV_WINDOW_NORMAL | CV_WINDOW_KEEPRATIO);
00111
00112 cv::resizeWindow(window, 1024, 768);
00113 }
00114
00115
00116 void ScreenInformationAlgorithm::showImage(const std::string& window, cv::Mat image)
00117 {
00118
00119 cv::imshow(window, image);
00120 cv::waitKey(30);
00121 }
00122
00123
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;
00136 break;
00137 default:
00138 if(code<100)
00139 {
00140 return 2;
00141 }
00142 else
00143 {
00144 return 3;
00145 }
00146 break;
00147 }
00148
00149 }
00150
00151
00152 void ScreenInformationAlgorithm::addText(int i, int code)
00153 {
00154
00155 int fontFace = CV_FONT_HERSHEY_SIMPLEX;
00156 double fontScale = 2.0;
00157 float fontShare = 0.0;
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
00166 if(code<10)
00167 {
00168
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
00172 else if(code<100)
00173 {
00174
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
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
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
00196 void ScreenInformationAlgorithm::addOwnLocation(int x, int y)
00197 {
00198 this->drawCircle(x,y,30,colors[0]);
00199 }
00200
00201
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;
00209 case 101: x = 666; y = 678; break;
00210 case 102: x = 1420; y = 672; break;
00211 case 103: x = 450; y = 1113; break;
00212 case 104: x = 1467; y = 1485; break;
00213 case 105: x = 1350; y = 1074; break;
00214 case 106: x = 1083; y = 618; break;
00215 case 107: x = 1050; y = 1030; break;
00216 case 108: x = 2120; y = 1400; break;
00217 case 109: x = 1044; y = 678; break;
00218 case 110: x = 2150; y = 447; break;
00219 case 201: x = 575; y = 825; break;
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;
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;
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;
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
00250 void ScreenInformationAlgorithm::drawCircle(int x, int y, int r, CvScalar color)
00251 {
00252 int img_x = x;
00253 int img_y = y;
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
00263
00264
00265 void ScreenInformationAlgorithm::iteration()
00266 {
00267 if(this->lastCode != this->code)
00268 {
00269 int i=getImageIndex(this->code);
00270 this->images[0] = this->images[i].clone();
00271 this->addText(i,this->code);
00272 if(i==3)
00273 {
00274
00275 this->addOwnLocation(ownX, ownY);
00276
00277 this->addLocationWithCode(this->code);
00278 }
00279
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
00289 }
00290
00291 void ScreenInformationAlgorithm::setAnswer(std::string newAnswer)
00292 {
00293 this->answer=newAnswer;
00294 }