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 <QActionGroup>
34 #include <QInputDialog>
35 
38 
39 namespace rtabmap {
40 
41 EditMapArea::EditMapArea(QWidget *parent)
42  : QWidget(parent)
43 {
44  setAttribute(Qt::WA_StaticContents);
45  modified_ = false;
46  scribbling_ = false;
47  myPenWidth_ = 3;
48 
49  menu_ = new QMenu(tr(""), this);
50  setPenWidth_ = menu_->addAction(tr("Set Pen Width..."));
51  addObstacle_ = menu_->addAction(tr("Add Obstacle"));
52  addObstacle_->setCheckable(true);
53  addObstacle_->setChecked(true);
54  clearObstacle_ = menu_->addAction(tr("Clear Obstacle"));
55  clearObstacle_->setCheckable(true);
56  clearObstacle_->setChecked(false);
57  setUnknown_ = menu_->addAction(tr("Set Unknown"));
58  setUnknown_->setCheckable(true);
59  setUnknown_->setChecked(false);
60  QActionGroup * group = new QActionGroup(this);
61  group->addAction(addObstacle_);
62  group->addAction(clearObstacle_);
63  group->addAction(setUnknown_);
64  resetChanges_ = menu_->addAction(tr("Reset Changes"));
65 }
66 
67 void EditMapArea::setMap(const cv::Mat &map)
68 {
69  UASSERT(!map.empty());
70  UASSERT(map.type() == CV_8UC1);
71  originalMap_ = map;
72 
73  map_ = uCvMat2QImage(map, true).convertToFormat(QImage::Format_RGB32);
74 
75  modified_ = false;
76  update();
77 }
78 
80 {
81  cv::Mat modifiedMap = originalMap_.clone();
82  if(modified_)
83  {
84  UASSERT(map_.width() == modifiedMap.cols &&
85  map_.height() == modifiedMap.rows);
86  UASSERT(modifiedMap.type() == CV_8UC1);
87  for(int j=0; j<map_.height(); ++j)
88  {
89  for(int i=0; i<map_.width(); ++i)
90  {
91  modifiedMap.at<unsigned char>(j,i) = qRed(map_.pixel(i, j));
92  }
93  }
94  }
95  return modifiedMap;
96 }
97 
98 void EditMapArea::setPenWidth(int newWidth)
99 {
100  myPenWidth_ = newWidth;
101 }
102 
104 {
105  map_ = uCvMat2QImage(originalMap_).convertToFormat(QImage::Format_RGB32);
106  modified_ = false;
107  update();
108 }
109 
110 void EditMapArea::mousePressEvent(QMouseEvent *event)
111 {
112  if (event->button() == Qt::LeftButton) {
113  float scale, offsetX, offsetY;
114  computeScaleOffsets(rect(), scale, offsetX, offsetY);
115  lastPoint_.setX((event->pos().x()-offsetX)/scale);
116  lastPoint_.setY((event->pos().y()-offsetY)/scale);
117 
118  scribbling_ = true;
119  }
120 }
121 
122 void EditMapArea::mouseMoveEvent(QMouseEvent *event)
123 {
124  if ((event->buttons() & Qt::LeftButton) && scribbling_)
125  {
126  float scale, offsetX, offsetY;
127  computeScaleOffsets(rect(), scale, offsetX, offsetY);
128  QPoint to;
129  to.setX((event->pos().x()-offsetX)/scale);
130  to.setY((event->pos().y()-offsetY)/scale);
131  drawLineTo(to);
132  }
133 }
134 
135 void EditMapArea::mouseReleaseEvent(QMouseEvent *event)
136 {
137  if (event->button() == Qt::LeftButton && scribbling_) {
138  float scale, offsetX, offsetY;
139  computeScaleOffsets(rect(), scale, offsetX, offsetY);
140  QPoint to;
141  to.setX((event->pos().x()-offsetX)/scale);
142  to.setY((event->pos().y()-offsetY)/scale);
143  drawLineTo(to);
144  scribbling_ = false;
145  }
146 }
147 
148 void EditMapArea::computeScaleOffsets(const QRect & targetRect, float & scale, float & offsetX, float & offsetY) const
149 {
150  scale = 1.0f;
151  offsetX = 0.0f;
152  offsetY = 0.0f;
153 
154  if(!map_.isNull())
155  {
156  float w = map_.width();
157  float h = map_.height();
158  float widthRatio = float(targetRect.width()) / w;
159  float heightRatio = float(targetRect.height()) / h;
160 
161  //printf("w=%f, h=%f, wR=%f, hR=%f, sW=%d, sH=%d\n", w, h, widthRatio, heightRatio, this->rect().width(), this->rect().height());
162  if(widthRatio < heightRatio)
163  {
164  scale = widthRatio;
165  }
166  else
167  {
168  scale = heightRatio;
169  }
170 
171  //printf("ratio=%f\n",ratio);
172 
173  w *= scale;
174  h *= scale;
175 
176  if(w < targetRect.width())
177  {
178  offsetX = (targetRect.width() - w)/2.0f;
179  }
180  if(h < targetRect.height())
181  {
182  offsetY = (targetRect.height() - h)/2.0f;
183  }
184  //printf("offsetX=%f, offsetY=%f\n",offsetX, offsetY);
185  }
186 }
187 
188 void EditMapArea::paintEvent(QPaintEvent *event)
189 {
190  //Scale
191  float ratio, offsetX, offsetY;
192  this->computeScaleOffsets(event->rect(), ratio, offsetX, offsetY);
193  QPainter painter(this);
194 
195  painter.translate(offsetX, offsetY);
196  painter.scale(ratio, ratio);
197  painter.drawImage(QPoint(0,0), map_);
198 }
199 
200 void EditMapArea::resizeEvent(QResizeEvent *event)
201 {
202  QWidget::resizeEvent(event);
203 }
204 
205 void EditMapArea::contextMenuEvent(QContextMenuEvent * e)
206 {
207  QAction * action = menu_->exec(e->globalPos());
208  if(action == setPenWidth_)
209  {
210  bool ok;
211  int width = QInputDialog::getInt(this, tr("Set Pen Width"), tr("Width:"), penWidth(), 1, 99, 1, &ok);
212  if(ok)
213  {
214  myPenWidth_ = width;
215  }
216  }
217  else if(action == resetChanges_)
218  {
219  this->resetChanges();
220  }
221 }
222 
223 void EditMapArea::drawLineTo(const QPoint &endPoint)
224 {
225  QPainter painter(&map_);
226  QColor color;
227 
228  //base on util3d::convertMap2Image8U();
229  if(addObstacle_->isChecked())
230  {
231  color.setRgb(0,0,0);
232  }
233  else if(clearObstacle_->isChecked())
234  {
235  color.setRgb(178,178,178);
236  }
237  else //unknown
238  {
239  color.setRgb(89,89,89);
240  }
241  painter.setPen(QPen(color, myPenWidth_, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
242  painter.drawLine(lastPoint_, endPoint);
243  modified_ = true;
244 
245  update();
246  lastPoint_ = endPoint;
247 }
248 
249 }
w
RowVector3d w
rtabmap::EditMapArea::setPenWidth_
QAction * setPenWidth_
Definition: EditMapArea.h:84
update
def update(text)
rtabmap::EditMapArea::resetChanges
void resetChanges()
Definition: EditMapArea.cpp:103
rtabmap::EditMapArea::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: EditMapArea.cpp:135
rtabmap::EditMapArea::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: EditMapArea.cpp:122
h
const double h
rtabmap::EditMapArea::getModifiedMap
cv::Mat getModifiedMap() const
Definition: EditMapArea.cpp:79
EditMapArea.h
rtabmap::EditMapArea::scribbling_
bool scribbling_
Definition: EditMapArea.h:76
rtabmap::EditMapArea::originalMap_
cv::Mat originalMap_
Definition: EditMapArea.h:79
rtabmap::EditMapArea::EditMapArea
EditMapArea(QWidget *parent=0)
Definition: EditMapArea.cpp:41
rtabmap::EditMapArea::penWidth
int penWidth() const
Definition: EditMapArea.h:57
rtabmap::EditMapArea::map_
QImage map_
Definition: EditMapArea.h:78
rtabmap::EditMapArea::computeScaleOffsets
void computeScaleOffsets(const QRect &targetRect, float &scale, float &offsetX, float &offsetY) const
Definition: EditMapArea.cpp:148
rtabmap::EditMapArea::modified_
bool modified_
Definition: EditMapArea.h:75
rtabmap::EditMapArea::resetChanges_
QAction * resetChanges_
Definition: EditMapArea.h:83
rtabmap::EditMapArea::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event)
Definition: EditMapArea.cpp:110
rtabmap::EditMapArea::myPenWidth_
int myPenWidth_
Definition: EditMapArea.h:77
scale
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics scale
j
std::ptrdiff_t j
rtabmap::EditMapArea::paintEvent
virtual void paintEvent(QPaintEvent *event)
Definition: EditMapArea.cpp:188
UASSERT
#define UASSERT(condition)
action
action
f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
rtabmap::EditMapArea::resizeEvent
virtual void resizeEvent(QResizeEvent *event)
Definition: EditMapArea.cpp:200
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
ULogger.h
ULogger class and convenient macros.
rtabmap::EditMapArea::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *e)
Definition: EditMapArea.cpp:205
rtabmap::EditMapArea::addObstacle_
QAction * addObstacle_
Definition: EditMapArea.h:85
rtabmap::EditMapArea::menu_
QMenu * menu_
Definition: EditMapArea.h:82
ratio
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size ratio
rtabmap::EditMapArea::setUnknown_
QAction * setUnknown_
Definition: EditMapArea.h:87
float
float
rtabmap::EditMapArea::drawLineTo
void drawLineTo(const QPoint &endPoint)
Definition: EditMapArea.cpp:223
uCvMat2QImage
QImage uCvMat2QImage(const cv::Mat &image, bool isBgr=true, uCvQtDepthColorMap colorMap=uCvQtDepthWhiteToBlack, float depthMin=0, float depthMax=0)
Definition: UCv2Qt.h:47
rtabmap::EditMapArea::setPenWidth
void setPenWidth(int newWidth)
Definition: EditMapArea.cpp:98
rtabmap
Definition: CameraARCore.cpp:35
i
int i
rtabmap::EditMapArea::clearObstacle_
QAction * clearObstacle_
Definition: EditMapArea.h:86
rtabmap::EditMapArea::setMap
void setMap(const cv::Mat &map)
Definition: EditMapArea.cpp:67
rtabmap::EditMapArea::lastPoint_
QPoint lastPoint_
Definition: EditMapArea.h:80


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:09