1 /* ----------------------------- MNI Header -----------------------------------
2 @NAME       : nd_loop.c
3 @DESCRIPTION: File containing routines for doing n-dimensional looping
4 @METHOD     :
5 @GLOBALS    :
6 @CREATED    : March 10, 1994 (Peter Neelin)
7 @MODIFIED   :
8  * $Log: nd_loop.c,v $
9  * Revision 6.4  2008-01-17 02:33:02  rotor
10  *  * removed all rcsids
11  *  * removed a bunch of ^L's that somehow crept in
12  *  * removed old (and outdated) BUGS file
13  *
14  * Revision 6.3  2008/01/12 19:08:14  stever
15  * Add __attribute__ ((unused)) to all rcsid variables.
16  *
17  * Revision 6.2  2004/10/15 13:47:13  bert
18  * Minor changes for Windows compatibility
19  *
20  * Revision 6.1  2002/01/14 21:28:26  neelin
21  * Moved nd_loop, voxel_loop, ParseArgv and time_stamp from ../progs/Proglib
22  * in order to include them in the main minc library.
23  *
24  * Revision 6.1  1999/10/19 14:45:13  neelin
25  * Fixed Log subsitutions for CVS
26  *
27  * Revision 6.0  1997/09/12 13:23:41  neelin
28  * Release of minc version 0.6
29  *
30  * Revision 5.0  1997/08/21  13:24:41  neelin
31  * Release of minc version 0.5
32  *
33  * Revision 4.0  1997/05/07  20:00:50  neelin
34  * Release of minc version 0.4
35  *
36  * Revision 3.0  1995/05/15  19:31:35  neelin
37  * Release of minc version 0.3
38  *
39  * Revision 1.3  1995/02/08  19:31:47  neelin
40  * Moved ARGSUSED statements for irix 5 lint.
41  *
42  * Revision 1.2  1994/12/02  09:19:59  neelin
43  * Added comments to clarify use of routines.
44  *
45  * Revision 1.1  94/12/02  08:40:12  neelin
46  * Initial revision
47  *
48 @COPYRIGHT  :
49               Copyright 1994 Peter Neelin, McConnell Brain Imaging Centre,
50               Montreal Neurological Institute, McGill University.
51               Permission to use, copy, modify, and distribute this
52               software and its documentation for any purpose and without
53               fee is hereby granted, provided that the above copyright
54               notice appear in all copies.  The author and McGill University
55               make no representations about the suitability of this
56               software for any purpose.  It is provided "as is" without
57               express or implied warranty.
58 ---------------------------------------------------------------------------- */
59 
60 #include "minc_private.h"
61 #include "nd_loop.h"
62 
63 /* ----------------------------- MNI Header -----------------------------------
64 @NAME       : nd_begin_looping
65 @INPUT      : start - vector of indices specifying starting subscript
66                  for each dimension
67               ndims - number of dimensions in vector
68 @OUTPUT     : current - vector of indices giving current subscript
69 @RETURNS    : (none)
70 @DESCRIPTION: Sets up current variable for looping
71 @METHOD     :
72 @GLOBALS    :
73 @CALLS      :
74 @CREATED    : October 28, 1994 (Peter Neelin)
75 @MODIFIED   :
76 ---------------------------------------------------------------------------- */
nd_begin_looping(long start[],long current[],int ndims)77 MNCAPI void nd_begin_looping(long start[], long current[], int ndims)
78 {
79    int idim;
80 
81    for (idim=0; idim < ndims; idim++) {
82       current[idim] = start[idim];
83    }
84 }
85 
86 /* ----------------------------- MNI Header -----------------------------------
87 @NAME       : nd_end_of_loop
88 @INPUT      : current - vector of indices giving current subscript
89               end - vector of indices specifying last subscripts plus one
90               ndims - number of dimensions in vector
91 @OUTPUT     : (none)
92 @RETURNS    : TRUE if end of loop.
93 @DESCRIPTION: Tests for end of a multi-dimensional loop.
94 @METHOD     :
95 @GLOBALS    :
96 @CALLS      :
97 @CREATED    : March 10, 1994 (Peter Neelin)
98 @MODIFIED   :
99 ---------------------------------------------------------------------------- */
nd_end_of_loop(long current[],long end[],int ndims)100 MNCAPI int nd_end_of_loop(long current[], long end[], int ndims)
101      /* ARGSUSED */
102 {
103    return (current[0] >= end[0]);
104 }
105 
106 /* ----------------------------- MNI Header -----------------------------------
107 @NAME       : nd_update_current_count
108 @INPUT      : current - vector of indices giving current subscript
109               increment - vector of indices specifying increment
110                  for each dimension
111               end - vector of indices specifying last subscripts plus one
112               ndims - number of dimensions in vector
113 @OUTPUT     : current_count - vector of indices giving count for current
114                  position
115 @RETURNS    : (none)
116 @DESCRIPTION: Sets the count so that we don't go past end
117 @METHOD     :
118 @GLOBALS    :
119 @CALLS      :
120 @CREATED    : October 28, 1994 (Peter Neelin)
121 @MODIFIED   :
122 ---------------------------------------------------------------------------- */
nd_update_current_count(long current[],long increment[],long end[],long current_count[],int ndims)123 MNCAPI void nd_update_current_count(long current[],
124                                     long increment[], long end[],
125                                     long current_count[],
126                                     int ndims)
127 {
128    int idim;
129 
130    for (idim=0; idim < ndims; idim++) {
131       current_count[idim] = increment[idim];
132       if ((current[idim] + current_count[idim]) > end[idim]) {
133          current_count[idim] = end[idim] - current[idim];
134       }
135    }
136 
137 }
138 
139 /* ----------------------------- MNI Header -----------------------------------
140 @NAME       : nd_increment_loop
141 @INPUT      : current - vector of indices giving current subscript
142               start - vector of indices specifying starting subscript
143                  for each dimension
144               increment - vector of indices specifying increment
145                  for each dimension
146               end - vector of indices specifying last subscripts plus one
147               ndims - number of dimensions in vector
148 @OUTPUT     : current - vector of indices giving new subscript
149 @RETURNS    : (none)
150 @DESCRIPTION: Does incrementing for multi-dimensional loop
151 @METHOD     :
152 @GLOBALS    :
153 @CALLS      :
154 @CREATED    : March 10, 1994 (Peter Neelin)
155 @MODIFIED   :
156 ---------------------------------------------------------------------------- */
nd_increment_loop(long current[],long start[],long increment[],long end[],int ndims)157 MNCAPI void nd_increment_loop(long current[],
158                               long start[], long increment[], long end[],
159                               int ndims)
160 {
161    int idim;
162 
163    idim = ndims-1;
164    current[idim] += increment[idim];
165    while ( (idim>0) && (current[idim] >= end[idim])) {
166       current[idim] = start[idim];
167       idim--;
168       current[idim] += increment[idim];
169    }
170 
171 }
172 
173