Point Cloud Library (PCL)  1.9.1-dev
ifs_io.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2013, Open Perception, 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  */
37 
38 #pragma once
39 
40 #include <pcl/point_cloud.h>
41 #include <pcl/PCLPointCloud2.h>
42 #include <pcl/conversions.h>
43 #include <pcl/io/boost.h>
44 #include <pcl/PolygonMesh.h>
45 
46 namespace pcl
47 {
48  /** \brief Indexed Face set (IFS) file format reader. This file format is used for
49  * the Brown Mesh Set for instance.
50  * \author Nizar Sallem
51  * \ingroup io
52  */
53  class PCL_EXPORTS IFSReader
54  {
55  public:
56  /** Empty constructor */
57  IFSReader () {}
58  /** Empty destructor */
59  ~IFSReader () {}
60 
61  /** \brief we support two versions
62  * 1.0 classic
63  * 1.1 with texture coordinates addon
64  */
65  enum
66  {
67  IFS_V1_0 = 0,
68  IFS_V1_1 = 1
69  };
70 
71  /** \brief Read a point cloud data header from an IFS file.
72  *
73  * Load only the meta information (number of points, their types, etc),
74  * and not the points themselves, from a given IFS file. Useful for fast
75  * evaluation of the underlying data structure.
76  *
77  * \param[in] file_name the name of the file to load
78  * \param[out] cloud the resultant point cloud dataset (only header will be filled)
79  * \param[out] ifs_version the IFS version of the file (IFS_V1_0 or IFS_V1_1)
80  * \param[out] data_idx the offset of cloud data within the file
81  *
82  * \return
83  * * < 0 (-1) on error
84  * * == 0 on success
85  */
86  int
87  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
88  int &ifs_version, unsigned int &data_idx);
89 
90  /** \brief Read a point cloud data from an IFS file and store it into a pcl/PCLPointCloud2.
91  * \param[in] file_name the name of the file containing the actual PointCloud data
92  * \param[out] cloud the resultant PCLPointCloud2 blob read from disk
93  * \param[out] ifs_version the IFS version of the file (either IFS_V1_0 or IFS_V1_1)
94  *
95  * \return
96  * * < 0 (-1) on error
97  * * == 0 on success
98  */
99  int
100  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud, int &ifs_version);
101 
102  /** \brief Read a point cloud data from an IFS file and store it into a PolygonMesh.
103  * \param[in] file_name the name of the file containing the mesh data
104  * \param[out] mesh the resultant PolygonMesh
105  * \param[out] ifs_version the IFS version of the file (either IFS_V1_0 or IFS_V1_1)
106  *
107  * \return
108  * * < 0 (-1) on error
109  * * == 0 on success
110  */
111  int
112  read (const std::string &file_name, pcl::PolygonMesh &mesh, int &ifs_version);
113 
114  /** \brief Read a point cloud data from an IFS file, and convert it to the
115  * given template pcl::PointCloud format.
116  * \param[in] file_name the name of the file containing the actual PointCloud data
117  * \param[out] cloud the resultant PointCloud message read from disk
118  *
119  * \return
120  * * < 0 (-1) on error
121  * * == 0 on success
122  */
123  template<typename PointT> int
124  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud)
125  {
126  pcl::PCLPointCloud2 blob;
127  int ifs_version;
128  cloud.sensor_origin_ = Eigen::Vector4f::Zero ();
129  cloud.sensor_orientation_ = Eigen::Quaternionf::Identity ();
130  int res = read (file_name, blob, ifs_version);
131 
132  // If no error, convert the data
133  if (res == 0)
134  pcl::fromPCLPointCloud2 (blob, cloud);
135  return (res);
136  }
137  };
138 
139  /** \brief Point Cloud Data (IFS) file format writer.
140  * \author Nizar Sallem
141  * \ingroup io
142  */
143  class PCL_EXPORTS IFSWriter
144  {
145  public:
148 
149  /** \brief Save point cloud data to an IFS file containing 3D points.
150  * \param[in] file_name the output file name
151  * \param[in] cloud the point cloud data
152  * \param[in] cloud_name the point cloud name to be stored inside the IFS file.
153  *
154  * \return
155  * * 0 on success
156  * * < 0 on error
157  */
158  int
159  write (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
160  const std::string &cloud_name = "cloud");
161 
162  /** \brief Save point cloud data to an IFS file containing 3D points.
163  * \param[in] file_name the output file name
164  * \param[in] cloud the point cloud
165  * \param[in] cloud_name the point cloud name to be stored inside the IFS file.
166  *
167  * \return
168  * * 0 on success
169  * * < 0 on error
170  */
171  template<typename PointT> int
172  write (const std::string &file_name, const pcl::PointCloud<PointT> &cloud,
173  const std::string &cloud_name = "cloud")
174  {
175  pcl::PCLPointCloud2 blob;
176  pcl::toPCLPointCloud2<PointT> (cloud, blob);
177  return (write (file_name, blob, cloud_name));
178  }
179  };
180 
181  namespace io
182  {
183  /** \brief Load an IFS file into a PCLPointCloud2 blob type.
184  * \param[in] file_name the name of the file to load
185  * \param[out] cloud the resultant templated point cloud
186  * \return 0 on success < 0 on error
187  *
188  * \ingroup io
189  */
190  inline int
191  loadIFSFile (const std::string &file_name, pcl::PCLPointCloud2 &cloud)
192  {
193  pcl::IFSReader p;
194  int ifs_version;
195  return (p.read (file_name, cloud, ifs_version));
196  }
197 
198  /** \brief Load any IFS file into a templated PointCloud type.
199  * \param[in] file_name the name of the file to load
200  * \param[out] cloud the resultant templated point cloud
201  * \return 0 on success < 0 on error
202  *
203  * \ingroup io
204  */
205  template<typename PointT> inline int
206  loadIFSFile (const std::string &file_name, pcl::PointCloud<PointT> &cloud)
207  {
208  pcl::IFSReader p;
209  return (p.read<PointT> (file_name, cloud));
210  }
211 
212  /** \brief Load any IFS file into a PolygonMesh type.
213  * \param[in] file_name the name of the file to load
214  * \param[out] mesh the resultant mesh
215  * \return 0 on success < 0 on error
216  *
217  * \ingroup io
218  */
219  inline int
220  loadIFSFile (const std::string &file_name, pcl::PolygonMesh &mesh)
221  {
222  pcl::IFSReader p;
223  int ifs_version;
224  return (p.read (file_name, mesh, ifs_version));
225  }
226 
227  /** \brief Save point cloud data to an IFS file containing 3D points
228  * \param[in] file_name the output file name
229  * \param[in] cloud the point cloud data message
230  * \return 0 on success < 0 on error
231  *
232  * \ingroup io
233  */
234  inline int
235  saveIFSFile (const std::string &file_name, const pcl::PCLPointCloud2 &cloud)
236  {
237  pcl::IFSWriter w;
238  return (w.write (file_name, cloud));
239  }
240 
241  /** \brief Save point cloud data to an IFS file containing 3D points
242  * \param[in] file_name the output file name
243  * \param[in] cloud the point cloud
244  * \return 0 on success < 0 on error
245  *
246  * \ingroup io
247  */
248  template<typename PointT> int
249  saveIFSFile (const std::string &file_name, const pcl::PointCloud<PointT> &cloud)
250  {
251  pcl::IFSWriter w;
252  return (w.write<PointT> (file_name, cloud));
253  }
254  }
255 }
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map...
Definition: conversions.h:168
int loadIFSFile(const std::string &file_name, pcl::PolygonMesh &mesh)
Load any IFS file into a PolygonMesh type.
Definition: ifs_io.h:220
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
IFSReader()
Empty constructor.
Definition: ifs_io.h:57
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:420
int saveIFSFile(const std::string &file_name, const pcl::PointCloud< PointT > &cloud)
Save point cloud data to an IFS file containing 3D points.
Definition: ifs_io.h:249
Indexed Face set (IFS) file format reader.
Definition: ifs_io.h:53
int read(const std::string &file_name, pcl::PointCloud< PointT > &cloud)
Read a point cloud data from an IFS file, and convert it to the given template pcl::PointCloud format...
Definition: ifs_io.h:124
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, int &ifs_version)
Read a point cloud data from an IFS file and store it into a pcl/PCLPointCloud2.
~IFSReader()
Empty destructor.
Definition: ifs_io.h:59
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:422
Point Cloud Data (IFS) file format writer.
Definition: ifs_io.h:143
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63
int write(const std::string &file_name, const pcl::PointCloud< PointT > &cloud, const std::string &cloud_name="cloud")
Save point cloud data to an IFS file containing 3D points.
Definition: ifs_io.h:172
A point structure representing Euclidean xyz coordinates, and the RGB color.
int write(const std::string &file_name, const pcl::PCLPointCloud2 &cloud, const std::string &cloud_name="cloud")
Save point cloud data to an IFS file containing 3D points.