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

Linux Cross Reference
Tina5/tina-tools/tinatool/tlbase/tlbaseSeq_roitool.c

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

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/tlbase/tlbaseSeq_roitool.c,v $
 37  * Date    :  $Date: 2005/02/07 23:29:19 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: tlbaseSeq_roitool.c,v 1.5 2005/02/07 23:29:19 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes :    
 44  *
 45  *********
 46 */
 47 
 48 #include "tlbaseSeq_roitool.h"
 49 
 50 #if HAVE_CONFIG_H
 51 #include <config.h>
 52 #endif
 53 
 54 
 55 #include <stdio.h>
 56 #include <sys/param.h>
 57 #include <string.h>   
 58 
 59 #include <tina/sys/sysDef.h>
 60 #include <tina/sys/sysPro.h>
 61 #include <tina/math/mathDef.h>
 62 #include <tina/math/mathPro.h>
 63 #include <tina/geometry/geomDef.h>
 64 #include <tina/geometry/geomPro.h>
 65 #include <tina/image/imgDef.h>
 66 #include <tina/image/imgPro.h>
 67 
 68 #include <tinatool/draw/drawDef.h>
 69 #include <tinatool/draw/drawPro.h>
 70 #include <tinatool/wdgts/wdgtsDef.h>
 71 #include <tinatool/wdgts/wdgtsPro.h>
 72 #include <tinatool/tlbase/tlbase_InfrDef.h>
 73 #include <tinatool/tlbase/tlbase_InfrPro.h>
 74 #include <tinatool/tlbase/tlbase_SeqDef.h>
 75 #include <tinatool/tlbase/tlbase_MonoPro.h>
 76 #include <tinatool/tlbase/tlbase_ImcPro.h>
 77 #include <tinatool/tlbase/tlbase_SterPro.h>
 78 
 79 #include <tinatool/tlbase/tlbaseSeq_tool.h>
 80 #include <tinatool/tlbase/tlbaseSeq_spline.h>
 81 #include <tinatool/tlbase/tlbaseSeq_funcs.h>
 82 #include <tinatool/tlbase/tlbaseSeq_draw.h>
 83 #include <tinatool/tlbase/tlbaseSeq_disp.h>
 84 #include <tinatool/tlbase/tlbaseSeq_tv.h>
 85 #include <tinatool/tlbase/tlbaseSeq_image.h>
 86 
 87 static char fbasename[64] = "";
 88 static char directory[64] = "";
 89 static void *pdir=NULL, *pfile=NULL;
 90 static Bool news = true;
 91 static Vec3 centre = {Vec3_id};
 92 static double radius = 1000.0;
 93 static double zscale = 4.0;
 94 static Br_imstack * ims = NULL;
 95 static int nvoi_cons = 0;
 96 
 97 /*static int oldvoi = 0;*/
 98 /*static Tstring *tstr = NULL;*/
 99 
100 static List *points = NULL;
101 
102 static void threed_init(Tv * tv)
103 {
104     Vec3    aim = {Vec3_id};
105     Vec3    down = {Vec3_id};
106     double   pscale;
107     List   *geom = threed_geom_get();
108 
109     (void) tv_set_zoomlevel(tv, ZOOM3);
110 
111     if (geom == NULL)
112         return;
113 
114     geom_hull_get(geom, &centre, &radius);
115     pscale = 3.0;
116     aim = vec3_ez();
117     down = vec3_ey();
118     tv_camera3(tv, centre, radius, pscale, aim, down);
119 }
120 
121 static void im_nvoi_choice_proc(int val)
122 {
123     if (val < NVOI)
124       {
125         seq_nvoi_set(val);
126         nvoi_set(val);
127       }
128     draw_new_threed();
129     redraw_splines(get_slice_no(), seq_tv_get());
130 }
131 
132 Br_imstack *Br_imstack_get(void)
133 {
134   return ims;
135 }
136 
137 void Br_imstack_set(Br_imstack * im)
138 {
139   ims = im;
140 }
141 
142 
143 
144 
145 static void initialise_seq_ims(void)
146 {
147 /* include all the stuff to do the initialisation of the slice */
148   int lz=0, uz=0, file_type;
149   Imrect *seq_im=NULL;
150   Vartype type;/* should type be int??*/
151   double zscale;
152   Vec3 *iscale = NULL;
153   Sequence *data = NULL;
154   /* get sequence image data and store as imstack, 
155      AND specify the voi you want to use */
156   Br_imstack *ims = Br_imstack_get();
157 
158   if (ims==NULL)
159     {
160 
161        seq_imstack_free(ims); /* Not really nesc anymore..*/ 
162       
163       /*Look at the current imrect. 
164         find out what sort it is */
165       
166       if ((seq_im=seq_image_get())!=NULL)
167         {
168           type=seq_im->vtype;
169           
170           data=seq_get_current();
171           file_type=get_seq_filechoice();
172           /* Get the zscale */
173           
174           iscale = seq_dim_to_vec3();
175           
176           zscale=iscale->el[2];
177           /* get the uz and lz values from the sequence data*/
178           lz=data->offset;
179           uz=get_seq_end()+1; /* so that end of seq is accounted for in seq_imstack
180                                  make */
181           
182           /* get the type from the sequence stuff */
183           ims=seq_imstack_make(type, lz, uz, zscale);
184           
185           /* Assigning the slices of the imstack- start at 0 */
186           
187           /*ims->nvoi = 0;*/
188 
189           Br_imstack_set(ims);
190           ims->nvoi = nvoi_get();
191          
192         }
193     }
194 }
195 
196 
197 
198 static void spline_draw_proc(void)
199 {
200   initialise_seq_ims();
201   tv_set_mouse(seq_tv_get(), spline2_periodic_draw_mouse());
202   tv_set_activity(seq_tv_get(), MOUSE);
203 }
204         
205 static void spline_edit_proc(void)
206 {
207   initialise_seq_ims();
208   tv_set_mouse(seq_tv_get(), spline2_edit_mouse());
209   tv_set_activity(seq_tv_get(), MOUSE);
210 }
211 
212 void nvoi_set(int nvoi)
213 {
214   nvoi_cons = nvoi; 
215 }
216 
217 int nvoi_get(void)
218 {
219   return(nvoi_cons);  
220 }
221 
222 void seq_nvoi_set(int nvoi)
223 {
224   Br_imstack *ims = Br_imstack_get();
225   if (ims==NULL)
226     return;
227   ims->nvoi=nvoi;
228 
229 }
230 
231 
232 
233 
234 static void interp_all_proc(void)
235 {
236 /*    int lz = seq_lz_get();
237     int uz = seq_uz_get(); 
238     seq_interpolate_all_splines_between(lz, uz);
239     redraw_all();*/
240 }
241 
242 
243 static void voi_empty_proc(void)
244 {
245   
246   int z, oldz = seq_z_get();
247   Br_imstack *ims = Br_imstack_get();
248 
249   if (ims == NULL)
250     return;
251   for (z = ims->lz; z < ims->uz; z++)
252     {
253       seq_z_set(z);
254       seq_voi_empty();
255     }   
256   seq_z_set(oldz);
257   tv_repaint(seq_tv_get());
258   update_view();
259   redraw_splines(get_slice_no(), seq_tv_get());
260 }
261 
262 static void voi_empty_all_proc(void)
263 {
264   int i, z, oldz = seq_z_get();
265   Br_imstack *ims = Br_imstack_get();
266 
267   if (ims == NULL)
268     return;
269   for (z = ims->lz; z < ims->uz; z++)
270     {
271       seq_z_set(z);
272       for (i=0; i<NVOI; i++)
273         {
274           seq_voi_empty();
275         }   
276     }
277 
278   seq_imstack_free(ims);
279   Br_imstack_set(NULL);
280 
281   initialise_seq_ims();
282 
283   seq_z_set(oldz);
284   tv_repaint(seq_tv_get());
285   update_view();
286   redraw_splines(get_slice_no(), seq_tv_get()); 
287   
288 
289 }
290 
291 static void print_area_proc()
292 {
293   Br_imstack *ims = Br_imstack_get();
294   Tstring *string = NULL;
295   float area;
296  
297   if(ims == NULL)
298     return;
299 
300   seq_z_set(get_slice_no());
301 
302   string = str2_of_spline2(VOI(ims)->spline);
303   area = str2_area(string);
304   format("Area of voi %i: %f\n", ims->nvoi, area);
305 }
306 
307   
308 
309 double seq_all_vois_area()
310 {
311   Br_imstack *ims = Br_imstack_get();
312   Tstring *string = NULL;
313   int z, zold, vol_no=ims->nvoi;
314   double total=0.0;
315   
316   if (ims == NULL)
317     return(0.0);
318   
319   zold = seq_z_get();
320   
321   for (z = ims->lz; z < ims->uz; z++)
322     {
323       seq_z_set(z);
324       
325       string = str2_of_spline2(VOI(ims)->spline);
326       if (string!=NULL) total += str2_area(string);
327     }
328   seq_z_set(zold);
329   return(total);
330 }
331 
332 void seq_voi_print(FILE *fp)
333 {
334   int oldz, z, vol_no;
335   Spline2 *spline = NULL;
336   Br_imstack *ims = Br_imstack_get();
337 
338   if (ims == NULL)
339     return;
340   oldz = seq_z_get();
341   for (z = ims->lz; z < ims->uz; z++)
342     {
343       seq_z_set(z);
344       /* vol_no=ims->nvoi;*/
345       
346       if ((spline = VOI(ims)->spline) != NULL)
347         {
348           fprintf(fp,"slicez %d \n", z);
349           spline2_print(fp, spline);
350         } 
351     }
352   seq_z_set(oldz);
353 }
354 
355 static void print_voi_proc()
356 {   
357     char fullname[128];
358     char svoi[8];
359     FILE *fp=NULL;
360     Br_imstack *ims = Br_imstack_get();
361     if (ims == NULL)
362       return;
363 
364     sprintf(svoi,"voi%d\0",seq_nvoi_get());
365     string_append(fullname, directory,"/", fbasename, ".", svoi, NULL);
366     fp = fopen(fullname, "w");
367     if(fp!=NULL)
368     {
369        fprintf(fp,"%s %4.2f \n",fbasename,seq_all_vois_area());
370        if(fp!=NULL) seq_voi_print(fp);
371        fclose(fp);
372     }
373 
374 }   
375 
376 void seq_voi_read(FILE *fp)
377 {
378   int oldz, z, num, vol_no;
379   char temp[24];
380   Spline2 *spline = NULL;
381   Br_imstack *ims = Br_imstack_get();
382   Sequence *data = NULL;
383   int start, end;
384   
385   if ((data = seq_get_current())==NULL)
386     return;
387   
388   start = ims->lz;
389   end = ims->uz;
390 
391   if (ims == NULL)
392     return;
393   oldz = seq_z_get();
394   num = fscanf(fp,"%*s %d",&z);
395   while (num > 0)
396     {
397       spline = spline2_read(fp);
398       if ((z>=start) && (z<end))
399         {
400           
401           seq_z_set(z);
402 
403           
404           /*  vol_no=ims->nvoi;*/
405           if (VOI(ims)->spline != NULL)
406             {
407               seq_voi_empty();
408             }
409           seq_spline_set(spline);
410           
411           VOI(ims)->string = str_list_from_tstr(spline, z);
412           
413         }
414       else
415         {
416           if (z < start)
417             format("\nWarning: spline slice %d lower than sequence start", z);
418           if (z > end)
419             format("\nWarning: spline slice %d higher than sequence end", z);
420         }
421       
422       
423       num = fscanf(fp,"%*s %d",&z);
424     } 
425  
426   seq_z_set(oldz);
427   
428 }
429 
430 static void read_voi_proc()
431 {
432     char fullname[128];
433     char svoi[8];
434     FILE *fp=NULL;
435     Br_imstack *ims = Br_imstack_get();
436 
437     if ((seq_image_get())==NULL)
438       return; /* mjs just to make sure it doen't crash */
439     
440     initialise_seq_ims();
441     spline_draw_proc();
442 
443     sprintf(svoi,"voi%d\0",seq_nvoi_get());
444     string_append(fullname, directory,"/", fbasename, ".", svoi, NULL);
445     fp = fopen(fullname, "r");
446     if(fp!=NULL)
447     {
448        fscanf(fp,"%*s %*s");
449        seq_voi_read(fp);
450        fclose(fp);
451     }
452    
453     redraw_splines(get_slice_no(), seq_tv_get());
454     update_view();
455 
456 }
457 
458 
459 static void tv_choice_proc(int val)
460 {
461 tv_set_next(threed_tv());
462 }
463 
464 
465 static Vec3 map(Vec2 p, double *z)
466 {
467   return (vec3(vec2_x(p), vec2_y(p), *z));
468 }
469 
470 void update_view(void)
471 {
472   List *list=NULL;
473   Tv *tv3d=threed_tv_get();
474 
475   list=update_string_list();
476   threed_geom_null();
477   threed_geom_set(list);
478   tv_repaint(tv3d);
479   
480 }
481 
482 static void import_proc(void)
483 {
484   Spline2 *spline=NULL;
485   Tv *tv3d=NULL;
486   List *line_list=NULL, *end=NULL;
487   Tstring *str_line_list=NULL, *str=NULL;
488   List *line_str_line_list=NULL, *ptr3=NULL;
489   Sequence *data=NULL;
490   
491   Line3 *line_3d=NULL;
492   int i=0, slice_no=0, vol_no, j;
493   double z_coord; 
494   Tv *tv=NULL;  
495   List *input_ROI=NULL, *lptr=NULL/*, *start, *end*/;
496   Ipos pos;
497   Br_imstack *ims = Br_imstack_get();
498 
499 
500   if (ims == NULL)
501     return;
502 
503   /*vol_no=ims->nvoi;*/
504 
505   tv=seq_tv_get(); 
506   if((input_ROI=tv_poly_get())==NULL)
507     return;
508 
509   /* make the points list, then put in spline_save_periodic*/
510   
511   if (news)
512     {
513       dd_list_rm(points, rfree);
514       points = NULL;
515       news = false;
516     }
517   for (lptr=input_ROI; lptr!=NULL; lptr=lptr->next)
518     {
519       points = dd_ref_addtostart(points, vec2_make(*(Vec2 *)lptr->to), VEC2);
520     }
521  
522   tv=seq_tv_get();
523   
524   spline = spline2_interpolate_ddlist(SPLINE_PERIODIC, points);
525   news=true;
526   slice_no=get_slice_no();
527   seq_z_set(slice_no);
528   if (VOI(ims)->spline != NULL)
529     {
530       seq_voi_empty();
531     }
532   
533   
534   seq_spline_set(spline);
535   tv_repaint(tv);   
536   redraw_splines(slice_no, tv);
537   tv3d=threed_tv_get();
538   
539   VOI(ims)->string = str_list_from_tstr(spline, slice_no);
540   threed_geom_null();
541   line_str_line_list=update_string_list();
542   threed_geom_set(line_str_line_list);
543   tv_repaint(tv3d);
544 }
545 
546 static void export_proc()
547 {
548   Tv *tv;
549   int slice_no, n, i;
550   Spline2 *spline=NULL;
551   Vec2 p;
552   Vec2 *vptr=NULL;
553   List *exp_list=NULL;
554   Br_imstack *ims = Br_imstack_get();
555 
556   if (ims == NULL)
557     return;
558   
559   tv=imcalc_tv_get();
560   
561   slice_no=get_slice_no();
562   seq_z_set(slice_no);
563 
564   if ((spline = VOI(ims)->spline) == NULL)
565     {
566       format("There's no spline in this volume of interest\n");
567      
568       return;
569     }
570 
571   n = spline->n;
572 
573   for (i = 0; i < n; i++)
574     {
575       p = spline2_eval(spline, (double) i);
576       vptr = vec2_make(p);
577       exp_list = dd_ref_addtostart(exp_list, vptr, VEC2);
578       /*exp_list = dd_list_addtoend(exp_list, (dd_link_alloc(vptr, VEC2)));*/
579     }
580 
581   tv_reset_draw(tv);
582   tv_repaint(tv); 
583   tv_poly_set(exp_list);
584   tv_set_color(tv, red);
585   tv_poly_draw(tv);
586 
587 }
588 
589 
590 /* Made static to avoid clashes with tv_proc in other tools PAB 2/2/2005 */
591 
592 static void tv_proc()
593 {
594   threed_tv_set(threed_tv_make());
595   tv_set_next(threed_tv());
596 }
597 
598 
599 static void scan_proc(void)
600 {
601   scan_files(directory, fbasename);
602   tw_sglobal_reset(pdir);
603   tw_sglobal_reset(pfile);
604 }
605 
606 void    seqroi_tool(int x, int y)
607 {
608   
609   static void *tool=NULL;
610   
611   
612   if (tool)
613     {
614       tw_show_tool(tool);
615       return;
616     }
617   
618   initialise_seq_ims();
619 
620   tv_proc();
621   
622   tool=(void *)tw_tool("SeqROItool", x, y);
623   
624   tw_choice("Tv : ", tv_choice_proc, 0, "3D rep", NULL);
625   tw_help_button("seqroi");
626   tw_newrow();
627    
628   tw_button("Draw Spline", spline_draw_proc, NULL);
629   tw_label("  mark | create | remove    ");
630   tw_newrow();
631   
632   tw_button("Edit Spline", spline_edit_proc,NULL);
633   tw_label("   move | insert | delete");
634   tw_newrow();
635     tw_button("Import from poly", import_proc, NULL);
636   tw_button("Export to imcalc", export_proc, NULL);
637   tw_button("Area", print_area_proc, NULL);
638   tw_newrow();
639    
640   tw_label(" File Management ");
641   tw_newrow();
642   pdir = (void *) tw_sglobal("Directory: ", directory, 40);
643   tw_button("scan", scan_proc, NULL);
644   tw_newrow();
645   pfile = (void *)tw_sglobal("Filename: ", fbasename, 40);
646   tw_newrow();
647   
648   tw_choice("VOL: ", im_nvoi_choice_proc, 0,
649             "", "1", "2", "3", "4", "5", NULL);
650   tw_newrow();    
651   /*tw_button("Interpolate VOL", interp_all_proc, NULL);*/ 
652   tw_button("Empty VOL ", voi_empty_proc, NULL);
653   tw_button("Empty all VOL ", voi_empty_all_proc, NULL);
654   tw_newrow();
655   tw_button("print vol", print_voi_proc, NULL);
656   tw_button("read vol", read_voi_proc, NULL);
657   tw_newrow();
658   
659   
660  tw_end_tool();
661  
662  
663 }
664 
665 
666 
667 

~ [ 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.