Subversion Repositories wimsdev

Rev

Rev 7840 | Rev 8160 | 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
 
18
static int _lcomp(const void *l1, const void *l2)
19
{
20
    const double *d1, *d2;
21
    double d;
22
    d1=(const double *) l1; d2=(const double *) l2;
23
    d=*d1-*d2;
24
    if(d<0) return -1; if(d>0) return 1;
25
    return 0;
26
}
27
 
28
static double lc_scalex(leveldata *ld, int x)
29
{
30
    if(x<0) x=0; if(x>=ld->xsize) x=ld->xsize-1;
31
    return ld->xspan*((double) x - 0.40127)/ld->xsize+ld->xrange[0];
32
}
33
 
34
static double lc_scaley(leveldata *ld, int y)
35
{
36
    if(y<0) y=0; if(y>=ld->ysize) y=ld->ysize-1;
37
    return -ld->yspan*((double) y - 0.40127)/ld->ysize+ld->yrange[1];
38
}
39
 
40
static int _getlevel(leveldata *ld, int x, int y)
41
{
42
    double dd;
43
    int i;
7840 bpr 44
 
10 reyssat 45
    eval_setval(ld->xevpos,lc_scalex(ld,x));
46
    eval_setval(ld->yevpos,lc_scaley(ld,y));
7847 bpr 47
    dd=checked_eval(ld->fn);
10 reyssat 48
    for(i=0;i<ld->levelcnt && dd>ld->levels[i];i++);
49
    return i;
50
}
51
 
52
static void lc_replacexy(leveldata *ld)
53
{
54
    char *pp;
55
    for(pp=varchr(ld->fn,ld->xname);pp!=NULL;pp=varchr(pp,ld->xname)) {
7840 bpr 56
      string_modify(ld->fn,pp,pp+strlen(ld->xname),EV_X);
57
      pp+=strlen(EV_X);
10 reyssat 58
    }
59
    for(pp=varchr(ld->fn,ld->yname);pp!=NULL;pp=varchr(pp,ld->yname)) {
7840 bpr 60
      string_modify(ld->fn,pp,pp+strlen(ld->yname),EV_Y);
61
      pp+=strlen(EV_Y);
10 reyssat 62
    }
63
}
64
 
7840 bpr 65
/* produces level curve data. Returns non-zero if error. */
10 reyssat 66
int levelcurve(leveldata *ld)
67
{
68
    int xx,yy,xi,yi,xsteps,ysteps;
69
    int i,x,y,xt,yt;
70
    short int l1[LEVELSIZE_LIM+16], l2[LEVELSIZE_LIM+16];
71
    short int l3[LEVELGRAIN_LIM+2][LEVELGRAIN_LIM+2];
72
    char f[MAX_LINELEN+1];
7840 bpr 73
 
10 reyssat 74
    ld->datacnt=0;
75
    if(ld->fn==NULL || *(ld->fn)==0 || ld->levelcnt<1) return 1;
76
    if(ld->grain<1) ld->grain=4;
77
    if(ld->grain>LEVELGRAIN_LIM) ld->grain=LEVELGRAIN_LIM;
78
    if(ld->levelcnt>LEVEL_LIM) ld->levelcnt=LEVEL_LIM;
79
    if(ld->xsize<10 || ld->xsize>LEVELSIZE_LIM) return 2;
80
    if(ld->ysize<10 || ld->ysize>LEVELSIZE_LIM) return 2;
81
    if(check_parentheses(ld->fn,0)) return 3;
82
    ld->xspan=ld->xrange[1]-ld->xrange[0];
83
    ld->yspan=ld->yrange[1]-ld->yrange[0];
84
    if(ld->levelcnt>1) qsort(ld->levels,ld->levelcnt,sizeof(double),_lcomp);
85
    if(ld->xname==NULL || *(ld->xname)==0) ld->xname="x";
86
    if(ld->yname==NULL || *(ld->yname)==0) ld->yname="y";
87
    snprintf(f,sizeof(f),"%s",ld->fn); substitute(f); ld->fn=f;
88
    ld->xevpos=eval_getpos(EV_X); ld->yevpos=eval_getpos(EV_Y);
89
    if(ld->xevpos<0 || ld->yevpos<0) return 4;
90
    lc_replacexy(ld); evalue_compile(f);
91
    xsteps=(ld->xsize+ld->grain-1)/ld->grain+1;
92
    ysteps=(ld->ysize+ld->grain-1)/ld->grain+1;
93
    for(yy=yi=0;yi<ysteps;yy+=ld->grain,yi++) l2[yi]=_getlevel(ld,0,yy);
94
    l2[ysteps]=l2[ysteps-1];
95
    for(xi=1,xx=ld->grain;xi<xsteps && ld->datacnt<LEVELPOINT_LIM;xx+=ld->grain,xi++) {
7840 bpr 96
      memmove(l1,l2,(ysteps+1)*sizeof(short int));
97
      for(yi=yy=0;yi<ysteps;yy+=ld->grain,yi++) l2[yi]=_getlevel(ld,xx,yy);
98
      l2[ysteps]=l2[ysteps-1];
99
      for(yi=0;yi<ysteps && ld->datacnt<LEVELPOINT_LIM;yi++)
100
        if(l1[yi]!=l1[yi+1] || l1[yi]!=l2[yi] || l1[yi]!=l2[yi+1]) {
101
            for(x=0,xt=(xi-1)*ld->grain;x<=ld->grain;x++,xt++)
7847 bpr 102
              for(y=0,yt=yi*ld->grain;y<=ld->grain;y++,yt++) {
7840 bpr 103
                if(x==0 && y==0) {l3[x][y]=l1[yi]; continue;}
104
                if(x==0 && y==ld->grain) {l3[x][y]=l1[yi+1]; continue;}
105
                if(x==ld->grain && y==0) {l3[x][y]=l2[yi]; continue;}
106
                if(x==ld->grain && y==ld->grain) {l3[x][y]=l2[yi+1]; continue;}
107
                l3[x][y]=_getlevel(ld,xt,yt);
7847 bpr 108
              }
7840 bpr 109
            for(x=0,xt=(xi-1)*ld->grain;x<ld->grain;x++,xt++)
7847 bpr 110
              for(y=0,yt=yi*ld->grain;y<ld->grain;y++,yt++) {
7840 bpr 111
                i=l3[x][y];
112
                if((i!=l3[x][y+1] || i!=l3[x+1][y]) &&
113
                   ld->datacnt<LEVELPOINT_LIM &&
114
                   (i!=l3[x][y+1] || ld->datacnt==0 ||
115
                  l3[x+1][y+1]==l3[x+1][y] ||
116
                  ld->xdata[ld->datacnt-1]!=xt ||
117
                  ld->ydata[ld->datacnt-1]!=yt-1)) {
118
                  ld->xdata[ld->datacnt]=xt;
119
                  ld->ydata[ld->datacnt++]=yt;
120
                }
7847 bpr 121
              }
7840 bpr 122
        }
10 reyssat 123
    }
124
    return 0;
125
}
126