~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/tools/sequence/seqroi_tool.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include <stdio.h>
  2 #include <sys/param.h>
  3 #include <string.h>   
  4 #include <tina/sys.h>
  5 #include <tina/sysfuncs.h>
  6 #include <tina/math.h>  
  7 #include <tina/mathfuncs.h>
  8 #include <tina/vision.h>
  9 #include <tina/visionfuncs.h>
 10 #include <tina/file.h>
 11 #include <tina/filefuncs.h>
 12 #include <tina/file_gen.h>
 13 #include <tina/tv.h>
 14 #include <tina/tvfuncs.h>
 15 #include <tina/toolsfuncs.h>
 16 #include <tina/tw_Xfuncs.h>
 17 #include <tina/seqvoi.h>
 18 #include <tina/seqvoifuncs.h>
 19 #include <tina/draw.h>
 20 #include <tina/drawfuncs.h>
 21 #include <tina/seqoral.h>
 22 
 23 #define VOXELS       450
 24 
 25 extern void update_view(void);
 26 static char fbasename[64] = "";
 27 static char directory[64] = "";
 28 static void *pdir=NULL, *pfile=NULL;
 29 static Bool news = true;
 30 static void threed_init(Tv * tv);
 31 static Vec3 centre = {Vec3_id};
 32 static double radius = 1000.0;
 33 static double zscale = 4.0;
 34 static Br_imstack * ims = NULL;
 35 static void im_nvoi_choice_proc(int val);
 36 static int nvoi_cons = 0;
 37 
 38 Spline2 *spline2_interpolate_ddlist(int type, List * points);
 39 
 40 /*static int oldvoi = 0;*/
 41 /*static Tstring *tstr = NULL;*/
 42 
 43 extern void redraw_splines(int slice_no, Tv * tv);
 44 int choose_colour(int vol_no);
 45 
 46 static List *points = NULL;
 47 
 48 
 49 Br_imstack *Br_imstack_get(void)
 50 {
 51   return ims;
 52 }
 53 
 54 void Br_imstack_set(Br_imstack * im)
 55 {
 56   ims = im;
 57 }
 58 
 59 static void threed_init(Tv * tv)
 60 {
 61     Vec3    aim = {Vec3_id};
 62     Vec3    down = {Vec3_id};
 63     double   pscale;
 64     List   *geom = threed_geom_get();
 65 
 66     (void) tv_set_zoomlevel(tv, ZOOM3);
 67 
 68     if (geom == NULL)
 69         return;
 70 
 71     geom_hull_get(geom, &centre, &radius);
 72     pscale = 3.0;
 73     aim = vec3_ez();
 74     down = vec3_ey();
 75     tv_camera3(tv, centre, radius, pscale, aim, down);
 76 }
 77 
 78 
 79 static void initialise_seq_ims(void)
 80 {
 81 /* include all the stuff to do the initialisation of the slice */
 82   int lz=0, uz=0, file_type;
 83   Imrect *seq_im=NULL;
 84   Vartype type;/* should type be int??*/
 85   double zscale;
 86   Vec3 *iscale = NULL;
 87   Sequence *data = NULL;
 88   /* get sequence image data and store as imstack, 
 89      AND specify the voi you want to use */
 90   Br_imstack *ims = Br_imstack_get();
 91 
 92   if (ims==NULL)
 93     {
 94 
 95        seq_imstack_free(ims); /* Not really nesc anymore..*/ 
 96       
 97       /*Look at the current imrect. 
 98         find out what sort it is */
 99       
100       if ((seq_im=(Imrect *)seq_image_get())!=NULL)
101         {
102           type=seq_im->vtype;
103           
104           data=(Sequence *)get_seq_ptr();
105           file_type=get_seq_filechoice();
106           /* Get the zscale */
107           /*iscale=(Vec3 *)seq_input(data, file_type);*/
108           if (prop_get(seq_im->props,VOXELS)!=NULL)
109             {    
110               iscale = (Vec3 *)prop_get(seq_im->props,VOXELS);
111             }
112           
113           zscale=iscale->el[2];
114           /* get the uz and lz values from the sequence data*/
115           lz=data->seq_start;
116           uz=data->seq_end+1; /* so that end of seq is accounted for in seq_imstack
117                                  make */
118           
119           /* get the type from the sequence stuff */
120           ims=seq_imstack_make(type, lz, uz, zscale);
121           
122           /* Assigning the slices of the imstack- start at 0 */
123           
124           /*ims->nvoi = 0;*/
125 
126           Br_imstack_set(ims);
127           ims->nvoi = nvoi_get();
128          
129         }
130     }
131 }
132 
133 
134 
135 static void spline_draw_proc(void)
136 {
137   initialise_seq_ims();
138   tv_set_mouse((Tv *)seq_tv_get(), spline2_periodic_draw_mouse());
139   (void) tv_set_activity((Tv *)seq_tv_get(), MOUSE);
140 }
141         
142 static void spline_edit_proc(void)
143 {
144   initialise_seq_ims();
145   tv_set_mouse((Tv *)seq_tv_get(), spline2_edit_mouse());
146   (void) tv_set_activity((Tv *)seq_tv_get(), MOUSE);
147 }
148 
149 void nvoi_set(int nvoi)
150 {
151   nvoi_cons = nvoi; 
152 }
153 
154 int nvoi_get(void)
155 {
156   return(nvoi_cons);  
157 }
158 
159 void seq_nvoi_set(int nvoi)
160 {
161   Br_imstack *ims = Br_imstack_get();
162   if (ims==NULL)
163     return;
164   ims->nvoi=nvoi;
165 
166 }
167 
168 
169 static void im_nvoi_choice_proc(int val)
170 {
171     if (val < NVOI)
172       {
173         seq_nvoi_set(val);
174         nvoi_set(val);
175       }
176     draw_new_threed();
177     redraw_splines(get_slice_no(), (Tv *)seq_tv_get());
178 }
179 
180 static void interp_all_proc(void)
181 {
182 /*    int lz = seq_lz_get();
183     int uz = seq_uz_get(); 
184     seq_interpolate_all_splines_between(lz, uz);
185     redraw_all();*/
186 }
187 
188 
189 static void voi_empty_proc(void)
190 {
191   
192   int z, oldz = seq_z_get();
193   Br_imstack *ims = Br_imstack_get();
194 
195   if (ims == NULL)
196     return;
197   for (z = ims->lz; z < ims->uz; z++)
198     {
199       seq_z_set(z);
200       seq_voi_empty();
201     }   
202   seq_z_set(oldz);
203   tv_repaint((Tv *)seq_tv_get());
204   update_view();
205   redraw_splines(get_slice_no(), (Tv *)seq_tv_get());
206 }
207 
208 static void voi_empty_all_proc(void)
209 {
210   int i, z, oldz = seq_z_get();
211   Br_imstack *ims = Br_imstack_get();
212 
213   if (ims == NULL)
214     return;
215   for (z = ims->lz; z < ims->uz; z++)
216     {
217       seq_z_set(z);
218       for (i=0; i<NVOI; i++)
219         {
220           seq_voi_empty();
221         }   
222     }
223 
224   seq_imstack_free(ims);
225   Br_imstack_set(NULL);
226 
227   seq_z_set(oldz);
228   tv_repaint((Tv *)seq_tv_get());
229   update_view();
230   redraw_splines(get_slice_no(), (Tv *)seq_tv_get()); 
231 
232 }
233 
234 static void print_area_proc()
235 {
236   Br_imstack *ims = Br_imstack_get();
237   Tstring *string = NULL;
238   float area;
239  
240   if(ims == NULL)
241     return;
242 
243   seq_z_set(get_slice_no());
244 
245   string = str2_of_spline2(VOI(ims)->spline);
246   area = str2_area(string);
247   format("Area of voi %i: %f\n", ims->nvoi, area);
248 }
249 
250   
251 
252 double seq_all_vois_area()
253 {
254   Br_imstack *ims = Br_imstack_get();
255   Tstring *string = NULL;
256   int z, zold, vol_no=ims->nvoi;
257   double total=0.0;
258   
259   if (ims == NULL)
260     return(0.0);
261   
262   zold = seq_z_get();
263   
264   for (z = ims->lz; z < ims->uz; z++)
265     {
266       seq_z_set(z);
267       
268       string = str2_of_spline2(VOI(ims)->spline);
269       if (string!=NULL) total += str2_area(string);
270     }
271   seq_z_set(zold);
272   return(total);
273 }
274 
275 void seq_voi_print(FILE *fp)
276 {
277   int oldz, z, vol_no;
278   Spline2 *spline = NULL;
279   Br_imstack *ims = Br_imstack_get();
280 
281   if (ims == NULL)
282     return;
283   oldz = seq_z_get();
284   for (z = ims->lz; z < ims->uz; z++)
285     {
286       seq_z_set(z);
287       /* vol_no=ims->nvoi;*/
288       
289       if ((spline = VOI(ims)->spline) != NULL)
290         {
291           fprintf(fp,"slicez %d \n", z);
292           spline2_print(fp, spline);
293         } 
294     }
295   seq_z_set(oldz);
296 }
297 
298 static void print_voi_proc()
299 {   
300     char fullname[128];
301     char svoi[8];
302     FILE *fp=NULL;
303     Br_imstack *ims = Br_imstack_get();
304     if (ims == NULL)
305       return;
306 
307     sprintf(svoi,"voi%d\0",seq_nvoi_get());
308     string_append(fullname, directory,"/", fbasename, ".", svoi, NULL);
309     fp = fopen(fullname, "w");
310     if(fp!=NULL)
311     {
312        fprintf(fp,"%s %4.2f \n",fbasename,seq_all_vois_area());
313        if(fp!=NULL) seq_voi_print(fp);
314        fclose(fp);
315     }
316 
317 }   
318 
319 void seq_voi_read(FILE *fp)
320 {
321   int oldz, z, num, vol_no;
322   char temp[24];
323   Spline2 *spline = NULL;
324   Br_imstack *ims = Br_imstack_get();
325   Sequence *data = NULL;
326   int start, end;
327   
328   if ((data = (Sequence *)get_seq_ptr())==NULL)
329     return;
330   
331   start = ims->lz;
332   end = ims->uz;
333 
334   if (ims == NULL)
335     return;
336   oldz = seq_z_get();
337   num = fscanf(fp,"%*s %d",&z);
338   while (num > 0)
339     {
340       spline = spline2_read(fp);
341       if ((z>=start) && (z<end))
342         {
343           
344           seq_z_set(z);
345 
346           
347           /*  vol_no=ims->nvoi;*/
348           if (VOI(ims)->spline != NULL)
349             {
350               seq_voi_empty();
351             }
352           seq_spline_set(spline);
353           
354           VOI(ims)->string = (Tstring *)str_list_from_tstr(spline, z);
355           
356         }
357       else
358         {
359           if (z < start)
360             format("\nWarning: spline slice %d lower than sequence start", z);
361           if (z > end)
362             format("\nWarning: spline slice %d higher than sequence end", z);
363         }
364       
365       
366       num = fscanf(fp,"%*s %d",&z);
367     } 
368  
369   seq_z_set(oldz);
370   
371 }
372 
373 static void read_voi_proc()
374 {
375     char fullname[128];
376     char svoi[8];
377     FILE *fp=NULL;
378     Br_imstack *ims = Br_imstack_get();
379 
380     if (((Imrect *)seq_image_get())==NULL)
381       return; /* mjs just to make sure it doen't crash */
382     
383     initialise_seq_ims();
384     spline_draw_proc();
385 
386     sprintf(svoi,"voi%d\0",seq_nvoi_get());
387     string_append(fullname, directory,"/", fbasename, ".", svoi, NULL);
388     fp = fopen(fullname, "r");
389     if(fp!=NULL)
390     {
391        fscanf(fp,"%*s %*s");
392        seq_voi_read(fp);
393        fclose(fp);
394     }
395    
396     redraw_splines(get_slice_no(), (Tv *)seq_tv_get());
397     update_view();
398 
399 }
400 
401 
402 static void tv_choice_proc(int val)
403 {
404 tv_set_next(threed_tv());
405 }
406 
407 
408 static Vec3 map(Vec2 p, double *z)
409 {
410   return (vec3(vec2_x(p), vec2_y(p), *z));
411 }
412 
413 extern void update_view(void)
414 {
415   List *list=NULL;
416   Tv *tv3d=threed_tv_get();
417 
418   list=(List *)update_string_list();
419   threed_geom_null();
420   threed_geom_set(list);
421   tv_repaint(tv3d);
422   
423 }
424 
425 static void import_proc(void)
426 {
427   Spline2 *spline=NULL;
428   Tv *tv3d=NULL;
429   List *line_list=NULL, *end=NULL;
430   Tstring *str_line_list=NULL, *str=NULL;
431   List *line_str_line_list=NULL, *ptr3=NULL;
432   Sequence *data=NULL;
433   
434   Line3 *line_3d=NULL;
435   int i=0, slice_no=0, vol_no, j;
436   double z_coord; 
437   Tv *tv=NULL;  
438   List *input_ROI=NULL, *lptr=NULL/*, *start, *end*/;
439   Ipos pos;
440   Br_imstack *ims = Br_imstack_get();
441 
442 
443   if (ims == NULL)
444     return;
445 
446   /*vol_no=ims->nvoi;*/
447 
448   tv=(Tv *)seq_tv_get(); 
449   if((input_ROI=(List *)tv_poly_get())==NULL)
450     return;
451 
452   /* make the points list, then put in spline_save_periodic*/
453   
454   if (news)
455     {
456       dd_list_rm(points, rfree);
457       points = NULL;
458       news = false;
459     }
460   for (lptr=input_ROI; lptr!=NULL; lptr=lptr->next)
461     {
462       points = dd_ref_addtostart(points, vec2_make(*(Vec2 *)lptr->to), VEC2);
463     }
464  
465   tv=(Tv *)seq_tv_get();
466   
467   spline = spline2_interpolate_ddlist(SPLINE_PERIODIC, points);
468   news=true;
469   slice_no=get_slice_no();
470   seq_z_set(slice_no);
471   if (VOI(ims)->spline != NULL)
472     {
473       seq_voi_empty();
474     }
475   
476   
477   seq_spline_set(spline);
478   tv_repaint(tv);   
479   redraw_splines(slice_no, tv);
480   tv3d=threed_tv_get();
481   
482   VOI(ims)->string = (Tstring *)str_list_from_tstr(spline, slice_no);
483   threed_geom_null();
484   line_str_line_list=(List *)update_string_list();
485   threed_geom_set(line_str_line_list);
486   tv_repaint(tv3d);
487 }
488 
489 static void export_proc()
490 {
491   Tv *tv;
492   int slice_no, n, i;
493   Spline2 *spline=NULL;
494   Vec2 p;
495   Vec2 *vptr=NULL;
496   List *exp_list=NULL;
497   Br_imstack *ims = Br_imstack_get();
498 
499   if (ims == NULL)
500     return;
501   
502   tv=imcalc_tv_get();
503   
504   slice_no=get_slice_no();
505   seq_z_set(slice_no);
506 
507   if ((spline = VOI(ims)->spline) == NULL)
508     {
509       format("There's no spline in this volume of interest\n");
510      
511       return;
512     }
513 
514   n = spline->n;
515 
516   for (i = 0; i < n; i++)
517     {
518       p = spline2_eval(spline, (double) i);
519       vptr = vec2_make(p);
520       exp_list = dd_ref_addtostart(exp_list, vptr, VEC2);
521       /*exp_list = dd_list_addtoend(exp_list, (dd_link_alloc(vptr, VEC2)));*/
522     }
523 
524   tv_reset_draw(tv);
525   tv_repaint(tv); 
526   tv_poly_set(exp_list);
527   tv_set_color(tv, red);
528   tv_poly_draw(tv);
529 
530 }
531 
532 
533 
534 tv_proc()
535 {
536   threed_tv_set(threed_tv_make());
537   tv_set_next(threed_tv());
538 }
539 
540 
541 static void scan_proc(void)
542 {
543   scan_files(directory, fbasename);
544   tw_sglobal_reset(pdir);
545   tw_sglobal_reset(pfile);
546 }
547 
548 void    seqroi_tool(int x, int y)
549 {
550   
551   static void *tool=NULL;
552   
553   
554   if (tool)
555     {
556       tw_show_tool(tool);
557       return;
558     }
559   
560   initialise_seq_ims();
561 
562   tv_proc();
563   
564   tool=(void *)tw_tool("SeqROItool", x, y);
565   
566   tw_choice("Tv : ", tv_choice_proc, 0, "3D rep", NULL);
567   tw_help_button("seqroi");
568   tw_newrow();
569    
570   tw_button("Draw Spline", spline_draw_proc, NULL);
571   tw_label("  mark | create | remove    ");
572   tw_newrow();
573   
574   tw_button("Edit Spline", spline_edit_proc,NULL);
575   tw_label("   move | insert | delete");
576   tw_newrow();
577     tw_button("Import from poly", import_proc, NULL);
578   tw_button("Export to imcalc", export_proc, NULL);
579   tw_button("Area", print_area_proc, NULL);
580   tw_newrow();
581    
582   tw_label(" File Management ");
583   tw_newrow();
584   pdir = (void *) tw_sglobal("Directory: ", directory, 40);
585   tw_button("scan", scan_proc, NULL);
586   tw_newrow();
587   pfile = (void *)tw_sglobal("Filename: ", fbasename, 40);
588   tw_newrow();
589   
590   tw_choice("VOL: ", im_nvoi_choice_proc, 0,
591             "", "1", "2", "3", "4", "5", NULL);
592   tw_newrow();    
593   /*tw_button("Interpolate VOL", interp_all_proc, NULL);*/ 
594   tw_button("Empty VOL ", voi_empty_proc, NULL);
595   tw_button("Empty all VOL ", voi_empty_all_proc, NULL);
596   tw_newrow();
597   tw_button("print vol", print_voi_proc, NULL);
598   tw_button("read vol", read_voi_proc, NULL);
599   tw_newrow();
600   
601   
602  tw_end_tool();
603  
604  
605 }
606 
607 
608 
609 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.