stdr_map_loader.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  STDR Simulator - Simple Two DImensional Robot Simulator
3  Copyright (C) 2013 STDR Simulator
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 3 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 
16  Authors :
17  * Manos Tsardoulias, etsardou@gmail.com
18  * Aris Thallas, aris.thallas@gmail.com
19  * Chris Zalidis, zalidis@gmail.com
20 ******************************************************************************/
21 
23 
24 namespace stdr_gui
25 {
32  CMapLoader::CMapLoader(int argc, char **argv):
33  argc_(argc),
34  argv_(argv)
35  {
36  setupUi(this);
37  internal_img_ = new QImage(100,100,QImage::Format_RGB32);
38  map_min_ = QPoint(0,0);
39  map_max_ = QPoint(0,0);
40  zoom_ = 0;
41  }
42 
48  void CMapLoader::resizeEvent(QResizeEvent *e)
49  {
51  }
52 
59  std::pair<int,int> CMapLoader::checkDimensions(int w,int h)
60  {
61  float containerWidth = this->width();
62  float containerHeight = this->height();
63  float aspectRatio = (float)w / (float)h;
64  float finalW,finalH;
65  if(containerHeight * aspectRatio > containerWidth)
66  {
67  finalW = containerWidth;
68  finalH = containerWidth / aspectRatio;
69  }
70  else
71  {
72  finalW = containerHeight * aspectRatio;
73  finalH = containerHeight;
74  }
75  return std::pair<int,int>(finalW,finalH);
76  }
77 
83  void CMapLoader::updateImage(QImage *img)
84  {
85  internal_img_ = img;
86  std::pair<int,int> newDims = checkDimensions(img->width(),img->height());
87 
88  map->setPixmap(
89  QPixmap().fromImage(
90  (*img).
91  copy(map_min_.x(),
92  map_min_.y(),
93  map_max_.x() - map_min_.x(),
94  map_max_.y() - map_min_.y()).
95  scaled(newDims.first,newDims.second,
96  Qt::IgnoreAspectRatio,
97  Qt::SmoothTransformation)));
98 
99  map->resize(newDims.first,newDims.second);
100  }
101 
108  void CMapLoader::drawGrid(QImage *img,float resolution)
109  {
110  QPainter painter(img);
111  painter.setPen(QColor(100,100,100,150));
112  int pix = 1.0 / resolution;
113  for(unsigned int i = 1 ; i <= img->width() / pix + 1 ; i++)
114  {
115  painter.drawLine(0, i * pix, img->width() - 1, i * pix);
116  }
117  for(unsigned int i = 1 ; i <= img->height() / pix + 1 ; i++)
118  {
119  painter.drawLine(i * pix, 0, i * pix, img->height() - 1);
120  }
121  }
122 
129  void CMapLoader::updateZoom(QPoint p,bool zoomIn)
130  {
131  QPoint np = getGlobalPoint(p);
132  np.setY(internal_img_->height() - np.y());
133  int prevZoom = zoom_;
134  if(zoomIn)
135  {
136  zoom_++;
137  }
138  else
139  {
140  zoom_--;
141  }
142  if(zoom_ < 0)
143  {
144  zoom_ = 0;
145  return;
146  }
147  float intW = internal_img_->width();
148  float intH = internal_img_->height();
149  float newWidth = internal_img_->width() / pow(ZOOM_RATIO,zoom_);
150  float newHeight = internal_img_->height() / pow(ZOOM_RATIO,zoom_);
151  QPoint evOriginal = np;
152 
153  float xmin,xmax,ymin,ymax;
154  float prevxmin, prevymin, prevWidth, prevHeight;
155  prevxmin = map_min_.x();
156  prevymin = map_min_.y();
157  prevWidth = map_max_.x() - map_min_.x();
158  prevHeight = map_max_.y() - map_min_.y();
159  float xhit = evOriginal.x();
160  float yhit = evOriginal.y();
161 
162  xmin = xhit - (xhit - prevxmin) * newWidth / prevWidth;
163  xmax = xmin + newWidth ;
164  ymin = yhit - (yhit - prevymin) * newHeight / prevHeight;
165  ymax = ymin + newHeight;
166 
167  if(xmin < 0)
168  {
169  xmax += - xmin;
170  xmin = 0;
171  }
172  else if(xmax > internal_img_->width() - 1)
173  {
174  xmin -= xmax - internal_img_->width() + 1;
175  xmax = internal_img_->width() - 1;
176  }
177  if(ymin < 0)
178  {
179  ymax += - ymin;
180  ymin = 0;
181  }
182  else if(ymax > internal_img_->height() - 1)
183  {
184  ymin -= ymax - internal_img_->height() + 1;
185  ymax = internal_img_->height() - 1;
186  }
187  map_min_ = QPoint(xmin,ymin);
188  map_max_ = QPoint(xmax,ymax);
189  }
190 
197  {
198  int xcenter = (map_max_.x() + map_min_.x()) / 2;
199  int ycenter = (map_max_.y() + map_min_.y()) / 2;
200  QPoint p(xcenter, ycenter);
201  //~ float move_by = (map_max_.x() - map_min_.x()) / 10.0;
202  float move_by = 50;
203 
204  switch(key)
205  {
206  case Qt::Key_Right:
207  p.setX(p.x() + move_by);
208  break;
209  case Qt::Key_Left:
210  p.setX(p.x() - move_by);
211  break;
212  case Qt::Key_Up:
213  p.setY(p.y() - move_by);
214  break;
215  case Qt::Key_Down:
216  p.setY(p.y() + move_by);
217  break;
218  default:
219  return;
220  }
221  updateCenter(p);
222  }
223 
224 
231  {
232 
233  float intW = internal_img_->width();
234  float intH = internal_img_->height();
235  float newWidth = internal_img_->width() / pow(ZOOM_RATIO,zoom_);
236  float newHeight = internal_img_->height() / pow(ZOOM_RATIO,zoom_);
237  QPoint evOriginal = p;
238  //~ evOriginal.setY(internal_img_->height() - evOriginal.y());
239 
240  float xmin,xmax,ymin,ymax;
241  xmin = evOriginal.x() - newWidth / 2;
242  xmax = evOriginal.x() + newWidth / 2;
243  ymin = evOriginal.y() - newHeight / 2;
244  ymax = evOriginal.y() + newHeight / 2;
245  if(xmin < 0)
246  {
247  xmax += - xmin;
248  xmin = 0;
249  }
250  else if(xmax > internal_img_->width() - 1)
251  {
252  xmin -= xmax - internal_img_->width() + 1;
253  xmax = internal_img_->width() - 1;
254  }
255  if(ymin < 0)
256  {
257  ymax += - ymin;
258  ymin = 0;
259  }
260  else if(ymax > internal_img_->height() - 1)
261  {
262  ymin -= ymax - internal_img_->height() + 1;
263  ymax = internal_img_->height() - 1;
264  }
265  map_min_ = QPoint(xmin,ymin);
266  map_max_ = QPoint(xmax,ymax);
267  }
268 
274  QPoint CMapLoader::pointUnscaled(QPoint p)
275  {
276  QPoint newPoint;
277  float x = p.x();
278  float y = p.y();
279  float initialWidth = internal_img_->width();
280  float currentWidth = map->width();
281  //~ ROS_ERROR("InitialW, CurrW : %f %f",initialWidth,currentWidth);
282  float climax = initialWidth / currentWidth;
283  newPoint.setX(x * climax);
284  newPoint.setY(y * climax);
285  return newPoint;
286  }
287 
293  {
294  zoom_ = 0;
295  map_min_ = QPoint(0,0);
296  map_max_ = QPoint(internal_img_->width() - 1,internal_img_->height() - 1);
297  }
298 
304  QPoint CMapLoader::getGlobalPoint(QPoint p){
305  //~ ROS_ERROR("Robot place set (loader) : %d %d",p.x(),p.y());
306  QPoint np = pointUnscaled(p);
307  //~ ROS_ERROR("Robot place set (unscaled): %d %d",np.x(),np.y());
308  int xev = np.x() / pow(ZOOM_RATIO,zoom_) + map_min_.x();
309  int yev = np.y() / pow(ZOOM_RATIO,zoom_) + map_min_.y();
310  //~ ROS_ERROR("Robot place set (unzoomed): %d %d",xev,yev);
311  return QPoint(xev,internal_img_->height() - yev);
312  }
313 
320  {
322  map_min_ = QPoint(0, 0);
323  map_max_ = QPoint(s.width(), s.height());
324  }
325 }
void updateCenter(QPoint p)
Updates the image center.
QPoint map_max_
The original image size.
QPoint map_min_
The lower right point of map visualization.
void drawGrid(QImage *img, float resolution)
Draws a grid in an image.
TFSIMD_FORCE_INLINE const tfScalar & y() const
QPoint getGlobalPoint(QPoint p)
Calculates the "real" point in the image.
CMapLoader(int argc, char **argv)
Default contructor.
void setInitialImageSize(QSize s)
Sets the initial image size.
void updateImage(QImage *img)
Updates the image.
int zoom_
Internal image used before a map is loaded.
TFSIMD_FORCE_INLINE const tfScalar & x() const
void updateZoom(QPoint p, bool zoomIn)
Updates the zoom of the image.
void resizeEvent(QResizeEvent *e)
Captures the resize event.
QImage * internal_img_
The upper left point of map visualization.
The main namespace for STDR GUI.
std::pair< int, int > checkDimensions(int w, int h)
Return the dimensions according to the container size.
void resetZoom(void)
Resets the zoom of the image.
void moveDirectionally(int key)
Updates the image center by moving directionally.
#define ZOOM_RATIO
QPoint pointUnscaled(QPoint p)
Unscales the input point.


stdr_gui
Author(s): Manos Tsardoulias
autogenerated on Mon Jun 10 2019 15:15:16