Point Cloud Library (PCL)  1.11.1-dev
file_io.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder(s) nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id: file_io.h 827 2011-05-04 02:00:04Z nizar $
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/conversions.h> // for fromPCLPointCloud2, toPCLPointCloud2
41 #include <pcl/point_cloud.h> // for PointCloud
42 #include <pcl/PCLPointCloud2.h> // for PCLPointCloud2
43 #include <pcl/io/boost.h>
44 #include <cmath>
45 #include <sstream>
46 #include <Eigen/Geometry> // for Quaternionf
47 
48 namespace pcl
49 {
50  /** \brief Point Cloud Data (FILE) file format reader interface.
51  * Any (FILE) format file reader should implement its virtual methods.
52  * \author Nizar Sallem
53  * \ingroup io
54  */
56  {
57  public:
58  /** \brief empty constructor */
60  /** \brief empty destructor */
61  virtual ~FileReader() {}
62  /** \brief Read a point cloud data header from a FILE file.
63  *
64  * Load only the meta information (number of points, their types, etc),
65  * and not the points themselves, from a given FILE file. Useful for fast
66  * evaluation of the underlying data structure.
67  *
68  * Returns:
69  * * < 0 (-1) on error
70  * * > 0 on success
71  * \param[in] file_name the name of the file to load
72  * \param[out] cloud the resultant point cloud dataset (only the header will be filled)
73  * \param[out] origin the sensor acquisition origin (only for > FILE_V7 - null if not present)
74  * \param[out] orientation the sensor acquisition orientation (only for > FILE_V7 - identity if not present)
75  * \param[out] file_version the FILE version of the file (either FILE_V6 or FILE_V7)
76  * \param[out] data_type the type of data (binary data=1, ascii=0, etc)
77  * \param[out] data_idx the offset of cloud data within the file
78  * \param[in] offset the offset in the file where to expect the true header to begin.
79  * One usage example for setting the offset parameter is for reading
80  * data from a TAR "archive containing multiple files: TAR files always
81  * add a 512 byte header in front of the actual file, so set the offset
82  * to the next byte after the header (e.g., 513).
83  */
84  virtual int
85  readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
86  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
87  int &file_version, int &data_type, unsigned int &data_idx, const int offset = 0) = 0;
88 
89  /** \brief Read a point cloud data from a FILE 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 PointCloud message read from disk
92  * \param[out] origin the sensor acquisition origin (only for > FILE_V7 - null if not present)
93  * \param[out] orientation the sensor acquisition orientation (only for > FILE_V7 - identity if not present)
94  * \param[out] file_version the FILE version of the file (either FILE_V6 or FILE_V7)
95  * \param[in] offset the offset in the file where to expect the true header to begin.
96  * One usage example for setting the offset parameter is for reading
97  * data from a TAR "archive containing multiple files: TAR files always
98  * add a 512 byte header in front of the actual file, so set the offset
99  * to the next byte after the header (e.g., 513).
100  */
101  virtual int
102  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
103  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version,
104  const int offset = 0) = 0;
105 
106  /** \brief Read a point cloud data from a FILE file (FILE_V6 only!) and store it into a pcl/PCLPointCloud2.
107  *
108  * \note This function is provided for backwards compatibility only and
109  * it can only read FILE_V6 files correctly, as pcl::PCLPointCloud2
110  * does not contain a sensor origin/orientation. Reading any file
111  * > FILE_V6 will generate a warning.
112  *
113  * \param[in] file_name the name of the file containing the actual PointCloud data
114  * \param[out] cloud the resultant PointCloud message read from disk
115  *
116  * \param[in] offset the offset in the file where to expect the true header to begin.
117  * One usage example for setting the offset parameter is for reading
118  * data from a TAR "archive containing multiple files: TAR files always
119  * add a 512 byte header in front of the actual file, so set the offset
120  * to the next byte after the header (e.g., 513).
121  */
122  int
123  read (const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset = 0)
124  {
125  Eigen::Vector4f origin;
126  Eigen::Quaternionf orientation;
127  int file_version;
128  return (read (file_name, cloud, origin, orientation, file_version, offset));
129  }
130 
131  /** \brief Read a point cloud data from any FILE file, and convert it to the given template format.
132  * \param[in] file_name the name of the file containing the actual PointCloud data
133  * \param[out] cloud the resultant PointCloud message read from disk
134  * \param[in] offset the offset in the file where to expect the true header to begin.
135  * One usage example for setting the offset parameter is for reading
136  * data from a TAR "archive containing multiple files: TAR files always
137  * add a 512 byte header in front of the actual file, so set the offset
138  * to the next byte after the header (e.g., 513).
139  */
140  template<typename PointT> inline int
141  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud, const int offset =0)
142  {
143  pcl::PCLPointCloud2 blob;
144  int file_version;
145  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
146  file_version, offset);
147 
148  // Exit in case of error
149  if (res < 0)
150  return res;
151  pcl::fromPCLPointCloud2 (blob, cloud);
152  return (0);
153  }
154  };
155 
156  /** \brief Point Cloud Data (FILE) file format writer.
157  * Any (FILE) format file reader should implement its virtual methods
158  * \author Nizar Sallem
159  * \ingroup io
160  */
162  {
163  public:
164  /** \brief Empty constructor */
166 
167  /** \brief Empty destructor */
168  virtual ~FileWriter () {}
169 
170  /** \brief Save point cloud data to a FILE file containing n-D points
171  * \param[in] file_name the output file name
172  * \param[in] cloud the point cloud data message
173  * \param[in] origin the sensor acquisition origin
174  * \param[in] orientation the sensor acquisition orientation
175  * \param[in] binary set to true if the file is to be written in a binary
176  * FILE format, false (default) for ASCII
177  */
178  virtual int
179  write (const std::string &file_name, const pcl::PCLPointCloud2 &cloud,
180  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
181  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
182  const bool binary = false) = 0;
183 
184  /** \brief Save point cloud data to a FILE file containing n-D points
185  * \param[in] file_name the output file name
186  * \param[in] cloud the point cloud data message (boost shared pointer)
187  * \param[in] binary set to true if the file is to be written in a binary
188  * FILE format, false (default) for ASCII
189  * \param[in] origin the sensor acquisition origin
190  * \param[in] orientation the sensor acquisition orientation
191  */
192  inline int
193  write (const std::string &file_name, const pcl::PCLPointCloud2::ConstPtr &cloud,
194  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
195  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
196  const bool binary = false)
197  {
198  return (write (file_name, *cloud, origin, orientation, binary));
199  }
200 
201  /** \brief Save point cloud data to a FILE file containing n-D points
202  * \param[in] file_name the output file name
203  * \param[in] cloud the pcl::PointCloud data
204  * \param[in] binary set to true if the file is to be written in a binary
205  * FILE format, false (default) for ASCII
206  */
207  template<typename PointT> inline int
208  write (const std::string &file_name,
209  const pcl::PointCloud<PointT> &cloud,
210  const bool binary = false)
211  {
212  Eigen::Vector4f origin = cloud.sensor_origin_;
213  Eigen::Quaternionf orientation = cloud.sensor_orientation_;
214 
215  pcl::PCLPointCloud2 blob;
216  pcl::toPCLPointCloud2 (cloud, blob);
217 
218  // Save the data
219  return (write (file_name, blob, origin, orientation, binary));
220  }
221  };
222 
223  /** \brief inserts a value of type Type (uchar, char, uint, int, float, double, ...) into a stringstream.
224  *
225  * If the value is NaN, it inserts "nan".
226  *
227  * \param[in] cloud the cloud to copy from
228  * \param[in] point_index the index of the point
229  * \param[in] point_size the size of the point in the cloud
230  * \param[in] field_idx the index of the dimension/field
231  * \param[in] fields_count the current fields count
232  * \param[out] stream the ostringstream to copy into
233  */
234  template <typename Type> inline
235  std::enable_if_t<std::is_floating_point<Type>::value>
237  const pcl::index_t point_index,
238  const int point_size,
239  const unsigned int field_idx,
240  const unsigned int fields_count,
241  std::ostream &stream)
242  {
243  Type value;
244  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (Type)], sizeof (Type));
245  if (std::isnan (value))
246  stream << "nan";
247  else
248  stream << value;
249  }
250 
251  template <typename Type> inline
252  std::enable_if_t<std::is_integral<Type>::value>
254  const pcl::index_t point_index,
255  const int point_size,
256  const unsigned int field_idx,
257  const unsigned int fields_count,
258  std::ostream &stream)
259  {
260  Type value;
261  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (Type)], sizeof (Type));
262  stream << value;
263  }
264 
265  template <> inline void
266  copyValueString<std::int8_t> (const pcl::PCLPointCloud2 &cloud,
267  const pcl::index_t point_index,
268  const int point_size,
269  const unsigned int field_idx,
270  const unsigned int fields_count,
271  std::ostream &stream)
272  {
273  std::int8_t value;
274  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (std::int8_t)], sizeof (std::int8_t));
275  //Cast to int to prevent value is handled as char
276  stream << boost::numeric_cast<int>(value);
277  }
278 
279  template <> inline void
280  copyValueString<std::uint8_t> (const pcl::PCLPointCloud2 &cloud,
281  const pcl::index_t point_index,
282  const int point_size,
283  const unsigned int field_idx,
284  const unsigned int fields_count,
285  std::ostream &stream)
286  {
287  std::uint8_t value;
288  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (std::uint8_t)], sizeof (std::uint8_t));
289  //Cast to unsigned int to prevent value is handled as char
290  stream << boost::numeric_cast<unsigned int>(value);
291  }
292 
293  /** \brief Check whether a given value of type Type (uchar, char, uint, int, float, double, ...) is finite or not
294  *
295  * \param[in] cloud the cloud that contains the data
296  * \param[in] point_index the index of the point
297  * \param[in] point_size the size of the point in the cloud
298  * \param[in] field_idx the index of the dimension/field
299  * \param[in] fields_count the current fields count
300  *
301  * \return true if the value is finite, false otherwise
302  */
303  template <typename Type> inline
304  std::enable_if_t<std::is_floating_point<Type>::value, bool>
306  const pcl::index_t point_index,
307  const int point_size,
308  const unsigned int field_idx,
309  const unsigned int fields_count)
310  {
311  Type value;
312  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (Type)], sizeof (Type));
313  return std::isfinite (value);
314  }
315 
316  template <typename Type> inline
317  std::enable_if_t<std::is_integral<Type>::value, bool>
318  isValueFinite (const pcl::PCLPointCloud2& /* cloud */,
319  const pcl::index_t /* point_index */,
320  const int /* point_size */,
321  const unsigned int /* field_idx */,
322  const unsigned int /* fields_count */)
323  {
324  return true;
325  }
326 
327  namespace detail {
328  template <typename Type>
329  inline void
330  copyStringValue(const std::string& st,
331  pcl::PCLPointCloud2& cloud,
332  pcl::index_t point_index,
333  unsigned int field_idx,
334  unsigned int fields_count,
335  std::istringstream& is)
336  {
337  Type value;
338  if (boost::iequals(st, "nan")) {
339  value = std::numeric_limits<Type>::quiet_NaN();
340  cloud.is_dense = false;
341  }
342  else {
343  is.str(st);
344  if (!(is >> value))
345  value = static_cast<Type>(atof(st.c_str()));
346  }
347 
348  memcpy(&cloud.data[point_index * cloud.point_step + cloud.fields[field_idx].offset +
349  fields_count * sizeof(Type)],
350  reinterpret_cast<char*>(&value),
351  sizeof(Type));
352  }
353 
354  template <>
355  inline void
356  copyStringValue<std::int8_t>(const std::string& st,
357  pcl::PCLPointCloud2& cloud,
358  pcl::index_t point_index,
359  unsigned int field_idx,
360  unsigned int fields_count,
361  std::istringstream& is)
362  {
363  std::int8_t value;
364  int val;
365  is.str(st);
366  // is >> val; -- unfortunately this fails on older GCC versions and CLANG on MacOS
367  if (!(is >> val)) {
368  val = static_cast<int>(atof(st.c_str()));
369  }
370  value = static_cast<std::int8_t>(val);
371 
372  memcpy(&cloud.data[point_index * cloud.point_step + cloud.fields[field_idx].offset +
373  fields_count * sizeof(std::int8_t)],
374  reinterpret_cast<char*>(&value),
375  sizeof(std::int8_t));
376  }
377 
378  template <>
379  inline void
380  copyStringValue<std::uint8_t>(const std::string& st,
381  pcl::PCLPointCloud2& cloud,
382  pcl::index_t point_index,
383  unsigned int field_idx,
384  unsigned int fields_count,
385  std::istringstream& is)
386  {
387  std::uint8_t value;
388  int val;
389  is.str(st);
390  // is >> val; -- unfortunately this fails on older GCC versions and CLANG on
391  // MacOS
392  if (!(is >> val)) {
393  val = static_cast<int>(atof(st.c_str()));
394  }
395  value = static_cast<std::uint8_t>(val);
396 
397  memcpy(&cloud.data[point_index * cloud.point_step + cloud.fields[field_idx].offset +
398  fields_count * sizeof(std::uint8_t)],
399  reinterpret_cast<char*>(&value),
400  sizeof(std::uint8_t));
401  }
402  } // namespace detail
403 
404  /**
405  * \brief Copy one single value of type T (uchar, char, uint, int, float, double, ...) from a string
406  * \details Uses `istringstream` to do the conversion in classic locale
407  * Checks if the st is "nan" and converts it accordingly.
408  *
409  * \param[in] st the string containing the value to convert and copy
410  * \param[out] cloud the cloud to copy it to
411  * \param[in] point_index the index of the point
412  * \param[in] field_idx the index of the dimension/field
413  * \param[in] fields_count the current fields count
414  */
415  template <typename Type> inline void
416  copyStringValue (const std::string &st, pcl::PCLPointCloud2 &cloud,
417  pcl::index_t point_index, unsigned int field_idx, unsigned int fields_count)
418  {
419  std::istringstream is;
420  is.imbue (std::locale::classic ());
421  detail::copyStringValue<Type> (st, cloud,point_index, field_idx, fields_count, is);
422  }
423 /**
424  * \brief Copy one single value of type T (uchar, char, uint, int, float, double, ...) from a string
425  * \details Uses the provided `istringstream` to do the conversion, respecting its locale settings
426  * Checks if the st is "nan" and converts it accordingly.
427  *
428  * \param[in] st the string containing the value to convert and copy
429  * \param[out] cloud the cloud to copy it to
430  * \param[in] point_index the index of the point
431  * \param[in] field_idx the index of the dimension/field
432  * \param[in] fields_count the current fields count
433  * \param[in,out] is input string stream for helping to convert st into cloud
434  */
435  template <typename Type> inline void
436  copyStringValue (const std::string &st, pcl::PCLPointCloud2 &cloud,
437  pcl::index_t point_index, unsigned int field_idx, unsigned int fields_count,
438  std::istringstream& is)
439  {
440  detail::copyStringValue<Type> (st, cloud,point_index, field_idx, fields_count, is);
441  }
442 }
pcl::FileReader::~FileReader
virtual ~FileReader()
empty destructor
Definition: file_io.h:61
pcl::FileReader::read
int read(const std::string &file_name, pcl::PointCloud< PointT > &cloud, const int offset=0)
Read a point cloud data from any FILE file, and convert it to the given template format.
Definition: file_io.h:141
pcl
Definition: convolution.h:46
pcl::detail::copyStringValue
void copyStringValue(const std::string &st, pcl::PCLPointCloud2 &cloud, pcl::index_t point_index, unsigned int field_idx, unsigned int fields_count, std::istringstream &is)
Definition: file_io.h:330
pcl::PCLPointCloud2::point_step
index_t point_step
Definition: PCLPointCloud2.h:27
pcl::FileReader::FileReader
FileReader()
empty constructor
Definition: file_io.h:59
pcl::FileWriter::~FileWriter
virtual ~FileWriter()
Empty destructor.
Definition: file_io.h:168
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::index_t
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
Definition: types.h:110
pcl::PCLPointCloud2::is_dense
std::uint8_t is_dense
Definition: PCLPointCloud2.h:32
pcl::FileWriter
Point Cloud Data (FILE) file format writer.
Definition: file_io.h:161
pcl::FileWriter::write
int write(const std::string &file_name, const pcl::PointCloud< PointT > &cloud, const bool binary=false)
Save point cloud data to a FILE file containing n-D points.
Definition: file_io.h:208
pcl::FileWriter::FileWriter
FileWriter()
Empty constructor.
Definition: file_io.h:165
pcl::read
void read(std::istream &stream, Type &value)
Function for reading data from a stream.
Definition: region_xy.h:46
pcl::PCLPointCloud2::fields
std::vector<::pcl::PCLPointField > fields
Definition: PCLPointCloud2.h:23
pcl::PCLPointCloud2::ConstPtr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Definition: PCLPointCloud2.h:36
pcl::FileReader::read
int read(const std::string &file_name, pcl::PCLPointCloud2 &cloud, const int offset=0)
Read a point cloud data from a FILE file (FILE_V6 only!) and store it into a pcl/PCLPointCloud2.
Definition: file_io.h:123
pcl::isValueFinite
std::enable_if_t< std::is_floating_point< Type >::value, bool > isValueFinite(const pcl::PCLPointCloud2 &cloud, const pcl::index_t point_index, const int point_size, const unsigned int field_idx, const unsigned int fields_count)
Check whether a given value of type Type (uchar, char, uint, int, float, double, ....
Definition: file_io.h:305
pcl::FileReader
Point Cloud Data (FILE) file format reader interface.
Definition: file_io.h:55
pcl::PointCloud::sensor_origin_
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:399
pcl::PointCloud::sensor_orientation_
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:401
pcl::PCLPointCloud2::data
std::vector< std::uint8_t > data
Definition: PCLPointCloud2.h:30
pcl::PCLPointCloud2
Definition: PCLPointCloud2.h:16
pcl::write
void write(std::ostream &stream, Type value)
Function for writing data to a stream.
Definition: region_xy.h:63
pcl::FileWriter::write
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 FILE file containing n-D points.
Definition: file_io.h:193
pcl::toPCLPointCloud2
void toPCLPointCloud2(const pcl::PointCloud< PointT > &cloud, pcl::PCLPointCloud2 &msg)
Convert a pcl::PointCloud<T> object to a PCLPointCloud2 binary data blob.
Definition: conversions.h:240
pcl::copyValueString
std::enable_if_t< std::is_floating_point< Type >::value > copyValueString(const pcl::PCLPointCloud2 &cloud, const pcl::index_t point_index, const int point_size, const unsigned int field_idx, const unsigned int fields_count, std::ostream &stream)
inserts a value of type Type (uchar, char, uint, int, float, double, ...) into a stringstream.
Definition: file_io.h:236
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:323
pcl::copyStringValue
void copyStringValue(const std::string &st, pcl::PCLPointCloud2 &cloud, pcl::index_t point_index, unsigned int field_idx, unsigned int fields_count)
Copy one single value of type T (uchar, char, uint, int, float, double, ...) from a string.
Definition: file_io.h:416
pcl::fromPCLPointCloud2
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:167