N-Dimensional loop and iterator

Josh Cates cates at rolle.engr.utk.edu
Wed May 24 15:36:45 EDT 2000


Hello Insight developers,

Here is a variation on Luis Ibanez's N-D looping code (email from May 3
re:  N-Dimensional loop and iterator) which may be of interest to some.  
I used this approach in an algorithm that iterates one-dimensionally over
a data range (an entire image), but requires knowledge of location in N-D
space.

The only difference between my code and Luis's is that I am using the
iterator instead of the N-D coordinates to control the loop. The
coordinates are a by-product of 1-D array location rather than the
reverse. Its just a shift in perspective, but this "data-centered"
approach may be more appealing for low-level algorithms.

------------------------------------------------------------
L = number of dimensions 
it= image iterator
Location[]= array of L elements holding the current
            coordinates, initialized to (0,0,0...)
N[]= array of L elements representing the  lengths of
     each dimension


for (it=Image.begin(); it<Image.end(); it++)
{

  /*
   ...
   do something to  *it  which is a function of Location
   ...
   */

  for (i=L-1; i>=0; i--)
  {
    Location[i]++;
    if (Location[i]==N[i]) Location[i]=0;
    else break;
  }

}
-------------------------------------------------------------

Some comments:

* It may be more efficient to incorporate a separate FOR loop for the
fastest increasing dimension in which the function on Location takes place
(as in Luis's implementation). I left it out here for the sake of clarity.

* In the context of Insight toolkit, replace the above variable types with
appropriate Insight types

* For arbitrary 1-D regions, calculate the starting Location[] and specify
the iterator range accordingly

* Assumes data is contiguous and Location[0] is the slowest increasing
when moving linearly through the data.



Josh.

+--+--+--+--+--+--+--+--+--+--+--+--
 Josh Cates			
 Dept. of Electrical Engineering	
 University of Tennessee, Knoxville
 Email: jecates at utk.edu
 Phone: (865) 974-0694
 URL:   www.cs.utk.edu/~cates
--+--+--+--+--+--+--+--+--+--+--+---




More information about the Insight-developers mailing list