[Insight-developers] itk::ImageRegionIterator::operator--()

Miller, James V (CRD) millerjv@crd.ge.com
Sat, 23 Jun 2001 21:31:46 -0400


At long last, I added operator--() to the ImageRegionIterator class.

The main hurdle in adding operator--() was what would be the syntax for walking backwards through a
region.  

Luis had proposed that we replace the Begin()/End() routines with GotoBegin()/GotoEnd()

I hestitate to use GotoBegin()/GotoEnd() because it changes the meaning of what End is.  In STL,
iterators walk half-open intervals. begin() points to the first element, end() points one element
past the end.  To avoid confusion, I think we should stick with the half-open interval concept.
Currently, our Begin() points to the beginning of a region and End() points one pixel past the end of
the region.

To walk forward through a region, we use a loop like:

it = it.Begin();
for ( ; !it.IsAtEnd(); ++it)
{
// do something at each iterator position
}


To walk backward through a region, we should use a loop like:

it = it.End();
do
{
--it;

//do something at each iterator position

}
while (!it.IsAtBegin());

This last loop assumes the region is not empty.

Another option is add "reverse iterators" to itk.  A reverse iterator for a region would have Begin()
return an iterator to the last pixel in the region and End() return an iterator one pixel before the
first pixel in the region.  operator++ and operator-- would have the reverse semantics.  To walk a
region backwards using reverse iterators, the code would look like:

itk::ImageReverseRegionIterator<itk::PhysicalImage<itk::Vector<unsigned short, 5>, 3> > reverseIt(o3,
region);

reverseIt = reverseIt.Begin();
for (; !reverseIt.IsAtEnd(); ++reverseIt)
{
// do something at each iterator position.
}