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 2.1 by greg, Tue Nov 12 17:02:02 1991 UTC vs.
Revision 2.25 by greg, Thu Jan 24 23:15:46 2008 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1990 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 IES luminaire data to Radiance description
6   *
7   *      07Apr90         Greg Ward
8 + *
9 + *  Fixed correction factor for flat sources 29Oct2001 GW
10   */
11  
12   #include <stdio.h>
13 + #include <string.h>
14 + #include <math.h>
15 + #include <sys/types.h>
16   #include <ctype.h>
17 +
18 + #include "rtio.h"
19   #include "color.h"
20 + #include "paths.h"
21  
22   #define PI              3.14159265358979323846
23                                          /* floating comparisons */
24   #define FTINY           1e-6
25   #define FEQ(a,b)        ((a)<=(b)+FTINY&&(a)>=(b)-FTINY)
26 +                                        /* keywords */
27 + #define MAGICID         "IESNA"
28 + #define LMAGICID        5
29 + #define FIRSTREV        86
30 + #define LASTREV         95
31 +
32 + #define D86             0               /* keywords defined in LM-63-1986 */
33 +
34 + #define K_TST           0
35 + #define K_MAN           1
36 + #define K_LMC           2
37 + #define K_LMN           3
38 + #define K_LPC           4
39 + #define K_LMP           5
40 + #define K_BAL           6
41 + #define K_MTC           7
42 + #define K_OTH           8
43 + #define K_SCH           9
44 + #define K_MOR           10
45 + #define K_BLK           11
46 + #define K_EBK           12
47 +
48 + #define D91             ((1L<<13)-1)    /* keywords defined in LM-63-1991 */
49 +
50 + #define K_LMG           13
51 +
52 + #define D95             ((1L<<14)-1)    /* keywords defined in LM-63-1995 */
53 +
54 + char    k_kwd[][20] = {"TEST", "MANUFAC", "LUMCAT", "LUMINAIRE", "LAMPCAT",
55 +                        "LAMP", "BALLAST", "MAINTCAT", "OTHER", "SEARCH",
56 +                        "MORE", "BLOCK", "ENDBLOCK", "LUMINOUSGEOMETRY"};
57 +
58 + long k_defined[] = {D86, D86, D86, D86, D86, D91, D91, D91, D91, D95};
59 +
60 + int     filerev = FIRSTREV;
61 +
62 + #define keymatch(i,s)   (k_defined[filerev-FIRSTREV]&1L<<(i) &&\
63 +                                k_match(k_kwd[i],s))
64 +
65 + #define checklamp(s)    (!(k_defined[filerev-FIRSTREV]&(1<<K_LMP|1<<K_LPC)) ||\
66 +                                keymatch(K_LMP,s) || keymatch(K_LPC,s))
67 +
68                                          /* tilt specs */
69   #define TLTSTR          "TILT="
70   #define TLTSTRLEN       5
# Line 29 | Line 76 | static char SCCSid[] = "$SunId$ LBL";
76                                          /* photometric types */
77   #define PM_C            1
78   #define PM_B            2
79 + #define PM_A            3
80                                          /* unit types */
81   #define U_FEET          1
82   #define U_METERS        2
83                                          /* string lengths */
84   #define MAXLINE         132
85 < #define MAXWORD         76
38 < #define MAXPATH         128
85 > #define RMAXWORD        76
86                                          /* file types */
87   #define T_RAD           ".rad"
88   #define T_DST           ".dat"
89 < #define T_TLT           "+.dat"
89 > #define T_TLT           "%.dat"
90 > #define T_OCT           ".oct"
91                                          /* shape types */
92   #define RECT            1
93   #define DISK            2
# Line 49 | Line 97 | static char SCCSid[] = "$SunId$ LBL";
97  
98   #define F_M             .3048           /* feet to meters */
99  
100 < #define abspath(p)      ((p)[0] == '/' || (p)[0] == '.')
100 > #define abspath(p)      (ISDIRSEP((p)[0]) || (p)[0] == '.')
101  
102   static char     default_name[] = "default";
103  
# Line 64 | Line 112 | float  defcolor[3] = {1.,1.,1.};       /* default lamp color
112   float   *lampcolor = defcolor;          /* pointer to current lamp color */
113   double  multiplier = 1.0;               /* multiplier for all light sources */
114   char    units[64] = "meters";           /* output units */
115 < double  minaspect = 0.0;                /* minimum allowed aspect ratio */
116 < int     maxemitters = 1;                /* maximum emitters per hemisphere */
115 > int     out2stdout = 0;                 /* put out to stdout r.t. file */
116 > int     instantiate = 0;                /* instantiate geometry */
117   double  illumrad = 0.0;                 /* radius for illum sphere */
118  
119   typedef struct {
120 +        int     isillum;                        /* do as illum */
121          int     type;                           /* RECT, DISK, SPHERE */
122 +        double  mult;                           /* candela multiplier */
123          double  w, l, h;                        /* width, length, height */
124          double  area;                           /* max. projected area */
125 < } SHAPE;                                /* a source shape */
125 > } SRCINFO;                              /* a source shape (units=meters) */
126  
127   int     gargc;                          /* global argc (minus filenames) */
128   char    **gargv;                        /* global argv */
129  
80 extern char     *strcpy(), *strcat(), *stradd(), *tailtrunc(), *filetrunc(),
81                *filename(), *libname(), *fullname(), *malloc();
82 extern double   atof();
83 extern float    *matchlamp();
130  
131 + #define scnint(fp,ip)   cvtint(ip,getword(fp))
132 + #define scnflt(fp,rp)   cvtflt(rp,getword(fp))
133 + #define isint           isflt                   /* IES allows real as integer */
134  
135 < main(argc, argv)
136 < int     argc;
137 < char    *argv[];
135 >
136 > static int ies2rad(char *inpname, char *outname);
137 > static void initlamps(void);
138 > static int dosource(SRCINFO *sinf, FILE *in, FILE *out, char *mod, char *name);
139 > static int dotilt(FILE *in, FILE *out, char *dir, char *tltspec,
140 >                char *dfltname, char *tltid);
141 > static int cvgeometry(char *inpname, SRCINFO *sinf, char *outname, FILE *outfp);
142 > static int cvtint(int *ip, char *wrd);
143 > static int cvdata(FILE *in, FILE *out, int ndim, int npts[], double mult,
144 >                double lim[][2]);
145 > static int cvtflt(double *rp, char *wrd);
146 > static int makeshape(SRCINFO *shp, double width, double length, double height);
147 > static int putsource(SRCINFO *shp, FILE *fp, char *mod, char *name,
148 >                int dolower, int doupper, int dosides);
149 > static void putrectsrc(SRCINFO *shp, FILE *fp, char *mod, char *name, int up);
150 > static void putsides(SRCINFO *shp, FILE *fp, char *mod, char *name);
151 > static void putdisksrc(SRCINFO *shp, FILE *fp, char *mod, char *name, int up);
152 > static void putspheresrc(SRCINFO *shp, FILE *fp, char *mod, char *name);
153 > static void putrect(SRCINFO *shp, FILE *fp, char *mod, char *name, char *suffix,
154 >                int a, int b, int c, int d);
155 > static void putpoint(SRCINFO *shp, FILE *fp, int p);
156 > static void putcyl(SRCINFO *shp, FILE *fp, char *mod, char *name);
157 > static char * tailtrunc(char *name);
158 > static char * filename(char *path);
159 > static char * libname(char *path, char *fname, char *suffix);
160 > static char * getword(FILE *fp);
161 > static char * fullnam(char *path, char *fname, char *suffix);
162 >
163 >
164 > int
165 > main(
166 >        int     argc,
167 >        char    *argv[]
168 > )
169   {
170          char    *outfile = NULL;
171          int     status;
172 <        char    outname[MAXWORD];
172 >        char    outname[RMAXWORD];
173          double  d1;
174          int     i;
175          
# Line 154 | Line 234 | char   *argv[];
234                  case 'f':               /* lamp data file */
235                          lampdat = argv[++i];
236                          break;
237 <                case 'o':               /* output file name */
237 >                case 'o':               /* output file root name */
238                          outfile = argv[++i];
239                          break;
240 <                case 's':               /* square emitters */
241 <                        minaspect = .6;
162 <                        if (argv[i][2] == '/') {
163 <                                maxemitters = atoi(argv[i]+3);
164 <                                if (maxemitters < 1)
165 <                                        goto badopt;
166 <                        }
240 >                case 's':               /* output to stdout */
241 >                        out2stdout = !out2stdout;
242                          break;
243                  case 'i':               /* illum */
244                          illumrad = atof(argv[++i]);
170                        if (illumrad < MINDIM)
171                                illumrad = MINDIM;
245                          break;
246 +                case 'g':               /* instatiate geometry? */
247 +                        instantiate = !instantiate;
248 +                        break;
249                  case 't':               /* override lamp type */
250                          lamptype = argv[++i];
251                          break;
# Line 199 | Line 275 | char   *argv[];
275                          exit(ies2rad(NULL, outfile) == 0 ? 0 : 1);
276                  else if (i == argc-1)
277                          exit(ies2rad(argv[i], outfile) == 0 ? 0 : 1);
278 <                else {
279 <                        fprintf(stderr, "%s: single input file required\n",
204 <                                        argv[0]);
205 <                        exit(1);
206 <                }
278 >                else
279 >                        goto needsingle;
280          } else if (i >= argc) {
281                  fprintf(stderr, "%s: missing output file specification\n",
282                                  argv[0]);
283                  exit(1);
284          }
285 +        if (out2stdout && i != argc-1)
286 +                goto needsingle;
287          status = 0;
288          for ( ; i < argc; i++) {
289                  tailtrunc(strcpy(outname,filename(argv[i])));
# Line 216 | Line 291 | char   *argv[];
291                          status = 1;
292          }
293          exit(status);
294 + needsingle:
295 +        fprintf(stderr, "%s: single input file required\n", argv[0]);
296 +        exit(1);
297   }
298  
299 <
300 < initlamps()                             /* set up lamps */
299 > void
300 > initlamps(void)                         /* set up lamps */
301   {
302          float   *lcol;
303          int     status;
# Line 260 | Line 338 | initlamps()                            /* set up lamps */
338  
339  
340   char *
341 < stradd(dst, src, sep)                   /* add a string at dst */
342 < register char   *dst, *src;
343 < int     sep;
341 > stradd(                 /* add a string at dst */
342 >        register char   *dst,
343 >        register char   *src,
344 >        int     sep
345 > )
346   {
347          if (src && *src) {
348                  do
# Line 277 | Line 357 | int    sep;
357  
358  
359   char *
360 < fullname(path, fname, suffix)           /* return full path name */
361 < char    *path, *fname, *suffix;
360 > fullnam(                /* return full path name */
361 >        char    *path,
362 >        char    *fname,
363 >        char    *suffix
364 > )
365   {
366          if (prefdir != NULL && abspath(prefdir))
367                  libname(path, fname, suffix);
368          else if (abspath(fname))
369                  strcpy(stradd(path, fname, 0), suffix);
370          else
371 <                libname(stradd(path, libdir, '/'), fname, suffix);
371 >                libname(stradd(path, libdir, DIRSEP), fname, suffix);
372  
373          return(path);
374   }
375  
376  
377   char *
378 < libname(path, fname, suffix)            /* return library relative name */
379 < char    *path, *fname, *suffix;
378 > libname(                /* return library relative name */
379 >        char    *path,
380 >        char    *fname,
381 >        char    *suffix
382 > )
383   {
384          if (abspath(fname))
385                  strcpy(stradd(path, fname, 0), suffix);
386          else
387 <                strcpy(stradd(stradd(path, prefdir, '/'), fname, 0), suffix);
387 >                strcpy(stradd(stradd(path, prefdir, DIRSEP), fname, 0), suffix);
388  
389          return(path);
390   }
391  
392  
393   char *
394 < filename(path)                  /* get final component of pathname */
395 < register char   *path;
394 > filename(                       /* get final component of pathname */
395 >        register char   *path
396 > )
397   {
398          register char   *cp;
399  
400          for (cp = path; *path; path++)
401 <                if (*path == '/')
401 >                if (ISDIRSEP(*path))
402                          cp = path+1;
403          return(cp);
404   }
405  
406  
407   char *
408 < filetrunc(path)                         /* truncate filename at end of path */
409 < char    *path;
408 > filetrunc(                              /* truncate filename at end of path */
409 >        char    *path
410 > )
411   {
412          register char   *p1, *p2;
413  
414          for (p1 = p2 = path; *p2; p2++)
415 <                if (*p2 == '/')
415 >                if (ISDIRSEP(*p2))
416                          p1 = p2;
417 +        if (p1 == path && ISDIRSEP(*p1))
418 +                p1++;
419          *p1 = '\0';
420          return(path);
421   }
422  
423  
424   char *
425 < tailtrunc(name)                         /* truncate tail of filename */
426 < char    *name;
425 > tailtrunc(                              /* truncate tail of filename */
426 >        char    *name
427 > )
428   {
429          register char   *p1, *p2;
430  
# Line 349 | Line 440 | char   *name;
440   }
441  
442  
443 < blanktrunc(s)                           /* truncate spaces at end of line */
444 < char    *s;
443 > void
444 > blanktrunc(                             /* truncate spaces at end of line */
445 >        char    *s
446 > )
447   {
448          register char   *cp;
449  
# Line 362 | Line 455 | char   *s;
455   }
456  
457  
458 < putheader(out)                          /* print header */
459 < FILE    *out;
458 > int
459 > k_match(                        /* header line matches keyword? */
460 >        register char   *kwd,
461 >        register char   *hdl
462 > )
463   {
464 +        if (!*hdl++ == '[')
465 +                return(0);
466 +        while (islower(*hdl) ? toupper(*hdl) == *kwd++ : *hdl == *kwd++)
467 +                if (!*hdl++)
468 +                        return(0);
469 +        return((!*kwd) & (*hdl == ']'));
470 + }
471 +
472 +
473 + char *
474 + keyargs(                                /* return keyword arguments */
475 +        register char   *hdl
476 + )
477 + {
478 +        while (*hdl && *hdl++ != ']')
479 +                ;
480 +        while (isspace(*hdl))
481 +                hdl++;
482 +        return(hdl);
483 + }
484 +
485 +
486 + void
487 + putheader(                              /* print header */
488 +        FILE    *out
489 + )
490 + {
491          register int    i;
492          
493          putc('#', out);
# Line 378 | Line 501 | FILE   *out;
501   }
502  
503  
504 < ies2rad(inpname, outname)               /* convert IES file */
505 < char    *inpname, *outname;
504 > int
505 > ies2rad(                /* convert IES file */
506 >        char    *inpname,
507 >        char    *outname
508 > )
509   {
510 <        char    buf[MAXLINE], tltid[MAXWORD];
510 >        SRCINFO srcinfo;
511 >        char    buf[MAXLINE], tltid[RMAXWORD];
512 >        char    geomfile[128];
513          FILE    *inpfp, *outfp;
514 +        int     lineno = 0;
515  
516 +        geomfile[0] = '\0';
517 +        srcinfo.isillum = 0;
518          if (inpname == NULL) {
519                  inpname = "<stdin>";
520                  inpfp = stdin;
# Line 391 | Line 522 | char   *inpname, *outname;
522                  perror(inpname);
523                  return(-1);
524          }
525 <        if ((outfp = fopen(fullname(buf,outname,T_RAD), "w")) == NULL) {
525 >        if (out2stdout)
526 >                outfp = stdout;
527 >        else if ((outfp = fopen(fullnam(buf,outname,T_RAD), "w")) == NULL) {
528                  perror(buf);
529                  fclose(inpfp);
530                  return(-1);
# Line 404 | Line 537 | char   *inpname, *outname;
537                  blanktrunc(buf);
538                  if (!buf[0])
539                          continue;
540 +                if (!lineno++ && !strncmp(buf, MAGICID, LMAGICID)) {
541 +                        filerev = atoi(buf+LMAGICID);
542 +                        if (filerev < FIRSTREV)
543 +                                filerev = FIRSTREV;
544 +                        else if (filerev > LASTREV)
545 +                                filerev = LASTREV;
546 +                }
547                  fputs("#<", outfp);
548                  fputs(buf, outfp);
549                  putc('\n', outfp);
550 <                if (lampcolor == NULL)
551 <                        lampcolor = matchlamp(buf);
550 >                if (lampcolor == NULL && checklamp(buf))
551 >                        lampcolor = matchlamp( buf[0] == '[' ?
552 >                                                keyargs(buf) : buf );
553 >                if (keymatch(K_LMG, buf)) {             /* geometry file */
554 >                        strcpy(geomfile, inpname);
555 >                        strcpy(filename(geomfile), keyargs(buf));
556 >                        srcinfo.isillum = 1;
557 >                }
558          }
559          if (lampcolor == NULL) {
560                  fprintf(stderr, "%s: warning - no lamp type\n", inpname);
561 +                fputs("# Unknown lamp type (used default)\n", outfp);
562                  lampcolor = defcolor;
563 <        }
563 >        } else if (lamptype == NULL)
564 >                fprintf(outfp,"# CIE(x,y) = (%f,%f)\n# Depreciation = %.1f%%\n",
565 >                                lampcolor[3], lampcolor[4], 100.*lampcolor[5]);
566          if (feof(inpfp)) {
567                  fprintf(stderr, "%s: not in IES format\n", inpname);
568                  goto readerr;
569          }
570 <        sscanf(buf+TLTSTRLEN, "%s", tltid);
570 >        atos(tltid, RMAXWORD, buf+TLTSTRLEN);
571          if (inpfp == stdin)
572                  buf[0] = '\0';
573          else
# Line 427 | Line 576 | char   *inpname, *outname;
576                  fprintf(stderr, "%s: bad tilt data\n", inpname);
577                  goto readerr;
578          }
579 <        if (dosource(inpfp, outfp, tltid, outname) != 0) {
579 >        if (dosource(&srcinfo, inpfp, outfp, tltid, outname) != 0) {
580                  fprintf(stderr, "%s: bad luminaire data\n", inpname);
581                  goto readerr;
582          }
434        fclose(outfp);
583          fclose(inpfp);
584 +                                        /* cvgeometry closes outfp */
585 +        if (cvgeometry(geomfile, &srcinfo, outname, outfp) != 0) {
586 +                fprintf(stderr, "%s: bad geometry file\n", geomfile);
587 +                return(-1);
588 +        }
589          return(0);
590   readerr:
438        fclose(outfp);
591          fclose(inpfp);
592 <        unlink(fullname(buf,outname,T_RAD));
592 >        fclose(outfp);
593 >        unlink(fullnam(buf,outname,T_RAD));
594          return(-1);
595   }
596  
597  
598 < dotilt(in, out, dir, tltspec, dfltname, tltid)  /* convert tilt data */
599 < FILE    *in, *out;
600 < char    *dir, *tltspec, *dfltname, *tltid;
598 > int
599 > dotilt( /* convert tilt data */
600 >        FILE    *in,
601 >        FILE    *out,
602 >        char    *dir,
603 >        char    *tltspec,
604 >        char    *dfltname,
605 >        char    *tltid
606 > )
607   {
608          int     nangles, tlt_type;
609 <        double  minmax[2];
610 <        char    buf[MAXPATH], tltname[MAXWORD];
609 >        double  minmax[1][2];
610 >        char    buf[PATH_MAX], tltname[RMAXWORD];
611          FILE    *datin, *datout;
612  
613          if (!strcmp(tltspec, TLTNONE)) {
# Line 458 | Line 617 | char   *dir, *tltspec, *dfltname, *tltid;
617                  datin = in;
618                  strcpy(tltname, dfltname);
619          } else {
620 <                if (tltspec[0] == '/')
620 >                if (ISDIRSEP(tltspec[0]))
621                          strcpy(buf, tltspec);
622                  else
623 <                        strcpy(stradd(buf, dir, '/'), tltspec);
623 >                        strcpy(stradd(buf, dir, DIRSEP), tltspec);
624                  if ((datin = fopen(buf, "r")) == NULL) {
625                          perror(buf);
626                          return(-1);
# Line 469 | Line 628 | char   *dir, *tltspec, *dfltname, *tltid;
628                  tailtrunc(strcpy(tltname,filename(tltspec)));
629          }
630          if (datin != NULL) {
631 <                if ((datout = fopen(fullname(buf,tltname,T_TLT),"w")) == NULL) {
631 >                if ((datout = fopen(fullnam(buf,tltname,T_TLT),"w")) == NULL) {
632                          perror(buf);
633                          if (datin != in)
634                                  fclose(datin);
635                          return(-1);
636                  }
637 <                if (fscanf(datin, "%d %d", &tlt_type, &nangles) != 2
637 >                if (!scnint(datin,&tlt_type) || !scnint(datin,&nangles)
638                          || cvdata(datin,datout,1,&nangles,1.,minmax) != 0) {
639                          fprintf(stderr, "%s: data format error\n", tltspec);
640                          fclose(datout);
641                          if (datin != in)
642                                  fclose(datin);
643 <                        unlink(fullname(buf,tltname,T_TLT));
643 >                        unlink(fullnam(buf,tltname,T_TLT));
644                          return(-1);
645                  }
646                  fclose(datout);
# Line 493 | Line 652 | char   *dir, *tltspec, *dfltname, *tltid;
652                  switch (tlt_type) {
653                  case TLT_VERT:                  /* vertical */
654                          fprintf(out, "4 noop %s tilt.cal %s\n", buf,
655 <                                minmax[1]>90.+FTINY ? "tilt_ang" : "tilt_ang2");
655 >                                minmax[0][1]>90.+FTINY ? "tilt_ang" : "tilt_ang2");
656                          break;
657                  case TLT_H0:                    /* horiz. in 0 deg. plane */
658                          fprintf(out, "6 noop %s tilt.cal %s -rz 90\n", buf,
659 <                        minmax[1]>90.+FTINY ? "tilt_xang" : "tilt_xang2");
659 >                        minmax[0][1]>90.+FTINY ? "tilt_xang" : "tilt_xang2");
660                          break;
661                  case TLT_H90:
662                          fprintf(out, "4 noop %s tilt.cal %s\n", buf,
663 <                        minmax[1]>90.+FTINY ? "tilt_xang" : "tilt_xang2");
663 >                        minmax[0][1]>90.+FTINY ? "tilt_xang" : "tilt_xang2");
664                          break;
665                  default:
666                          fprintf(stderr,
# Line 515 | Line 674 | char   *dir, *tltspec, *dfltname, *tltid;
674   }
675  
676  
677 < dosource(in, out, mod, name)            /* create source and distribution */
678 < FILE    *in, *out;
679 < char    *mod, *name;
677 > int
678 > dosource(       /* create source and distribution */
679 >        SRCINFO *sinf,
680 >        FILE    *in,
681 >        FILE    *out,
682 >        char    *mod,
683 >        char    *name
684 > )
685   {
686 <        SHAPE   srcshape;
523 <        char    buf[MAXPATH], id[MAXWORD];
686 >        char    buf[PATH_MAX], id[RMAXWORD];
687          FILE    *datout;
688          double  mult, bfactor, pfactor, width, length, height, wattage;
689          double  bounds[2][2];
690          int     nangles[2], pmtype, unitype;
691          double  d1;
692 +        int     doupper, dolower, dosides;
693  
694 <        if (fscanf(in, "%*d %*f %lf %d %d %d %d %lf %lf %lf %lf %lf %lf",
695 <                        &mult, &nangles[0], &nangles[1], &pmtype, &unitype,
696 <                        &width, &length, &height, &bfactor, &pfactor,
697 <                        &wattage) != 11) {
694 >        if (!isint(getword(in)) || !isflt(getword(in)) || !scnflt(in,&mult)
695 >                        || !scnint(in,&nangles[0]) || !scnint(in,&nangles[1])
696 >                        || !scnint(in,&pmtype) || !scnint(in,&unitype)
697 >                        || !scnflt(in,&width) || !scnflt(in,&length)
698 >                        || !scnflt(in,&height) || !scnflt(in,&bfactor)
699 >                        || !scnflt(in,&pfactor) || !scnflt(in,&wattage)) {
700                  fprintf(stderr, "dosource: bad lamp specification\n");
701                  return(-1);
702          }
703 +        if (pmtype != PM_C && pmtype != PM_B) {
704 +                fprintf(stderr, "dosource: unsupported photometric type (%d)\n",
705 +                                pmtype);
706 +                return(-1);
707 +        }
708 +        sinf->mult = multiplier*mult*bfactor*pfactor;
709          if (nangles[0] < 2 || nangles[1] < 1) {
710                  fprintf(stderr, "dosource: too few measured angles\n");
711                  return(-1);
# Line 543 | Line 715 | char   *mod, *name;
715                  length *= F_M;
716                  height *= F_M;
717          }
718 <        if (makeshape(&srcshape, width, length, height) != 0) {
718 >        if (makeshape(sinf, width, length, height) != 0) {
719                  fprintf(stderr, "dosource: illegal source dimensions");
720                  return(-1);
721          }
722 <        if ((datout = fopen(fullname(buf,name,T_DST), "w")) == NULL) {
722 >        if ((datout = fopen(fullnam(buf,name,T_DST), "w")) == NULL) {
723                  perror(buf);
724                  return(-1);
725          }
726          if (cvdata(in, datout, 2, nangles, 1./WHTEFFICACY, bounds) != 0) {
727                  fprintf(stderr, "dosource: bad distribution data\n");
728                  fclose(datout);
729 <                unlink(fullname(buf,name,T_DST));
729 >                unlink(fullnam(buf,name,T_DST));
730                  return(-1);
731          }
732          fclose(datout);
# Line 567 | Line 739 | char   *mod, *name;
739          else if (pmtype == PM_B)
740                  fprintf(out, "5 ");
741          else if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.))
742 <                fprintf(out, "8 ");
742 >                fprintf(out, "7 ");
743          else
744 <                fprintf(out, "6 ");
744 >                fprintf(out, "5 ");
745 >        dolower = (bounds[0][0] < 90.-FTINY);
746 >        doupper = (bounds[0][1] > 90.+FTINY);
747 >        dosides = (doupper & dolower && sinf->h > MINDIM);
748          fprintf(out, "%s %s source.cal ",
749 <                        srcshape.type==SPHERE ? "corr" : "flatcorr",
749 >                        sinf->type==SPHERE ? "corr" :
750 >                        !dosides ? "flatcorr" :
751 >                        sinf->type==DISK ? "cylcorr" : "boxcorr",
752                          libname(buf,name,T_DST));
753          if (pmtype == PM_B) {
754                  if (FEQ(bounds[1][0],0.))
# Line 579 | Line 756 | char   *mod, *name;
756                  else
757                          fprintf(out, "srcB_horiz ");
758                  fprintf(out, "srcB_vert ");
759 <        } else {
759 >        } else /* pmtype == PM_C */ {
760                  if (nangles[1] >= 2) {
761                          d1 = bounds[1][1] - bounds[1][0];
762                          if (d1 <= 90.+FTINY)
763                                  fprintf(out, "src_phi4 ");
764 <                        else if (d1 <= 180.+FTINY)
765 <                                fprintf(out, "src_phi2 ");
766 <                        else
764 >                        else if (d1 <= 180.+FTINY) {
765 >                                if (FEQ(bounds[1][0],90.))
766 >                                        fprintf(out, "src_phi2+90 ");
767 >                                else
768 >                                        fprintf(out, "src_phi2 ");
769 >                        } else
770                                  fprintf(out, "src_phi ");
771 <                        fprintf(out, "src_theta -my ");
771 >                        fprintf(out, "src_theta ");
772                          if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.))
773                                  fprintf(out, "-rz -90 ");
774                  } else
775                          fprintf(out, "src_theta ");
776          }
777 <        fprintf(out, "\n0\n1 %g\n", multiplier*mult*bfactor*pfactor);
778 <        if (putsource(&srcshape, out, id, filename(name),
779 <                        bounds[0][0]<90., bounds[0][1]>90.) != 0)
777 >        if (!dosides || sinf->type == SPHERE)
778 >                fprintf(out, "\n0\n1 %g\n", sinf->mult/sinf->area);
779 >        else if (sinf->type == DISK)
780 >                fprintf(out, "\n0\n3 %g %g %g\n", sinf->mult,
781 >                                sinf->w, sinf->h);
782 >        else
783 >                fprintf(out, "\n0\n4 %g %g %g %g\n", sinf->mult,
784 >                                sinf->l, sinf->w, sinf->h);
785 >        if (putsource(sinf, out, id, filename(name),
786 >                        dolower, doupper, dosides) != 0)
787                  return(-1);
788          return(0);
789   }
790  
791  
792 < putsource(shp, fp, mod, name, dolower, doupper)         /* put out source */
793 < SHAPE   *shp;
794 < FILE    *fp;
795 < char    *mod, *name;
796 < int     dolower, doupper;
792 > int
793 > putsource( /* put out source */
794 >        SRCINFO *shp,
795 >        FILE    *fp,
796 >        char    *mod,
797 >        char    *name,
798 >        int     dolower,
799 >        int     doupper,
800 >        int dosides
801 > )
802   {
803 <        char    buf[MAXWORD];
803 >        char    lname[RMAXWORD];
804          
805 <        fprintf(fp, "\n%s %s %s_light\n", mod,
806 <                        illumrad>=MINDIM/2. ? "illum" : "light",
807 <                        name);
805 >        strcat(strcpy(lname, name), "_light");
806 >        fprintf(fp, "\n%s %s %s\n", mod,
807 >                        shp->isillum ? "illum" : "light", lname);
808          fprintf(fp, "0\n0\n3 %g %g %g\n",
809 <                        lampcolor[0]/shp->area,
618 <                        lampcolor[1]/shp->area,
619 <                        lampcolor[2]/shp->area);
620 <        if (doupper && dolower && shp->type != SPHERE && shp->h > MINDIM) {
621 <                fprintf(fp, "\n%s glow %s_glow\n", mod, name);
622 <                fprintf(fp, "0\n0\n4 %g %g %g 0\n",
623 <                                lampcolor[0]/shp->area,
624 <                                lampcolor[1]/shp->area,
625 <                                lampcolor[2]/shp->area);
626 <        }
809 >                        lampcolor[0], lampcolor[1], lampcolor[2]);
810          switch (shp->type) {
811          case RECT:
629                strcat(strcpy(buf, name), "_light");
812                  if (dolower)
813 <                        putrectsrc(shp, fp, buf, name, 0);
813 >                        putrectsrc(shp, fp, lname, name, 0);
814                  if (doupper)
815 <                        putrectsrc(shp, fp, buf, name, 1);
816 <                if (doupper && dolower && shp->h > MINDIM) {
817 <                        strcat(strcpy(buf, name), "_glow");
636 <                        putsides(shp, fp, buf, name);
637 <                }
815 >                        putrectsrc(shp, fp, lname, name, 1);
816 >                if (dosides)
817 >                        putsides(shp, fp, lname, name);
818                  break;
819          case DISK:
640                strcat(strcpy(buf, name), "_light");
820                  if (dolower)
821 <                        putdisksrc(shp, fp, buf, name, 0);
821 >                        putdisksrc(shp, fp, lname, name, 0);
822                  if (doupper)
823 <                        putdisksrc(shp, fp, buf, name, 1);
824 <                if (doupper && dolower && shp->h > MINDIM) {
825 <                        strcat(strcpy(buf, name), "_glow");
647 <                        putcyl(shp, fp, buf, name);
648 <                }
823 >                        putdisksrc(shp, fp, lname, name, 1);
824 >                if (dosides)
825 >                        putcyl(shp, fp, lname, name);
826                  break;
827          case SPHERE:
828 <                strcat(strcpy(buf, name), "_light");
652 <                putspheresrc(shp, fp, buf, name);
828 >                putspheresrc(shp, fp, lname, name);
829                  break;
830          }
831          return(0);
832   }
833  
834  
835 < makeshape(shp, width, length, height)           /* make source shape */
836 < register SHAPE  *shp;
837 < double  width, length, height;
835 > int
836 > makeshape(              /* make source shape */
837 >        register SRCINFO        *shp,
838 >        double  width,
839 >        double  length,
840 >        double  height
841 > )
842   {
843 <        if (illumrad >= MINDIM/2.) {
843 >        if (illumrad/meters2out >= MINDIM/2.) {
844 >                shp->isillum = 1;
845                  shp->type = SPHERE;
846 <                shp->w = shp->l = shp->h = 2.*illumrad;
846 >                shp->w = shp->l = shp->h = 2.*illumrad / meters2out;
847          } else if (width < MINDIM) {
848                  width = -width;
849                  if (width < MINDIM) {
# Line 704 | Line 885 | double width, length, height;
885   }
886  
887  
888 < putrectsrc(shp, fp, mod, name, up)              /* rectangular source */
889 < SHAPE   *shp;
890 < FILE    *fp;
891 < char    *mod, *name;
892 < int     up;
888 > void
889 > putrectsrc(             /* rectangular source */
890 >        SRCINFO *shp,
891 >        FILE    *fp,
892 >        char    *mod,
893 >        char    *name,
894 >        int     up
895 > )
896   {
897          if (up)
898                  putrect(shp, fp, mod, name, ".u", 4, 5, 7, 6);
# Line 717 | Line 901 | int    up;
901   }
902  
903  
904 < putsides(shp, fp, mod, name)                    /* put out sides of box */
905 < register SHAPE  *shp;
906 < FILE    *fp;
907 < char    *mod, *name;
904 > void
905 > putsides(                       /* put out sides of box */
906 >        register SRCINFO        *shp,
907 >        FILE    *fp,
908 >        char    *mod,
909 >        char    *name
910 > )
911   {
912          putrect(shp, fp, mod, name, ".1", 0, 1, 5, 4);
913          putrect(shp, fp, mod, name, ".2", 1, 3, 7, 5);
# Line 729 | Line 916 | char   *mod, *name;
916   }
917          
918  
919 < putrect(shp, fp, mod, name, suffix, a, b, c, d) /* put out a rectangle */
920 < SHAPE   *shp;
921 < FILE    *fp;
922 < char    *mod, *name, *suffix;
923 < int     a, b, c, d;
919 > void
920 > putrect(        /* put out a rectangle */
921 >        SRCINFO *shp,
922 >        FILE    *fp,
923 >        char    *mod,
924 >        char    *name,
925 >        char    *suffix,
926 >        int     a,
927 >        int b,
928 >        int c,
929 >        int d
930 > )
931   {
932          fprintf(fp, "\n%s polygon %s%s\n0\n0\n12\n", mod, name, suffix);
933          putpoint(shp, fp, a);
# Line 743 | Line 937 | int    a, b, c, d;
937   }
938  
939  
940 < putpoint(shp, fp, p)                            /* put out a point */
941 < register SHAPE  *shp;
942 < FILE    *fp;
943 < int     p;
940 > void
941 > putpoint(                               /* put out a point */
942 >        register SRCINFO        *shp,
943 >        FILE    *fp,
944 >        int     p
945 > )
946   {
947          static double   mult[2] = {-.5, .5};
948  
# Line 757 | Line 953 | int    p;
953   }
954  
955  
956 < putdisksrc(shp, fp, mod, name, up)              /* put out a disk source */
957 < register SHAPE  *shp;
958 < FILE    *fp;
959 < char    *mod, *name;
960 < int     up;
956 > void
957 > putdisksrc(             /* put out a disk source */
958 >        register SRCINFO        *shp,
959 >        FILE    *fp,
960 >        char    *mod,
961 >        char    *name,
962 >        int     up
963 > )
964   {
965          if (up) {
966                  fprintf(fp, "\n%s ring %s.u\n", mod, name);
# Line 779 | Line 978 | int    up;
978   }
979  
980  
981 < putcyl(shp, fp, mod, name)                      /* put out a cylinder */
982 < register SHAPE  *shp;
983 < FILE    *fp;
984 < char    *mod, *name;
981 > void
982 > putcyl(                 /* put out a cylinder */
983 >        register SRCINFO        *shp,
984 >        FILE    *fp,
985 >        char    *mod,
986 >        char    *name
987 > )
988   {
989          fprintf(fp, "\n%s cylinder %s.c\n", mod, name);
990          fprintf(fp, "0\n0\n7\n");
# Line 792 | Line 994 | char   *mod, *name;
994   }
995  
996  
997 < putspheresrc(shp, fp, mod, name)                /* put out a sphere source */
998 < SHAPE   *shp;
999 < FILE    *fp;
1000 < char    *mod, *name;
997 > void
998 > putspheresrc(           /* put out a sphere source */
999 >        SRCINFO *shp,
1000 >        FILE    *fp,
1001 >        char    *mod,
1002 >        char    *name
1003 > )
1004   {
1005          fprintf(fp, "\n%s sphere %s.s\n", mod, name);
1006          fprintf(fp, "0\n0\n4 0 0 0 %g\n", .5*shp->w*meters2out);
1007   }
1008  
1009  
1010 < cvdata(in, out, ndim, npts, mult, lim)          /* convert data */
1011 < FILE    *in, *out;
1012 < int     ndim, npts[];
1013 < double  mult, lim[][2];
1010 > int
1011 > cvdata(         /* convert data */
1012 >        FILE    *in,
1013 >        FILE    *out,
1014 >        int     ndim,
1015 >        int     npts[],
1016 >        double  mult,
1017 >        double  lim[][2]
1018 > )
1019   {
1020 <        register double *pt[4];
1020 >        double  *pt[4];
1021          register int    i, j;
1022          double  val;
1023          int     total;
# Line 823 | Line 1033 | double mult, lim[][2];
1033          for (i = 0; i < ndim; i++) {
1034                  pt[i] = (double *)malloc(npts[i]*sizeof(double));
1035                  for (j = 0; j < npts[i]; j++)
1036 <                        fscanf(in, "%lf", &pt[i][j]);
1036 >                        if (!scnflt(in, &pt[i][j]))
1037 >                                return(-1);
1038                  if (lim != NULL) {
1039                          lim[i][0] = pt[i][0];
1040                          lim[i][1] = pt[i][npts[i]-1];
# Line 849 | Line 1060 | double mult, lim[][2];
1060                                  putc('\n', out);
1061                          }
1062                  }
1063 <                free((char *)pt[i]);
1063 >                free((void *)pt[i]);
1064          }
1065          for (i = 0; i < total; i++) {
1066                  if (i%4 == 0)
1067                          putc('\n', out);
1068 <                if (fscanf(in, "%lf", &val) != 1)
1068 >                if (!scnflt(in, &val))
1069                          return(-1);
1070                  fprintf(out, "\t%g", val*mult);
1071          }
1072          putc('\n', out);
1073 +        return(0);
1074 + }
1075 +
1076 +
1077 + char *
1078 + getword(                        /* scan a word from fp */
1079 +        register FILE   *fp
1080 + )
1081 + {
1082 +        static char     wrd[RMAXWORD];
1083 +        register char   *cp;
1084 +        register int    c;
1085 +
1086 +        while (isspace(c=getc(fp)))
1087 +                ;
1088 +        for (cp = wrd; c != EOF && cp < wrd+RMAXWORD-1;
1089 +                        *cp++ = c, c = getc(fp))
1090 +                if (isspace(c) || c == ',') {
1091 +                        while (isspace(c))
1092 +                                c = getc(fp);
1093 +                        if ((c != EOF) & (c != ','))
1094 +                                ungetc(c, fp);
1095 +                        *cp = '\0';
1096 +                        return(wrd);
1097 +                }
1098 +        *cp = '\0';
1099 +        return(cp > wrd ? wrd : NULL);
1100 + }
1101 +
1102 +
1103 + int
1104 + cvtint(                 /* convert a word to an integer */
1105 +        int     *ip,
1106 +        char    *wrd
1107 + )
1108 + {
1109 +        if (wrd == NULL || !isint(wrd))
1110 +                return(0);
1111 +        *ip = atoi(wrd);
1112 +        return(1);
1113 + }
1114 +
1115 +
1116 + int
1117 + cvtflt(                 /* convert a word to a double */
1118 +        double  *rp,
1119 +        char    *wrd
1120 + )
1121 + {
1122 +        if (wrd == NULL || !isflt(wrd))
1123 +                return(0);
1124 +        *rp = atof(wrd);
1125 +        return(1);
1126 + }
1127 +
1128 +
1129 + int
1130 + cvgeometry(
1131 +        char    *inpname,
1132 +        register SRCINFO        *sinf,
1133 +        char    *outname,
1134 +        FILE    *outfp                  /* close output file upon return */
1135 + )
1136 + {
1137 +        char    buf[256];
1138 +        register char   *cp;
1139 +
1140 +        if (inpname == NULL || !inpname[0]) {   /* no geometry file */
1141 +                fclose(outfp);
1142 +                return(0);
1143 +        }
1144 +        putc('\n', outfp);
1145 +        strcpy(buf, "mgf2rad ");                /* build mgf2rad command */
1146 +        cp = buf+8;
1147 +        if (!FEQ(sinf->mult, 1.0)) {
1148 +                sprintf(cp, "-m %f ", sinf->mult);
1149 +                cp += strlen(cp);
1150 +        }
1151 +        sprintf(cp, "-g %f %s ",
1152 +                sqrt(sinf->w*sinf->w + sinf->h*sinf->h + sinf->l*sinf->l),
1153 +                        inpname);
1154 +        cp += strlen(cp);
1155 +        if (instantiate) {              /* instantiate octree */
1156 +                strcpy(cp, "| oconv - > ");
1157 +                cp += 12;
1158 +                fullnam(cp,outname,T_OCT);
1159 +                if (fdate(inpname) > fdate(outname) &&
1160 +                                system(buf)) {          /* create octree */
1161 +                        fclose(outfp);
1162 +                        return(-1);
1163 +                }
1164 +                fprintf(outfp, "void instance %s_inst\n", outname);
1165 +                if (!FEQ(meters2out, 1.0))
1166 +                        fprintf(outfp, "3 %s -s %f\n",
1167 +                                        libname(buf,outname,T_OCT),
1168 +                                        meters2out);
1169 +                else
1170 +                        fprintf(outfp, "1 %s\n", libname(buf,outname,T_OCT));
1171 +                fprintf(outfp, "0\n0\n");
1172 +                fclose(outfp);
1173 +        } else {                        /* else append to luminaire file */
1174 +                if (!FEQ(meters2out, 1.0)) {    /* apply scalefactor */
1175 +                        sprintf(cp, "| xform -s %f ", meters2out);
1176 +                        cp += strlen(cp);
1177 +                }
1178 +                if (!out2stdout) {
1179 +                        fclose(outfp);
1180 +                        strcpy(cp, ">> ");      /* append works for DOS? */
1181 +                        cp += 3;
1182 +                        fullnam(cp,outname,T_RAD);
1183 +                }
1184 +                if (system(buf))
1185 +                        return(-1);
1186 +        }
1187          return(0);
1188   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines