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

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

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