Main MRPT website > C++ reference for MRPT 1.9.9
PbMap.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /* Plane-based Map (PbMap) library
11  * Construction of plane-based maps and localization in it from RGBD Images.
12  * Writen by Eduardo Fernandez-Moral. See docs for <a
13  * href="group__mrpt__pbmap__grp.html" >mrpt-pbmap</a>
14  */
15 #include "pbmap-precomp.h" // Precompiled headers
16 
17 #include <mrpt/pbmap.h>
18 
19 #include <mrpt/utils/CStream.h>
20 #include <pcl/io/io.h>
21 #include <pcl/io/pcd_io.h>
22 
23 using namespace std;
24 using namespace mrpt::utils;
25 using namespace mrpt::pbmap;
26 
28 
29 /*---------------------------------------------------------------
30  Constructor
31  ---------------------------------------------------------------*/
33  : FloorPlane(-1),
34  globalMapPtr(new pcl::PointCloud<pcl::PointXYZRGBA>()),
35  edgeCloudPtr(new pcl::PointCloud<pcl::PointXYZRGBA>),
36  outEdgeCloudPtr(new pcl::PointCloud<pcl::PointXYZRGBA>)
37 {
38 }
39 
40 /*---------------------------------------------------------------
41  writeToStream
42  ---------------------------------------------------------------*/
43 void PbMap::writeToStream(mrpt::utils::CStream& out, int* out_Version) const
44 {
45  // cout << "Write PbMap. Version " << *out_Version << endl;
46  if (out_Version)
47  { // cout << "There is version\n";
48  *out_Version = 0;
49  }
50  else
51  {
52  // Write label
53  out << label;
54 
55  // The data
56  uint32_t n = uint32_t(vPlanes.size());
57  out << n;
58  // cout << "Write " << n << " planes\n";
59  for (uint32_t i = 0; i < n; i++) out << vPlanes[i];
60  }
61  // cout << "Exit Write PbMap. " << endl;
62 }
63 
64 /*---------------------------------------------------------------
65  readFromStream
66  ---------------------------------------------------------------*/
67 void PbMap::readFromStream(mrpt::utils::CStream& in, int version)
68 {
69  switch (version)
70  {
71  case 0:
72  {
73  // cout << "Read planes\n";
74 
75  // Read label
76  in >> label;
77  // cout << "PbMap label " << label << endl;
78 
79  // Delete previous content:
80  vPlanes.clear();
81 
82  // The data
83  // First, write the number of planes:
84  uint32_t n;
85  in >> n;
86  vPlanes.resize(n);
87  for (uint32_t i = 0; i < n; i++)
88  {
89  // cout << "plane\n";
90 
91  Plane pl;
92  pl.id = i;
93  in >> pl;
94  vPlanes[i] = pl;
95  }
96  // cout << "Finish reading planes\n";
97  }
98  break;
99  default:
101  };
102 }
103 
104 void PbMap::savePbMap(string filePath)
105 {
106  // boost::mutex::scoped_lock lock (mtx_pbmap_busy);
107 
108  // cout << "PbMap::savePbMap\n";
109  // Serialize PbMap
110  mrpt::utils::CFileGZOutputStream serialize_pbmap(
111  filePath + "/planes.pbmap");
112  serialize_pbmap << *this;
113  serialize_pbmap.close();
114 
115  // cout << "PbMap::save cloud\n";
116  // Save reconstructed point cloud
117  pcl::io::savePCDFile(filePath + "/cloud.pcd", *this->globalMapPtr);
118 }
119 
120 void PbMap::loadPbMap(std::string filePath)
121 {
122  // Read in the cloud data
123  pcl::PCDReader reader;
124  string PbMapFile = filePath;
125  reader.read(PbMapFile.append("/cloud.pcd"), *(this->globalMapPtr));
126  // cout << "Size " << globalMapPtr->size() << " " << globalMapPtr->empty()
127  // << endl;
128 
129  // Load Previous Map
130  PbMapFile = filePath;
131  mrpt::utils::CFileGZInputStream serialized_pbmap;
132  if (serialized_pbmap.open(PbMapFile.append("/planes.pbmap")))
133  {
134  serialized_pbmap >> *this;
135  }
136  else
137  cout << "Error: cannot open " << PbMapFile << "\n";
138  serialized_pbmap.close();
139 
140  // std::cout << "Load PbMap from " << filePath << "\n";
141 }
142 
143 // Merge two pbmaps.
144 void PbMap::MergeWith(PbMap& pbm, Eigen::Matrix4f& T)
145 {
146  // Rotate and translate PbMap
147  for (size_t i = 0; i < pbm.vPlanes.size(); i++)
148  {
149  Plane plane = pbm.vPlanes[i];
150  // Plane plane = &pbm.vPlanes[i]; //Warning: It modifies the
151  // source!!!
152 
153  // Transform normal and ppal direction
154  plane.v3normal = T.block(0, 0, 3, 3) * plane.v3normal;
155  plane.v3PpalDir = T.block(0, 0, 3, 3) * plane.v3PpalDir;
156 
157  // Transform centroid
158  plane.v3center =
159  T.block(0, 0, 3, 3) * plane.v3center + T.block(0, 3, 3, 1);
160 
161  // Transform convex hull points
162  pcl::transformPointCloud(
163  *plane.polygonContourPtr, *plane.polygonContourPtr, T);
164 
165  pcl::transformPointCloud(
166  *plane.planePointCloudPtr, *plane.planePointCloudPtr, T);
167 
168  plane.id = vPlanes.size();
169 
170  vPlanes.push_back(plane);
171  }
172 
173  // Rotate and translate the point cloud
174  pcl::PointCloud<pcl::PointXYZRGBA>::Ptr alignedPointCloud(
175  new pcl::PointCloud<pcl::PointXYZRGBA>);
176  pcl::transformPointCloud(*pbm.globalMapPtr, *alignedPointCloud, T);
177 
178  *globalMapPtr += *alignedPointCloud;
179 }
180 
181 #include <fstream>
182 // Print PbMap content to a text file
183 void PbMap::printPbMap(string txtFilePbm)
184 {
185  cout << "PbMap 0.2\n\n";
186 
187  ofstream pbm;
188  pbm.open(txtFilePbm.c_str());
189  pbm << "PbMap 0.2\n\n";
190  pbm << "MapPlanes " << vPlanes.size() << endl;
191  for (unsigned i = 0; i < vPlanes.size(); i++)
192  {
193  pbm << " ID " << vPlanes[i].id << " obs " << vPlanes[i].numObservations;
194  pbm << " areaVoxels " << vPlanes[i].areaVoxels << " areaHull "
195  << vPlanes[i].areaHull;
196  pbm << " ratioXY " << vPlanes[i].elongation << " structure "
197  << vPlanes[i].bFromStructure << " label " << vPlanes[i].label;
198  pbm << "\n normal\n"
199  << vPlanes[i].v3normal << "\n center\n"
200  << vPlanes[i].v3center;
201  pbm << "\n PpalComp\n"
202  << vPlanes[i].v3PpalDir << "\n RGB\n"
203  << vPlanes[i].v3colorNrgb;
204  pbm << "\n Neighbors (" << vPlanes[i].neighborPlanes.size() << "): ";
206  vPlanes[i].neighborPlanes.begin();
207  it != vPlanes[i].neighborPlanes.end(); it++)
208  pbm << it->first << " ";
209  pbm << "\n CommonObservations: ";
211  vPlanes[i].neighborPlanes.begin();
212  it != vPlanes[i].neighborPlanes.end(); it++)
213  pbm << it->second << " ";
214  pbm << "\n ConvexHull (" << vPlanes[i].polygonContourPtr->size()
215  << "): \n";
216  for (unsigned j = 0; j < vPlanes[i].polygonContourPtr->size(); j++)
217  pbm << "\t" << vPlanes[i].polygonContourPtr->points[j].x << " "
218  << vPlanes[i].polygonContourPtr->points[j].y << " "
219  << vPlanes[i].polygonContourPtr->points[j].z << endl;
220  pbm << endl;
221  }
222  pbm.close();
223 }
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr polygonContourPtr
Definition: Plane.h:184
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
A class used to store a planar feature (Plane for short).
Definition: Plane.h:48
Eigen::Vector3f v3PpalDir
Definition: Plane.h:149
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
bool open(const std::string &fileName)
Opens the file for read.
GLenum GLsizei n
Definition: glext.h:5074
Scalar * iterator
Definition: eigen_plugins.h:26
for(ctr=DCTSIZE;ctr > 0;ctr--)
Definition: jidctflt.cpp:56
STL namespace.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Eigen::Vector3f v3normal
Definition: Plane.h:144
Transparently opens a compressed "gz" file and reads uncompressed data from it.
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr planePointCloudPtr
Definition: Plane.h:188
GLsizei const GLchar ** string
Definition: glext.h:4101
pcl::PointCloud< PointT >::Ptr globalMapPtr
Definition: pbmap/PbMap.h:69
#define CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level...
unsigned id
! Parameters to allow the plane-based representation of the map by a graph
Definition: Plane.h:129
GLuint in
Definition: glext.h:7274
Eigen::Vector3f v3center
! Geometric description
Definition: Plane.h:143
std::vector< Plane > vPlanes
Definition: pbmap/PbMap.h:59
unsigned __int32 uint32_t
Definition: rptypes.h:47
A class used to store a Plane-based Map (PbMap).
Definition: pbmap/PbMap.h:49



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019