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 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU 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 * ANY users of TINA who require exemption from the existing licence must
20 * negotiate a new licence with Dr. Neil.A.Thacker, the sole agent for
21 * the University of Manchester.
22 *
23 **********
24 *
25 * Program : TINA
26 * File : $Source: geomImg_poly_crop.c $
27 * Date : $Date: 2012/06/21 11:40 $
28 * Version : $Revision: 1.6 $
29 *
30 * Author : Legacy TINA modified NAT/HR
31 *
32 * Notes : get intersection: software copied from pairwise code
33 *
34 *********
35 */
36
37
38 #if HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <math.h>
43 #include <tina/sys/sysDef.h>
44 #include <tina/sys/sysPro.h>
45 #include <tina/math/mathDef.h>
46 #include <tina/math/mathPro.h>
47 #include <tina/image/imgDef.h>
48 #include <tina/image/imgPro.h>
49 #include <tina/geometry/geomDef.h>
50 #include <tina/geometry/geom_LineDef.h>
51 #include <tina/geometry/geom_LinePro.h>
52 #include <tina/geometry/geomImg_poly_crop.h> /* HR */
53
54 #define FALSE 0
55 #define TRUE 1
56
57 Vec2 get_intersection(Line2 *l1,Line2 *l2,Bool *line1,Bool *line2)
58 {
59 Vec2 v1,v2,p,q,cp,b,isct;
60 float d1,d2,a;
61
62 v1 = l1->v;
63 v2 = l2->v;
64 *line1 = FALSE;
65 *line2 = FALSE;
66
67 p = l1->p1;
68 q = l2->p1;
69
70 isct.el[0] = p.el[0];
71 isct.el[1] = p.el[1];
72 /*
73 isct.el[0] = (l1->p1.el[0]+l1->p2.el[0])/2.0;
74 isct.el[1] = (l1->p1.el[1]+l1->p2.el[1])/2.0;
75 */
76 if (l1->label == l2->label)
77 {
78 /* *line1 = TRUE;*/
79 return(isct);
80 }
81 cp.el[0] = -v2.el[1];
82 cp.el[1] = v2.el[0];
83
84 a = (float)vec2_dot(cp,v1);
85 /* test this ... done NAT/PAR 8/12/93 */
86
87 #ifdef _PCC
88 if (fabs(a)< 0.0001)
89 {
90 a = (float)_copysign(0.0001,a);
91 return(isct);
92 }
93 #else
94 if (fabs(a)< 0.0001)
95 {
96 a = copysign(0.0001,a);
97 return(isct);
98 }
99 #endif
100
101 a = (float)vec2_dot(cp,vec2_diff(p,q))/a;
102 b = vec2_times(a,v1);
103
104 isct.el[0] = p.el[0] - b.el[0];
105 isct.el[1] = p.el[1] - b.el[1];
106
107 d1 = (float)vec2_dist(isct,l1->p1);
108 d2 = (float)vec2_dist(isct,l1->p2);
109 if((d1<fabs(l1->length)-0.001)&&(d2<=fabs(l1->length)-0.001)) /* NAT fixed */
110 *line1 = TRUE;
111
112 d1 = (float)vec2_dist(isct,l2->p1);
113 d2 = (float)vec2_dist(isct,l2->p2);
114 if((d1<fabs(l2->length)-0.001)&&(d2<=fabs(l2->length)-0.001)) /* NAT fixed */
115 *line2 = TRUE;
116
117 return(isct);
118 }
119
120 void imf_poly_crop(Imrect *im1, List *poly)
121 {
122 Imregion *roi;
123 float *row1, *row2;
124 Vec2 p1=vec2_zero(),p2=vec2_zero();
125 Vec2 *pfirst;
126 List *lptr;
127 Line2 *l1,*l2,*llast;
128 Line2 **lptrs;
129 int *label;
130 Bool insct1,insct2;
131 int lx, ux, ly, uy;
132 int i, j, k, count, lcount=0;
133
134 if (im1 == NULL)
135 return;
136
137 roi = im1->region;
138 if (roi == NULL)
139 return;
140 lx = roi->lx;
141 ux = roi->ux;
142 ly = roi->ly;
143 uy = roi->uy;
144
145 row1 = fvector_alloc(lx, ux);
146 row2 = fvector_alloc(lx, ux);
147 pfirst = (Vec2 *)poly->to;
148 p1 = *pfirst;
149 for (lptr = poly, lcount = 0; lptr!=NULL; lptr = lptr->next)
150 {
151 lcount++;
152 }
153 lptrs = (Line2 **)pvector_alloc(0,lcount);
154 label = (int *)ivector_alloc(0,lcount);
155
156 for (lptr = poly->next, i=0; lptr!=NULL; lptr = lptr->next, i++)
157 {
158 p2 = *(Vec2 *)lptr->to;
159 l1 = line2_make(p1,p2,LINE_NO_FIT);
160 lptrs[i] = l1;
161 p1 = p2;
162 }
163 llast = lptrs[0];
164 l1 = line2_make(p1,*pfirst,LINE_NO_FIT);
165 lptrs[i] = l1;
166
167 l2 = line2_make(p1,p2,LINE_NO_FIT);
168 for (i = ly; i < uy; ++i)
169 {
170 p1 = vec2((double)lx -0.51, (double)(i)+0.51);
171 l2->p1 = p1;
172 l2->p = p1;
173 im_get_rowf(row1, im1, i, lx, ux);
174 for (k=0; k<lcount; k++)
175 {
176 label[k] = 1;
177 }
178 for (j = ux-1; j >=lx ; j--)
179 {
180 count = 0;
181 p2 = vec2((double)j +0.51, (double)(i)+0.51);
182 l2->p2 = p2;
183 l2->v = vec2_diff(p2, p1);
184 l2->length = (float)vec2_mod(l2->v);
185 l2->v = vec2_times(1.0 / l2->length, l2->v);
186 for (k=0 ; k<lcount; k++)
187 {
188 l1 = lptrs[k];
189 if (label[k])
190 {
191 get_intersection(l1,l2,&insct1,&insct2);
192 if (insct1==true&&insct2==true) count++;
193 else label[k] = 0;
194 if (l1->p2.el[1] == l2->p2.el[1]
195 && llast->p1.el[1] == l2->p2.el[1])
196 {
197 if ( (l1->p2.el[1] - l1->p1.el[1])*(llast->p2.el[1] - llast->p1.el[1]) < 0)
198 count ++;
199 }
200 }
201 llast = l1;
202 }
203 if( count%2 != 0) row2[j] = row1[j];
204 else row2[j] = 0.0;
205 if (count == 0)
206 {
207 for (;j>=lx; j--) row2[j] = 0.0;
208 break;
209 }
210 }
211 im_put_rowf(row2,im1,i,lx,ux);
212 }
213 line2_free(l2);
214 for (k=0; k<lcount; k++)
215 {
216 line2_free(lptrs[k]);
217 }
218
219 /*
220 pvector_free((void *)lptrs, 0);
221 ivector_free((void *)label,0);
222 fvector_free((void *) row1, lx);
223 fvector_free((void *) row2, lx);
224 */
225 pvector_free(lptrs, 0); /* NAT fixed */
226 ivector_free(label,0); /* NAT fixed */
227 fvector_free(row1, lx); /* NAT fixed */
228 fvector_free(row2, lx); /* NAT fixed */
229
230 }
231
232 void im_poly_crop(Imrect *im, List *poly)
233 {
234 if(im == NULL )
235 return;
236 switch(im->vtype)
237 {
238 case uchar_v:
239 case char_v:
240 case short_v:
241 case ushort_v:
242 case int_v:
243 case float_v:
244 imf_poly_crop(im,poly);
245 break;
246 case complex_v:
247 break; /* NAT fixed */
248 }
249 }
250
251
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.