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

Linux Cross Reference
Tina5/tina-libs/tina/geometry/geomImg_poly_crop.c

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

  1 /**********
  2  * 
  3  * This file is part of the TINA Open Source Image Analysis Environment
  4  * henceforth known as TINA
  5  *
  6  * TINA is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as 
  8  * published by the Free Software Foundation.
  9  *
 10  * TINA is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public License
 16  * along with TINA; if not, write to the Free Software Foundation, Inc., 
 17  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  *
 19  **********
 20  * 
 21  * Program :    TINA
 22  * File    :  $Source: /home/tina/cvs/tina-libs/tina/geometry/geomImg_poly_crop.c,v $
 23  * Date    :  $Date: 2005/01/23 19:10:21 $
 24  * Version :  $Revision: 1.5 $
 25  * CVS Id  :  $Id: geomImg_poly_crop.c,v 1.5 2005/01/23 19:10:21 paul Exp $
 26  *
 27  * Author  : Legacy TINA
 28  *
 29  * Notes : get intersection: software copied from pairwise code
 30  *
 31  *********
 32 */
 33 
 34 #include "geomImg_poly_crop.h"
 35 
 36 #if HAVE_CONFIG_H
 37   #include <config.h>
 38 #endif
 39 
 40 #include <math.h>
 41 #include <tina/sys/sysDef.h>
 42 #include <tina/sys/sysPro.h>
 43 #include <tina/math/mathDef.h>
 44 #include <tina/math/mathPro.h>
 45 #include <tina/image/imgDef.h>
 46 #include <tina/image/imgPro.h>
 47 #include <tina/geometry/geomDef.h>
 48 #include <tina/geometry/geom_LineDef.h>
 49 #include <tina/geometry/geom_LinePro.h>
 50 
 51 #define FALSE 0
 52 #define TRUE 1
 53 
 54 Vec2 get_intersection(Line2 *l1,Line2 *l2,Bool *line1,Bool *line2)
 55 {
 56    Vec2 v1,v2,p,q,cp,b,isct;
 57    float d1,d2,a;
 58  
 59    v1 = l1->v;
 60    v2 = l2->v;
 61    *line1 = FALSE;
 62    *line2 = FALSE;
 63  
 64     p = l1->p1;
 65     q = l2->p1;
 66 
 67   isct.el[0] = p.el[0];
 68   isct.el[1] = p.el[1];
 69   if (l1 == l2) return(isct);
 70 
 71     cp.el[0] = -v2.el[1];
 72     cp.el[1] = v2.el[0];
 73  
 74     a = (float)vec2_dot(cp,v1);
 75 /* test this ... done NAT/PAR 8/12/93 */
 76 #ifdef _PCC
 77     if (fabs(a)< 0.0001)
 78     {
 79        a = (float)_copysign(0.0001,a);
 80        return(isct);
 81     }
 82 #else
 83     if (fabs(a)< 0.0001)
 84     {
 85        a = copysign(0.0001,a);
 86        return(isct);
 87     }
 88 #endif
 89     a = (float)vec2_dot(cp,vec2_diff(p,q))/a;
 90     b = vec2_times(a,v1);
 91  
 92     isct.el[0] = p.el[0] - b.el[0];
 93     isct.el[1] = p.el[1] - b.el[1];
 94  
 95     d1 = (float)vec2_dist(isct,l1->p1);
 96     d2 = (float)vec2_dist(isct,l1->p2);
 97     if((d1<fabs(l1->length)+0.001)&&(d2<=fabs(l1->length)+0.001))
 98       *line1 = TRUE;
 99  
100     d1 = (float)vec2_dist(isct,l2->p1);
101     d2 = (float)vec2_dist(isct,l2->p2);
102     if((d1<fabs(l2->length)+0.001)&&(d2<=fabs(l2->length)+0.001))
103       *line2 = TRUE;
104  
105     return(isct);
106 }
107 
108 void    imf_poly_crop(Imrect *im1, List *poly)
109 {
110     Imregion *roi;
111     float  *row1, *row2;
112     Vec2    p1=vec2_zero(),p2=vec2_zero();
113     Vec2   *pfirst;
114     List   *lptr;
115     Line2  *l1,*l2,*llast;
116     Line2  **lptrs;
117     int    *label;
118     Bool    insct1,insct2;
119     int     lx, ux, ly, uy;
120     int     i, j, k, count, lcount=0;
121  
122     if (im1 == NULL)
123         return;
124  
125     roi = im1->region;
126     if (roi == NULL)
127         return;
128     lx = roi->lx;
129     ux = roi->ux;
130     ly = roi->ly;
131     uy = roi->uy;
132  
133     row1 = fvector_alloc(lx, ux);
134     row2 = fvector_alloc(lx, ux);
135     pfirst = (Vec2 *)poly->to;
136     p1 = *pfirst;
137     for (lptr = poly, lcount = 0; lptr!=NULL; lptr = lptr->next)
138     {
139        lcount++;
140     }
141     lptrs = (Line2 **)pvector_alloc(0,lcount);
142     label = (int *)ivector_alloc(0,lcount);
143     
144     for (lptr = poly->next, i=0; lptr!=NULL; lptr = lptr->next, i++)
145     {
146         p2 = *(Vec2 *)lptr->to;
147         l1 = line2_make(p1,p2,LINE_NO_FIT);
148         lptrs[i] = l1;
149         p1 = p2;
150     }
151     llast = lptrs[0];
152     l1 = line2_make(p1,*pfirst,LINE_NO_FIT);
153     lptrs[i] = l1;
154  
155     l2 = line2_make(p1,p2,LINE_NO_FIT);
156     for (i = ly; i < uy; ++i)
157     {
158         p1 = vec2((double)lx -0.51, (double)(i)+0.51);
159         l2->p1 = p1;
160         l2->p = p1;
161         im_get_rowf(row1, im1, i, lx, ux);
162         for (k=0; k<lcount; k++)
163         {
164            label[k] = 1;
165         }
166         for (j = ux-1; j >=lx ; j--)
167         {
168             count = 0;
169             p2 = vec2((double)j +0.51, (double)(i)+0.51);
170             l2->p2 = p2;
171             l2->v = vec2_diff(p2, p1);
172             l2->length = (float)vec2_mod(l2->v);
173             l2->v = vec2_times(1.0 / l2->length, l2->v);
174             for (k=0 ; k<lcount; k++)
175             {
176                 l1 = lptrs[k];
177                 if (label[k])
178                 {
179                    get_intersection(l1,l2,&insct1,&insct2);
180                    if (insct1==true&&insct2==true) count++;
181                    else label[k] = 0;
182                    if (l1->p2.el[1] == l2->p2.el[1]
183                    && llast->p1.el[1] == l2->p2.el[1])
184                    {
185                        if ( (l1->p2.el[1] - l1->p1.el[1])*(llast->p2.el[1] - llast->p1.el[1]) < 0)
186                            count ++;
187                    }
188                 }
189                 llast = l1;
190             }
191             if( count%2 != 0) row2[j] = row1[j];
192             else row2[j] = 0.0;
193             if (count == 0)
194             {
195                 for (;j>=lx; j--) row2[j] = 0.0;
196                 break; 
197             }
198         }
199         im_put_rowf(row2,im1,i,lx,ux);
200     }
201     line2_free(l2);
202     for (k=0; k<lcount; k++)
203     {
204         line2_free(lptrs[k]);
205     }
206  
207     pvector_free(lptrs, 0);
208     ivector_free(label,0);
209     fvector_free(row1, lx);
210     fvector_free(row2, lx);
211 }
212 
213 void im_poly_crop(Imrect *im, List *poly)
214 {
215     if(im == NULL )
216         return;
217     switch(im->vtype)
218     {
219         case uchar_v:
220         case char_v:
221         case short_v:
222         case ushort_v:
223         case int_v:
224         case float_v:
225            imf_poly_crop(im,poly);
226         break;
227         case complex_v: 
228         break;
229         default:
230         break;
231     }
232 }
233 
234 

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