Point Cloud Library (PCL)  1.11.1-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  std::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 Indices& 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  std::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  Indices indices_;
183  Indices::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 
203  void operator ++ () override
204  {
205  ++iterator_;
206  }
207 
208  void operator ++ (int) override
209  {
210  iterator_++;
211  }
212 
213  const PointT& operator* () const override
214  {
215  return (*iterator_);
216  }
217 
218  const PointT* operator-> () const override
219  {
220  return (&(*iterator_));
221  }
222 
223  unsigned getCurrentPointIndex () const override
224  {
225  return (unsigned (iterator_ - cloud_.begin ()));
226  }
227 
228  unsigned getCurrentIndex () const override
229  {
230  return (unsigned (iterator_ - cloud_.begin ()));
231  }
232 
233  std::size_t size () const override
234  {
235  return cloud_.size ();
236  }
237 
238  void reset () override
239  {
240  iterator_ = cloud_.begin ();
241  }
242 
243  bool isValid () const override
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 Indices& 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 
276 
277  void operator ++ () override
278  {
279  ++iterator_;
280  }
281 
282  void operator ++ (int) override
283  {
284  iterator_++;
285  }
286 
287  const PointT& operator* () const override
288  {
289  return (cloud_[*iterator_]);
290  }
291 
292  const PointT* operator-> () const override
293  {
294  return (&(cloud_.points [*iterator_]));
295  }
296 
297  unsigned getCurrentPointIndex () const override
298  {
299  return (unsigned (*iterator_));
300  }
301 
302  unsigned getCurrentIndex () const override
303  {
304  return (unsigned (iterator_ - indices_.begin ()));
305  }
306 
307  std::size_t size () const override
308  {
309  return indices_.size ();
310  }
311 
312  void reset () override
313  {
314  iterator_ = indices_.begin ();
315  }
316 
317  bool isValid () const override
318  {
319  return (iterator_ != indices_.end ());
320  }
321 
322  private:
323  const PointCloud<PointT>& cloud_;
324  Indices indices_;
325  Indices::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 Indices& 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  Indices indices;
358  indices.reserve (corrs.size ());
359  if (source)
360  {
361  for (const auto &corr : corrs)
362  indices.push_back (corr.index_query);
363  }
364  else
365  {
366  for (const auto &corr : corrs)
367  indices.push_back (corr.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> std::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 Indices& 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  Indices indices;
472  indices.reserve (corrs.size ());
473  if (source)
474  {
475  for (const auto &corr : corrs)
476  indices.push_back (corr.index_query);
477  }
478  else
479  {
480  for (const auto &corr : corrs)
481  indices.push_back (corr.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> std::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 
pcl::ConstCloudIterator::ConstIteratorIdx::~ConstIteratorIdx
~ConstIteratorIdx()
Definition: cloud_iterator.hpp:275
pcl
Definition: convolution.h:46
pcl::CloudIterator::isValid
bool isValid() const
Definition: cloud_iterator.hpp:437
pcl::IteratorIdx
Definition: cloud_iterator.hpp:116
pcl::ConstCloudIterator::isValid
bool isValid() const
Definition: cloud_iterator.hpp:551
pcl::ConstCloudIterator::ConstCloudIterator
ConstCloudIterator(const PointCloud< PointT > &cloud)
Definition: cloud_iterator.hpp:445
pcl::IteratorIdx::operator++
void operator++()
Definition: cloud_iterator.hpp:135
pcl::ConstCloudIterator::ConstIteratorIdx::getCurrentIndex
unsigned getCurrentIndex() const override
Definition: cloud_iterator.hpp:302
pcl::ConstCloudIterator::ConstIteratorIdx::reset
void reset() override
Definition: cloud_iterator.hpp:312
pcl::IteratorIdx::size
std::size_t size() const
Definition: cloud_iterator.hpp:165
pcl::ConstCloudIterator::operator->
const PointT * operator->() const
Definition: cloud_iterator.hpp:516
pcl::IteratorIdx::operator->
PointT * operator->()
Definition: cloud_iterator.hpp:150
pcl::ConstCloudIterator::getCurrentPointIndex
unsigned getCurrentPointIndex() const
Definition: cloud_iterator.hpp:523
pcl::ConstCloudIterator::DefaultConstIterator::isValid
bool isValid() const override
Definition: cloud_iterator.hpp:243
pcl::ConstCloudIterator::DefaultConstIterator::size
std::size_t size() const override
Definition: cloud_iterator.hpp:233
pcl::IteratorIdx::getCurrentIndex
unsigned getCurrentIndex() const
Definition: cloud_iterator.hpp:160
pcl::ConstCloudIterator::operator*
const PointT & operator*() const
Definition: cloud_iterator.hpp:509
pcl::ConstCloudIterator::ConstIteratorIdx::getCurrentPointIndex
unsigned getCurrentPointIndex() const override
Definition: cloud_iterator.hpp:297
pcl::ConstCloudIterator::DefaultConstIterator::getCurrentIndex
unsigned getCurrentIndex() const override
Definition: cloud_iterator.hpp:228
pcl::ConstCloudIterator::DefaultConstIterator::~DefaultConstIterator
~DefaultConstIterator()
Definition: cloud_iterator.hpp:199
pcl::IteratorIdx::IteratorIdx
IteratorIdx(PointCloud< PointT > &cloud, const Indices &indices)
Definition: cloud_iterator.hpp:119
pcl::DefaultIterator::operator->
PointT * operator->()
Definition: cloud_iterator.hpp:78
pcl::ConstCloudIterator::DefaultConstIterator::reset
void reset() override
Definition: cloud_iterator.hpp:238
pcl::CloudIterator
Iterator class for point clouds with or without given indices.
Definition: cloud_iterator.h:51
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::CloudIterator::operator++
void operator++()
Definition: cloud_iterator.hpp:381
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::IteratorIdx::reset
void reset()
Definition: cloud_iterator.hpp:170
pcl::ConstCloudIterator::DefaultConstIterator::DefaultConstIterator
DefaultConstIterator(const PointCloud< PointT > &cloud)
Definition: cloud_iterator.hpp:193
pcl::CloudIterator::size
std::size_t size() const
Size of the range the iterator is going through.
Definition: cloud_iterator.hpp:423
pcl::DefaultIterator::isValid
bool isValid() const
Definition: cloud_iterator.hpp:103
pcl::ConstCloudIterator::ConstIteratorIdx::ConstIteratorIdx
ConstIteratorIdx(const PointCloud< PointT > &cloud, const Indices &indices)
Definition: cloud_iterator.hpp:259
pcl::ConstCloudIterator::operator++
void operator++()
Definition: cloud_iterator.hpp:495
pcl::CloudIterator::getCurrentIndex
unsigned getCurrentIndex() const
Definition: cloud_iterator.hpp:416
pcl::IteratorIdx::isValid
bool isValid() const
Definition: cloud_iterator.hpp:175
pcl::ConstCloudIterator::ConstIteratorIdx
Definition: cloud_iterator.hpp:256
pcl::CloudIterator::CloudIterator
CloudIterator(PointCloud< PointT > &cloud)
Definition: cloud_iterator.hpp:331
pcl::ConstCloudIterator::DefaultConstIterator
Definition: cloud_iterator.hpp:190
pcl::ConstCloudIterator::reset
void reset()
Definition: cloud_iterator.hpp:544
pcl::ConstCloudIterator::DefaultConstIterator::getCurrentPointIndex
unsigned getCurrentPointIndex() const override
Definition: cloud_iterator.hpp:223
pcl::ConstCloudIterator::ConstIteratorIdx::ConstIteratorIdx
ConstIteratorIdx(const PointCloud< PointT > &cloud, const PointIndices &indices)
Definition: cloud_iterator.hpp:267
pcl::DefaultIterator::getCurrentIndex
unsigned getCurrentIndex() const
Definition: cloud_iterator.hpp:88
pcl::IteratorIdx::IteratorIdx
IteratorIdx(PointCloud< PointT > &cloud, const PointIndices &indices)
Definition: cloud_iterator.hpp:126
pcl::PointIndices
Definition: PointIndices.h:11
pcl::CloudIterator::operator*
PointT & operator*() const
Definition: cloud_iterator.hpp:395
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::ConstCloudIterator::size
std::size_t size() const
Size of the range the iterator is going through.
Definition: cloud_iterator.hpp:537
pcl::DefaultIterator::getCurrentPointIndex
unsigned getCurrentPointIndex() const
Definition: cloud_iterator.hpp:83
pcl::DefaultIterator::~DefaultIterator
~DefaultIterator()
Definition: cloud_iterator.hpp:59
pcl::CloudIterator::~CloudIterator
~CloudIterator()
Definition: cloud_iterator.hpp:374
pcl::DefaultIterator
Definition: cloud_iterator.hpp:50
pcl::ConstCloudIterator::ConstIteratorIdx::isValid
bool isValid() const override
Definition: cloud_iterator.hpp:317
pcl::ConstCloudIterator::ConstIteratorIdx::size
std::size_t size() const override
Definition: cloud_iterator.hpp:307
pcl::PointCloud::const_iterator
typename VectorType::const_iterator const_iterator
Definition: point_cloud.h:419
pcl::IteratorIdx::getCurrentPointIndex
unsigned getCurrentPointIndex() const
Definition: cloud_iterator.hpp:155
pcl::CloudIterator::getCurrentPointIndex
unsigned getCurrentPointIndex() const
Definition: cloud_iterator.hpp:409
pcl::CloudIterator::reset
void reset()
Definition: cloud_iterator.hpp:430
pcl::DefaultIterator::size
std::size_t size() const
Definition: cloud_iterator.hpp:93
pcl::ConstCloudIterator
Iterator class for point clouds with or without given indices.
Definition: cloud_iterator.h:120
pcl::DefaultIterator::reset
void reset()
Definition: cloud_iterator.hpp:98
pcl::DefaultIterator::DefaultIterator
DefaultIterator(PointCloud< PointT > &cloud)
Definition: cloud_iterator.hpp:53
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:89
pcl::ConstCloudIterator::~ConstCloudIterator
~ConstCloudIterator()
Definition: cloud_iterator.hpp:488
pcl::DefaultIterator::operator*
PointT & operator*() const
Definition: cloud_iterator.hpp:73
pcl::CloudIterator::operator->
PointT * operator->() const
Definition: cloud_iterator.hpp:402
pcl::PointCloud::iterator
typename VectorType::iterator iterator
Definition: point_cloud.h:418
pcl::DefaultIterator::operator++
void operator++()
Definition: cloud_iterator.hpp:63
pcl::ConstCloudIterator::getCurrentIndex
unsigned getCurrentIndex() const
Definition: cloud_iterator.hpp:530
pcl::IteratorIdx::~IteratorIdx
virtual ~IteratorIdx()
Definition: cloud_iterator.hpp:133
pcl::IteratorIdx::operator*
PointT & operator*() const
Definition: cloud_iterator.hpp:145