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

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

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

  1 #include <tina/all_tina.h>
  2 #include <values.h>
  3 #include <tina/seqoral.h>
  4 #include <tina/graphic.h>
  5 #include <tina/seqvoi.h>
  6 #include <tina/seqvoifuncs.h>
  7 
  8 #define DD_VEC2(_ptr) (*(Vec2 *)(_ptr)->to)
  9 
 10 
 11 static Tstring *tstr = NULL;
 12 static Bool newspline = true;
 13 static List *points = NULL;
 14 extern Tstring *str_list_from_tstr(Spline2 *spline, int slice);
 15 Spline2 *seq_spline_get(void);
 16 void seq_spline_changed(void);
 17 static void spline_remove_point(Tv * tv, Ipos pos);
 18 static void spline_insert_point(Tv * tv, Ipos pos);
 19 static void spline_replace_up(Tv * tv, Ipos pos);
 20 static void spline_replace_drag(Tv * tv, Ipos pos);
 21 static void spline_replace_down(Tv * tv, Ipos pos);
 22 extern List *update_string_list(void);
 23 static int iclose;
 24 
 25 void tstring_set(Tstring *tstring)
 26 {
 27   tstr = tstring;
 28 }
 29 
 30 Tstring *tstring_get(void)
 31 {
 32   return tstr;
 33 }
 34 
 35 
 36 static double area(List * points)
 37 {
 38     List *ptr = NULL;
 39     double a = 0.0;
 40 
 41     for (ptr = points; ptr != NULL; ptr = ptr->next)
 42     {
 43         Vec2 v1, v2;
 44         v1 = *((Vec2 *) ptr->to);
 45         if (ptr->next != NULL)
 46             v2 = *((Vec2 *) ptr->next->to);
 47         else
 48             v2 = *((Vec2 *) points->to);
 49         a += vec2_cross(v1, v2);
 50     }
 51     a *= 0.5;
 52     return (a);
 53 }
 54 
 55 int choose_colour(int vol_no)
 56 {
 57   
 58   int colour;
 59   
 60   colour=coffee+vol_no;
 61   return colour;
 62 }
 63 
 64 static void spline_mark_point(Tv * tv, Ipos pos)
 65 {
 66     Vec2 v;
 67     int vol_no;
 68     Br_imstack *ims = (Br_imstack *)Br_imstack_get();
 69 
 70     if (newspline)
 71     {
 72         dd_list_rm(points, rfree);
 73         points = NULL;
 74         newspline = false;
 75     }
 76     v = tv_backproj2(tv, pos);
 77     points = dd_ref_addtostart(points, vec2_make(v), VEC2);
 78     tv_save_draw(tv);
 79     vol_no=ims->nvoi;
 80     tv_set_color(tv, choose_colour(vol_no));
 81     tv_bigdot2(tv, v, 2);
 82     tv_reset_draw(tv);
 83 }
 84 
 85 
 86 
 87 /*
 88 Set B-spline representing active VOI in current slice.
 89 */
 90 void seq_spline_set(Spline2 * spline)
 91 {
 92   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
 93   if (ims != NULL)
 94     svoi_spline_set(VOI(ims), spline);
 95 }
 96 
 97 static Vec3 map(Vec2 p, double *z)
 98 {
 99   return (vec3(vec2_x(p), vec2_y(p), *z));
100 }
101 
102 
103 void seq_string_set(Tstring * string)
104 {
105   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
106   if (ims != NULL)
107     svoi_string_set(VOI(ims), string);
108 }
109 
110 
111 /* function to create ddlist of line3 for drawing strings */
112 
113 List *make_dd_line3(double z_coord)
114 { 
115   List *line_list=NULL, *ptr=NULL, *ptr2=NULL;
116   Line3 *line_3d=NULL;
117   double *z=&z_coord; 
118   Vec2 p1_2d, p2_2d;
119   Vec3 p1_3d, p2_3d;
120   Sequence *data=NULL;
121   Tstring *str=NULL;
122 
123   str = tstring_get();
124    
125   for (ptr=str->start;ptr!=NULL; ptr=ptr->next)
126     {
127       
128       if (ptr==(List *)str->start)
129         {
130           p1_2d=DD_VEC2(ptr);
131           p2_2d=DD_VEC2(str->start);
132           /* Got the vectors - next map to 3d space*/
133           p1_3d=map(p1_2d, z);
134           p2_3d=map(p2_2d, z);
135           
136           line_3d=line3_make(p1_3d, p2_3d, LINE3);
137           line_list=dd_link_alloc(line_3d, LINE3);
138           ptr2=line_list;
139           
140         }
141       
142       else
143         {
144           if (ptr==(List *)str->end)
145             {
146               p1_2d=DD_VEC2(ptr);
147               p2_2d=DD_VEC2(str->start);
148               /* Got the vectors - next map to 3d space*/
149               p1_3d=map(p1_2d, z);
150               p2_3d=map(p2_2d, z);
151               
152               line_3d=line3_make(p1_3d, p2_3d, LINE3);
153               ptr2=dd_ref_addtoend(ptr2, line_3d, LINE3);
154               
155               break;
156             }
157           else
158             {
159               p1_2d=DD_VEC2(ptr);
160               p2_2d=DD_VEC2(ptr->next);
161               /* Got the vectors - next map to 3d space*/
162               p1_3d=map(p1_2d, z);
163               p2_3d=map(p2_2d, z);
164               
165               line_3d=line3_make(p1_3d, p2_3d, LINE3);
166               ptr2=dd_ref_addtoend(ptr2, line_3d, LINE3);
167               
168             }
169         }
170     }
171   return(line_list); 
172 }
173 
174 static void make_prop_list(List *d_ptr, int vol_no, int col)
175 { 
176   Graphic *gr=NULL;
177   Line3 *p_ptr = NULL;
178 
179   if( d_ptr == NULL || d_ptr->to == NULL) 
180      printf("NULL pointer passed to make_prop_list()");
181   (Line3 *)p_ptr=d_ptr->to;
182   if ((gr = prop_get(p_ptr->props,GRAPHIC))==NULL) 
183   {
184        gr=(Graphic *)ralloc(sizeof(Graphic));
185        p_ptr->props = proplist_add(p_ptr->props, (void *)gr, GRAPHIC, rfree);
186   }
187   gr->colour=col;
188 
189 }
190 /* draw_new_threed() determines which slice you are on and 
191    colours the Line3's for that string in red */ 
192 extern void draw_new_threed(void)
193 {
194   int i, col; 
195   List *line_str_line_list=NULL;
196   Sequence *data=(Sequence *)get_seq_ptr();
197   int vol_no, old_vol;
198   Tstring *t_ptr = NULL;
199   List *d_ptr = NULL;
200   Tv *tv3d =(Tv *) threed_tv_get();
201   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
202 
203   if (data == NULL)
204     return;
205 
206   if (ims == NULL)
207     return;
208 
209   old_vol=seq_nvoi_get();
210   
211   for (vol_no=0; vol_no<NVOI; vol_no++)
212     {
213       seq_nvoi_set(vol_no);
214       for (i=ims->lz; i<ims->uz; i++)
215         {
216           seq_z_set(i);
217           if ((ims != NULL ) && ((t_ptr=VOI(ims)->string)!=NULL))
218             {     
219               if (i==data->goto_frame)
220                 {               
221                   for (d_ptr=t_ptr->start; ; d_ptr=d_ptr->next)
222                     {
223                       if (d_ptr==NULL) printf("NULL pointer in draw_new_threed()");
224                       if (vol_no == old_vol)
225                         col = red;
226                       else
227                         col = choose_colour(vol_no) + 6;
228                       make_prop_list(d_ptr, vol_no, col);                     
229                       if (d_ptr->next==NULL)
230                         break;
231                     }
232                 }
233             
234               else
235                 {                 
236                   for (d_ptr=t_ptr->start; ; d_ptr=d_ptr->next)
237                     {
238                       if (d_ptr==NULL) printf("NULL pointer in draw_new_threed()");
239                       col = choose_colour(vol_no);
240                       make_prop_list(d_ptr, vol_no, col);                     
241                       if (d_ptr->next==NULL)
242                         break;
243                     }
244                 }             
245             }     
246         }
247     }
248   tv_repaint(tv3d);
249   seq_nvoi_set(old_vol);
250 }
251 
252 
253 /* update_string_list() takes the tstring of Line3's just created, colours 
254    them red, and adds the tstring to the list of all the strings */ 
255 List *update_string_list(void)
256 {
257   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
258   int i, col; 
259   List *line_str_line_list=NULL, *ptr=NULL;
260   Sequence *data=NULL;
261   int vol_no, old_vol;
262   Tstring *t_ptr = NULL;
263   Graphic *gr=NULL;
264   Line3 *p_ptr=NULL;
265   List *d_ptr=NULL;
266 
267   if (ims == NULL)
268     return(NULL);
269   
270   data=(Sequence *)get_seq_ptr();
271   old_vol=seq_nvoi_get();
272   
273  
274 
275   for (vol_no=0; vol_no<NVOI; vol_no++)
276     {
277       seq_nvoi_set(vol_no);
278       for (i=ims->lz; i<ims->uz; i++)
279         {
280           seq_z_set(i);
281           if ((ims!=NULL) && ((t_ptr=VOI(ims)->string)!=NULL))
282             {
283               if (i==data->goto_frame)
284                 {
285                   
286                   for (d_ptr=t_ptr->start; ; d_ptr=d_ptr->next)
287                     {
288                       if (vol_no == old_vol)
289                         col = red;
290                       else
291                         col = choose_colour(vol_no) + 6;
292                       make_prop_list(d_ptr, vol_no, col);       
293                       if (d_ptr->next==NULL)
294                         break;
295                     }
296                 }
297             
298               else
299                 {
300                   
301                   for (d_ptr=t_ptr->start; ; d_ptr=d_ptr->next)
302                     {
303                       col = choose_colour(vol_no);
304                       make_prop_list(d_ptr, vol_no, col);       
305                       if (d_ptr->next==NULL)
306                         break;
307                     }
308                 }             
309             }     
310           
311          
312           if (line_str_line_list == NULL)
313             {  
314               line_str_line_list=link_alloc(VOI(ims)->string, STRING);
315               ptr=line_str_line_list;   
316             }
317           else
318             {    
319               ptr=ref_addtoend(ptr, VOI(ims)->string, STRING );
320             }     
321         }
322     }
323   seq_nvoi_set(old_vol);
324   return(line_str_line_list);
325 }
326 
327 
328 extern int get_slice_no(void)
329 {
330   Sequence *data=NULL;
331   int slice_no;
332 
333   if ((data=(Sequence *)get_seq_ptr()) != NULL)
334     {
335       slice_no=data->goto_frame;
336       return slice_no;
337     }
338   else 
339     return 0;
340 }
341 
342 /* constructing interpolating spline */
343 static void spline_save_periodic(Tv * tv, Ipos pos)
344 {
345     Spline2 *spline=NULL;
346     Tv *tv3d=NULL;
347     List *line_list=NULL, *end=NULL;
348     Tstring *str_line_list=NULL;
349     List *line_str_line_list=NULL, *ptr3=NULL;
350     Sequence *data=NULL;
351     Br_imstack *ims = (Br_imstack *)Br_imstack_get();
352     Line3 *line_3d=NULL;
353     int i=0, slice_no=0, /*vol_no=ims->nvoi,*/ j;
354     double z_coord; 
355     
356     if(dd_list_length(points) < 3)
357     {
358         error("spline_save_periodic: < 3 points", warning);
359         return;
360     }
361     if (area(points) < 0.0)
362         points = dd_list_reverse(points);
363     spline = spline2_interpolate_ddlist(SPLINE_PERIODIC, points);
364     newspline = true;
365     /*seq_spline_set(spline);  Also frees string*/
366     
367     slice_no=get_slice_no();
368     seq_z_set(slice_no);
369     if (VOI(ims)->spline != NULL)
370       {
371          seq_voi_empty();
372       }
373     
374  
375     seq_spline_set(spline);
376     tv_repaint(tv);   
377     redraw_splines(slice_no, tv);
378     tv3d=threed_tv_get();
379  
380     VOI(ims)->string = str_list_from_tstr(spline, slice_no);
381     threed_geom_null();
382     line_str_line_list=update_string_list();
383     threed_geom_set(line_str_line_list);
384     tv_repaint(tv3d);
385    
386    
387 }
388 
389 extern Tstring *str_list_from_tstr(Spline2 *spline, int slice)
390 {
391   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
392   List *line_list=NULL, *end=NULL;
393   Tstring *str_line_list=NULL, *str=NULL;
394   double z_coord;
395 
396   if ((str=str2_of_spline2(spline))==NULL)
397     return NULL;
398 
399   tstring_set(str);
400 
401   z_coord=ims->slice[slice]->z;
402   line_list=make_dd_line3(z_coord);
403   end=dd_get_end(line_list);
404   str_line_list=str_make(LIST, line_list, end);
405   
406   return str_line_list;
407 }
408 
409 static void spline_rm(Tv * tv, Ipos pos)
410 {
411   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
412 
413   if (ims != NULL && VOI(ims)!=NULL)
414     {
415       seq_z_set(get_slice_no());
416       seq_voi_empty();
417       /*spline2_free(VOI(ims)->spline);
418       VOI(ims)->spline = NULL;
419       str_free(VOI(ims)->string, rfree);
420       VOI(ims)->string = NULL;*/
421     }
422 
423   tv_repaint(tv);
424   update_view();  
425   redraw_splines(get_slice_no(), tv);
426     
427 }
428 
429 Tv_mouse spline2_periodic_draw_mouse(void)
430 {
431     return (mouse_define(MOUSE_NAME, "spline draw",
432                          LEFT_NAME, "mark",
433                          LEFT_DOWN, spline_mark_point,
434                          MIDDLE_NAME, "save",
435                          MIDDLE_DOWN, spline_save_periodic,
436                          RIGHT_NAME, "remove", 
437                          RIGHT_DOWN, spline_rm,
438                          NULL));
439 }
440 
441 
442 Tv_mouse spline2_edit_mouse(void)
443 {
444   return (mouse_define(MOUSE_NAME, "spline edit",
445                        LEFT_NAME, "replace",
446                        LEFT_DOWN, spline_replace_down,
447                        LEFT_DRAG, spline_replace_drag,
448                        LEFT_UP, spline_replace_up,
449                        MIDDLE_NAME, "add",
450                        MIDDLE_DOWN, spline_insert_point,
451                        RIGHT_NAME, "delete",
452                        RIGHT_DOWN, spline_remove_point,
453                        NULL));
454 }
455 
456 
457 /* replacing spline interpolation points */
458 
459 
460 static void spline_replace_down(Tv * tv, Ipos pos)
461 {
462   Spline2 *spline = NULL;
463   Vec2 p;
464 
465   if ((spline = seq_spline_get()) == NULL)
466     return;
467   p = tv_backproj2(tv, pos);
468   iclose = ROUND(spline2_param(spline, p));
469   if (iclose == spline->n)
470     iclose = 0;
471   spline2_replace_point(spline, iclose, p);
472   tv_set_overlay(tv);
473   spline2_draw(tv, spline);
474 }
475 
476 static void spline_replace_drag(Tv * tv, Ipos pos)
477 {
478   Spline2 *spline = NULL;
479   Vec2 p;
480   
481   if ((spline = seq_spline_get()) == NULL)
482     return;
483   spline2_draw(tv, spline);
484   p = tv_backproj2(tv, pos);
485   spline2_replace_point(spline, iclose, p);
486   spline2_draw(tv, spline);
487 }
488 
489 static void spline_replace_up(Tv * tv, Ipos pos)
490 {
491   Spline2 *spline = NULL;
492   Vec2 p;
493   
494   if ((spline = seq_spline_get()) == NULL)
495     return;
496   p = tv_backproj2(tv, pos);
497   spline2_replace_point(spline, iclose, p);
498   seq_spline_changed();
499   
500   tv_reset_draw(tv);
501   tv_repaint(tv);
502   tv_set_color(tv, magenta);
503  
504   redraw_splines(get_slice_no(), tv);
505 }
506 
507 static void spline_insert_point(Tv * tv, Ipos pos)
508 {
509   Spline2 *spline=NULL;
510   int ibelow, vol_no;
511   Vec2 p;
512   Br_imstack *ims = (Br_imstack *)Br_imstack_get();  
513   if ((spline = seq_spline_get()) == NULL)
514     return;
515   
516   p = tv_backproj2(tv, pos);
517   ibelow = spline2_param(spline, p); 
518   spline2_add_point(spline, ibelow, p);
519   seq_spline_changed();
520   
521   tv_repaint(tv);
522   vol_no=ims->nvoi;
523   tv_set_color(tv, choose_colour(vol_no));
524  
525   redraw_splines(get_slice_no(), tv);
526 }
527 
528 static void spline_remove_point(Tv * tv, Ipos pos)
529 {
530   Spline2 *spline = NULL;
531   Vec2 p;
532   int iclose, vol_no;
533   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
534 
535   if ((spline = seq_spline_get()) == NULL || spline->n <= 3 /** 5 **/ )
536     return;
537   
538   p = tv_backproj2(tv, pos);
539   iclose = ROUND(spline2_param(spline, p));
540   if (iclose == spline->n)
541     iclose = 0;
542   spline2_delete_point(spline, iclose);
543   seq_spline_changed();
544   
545   tv_repaint(tv);
546   vol_no=ims->nvoi;
547   tv_set_color(tv, choose_colour(vol_no));
548 
549   redraw_splines(get_slice_no(), tv);
550 }
551 
552 /*
553   Get B-spline representing active VOI in current slice.
554   If not available spline will be computed by recursive fit 
555   from string or snake.
556 */
557 Spline2 *seq_spline_get(void)
558 {
559   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
560   int slice_no=get_slice_no();
561   int vol_no=ims->nvoi;
562 
563   if (ims == NULL)
564     return NULL;
565   else
566     return ims->slice[slice_no]->voi[vol_no]->spline;
567 }
568 
569 
570 
571 /*
572   Notify change in spline for active VOI in current slice.
573   Other representations will then be recomputed on ims_get_... request.
574 */
575 void seq_spline_changed(void)
576 {
577   Br_imstack *ims = (Br_imstack *)Br_imstack_get();
578   int slice_no, vol_no;
579   Spline2 *spline = NULL; 
580 
581   if (ims != NULL)
582     {   
583 
584       slice_no=get_slice_no();
585       vol_no=ims->nvoi;
586       spline=ims->slice[slice_no]->voi[vol_no]->spline;
587 
588       str_free(ims->slice[slice_no]->voi[vol_no]->string, rfree);
589       ims->slice[slice_no]->voi[vol_no]->string=NULL;
590      
591       ims->slice[slice_no]->voi[vol_no]->string=str_list_from_tstr(spline, slice_no);
592       update_view();
593     }
594   
595 }
596 

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