Back to index

radiance  4R0+20100331
plotin.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: plotin.c,v 1.2 2003/11/15 02:13:37 schorsch Exp $";
00003 #endif
00004 /*
00005  *  Program to convert plot(5) files to metafiles
00006  *
00007  *  cc -o plotin plotin.c primout.o mfio.o syscalls.o misc.o -lm
00008  *
00009  *     5/20/85
00010  */
00011 
00012 #include  <string.h>
00013 
00014 #include  "meta.h"
00015 #include  "plot.h"
00016 
00017 #define  getsi(f, m)  ICONV(geti(f)-(m), size)
00018 
00019 
00020 
00021 char  *progname;
00022 
00023 static int  xmin = 0, ymin = 0,           /* current space settings */
00024            size =  XYSIZE;
00025 
00026 static int  curx = -1,             /* current position */
00027            cury = -1;
00028 
00029 static short  curmod = 0;   /* current line drawing mode */
00030 
00031 static void convert(FILE  *infp);
00032 static int geti(FILE  *fp);
00033 static int quadr(int x, int y, int xp, int yp);
00034 static void step(int d);
00035 
00036 static void linemod(char  s[]);
00037 static void erase(void);
00038 static void space(int  xmn, int ymn, int xmx, int ymx);
00039 static void move(int  x, int  y);
00040 static void cont(int  x, int  y);
00041 static void line(int  x0, int y0, int x1, int y1);
00042 static void label(register char  *s);
00043 static void point(int  x, int  y);
00044 static void arc(int x, int y, int x0, int y0, int x1, int y1);
00045 static void circle(int x, int y, int r);
00046 
00047 
00048 int
00049 main(
00050        int  argc,
00051        char  **argv
00052 )
00053 {
00054  FILE  *fp;
00055 
00056  progname = *argv++;
00057  argc--;
00058 
00059  while (argc && **argv == '-')  {
00060     switch (*(*argv+1))  {
00061        default:
00062          error(WARNING, "unknown option");
00063          break;
00064        }
00065     argv++;
00066     argc--;
00067     }
00068 
00069  if (argc)
00070     while (argc)  {
00071          fp = efopen(*argv, "r");
00072          convert(fp);
00073          fclose(fp);
00074          argv++;
00075          argc--;
00076          }
00077  else
00078     convert(stdin);
00079 
00080  pglob(PEOF, 0200, NULL);
00081 
00082  return(0);
00083 }
00084 
00085 
00086 
00087 
00088 void
00089 convert(             /* convert to meta-file */
00090        FILE  *infp
00091 )
00092 {
00093     char  *fgets(), sbuf[BUFSIZ];
00094     int  command;
00095     int  a1, a2, a3, a4, a5, a6;
00096 
00097     while ((command = getc(infp)) != EOF)
00098        switch (command) {
00099            case 'm':
00100               a1 = getsi(infp, xmin);
00101               a2 = getsi(infp, ymin);
00102               move(a1, a2);
00103               break;
00104            case 'n':
00105               a1 = getsi(infp, xmin);
00106               a2 = getsi(infp, ymin);
00107               cont(a1, a2);
00108               break;
00109            case 'p':
00110               a1 = getsi(infp, xmin);
00111               a2 = getsi(infp, ymin);
00112               point(a1, a2);
00113               break;
00114            case 'l':
00115               a1 = getsi(infp, xmin);
00116               a2 = getsi(infp, ymin);
00117               a3 = getsi(infp, xmin);
00118               a4 = getsi(infp, ymin);
00119               line(a1, a2, a3, a4);
00120               break;
00121            case 't':
00122               fgets(sbuf, sizeof(sbuf), infp);
00123               sbuf[strlen(sbuf)-1] = '\0';
00124               label(sbuf);
00125               break;
00126            case 'a':
00127               a1 = getsi(infp, xmin);
00128               a2 = getsi(infp, ymin);
00129               a3 = getsi(infp, xmin);
00130               a4 = getsi(infp, ymin);
00131               a5 = getsi(infp, xmin);
00132               a6 = getsi(infp, ymin);
00133               arc(a1, a2, a3, a4, a5, a6);
00134               break;
00135            case 'c':
00136               a1 = getsi(infp, xmin);
00137               a2 = getsi(infp, ymin);
00138               a3 = getsi(infp, 0);
00139               circle(a1, a2, a3);
00140               break;
00141            case 'e':
00142               erase();
00143               break;
00144            case 'f':
00145               fgets(sbuf, sizeof(sbuf), infp);
00146               linemod(sbuf);
00147               break;
00148            case 's':
00149               a1 = geti(infp);
00150               a2 = geti(infp);
00151               a3 = geti(infp);
00152               a4 = geti(infp);
00153               space(a1, a2, a3, a4);
00154               break;
00155            default:
00156               error(USER, "unknown command in convert");
00157               break;
00158        }
00159 }
00160 
00161 
00162 
00163 
00164 int
00165 geti(         /* get two-byte integer from file */
00166        register FILE  *fp
00167 )
00168 {
00169     register int  i;
00170 
00171     i = getc(fp);
00172     i |= getc(fp) << 8;
00173     if (i & 0100000)
00174        i |= ~0177777;              /* manual sign extend */
00175     if (feof(fp))
00176        error(USER, "unexpected end of file in geti");
00177     
00178     return(i);
00179 }
00180 
00181 
00182 
00183 void
00184 move(                /* move to new position */
00185        register int  x,
00186        register int  y
00187 )
00188 {
00189     curx = x;
00190     cury = y;
00191 }
00192 
00193 
00194 
00195 void
00196 cont(                /* draw line to point */
00197        register int  x,
00198        register int  y
00199 )
00200 {
00201     plseg(curmod, curx, cury, x, y);
00202     curx = x;
00203     cury = y;
00204 }
00205 
00206 
00207 void
00208 point(               /* draw a point */
00209        register int  x,
00210        register int  y
00211 )
00212 {
00213     plseg(0, x, y, x, y);
00214     curx = x;
00215     cury = y;
00216 }
00217 
00218 
00219 
00220 void
00221 line(         /* draw a line segment */
00222        int  x0,
00223        int y0,
00224        int x1,
00225        int y1
00226 )
00227 {
00228     move(x0, y0);
00229     cont(x1, y1);
00230 }
00231 
00232 
00233 void
00234 label(               /* print a label */
00235        register char  *s
00236 )
00237 {
00238     pprim(PMSTR, 0, curx, cury, curx, cury, s);
00239 }
00240 
00241 
00242 
00243 static int del = 100;
00244 
00245 void
00246 step(
00247        int d
00248 )
00249 {
00250        del = d;
00251 }
00252 
00253 void
00254 arc(
00255        int x,
00256        int y,
00257        int x0,
00258        int y0,
00259        int x1,
00260        int y1
00261 )
00262 {
00263        double pc;
00264        double sqrt();
00265        int flg,m,xc,yc,xs,ys,qs,qf;
00266        float dx,dy,r;
00267        char use;
00268        dx = x-x0;
00269        dy = y-y0;
00270        r = dx*dx+dy*dy;
00271        pc = r;
00272        pc = sqrt(pc);
00273        flg = pc/4;
00274        if(flg == 0)step(1);
00275        else if(flg < del)step(flg);
00276        xc = xs = x0;
00277        yc = ys = y0;
00278        move(xs,ys);
00279        if(x0 == x1 && y0 == y1)flg=0;
00280        else flg=1;
00281        qs = quadr(x,y,x0,y0);
00282        qf = quadr(x,y,x1,y1);
00283        if(abs(x-x1) < abs(y-y1)){
00284               use = 'x';
00285               if(qs == 2 || qs ==3)m = -1;
00286               else m=1;
00287        }
00288        else {
00289               use = 'y';
00290               if(qs > 2)m= -1;
00291               else m= 1;
00292        }
00293        while(1){
00294               switch(use){
00295               case 'x':     
00296                      if(qs == 2 || qs == 3)yc -= del;
00297                      else yc += del;
00298                      dy = yc-y;
00299                      pc = r-dy*dy;
00300                      xc = m*sqrt(pc)+x;
00301                      if((x < xs && x >= xc) || ( x > xs && x <= xc) ||
00302                          (y < ys && y >= yc) || ( y > ys && y <=  yc) )
00303                      {
00304                             if(++qs > 4)qs=1;
00305                             if(qs == 2 || qs == 3)m= -1;
00306                             else m=1;
00307                             flg=1;
00308                      }
00309                      cont(xc,yc);
00310                      xs = xc; 
00311                      ys = yc;
00312                      if(qs == qf && flg == 1)
00313                             switch(qf){
00314                             case 3:
00315                             case 4:       
00316                                    if(xs >= x1)return;
00317                                    continue;
00318                             case 1:
00319                             case 2:
00320                                    if(xs <= x1)return;
00321                             }
00322                      continue;
00323               case 'y':     
00324                      if(qs > 2)xc += del;
00325                      else xc -= del;
00326                      dx = xc-x;
00327                      pc = r-dx*dx;
00328                      yc = m*sqrt(pc)+y;
00329                      if((x < xs && x >= xc) || ( x > xs && x <= xc ) ||
00330                          (y < ys && y >= yc) || (y > ys && y <= yc) )
00331                      {
00332                             if(++qs > 4)qs=1;
00333                             if(qs > 2)m = -1;
00334                             else m = 1;
00335                             flg=1;
00336                      }
00337                      cont(xc,yc);
00338                      xs = xc; 
00339                      ys = yc;
00340                      if(qs == qf && flg == 1)
00341                             switch(qs){
00342                             case 1:
00343                             case 4:
00344                                    if(ys >= y1)return;
00345                                    continue;
00346                             case 2:
00347                             case 3:
00348                                    if(ys <= y1)return;
00349                             }
00350               }
00351        }
00352 }
00353 
00354 int
00355 quadr(
00356        int x,
00357        int y,
00358        int xp,
00359        int yp
00360 )
00361 {
00362        if(x < xp)
00363               if(y <= yp)return(1);
00364               else return(4);
00365        else if(x > xp)
00366               if(y < yp)return(2);
00367               else return(3);
00368        else if(y < yp)return(2);
00369        else return(4);
00370 }
00371 
00372 
00373 void
00374 circle(
00375        int x,
00376        int y,
00377        int r
00378 )
00379 {
00380        arc(x,y,x+r,y,x+r,y);
00381 }
00382 
00383 
00384 
00385 void
00386 erase(void)                        /* erase plot */
00387 {
00388     pglob(PEOP, 0200, NULL);
00389 }
00390 
00391 
00392 void
00393 linemod(             /* set line mode according to s */
00394        char  s[]
00395 )
00396 {
00397     switch (s[2]) {
00398        case 'l':            /* solid */
00399            curmod = 0;
00400            break;
00401        case 't':            /* dotted */
00402            curmod = 1;
00403            break;
00404        case 'n':            /* long dashed */
00405            curmod = 2;
00406            break;
00407        case 'o':            /* short dashed (dot dashed) */
00408            curmod = 3;
00409            break;
00410        default:
00411            error(WARNING, "unknown line mode in linemod");
00412            break;
00413     }
00414 }
00415 
00416 
00417 void
00418 space( /* change space */
00419        int  xmn,
00420        int ymn,
00421        int xmx,
00422        int ymx
00423 )
00424 {
00425     if (xmn >= xmx || ymn >= ymx)
00426        error(USER, "illegal space specification in space");
00427     
00428     xmin = xmn;
00429     ymin = ymn;
00430     size = min(xmx-xmn, ymx-ymn);
00431 }