[Insight-users] Iterator moving in reverse

Luis Ibanez luis . ibanez at kitware . com
Thu, 18 Dec 2003 21:14:21 -0500


Hi Radhika,

You may want to look the Chapter on
Iterators in the SoftwareGuide

   http://www . itk . org/ItkSoftwareGuide . pdf

Chapter 11, pdf-page 481.

---

About your questions:

1) In your code, when you move an iterator
    to the End position, this is one past the
    last pixel. Just as in STL where the end()
    method of the containers return a position
    one past the last element.

    The solution in your case is to use a
    do-while statement like:

    it.GoToEnd();
    do {
       --it;       //  first move back
       it.Get();
       it.GetIndex();
       }  while( !it.IsAtBegin() )



2) Reverse iteration is a property missing
    in the family of IteratorWithIndex.
    There is no technical reason for not
    implementing reverse iteration on them,
    it simply has not been done.

    You are welcome to log this issue as
    a feature request in the bug tracker

      http://www . itk . org/Bug/

----

As an alternative, you can use the
reverse iterator:
ImageRegionReverseConstIterator
http://www . itk . org/Insight/Doxygen/html/classitk_1_1ImageRegionReverseConstIterator . html

Notice that for this iterator, the operator++
will make it walk backwards.


Regards,


   Luis



--------------------------------------
Radhika Sivaramakrishna wrote:
> Hi Luis,
>  
> 1)I was trying to write a simple iterator loop to find the starting and 
> ending non-zero slices of a given binary image. Here is part of the code 
> for that. It worked correctly for identifying the starting slice but not 
> the ending slice, though I cannot figure out why.
>  
> #include "itkImageRegionIterator.h"
>  
> const unsigned int Dimension = 3;
>  
>  typedef unsigned char   PixelType;
>  
>  typedef itk::Image< PixelType,  Dimension >  ImageType;
> typedef itk::ImageRegionIterator< ImageType > IteratorType3D;
>  
> IteratorType3D 
> startandendit(readermask->GetOutput(),readermask->GetOutput()->GetBufferedRegion());
>   startandendit.GoToBegin();
>   while (startandendit.Get() == 0 && !startandendit.IsAtEnd())
>   ++startandendit;
>   if (!startandendit.IsAtEnd())
>   {
>  sstart = startandendit.GetIndex()[2];
>  startandendit.GoToEnd();
>  while (startandendit.Get() == 0 && !startandendit.IsAtBegin())
>   --startandendit;
>  send = startandendit.GetIndex()[2];
>   }
>   else
>   {
>    std::cout << " Empty image " << std::endl;
>    exit(1);
>   }
>   std::cout << " Start and end slices " << sstart << " " << send << 
> std::endl;
>  
> 2) Also, I found I could not use ImageRegionIteratorwithIndex for this 
> since it did not have the functions IsAtBegin and GoToEnd. Why is that.