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