Rev 7676 | Rev 8185 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | |||
7676 | bpr | 18 | /* Compare two curves */ |
10 | reyssat | 19 | /* Input parameters: environment. |
7676 | bpr | 20 | * |
10 | reyssat | 21 | * w_curvecomp_1 and w_curvecomp_2: curves to compare, as lists of points. |
22 | * w_curvecomp_xrange and w_curvecomp_yrange: list of 2 integers each. |
||
23 | * w_curvecomp_tolerance: Maximal tolerance of distances. |
||
7676 | bpr | 24 | * |
10 | reyssat | 25 | * Output: 10 double numbers separated by white spaces. |
7676 | bpr | 26 | * |
10 | reyssat | 27 | * Number 1: Average distance of curve 1 with respect to curve 2. |
28 | * Number 2: Average distance of curve 2 with respect to curve 1. |
||
29 | * Number 3: Maximal distance of curve 1 with respect to curve 2. |
||
30 | * Number 4: Maximal distance of curve 2 with respect to curve 1. |
||
31 | * Number 5: Proportion of curve 1 close to curve 2. |
||
32 | * Number 6: Proportion of curve 2 close to curve 1. |
||
33 | * Number 7: Maximal jump of curve 1. |
||
34 | * Number 8: Maximal jump of curve 2. |
||
35 | * Number 9: Ratio of repetitions found in curve 1. |
||
36 | * Number 10: Ratio of repetitions found in curve 2. |
||
37 | * Furthermore, words "fnofx" and/or "fnofy" will appear if curve 2 |
||
38 | * represents the graph of a function of x (and/or y). |
||
7676 | bpr | 39 | * |
10 | reyssat | 40 | * Returns empty if one of the curves is degenerated. |
41 | */ |
||
42 | |||
43 | /*************** Customization: change values hereafter ****************/ |
||
44 | |||
7676 | bpr | 45 | /* limit of point buffers */ |
46 | #define pointlim 4096 |
||
10 | reyssat | 47 | #define default_tolerance 10 |
48 | |||
49 | /***************** Nothing should need change hereafter *****************/ |
||
50 | |||
51 | #include "../wims.h" |
||
8147 | bpr | 52 | #include "../Lib/libwims.h" |
53 | |||
10 | reyssat | 54 | char curve1[2*MAX_LINELEN+2], curve2[2*MAX_LINELEN+2]; |
55 | int bx[2], by[2]; |
||
56 | int tol=default_tolerance; |
||
57 | int xfn=0, yfn=0; |
||
58 | int discrete; |
||
59 | double dist1, dist2, max1, max2, ratio1, ratio2, rep1, rep2; |
||
60 | double djump1, djump2; |
||
61 | struct cv { |
||
62 | int x,y, closest, rep; |
||
63 | double dist; |
||
64 | } cv1[pointlim], cv2[pointlim]; |
||
65 | int points1,points2; |
||
66 | |||
67 | int Abs(int t) {if(t>=0) return t; else return -t;} |
||
68 | int Min(int x,int y) {if(x>y) return y; else return x;} |
||
69 | int Max(int x,int y) {if(x<y) return y; else return x;} |
||
70 | |||
71 | int listbuf[pointlim*2], listcnt; |
||
72 | |||
73 | void reverse(struct cv *cvbuf, int cnt) |
||
74 | { |
||
75 | int i; |
||
76 | struct cv cvt; |
||
77 | for(i=0;i<cnt/2;i++) { |
||
7676 | bpr | 78 | memmove(&cvt,cvbuf+i,sizeof(cvt)); |
79 | memmove(cvbuf+i,cvbuf+(cnt-i-1),sizeof(cvt)); |
||
80 | memmove(cvbuf+(cnt-i-1),&cvt,sizeof(cvt)); |
||
10 | reyssat | 81 | } |
82 | } |
||
83 | |||
84 | int str2list(char *p, int lim) |
||
85 | { |
||
86 | char *p2; |
||
87 | for(p2=strchr(p,';'); p2; p2=strchr(p2+1,';')) *p2=','; |
||
88 | for(listcnt=0; p && listcnt<lim; p=p2) { |
||
7676 | bpr | 89 | p2=strchr(p,','); if(p2!=NULL) *p2++=0; |
90 | p=find_word_start(p); if(*p==0) continue; |
||
91 | listbuf[listcnt++]=atoi(p); |
||
10 | reyssat | 92 | } |
93 | return listcnt; |
||
94 | } |
||
95 | |||
96 | int list2curve(struct cv *cvbuf) |
||
97 | { |
||
98 | int i, j, m, t, st, xx, yy, x2, y2, x3, y3, ll; |
||
99 | |||
100 | ll=listcnt/2; if(ll<2) return 0; |
||
101 | xx=listbuf[0]; yy=listbuf[1]; |
||
102 | j=0; if(xx>=bx[0] && xx<=bx[1] && yy>=by[0] && yy<=by[1]) { |
||
7676 | bpr | 103 | cvbuf[0].x=xx; cvbuf[0].y=yy; j++; |
10 | reyssat | 104 | } |
105 | for(i=1; i<ll && j<pointlim; i++) { |
||
7676 | bpr | 106 | x2=listbuf[2*i]; y2=listbuf[2*i+1]; |
107 | m=Max(Abs(x2-xx),Abs(y2-yy)); if(m<=0) continue; |
||
108 | if(discrete==1) st=m; else st=1; |
||
109 | for(t=st; t<=m && j<pointlim; t++) { |
||
110 | x3=(double) (x2*t+xx*(m-t))/m+0.5; |
||
111 | y3=(double) (y2*t+yy*(m-t))/m+0.5; |
||
112 | if(x3>=bx[0] && x3<=bx[1] && y3>=by[0] && y3<=by[1]) { |
||
113 | cvbuf[j].x=x3; cvbuf[j].y=y3; cvbuf[j].dist=-1; |
||
114 | cvbuf[j].rep=0; j++; |
||
115 | } |
||
116 | } |
||
117 | xx=x2; yy=y2; |
||
10 | reyssat | 118 | } |
119 | return j; |
||
120 | } |
||
121 | |||
122 | void compare(void) |
||
123 | { |
||
124 | int i, j, cl; |
||
125 | double d, dt; |
||
7676 | bpr | 126 | |
10 | reyssat | 127 | d=2*pointlim; cl=-1; |
128 | for(i=0,djump1=1;i<points1-1;i++) { |
||
7676 | bpr | 129 | dt=sqrt(pow(cv1[i].x-cv1[i+1].x,2)+pow(cv1[i].y-cv1[i+1].y,2)); |
130 | if(dt>djump1) djump1=dt; |
||
10 | reyssat | 131 | } |
132 | for(i=0,djump2=1;i<points2-1;i++) { |
||
7676 | bpr | 133 | dt=sqrt(pow(cv2[i].x-cv2[i+1].x,2)+pow(cv2[i].y-cv2[i+1].y,2)); |
134 | if(dt>djump2) djump2=dt; |
||
10 | reyssat | 135 | } |
136 | for(i=0;i<points1;i++) { |
||
7676 | bpr | 137 | for(j=0;j<points2;j++) { |
138 | dt=sqrt(pow(cv2[j].x-cv1[i].x,2)+pow(cv2[j].y-cv1[i].y,2)); |
||
139 | if(dt<d) {d=dt; cl=j;} |
||
140 | else {dt=(dt-d)/djump2; if(dt>2) j+=dt-1;} |
||
141 | } |
||
142 | cv1[i].dist=d; cv1[i].closest=cl; |
||
143 | if(i<points1) |
||
144 | d+=sqrt(pow(cv1[i].x-cv1[i+1].x,2)+pow(cv1[i].y-cv1[i+1].y,2))+0.1; |
||
10 | reyssat | 145 | } |
146 | d=2*pointlim; for(i=0;i<points2;i++) { |
||
7676 | bpr | 147 | for(j=0;j<points1;j++) { |
148 | dt=sqrt(pow(cv1[j].x-cv2[i].x,2)+pow(cv1[j].y-cv2[i].y,2)); |
||
149 | if(dt<d) {d=dt; cl=j;} |
||
150 | else {dt=(dt-d)/djump1; if(dt>2) j+=dt-1;} |
||
151 | } |
||
152 | cv2[i].dist=d; cv2[i].closest=cl; |
||
153 | if(i<points2) |
||
154 | d+=sqrt(pow(cv2[i].x-cv2[i+1].x,2)+pow(cv2[i].y-cv2[i+1].y,2))+0.1; |
||
10 | reyssat | 155 | } |
156 | for(i=1, cl=cv1[0].closest;i<points1;i++) { |
||
7676 | bpr | 157 | j=cv1[i].closest; if(j!=cl) cv2[j].rep++; |
158 | cl=j; |
||
10 | reyssat | 159 | } |
160 | for(i=1, cl=cv2[0].closest;i<points2;i++) { |
||
7676 | bpr | 161 | j=cv2[i].closest; if(j!=cl) cv1[j].rep++; |
162 | cl=j; |
||
10 | reyssat | 163 | } |
164 | for(i=cl=0; i<points1; i++) if(cv1[i].rep>1) cl+=cv1[i].rep-1; |
||
165 | rep1=(double) cl/points1; |
||
166 | for(i=cl=0; i<points1; i++) if(cv2[i].rep>1) cl+=cv2[i].rep-1; |
||
167 | rep2=(double) cl/points2; |
||
168 | } |
||
169 | |||
170 | void check(void) |
||
171 | { |
||
172 | int i,j,xret1,yret1,xret2,yret2; |
||
173 | int xx,yy,xmin,xmax,ymin,ymax; |
||
174 | double d; |
||
175 | max1=max2=0; |
||
176 | for(i=j=0,d=0;i<points1;i++) { |
||
7676 | bpr | 177 | if(cv1[i].dist<=tol) j++; |
178 | d+=pow(cv1[i].dist,4); |
||
179 | if(max1<cv1[i].dist) max1=cv1[i].dist; |
||
10 | reyssat | 180 | } |
181 | ratio1=(double) j/points1; dist1=pow(d/points1,0.25)*1.8; |
||
182 | for(i=j=0,d=0;i<points2;i++) { |
||
7676 | bpr | 183 | if(cv2[i].dist<=tol) j++; |
184 | d+=pow(cv2[i].dist,4); |
||
185 | if(max2<cv2[i].dist) max2=cv2[i].dist; |
||
10 | reyssat | 186 | } |
187 | ratio2=(double) j/points2; dist2=pow(d/points2,0.25)*1.8; |
||
188 | xret1=xret2=yret1=yret2=0; |
||
189 | xmin=xmax=cv2[0].x; ymin=ymax=cv2[0].y; |
||
190 | for(i=1;i<points2;i++) { |
||
7676 | bpr | 191 | xx=cv2[i].x; yy=cv2[i].y; |
192 | xret1=Max(xret1,xmax-xx); xret2=Max(xret2,xx-xmin); |
||
193 | yret1=Max(yret1,ymax-yy); yret2=Max(yret2,yy-ymin); |
||
194 | xmin=Min(xx,xmin);xmax=Max(xx,xmax); |
||
195 | ymin=Min(yy,ymin);ymax=Max(yy,ymax); |
||
10 | reyssat | 196 | } |
197 | if(Min(xret1,xret2)<=2) xfn=1; |
||
198 | if(Min(yret1,yret2)<=2) yfn=1; |
||
199 | } |
||
200 | |||
201 | void output(void) |
||
202 | { |
||
203 | printf("%.2f %.2f %.2f %.2f \ |
||
204 | %.3f %.3f %.1f %.1f \ |
||
205 | %.3f %.3f", |
||
7676 | bpr | 206 | dist1, dist2, max1, max2, |
207 | ratio1, ratio2, djump1, djump2, |
||
208 | rep1, rep2); |
||
10 | reyssat | 209 | if(xfn) printf(" fx"); |
210 | if(yfn) printf(" fy"); |
||
211 | } |
||
212 | |||
213 | void test(void) |
||
214 | { |
||
215 | int i; |
||
216 | for(i=0;i<points1;i++) printf("%d,%d,%d,%.2f\n", |
||
7676 | bpr | 217 | cv1[i].x,cv1[i].y,cv1[i].closest,cv1[i].dist); |
10 | reyssat | 218 | printf("\n"); |
219 | for(i=0;i<points2;i++) printf("%d,%d,%d,%.2f\n", |
||
7676 | bpr | 220 | cv2[i].x,cv2[i].y,cv2[i].closest,cv2[i].dist); |
10 | reyssat | 221 | printf("\n"); |
222 | } |
||
223 | |||
224 | int main(int argc, char *argv[]) |
||
225 | { |
||
226 | int i; |
||
227 | char *c1, *c2, *op; |
||
228 | c1=getenv("w_curvecomp_1"); c2=getenv("w_curvecomp_2"); |
||
7676 | bpr | 229 | /* nothing to do */ |
10 | reyssat | 230 | if(c1==NULL || c2==NULL || *c1==0 || *c2==0) return 0; |
231 | snprintf(curve1,sizeof(curve1),"%s",c1); |
||
232 | snprintf(curve2,sizeof(curve2),"%s",c2); |
||
233 | bx[0]=by[0]=bx[1]=by[1]=-1; |
||
234 | c1=getenv("w_curvecomp_xrange"); c2=getenv("w_curvecomp_yrange"); |
||
235 | if(c1!=NULL && *c1!=0) { |
||
7676 | bpr | 236 | str2list(c1,2); for(i=0;i<listcnt;i++) bx[i]=listbuf[i]; |
10 | reyssat | 237 | } |
238 | if(c2!=NULL && *c2!=0) { |
||
7676 | bpr | 239 | str2list(c2,2); for(i=0;i<listcnt;i++) by[i]=listbuf[i]; |
10 | reyssat | 240 | } |
241 | op=getenv("w_curvecomp_option"); if(op==NULL) op=""; |
||
242 | if(bx[0]==-1) bx[0]=0; if(bx[1]==-1) bx[1]=pointlim; |
||
243 | if(by[0]==-1) by[0]=0; if(by[1]==-1) by[1]=pointlim; |
||
244 | c1=getenv("w_curvecomp_tolerance"); |
||
245 | if(c1!=NULL && *c1!=0) tol=atoi(c1); |
||
246 | tol=Min(30,Max(5,tol)); |
||
7676 | bpr | 247 | |
10 | reyssat | 248 | if(strstr(op,"discrete1")!=NULL) discrete=1; else discrete=0; |
249 | str2list(curve1,pointlim*2); points1=list2curve(cv1); |
||
250 | if(strstr(op,"discrete2")!=NULL) discrete=1; else discrete=0; |
||
251 | str2list(curve2,pointlim*2); points2=list2curve(cv2); |
||
252 | if(points1<2 || points2<2) return 0; |
||
253 | compare(); check(); output(); |
||
254 | return 0; |
||
255 | } |