Point Cloud Library (PCL)  1.9.0-dev
cloud_iterator.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, 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 
39 #ifndef PCL_POINT_CLOUD_ITERATOR_HPP_
40 #define PCL_POINT_CLOUD_ITERATOR_HPP_
41 
42 #include <pcl/cloud_iterator.h>
43 
44 namespace pcl
45 {
46  /** \brief
47  * \author Suat Gedikli
48  */
49  template <class PointT>
50  class DefaultIterator : public CloudIterator<PointT>::Iterator
51  {
52  public:
54  : cloud_ (cloud)
55  , iterator_ (cloud.begin ())
56  {
57  }
58 
60  {
61  }
62 
63  void operator ++ ()
64  {
65  ++iterator_;
66  }
67 
68  void operator ++ (int)
69  {
70  iterator_++;
71  }
72 
73  PointT& operator* () const
74  {
75  return (*iterator_);
76  }
77 
79  {
80  return (&(*iterator_));
81  }
82 
83  unsigned getCurrentPointIndex () const
84  {
85  return (iterator_ - cloud_.begin ());
86  }
87 
88  unsigned getCurrentIndex () const
89  {
90  return (iterator_ - cloud_.begin ());
91  }
92 
93  size_t size () const
94  {
95  return cloud_.size ();
96  }
97 
98  void reset ()
99  {
100  iterator_ = cloud_.begin ();
101  }
102 
103  bool isValid () const
104  {
105  return (iterator_ != cloud_.end ());
106  }
107  private:
108  PointCloud<PointT>& cloud_;
109  typename PointCloud<PointT>::iterator iterator_;
110  };
111 
112  /** \brief
113  * \author Suat Gedikli
114  */
115  template <class PointT>
116  class IteratorIdx : public CloudIterator<PointT>::Iterator
117  {
118  public:
119  IteratorIdx (PointCloud<PointT>& cloud, const std::vector<int>& indices)
120  : cloud_ (cloud)
121  , indices_ (indices)
122  , iterator_ (indices_.begin ())
123  {
124  }
125 
126  IteratorIdx (PointCloud<PointT>& cloud, const PointIndices& indices)
127  : cloud_ (cloud)
128  , indices_ (indices.indices)
129  , iterator_ (indices_.begin ())
130  {
131  }
132 
133  virtual ~IteratorIdx () {}
134 
136  {
137  ++iterator_;
138  }
139 
140  void operator ++ (int)
141  {
142  iterator_++;
143  }
144 
146  {
147  return (cloud_.points [*iterator_]);
148  }
149 
151  {
152  return (&(cloud_.points [*iterator_]));
153  }
154 
155  unsigned getCurrentPointIndex () const
156  {
157  return (*iterator_);
158  }
159 
160  unsigned getCurrentIndex () const
161  {
162  return (iterator_ - indices_.begin ());
163  }
164 
165  size_t size () const
166  {
167  return indices_.size ();
168  }
169 
170  void reset ()
171  {
172  iterator_ = indices_.begin ();
173  }
174 
175  bool isValid () const
176  {
177  return (iterator_ != indices_.end ());
178  }
179 
180  private:
181  PointCloud<PointT>& cloud_;
182  std::vector<int> indices_;
183  std::vector<int>::iterator iterator_;
184  };
185 
186  /** \brief
187  * \author Suat Gedikli
188  */
189  template <class PointT>
191  {
192  public:
194  : cloud_ (cloud)
195  , iterator_ (cloud.begin ())
196  {
197  }
198 
200  {
201  }
202 
204  {
205  ++iterator_;
206  }
207 
208  void operator ++ (int)
209  {
210  iterator_++;
211  }
212 
213  const PointT& operator* () const
214  {
215  return (*iterator_);
216  }
217 
218  const PointT* operator-> () const
219  {
220  return (&(*iterator_));
221  }
222 
223  unsigned getCurrentPointIndex () const
224  {
225  return (unsigned (iterator_ - cloud_.begin ()));
226  }
227 
228  unsigned getCurrentIndex () const
229  {
230  return (unsigned (iterator_ - cloud_.begin ()));
231  }
232 
233  size_t size () const
234  {
235  return cloud_.size ();
236  }
237 
238  void reset ()
239  {
240  iterator_ = cloud_.begin ();
241  }
242 
243  bool isValid () const
244  {
245  return (iterator_ != cloud_.end ());
246  }
247  private:
248  const PointCloud<PointT>& cloud_;
249  typename PointCloud<PointT>::const_iterator iterator_;
250  };
251 
252  /** \brief
253  * \author Suat Gedikli
254  */
255  template <class PointT>
257  {
258  public:
260  const std::vector<int>& indices)
261  : cloud_ (cloud)
262  , indices_ (indices)
263  , iterator_ (indices_.begin ())
264  {
265  }
266 
268  const PointIndices& indices)
269  : cloud_ (cloud)
270  , indices_ (indices.indices)
271  , iterator_ (indices_.begin ())
272  {
273  }
274 
275  virtual ~ConstIteratorIdx () {}
276 
278  {
279  ++iterator_;
280  }
281 
282  void operator ++ (int)
283  {
284  iterator_++;
285  }
286 
287  const PointT& operator* () const
288  {
289  return (cloud_.points[*iterator_]);
290  }
291 
292  const PointT* operator-> () const
293  {
294  return (&(cloud_.points [*iterator_]));
295  }
296 
297  unsigned getCurrentPointIndex () const
298  {
299  return (unsigned (*iterator_));
300  }
301 
302  unsigned getCurrentIndex () const
303  {
304  return (unsigned (iterator_ - indices_.begin ()));
305  }
306 
307  size_t size () const
308  {
309  return indices_.size ();
310  }
311 
312  void reset ()
313  {
314  iterator_ = indices_.begin ();
315  }
316 
317  bool isValid () const
318  {
319  return (iterator_ != indices_.end ());
320  }
321 
322  private:
323  const PointCloud<PointT>& cloud_;
324  std::vector<int> indices_;
325  std::vector<int>::iterator iterator_;
326  };
327 } // namespace pcl
328 
329 //////////////////////////////////////////////////////////////////////////////
330 template <class PointT>
332  : iterator_ (new DefaultIterator<PointT> (cloud))
333 {
334 }
335 
336 //////////////////////////////////////////////////////////////////////////////
337 template <class PointT>
339  PointCloud<PointT>& cloud, const std::vector<int>& indices)
340  : iterator_ (new IteratorIdx<PointT> (cloud, indices))
341 {
342 }
343 
344 //////////////////////////////////////////////////////////////////////////////
345 template <class PointT>
347  PointCloud<PointT>& cloud, const PointIndices& indices)
348  : iterator_ (new IteratorIdx<PointT> (cloud, indices))
349 {
350 }
351 
352 //////////////////////////////////////////////////////////////////////////////
353 template <class PointT>
355  PointCloud<PointT>& cloud, const Correspondences& corrs, bool source)
356 {
357  std::vector<int> indices;
358  indices.reserve (corrs.size ());
359  if (source)
360  {
361  for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
362  indices.push_back (indexIt->index_query);
363  }
364  else
365  {
366  for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
367  indices.push_back (indexIt->index_match);
368  }
369  iterator_ = new IteratorIdx<PointT> (cloud, indices);
370 }
371 
372 //////////////////////////////////////////////////////////////////////////////
373 template <class PointT>
375 {
376  delete iterator_;
377 }
378 
379 //////////////////////////////////////////////////////////////////////////////
380 template <class PointT> void
382 {
383  iterator_->operator++ ();
384 }
385 
386 //////////////////////////////////////////////////////////////////////////////
387 template <class PointT> void
389 {
390  iterator_->operator++ (0);
391 }
392 
393 //////////////////////////////////////////////////////////////////////////////
394 template <class PointT> PointT&
396 {
397  return (iterator_->operator * ());
398 }
399 
400 //////////////////////////////////////////////////////////////////////////////
401 template <class PointT> PointT*
403 {
404  return (iterator_->operator-> ());
405 }
406 
407 //////////////////////////////////////////////////////////////////////////////
408 template <class PointT> unsigned
410 {
411  return (iterator_->getCurrentPointIndex ());
412 }
413 
414 //////////////////////////////////////////////////////////////////////////////
415 template <class PointT> unsigned
417 {
418  return (iterator_->getCurrentIndex ());
419 }
420 
421 //////////////////////////////////////////////////////////////////////////////
422 template <class PointT> size_t
424 {
425  return (iterator_->size ());
426 }
427 
428 //////////////////////////////////////////////////////////////////////////////
429 template <class PointT> void
431 {
432  iterator_->reset ();
433 }
434 
435 //////////////////////////////////////////////////////////////////////////////
436 template <class PointT> bool
438 {
439  return (iterator_->isValid ());
440 }
441 
442 
443 //////////////////////////////////////////////////////////////////////////////
444 template <class PointT>
446  : iterator_ (new typename pcl::ConstCloudIterator<PointT>::DefaultConstIterator (cloud))
447 {
448 }
449 
450 //////////////////////////////////////////////////////////////////////////////
451 template <class PointT>
453  const PointCloud<PointT>& cloud, const std::vector<int>& indices)
454  : iterator_ (new typename pcl::ConstCloudIterator<PointT>::ConstIteratorIdx (cloud, indices))
455 {
456 }
457 
458 //////////////////////////////////////////////////////////////////////////////
459 template <class PointT>
461  const PointCloud<PointT>& cloud, const PointIndices& indices)
462  : iterator_ (new typename pcl::ConstCloudIterator<PointT>::ConstIteratorIdx (cloud, indices))
463 {
464 }
465 
466 //////////////////////////////////////////////////////////////////////////////
467 template <class PointT>
469  const PointCloud<PointT>& cloud, const Correspondences& corrs, bool source)
470 {
471  std::vector<int> indices;
472  indices.reserve (corrs.size ());
473  if (source)
474  {
475  for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
476  indices.push_back (indexIt->index_query);
477  }
478  else
479  {
480  for (typename Correspondences::const_iterator indexIt = corrs.begin (); indexIt != corrs.end (); ++indexIt)
481  indices.push_back (indexIt->index_match);
482  }
483  iterator_ = new typename pcl::ConstCloudIterator<PointT>::ConstIteratorIdx (cloud, indices);
484 }
485 
486 //////////////////////////////////////////////////////////////////////////////
487 template <class PointT>
489 {
490  delete iterator_;
491 }
492 
493 //////////////////////////////////////////////////////////////////////////////
494 template <class PointT> void
496 {
497  iterator_->operator++ ();
498 }
499 
500 //////////////////////////////////////////////////////////////////////////////
501 template <class PointT> void
503 {
504  iterator_->operator++ (0);
505 }
506 
507 //////////////////////////////////////////////////////////////////////////////
508 template <class PointT> const PointT&
510 {
511  return (iterator_->operator * ());
512 }
513 
514 //////////////////////////////////////////////////////////////////////////////
515 template <class PointT> const PointT*
517 {
518  return (iterator_->operator-> ());
519 }
520 
521 //////////////////////////////////////////////////////////////////////////////
522 template <class PointT> unsigned
524 {
525  return (iterator_->getCurrentPointIndex ());
526 }
527 
528 //////////////////////////////////////////////////////////////////////////////
529 template <class PointT> unsigned
531 {
532  return (iterator_->getCurrentIndex ());
533 }
534 
535 //////////////////////////////////////////////////////////////////////////////
536 template <class PointT> size_t
538 {
539  return (iterator_->size ());
540 }
541 
542 //////////////////////////////////////////////////////////////////////////////
543 template <class PointT> void
545 {
546  iterator_->reset ();
547 }
548 
549 //////////////////////////////////////////////////////////////////////////////
550 template <class PointT> bool
552 {
553  return (iterator_->isValid ());
554 }
555 
556 #endif // PCL_POINT_CLOUD_ITERATOR_HPP_
557 
CloudIterator(PointCloud< PointT > &cloud)
unsigned getCurrentIndex() const
Iterator class for point clouds with or without given indices.
IteratorIdx(PointCloud< PointT > &cloud, const std::vector< int > &indices)
Iterator class for point clouds with or without given indices.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
unsigned getCurrentPointIndex() const
PointT & operator*() const
VectorType::iterator iterator
Definition: point_cloud.h:440
unsigned getCurrentPointIndex() const
unsigned getCurrentPointIndex() const
const PointT & operator*() const
size_t size() const
Size of the range the iterator is going through.
bool isValid() const
size_t size() const
Size of the range the iterator is going through.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
ConstIteratorIdx(const PointCloud< PointT > &cloud, const std::vector< int > &indices)
unsigned getCurrentIndex() const
PointCloud represents the base class in PCL for storing collections of 3D points. ...
size_t size() const
PointT * operator->() const
DefaultConstIterator(const PointCloud< PointT > &cloud)
const PointT * operator->() const
ConstIteratorIdx(const PointCloud< PointT > &cloud, const PointIndices &indices)
unsigned getCurrentIndex() const
A point structure representing Euclidean xyz coordinates, and the RGB color.
size_t size() const
PointT & operator*() const
bool isValid() const
DefaultIterator(PointCloud< PointT > &cloud)
ConstCloudIterator(const PointCloud< PointT > &cloud)
unsigned getCurrentPointIndex() const
IteratorIdx(PointCloud< PointT > &cloud, const PointIndices &indices)
VectorType::const_iterator const_iterator
Definition: point_cloud.h:441
unsigned getCurrentIndex() const