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.25 by greg, Wed Dec 28 18:39:07 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
133 < #define typ     (buf+128)
103 < #define id      (buf+256)
104 < #define alias   (buf+384)
105 <        char    buf[512];
132 >        char  buf[512];
133 >        char  mod[128], typ[32], id[128], alias[128];
134          FUNARGS fa;
135          register FILE   *fp;
136          register int    c;
# Line 141 | Line 169 | char   *inp;
169                          break;
170                  default:                /* Radiance primitive */
171                          ungetc(c, fp);
172 <                        if (fscanf(fp, "%s %s %s", mod, typ, id) != 3) {
172 >                        if (fgetword(mod, sizeof(mod), fp) == NULL ||
173 >                                        fgetword(typ, sizeof(typ), fp) == NULL ||
174 >                                        fgetword(id, sizeof(id), fp) == NULL) {
175                                  fputs(inp, stderr);
176                                  fputs(": unexpected EOF\n", stderr);
177                                  exit(1);
178                          }
179 +                        unspace(mod);
180 +                        unspace(id);
181                          if (!strcmp(typ, "alias")) {
182                                  strcpy(alias, "EOF");
183 <                                fscanf(fp, "%s", alias);
183 >                                fgetword(alias, sizeof(alias), fp);
184 >                                unspace(alias);
185                                  newmat(id, alias);
186                          } else {
187                                  if (!readfargs(&fa, fp)) {
# Line 167 | Line 200 | char   *inp;
200                  pclose(fp);
201          else
202                  fclose(fp);
170 #undef mod
171 #undef typ
172 #undef id
173 #undef alias
203   }
204  
205  
206 < cvtprim(inp, mod, typ, id, fa)  /* process Radiance primitive */
207 < char    *inp, *mod, *typ, *id;
208 < FUNARGS *fa;
206 > void
207 > unspace(        /* replace spaces with underscores in s */
208 >        char *s
209 > )
210   {
211 +        while (*s) {
212 +                if (isspace(*s))
213 +                        *s = '_';
214 +                ++s;
215 +        }
216 + }
217 +
218 +
219 + void
220 + cvtprim(        /* process Radiance primitive */
221 +        char    *inp,
222 +        char    *mod,
223 +        char    *typ,
224 +        char    *id,
225 +        FUNARGS *fa
226 + )
227 + {
228          int     (*df)();
229  
230          df = (int (*)())lu_find(&rdispatch, typ)->data;
231          if (df != NULL) {                               /* convert */
232                  if ((*df)(mod, typ, id, fa) < 0) {
233 <                        fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id);
233 >                        fprintf(stderr, "%s: bad %s \"%s\"\n", "rad2mgf", typ, id);
234                          exit(1);
235                  }
236 <        } else if (lu_find(&rmats, mod)->data != NULL)  /* make alias */
237 <                newmat(id, mod);
236 >        } else {                                        /* unsupported */
237 >                o_unsupported(mod, typ, id, fa);
238 >                if (lu_find(&rmats, mod)->data != NULL) /* make alias */
239 >                        newmat(id, mod);
240 >        }
241   }
242  
243  
244 < newmat(id, alias)               /* add a modifier to the alias list */
245 < char    *id;
246 < char    *alias;
244 > void
245 > newmat(         /* add a modifier to the alias list */
246 >        char    *id,
247 >        char    *alias
248 > )
249   {
250          register LUENT  *lp, *lpa;
251  
# Line 227 | Line 279 | memerr:
279   }
280  
281  
282 < setmat(id)                      /* set material to this one */
283 < char    *id;
282 > void
283 > setmat(                 /* set material to this one */
284 >        char    *id
285 > )
286   {
287          if (!strcmp(id, curmat))        /* already set? */
288                  return;
# Line 239 | Line 293 | char   *id;
293   }
294  
295  
296 < setobj(id)                      /* set object name to this one */
297 < char    *id;
296 > void
297 > setobj(                 /* set object name to this one */
298 >        char    *id
299 > )
300   {
301          register char   *cp, *cp2;
302          char    *end = NULL;
# Line 252 | Line 308 | char   *id;
308          if (end == NULL)
309                  end = cp;
310                                  /* copy to current object */
311 <        for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++)
311 >        cp2 = curobj;
312 >        if (!isalpha(*id)) {    /* start with letter */
313 >                diff = *cp2 != 'O';
314 >                *cp2++ = 'O';
315 >        }
316 >        for (cp = id; cp < end; *cp2++ = *cp++) {
317 >                if ((*cp < '!') | (*cp > '~'))  /* limit to visible chars */
318 >                        *cp = '?';
319                  diff += *cp != *cp2;
320 +        }
321          if (!diff && !*cp2)
322                  return;
323          *cp2 = '\0';
# Line 262 | Line 326 | char   *id;
326   }
327  
328  
329 < init()                  /* initialize dispatch table and output */
329 > void
330 > init(void)                      /* initialize dispatch table and output */
331   {
332          lu_init(&vertab, NVERTS);
333          lu_init(&rdispatch, 22);
# Line 275 | Line 340 | init()                 /* initialize dispatch table and output */
340          add2dispatch("tube", o_cylinder);
341          add2dispatch("ring", o_ring);
342          add2dispatch("instance", o_instance);
343 +        add2dispatch("mesh", o_instance);
344          add2dispatch("plastic", o_plastic);
345          add2dispatch("plastic2", o_plastic);
346          add2dispatch("metal", o_metal);
347          add2dispatch("metal2", o_metal);
348          add2dispatch("glass", o_glass);
349 +        add2dispatch("dielectric", o_dielectric);
350          add2dispatch("trans", o_trans);
351          add2dispatch("trans2", o_trans);
352          add2dispatch("mirror", o_mirror);
# Line 287 | Line 354 | init()                 /* initialize dispatch table and output */
354          add2dispatch("spotlight", o_light);
355          add2dispatch("glow", o_light);
356          add2dispatch("illum", o_illum);
357 <        puts("# The following was converted from Radiance scene input");
357 >        puts("# The following was converted from RADIANCE scene input");
358          if (hasmult)
359                  printf("xf -s %.4e\n", unit_mult);
360          printf("o %s\n", curobj);
361   }
362  
363  
364 < uninit()                        /* mark end of MGF file */
364 > void
365 > uninit(void)                    /* mark end of MGF file */
366   {
367          puts("o");
368          if (hasmult)
369                  puts("xf");
370 <        puts("# End of data converted from Radiance scene input");
370 >        puts("# End of data converted from RADIANCE scene input");
371          lu_done(&rdispatch);
372          lu_done(&rmats);
373          lu_done(&vertab);
374   }
375  
376  
377 < add2dispatch(name, func)        /* add function to dispatch table */
378 < char    *name;
311 < int     (*func)();
377 > void
378 > clrverts(void)                  /* clear vertex table */
379   {
380 +        register int    i;
381 +
382 +        lu_done(&vertab);
383 +        for (i = 0; i < NVERTS; i++)
384 +                vert[i].lused = 0;
385 +        lu_init(&vertab, NVERTS);
386 + }
387 +
388 +
389 + void
390 + add2dispatch(   /* add function to dispatch table */
391 +        char    *name,
392 +        int     (*func)()
393 + )
394 + {
395          register LUENT  *lp;
396  
397          lp = lu_find(&rdispatch, name);
# Line 324 | Line 406 | int    (*func)();
406  
407  
408   char *
409 < getvertid(vname, vp)            /* get/set vertex ID for this point */
410 < char    *vname;
411 < FVECT   vp;
409 > getvertid(              /* get/set vertex ID for this point */
410 >        char    *vname,
411 >        FVECT   vp
412 > )
413   {
414 <        char    vkey[VKLEN];
414 >        static char     vkey[VKLEN];
415          register LUENT  *lp;
416          register int    i, vndx;
417  
418 <        clock++;                        /* increment counter */
418 >        vclock++;                       /* increment counter */
419          mkvkey(vkey, vp);
420          if ((lp = lu_find(&vertab, vkey)) == NULL)
421                  goto memerr;
# Line 358 | Line 441 | FVECT  vp;
441                  lp->data = (char *)&vert[vndx];                 /* set it */
442          } else
443                  vndx = (struct vert *)lp->data - vert;
444 <        vert[vndx].lused = clock;               /* record this use */
444 >        vert[vndx].lused = vclock;              /* record this use */
445          sprintf(vname, "v%d", vndx);
446          return(vname);
447   memerr:
# Line 368 | Line 451 | memerr:
451  
452  
453   int
454 < o_face(mod, typ, id, fa)                /* print out a polygon */
455 < char    *mod, *typ, *id;
456 < FUNARGS *fa;
454 > o_unsupported(          /* mark unsupported primitive */
455 >        char    *mod,
456 >        char    *typ,
457 >        char    *id,
458 >        FUNARGS *fa
459 > )
460   {
461 <        char    entbuf[512];
461 >        register int    i;
462 >
463 >        fputs("\n# Unsupported RADIANCE primitive:\n", stdout);
464 >        printf("# %s %s %s", mod, typ, id);
465 >        printf("\n# %d", fa->nsargs);
466 >        for (i = 0; i < fa->nsargs; i++)
467 >                printf(" %s", fa->sarg[i]);
468 > #ifdef IARGS
469 >        printf("\n# %d", fa->niargs);
470 >        for (i = 0; i < fa->niargs; i++)
471 >                printf(" %ld", fa->iarg[i]);
472 > #else
473 >        fputs("\n# 0", stdout);
474 > #endif
475 >        printf("\n# %d", fa->nfargs);
476 >        for (i = 0; i < fa->nfargs; i++)
477 >                printf(" %g", fa->farg[i]);
478 >        fputs("\n\n", stdout);
479 >        return(0);
480 > }
481 >
482 >
483 > int
484 > o_face(         /* print out a polygon */
485 >        char    *mod,
486 >        char    *typ,
487 >        char    *id,
488 >        FUNARGS *fa
489 > )
490 > {
491 >        char    entbuf[2048], *linestart;
492          register char   *cp;
493          register int    i;
494  
495 <        if (fa->nfargs < 9 | fa->nfargs % 3)
495 >        if ((fa->nfargs < 9) | (fa->nfargs % 3))
496                  return(-1);
497          setmat(mod);
498          setobj(id);
499 <        cp = entbuf;
499 >        cp = linestart = entbuf;
500          *cp++ = 'f';
501          for (i = 0; i < fa->nfargs; i += 3) {
502                  *cp++ = ' ';
503 +                if (cp - linestart > 72) {
504 +                        *cp++ = '\\'; *cp++ = '\n';
505 +                        linestart = cp;
506 +                        *cp++ = ' '; *cp++ = ' ';
507 +                }
508                  getvertid(cp, fa->farg + i);
509                  while (*cp)
510                          cp++;
# Line 394 | Line 515 | FUNARGS        *fa;
515  
516  
517   int
518 < o_cone(mod, typ, id, fa)        /* print out a cone */
519 < char    *mod, *typ, *id;
520 < register FUNARGS        *fa;
518 > o_cone( /* print out a cone */
519 >        char    *mod,
520 >        char    *typ,
521 >        char    *id,
522 >        register FUNARGS        *fa
523 > )
524   {
525          char    v1[6], v2[6];
526  
# Line 417 | Line 541 | register FUNARGS       *fa;
541  
542  
543   int
544 < o_sphere(mod, typ, id, fa)      /* print out a sphere */
545 < char    *mod, *typ, *id;
546 < register FUNARGS        *fa;
544 > o_sphere(       /* print out a sphere */
545 >        char    *mod,
546 >        char    *typ,
547 >        char    *id,
548 >        register FUNARGS        *fa
549 > )
550   {
551          char    cent[6];
552  
# Line 434 | Line 561 | register FUNARGS       *fa;
561  
562  
563   int
564 < o_cylinder(mod, typ, id, fa)    /* print out a cylinder */
565 < char    *mod, *typ, *id;
566 < register FUNARGS        *fa;
564 > o_cylinder(     /* print out a cylinder */
565 >        char    *mod,
566 >        char    *typ,
567 >        char    *id,
568 >        register FUNARGS        *fa
569 > )
570   {
571          char    v1[6], v2[6];
572  
# Line 453 | Line 583 | register FUNARGS       *fa;
583  
584  
585   int
586 < o_ring(mod, typ, id, fa)        /* print out a ring */
587 < char    *mod, *typ, *id;
588 < register FUNARGS        *fa;
586 > o_ring( /* print out a ring */
587 >        char    *mod,
588 >        char    *typ,
589 >        char    *id,
590 >        register FUNARGS        *fa
591 > )
592   {
593          if (fa->nfargs != 8)
594                  return(-1);
# Line 476 | Line 609 | register FUNARGS       *fa;
609  
610  
611   int
612 < o_instance(mod, typ, id, fa)    /* convert an instance */
613 < char    *mod, *typ, *id;
614 < FUNARGS *fa;
612 > o_instance(     /* convert an instance (or mesh) */
613 >        char    *mod,
614 >        char    *typ,
615 >        char    *id,
616 >        FUNARGS *fa
617 > )
618   {
619          register int    i;
620          register char   *cp;
# Line 511 | Line 647 | FUNARGS        *fa;
647                  fputs(fa->sarg[i], stdout);
648          }
649          putchar('\n');
650 +        clrverts();                     /* vertex id's no longer reliable */
651          return(0);
652   }
653  
654  
655   int
656 < o_source(mod, typ, id, fa)      /* convert a source */
657 < char    *mod, *typ, *id;
658 < FUNARGS *fa;
656 > o_illum(        /* convert an illum material */
657 >        char    *mod,
658 >        char    *typ,
659 >        char    *id,
660 >        FUNARGS *fa
661 > )
662   {
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 {
663          if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
664                  newmat(id, fa->sarg[0]);        /* just create alias */
665                  return(0);
# Line 541 | Line 672 | FUNARGS        *fa;
672  
673  
674   int
675 < o_plastic(mod, typ, id, fa)     /* convert a plastic material */
676 < char    *mod, *typ, *id;
677 < register FUNARGS        *fa;
675 > o_plastic(      /* convert a plastic material */
676 >        char    *mod,
677 >        char    *typ,
678 >        char    *id,
679 >        register FUNARGS        *fa
680 > )
681   {
682          COLOR   cxyz, rrgb;
683          double  d;
# Line 569 | Line 703 | register FUNARGS       *fa;
703  
704  
705   int
706 < o_metal(mod, typ, id, fa)       /* convert a metal material */
707 < char    *mod, *typ, *id;
708 < register FUNARGS        *fa;
706 > o_metal(        /* convert a metal material */
707 >        char    *mod,
708 >        char    *typ,
709 >        char    *id,
710 >        register FUNARGS        *fa
711 > )
712   {
713          COLOR   cxyz, rrgb;
714          double  d;
# Line 595 | Line 732 | register FUNARGS       *fa;
732  
733  
734   int
735 < o_glass(mod, typ, id, fa)       /* convert a glass material */
736 < char    *mod, *typ, *id;
737 < register FUNARGS        *fa;
735 > o_glass(        /* convert a glass material */
736 >        char    *mod,
737 >        char    *typ,
738 >        char    *id,
739 >        register FUNARGS        *fa
740 > )
741   {
742          COLOR   cxyz, rrgb, trgb;
743          double  nrfr = 1.52, F, d;
# Line 608 | Line 748 | register FUNARGS       *fa;
748          newmat(id, NULL);
749          if (fa->nfargs == 4)
750                  nrfr = fa->farg[3];
751 +        printf("\tir %f 0\n", nrfr);
752          F = (1. - nrfr)/(1. + nrfr);            /* use normal incidence */
753          F *= F;
754          for (i = 0; i < 3; i++) {
# Line 633 | Line 774 | register FUNARGS       *fa;
774  
775  
776   int
777 < o_mirror(mod, typ, id, fa)      /* convert a mirror material */
778 < char    *mod, *typ, *id;
779 < register FUNARGS        *fa;
777 > o_dielectric(   /* convert a dielectric material */
778 >        char    *mod,
779 >        char    *typ,
780 >        char    *id,
781 >        register FUNARGS        *fa
782 > )
783   {
784 +        COLOR   cxyz, trgb;
785 +        double  F, d;
786 +        register int    i;
787 +
788 +        if (fa->nfargs != 5)
789 +                return(-1);
790 +        newmat(id, NULL);
791 +        F = (1. - fa->farg[3])/(1. + fa->farg[3]);      /* normal incidence */
792 +        F *= F;
793 +        for (i = 0; i < 3; i++)
794 +                trgb[i] = (1. - F)*pow(fa->farg[i], C_1SIDEDTHICK/unit_mult);
795 +        printf("\tir %f 0\n", fa->farg[3]);     /* put index of refraction */
796 +        printf("\tsides 1\n");
797 +        puts("\tc");                            /* put reflected component */
798 +        printf("\trs %.4f 0\n", F);
799 +        rgb_cie(cxyz, trgb);                    /* put transmitted component */
800 +        puts("\tc");
801 +        d = cxyz[0] + cxyz[1] + cxyz[2];
802 +        if (d > FTINY)
803 +                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
804 +        printf("\tts %.4f 0\n", cxyz[1]);
805 +        return(0);
806 + }
807 +
808 +
809 + int
810 + o_mirror(       /* convert a mirror material */
811 +        char    *mod,
812 +        char    *typ,
813 +        char    *id,
814 +        register FUNARGS        *fa
815 + )
816 + {
817          COLOR   cxyz, rrgb;
818          double  d;
819  
# Line 659 | Line 836 | register FUNARGS       *fa;
836  
837  
838   int
839 < o_trans(mod, typ, id, fa)       /* convert a trans material */
840 < char    *mod, *typ, *id;
841 < register FUNARGS        *fa;
839 > o_trans(        /* convert a trans material */
840 >        char    *mod,
841 >        char    *typ,
842 >        char    *id,
843 >        register FUNARGS        *fa
844 > )
845   {
846          COLOR   cxyz, rrgb;
847          double  rough, trans, tspec, d;
# Line 698 | Line 878 | register FUNARGS       *fa;
878  
879  
880   int
881 < o_light(mod, typ, id, fa)               /* convert a light type */
882 < char    *mod, *typ, *id;
883 < register FUNARGS        *fa;
881 > o_light(                /* convert a light type */
882 >        char    *mod,
883 >        char    *typ,
884 >        char    *id,
885 >        register FUNARGS        *fa
886 > )
887   {
888          COLOR   cxyz, rrgb;
889          double  d;
# Line 714 | Line 897 | register FUNARGS       *fa;
897          puts("\tc");
898          if (d > FTINY)
899                  printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
900 <        printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY);
900 >        printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY));
901          return(0);
902   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines