area.cc
Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *---------------------           WMPSNIFFER          --------------------
00003  *------------------------------------------------------------------------
00004  *                                                         V7.0B  11/05/10
00005  *
00006  *
00007  *  File: area.cc
00008  *  Authors: Danilo Tardioli
00009  *  ----------------------------------------------------------------------
00010  *  Copyright (C) 2000-2012, Universidad de Zaragoza, SPAIN
00011  *
00012  *  Contact Addresses: Danilo Tardioli                   dantard@unizar.es
00013  *
00014  *  RT-WMP is free software; you can  redistribute it and/or  modify it
00015  *  under the terms of the GNU General Public License  as published by the
00016  *  Free Software Foundation;  either  version 2, or (at  your option) any
00017  *  later version.
00018  *
00019  *  RT-WMP  is distributed  in the  hope  that  it will be   useful, but
00020  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
00021  *  MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
00022  *  General Public License for more details.
00023  *
00024  *  You should have received  a  copy of  the  GNU General Public  License
00025  *  distributed with RT-WMP;  see file COPYING.   If not,  write to the
00026  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
00027  *  02111-1307, USA.
00028  *
00029  *  As a  special exception, if you  link this  unit  with other  files to
00030  *  produce an   executable,   this unit  does  not  by  itself cause  the
00031  *  resulting executable to be covered by the  GNU General Public License.
00032  *  This exception does  not however invalidate  any other reasons why the
00033  *  executable file might be covered by the GNU Public License.
00034  *
00035  *----------------------------------------------------------------------*/
00036 #include "area.h"
00037 #include <cmath>
00038 #include "values.h"
00039 #include <cstdio>
00040 #include <vector>
00041 #include <exception>
00042 
00043 #include <gtk-2.0/gdk/gdk.h>
00044 #define MOBILE_NODES
00045 #define FIXED_ENVIRONMENT
00046 #define ARROW_ONLY_ON_LAST
00047 
00048 #include "pthread.h"
00049 #define WAIT(p) pthread_mutex_lock(p)
00050 #define SIGNAL(p) pthread_mutex_unlock(p)
00051 
00052 #define X_BORDER 10
00053 #define Y_BORDER 10
00054 
00055 class area* area_c_wrapper;
00056 
00057 bool area::on_area_button_press_event(GdkEventButton *ev){
00058 
00059         //pointVec.at(i).x_draw= X_BORDER + (int) ((pointVec.at(i).x - min_x)*scalex);
00060         //pointVec.at(i).y_draw= (int) w_height + Y_BORDER - (int) ((pointVec.at(i).y - min_y)*scaley);
00061 
00062         double w_height=this->get_height()-2*Y_BORDER;
00063         yClick= - (min_x + ( ev->x - (double) X_BORDER  ) / scalex);
00064         xClick= min_y - ( ev->y - w_height - (double) Y_BORDER) /scaley;
00065         //fprintf(stderr,"aaa %f %f\n",xClick,yClick);
00066         sem_post(&clickSem);
00067         return 0;
00068 }
00069 
00070 void area::waitClick(double &x, double &y){
00071         sem_wait(&clickSem);
00072         if (interrupted){
00073                 interrupted=false;
00074                 throw 0;
00075         }
00076         x=xClick;
00077         y=yClick;
00078 }
00079 
00080 void area::waitClickInterrupt(){
00081         interrupted=true;
00082         sem_post(&clickSem);
00083 }
00084 
00085 bool area::on_area_button_release_event(GdkEventButton *ev){
00086         return 0;
00087 }
00088 
00089 bool area::on_area_expose_event(GdkEventExpose *ev){
00090         return 0;
00091 }
00092 
00093 int area::get_selected(){
00094         return painted;
00095 }
00096 
00097 bool area::on_area_motion_notify_event(GdkEventMotion *ev){
00098         WAIT(&sem);
00099         if (selected==-1){
00100                         for (unsigned i=0;i<roboVec.size();i++){
00101                         double x_rob=roboVec.at(i).x_draw;
00102                         double y_rob=roboVec.at(i).y_draw;
00103                         if (selected==-1 && sqrt( (x_rob-ev->x)*(x_rob-ev->x) + (y_rob-ev->y)*(y_rob-ev->y) ) < 20){
00104                                 roboVec.at(i).has_mouse_over=1;
00105                                 selected=i;
00106                                 painted=i;
00107                         } else roboVec.at(i).has_mouse_over=0;
00108                 }
00109         }
00110         if (!(ev->state & 256)) {
00111                 selected=-1;
00112                 SIGNAL(&sem);
00113                 return true;
00114         }
00115 #ifdef MOBILE_NODES
00116         if (selected!=-1){
00117 #ifdef FIXED_ENVIRONMENT
00118                 if (ev->x+X_BORDER>this->get_width() || ev->y+Y_BORDER>this->get_height()
00119                                 || ev->x < X_BORDER || ev->y < Y_BORDER) {
00120                         SIGNAL(&sem);
00121                         return true;
00122                 }
00123 #endif
00124                 double x=(min_x*scalex + ev->x - X_BORDER)/scalex;
00125                 double y=(min_y*scaley - ev->y - Y_BORDER + this->get_height())/scaley;
00126                 roboVec.at(selected).x=x;
00127                 roboVec.at(selected).y=y;
00128         }
00129 #endif
00130         compute_environment();
00131         SIGNAL(&sem);
00132         return true;
00133 }
00134 
00135 
00136 bool area::timer_callback(){
00137                 return 1;
00138 }
00139 
00140 area::area(): DrawingArea(){
00141 
00142         pthread_mutex_init(&sem, NULL);
00143         sem_init(&clickSem,1,0);
00144 
00145         maximumNumOfPoints=360;
00146         fixed_scale=-1;
00147         snap_id=0;
00148         has_bg_image=false;
00149         join_points=false;
00150         marginSetted=false;
00151     Glib::RefPtr<Gdk::Colormap> colormap = get_default_colormap ();
00152     cols[0] = Gdk::Color("black");
00153     cols[2] = Gdk::Color("red");
00154     cols[3] = Gdk::Color("green");
00155     cols[1] = Gdk::Color("orange");
00156     cols[4] = Gdk::Color("blue");
00157     cols[5] = Gdk::Color("violet");
00158     for (unsigned i=0;i<6;i++){
00159         colormap->alloc_color(cols[i]);
00160     }
00161     this->set_events(
00162     Gdk::BUTTON_PRESS_MASK |    //mouse button down
00163     Gdk::BUTTON_RELEASE_MASK |    //mouse button up
00164     Gdk::POINTER_MOTION_MASK);
00165 
00166 
00167     painted=selected=-1;
00168     area_c_wrapper=this;
00169         this->signal_size_allocate().connect(sigc::mem_fun(*this, &area::on_size_alloc));
00170         //this->signal_motion_notify_event().connect(sigc::mem_fun(*this, &area::on_area_motion_notify_event));
00171         this->signal_button_press_event().connect(sigc::mem_fun(*this, &area::on_area_button_press_event));
00172         timesnr_12 = new Pango::FontDescription("Times new roman 9");
00173         timesnr_8 = new Pango::FontDescription("Times new roman 7");
00174         maintain_aspect_ratio=true;
00175         interrupted=false;
00176 }
00177 int area::getRobotId(std::string name){
00178         return roboMap[name];
00179 }
00180 
00181 int area::addRobot(std::string name){
00182         WAIT(&sem);
00183         Robot r;
00184         r.x=0;
00185         r.y=0;
00186         r.a=0;
00187         r.col=0;
00188         r.name=name;
00189         roboVec.push_back(r);
00190         compute_environment();
00191         int id=roboVec.size() - 1;
00192         roboMap[name]=id;
00193         SIGNAL(&sem);
00194         return (id);
00195 }
00196 
00197 void area::on_size_alloc(Gtk::Allocation &al){
00198         WAIT(&sem);
00199         compute_environment();
00200         SIGNAL(&sem);
00201 }
00202 
00203 void area::addPoint(double x, double y, int color, int with_respect_to_robot){
00204         /*WAIT(&sem);
00205         Point p;
00206         p.col=color;
00207         if (1 || with_respect_to_robot<0){
00208                 p.x=x;
00209                 p.y=y;
00210         }
00211         p.cell_size=1.0;
00212         pointVec.push_back(p);
00213     if (pointVec.size()>maximumNumOfPoints) pointVec.erase(pointVec.begin());
00214     compute_environment();
00215         SIGNAL(&sem);*/
00216         addGridPoint(x,y,0.0,color,with_respect_to_robot);
00217 }
00218 
00219 void area::addGridPoint(double x, double y, double cell_size, int color, int with_respect_to_robot){
00220         WAIT(&sem);
00221         Point p;
00222         p.col=color;
00223         p.cell_size=cell_size;
00224         if (cell_size>0){
00225                 p.x=round(x/cell_size)*cell_size;
00226                 p.y=round(y/cell_size)*cell_size;
00227         }else{
00228                 p.x=x;
00229                 p.y=y;
00230         }
00231         for (unsigned i=0;i<pointVec.size();i++){
00232                 if (pointVec[i].x==p.x && pointVec[i].y==p.y){
00233                         SIGNAL(&sem);
00234                         return;
00235                 }
00236         }
00237         pointVec.push_back(p);
00238     if (pointVec.size()>maximumNumOfPoints) pointVec.erase(pointVec.begin());
00239     compute_environment();
00240         SIGNAL(&sem);
00241 }
00242 
00243 void addLine(double x1, double y1, double x2, double y2, int color){
00244 
00245 }
00246 
00247 void area::addGoal(int r1, double x2, double y2, int color,int arrow){
00248         WAIT(&sem);
00249         Goal g;
00250         g.x=x2;
00251         g.y=y2;
00252         g.r1=r1;
00253         g.col=color;
00254         goalVec.push_back(g);
00255         compute_environment();
00256         SIGNAL(&sem);
00257 }
00258 
00259 void area::addPoint(double y, int color){
00260         WAIT(&sem);
00261         Point p;
00262         p.col=color;
00263         p.y=y;
00264         p.x=pointVec.size();
00265         pointVec.push_back(p);
00266     if (pointVec.size()>maximumNumOfPoints) {
00267         pointVec.erase(pointVec.begin());
00268         for (unsigned i=0;i<pointVec.size();i++){
00269                 pointVec[i].x=i;
00270         }
00271     }
00272     compute_environment();
00273         SIGNAL(&sem);
00274 }
00275 
00276 void area::setMaximumNumOfPoints(int n){
00277         maximumNumOfPoints=n;
00278 }
00279 
00280 void area::addLink(int r1, int r2, int arrow, int color, float value, int set){
00281         WAIT(&sem);
00282 #ifdef ARROW_ONLY_ON_LAST
00283     for (unsigned i=0;i<linkVec[set].size();i++) linkVec[set].at(i).arrow=0;
00284 #endif
00285     Link l;
00286         l.r1=r1;
00287         l.r2=r2;
00288         l.col=color;
00289         l.arrow=arrow;
00290         l.has_value=true;
00291         l.value=value;
00292         linkVec[set].push_back(l);
00293         SIGNAL(&sem);
00294 }
00295 
00296 void area::addLink(int r1, int r2, int arrow, int color, int set){
00297         WAIT(&sem);
00298 #ifdef ARROW_ONLY_ON_LAST
00299     for (unsigned i=0;i<linkVec[set].size();i++) linkVec[set].at(i).arrow=0;
00300 #endif
00301     Link l;
00302         l.r1=r1;
00303         l.r2=r2;
00304         l.col=color;
00305         l.arrow=arrow;
00306         l.has_value=false;
00307         linkVec[set].push_back(l);
00308         SIGNAL(&sem);
00309 }
00310 
00311 int area::addRobot(){
00312         WAIT(&sem);
00313         Robot r;
00314         r.x=0;
00315         r.y=0;
00316         r.a=0;
00317         r.name.assign("");
00318         r.col=0;
00319         roboVec.push_back(r);
00320         compute_environment();
00321         SIGNAL(&sem);
00322         return (roboVec.size() - 1);
00323 }
00324 int area::getNumOfRobots(){
00325         return roboVec.size();
00326 }
00327 
00328 void area::setRobotPose(int id, double x0, double y0, double ang, int col=0){
00329         WAIT(&sem);
00330         roboVec.at(id).x=x0;
00331         roboVec.at(id).y=y0;
00332         roboVec.at(id).a=ang;
00333         roboVec.at(id).col=col;
00334         compute_environment();
00335         SIGNAL(&sem);
00336 }
00337 
00338 int area::getRobotPose(int id, double* x0, double* y0, double* ang){
00339         WAIT(&sem);
00340         if (id >= 0 && (unsigned) id < roboVec.size()){
00341                 *x0=roboVec.at(id).x;
00342                 *y0=roboVec.at(id).y;
00343                 *ang=roboVec.at(id).a;
00344                 SIGNAL(&sem);
00345                 return id;
00346         }
00347         SIGNAL(&sem);
00348         return -1;
00349 }
00350 
00351 void area::draw_robot(double x, double y, double a, int col,int mouse_over, double scale=0.1){
00352                 static const double ROBOT [2][7] = {{-312.5, 312.5, 312.5, -312.5, 312.5, -312.5, -312.5},
00353                                                     {   250,   250,  -250,   -250,     0,    250,   -250}};
00354             int robotX[7];
00355             int robotY[7];
00356                 double s = sin(a+M_PI/2);
00357             double c = cos(a+M_PI/2);
00358             if (scale>0.05) scale=0.05;
00359             if (scale<0.001) scale=0.001;
00360             for (unsigned i = 0; i < 7; i++){
00361                 robotX[i] = (int) rint(ROBOT[0][i] * c * scale + ROBOT[1][i] * s * scale);
00362                 robotY[i] = (int) rint(ROBOT[1][i] * c * scale - ROBOT[0][i] * s * scale);
00363             }
00364             //XXXX
00365             mouse_over=0;
00366             if (mouse_over==1) gc->set_foreground(cols[2]);
00367             else gc->set_foreground(cols[col]);
00368 
00369                 for (unsigned i=0;i<6;i++){
00370                 window->draw_line(gc, (int)x+robotX[i], (int)y+robotY[i],(int)x+robotX[i+1], (int)y+robotY[i+1]);
00371                 }
00372 }
00373 
00374 void area::draw_triangle(double x, double y, double a, int col, double scale){
00375                 static const double TRIANGLE [2][4] = {{-100.5, 100.5, 0.0, -100.5}, {0.0,0.0,100.5, 0.0 } };
00376             int triangleX[4];
00377             int triangleY[4];
00378                 double s = sin(a+M_PI/2);
00379             double c = cos(a+M_PI/2);
00380             if (scale>0.1) scale=0.1;
00381             if (scale<0.01) scale=0.01;
00382 
00383             for (unsigned i = 0; i < 4; i++){
00384                 triangleX[i] = (int) rint(TRIANGLE[0][i] * c * scale + TRIANGLE[1][i] * s * scale);
00385                 triangleY[i] = (int) rint(TRIANGLE[1][i] * c * scale - TRIANGLE[0][i] * s * scale);
00386             }
00387             gc->set_foreground(cols[col]);
00388                 for (unsigned i=0;i<3;i++){
00389                 window->draw_line(gc, (int)x+triangleX[i], (int)y+triangleY[i],(int)x+triangleX[i+1], (int)y+triangleY[i+1]);
00390                 }
00391 }
00392 void area::setMaintainAspectRatio(bool val){
00393         maintain_aspect_ratio=val;
00394 }
00395 
00396 void area::clearLinks(int set){
00397         WAIT(&sem);
00398         linkVec[set].clear();
00399         SIGNAL(&sem);
00400 }
00401 
00402 void area::clearRobots(){
00403         WAIT(&sem);
00404         roboVec.clear();
00405         roboMap.clear();
00406         SIGNAL(&sem);
00407 }
00408 void area::clearPoints(){
00409         WAIT(&sem);
00410         pointVec.clear();
00411         SIGNAL(&sem);
00412 }
00413 void area::on_realize(){
00414         WAIT(&sem);
00415         Gtk::DrawingArea::on_realize();
00416         window = get_window();
00417         gc = Gdk::GC::create(window);
00418         window->clear();
00419         initialized=1;
00420         SIGNAL(&sem);
00421 }
00422 void area::setMargins(double maxx,double maxy, double minx, double miny){
00423         marginSetted=true;
00424         canvas_maxx=maxx;
00425         canvas_maxy=maxy;
00426         canvas_minx=minx;
00427         canvas_miny=miny;
00428 }
00429 
00430 void area::compute_environment(){
00431         double width=0, height=0;
00432         max_x = -2^30; max_y= -2^30; min_x=2^30; min_y=2^30;
00433         for (unsigned i=0;i< roboVec.size();i++){
00434                 max_x= roboVec.at(i).x>max_x+25? roboVec.at(i).x+25:max_x;
00435                 max_y= roboVec.at(i).y>max_y+25? roboVec.at(i).y+25:max_y;
00436                 min_y= roboVec.at(i).y<min_y-25? roboVec.at(i).y:min_y-25;
00437                 min_x= roboVec.at(i).x<min_x-25? roboVec.at(i).x:min_x-25;
00438         }
00439         for (unsigned i=0;i< pointVec.size();i++){
00440                 max_x= pointVec.at(i).x>max_x? pointVec.at(i).x:max_x;
00441                 max_y= pointVec.at(i).y>max_y? pointVec.at(i).y:max_y;
00442                 min_y= pointVec.at(i).y<min_y? pointVec.at(i).y:min_y;
00443                 min_x= pointVec.at(i).x<min_x? pointVec.at(i).x:min_x;
00444         }
00445         for (unsigned i=0;i< goalVec.size();i++){
00446                 max_x= goalVec.at(i).x>max_x? goalVec.at(i).x:max_x;
00447                 max_y= goalVec.at(i).y>max_y? goalVec.at(i).y:max_y;
00448                 min_y= goalVec.at(i).y<min_y? goalVec.at(i).y:min_y;
00449                 min_x= goalVec.at(i).x<min_x? goalVec.at(i).x:min_x;
00450         }
00451 
00452         if (marginSetted){
00453                 if (max_x < canvas_maxx) {
00454                         max_x = canvas_maxx;
00455                 }
00456                 if (max_y < canvas_maxy) {
00457                         max_y = canvas_maxy;
00458                 }
00459                 if (min_x > canvas_minx) {
00460                         min_x = canvas_minx;
00461                 }
00462                 if (min_y > canvas_miny) {
00463                         min_y = canvas_miny;
00464                 }
00465         }
00466 
00467         width=fabs(max_x-min_x);
00468         height=fabs(max_y-min_y);
00469 
00470         double w_width=this->get_width()-2*X_BORDER;
00471         double w_height=this->get_height()-2*Y_BORDER;
00472         double r1=w_width/width;
00473         double r2=w_height/height;
00474 
00475         if (maintain_aspect_ratio){
00476                 scalex = scaley = r1<r2?r1:r2;
00477         }else{
00478                 scalex=r1;//1/width;
00479                 scaley=r2;//1/height;
00480         }
00481 
00482         for (unsigned i=0;i< roboVec.size();i++){
00483                 roboVec.at(i).x_draw= X_BORDER + (int) ((roboVec.at(i).x - min_x)*scalex);
00484         roboVec.at(i).y_draw= (int) w_height + Y_BORDER - (int) ((roboVec.at(i).y - min_y)*scaley);
00485     }
00486     for (unsigned i=0;i< pointVec.size();i++){
00487         pointVec.at(i).x_draw= X_BORDER + (int) ((pointVec.at(i).x - min_x)*scalex);
00488         pointVec.at(i).y_draw= (int) w_height + Y_BORDER - (int) ((pointVec.at(i).y - min_y)*scaley);
00489     }
00490 
00491     for (unsigned i=0;i< imgVec.size();i++){
00492         imgVec.at(i).x_draw= X_BORDER + (int) ((imgVec.at(i).x - min_x)*scalex);
00493         imgVec.at(i).y_draw= (int) w_height + Y_BORDER - (int) ((imgVec.at(i).y - min_y)*scaley);
00494 
00495         double actx=imgVec.at(i).sizex*scalex*0.020;
00496         //double acty=imgVec.at(i).sizey*scaley*0.020;
00497 
00498         if (imgVec.at(i).sizex!=0 && actx!=0 ){
00499                 imgVec.at(i).scale=actx/imgVec.at(i).sizex;
00500         } else{
00501                 imgVec.at(i).scale=1;
00502         }
00503     }
00504 
00505     for (unsigned i=0;i< goalVec.size();i++){
00506         goalVec.at(i).x_draw= X_BORDER + (int) ((goalVec.at(i).x - min_x)*scalex);
00507         goalVec.at(i).y_draw= (int) w_height + Y_BORDER - (int) ((goalVec.at(i).y - min_y)*scaley);
00508     }
00509     zoom=sqrt(w_width*w_height/(2500*width*height));
00510 }
00511 
00512 void area::setJoinPoints(bool n){
00513         join_points=n;
00514 }
00515 
00516 void area::setBackGroundImage(std::string s){
00517         bg_image = Gdk::Pixbuf::create_from_file(s.c_str());
00518         has_bg_image=true;
00519 }
00520 
00521 int area::addImage(std::string s, double x, double y, double scale, std::string s1){
00522         WAIT(&sem);
00523         Img m;
00524         m.scale_type=0;
00525         m.filename.assign(s.c_str());
00526         m.text.assign(s1);
00527         m.x=x;
00528         m.y=y;
00529         m.scale=scale;
00530 
00531         m.pixbuf = Gdk::Pixbuf::create_from_file(s.c_str());
00532 
00533         imgVec.push_back(m);
00534         compute_environment();
00535         SIGNAL(&sem);
00536         return imgVec.size()-1;
00537 
00538 }
00539 
00540 int area::addImage(std::string s, double x, double y, double scale){
00541         return addImage(s,x,y,scale,"");
00542 }
00543 
00544 int area::addImage(std::string s, double x, double y, double sizex, double sizey, std::string s1){
00545         WAIT(&sem);
00546         Img m;
00547         m.scale_type=1;
00548         m.filename.assign(s.c_str());
00549         m.text.assign(s1);
00550         m.x=x;
00551         m.y=y;
00552         m.sizex=sizex;
00553         m.sizey=sizey;
00554 
00555         m.pixbuf = Gdk::Pixbuf::create_from_file(s.c_str());
00556 
00557         imgVec.push_back(m);
00558         compute_environment();
00559         SIGNAL(&sem);
00560         return imgVec.size()-1;
00561 }
00562 
00563 
00564 void area::paint(){
00565         WAIT(&sem);
00566         get_window()->clear();
00567         Glib::RefPtr<Pango::Layout> layout = create_pango_layout("");
00568     layout->set_font_description(*timesnr_12);
00569     char tmp[64];
00570 
00571 
00572         if (has_bg_image){
00573                 /* Paint image >*/
00574                 Glib::RefPtr<Gdk::Pixbuf> image2 = bg_image->scale_simple(get_width(),get_height(),Gdk::INTERP_NEAREST);
00575                 image2->render_to_drawable(get_window(),
00576                    get_style()->get_black_gc(),
00577                    0, 0, 0, 0, image2->get_width(),
00578                    image2->get_height(),
00579                    Gdk::RGB_DITHER_NONE,0, 0);
00580         }
00581         /* < Paint image */
00582         for (unsigned i=0;i< imgVec.size();i++){
00583                 double scale=imgVec.at(i).scale;
00584                 Glib::RefPtr<Gdk::Pixbuf> image4 ;
00585                 try{
00586                 image4 = imgVec.at(i).pixbuf->scale_simple((int)(imgVec.at(i).pixbuf->get_width()*scale)+1,
00587                                         (int)(imgVec.at(i).pixbuf->get_height()*scale)+1,Gdk::INTERP_NEAREST);
00588 
00589                 image4->render_to_drawable(get_window(),
00590                            get_style()->get_black_gc(),
00591                            0, 0, imgVec.at(i).x_draw-image4->get_width(), imgVec.at(i).y_draw - image4->get_height(), image4->get_width(),
00592                            image4->get_height(),
00593                            Gdk::RGB_DITHER_NONE,0, 0);
00594                 } catch(...){
00595 
00596                 }
00597                 layout->set_font_description(*timesnr_8);
00598                 sprintf(tmp,"%s",imgVec.at(i).text.c_str());
00599                 layout->set_markup(tmp);
00600                 int y_txt=imgVec.at(i).y_draw;
00601                 int x_txt=imgVec.at(i).x_draw - image4->get_width()-5;
00602                 get_window()->draw_layout(Gdk::GC::create(get_window()),x_txt,y_txt, layout);
00603 
00604         }
00605 
00606     for (unsigned i=0;i< goalVec.size();i++){
00607                         gc->set_foreground(cols[goalVec.at(i).col]);
00608                         window->draw_line(gc, roboVec.at(goalVec.at(i).r1).x_draw, roboVec.at(goalVec.at(i).r1).y_draw,
00609                                         goalVec.at(i).x_draw,goalVec.at(i).y_draw);
00610                         sprintf(tmp,"(%3.1f,%3.1f)",goalVec.at(i).x,goalVec.at(i).y);
00611                         layout->set_markup(tmp);
00612                         get_window()->draw_layout(Gdk::GC::create(get_window()),goalVec.at(i).x_draw-50,goalVec.at(i).y_draw-20, layout);
00613                         layout->set_markup("*");
00614                         get_window()->draw_layout(Gdk::GC::create(get_window()),goalVec.at(i).x_draw,goalVec.at(i).y_draw-5, layout);
00615                         //if (goalVec.at(i).arrow){
00616                                 //draw_triangle(goalVec.at(i).x_draw,goalVec.at(i).y_draw,ang, goalVec.at(i).col, 10);
00617                         //}
00618         }
00619 
00620         for (unsigned i=0;i< roboVec.size();i++){
00621                 //XXXXXXXX
00622                 //roboVec.at(i).col=0;
00623                 draw_robot(roboVec.at(i).x_draw,roboVec.at(i).y_draw,roboVec.at(i).a,
00624                                                 roboVec.at(i).col,roboVec.at(i).has_mouse_over,0.025); //zoom
00625 
00626                 //sprintf(tmp,"%s\n(%3.1f:%3.1f)",roboVec.at(i).name.c_str(),roboVec.at(i).x,roboVec.at(i).y);
00627                 sprintf(tmp,"%s",roboVec.at(i).name.c_str());
00628 
00629                 layout->set_markup(tmp);
00630 
00631                 get_window()->draw_layout(Gdk::GC::create(get_window()),roboVec.at(i).x_draw-20,roboVec.at(i).y_draw+7, layout);
00632         }
00633 
00634         for (int set = 0; set < LINK_SETS; set++) {
00635                 for (unsigned i = 0; i < linkVec[set].size(); i++) {
00636                         gc->set_foreground(cols[linkVec[set].at(i).col]);
00637                         window->draw_line(gc, roboVec.at(linkVec[set].at(i).r1).x_draw,
00638                                         roboVec.at(linkVec[set].at(i).r1).y_draw, roboVec.at(
00639                                                         linkVec[set].at(i).r2).x_draw, roboVec.at(
00640                                                         linkVec[set].at(i).r2).y_draw);
00641                         int disp=0;
00642                         if (linkVec[set].at(i).r2 > linkVec[set].at(i).r1){
00643                                 disp = 5;
00644                         }else{
00645                                 disp = -5;
00646                         }
00647 
00648                         int x_text = roboVec.at(linkVec[set].at(i).r1).x_draw
00649                                         + (roboVec.at(linkVec[set].at(i).r2).x_draw - roboVec.at(
00650                                                         linkVec[set].at(i).r1).x_draw) / 2;
00651                         int y_text = roboVec.at(linkVec[set].at(i).r1).y_draw
00652                                         + (roboVec.at(linkVec[set].at(i).r2).y_draw - roboVec.at(
00653                                                         linkVec[set].at(i).r1).y_draw) / 2;
00654                         if (linkVec[set].at(i).has_value){
00655                                 sprintf(tmp, "%3.1f", linkVec[set].at(i).value);
00656                                 layout->set_markup(tmp);
00657                                 get_window()->draw_layout(Gdk::GC::create(get_window()), x_text+disp,
00658                                                 y_text+disp, layout);
00659                         }
00660 
00661 /*                      double c1 = roboVec.at(linkVec[set].at(i).r2).x - roboVec.at(
00662                                         linkVec[set].at(i).r1).x;
00663                         double c2 = roboVec.at(linkVec[set].at(i).r2).y - roboVec.at(
00664                                         linkVec[set].at(i).r1).y;
00665 */
00666 
00667                         double ac1 = roboVec.at(linkVec[set].at(i).r2).x_draw - roboVec.at(
00668                                         linkVec[set].at(i).r1).x_draw;
00669                         double ac2 = roboVec.at(linkVec[set].at(i).r2).y_draw - roboVec.at(
00670                                         linkVec[set].at(i).r1).y_draw;
00671 
00672 
00673                         double ang = atan2(ac2, ac1);
00674 
00675                         double d1 = -15*cos(ang);
00676                         double d2 = -15*sin(ang);
00677 
00678                         if (linkVec[set].at(i).arrow) {
00679                                 draw_triangle(roboVec.at(linkVec[set].at(i).r2).x_draw+d1,
00680                                                 roboVec.at(linkVec[set].at(i).r2).y_draw+d2, -ang,
00681                                                 linkVec[set].at(i).col, 10);
00682                         }
00683                 }
00684         }
00685     gc->set_foreground(cols[2]);
00686         for (unsigned i=0;i< pointVec.size();i++){
00687             Point p;
00688             p=pointVec.at(i);
00689             if (join_points) {
00690                 Point p1;
00691                 if (i!=0) p1=pointVec.at(i-1);
00692                 else p1=p;
00693                 window->draw_line(gc, p.x_draw, p.y_draw, p1.x_draw,p1.y_draw);
00694             }else{
00695                 window->draw_point(gc,p.x_draw,p.y_draw);
00696                 window->draw_point(gc,p.x_draw,p.y_draw-1);
00697                 window->draw_point(gc,p.x_draw,p.y_draw+1);
00698                 window->draw_point(gc,p.x_draw+1,p.y_draw);
00699                 window->draw_point(gc,p.x_draw-1,p.y_draw);
00700             }
00701     }
00702     SIGNAL(&sem);
00703 }
00704 
00705 
00706 void area::grabSnapshot(){
00707         WAIT(&sem);
00708         Glib::RefPtr<Gdk::Drawable> drwbl = this->get_window();
00709         Glib::RefPtr<Gdk::Pixbuf> pxb;
00710         pxb = Gdk::Pixbuf::create(drwbl, drwbl->get_colormap(), 0, 0, 0, 0,
00711                         this->get_width(), this->get_height());
00712         char name[256];
00713         sprintf(name, "snapshot_%04d.png", snap_id);
00714         try {
00715                 pxb->save(name, "png");
00716                 snap_id++;
00717         } catch (Glib::Exception &e) {
00718                 //std::cerr << "could not save: " << e.what() << std::endl;
00719         }
00720         SIGNAL(&sem);
00721 
00722 }
00723 
00724 void area::clearGoals(){
00725         WAIT(&sem);
00726         goalVec.clear();
00727         SIGNAL(&sem);
00728 }
00729 #include <gdk/gdk.h>
00730 
00731 bool area::on_expose_event(GdkEventExpose* e){
00732         paint();
00733         return true;
00734 }
00735 
00736 void area::setRobotColor(int idx, int c){
00737         roboVec.at(idx).col=c;
00738 }
00739 


ros_rt_wmp_sniffer
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:57