| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: geom2mgf.c,v 1.2 2003/02/28 20:11:29 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/* Convert Pete Shirley's simple format to MGF */
|
| 5 |
|
| 6 |
#include <stdio.h>
|
| 7 |
|
| 8 |
#define PI 3.14159265358979323846
|
| 9 |
#define WHTEFFICACY 179.0 /* lumens/watt conversion factor */
|
| 10 |
|
| 11 |
#define FEQ(a,b) ((a)-(b) < 1e-4 && (a)-(b) > -1e-4)
|
| 12 |
|
| 13 |
|
| 14 |
main(argc, argv) /* input files are passed parameters */
|
| 15 |
int argc;
|
| 16 |
char **argv;
|
| 17 |
{
|
| 18 |
int i;
|
| 19 |
|
| 20 |
if (argc == 1)
|
| 21 |
translate(NULL);
|
| 22 |
else
|
| 23 |
for (i = 1; i < argc; i++)
|
| 24 |
translate(argv[i]);
|
| 25 |
exit(0);
|
| 26 |
}
|
| 27 |
|
| 28 |
|
| 29 |
translate(fname) /* translate file fname */
|
| 30 |
char *fname;
|
| 31 |
{
|
| 32 |
FILE *fp;
|
| 33 |
register int c;
|
| 34 |
|
| 35 |
if (fname == NULL) {
|
| 36 |
fname = "<stdin>";
|
| 37 |
fp = stdin;
|
| 38 |
} else if ((fp = fopen(fname, "r")) == NULL) {
|
| 39 |
perror(fname);
|
| 40 |
exit(1);
|
| 41 |
}
|
| 42 |
while ((c = getc(fp)) != EOF)
|
| 43 |
switch (c) {
|
| 44 |
case ' ': /* white space */
|
| 45 |
case '\t':
|
| 46 |
case '\n':
|
| 47 |
case '\f':
|
| 48 |
case '\r':
|
| 49 |
break;
|
| 50 |
case 'q': /* quad */
|
| 51 |
doquad(fname, fp);
|
| 52 |
break;
|
| 53 |
case 's': /* sphere */
|
| 54 |
dosphere(fname, fp);
|
| 55 |
break;
|
| 56 |
default: /* illegal */
|
| 57 |
fprintf(stderr, "%s: illegal character (%d)\n",
|
| 58 |
fname, c);
|
| 59 |
exit(1);
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
|
| 64 |
doquad(fn, fp) /* translate quadrilateral */
|
| 65 |
char *fn;
|
| 66 |
FILE *fp;
|
| 67 |
{
|
| 68 |
double vert[3];
|
| 69 |
register int i;
|
| 70 |
|
| 71 |
for (i = 0; i < 4; i++) {
|
| 72 |
if (fscanf(fp, "%lf %lf %lf", &vert[0],
|
| 73 |
&vert[1], &vert[2]) != 3) {
|
| 74 |
fprintf(stderr, "%s: bad quad\n", fn);
|
| 75 |
exit(1);
|
| 76 |
}
|
| 77 |
printf("v qv%d =\n\tp %.9g %.9g %.9g\n",
|
| 78 |
i, vert[0], vert[1], vert[2]);
|
| 79 |
}
|
| 80 |
domat(fn, fp);
|
| 81 |
printf("f qv0 qv1 qv2 qv3\n");
|
| 82 |
}
|
| 83 |
|
| 84 |
|
| 85 |
dosphere(fn, fp) /* translate sphere */
|
| 86 |
char *fn;
|
| 87 |
FILE *fp;
|
| 88 |
{
|
| 89 |
double cent[3], radius;
|
| 90 |
|
| 91 |
if (fscanf(fp, "%lf %lf %lf %lf", ¢[0], ¢[1],
|
| 92 |
¢[2], &radius) != 4) {
|
| 93 |
fprintf(stderr, "%s: bad sphere\n", fn);
|
| 94 |
exit(1);
|
| 95 |
}
|
| 96 |
printf("v sc = \n\tp %.9g %.9g %.9g\n",
|
| 97 |
cent[0], cent[1], cent[2]);
|
| 98 |
domat(fn, fp);
|
| 99 |
printf("sph sc %.9g\n", radius);
|
| 100 |
}
|
| 101 |
|
| 102 |
|
| 103 |
domat(fn, fp) /* translate material */
|
| 104 |
char *fn;
|
| 105 |
FILE *fp;
|
| 106 |
{
|
| 107 |
static short curtype = 0;
|
| 108 |
static double curv1, curv2;
|
| 109 |
double d1, d2;
|
| 110 |
double f;
|
| 111 |
|
| 112 |
nextchar:
|
| 113 |
switch(getc(fp)) {
|
| 114 |
case ' ':
|
| 115 |
case '\t':
|
| 116 |
case '\n':
|
| 117 |
case '\f':
|
| 118 |
case '\r':
|
| 119 |
goto nextchar;
|
| 120 |
case 'd': /* diffuse reflector */
|
| 121 |
if (fscanf(fp, "%lf", &d1) != 1)
|
| 122 |
break;
|
| 123 |
if (curtype == 'd' && FEQ(d1, curv1))
|
| 124 |
return;
|
| 125 |
curtype = 'd';
|
| 126 |
curv1 = d1;
|
| 127 |
printf("m\n\tc\n");
|
| 128 |
printf("\trd %.4f\n", d1);
|
| 129 |
return;
|
| 130 |
case 's': /* specular reflector */
|
| 131 |
if (fscanf(fp, "%lf", &d1) != 1)
|
| 132 |
break;
|
| 133 |
if (curtype == 's' && FEQ(d1, curv1))
|
| 134 |
return;
|
| 135 |
curtype = 's';
|
| 136 |
curv1 = d1;
|
| 137 |
printf("m\n\tc\n");
|
| 138 |
printf("\trs %.4f 0\n", d1);
|
| 139 |
return;
|
| 140 |
case 't': /* dielectric */
|
| 141 |
if (fscanf(fp, "%lf", &d1) != 1)
|
| 142 |
break;
|
| 143 |
if (curtype == 't' && FEQ(d1, curv1))
|
| 144 |
return;
|
| 145 |
curtype = 't';
|
| 146 |
curv1 = d1;
|
| 147 |
printf("m\n\tc\n");
|
| 148 |
printf("\tsides 1\n\tir %.3f 0\n", d1);
|
| 149 |
f = (1.0 - d1)/(1.0 + d1);
|
| 150 |
f *= f;
|
| 151 |
printf("\trs %.4f 0\n", f);
|
| 152 |
printf("\tts %.4f 0\n", 1.0-f);
|
| 153 |
return;
|
| 154 |
case 'l': /* light source */
|
| 155 |
if (fscanf(fp, "%lf %lf", &d1, &d2) != 2)
|
| 156 |
break;
|
| 157 |
if (curtype == 'l' && FEQ(d1, curv1) && FEQ(d2, curv2))
|
| 158 |
return;
|
| 159 |
curtype = 'l';
|
| 160 |
curv1 = d1;
|
| 161 |
curv2 = d2;
|
| 162 |
printf("m\n\tc\n");
|
| 163 |
printf("\tsides 1\n\trd %.4f\n\ted %.2f\n",
|
| 164 |
d1, PI*WHTEFFICACY*d2);
|
| 165 |
return;
|
| 166 |
}
|
| 167 |
fprintf(stderr, "%s: material format error\n", fn);
|
| 168 |
exit(1);
|
| 169 |
}
|