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.2 by greg, Fri Jul 8 16:10:06 1994 UTC vs.
Revision 2.22 by schorsch, Sat Nov 15 16:29:11 2003 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 "platform.h"
9 > #include "standard.h"
10 > #include <ctype.h>
11   #include <string.h>
12 < #include "fvect.h"
12 > #include <stdio.h>
13 >
14 > #include "rtprocess.h"
15   #include "object.h"
16   #include "color.h"
17   #include "lookup.h"
18  
19 < 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();
19 > #define C_1SIDEDTHICK   0.005
20  
22 extern void     free();
23 extern char     *malloc();
21  
22   LUTAB   rmats = LU_SINIT(free,NULL);            /* defined material table */
23  
# Line 33 | Line 30 | double unit_mult = 1.;                         /* units multiplier */
30  
31   #define hasmult         (unit_mult < .999 || unit_mult > 1.001)
32  
33 + /*
34 + * Stuff for tracking and reusing vertices:
35 + */
36  
37 < main(argc, argv)
38 < int     argc;
39 < char    **argv;
37 > char    VKFMT[] = "%+16.9e %+16.9e %+16.9e";
38 > #define VKLEN           64
39 >
40 > #define mkvkey(k,v)     sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2])
41 >
42 > #define NVERTS          256
43 >
44 > long    vclock;         /* incremented at each vertex request */
45 >
46 > struct vert {
47 >        long    lused;          /* when last used (0 if unassigned) */
48 >        FVECT   p;              /* track point position only */
49 > } vert[NVERTS];         /* our vertex cache */
50 >
51 > LUTAB   vertab = LU_SINIT(free,NULL);   /* our vertex lookup table */
52 >
53 > static void rad2mgf(char *inp);
54 > static void cvtprim(char *inp, char *mod, char *typ, char *id, FUNARGS *fa);
55 > static void newmat(char *id, char *alias);
56 > static void setmat(char *id);
57 > static void setobj(char *id);
58 > static void init(void);
59 > static void uninit(void);
60 > static void clrverts(void);
61 > static void add2dispatch(char *name, int (*func)());
62 > static char *getvertid(char *vname, FVECT vp);
63 > static int o_unsupported(char *mod, char *typ, char *id, FUNARGS *fa);
64 > static int o_face(char *mod, char *typ, char *id, FUNARGS *fa);
65 > static int o_cone(char *mod, char *typ, char *id, FUNARGS *fa);
66 > static int o_sphere(char *mod, char *typ, char *id, FUNARGS *fa);
67 > static int o_cylinder(char *mod, char *typ, char *id, FUNARGS *fa);
68 > static int o_ring(char *mod, char *typ, char *id, FUNARGS *fa);
69 > static int o_instance(char *mod, char *typ, char *id, FUNARGS *fa);
70 > static int o_illum(char *mod, char *typ, char *id, FUNARGS *fa);
71 > static int o_plastic(char *mod, char *typ, char *id, FUNARGS *fa);
72 > static int o_metal(char *mod, char *typ, char *id, FUNARGS *fa);
73 > static int o_glass(char *mod, char *typ, char *id, FUNARGS *fa);
74 > static int o_dielectric(char *mod, char *typ, char *id, FUNARGS *fa);
75 > static int o_mirror(char *mod, char *typ, char *id, FUNARGS *fa);
76 > static int o_trans(char *mod, char *typ, char *id, FUNARGS *fa);
77 > static int o_light(char *mod, char *typ, char *id, FUNARGS *fa);
78 >
79 >
80 > int
81 > main(
82 >        int     argc,
83 >        char    **argv
84 > )
85   {
86          int     i;
87  
# Line 60 | Line 105 | char   **argv;
105                                  goto unkopt;
106                          }
107                          break;
108 +                default:
109 +                        goto unkopt;
110                  }
111          init();
112          if (i >= argc)
# Line 75 | Line 122 | unkopt:
122   }
123  
124  
125 < rad2mgf(inp)            /* convert a Radiance file to MGF */
126 < char    *inp;
125 > void
126 > rad2mgf(                /* convert a Radiance file to MGF */
127 >        char    *inp
128 > )
129   {
130   #define mod     buf
131   #define typ     (buf+128)
# Line 154 | Line 203 | char   *inp;
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 > cvtprim(        /* process Radiance primitive */
208 >        char    *inp,
209 >        char    *mod,
210 >        char    *typ,
211 >        char    *id,
212 >        FUNARGS *fa
213 > )
214   {
215          int     (*df)();
216  
217          df = (int (*)())lu_find(&rdispatch, typ)->data;
218          if (df != NULL) {                               /* convert */
219                  if ((*df)(mod, typ, id, fa) < 0) {
220 <                        fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id);
220 >                        fprintf(stderr, "%s: bad %s \"%s\"\n", "rat2mgf", typ, id);
221                          exit(1);
222                  }
223 <        } else if (lu_find(&rmats, mod)->data != NULL)  /* make alias */
224 <                newmat(id, mod);
223 >        } else {                                        /* unsupported */
224 >                o_unsupported(mod, typ, id, fa);
225 >                if (lu_find(&rmats, mod)->data != NULL) /* make alias */
226 >                        newmat(id, mod);
227 >        }
228   }
229  
230  
231 < newmat(id, alias)               /* add a modifier to the alias list */
232 < char    *id;
233 < char    *alias;
231 > void
232 > newmat(         /* add a modifier to the alias list */
233 >        char    *id,
234 >        char    *alias
235 > )
236   {
237          register LUENT  *lp, *lpa;
238  
# Line 207 | Line 266 | memerr:
266   }
267  
268  
269 < setmat(id)                      /* set material to this one */
270 < char    *id;
269 > void
270 > setmat(                 /* set material to this one */
271 >        char    *id
272 > )
273   {
274          if (!strcmp(id, curmat))        /* already set? */
275                  return;
# Line 219 | Line 280 | char   *id;
280   }
281  
282  
283 < setobj(id)                      /* set object name to this one */
284 < char    *id;
283 > void
284 > setobj(                 /* set object name to this one */
285 >        char    *id
286 > )
287   {
288          register char   *cp, *cp2;
289          char    *end = NULL;
# Line 232 | Line 295 | char   *id;
295          if (end == NULL)
296                  end = cp;
297                                  /* copy to current object */
298 <        for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++)
298 >        cp2 = curobj;
299 >        if (!isalpha(*id)) {    /* start with letter */
300 >                diff = *cp2 != 'O';
301 >                *cp2++ = 'O';
302 >        }
303 >        for (cp = id; cp < end; *cp2++ = *cp++) {
304 >                if ((*cp < '!') | (*cp > '~'))  /* limit to visible chars */
305 >                        *cp = '?';
306                  diff += *cp != *cp2;
307 +        }
308          if (!diff && !*cp2)
309                  return;
310          *cp2 = '\0';
# Line 242 | Line 313 | char   *id;
313   }
314  
315  
316 < init()                  /* initialize dispatch table and output */
316 > void
317 > init(void)                      /* initialize dispatch table and output */
318   {
319 +        lu_init(&vertab, NVERTS);
320          lu_init(&rdispatch, 22);
321          add2dispatch("polygon", o_face);
322          add2dispatch("cone", o_cone);
# Line 254 | Line 327 | init()                 /* initialize dispatch table and output */
327          add2dispatch("tube", o_cylinder);
328          add2dispatch("ring", o_ring);
329          add2dispatch("instance", o_instance);
330 +        add2dispatch("mesh", o_instance);
331          add2dispatch("plastic", o_plastic);
332          add2dispatch("plastic2", o_plastic);
333          add2dispatch("metal", o_metal);
334          add2dispatch("metal2", o_metal);
335          add2dispatch("glass", o_glass);
336 +        add2dispatch("dielectric", o_dielectric);
337          add2dispatch("trans", o_trans);
338          add2dispatch("trans2", o_trans);
339          add2dispatch("mirror", o_mirror);
# Line 266 | Line 341 | init()                 /* initialize dispatch table and output */
341          add2dispatch("spotlight", o_light);
342          add2dispatch("glow", o_light);
343          add2dispatch("illum", o_illum);
344 <        puts("# The following was converted from Radiance scene input");
344 >        puts("# The following was converted from RADIANCE scene input");
345          if (hasmult)
346                  printf("xf -s %.4e\n", unit_mult);
347          printf("o %s\n", curobj);
348   }
349  
350  
351 < uninit()                        /* mark end of MGF file */
351 > void
352 > uninit(void)                    /* mark end of MGF file */
353   {
354          puts("o");
355          if (hasmult)
356                  puts("xf");
357 <        puts("# End of data converted from Radiance scene input");
357 >        puts("# End of data converted from RADIANCE scene input");
358          lu_done(&rdispatch);
359          lu_done(&rmats);
360 +        lu_done(&vertab);
361   }
362  
363  
364 < add2dispatch(name, func)        /* add function to dispatch table */
365 < char    *name;
289 < int     (*func)();
364 > void
365 > clrverts(void)                  /* clear vertex table */
366   {
367 +        register int    i;
368 +
369 +        lu_done(&vertab);
370 +        for (i = 0; i < NVERTS; i++)
371 +                vert[i].lused = 0;
372 +        lu_init(&vertab, NVERTS);
373 + }
374 +
375 +
376 + void
377 + add2dispatch(   /* add function to dispatch table */
378 +        char    *name,
379 +        int     (*func)()
380 + )
381 + {
382          register LUENT  *lp;
383  
384          lp = lu_find(&rdispatch, name);
# Line 301 | Line 392 | int    (*func)();
392   }
393  
394  
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
395   char *
396 < getvertid(vname, vp)            /* get/set vertex ID for this point */
397 < char    *vname;
398 < FVECT   vp;
396 > getvertid(              /* get/set vertex ID for this point */
397 >        char    *vname,
398 >        FVECT   vp
399 > )
400   {
401 <        char    vkey[VKLEN];
401 >        static char     vkey[VKLEN];
402          register LUENT  *lp;
403          register int    i, vndx;
404  
405 <        if (!vertab.tsiz && !lu_init(&vertab, NVERTS))
335 <                goto memerr;
336 <        clock++;                        /* increment counter */
405 >        vclock++;                       /* increment counter */
406          mkvkey(vkey, vp);
407          if ((lp = lu_find(&vertab, vkey)) == NULL)
408                  goto memerr;
# Line 353 | Line 422 | FVECT  vp;
422                          mkvkey(vkey, vert[vndx].p);
423                          lu_delete(&vertab, vkey);
424                  }
425 <                vert[vndx].lused = clock;                       /* assign it */
357 <                VCOPY(vert[vndx].p, vp);
425 >                VCOPY(vert[vndx].p, vp);                        /* assign it */
426                  printf("v v%d =\n\tp %.15g %.15g %.15g\n",      /* print it */
427                                  vndx, vp[0], vp[1], vp[2]);
428                  lp->data = (char *)&vert[vndx];                 /* set it */
429          } else
430                  vndx = (struct vert *)lp->data - vert;
431 +        vert[vndx].lused = vclock;              /* record this use */
432          sprintf(vname, "v%d", vndx);
433          return(vname);
434   memerr:
# Line 369 | Line 438 | memerr:
438  
439  
440   int
441 < o_face(mod, typ, id, fa)                /* print out a polygon */
442 < char    *mod, *typ, *id;
443 < FUNARGS *fa;
441 > o_unsupported(          /* mark unsupported primitive */
442 >        char    *mod,
443 >        char    *typ,
444 >        char    *id,
445 >        FUNARGS *fa
446 > )
447   {
448 <        char    entbuf[512];
448 >        register int    i;
449 >
450 >        fputs("\n# Unsupported RADIANCE primitive:\n", stdout);
451 >        printf("# %s %s %s", mod, typ, id);
452 >        printf("\n# %d", fa->nsargs);
453 >        for (i = 0; i < fa->nsargs; i++)
454 >                printf(" %s", fa->sarg[i]);
455 > #ifdef IARGS
456 >        printf("\n# %d", fa->niargs);
457 >        for (i = 0; i < fa->niargs; i++)
458 >                printf(" %ld", fa->iarg[i]);
459 > #else
460 >        fputs("\n# 0", stdout);
461 > #endif
462 >        printf("\n# %d", fa->nfargs);
463 >        for (i = 0; i < fa->nfargs; i++)
464 >                printf(" %g", fa->farg[i]);
465 >        fputs("\n\n", stdout);
466 >        return(0);
467 > }
468 >
469 >
470 > int
471 > o_face(         /* print out a polygon */
472 >        char    *mod,
473 >        char    *typ,
474 >        char    *id,
475 >        FUNARGS *fa
476 > )
477 > {
478 >        char    entbuf[2048], *linestart;
479          register char   *cp;
480          register int    i;
481  
482 <        if (fa->nfargs < 9 | fa->nfargs % 3)
482 >        if ((fa->nfargs < 9) | (fa->nfargs % 3))
483                  return(-1);
484          setmat(mod);
485          setobj(id);
486 <        cp = entbuf;
486 >        cp = linestart = entbuf;
487          *cp++ = 'f';
488          for (i = 0; i < fa->nfargs; i += 3) {
489                  *cp++ = ' ';
490 +                if (cp - linestart > 72) {
491 +                        *cp++ = '\\'; *cp++ = '\n';
492 +                        linestart = cp;
493 +                        *cp++ = ' '; *cp++ = ' ';
494 +                }
495                  getvertid(cp, fa->farg + i);
496                  while (*cp)
497                          cp++;
# Line 395 | Line 502 | FUNARGS        *fa;
502  
503  
504   int
505 < o_cone(mod, typ, id, fa)        /* print out a cone */
506 < char    *mod, *typ, *id;
507 < register FUNARGS        *fa;
505 > o_cone( /* print out a cone */
506 >        char    *mod,
507 >        char    *typ,
508 >        char    *id,
509 >        register FUNARGS        *fa
510 > )
511   {
512          char    v1[6], v2[6];
513  
# Line 418 | Line 528 | register FUNARGS       *fa;
528  
529  
530   int
531 < o_sphere(mod, typ, id, fa)      /* print out a sphere */
532 < char    *mod, *typ, *id;
533 < register FUNARGS        *fa;
531 > o_sphere(       /* print out a sphere */
532 >        char    *mod,
533 >        char    *typ,
534 >        char    *id,
535 >        register FUNARGS        *fa
536 > )
537   {
538          char    cent[6];
539  
# Line 435 | Line 548 | register FUNARGS       *fa;
548  
549  
550   int
551 < o_cylinder(mod, typ, id, fa)    /* print out a cylinder */
552 < char    *mod, *typ, *id;
553 < register FUNARGS        *fa;
551 > o_cylinder(     /* print out a cylinder */
552 >        char    *mod,
553 >        char    *typ,
554 >        char    *id,
555 >        register FUNARGS        *fa
556 > )
557   {
558          char    v1[6], v2[6];
559  
# Line 454 | Line 570 | register FUNARGS       *fa;
570  
571  
572   int
573 < o_ring(mod, typ, id, fa)        /* print out a ring */
574 < char    *mod, *typ, *id;
575 < register FUNARGS        *fa;
573 > o_ring( /* print out a ring */
574 >        char    *mod,
575 >        char    *typ,
576 >        char    *id,
577 >        register FUNARGS        *fa
578 > )
579   {
580          if (fa->nfargs != 8)
581                  return(-1);
# Line 477 | Line 596 | register FUNARGS       *fa;
596  
597  
598   int
599 < o_instance(mod, typ, id, fa)    /* convert an instance */
600 < char    *mod, *typ, *id;
601 < FUNARGS *fa;
599 > o_instance(     /* convert an instance (or mesh) */
600 >        char    *mod,
601 >        char    *typ,
602 >        char    *id,
603 >        FUNARGS *fa
604 > )
605   {
606          register int    i;
607          register char   *cp;
# Line 512 | Line 634 | FUNARGS        *fa;
634                  fputs(fa->sarg[i], stdout);
635          }
636          putchar('\n');
637 +        clrverts();                     /* vertex id's no longer reliable */
638          return(0);
639   }
640  
641  
642   int
643 < o_source(mod, typ, id, fa)      /* convert a source */
644 < char    *mod, *typ, *id;
645 < FUNARGS *fa;
643 > o_illum(        /* convert an illum material */
644 >        char    *mod,
645 >        char    *typ,
646 >        char    *id,
647 >        FUNARGS *fa
648 > )
649   {
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 {
650          if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) {
651                  newmat(id, fa->sarg[0]);        /* just create alias */
652                  return(0);
# Line 542 | Line 659 | FUNARGS        *fa;
659  
660  
661   int
662 < o_plastic(mod, typ, id, fa)     /* convert a plastic material */
663 < char    *mod, *typ, *id;
664 < register FUNARGS        *fa;
662 > o_plastic(      /* convert a plastic material */
663 >        char    *mod,
664 >        char    *typ,
665 >        char    *id,
666 >        register FUNARGS        *fa
667 > )
668   {
669          COLOR   cxyz, rrgb;
670          double  d;
# Line 570 | Line 690 | register FUNARGS       *fa;
690  
691  
692   int
693 < o_metal(mod, typ, id, fa)       /* convert a metal material */
694 < char    *mod, *typ, *id;
695 < register FUNARGS        *fa;
693 > o_metal(        /* convert a metal material */
694 >        char    *mod,
695 >        char    *typ,
696 >        char    *id,
697 >        register FUNARGS        *fa
698 > )
699   {
700          COLOR   cxyz, rrgb;
701          double  d;
# Line 596 | Line 719 | register FUNARGS       *fa;
719  
720  
721   int
722 < o_glass(mod, typ, id, fa)       /* convert a glass material */
723 < char    *mod, *typ, *id;
724 < register FUNARGS        *fa;
722 > o_glass(        /* convert a glass material */
723 >        char    *mod,
724 >        char    *typ,
725 >        char    *id,
726 >        register FUNARGS        *fa
727 > )
728   {
729          COLOR   cxyz, rrgb, trgb;
730          double  nrfr = 1.52, F, d;
# Line 609 | Line 735 | register FUNARGS       *fa;
735          newmat(id, NULL);
736          if (fa->nfargs == 4)
737                  nrfr = fa->farg[3];
738 +        printf("\tir %f 0\n", nrfr);
739          F = (1. - nrfr)/(1. + nrfr);            /* use normal incidence */
740          F *= F;
741          for (i = 0; i < 3; i++) {
# Line 634 | Line 761 | register FUNARGS       *fa;
761  
762  
763   int
764 < o_mirror(mod, typ, id, fa)      /* convert a mirror material */
765 < char    *mod, *typ, *id;
766 < register FUNARGS        *fa;
764 > o_dielectric(   /* convert a dielectric material */
765 >        char    *mod,
766 >        char    *typ,
767 >        char    *id,
768 >        register FUNARGS        *fa
769 > )
770   {
771 +        COLOR   cxyz, trgb;
772 +        double  F, d;
773 +        register int    i;
774 +
775 +        if (fa->nfargs != 5)
776 +                return(-1);
777 +        newmat(id, NULL);
778 +        F = (1. - fa->farg[3])/(1. + fa->farg[3]);      /* normal incidence */
779 +        F *= F;
780 +        for (i = 0; i < 3; i++)
781 +                trgb[i] = (1. - F)*pow(fa->farg[i], C_1SIDEDTHICK/unit_mult);
782 +        printf("\tir %f 0\n", fa->farg[3]);     /* put index of refraction */
783 +        printf("\tsides 1\n");
784 +        puts("\tc");                            /* put reflected component */
785 +        printf("\trs %.4f 0\n", F);
786 +        rgb_cie(cxyz, trgb);                    /* put transmitted component */
787 +        puts("\tc");
788 +        d = cxyz[0] + cxyz[1] + cxyz[2];
789 +        if (d > FTINY)
790 +                printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
791 +        printf("\tts %.4f 0\n", cxyz[1]);
792 +        return(0);
793 + }
794 +
795 +
796 + int
797 + o_mirror(       /* convert a mirror material */
798 +        char    *mod,
799 +        char    *typ,
800 +        char    *id,
801 +        register FUNARGS        *fa
802 + )
803 + {
804          COLOR   cxyz, rrgb;
805          double  d;
806  
# Line 660 | Line 823 | register FUNARGS       *fa;
823  
824  
825   int
826 < o_trans(mod, typ, id, fa)       /* convert a trans material */
827 < char    *mod, *typ, *id;
828 < register FUNARGS        *fa;
826 > o_trans(        /* convert a trans material */
827 >        char    *mod,
828 >        char    *typ,
829 >        char    *id,
830 >        register FUNARGS        *fa
831 > )
832   {
833          COLOR   cxyz, rrgb;
834          double  rough, trans, tspec, d;
# Line 699 | Line 865 | register FUNARGS       *fa;
865  
866  
867   int
868 < o_light(mod, typ, id, fa)               /* convert a light type */
869 < char    *mod, *typ, *id;
870 < register FUNARGS        *fa;
868 > o_light(                /* convert a light type */
869 >        char    *mod,
870 >        char    *typ,
871 >        char    *id,
872 >        register FUNARGS        *fa
873 > )
874   {
875          COLOR   cxyz, rrgb;
876          double  d;
# Line 715 | Line 884 | register FUNARGS       *fa;
884          puts("\tc");
885          if (d > FTINY)
886                  printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d);
887 <        printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY);
887 >        printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY));
888          return(0);
889   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines