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

Comparing ray/src/util/wrapBSDF.c (file contents):
Revision 2.2 by greg, Wed Feb 11 19:38:18 2015 UTC vs.
Revision 2.7 by greg, Mon Feb 16 19:11:28 2015 UTC

# Line 15 | Line 15 | static const char RCSid[] = "$Id$";
15   #include "bsdf_m.h"
16                                          /* XML template file names */
17   const char      def_template[] = "minimalBSDFt.xml";
18 < const char      win6_template[] = "window6BSDFt.xml";
18 > const char      win6_template[] = "WINDOW6BSDFt.xml";
19  
20   const char      stdin_name[] = "<stdin>";
21
21                                          /* input files (can be stdin_name) */
22   const char      *xml_input = NULL;
23                                          /* unit for materials & geometry */
# Line 27 | Line 26 | const char     legal_units[] = "meter|foot|inch|centimeter
26                                          /* system materials & geometry */
27   const char      *mgf_geometry = NULL;
28  
29 <                                        /* angle basis */
29 >                                        /* angle bases */
30   enum { ABdefault=-1, ABklemsFull=0, ABklemsHalf, ABklemsQuarter,
31                  ABtensorTree3, ABtensorTree4, ABend };
32  
33   int             angle_basis = ABdefault;
34  
35 + int             correct_solid_angle = 0;
36 +
37 + const char      *klems_basis_name[] = {
38 +        "LBNL/Klems Full",
39 +        "LBNL/Klems Half",
40 +        "LBNL/Klems Quarter",
41 + };
42                                          /* field IDs and nicknames */
43   struct s_fieldID {
44          char            nickName[4];
45 <        int             has_unit;
45 >        short           has_unit;
46 >        short           win_need;
47          const char      *fullName;
48   } XMLfieldID[] = {
49 <        {"m", 0, "Manufacturer"},
50 <        {"n", 0, "Name"},
51 <        {"c", 0, "ThermalConductivity"},
52 <        {"ef", 0, "EmissivityFront"},
53 <        {"eb", 0, "EmissivityBack"},
54 <        {"tir", 0, "TIR"},
55 <        {"eo", 0, "EffectiveOpennessFraction"},
56 <        {"t", 1, "Thickness"},
57 <        {"h", 1, "Height"},
58 <        {"w", 1, "Width"},
59 <        {"\0", 0, NULL} /* terminator */
49 >        {"m", 0, 1, "Manufacturer"},
50 >        {"n", 0, 1, "Name"},
51 >        {"c", 0, 0, "ThermalConductivity"},
52 >        {"ef", 0, 0, "EmissivityFront"},
53 >        {"eb", 0, 0, "EmissivityBack"},
54 >        {"tir", 0, 0, "TIR"},
55 >        {"eo", 0, 0, "EffectiveOpennessFraction"},
56 >        {"t", 1, 1, "Thickness"},
57 >        {"h", 1, 0, "Height"},
58 >        {"w", 1, 0, "Width"},
59 >        {"\0", 0, 0, NULL}      /* terminator */
60   };
61                                          /* field assignments */
62   #define MAXASSIGN       12
63   const char      *field_assignment[MAXASSIGN];
64   int             nfield_assign = 0;
65   #define FASEP   ';'
59
66                                          /* data file(s) & spectra */
67   enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
68  
# Line 76 | Line 82 | const char     *spectr_file[MAXFILES]; /* custom spectral
82  
83   const char      top_level_name[] = "WindowElement";
84  
85 < static char     *basis_definition[] = {
85 > static char     basis_definition[][256] = {
86  
87 +        "\t<DataDefinition>\n"
88          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
89          "\t\t<AngleBasis>\n"
90          "\t\t\t<AngleBasisName>LBNL/Klems Full</AngleBasisName>\n"
91 <        "\t\t</AngleBasis>\n",
91 >        "\t\t\t</AngleBasis>\n"
92 >        "\t</DataDefinition>\n",
93  
94 +        "\t<DataDefinition>\n"
95          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
96          "\t\t<AngleBasis>\n"
97          "\t\t\t<AngleBasisName>LBNL/Klems Half</AngleBasisName>\n"
98 <        "\t\t</AngleBasis>\n",
98 >        "\t\t\t</AngleBasis>\n"
99 >        "\t</DataDefinition>\n",
100  
101 +        "\t<DataDefinition>\n"
102          "\t\t<IncidentDataStructure>Columns</IncidentDataStructure>\n"
103          "\t\t<AngleBasis>\n"
104          "\t\t\t<AngleBasisName>LBNL/Klems Quarter</AngleBasisName>\n"
105 <        "\t\t</AngleBasis>\n",
105 >        "\t\t\t</AngleBasis>\n"
106 >        "\t</DataDefinition>\n",
107  
108 <        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n",
108 >        "\t<DataDefinition>\n"
109 >        "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n"
110 >        "\t</DataDefinition>\n",
111  
112 <        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n",
112 >        "\t<DataDefinition>\n"
113 >        "\t\t<IncidentDataStructure>TensorTree4</IncidentDataStructure>\n"
114 >        "\t</DataDefinition>\n",
115   };
116  
117   /* Copy data from file descriptor to stdout and close */
# Line 144 | Line 160 | input2str(const char *inpspec)
160                          return "";
161                  }
162                  len = lseek(fd, 0L, SEEK_END);
163 <                if (len < 0) {
164 <                        fprintf(stderr, "%s: seek error\n", inpspec);
163 >                if (len > 0) {
164 >                        lseek(fd, 0L, SEEK_SET);
165 >                        str = (char *)malloc(len+1);
166 >                        if (str == NULL) {
167 >                                close(fd);
168 >                                goto memerr;
169 >                        }
170 >                        if (read(fd, str, len) != len) {
171 >                                fprintf(stderr, "%s: read error\n", inpspec);
172 >                                free(str);
173 >                                close(fd);
174 >                                return "";
175 >                        }
176 >                        str[len] = '\0';
177                          close(fd);
178 <                        return "";
178 >                        return str;
179                  }
180 <                lseek(fd, 0L, SEEK_SET);
153 <                str = (char *)malloc(len+1);
154 <                if (str == NULL) {
155 <                        close(fd);
156 <                        goto memerr;
157 <                }
158 <                if (read(fd, str, len) != len) {
159 <                        fprintf(stderr, "%s: read error\n", inpspec);
160 <                        free(str);
161 <                        close(fd);
162 <                        return "";
163 <                }
164 <                str[len] = '\0';
165 <                close(fd);
166 <                return str;
180 >                fp = fdopen(fd, "r");           /* not a regular file */
181          }
182                                                  /* reading from stream */
183          str = (char *)malloc((len=8192)+1);
# Line 204 | Line 218 | mat_assignments(const char *caller, const char *fn, ez
218   {
219          int     i;
220  
207        if (!nfield_assign)
208                return 1;
221          wtl = ezxml_child(wtl, "Material");
222          if (wtl == NULL) {
223                  fprintf(stderr, "%s: missing <Material> tag\n", fn);
# Line 214 | Line 226 | mat_assignments(const char *caller, const char *fn, ez
226          for (i = 0; i < nfield_assign; i++) {
227                  const char      *fnext = field_assignment[i];
228                  for ( ; ; ) {
229 +                        int     added = 0;
230                          ezxml_t fld;
231                          char    sbuf[512];
232                          int     j;
# Line 222 | Line 235 | mat_assignments(const char *caller, const char *fn, ez
235                                  ++fnext;
236                          if (!*fnext)
237                                  break;
238 <                        for (j = 0; (*fnext != '=') & !isspace(*fnext); ) {
238 >                        for (j = 0; *fnext != '=' && !isspace(*fnext); ) {
239                                  if (!*fnext | (*fnext == FASEP) |
240                                                  (j >= sizeof(sbuf)-1)) {
241                                          fprintf(stderr,
# Line 242 | Line 255 | mat_assignments(const char *caller, const char *fn, ez
255                                                  /* check if tag exists */
256                          fld = ezxml_child(wtl, sbuf);
257                          if (fld == NULL) {      /* otherwise, create one */
258 <                                fprintf(stderr, "%s: warning - adding tag <%s>\n",
259 <                                        fn, sbuf);
258 >                                if (!XMLfieldID[j].nickName[0])
259 >                                        fprintf(stderr,
260 >                                                "%s: warning - adding tag <%s>\n",
261 >                                                        fn, sbuf);
262 >                                ezxml_add_txt(wtl, "\t");
263                                  fld = ezxml_add_child_d(wtl, sbuf, strlen(wtl->txt));
264 +                                ++added;
265                          }
266                          if (XMLfieldID[j].has_unit)
267                                  ezxml_set_attr(fld, "unit", attr_unit);
268 +                        XMLfieldID[j].win_need = 0;
269                          while (isspace(*fnext))
270                                  ++fnext;
271                          if (*fnext++ != '=') {
# Line 256 | Line 274 | mat_assignments(const char *caller, const char *fn, ez
274                                                  caller, field_assignment[i]);
275                                  return 0;
276                          }
277 <                        for (j = 0; *fnext & (*fnext != FASEP); ) {
277 >                        for (j = 0; *fnext && *fnext != FASEP; ) {
278                                  if (j >= sizeof(sbuf)-1) {
279                                          fprintf(stderr,
280                                          "%s: field too long in '%s'\n",
# Line 267 | Line 285 | mat_assignments(const char *caller, const char *fn, ez
285                          }
286                          sbuf[j] = '\0';
287                          ezxml_set_txt_d(fld, sbuf);
288 +                        if (added)
289 +                                ezxml_add_txt(wtl, "\n\t");
290                          fnext += (*fnext == FASEP);
291                  }
292          }
293 +                                        /* check required WINDOW settings */
294 +        if (xml_input == win6_template)
295 +            for (i = 0; XMLfieldID[i].nickName[0]; i++)
296 +                if (XMLfieldID[i].win_need &&
297 +                        !ezxml_txt(ezxml_child(wtl,XMLfieldID[i].fullName))[0]) {
298 +                        fprintf(stderr,
299 +                        "%s: missing required '%s' assignment for WINDOW <%s>\n",
300 +                                        caller, XMLfieldID[i].nickName,
301 +                                        XMLfieldID[i].fullName);
302 +                        return 0;
303 +                }
304          return 1;               /* no errors */
305   }
306  
# Line 298 | Line 329 | finish_angle_basis(ezxml_t ab)
329                  sprintf(buf, "%g", i ?
330                  .5*(abase_list[n].lat[i].tmin + abase_list[n].lat[i+1].tmin) :
331                                  .0);
332 +                ezxml_add_txt(abb, "\n\t\t\t\t");
333                  ezxml_set_txt_d(ezxml_add_child(abb,"Theta",strlen(abb->txt)), buf);
334                  sprintf(buf, "%d", abase_list[n].lat[i].nphis);
335 +                ezxml_add_txt(abb, "\n\t\t\t\t");
336                  ezxml_set_txt_d(ezxml_add_child(abb,"nPhis",strlen(abb->txt)), buf);
337 +                ezxml_add_txt(abb, "\n\t\t\t\t");
338                  tb = ezxml_add_child(abb, "ThetaBounds", strlen(abb->txt));
339 +                ezxml_add_txt(tb, "\n\t\t\t\t\t");
340                  sprintf(buf, "%g", abase_list[n].lat[i].tmin);
341 <                ezxml_set_txt(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
341 >                ezxml_set_txt_d(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
342 >                ezxml_add_txt(tb, "\n\t\t\t\t\t");
343                  sprintf(buf, "%g", abase_list[n].lat[i+1].tmin);
344 <                ezxml_set_txt(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
344 >                ezxml_set_txt_d(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
345 >                ezxml_add_txt(tb, "\n\t\t\t\t");
346 >                ezxml_add_txt(abb, "\n\t\t\t");
347 >                ezxml_add_txt(ab, "\n\t\t\t");
348          }
349          return 1;
350   }
# Line 321 | Line 360 | determine_angle_basis(const char *fn, ezxml_t wtl)
360          if (wtl == NULL)
361                  return -1;
362          ids = ezxml_txt(ezxml_child(wtl, "IncidentDataStructure"));
363 <        if (ids == NULL)
363 >        if (!ids[0])
364                  return -1;
365          for (i = 0; i < ABend; i++) {
366                  ezxml_t parsed = ezxml_parse_str(basis_definition[i],
# Line 344 | Line 383 | determine_angle_basis(const char *fn, ezxml_t wtl)
383          return -1;
384   }
385  
386 + /* Filter Klems angle basis, factoring out incident projected solid angle */
387 + static int
388 + filter_klems_matrix(FILE *fp)
389 + {
390 + #define MAX_COLUMNS     145
391 +        const char      *bn = klems_basis_name[angle_basis];
392 +        float           col_corr[MAX_COLUMNS];
393 +        int             i, j, n = nabases;
394 +                                        /* get angle basis */
395 +        while (n-- > 0)
396 +                if (!strcasecmp(bn, abase_list[n].name))
397 +                        break;
398 +        if (n < 0)
399 +                return 0;
400 +        if (abase_list[n].nangles > MAX_COLUMNS) {
401 +                fputs("Internal error - too many Klems columns!\n", stderr);
402 +                return 0;
403 +        }
404 +                                        /* get correction factors */
405 +        for (j = abase_list[n].nangles; j--; )
406 +                col_corr[j] = 1.f / io_getohm(j, &abase_list[n]);
407 +                                        /* read/correct/write matrix */
408 +        for (i = 0; i < abase_list[n].nangles; i++) {
409 +            for (j = 0; j < abase_list[n].nangles; j++) {
410 +                double  d;
411 +                if (fscanf(fp, "%lf", &d) != 1)
412 +                        return 0;
413 +                if (d < -1e-3) {
414 +                        fputs("Negative BSDF data!\n", stderr);
415 +                        return 0;
416 +                }
417 +                printf(" %.3e", d*col_corr[j]*(d > 0));
418 +            }
419 +            fputc('\n', stdout);
420 +        }
421 +        while ((i = getc(fp)) != EOF)
422 +                if (!isspace(i)) {
423 +                        fputs("Unexpected data past EOF\n", stderr);
424 +                        return 0;
425 +                }
426 +        return 1;                       /* all is good */
427 + #undef MAX_COLUMNS
428 + }
429 +
430   /* Write out BSDF data block with surrounding tags */
431   static int
432   writeBSDFblock(const char *caller, struct s_dfile *df)
433   {
434 +        int     correct_klems = correct_solid_angle;
435          char    *cp;
436  
437          puts("\t<WavelengthData>");
# Line 355 | Line 439 | writeBSDFblock(const char *caller, struct s_dfile *df)
439          switch (df->spectrum) {
440          case DSvisible:
441                  puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
442 <                puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
442 >                puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
443                  puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
444                  break;
445          case DSxbar31:
# Line 413 | Line 497 | writeBSDFblock(const char *caller, struct s_dfile *df)
497          puts("</WavelengthDataDirection>");
498          switch (angle_basis) {
499          case ABklemsFull:
416                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>");
417                break;
500          case ABklemsHalf:
419                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Half</ColumnAngleBasis>");
420                break;
501          case ABklemsQuarter:
502 <                puts("\t\t\t<ColumnAngleBasis>LBNL/Klems Quarter</ColumnAngleBasis>");
502 >                fputs("\t\t\t<ColumnAngleBasis>", stdout);
503 >                fputs(klems_basis_name[angle_basis], stdout);
504 >                puts("</ColumnAngleBasis>");
505 >                fputs("\t\t\t<RowAngleBasis>", stdout);
506 >                fputs(klems_basis_name[angle_basis], stdout);
507 >                puts("</RowAngleBasis>");
508                  break;
509          case ABtensorTree3:
510          case ABtensorTree4:
511                  puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
512 +                correct_klems = 0;
513                  break;
514          default:
515                  fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
# Line 432 | Line 518 | writeBSDFblock(const char *caller, struct s_dfile *df)
518          puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
519          puts("\t\t\t<ScatteringData>");
520          fflush(stdout);
521 <        if (df->fname == stdin_name) {
521 >        if (correct_klems) {                    /* correct Klems matrix data */
522 >                FILE    *fp = stdin;
523 >                if (df->fname[0] == '!')
524 >                        fp = popen(df->fname+1, "r");
525 >                else if (df->fname != stdin_name)
526 >                        fp = fopen(df->fname, "r");
527 >                if (fp == NULL) {
528 >                        fprintf(stderr, "%s: cannot open '%s'\n",
529 >                                        caller, df->fname);
530 >                        return 0;
531 >                }
532 >                if (!filter_klems_matrix(fp)) {
533 >                        fprintf(stderr, "%s: Klems data error from '%s'\n",
534 >                                        caller, df->fname);
535 >                        return 0;
536 >                }
537 >                if (df->fname[0] != '!') {
538 >                        fclose(fp);
539 >                } else if (pclose(fp)) {
540 >                        fprintf(stderr, "%s: error running '%s'\n",
541 >                                        caller, df->fname);
542 >                        return 0;
543 >                }
544 >        } else if (df->fname == stdin_name) {
545                  copy_and_close(fileno(stdin));
546          } else if (df->fname[0] != '!') {
547                  if (!copy_and_close(open(df->fname, O_RDONLY))) {
# Line 479 | Line 588 | writeBSDF(const char *caller, ezxml_t fl)
588                  }
589          fputs(xml+ei, stdout);                  /* write trailer */
590          free(xml);                              /* free string */
591 +        fputc('\n', stdout);
592          return (fflush(stdout) == 0);
593   }
594  
# Line 540 | Line 650 | wrapBSDF(const char *caller)
650                  goto failure;
651          }
652                                          /* make material assignments */
653 <        if (!mat_assignments(caller, xml_path, wtl)) {
653 >        if (!mat_assignments(caller, xml_path, wtl))
654                  goto failure;
545        }
655          if (mgf_geometry != NULL) {     /* add geometry if specified */
656                  ezxml_t geom = ezxml_child(wtl, "Geometry");
657                  if (geom == NULL)
658 <                        geom = ezxml_add_child(wtl, "Geometry", 0);
658 >                        geom = ezxml_add_child(wtl, "Geometry", strlen(wtl->txt));
659                  ezxml_set_attr(geom, "format", "MGF");
660                  geom = ezxml_child(geom, "MGFblock");
661                  if (geom == NULL) {
662                          geom = ezxml_child(wtl, "Geometry");
663 <                        geom = ezxml_add_child(geom, "MGFblock", strlen(geom->txt));
663 >                        geom = ezxml_add_child(geom, "MGFblock", 0);
664                  }
665                  ezxml_set_attr(geom, "unit", attr_unit);
666                  ezxml_set_txt(geom, input2str(mgf_geometry));
# Line 560 | Line 669 | wrapBSDF(const char *caller)
669          }
670                                          /* check basis */
671          if (angle_basis != ABdefault) {
672 +                size_t  offset = 0;
673                  ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition");
674 <                if (dd == NULL) {
675 <                        dd = ezxml_add_child(wtl, "DataDefinition", 0);
676 <                } else {
567 <                        if (ezxml_child(dd, "DataDefinition") != NULL)
674 >                if (dd != NULL) {
675 >                        offset = dd->off;
676 >                        if (dd->child != NULL)
677                                  fprintf(stderr,
678                  "%s: warning - replacing existing <DataDefinition> in '%s'\n",
679                                                  caller, xml_path);
680 <                        while (dd->child != NULL)
681 <                                ezxml_remove(dd->child);
682 <                }
683 <                ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
680 >                        ezxml_remove(dd);
681 >                } else
682 >                        offset = strlen(wtl->txt);
683 >                dd = ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
684                                          strlen(basis_definition[angle_basis])),
685 <                                dd, 0);
685 >                                wtl, offset);
686                  if ((ab = ezxml_child(dd, "AngleBasis")) != NULL &&
687                                  !finish_angle_basis(ab))
688                          goto failure;
# Line 664 | Line 773 | main(int argc, char *argv[])
773                                  angle_basis = ABtensorTree4;
774                          else
775                                  UsageExit(argv[0]);
776 +                        continue;
777 +                case 'c':               /* correct solid angle */
778 +                        correct_solid_angle ^= 1;
779                          continue;
780                  case 't':               /* transmission */
781                          if (i >= argc-1)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines