[Insight-developers] itk::ImageRegionIterator::operator--() [
ReverseIterators]
Miller, James V (CRD)
millerjv@crd.ge.com
Sat, 23 Jun 2001 23:10:31 -0400
To complicate our lives further, I decided to add reverse iterators to itk to see how they look.
Please take a look at their use to see if we would like to keep them in itk. I modified
Testing/Code/Common/itkImageIteratorTest.cxx to use ImageRegionIterators and
ImageRegionReverseIterators exercising both operator++ and operator--
I checked in:
itkImageReverseIterator
itkImageRegionReverseIterator
(can't decide whether the latter should be ImageRegionReverseIterator or ImageReverseRegionIterator)
Anyhow, ImageIterators are bidirectional (see earlier message) and ImageReverseIterators are
bidirectional and they walk a region backwards. So ImageRegionReverseIterator::Begin() returns an
iterator to the last pixel in the region and ImageRegionReverseIterator::End() returns an iterator
that points to one pixel before the region.
To walk a region forward, use an ImageRegionIterator like:
itk::ImageRegionIterator<itk::PhysicalImage<itk::Vector<unsigned short, 5>, 3> > itt(o3,
region);
it = it.Begin();
for (; !it.IsAtEnd(); ++it)
{
// do something at each iterator position.
}
To walk are region backwards, use an ImageRegionReverseIterator like:
itk::ImageRegionReverseIterator<itk::PhysicalImage<itk::Vector<unsigned
short, 5>, 3> > reverseIt(o3,
region);
reverseIt = reverseIt.Begin();
for (; !reverseIt.IsAtEnd(); ++reverseIt)
{
// do something at each iterator position.
}
Notice the only difference in the above code is which iterator is created.
I would suggest algorithms that scan forward through the entire region, then backward use a
combination of Iterators and ReverseIterators.
If an algorithm needs to revisit the previous pixel while scanning a region, both iterators have
defined operator--.
-----Original Message-----
From: Miller, James V (CRD)
To: 'insight-developers@public.kitware.com '
Sent: 6/23/2001 9:31 PM
Subject: [Insight-developers] itk::ImageRegionIterator::operator--()
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.
}
_______________________________________________
Insight-developers mailing list
Insight-developers@public.kitware.com
http://public.kitware.com/mailman/listinfo/insight-developers