Main MRPT website > C++ reference
MRPT logo
CColouredPointsMap.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, 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 #ifndef CColouredPointsMap_H
10 #define CColouredPointsMap_H
11 
16 #include <mrpt/math/CMatrix.h>
17 
19 
20 namespace mrpt
21 {
22  namespace slam
23  {
24  class CObservation3DRangeScan;
25 
26 
27  DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CColouredPointsMap, CPointsMap,MAPS_IMPEXP )
28 
29  /** A map of 2D/3D points with individual colours (RGB).
30  * For different color schemes, see CColouredPointsMap::colorScheme
31  * Colors are defined in the range [0,1].
32  * \sa mrpt::slam::CPointsMap, mrpt::slam::CMetricMap, mrpt::utils::CSerializable
33  * \ingroup mrpt_maps_grp
34  */
36  {
37  // This must be added to any CSerializable derived class:
39 
40  public:
41  /** Destructor
42  */
43  virtual ~CColouredPointsMap();
44 
45  /** Default constructor
46  */
47  CColouredPointsMap();
48 
49  // --------------------------------------------
50  /** @name Pure virtual interfaces to be implemented by any class derived from CPointsMap
51  @{ */
52 
53  /** Reserves memory for a given number of points: the size of the map does not change, it only reserves the memory.
54  * This is useful for situations where it is approximately known the final size of the map. This method is more
55  * efficient than constantly increasing the size of the buffers. Refer to the STL C++ library's "reserve" methods.
56  */
57  virtual void reserve(size_t newLength);
58 
59  /** Resizes all point buffers so they can hold the given number of points: newly created points are set to default values,
60  * and old contents are not changed.
61  * \sa reserve, setPoint, setPointFast, setSize
62  */
63  virtual void resize(size_t newLength);
64 
65  /** Resizes all point buffers so they can hold the given number of points, *erasing* all previous contents
66  * and leaving all points to default values.
67  * \sa reserve, setPoint, setPointFast, setSize
68  */
69  virtual void setSize(size_t newLength);
70 
71  /** Changes the coordinates of the given point (0-based index), *without* checking for out-of-bounds and *without* calling mark_as_modified() \sa setPoint */
72  virtual void setPointFast(size_t index,float x, float y, float z)
73  {
74  this->x[index] = x;
75  this->y[index] = y;
76  this->z[index] = z;
77  }
78 
79  /** The virtual method for \a insertPoint() *without* calling mark_as_modified() */
80  virtual void insertPointFast( float x, float y, float z = 0 );
81 
82  /** Virtual assignment operator, to be implemented in derived classes.
83  */
84  virtual void copyFrom(const CPointsMap &obj);
85 
86  /** Get all the data fields for one point as a vector: [X Y Z R G B]
87  * Unlike getPointAllFields(), this method does not check for index out of bounds
88  * \sa getPointAllFields, setPointAllFields, setPointAllFieldsFast
89  */
90  virtual void getPointAllFieldsFast( const size_t index, std::vector<float> & point_data ) const {
91  point_data.resize(6);
92  point_data[0] = x[index];
93  point_data[1] = y[index];
94  point_data[2] = z[index];
95  point_data[3] = m_color_R[index];
96  point_data[4] = m_color_G[index];
97  point_data[5] = m_color_B[index];
98  }
99 
100  /** Set all the data fields for one point as a vector: [X Y Z R G B]
101  * Unlike setPointAllFields(), this method does not check for index out of bounds
102  * \sa setPointAllFields, getPointAllFields, getPointAllFieldsFast
103  */
104  virtual void setPointAllFieldsFast( const size_t index, const std::vector<float> & point_data ) {
105  ASSERTDEB_(point_data.size()==6)
106  x[index] = point_data[0];
107  y[index] = point_data[1];
108  z[index] = point_data[2];
109  m_color_R[index] = point_data[3];
110  m_color_G[index] = point_data[4];
111  m_color_B[index] = point_data[5];
112  }
113 
114  /** See CPointsMap::loadFromRangeScan() */
115  virtual void loadFromRangeScan(
116  const CObservation2DRangeScan &rangeScan,
117  const CPose3D *robotPose = NULL );
118 
119  /** See CPointsMap::loadFromRangeScan() */
120  virtual void loadFromRangeScan(
121  const CObservation3DRangeScan &rangeScan,
122  const CPose3D *robotPose = NULL );
123 
124  protected:
125 
126  /** Auxiliary method called from within \a addFrom() automatically, to finish the copying of class-specific data */
127  virtual void addFrom_classSpecific(const CPointsMap &anotherMap, const size_t nPreviousPoints);
128 
130  // Friend methods:
131  template <class Derived> friend struct detail::loadFromRangeImpl;
132  template <class Derived> friend struct detail::pointmap_traits;
133 
134  public:
135 
136 
137  /** @} */
138  // --------------------------------------------
139 
140  /** Save to a text file. In each line contains X Y Z (meters) R G B (range [0,1]) for each point in the map.
141  * Returns false if any error occured, true elsewere.
142  */
143  bool save3D_and_colour_to_text_file(const std::string &file) const;
145  /** Changes a given point from map. First index is 0.
146  * \exception Throws std::exception on index out of bound.
147  */
148  virtual void setPoint(size_t index,float x, float y, float z, float R, float G, float B);
149 
150  // The following overloads must be repeated here (from CPointsMap) due to the shadowing of the above "setPoint()"
151  /// \overload
152  inline void setPoint(size_t index,float x, float y, float z) {
153  ASSERT_BELOW_(index,this->size())
154  setPointFast(index,x,y,z);
155  mark_as_modified();
156  }
157  /// \overload
158  inline void setPoint(size_t index,mrpt::math::TPoint3Df &p) { setPoint(index,p.x,p.y,p.z); }
159  /// \overload
160  inline void setPoint(size_t index,float x, float y) { setPoint(index,x,y,0); }
161 
162 
163  /** Adds a new point given its coordinates and color (colors range is [0,1]) */
164  virtual void insertPoint( float x, float y, float z, float R, float G, float B );
165  // The following overloads must be repeated here (from CPointsMap) due to the shadowing of the above "insertPoint()"
166  /// \overload of \a insertPoint()
167  inline void insertPoint( const CPoint3D &p ) { insertPoint(p.x(),p.y(),p.z()); }
168  /// \overload
169  inline void insertPoint( const mrpt::math::TPoint3D &p ) { insertPoint(p.x,p.y,p.z); }
170  /// \overload
171  inline void insertPoint( const mrpt::math::TPoint3Df &p ) { insertPoint(p.x,p.y,p.z); }
172  /// \overload
173  inline void insertPoint( float x, float y, float z) { insertPointFast(x,y,z); mark_as_modified(); }
174 
175  /** Changes just the color of a given point from the map. First index is 0.
176  * \exception Throws std::exception on index out of bound.
177  */
178  void setPointColor(size_t index,float R, float G, float B);
180  /** Like \c setPointColor but without checking for out-of-index erors */
181  inline void setPointColor_fast(size_t index,float R, float G, float B)
182  {
183  this->m_color_R[index]=R;
184  this->m_color_G[index]=G;
185  this->m_color_B[index]=B;
186  }
187 
188  /** Retrieves a point and its color (colors range is [0,1])
189  */
190  virtual void getPoint( size_t index, float &x, float &y, float &z, float &R, float &G, float &B ) const;
192  /** Retrieves a point */
193  unsigned long getPoint( size_t index, float &x, float &y, float &z) const;
194 
195  /** Retrieves a point color (colors range is [0,1]) */
196  void getPointColor( size_t index, float &R, float &G, float &B ) const;
197 
198  /** Like \c getPointColor but without checking for out-of-index erors */
199  inline void getPointColor_fast( size_t index, float &R, float &G, float &B ) const
200  {
201  R = m_color_R[index];
202  G = m_color_G[index];
203  B = m_color_B[index];
204  }
205 
206  /** Returns true if the point map has a color field for each point */
207  virtual bool hasColorPoints() const { return true; }
208 
209  /** Override of the default 3D scene builder to account for the individual points' color.
210  * \sa mrpt::global_settings::POINTSMAPS_3DOBJECT_POINTSIZE
211  */
212  virtual void getAs3DObject ( mrpt::opengl::CSetOfObjectsPtr &outObj ) const;
213 
214  /** Colour a set of points from a CObservationImage and the global pose of the robot
215  */
216  bool colourFromObservation( const CObservationImage &obs, const CPose3D &robotPose );
217 
218  /** The choices for coloring schemes:
219  * - cmFromHeightRelativeToSensor: The Z coordinate wrt the sensor will be used to obtain the color using the limits z_min,z_max.
220  * - cmFromIntensityImage: When inserting 3D range scans, take the color from the intensity image channel, if available.
221  * \sa TColourOptions
222  */
224  {
225  cmFromHeightRelativeToSensor = 0,
226  cmFromHeightRelativeToSensorJet = 0,
227  cmFromHeightRelativeToSensorGray = 1,
228  cmFromIntensityImage = 2
229  };
230 
231  /** The definition of parameters for generating colors from laser scans */
233  {
234  /** Initilization of default parameters */
235  TColourOptions( );
236  virtual ~TColourOptions() {}
237  /** See utils::CLoadableOptions
238  */
239  void loadFromConfigFile(
240  const mrpt::utils::CConfigFileBase &source,
241  const std::string &section);
242 
243  /** See utils::CLoadableOptions
244  */
245  void dumpToTextStream(CStream &out) const;
246 
248  float z_min,z_max;
249  float d_max;
250  };
251 
252  TColourOptions colorScheme; //!< The options employed when inserting laser scans in the map.
253 
254  void resetPointsMinDist( float defValue = 2000.0f ); //!< Reset the minimum-observed-distance buffer for all the points to a predefined value
255 
256  /** @name PCL library support
257  @{ */
258 
259  /** Save the point cloud as a PCL PCD file, in either ASCII or binary format \return false on any error */
260  virtual bool savePCDFile(const std::string &filename, bool save_as_binary) const;
262  /** Loads a PCL point cloud (WITH RGB information) into this MRPT class (for clouds without RGB data, see CPointsMap::setFromPCLPointCloud() ).
263  * Usage example:
264  * \code
265  * pcl::PointCloud<pcl::PointXYZRGB> cloud;
266  * mrpt::slam::CColouredPointsMap pc;
267  *
268  * pc.setFromPCLPointCloudRGB(cloud);
269  * \endcode
270  * \sa CPointsMap::setFromPCLPointCloud()
271  */
272  template <class POINTCLOUD>
273  void setFromPCLPointCloudRGB(const POINTCLOUD &cloud)
274  {
275  const size_t N = cloud.points.size();
276  clear();
277  reserve(N);
278  const float f = 1.0f/255.0f;
279  for (size_t i=0;i<N;++i)
280  this->insertPoint(cloud.points[i].x,cloud.points[i].y,cloud.points[i].z,cloud.points[i].r*f,cloud.points[i].g*f,cloud.points[i].b*f);
281  }
283  /** Like CPointsMap::getPCLPointCloud() but for PointCloud<PointXYZRGB> */
284  template <class POINTCLOUD>
285  void getPCLPointCloudXYZRGB(POINTCLOUD &cloud) const
286  {
287  const size_t nThis = this->size();
288  this->getPCLPointCloud(cloud); // 1st: xyz data
289  // 2nd: RGB data
290  for (size_t i = 0; i < nThis; ++i) {
291  float R,G,B;
292  this->getPointColor_fast(i,R,G,B);
293  cloud.points[i].r = static_cast<uint8_t>(R*255);
294  cloud.points[i].g = static_cast<uint8_t>(G*255);
295  cloud.points[i].b = static_cast<uint8_t>(B*255);
296  }
297  }
298  /** @} */
299 
300  protected:
301  /** The color data */
302  std::vector<float> m_color_R,m_color_G,m_color_B;
303 
304  /** Minimum distance from where the points have been seen */
305  //std::vector<float> m_min_dist;
306 
307  /** Clear the map, erasing all the points.
308  */
309  virtual void internal_clear();
310 
311  /** @name Redefinition of PLY Import virtual methods from CPointsMap
312  @{ */
313  /** In a base class, will be called after PLY_import_set_vertex_count() once for each loaded point.
314  * \param pt_color Will be NULL if the loaded file does not provide color info.
315  */
316  virtual void PLY_import_set_vertex(const size_t idx, const mrpt::math::TPoint3Df &pt, const mrpt::utils::TColorf *pt_color = NULL);
317 
318  /** In a base class, reserve memory to prepare subsequent calls to PLY_import_set_vertex */
319  virtual void PLY_import_set_vertex_count(const size_t N);
320  /** @} */
321 
322  /** @name Redefinition of PLY Export virtual methods from CPointsMap
323  @{ */
324  /** In a base class, will be called after PLY_export_get_vertex_count() once for each exported point.
325  * \param pt_color Will be NULL if the loaded file does not provide color info.
326  */
327  virtual void PLY_export_get_vertex(
328  const size_t idx,
330  bool &pt_has_color,
331  mrpt::utils::TColorf &pt_color) const;
332  /** @} */
334  }; // End of class def.
336 
337  } // End of namespace
338 
339 #include <mrpt/utils/adapters.h>
340  namespace utils
341  {
342  /** Specialization mrpt::utils::PointCloudAdapter<mrpt::slam::CColouredPointsMap> \ingroup mrpt_adapters_grp */
343  template <>
345  {
346  private:
348  public:
349  typedef float coords_t; //!< The type of each point XYZ coordinates
350  static const int HAS_RGB = 1; //!< Has any color RGB info?
351  static const int HAS_RGBf = 1; //!< Has native RGB info (as floats)?
352  static const int HAS_RGBu8 = 0; //!< Has native RGB info (as uint8_t)?
353 
354  /** Constructor (accept a const ref for convenience) */
355  inline PointCloudAdapter(const mrpt::slam::CColouredPointsMap &obj) : m_obj(*const_cast<mrpt::slam::CColouredPointsMap*>(&obj)) { }
356  /** Get number of points */
357  inline size_t size() const { return m_obj.size(); }
358  /** Set number of points (to uninitialized values) */
359  inline void resize(const size_t N) { m_obj.resize(N); }
360 
361  /** Get XYZ coordinates of i'th point */
362  template <typename T>
363  inline void getPointXYZ(const size_t idx, T &x,T &y, T &z) const {
364  m_obj.getPointFast(idx,x,y,z);
365  }
366  /** Set XYZ coordinates of i'th point */
367  inline void setPointXYZ(const size_t idx, const coords_t x,const coords_t y, const coords_t z) {
368  m_obj.setPointFast(idx,x,y,z);
369  }
370 
371  /** Get XYZ_RGBf coordinates of i'th point */
372  template <typename T>
373  inline void getPointXYZ_RGBf(const size_t idx, T &x,T &y, T &z, float &r,float &g,float &b) const {
374  m_obj.getPoint(idx,x,y,z,r,g,b);
375  }
376  /** Set XYZ_RGBf coordinates of i'th point */
377  inline void setPointXYZ_RGBf(const size_t idx, const coords_t x,const coords_t y, const coords_t z, const float r,const float g,const float b) {
378  m_obj.setPoint(idx,x,y,z,r,g,b);
379  }
380 
381  /** Get XYZ_RGBu8 coordinates of i'th point */
382  template <typename T>
383  inline void getPointXYZ_RGBu8(const size_t idx, T &x,T &y, T &z, uint8_t &r,uint8_t &g,uint8_t &b) const {
384  float Rf,Gf,Bf;
385  m_obj.getPoint(idx,x,y,z,Rf,Gf,Bf);
386  r=Rf*255; g=Gf*255; b=Bf*255;
387  }
388  /** Set XYZ_RGBu8 coordinates of i'th point */
389  inline void setPointXYZ_RGBu8(const size_t idx, const coords_t x,const coords_t y, const coords_t z, const uint8_t r,const uint8_t g,const uint8_t b) {
390  m_obj.setPoint(idx,x,y,z,r/255.f,g/255.f,b/255.f);
391  }
392 
393  /** Get RGBf color of i'th point */
394  inline void getPointRGBf(const size_t idx, float &r,float &g,float &b) const { m_obj.getPointColor_fast(idx,r,g,b); }
395  /** Set XYZ_RGBf coordinates of i'th point */
396  inline void setPointRGBf(const size_t idx, const float r,const float g,const float b) { m_obj.setPointColor_fast(idx,r,g,b); }
397 
398  /** Get RGBu8 color of i'th point */
399  inline void getPointRGBu8(const size_t idx, uint8_t &r,uint8_t &g,uint8_t &b) const {
400  float R,G,B;
401  m_obj.getPointColor_fast(idx,R,G,B);
402  r=R*255; g=G*255; b=B*255;
403  }
404  /** Set RGBu8 coordinates of i'th point */
405  inline void setPointRGBu8(const size_t idx,const uint8_t r,const uint8_t g,const uint8_t b) {
406  m_obj.setPointColor_fast(idx,r/255.f,g/255.f,b/255.f);
407  }
408 
409  }; // end of PointCloudAdapter<mrpt::slam::CColouredPointsMap>
410 
411  }
412 
413 } // End of namespace
414 
415 #endif
void setFromPCLPointCloudRGB(const POINTCLOUD &cloud)
Loads a PCL point cloud (WITH RGB information) into this MRPT class (for clouds without RGB data...
void insertPoint(const mrpt::math::TPoint3D &p)
The definition of parameters for generating colors from laser scans.
void setPoint(size_t index, float x, float y, float z)
float coords_t
The type of each point XYZ coordinates.
void getPointXYZ_RGBf(const size_t idx, T &x, T &y, T &z, float &r, float &g, float &b) const
Get XYZ_RGBf coordinates of i'th point.
void getPointFast(size_t index, float &x, float &y, float &z) const
Just like getPoint() but without checking out-of-bound index and without returning the point weight...
Definition: CPointsMap.h:362
void setPointXYZ_RGBf(const size_t idx, const coords_t x, const coords_t y, const coords_t z, const float r, const float g, const float b)
Set XYZ_RGBf coordinates of i'th point.
A map of 2D/3D points with individual colours (RGB).
void insertPoint(const CPoint3D &p)
size_t size(const MATRIXLIKE &m, int dim)
virtual void setPointFast(size_t index, float x, float y, float z)
Changes the coordinates of the given point (0-based index), without checking for out-of-bounds and wi...
size_t size() const
Returns the number of stored points in the map.
Definition: CPointsMap.h:331
virtual bool hasColorPoints() const
Returns true if the point map has a color field for each point.
#define ASSERT_BELOW_(__A, __B)
void resize(const size_t N)
Set number of points (to uninitialized values)
double z
X,Y,Z coordinates.
void setPointXYZ(const size_t idx, const coords_t x, const coords_t y, const coords_t z)
Set XYZ coordinates of i'th point.
void setPointXYZ_RGBu8(const size_t idx, const coords_t x, const coords_t y, const coords_t z, const uint8_t r, const uint8_t g, const uint8_t b)
Set XYZ_RGBu8 coordinates of i'th point.
void getPointXYZ_RGBu8(const size_t idx, T &x, T &y, T &z, uint8_t &r, uint8_t &g, uint8_t &b) const
Get XYZ_RGBu8 coordinates of i'th point.
TColouringMethod
The choices for coloring schemes:
std::vector< float > m_color_R
The color data.
This class allows loading and storing values and vectors of different types from a configuration text...
void insertPoint(float x, float y, float z)
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:115
Lightweight 3D point (float version).
TColourOptions colorScheme
The options employed when inserting laser scans in the map.
void setPointRGBu8(const size_t idx, const uint8_t r, const uint8_t g, const uint8_t b)
Set RGBu8 coordinates of i'th point.
virtual void resize(size_t newLength)
Resizes all point buffers so they can hold the given number of points: newly created points are set t...
EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
Changes the size of matrix, maintaining its previous content as possible and padding with zeros where...
void setPoint(size_t index, mrpt::math::TPoint3Df &p)
virtual void setPointAllFieldsFast(const size_t index, const std::vector< float > &point_data)
Set all the data fields for one point as a vector: [X Y Z R G B] Unlike setPointAllFields(), this method does not check for index out of bounds.
A class used to store a 3D point.
Definition: CPoint3D.h:32
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
void getPointRGBf(const size_t idx, float &r, float &g, float &b) const
Get RGBf color of i'th point.
void getPointXYZ(const size_t idx, T &x, T &y, T &z) const
Get XYZ coordinates of i'th point.
void insertPoint(const mrpt::math::TPoint3Df &p)
void setPointColor_fast(size_t index, float R, float G, float B)
Like setPointColor but without checking for out-of-index erors.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
An adapter to different kinds of point cloud object.
Definition: adapters.h:38
void getPCLPointCloudXYZRGB(POINTCLOUD &cloud) const
Like CPointsMap::getPCLPointCloud() but for PointCloud
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:69
void getPointRGBu8(const size_t idx, uint8_t &r, uint8_t &g, uint8_t &b) const
Get RGBu8 color of i'th point.
A RGB color - floats in the range [0,1].
Definition: TColor.h:52
virtual void setPoint(size_t index, float x, float y, float z, float R, float G, float B)
Changes a given point from map.
A cloud of points in 2D or 3D, which can be built from a sequence of laser scans or other sensors...
Definition: CPointsMap.h:59
Lightweight 3D point.
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
void setPoint(size_t index, float x, float y)
void getPointColor_fast(size_t index, float &R, float &G, float &B) const
Like getPointColor but without checking for out-of-index erors.
#define ASSERTDEB_(f)
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
virtual void getPoint(size_t index, float &x, float &y, float &z, float &R, float &G, float &B) const
Retrieves a point and its color (colors range is [0,1])



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014