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

Linux Cross Reference
Tina4/src/vision/improc/im_poly_crop.c

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

  1 #include <math.h>
  2 #include <tina/sys.h>
  3 #include <tina/sysfuncs.h>
  4 #include <tina/math.h>
  5 #include <tina/mathfuncs.h>
  6 #include <tina/vision.h>
  7 #include <tina/visionfuncs.h>
  8  
  9 /* software copied from pairwise code */
 10 static Vec2 get_intersection(Line2 *l1,Line2 *l2,Bool *line1,Bool *line2)
 11 {
 12   Vec2 v1,v2,p,q,cp,b,isct;
 13   float d1,d2,a;
 14  
 15   v1 = l1->v;
 16   v2 = l2->v;
 17   *line1 = FALSE;
 18   *line2 = FALSE;
 19  
 20     p = l1->p1;
 21     q = l2->p1;
 22     cp.el[0] = -v2.el[1];
 23     cp.el[1] = v2.el[0];
 24  
 25     a = (float)vec2_dot(cp,v1);
 26 /* test this ... done NAT/PAR 8/12/93 */
 27 #ifdef _PCC
 28     if (fabs(a)< 0.0001)
 29     {
 30        a = (float)_copysign(0.0001,a);
 31        return(isct);
 32     }
 33 #else
 34     if (fabs(a)< 0.0001)
 35     {
 36        a = copysign(0.0001,a);
 37        return(isct);
 38     }
 39 #endif
 40     a = (float)vec2_dot(cp,vec2_diff(p,q))/a;
 41     b = vec2_times(a,v1);
 42  
 43     isct.el[0] = p.el[0] - b.el[0];
 44     isct.el[1] = p.el[1] - b.el[1];
 45  
 46     d1 = (float)vec2_dist(isct,l1->p1);
 47     d2 = (float)vec2_dist(isct,l1->p2);
 48     if((d1<fabs(l1->length)+0.001)&&(d2<=fabs(l1->length)+0.001))
 49       *line1 = TRUE;
 50  
 51     d1 = (float)vec2_dist(isct,l2->p1);
 52     d2 = (float)vec2_dist(isct,l2->p2);
 53     if((d1<fabs(l2->length)+0.001)&&(d2<=fabs(l2->length)+0.001))
 54       *line2 = TRUE;
 55  
 56     return(isct);
 57 }
 58 
 59 void    imf_poly_crop(Imrect *im1, List *poly)
 60 {
 61     Imregion *roi;
 62     float  *row1, *row2;
 63     Vec2    p1=vec2_zero(),p2=vec2_zero();
 64     Vec2   *pfirst;
 65     List   *lptr,*pptr;
 66     Line2  *l1,*l2,*llast;
 67     Line2  **lptrs;
 68     int    *label;
 69     Bool    insct1,insct2;
 70     int     lx, ux, ly, uy;
 71     int     i, j, k, count, lcount=0;
 72  
 73     if (im1 == NULL)
 74         return;
 75  
 76     roi = im1->region;
 77     if (roi == NULL)
 78         return;
 79     lx = roi->lx;
 80     ux = roi->ux;
 81     ly = roi->ly;
 82     uy = roi->uy;
 83  
 84     row1 = fvector_alloc(lx, ux);
 85     row2 = fvector_alloc(lx, ux);
 86     pfirst = (Vec2 *)poly->to;
 87     p1 = *pfirst;
 88     for (lptr = poly, lcount = 0; lptr!=NULL; lptr = lptr->next)
 89     {
 90        lcount++;
 91     }
 92     lptrs = (Line2 **)pvector_alloc(0,lcount);
 93     label = (int *)ivector_alloc(0,lcount);
 94     
 95     for (lptr = poly->next, i=0; lptr!=NULL; lptr = lptr->next, i++)
 96     {
 97         p2 = *(Vec2 *)lptr->to;
 98         l1 = line2_make(p1,p2,LINE_NO_FIT);
 99         lptrs[i] = l1;
100         p1 = p2;
101     }
102     llast = lptrs[0];
103     l1 = line2_make(p1,*pfirst,LINE_NO_FIT);
104     lptrs[i] = l1;
105  
106     l2 = line2_make(p1,p2,LINE_NO_FIT);
107     for (i = ly; i < uy; ++i)
108     {
109         p1 = vec2((double)lx -0.51, (double)(i)+0.51);
110         l2->p1 = p1;
111         l2->p = p1;
112         im_get_rowf(row1, im1, i, lx, ux);
113         for (k=0; k<lcount; k++)
114         {
115            label[k] = 1;
116         }
117         for (j = ux-1; j >=lx ; j--)
118         {
119             count = 0;
120             p2 = vec2((double)j +0.51, (double)(i)+0.51);
121             l2->p2 = p2;
122             l2->v = vec2_diff(p2, p1);
123             l2->length = (float)vec2_mod(l2->v);
124             l2->v = vec2_times(1.0 / l2->length, l2->v);
125             for (k=0 ; k<lcount; k++)
126             {
127                 l1 = lptrs[k];
128                 if (label[k])
129                 {
130                    get_intersection(l1,l2,&insct1,&insct2);
131                    if (insct1==true&&insct2==true) count++;
132                    else label[k] = 0;
133                    if (l1->p2.el[1] == l2->p2.el[1]
134                    && llast->p1.el[1] == l2->p2.el[1])
135                    {
136                        if ( (l1->p2.el[1] - l1->p1.el[1])*(llast->p2.el[1] - llast->p1.el[1]) < 0)
137                            count ++;
138                    }
139                 }
140                 llast = l1;
141             }
142             if( count%2 != 0) row2[j] = row1[j];
143             else row2[j] = 0.0;
144             if (count == 0)
145             {
146                 for (;j>=lx; j--) row2[j] = 0.0;
147                 break; 
148             }
149         }
150         im_put_rowf(row2,im1,i,lx,ux);
151     }
152     line2_free(l2);
153     for (k=0; k<lcount; k++)
154     {
155         line2_free(lptrs[k]);
156     }
157  
158     pvector_free((void *)lptrs, 0);
159     ivector_free((void *)label,0);
160     fvector_free((void *) row1, lx);
161     fvector_free((void *) row2, lx);
162 }
163 
164 void im_poly_crop(Imrect *im, List *poly)
165 {
166     if(im == NULL )
167         return;
168     switch(im->vtype)
169     {
170         case uchar_v:
171         case char_v:
172         case short_v:
173         case ushort_v:
174         case int_v:
175         case float_v:
176            imf_poly_crop(im,poly);
177         break;
178         case complex_v: 
179         break;
180     }
181 }
182 
183 

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