ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/rad2mgf.c
(Generate patch)

Comparing ray/src/cv/rad2mgf.c (file contents):
Revision 2.4 by greg, Sat Jul 9 09:42:48 1994 UTC vs.
Revision 2.24 by greg, Wed Dec 28 18:35:42 2005 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Convert Radiance scene description to MGF
6   */
7  
8 < #include <stdio.h>
8 > #include <ctype.h>
9   #include <string.h>
10 < #include "fvect.h"
10 > #include <stdio.h>
11 >
12 > #include "platform.h"
13 > #include "rtmath.h"
14 > #include "rtio.h"
15 > #include "rtprocess.h"
16   #include "object.h"
17   #include "color.h"
18   #include "lookup.h"
19  
20 < 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();
20 > #define C_1SIDEDTHICK   0.005
21  
22 extern void     free();
23 extern char     *malloc();
22  
23   LUTAB   rmats = LU_SINIT(free,NULL);            /* defined material table */
24  
# Line 37 | Line 35 | double unit_mult = 1.;                         /* units multiplier */
35   * Stuff for tracking and reusing vertices:
36   */
37  
38 < char    VKFMT[] = "%+1.9e %+1.9e %+1.9e";
38 > char    VKFMT[] = "%+16.9e %+16.9e %+16.9e";
39   #define VKLEN           64
40  
41   #define mkvkey(k,v)     sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
42  
43   #define NVERTS          256
44  
45 < long    clock;          /* incremented at each vertex request */
45 > long    vclock;         /* incremented at each vertex request */
46  
47   struct vert {
48          long    lused;          /* when last used (0 if unassigned) */
# Line 53 | Line 51 | struct vert {
51  
52   LUTAB   vertab = LU_SINIT(free,NULL);   /* our vertex lookup table */
53  
54 + void rad2mgf(char *inp);
55 + void cvtprim(char *inp, char *mod, char *typ, char *id, FUNARGS *fa);
56 + void newmat(char *id, char *alias);
57 + void setmat(char *id);
58 + void setobj(char *id);
59 + void init(void);
60 + void uninit(void);
61 + void clrverts(void);
62 + void unspace(char *s);
63 + void add2dispatch(char *name, int (*func)());
64 + char *getvertid(char *vname, FVECT vp);
65 + int o_unsupported(char *mod, char *typ, char *id, FUNARGS *fa);
66 + int o_face(char *mod, char *typ, char *id, FUNARGS *fa);
67 + int o_cone(char *mod, char *typ, char *id, FUNARGS *fa);
68 + int o_sphere(char *mod, char *typ, char *id, FUNARGS *fa);
69 + int o_cylinder(char *mod, char *typ, char *id, FUNARGS *fa);
70 + int o_ring(char *mod, char *typ, char *id, FUNARGS *fa);
71 + int o_instance(char *mod, char *typ, char *id, FUNARGS *fa);
72 + int o_illum(char *mod, char *typ, char *id, FUNARGS *fa);
73 + int o_plastic(char *mod, char *typ, char *id, FUNARGS *fa);
74 + int o_metal(char *mod, char *typ, char *id, FUNARGS *fa);
75 + int o_glass(char *mod, char *typ, char *id, FUNARGS *fa);
76 + int o_dielectric(char *mod, char *typ, char *id, FUNARGS *fa);
77 + int o_mirror(char *mod, char *typ, char *id, FUNARGS *fa);
78 + int o_trans(char *mod, char *typ, char *id, FUNARGS *fa);
79 + int o_light(char *mod, char *typ, char *id, FUNARGS *fa);
80  
81 < main(argc, argv)
82 < int     argc;
83 < char    **argv;
81 >
82 > int
83 > main(
84 >        int     argc,
85 >        char    **argv
86 > )
87   {
88          int     i;
89  
# Line 80 | Line 107 | char   **argv;
107                                  goto unkopt;
108                          }
109                          break;
110 +                default:
111 +                        goto unkopt;
112                  }
113          init();
114          if (i >= argc)
# Line 95 | Line 124 | unkopt:
124   }
125  
126  
127 < rad2mgf(inp)            /* convert a Radiance file to MGF */
128 < char    *inp;
127 > void
128 > rad2mgf(                /* convert a Radiance file to MGF */
129 >        char    *inp
130 > )
131   {
132 < #define mod     buf
102 < #define typ     (buf+128)
103 < #define id      (buf+256)
104 < #define alias   (buf+384)
105 <        char    buf[512];
132 >        char  mod[128], typ[32], id[128], alias[128];
133          FUNARGS fa;
134          register FILE   *fp;
135          register int    c;
# Line 141 | Line 168 | char   *inp;
168                          break;
169                  default:                /* Radiance primitive */
170                          ungetc(c, fp);
171 <                        if (fscanf(fp, "%s %s %s", mod, typ, id) != 3) {
171 >                        if (fgetword(mod, sizeof(mod), fp) == NULL ||
172 >                                        fgetword(typ, sizeof(typ), fp) == NULL ||
173 >                                        fgetword(id, sizeof(id), fp) == NULL) {
174                                  fputs(inp, stderr);
175                                  fputs(": unexpected EOF\n", stderr);
176                                  exit(1);
177                          }
178 +                        unspace(mod);
179 +                        unspace(id);
180                          if (!strcmp(typ, "alias")) {
181                                  strcpy(alias, "EOF");
182 <                                fscanf(fp, "%s", alias);
182 >                                fgetword(alias, sizeof(alias), fp);
183 >                                unspace(alias);
184                                  newmat(id, alias);
185                          } else {
186                                  if (!readfargs(&fa, fp)) {
# Line 167 | Line 199 | char   *inp;
199                  pclose(fp);
200          else
201                  fclose(fp);
170 #undef mod
171 #undef typ
172 #undef id
173 #undef alias
202   }
203  
204  
205 < cvtprim(inp, mod, typ, id, fa)  /* process Radiance primitive */
206 < char    *inp, *mod, *typ, *id;
207 < FUNARGS *fa;
205 > void
206 > unspace(        /* replace spaces with underscores in s */
207 >        char *s
208 > )
209   {
210 +        while (*s) {
211 +                if (isspace(*s))
212 +                        *s = '_';
213 +                ++s;
214 +        }
215 + }
216 +
217 +
218 + void
219 + cvtprim(        /* process Radiance primitive */
220 +        char    *inp,
221 +        char    *mod,
222 +        char    *typ,
223 +        char    *id,
224 +        FUNARGS *fa
225 + )
226 + {
227          int     (*df)();
228  
229          df = (int (*)())lu_find(&rdispatch, typ)->data;
230          if (df != NULL) {                               /* convert */
231                  if ((*df)(mod, typ, id, fa) < 0) {
232 <                        fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id);
232 >                        fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id);
233                          exit(1);
234                  }
235 <        } else if (lu_find(&rmats, mod)->data != NULL)  /* make alias */
236 <                newmat(id, mod);
235 >        } else {                                        /* unsupported */
236 >                o_unsupported(mod, typ, id, fa);
237 >                if (lu_find(&rmats, mod)->data != NULL) /* make alias */
238 >                        newmat(id, mod);
239 >        }
240   }
241  
242  
243 < newmat(id, alias)               /* add a modifier to the alias list */
244 < char    *id;
245 < char    *alias;
243 > void
244 > newmat(         /* add a modifier to the alias list */
245 >        char    *id,
246 >        char    *alias
247 > )
248   {
249          register LUENT  *lp, *lpa;
250  
# Line 227 | Line 278 | memerr:
278   }
279  
280  
281 < setmat(id)                      /* set material to this one */
282 < char    *id;
281 > void
282 > setmat(                 /* set material to this one */
283 >        char    *id
284 > )
285   {
286          if (!strcmp(id, curmat))        /* already set? */
287                  return;
# Line 239 | Line 292 | char   *id;
292   }
293  
294  
295 < setobj(id)                      /* set object name to this one */
296 < char    *id;
295 > void
296 > setobj(                 /* set object name to this one */
297 >        char    *id
298 > )
299   {
300          register char   *cp, *cp2;
301          char    *end = NULL;
# Line 252 | Line 307 | char   *id;
307          if (end == NULL)
308                  end = cp;
309                                  /* copy to current object */
310 <        for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++)
310 >        cp2 = curobj;
311 >        if (!isalpha(*id)) {    /* start with letter */
312 >                diff = *cp2 != 'O';
313 >                *cp2++ = 'O';
314 >        }
315 >        for (cp = id; cp < end; *cp2++ = *cp++) {
316 >                if ((*cp < '!') | (*cp > '~'))  /* limit to visible chars */
317 >                        *cp = '?';
318                  diff += *cp != *cp2;
319 +        }
320          if (!diff && !*cp2)
321                  return;
322          *cp2 = '\0';
# Line 262 | Line 325 | char   *id;
325   }
326  
327  
328 < init()                  /* initialize dispatch table and output */
328 > void
329 > init(void)                      /* initialize dispatch table and output */
330   {
331          lu_init(&vertab, NVERTS);
332          lu_init(&rdispatch, 22);
# Line 275 | Line 339 | init()                 /* initialize dispatch table and output */
339          add2dispatch("tube", o_cylinder);
340          add2dispatch("ring", o_ring);
341          add2dispatch("instance", o_instance);
342 +        add2dispatch("mesh", o_instance);
343          add2dispatch("plastic", o_plastic);
344          add2dispatch("plastic2", o_plastic);
345          add2dispatch("metal", o_metal);
346          add2dispatch("metal2", o_metal);
347          add2dispatch("glass", o_glass);
348 +        add2dispatch("dielectric", o_dielectric);
349          add2dispatch("trans", o_trans);
350          add2dispatch("trans2", o_trans);
351          add2dispatch("mirror", o_mirror);
# Line 287 | Line 353 | init()                 /* initialize dispatch table and output */
353          add2dispatch("spotlight", o_light);
354          add2dispatch("glow", o_light);
355          add2dispatch("illum", o_illum);
356 <        puts("# The following was converted from Radiance scene input");
356 >        puts("# The following was converted from RADIANCE scene input");
357          if (hasmult)
358                  printf("xf -s %.4e\n", unit_mult);
359          printf("o %s\n", curobj);
360   }
361  
362  
363 < uninit()                        /* mark end of MGF file */
363 > void
364 > uninit(void)                    /* mark end of MGF file */
365   {
366          puts("o");
367          if (hasmult)
368                  puts("xf");
369 <        puts("# End of data converted from Radiance scene input");
369 >        puts("# End of data converted from RADIANCE scene input");
370          lu_done(&rdispatch);
371          lu_done(&rmats);
372          lu_done(&vertab);
373   }
374  
375  
376 < add2dispatch(name, func)        /* add function to dispatch table */
377 < char    *name;
311 < int     (*func)();
376 > void
377 > clrverts(void)                  /* clear vertex table */
378   {
379 +        register int    i;
380 +
381 +        lu_done(&vertab);
382 +        for (i = 0; i < NVERTS; i++)
383 +                vert[i].lused = 0;
384 +        lu_init(&vertab, NVERTS);
385 + }
386 +
387 +
388 + void
389 + add2dispatch(   /* add function to dispatch table */
390 +        char    *name,
391 +        int     (*func)()
392 + )
393 + {
394          register LUENT  *lp;
395  
396          lp = lu_find(&rdispatch, name);
# Line 324 | Line 405 | int    (*func)();
405  
406  
407   char *
408 < getvertid(vname, vp)            /* get/set vertex ID for this point */
409 < char    *vname;
410 < FVECT   vp;
408 > getvertid(              /* get/set vertex ID for this point */
409 >        char    *vname,
410 >        FVECT   vp
411 > )
412   {
413 <        char    vkey[VKLEN];
413 >        static char     vkey[VKLEN];
414          register LUENT  *lp;
415          register int    i, vndx;
416  
417 <        clock++;                        /* increment counter */
417 >        vclock++;                       /* increment counter */
418          mkvkey(vkey, vp);
419          if ((lp = lu_find(&vertab, vkey)) == NULL)
420                  goto memerr;
# Line 358 | Line 440 | FVECT  vp;
440                  lp->data = (char *)&vert[vndx];                 /* set it */
441          } else
442                  vndx = (struct vert *)lp->data - vert;
443 <        vert[vndx].lused = clock;               /* record this use */
443 >        vert[vndx].lused = vclock;              /* record this use */
444          sprintf(vname, "v%d", vndx);
445          return(vname);
446   memerr:
# Line 368 | Line 450 | memerr:
450  
451  
452   int
453 < o_face(mod, typ, id, fa)                /* print out a polygon */
454 < char    *mod, *typ, *id;
455 < FUNARGS *fa;
453 > o_unsupported(          /* mark unsupported primitive */
454 >        char    *mod,
455 >        char    *typ,
456 >        char    *id,
457 >        FUNARGS *fa
458 > )
459   {
460 <        char    entbuf[512];
460 >        register int    i;
461 >
462 >        fputs("\n# Unsupported RADIANCE primitive:\n", stdout);
463 >        printf("# %s %s %s", mod, typ, id);
464 >        printf("\n# %d", fa->nsargs);
465 >        for (i = 0; i < fa->nsargs; i++)
466 >                printf(" %s", fa->sarg[i]);
467 > #ifdef IARGS
468 >        printf("\n# %d", fa->niargs);
469 >        for (i = 0; i < fa->niargs; i++)
470 >                printf(" %ld", fa->iarg[i]);
471 > #else
472 >        fputs("\n# 0", stdout);
473 > #endif
474 >        printf("\n# %d", fa->nfargs);
475 >        for (i = 0; i < fa->nfargs; i++)
476 >                printf(" %g", fa->farg[i]);
477 >        fputs("\n\n", stdout);
478 >        return(0);
479 > }
480 >
481 >
482 > int
483 > o_face(         /* print out a polygon */
484 >        char    *mod,
485 >        char    *typ,
486 >        char    *id,
487 >        FUNARGS *fa
488 > )
489 > {
490 >        char    entbuf[2048], *linestart;
491          register char   *cp;
492          register int    i;
493  
494 <        if (fa->nfargs < 9 | fa->nfargs % 3)
494 >        if ((fa->nfargs < 9) | (fa->nfargs % 3))
495                  return(-1);
496          setmat(mod);
497          setobj(id);
498 <        cp = entbuf;
498 >        cp = linestart = entbuf;
499          *cp++ = 'f';
500          for (i = 0; i < fa->nfargs; i += 3) {
501                  *cp++ = ' ';
502 +                if (cp - linestart > 72) {
503 +                        *cp++ = '\\'; *cp++ = '\n';
504 +                        linestart = cp;
505 +                        *cp++ = ' '; *cp++ = ' ';
506 +                }
507                  getvertid(cp, fa->farg + i);
508                  while (*cp)
509                          cp++;
# Line 394 | Line 514 | FUNARGS        *fa;
514  
515  
516   int
517 < o_cone(mod, typ, id, fa)        /* print out a cone */
518 < char    *mod, *typ, *id;
519 < register FUNARGS        *fa;
517 > o_cone( /* print out a cone */
518 >        char    *mod,
519 >        char    *typ,
520 >        char    *id,
521 >        register FUNARGS        *fa
522 > )
523   {
524          char    v1[6], v2[6];
525  
# Line 417 | Line 540 | register FUNARGS       *fa;
540  
541  
542   int
543 < o_sphere(mod, typ, id, fa)      /* print out a sphere */
544 < char    *mod, *typ, *id;
545 < register FUNARGS        *fa;
543 > o_sphere(       /* print out a sphere */
544 >        char    *mod,
545 >        char    *typ,
546 >        char    *id,
547 >        register FUNARGS        *fa
548 > )
549   {
550          char    cent[6];
551  
# Line 434 | Line 560 | register FUNARGS       *fa;
560  
561  
562   int
563 < o_cylinder(mod, typ, id, fa)    /* print out a cylinder */
564 < char    *mod, *typ, *id;
565 < register FUNARGS        *fa;
563 > o_cylinder(     /* print out a cylinder */
564 >        char    *mod,
565 >        char    *typ,
566 >        char    *id,
567 >        register FUNARGS        *fa
568 > )
569   {
570          char    v1[6], v2[6];
571  
# Line 453 | Line 582 | register FUNARGS       *fa;
582  
583  
584   int
585 < o_ring(mod, typ, id, fa)        /* print out a ring */
586 < char    *mod, *typ, *id;
587 < register FUNARGS        *fa;
585 > o_ring( /* print out a ring */
586 >        char    *mod,
587 >        char    *typ,
588 >        char    *id,
589 >        register FUNARGS        *fa
590 > )
591   {
592          if (fa->nfargs != 8)
593                  return(-1);
# Line 476 | Line 608 | register FUNARGS       *fa;
608  
609  
610   int
611 < o_instance(mod, typ, id, fa)    /* convert an instance */
612 < char    *mod, *typ, *id;
613 < FUNARGS *fa;
611 > o_instance(     /* convert an instance (or mesh) */
612 >        char    *mod,
613 >        char    *typ,
614 >        char    *id,
615 >        FUNARGS *fa
616 > )
617   {
618          register int    i;
619          register char   *cp;
# Line 511 | Line 646 | FUNARGS        *fa;
646                  fputs(fa->sarg[i], stdout);
647          }
648          putchar('\n');
649 +        clrverts();                     /* vertex id's no longer reliable */
650          return(0);
651   }
652  
653  
654   int
655 < o_source(mod, typ, id, fa)      /* convert a source */
656 < char    *mod, *typ, *id;
657 < FUNARGS *fa;
655 > o_illum(        /* convert an illum material */
656 >        char    *mod,
657 >        char    *typ,
658 >        char    *id,
659 >        FUNARGS *fa
660 > )
661   {
523        return(0);              /* there is no MGF equivalent! */
524 }
525
526
527 int
528 o_illum(mod, typ, id, fa)       /* convert an illum material */
529 char    *mod, *typ, *id;
530 FUNARGS *fa;
531 {
662          if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
663                  newmat(id, fa->sarg[0]);        /* just create alias */
664                  return(0);
# Line 541 | Line 671 | FUNARGS        *fa;
671  
672  
673   int
674 < o_plastic(mod, typ, id, fa)     /* convert a plastic material */
675 < char    *mod, *typ, *id;
676 < register FUNARGS        *fa;
674 > o_plastic(      /* convert a plastic material */
675 >        char    *mod,
676 >        char    *typ,
677 >        char    *id,
678 >        register FUNARGS        *fa
679 > )
680   {
681          COLOR   cxyz, rrgb;
682          double  d;
# Line 569 | Line 702 | register FUNARGS       *fa;
702  
703  
704   int
705 < o_metal(mod, typ, id, fa)       /* convert a metal material */
706 < char    *mod, *typ, *id;
707 < register FUNARGS        *fa;
705 > o_metal(        /* convert a metal material */
706 >        char    *mod,
707 >        char    *typ,
708 >        char    *id,
709 >        register FUNARGS        *fa
710 > )
711   {
712          COLOR   cxyz, rrgb;
713          double  d;
# Line 595 | Line 731 | register FUNARGS       *fa;
731  
732  
733   int
734 < o_glass(mod, typ, id, fa)       /* convert a glass material */
735 < char    *mod, *typ, *id;
736 < register FUNARGS        *fa;
734 > o_glass(        /* convert a glass material */
735 >        char    *mod,
736 >        char    *typ,
737 >        char    *id,
738 >        register FUNARGS        *fa
739 > )
740   {
741          COLOR   cxyz, rrgb, trgb;
742          double  nrfr = 1.52, F, d;
# Line 608 | Line 747 | register FUNARGS       *fa;
747          newmat(id, NULL);
748          if (fa->nfargs == 4)
749                  nrfr = fa->farg[3];
750 +        printf("\tir %f 0\n", nrfr);
751          F = (1. - nrfr)/(1. + nrfr);            /* use normal incidence */
752          F *= F;
753          for (i = 0; i < 3; i++) {
# Line 633 | Line 773 | register FUNARGS       *fa;
773  
774  
775   int
776 < o_mirror(mod, typ, id, fa)      /* convert a mirror material */
777 < char    *mod, *typ, *id;
778 < register FUNARGS        *fa;
776 > o_dielectric(   /* convert a dielectric material */
777 >        char    *mod,
778 >        char    *typ,
779 >        char    *id,
780 >        register FUNARGS        *fa
781 > )
782   {
783 +        COLOR   cxyz, trgb;
784 +        double  F, d;
785 +        register int    i;
786 +
787 +        if (fa->nfargs != 5)
788 +                return(-1);
789 +        newmat(id, NULL);
790 +        F = (1. - fa->farg[3])/(1. + fa->farg[3]);      /* normal incidence */
791 +        F *= F;
792 +        for (i = 0; i < 3; i++)
793 +                trgb[i] = (1. - F)*pow(fa->farg[i], C_1SIDEDTHICK/unit_mult);
794 +        printf("\tir %f 0\n", fa->farg[3]);     /* put index of refraction */
795 +        printf("\tsides 1\n");
796 +        puts("\tc");                            /* put reflected component */
797 +        printf("\trs %.4f 0\n", F);
798 +        rgb_cie(cxyz, trgb);                    /* put transmitted component */
799 +        puts("\tc");
800 +        d = cxyz[0] + cxyz[1] + cxyz[2];
801 +        if (d > FTINY)
802 +                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
803 +        printf("\tts %.4f 0\n", cxyz[1]);
804 +        return(0);
805 + }
806 +
807 +
808 + int
809 + o_mirror(       /* convert a mirror material */
810 +        char    *mod,
811 +        char    *typ,
812 +        char    *id,
813 +        register FUNARGS        *fa
814 + )
815 + {
816          COLOR   cxyz, rrgb;
817          double  d;
818  
# Line 659 | Line 835 | register FUNARGS       *fa;
835  
836  
837   int
838 < o_trans(mod, typ, id, fa)       /* convert a trans material */
839 < char    *mod, *typ, *id;
840 < register FUNARGS        *fa;
838 > o_trans(        /* convert a trans material */
839 >        char    *mod,
840 >        char    *typ,
841 >        char    *id,
842 >        register FUNARGS        *fa
843 > )
844   {
845          COLOR   cxyz, rrgb;
846          double  rough, trans, tspec, d;
# Line 698 | Line 877 | register FUNARGS       *fa;
877  
878  
879   int
880 < o_light(mod, typ, id, fa)               /* convert a light type */
881 < char    *mod, *typ, *id;
882 < register FUNARGS        *fa;
880 > o_light(                /* convert a light type */
881 >        char    *mod,
882 >        char    *typ,
883 >        char    *id,
884 >        register FUNARGS        *fa
885 > )
886   {
887          COLOR   cxyz, rrgb;
888          double  d;
# Line 714 | Line 896 | register FUNARGS       *fa;
896          puts("\tc");
897          if (d > FTINY)
898                  printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
899 <        printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY);
899 >        printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY));
900          return(0);
901   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines