Point Cloud Library (PCL)  1.11.0-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/memory.h>
43 #include <pcl/pcl_macros.h>
44 #include <pcl/point_cloud.h>
45 #include <pcl/io/file_io.h>
46 
47 namespace pcl
48 {
49  /** \brief Point Cloud Data (PCD) file format reader.
50  * \author Radu B. Rusu
51  * \ingroup io
52  */
54  {
55  public:
56  /** Empty constructor */
57  PCDReader () {}
58  /** Empty destructor */
59  ~PCDReader () {}
60 
61  /** \brief Various PCD file versions.
62  *
63  * PCD_V6 represents PCD files with version 0.6, which contain the following fields:
64  * - lines beginning with # are treated as comments
65  * - FIELDS ...
66  * - SIZE ...
67  * - TYPE ...
68  * - COUNT ...
69  * - WIDTH ...
70  * - HEIGHT ...
71  * - POINTS ...
72  * - DATA ascii/binary
73  *
74  * Everything that follows \b DATA is interpreted as data points and
75  * will be read accordingly.
76  *
77  * PCD_V7 represents PCD files with version 0.7 and has an important
78  * addon: it adds sensor origin/orientation (aka viewpoint) information
79  * to a dataset through the use of a new header field:
80  * - VIEWPOINT tx ty tz qw qx qy qz
81  */
82  enum
83  {
84  PCD_V6 = 0,
85  PCD_V7 = 1
86  };
87 
88  /** \brief Read a point cloud data header from a PCD-formatted, binary istream.
89  *
90  * Load only the meta information (number of points, their types, etc),
91  * and not the points themselves, from a given PCD stream. Useful for fast
92  * evaluation of the underlying data structure.
93  *
94  * \attention The PCD data is \b always stored in ROW major format! The
95  * read/write PCD methods will detect column major input and automatically convert it.
96  *
97  * \param[in] binary_istream a std::istream with openmode set to std::ios::binary.
98  * \param[out] cloud the resultant point cloud dataset (only these
99  * members will be filled: width, height, point_step,
100  * row_step, fields[]; data is resized but not written)
101  * \param[out] origin the sensor acquisition origin (only for > PCD_V7 - null if not present)
102  * \param[out] orientation the sensor acquisition orientation (only for > PCD_V7 - identity if not present)
103  * \param[out] pcd_version the PCD version of the file (i.e., PCD_V6, PCD_V7)
104  * \param[out] data_type the type of data (0 = ASCII, 1 = Binary, 2 = Binary compressed)
105  * \param[out] data_idx the offset of cloud data within the file
106  *
107  * \return
108  * * < 0 (-1) on error
109  * * == 0 on success
110  */
111  int
112  readHeader (std::istream &binary_istream, pcl::PCLPointCloud2 &cloud,
113  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version,
114  int &data_type, unsigned int &data_idx);
115 
116  /** \brief Read a point cloud data header from a PCD file.
117  *
118  * Load only the meta information (number of points, their types, etc),
119  * and not the points themselves, from a given PCD file. Useful for fast
120  * evaluation of the underlying data structure.
121  *
122  * \attention The PCD data is \b always stored in ROW major format! The
123  * read/write PCD methods will detect column major input and automatically convert it.
124  *
125  * \param[in] file_name the name of the file to load
126  * \param[out] cloud the resultant point cloud dataset (only these
127  * members will be filled: width, height, point_step,
128  * row_step, fields[]; data is resized but not written)
129  * \param[out] origin the sensor acquisition origin (only for > PCD_V7 - null if not present)
130  * \param[out] orientation the sensor acquisition orientation (only for > PCD_V7 - identity if not present)
131  * \param[out] pcd_version the PCD version of the file (i.e., PCD_V6, PCD_V7)
132  * \param[out] data_type the type of data (0 = ASCII, 1 = Binary, 2 = Binary compressed)
133  * \param[out] data_idx the offset of cloud data within the file
134  * \param[in] offset the offset of where to expect the PCD Header in the
135  * file (optional parameter). One usage example for setting the offset
136  * parameter is for reading data from a TAR "archive containing multiple
137  * PCD files: TAR files always add a 512 byte header in front of the
138  * actual file, so set the offset to the next byte after the header
139  * (e.g., 513).
140  *
141  * \return
142  * * < 0 (-1) on error
143  * * == 0 on success
144  */
145  int
146  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
147  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version,
148  int &data_type, unsigned int &data_idx, const int offset = 0) override;
149 
150 
151  /** \brief Read a point cloud data header from a PCD file.
152  *
153  * Load only the meta information (number of points, their types, etc),
154  * and not the points themselves, from a given PCD file. Useful for fast
155  * evaluation of the underlying data structure.
156  *
157  * \attention The PCD data is \b always stored in ROW major format! The
158  * read/write PCD methods will detect column major input and automatically convert it.
159  *
160  * \param[in] file_name the name of the file to load
161  * \param[out] cloud the resultant point cloud dataset (only these
162  * members will be filled: width, height, point_step,
163  * row_step, fields[]; data is resized but not written)
164  * \param[in] offset the offset of where to expect the PCD Header in the
165  * file (optional parameter). One usage example for setting the offset
166  * parameter is for reading data from a TAR "archive containing multiple
167  * PCD files: TAR files always add a 512 byte header in front of the
168  * actual file, so set the offset to the next byte after the header
169  * (e.g., 513).
170  *
171  * \return
172  * * < 0 (-1) on error
173  * * == 0 on success
174  */
175  int
176  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0);
177 
178  /** \brief Read the point cloud data (body) from a PCD stream.
179  *
180  * Reads the cloud points from a text-formatted stream. For use after
181  * readHeader(), when the resulting data_type == 0.
182  *
183  * \attention This assumes the stream has been seeked to the position
184  * indicated by the data_idx result of readHeader().
185  *
186  * \param[in] stream the stream from which to read the body.
187  * \param[out] cloud the resultant point cloud dataset to be filled.
188  * \param[in] pcd_version the PCD version of the stream (from readHeader()).
189  *
190  * \return
191  * * < 0 (-1) on error
192  * * == 0 on success
193  */
194  int
195  readBodyASCII (std::istream &stream, pcl::PCLPointCloud2 &cloud, int pcd_version);
196 
197  /** \brief Read the point cloud data (body) from a block of memory.
198  *
199  * Reads the cloud points from a binary-formatted memory block. For use
200  * after readHeader(), when the resulting data_type is nonzero.
201  *
202  * \param[in] data the memory location from which to read the body.
203  * \param[out] cloud the resultant point cloud dataset to be filled.
204  * \param[in] pcd_version the PCD version of the stream (from readHeader()).
205  * \param[in] compressed indicates whether the PCD block contains compressed
206  * data. This should be true if the data_type returne by readHeader() == 2.
207  * \param[in] data_idx the offset of the body, as reported by readHeader().
208  *
209  * \return
210  * * < 0 (-1) on error
211  * * == 0 on success
212  */
213  int
214  readBodyBinary (const unsigned char *data, pcl::PCLPointCloud2 &cloud,
215  int pcd_version, bool compressed, unsigned int data_idx);
216 
217  /** \brief Read a point cloud data from a PCD file and store it into a pcl/PCLPointCloud2.
218  * \param[in] file_name the name of the file containing the actual PointCloud data
219  * \param[out] cloud the resultant PointCloud message read from disk
220  * \param[out] origin the sensor acquisition origin (only for > PCD_V7 - null if not present)
221  * \param[out] orientation the sensor acquisition orientation (only for > PCD_V7 - identity if not present)
222  * \param[out] pcd_version the PCD version of the file (either PCD_V6 or PCD_V7)
223  * \param[in] offset the offset of where to expect the PCD Header in the
224  * file (optional parameter). One usage example for setting the offset
225  * parameter is for reading data from a TAR "archive containing multiple
226  * PCD files: TAR files always add a 512 byte header in front of the
227  * actual file, so set the offset to the next byte after the header
228  * (e.g., 513).
229  *
230  * \return
231  * * < 0 (-1) on error
232  * * == 0 on success
233  */
234  int
235  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
236  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version, const int offset = 0) override;
237 
238  /** \brief Read a point cloud data from a PCD (PCD_V6) and store it into a pcl/PCLPointCloud2.
239  *
240  * \note This function is provided for backwards compatibility only and
241  * it can only read PCD_V6 files correctly, as pcl::PCLPointCloud2
242  * does not contain a sensor origin/orientation. Reading any file
243  * > PCD_V6 will generate a warning.
244  *
245  * \param[in] file_name the name of the file containing the actual PointCloud data
246  * \param[out] cloud the resultant PointCloud message read from disk
247  * \param[in] offset the offset of where to expect the PCD Header in the
248  * file (optional parameter). One usage example for setting the offset
249  * parameter is for reading data from a TAR "archive containing multiple
250  * PCD files: TAR files always add a 512 byte header in front of the
251  * actual file, so set the offset to the next byte after the header
252  * (e.g., 513).
253  *
254  * \return
255  * * < 0 (-1) on error
256  * * == 0 on success
257  */
258  int
259  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0);
260 
261  /** \brief Read a point cloud data from any PCD file, and convert it to the given template format.
262  * \param[in] file_name the name of the file containing the actual PointCloud data
263  * \param[out] cloud the resultant PointCloud message read from disk
264  * \param[in] offset the offset of where to expect the PCD Header in the
265  * file (optional parameter). One usage example for setting the offset
266  * parameter is for reading data from a TAR "archive containing multiple
267  * PCD files: TAR files always add a 512 byte header in front of the
268  * actual file, so set the offset to the next byte after the header
269  * (e.g., 513).
270  *
271  * \return
272  * * < 0 (-1) on error
273  * * == 0 on success
274  */
275  template<typename PointT> int
276  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud, const int offset = 0)
277  {
278  pcl::PCLPointCloud2 blob;
279  int pcd_version;
280  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
281  pcd_version, offset);
282 
283  // If no error, convert the data
284  if (res == 0)
285  pcl::fromPCLPointCloud2 (blob, cloud);
286  return (res);
287  }
288 
290  };
291 
292  /** \brief Point Cloud Data (PCD) file format writer.
293  * \author Radu Bogdan Rusu
294  * \ingroup io
295  */
297  {
298  public:
299  PCDWriter() : map_synchronization_(false) {}
301 
302  /** \brief Set whether mmap() synchornization via msync() is desired before munmap() calls.
303  * Setting this to true could prevent NFS data loss (see
304  * http://www.pcl-developers.org/PCD-IO-consistency-on-NFS-msync-needed-td4885942.html).
305  * Default: false
306  * \note This option should be used by advanced users only!
307  * \note Please note that using msync() on certain systems can reduce the I/O performance by up to 80%!
308  * \param[in] sync set to true if msync() should be called before munmap()
309  */
310  void
312  {
313  map_synchronization_ = sync;
314  }
315 
316  /** \brief Generate the header of a PCD file format
317  * \param[in] cloud the point cloud data message
318  * \param[in] origin the sensor acquisition origin
319  * \param[in] orientation the sensor acquisition orientation
320  */
321  std::string
322  generateHeaderBinary (const pcl::PCLPointCloud2 &cloud,
323  const Eigen::Vector4f &origin,
324  const Eigen::Quaternionf &orientation);
325 
326  /** \brief Generate the header of a BINARY_COMPRESSED PCD file format
327  * \param[out] os the stream into which to write the header
328  * \param[in] cloud the point cloud data message
329  * \param[in] origin the sensor acquisition origin
330  * \param[in] orientation the sensor acquisition orientation
331  *
332  * \return
333  * * < 0 (-1) on error
334  * * == 0 on success
335  */
336  int
337  generateHeaderBinaryCompressed (std::ostream &os,
338  const pcl::PCLPointCloud2 &cloud,
339  const Eigen::Vector4f &origin,
340  const Eigen::Quaternionf &orientation);
341 
342  /** \brief Generate the header of a BINARY_COMPRESSED PCD file format
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>
pcl_macros.h
Defines all the PCL and non-PCL macros used.
pcl
Definition: convolution.h:46
pcl::PCDWriter::PCDWriter
PCDWriter()
Definition: pcd_io.h:299
pcl::PCDWriter::write
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
pcl::PCDWriter::write
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
pcl::PCDWriter::writeBinaryCompressed
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.
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:53
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::io::savePCDFileASCII
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
pcl::FileWriter
Point Cloud Data (FILE) file format writer.
Definition: file_io.h:161
pcl::io::savePCDFile
int savePCDFile(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_mode=false)
Save point cloud data to a PCD file containing n-D points.
Definition: pcd_io.h:671
pcl::read
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
pcl::PCDReader::~PCDReader
~PCDReader()
Empty destructor.
Definition: pcd_io.h:59
pcl::PCDWriter::setMapSynchronization
void setMapSynchronization(bool sync)
Set whether mmap() synchornization via msync() is desired before munmap() calls.
Definition: pcd_io.h:311
pcl::PCDReader::read
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:276
pcl::PCLPointCloud2::ConstPtr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Definition: PCLPointCloud2.h:35
pcl::PCDWriter::write
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
pcl::PCDReader::PCDReader
PCDReader()
Empty constructor.
Definition: pcd_io.h:57
PCL_MAKE_ALIGNED_OPERATOR_NEW
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
pcl::PCDWriter::write
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
pcl::FileReader
Point Cloud Data (FILE) file format reader interface.
Definition: file_io.h:55
pcl::PointCloud::sensor_origin_
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:422
pcl::PointCloud::sensor_orientation_
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:424
pcl::io::savePCDFileBinary
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
pcl::PCDWriter::~PCDWriter
~PCDWriter()
Definition: pcd_io.h:300
pcl::PCLPointCloud2
Definition: PCLPointCloud2.h:15
pcl::write
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63
pcl::io::loadPCDFile
int loadPCDFile(const std::string &file_name, pcl::PCLPointCloud2 &cloud)
Load a PCD v.6 file into a templated PointCloud type.
Definition: pcd_io.h:620
pcl::PCDReader
Point Cloud Data (PCD) file format reader.
Definition: pcd_io.h:53
pcl::io::savePCDFileBinaryCompressed
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
pcl::PCDReader::read
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.
memory.h
Defines functions, macros and traits for allocating and using memory.
pcl::PCDWriter
Point Cloud Data (PCD) file format writer.
Definition: pcd_io.h:296
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:331
pcl::fromPCLPointCloud2
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