-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanatomy_menu.c
More file actions
586 lines (531 loc) · 16.5 KB
/
anatomy_menu.c
File metadata and controls
586 lines (531 loc) · 16.5 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#if defined(__GNUC__)
#ident "University of Edinburgh $Id$"
#else
static char _anatomy_menu_c[] = "University of Edinburgh $Id$";
#endif
/*!
* \file anatomy_menu.c
* \author Richard Baldock
* \date April 2009
* \version $Id$
* \par
* Address:
* MRC Human Genetics Unit,
* MRC Institute of Genetics and Molecular Medicine,
* University of Edinburgh,
* Western General Hospital,
* Edinburgh, EH4 2XU, UK.
* \par
* Copyright (C), [2012],
* The University Court of the University of Edinburgh,
* Old College, Edinburgh, UK.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
* \ingroup MAPaint
* \brief
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <MAPaint.h>
static MenuItem anatomy_menu_itemsP[] = { /* anatomy_menu items */
{NULL},
};
MenuItem *anatomy_menu_items = &(anatomy_menu_itemsP[0]);
typedef struct _AnatObjItem {
String shortName;
String fullName;
WlzObject *domain;
} AnatObjItem;
static AnatObjItem *anatObjItems=NULL;
static int numAnatObjItems=0;
static int maxAnatObjItems=0;
String getAnatShortNameFromCoord(
int k,
int l,
int p)
{
String name="no anatomy";
int i, i_min, vol, vol_min;
int domainIndexList[32];
int numDomains=0;
WlzErrorNum errNum=WLZ_ERR_NONE;
/* edit this so that the name returned is of the
smallest domain that contains the coordinate */
for(i=0; i < numAnatObjItems; i++){
if(anatObjItems[i].domain &&
WlzInsideDomain(anatObjItems[i].domain,
(double) p, (double) l, (double) k, &errNum) ){
domainIndexList[numDomains++] = i;
}
}
if( numDomains ){
if( numDomains == 1) {
return anatObjItems[domainIndexList[0]].shortName;
}
else {
i_min = 0;
vol_min = (int) WlzVolume(anatObjItems[domainIndexList[0]].domain, NULL);
for(i=1; i < numDomains; i++){
vol = (int) WlzVolume(anatObjItems[domainIndexList[i]].domain, NULL);
if( vol < vol_min ){
i_min = i;
}
}
return anatObjItems[domainIndexList[i_min]].shortName;
}
}
if( errNum != WLZ_ERR_NONE ){
MAPaintReportWlzError(globals.topl, "getAnatShortNameFromCoord", errNum);
}
return name;
}
String getAnatFullNameFromCoord(
int k,
int l,
int p)
{
String name="no anatomy";
int i, i_min, vol, vol_min;
int domainIndexList[32];
int numDomains=0;
WlzErrorNum errNum=WLZ_ERR_NONE;
/* edit this so that the name returned is of the
smallest domain that contains the coordinate */
for(i=0; i < numAnatObjItems; i++){
if(anatObjItems[i].domain &&
WlzInsideDomain(anatObjItems[i].domain,
(double) p, (double) l, (double) k, &errNum) ){
domainIndexList[numDomains++] = i;
}
}
if( numDomains ){
if( numDomains == 1) {
return anatObjItems[domainIndexList[0]].fullName;
}
else {
i_min = 0;
vol_min = (int) WlzVolume(anatObjItems[domainIndexList[0]].domain, NULL);
for(i=1; i < numDomains; i++){
vol = (int) WlzVolume(anatObjItems[domainIndexList[i]].domain, NULL);
if( vol < vol_min ){
vol_min = vol;
i_min = i;
}
}
return anatObjItems[domainIndexList[i_min]].fullName;
}
}
if( errNum != WLZ_ERR_NONE ){
MAPaintReportWlzError(globals.topl, "getAnatFullNameFromCoord", errNum);
}
return name;
}
void read_anatomy_cb(
Widget widget,
XtPointer client_data,
XtPointer call_data)
{
String fileStr = (String) client_data;
FILE *fp;
WlzObject *anatObj;
/* open the object file and read the domain */
if( fileStr ){
if( (fp = fopen(fileStr, "r")) == NULL ){
HGU_XmUserError(globals.topl,
"Read Anatomy Domain:\n"
" Failed to open the file for the\n"
" selected anatomy component. Please\n"
" report this error to the Mouse\n"
" Atlas team",
XmDIALOG_FULL_APPLICATION_MODAL);
return;
}
}
else {
HGU_XmUserError(globals.topl,
"Read Anatomy Domain:\n"
" NULL file string for the \n"
" selected anatomy component. Please\n"
" report this error to the Mouse\n"
" Atlas team",
XmDIALOG_FULL_APPLICATION_MODAL);
return;
}
if( (anatObj = WlzReadObj(fp, NULL)) == NULL ){
HGU_XmUserError(globals.topl,
"Read Anatomy Domain:\n"
" Can't read the domain for the \n"
" selected anatomy component. Please\n"
" report this error to the Mouse\n"
" Atlas team",
XmDIALOG_FULL_APPLICATION_MODAL);
(void) fclose(fp);
return;
}
(void) fclose(fp);
/* note for the anatomy domain we assume that the user wishes
to append the domain to the current therefore do not
check for replace and do not check for overlaps */
/* set the anatomy as a domain */
if( globals.current_domain > globals.cmapstruct->num_overlays )
{
set_grey_values_from_domain( anatObj, globals.obj,
globals.current_col, 255);
}
else
{
set_grey_values_from_domain(anatObj, globals.obj, globals.current_col,
globals.cmapstruct->ovly_planes);
}
WlzFreeObj(anatObj);
if( paint_key ){
display_view_cb(widget, (XtPointer) paint_key, call_data);
}
display_all_views_cb(widget, (XtPointer) NULL, call_data);
/* display the object in 3D */
if( globals.domain_display_list[globals.current_domain] ){
glDeleteLists(globals.domain_display_list[globals.current_domain], 1);
globals.domain_display_list[globals.current_domain] = 0;
}
else {
Widget toggle;
/* set the toggle to be checked */
if( (toggle = XtNameToWidget(globals.topl,
"*domain_menu*threed_display_domain")) ){
XtVaSetValues(toggle, XmNset, True, NULL);
}
}
if((anatObj = get_domain_from_object(globals.obj, globals.current_domain))){
MAOpenGLDisplayDomainIndex(anatObj, globals.current_domain);
MAOpenGLDrawScene( globals.canvas );
WlzFreeObj(anatObj);
}
/* set the domain menu entry - don't do this */
/* sscanf(XtName(widget), "%s", anatStr);
set_domain_menu_entry(globals.current_domain, anatStr);*/
return;
}
void freeAnatomyMenuItems(
MenuItem *menu_items)
{
int itemIndex;
/* termination condition */
if( menu_items == NULL ){
return;
}
/* for each item remove the sub-items */
itemIndex = 0;
while( menu_items[itemIndex].name ){
/* should be able to free the name but the widget
is still using it */
AlcFree(menu_items[itemIndex].name);
/* free any sub-items */
if( menu_items[itemIndex].subitems ){
freeAnatomyMenuItems(menu_items[itemIndex].subitems);
}
itemIndex++;
}
/* free this array */
AlcFree((void *) menu_items);
return;
}
MenuItem *createAnatomyMenuItems(
String name,
String dirStr)
{
MenuItem *items=NULL;
struct dirent *dp;
DIR *dfd;
int i, numDirs, dirIndex;
String newDirStr;
String objFileStr=NULL, infoFileStr=NULL;
String str;
struct stat statBuf;
char strBuf[256];
/* open the directory and count the subdirectories */
if((dirStr == NULL) || ((dfd = opendir(dirStr)) == NULL) ){
return NULL;
}
numDirs = 0;
while( (dp = readdir(dfd)) != NULL ){
if((strcmp(dp->d_name, ".") == 0) ||
(strcmp(dp->d_name, "..") == 0)){
continue;
}
newDirStr = (String) AlcMalloc(strlen(dp->d_name)+strlen(dirStr)+2);
sprintf(newDirStr, "%s/%s", dirStr, dp->d_name);
if((stat(newDirStr, &statBuf) == -1) ||
#ifdef LINUX2 /* don't understand why I need to do this */
((statBuf.st_mode&__S_IFMT) != __S_IFDIR) ){
#else
((statBuf.st_mode&S_IFMT) != S_IFDIR) ){
#endif /* LINUX2 */
/* its an ordinary file - check for object or info file */
/* this also collects the domain files and puts them on a list */
sprintf(strBuf, "%s.wlz", name);
if( strcmp(dp->d_name, strBuf) == 0 ){
FILE *fp;
objFileStr = newDirStr;
/* add it to the list */
if( numAnatObjItems >= maxAnatObjItems ){
maxAnatObjItems += 64;
anatObjItems = (AnatObjItem *)
AlcRealloc(anatObjItems, sizeof(AnatObjItem)*maxAnatObjItems);
}
/* copy the short name and strip underlines */
anatObjItems[numAnatObjItems].shortName = AlcStrDup(name);
for(i=0; i < strlen(name); i++){
if(name[i] == '_'){
anatObjItems[numAnatObjItems].shortName[i] = ' ';
}
}
/* find and copy the fullname */
anatObjItems[numAnatObjItems].fullName =
AlcStrDup(strstr(dirStr, "anatomy") + 8);
for(i=0; i < strlen(anatObjItems[numAnatObjItems].fullName); i++){
if(anatObjItems[numAnatObjItems].fullName[i] == '_'){
anatObjItems[numAnatObjItems].fullName[i] = ' ';
}
}
if( (fp = fopen(objFileStr, "rb")) ){
anatObjItems[numAnatObjItems].domain = WlzReadObj(fp, NULL);
(void) fclose(fp);
}
numAnatObjItems++;
continue;
}
sprintf(strBuf, "%s.html", name);
if( strcmp(dp->d_name, strBuf) == 0 ){
infoFileStr = newDirStr;
continue;
}
}
AlcFree((void *) newDirStr);
numDirs++;
}
rewinddir(dfd);
/* allocate space */
items = (MenuItem *) AlcMalloc(sizeof(MenuItem) * (numDirs+4));
/* fill in items, calling recursively as required, attaching
object filename if present */
dirIndex = 0;
/* the information button */
if( infoFileStr ){
str = (String) AlcMalloc(strlen(name) + 8);
sprintf(str, "%s info.", name);
items[dirIndex].name = str;
items[dirIndex].wclass = &xmPushButtonGadgetClass;
items[dirIndex].mnemonic = '\0';
items[dirIndex].accelerator = NULL;
items[dirIndex].accel_text = NULL;
items[dirIndex].item_set = False;
items[dirIndex].callback = NULL;
items[dirIndex].callback_data = infoFileStr;
items[dirIndex].help_callback = NULL;
items[dirIndex].help_callback_data = NULL;
items[dirIndex].tear_off_model = XmTEAR_OFF_DISABLED;
items[dirIndex].radio_behavior = False;
items[dirIndex].always_one = False;
items[dirIndex].subitems = NULL;
dirIndex++;
}
/* select the woolz domain */
if( objFileStr ){
str = (String) AlcMalloc(strlen(name) + 8);
sprintf(str, "%s domain", name);
items[dirIndex].name = str;
items[dirIndex].wclass = &xmPushButtonGadgetClass;
items[dirIndex].mnemonic = '\0';
items[dirIndex].accelerator = NULL;
items[dirIndex].accel_text = NULL;
items[dirIndex].item_set = False;
items[dirIndex].callback = read_anatomy_cb;
items[dirIndex].callback_data = objFileStr;
items[dirIndex].help_callback = NULL;
items[dirIndex].help_callback_data = NULL;
items[dirIndex].tear_off_model = XmTEAR_OFF_DISABLED;
items[dirIndex].radio_behavior = False;
items[dirIndex].always_one = False;
items[dirIndex].subitems = NULL;
dirIndex++;
}
/* dividing line */
if( dirIndex && numDirs){
items[dirIndex].name = AlcStrDup("");
items[dirIndex].wclass = &xmSeparatorGadgetClass;
items[dirIndex].mnemonic = '\0';
items[dirIndex].accelerator = NULL;
items[dirIndex].accel_text = NULL;
items[dirIndex].item_set = False;
items[dirIndex].callback = NULL;
items[dirIndex].callback_data = NULL;
items[dirIndex].help_callback = NULL;
items[dirIndex].help_callback_data = NULL;
items[dirIndex].tear_off_model = XmTEAR_OFF_DISABLED;
items[dirIndex].radio_behavior = False;
items[dirIndex].always_one = False;
items[dirIndex].subitems = NULL;
dirIndex++;
}
/* now the subdirectories */
while( (dp = readdir(dfd)) != NULL ){
if((strcmp(dp->d_name, ".") == 0) ||
(strcmp(dp->d_name, "..") == 0)){
continue;
}
newDirStr = (String) AlcMalloc(strlen(dp->d_name)+strlen(dirStr)+2);
sprintf(newDirStr, "%s/%s", dirStr, dp->d_name);
if((stat(newDirStr, &statBuf) == -1) ||
#ifdef LINUX2 /* don't understand why I need to do this */
((statBuf.st_mode&__S_IFMT) != __S_IFDIR) ){
#else
((statBuf.st_mode&S_IFMT) != S_IFDIR) ){
#endif /* LINUX2 */
/* its an ordinary file */
AlcFree((void *) newDirStr);
continue;
}
str = (String) AlcMalloc(strlen(dp->d_name) + 1);
sprintf(str, "%s", dp->d_name);
items[dirIndex].name = str;
items[dirIndex].wclass = &xmCascadeButtonWidgetClass;
items[dirIndex].mnemonic = '\0';
items[dirIndex].accelerator = NULL;
items[dirIndex].accel_text = NULL;
items[dirIndex].item_set = False;
items[dirIndex].callback = NULL;
items[dirIndex].callback_data = NULL;
items[dirIndex].help_callback = NULL;
items[dirIndex].help_callback_data = NULL;
items[dirIndex].tear_off_model = XmTEAR_OFF_DISABLED;
items[dirIndex].radio_behavior = False;
items[dirIndex].always_one = False;
items[dirIndex].subitems = createAnatomyMenuItems(dp->d_name,
newDirStr);
dirIndex++;
AlcFree((void *) newDirStr);
}
/* set the last item to NULL */
items[dirIndex].name = NULL;
/* close directory and free space */
closedir(dfd);
if( dirIndex == 0 ){
AlcFree((void *) items);
items = NULL;
}
return items;
}
/* set the anatomy menu by creating a hierarchy of MenuItem
structures then call HGU_XmBuildPulldownMenu */
void set_anatomy_menu(
Widget topl)
{
struct dirent *dp;
DIR *dfd;
Widget menu_button, menu;
MenuItem *menu_items;
String theilerDir, dirStr=NULL;
/* open the current theiler stage directory */
if( globals.theiler_stage && globals.base_theiler_dir ){
theilerDir = (String) AlcMalloc(strlen(globals.base_theiler_dir) +
strlen(globals.theiler_stage) + 2);
sprintf(theilerDir, "%s/%s", globals.base_theiler_dir,
globals.theiler_stage);
if( (dfd = opendir(theilerDir)) == NULL ){
/* failed - should give a warning */
AlcFree((void *) theilerDir);
return;
}
}
else {
return;
}
/* check the anatomy directory exists it is defined to have the name
"anatomy" and must be a directory */
while( (dp = readdir(dfd)) != NULL ){
if( strcmp(dp->d_name, "anatomy") == 0 ){
struct stat statBuf;
dirStr = (String) AlcMalloc(strlen(theilerDir) +
strlen(dp->d_name) + 2);
(void) sprintf(dirStr, "%s/%s", theilerDir, dp->d_name);
/* check it is a directory */
if((stat(dirStr, &statBuf) == -1) ||
#ifdef LINUX2 /* don't understand why I need to do this */
((statBuf.st_mode&__S_IFMT) != __S_IFDIR) ){
#else
((statBuf.st_mode&S_IFMT) != S_IFDIR) ){
#endif /* LINUX2 */
AlcFree((void *) dirStr);
dp = NULL;
}
break;
}
}
if( dp == NULL ){
/* no anatomy directory, should give warning */
closedir(dfd);
AlcFree((void *) theilerDir);
return;
}
/* clear any existing domains on the list */
while( numAnatObjItems > 0){
numAnatObjItems--;
if( anatObjItems[numAnatObjItems].shortName ){
AlcFree(anatObjItems[numAnatObjItems].shortName);
anatObjItems[numAnatObjItems].shortName = NULL;
}
if( anatObjItems[numAnatObjItems].fullName ){
AlcFree(anatObjItems[numAnatObjItems].fullName);
anatObjItems[numAnatObjItems].fullName = NULL;
}
if( anatObjItems[numAnatObjItems].domain ){
WlzFreeObj(anatObjItems[numAnatObjItems].domain);
anatObjItems[numAnatObjItems].domain = NULL;
}
}
/* recursively create the menu item arrays */
menu_button = XtNameToWidget(topl, "*menubar*anatomy_menu");
menu_items = createAnatomyMenuItems(dp->d_name, dirStr);
/* if the menu_button has a sub-menu then destroy it */
XtVaGetValues(menu_button, XmNsubMenuId, &menu, NULL);
if( menu ){
XtDestroyWidget( menu );
}
/* create the menu */
menu = HGU_XmBuildPulldownMenu(menu_button, XmTEAR_OFF_DISABLED,
False, False, menu_items);
XtSetSensitive(menu_button, True);
/* free space */
freeAnatomyMenuItems(menu_items);
AlcFree((void *) dirStr);
AlcFree((void *) theilerDir);
closedir(dfd);
return;
}
/* anatomy_menu initialisation procedure */
void anatomy_menu_init(
Widget topl)
{
/* do nothing here because if the theiler stage has been set
then the theiler_menu_init procedure will have set up the anatomy
menu via set_theiler_stage_cb */
return;
}