Back to index

radiance  4R0+20100331
geom2mgf.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: geom2mgf.c,v 1.2 2003/02/28 20:11:29 greg Exp $";
00003 #endif
00004 /* Convert Pete Shirley's simple format to MGF */
00005 
00006 #include <stdio.h>
00007 
00008 #define PI           3.14159265358979323846
00009 #define WHTEFFICACY  179.0         /* lumens/watt conversion factor */
00010 
00011 #define FEQ(a,b)     ((a)-(b) < 1e-4 && (a)-(b) > -1e-4)
00012 
00013 
00014 main(argc, argv)            /* input files are passed parameters */
00015 int    argc;
00016 char   **argv;
00017 {
00018        int    i;
00019 
00020        if (argc == 1)
00021               translate(NULL);
00022        else
00023               for (i = 1; i < argc; i++)
00024                      translate(argv[i]);
00025        exit(0);
00026 }
00027 
00028 
00029 translate(fname)            /* translate file fname */
00030 char   *fname;
00031 {
00032        FILE   *fp;
00033        register int  c;
00034 
00035        if (fname == NULL) {
00036               fname = "<stdin>";
00037               fp = stdin;
00038        } else if ((fp = fopen(fname, "r")) == NULL) {
00039               perror(fname);
00040               exit(1);
00041        }
00042        while ((c = getc(fp)) != EOF)
00043               switch (c) {
00044               case ' ':            /* white space */
00045               case '\t':
00046               case '\n':
00047               case '\f':
00048               case '\r':
00049                      break;
00050               case 'q':            /* quad */
00051                      doquad(fname, fp);
00052                      break;
00053               case 's':            /* sphere */
00054                      dosphere(fname, fp);
00055                      break;
00056               default:             /* illegal */
00057                      fprintf(stderr, "%s: illegal character (%d)\n",
00058                                    fname, c);
00059                      exit(1);
00060               }
00061 }
00062 
00063 
00064 doquad(fn, fp)                     /* translate quadrilateral */
00065 char   *fn;
00066 FILE   *fp;
00067 {
00068        double vert[3];
00069        register int  i;
00070 
00071        for (i = 0; i < 4; i++) {
00072               if (fscanf(fp, "%lf %lf %lf", &vert[0],
00073                             &vert[1], &vert[2]) != 3) {
00074                      fprintf(stderr, "%s: bad quad\n", fn);
00075                      exit(1);
00076               }
00077               printf("v qv%d =\n\tp %.9g %.9g %.9g\n",
00078                             i, vert[0], vert[1], vert[2]);
00079        }
00080        domat(fn, fp);
00081        printf("f qv0 qv1 qv2 qv3\n");
00082 }
00083 
00084 
00085 dosphere(fn, fp)            /* translate sphere */
00086 char   *fn;
00087 FILE   *fp;
00088 {
00089        double cent[3], radius;
00090 
00091        if (fscanf(fp, "%lf %lf %lf %lf", &cent[0], &cent[1],
00092                      &cent[2], &radius) != 4) {
00093               fprintf(stderr, "%s: bad sphere\n", fn);
00094               exit(1);
00095        }
00096        printf("v sc = \n\tp %.9g %.9g %.9g\n",
00097                      cent[0], cent[1], cent[2]);
00098        domat(fn, fp);
00099        printf("sph sc %.9g\n", radius);
00100 }
00101 
00102 
00103 domat(fn, fp)               /* translate material */
00104 char   *fn;
00105 FILE   *fp;
00106 {
00107        static short  curtype = 0;
00108        static double curv1, curv2;
00109        double d1, d2;
00110        double f;
00111 
00112 nextchar:
00113        switch(getc(fp)) {
00114        case ' ':
00115        case '\t':
00116        case '\n':
00117        case '\f':
00118        case '\r':
00119               goto nextchar;
00120        case 'd':                   /* diffuse reflector */
00121               if (fscanf(fp, "%lf", &d1) != 1)
00122                      break;
00123               if (curtype == 'd' && FEQ(d1, curv1))
00124                      return;
00125               curtype = 'd';
00126               curv1 = d1;
00127               printf("m\n\tc\n");
00128               printf("\trd %.4f\n", d1);
00129               return;
00130        case 's':                   /* specular reflector */
00131               if (fscanf(fp, "%lf", &d1) != 1)
00132                      break;
00133               if (curtype == 's' && FEQ(d1, curv1))
00134                      return;
00135               curtype = 's';
00136               curv1 = d1;
00137               printf("m\n\tc\n");
00138               printf("\trs %.4f 0\n", d1);
00139               return;
00140        case 't':                   /* dielectric */
00141               if (fscanf(fp, "%lf", &d1) != 1)
00142                      break;
00143               if (curtype == 't' && FEQ(d1, curv1))
00144                      return;
00145               curtype = 't';
00146               curv1 = d1;
00147               printf("m\n\tc\n");
00148               printf("\tsides 1\n\tir %.3f 0\n", d1);
00149               f = (1.0 - d1)/(1.0 + d1);
00150               f *= f;
00151               printf("\trs %.4f 0\n", f);
00152               printf("\tts %.4f 0\n", 1.0-f);
00153               return;
00154        case 'l':                   /* light source */
00155               if (fscanf(fp, "%lf %lf", &d1, &d2) != 2)
00156                      break;
00157               if (curtype == 'l' && FEQ(d1, curv1) && FEQ(d2, curv2))
00158                      return;
00159               curtype = 'l';
00160               curv1 = d1;
00161               curv2 = d2;
00162               printf("m\n\tc\n");
00163               printf("\tsides 1\n\trd %.4f\n\ted %.2f\n",
00164                             d1, PI*WHTEFFICACY*d2);
00165               return;
00166        }
00167        fprintf(stderr, "%s: material format error\n", fn);
00168        exit(1);
00169 }