Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: io.h 6126 2012-07-03 20:19:58Z aichim $
37  *
38  */
39 
40 #ifndef PCL_COMMON_IO_H_
41 #define PCL_COMMON_IO_H_
42 
43 #include <string>
44 #include <boost/fusion/sequence/intrinsic/at_key.hpp>
45 #include <pcl/pcl_base.h>
46 #include <pcl/PointIndices.h>
47 #include <pcl/ros/conversions.h>
48 #include <locale>
49 
50 namespace pcl
51 {
57  inline int
58  getFieldIndex (const sensor_msgs::PointCloud2 &cloud, const std::string &field_name)
59  {
60  // Get the index we need
61  for (size_t d = 0; d < cloud.fields.size (); ++d)
62  if (cloud.fields[d].name == field_name)
63  return (static_cast<int>(d));
64  return (-1);
65  }
66 
73  template <typename PointT> inline int
74  getFieldIndex (const pcl::PointCloud<PointT> &cloud, const std::string &field_name,
75  std::vector<sensor_msgs::PointField> &fields);
76 
82  template <typename PointT> inline int
83  getFieldIndex (const std::string &field_name,
84  std::vector<sensor_msgs::PointField> &fields);
85 
91  template <typename PointT> inline void
92  getFields (const pcl::PointCloud<PointT> &cloud, std::vector<sensor_msgs::PointField> &fields);
93 
98  template <typename PointT> inline void
99  getFields (std::vector<sensor_msgs::PointField> &fields);
100 
105  template <typename PointT> inline std::string
106  getFieldsList (const pcl::PointCloud<PointT> &cloud);
107 
112  inline std::string
114  {
115  std::string result;
116  for (size_t i = 0; i < cloud.fields.size () - 1; ++i)
117  result += cloud.fields[i].name + " ";
118  result += cloud.fields[cloud.fields.size () - 1].name;
119  return (result);
120  }
121 
126  inline int
127  getFieldSize (const int datatype)
128  {
129  switch (datatype)
130  {
133  return (1);
134 
137  return (2);
138 
142  return (4);
143 
145  return (8);
146 
147  default:
148  return (0);
149  }
150  }
151 
156  PCL_EXPORTS void
157  getFieldsSizes (const std::vector<sensor_msgs::PointField> &fields,
158  std::vector<int> &field_sizes);
159 
165  inline int
166  getFieldType (const int size, char type)
167  {
168  type = std::toupper (type, std::locale::classic ());
169  switch (size)
170  {
171  case 1:
172  if (type == 'I')
174  if (type == 'U')
176 
177  case 2:
178  if (type == 'I')
180  if (type == 'U')
182 
183  case 4:
184  if (type == 'I')
186  if (type == 'U')
188  if (type == 'F')
190 
191  case 8:
193 
194  default:
195  return (-1);
196  }
197  }
198 
203  inline char
204  getFieldType (const int type)
205  {
206  switch (type)
207  {
211  return ('I');
212 
216  return ('U');
217 
220  return ('F');
221  default:
222  return ('?');
223  }
224  }
225 
231  template <typename PointInT, typename PointOutT> void
232  copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in,
233  pcl::PointCloud<PointOutT> &cloud_out);
234 
242  PCL_EXPORTS bool
244  const sensor_msgs::PointCloud2 &cloud2,
245  sensor_msgs::PointCloud2 &cloud_out);
246 
253  PCL_EXPORTS void
254  copyPointCloud (const sensor_msgs::PointCloud2 &cloud_in,
255  const std::vector<int> &indices,
256  sensor_msgs::PointCloud2 &cloud_out);
257 
263  PCL_EXPORTS void
264  copyPointCloud (const sensor_msgs::PointCloud2 &cloud_in,
265  sensor_msgs::PointCloud2 &cloud_out);
266 
273  template <typename PointT> void
274  copyPointCloud (const pcl::PointCloud<PointT> &cloud_in,
275  const std::vector<int> &indices,
276  pcl::PointCloud<PointT> &cloud_out);
283  template <typename PointT> void
284  copyPointCloud (const pcl::PointCloud<PointT> &cloud_in,
285  const std::vector<int, Eigen::aligned_allocator<int> > &indices,
286  pcl::PointCloud<PointT> &cloud_out);
287 
294  template <typename PointInT, typename PointOutT> void
295  copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in,
296  const std::vector<int> &indices,
297  pcl::PointCloud<PointOutT> &cloud_out);
298 
305  template <typename PointInT, typename PointOutT> void
306  copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in,
307  const std::vector<int, Eigen::aligned_allocator<int> > &indices,
308  pcl::PointCloud<PointOutT> &cloud_out);
309 
316  template <typename PointT> void
317  copyPointCloud (const pcl::PointCloud<PointT> &cloud_in,
318  const PointIndices &indices,
319  pcl::PointCloud<PointT> &cloud_out);
320 
327  template <typename PointInT, typename PointOutT> void
328  copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in,
329  const PointIndices &indices,
330  pcl::PointCloud<PointOutT> &cloud_out);
331 
338  template <typename PointT> void
339  copyPointCloud (const pcl::PointCloud<PointT> &cloud_in,
340  const std::vector<pcl::PointIndices> &indices,
341  pcl::PointCloud<PointT> &cloud_out);
342 
349  template <typename PointInT, typename PointOutT> void
350  copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in,
351  const std::vector<pcl::PointIndices> &indices,
352  pcl::PointCloud<PointOutT> &cloud_out);
353 
365  template <typename PointIn1T, typename PointIn2T, typename PointOutT> void
367  const pcl::PointCloud<PointIn2T> &cloud2_in,
368  pcl::PointCloud<PointOutT> &cloud_out);
369 
381  PCL_EXPORTS bool
382  concatenateFields (const sensor_msgs::PointCloud2 &cloud1_in,
383  const sensor_msgs::PointCloud2 &cloud2_in,
384  sensor_msgs::PointCloud2 &cloud_out);
385 
391  PCL_EXPORTS bool
392  getPointCloudAsEigen (const sensor_msgs::PointCloud2 &in, Eigen::MatrixXf &out);
393 
400  PCL_EXPORTS bool
401  getEigenAsPointCloud (Eigen::MatrixXf &in, sensor_msgs::PointCloud2 &out);
402 
403  namespace io
404  {
409  template <std::size_t N> void
410  swapByte (char* bytes);
411 
415  template <> inline void
416  swapByte<1> (char* bytes) { bytes[0] = bytes[0]; }
417 
418 
422  template <> inline void
423  swapByte<2> (char* bytes) { std::swap (bytes[0], bytes[1]); }
424 
428  template <> inline void
429  swapByte<4> (char* bytes)
430  {
431  std::swap (bytes[0], bytes[3]);
432  std::swap (bytes[1], bytes[2]);
433  }
434 
438  template <> inline void
439  swapByte<8> (char* bytes)
440  {
441  std::swap (bytes[0], bytes[7]);
442  std::swap (bytes[1], bytes[6]);
443  std::swap (bytes[2], bytes[5]);
444  std::swap (bytes[3], bytes[4]);
445  }
446 
450  template <typename T> void
451  swapByte (T& value)
452  {
453  pcl::io::swapByte<sizeof(T)> (reinterpret_cast<char*> (&value));
454  }
455  }
456 }
457 
458 #include <pcl/common/impl/io.hpp>
459 
460 #endif //#ifndef PCL_COMMON_IO_H_
461