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