Main MRPT website > C++ reference for MRPT 1.9.9
COctoMap.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 #include "maps-precomp.h" // Precomp header
11 
12 #include <mrpt/maps/COctoMap.h>
13 #include <mrpt/maps/CPointsMap.h>
16 #include <mrpt/utils/CStream.h>
17 #include <mrpt/system/filesystem.h>
19 
20 using namespace std;
21 using namespace mrpt;
22 using namespace mrpt::maps;
23 using namespace mrpt::obs;
24 using namespace mrpt::utils;
25 using namespace mrpt::poses;
26 using namespace mrpt::math;
27 using namespace mrpt::opengl;
28 
29 // =========== Begin of Map definition ============
31 
32 COctoMap::TMapDefinition::TMapDefinition() : resolution(0.10) {}
33 void COctoMap::TMapDefinition::loadFromConfigFile_map_specific(
35  const std::string& sectionNamePrefix)
36 {
37  // [<sectionNamePrefix>+"_creationOpts"]
38  const std::string sSectCreation =
39  sectionNamePrefix + string("_creationOpts");
40  MRPT_LOAD_CONFIG_VAR(resolution, double, source, sSectCreation);
41 
42  insertionOpts.loadFromConfigFile(
43  source, sectionNamePrefix + string("_insertOpts"));
44  likelihoodOpts.loadFromConfigFile(
45  source, sectionNamePrefix + string("_likelihoodOpts"));
46 }
47 
48 void COctoMap::TMapDefinition::dumpToTextStream_map_specific(
49  mrpt::utils::CStream& out) const
50 {
51  LOADABLEOPTS_DUMP_VAR(resolution, double);
52 
53  this->insertionOpts.dumpToTextStream(out);
54  this->likelihoodOpts.dumpToTextStream(out);
55 }
56 
57 mrpt::maps::CMetricMap* COctoMap::internal_CreateFromMapDefinition(
59 {
60  const COctoMap::TMapDefinition& def =
61  *dynamic_cast<const COctoMap::TMapDefinition*>(&_def);
62  COctoMap* obj = new COctoMap(def.resolution);
63  obj->insertionOptions = def.insertionOpts;
64  obj->likelihoodOptions = def.likelihoodOpts;
65  return obj;
66 }
67 // =========== End of Map definition Block =========
68 
70 
71 /*---------------------------------------------------------------
72  Constructor
73  ---------------------------------------------------------------*/
74 COctoMap::COctoMap(const double resolution)
75  : COctoMapBase<octomap::OcTree, octomap::OcTreeNode>(resolution)
76 {
77 }
78 
79 /*---------------------------------------------------------------
80  Destructor
81  ---------------------------------------------------------------*/
82 COctoMap::~COctoMap() {}
83 /*---------------------------------------------------------------
84  writeToStream
85  Implements the writing to a CStream capability of
86  CSerializable objects
87  ---------------------------------------------------------------*/
88 void COctoMap::writeToStream(mrpt::utils::CStream& out, int* version) const
89 {
90  if (version)
91  *version = 2;
92  else
93  {
94  this->likelihoodOptions.writeToStream(out);
95  this->renderingOptions.writeToStream(out); // Added in v1
96  out << genericMapParams; // v2
97 
98  CMemoryChunk chunk;
99  const string tmpFil = mrpt::system::getTempFileName();
100  const_cast<octomap::OcTree*>(&m_octomap)->writeBinary(tmpFil);
101  chunk.loadBufferFromFile(tmpFil);
102  mrpt::system::deleteFile(tmpFil);
103 
104  out << chunk;
105  }
106 }
107 
108 /*---------------------------------------------------------------
109  readFromStream
110  Implements the reading from a CStream capability of
111  CSerializable objects
112  ---------------------------------------------------------------*/
113 void COctoMap::readFromStream(mrpt::utils::CStream& in, int version)
114 {
115  switch (version)
116  {
117  case 0:
118  case 1:
119  case 2:
120  {
121  this->likelihoodOptions.readFromStream(in);
122  if (version >= 1) this->renderingOptions.readFromStream(in);
123  if (version >= 2) in >> genericMapParams;
124 
125  this->clear();
126 
127  CMemoryChunk chunk;
128  in >> chunk;
129 
130  if (chunk.getTotalBytesCount())
131  {
132  const string tmpFil = mrpt::system::getTempFileName();
133  if (!chunk.saveBufferToFile(tmpFil))
134  THROW_EXCEPTION("Error saving temporary file");
135  m_octomap.readBinary(tmpFil);
136  mrpt::system::deleteFile(tmpFil);
137  }
138  }
139  break;
140  default:
142  };
143 }
144 
145 bool COctoMap::internal_insertObservation(
146  const mrpt::obs::CObservation* obs, const CPose3D* robotPose)
147 {
148  octomap::point3d sensorPt;
149  octomap::Pointcloud scan;
150  if (!internal_build_PointCloud_for_observation(
151  obs, robotPose, sensorPt, scan))
152  return false; // Nothing to do.
153  // Insert rays:
154  m_octomap.insertPointCloud(
155  scan, sensorPt, insertionOptions.maxrange, insertionOptions.pruning);
156  return true;
157 }
158 
159 /** Builds a renderizable representation of the octomap as a
160  * mrpt::opengl::COctoMapVoxels object. */
161 void COctoMap::getAsOctoMapVoxels(mrpt::opengl::COctoMapVoxels& gl_obj) const
162 {
163  // Go thru all voxels:
164  // OcTreeVolume voxel; // current voxel, possibly transformed
165  octomap::OcTree::tree_iterator it_end = m_octomap.end_tree();
166 
167  const unsigned char max_depth = 0; // all
168  const TColorf general_color = gl_obj.getColor();
169  const TColor general_color_u(
170  general_color.R * 255, general_color.G * 255, general_color.B * 255,
171  general_color.A * 255);
172 
173  gl_obj.clear();
174  gl_obj.reserveGridCubes(this->calcNumNodes());
175 
176  gl_obj.resizeVoxelSets(2); // 2 sets of voxels: occupied & free
177 
178  gl_obj.showVoxels(
179  VOXEL_SET_OCCUPIED, renderingOptions.visibleOccupiedVoxels);
180  gl_obj.showVoxels(VOXEL_SET_FREESPACE, renderingOptions.visibleFreeVoxels);
181 
182  const size_t nLeafs = this->getNumLeafNodes();
183  gl_obj.reserveVoxels(VOXEL_SET_OCCUPIED, nLeafs);
184  gl_obj.reserveVoxels(VOXEL_SET_FREESPACE, nLeafs);
185 
186  double xmin, xmax, ymin, ymax, zmin, zmax, inv_dz;
187  this->getMetricMin(xmin, ymin, zmin);
188  this->getMetricMax(xmax, ymax, zmax);
189  inv_dz = 1 / (zmax - zmin + 0.01);
190 
191  for (octomap::OcTree::tree_iterator it = m_octomap.begin_tree(max_depth);
192  it != it_end; ++it)
193  {
194  const octomap::point3d vx_center = it.getCoordinate();
195  const double vx_length = it.getSize();
196  const double L = 0.5 * vx_length;
197 
198  if (it.isLeaf())
199  {
200  // voxels for leaf nodes
201  const double occ = it->getOccupancy();
202  if ((occ >= 0.5 && renderingOptions.generateOccupiedVoxels) ||
203  (occ < 0.5 && renderingOptions.generateFreeVoxels))
204  {
205  mrpt::utils::TColor vx_color;
206  double coefc, coeft;
207  switch (gl_obj.getVisualizationMode())
208  {
209  case COctoMapVoxels::FIXED:
210  vx_color = general_color_u;
211  break;
212  case COctoMapVoxels::COLOR_FROM_HEIGHT:
213  coefc = 255 * inv_dz * (vx_center.z() - zmin);
214  vx_color = TColor(
215  coefc * general_color.R, coefc * general_color.G,
216  coefc * general_color.B, 255.0 * general_color.A);
217  break;
218 
219  case COctoMapVoxels::COLOR_FROM_OCCUPANCY:
220  coefc = 240 * (1 - occ) + 15;
221  vx_color = TColor(
222  coefc * general_color.R, coefc * general_color.G,
223  coefc * general_color.B, 255.0 * general_color.A);
224  break;
225 
226  case COctoMapVoxels::TRANSPARENCY_FROM_OCCUPANCY:
227  coeft = 255 - 510 * (1 - occ);
228  if (coeft < 0)
229  {
230  coeft = 0;
231  }
232  vx_color = TColor(
233  255 * general_color.R, 255 * general_color.G,
234  255 * general_color.B, coeft);
235  break;
236 
237  case COctoMapVoxels::TRANS_AND_COLOR_FROM_OCCUPANCY:
238  coefc = 240 * (1 - occ) + 15;
239  vx_color = TColor(
240  coefc * general_color.R, coefc * general_color.G,
241  coefc * general_color.B, 50);
242  break;
243 
244  case COctoMapVoxels::MIXED:
245  coefc = 255 * inv_dz * (vx_center.z() - zmin);
246  coeft = 255 - 510 * (1 - occ);
247  if (coeft < 0)
248  {
249  coeft = 0;
250  }
251  vx_color = TColor(
252  coefc * general_color.R, coefc * general_color.G,
253  coefc * general_color.B, coeft);
254  break;
255 
256  default:
257  THROW_EXCEPTION("Unknown coloring scheme!")
258  }
259 
260  const size_t vx_set = (m_octomap.isNodeOccupied(*it))
263 
264  gl_obj.push_back_Voxel(
265  vx_set,
267  TPoint3D(vx_center.x(), vx_center.y(), vx_center.z()),
268  vx_length, vx_color));
269  }
270  }
271  else if (renderingOptions.generateGridLines)
272  {
273  // Not leaf-nodes:
274  const mrpt::math::TPoint3D pt_min(
275  vx_center.x() - L, vx_center.y() - L, vx_center.z() - L);
276  const mrpt::math::TPoint3D pt_max(
277  vx_center.x() + L, vx_center.y() + L, vx_center.z() + L);
278  gl_obj.push_back_GridCube(
279  COctoMapVoxels::TGridCube(pt_min, pt_max));
280  }
281  } // end for each voxel
282 
283  // if we use transparency, sort cubes by "Z" as an approximation to
284  // far-to-near render ordering:
285  if (gl_obj.isCubeTransparencyEnabled()) gl_obj.sort_voxels_by_z();
286 
287  // Set bounding box:
288  {
289  mrpt::math::TPoint3D bbmin, bbmax;
290  this->getMetricMin(bbmin.x, bbmin.y, bbmin.z);
291  this->getMetricMax(bbmax.x, bbmax.y, bbmax.z);
292  gl_obj.setBoundingBox(bbmin, bbmax);
293  }
294 }
void showVoxels(unsigned int voxel_set, bool show)
Shows/hides the voxels (voxel_set is a 0-based index for the set of voxels to modify, e.g.
Virtual base for specifying the kind and parameters of one map (normally, to be inserted into mrpt::m...
void reserveVoxels(const size_t set_index, const size_t nVoxels)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
double resolution
The finest resolution of the octomap (default: 0.10 meters)
Definition: COctoMap.h:48
#define THROW_EXCEPTION(msg)
virtual void writeToStream(mrpt::utils::CStream &out, int *getVersion) const =0
Introduces a pure virtual method responsible for writing to a CStream.
#define LOADABLEOPTS_DUMP_VAR(variableName, variableType)
Macro for dumping a variable to a stream, within the method "dumpToTextStream(out)" (Variable types a...
void setBoundingBox(const mrpt::math::TPoint3D &bb_min, const mrpt::math::TPoint3D &bb_max)
Manually changes the bounding box (normally the user doesn&#39;t need to call this)
void reserveGridCubes(const size_t nCubes)
STL namespace.
void clear()
Clears everything.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:189
This class allows loading and storing values and vectors of different types from a configuration text...
A three-dimensional probabilistic occupancy grid, implemented as an octo-tree with the "octomap" C++ ...
Definition: COctoMapBase.h:48
A flexible renderer of voxels, typically from a 3D octo map (see mrpt::maps::COctoMap).
void push_back_GridCube(const TGridCube &c)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:281
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
mrpt::maps::COctoMap::TInsertionOptions insertionOpts
Observations insertion options.
Definition: COctoMap.h:50
mrpt::maps::COctoMap::TLikelihoodOptions likelihoodOpts
Probabilistic observation likelihood options.
Definition: COctoMap.h:52
A RGB color - 8bit.
Definition: TColor.h:25
This namespace contains representation of robot actions and observations.
double x
X,Y,Z coordinates.
A memory buffer (implements CStream) which can be itself serialized.
Definition: CMemoryChunk.h:24
A three-dimensional probabilistic occupancy grid, implemented as an octo-tree with the "octomap" C++ ...
Definition: COctoMap.h:33
GLsizei const GLchar ** string
Definition: glext.h:4101
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
The info of each grid block.
void push_back_Voxel(const size_t set_index, const TVoxel &v)
GLclampd zmax
Definition: glext.h:7918
The info of each of the voxels.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MAP_DEFINITION_REGISTER(_CLASSNAME_STRINGS, _CLASSNAME_WITH_NS)
Registers one map class into TMetricMapInitializer factory.
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:55
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:41
void resizeVoxelSets(const size_t nVoxelSets)
visualization_mode_t getVisualizationMode() const
GLuint in
Definition: glext.h:7274
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
A RGB color - floats in the range [0,1].
Definition: TColor.h:78
bool deleteFile(const std::string &fileName)
Deletes a single file.
Definition: filesystem.cpp:179
#define MRPT_LOAD_CONFIG_VAR( variableName, variableType, configFileObject, sectionNameStr)
An useful macro for loading variables stored in a INI-like file under a key with the same name that t...
Lightweight 3D point.
bool loadBufferFromFile(const std::string &file_name)
Loads the entire buffer from a file *.
bool isCubeTransparencyEnabled() const
mrpt::utils::TColorf getColor() const
Returns the object color property as a TColorf.



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