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
00018 this->config_=new_cfg;
00019
00020 this->unlock();
00021 }
00022
00023 cv::Mat TibiDaboQuestionsAlgorithm::loadImage(std::string path, int read_mode)
00024 {
00025
00026 cv::Mat loadedImage = cv::imread(path, read_mode);
00027
00028
00029
00030
00031
00032
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);
00049
00050 }
00051
00052 void TibiDaboQuestionsAlgorithm::showImage(const std::string& window, cv::Mat image)
00053 {
00054 this->createWindow(WINDOW);
00055 cv::imshow(window, image);
00056 cv::waitKey(30);
00057 }
00058
00059 void TibiDaboQuestionsAlgorithm::drawCircle(int x, int y, int r, CvScalar color)
00060 {
00061 int img_x = x;
00062 int img_y = y;
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;
00078 case 101: x = 666; y = 678; break;
00079 case 102: x = 1420; y = 672; break;
00080 case 103: x = 450; y = 1113; break;
00081 case 104: x = 1467; y = 1485; break;
00082 case 105: x = 1350; y = 1074; break;
00083 case 106: x = 1083; y = 618; break;
00084 case 107: x = 1050; y = 1030; break;
00085 case 108: x = 2120; y = 1400; break;
00086 case 109: x = 1044; y = 678; break;
00087 case 110: x = 2150; y = 447; break;
00088 case 201: x = 575; y = 825; break;
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;
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;
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;
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
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);
00137 this->images.push_back(backgroundBlank2);
00138 this->images.push_back(mapImage);
00139 this->images.push_back(backgroundLogo);
00140 this->images.push_back(backgroundBlack);
00141
00142
00143 this->fontFace = CV_FONT_HERSHEY_SIMPLEX;
00144 this->fontScale = 4.0;
00145 this->fontShare = 0.0;
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));
00160 this->colors.push_back(CV_RGB( 0, 240, 0));
00161 this->colors.push_back(CV_RGB( 0, 0, 240));
00162 this->colors.push_back(CV_RGB( 0, 240, 240));
00163 this->colors.push_back(CV_RGB(240, 0, 240));
00164 this->colors.push_back(CV_RGB(240, 240, 0));
00165 this->colors.push_back(CV_RGB( 0, 0, 0));
00166 this->colors.push_back(CV_RGB(240, 240, 240));
00167 this->colors.push_back(CV_RGB(255, 0, 0));
00168 }
00169
00170 void TibiDaboQuestionsAlgorithm::iteration(int newStatus, std::string newAnswer)
00171 {
00172
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
00209
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
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
00222
00223 this->image = this->images[1].clone();
00224
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
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
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
00255
00256 this->image = images[2].clone();
00257
00258 int ownX = 1050;
00259 int ownY = 900;
00260 this->drawCircle(ownX,ownY,30,colors[0]);
00261
00262 this->drawAskedLocation(this->status_);
00263
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
00272 this->showImage(WINDOW, this->image);
00273 }