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

Linux Cross Reference
Tina5/tina-tools/tinatool/draw/drawTv_roi.c

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

  1 /*********
  2  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  3  * University of Manchester, UK.  All rights reserved.
  4  * 
  5  * Redistribution and use in source and binary forms, with or without modification, 
  6  * are permitted provided that the following conditions are met:
  7  * 
  8  *   . Redistributions of source code must retain the above copyright notice, 
  9  *     this list of conditions and the following disclaimer.
 10  *    
 11  *   . Redistributions in binary form must reproduce the above copyright notice,
 12  *     this list of conditions and the following disclaimer in the documentation 
 13  *     and/or other materials provided with the distribution.
 14  * 
 15  *   . Neither the name of the University of Manchester nor the names of its
 16  *     contributors may be used to endorse or promote products derived from this 
 17  *     software without specific prior written permission.
 18  * 
 19  * 
 20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 30  * POSSIBILITY OF SUCH DAMAGE.
 31  *
 32  **********
 33  *
 34  * Program :    TINA
 35  * File    :  $Source: /home/tina/cvs/tina-tools/tinatool/draw/drawTv_roi.c,v $
 36  * Date    :  $Date: 2003/10/01 16:02:47 $
 37  * Version :  $Revision: 1.2 $
 38  * CVS Id  :  $Id: drawTv_roi.c,v 1.2 2003/10/01 16:02:47 tony Exp $
 39  *
 40  * Author  : Legacy TINA
 41  *
 42  * Notes :
 43  *
 44  *********
 45 */
 46 
 47 #include "drawTv_roi.h"
 48 
 49 #if HAVE_CONFIG_H
 50   #include <config.h>
 51 #endif
 52 
 53 #include <stdio.h>
 54 #include <math.h>
 55 #include <tina/sys/sysDef.h>
 56 #include <tina/sys/sysPro.h>
 57 #include <tina/math/mathDef.h>
 58 #include <tina/math/mathPro.h>
 59 #include <tina/geometry/geomDef.h>
 60 #include <tina/geometry/geomPro.h>
 61 #include <tinatool/draw/draw_TvDef.h>
 62 #include <tinatool/draw/draw_TvPro.h>
 63 
 64 static List *roi_poly = NULL;
 65 
 66 List *tv_poly_get()
 67 {
 68   return (roi_poly);
 69 }
 70 
 71 
 72 void tv_poly_set(List * new_poly)
 73 {
 74   if (roi_poly != NULL)
 75     dd_list_rm(roi_poly, vec2_free);
 76   roi_poly = dd_get_start(new_poly);
 77 }
 78 
 79 
 80 void tv_poly_now(List * current)
 81 {
 82   roi_poly = current;
 83 }
 84 
 85 
 86 Imregion *tv_get_im_roi(Tv * tv)
 87 {
 88   int lx, ly, ux, uy;
 89   Imregion *roi_alloc();
 90   Vec2 ul =
 91   {Vec2_id};
 92   Vec2 lr =
 93   {Vec2_id};
 94 
 95   if (tv == NULL || tv->tv_screen == NULL)
 96     return (NULL);
 97 
 98   ul = tv->ul;
 99   lr = tv->lr;
100   lx = (int) vec2_x(ul);
101   ly = (int) vec2_y(ul);
102   ux = (int) vec2_x(lr);
103   uy = (int) vec2_y(lr);
104   return (roi_alloc(lx, ly, ux, uy));
105 }
106 
107 
108 Imregion *tv_get_tv_screen_region(Tv * tv)
109 {
110   int lx, ly, ux, uy;
111   Imregion *roi_alloc();
112   Vec2 ul =
113   {Vec2_id};
114   Vec2 lr =
115   {Vec2_id};
116 
117   if (tv == NULL || tv->tv_screen == NULL)
118     return (NULL);
119 
120   ul = tv_backproj2(tv, ipos(0, 0));
121   lr = tv_backproj2(tv, ipos(tv->width, tv->height));
122   lx = (int) vec2_x(ul);
123   ly = (int) vec2_y(ul);
124   ux = (int) vec2_x(lr);
125   uy = (int) vec2_y(lr);
126   if (lx > ux)
127     SWAP(int, lx, ux);
128 
129   if (ly > uy)
130     SWAP(int, ly, uy);
131 
132   return (roi_alloc(lx, ly, ux, uy));
133 }
134 
135 
136 void tv_get_roi(Tv * tv, float *x1, float *y1, float *x2, float *y2)
137 {
138   *x1 = vec2_x(tv->ul);
139   *y1 = vec2_y(tv->ul);
140   *x2 = vec2_x(tv->lr);
141   *y2 = vec2_y(tv->lr);
142 }
143 
144 
145 void tv_set_roi(Tv * tv, Imregion * roi)
146 {
147   tv->lr.el[0] = (float) roi->ux;
148   tv->lr.el[1] = (float) roi->uy;
149   tv->ul.el[0] = (float) roi->lx;
150   tv->ul.el[1] = (float) roi->ly;
151 }
152 
153 
154 void tv_poly_rect_roi(Tv *tv)
155 {
156   List  *lptr;
157   Vec2    *p, lr, ul;
158  
159   if (!(lptr = tv_poly_get()))
160     return;
161   if (!(p = (Vec2 *) lptr->to))
162     return;
163 
164   lr.el[0] = p->el[0];
165   lr.el[1] = p->el[1];
166   ul.el[0] = p->el[0];
167   ul.el[1] = p->el[1];
168 
169   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next) 
170   {
171     p = (Vec2 *) lptr->to;
172     if (p->el[0] < ul.el[0])
173       ul.el[0] = p->el[0];
174     if (p->el[1] < ul.el[1])
175       ul.el[1] = p->el[1];
176     if (p->el[0] > lr.el[0])
177       lr.el[0] = p->el[0];
178     if (p->el[1] > lr.el[1])
179       lr.el[1] = p->el[1];
180   }
181   
182   tv->lr.el[0] = (float)lr.el[0]; 
183   tv->lr.el[1] = (float)lr.el[1]; 
184   tv->ul.el[0] = (float)ul.el[0]; 
185   tv->ul.el[1] = (float)ul.el[1]; 
186 
187 }
188 
189 void tv_poly_draw(Tv * tv)
190 {
191   List *lptr;
192   Vec2 p1, p2, *pfirst;
193 
194   if (!(lptr = tv_poly_get()))
195     return;
196   if (!(pfirst = (Vec2 *) lptr->to))
197     return;
198   p1 = *pfirst;
199   for (lptr = roi_poly->next; lptr != NULL; lptr = lptr->next)
200   {
201     tv_bigdot2(tv, p1, 2);
202     p2 = *(Vec2 *) lptr->to;
203     tv_line2(tv, p1, p2);
204     p1 = p2;
205   }
206   tv_bigdot2(tv, p1, 2);
207   tv_line2(tv, p2, *pfirst);
208 }
209 
210 
211 void tv_poly_addto_p(Vec2 v)
212 {
213   List *lptr;
214   Vec2 *p, temp;
215 
216   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
217   {
218     if (!(p = (Vec2 *) lptr->to))
219       continue;
220     temp = vec2_sum(*p, v);
221     vec2_x(*p) = vec2_x(temp);
222     vec2_y(*p) = vec2_y(temp);
223   }
224 }
225 
226 
227 void tv_poly_expand_p(Vec2 v)
228 {
229   List *lptr;
230   Bool safe = true;
231   Vec2 *p, unit, temp, spare, centre;
232   double scale = vec2_mod(v);
233   double direction = 1.0;
234   int count = 0;
235 
236   vec2_y(centre) = vec2_x(centre) = 0.0;
237 
238   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
239   {
240     if (!(p = (Vec2 *) lptr->to))
241       continue;
242     vec2_x(centre) += vec2_x(*p);
243     vec2_y(centre) += vec2_y(*p);
244     count++;
245   }
246   vec2_x(centre) /= (double) count;
247   vec2_y(centre) /= (double) count;
248 
249   if (vec2_x(v) < 0.0 || vec2_y(v) < 0.0)
250     direction = -1.0;
251 
252   for (lptr = tv_poly_get(); safe && lptr != NULL; lptr = lptr->next)
253   {
254     if (!(p = (Vec2 *) lptr->to))
255       continue;
256     temp = vec2_diff(*p, centre);
257     unit = vec2_unit(temp);
258     vec2_x(spare) = vec2_x(temp) + (direction * scale * vec2_x(unit));
259     vec2_y(spare) = vec2_y(temp) + (direction * scale * vec2_y(unit));
260     if ((vec2_x(temp) * vec2_x(spare) < 0) || (vec2_y(temp) * vec2_y(spare) < 0))
261     {
262       safe = false;
263       continue;
264     }
265   }
266 
267   for (lptr = tv_poly_get(); safe && lptr != NULL; lptr = lptr->next)
268   {
269     if (!(p = (Vec2 *) lptr->to))
270       continue;
271     temp = vec2_diff(*p, centre);
272     unit = vec2_unit(temp);
273     vec2_x(*p) = vec2_x(temp) + (direction * scale * vec2_x(unit)) + vec2_x(centre);
274     vec2_y(*p) = vec2_y(temp) + (direction * scale * vec2_y(unit)) + vec2_y(centre);
275   }
276 }
277 
278 
279 void tv_poly_rotate_p(Vec2 cp, Vec2 op)
280 {
281   List *lptr;
282   Vec2 *p, centre, temp;
283   double r1, t1, r2, t2, theta;
284   int count = 0;
285 
286   vec2_y(centre) = vec2_x(centre) = 0.0;
287 
288   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
289   {
290     if (!(p = (Vec2 *) lptr->to))
291       continue;
292     vec2_x(centre) += vec2_x(*p);
293     vec2_y(centre) += vec2_y(*p);
294     count++;
295   }
296   vec2_x(centre) /= (double) count;
297   vec2_y(centre) /= (double) count;
298 
299   vec2_to_polar(op, &r1, &t1);
300   vec2_to_polar(cp, &r2, &t2);
301   theta = t1 - t2;
302 
303   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
304   {
305     if (!(p = (Vec2 *) lptr->to))
306       continue;
307     vec2_x(temp) = vec2_x(*p);
308     vec2_y(temp) = vec2_y(*p);
309     temp = vec2_diff(temp, centre);
310     vec2_to_polar(temp, &r1, &t1);
311     t1 += theta;
312     temp = vec2_of_polar(r1, t1);
313     temp = vec2_sum(temp, centre);
314     vec2_x(*p) = vec2_x(temp);
315     vec2_y(*p) = vec2_y(temp);
316   }
317 }
318 
319 
320 void tv_poly_replace_p(Vec2 * op, Vec2 * np)
321 {
322   Bool nopoint = true;
323   List *lptr;
324   Vec2 v, *vptr;
325 
326   lptr = tv_poly_get();
327   while (nopoint && lptr)
328   {
329     if ((vptr = (Vec2 *) lptr->to))
330     {
331       v = vec2_diff(*vptr, *op);
332       if ((vec2_x(v) == 0.0) && (vec2_y(v) == 0.0))
333       {
334         if (np)
335         {
336           vec2_x(*vptr) = vec2_x(*np);
337           vec2_y(*vptr) = vec2_y(*np);
338         } else if (lptr == tv_poly_get())
339           tv_poly_now(dd_link_rm(lptr, vec2_free));
340         else
341           dd_link_rm(lptr, vec2_free);
342 
343         nopoint = false;
344         continue;
345       }
346     }
347     lptr = lptr->next;
348   }
349 }
350 
351 
352 void tv_roi_proc(Tv * tv, int state, Ipos pos)
353 {
354   static Vec2 p1 = {Vec2_id};
355   static Vec2 p2 = {Vec2_id};
356 
357   switch (state)
358   {
359   case LEFT_DOWN:
360     tv_poly_set(NULL);
361     tv_set_overlay(tv);
362     p1 = p2 = tv_backproj2(tv, pos);
363     tv_rect2(tv, p1, p2);
364     break;
365   case LEFT_DRAG:
366     tv_rect2(tv, p1, p2);
367     p2 = tv_backproj2(tv, pos);
368     tv_rect2(tv, p1, p2);
369     break;
370   case LEFT_UP:
371     tv_rect2(tv, p1, p2);
372     if (vec2_x(p1) < vec2_x(p2))
373     {
374       vec2_x(tv->ul) = vec2_x(p1);
375       vec2_x(tv->lr) = vec2_x(p2);
376     } else
377     {
378       vec2_x(tv->ul) = vec2_x(p2);
379       vec2_x(tv->lr) = vec2_x(p1);
380     }
381     if (vec2_y(p1) < vec2_y(p2))
382     {
383       vec2_y(tv->ul) = vec2_y(p1);
384       vec2_y(tv->lr) = vec2_y(p2);
385     } else
386     {
387       vec2_y(tv->ul) = vec2_y(p2);
388       vec2_y(tv->lr) = vec2_y(p1);
389     }
390     tv_rect2(tv, tv->ul, tv->lr);
391     tv_reset_draw(tv);
392     p1 = p2;
393     break;
394   case MIDDLE_DOWN:
395     tv_set_overlay(tv);
396     tv_rect2(tv, tv->ul, tv->lr);
397     tv_reset_draw(tv);
398     break;
399   }
400 }
401 
402 
403 void tv_polyroi_proc(Tv * tv, int state, Ipos pos)
404 {
405   static Vec2 p1 = {Vec2_id};
406   static Vec2 p2 = {Vec2_id};
407 
408   switch (state)
409   {
410   case LEFT_DOWN:
411     if (roi_poly == NULL)
412       break;
413     tv_set_color(tv, red);
414     p2 = tv_backproj2(tv, pos);
415     tv_set_overlay(tv);
416     tv_line2(tv, p1, p2);
417     break;
418   case LEFT_DRAG:
419     if (roi_poly == NULL)
420       break;
421     tv_line2(tv, p1, p2);
422     p2 = tv_backproj2(tv, pos);
423     tv_line2(tv, p1, p2);
424     break;
425   case LEFT_UP:
426     if (roi_poly == NULL)
427       break;
428     roi_poly = dd_ref_addtoend(roi_poly, (void *) vec2_copy(&p2), VEC2);
429     p1 = p2;
430     tv_reset_draw(tv);
431     tv_set_color(tv, red);
432     tv_bigdot2(tv, p1, 2);
433     break;
434   case MIDDLE_DOWN:
435     roi_poly = dd_get_start(roi_poly);
436     tv_repaint(tv);
437     tv_set_color(tv, red);
438     tv_poly_draw(tv);
439     tv_poly_rect_roi(tv);
440     break;
441   case RIGHT_DOWN:
442     tv_poly_set(NULL);
443     tv_repaint(tv);
444     p1 = tv_backproj2(tv, pos);
445     tv_set_color(tv, red);
446     tv_bigdot2(tv, p1, 2);
447     break;
448   case RIGHT_UP:
449     roi_poly = dd_ref_addtostart(roi_poly, (void *) vec2_copy(&p1), VEC2);
450     break;
451   }
452 }
453 
454 
455 Vec2 *roipnt_closest(Tv * tv, Ipos pos)
456 {
457   Vec2 p = {Vec2_id};
458   Vec2 *vptr, *bestp = NULL;
459   List *lptr;
460   double dist, closest;
461 
462   p = tv_backproj2(tv, pos);
463 
464   lptr = tv_poly_get();
465   if(lptr==NULL) return(NULL);
466   while (lptr)
467   {
468     if ((vptr = (Vec2 *) lptr->to))
469     {
470       dist = vec2_dist(*vptr, p);
471       if (dist < closest || !bestp)
472       {
473         bestp = vptr;
474         closest = dist;
475       }
476     }
477     lptr = lptr->next;
478   }
479 
480   return (bestp);
481 }
482 
483 
484 List *roiline_closest(Tv * tv, Ipos pos)
485 {
486   List *lptr, *bestp = NULL;
487   Vec2 p, *vptr1, *vptr2, direct;
488   double dist, closest;
489 
490   p = tv_backproj2(tv, pos);
491 
492   lptr = tv_poly_get();
493   if(lptr==NULL) return(NULL);
494 
495   while (lptr)
496   {
497     if ((vptr1 = (Vec2 *) lptr->to) &&
498         (lptr = lptr->next) &&
499         (vptr2 = (Vec2 *) lptr->to))
500     {
501       direct = vec2_unit(vec2_diff(*vptr1, *vptr2));
502       dist = vec2_dist_point_line(p, *vptr1, direct);
503       if (dist < closest || !bestp)
504       {
505         bestp = lptr->last;
506         closest = dist;
507       }
508     }
509   }
510 /*
511  * last point & first pooint edge check
512  */
513   vptr2 = (Vec2 *) (tv_poly_get()->to);
514   direct = vec2_unit(vec2_diff(*vptr1, *vptr2));
515   dist = vec2_dist_point_line(p, *vptr1, direct);
516   if (dist < closest)
517     bestp = dd_get_end(tv_poly_get());
518 
519   return (bestp);
520 }
521 
522 void tv_roipnt_proc(Tv * tv, int state, Ipos pos)
523 {
524   static Vec2 op = {Vec2_id};
525   static Vec2 cp = {Vec2_id};
526   Vec2   *pnt;
527   List *lptr;
528 
529   switch (state)
530   {
531   case LEFT_DOWN:
532     tv_repaint(tv);
533     tv_save_draw(tv);
534     tv_set_overlay(tv);
535     tv_poly_draw(tv);
536     if ((pnt = roipnt_closest(tv, pos))!=NULL)
537         cp = op = *pnt;
538     break;
539   case LEFT_DRAG:
540     tv_poly_draw(tv);
541     cp = tv_backproj2(tv, pos);
542     tv_poly_replace_p(&op, &cp);
543     op = cp;
544     tv_poly_draw(tv);
545     break;
546   case LEFT_UP:
547     tv_poly_replace_p(&op, &cp);
548     tv_reset_draw(tv);
549     tv_repaint(tv);
550     tv_set_color(tv, red);
551     tv_poly_draw(tv);
552     tv_poly_rect_roi(tv);
553     break;
554   case MIDDLE_DOWN:
555     if ((pnt = roipnt_closest(tv, pos))!=NULL)
556     {
557         op = *pnt;
558         tv_poly_replace_p(&op, NULL);
559     }
560     tv_repaint(tv);
561     tv_set_color(tv, red);
562     tv_poly_draw(tv);
563     tv_poly_rect_roi(tv);
564     break;
565   case RIGHT_DOWN:
566     cp = tv_backproj2(tv, pos);
567     if ((lptr = roiline_closest(tv, pos)))
568       dd_link_addafter(lptr, dd_link_alloc((void *) vec2_copy(&cp), VEC2));
569     tv_reset_draw(tv);
570     tv_repaint(tv);
571     tv_set_color(tv, red);
572     tv_poly_draw(tv);
573     tv_poly_rect_roi(tv);
574     break;
575   }
576 }
577 
578 void tv_roigbl_proc(Tv * tv, int state, Ipos pos)
579 {
580   static Vec2 op = {Vec2_id};
581   static Vec2 cp = {Vec2_id};
582   List *lptr;
583 
584   switch (state)
585   {
586   case LEFT_DOWN:
587     tv_save_draw(tv);
588     tv_set_overlay(tv);
589     tv_poly_draw(tv);
590     op = tv_backproj2(tv, pos);
591     break;
592   case LEFT_DRAG:
593     tv_poly_draw(tv);
594     cp = tv_backproj2(tv, pos);
595     tv_poly_addto_p(vec2_diff(cp, op));
596     op = cp;
597     tv_poly_draw(tv);
598     break;
599   case LEFT_UP:
600     tv_poly_addto_p(vec2_diff(cp, op));
601     tv_reset_draw(tv);
602     tv_repaint(tv);
603     tv_set_color(tv, red);
604     tv_poly_draw(tv);
605     tv_poly_rect_roi(tv);
606     break;
607   case MIDDLE_DOWN:
608     tv_save_draw(tv);
609     tv_set_overlay(tv);
610     tv_poly_draw(tv);
611     op = tv_backproj2(tv, pos);
612     break;
613   case MIDDLE_DRAG:
614     tv_poly_draw(tv);
615     cp = tv_backproj2(tv, pos);
616     tv_poly_expand_p(vec2_diff(cp, op));
617     op = cp;
618     tv_poly_draw(tv);
619     break;
620   case MIDDLE_UP:
621     tv_poly_expand_p(vec2_diff(cp, op));
622     tv_reset_draw(tv);
623     tv_repaint(tv);
624     tv_set_color(tv, red);
625     tv_poly_draw(tv);
626     tv_poly_rect_roi(tv);
627     break;
628   case RIGHT_DOWN:
629     tv_save_draw(tv);
630     tv_set_overlay(tv);
631     tv_poly_draw(tv);
632     op = tv_backproj2(tv, pos);
633     break;
634   case RIGHT_DRAG:
635     tv_poly_draw(tv);
636     cp = tv_backproj2(tv, pos);
637     tv_poly_rotate_p(cp, op);
638     op = cp;
639     tv_poly_draw(tv);
640     break;
641   case RIGHT_UP:
642     tv_poly_rotate_p(cp, op);
643     tv_reset_draw(tv);
644     tv_repaint(tv);
645     tv_set_color(tv, red);
646     tv_poly_draw(tv);
647     tv_poly_rect_roi(tv);
648     break;
649   }
650 }
651 

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