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