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