Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
file_io.h
Go to the documentation of this file.
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 Willow Garage, Inc. 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 4932 2012-03-07 07:10:07Z rusu $
35  *
36  */
37 
38 #ifndef PCL_IO_FILE_IO_H_
39 #define PCL_IO_FILE_IO_H_
40 
41 #include <pcl/pcl_macros.h>
42 #include <pcl/common/io.h>
43 #include <boost/numeric/conversion/cast.hpp>
44 #include <cmath>
45 #include <sstream>
46 
47 namespace pcl
48 {
55  {
56  public:
60  virtual ~FileReader() {}
83  virtual int
84  readHeader (const std::string &file_name, sensor_msgs::PointCloud2 &cloud,
85  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
86  int &file_version, int &data_type, unsigned int &data_idx, const int offset = 0) = 0;
87 
100  virtual int
101  read (const std::string &file_name, sensor_msgs::PointCloud2 &cloud,
102  Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &file_version,
103  const int offset = 0) = 0;
104 
121  int
122  read (const std::string &file_name, sensor_msgs::PointCloud2 &cloud, const int offset = 0)
123  {
124  Eigen::Vector4f origin;
125  Eigen::Quaternionf orientation;
126  int file_version;
127  return (read (file_name, cloud, origin, orientation, file_version, offset));
128  }
129 
139  template<typename PointT> inline int
140  read (const std::string &file_name, pcl::PointCloud<PointT> &cloud, const int offset =0)
141  {
143  int file_version;
144  int res = read (file_name, blob, cloud.sensor_origin_, cloud.sensor_orientation_,
145  file_version, offset);
146 
147  // Exit in case of error
148  if (res < 0)
149  return res;
150  pcl::fromROSMsg (blob, cloud);
151  return (0);
152  }
153  };
154 
161  {
162  public:
165 
167  virtual ~FileWriter () {}
168 
177  virtual int
178  write (const std::string &file_name, const sensor_msgs::PointCloud2 &cloud,
179  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
180  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
181  const bool binary = false) = 0;
182 
191  inline int
192  write (const std::string &file_name, const sensor_msgs::PointCloud2::ConstPtr &cloud,
193  const Eigen::Vector4f &origin = Eigen::Vector4f::Zero (),
194  const Eigen::Quaternionf &orientation = Eigen::Quaternionf::Identity (),
195  const bool binary = false)
196  {
197  return (write (file_name, *cloud, origin, orientation, binary));
198  }
199 
206  template<typename PointT> inline int
207  write (const std::string &file_name,
208  const pcl::PointCloud<PointT> &cloud,
209  const bool binary = false)
210  {
211  Eigen::Vector4f origin = cloud.sensor_origin_;
212  Eigen::Quaternionf orientation = cloud.sensor_orientation_;
213 
215  pcl::toROSMsg (cloud, blob);
216 
217  // Save the data
218  return (write (file_name, blob, origin, orientation, binary));
219  }
220  };
221 
233  template <typename Type> inline void
235  const unsigned int point_index,
236  const int point_size,
237  const unsigned int field_idx,
238  const unsigned int fields_count,
239  std::ostream &stream)
240  {
241  Type value;
242  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (Type)], sizeof (Type));
243  if (pcl_isnan (value))
244  stream << "nan";
245  else
246  stream << boost::numeric_cast<Type>(value);
247  }
248  template <> inline void
250  const unsigned int point_index,
251  const int point_size,
252  const unsigned int field_idx,
253  const unsigned int fields_count,
254  std::ostream &stream)
255  {
256  int8_t value;
257  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (int8_t)], sizeof (int8_t));
258  if (pcl_isnan (value))
259  stream << "nan";
260  else
261  // Numeric cast doesn't give us what we want for int8_t
262  stream << boost::numeric_cast<int>(value);
263  }
264  template <> inline void
266  const unsigned int point_index,
267  const int point_size,
268  const unsigned int field_idx,
269  const unsigned int fields_count,
270  std::ostream &stream)
271  {
272  uint8_t value;
273  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (uint8_t)], sizeof (uint8_t));
274  if (pcl_isnan (value))
275  stream << "nan";
276  else
277  // Numeric cast doesn't give us what we want for uint8_t
278  stream << boost::numeric_cast<int>(value);
279  }
280 
291  template <typename Type> inline bool
293  const unsigned int point_index,
294  const int point_size,
295  const unsigned int field_idx,
296  const unsigned int fields_count)
297  {
298  Type value;
299  memcpy (&value, &cloud.data[point_index * point_size + cloud.fields[field_idx].offset + fields_count * sizeof (Type)], sizeof (Type));
300  if (!pcl_isfinite (value))
301  return (false);
302  return (true);
303  }
304 
316  template <typename Type> inline void
317  copyStringValue (const std::string &st, sensor_msgs::PointCloud2 &cloud,
318  unsigned int point_index, unsigned int field_idx, unsigned int fields_count)
319  {
320  Type value;
321  if (st == "nan")
322  {
323  value = std::numeric_limits<Type>::quiet_NaN ();
324  cloud.is_dense = false;
325  }
326  else
327  {
328  std::istringstream is (st);
329  is.imbue (std::locale::classic ());
330  is >> value;
331  }
332 
333  memcpy (&cloud.data[point_index * cloud.point_step +
334  cloud.fields[field_idx].offset +
335  fields_count * sizeof (Type)], reinterpret_cast<char*> (&value), sizeof (Type));
336  }
337 
338  template <> inline void
339  copyStringValue<int8_t> (const std::string &st, sensor_msgs::PointCloud2 &cloud,
340  unsigned int point_index, unsigned int field_idx, unsigned int fields_count)
341  {
342  int8_t value;
343  if (st == "nan")
344  {
345  value = static_cast<int8_t> (std::numeric_limits<int>::quiet_NaN ());
346  cloud.is_dense = false;
347  }
348  else
349  {
350  int val;
351  std::istringstream is (st);
352  is.imbue (std::locale::classic ());
353  is >> val;
354  value = static_cast<int8_t> (val);
355  }
356 
357  memcpy (&cloud.data[point_index * cloud.point_step +
358  cloud.fields[field_idx].offset +
359  fields_count * sizeof (int8_t)], reinterpret_cast<char*> (&value), sizeof (int8_t));
360  }
361 
362  template <> inline void
363  copyStringValue<uint8_t> (const std::string &st, sensor_msgs::PointCloud2 &cloud,
364  unsigned int point_index, unsigned int field_idx, unsigned int fields_count)
365  {
366  uint8_t value;
367  if (st == "nan")
368  {
369  value = static_cast<uint8_t> (std::numeric_limits<int>::quiet_NaN ());
370  cloud.is_dense = false;
371  }
372  else
373  {
374  int val;
375  std::istringstream is (st);
376  is.imbue (std::locale::classic ());
377  is >> val;
378  value = static_cast<uint8_t> (val);
379  }
380 
381  memcpy (&cloud.data[point_index * cloud.point_step +
382  cloud.fields[field_idx].offset +
383  fields_count * sizeof (uint8_t)], reinterpret_cast<char*> (&value), sizeof (uint8_t));
384  }
385 }
386 
387 #endif //#ifndef PCL_IO_FILE_IO_H_