Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pcd_io.h
Go to the documentation of this file.
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 Willow Garage, Inc. 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: pcd_io.h 6122 2012-07-03 18:59:43Z aichim $
37  *
38  */
39 
40 #ifndef PCL_IO_PCD_IO_H_
41 #define PCL_IO_PCD_IO_H_
42 
43 #include <pcl/point_cloud.h>
44 #include <pcl/io/file_io.h>
45 
46 namespace pcl
47 {
53  {
54  public:
56  PCDReader () : FileReader () {}
58  ~PCDReader () {}
80  enum
81  {
82  PCD_V6 = 0,
83  PCD_V7 = 1
84  };
85 
113  int
114  readHeader (const std::string &file_name, sensor_msgs::PointCloud2 &cloud,
115  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version,
116  int &data_type, unsigned int &data_idx, const int offset = 0);
117 
118 
141  int
142  readHeader (const std::string &file_name, sensor_msgs::PointCloud2 &cloud, const int offset = 0);
143 
170  int
171  readHeaderEigen (const std::string &file_name, pcl::PointCloud<Eigen::MatrixXf> &cloud,
172  int &pcd_version, int &data_type, unsigned int &data_idx, const int offset = 0);
173 
191  int
192  read (const std::string &file_name, sensor_msgs::PointCloud2 &cloud,
193  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &pcd_version, const int offset = 0);
194 
215  int
216  read (const std::string &file_name, sensor_msgs::PointCloud2 &cloud, const int offset = 0);
217 
232  template<typename PointT> int
233  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud, const int offset = 0)
234  {
236  int pcd_version;
237  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
238  pcd_version, offset);
239 
240  // If no error, convert the data
241  if (res == 0)
242  pcl::fromROSMsg (blob, cloud);
243  return (res);
244  }
245 
263  int
264  readEigen (const std::string &file_name, pcl::PointCloud<Eigen::MatrixXf> &cloud, const int offset = 0);
265  };
266 
272  {
273  public:
274  PCDWriter() : FileWriter(), map_synchronization_(false) {}
276 
285  void
286  setMapSynchronization (bool sync)
287  {
288  map_synchronization_ = sync;
289  }
290 
296  std::string
297  generateHeaderBinary (const sensor_msgs::PointCloud2 &cloud,
298  const Eigen::Vector4f &origin,
299  const Eigen::Quaternionf &orientation);
300 
306  std::string
307  generateHeaderBinaryCompressed (const sensor_msgs::PointCloud2 &cloud,
308  const Eigen::Vector4f &origin,
309  const Eigen::Quaternionf &orientation);
310 
316  std::string
317  generateHeaderASCII (const sensor_msgs::PointCloud2 &cloud,
318  const Eigen::Vector4f &origin,
319  const Eigen::Quaternionf &orientation);
320 
326  template <typename PointT> static std::string
327  generateHeader (const pcl::PointCloud<PointT> &cloud,
328  const int nr_points = std::numeric_limits<int>::max ());
329 
339  std::string
340  generateHeaderEigen (const pcl::PointCloud<Eigen::MatrixXf> &cloud,
341  const int nr_points = std::numeric_limits<int>::max ());
342 
359  int
360  writeASCII (const std::string &file_name, const sensor_msgs::PointCloud2 &cloud,
361  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
362  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
363  const int precision = 8);
364 
371  int
372  writeBinary (const std::string &file_name, const sensor_msgs::PointCloud2 &cloud,
373  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
374  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity ());
375 
382  int
383  writeBinaryCompressed (const std::string &file_name, const sensor_msgs::PointCloud2 &cloud,
384  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
385  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity ());
386 
404  inline int
405  write (const std::string &file_name, const sensor_msgs::PointCloud2 &cloud,
406  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
407  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
408  const bool binary = false)
409  {
410  if (binary)
411  return (writeBinary (file_name, cloud, origin, orientation));
412  else
413  return (writeASCII (file_name, cloud, origin, orientation, 8));
414  }
415 
431  inline int
432  write (const std::string &file_name, const sensor_msgs::PointCloud2::ConstPtr &cloud,
433  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
434  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
435  const bool binary = false)
436  {
437  return (write (file_name, *cloud, origin, orientation, binary));
438  }
439 
444  template <typename PointT> int
445  writeBinary (const std::string &file_name,
446  const pcl::PointCloud<PointT> &cloud);
447 
456  int
457  writeBinaryEigen (const std::string &file_name,
458  const pcl::PointCloud<Eigen::MatrixXf> &cloud);
459 
464  template <typename PointT> int
465  writeBinaryCompressed (const std::string &file_name,
466  const pcl::PointCloud<PointT> &cloud);
467 
476  int
477  writeBinaryCompressedEigen (const std::string &file_name,
478  const pcl::PointCloud<Eigen::MatrixXf> &cloud);
479 
485  template <typename PointT> int
486  writeBinary (const std::string &file_name,
487  const pcl::PointCloud<PointT> &cloud,
488  const std::vector<int> &indices);
489 
495  template <typename PointT> int
496  writeASCII (const std::string &file_name,
497  const pcl::PointCloud<PointT> &cloud,
498  const int precision = 8);
499 
509  int
510  writeASCIIEigen (const std::string &file_name,
512  const int precision = 8);
513 
520  template <typename PointT> int
521  writeASCII (const std::string &file_name,
522  const pcl::PointCloud<PointT> &cloud,
523  const std::vector<int> &indices,
524  const int precision = 8);
525 
539  template<typename PointT> inline int
540  write (const std::string &file_name,
541  const pcl::PointCloud<PointT> &cloud,
542  const bool binary = false)
543  {
544  if (binary)
545  return (writeBinary<PointT> (file_name, cloud));
546  else
547  return (writeASCII<PointT> (file_name, cloud));
548  }
549 
564  template<typename PointT> inline int
565  write (const std::string &file_name,
566  const pcl::PointCloud<PointT> &cloud,
567  const std::vector<int> &indices,
568  bool binary = false)
569  {
570  if (binary)
571  return (writeBinary<PointT> (file_name, cloud, indices));
572  else
573  return (writeASCII<PointT> (file_name, cloud, indices));
574  }
575 
576  private:
578  bool map_synchronization_;
579 
580  typedef std::pair<std::string, pcl::ChannelProperties> pair_channel_properties;
584  struct ChannelPropertiesComparator
585  {
586  bool
587  operator()(const pair_channel_properties &lhs, const pair_channel_properties &rhs)
588  {
589  return (lhs.second.offset < rhs.second.offset);
590  }
591  };
592  };
593 
594  namespace io
595  {
605  inline int
606  loadPCDFile (const std::string &file_name, sensor_msgs::PointCloud2 &cloud)
607  {
608  pcl::PCDReader p;
609  return (p.read (file_name, cloud));
610  }
611 
620  inline int
621  loadPCDFile (const std::string &file_name, sensor_msgs::PointCloud2 &cloud,
622  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation)
623  {
624  pcl::PCDReader p;
625  int pcd_version;
626  return (p.read (file_name, cloud, origin, orientation, pcd_version));
627  }
628 
634  template<typename PointT> inline int
635  loadPCDFile (const std::string &file_name, pcl::PointCloud<PointT> &cloud)
636  {
637  pcl::PCDReader p;
638  return (p.read (file_name, cloud));
639  }
640 
656  inline int
657  savePCDFile (const std::string &file_name, const sensor_msgs::PointCloud2 &cloud,
658  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
659  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
660  const bool binary_mode = false)
661  {
662  PCDWriter w;
663  return (w.write (file_name, cloud, origin, orientation, binary_mode));
664  }
665 
680  template<typename PointT> inline int
681  savePCDFile (const std::string &file_name, const pcl::PointCloud<PointT> &cloud, bool binary_mode = false)
682  {
683  PCDWriter w;
684  return (w.write<PointT> (file_name, cloud, binary_mode));
685  }
686 
703  template<typename PointT> inline int
704  savePCDFileASCII (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
705  {
706  PCDWriter w;
707  return (w.write<PointT> (file_name, cloud, false));
708  }
709 
719  template<typename PointT> inline int
720  savePCDFileBinary (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
721  {
722  PCDWriter w;
723  return (w.write<PointT> (file_name, cloud, true));
724  }
725 
743  template<typename PointT> int
744  savePCDFile (const std::string &file_name,
745  const pcl::PointCloud<PointT> &cloud,
746  const std::vector<int> &indices,
747  const bool binary_mode = false)
748  {
749  // Save the data
750  PCDWriter w;
751  return (w.write<PointT> (file_name, cloud, indices, binary_mode));
752  }
753  }
754 }
755 
756 #include <pcl/io/impl/pcd_io.hpp>
757 
758 #endif //#ifndef PCL_IO_PCD_IO_H_