ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/rad2mgf.c
Revision: 2.2
Committed: Fri Jul 8 16:10:06 1994 UTC (29 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +142 -78 lines
Log Message:
numerous bug fixes and enhancements

File Contents

# Content
1 /* Copyright (c) 1994 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Convert Radiance scene description to MGF
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include "fvect.h"
14 #include "object.h"
15 #include "color.h"
16 #include "lookup.h"
17
18 int o_face(), o_cone(), o_sphere(), o_ring(), o_cylinder();
19 int o_instance(), o_source(), o_illum();
20 int o_plastic(), o_metal(), o_glass(), o_mirror(), o_trans(), o_light();
21
22 extern void free();
23 extern char *malloc();
24
25 LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */
26
27 LUTAB rdispatch = LU_SINIT(NULL,NULL); /* function dispatch table */
28
29 char curmat[80]; /* current material */
30 char curobj[128] = "Untitled"; /* current object name */
31
32 double unit_mult = 1.; /* units multiplier */
33
34 #define hasmult (unit_mult < .999 || unit_mult > 1.001)
35
36
37 main(argc, argv)
38 int argc;
39 char **argv;
40 {
41 int i;
42
43 for (i = 1; i < argc && argv[i][0] == '-'; i++)
44 switch (argv[i][1]) {
45 case 'd': /* units */
46 switch (argv[i][2]) {
47 case 'm': /* meters */
48 unit_mult = 1.;
49 break;
50 case 'c': /* centimeters */
51 unit_mult = .01;
52 break;
53 case 'f': /* feet */
54 unit_mult = 12.*.0254;
55 break;
56 case 'i': /* inches */
57 unit_mult = .0254;
58 break;
59 default:
60 goto unkopt;
61 }
62 break;
63 }
64 init();
65 if (i >= argc)
66 rad2mgf(NULL);
67 else
68 for ( ; i < argc; i++)
69 rad2mgf(argv[i]);
70 uninit();
71 exit(0);
72 unkopt:
73 fprintf(stderr, "Usage: %s [-d{m|c|f|i}] file ..\n", argv[0]);
74 exit(1);
75 }
76
77
78 rad2mgf(inp) /* convert a Radiance file to MGF */
79 char *inp;
80 {
81 #define mod buf
82 #define typ (buf+128)
83 #define id (buf+256)
84 #define alias (buf+384)
85 char buf[512];
86 FUNARGS fa;
87 register FILE *fp;
88 register int c;
89
90 if (inp == NULL) {
91 inp = "standard input";
92 fp = stdin;
93 } else if (inp[0] == '!') {
94 if ((fp = popen(inp+1, "r")) == NULL) {
95 fputs(inp, stderr);
96 fputs(": cannot execute\n", stderr);
97 exit(1);
98 }
99 } else if ((fp = fopen(inp, "r")) == NULL) {
100 fputs(inp, stderr);
101 fputs(": cannot open\n", stderr);
102 exit(1);
103 }
104 printf("# Begin conversion from: %s\n", inp);
105 while ((c = getc(fp)) != EOF)
106 switch (c) {
107 case ' ': /* white space */
108 case '\t':
109 case '\n':
110 case '\r':
111 case '\f':
112 break;
113 case '#': /* comment */
114 if (fgets(buf, sizeof(buf), fp) != NULL)
115 printf("# %s", buf);
116 break;
117 case '!': /* inline command */
118 ungetc(c, fp);
119 fgetline(buf, sizeof(buf), fp);
120 rad2mgf(buf);
121 break;
122 default: /* Radiance primitive */
123 ungetc(c, fp);
124 if (fscanf(fp, "%s %s %s", mod, typ, id) != 3) {
125 fputs(inp, stderr);
126 fputs(": unexpected EOF\n", stderr);
127 exit(1);
128 }
129 if (!strcmp(typ, "alias")) {
130 strcpy(alias, "EOF");
131 fscanf(fp, "%s", alias);
132 newmat(id, alias);
133 } else {
134 if (!readfargs(&fa, fp)) {
135 fprintf(stderr,
136 "%s: bad argument syntax for %s \"%s\"\n",
137 inp, typ, id);
138 exit(1);
139 }
140 cvtprim(inp, mod, typ, id, &fa);
141 freefargs(&fa);
142 }
143 break;
144 }
145 printf("# End conversion from: %s\n", inp);
146 if (inp[0] == '!')
147 pclose(fp);
148 else
149 fclose(fp);
150 #undef mod
151 #undef typ
152 #undef id
153 #undef alias
154 }
155
156
157 cvtprim(inp, mod, typ, id, fa) /* process Radiance primitive */
158 char *inp, *mod, *typ, *id;
159 FUNARGS *fa;
160 {
161 int (*df)();
162
163 df = (int (*)())lu_find(&rdispatch, typ)->data;
164 if (df != NULL) { /* convert */
165 if ((*df)(mod, typ, id, fa) < 0) {
166 fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id);
167 exit(1);
168 }
169 } else if (lu_find(&rmats, mod)->data != NULL) /* make alias */
170 newmat(id, mod);
171 }
172
173
174 newmat(id, alias) /* add a modifier to the alias list */
175 char *id;
176 char *alias;
177 {
178 register LUENT *lp, *lpa;
179
180 if (alias != NULL) { /* look up alias */
181 if ((lpa = lu_find(&rmats, alias)) == NULL)
182 goto memerr;
183 if (lpa->data == NULL)
184 alias = NULL; /* doesn't exist! */
185 }
186 if ((lp = lu_find(&rmats, id)) == NULL) /* look up material */
187 goto memerr;
188 if (alias != NULL && lp->data == lpa->key)
189 return; /* alias set already */
190 if (lp->data == NULL) { /* allocate material */
191 if ((lp->key = (char *)malloc(strlen(id)+1)) == NULL)
192 goto memerr;
193 strcpy(lp->key, id);
194 }
195 if (alias == NULL) { /* set this material */
196 lp->data = lp->key;
197 printf("m %s =\n", id);
198 } else { /* set this alias */
199 lp->data = lpa->key;
200 printf("m %s = %s\n", id, alias);
201 }
202 strcpy(curmat, id);
203 return;
204 memerr:
205 fputs("Out of memory in newmat!\n", stderr);
206 exit(1);
207 }
208
209
210 setmat(id) /* set material to this one */
211 char *id;
212 {
213 if (!strcmp(id, curmat)) /* already set? */
214 return;
215 if (!strcmp(id, VOIDID)) /* cannot set */
216 return;
217 printf("m %s\n", id);
218 strcpy(curmat, id);
219 }
220
221
222 setobj(id) /* set object name to this one */
223 char *id;
224 {
225 register char *cp, *cp2;
226 char *end = NULL;
227 int diff = 0;
228 /* use all but final suffix */
229 for (cp = id; *cp; cp++)
230 if (*cp == '.')
231 end = cp;
232 if (end == NULL)
233 end = cp;
234 /* copy to current object */
235 for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++)
236 diff += *cp != *cp2;
237 if (!diff && !*cp2)
238 return;
239 *cp2 = '\0';
240 fputs("o\no ", stdout);
241 puts(curobj);
242 }
243
244
245 init() /* initialize dispatch table and output */
246 {
247 lu_init(&rdispatch, 22);
248 add2dispatch("polygon", o_face);
249 add2dispatch("cone", o_cone);
250 add2dispatch("cup", o_cone);
251 add2dispatch("sphere", o_sphere);
252 add2dispatch("bubble", o_sphere);
253 add2dispatch("cylinder", o_cylinder);
254 add2dispatch("tube", o_cylinder);
255 add2dispatch("ring", o_ring);
256 add2dispatch("instance", o_instance);
257 add2dispatch("plastic", o_plastic);
258 add2dispatch("plastic2", o_plastic);
259 add2dispatch("metal", o_metal);
260 add2dispatch("metal2", o_metal);
261 add2dispatch("glass", o_glass);
262 add2dispatch("trans", o_trans);
263 add2dispatch("trans2", o_trans);
264 add2dispatch("mirror", o_mirror);
265 add2dispatch("light", o_light);
266 add2dispatch("spotlight", o_light);
267 add2dispatch("glow", o_light);
268 add2dispatch("illum", o_illum);
269 puts("# The following was converted from Radiance scene input");
270 if (hasmult)
271 printf("xf -s %.4e\n", unit_mult);
272 printf("o %s\n", curobj);
273 }
274
275
276 uninit() /* mark end of MGF file */
277 {
278 puts("o");
279 if (hasmult)
280 puts("xf");
281 puts("# End of data converted from Radiance scene input");
282 lu_done(&rdispatch);
283 lu_done(&rmats);
284 }
285
286
287 add2dispatch(name, func) /* add function to dispatch table */
288 char *name;
289 int (*func)();
290 {
291 register LUENT *lp;
292
293 lp = lu_find(&rdispatch, name);
294 if (lp->key != NULL) {
295 fputs(name, stderr);
296 fputs(": duplicate dispatch entry!\n", stderr);
297 exit(1);
298 }
299 lp->key = name;
300 lp->data = (char *)func;
301 }
302
303
304 /*
305 * Stuff for tracking and reusing vertices:
306 */
307
308 char VKFMT[] = "%+1.9e %+1.9e %+1.9e";
309 #define VKLEN 64
310
311 #define mkvkey(k,v) sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
312
313 #define NVERTS 256
314
315 long clock; /* incremented at each vertex request */
316
317 struct vert {
318 long lused; /* when last used (0 if unassigned) */
319 FVECT p; /* track point position only */
320 } vert[NVERTS];
321
322 LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */
323
324
325 char *
326 getvertid(vname, vp) /* get/set vertex ID for this point */
327 char *vname;
328 FVECT vp;
329 {
330 char vkey[VKLEN];
331 register LUENT *lp;
332 register int i, vndx;
333
334 if (!vertab.tsiz && !lu_init(&vertab, NVERTS))
335 goto memerr;
336 clock++; /* increment counter */
337 mkvkey(vkey, vp);
338 if ((lp = lu_find(&vertab, vkey)) == NULL)
339 goto memerr;
340 if (lp->data == NULL) { /* allocate new vertex entry */
341 if (lp->key != NULL) /* reclaim deleted entry */
342 vertab.ndel--;
343 else {
344 if ((lp->key = (char *)malloc(VKLEN)) == NULL)
345 goto memerr;
346 strcpy(lp->key, vkey);
347 }
348 vndx = 0; /* find oldest vertex */
349 for (i = 1; i < NVERTS; i++)
350 if (vert[i].lused < vert[vndx].lused)
351 vndx = i;
352 if (vert[vndx].lused) { /* free old entry first */
353 mkvkey(vkey, vert[vndx].p);
354 lu_delete(&vertab, vkey);
355 }
356 vert[vndx].lused = clock; /* assign it */
357 VCOPY(vert[vndx].p, vp);
358 printf("v v%d =\n\tp %.15g %.15g %.15g\n", /* print it */
359 vndx, vp[0], vp[1], vp[2]);
360 lp->data = (char *)&vert[vndx]; /* set it */
361 } else
362 vndx = (struct vert *)lp->data - vert;
363 sprintf(vname, "v%d", vndx);
364 return(vname);
365 memerr:
366 fputs("Out of memory in getvertid!\n", stderr);
367 exit(1);
368 }
369
370
371 int
372 o_face(mod, typ, id, fa) /* print out a polygon */
373 char *mod, *typ, *id;
374 FUNARGS *fa;
375 {
376 char entbuf[512];
377 register char *cp;
378 register int i;
379
380 if (fa->nfargs < 9 | fa->nfargs % 3)
381 return(-1);
382 setmat(mod);
383 setobj(id);
384 cp = entbuf;
385 *cp++ = 'f';
386 for (i = 0; i < fa->nfargs; i += 3) {
387 *cp++ = ' ';
388 getvertid(cp, fa->farg + i);
389 while (*cp)
390 cp++;
391 }
392 puts(entbuf);
393 return(0);
394 }
395
396
397 int
398 o_cone(mod, typ, id, fa) /* print out a cone */
399 char *mod, *typ, *id;
400 register FUNARGS *fa;
401 {
402 char v1[6], v2[6];
403
404 if (fa->nfargs != 8)
405 return(-1);
406 setmat(mod);
407 setobj(id);
408 getvertid(v1, fa->farg);
409 getvertid(v2, fa->farg + 3);
410 if (typ[1] == 'u') /* cup -> inverted cone */
411 printf("cone %s %.12g %s %.12g\n",
412 v1, -fa->farg[6], v2, -fa->farg[7]);
413 else
414 printf("cone %s %.12g %s %.12g\n",
415 v1, fa->farg[6], v2, fa->farg[7]);
416 return(0);
417 }
418
419
420 int
421 o_sphere(mod, typ, id, fa) /* print out a sphere */
422 char *mod, *typ, *id;
423 register FUNARGS *fa;
424 {
425 char cent[6];
426
427 if (fa->nfargs != 4)
428 return(-1);
429 setmat(mod);
430 setobj(id);
431 printf("sph %s %.12g\n", getvertid(cent, fa->farg),
432 typ[0]=='b' ? -fa->farg[3] : fa->farg[3]);
433 return(0);
434 }
435
436
437 int
438 o_cylinder(mod, typ, id, fa) /* print out a cylinder */
439 char *mod, *typ, *id;
440 register FUNARGS *fa;
441 {
442 char v1[6], v2[6];
443
444 if (fa->nfargs != 7)
445 return(-1);
446 setmat(mod);
447 setobj(id);
448 getvertid(v1, fa->farg);
449 getvertid(v2, fa->farg + 3);
450 printf("cyl %s %.12g %s\n", v1,
451 typ[0]=='t' ? -fa->farg[6] : fa->farg[6], v2);
452 return(0);
453 }
454
455
456 int
457 o_ring(mod, typ, id, fa) /* print out a ring */
458 char *mod, *typ, *id;
459 register FUNARGS *fa;
460 {
461 if (fa->nfargs != 8)
462 return(-1);
463 setmat(mod);
464 setobj(id);
465 printf("v cent =\n\tp %.12g %.12g %.12g\n",
466 fa->farg[0], fa->farg[1], fa->farg[2]);
467 printf("\tn %.12g %.12g %.12g\n",
468 fa->farg[3], fa->farg[4], fa->farg[5]);
469 if (fa->farg[6] < fa->farg[7])
470 printf("ring cent %.12g %.12g\n",
471 fa->farg[6], fa->farg[7]);
472 else
473 printf("ring cent %.12g %.12g\n",
474 fa->farg[7], fa->farg[6]);
475 return(0);
476 }
477
478
479 int
480 o_instance(mod, typ, id, fa) /* convert an instance */
481 char *mod, *typ, *id;
482 FUNARGS *fa;
483 {
484 register int i;
485 register char *cp;
486 char *start = NULL, *end = NULL;
487 /*
488 * We don't really know how to do this, so we just create
489 * a reference to an undefined MGF file and it's the user's
490 * responsibility to create this file and put the appropriate
491 * stuff into it.
492 */
493 if (fa->nsargs < 1)
494 return(-1);
495 setmat(mod); /* only works if surfaces are void */
496 setobj(id);
497 for (cp = fa->sarg[0]; *cp; cp++) /* construct MGF file name */
498 if (*cp == '/')
499 start = cp+1;
500 else if (*cp == '.')
501 end = cp;
502 if (start == NULL)
503 start = fa->sarg[0];
504 if (end == NULL || start >= end)
505 end = cp;
506 fputs("i ", stdout); /* print include entity */
507 for (cp = start; cp < end; cp++)
508 putchar(*cp);
509 fputs(".mgf", stdout); /* add MGF suffix */
510 for (i = 1; i < fa->nsargs; i++) { /* add transform */
511 putchar(' ');
512 fputs(fa->sarg[i], stdout);
513 }
514 putchar('\n');
515 return(0);
516 }
517
518
519 int
520 o_source(mod, typ, id, fa) /* convert a source */
521 char *mod, *typ, *id;
522 FUNARGS *fa;
523 {
524 return(0); /* there is no MGF equivalent! */
525 }
526
527
528 int
529 o_illum(mod, typ, id, fa) /* convert an illum material */
530 char *mod, *typ, *id;
531 FUNARGS *fa;
532 {
533 if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
534 newmat(id, fa->sarg[0]); /* just create alias */
535 return(0);
536 }
537 /* else create invisible material */
538 newmat(id, NULL);
539 puts("\tts 1 0");
540 return(0);
541 }
542
543
544 int
545 o_plastic(mod, typ, id, fa) /* convert a plastic material */
546 char *mod, *typ, *id;
547 register FUNARGS *fa;
548 {
549 COLOR cxyz, rrgb;
550 double d;
551
552 if (fa->nfargs != (typ[7]=='2' ? 6 : 5))
553 return(-1);
554 newmat(id, NULL);
555 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
556 rgb_cie(cxyz, rrgb);
557 puts("\tc"); /* put diffuse component */
558 d = cxyz[0] + cxyz[1] + cxyz[2];
559 if (d > FTINY)
560 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
561 printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
562 if (fa->farg[3] > FTINY) { /* put specular component */
563 puts("\tc");
564 printf("\trs %.4f %.4f\n", fa->farg[3],
565 typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
566 fa->farg[4]);
567 }
568 return(0);
569 }
570
571
572 int
573 o_metal(mod, typ, id, fa) /* convert a metal material */
574 char *mod, *typ, *id;
575 register FUNARGS *fa;
576 {
577 COLOR cxyz, rrgb;
578 double d;
579
580 if (fa->nfargs != (typ[5]=='2' ? 6 : 5))
581 return(-1);
582 newmat(id, NULL);
583 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
584 rgb_cie(cxyz, rrgb);
585 puts("\tc"); /* put diffuse component */
586 d = cxyz[0] + cxyz[1] + cxyz[2];
587 if (d > FTINY)
588 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
589 printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3]));
590 /* put specular component */
591 printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3],
592 typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) :
593 fa->farg[4]);
594 return(0);
595 }
596
597
598 int
599 o_glass(mod, typ, id, fa) /* convert a glass material */
600 char *mod, *typ, *id;
601 register FUNARGS *fa;
602 {
603 COLOR cxyz, rrgb, trgb;
604 double nrfr = 1.52, F, d;
605 register int i;
606
607 if (fa->nfargs != 3 && fa->nfargs != 4)
608 return(-1);
609 newmat(id, NULL);
610 if (fa->nfargs == 4)
611 nrfr = fa->farg[3];
612 F = (1. - nrfr)/(1. + nrfr); /* use normal incidence */
613 F *= F;
614 for (i = 0; i < 3; i++) {
615 trgb[i] = fa->farg[i] * (1. - F)*(1. - F) /
616 (1. - F*F*fa->farg[i]*fa->farg[i]);
617 rrgb[i] = F * (1. + (1. - 2.*F)*fa->farg[i]) /
618 (1. - F*F*fa->farg[i]*fa->farg[i]);
619 }
620 rgb_cie(cxyz, rrgb); /* put reflected component */
621 puts("\tc");
622 d = cxyz[0] + cxyz[1] + cxyz[2];
623 if (d > FTINY)
624 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
625 printf("\trs %.4f 0\n", cxyz[1]);
626 rgb_cie(cxyz, trgb); /* put transmitted component */
627 puts("\tc");
628 d = cxyz[0] + cxyz[1] + cxyz[2];
629 if (d > FTINY)
630 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
631 printf("\tts %.4f 0\n", cxyz[1]);
632 return(0);
633 }
634
635
636 int
637 o_mirror(mod, typ, id, fa) /* convert a mirror material */
638 char *mod, *typ, *id;
639 register FUNARGS *fa;
640 {
641 COLOR cxyz, rrgb;
642 double d;
643
644 if (fa->nsargs == 1) { /* use alternate material */
645 newmat(id, fa->sarg[0]);
646 return(0);
647 }
648 if (fa->nfargs != 3)
649 return(-1);
650 newmat(id, NULL);
651 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
652 rgb_cie(cxyz, rrgb);
653 puts("\tc"); /* put specular component */
654 d = cxyz[0] + cxyz[1] + cxyz[2];
655 if (d > FTINY)
656 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
657 printf("\trs %.4f 0\n", cxyz[1]);
658 return(0);
659 }
660
661
662 int
663 o_trans(mod, typ, id, fa) /* convert a trans material */
664 char *mod, *typ, *id;
665 register FUNARGS *fa;
666 {
667 COLOR cxyz, rrgb;
668 double rough, trans, tspec, d;
669
670 if (typ[4] == '2') { /* trans2 */
671 if (fa->nfargs != 8)
672 return(-1);
673 rough = .5*(fa->farg[4] + fa->farg[5]);
674 trans = fa->farg[6];
675 tspec = fa->farg[7];
676 } else { /* trans */
677 if (fa->nfargs != 7)
678 return(-1);
679 rough = fa->farg[4];
680 trans = fa->farg[5];
681 tspec = fa->farg[6];
682 }
683 newmat(id, NULL);
684 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
685 rgb_cie(cxyz, rrgb);
686 puts("\tc"); /* put transmitted diffuse */
687 d = cxyz[0] + cxyz[1] + cxyz[2];
688 if (d > FTINY)
689 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
690 printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec));
691 /* put transmitted specular */
692 printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough);
693 /* put reflected diffuse */
694 printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans));
695 puts("\tc"); /* put reflected specular */
696 printf("\trs %.4f %.4f\n", fa->farg[3], rough);
697 return(0);
698 }
699
700
701 int
702 o_light(mod, typ, id, fa) /* convert a light type */
703 char *mod, *typ, *id;
704 register FUNARGS *fa;
705 {
706 COLOR cxyz, rrgb;
707 double d;
708
709 if (fa->nfargs < 3)
710 return(-1);
711 newmat(id, NULL);
712 rrgb[0] = fa->farg[0]; rrgb[1] = fa->farg[1]; rrgb[2] = fa->farg[2];
713 rgb_cie(cxyz, rrgb);
714 d = cxyz[0] + cxyz[1] + cxyz[2];
715 puts("\tc");
716 if (d > FTINY)
717 printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
718 printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY);
719 return(0);
720 }