Point Cloud Library (PCL)  1.9.1-dev
ply_io.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/io/boost.h>
43 #include <pcl/io/file_io.h>
44 #include <pcl/io/ply/ply_parser.h>
45 #include <pcl/PolygonMesh.h>
46 #include <sstream>
47 
48 namespace pcl
49 {
50  /** \brief Point Cloud Data (PLY) file format reader.
51  *
52  * The PLY data format is organized in the following way:
53  * lines beginning with "comment" are treated as comments
54  * - ply
55  * - format [ascii|binary_little_endian|binary_big_endian] 1.0
56  * - element vertex COUNT
57  * - property float x
58  * - property float y
59  * - [property float z]
60  * - [property float normal_x]
61  * - [property float normal_y]
62  * - [property float normal_z]
63  * - [property uchar red]
64  * - [property uchar green]
65  * - [property uchar blue] ...
66  * - ascii/binary point coordinates
67  * - [element camera 1]
68  * - [property float view_px] ...
69  * - [element range_grid COUNT]
70  * - [property list uchar int vertex_indices]
71  * - end header
72  *
73  * \author Nizar Sallem
74  * \ingroup io
75  */
76  class PCL_EXPORTS PLYReader : public FileReader
77  {
78  public:
79  enum
80  {
81  PLY_V0 = 0,
82  PLY_V1 = 1
83  };
84 
86  : FileReader ()
87  , parser_ ()
88  , origin_ (Eigen::Vector4f::Zero ())
89  , orientation_ (Eigen::Matrix3f::Zero ())
90  , cloud_ ()
91  , vertex_count_ (0)
92  , vertex_offset_before_ (0)
93  , range_grid_ (0)
94  , rgb_offset_before_ (0)
95  , do_resize_ (false)
96  , polygons_ (0)
97  , r_(0), g_(0), b_(0)
98  , a_(0), rgba_(0)
99  {}
100 
101  PLYReader (const PLYReader &p)
102  : FileReader ()
103  , parser_ ()
104  , origin_ (Eigen::Vector4f::Zero ())
105  , orientation_ (Eigen::Matrix3f::Zero ())
106  , cloud_ ()
107  , vertex_count_ (0)
108  , vertex_offset_before_ (0)
109  , range_grid_ (0)
110  , rgb_offset_before_ (0)
111  , do_resize_ (false)
112  , polygons_ (0)
113  , r_(0), g_(0), b_(0)
114  , a_(0), rgba_(0)
115  {
116  *this = p;
117  }
118 
119  PLYReader&
120  operator = (const PLYReader &p)
121  {
122  origin_ = p.origin_;
123  orientation_ = p.orientation_;
124  range_grid_ = p.range_grid_;
125  polygons_ = p.polygons_;
126  return (*this);
127  }
128 
129  ~PLYReader () { delete range_grid_; }
130  /** \brief Read a point cloud data header from a PLY file.
131  *
132  * Load only the meta information (number of points, their types, etc),
133  * and not the points themselves, from a given PLY file. Useful for fast
134  * evaluation of the underlying data structure.
135  *
136  * Returns:
137  * * < 0 (-1) on error
138  * * > 0 on success
139  * \param[in] file_name the name of the file to load
140  * \param[out] cloud the resultant point cloud dataset (only the header will be filled)
141  * \param[in] origin the sensor data acquisition origin (translation)
142  * \param[in] orientation the sensor data acquisition origin (rotation)
143  * \param[out] ply_version the PLY version read from the file
144  * \param[out] data_type the type of PLY data stored in the file
145  * \param[out] data_idx the data index
146  * \param[in] offset the offset in the file where to expect the true header to begin.
147  * One usage example for setting the offset parameter is for reading
148  * data from a TAR "archive containing multiple files: TAR files always
149  * add a 512 byte header in front of the actual file, so set the offset
150  * to the next byte after the header (e.g., 513).
151  */
152  int
153  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
154  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
155  int &ply_version, int &data_type, unsigned int &data_idx, const int offset = 0) override;
156 
157  /** \brief Read a point cloud data from a PLY file and store it into a pcl/PCLPointCloud2.
158  * \param[in] file_name the name of the file containing the actual PointCloud data
159  * \param[out] cloud the resultant PointCloud message read from disk
160  * \param[in] origin the sensor data acquisition origin (translation)
161  * \param[in] orientation the sensor data acquisition origin (rotation)
162  * \param[out] ply_version the PLY version read from the file
163  * \param[in] offset the offset in the file where to expect the true header to begin.
164  * One usage example for setting the offset parameter is for reading
165  * data from a TAR "archive containing multiple files: TAR files always
166  * add a 512 byte header in front of the actual file, so set the offset
167  * to the next byte after the header (e.g., 513).
168  */
169  int
170  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
171  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int& ply_version, const int offset = 0) override;
172 
173  /** \brief Read a point cloud data from a PLY file and store it into a pcl/PCLPointCloud2.
174  * \note This function is provided for backwards compatibility only
175  * \param[in] file_name the name of the file containing the actual PointCloud data
176  * \param[out] cloud the resultant PointCloud message read from disk
177  * \param[in] offset the offset in the file where to expect the true header to begin.
178  * One usage example for setting the offset parameter is for reading
179  * data from a TAR "archive containing multiple files: TAR files always
180  * add a 512 byte header in front of the actual file, so set the offset
181  * to the next byte after the header (e.g., 513).
182  */
183  inline int
184  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0)
185  {
186  Eigen::Vector4f origin;
187  Eigen::Quaternionf orientation;
188  int ply_version;
189  return read (file_name, cloud, origin, orientation, ply_version, offset);
190  }
191 
192  /** \brief Read a point cloud data from any PLY file, and convert it to the given template format.
193  * \param[in] file_name the name of the file containing the actual PointCloud data
194  * \param[out] cloud the resultant PointCloud message read from disk
195  * \param[in] offset the offset in the file where to expect the true header to begin.
196  * One usage example for setting the offset parameter is for reading
197  * data from a TAR "archive containing multiple files: TAR files always
198  * add a 512 byte header in front of the actual file, so set the offset
199  * to the next byte after the header (e.g., 513).
200  */
201  template<typename PointT> inline int
202  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud, const int offset = 0)
203  {
204  pcl::PCLPointCloud2 blob;
205  int ply_version;
206  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
207  ply_version, offset);
208 
209  // Exit in case of error
210  if (res < 0)
211  return (res);
212  pcl::fromPCLPointCloud2 (blob, cloud);
213  return (0);
214  }
215 
216  /** \brief Read a point cloud data from a PLY file and store it into a pcl/PolygonMesh.
217  *
218  * \param[in] file_name the name of the file containing the actual PointCloud data
219  * \param[out] mesh the resultant PolygonMesh message read from disk
220  * \param[in] origin the sensor data acquisition origin (translation)
221  * \param[in] orientation the sensor data acquisition origin (rotation)
222  * \param[out] ply_version the PLY version read from the file
223  * \param[in] offset the offset in the file where to expect the true header to begin.
224  * One usage example for setting the offset parameter is for reading
225  * data from a TAR "archive containing multiple files: TAR files always
226  * add a 512 byte header in front of the actual file, so set the offset
227  * to the next byte after the header (e.g., 513).
228  */
229  int
230  read (const std::string &file_name, pcl::PolygonMesh &mesh,
231  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
232  int& ply_version, const int offset = 0);
233 
234  /** \brief Read a point cloud data from a PLY file and store it into a pcl/PolygonMesh.
235  *
236  * \param[in] file_name the name of the file containing the actual PointCloud data
237  * \param[out] mesh the resultant PolygonMesh message read from disk
238  * \param[in] offset the offset in the file where to expect the true header to begin.
239  * One usage example for setting the offset parameter is for reading
240  * data from a TAR "archive containing multiple files: TAR files always
241  * add a 512 byte header in front of the actual file, so set the offset
242  * to the next byte after the header (e.g., 513).
243  */
244  int
245  read (const std::string &file_name, pcl::PolygonMesh &mesh, const int offset = 0);
246 
247  private:
249 
250  bool
251  parse (const std::string& istream_filename);
252 
253  /** \brief Info callback function
254  * \param[in] filename PLY file read
255  * \param[in] line_number line triggering the callback
256  * \param[in] message information message
257  */
258  void
259  infoCallback (const std::string& filename, std::size_t line_number, const std::string& message)
260  {
261  PCL_DEBUG ("[pcl::PLYReader] %s:%lu: %s\n", filename.c_str (), line_number, message.c_str ());
262  }
263 
264  /** \brief Warning callback function
265  * \param[in] filename PLY file read
266  * \param[in] line_number line triggering the callback
267  * \param[in] message warning message
268  */
269  void
270  warningCallback (const std::string& filename, std::size_t line_number, const std::string& message)
271  {
272  PCL_WARN ("[pcl::PLYReader] %s:%lu: %s\n", filename.c_str (), line_number, message.c_str ());
273  }
274 
275  /** \brief Error callback function
276  * \param[in] filename PLY file read
277  * \param[in] line_number line triggering the callback
278  * \param[in] message error message
279  */
280  void
281  errorCallback (const std::string& filename, std::size_t line_number, const std::string& message)
282  {
283  PCL_ERROR ("[pcl::PLYReader] %s:%lu: %s\n", filename.c_str (), line_number, message.c_str ());
284  }
285 
286  /** \brief function called when the keyword element is parsed
287  * \param[in] element_name element name
288  * \param[in] count number of instances
289  */
290  boost::tuple<boost::function<void ()>, boost::function<void ()> >
291  elementDefinitionCallback (const std::string& element_name, std::size_t count);
292 
293  bool
294  endHeaderCallback ();
295 
296  /** \brief function called when a scalar property is parsed
297  * \param[in] element_name element name to which the property belongs
298  * \param[in] property_name property name
299  */
300  template <typename ScalarType> boost::function<void (ScalarType)>
301  scalarPropertyDefinitionCallback (const std::string& element_name, const std::string& property_name);
302 
303  /** \brief function called when a list property is parsed
304  * \param[in] element_name element name to which the property belongs
305  * \param[in] property_name list property name
306  */
307  template <typename SizeType, typename ScalarType>
308  boost::tuple<boost::function<void (SizeType)>, boost::function<void (ScalarType)>, boost::function<void ()> >
309  listPropertyDefinitionCallback (const std::string& element_name, const std::string& property_name);
310 
311  /** \brief function called at the beginning of a list property parsing.
312  * \param[in] size number of elements in the list
313  */
314  template <typename SizeType> void
315  vertexListPropertyBeginCallback (const std::string& property_name, SizeType size);
316 
317  /** \brief function called when a list element is parsed.
318  * \param[in] value the list's element value
319  */
320  template <typename ContentType> void
321  vertexListPropertyContentCallback (ContentType value);
322 
323  /** \brief function called at the end of a list property parsing */
324  inline void
325  vertexListPropertyEndCallback ();
326 
327  /** Callback function for an anonymous vertex scalar property.
328  * Writes down a double value in cloud data.
329  * param[in] value double value parsed
330  */
331  template<typename Scalar> void
332  vertexScalarPropertyCallback (Scalar value);
333 
334  /** Callback function for vertex RGB color.
335  * This callback is in charge of packing red green and blue in a single int
336  * before writing it down in cloud data.
337  * param[in] color_name color name in {red, green, blue}
338  * param[in] color value of {red, green, blue} property
339  */
340  inline void
341  vertexColorCallback (const std::string& color_name, pcl::io::ply::uint8 color);
342 
343  /** Callback function for vertex intensity.
344  * converts intensity from int to float before writing it down in cloud data.
345  * param[in] intensity
346  */
347  inline void
348  vertexIntensityCallback (pcl::io::ply::uint8 intensity);
349 
350  /** Callback function for vertex alpha.
351  * extracts RGB value, append alpha and put it back
352  * param[in] alpha
353  */
354  inline void
355  vertexAlphaCallback (pcl::io::ply::uint8 alpha);
356 
357  /** Callback function for origin x component.
358  * param[in] value origin x value
359  */
360  inline void
361  originXCallback (const float& value) { origin_[0] = value; }
362 
363  /** Callback function for origin y component.
364  * param[in] value origin y value
365  */
366  inline void
367  originYCallback (const float& value) { origin_[1] = value; }
368 
369  /** Callback function for origin z component.
370  * param[in] value origin z value
371  */
372  inline void
373  originZCallback (const float& value) { origin_[2] = value; }
374 
375  /** Callback function for orientation x axis x component.
376  * param[in] value orientation x axis x value
377  */
378  inline void
379  orientationXaxisXCallback (const float& value) { orientation_ (0,0) = value; }
380 
381  /** Callback function for orientation x axis y component.
382  * param[in] value orientation x axis y value
383  */
384  inline void
385  orientationXaxisYCallback (const float& value) { orientation_ (0,1) = value; }
386 
387  /** Callback function for orientation x axis z component.
388  * param[in] value orientation x axis z value
389  */
390  inline void
391  orientationXaxisZCallback (const float& value) { orientation_ (0,2) = value; }
392 
393  /** Callback function for orientation y axis x component.
394  * param[in] value orientation y axis x value
395  */
396  inline void
397  orientationYaxisXCallback (const float& value) { orientation_ (1,0) = value; }
398 
399  /** Callback function for orientation y axis y component.
400  * param[in] value orientation y axis y value
401  */
402  inline void
403  orientationYaxisYCallback (const float& value) { orientation_ (1,1) = value; }
404 
405  /** Callback function for orientation y axis z component.
406  * param[in] value orientation y axis z value
407  */
408  inline void
409  orientationYaxisZCallback (const float& value) { orientation_ (1,2) = value; }
410 
411  /** Callback function for orientation z axis x component.
412  * param[in] value orientation z axis x value
413  */
414  inline void
415  orientationZaxisXCallback (const float& value) { orientation_ (2,0) = value; }
416 
417  /** Callback function for orientation z axis y component.
418  * param[in] value orientation z axis y value
419  */
420  inline void
421  orientationZaxisYCallback (const float& value) { orientation_ (2,1) = value; }
422 
423  /** Callback function for orientation z axis z component.
424  * param[in] value orientation z axis z value
425  */
426  inline void
427  orientationZaxisZCallback (const float& value) { orientation_ (2,2) = value; }
428 
429  /** Callback function to set the cloud height
430  * param[in] height cloud height
431  */
432  inline void
433  cloudHeightCallback (const int &height) { cloud_->height = height; }
434 
435  /** Callback function to set the cloud width
436  * param[in] width cloud width
437  */
438  inline void
439  cloudWidthCallback (const int &width) { cloud_->width = width; }
440 
441  /** Append a scalar property to the cloud fields.
442  * param[in] name property name
443  * param[in] count property count: 1 for scalar properties and higher for a
444  * list property.
445  */
446  template<typename Scalar> void
447  appendScalarProperty (const std::string& name, const size_t& count = 1);
448 
449  /** Amend property from cloud fields identified by \a old_name renaming
450  * it \a new_name.
451  * param[in] old_name property old name
452  * param[in] new_name property new name
453  */
454  void
455  amendProperty (const std::string& old_name, const std::string& new_name, uint8_t datatype = 0);
456 
457  /** Callback function for the begin of vertex line */
458  void
459  vertexBeginCallback ();
460 
461  /** Callback function for the end of vertex line */
462  void
463  vertexEndCallback ();
464 
465  /** Callback function for the begin of range_grid line */
466  void
467  rangeGridBeginCallback ();
468 
469  /** Callback function for the begin of range_grid vertex_indices property
470  * param[in] size vertex_indices list size
471  */
472  void
473  rangeGridVertexIndicesBeginCallback (pcl::io::ply::uint8 size);
474 
475  /** Callback function for each range_grid vertex_indices element
476  * param[in] vertex_index index of the vertex in vertex_indices
477  */
478  void
479  rangeGridVertexIndicesElementCallback (pcl::io::ply::int32 vertex_index);
480 
481  /** Callback function for the end of a range_grid vertex_indices property */
482  void
483  rangeGridVertexIndicesEndCallback ();
484 
485  /** Callback function for the end of a range_grid element end */
486  void
487  rangeGridEndCallback ();
488 
489  /** Callback function for obj_info */
490  void
491  objInfoCallback (const std::string& line);
492 
493  /** Callback function for the begin of face line */
494  void
495  faceBeginCallback ();
496 
497  /** Callback function for the begin of face vertex_indices property
498  * param[in] size vertex_indices list size
499  */
500  void
501  faceVertexIndicesBeginCallback (pcl::io::ply::uint8 size);
502 
503  /** Callback function for each face vertex_indices element
504  * param[in] vertex_index index of the vertex in vertex_indices
505  */
506  void
507  faceVertexIndicesElementCallback (pcl::io::ply::int32 vertex_index);
508 
509  /** Callback function for the end of a face vertex_indices property */
510  void
511  faceVertexIndicesEndCallback ();
512 
513  /** Callback function for the end of a face element end */
514  void
515  faceEndCallback ();
516 
517  /// origin
518  Eigen::Vector4f origin_;
519 
520  /// orientation
521  Eigen::Matrix3f orientation_;
522 
523  //vertex element artifacts
524  pcl::PCLPointCloud2 *cloud_;
525  size_t vertex_count_;
526  int vertex_offset_before_;
527  //range element artifacts
528  std::vector<std::vector <int> > *range_grid_;
529  size_t rgb_offset_before_;
530  bool do_resize_;
531  //face element artifact
532  std::vector<pcl::Vertices> *polygons_;
533  public:
534  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
535 
536  private:
537  // RGB values stored by vertexColorCallback()
538  int32_t r_, g_, b_;
539  // Color values stored by vertexAlphaCallback()
540  uint32_t a_, rgba_;
541  };
542 
543  /** \brief Point Cloud Data (PLY) file format writer.
544  * \author Nizar Sallem
545  * \ingroup io
546  */
547  class PCL_EXPORTS PLYWriter : public FileWriter
548  {
549  public:
550  ///Constructor
551  PLYWriter () : FileWriter () {};
552 
553  ///Destructor
554  ~PLYWriter () {};
555 
556  /** \brief Generate the header of a PLY v.7 file format
557  * \param[in] cloud the point cloud data message
558  * \param[in] origin the sensor data acquisition origin (translation)
559  * \param[in] orientation the sensor data acquisition origin (rotation)
560  * \param[in] valid_points number of valid points (finite ones for range_grid and
561  * all of them for camer)
562  * \param[in] use_camera if set to true then PLY file will use element camera else
563  * element range_grid will be used.
564  */
565  inline std::string
567  const Eigen::Vector4f &origin,
568  const Eigen::Quaternionf &orientation,
569  int valid_points,
570  bool use_camera = true)
571  {
572  return (generateHeader (cloud, origin, orientation, true, use_camera, valid_points));
573  }
574 
575  /** \brief Generate the header of a PLY v.7 file format
576  * \param[in] cloud the point cloud data message
577  * \param[in] origin the sensor data acquisition origin (translation)
578  * \param[in] orientation the sensor data acquisition origin (rotation)
579  * \param[in] valid_points number of valid points (finite ones for range_grid and
580  * all of them for camer)
581  * \param[in] use_camera if set to true then PLY file will use element camera else
582  * element range_grid will be used.
583  */
584  inline std::string
586  const Eigen::Vector4f &origin,
587  const Eigen::Quaternionf &orientation,
588  int valid_points,
589  bool use_camera = true)
590  {
591  return (generateHeader (cloud, origin, orientation, false, use_camera, valid_points));
592  }
593 
594  /** \brief Save point cloud data to a PLY file containing n-D points, in ASCII format
595  * \param[in] file_name the output file name
596  * \param[in] cloud the point cloud data message
597  * \param[in] origin the sensor data acquisition origin (translation)
598  * \param[in] orientation the sensor data acquisition origin (rotation)
599  * \param[in] precision the specified output numeric stream precision (default: 8)
600  * \param[in] use_camera if set to true then PLY file will use element camera else
601  * element range_grid will be used.
602  */
603  int
604  writeASCII (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
605  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
606  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
607  int precision = 8,
608  bool use_camera = true);
609 
610  /** \brief Save point cloud data to a PLY file containing n-D points, in BINARY format
611  * \param[in] file_name the output file name
612  * \param[in] cloud the point cloud data message
613  * \param[in] origin the sensor data acquisition origin (translation)
614  * \param[in] orientation the sensor data acquisition origin (rotation)
615  * \param[in] use_camera if set to true then PLY file will use element camera else
616  * element range_grid will be used
617  */
618  int
619  writeBinary (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
620  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
621  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
622  bool use_camera = true);
623 
624  /** \brief Save point cloud data to a PLY file containing n-D points
625  * \param[in] file_name the output file name
626  * \param[in] cloud the point cloud data message
627  * \param[in] origin the sensor acquisition origin
628  * \param[in] orientation the sensor acquisition orientation
629  * \param[in] binary set to true if the file is to be written in a binary
630  * PLY format, false (default) for ASCII
631  */
632  inline int
633  write (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
634  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
635  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
636  const bool binary = false) override
637  {
638  if (binary)
639  return (this->writeBinary (file_name, cloud, origin, orientation, true));
640  else
641  return (this->writeASCII (file_name, cloud, origin, orientation, 8, true));
642  }
643 
644  /** \brief Save point cloud data to a PLY file containing n-D points
645  * \param[in] file_name the output file name
646  * \param[in] cloud the point cloud data message
647  * \param[in] origin the sensor acquisition origin
648  * \param[in] orientation the sensor acquisition orientation
649  * \param[in] binary set to true if the file is to be written in a binary
650  * PLY format, false (default) for ASCII
651  * \param[in] use_camera set to true to use camera element and false to
652  * use range_grid element
653  */
654  inline int
655  write (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
656  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
657  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
658  bool binary = false,
659  bool use_camera = true)
660  {
661  if (binary)
662  return (this->writeBinary (file_name, cloud, origin, orientation, use_camera));
663  else
664  return (this->writeASCII (file_name, cloud, origin, orientation, 8, use_camera));
665  }
666 
667  /** \brief Save point cloud data to a PLY file containing n-D points
668  * \param[in] file_name the output file name
669  * \param[in] cloud the point cloud data message (boost shared pointer)
670  * \param[in] origin the sensor acquisition origin
671  * \param[in] orientation the sensor acquisition orientation
672  * \param[in] binary set to true if the file is to be written in a binary
673  * PLY format, false (default) for ASCII
674  * \param[in] use_camera set to true to use camera element and false to
675  * use range_grid element
676  */
677  inline int
678  write (const std::string &file_name, const pcl::PCLPointCloud2::ConstPtr &cloud,
679  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
680  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
681  bool binary = false,
682  bool use_camera = true)
683  {
684  return (write (file_name, *cloud, origin, orientation, binary, use_camera));
685  }
686 
687  /** \brief Save point cloud data to a PLY file containing n-D points
688  * \param[in] file_name the output file name
689  * \param[in] cloud the pcl::PointCloud data
690  * \param[in] binary set to true if the file is to be written in a binary
691  * PLY format, false (default) for ASCII
692  * \param[in] use_camera set to true to use camera element and false to
693  * use range_grid element
694  */
695  template<typename PointT> inline int
696  write (const std::string &file_name,
697  const pcl::PointCloud<PointT> &cloud,
698  bool binary = false,
699  bool use_camera = true)
700  {
701  Eigen::Vector4f origin = cloud.sensor_origin_;
702  Eigen::Quaternionf orientation = cloud.sensor_orientation_;
703 
704  pcl::PCLPointCloud2 blob;
705  pcl::toPCLPointCloud2 (cloud, blob);
706 
707  // Save the data
708  return (this->write (file_name, blob, origin, orientation, binary, use_camera));
709  }
710 
711  private:
712  /** \brief Generate a PLY header.
713  * \param[in] cloud the input point cloud
714  * \param[in] binary whether the PLY file should be saved as binary data (true) or ascii (false)
715  */
716  std::string
717  generateHeader (const pcl::PCLPointCloud2 &cloud,
718  const Eigen::Vector4f &origin,
719  const Eigen::Quaternionf &orientation,
720  bool binary,
721  bool use_camera,
722  int valid_points);
723 
724  void
725  writeContentWithCameraASCII (int nr_points,
726  int point_size,
727  const pcl::PCLPointCloud2 &cloud,
728  const Eigen::Vector4f &origin,
729  const Eigen::Quaternionf &orientation,
730  std::ofstream& fs);
731 
732  void
733  writeContentWithRangeGridASCII (int nr_points,
734  int point_size,
735  const pcl::PCLPointCloud2 &cloud,
736  std::ostringstream& fs,
737  int& nb_valid_points);
738  };
739 
740  namespace io
741  {
742  /** \brief Load a PLY v.6 file into a templated PointCloud type.
743  *
744  * Any PLY files containing sensor data will generate a warning as a
745  * pcl/PCLPointCloud2 message cannot hold the sensor origin.
746  *
747  * \param[in] file_name the name of the file to load
748  * \param[in] cloud the resultant templated point cloud
749  * \ingroup io
750  */
751  inline int
752  loadPLYFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud)
753  {
754  pcl::PLYReader p;
755  return (p.read (file_name, cloud));
756  }
757 
758  /** \brief Load any PLY file into a templated PointCloud type.
759  * \param[in] file_name the name of the file to load
760  * \param[in] cloud the resultant templated point cloud
761  * \param[in] origin the sensor acquisition origin (only for > PLY_V7 - null if not present)
762  * \param[in] orientation the sensor acquisition orientation if available,
763  * identity if not present
764  * \ingroup io
765  */
766  inline int
767  loadPLYFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
768  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation)
769  {
770  pcl::PLYReader p;
771  int ply_version;
772  return (p.read (file_name, cloud, origin, orientation, ply_version));
773  }
774 
775  /** \brief Load any PLY file into a templated PointCloud type
776  * \param[in] file_name the name of the file to load
777  * \param[in] cloud the resultant templated point cloud
778  * \ingroup io
779  */
780  template<typename PointT> inline int
781  loadPLYFile (const std::string &file_name, pcl::PointCloud<PointT> &cloud)
782  {
783  pcl::PLYReader p;
784  return (p.read (file_name, cloud));
785  }
786 
787  /** \brief Load a PLY file into a PolygonMesh type.
788  *
789  * Any PLY files containing sensor data will generate a warning as a
790  * pcl/PolygonMesh message cannot hold the sensor origin.
791  *
792  * \param[in] file_name the name of the file to load
793  * \param[in] mesh the resultant polygon mesh
794  * \ingroup io
795  */
796  inline int
797  loadPLYFile (const std::string &file_name, pcl::PolygonMesh &mesh)
798  {
799  pcl::PLYReader p;
800  return (p.read (file_name, mesh));
801  }
802 
803  /** \brief Save point cloud data to a PLY file containing n-D points
804  * \param[in] file_name the output file name
805  * \param[in] cloud the point cloud data message
806  * \param[in] origin the sensor data acquisition origin (translation)
807  * \param[in] orientation the sensor data acquisition origin (rotation)
808  * \param[in] binary_mode true for binary mode, false (default) for ASCII
809  * \param[in] use_camera
810  * \ingroup io
811  */
812  inline int
813  savePLYFile (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
814  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
815  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
816  bool binary_mode = false, bool use_camera = true)
817  {
818  PLYWriter w;
819  return (w.write (file_name, cloud, origin, orientation, binary_mode, use_camera));
820  }
821 
822  /** \brief Templated version for saving point cloud data to a PLY file
823  * containing a specific given cloud format
824  * \param[in] file_name the output file name
825  * \param[in] cloud the point cloud data message
826  * \param[in] binary_mode true for binary mode, false (default) for ASCII
827  * \ingroup io
828  */
829  template<typename PointT> inline int
830  savePLYFile (const std::string &file_name, const pcl::PointCloud<PointT> &cloud, bool binary_mode = false)
831  {
832  PLYWriter w;
833  return (w.write<PointT> (file_name, cloud, binary_mode));
834  }
835 
836  /** \brief Templated version for saving point cloud data to a PLY file
837  * containing a specific given cloud format.
838  * \param[in] file_name the output file name
839  * \param[in] cloud the point cloud data message
840  * \ingroup io
841  */
842  template<typename PointT> inline int
843  savePLYFileASCII (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
844  {
845  PLYWriter w;
846  return (w.write<PointT> (file_name, cloud, false));
847  }
848 
849  /** \brief Templated version for saving point cloud data to a PLY file containing a specific given cloud format.
850  * \param[in] file_name the output file name
851  * \param[in] cloud the point cloud data message
852  * \ingroup io
853  */
854  template<typename PointT> inline int
855  savePLYFileBinary (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
856  {
857  PLYWriter w;
858  return (w.write<PointT> (file_name, cloud, true));
859  }
860 
861  /** \brief Templated version for saving point cloud data to a PLY file containing a specific given cloud format
862  * \param[in] file_name the output file name
863  * \param[in] cloud the point cloud data message
864  * \param[in] indices the set of indices to save
865  * \param[in] binary_mode true for binary mode, false (default) for ASCII
866  * \ingroup io
867  */
868  template<typename PointT> int
869  savePLYFile (const std::string &file_name, const pcl::PointCloud<PointT> &cloud,
870  const std::vector<int> &indices, bool binary_mode = false)
871  {
872  // Copy indices to a new point cloud
873  pcl::PointCloud<PointT> cloud_out;
874  copyPointCloud (cloud, indices, cloud_out);
875  // Save the data
876  PLYWriter w;
877  return (w.write<PointT> (file_name, cloud_out, binary_mode));
878  }
879 
880  /** \brief Saves a PolygonMesh in ascii PLY format.
881  * \param[in] file_name the name of the file to write to disk
882  * \param[in] mesh the polygonal mesh to save
883  * \param[in] precision the output ASCII precision default 5
884  * \ingroup io
885  */
886  PCL_EXPORTS int
887  savePLYFile (const std::string &file_name, const pcl::PolygonMesh &mesh, unsigned precision = 5);
888 
889  /** \brief Saves a PolygonMesh in binary PLY format.
890  * \param[in] file_name the name of the file to write to disk
891  * \param[in] mesh the polygonal mesh to save
892  * \ingroup io
893  */
894  PCL_EXPORTS int
895  savePLYFileBinary (const std::string &file_name, const pcl::PolygonMesh &mesh);
896  }
897 }
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map...
Definition: conversions.h:168
PLYReader(const PLYReader &p)
Definition: ply_io.h:101
std::string generateHeaderASCII(const pcl::PCLPointCloud2 &cloud, const Eigen::Vector4f &origin, const Eigen::Quaternionf &orientation, int valid_points, bool use_camera=true)
Generate the header of a PLY v.7 file format.
Definition: ply_io.h:585
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
~PLYWriter()
Destructor.
Definition: ply_io.h:554
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
Point Cloud Data (FILE) file format writer.
Definition: file_io.h:161
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
int write(const std::string &file_name, const pcl::PCLPointCloud2::ConstPtr &cloud, const Eigen::Vector4f &origin=Eigen::Vector4f::Zero(), const Eigen::Quaternionf &orientation=Eigen::Quaternionf::Identity(), bool binary=false, bool use_camera=true)
Save point cloud data to a PLY file containing n-D points.
Definition: ply_io.h:678
Definition: bfgs.h:9
int read(const std::string &file_name, pcl::PointCloud< PointT > &cloud, const int offset=0)
Read a point cloud data from any PLY file, and convert it to the given template format.
Definition: ply_io.h:202
std::string generateHeaderBinary(const pcl::PCLPointCloud2 &cloud, const Eigen::Vector4f &origin, const Eigen::Quaternionf &orientation, int valid_points, bool use_camera=true)
Generate the header of a PLY v.7 file format.
Definition: ply_io.h:566
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:420
PCL_EXPORTS int savePLYFileBinary(const std::string &file_name, const pcl::PolygonMesh &mesh)
Saves a PolygonMesh in binary PLY format.
Point Cloud Data (PLY) file format writer.
Definition: ply_io.h:547
int savePLYFileASCII(const std::string &file_name, const pcl::PointCloud< PointT > &cloud)
Templated version for saving point cloud data to a PLY file containing a specific given cloud format...
Definition: ply_io.h:843
int loadPLYFile(const std::string &file_name, pcl::PolygonMesh &mesh)
Load a PLY file into a PolygonMesh type.
Definition: ply_io.h:797
Class ply_parser parses a PLY file and generates appropriate atomic parsers for the body...
Definition: ply_parser.h:76
Point Cloud Data (FILE) file format reader interface.
Definition: file_io.h:55
int write(const std::string &file_name, const pcl::PCLPointCloud2 &cloud, const Eigen::Vector4f &origin=Eigen::Vector4f::Zero(), const Eigen::Quaternionf &orientation=Eigen::Quaternionf::Identity(), bool binary=false, bool use_camera=true)
Save point cloud data to a PLY file containing n-D points.
Definition: ply_io.h:655
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:422
boost::int32_t int32
Definition: ply.h:60
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
PointCloud represents the base class in PCL for storing collections of 3D points. ...
int write(const std::string &file_name, const pcl::PCLPointCloud2 &cloud, const Eigen::Vector4f &origin=Eigen::Vector4f::Zero(), const Eigen::Quaternionf &orientation=Eigen::Quaternionf::Identity(), const bool binary=false) override
Save point cloud data to a PLY file containing n-D points.
Definition: ply_io.h:633
int write(const std::string &file_name, const pcl::PointCloud< PointT > &cloud, bool binary=false, bool use_camera=true)
Save point cloud data to a PLY file containing n-D points.
Definition: ply_io.h:696
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
Definition: conversions.h:241
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset=0)
Read a point cloud data from a PLY file and store it into a pcl/PCLPointCloud2.
Definition: ply_io.h:184
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &ply_version, const int offset=0) override
Read a point cloud data from a PLY file and store it into a pcl/PCLPointCloud2.
Point Cloud Data (PLY) file format reader.
Definition: ply_io.h:76
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63
boost::uint8_t uint8
Definition: ply.h:61
A point structure representing Euclidean xyz coordinates, and the RGB color.
PLYWriter()
Constructor.
Definition: ply_io.h:551
PCL_EXPORTS int savePLYFile(const std::string &file_name, const pcl::PolygonMesh &mesh, unsigned precision=5)
Saves a PolygonMesh in ascii PLY format.