N-Dimensional loop and iterator

Luis Ibanez ibanez at cs.unc.edu
Wed May 3 14:01:16 EDT 2000


Hi,

Here are some possible ways to wrap up an N-Dimensional loop.
(or rather the N (1-D loops ) needed to go through an image
of arbitrary dimension).

They were generated each one evolving from the previous,... so if
you are in a hurry, just take the last one (NDLoop4) which is
the one in better shape.  It is however interesting to take a look
at the others because for some, the only difference is how the
syntax is presented to the final user (that is, the programer using
the tool kit).  For example, the second method looks very nice
from the point of view of the user's code.

As they are just a proof of concept, the real Insight classes are
not being used here. (an Index defined just as a vector<unsigned int>
is used instead of itkIndex) but coupling should not be a problem.

=========================================

This is the core of the approach:
Let's say that "begin" and "end" are the indexes of the extreme
coordinates of the region we want to cover. We assume that
the coordinate corresponding to Index[0] is the inner loop,
and the on associated with Index[N-1] is the slowest changing
index.


typedef  vector<unsigned int> Index;
 Index index = begin;

  const unsigned int N = begin.size();

  bool remaining = true;
  while ( remaining )
  {

    // Go through the inner loop
    for ( unsigned int i=0; i<end[0]; i++ )
    {
      action( index );  // do something in the inner loop
      index[0]++;       // advance
    }

    // Update higher dimensions
    for ( unsigned int n=1; n<N; n++ )
    {
      index[n-1]=0;
      index[n]++;
      remaining = false;
      if ( index[n] < end[n] )
      {
        remaining = true;
        break;
      }
    }

  }


========================================

"action()" is the placeholder for whatever we want to do
at each pixel/voxel position as we go through the image.

It is implemented in different ways in the four examples.
In out last version it is a class that defines a method

" void Run(const Index &)"

The final use is somthing like

------------------------------------------------------------------
Method 1
// declare the Functor class
myAction action;   
// "ForAll" implement the NDloop
ForAll < myIndexType, myAction >  myLoop;  
// here we execute the NDloop
myLoop.DoInRegion ( begin , end , action ); 
-------------------------------------------------------------------
Method 2
DoInRegion ( begin , end , printIndex() );
// DoInRegion is a templated function that executes the
// Functor class"printIndex()" at each pixel between "begin"
// and end "indexes"
-------------------------------------------------------------------
Method 3
 Do<printIndex> myLoop;   // "Do" is a class templated on the Functor
myLoop.ExecuteInRegion ( begin , end ); // "Do" imnplemets the NDLoop
---------------------------------------------------------------------
Method 4
Do< printIndex >::ForEachInRegion ( begin , end );
// "Do" is still a templated class, and the static method
// "ForEachInRegion" implements the NDLoop.
---------------------------------------------------------------------

In all the cases "printIndex" is just a dummy method that print
the index, to verify that we are covering all the image.  It will
be replaced with the real action to perform at each location.



All of them compile and run fine with g++ 2.95.1 and with
Visual C++ 6.0




Luis

--
______________________________________________________________________

Luis Ibanez
Research Assistant Professor - Division of Neurosurgery
University of North Carolina at Chapel Hill
Sitterson Hall, CB#3175, Chapel Hill, NC 27599
email : ibanez at cs.unc.edu       home  : http://www.cs.unc.edu/~ibanez
phone : (919)-843-9961          fax   : (919)-962-1799
______________________________________________________________________
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkNDLoop3.tgz
Type: application/x-unknown-content-type-winzip
Size: 903 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/insight-developers/attachments/20000503/0ae05e37/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkNDLoop2.tgz
Type: application/x-unknown-content-type-winzip
Size: 867 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/insight-developers/attachments/20000503/0ae05e37/attachment-0001.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkNDLoop1.tgz
Type: application/x-unknown-content-type-winzip
Size: 909 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/insight-developers/attachments/20000503/0ae05e37/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkNDLoop4.tgz
Type: application/x-unknown-content-type-winzip
Size: 900 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/insight-developers/attachments/20000503/0ae05e37/attachment-0003.bin>


More information about the Insight-developers mailing list