Back to index

radiance  4R0+20100331
mgf2meta.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: mgf2meta.c,v 2.11 2003/11/17 13:20:10 schorsch Exp $";
00003 #endif
00004 /*
00005  * Convert MGF (Materials and Geometry Format) to Metafile 2-d graphics
00006  */
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <math.h>
00012 
00013 #include "meta.h"
00014 #include "random.h"
00015 #include "mgflib/parser.h"
00016 #include "plocate.h" /* XXX shouldn't this rather be in rtmath.h? */
00017 
00018 #define MSIZE ((1<<14)-1)
00019 #define       MX(v)  (int)(MSIZE*(v)[(proj_axis+1)%3])
00020 #define       MY(v)  (int)(MSIZE*(v)[(proj_axis+2)%3])
00021 
00022 int    proj_axis;
00023 double limit[3][2];
00024 int    layer;
00025 long   rthresh = 1;
00026 
00027 extern int    mg_nqcdivs;
00028 
00029 static int r_face(int ac, char **av);
00030 static void newlayer(void);
00031 static int doline(int v1x, int v1y, int v2x, int v2y);
00032 
00033 
00034 int
00035 main(         /* convert files to stdout */
00036        int    argc,
00037        char   *argv[]
00038 )
00039 {
00040        int    i;
00041                             /* initialize dispatch table */
00042        mg_ehand[MG_E_FACE] = r_face;
00043        mg_ehand[MG_E_POINT] = c_hvertex;
00044        mg_ehand[MG_E_VERTEX] = c_hvertex;
00045        mg_ehand[MG_E_XF] = xf_handler;
00046        mg_nqcdivs = 3;             /* reduce object subdivision */
00047        mg_init();           /* initialize the parser */
00048                                    /* get arguments */
00049        if (argc > 9 && !strcmp(argv[1], "-t")) {
00050               rthresh = atof(argv[2])*MSIZE + 0.5;
00051               rthresh *= rthresh;
00052               argv += 2;
00053               argc -= 2;
00054        }
00055        if (argc < 8 || (proj_axis = argv[1][0]-'x') < 0 || proj_axis > 2)
00056               goto userr;
00057        limit[0][0] = atof(argv[2]); limit[0][1] = atof(argv[3]);
00058        limit[1][0] = atof(argv[4]); limit[1][1] = atof(argv[5]);
00059        limit[2][0] = atof(argv[6]); limit[2][1] = atof(argv[7]);
00060        
00061        if (argc == 8) {            /* convert stdin */
00062               if (mg_load(NULL) != MG_OK)
00063                      exit(1);
00064        } else                      /* convert each file */
00065               for (i = 8; i < argc; i++) {
00066                      if (mg_load(argv[i]) != MG_OK)
00067                             exit(1);
00068                      newlayer();
00069               }
00070        mendpage();                 /* print page */
00071        mdone();                    /* close output */
00072        exit(0);
00073 userr:
00074        fputs("Usage: mgf2meta [-t thresh] {x|y|z} xmin xmax ymin ymax zmin zmax [file.mgf] ..\n",
00075                      stderr);
00076        exit(1);
00077 }
00078 
00079 
00080 int
00081 r_face(                     /* convert a face */
00082        int    ac,
00083        char   **av
00084 )
00085 {
00086        static FVECT  bbmin = {0,0,0}, bbmax = {1,1,1};
00087        register int  i, j;
00088        register C_VERTEX    *cv;
00089        FVECT  v1, v2, vo;
00090 
00091        if (ac < 4)
00092               return(MG_EARGC);
00093                                           /* connect to last point */
00094        if ((cv = c_getvert(av[ac-1])) == NULL)
00095               return(MG_EUNDEF);
00096        xf_xfmpoint(vo, cv->p);
00097        for (j = 0; j < 3; j++)
00098               vo[j] = (vo[j] - limit[j][0])/(limit[j][1]-limit[j][0]);
00099        for (i = 1; i < ac; i++) {         /* go around face */
00100               if ((cv = c_getvert(av[i])) == NULL)
00101                      return(MG_EUNDEF);
00102               xf_xfmpoint(v2, cv->p);
00103               for (j = 0; j < 3; j++)
00104                      v2[j] = (v2[j] - limit[j][0])/(limit[j][1]-limit[j][0]);
00105               VCOPY(v1, vo);
00106               VCOPY(vo, v2);
00107               if (clip(v1, v2, bbmin, bbmax))
00108                      doline(MX(v1), MY(v1), MX(v2), MY(v2));
00109        }
00110        return(MG_OK);
00111 }
00112 
00113 
00114 #define  HTBLSIZ     16381         /* prime hash table size */
00115 
00116 short  hshtab[HTBLSIZ][4];         /* done line segments */
00117 
00118 #define  hash(mx1,my1,mx2,my2)     ((long)(mx1)<<15 ^ (long)(my1)<<10 ^ \
00119                                    (long)(mx2)<<5 ^ (long)(my2))
00120 
00121 
00122 void
00123 newlayer(void)                            /* start a new layer */
00124 {
00125        (void)memset((char *)hshtab, '\0', sizeof(hshtab));
00126        if (++layer >= 16) {
00127               mendpage();
00128               layer = 0;
00129        }
00130 }
00131 
00132 
00133 int
00134 doline(              /* draw line conditionally */
00135        int    v1x,
00136        int v1y,
00137        int v2x,
00138        int v2y
00139 )
00140 {
00141        register int  h;
00142 
00143        if (v1x > v2x || (v1x == v2x && v1y > v2y)) {    /* sort endpoints */
00144               h=v1x; v1x=v2x; v2x=h;
00145               h=v1y; v1y=v2y; v2y=h;
00146        }
00147        h = hash(v1x, v1y, v2x, v2y) % HTBLSIZ;
00148        if (hshtab[h][0] == v1x && hshtab[h][1] == v1y &&
00149                      hshtab[h][2] == v2x && hshtab[h][3] == v2y)
00150               return(0);
00151        hshtab[h][0] = v1x; hshtab[h][1] = v1y;
00152        hshtab[h][2] = v2x; hshtab[h][3] = v2y;
00153        if ((long)(v2x-v1x)*(v2x-v1x) + (long)(v2y-v1y)*(v2y-v1y)
00154                      <= random() % rthresh)
00155               return(0);
00156        mline(v1x, v1y, layer/4, 0, layer%4);
00157        mdraw(v2x, v2y);
00158        return(1);
00159 }