Filter:   InfoImg
download neighbour.c
Language: C
License: GPL
Copyright: (C) 1998 Gloria Bueno (C) 1998,1999,2000,2001,2002 Yohann Sulaiman. (C) 2002 Free Software Foundation, Inc.
LOC: 187
Project Info
MultiSlice RTP Environment(yhs)
Server: SourceForge
Type: cvs
...eForge\y\yhs\yhs\root\root\
   app_glob.c
   command_run.c
   command_slice.c
   ctomass.c
   edge.c
   editing_tools.c
   exit.c
   FILE_ID.DIZ
   file_viewer.c
   file2.raw
   filters.c
   glxdino.c
   glxmotif.c
   glxsimple.c
   gpl.html
   icon.h
   initapp.c
   inject.c
   label.c
   log.h
   logmen.h
   logo.h
   main.h
   makefile.32
   makefile.64
   makefile.f32
   measure.c
   menu.c
   multislice.c
   neighbour.c
   open.c
   open.old.c
   paperplane.c
   print.c
   program1.c
   region.c
   region_ops.c
   save.c
   slice.c
   split_merge.c
   textfun.c
   version.c
   viewslice.c
   wals.study
   watershed.c
   xbutton.c
   xmenu.c
   xwins.h
   xwinutil.c
   ys_ext.c
   zoom.c
   zoom2.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  /*

MultiSlice RTP Environment
(C) 1998,1999,2000,2001,2002 Yohann Sulaiman.
(C) 1998 Gloria Bueno
(C) 2002  Free Software Foundation, Inc. 


    This file is part of the source code of the MultiSlice RTP Environment.

    MultiSlice RTP Environment is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    MultiSlice RTP Environment  is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MultiSlice RTP Environment; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


/*----------------------------------------------------------------------*/
/*                                                             	 	*/
/*     File: neighbour.c						*/
/*									*/
/*     CT Image Application Program					*/
/*									*/
/*     OSF/Motif version.						*/
/*									*/
/*     V1.0 March 1997							*/
/*									*/
/*----------------------------------------------------------------------*/

#include "main.h"

#define UP    0
#define DOWN  1
#define LEFT  2
#define RIGHT 3


struct neighbour {
    char reg_number;
    short carry;
};
typedef struct neighbour NEIGH;


NEIGH neigh[4][4] = {
    {'2',1,'2',0,'1',1,'1',0},
    {'3',1,'3',0,'0',0,'0',1},
    {'0',0,'0',1,'3',1,'3',0},
    {'1',0,'1',1,'2',0,'2',1}
};


/* Function prototypes */
void get_neighbours(PATHPTR *list, TREEPTR root, char c[]);
static void analize(PATHPTR *list, TREEPTR root, char path[], short where);
static void include_sub_neigh(PATHPTR *list,char path[],
			TREEPTR node, short level,short where);

static void get_4_neighbours(char c[],char v1[],char v2[],char v3[],char v4[]);
static void analize_label(char *c, short i, short pos);

void initialize_label(char c[], int start);
void copy_label (char *dest, char *source);
void write_label(char *c);
void insert(PATHPTR *list, char n[]);

extern TREEPTR get_leaf(char path[]);
extern void insert_path(PATHPTR *first, PATHPTR *end, char n[]);
extern short yes;
PATHPTR end=NULL;



/* Function definitions */

/*----------------------------------------------------------------------*/
/* g e t _ n e i g h b o u r s						*/
/*									*/
/* Return the pointer to a list of the neighbours of a given region	*/
/*----------------------------------------------------------------------*/

void get_neighbours(PATHPTR *list, TREEPTR root, char c[])
{
    char neigh1[10],neigh2[10],neigh3[10],neigh4[10];

    get_4_neighbours(c,neigh1,neigh2,neigh3,neigh4);
    analize(list,root,neigh1,UP);
    analize(list,root,neigh2,DOWN);
    analize(list,root,neigh3,LEFT);
    analize(list,root,neigh4,RIGHT);
}



/*----------------------------------------------------------------------*/
/* g e t _ 4 _ n e i g h b o u r s					*/
/*									*/
/* Compute the 4-adjacent neighbours that belong to the same level that	*/
/* the given region							*/
/*----------------------------------------------------------------------*/

void get_4_neighbours(char c[],char v1[],char v2[],char v3[],char v4[])
{
    short length;

    initialize_label(v1,0); initialize_label(v2,0);
    initialize_label(v3,0); initialize_label(v4,0);
    copy_label(v1,c); copy_label(v2,c); copy_label(v3,c); copy_label(v4,c);

    length = strlen(c);
    analize_label(v1,length-1,UP);
    analize_label(v2,length-1,DOWN);
    analize_label(v3,length-1,LEFT);
    analize_label(v4,length-1,RIGHT);
}



/*----------------------------------------------------------------------*/
/* a n a l i z e _ l a b e l						*/
/*									*/
/* Modifies the label according to the technique presented in the	*/
/* report to find the 4-adjacent neighbours				*/
/*----------------------------------------------------------------------*/

void analize_label(char *subc, short i, short pos)
{
    short n;
    char last[2];

    last[1]='\0';
    if (i>=0) {
	last[0] = subc[i];
	n=atoi(last);
	subc[i]=neigh[n][pos].reg_number;
	if (neigh[n][pos].carry) analize_label(subc,i-1,pos);
    } else initialize_label(subc,0);
}



/*----------------------------------------------------------------------*/
/* a n a l i z e							*/
/*									*/
/* Analise the path to reach the neighbour in the same level to find	*/
/* out if the neighbours in that branch are placed in a higher or lower */
/* level in the hierarchy						*/
/*----------------------------------------------------------------------*/

void analize(PATHPTR *list, TREEPTR root, char path[], short where)
{
    short i, length;
    TREEPTR node;

    i=0;
    node=root;
    length = strlen(path);
    if (length>0) {
	while ( (node->region==NULL) && (i<length) ) {
	    switch (path[i]) {
		case '0': node=node->subreg0; break;
		case '1': node=node->subreg1; break;
		case '2': node=node->subreg2; break;
		case '3': node=node->subreg3; break;
	    }
	    i++;
	}
	if (i==length) {
	    if (node->region!=NULL) insert(list,path);
	    else include_sub_neigh(list,path,node,i,where);
	}
	if (i<length) {
	    initialize_label(path,i);
	    insert(list,path);
	}
    }
}



/*----------------------------------------------------------------------*/
/* i n c l u d e _ s u b _ n e i g h					*/
/*									*/
/* Add to the list those neighbours that are placed in a lower level	*/
/* in the hierarchy							*/
/*----------------------------------------------------------------------*/

void include_sub_neigh(PATHPTR *list,char path[],
				TREEPTR node,short level,short where)
{
    TREEPTR node1, node2;
    char neigh1[10], neigh2[10];

    initialize_label(neigh1,0); initialize_label(neigh2,0);
    copy_label(neigh1,path); copy_label(neigh2,path);

    switch (where) {
	case UP :
	    node1=node->subreg2;
	    neigh1[level]='2';
	    if (node1->region==NULL)
			include_sub_neigh(list,neigh1,node1,level+1,where);
	    	else { insert(list,neigh1); initialize_label(neigh1,level+1); }
	    node2=node->subreg3;
	    neigh2[level]='3';
	    if (node2->region==NULL)
			include_sub_neigh(list,neigh2,node2,level+1,where);
		else { insert(list,neigh2); initialize_label(neigh2,level+1); }
	    break;

	case DOWN :
	    node1=node->subreg0;
	    neigh1[level]='0';
	    if (node1->region==NULL)
			include_sub_neigh(list,neigh1,node1,level+1,where);
		else { insert(list,neigh1); initialize_label(neigh1,level+1); }
	    node2=node->subreg1;
	    neigh2[level]='1';
	    if (node2->region==NULL)
			include_sub_neigh(list,neigh2,node2,level+1,where);
		else { insert(list,neigh2); initialize_label(neigh2,level+1); }
	    break;

	case LEFT :
	    node1=node->subreg1;
	    neigh1[level]='1';
	    if (node1->region==NULL)
			include_sub_neigh(list,neigh1,node1,level+1,where);
		else { insert(list,neigh1); initialize_label(neigh1,level+1); }
	    node2=node->subreg3;
	    neigh2[level]='3';
	    if (node2->region==NULL)
			include_sub_neigh(list,neigh2,node2,level+1,where);
		else { insert(list,neigh2); initialize_label(neigh2,level+1); }
	    break;

	case RIGHT :
	    node1=node->subreg0;
	    neigh1[level]='0';
	    if (node1->region==NULL)
			include_sub_neigh(list,neigh1,node1,level+1,where);
		else { insert(list,neigh1); initialize_label(neigh1,level+1); }
	    node2=node->subreg2;
	    neigh2[level]='2';
	    if (node2->region==NULL)
			include_sub_neigh(list,neigh2,node2,level+1,where);
		else { insert(list,neigh2); initialize_label(neigh2,level+1); }
	    break;
    }
}



/*----------------------------------------------------------------------*/
/* c o p y _ l a b e l							*/
/*									*/
/* Copy the source label in the destination label			*/
/*----------------------------------------------------------------------*/

void copy_label(char *dest, char *source)
{
    short len,i,n;
    char num;

    for (i=0; i<10; i++) dest[i]=source[i];
}



/*----------------------------------------------------------------------*/
/* initialize_label							*/
/*									*/
/* Set to null the characters since the position specified till the	*/
/* end of the label								*/
/*----------------------------------------------------------------------*/

void initialize_label(char c[], int start)
{
    short i;

    for (i=start; i<=10; i++) c[i]='\0';
}



/*----------------------------------------------------------------------*/
/* w r i t e _ l a b e l						*/
/*									*/
/* Print the label in the screen					*/
/*----------------------------------------------------------------------*/

void write_label(char *c)
{
    short i, length;

    length=strlen(c);
    for (i=0; i<length; i++)
	fprintf(stderr,"%c",c[i]);
    fprintf(stderr," ");
}



/*----------------------------------------------------------------------*/
/* i n s e r t								*/
/*									*/
/* Insert the label in a list						*/
/*----------------------------------------------------------------------*/

void insert(PATHPTR *list, char n[])
{
    PATHPTR newone, last;


    if (get_leaf(n)->included==yes) 
	/*Add at the end of the list*/
	insert_path(list,&end,n);
    else {

      if (strlen(n)>0) {
	newone = (PATHPTR)malloc(sizeof(PATH));
	initialize_label(newone->path,0);
	copy_label(newone->path,n);
	newone->next = NULL;
    
	if (*list==NULL) 
		*list=end=newone;
	else {
	    newone->next = *list;
	    *list=newone;
	}
      }
    }
}