Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
io.hpp
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.hpp 6126 2012-07-03 20:19:58Z aichim $
37  *
38  */
39 
40 #ifndef PCL_IO_IMPL_IO_HPP_
41 #define PCL_IO_IMPL_IO_HPP_
42 
43 #include <pcl/common/concatenate.h>
44 
46 template <typename PointT> int
48  const std::string &field_name,
49  std::vector<sensor_msgs::PointField> &fields)
50 {
51  fields.clear ();
52  // Get the fields list
53  pcl::for_each_type<typename pcl::traits::fieldList<PointT>::type>(pcl::detail::FieldAdder<PointT>(fields));
54  for (size_t d = 0; d < fields.size (); ++d)
55  if (fields[d].name == field_name)
56  return (static_cast<int>(d));
57  return (-1);
58 }
59 
61 template <typename PointT> int
62 pcl::getFieldIndex (const std::string &field_name,
63  std::vector<sensor_msgs::PointField> &fields)
64 {
65  fields.clear ();
66  // Get the fields list
67  pcl::for_each_type<typename pcl::traits::fieldList<PointT>::type>(pcl::detail::FieldAdder<PointT>(fields));
68  for (size_t d = 0; d < fields.size (); ++d)
69  if (fields[d].name == field_name)
70  return (static_cast<int>(d));
71  return (-1);
72 }
73 
75 template <typename PointT> void
76 pcl::getFields (const pcl::PointCloud<PointT> &, std::vector<sensor_msgs::PointField> &fields)
77 {
78  fields.clear ();
79  // Get the fields list
80  pcl::for_each_type<typename pcl::traits::fieldList<PointT>::type>(pcl::detail::FieldAdder<PointT>(fields));
81 }
82 
84 template <typename PointT> void
85 pcl::getFields (std::vector<sensor_msgs::PointField> &fields)
86 {
87  fields.clear ();
88  // Get the fields list
89  pcl::for_each_type<typename pcl::traits::fieldList<PointT>::type>(pcl::detail::FieldAdder<PointT>(fields));
90 }
91 
93 template <typename PointT> std::string
95 {
96  // Get the fields list
97  std::vector<sensor_msgs::PointField> fields;
98  pcl::for_each_type<typename pcl::traits::fieldList<PointT>::type>(pcl::detail::FieldAdder<PointT>(fields));
99  std::string result;
100  for (size_t i = 0; i < fields.size () - 1; ++i)
101  result += fields[i].name + " ";
102  result += fields[fields.size () - 1].name;
103  return (result);
104 }
105 
107 template <typename PointInT, typename PointOutT> void
109 {
110  // Allocate enough space and copy the basics
111  cloud_out.points.resize (cloud_in.points.size ());
112  cloud_out.header = cloud_in.header;
113  cloud_out.width = cloud_in.width;
114  cloud_out.height = cloud_in.height;
115  cloud_out.is_dense = cloud_in.is_dense;
116  // Copy all the data fields from the input cloud to the output one
117  typedef typename pcl::traits::fieldList<PointInT>::type FieldListInT;
118  typedef typename pcl::traits::fieldList<PointOutT>::type FieldListOutT;
119  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
120  // Iterate over each point
121  for (size_t i = 0; i < cloud_in.points.size (); ++i)
122  {
123  // Iterate over each dimension
124  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (cloud_in.points[i], cloud_out.points[i]));
125  }
126 }
127 
129 template <typename PointT> void
130 pcl::copyPointCloud (const pcl::PointCloud<PointT> &cloud_in, const std::vector<int> &indices,
131  pcl::PointCloud<PointT> &cloud_out)
132 {
133  // Allocate enough space and copy the basics
134  cloud_out.points.resize (indices.size ());
135  cloud_out.header = cloud_in.header;
136  cloud_out.width = static_cast<uint32_t>(indices.size ());
137  cloud_out.height = 1;
138  if (cloud_in.is_dense)
139  cloud_out.is_dense = true;
140  else
141  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
142  // To verify this, we would need to iterate over all points and check for NaNs
143  cloud_out.is_dense = false;
144 
145  // Copy all the data fields from the input cloud to the output one
146  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
147  // Iterate over each point
148  for (size_t i = 0; i < indices.size (); ++i)
149  // Iterate over each dimension
150  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointT, PointT> (cloud_in.points[indices[i]], cloud_out.points[i]));
151 }
152 
154 template <typename PointT> void
156  const std::vector<int, Eigen::aligned_allocator<int> > &indices,
157  pcl::PointCloud<PointT> &cloud_out)
158 {
159  // Allocate enough space and copy the basics
160  cloud_out.points.resize (indices.size ());
161  cloud_out.header = cloud_in.header;
162  cloud_out.width = static_cast<uint32_t> (indices.size ());
163  cloud_out.height = 1;
164  if (cloud_in.is_dense)
165  cloud_out.is_dense = true;
166  else
167  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
168  // To verify this, we would need to iterate over all points and check for NaNs
169  cloud_out.is_dense = false;
170 
171  // Copy all the data fields from the input cloud to the output one
172  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
173  // Iterate over each point
174  for (size_t i = 0; i < indices.size (); ++i)
175  // Iterate over each dimension
176  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointT, PointT> (cloud_in.points[indices[i]], cloud_out.points[i]));
177 }
178 
180 template <typename PointInT, typename PointOutT> void
181 pcl::copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in, const std::vector<int> &indices,
182  pcl::PointCloud<PointOutT> &cloud_out)
183 {
184  // Allocate enough space and copy the basics
185  cloud_out.points.resize (indices.size ());
186  cloud_out.header = cloud_in.header;
187  cloud_out.width = indices.size ();
188  cloud_out.height = 1;
189  if (cloud_in.is_dense)
190  cloud_out.is_dense = true;
191  else
192  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
193  // To verify this, we would need to iterate over all points and check for NaNs
194  cloud_out.is_dense = false;
195 
196  // Copy all the data fields from the input cloud to the output one
197  typedef typename pcl::traits::fieldList<PointInT>::type FieldListInT;
198  typedef typename pcl::traits::fieldList<PointOutT>::type FieldListOutT;
199  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
200  // Iterate over each point
201  for (size_t i = 0; i < indices.size (); ++i)
202  // Iterate over each dimension
203  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (cloud_in.points[indices[i]], cloud_out.points[i]));
204 }
205 
207 template <typename PointInT, typename PointOutT> void
209  const std::vector<int, Eigen::aligned_allocator<int> > &indices,
210  pcl::PointCloud<PointOutT> &cloud_out)
211 {
212  // Allocate enough space and copy the basics
213  cloud_out.points.resize (indices.size ());
214  cloud_out.header = cloud_in.header;
215  cloud_out.width = static_cast<uint32_t> (indices.size ());
216  cloud_out.height = 1;
217  if (cloud_in.is_dense)
218  cloud_out.is_dense = true;
219  else
220  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
221  // To verify this, we would need to iterate over all points and check for NaNs
222  cloud_out.is_dense = false;
223 
224  // Copy all the data fields from the input cloud to the output one
225  typedef typename pcl::traits::fieldList<PointInT>::type FieldListInT;
226  typedef typename pcl::traits::fieldList<PointOutT>::type FieldListOutT;
227  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
228  // Iterate over each point
229  for (size_t i = 0; i < indices.size (); ++i)
230  // Iterate over each dimension
231  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (cloud_in.points[indices[i]], cloud_out.points[i]));
232 }
233 
235 template <typename PointT> void
237  pcl::PointCloud<PointT> &cloud_out)
238 {
239  // Allocate enough space and copy the basics
240  cloud_out.points.resize (indices.indices.size ());
241  cloud_out.header = cloud_in.header;
242  cloud_out.width = indices.indices.size ();
243  cloud_out.height = 1;
244  if (cloud_in.is_dense)
245  cloud_out.is_dense = true;
246  else
247  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
248  // To verify this, we would need to iterate over all points and check for NaNs
249  cloud_out.is_dense = false;
250 
251  // Copy all the data fields from the input cloud to the output one
252  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
253  // Iterate over each point
254  for (size_t i = 0; i < indices.indices.size (); ++i)
255  // Iterate over each dimension
256  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointT, PointT> (cloud_in.points[indices.indices[i]], cloud_out.points[i]));
257 }
258 
260 template <typename PointInT, typename PointOutT> void
262  pcl::PointCloud<PointOutT> &cloud_out)
263 {
264  // Allocate enough space and copy the basics
265  cloud_out.points.resize (indices.indices.size ());
266  cloud_out.header = cloud_in.header;
267  cloud_out.width = indices.indices.size ();
268  cloud_out.height = 1;
269  if (cloud_in.is_dense)
270  cloud_out.is_dense = true;
271  else
272  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
273  // To verify this, we would need to iterate over all points and check for NaNs
274  cloud_out.is_dense = false;
275 
276  // Copy all the data fields from the input cloud to the output one
277  typedef typename pcl::traits::fieldList<PointInT>::type FieldListInT;
278  typedef typename pcl::traits::fieldList<PointOutT>::type FieldListOutT;
279  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
280  // Iterate over each point
281  for (size_t i = 0; i < indices.indices.size (); ++i)
282  // Iterate over each dimension
283  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (cloud_in.points[indices.indices[i]], cloud_out.points[i]));
284 }
285 
287 template <typename PointT> void
288 pcl::copyPointCloud (const pcl::PointCloud<PointT> &cloud_in, const std::vector<pcl::PointIndices> &indices,
289  pcl::PointCloud<PointT> &cloud_out)
290 {
291  int nr_p = 0;
292  for (size_t i = 0; i < indices.size (); ++i)
293  nr_p += indices[i].indices.size ();
294 
295  // Allocate enough space and copy the basics
296  cloud_out.points.resize (nr_p);
297  cloud_out.header = cloud_in.header;
298  cloud_out.width = nr_p;
299  cloud_out.height = 1;
300  if (cloud_in.is_dense)
301  cloud_out.is_dense = true;
302  else
303  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
304  // To verify this, we would need to iterate over all points and check for NaNs
305  cloud_out.is_dense = false;
306 
307  // Copy all the data fields from the input cloud to the output one
308  typedef typename pcl::traits::fieldList<PointT>::type FieldList;
309  // Iterate over each cluster
310  int cp = 0;
311  for (size_t cc = 0; cc < indices.size (); ++cc)
312  {
313  // Iterate over each idx
314  for (size_t i = 0; i < indices[cc].indices.size (); ++i)
315  {
316  // Iterate over each dimension
317  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointT, PointT> (cloud_in.points[indices[cc].indices[i]], cloud_out.points[cp]));
318  cp++;
319  }
320  }
321 }
322 
324 template <typename PointInT, typename PointOutT> void
325 pcl::copyPointCloud (const pcl::PointCloud<PointInT> &cloud_in, const std::vector<pcl::PointIndices> &indices,
326  pcl::PointCloud<PointOutT> &cloud_out)
327 {
328  int nr_p = 0;
329  for (size_t i = 0; i < indices.size (); ++i)
330  nr_p += indices[i].indices.size ();
331 
332  // Allocate enough space and copy the basics
333  cloud_out.points.resize (nr_p);
334  cloud_out.header = cloud_in.header;
335  cloud_out.width = nr_p;
336  cloud_out.height = 1;
337  if (cloud_in.is_dense)
338  cloud_out.is_dense = true;
339  else
340  // It's not necessarily true that is_dense is false if cloud_in.is_dense is false
341  // To verify this, we would need to iterate over all points and check for NaNs
342  cloud_out.is_dense = false;
343 
344  // Copy all the data fields from the input cloud to the output one
345  typedef typename pcl::traits::fieldList<PointInT>::type FieldListInT;
346  typedef typename pcl::traits::fieldList<PointOutT>::type FieldListOutT;
347  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
348  // Iterate over each cluster
349  int cp = 0;
350  for (size_t cc = 0; cc < indices.size (); ++cc)
351  {
352  // Iterate over each idx
353  for (size_t i = 0; i < indices[cc].indices.size (); ++i)
354  {
355  // Iterate over each dimension
356  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointInT, PointOutT> (cloud_in.points[indices[cc].indices[i]], cloud_out.points[cp]));
357  ++cp;
358  }
359  }
360 }
361 
363 template <typename PointIn1T, typename PointIn2T, typename PointOutT> void
365  const pcl::PointCloud<PointIn2T> &cloud2_in,
366  pcl::PointCloud<PointOutT> &cloud_out)
367 {
368  typedef typename pcl::traits::fieldList<PointIn1T>::type FieldList1;
369  typedef typename pcl::traits::fieldList<PointIn2T>::type FieldList2;
370 
371  if (cloud1_in.points.size () != cloud2_in.points.size ())
372  {
373  PCL_ERROR ("[pcl::concatenateFields] The number of points in the two input datasets differs!\n");
374  return;
375  }
376 
377  // Resize the output dataset
378  cloud_out.points.resize (cloud1_in.points.size ());
379  cloud_out.header = cloud1_in.header;
380  cloud_out.width = cloud1_in.width;
381  cloud_out.height = cloud1_in.height;
382  if (!cloud1_in.is_dense || !cloud2_in.is_dense)
383  cloud_out.is_dense = false;
384  else
385  cloud_out.is_dense = true;
386 
387  // Iterate over each point
388  for (size_t i = 0; i < cloud_out.points.size (); ++i)
389  {
390  // Iterate over each dimension
391  pcl::for_each_type <FieldList1> (pcl::NdConcatenateFunctor <PointIn1T, PointOutT> (cloud1_in.points[i], cloud_out.points[i]));
392  pcl::for_each_type <FieldList2> (pcl::NdConcatenateFunctor <PointIn2T, PointOutT> (cloud2_in.points[i], cloud_out.points[i]));
393  }
394 }
395 
396 #endif // PCL_IO_IMPL_IO_H_
397