Point Cloud Library (PCL)  1.9.1-dev
pcd_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/pcl_macros.h>
43 #include <pcl/point_cloud.h>
44 #include <pcl/io/file_io.h>
45 
46 namespace pcl
47 {
48  /** \brief Point Cloud Data (PCD) file format reader.
49  * \author Radu B. Rusu
50  * \ingroup io
51  */
52  class PCL_EXPORTS PCDReader : public FileReader
53  {
54  public:
55  /** Empty constructor */
56  PCDReader () {}
57  /** Empty destructor */
58  ~PCDReader () {}
59 
60  /** \brief Various PCD file versions.
61  *
62  * PCD_V6 represents PCD files with version 0.6, which contain the following fields:
63  * - lines beginning with # are treated as comments
64  * - FIELDS ...
65  * - SIZE ...
66  * - TYPE ...
67  * - COUNT ...
68  * - WIDTH ...
69  * - HEIGHT ...
70  * - POINTS ...
71  * - DATA ascii/binary
72  *
73  * Everything that follows \b DATA is interpreted as data points and
74  * will be read accordingly.
75  *
76  * PCD_V7 represents PCD files with version 0.7 and has an important
77  * addon: it adds sensor origin/orientation (aka viewpoint) information
78  * to a dataset through the use of a new header field:
79  * - VIEWPOINT tx ty tz qw qx qy qz
80  */
81  enum
82  {
83  PCD_V6 = 0,
84  PCD_V7 = 1
85  };
86 
87  /** \brief Read a point cloud data header from a PCD-formatted, binary istream.
88  *
89  * Load only the meta information (number of points, their types, etc),
90  * and not the points themselves, from a given PCD stream. Useful for fast
91  * evaluation of the underlying data structure.
92  *
93  * \attention The PCD data is \b always stored in ROW major format! The
94  * read/write PCD methods will detect column major input and automatically convert it.
95  *
96  * \param[in] binary_istream a std::istream with openmode set to std::ios::binary.
97  * \param[out] cloud the resultant point cloud dataset (only these
98  * members will be filled: width, height, point_step,
99  * row_step, fields[]; data is resized but not written)
100  * \param[out] origin the sensor acquisition origin (only for > PCD_V7 - null if not present)
101  * \param[out] orientation the sensor acquisition orientation (only for > PCD_V7 - identity if not present)
102  * \param[out] pcd_version the PCD version of the file (i.e., PCD_V6, PCD_V7)
103  * \param[out] data_type the type of data (0 = ASCII, 1 = Binary, 2 = Binary compressed)
104  * \param[out] data_idx the offset of cloud data within the file
105  *
106  * \return
107  * * < 0 (-1) on error
108  * * == 0 on success
109  */
110  int
111  readHeader (std::istream &binary_istream, pcl::PCLPointCloud2 &cloud,
112  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version,
113  int &data_type, unsigned int &data_idx);
114 
115  /** \brief Read a point cloud data header from a PCD file.
116  *
117  * Load only the meta information (number of points, their types, etc),
118  * and not the points themselves, from a given PCD file. Useful for fast
119  * evaluation of the underlying data structure.
120  *
121  * \attention The PCD data is \b always stored in ROW major format! The
122  * read/write PCD methods will detect column major input and automatically convert it.
123  *
124  * \param[in] file_name the name of the file to load
125  * \param[out] cloud the resultant point cloud dataset (only these
126  * members will be filled: width, height, point_step,
127  * row_step, fields[]; data is resized but not written)
128  * \param[out] origin the sensor acquisition origin (only for > PCD_V7 - null if not present)
129  * \param[out] orientation the sensor acquisition orientation (only for > PCD_V7 - identity if not present)
130  * \param[out] pcd_version the PCD version of the file (i.e., PCD_V6, PCD_V7)
131  * \param[out] data_type the type of data (0 = ASCII, 1 = Binary, 2 = Binary compressed)
132  * \param[out] data_idx the offset of cloud data within the file
133  * \param[in] offset the offset of where to expect the PCD Header in the
134  * file (optional parameter). One usage example for setting the offset
135  * parameter is for reading data from a TAR "archive containing multiple
136  * PCD files: TAR files always add a 512 byte header in front of the
137  * actual file, so set the offset to the next byte after the header
138  * (e.g., 513).
139  *
140  * \return
141  * * < 0 (-1) on error
142  * * == 0 on success
143  */
144  int
145  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
146  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version,
147  int &data_type, unsigned int &data_idx, const int offset = 0) override;
148 
149 
150  /** \brief Read a point cloud data header from a PCD file.
151  *
152  * Load only the meta information (number of points, their types, etc),
153  * and not the points themselves, from a given PCD file. Useful for fast
154  * evaluation of the underlying data structure.
155  *
156  * \attention The PCD data is \b always stored in ROW major format! The
157  * read/write PCD methods will detect column major input and automatically convert it.
158  *
159  * \param[in] file_name the name of the file to load
160  * \param[out] cloud the resultant point cloud dataset (only these
161  * members will be filled: width, height, point_step,
162  * row_step, fields[]; data is resized but not written)
163  * \param[in] offset the offset of where to expect the PCD Header in the
164  * file (optional parameter). One usage example for setting the offset
165  * parameter is for reading data from a TAR "archive containing multiple
166  * PCD files: TAR files always add a 512 byte header in front of the
167  * actual file, so set the offset to the next byte after the header
168  * (e.g., 513).
169  *
170  * \return
171  * * < 0 (-1) on error
172  * * == 0 on success
173  */
174  int
175  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0);
176 
177  /** \brief Read the point cloud data (body) from a PCD stream.
178  *
179  * Reads the cloud points from a text-formatted stream. For use after
180  * readHeader(), when the resulting data_type == 0.
181  *
182  * \attention This assumes the stream has been seeked to the position
183  * indicated by the data_idx result of readHeader().
184  *
185  * \param[in] stream the stream from which to read the body.
186  * \param[out] cloud the resultant point cloud dataset to be filled.
187  * \param[in] pcd_version the PCD version of the stream (from readHeader()).
188  *
189  * \return
190  * * < 0 (-1) on error
191  * * == 0 on success
192  */
193  int
194  readBodyASCII (std::istream &stream, pcl::PCLPointCloud2 &cloud, int pcd_version);
195 
196  /** \brief Read the point cloud data (body) from a block of memory.
197  *
198  * Reads the cloud points from a binary-formatted memory block. For use
199  * after readHeader(), when the resulting data_type is nonzero.
200  *
201  * \param[in] data the memory location from which to read the body.
202  * \param[out] cloud the resultant point cloud dataset to be filled.
203  * \param[in] pcd_version the PCD version of the stream (from readHeader()).
204  * \param[in] compressed indicates whether the PCD block contains compressed
205  * data. This should be true if the data_type returne by readHeader() == 2.
206  * \param[in] data_idx the offset of the body, as reported by readHeader().
207  *
208  * \return
209  * * < 0 (-1) on error
210  * * == 0 on success
211  */
212  int
213  readBodyBinary (const unsigned char *data, pcl::PCLPointCloud2 &cloud,
214  int pcd_version, bool compressed, unsigned int data_idx);
215 
216  /** \brief Read a point cloud data from a PCD file and store it into a pcl/PCLPointCloud2.
217  * \param[in] file_name the name of the file containing the actual PointCloud data
218  * \param[out] cloud the resultant PointCloud message read from disk
219  * \param[out] origin the sensor acquisition origin (only for > PCD_V7 - null if not present)
220  * \param[out] orientation the sensor acquisition orientation (only for > PCD_V7 - identity if not present)
221  * \param[out] pcd_version the PCD version of the file (either PCD_V6 or PCD_V7)
222  * \param[in] offset the offset of where to expect the PCD Header in the
223  * file (optional parameter). One usage example for setting the offset
224  * parameter is for reading data from a TAR "archive containing multiple
225  * PCD files: TAR files always add a 512 byte header in front of the
226  * actual file, so set the offset to the next byte after the header
227  * (e.g., 513).
228  *
229  * \return
230  * * < 0 (-1) on error
231  * * == 0 on success
232  */
233  int
234  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
235  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version, const int offset = 0) override;
236 
237  /** \brief Read a point cloud data from a PCD (PCD_V6) and store it into a pcl/PCLPointCloud2.
238  *
239  * \note This function is provided for backwards compatibility only and
240  * it can only read PCD_V6 files correctly, as pcl::PCLPointCloud2
241  * does not contain a sensor origin/orientation. Reading any file
242  * > PCD_V6 will generate a warning.
243  *
244  * \param[in] file_name the name of the file containing the actual PointCloud data
245  * \param[out] cloud the resultant PointCloud message read from disk
246  * \param[in] offset the offset of where to expect the PCD Header in the
247  * file (optional parameter). One usage example for setting the offset
248  * parameter is for reading data from a TAR "archive containing multiple
249  * PCD files: TAR files always add a 512 byte header in front of the
250  * actual file, so set the offset to the next byte after the header
251  * (e.g., 513).
252  *
253  * \return
254  * * < 0 (-1) on error
255  * * == 0 on success
256  */
257  int
258  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0);
259 
260  /** \brief Read a point cloud data from any PCD file, and convert it to the given template format.
261  * \param[in] file_name the name of the file containing the actual PointCloud data
262  * \param[out] cloud the resultant PointCloud message read from disk
263  * \param[in] offset the offset of where to expect the PCD Header in the
264  * file (optional parameter). One usage example for setting the offset
265  * parameter is for reading data from a TAR "archive containing multiple
266  * PCD files: TAR files always add a 512 byte header in front of the
267  * actual file, so set the offset to the next byte after the header
268  * (e.g., 513).
269  *
270  * \return
271  * * < 0 (-1) on error
272  * * == 0 on success
273  */
274  template<typename PointT> int
275  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud, const int offset = 0)
276  {
277  pcl::PCLPointCloud2 blob;
278  int pcd_version;
279  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
280  pcd_version, offset);
281 
282  // If no error, convert the data
283  if (res == 0)
284  pcl::fromPCLPointCloud2 (blob, cloud);
285  return (res);
286  }
287 
289  };
290 
291  /** \brief Point Cloud Data (PCD) file format writer.
292  * \author Radu Bogdan Rusu
293  * \ingroup io
294  */
295  class PCL_EXPORTS PCDWriter : public FileWriter
296  {
297  public:
298  PCDWriter() : map_synchronization_(false) {}
300 
301  /** \brief Set whether mmap() synchornization via msync() is desired before munmap() calls.
302  * Setting this to true could prevent NFS data loss (see
303  * http://www.pcl-developers.org/PCD-IO-consistency-on-NFS-msync-needed-td4885942.html).
304  * Default: false
305  * \note This option should be used by advanced users only!
306  * \note Please note that using msync() on certain systems can reduce the I/O performance by up to 80%!
307  * \param[in] sync set to true if msync() should be called before munmap()
308  */
309  void
311  {
312  map_synchronization_ = sync;
313  }
314 
315  /** \brief Generate the header of a PCD file format
316  * \param[in] cloud the point cloud data message
317  * \param[in] origin the sensor acquisition origin
318  * \param[in] orientation the sensor acquisition orientation
319  */
320  std::string
321  generateHeaderBinary (const pcl::PCLPointCloud2 &cloud,
322  const Eigen::Vector4f &origin,
323  const Eigen::Quaternionf &orientation);
324 
325  /** \brief Generate the header of a BINARY_COMPRESSED PCD file format
326  * \param[out] os the stream into which to write the header
327  * \param[in] cloud the point cloud data message
328  * \param[in] origin the sensor acquisition origin
329  * \param[in] orientation the sensor acquisition orientation
330  *
331  * \return
332  * * < 0 (-1) on error
333  * * == 0 on success
334  */
335  int
336  generateHeaderBinaryCompressed (std::ostream &os,
337  const pcl::PCLPointCloud2 &cloud,
338  const Eigen::Vector4f &origin,
339  const Eigen::Quaternionf &orientation);
340 
341  /** \brief Generate the header of a BINARY_COMPRESSED PCD file format
342  * \param[out] os the stream into which to write the header
343  * \param[in] cloud the point cloud data message
344  * \param[in] origin the sensor acquisition origin
345  * \param[in] orientation the sensor acquisition orientation
346  */
347  std::string
348  generateHeaderBinaryCompressed (const pcl::PCLPointCloud2 &cloud,
349  const Eigen::Vector4f &origin,
350  const Eigen::Quaternionf &orientation);
351 
352  /** \brief Generate the header of a PCD file format
353  * \param[in] cloud the point cloud data message
354  * \param[in] origin the sensor acquisition origin
355  * \param[in] orientation the sensor acquisition orientation
356  */
357  std::string
358  generateHeaderASCII (const pcl::PCLPointCloud2 &cloud,
359  const Eigen::Vector4f &origin,
360  const Eigen::Quaternionf &orientation);
361 
362  /** \brief Generate the header of a PCD file format
363  * \param[in] cloud the point cloud data message
364  * \param[in] nr_points if given, use this to fill in WIDTH, HEIGHT (=1), and POINTS in the header
365  * By default, nr_points is set to INTMAX, and the data in the header is used instead.
366  */
367  template <typename PointT> static std::string
368  generateHeader (const pcl::PointCloud<PointT> &cloud,
369  const int nr_points = std::numeric_limits<int>::max ());
370 
371  /** \brief Save point cloud data to a PCD file containing n-D points, in ASCII format
372  * \param[in] file_name the output file name
373  * \param[in] cloud the point cloud data message
374  * \param[in] origin the sensor acquisition origin
375  * \param[in] orientation the sensor acquisition orientation
376  * \param[in] precision the specified output numeric stream precision (default: 8)
377  *
378  * Caution: PointCloud structures containing an RGB field have
379  * traditionally used packed float values to store RGB data. Storing a
380  * float as ASCII can introduce variations to the smallest bits, and
381  * thus significantly alter the data. This is a known issue, and the fix
382  * involves switching RGB data to be stored as a packed integer in
383  * future versions of PCL.
384  *
385  * As an intermediary solution, precision 8 is used, which guarantees lossless storage for RGB.
386  */
387  int
388  writeASCII (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
389  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
390  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
391  const int precision = 8);
392 
393  /** \brief Save point cloud data to a PCD file containing n-D points, in BINARY format
394  * \param[in] file_name the output file name
395  * \param[in] cloud the point cloud data message
396  * \param[in] origin the sensor acquisition origin
397  * \param[in] orientation the sensor acquisition orientation
398  */
399  int
400  writeBinary (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
401  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
402  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity ());
403 
404  /** \brief Save point cloud data to a PCD file containing n-D points, in BINARY_COMPRESSED format
405  * \param[in] file_name the output file name
406  * \param[in] cloud the point cloud data message
407  * \param[in] origin the sensor acquisition origin
408  * \param[in] orientation the sensor acquisition orientation
409  * \return
410  * (-1) for a general error
411  * (-2) if the input cloud is too large for the file format
412  * 0 on success
413  */
414  int
415  writeBinaryCompressed (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
416  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
417  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity ());
418 
419  /** \brief Save point cloud data to a std::ostream containing n-D points, in BINARY_COMPRESSED format
420  * \param[out] os the stream into which to write the data
421  * \param[in] cloud the point cloud data message
422  * \param[in] origin the sensor acquisition origin
423  * \param[in] orientation the sensor acquisition orientation
424  * \return
425  * (-1) for a general error
426  * (-2) if the input cloud is too large for the file format
427  * 0 on success
428  */
429  int
430  writeBinaryCompressed (std::ostream &os, const pcl::PCLPointCloud2 &cloud,
431  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
432  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity ());
433 
434  /** \brief Save point cloud data to a PCD file containing n-D points
435  * \param[in] file_name the output file name
436  * \param[in] cloud the point cloud data message
437  * \param[in] origin the sensor acquisition origin
438  * \param[in] orientation the sensor acquisition orientation
439  * \param[in] binary set to true if the file is to be written in a binary
440  * PCD format, false (default) for ASCII
441  *
442  * Caution: PointCloud structures containing an RGB field have
443  * traditionally used packed float values to store RGB data. Storing a
444  * float as ASCII can introduce variations to the smallest bits, and
445  * thus significantly alter the data. This is a known issue, and the fix
446  * involves switching RGB data to be stored as a packed integer in
447  * future versions of PCL.
448  *
449  * As an intermediary solution, precision 8 is used, which guarantees lossless storage for RGB.
450  */
451  inline int
452  write (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
453  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
454  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
455  const bool binary = false) override
456  {
457  if (binary)
458  return (writeBinary (file_name, cloud, origin, orientation));
459  return (writeASCII (file_name, cloud, origin, orientation, 8));
460  }
461 
462  /** \brief Save point cloud data to a PCD file containing n-D points
463  * \param[in] file_name the output file name
464  * \param[in] cloud the point cloud data message (boost shared pointer)
465  * \param[in] binary set to true if the file is to be written in a binary PCD format,
466  * false (default) for ASCII
467  * \param[in] origin the sensor acquisition origin
468  * \param[in] orientation the sensor acquisition orientation
469  *
470  * Caution: PointCloud structures containing an RGB field have
471  * traditionally used packed float values to store RGB data. Storing a
472  * float as ASCII can introduce variations to the smallest bits, and
473  * thus significantly alter the data. This is a known issue, and the fix
474  * involves switching RGB data to be stored as a packed integer in
475  * future versions of PCL.
476  */
477  inline int
478  write (const std::string &file_name, const pcl::PCLPointCloud2::ConstPtr &cloud,
479  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
480  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
481  const bool binary = false)
482  {
483  return (write (file_name, *cloud, origin, orientation, binary));
484  }
485 
486  /** \brief Save point cloud data to a PCD file containing n-D points, in BINARY format
487  * \param[in] file_name the output file name
488  * \param[in] cloud the point cloud data message
489  */
490  template <typename PointT> int
491  writeBinary (const std::string &file_name,
492  const pcl::PointCloud<PointT> &cloud);
493 
494  /** \brief Save point cloud data to a binary comprssed PCD file
495  * \param[in] file_name the output file name
496  * \param[in] cloud the point cloud data message
497  * \return
498  * (-1) for a general error
499  * (-2) if the input cloud is too large for the file format
500  * 0 on success
501  */
502  template <typename PointT> int
503  writeBinaryCompressed (const std::string &file_name,
504  const pcl::PointCloud<PointT> &cloud);
505 
506  /** \brief Save point cloud data to a PCD file containing n-D points, in BINARY format
507  * \param[in] file_name the output file name
508  * \param[in] cloud the point cloud data message
509  * \param[in] indices the set of point indices that we want written to disk
510  */
511  template <typename PointT> int
512  writeBinary (const std::string &file_name,
513  const pcl::PointCloud<PointT> &cloud,
514  const std::vector<int> &indices);
515 
516  /** \brief Save point cloud data to a PCD file containing n-D points, in ASCII format
517  * \param[in] file_name the output file name
518  * \param[in] cloud the point cloud data message
519  * \param[in] precision the specified output numeric stream precision (default: 8)
520  */
521  template <typename PointT> int
522  writeASCII (const std::string &file_name,
523  const pcl::PointCloud<PointT> &cloud,
524  const int precision = 8);
525 
526  /** \brief Save point cloud data to a PCD file containing n-D points, in ASCII format
527  * \param[in] file_name the output file name
528  * \param[in] cloud the point cloud data message
529  * \param[in] indices the set of point indices that we want written to disk
530  * \param[in] precision the specified output numeric stream precision (default: 8)
531  */
532  template <typename PointT> int
533  writeASCII (const std::string &file_name,
534  const pcl::PointCloud<PointT> &cloud,
535  const std::vector<int> &indices,
536  const int precision = 8);
537 
538  /** \brief Save point cloud data to a PCD file containing n-D points
539  * \param[in] file_name the output file name
540  * \param[in] cloud the pcl::PointCloud data
541  * \param[in] binary set to true if the file is to be written in a binary
542  * PCD format, false (default) for ASCII
543  *
544  * Caution: PointCloud structures containing an RGB field have
545  * traditionally used packed float values to store RGB data. Storing a
546  * float as ASCII can introduce variations to the smallest bits, and
547  * thus significantly alter the data. This is a known issue, and the fix
548  * involves switching RGB data to be stored as a packed integer in
549  * future versions of PCL.
550  */
551  template<typename PointT> inline int
552  write (const std::string &file_name,
553  const pcl::PointCloud<PointT> &cloud,
554  const bool binary = false)
555  {
556  if (binary)
557  return (writeBinary<PointT> (file_name, cloud));
558  return (writeASCII<PointT> (file_name, cloud));
559  }
560 
561  /** \brief Save point cloud data to a PCD file containing n-D points
562  * \param[in] file_name the output file name
563  * \param[in] cloud the pcl::PointCloud data
564  * \param[in] indices the set of point indices that we want written to disk
565  * \param[in] binary set to true if the file is to be written in a binary
566  * PCD format, false (default) for ASCII
567  *
568  * Caution: PointCloud structures containing an RGB field have
569  * traditionally used packed float values to store RGB data. Storing a
570  * float as ASCII can introduce variations to the smallest bits, and
571  * thus significantly alter the data. This is a known issue, and the fix
572  * involves switching RGB data to be stored as a packed integer in
573  * future versions of PCL.
574  */
575  template<typename PointT> inline int
576  write (const std::string &file_name,
577  const pcl::PointCloud<PointT> &cloud,
578  const std::vector<int> &indices,
579  bool binary = false)
580  {
581  if (binary)
582  return (writeBinary<PointT> (file_name, cloud, indices));
583  return (writeASCII<PointT> (file_name, cloud, indices));
584  }
585 
586  protected:
587  /** \brief Set permissions for file locking (Boost 1.49+).
588  * \param[in] file_name the file name to set permission for file locking
589  * \param[in,out] lock the file lock
590  */
591  void
592  setLockingPermissions (const std::string &file_name,
593  boost::interprocess::file_lock &lock);
594 
595  /** \brief Reset permissions for file locking (Boost 1.49+).
596  * \param[in] file_name the file name to reset permission for file locking
597  * \param[in,out] lock the file lock
598  */
599  void
600  resetLockingPermissions (const std::string &file_name,
601  boost::interprocess::file_lock &lock);
602 
603  private:
604  /** \brief Set to true if msync() should be called before munmap(). Prevents data loss on NFS systems. */
605  bool map_synchronization_;
606  };
607 
608  namespace io
609  {
610  /** \brief Load a PCD v.6 file into a templated PointCloud type.
611  *
612  * Any PCD files > v.6 will generate a warning as a
613  * pcl/PCLPointCloud2 message cannot hold the sensor origin.
614  *
615  * \param[in] file_name the name of the file to load
616  * \param[out] cloud the resultant templated point cloud
617  * \ingroup io
618  */
619  inline int
620  loadPCDFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud)
621  {
622  pcl::PCDReader p;
623  return (p.read (file_name, cloud));
624  }
625 
626  /** \brief Load any PCD file into a templated PointCloud type.
627  * \param[in] file_name the name of the file to load
628  * \param[out] cloud the resultant templated point cloud
629  * \param[out] origin the sensor acquisition origin (only for > PCD_V7 - null if not present)
630  * \param[out] orientation the sensor acquisition orientation (only for >
631  * PCD_V7 - identity if not present)
632  * \ingroup io
633  */
634  inline int
635  loadPCDFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
636  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation)
637  {
638  pcl::PCDReader p;
639  int pcd_version;
640  return (p.read (file_name, cloud, origin, orientation, pcd_version));
641  }
642 
643  /** \brief Load any PCD file into a templated PointCloud type
644  * \param[in] file_name the name of the file to load
645  * \param[out] cloud the resultant templated point cloud
646  * \ingroup io
647  */
648  template<typename PointT> inline int
649  loadPCDFile (const std::string &file_name, pcl::PointCloud<PointT> &cloud)
650  {
651  pcl::PCDReader p;
652  return (p.read (file_name, cloud));
653  }
654 
655  /** \brief Save point cloud data to a PCD file containing n-D points
656  * \param[in] file_name the output file name
657  * \param[in] cloud the point cloud data message
658  * \param[in] origin the sensor acquisition origin
659  * \param[in] orientation the sensor acquisition orientation
660  * \param[in] binary_mode true for binary mode, false (default) for ASCII
661  *
662  * Caution: PointCloud structures containing an RGB field have
663  * traditionally used packed float values to store RGB data. Storing a
664  * float as ASCII can introduce variations to the smallest bits, and
665  * thus significantly alter the data. This is a known issue, and the fix
666  * involves switching RGB data to be stored as a packed integer in
667  * future versions of PCL.
668  * \ingroup io
669  */
670  inline int
671  savePCDFile (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
672  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
673  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
674  const bool binary_mode = false)
675  {
676  PCDWriter w;
677  return (w.write (file_name, cloud, origin, orientation, binary_mode));
678  }
679 
680  /** \brief Templated version for saving point cloud data to a PCD file
681  * containing a specific given cloud format
682  * \param[in] file_name the output file name
683  * \param[in] cloud the point cloud data message
684  * \param[in] binary_mode true for binary mode, false (default) for ASCII
685  *
686  * Caution: PointCloud structures containing an RGB field have
687  * traditionally used packed float values to store RGB data. Storing a
688  * float as ASCII can introduce variations to the smallest bits, and
689  * thus significantly alter the data. This is a known issue, and the fix
690  * involves switching RGB data to be stored as a packed integer in
691  * future versions of PCL.
692  * \ingroup io
693  */
694  template<typename PointT> inline int
695  savePCDFile (const std::string &file_name, const pcl::PointCloud<PointT> &cloud, bool binary_mode = false)
696  {
697  PCDWriter w;
698  return (w.write<PointT> (file_name, cloud, binary_mode));
699  }
700 
701  /**
702  * \brief Templated version for saving point cloud data to a PCD file
703  * containing a specific given cloud format.
704  *
705  * This version is to retain backwards compatibility.
706  * \param[in] file_name the output file name
707  * \param[in] cloud the point cloud data message
708  *
709  * Caution: PointCloud structures containing an RGB field have
710  * traditionally used packed float values to store RGB data. Storing a
711  * float as ASCII can introduce variations to the smallest bits, and
712  * thus significantly alter the data. This is a known issue, and the fix
713  * involves switching RGB data to be stored as a packed integer in
714  * future versions of PCL.
715  * \ingroup io
716  */
717  template<typename PointT> inline int
718  savePCDFileASCII (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
719  {
720  PCDWriter w;
721  return (w.write<PointT> (file_name, cloud, false));
722  }
723 
724  /**
725  * \brief Templated version for saving point cloud data to a PCD file
726  * containing a specific given cloud format. The resulting file will be an uncompressed binary.
727  *
728  * This version is to retain backwards compatibility.
729  * \param[in] file_name the output file name
730  * \param[in] cloud the point cloud data message
731  * \ingroup io
732  */
733  template<typename PointT> inline int
734  savePCDFileBinary (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
735  {
736  PCDWriter w;
737  return (w.write<PointT> (file_name, cloud, true));
738  }
739 
740  /**
741  * \brief Templated version for saving point cloud data to a PCD file
742  * containing a specific given cloud format
743  *
744  * \param[in] file_name the output file name
745  * \param[in] cloud the point cloud data message
746  * \param[in] indices the set of indices to save
747  * \param[in] binary_mode true for binary mode, false (default) for ASCII
748  *
749  * Caution: PointCloud structures containing an RGB field have
750  * traditionally used packed float values to store RGB data. Storing a
751  * float as ASCII can introduce variations to the smallest bits, and
752  * thus significantly alter the data. This is a known issue, and the fix
753  * involves switching RGB data to be stored as a packed integer in
754  * future versions of PCL.
755  * \ingroup io
756  */
757  template<typename PointT> int
758  savePCDFile (const std::string &file_name,
759  const pcl::PointCloud<PointT> &cloud,
760  const std::vector<int> &indices,
761  const bool binary_mode = false)
762  {
763  // Save the data
764  PCDWriter w;
765  return (w.write<PointT> (file_name, cloud, indices, binary_mode));
766  }
767 
768 
769  /**
770  * \brief Templated version for saving point cloud data to a PCD file
771  * containing a specific given cloud format. This method will write a compressed binary file.
772  *
773  * This version is to retain backwards compatibility.
774  * \param[in] file_name the output file name
775  * \param[in] cloud the point cloud data message
776  * \ingroup io
777  */
778  template<typename PointT> inline int
779  savePCDFileBinaryCompressed (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
780  {
781  PCDWriter w;
782  return (w.writeBinaryCompressed<PointT> (file_name, cloud));
783  }
784 
785  }
786 }
787 
788 #include <pcl/io/impl/pcd_io.hpp>
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
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(), const bool binary=false)
Save point cloud data to a PCD file containing n-D points.
Definition: pcd_io.h:478
int writeBinaryCompressed(const std::string &file_name, const pcl::PCLPointCloud2 &cloud, const Eigen::Vector4f &origin=Eigen::Vector4f::Zero(), const Eigen::Quaternionf &orientation=Eigen::Quaternionf::Identity())
Save point cloud data to a PCD file containing n-D points, in BINARY_COMPRESSED format.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
~PCDReader()
Empty destructor.
Definition: pcd_io.h:58
int savePCDFileASCII(const std::string &file_name, const pcl::PointCloud< PointT > &cloud)
Templated version for saving point cloud data to a PCD file containing a specific given cloud format...
Definition: pcd_io.h:718
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
void setMapSynchronization(bool sync)
Set whether mmap() synchornization via msync() is desired before munmap() calls.
Definition: pcd_io.h:310
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: pcl_macros.h:344
int loadPCDFile(const std::string &file_name, pcl::PointCloud< PointT > &cloud)
Load any PCD file into a templated PointCloud type.
Definition: pcd_io.h:649
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:422
int savePCDFile(const std::string &file_name, const pcl::PointCloud< PointT > &cloud, const std::vector< int > &indices, const bool binary_mode=false)
Templated version for saving point cloud data to a PCD file containing a specific given cloud format...
Definition: pcd_io.h:758
Point Cloud Data (FILE) file format reader interface.
Definition: file_io.h:55
int savePCDFileBinary(const std::string &file_name, const pcl::PointCloud< PointT > &cloud)
Templated version for saving point cloud data to a PCD file containing a specific given cloud format...
Definition: pcd_io.h:734
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:424
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 PCD file containing n-D points.
Definition: pcd_io.h:452
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version, const int offset=0) override
Read a point cloud data from a PCD file and store it into a pcl/PCLPointCloud2.
PCDReader()
Empty constructor.
Definition: pcd_io.h:56
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, const std::vector< int > &indices, bool binary=false)
Save point cloud data to a PCD file containing n-D points.
Definition: pcd_io.h:576
int write(const std::string &file_name, const pcl::PointCloud< PointT > &cloud, const bool binary=false)
Save point cloud data to a PCD file containing n-D points.
Definition: pcd_io.h:552
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63
int read(const std::string &file_name, pcl::PointCloud< PointT > &cloud, const int offset=0)
Read a point cloud data from any PCD file, and convert it to the given template format.
Definition: pcd_io.h:275
Point Cloud Data (PCD) file format reader.
Definition: pcd_io.h:52
A point structure representing Euclidean xyz coordinates, and the RGB color.
boost::shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Point Cloud Data (PCD) file format writer.
Definition: pcd_io.h:295
int savePCDFileBinaryCompressed(const std::string &file_name, const pcl::PointCloud< PointT > &cloud)
Templated version for saving point cloud data to a PCD file containing a specific given cloud format...
Definition: pcd_io.h:779