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.23 by schorsch, Sat Nov 15 13:29:23 2003 UTC vs.
Revision 2.30 by greg, Mon Jun 4 23:05:34 2018 UTC

# Line 2 | Line 2
2   static const char       RCSid[] = "$Id$";
3   #endif
4   /*
5 < * Convert IES luminaire data to Radiance description
5 > * ies2rad -- Convert IES luminaire data to Radiance description
6   *
7 + * ies2rad converts an IES LM-63 luminare description to a Radiance
8 + * luminaire description.  In addition, ies2rad manages a local
9 + * database of Radiance luminaire files.
10 + *
11 + * Ies2rad generates two or three files for each luminaire. For a
12 + * luminaire named LUM, ies2rad will generate LUM.rad, a Radiance
13 + * scene description file which describes the light source, LUM.dat,
14 + * which contains the photometric data from the IES LM-63 file, and
15 + * (if tilt data is provided) LUM%.dat, which contains the tilt data
16 + * from the IES file.
17 + *
18 + * Ies2rad is supported by the Radiance function files source.cal and
19 + * tilt.cal, which transform the coordinates in the IES data into
20 + * Radiance (θ,φ) luminaire coordinates and then apply photometric and
21 + * tilt data to generate Radiance light. θ is altitude from the
22 + * negative z-axis and φ is azimuth from the positive x-axis,
23 + * increasing towards the positive y-axis. [??? Greg, is there a
24 + * source for this convention?] This system matches none of the usual
25 + * goniophotometric conventions, but it is closest to IES type C; V in
26 + * type C photometry is θ in Radiance and L is -φ.
27 + *
28 + * The ies2rad scene description for a luminaire LUM, with tilt data,
29 + * uses the following Radiance scene description primitives:
30 + *
31 + *     void brightdata LUM_tilt
32 + *     …
33 + *     LUM_tilt brightdata LUM_dist
34 + *     …
35 + *     LUM_dist light LUM_light
36 + *     …
37 + *     LUM_light surface1 name1
38 + *     …
39 + *     LUM_light surface2 name2
40 + *     …
41 + *     LUM_light surface_n name_n
42 + *
43 + * Without tilt data, the primitives are:
44 + *
45 + *     void brightdata LUM_dist
46 + *     …
47 + *     LUM_dist light LUM_light
48 + *     …
49 + *     LUM_light surface1 name1
50 + *     …
51 + *     LUM_light surface2 name2
52 + *     …
53 + *     LUM_light surface_n name_n
54 + *
55 + * As many surfaces are given as required to describe the light
56 + * source. Illum may be used rather than light so that a visible form
57 + * (impostor) may be given to the luminaire, rather than a simple
58 + * glowing shape. If an impostor is provided, it must be wholly
59 + * contained within the illum and if it provides impostor light
60 + * sources, those must be given with glow, so that they do not
61 + * themselves illuminate the scene, providing incorrect results.
62 + *
63 + * The ies2rad code uses the "bsd" style. For emacs, this is set up
64 + * automatically in the "Local Variables" section at the end of the
65 + * file. For vim, use ":set tabstop=8 shiftwidth=8".
66 + *
67   *      07Apr90         Greg Ward
68   *
69   *  Fixed correction factor for flat sources 29Oct2001 GW
70 + *  Extensive comments added by Randolph Fritz May2018
71   */
72  
73   #include <stdio.h>
# Line 20 | Line 81 | static const char      RCSid[] = "$Id$";
81   #include "paths.h"
82  
83   #define PI              3.14159265358979323846
84 <                                        /* floating comparisons */
84 >
85 > /* floating comparisons -- floating point numbers within FTINY of each
86 > * other are considered equal */
87   #define FTINY           1e-6
88   #define FEQ(a,b)        ((a)<=(b)+FTINY&&(a)>=(b)-FTINY)
89 <                                        /* keywords */
89 >
90 >
91 > /* IESNA LM-63 keywords and constants */
92 > /* Since 1991, LM-63 files have begun with the magic keyword IESNA */
93   #define MAGICID         "IESNA"
94   #define LMAGICID        5
95 + /* But newer files start with IESNA:LM-63- */
96 + #define MAGICID2        "IESNA:LM-63-"
97 + #define LMAGICID2       12
98 + /* ies2rad supports the 1986, 1991, and 1995 versions of
99 + * LM-63. FIRSTREV describes the first version; LASTREV describes the
100 + * 1995 version. */
101   #define FIRSTREV        86
102   #define LASTREV         95
103  
104 < #define D86             0               /* keywords defined in LM-63-1986 */
104 > /* The following definitions support LM-63 file keyword reading and
105 > * analysis.
106 > *
107 > * This section defines two function-like macros: keymatch(i,s), which
108 > * checks to see if keyword i matches string s, and checklamp(s),
109 > * which checks to see if a string matches the keywords "LAMP" or
110 > * "LAMPCAT".
111 > *
112 > * LM-63-1986 files begin with a list of free-form label lines.
113 > * LM-63-1991 files begin with the identifying line "IESNA91" followed
114 > * by a list of formatted keywords.  LM-63-1995 files begin with the
115 > * identifying line "IESNA:LM-63-1995" followed by a list of formatted
116 > * keywords.
117 > *
118 > * The K_* #defines enumerate the keywords used in the different
119 > * versions of the file and give them symbolic names.
120 > *
121 > * The D86, D91, and D95 #defines validate the keywords in the 1986,
122 > * 1991, and 1995 versions of the standard, one bit per keyword.
123 > * Since the 1986 standard does not use keywords, D86 is zero.  The
124 > * 1991 standard has 13 keywords, and D91 has the lower 13 bits set.
125 > * The 1995 standard has 14 keywords, and D95 has the lower 14 bits
126 > * set.
127 > *
128 > */
129 > #define D86             0
130  
131   #define K_TST           0
132   #define K_MAN           1
# Line 45 | Line 142 | static const char      RCSid[] = "$Id$";
142   #define K_BLK           11
143   #define K_EBK           12
144  
145 < #define D91             ((1L<<13)-1)    /* keywords defined in LM-63-1991 */
145 > /* keywords defined in LM-63-1991 */
146 > #define D91             ((1L<<13)-1)
147  
148   #define K_LMG           13
149  
150 < #define D95             ((1L<<14)-1)    /* keywords defined in LM-63-1995 */
150 > /* keywords defined in LM-63-1995 */
151 > #define D95             ((1L<<14)-1)
152  
153   char    k_kwd[][20] = {"TEST", "MANUFAC", "LUMCAT", "LUMINAIRE", "LAMPCAT",
154                          "LAMP", "BALLAST", "MAINTCAT", "OTHER", "SEARCH",
# Line 65 | Line 164 | int    filerev = FIRSTREV;
164   #define checklamp(s)    (!(k_defined[filerev-FIRSTREV]&(1<<K_LMP|1<<K_LPC)) ||\
165                                  keymatch(K_LMP,s) || keymatch(K_LPC,s))
166  
167 <                                        /* tilt specs */
167 > /* tilt specs
168 > *
169 > * This next series of definitions address metal-halide lamps, which
170 > * change their brightness depending on the angle at which they are
171 > * mounted. The section begins with "TILT=".  The constants in this
172 > * section are all defined in LM-63.
173 > *
174 > */
175 >
176   #define TLTSTR          "TILT="
177   #define TLTSTRLEN       5
178   #define TLTNONE         "NONE"
# Line 73 | Line 180 | int    filerev = FIRSTREV;
180   #define TLT_VERT        1
181   #define TLT_H0          2
182   #define TLT_H90         3
183 <                                        /* photometric types */
183 >
184 > /* Constants from LM-63 files */
185 >
186 > /* photometric types
187 > *
188 > * This enumeration reflects three different methods of measuring the
189 > * distribution of light from a luminaire -- "goniophotometry" -- and
190 > * the different coordinate systems related to these
191 > * goniophotometers.  All are described in IES standard LM-75-01.
192 > * Earlier and shorter descriptions may be found the LM-63 standards
193 > * from 1986, 1991, and 1995.
194 > *
195 > * ies2rad does not support type A photometry.
196 > *
197 > * In the 1986 file format, LM-63-86, 1 is used for type C and type A
198 > * photometric data.
199 > *
200 > */
201   #define PM_C            1
202   #define PM_B            2
203 <                                        /* unit types */
203 > #define PM_A            3
204 >
205 > /* unit types */
206   #define U_FEET          1
207   #define U_METERS        2
208 <                                        /* string lengths */
209 < #define MAXLINE         132
208 >
209 > /* string lengths */
210 > /* Maximum input line is 132 characters including CR LF at end. */
211 > #define MAXLINE         133
212   #define RMAXWORD        76
213 <                                        /* file types */
213 >
214 > /* End of LM-63-related #defines */
215 >
216 > /* file extensions */
217   #define T_RAD           ".rad"
218   #define T_DST           ".dat"
219   #define T_TLT           "%.dat"
220   #define T_OCT           ".oct"
221 <                                        /* shape types */
221 >
222 > /* shape types
223 > * These #defines enumerate the shapes of the Radiance objects which
224 > * emit the light.
225 > */
226   #define RECT            1
227   #define DISK            2
228   #define SPHERE          3
229  
230 < #define MINDIM          .001            /* minimum dimension (point source) */
230 > /* The diameter of a point source luminaire model. Also the minimum
231 > * size (in meters) that the luminous opening of a luminaire must have
232 > * to be treated as other than a point source. */
233 > #define MINDIM          .001
234  
235 < #define F_M             .3048           /* feet to meters */
235 > /* feet to meters */
236 > /* length_in_meters = length_in_feet * F_M */
237 > #define F_M             .3048
238  
239 + /* abspath - return true if a path begins with a directory separator
240 + * or a '.' (current directory) */
241   #define abspath(p)      (ISDIRSEP((p)[0]) || (p)[0] == '.')
242  
243 + /* Global variables.
244 + *
245 + * Mostly, these are a way of communicating command line parameters to
246 + * the rest of the program.
247 + */
248   static char     default_name[] = "default";
249  
250   char    *libdir = NULL;                 /* library directory location */
# Line 115 | Line 262 | int    out2stdout = 0;                 /* put out to stdout r.t. file *
262   int     instantiate = 0;                /* instantiate geometry */
263   double  illumrad = 0.0;                 /* radius for illum sphere */
264  
265 + /* This struct describes the Radiance source object */
266   typedef struct {
267          int     isillum;                        /* do as illum */
268          int     type;                           /* RECT, DISK, SPHERE */
# Line 123 | Line 271 | typedef struct {
271          double  area;                           /* max. projected area */
272   } SRCINFO;                              /* a source shape (units=meters) */
273  
274 < int     gargc;                          /* global argc (minus filenames) */
274 > /* A count and pointer to the list of input file names */
275 > int     gargc;                          /* global argc */
276   char    **gargv;                        /* global argv */
277  
278 <
278 > /* macros to scan numbers out of IES files
279 > *
280 > * fp is a file pointer.  scnint() places the number in the integer
281 > * indicated by ip; scnflt() places the number in the double indicated
282 > * by rp. The macros return 1 if successful, 0 if not.
283 > *
284 > */
285   #define scnint(fp,ip)   cvtint(ip,getword(fp))
286   #define scnflt(fp,rp)   cvtflt(rp,getword(fp))
132 #define isint           isflt                   /* IES allows real as integer */
287  
288 + /* The original (1986) version of LM-63 allows decimals points in
289 + * integers, so that, for instance, the number of lamps may be written
290 + * 3.0 (the number, obviously, must still be an integer.) This
291 + * confusing define accommodates that.  */
292 + #define isint           isflt
293  
294 + /* Function declarations */
295   static int ies2rad(char *inpname, char *outname);
296   static void initlamps(void);
297   static int dosource(SRCINFO *sinf, FILE *in, FILE *out, char *mod, char *name);
# Line 159 | Line 319 | static char * libname(char *path, char *fname, char *s
319   static char * getword(FILE *fp);
320   static char * fullnam(char *path, char *fname, char *suffix);
321  
322 <
322 > /* main - process arguments and run the conversion
323 > *
324 > * Refer to the man page for details of the arguments.
325 > *
326 > * Following Unix environment conventions, main() exits with 0 on
327 > * success and 1 on failure.
328 > *
329 > * ies2rad outputs either two or three files for a given IES
330 > * file. There is always a .rad file containing Radiance scene
331 > * description primitives and a .dat file for the photometric data. If
332 > * tilt data is given, that is placed in a separate .dat file.  So
333 > * ies2rad must have a filename to operate. Sometimes this name is the
334 > * input file name, shorn of its extension; sometimes it is given in
335 > * the -o option. But an output file name is required for ies2rad to
336 > * do its work.
337 > *
338 > * Older versions of the LM-63 standard allowed inclusion of multiple
339 > * luminaires in one IES file; this is not supported by ies2rad.
340 > *
341 > * This code sometimes does not check to make sure it has not run out
342 > * of arguments; this can lead to segmentation faults and perhaps
343 > * other errors.
344 > *
345 > */
346   int
347   main(
348          int     argc,
# Line 171 | Line 354 | main(
354          char    outname[RMAXWORD];
355          double  d1;
356          int     i;
357 <        
357 >
358 >        /* Scan the options */
359          for (i = 1; i < argc && argv[i][0] == '-'; i++)
360                  switch (argv[i][1]) {
361                  case 'd':               /* dimensions */
# Line 242 | Line 426 | main(
426                  case 'i':               /* illum */
427                          illumrad = atof(argv[++i]);
428                          break;
429 <                case 'g':               /* instatiate geometry? */
429 >                case 'g':               /* instantiate geometry? */
430                          instantiate = !instantiate;
431                          break;
432                  case 't':               /* override lamp type */
# Line 265 | Line 449 | main(
449                                          argv[0], argv[i]);
450                          exit(1);
451                  }
452 +        /* Save pointers to the list of input file names */
453          gargc = i;
454          gargv = argv;
455 <        initlamps();                    /* get lamp data (if needed) */
456 <                                        /* convert ies file(s) */
455 >
456 >        /* get lamp data (if needed) */
457 >        initlamps();
458 >
459 >        /* convert ies file(s) */
460 >        /* If an output file name is specified */
461          if (outfile != NULL) {
462                  if (i == argc)
463 +                        /* If no input filename is given, use stdin as
464 +                         * the source for the IES file */
465                          exit(ies2rad(NULL, outfile) == 0 ? 0 : 1);
466                  else if (i == argc-1)
467 +                        /* If exactly one input file name is given, use it. */
468                          exit(ies2rad(argv[i], outfile) == 0 ? 0 : 1);
469                  else
470 <                        goto needsingle;
470 >                        goto needsingle; /* Otherwise, error. */
471          } else if (i >= argc) {
472 +                /* If an output file and an input file are not give, error. */
473                  fprintf(stderr, "%s: missing output file specification\n",
474                                  argv[0]);
475                  exit(1);
476          }
477 +        /* If no input or output file is given, error. */
478          if (out2stdout && i != argc-1)
479                  goto needsingle;
480 +        /* Otherwise, process each input file in turn. */
481          status = 0;
482          for ( ; i < argc; i++) {
483                  tailtrunc(strcpy(outname,filename(argv[i])));
# Line 295 | Line 490 | needsingle:
490          exit(1);
491   }
492  
493 + /* Initlamps -- If necessary, read lamp data table */
494   void
495   initlamps(void)                         /* set up lamps */
496   {
497          float   *lcol;
498          int     status;
499  
500 +        /* If the lamp name is set to default, don't bother to read
501 +         * the lamp data table. */
502          if (lamptype != NULL && !strcmp(lamptype, default_name) &&
503                          deflamp == NULL)
504 <                return;                         /* no need for data */
505 <                                                /* else load file */
506 <        if ((status = loadlamps(lampdat)) < 0)
507 <                exit(1);
504 >                return;
505 >
506 >        if ((status = loadlamps(lampdat)) < 0) /* Load the lamp data table */
507 >                exit(1);                       /* Exit if problems
508 >                                                * with the file. */
509          if (status == 0) {
510 +                /* If can't open the file, just use the standard default lamp */
511                  fprintf(stderr, "%s: warning - no lamp data\n", lampdat);
512                  lamptype = default_name;
513                  return;
514          }
515 <        if (deflamp != NULL) {                  /* match default type */
515 >        if (deflamp != NULL) {
516 >                /* Look up the specified default lamp type */
517                  if ((lcol = matchlamp(deflamp)) == NULL)
518 +                        /* If it can't be found, use the default */
519                          fprintf(stderr,
520                                  "%s: warning - unknown default lamp type\n",
521                                          deflamp);
522                  else
523 +                        /* Use the selected default lamp color */
524                          copycolor(defcolor, lcol);
525          }
526 <        if (lamptype != NULL) {                 /* match selected type */
526 >        /* If a lamp type is specified and can be found, use it, and
527 >         * release the lamp data table memory; it won't be needed any more. */
528 >        if (lamptype != NULL) {
529                  if (strcmp(lamptype, default_name)) {
530                          if ((lcol = matchlamp(lamptype)) == NULL) {
531                                  fprintf(stderr,
# Line 332 | Line 537 | initlamps(void)                                /* set up lamps */
537                  }
538                  freelamps();                    /* all done with data */
539          }
540 <                                                /* else keep lamp data */
540 >        /* else keep lamp data */
541   }
542  
543 + /*
544 + * File path operations
545 + *
546 + * These provide file path operations that operate on both MS-Windows
547 + * and *nix. They will ignore and pass, but will not necessarily
548 + * process correctly, Windows drive letters. Paths including Windows
549 + * UNC network names (\\server\folder\file) may also cause problems.
550 + *
551 + */
552  
553 + /*
554 + * stradd()
555 + *
556 + * Add a string to the end of a string, optionally concatenating a
557 + * file path separator character.  If the path already ends with a
558 + * path separator, no additional separator is appended.
559 + *
560 + */
561   char *
562   stradd(                 /* add a string at dst */
563 <        register char   *dst,
564 <        register char   *src,
563 >        char    *dst,
564 >        char    *src,
565          int     sep
566   )
567   {
# Line 354 | Line 576 | stradd(                        /* add a string at dst */
576          return(dst);
577   }
578  
579 <
579 > /*
580 > * fullnam () - return a usable path name for an output file
581 > */
582   char *
583 < fullnam(                /* return full path name */
584 <        char    *path,
585 <        char    *fname,
586 <        char    *suffix
583 > fullnam(
584 >        char    *path,          /* The base directory path */
585 >        char    *fname,         /* The file name */
586 >        char    *suffix         /* A suffix, which usually contains
587 >                                 * a file name extension. */
588   )
589   {
590 +        extern char *prefdir;
591 +        extern char *libdir;
592 +
593          if (prefdir != NULL && abspath(prefdir))
594 +                /* If the subdirectory path is absolute or '.', just
595 +                 * concatenate the names together */
596                  libname(path, fname, suffix);
597          else if (abspath(fname))
598 +                /* If there is no subdirectory, and the file name is
599 +                 * an absolute path or '.', concatenate the path,
600 +                 * filename, and suffix. */
601                  strcpy(stradd(path, fname, 0), suffix);
602          else
603 +                /* If the file name is relative, concatenate path,
604 +                 * library directory, directory separator, file name,
605 +                 * and suffix.  */
606                  libname(stradd(path, libdir, DIRSEP), fname, suffix);
607  
608          return(path);
609   }
610  
611  
612 + /*
613 + * libname - convert a file name to a path
614 + */
615   char *
616 < libname(                /* return library relative name */
617 <        char    *path,
618 <        char    *fname,
619 <        char    *suffix
616 > libname(
617 >        char    *path,          /* The base directory path */
618 >        char    *fname,         /* The file name */
619 >        char    *suffix         /* A suffix, which usually contains
620 >                                 * a file name extension. */
621   )
622   {
623 +        extern char *prefdir;   /* The subdirectory where the file
624 +                                 * name is stored. */
625 +
626          if (abspath(fname))
627 +                /* If the file name begins with '/' or '.', combine
628 +                 * it with the path and attach the suffix */
629                  strcpy(stradd(path, fname, 0), suffix);
630          else
631 +                /* If the file name is relative, attach it to the
632 +                 * path, include the subdirectory, and append the suffix. */
633                  strcpy(stradd(stradd(path, prefdir, DIRSEP), fname, 0), suffix);
634  
635          return(path);
636   }
637  
638 <
638 > /* filename - find the base file name in a buffer containing a path
639 > *
640 > * The pointer is to a character within the buffer, not a string in itself;
641 > * it will become invalid when the buffer is freed.
642 > *
643 > */
644   char *
645 < filename(                       /* get final component of pathname */
646 <        register char   *path
645 > filename(
646 >        char    *path
647   )
648   {
649 <        register char   *cp;
649 >        char    *cp;
650  
651          for (cp = path; *path; path++)
652                  if (ISDIRSEP(*path))
# Line 403 | Line 655 | filename(                      /* get final component of pathname */
655   }
656  
657  
658 + /* filetrunc() - return the directory portion of a path
659 + *
660 + * The path is passed in in a pointer to a buffer; a null character is
661 + * inserted in the buffer after the last directory separator
662 + *
663 + */
664   char *
665 < filetrunc(                              /* truncate filename at end of path */
665 > filetrunc(
666          char    *path
667   )
668   {
669 <        register char   *p1, *p2;
669 >        char    *p1, *p2;
670  
671          for (p1 = p2 = path; *p2; p2++)
672                  if (ISDIRSEP(*p2))
# Line 419 | Line 677 | filetrunc(                             /* truncate filename at end of path */
677          return(path);
678   }
679  
680 <
680 > /* tailtrunc() - trim a file name extension, if any.
681 > *
682 > * The file name is passed in in a buffer indicated by *name; the
683 > * period which begins the extension is replaced with a 0 byte.
684 > */
685   char *
686 < tailtrunc(                              /* truncate tail of filename */
686 > tailtrunc(
687          char    *name
688   )
689   {
690 <        register char   *p1, *p2;
690 >        char    *p1, *p2;
691  
692 +        /* Skip leading periods */
693          for (p1 = filename(name); *p1 == '.'; p1++)
694                  ;
695 +        /* Find the last period in a file name */
696          p2 = NULL;
697          for ( ; *p1; p1++)
698                  if (*p1 == '.')
699                          p2 = p1;
700 +        /* If present, trim the filename at that period */
701          if (p2 != NULL)
702                  *p2 = '\0';
703          return(name);
704   }
705  
706 <
706 > /* blanktrunc() - trim spaces at the end of a string
707 > *
708 > * the string is passed in a character array, which is modified
709 > */
710   void
711 < blanktrunc(                             /* truncate spaces at end of line */
711 > blanktrunc(
712          char    *s
713   )
714   {
715 <        register char   *cp;
715 >        char    *cp;
716  
717          for (cp = s; *cp; cp++)
718                  ;
# Line 453 | Line 721 | blanktrunc(                            /* truncate spaces at end of line */
721          *++cp = '\0';
722   }
723  
724 <
724 > /* k_match - return true if keyword matches header line */
725   int
726 < k_match(                        /* header line matches keyword? */
727 <        register char   *kwd,
728 <        register char   *hdl
726 > k_match(
727 >        char    *kwd,           /* keyword */
728 >        char    *hdl            /* header line */
729   )
730   {
731 <        if (!*hdl++ == '[')
731 >        /* The line has to begin with '[' */
732 >        if (*hdl++ != '[')
733                  return(0);
734 <        while (islower(*hdl) ? toupper(*hdl) == *kwd++ : *hdl == *kwd++)
734 >        /* case-independent keyword match */
735 >        while (toupper(*hdl) == *kwd++)
736                  if (!*hdl++)
737                          return(0);
738 <        return((!*kwd) & (*hdl == ']'));
738 >        /* If we have come to the end of the keyword, and the keyword
739 >         * at the beginning of the matched line is terminated with
740 >         * ']', return 1 */
741 >        return(!kwd[-1] & (*hdl == ']'));
742   }
743  
744 <
744 > /* keyargs - return the argument of a keyword, without leading spaces
745 > *
746 > * keyargs is passed a pointer to a buffer; it returns a pointer to
747 > * where the argument starts in the buffer
748 > *
749 > */
750   char *
751 < keyargs(                                /* return keyword arguments */
752 <        register char   *hdl
751 > keyargs(
752 >        char    *hdl /* header line */
753   )
754   {
755          while (*hdl && *hdl++ != ']')
# Line 482 | Line 760 | keyargs(                               /* return keyword arguments */
760   }
761  
762  
763 + /* putheader - output the header of the .rad file
764 + *
765 + * Header is:
766 + *   # <file> <file> <file> (all files from input line)
767 + *   # Dimensions in [feet,meters,etc.]
768 + *
769 + * ??? Is listing all the input file names correct behavior?
770 + *
771 + */
772   void
773 < putheader(                              /* print header */
773 >
774 > putheader(
775          FILE    *out
776   )
777   {
778 <        register int    i;
779 <        
778 >        int     i;
779 >
780          putc('#', out);
781          for (i = 0; i < gargc; i++) {
782                  putc(' ', out);
# Line 499 | Line 787 | putheader(                             /* print header */
787          putc('\n', out);
788   }
789  
790 <
790 > /* ies2rad - convert an IES LM-63 file to a Radiance light source desc.
791 > *
792 > * Return -1 in case of failure, 0 in case of success.
793 > *
794 > * The file version recognition is confused and will treat 1995 and
795 > * 2002 version files as 1986 version files.
796 > *
797 > */
798   int
799   ies2rad(                /* convert IES file */
800          char    *inpname,
# Line 512 | Line 807 | ies2rad(               /* convert IES file */
807          FILE    *inpfp, *outfp;
808          int     lineno = 0;
809  
810 +        /* Open input and output files */
811          geomfile[0] = '\0';
812          srcinfo.isillum = 0;
813          if (inpname == NULL) {
# Line 528 | Line 824 | ies2rad(               /* convert IES file */
824                  fclose(inpfp);
825                  return(-1);
826          }
827 +
828 +        /* Output the output file header */
829          putheader(outfp);
830 +
831 +        /* If the lamp type wasn't given on the command line, mark
832 +         * the lamp color as missing */
833          if (lamptype == NULL)
834                  lampcolor = NULL;
835 +
836 +        /* Read the input file header, copying lines to the .rad file
837 +         * and looking for a lamp type. Stop at EOF or a line
838 +         * beginning with "TILT=". */
839          while (fgets(buf,sizeof(buf),inpfp) != NULL
840                          && strncmp(buf,TLTSTR,TLTSTRLEN)) {
841 <                blanktrunc(buf);
842 <                if (!buf[0])
841 >                blanktrunc(buf); /* Trim trailing spaces, CR, LF. */
842 >                if (!buf[0])     /* Skip blank lines */
843                          continue;
844 <                if (!lineno++ && !strncmp(buf, MAGICID, LMAGICID)) {
845 <                        filerev = atoi(buf+LMAGICID);
844 >                /* increment the header line count, and check for the
845 >                 * "TILT=" line that terminates the header */
846 >                if (!lineno++) {        /* first line may be magic */
847 >                        if (!strncmp(buf, MAGICID2, LMAGICID2))
848 >                                filerev = atoi(buf+LMAGICID2) - 1900;
849 >                        else if (!strncmp(buf, MAGICID, LMAGICID))
850 >                                filerev = atoi(buf+LMAGICID);
851                          if (filerev < FIRSTREV)
852                                  filerev = FIRSTREV;
853                          else if (filerev > LASTREV)
854                                  filerev = LASTREV;
855                  }
856 +                /* Output the header line as a comment in the .rad file. */
857                  fputs("#<", outfp);
858                  fputs(buf, outfp);
859                  putc('\n', outfp);
860 +
861 +                /* If the header line is a keyword line (file version
862 +                 * later than 1986 and begins with '['), check a lamp
863 +                 * in the "[LAMP]" and "[LAMPCAT]" keyword lines;
864 +                 * otherwise check all lines.  */
865                  if (lampcolor == NULL && checklamp(buf))
866                          lampcolor = matchlamp( buf[0] == '[' ?
867                                                  keyargs(buf) : buf );
868 <                if (keymatch(K_LMG, buf)) {             /* geometry file */
868 >                /* Look for a materials and geometry file in the keywords. */
869 >                if (keymatch(K_LMG, buf)) {
870                          strcpy(geomfile, inpname);
871                          strcpy(filename(geomfile), keyargs(buf));
872                          srcinfo.isillum = 1;
873                  }
874          }
875 +
876 +        /* Done reading header information. If a lamp color still
877 +         * hasn't been found, print a warning and use the default
878 +         * color; if a lamp type hasn't been found, but a color has
879 +         * been specified, used the specified color. */
880          if (lampcolor == NULL) {
881                  fprintf(stderr, "%s: warning - no lamp type\n", inpname);
882                  fputs("# Unknown lamp type (used default)\n", outfp);
# Line 562 | Line 884 | ies2rad(               /* convert IES file */
884          } else if (lamptype == NULL)
885                  fprintf(outfp,"# CIE(x,y) = (%f,%f)\n# Depreciation = %.1f%%\n",
886                                  lampcolor[3], lampcolor[4], 100.*lampcolor[5]);
887 +        /* If the file ended before a "TILT=" line, that's an error. */
888          if (feof(inpfp)) {
889                  fprintf(stderr, "%s: not in IES format\n", inpname);
890                  goto readerr;
891          }
892 +
893 +        /* Process the tilt section of the file. */
894 +        /* Get the tilt file name, or the keyword "INCLUDE". */
895          atos(tltid, RMAXWORD, buf+TLTSTRLEN);
896          if (inpfp == stdin)
897                  buf[0] = '\0';
898          else
899                  filetrunc(strcpy(buf, inpname));
900 +        /* Process the tilt data. */
901          if (dotilt(inpfp, outfp, buf, tltid, outname, tltid) != 0) {
902                  fprintf(stderr, "%s: bad tilt data\n", inpname);
903                  goto readerr;
904          }
905 +
906 +        /* Process the luminaire data. */
907          if (dosource(&srcinfo, inpfp, outfp, tltid, outname) != 0) {
908                  fprintf(stderr, "%s: bad luminaire data\n", inpname);
909                  goto readerr;
910          }
911 +
912 +        /* Close the input file */
913          fclose(inpfp);
914 <                                        /* cvgeometry closes outfp */
914 >
915 >        /* Process an MGF file, if present. cvgeometry() closes outfp. */
916          if (cvgeometry(geomfile, &srcinfo, outname, outfp) != 0) {
917                  fprintf(stderr, "%s: bad geometry file\n", geomfile);
918                  return(-1);
919          }
920          return(0);
921 +
922   readerr:
923 +        /* If there is an error reading the file, close the input and
924 +         * .rad output files, and delete the .rad file, returning -1. */
925          fclose(inpfp);
926          fclose(outfp);
927          unlink(fullnam(buf,outname,T_RAD));
928          return(-1);
929   }
930  
931 <
931 > /* dotilt -- process tilt data
932 > *
933 > * Generate a brightdata primitive which describes the effect of
934 > * luminaire tilt on luminaire output and return its identifier in tltid.
935 > *
936 > * Tilt data (if present) is given as a number 1, 2, or 3, which
937 > * specifies the orientation of the lamp within the luminaire, a
938 > * number, n, of (angle, multiplier) pairs, followed by n angles and n
939 > * multipliers.
940 > *
941 > * returns 0 for success, -1 for error
942 > */
943   int
944 < dotilt( /* convert tilt data */
944 > dotilt(
945          FILE    *in,
946          FILE    *out,
947          char    *dir,
# Line 609 | Line 955 | dotilt(        /* convert tilt data */
955          char    buf[PATH_MAX], tltname[RMAXWORD];
956          FILE    *datin, *datout;
957  
958 +        /* Decide where the tilt data is; if the luminaire description
959 +         * doesn't have a tilt section, set the identifier to "void". */
960          if (!strcmp(tltspec, TLTNONE)) {
961 +                /* If the line is "TILT=NONE", set the input file
962 +                 * pointer to NULL and the identifier to "void". */
963                  datin = NULL;
964                  strcpy(tltid, "void");
965          } else if (!strcmp(tltspec, TLTINCL)) {
966 +                /* If the line is "TILT=INCLUDE" use the main IES
967 +                 * file as the source of tilt data. */
968                  datin = in;
969                  strcpy(tltname, dfltname);
970          } else {
971 +                /* If the line is "TILE=<filename>", use that file
972 +                 * name as the source of tilt data. */
973                  if (ISDIRSEP(tltspec[0]))
974                          strcpy(buf, tltspec);
975                  else
# Line 626 | Line 980 | dotilt(        /* convert tilt data */
980                  }
981                  tailtrunc(strcpy(tltname,filename(tltspec)));
982          }
983 +        /* If tilt data is present, read, process, and output it. */
984          if (datin != NULL) {
985 +                /* Try to open the output file */
986                  if ((datout = fopen(fullnam(buf,tltname,T_TLT),"w")) == NULL) {
987                          perror(buf);
988                          if (datin != in)
989                                  fclose(datin);
990                          return(-1);
991                  }
992 +                /* Try to copy the tilt data to the tilt data file */
993                  if (!scnint(datin,&tlt_type) || !scnint(datin,&nangles)
994                          || cvdata(datin,datout,1,&nangles,1.,minmax) != 0) {
995                          fprintf(stderr, "%s: data format error\n", tltspec);
# Line 645 | Line 1002 | dotilt(        /* convert tilt data */
1002                  fclose(datout);
1003                  if (datin != in)
1004                          fclose(datin);
1005 +
1006 +                /* Generate the identifier of the brightdata; the filename
1007 +                 * with "_tilt" appended. */
1008                  strcat(strcpy(tltid, filename(tltname)), "_tilt");
1009 +                /* Write out the brightdata primitive */
1010                  fprintf(out, "\nvoid brightdata %s\n", tltid);
1011                  libname(buf,tltname,T_TLT);
1012 +                /* Generate the tilt description */
1013                  switch (tlt_type) {
1014 <                case TLT_VERT:                  /* vertical */
1014 >                case TLT_VERT:
1015 >                        /* The lamp is mounted vertically; either
1016 >                         * base up or base down. */
1017                          fprintf(out, "4 noop %s tilt.cal %s\n", buf,
1018                                  minmax[0][1]>90.+FTINY ? "tilt_ang" : "tilt_ang2");
1019                          break;
1020 <                case TLT_H0:                    /* horiz. in 0 deg. plane */
1020 >                case TLT_H0:
1021 >                        /* The lamp is mounted horizontally and
1022 >                         * rotates but does not tilt when the
1023 >                         * luminaire is tilted. */
1024                          fprintf(out, "6 noop %s tilt.cal %s -rz 90\n", buf,
1025                          minmax[0][1]>90.+FTINY ? "tilt_xang" : "tilt_xang2");
1026                          break;
1027                  case TLT_H90:
1028 +                        /* The lamp is mounted horizontally, and
1029 +                         * tilts when the luminaire is tilted. */
1030                          fprintf(out, "4 noop %s tilt.cal %s\n", buf,
1031                          minmax[0][1]>90.+FTINY ? "tilt_xang" : "tilt_xang2");
1032                          break;
1033                  default:
1034 +                        /* otherwise, this is a bad IES file */
1035                          fprintf(stderr,
1036                                  "%s: illegal lamp to luminaire geometry (%d)\n",
1037                                  tltspec, tlt_type);
1038                          return(-1);
1039                  }
1040 +                /* And finally output the numbers of integer and real
1041 +                 * arguments, of which there are none. */
1042                  fprintf(out, "0\n0\n");
1043          }
1044          return(0);
1045   }
1046  
1047 <
1047 > /* dosource -- create the source and distribution primitives */
1048   int
1049 < dosource(       /* create source and distribution */
1049 > dosource(
1050          SRCINFO *sinf,
1051          FILE    *in,
1052          FILE    *out,
# Line 688 | Line 1060 | dosource(      /* create source and distribution */
1060          double  bounds[2][2];
1061          int     nangles[2], pmtype, unitype;
1062          double  d1;
1063 <        int     doupper, dolower, dosides;
1063 >        int     doupper, dolower, dosides;
1064  
1065 +        /* Read in the luminaire description header */
1066          if (!isint(getword(in)) || !isflt(getword(in)) || !scnflt(in,&mult)
1067                          || !scnint(in,&nangles[0]) || !scnint(in,&nangles[1])
1068                          || !scnint(in,&pmtype) || !scnint(in,&unitype)
# Line 699 | Line 1072 | dosource(      /* create source and distribution */
1072                  fprintf(stderr, "dosource: bad lamp specification\n");
1073                  return(-1);
1074          }
1075 +        /* Type A photometry is not supported */
1076 +        if (pmtype != PM_C && pmtype != PM_B) {
1077 +                fprintf(stderr, "dosource: unsupported photometric type (%d)\n",
1078 +                                pmtype);
1079 +                return(-1);
1080 +        }
1081 +
1082 +        /* Multiplier = the multiplier from the -m option, times the
1083 +         * multiplier from the IES file, times the ballast factor,
1084 +         * times the "ballast lamp photometric factor," which was part
1085 +         * of the 1986 and 1991 standards. In the 1995 standard, it is
1086 +         * always supposed to be 1. */
1087          sinf->mult = multiplier*mult*bfactor*pfactor;
1088 +
1089 +        /* If the count of angles is wrong, raise an error and quit. */
1090          if (nangles[0] < 2 || nangles[1] < 1) {
1091                  fprintf(stderr, "dosource: too few measured angles\n");
1092                  return(-1);
1093          }
1094 +
1095 +        /* For internal computation, convert units to meters. */
1096          if (unitype == U_FEET) {
1097                  width *= F_M;
1098                  length *= F_M;
1099                  height *= F_M;
1100          }
1101 +
1102 +        /* Make decisions about the shape of the light source
1103 +         * geometry, and store them in sinf. */
1104          if (makeshape(sinf, width, length, height) != 0) {
1105                  fprintf(stderr, "dosource: illegal source dimensions");
1106                  return(-1);
1107          }
1108 +
1109 +        /* Copy the candela values into a Radiance data file. */
1110          if ((datout = fopen(fullnam(buf,name,T_DST), "w")) == NULL) {
1111                  perror(buf);
1112                  return(-1);
# Line 724 | Line 1118 | dosource(      /* create source and distribution */
1118                  return(-1);
1119          }
1120          fclose(datout);
1121 +
1122 +        /* Output explanatory comment */
1123          fprintf(out, "# %g watt luminaire, lamp*ballast factor = %g\n",
1124                          wattage, bfactor*pfactor);
1125 +        /* Output distribution "brightdata" primitive. Start handling
1126 +           the various cases of symmetry of the distribution. */
1127          strcat(strcpy(id, filename(name)), "_dist");
1128          fprintf(out, "\n%s brightdata %s\n", mod, id);
1129          if (nangles[1] < 2)
# Line 736 | Line 1134 | dosource(      /* create source and distribution */
1134                  fprintf(out, "7 ");
1135          else
1136                  fprintf(out, "5 ");
1137 <        dolower = (bounds[0][0] < 90.);
1138 <        doupper = (bounds[0][1] > 90.);
1139 <        dosides = (doupper & dolower && sinf->h > MINDIM);
1137 >
1138 >        /* If the generated source geometry will be a box, a flat
1139 >         * rectangle, or a disk figure out if it needs a top, a
1140 >         * bottom, and/or sides. */
1141 >        dolower = (bounds[0][0] < 90.-FTINY); /* Bottom */
1142 >        doupper = (bounds[0][1] > 90.+FTINY); /* Top */
1143 >        dosides = (doupper & dolower && sinf->h > MINDIM); /* Sides */
1144 >
1145 >        /* Select the appropriate function and parameters from source.cal */
1146          fprintf(out, "%s %s source.cal ",
1147                          sinf->type==SPHERE ? "corr" :
1148                          !dosides ? "flatcorr" :
# Line 750 | Line 1154 | dosource(      /* create source and distribution */
1154                  else
1155                          fprintf(out, "srcB_horiz ");
1156                  fprintf(out, "srcB_vert ");
1157 <        } else /* pmtype == PM_A */ {
1157 >        } else /* pmtype == PM_C */ {
1158                  if (nangles[1] >= 2) {
1159                          d1 = bounds[1][1] - bounds[1][0];
1160                          if (d1 <= 90.+FTINY)
# Line 768 | Line 1172 | dosource(      /* create source and distribution */
1172                  } else
1173                          fprintf(out, "src_theta ");
1174          }
1175 +        /* finish the brightdata primitive with appropriate data */
1176          if (!dosides || sinf->type == SPHERE)
1177                  fprintf(out, "\n0\n1 %g\n", sinf->mult/sinf->area);
1178          else if (sinf->type == DISK)
# Line 776 | Line 1181 | dosource(      /* create source and distribution */
1181          else
1182                  fprintf(out, "\n0\n4 %g %g %g %g\n", sinf->mult,
1183                                  sinf->l, sinf->w, sinf->h);
1184 +        /* Brightdata primitive written out. */
1185 +
1186 +        /* Finally, output the descriptions of the actual radiant
1187 +         * surfaces. */
1188          if (putsource(sinf, out, id, filename(name),
1189                          dolower, doupper, dosides) != 0)
1190                  return(-1);
1191          return(0);
1192   }
1193  
1194 <
1194 > /* putsource - output the actual light emitting geometry
1195 > *
1196 > * Three kinds of geometry are produced: rectangles and boxes, disks
1197 > * ("ring" primitive, but the radius of the hole is always zero) and
1198 > * cylinders, and spheres.
1199 > */
1200   int
1201 < putsource( /* put out source */
1201 > putsource(
1202          SRCINFO *shp,
1203          FILE    *fp,
1204          char    *mod,
1205          char    *name,
1206          int     dolower,
1207          int     doupper,
1208 <        int dosides
1208 >        int     dosides
1209   )
1210   {
1211          char    lname[RMAXWORD];
1212 <        
1212 >
1213 >        /* First, describe the light. If a materials and geometry
1214 >         * file is given, generate an illum instead. */
1215          strcat(strcpy(lname, name), "_light");
1216          fprintf(fp, "\n%s %s %s\n", mod,
1217                          shp->isillum ? "illum" : "light", lname);
# Line 803 | Line 1219 | putsource( /* put out source */
1219                          lampcolor[0], lampcolor[1], lampcolor[2]);
1220          switch (shp->type) {
1221          case RECT:
1222 +                /* Output at least one rectangle. If light is radiated
1223 +                 * from the sides of the luminaire, output rectangular
1224 +                 * sides as well. */
1225                  if (dolower)
1226                          putrectsrc(shp, fp, lname, name, 0);
1227                  if (doupper)
# Line 811 | Line 1230 | putsource( /* put out source */
1230                          putsides(shp, fp, lname, name);
1231                  break;
1232          case DISK:
1233 +                /* Output at least one disk. If light is radiated from
1234 +                 * the sides of luminaire, output a cylinder as well. */
1235                  if (dolower)
1236                          putdisksrc(shp, fp, lname, name, 0);
1237                  if (doupper)
# Line 819 | Line 1240 | putsource( /* put out source */
1240                          putcyl(shp, fp, lname, name);
1241                  break;
1242          case SPHERE:
1243 +                /* Output a sphere. */
1244                  putspheresrc(shp, fp, lname, name);
1245                  break;
1246          }
1247          return(0);
1248   }
1249  
1250 <
1250 > /* makeshape -- decide what shape will be used
1251 > *
1252 > * makeshape decides what Radiance geometry will be used to represent
1253 > * the light source and stores information about it in shp.
1254 > */
1255   int
1256 < makeshape(              /* make source shape */
1257 <        register SRCINFO        *shp,
1256 > makeshape(
1257 >        SRCINFO *shp,
1258          double  width,
1259          double  length,
1260          double  height
1261   )
1262   {
1263 +        /* Categorize the shape */
1264          if (illumrad/meters2out >= MINDIM/2.) {
1265 +                /* If the -i command line option is used, and the
1266 +                 * object is not a point source, output an "illum"
1267 +                 * sphere */
1268                  shp->isillum = 1;
1269                  shp->type = SPHERE;
1270                  shp->w = shp->l = shp->h = 2.*illumrad / meters2out;
1271          } else if (width < MINDIM) {
1272 +                /* The width is either zero or negative. */
1273                  width = -width;
1274                  if (width < MINDIM) {
1275 +                        /* The width is zero. Use a tiny sphere to
1276 +                         * represent a point source. */
1277                          shp->type = SPHERE;
1278                          shp->w = shp->l = shp->h = MINDIM;
1279                  } else if (height < .5*width) {
1280 +                        /* The width is negative and the height is
1281 +                         * modest; output either a disk or a thin
1282 +                         * vertical cylinder. */
1283                          shp->type = DISK;
1284                          shp->w = shp->l = width;
1285                          if (height >= MINDIM)
# Line 851 | Line 1287 | makeshape(             /* make source shape */
1287                          else
1288                                  shp->h = .5*MINDIM;
1289                  } else {
1290 +                        /* The width is negative and the object is
1291 +                         * tall; output a sphere. */
1292                          shp->type = SPHERE;
1293                          shp->w = shp->l = shp->h = width;
1294                  }
1295          } else {
1296 +                /* The width is positive. Output a box, possibly very
1297 +                 * thin. */
1298                  shp->type = RECT;
1299                  shp->w = width;
1300                  if (length >= MINDIM)
# Line 866 | Line 1306 | makeshape(             /* make source shape */
1306                  else
1307                          shp->h = .5*MINDIM;
1308          }
1309 +
1310 +        /* Done choosing the shape; calculate its area in the x-y plane. */
1311          switch (shp->type) {
1312          case RECT:
1313                  shp->area = shp->w * shp->l;
# Line 878 | Line 1320 | makeshape(             /* make source shape */
1320          return(0);
1321   }
1322  
1323 + /* Rectangular or box-shaped light source.
1324 + *
1325 + * putrectsrc, putsides, putrect, and putpoint are used to output the
1326 + * Radiance description of a box.  The box is centered on the origin
1327 + * and has the dimensions given in the IES file.  The coordinates
1328 + * range from [-1/2*length, -1/2*width, -1/2*height] to [1/2*length,
1329 + * 1/2*width, 1/2*height].
1330 + *
1331 + * The location of the point is encoded in the low-order three bits of
1332 + * an integer. If the integer is p, then: bit 0 is (p & 1),
1333 + * representing length (x), bit 1 is (p & 2) representing width (y),
1334 + * and bit 2 is (p & 4), representing height (z).
1335 + *
1336 + * Looking down from above (towards -z), the vertices of the box or
1337 + * rectangle are numbered so:
1338 + *
1339 + *     2,6                                        3,7
1340 + *        +--------------------------------------+
1341 + *        |                                      |
1342 + *        |                                      |
1343 + *        |                                      |
1344 + *        |                                      |
1345 + *        +--------------------------------------+
1346 + *     0,4                                        1,5
1347 + *
1348 + * The higher number of each pair is above the x-y plane (positive z),
1349 + * the lower number is below the x-y plane (negative z.)
1350 + *
1351 + */
1352  
1353 + /* putrecsrc - output a rectangle parallel to the x-y plane
1354 + *
1355 + * Putrecsrc calls out the vertices of a rectangle parallel to the x-y
1356 + * plane.  The order of the vertices is different for the upper and
1357 + * lower rectangles of a box, since a right-hand rule based on the
1358 + * order of the vertices is used to determine the surface normal of
1359 + * the rectangle, and the surface normal determines the direction the
1360 + * light radiated by the rectangle.
1361 + *
1362 + */
1363   void
1364 < putrectsrc(             /* rectangular source */
1364 > putrectsrc(
1365          SRCINFO *shp,
1366          FILE    *fp,
1367          char    *mod,
# Line 894 | Line 1375 | putrectsrc(            /* rectangular source */
1375                  putrect(shp, fp, mod, name, ".d", 0, 2, 3, 1);
1376   }
1377  
1378 <
1378 > /* putsides - put out sides of box */
1379   void
1380 < putsides(                       /* put out sides of box */
1381 <        register SRCINFO        *shp,
1380 > putsides(
1381 >        SRCINFO *shp,
1382          FILE    *fp,
1383          char    *mod,
1384          char    *name
# Line 908 | Line 1389 | putsides(                      /* put out sides of box */
1389          putrect(shp, fp, mod, name, ".3", 3, 2, 6, 7);
1390          putrect(shp, fp, mod, name, ".4", 2, 0, 4, 6);
1391   }
911        
1392  
1393 + /* putrect - put out a rectangle
1394 + *
1395 + * putrect generates the "polygon" primitive which describes a
1396 + * rectangle.
1397 + */
1398   void
1399 < putrect(        /* put out a rectangle */
1399 > putrect(
1400          SRCINFO *shp,
1401          FILE    *fp,
1402          char    *mod,
# Line 930 | Line 1415 | putrect(       /* put out a rectangle */
1415          putpoint(shp, fp, d);
1416   }
1417  
1418 <
1418 > /* putpoint -- output a the coordinates of a vertex
1419 > *
1420 > * putpoint maps vertex numbers to coordinates and outputs the
1421 > * coordinates.
1422 > */
1423   void
1424 < putpoint(                               /* put out a point */
1425 <        register SRCINFO        *shp,
1424 > putpoint(
1425 >        SRCINFO *shp,
1426          FILE    *fp,
1427          int     p
1428   )
# Line 946 | Line 1435 | putpoint(                              /* put out a point */
1435                          mult[p>>2]*shp->h*meters2out);
1436   }
1437  
1438 + /* End of routines to output a box-shaped light source */
1439  
1440 + /* Routines to output a cylindrical or disk shaped light source
1441 + *
1442 + * As with other shapes, the light source is centered on the origin.
1443 + * The "ring" and "cylinder" primitives are used.
1444 + *
1445 + */
1446   void
1447   putdisksrc(             /* put out a disk source */
1448 <        register SRCINFO        *shp,
1448 >        SRCINFO *shp,
1449          FILE    *fp,
1450          char    *mod,
1451          char    *name,
# Line 974 | Line 1470 | putdisksrc(            /* put out a disk source */
1470  
1471   void
1472   putcyl(                 /* put out a cylinder */
1473 <        register SRCINFO        *shp,
1473 >        SRCINFO *shp,
1474          FILE    *fp,
1475          char    *mod,
1476          char    *name
# Line 987 | Line 1483 | putcyl(                        /* put out a cylinder */
1483          fprintf(fp, "\t%g\n", .5*shp->w*meters2out);
1484   }
1485  
1486 + /* end of of routines to output cylinders and disks */
1487  
1488   void
1489   putspheresrc(           /* put out a sphere source */
# Line 1000 | Line 1497 | putspheresrc(          /* put out a sphere source */
1497          fprintf(fp, "0\n0\n4 0 0 0 %g\n", .5*shp->w*meters2out);
1498   }
1499  
1500 <
1500 > /* cvdata - convert LM-63 tilt and candela data to Radiance brightdata format
1501 > *
1502 > * The files created by this routine are intended for use with the Radiance
1503 > * "brightdata" material type.
1504 > *
1505 > * Two types of data are converted; one-dimensional tilt data, which
1506 > * is given in polar coordinates, and two-dimensional candela data,
1507 > * which is given in spherical co-ordinates.
1508 > *
1509 > * Return 0 for success, -1 for failure.
1510 > *
1511 > */
1512   int
1513 < cvdata(         /* convert data */
1514 <        FILE    *in,
1515 <        FILE    *out,
1516 <        int     ndim,
1517 <        int     npts[],
1518 <        double  mult,
1519 <        double  lim[][2]
1513 > cvdata(
1514 >        FILE    *in,            /* Input file */
1515 >        FILE    *out,           /* Output file */
1516 >        int     ndim,           /* Number of dimensions; 1 for
1517 >                                 * tilt data, 2 for photometric data. */
1518 >        int     npts[],         /* Number of points in each dimension */
1519 >        double  mult,           /* Multiple each value by this
1520 >                                 * number. For tilt data, always
1521 >                                 * 1. For candela values, the
1522 >                                 * efficacy of white Radiance light.  */
1523 >        double  lim[][2]        /* The range of angles in each dimension. */
1524   )
1525   {
1526 <        double  *pt[4];
1527 <        register int    i, j;
1526 >        double  *pt[4];         /* Four is the expected maximum of ndim. */
1527 >        int     i, j;
1528          double  val;
1529          int     total;
1530  
1531 +        /* Calculate and output the number of data values */
1532          total = 1; j = 0;
1533          for (i = 0; i < ndim; i++)
1534                  if (npts[i] > 1) {
# Line 1023 | Line 1536 | cvdata(                /* convert data */
1536                          j++;
1537                  }
1538          fprintf(out, "%d\n", j);
1539 <                                        /* get coordinates */
1539 >
1540 >        /* Read in the angle values, and note the first and last in
1541 >         * each dimension, if there is a place to store them. In the
1542 >         * case of tilt data, there is only one list of angles. In the
1543 >         * case of candela values, vertical angles appear first, and
1544 >         * horizontal angles occur second. */
1545          for (i = 0; i < ndim; i++) {
1546 +                /* Allocate space for the angle values. */
1547                  pt[i] = (double *)malloc(npts[i]*sizeof(double));
1548                  for (j = 0; j < npts[i]; j++)
1549                          if (!scnflt(in, &pt[i][j]))
# Line 1034 | Line 1553 | cvdata(                /* convert data */
1553                          lim[i][1] = pt[i][npts[i]-1];
1554                  }
1555          }
1556 <                                        /* write out in reverse */
1556 >
1557 >        /* Output the angles. If this is candela data, horizontal
1558 >         * angles output first. There are two cases: the first where
1559 >         * the angles are evenly spaced, the second where they are
1560 >         * not.
1561 >         *
1562 >         * When the angles are evenly spaced, three numbers are
1563 >         * output: the first angle, the last angle, and the number of
1564 >         * angles.  When the angles are not evenly spaced, instead
1565 >         * zero, zero, and the count of angles is given, followed by a
1566 >         * list of angles.  In this case, angles are output four to a line.
1567 >         */
1568          for (i = ndim-1; i >= 0; i--) {
1569                  if (npts[i] > 1) {
1570 +                        /* Determine if the angles are evenly spaces */
1571                          for (j = 1; j < npts[i]-1; j++)
1572                                  if (!FEQ(pt[i][j]-pt[i][j-1],
1573                                                  pt[i][j+1]-pt[i][j]))
1574                                          break;
1575 +                        /* If they are, output the first angle, the
1576 +                         * last angle, and a count */
1577                          if (j == npts[i]-1)
1578                                  fprintf(out, "%g %g %d\n", pt[i][0], pt[i][j],
1579                                                  npts[i]);
1580                          else {
1581 +                                /* otherwise, output 0, 0, and a
1582 +                                 * count, followed by the list of
1583 +                                 * angles, one to a line. */
1584                                  fprintf(out, "0 0 %d", npts[i]);
1585                                  for (j = 0; j < npts[i]; j++) {
1586                                          if (j%4 == 0)
# Line 1054 | Line 1590 | cvdata(                /* convert data */
1590                                  putc('\n', out);
1591                          }
1592                  }
1593 +                /* Free the storage containing the angle values. */
1594                  free((void *)pt[i]);
1595          }
1596 +
1597 +        /* Finally, read in the data values (candela or multiplier values,
1598 +         * depending on the part of the file) and output them four to
1599 +         * a line. */
1600          for (i = 0; i < total; i++) {
1601                  if (i%4 == 0)
1602                          putc('\n', out);
# Line 1067 | Line 1608 | cvdata(                /* convert data */
1608          return(0);
1609   }
1610  
1611 <
1611 > /* getword - get an LM-63 delimited word from fp
1612 > *
1613 > * Getword gets a word from an IES file delimited by either white
1614 > * space or a comma surrounded by white space. A pointer to the word
1615 > * is returned, which will persist only until getword is called again.
1616 > * At EOF, return NULL instead.
1617 > *
1618 > */
1619   char *
1620   getword(                        /* scan a word from fp */
1621 <        register FILE   *fp
1621 >        FILE    *fp
1622   )
1623   {
1624          static char     wrd[RMAXWORD];
1625 <        register char   *cp;
1626 <        register int    c;
1625 >        char    *cp;
1626 >        int     c;
1627  
1628 +        /* Skip initial spaces */
1629          while (isspace(c=getc(fp)))
1630                  ;
1631 +        /* Get characters to a delimiter or until wrd is full */
1632          for (cp = wrd; c != EOF && cp < wrd+RMAXWORD-1;
1633                          *cp++ = c, c = getc(fp))
1634                  if (isspace(c) || c == ',') {
1635 +                        /* If we find a delimiter */
1636 +                        /* Gobble up whitespace */
1637                          while (isspace(c))
1638                                  c = getc(fp);
1639 +                        /* If it's not a comma, put the first
1640 +                         * character of the next data item back */
1641                          if ((c != EOF) & (c != ','))
1642                                  ungetc(c, fp);
1643 +                        /* Close out the strimg */
1644                          *cp = '\0';
1645 +                        /* return it */
1646                          return(wrd);
1647                  }
1648 +        /* If we ran out of space or are at the end of the file,
1649 +         * return either the word or NULL, as appropriate. */
1650          *cp = '\0';
1651          return(cp > wrd ? wrd : NULL);
1652   }
1653  
1654 <
1654 > /* cvtint - convert an IES word to an integer
1655 > *
1656 > * A pointer to the word is passed in wrd; ip is expected to point to
1657 > * an integer.  cvtint() will silently truncate a floating point value
1658 > * to an integer; "1", "1.0", and "1.5" will all return 1.
1659 > *
1660 > * cvtint() returns 0 if it fails, 1 if it succeeds.
1661 > */
1662   int
1663 < cvtint(                 /* convert a word to an integer */
1663 > cvtint(
1664          int     *ip,
1665          char    *wrd
1666   )
# Line 1107 | Line 1672 | cvtint(                        /* convert a word to an integer */
1672   }
1673  
1674  
1675 + /* cvtflt - convert an IES word to a double precision floating-point number
1676 + *
1677 + * A pointer to the word is passed in wrd; rp is expected to point to
1678 + * a double.
1679 + *
1680 + * cvtflt returns 0 if it fails, 1 if it succeeds.
1681 + */
1682   int
1683 < cvtflt(                 /* convert a word to a double */
1683 > cvtflt(
1684          double  *rp,
1685          char    *wrd
1686   )
# Line 1119 | Line 1691 | cvtflt(                        /* convert a word to a double */
1691          return(1);
1692   }
1693  
1694 <
1694 > /* cvgeometry - process materials and geometry format luminaire data
1695 > *
1696 > * The materials and geometry format (MGF) for describing luminaires
1697 > * was a part of Radiance that was first adopted and then retracted by
1698 > * the IES as part of LM-63.  It provides a way of describing
1699 > * luminaire geometry similar to the Radiance scene description
1700 > * format.
1701 > *
1702 > * cvgeometry() generates an mgf2rad command and then, if "-g" is given
1703 > * on the command line, an oconv command, both of which are then
1704 > * executed with the system() function.
1705 > *
1706 > * The generated commands are:
1707 > *   mgf2rad -e <multiplier> -g <size> <mgf_filename> \
1708 > *     | xform -s <scale_factor> \
1709 > *     >> <luminare_scene_description_file
1710 > * or:
1711 > *   mgf2rad -e <multiplier> -g <size> <mgf_filename> \
1712 > *     oconv - > <instance_filename>
1713 > */
1714   int
1715   cvgeometry(
1716          char    *inpname,
1717 <        register SRCINFO        *sinf,
1717 >        SRCINFO *sinf,
1718          char    *outname,
1719          FILE    *outfp                  /* close output file upon return */
1720   )
1721   {
1722          char    buf[256];
1723 <        register char   *cp;
1723 >        char    *cp;
1724  
1725          if (inpname == NULL || !inpname[0]) {   /* no geometry file */
1726                  fclose(outfp);
# Line 1139 | Line 1730 | cvgeometry(
1730          strcpy(buf, "mgf2rad ");                /* build mgf2rad command */
1731          cp = buf+8;
1732          if (!FEQ(sinf->mult, 1.0)) {
1733 <                sprintf(cp, "-m %f ", sinf->mult);
1733 >                /* if there's an output multiplier, include in the
1734 >                 * mgf2rad command */
1735 >                sprintf(cp, "-e %f ", sinf->mult);
1736                  cp += strlen(cp);
1737          }
1738 +        /* Include the glow distance for the geometry */
1739          sprintf(cp, "-g %f %s ",
1740                  sqrt(sinf->w*sinf->w + sinf->h*sinf->h + sinf->l*sinf->l),
1741                          inpname);
1742          cp += strlen(cp);
1743          if (instantiate) {              /* instantiate octree */
1744 +                /* If "-g" is given on the command line, include an
1745 +                 * "oconv" command in the pipe. */
1746                  strcpy(cp, "| oconv - > ");
1747                  cp += 12;
1748                  fullnam(cp,outname,T_OCT);
1749 +                /* Only update if the input file is newer than the
1750 +                 * output file */
1751                  if (fdate(inpname) > fdate(outname) &&
1752                                  system(buf)) {          /* create octree */
1753                          fclose(outfp);
1754                          return(-1);
1755                  }
1756 +                /* Reference the instance file in the scene description */
1757                  fprintf(outfp, "void instance %s_inst\n", outname);
1758 +                /* If the geometry isn't in meters, scale it appropriately. */
1759                  if (!FEQ(meters2out, 1.0))
1760                          fprintf(outfp, "3 %s -s %f\n",
1761                                          libname(buf,outname,T_OCT),
1762                                          meters2out);
1763                  else
1764                          fprintf(outfp, "1 %s\n", libname(buf,outname,T_OCT));
1765 +                /* Close off the "instance" primitive. */
1766                  fprintf(outfp, "0\n0\n");
1767 +                /* And the Radiance scene description. */
1768                  fclose(outfp);
1769          } else {                        /* else append to luminaire file */
1770                  if (!FEQ(meters2out, 1.0)) {    /* apply scalefactor */
# Line 1180 | Line 1782 | cvgeometry(
1782          }
1783          return(0);
1784   }
1785 +
1786 + /* Set up emacs indentation */
1787 + /* Local Variables: */
1788 + /*   c-file-style: "bsd" */
1789 + /* End: */
1790 +
1791 + /* For vim, use ":set tabstop=8 shiftwidth=8" */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines