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

Comparing ray/src/cv/ies2rad.c (file contents):
Revision 1.1 by greg, Tue Dec 11 08:45:48 1990 UTC vs.
Revision 2.7 by greg, Mon Apr 5 16:40:07 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1990 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 12 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include <stdio.h>
14   #include <ctype.h>
15 + #include "color.h"
16 + #include "paths.h"
17  
18   #define PI              3.14159265358979323846
19                                          /* floating comparisons */
# Line 34 | Line 36 | static char SCCSid[] = "$SunId$ LBL";
36                                          /* string lengths */
37   #define MAXLINE         132
38   #define MAXWORD         76
37 #define MAXPATH         128
39                                          /* file types */
40   #define T_RAD           ".rad"
41   #define T_DST           ".dat"
# Line 48 | Line 49 | static char SCCSid[] = "$SunId$ LBL";
49  
50   #define F_M             .3048           /* feet to meters */
51  
52 < #define abspath(p)      ((p)[0] == '/' || (p)[0] == '.')
52 > #define abspath(p)      (ISDIRSEP((p)[0]) || (p)[0] == '.')
53  
54 + static char     default_name[] = "default";
55 +
56   char    *libdir = NULL;                 /* library directory location */
57   char    *prefdir = NULL;                /* subdirectory */
58   char    *lampdat = "lamp.tab";          /* lamp data file */
59  
60   double  meters2out = 1.0;               /* conversion from meters to output */
61   char    *lamptype = NULL;               /* selected lamp type */
62 < float   *lampcolor = NULL;              /* pointer to lamp color */
62 > char    *deflamp = NULL;                /* default lamp type */
63   float   defcolor[3] = {1.,1.,1.};       /* default lamp color */
64 + float   *lampcolor = defcolor;          /* pointer to current lamp color */
65   double  multiplier = 1.0;               /* multiplier for all light sources */
66   char    units[64] = "meters";           /* output units */
63 double  minaspect = 0.0;                /* minimum allowed aspect ratio */
64 int     maxemitters = 1;                /* maximum emitters per hemisphere */
67   double  illumrad = 0.0;                 /* radius for illum sphere */
68  
69   typedef struct {
70          int     type;                           /* RECT, DISK, SPHERE */
71          double  w, l, h;                        /* width, length, height */
72 <        double  area;                           /* effective radiating area */
72 >        double  area;                           /* max. projected area */
73   } SHAPE;                                /* a source shape */
74  
75   int     gargc;                          /* global argc (minus filenames) */
76   char    **gargv;                        /* global argv */
77  
78   extern char     *strcpy(), *strcat(), *stradd(), *tailtrunc(), *filetrunc(),
79 <                *filename(), *libname(), *fullname(), *malloc();
80 < extern double   atof();
79 >                *filename(), *libname(), *fullname(), *malloc(),
80 >                *getword(), *atos();
81   extern float    *matchlamp();
82  
83 + #define scnint(fp,ip)   cvtint(ip,getword(fp))
84 + #define scnflt(fp,rp)   cvtflt(rp,getword(fp))
85 + #define isint           isflt                   /* IES allows real as integer */
86  
87 +
88   main(argc, argv)
89   int     argc;
90   char    *argv[];
# Line 153 | Line 159 | char   *argv[];
159                  case 'o':               /* output file name */
160                          outfile = argv[++i];
161                          break;
156                case 's':               /* square emitters */
157                        minaspect = .6;
158                        if (argv[i][2] == '/') {
159                                maxemitters = atoi(argv[i]+3);
160                                if (maxemitters < 1)
161                                        goto badopt;
162                        }
163                        break;
162                  case 'i':               /* illum */
163                          illumrad = atof(argv[++i]);
164                          if (illumrad < MINDIM)
165                                  illumrad = MINDIM;
166                          break;
167 <                case 't':               /* select lamp type */
167 >                case 't':               /* override lamp type */
168                          lamptype = argv[++i];
169                          break;
170 +                case 'u':               /* default lamp type */
171 +                        deflamp = argv[++i];
172 +                        break;
173                  case 'c':               /* default lamp color */
174                          defcolor[0] = atof(argv[++i]);
175                          defcolor[1] = atof(argv[++i]);
# Line 185 | Line 186 | char   *argv[];
186                  }
187          gargc = i;
188          gargv = argv;
189 <                                        /* get lamp data */
189 <        if ((status = loadlamps(lampdat)) < 0)
190 <                exit(1);
191 <        if (status == 0 || (lamptype != NULL &&
192 <                        (lampcolor = matchlamp(lamptype)) == NULL)) {
193 <                lampcolor = defcolor;
194 <                fprintf(stderr, "%s: warning - no lamp data\n", argv[0]);
195 <        }
189 >        initlamps();                    /* get lamp data (if needed) */
190                                          /* convert ies file(s) */
191          if (outfile != NULL) {
192                  if (i == argc)
# Line 219 | Line 213 | char   *argv[];
213   }
214  
215  
216 + initlamps()                             /* set up lamps */
217 + {
218 +        float   *lcol;
219 +        int     status;
220 +
221 +        if (lamptype != NULL && !strcmp(lamptype, default_name) &&
222 +                        deflamp == NULL)
223 +                return;                         /* no need for data */
224 +                                                /* else load file */
225 +        if ((status = loadlamps(lampdat)) < 0)
226 +                exit(1);
227 +        if (status == 0) {
228 +                fprintf(stderr, "%s: warning - no lamp data\n", lampdat);
229 +                lamptype = default_name;
230 +                return;
231 +        }
232 +        if (deflamp != NULL) {                  /* match default type */
233 +                if ((lcol = matchlamp(deflamp)) == NULL)
234 +                        fprintf(stderr,
235 +                                "%s: warning - unknown default lamp type\n",
236 +                                        deflamp);
237 +                else
238 +                        copycolor(defcolor, lcol);
239 +        }
240 +        if (lamptype != NULL) {                 /* match selected type */
241 +                if (strcmp(lamptype, default_name)) {
242 +                        if ((lcol = matchlamp(lamptype)) == NULL) {
243 +                                fprintf(stderr,
244 +                                        "%s: warning - unknown lamp type\n",
245 +                                                lamptype);
246 +                                lamptype = default_name;
247 +                        } else
248 +                                copycolor(defcolor, lcol);
249 +                }
250 +                freelamps();                    /* all done with data */
251 +        }
252 +                                                /* else keep lamp data */
253 + }
254 +
255 +
256   char *
257   stradd(dst, src, sep)                   /* add a string at dst */
258   register char   *dst, *src;
# Line 245 | Line 279 | char   *path, *fname, *suffix;
279          else if (abspath(fname))
280                  strcpy(stradd(path, fname, 0), suffix);
281          else
282 <                libname(stradd(path, libdir, '/'), fname, suffix);
282 >                libname(stradd(path, libdir, DIRSEP), fname, suffix);
283  
284          return(path);
285   }
# Line 258 | Line 292 | char   *path, *fname, *suffix;
292          if (abspath(fname))
293                  strcpy(stradd(path, fname, 0), suffix);
294          else
295 <                strcpy(stradd(stradd(path, prefdir, '/'), fname, 0), suffix);
295 >                strcpy(stradd(stradd(path, prefdir, DIRSEP), fname, 0), suffix);
296  
297          return(path);
298   }
# Line 271 | Line 305 | register char  *path;
305          register char   *cp;
306  
307          for (cp = path; *path; path++)
308 <                if (*path == '/')
308 >                if (ISDIRSEP(*path))
309                          cp = path+1;
310          return(cp);
311   }
# Line 284 | Line 318 | char   *path;
318          register char   *p1, *p2;
319  
320          for (p1 = p2 = path; *p2; p2++)
321 <                if (*p2 == '/')
321 >                if (ISDIRSEP(*p2))
322                          p1 = p2;
323          *p1 = '\0';
324          return(path);
# Line 378 | Line 412 | char   *inpname, *outname;
412                  fprintf(stderr, "%s: not in IES format\n", inpname);
413                  goto readerr;
414          }
415 <        sscanf(buf+TLTSTRLEN, "%s", tltid);
415 >        atos(tltid, MAXWORD, buf+TLTSTRLEN);
416          if (inpfp == stdin)
417                  buf[0] = '\0';
418          else
# Line 418 | Line 452 | char   *dir, *tltspec, *dfltname, *tltid;
452                  datin = in;
453                  strcpy(tltname, dfltname);
454          } else {
455 <                if (tltspec[0] == '/')
455 >                if (ISDIRSEP(tltspec[0]))
456                          strcpy(buf, tltspec);
457                  else
458 <                        strcpy(stradd(buf, dir, '/'), tltspec);
458 >                        strcpy(stradd(buf, dir, DIRSEP), tltspec);
459                  if ((datin = fopen(buf, "r")) == NULL) {
460                          perror(buf);
461                          return(-1);
# Line 435 | Line 469 | char   *dir, *tltspec, *dfltname, *tltid;
469                                  fclose(datin);
470                          return(-1);
471                  }
472 <                if (fscanf(datin, "%d %d", &tlt_type, &nangles) != 2
472 >                if (!scnint(datin,&tlt_type) || !scnint(datin,&nangles)
473                          || cvdata(datin,datout,1,&nangles,1.,minmax) != 0) {
474                          fprintf(stderr, "%s: data format error\n", tltspec);
475                          fclose(datout);
# Line 487 | Line 521 | char   *mod, *name;
521          int     nangles[2], pmtype, unitype;
522          double  d1;
523  
524 <        if (fscanf(in, "%*d %*f %lf %d %d %d %d %lf %lf %lf %lf %lf %lf",
525 <                        &mult, &nangles[0], &nangles[1], &pmtype, &unitype,
526 <                        &width, &length, &height, &bfactor, &pfactor,
527 <                        &wattage) != 11) {
524 >        if (!isint(getword(in)) || !isflt(getword(in)) || !scnflt(in,&mult)
525 >                        || !scnint(in,&nangles[0]) || !scnint(in,&nangles[1])
526 >                        || !scnint(in,&pmtype) || !scnint(in,&unitype)
527 >                        || !scnflt(in,&width) || !scnflt(in,&length)
528 >                        || !scnflt(in,&height) || !scnflt(in,&bfactor)
529 >                        || !scnflt(in,&pfactor) || !scnflt(in,&wattage)) {
530                  fprintf(stderr, "dosource: bad lamp specification\n");
531                  return(-1);
532          }
# Line 511 | Line 547 | char   *mod, *name;
547                  perror(buf);
548                  return(-1);
549          }
550 <        if (cvdata(in, datout, 2, nangles, 1./683., bounds) != 0) {
550 >        if (cvdata(in, datout, 2, nangles, 1./WHTEFFICACY, bounds) != 0) {
551                  fprintf(stderr, "dosource: bad distribution data\n");
552                  fclose(datout);
553                  unlink(fullname(buf,name,T_DST));
# Line 527 | Line 563 | char   *mod, *name;
563          else if (pmtype == PM_B)
564                  fprintf(out, "5 ");
565          else if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.))
566 <                fprintf(out, "8 ");
566 >                fprintf(out, "7 ");
567          else
568 <                fprintf(out, "6 ");
568 >                fprintf(out, "5 ");
569          fprintf(out, "%s %s source.cal ",
570                          srcshape.type==SPHERE ? "corr" : "flatcorr",
571                          libname(buf,name,T_DST));
# Line 548 | Line 584 | char   *mod, *name;
584                                  fprintf(out, "src_phi2 ");
585                          else
586                                  fprintf(out, "src_phi ");
587 <                        fprintf(out, "src_theta -my ");
587 >                        fprintf(out, "src_theta ");
588                          if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.))
589                                  fprintf(out, "-rz -90 ");
590                  } else
# Line 579 | Line 615 | int    dolower, doupper;
615                          lampcolor[2]/shp->area);
616          if (doupper && dolower && shp->type != SPHERE && shp->h > MINDIM) {
617                  fprintf(fp, "\n%s glow %s_glow\n", mod, name);
618 <                fprintf(fp, "0\n0\n4 %g %g %g 0\n",
618 >                fprintf(fp, "0\n0\n4 %g %g %g -1\n",
619                                  lampcolor[0]/shp->area,
620                                  lampcolor[1]/shp->area,
621                                  lampcolor[2]/shp->area);
# Line 656 | Line 692 | double width, length, height;
692                  shp->area = shp->w * shp->l;
693                  break;
694          case DISK:
695 +        case SPHERE:
696                  shp->area = PI/4. * shp->w * shp->w;
697                  break;
661        case SPHERE:
662                shp->area = PI * shp->w * shp->w;
663                break;
698          }
699          return(0);
700   }
# Line 769 | Line 803 | FILE   *in, *out;
803   int     ndim, npts[];
804   double  mult, lim[][2];
805   {
806 <        register double *pt[4];
806 >        double  *pt[4];
807          register int    i, j;
808          double  val;
809          int     total;
# Line 785 | Line 819 | double mult, lim[][2];
819          for (i = 0; i < ndim; i++) {
820                  pt[i] = (double *)malloc(npts[i]*sizeof(double));
821                  for (j = 0; j < npts[i]; j++)
822 <                        fscanf(in, "%lf", &pt[i][j]);
822 >                        if (!scnflt(in, &pt[i][j]))
823 >                                return(-1);
824                  if (lim != NULL) {
825                          lim[i][0] = pt[i][0];
826                          lim[i][1] = pt[i][npts[i]-1];
# Line 816 | Line 851 | double mult, lim[][2];
851          for (i = 0; i < total; i++) {
852                  if (i%4 == 0)
853                          putc('\n', out);
854 <                if (fscanf(in, "%lf", &val) != 1)
854 >                if (!scnflt(in, &val))
855                          return(-1);
856                  fprintf(out, "\t%g", val*mult);
857          }
858          putc('\n', out);
859          return(0);
860 + }
861 +
862 +
863 + char *
864 + getword(fp)                     /* scan a word from fp */
865 + register FILE   *fp;
866 + {
867 +        static char     word[MAXWORD];
868 +        register char   *cp;
869 +        register int    c;
870 +
871 +        while (isspace(c=getc(fp)))
872 +                ;
873 +        for (cp = word; c != EOF && cp < word+MAXWORD-1;
874 +                        *cp++ = c, c = getc(fp))
875 +                if (isspace(c) || c == ',') {
876 +                        while (isspace(c))
877 +                                c = getc(fp);
878 +                        if (c != EOF & c != ',')
879 +                                ungetc(c, fp);
880 +                        *cp = '\0';
881 +                        return(word);
882 +                }
883 +        *cp = '\0';
884 +        return(cp > word ? word : NULL);
885 + }
886 +
887 +
888 + cvtint(ip, word)                /* convert a word to an integer */
889 + int     *ip;
890 + char    *word;
891 + {
892 +        if (word == NULL || !isint(word))
893 +                return(0);
894 +        *ip = atoi(word);
895 +        return(1);
896 + }
897 +
898 +
899 + cvtflt(rp, word)                /* convert a word to a double */
900 + double  *rp;
901 + char    *word;
902 + {
903 +        if (word == NULL || !isflt(word))
904 +                return(0);
905 +        *rp = atof(word);
906 +        return(1);
907   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines