NoEventsExample/MapBuilder.h
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 #ifndef MAPBUILDER_H_
29 #define MAPBUILDER_H_
30 
31 #include <QVBoxLayout>
32 #include <QtCore/QMetaType>
33 #include <QAction>
34 
35 #ifndef Q_MOC_RUN // Mac OS X issue
37 #include "rtabmap/core/util3d.h"
42 #include "rtabmap/core/Signature.h"
43 #endif
44 #include "rtabmap/utilite/UStl.h"
47 
48 using namespace rtabmap;
49 
50 // This class receives RtabmapEvent and construct/update a 3D Map
51 class MapBuilder : public QWidget
52 {
53  Q_OBJECT
54 public:
55  //Camera ownership is not transferred!
57  odometryCorrection_(Transform::getIdentity()),
58  paused_(false)
59  {
60  this->setWindowFlags(Qt::Dialog);
61  this->setWindowTitle(tr("3D Map"));
62  this->setMinimumWidth(800);
63  this->setMinimumHeight(600);
64 
65  cloudViewer_ = new CloudViewer(this);
66 
67  QVBoxLayout *layout = new QVBoxLayout();
68  layout->addWidget(cloudViewer_);
69  this->setLayout(layout);
70 
71  QAction * pause = new QAction(this);
72  this->addAction(pause);
73  pause->setShortcut(Qt::Key_Space);
74  connect(pause, SIGNAL(triggered()), this, SLOT(pauseDetection()));
75  }
76 
77  virtual ~MapBuilder()
78  {
79  }
80 
81  bool isPaused() const {return paused_;}
82 
84  const SensorData & data,
85  Transform pose,
86  const rtabmap::OdometryInfo & odom)
87  {
88  if(!this->isVisible())
89  {
90  return;
91  }
92 
93  if(pose.isNull())
94  {
95  //Odometry lost
96  cloudViewer_->setBackgroundColor(Qt::darkRed);
97 
98  pose = lastOdomPose_;
99  }
100  else
101  {
102  cloudViewer_->setBackgroundColor(cloudViewer_->getDefaultBackgroundColor());
103  }
104  if(!pose.isNull())
105  {
106  lastOdomPose_ = pose;
107 
108  // 3d cloud
109  if(data.depthOrRightRaw().cols == data.imageRaw().cols &&
110  data.depthOrRightRaw().rows == data.imageRaw().rows &&
111  !data.depthOrRightRaw().empty() &&
112  (data.stereoCameraModel().isValidForProjection() || data.cameraModels().size()))
113  {
114  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud = util3d::cloudRGBFromSensorData(
115  data,
116  4, // decimation
117  0.0f); // max depth
118  if(cloud->size())
119  {
120  if(!cloudViewer_->addCloud("cloudOdom", cloud, odometryCorrection_*pose))
121  {
122  UERROR("Adding cloudOdom to viewer failed!");
123  }
124  }
125  else
126  {
127  cloudViewer_->setCloudVisibility("cloudOdom", false);
128  UWARN("Empty cloudOdom!");
129  }
130  }
131 
132  if(!pose.isNull())
133  {
134  // update camera position
135  cloudViewer_->updateCameraTargetPosition(odometryCorrection_*pose);
136  }
137  }
138  cloudViewer_->update();
139  }
140 
141 
143  {
144 
145  //============================
146  // Add RGB-D clouds
147  //============================
148  const std::map<int, Transform> & poses = stats.poses();
149  QMap<std::string, Transform> clouds = cloudViewer_->getAddedClouds();
150  for(std::map<int, Transform>::const_iterator iter = poses.begin(); iter!=poses.end(); ++iter)
151  {
152  if(!iter->second.isNull())
153  {
154  std::string cloudName = uFormat("cloud%d", iter->first);
155 
156  // 3d point cloud
157  if(clouds.contains(cloudName))
158  {
159  // Update only if the pose has changed
160  Transform tCloud;
161  cloudViewer_->getPose(cloudName, tCloud);
162  if(tCloud.isNull() || iter->second != tCloud)
163  {
164  if(!cloudViewer_->updateCloudPose(cloudName, iter->second))
165  {
166  UERROR("Updating pose cloud %d failed!", iter->first);
167  }
168  }
169  cloudViewer_->setCloudVisibility(cloudName, true);
170  }
171  else if(uContains(stats.getSignatures(), iter->first))
172  {
173  Signature s = stats.getSignatures().at(iter->first);
174  s.sensorData().uncompressData(); // make sure data is uncompressed
175  // Add the new cloud
176  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud = util3d::cloudRGBFromSensorData(
177  s.sensorData(),
178  4, // decimation
179  4.0f); // max depth
180  if(cloud->size())
181  {
182  if(!cloudViewer_->addCloud(cloudName, cloud, iter->second))
183  {
184  UERROR("Adding cloud %d to viewer failed!", iter->first);
185  }
186  }
187  else
188  {
189  UWARN("Empty cloud %d!", iter->first);
190  }
191  }
192  }
193  else
194  {
195  UWARN("Null pose for %d ?!?", iter->first);
196  }
197  }
198 
199  //============================
200  // Add 3D graph (show all poses)
201  //============================
202  cloudViewer_->removeAllGraphs();
203  cloudViewer_->removeCloud("graph_nodes");
204  if(poses.size())
205  {
206  // Set graph
207  pcl::PointCloud<pcl::PointXYZ>::Ptr graph(new pcl::PointCloud<pcl::PointXYZ>);
208  pcl::PointCloud<pcl::PointXYZ>::Ptr graphNodes(new pcl::PointCloud<pcl::PointXYZ>);
209  for(std::map<int, Transform>::const_iterator iter=poses.begin(); iter!=poses.end(); ++iter)
210  {
211  graph->push_back(pcl::PointXYZ(iter->second.x(), iter->second.y(), iter->second.z()));
212  }
213  *graphNodes = *graph;
214 
215 
216  // add graph
217  cloudViewer_->addOrUpdateGraph("graph", graph, Qt::gray);
218  cloudViewer_->addCloud("graph_nodes", graphNodes, Transform::getIdentity(), Qt::green);
219  cloudViewer_->setCloudPointSize("graph_nodes", 5);
220  }
221 
222  odometryCorrection_ = stats.mapCorrection();
223 
224  cloudViewer_->update();
225  }
226 
227 protected Q_SLOTS:
229  {
230  paused_ = !paused_;
231  }
232 
233 protected:
237  bool paused_;
238 };
239 
240 
241 #endif /* MAPBUILDER_H_ */
bool isPaused() const
f
const std::map< int, Signature > & getSignatures() const
Definition: Statistics.h:207
void processStatistics(const rtabmap::Statistics &stats)
void processOdometry(const SensorData &data, Transform pose, const rtabmap::OdometryInfo &odom)
static Transform getIdentity()
Definition: Transform.cpp:364
Some conversion functions.
const cv::Mat & imageRaw() const
Definition: SensorData.h:161
Wrappers of STL for convenient functions.
bool isNull() const
Definition: Transform.cpp:107
pcl::PointCloud< pcl::PointXYZRGB >::Ptr RTABMAP_EXP cloudRGBFromSensorData(const SensorData &sensorData, int decimation=1, float maxDepth=0.0f, float minDepth=0.0f, std::vector< int > *validIndices=0, const ParametersMap &stereoParameters=ParametersMap(), const std::vector< float > &roiRatios=std::vector< float >())
Definition: util3d.cpp:1031
const cv::Mat & depthOrRightRaw() const
Definition: SensorData.h:162
CloudViewer * cloudViewer_
bool uContains(const std::list< V > &list, const V &value)
Definition: UStl.h:409
#define false
Definition: ConvertUTF.c:56
const std::map< int, Transform > & poses() const
Definition: Statistics.h:209
const std::vector< CameraModel > & cameraModels() const
Definition: SensorData.h:193
SensorData & sensorData()
Definition: Signature.h:134
#define UERROR(...)
ULogger class and convenient macros.
const StereoCameraModel & stereoCameraModel() const
Definition: SensorData.h:194
#define UWARN(...)
const Transform & mapCorrection() const
Definition: Statistics.h:211
std::string UTILITE_EXP uFormat(const char *fmt,...)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:31