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

Linux Cross Reference
Tina4/src/tv/tv_roi.c

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

  1 /**@(#)
  2 **/
  3 #include <stdio.h>
  4 #include <math.h>
  5 #include <tina/sys.h>
  6 #include <tina/math.h>
  7 #include <tina/mathfuncs.h>
  8 #include <tina/sysfuncs.h>
  9 #include <tina/vision.h>
 10 #include <tina/visionfuncs.h>
 11 #include <tina/tv.h>
 12 #include <tina/tvfuncs.h>
 13 
 14 static List *roi_poly = NULL;
 15 
 16 
 17 List *tv_poly_get()
 18 {
 19   return (roi_poly);
 20 }
 21 
 22 
 23 void tv_poly_set(List * new_poly)
 24 {
 25   if (roi_poly != NULL)
 26     dd_list_rm(roi_poly, vec2_free);
 27   roi_poly = dd_get_start(new_poly);
 28 }
 29 
 30 
 31 void tv_poly_now(List * current)
 32 {
 33   roi_poly = current;
 34 }
 35 
 36 
 37 Imregion *tv_get_im_roi(Tv * tv)
 38 {
 39   int lx, ly, ux, uy;
 40   Imregion *roi_alloc();
 41   Vec2 ul =
 42   {Vec2_id};
 43   Vec2 lr =
 44   {Vec2_id};
 45 
 46   if (tv == NULL || tv->tv_screen == NULL)
 47     return (NULL);
 48 
 49   ul = tv->ul;
 50   lr = tv->lr;
 51   lx = (int) vec2_x(ul);
 52   ly = (int) vec2_y(ul);
 53   ux = (int) vec2_x(lr);
 54   uy = (int) vec2_y(lr);
 55   return (roi_alloc(lx, ly, ux, uy));
 56 }
 57 
 58 
 59 Imregion *tv_get_tv_screen_region(Tv * tv)
 60 {
 61   int lx, ly, ux, uy;
 62   Imregion *roi_alloc();
 63   Vec2 ul =
 64   {Vec2_id};
 65   Vec2 lr =
 66   {Vec2_id};
 67 
 68   if (tv == NULL || tv->tv_screen == NULL)
 69     return (NULL);
 70 
 71   ul = tv_backproj2(tv, ipos(0, 0));
 72   lr = tv_backproj2(tv, ipos(tv->width, tv->height));
 73   lx = (int) vec2_x(ul);
 74   ly = (int) vec2_y(ul);
 75   ux = (int) vec2_x(lr);
 76   uy = (int) vec2_y(lr);
 77   if (lx > ux)
 78     SWAP(int, lx, ux);
 79 
 80   if (ly > uy)
 81     SWAP(int, ly, uy);
 82 
 83   return (roi_alloc(lx, ly, ux, uy));
 84 }
 85 
 86 
 87 void tv_get_roi(Tv * tv, float *x1, float *y1, float *x2, float *y2)
 88 {
 89   *x1 = vec2_x(tv->ul);
 90   *y1 = vec2_y(tv->ul);
 91   *x2 = vec2_x(tv->lr);
 92   *y2 = vec2_y(tv->lr);
 93 }
 94 
 95 
 96 void tv_set_roi(Tv * tv, Imregion * roi)
 97 {
 98   tv->lr.el[0] = (float) roi->ux;
 99   tv->lr.el[1] = (float) roi->uy;
100   tv->ul.el[0] = (float) roi->lx;
101   tv->ul.el[1] = (float) roi->ly;
102 }
103 
104 
105 void tv_poly_rect_roi(Tv *tv)
106 {
107   List  *lptr;
108   Vec2    *p, lr, ul;
109  
110   if (!(lptr = tv_poly_get()))
111     return;
112   if (!(p = (Vec2 *) lptr->to))
113     return;
114 
115   lr.el[0] = p->el[0];
116   lr.el[1] = p->el[1];
117   ul.el[0] = p->el[0];
118   ul.el[1] = p->el[1];
119 
120   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next) 
121   {
122     p = (Vec2 *) lptr->to;
123     if (p->el[0] < ul.el[0])
124       ul.el[0] = p->el[0];
125     if (p->el[1] < ul.el[1])
126       ul.el[1] = p->el[1];
127     if (p->el[0] > lr.el[0])
128       lr.el[0] = p->el[0];
129     if (p->el[1] > lr.el[1])
130       lr.el[1] = p->el[1];
131   }
132   
133   tv->lr.el[0] = (float)lr.el[0]; 
134   tv->lr.el[1] = (float)lr.el[1]; 
135   tv->ul.el[0] = (float)ul.el[0]; 
136   tv->ul.el[1] = (float)ul.el[1]; 
137 
138 }
139 
140 void tv_poly_draw(Tv * tv)
141 {
142   List *lptr;
143   Vec2 p1, p2, *pfirst;
144 
145   if (!(lptr = tv_poly_get()))
146     return;
147   if (!(pfirst = (Vec2 *) lptr->to))
148     return;
149   p1 = *pfirst;
150   for (lptr = roi_poly->next; lptr != NULL; lptr = lptr->next)
151   {
152     tv_bigdot2(tv, p1, 2);
153     p2 = *(Vec2 *) lptr->to;
154     tv_line2(tv, p1, p2);
155     p1 = p2;
156   }
157   tv_bigdot2(tv, p1, 2);
158   tv_line2(tv, p2, *pfirst);
159 }
160 
161 
162 void tv_poly_addto_p(Vec2 v)
163 {
164   List *lptr;
165   Vec2 *p, temp;
166 
167   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
168   {
169     if (!(p = (Vec2 *) lptr->to))
170       continue;
171     temp = vec2_sum(*p, v);
172     vec2_x(*p) = vec2_x(temp);
173     vec2_y(*p) = vec2_y(temp);
174   }
175 }
176 
177 
178 void tv_poly_expand_p(Vec2 v)
179 {
180   List *lptr;
181   Bool safe = true;
182   Vec2 *p, unit, temp, spare, centre;
183   double scale = vec2_mod(v);
184   double direction = 1.0;
185   int count = 0;
186 
187   vec2_y(centre) = vec2_x(centre) = 0.0;
188 
189   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
190   {
191     if (!(p = (Vec2 *) lptr->to))
192       continue;
193     vec2_x(centre) += vec2_x(*p);
194     vec2_y(centre) += vec2_y(*p);
195     count++;
196   }
197   vec2_x(centre) /= (double) count;
198   vec2_y(centre) /= (double) count;
199 
200   if (vec2_x(v) < 0.0 || vec2_y(v) < 0.0)
201     direction = -1.0;
202 
203   for (lptr = tv_poly_get(); safe && lptr != NULL; lptr = lptr->next)
204   {
205     if (!(p = (Vec2 *) lptr->to))
206       continue;
207     temp = vec2_diff(*p, centre);
208     unit = vec2_unit(temp);
209     vec2_x(spare) = vec2_x(temp) + (direction * scale * vec2_x(unit));
210     vec2_y(spare) = vec2_y(temp) + (direction * scale * vec2_y(unit));
211     if ((vec2_x(temp) * vec2_x(spare) < 0) || (vec2_y(temp) * vec2_y(spare) < 0))
212     {
213       safe = false;
214       continue;
215     }
216   }
217 
218   for (lptr = tv_poly_get(); safe && lptr != NULL; lptr = lptr->next)
219   {
220     if (!(p = (Vec2 *) lptr->to))
221       continue;
222     temp = vec2_diff(*p, centre);
223     unit = vec2_unit(temp);
224     vec2_x(*p) = vec2_x(temp) + (direction * scale * vec2_x(unit)) + vec2_x(centre);
225     vec2_y(*p) = vec2_y(temp) + (direction * scale * vec2_y(unit)) + vec2_y(centre);
226   }
227 }
228 
229 
230 void tv_poly_rotate_p(Vec2 cp, Vec2 op)
231 {
232   List *lptr;
233   Vec2 *p, centre, temp;
234   double r1, t1, r2, t2, theta;
235   int count = 0;
236 
237   vec2_y(centre) = vec2_x(centre) = 0.0;
238 
239   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
240   {
241     if (!(p = (Vec2 *) lptr->to))
242       continue;
243     vec2_x(centre) += vec2_x(*p);
244     vec2_y(centre) += vec2_y(*p);
245     count++;
246   }
247   vec2_x(centre) /= (double) count;
248   vec2_y(centre) /= (double) count;
249 
250   vec2_to_polar(op, &r1, &t1);
251   vec2_to_polar(cp, &r2, &t2);
252   theta = t1 - t2;
253 
254   for (lptr = tv_poly_get(); lptr != NULL; lptr = lptr->next)
255   {
256     if (!(p = (Vec2 *) lptr->to))
257       continue;
258     vec2_x(temp) = vec2_x(*p);
259     vec2_y(temp) = vec2_y(*p);
260     temp = vec2_diff(temp, centre);
261     vec2_to_polar(temp, &r1, &t1);
262     t1 += theta;
263     temp = vec2_of_polar(r1, t1);
264     temp = vec2_sum(temp, centre);
265     vec2_x(*p) = vec2_x(temp);
266     vec2_y(*p) = vec2_y(temp);
267   }
268 }
269 
270 
271 void tv_poly_replace_p(Vec2 * op, Vec2 * np)
272 {
273   Bool nopoint = true;
274   List *lptr;
275   Vec2 v, *vptr;
276 
277   lptr = tv_poly_get();
278   while (nopoint && lptr)
279   {
280     if ((vptr = (Vec2 *) lptr->to))
281     {
282       v = vec2_diff(*vptr, *op);
283       if ((vec2_x(v) == 0.0) && (vec2_y(v) == 0.0))
284       {
285         if (np)
286         {
287           vec2_x(*vptr) = vec2_x(*np);
288           vec2_y(*vptr) = vec2_y(*np);
289         } else if (lptr == tv_poly_get())
290           tv_poly_now(dd_link_rm(lptr, vec2_free));
291         else
292           dd_link_rm(lptr, vec2_free);
293 
294         nopoint = false;
295         continue;
296       }
297     }
298     lptr = lptr->next;
299   }
300 }
301 
302 
303 void tv_roi_proc(Tv * tv, int state, Ipos pos)
304 {
305   static Vec2 p1 = {Vec2_id};
306   static Vec2 p2 = {Vec2_id};
307 
308   switch (state)
309   {
310   case LEFT_DOWN:
311     tv_poly_set(NULL);
312     tv_set_overlay(tv);
313     p1 = p2 = tv_backproj2(tv, pos);
314     tv_rect2(tv, p1, p2);
315     break;
316   case LEFT_DRAG:
317     tv_rect2(tv, p1, p2);
318     p2 = tv_backproj2(tv, pos);
319     tv_rect2(tv, p1, p2);
320     break;
321   case LEFT_UP:
322     tv_rect2(tv, p1, p2);
323     if (vec2_x(p1) < vec2_x(p2))
324     {
325       vec2_x(tv->ul) = vec2_x(p1);
326       vec2_x(tv->lr) = vec2_x(p2);
327     } else
328     {
329       vec2_x(tv->ul) = vec2_x(p2);
330       vec2_x(tv->lr) = vec2_x(p1);
331     }
332     if (vec2_y(p1) < vec2_y(p2))
333     {
334       vec2_y(tv->ul) = vec2_y(p1);
335       vec2_y(tv->lr) = vec2_y(p2);
336     } else
337     {
338       vec2_y(tv->ul) = vec2_y(p2);
339       vec2_y(tv->lr) = vec2_y(p1);
340     }
341     tv_rect2(tv, tv->ul, tv->lr);
342     tv_reset_draw(tv);
343     p1 = p2;
344     break;
345   case MIDDLE_DOWN:
346     tv_set_overlay(tv);
347     tv_rect2(tv, tv->ul, tv->lr);
348     tv_reset_draw(tv);
349     break;
350   }
351 }
352 
353 
354 void tv_polyroi_proc(Tv * tv, int state, Ipos pos)
355 {
356   static Vec2 p1 = {Vec2_id};
357   static Vec2 p2 = {Vec2_id};
358 
359   switch (state)
360   {
361   case LEFT_DOWN:
362     if (roi_poly == NULL)
363       break;
364     tv_set_color(tv, red);
365     p2 = tv_backproj2(tv, pos);
366     tv_set_overlay(tv);
367     tv_line2(tv, p1, p2);
368     break;
369   case LEFT_DRAG:
370     if (roi_poly == NULL)
371       break;
372     tv_line2(tv, p1, p2);
373     p2 = tv_backproj2(tv, pos);
374     tv_line2(tv, p1, p2);
375     break;
376   case LEFT_UP:
377     if (roi_poly == NULL)
378       break;
379     roi_poly = dd_ref_addtoend(roi_poly, (void *) vec2_copy(&p2), VEC2);
380     p1 = p2;
381     tv_reset_draw(tv);
382     tv_set_color(tv, red);
383     tv_bigdot2(tv, p1, 2);
384     break;
385   case MIDDLE_DOWN:
386     roi_poly = dd_get_start(roi_poly);
387     tv_repaint(tv);
388     tv_set_color(tv, red);
389     tv_poly_draw(tv);
390     tv_poly_rect_roi(tv);
391     break;
392   case RIGHT_DOWN:
393     tv_poly_set(NULL);
394     tv_repaint(tv);
395     p1 = tv_backproj2(tv, pos);
396     tv_set_color(tv, red);
397     tv_bigdot2(tv, p1, 2);
398     break;
399   case RIGHT_UP:
400     roi_poly = dd_ref_addtostart(roi_poly, (void *) vec2_copy(&p1), VEC2);
401     break;
402   }
403 }
404 
405 
406 Vec2 *roipnt_closest(Tv * tv, Ipos pos)
407 {
408   Vec2 p = {Vec2_id};
409   Vec2 *vptr, *bestp = NULL;
410   List *lptr;
411   double dist, closest;
412 
413   p = tv_backproj2(tv, pos);
414 
415   lptr = tv_poly_get();
416   if(lptr==NULL) return(NULL);
417   while (lptr)
418   {
419     if ((vptr = (Vec2 *) lptr->to))
420     {
421       dist = vec2_dist(*vptr, p);
422       if (dist < closest || !bestp)
423       {
424         bestp = vptr;
425         closest = dist;
426       }
427     }
428     lptr = lptr->next;
429   }
430 
431   return (bestp);
432 }
433 
434 
435 List *roiline_closest(Tv * tv, Ipos pos)
436 {
437   List *lptr, *bestp = NULL;
438   Vec2 p, *vptr1, *vptr2, direct;
439   double dist, closest;
440 
441   p = tv_backproj2(tv, pos);
442 
443   lptr = tv_poly_get();
444   if(lptr==NULL) return(NULL);
445 
446   while (lptr)
447   {
448     if ((vptr1 = (Vec2 *) lptr->to) &&
449         (lptr = lptr->next) &&
450         (vptr2 = (Vec2 *) lptr->to))
451     {
452       direct = vec2_unit(vec2_diff(*vptr1, *vptr2));
453       dist = vec2_dist_point_line(p, *vptr1, direct);
454       if (dist < closest || !bestp)
455       {
456         bestp = lptr->last;
457         closest = dist;
458       }
459     }
460   }
461 /*
462  * last point & first pooint edge check
463  */
464   vptr2 = (Vec2 *) (tv_poly_get()->to);
465   direct = vec2_unit(vec2_diff(*vptr1, *vptr2));
466   dist = vec2_dist_point_line(p, *vptr1, direct);
467   if (dist < closest)
468     bestp = dd_get_end(tv_poly_get());
469 
470   return (bestp);
471 }
472 
473 void tv_roipnt_proc(Tv * tv, int state, Ipos pos)
474 {
475   static Vec2 op = {Vec2_id};
476   static Vec2 cp = {Vec2_id};
477   Vec2   *pnt;
478   List *lptr;
479 
480   switch (state)
481   {
482   case LEFT_DOWN:
483     tv_repaint(tv);
484     tv_save_draw(tv);
485     tv_set_overlay(tv);
486     tv_poly_draw(tv);
487     if ((pnt = roipnt_closest(tv, pos))!=NULL)
488         cp = op = *pnt;
489     break;
490   case LEFT_DRAG:
491     tv_poly_draw(tv);
492     cp = tv_backproj2(tv, pos);
493     tv_poly_replace_p(&op, &cp);
494     op = cp;
495     tv_poly_draw(tv);
496     break;
497   case LEFT_UP:
498     tv_poly_replace_p(&op, &cp);
499     tv_reset_draw(tv);
500     tv_repaint(tv);
501     tv_set_color(tv, red);
502     tv_poly_draw(tv);
503     tv_poly_rect_roi(tv);
504     break;
505   case MIDDLE_DOWN:
506     if ((pnt = roipnt_closest(tv, pos))!=NULL)
507     {
508         op = *pnt;
509         tv_poly_replace_p(&op, NULL);
510     }
511     tv_repaint(tv);
512     tv_set_color(tv, red);
513     tv_poly_draw(tv);
514     tv_poly_rect_roi(tv);
515     break;
516   case RIGHT_DOWN:
517     cp = tv_backproj2(tv, pos);
518     if ((lptr = roiline_closest(tv, pos)))
519       dd_link_addafter(lptr, dd_link_alloc((void *) vec2_copy(&cp), VEC2));
520     tv_reset_draw(tv);
521     tv_repaint(tv);
522     tv_set_color(tv, red);
523     tv_poly_draw(tv);
524     tv_poly_rect_roi(tv);
525     break;
526   }
527 }
528 
529 void tv_roigbl_proc(Tv * tv, int state, Ipos pos)
530 {
531   static Vec2 op = {Vec2_id};
532   static Vec2 cp = {Vec2_id};
533   List *lptr;
534 
535   switch (state)
536   {
537   case LEFT_DOWN:
538     tv_save_draw(tv);
539     tv_set_overlay(tv);
540     tv_poly_draw(tv);
541     op = tv_backproj2(tv, pos);
542     break;
543   case LEFT_DRAG:
544     tv_poly_draw(tv);
545     cp = tv_backproj2(tv, pos);
546     tv_poly_addto_p(vec2_diff(cp, op));
547     op = cp;
548     tv_poly_draw(tv);
549     break;
550   case LEFT_UP:
551     tv_poly_addto_p(vec2_diff(cp, op));
552     tv_reset_draw(tv);
553     tv_repaint(tv);
554     tv_set_color(tv, red);
555     tv_poly_draw(tv);
556     tv_poly_rect_roi(tv);
557     break;
558   case MIDDLE_DOWN:
559     tv_save_draw(tv);
560     tv_set_overlay(tv);
561     tv_poly_draw(tv);
562     op = tv_backproj2(tv, pos);
563     break;
564   case MIDDLE_DRAG:
565     tv_poly_draw(tv);
566     cp = tv_backproj2(tv, pos);
567     tv_poly_expand_p(vec2_diff(cp, op));
568     op = cp;
569     tv_poly_draw(tv);
570     break;
571   case MIDDLE_UP:
572     tv_poly_expand_p(vec2_diff(cp, op));
573     tv_reset_draw(tv);
574     tv_repaint(tv);
575     tv_set_color(tv, red);
576     tv_poly_draw(tv);
577     tv_poly_rect_roi(tv);
578     break;
579   case RIGHT_DOWN:
580     tv_save_draw(tv);
581     tv_set_overlay(tv);
582     tv_poly_draw(tv);
583     op = tv_backproj2(tv, pos);
584     break;
585   case RIGHT_DRAG:
586     tv_poly_draw(tv);
587     cp = tv_backproj2(tv, pos);
588     tv_poly_rotate_p(cp, op);
589     op = cp;
590     tv_poly_draw(tv);
591     break;
592   case RIGHT_UP:
593     tv_poly_rotate_p(cp, op);
594     tv_reset_draw(tv);
595     tv_repaint(tv);
596     tv_set_color(tv, red);
597     tv_poly_draw(tv);
598     tv_poly_rect_roi(tv);
599     break;
600   }
601 }
602 

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