EditMapArea.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <QWidget>
29 #include <QPainter>
30 #include <QMouseEvent>
31 #include <QMenu>
32 #include <QAction>
33 #include <QInputDialog>
34 
37 
38 namespace rtabmap {
39 
40 EditMapArea::EditMapArea(QWidget *parent)
41  : QWidget(parent)
42 {
43  setAttribute(Qt::WA_StaticContents);
44  modified_ = false;
45  scribbling_ = false;
46  myPenWidth_ = 3;
47 
48  menu_ = new QMenu(tr(""), this);
49  setPenWidth_ = menu_->addAction(tr("Set Pen Width..."));
50  addObstacle_ = menu_->addAction(tr("Add Obstacle"));
51  addObstacle_->setCheckable(true);
52  addObstacle_->setChecked(true);
53  clearObstacle_ = menu_->addAction(tr("Clear Obstacle"));
54  clearObstacle_->setCheckable(true);
55  clearObstacle_->setChecked(false);
56  setUnknown_ = menu_->addAction(tr("Set Unknown"));
57  setUnknown_->setCheckable(true);
58  setUnknown_->setChecked(false);
59  QActionGroup * group = new QActionGroup(this);
60  group->addAction(addObstacle_);
61  group->addAction(clearObstacle_);
62  group->addAction(setUnknown_);
63  resetChanges_ = menu_->addAction(tr("Reset Changes"));
64 }
65 
66 void EditMapArea::setMap(const cv::Mat &map)
67 {
68  UASSERT(!map.empty());
69  UASSERT(map.type() == CV_8UC1);
70  originalMap_ = map;
71 
72  map_ = uCvMat2QImage(map, true).convertToFormat(QImage::Format_RGB32);
73 
74  modified_ = false;
75  update();
76 }
77 
79 {
80  cv::Mat modifiedMap = originalMap_.clone();
81  if(modified_)
82  {
83  UASSERT(map_.width() == modifiedMap.cols &&
84  map_.height() == modifiedMap.rows);
85  UASSERT(modifiedMap.type() == CV_8UC1);
86  for(int j=0; j<map_.height(); ++j)
87  {
88  for(int i=0; i<map_.width(); ++i)
89  {
90  modifiedMap.at<unsigned char>(j,i) = qRed(map_.pixel(i, j));
91  }
92  }
93  }
94  return modifiedMap;
95 }
96 
97 void EditMapArea::setPenWidth(int newWidth)
98 {
99  myPenWidth_ = newWidth;
100 }
101 
103 {
104  map_ = uCvMat2QImage(originalMap_).convertToFormat(QImage::Format_RGB32);
105  modified_ = false;
106  update();
107 }
108 
109 void EditMapArea::mousePressEvent(QMouseEvent *event)
110 {
111  if (event->button() == Qt::LeftButton) {
112  float scale, offsetX, offsetY;
113  computeScaleOffsets(rect(), scale, offsetX, offsetY);
114  lastPoint_.setX((event->pos().x()-offsetX)/scale);
115  lastPoint_.setY((event->pos().y()-offsetY)/scale);
116 
117  scribbling_ = true;
118  }
119 }
120 
121 void EditMapArea::mouseMoveEvent(QMouseEvent *event)
122 {
123  if ((event->buttons() & Qt::LeftButton) && scribbling_)
124  {
125  float scale, offsetX, offsetY;
126  computeScaleOffsets(rect(), scale, offsetX, offsetY);
127  QPoint to;
128  to.setX((event->pos().x()-offsetX)/scale);
129  to.setY((event->pos().y()-offsetY)/scale);
130  drawLineTo(to);
131  }
132 }
133 
134 void EditMapArea::mouseReleaseEvent(QMouseEvent *event)
135 {
136  if (event->button() == Qt::LeftButton && scribbling_) {
137  float scale, offsetX, offsetY;
138  computeScaleOffsets(rect(), scale, offsetX, offsetY);
139  QPoint to;
140  to.setX((event->pos().x()-offsetX)/scale);
141  to.setY((event->pos().y()-offsetY)/scale);
142  drawLineTo(to);
143  scribbling_ = false;
144  }
145 }
146 
147 void EditMapArea::computeScaleOffsets(const QRect & targetRect, float & scale, float & offsetX, float & offsetY) const
148 {
149  scale = 1.0f;
150  offsetX = 0.0f;
151  offsetY = 0.0f;
152 
153  if(!map_.isNull())
154  {
155  float w = map_.width();
156  float h = map_.height();
157  float widthRatio = float(targetRect.width()) / w;
158  float heightRatio = float(targetRect.height()) / h;
159 
160  //printf("w=%f, h=%f, wR=%f, hR=%f, sW=%d, sH=%d\n", w, h, widthRatio, heightRatio, this->rect().width(), this->rect().height());
161  if(widthRatio < heightRatio)
162  {
163  scale = widthRatio;
164  }
165  else
166  {
167  scale = heightRatio;
168  }
169 
170  //printf("ratio=%f\n",ratio);
171 
172  w *= scale;
173  h *= scale;
174 
175  if(w < targetRect.width())
176  {
177  offsetX = (targetRect.width() - w)/2.0f;
178  }
179  if(h < targetRect.height())
180  {
181  offsetY = (targetRect.height() - h)/2.0f;
182  }
183  //printf("offsetX=%f, offsetY=%f\n",offsetX, offsetY);
184  }
185 }
186 
187 void EditMapArea::paintEvent(QPaintEvent *event)
188 {
189  //Scale
190  float ratio, offsetX, offsetY;
191  this->computeScaleOffsets(event->rect(), ratio, offsetX, offsetY);
192  QPainter painter(this);
193 
194  painter.translate(offsetX, offsetY);
195  painter.scale(ratio, ratio);
196  painter.drawImage(QPoint(0,0), map_);
197 }
198 
199 void EditMapArea::resizeEvent(QResizeEvent *event)
200 {
201  QWidget::resizeEvent(event);
202 }
203 
204 void EditMapArea::contextMenuEvent(QContextMenuEvent * e)
205 {
206  QAction * action = menu_->exec(e->globalPos());
207  if(action == setPenWidth_)
208  {
209  bool ok;
210  int width = QInputDialog::getInt(this, tr("Set Pen Width"), tr("Width:"), penWidth(), 1, 99, 1, &ok);
211  if(ok)
212  {
213  myPenWidth_ = width;
214  }
215  }
216  else if(action == resetChanges_)
217  {
218  this->resetChanges();
219  }
220 }
221 
222 void EditMapArea::drawLineTo(const QPoint &endPoint)
223 {
224  QPainter painter(&map_);
225  QColor color;
226 
227  //base on util3d::convertMap2Image8U();
228  if(addObstacle_->isChecked())
229  {
230  color.setRgb(0,0,0);
231  }
232  else if(clearObstacle_->isChecked())
233  {
234  color.setRgb(178,178,178);
235  }
236  else //unknown
237  {
238  color.setRgb(89,89,89);
239  }
240  painter.setPen(QPen(color, myPenWidth_, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
241  painter.drawLine(lastPoint_, endPoint);
242  modified_ = true;
243 
244  update();
245  lastPoint_ = endPoint;
246 }
247 
248 }
cv::Mat getModifiedMap() const
Definition: EditMapArea.cpp:78
f
EditMapArea(QWidget *parent=0)
Definition: EditMapArea.cpp:40
QAction * addObstacle_
Definition: EditMapArea.h:85
QAction * clearObstacle_
Definition: EditMapArea.h:86
GLM_FUNC_DECL genType e()
void setMap(const cv::Mat &map)
Definition: EditMapArea.cpp:66
QAction * setUnknown_
Definition: EditMapArea.h:87
GLM_FUNC_DECL detail::tmat4x4< T, P > scale(detail::tmat4x4< T, P > const &m, detail::tvec3< T, P > const &v)
void drawLineTo(const QPoint &endPoint)
virtual void contextMenuEvent(QContextMenuEvent *e)
#define UASSERT(condition)
QImage uCvMat2QImage(const cv::Mat &image, bool isBgr=true, uCvQtDepthColorMap colorMap=uCvQtDepthWhiteToBlack)
Definition: UCv2Qt.h:44
QAction * setPenWidth_
Definition: EditMapArea.h:84
void setPenWidth(int newWidth)
Definition: EditMapArea.cpp:97
virtual void paintEvent(QPaintEvent *event)
void computeScaleOffsets(const QRect &targetRect, float &scale, float &offsetX, float &offsetY) const
virtual void mousePressEvent(QMouseEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
QAction * resetChanges_
Definition: EditMapArea.h:83
ULogger class and convenient macros.
virtual void resizeEvent(QResizeEvent *event)
virtual void mouseReleaseEvent(QMouseEvent *event)
int penWidth() const
Definition: EditMapArea.h:57


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:58