ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/wrapBSDF.c
Revision: 2.10
Committed: Fri Feb 20 17:05:40 2015 UTC (7 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.9: +4 -6 lines
Log Message:
Turned missing WINDOW6 parameters into warning

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: wrapBSDF.c,v 2.9 2015/02/18 06:18:38 greg Exp $";
3 #endif
4 /*
5 * Wrap BSDF data in valid WINDOW XML file
6 *
7 * G. Ward February 2015
8 */
9
10 #include <ctype.h>
11 #include "rtio.h"
12 #include "rtprocess.h"
13 #include "ezxml.h"
14 #include "bsdf.h"
15 #include "bsdf_m.h"
16 /* XML template file names */
17 const char def_template[] = "minimalBSDFt.xml";
18 const char win6_template[] = "WINDOW6BSDFt.xml";
19
20 const char stdin_name[] = "<stdin>";
21 /* input files (can be stdin_name) */
22 const char *xml_input = NULL;
23 /* unit for materials & geometry */
24 const char *attr_unit = "meter";
25 const char legal_units[] = "meter|foot|inch|centimeter|millimeter";
26 /* system materials & geometry */
27 const char *mgf_geometry = NULL;
28
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 short has_unit;
46 short win_need;
47 const char *fullName;
48 } XMLfieldID[] = {
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 ';'
66 /* data file(s) & spectra */
67 enum { DTtransForward, DTtransBackward, DTreflForward, DTreflBackward };
68
69 enum { DSsolar=-1, DSnir=-2, DSxbar31=-3, DSvisible=-4, DSzbar31=-5 };
70
71 #define MAXFILES 20
72
73 struct s_dfile {
74 const char *fname; /* input data file name */
75 short type; /* BSDF data type */
76 short spectrum; /* BSDF sensor spectrum */
77 } data_file[MAXFILES];
78
79 int ndataf = 0; /* number of data files */
80
81 const char *spectr_file[MAXFILES]; /* custom spectral curve input */
82
83 const char top_level_name[] = "WindowElement";
84
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\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\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\t</AngleBasis>\n"
106 "\t</DataDefinition>\n",
107
108 "\t<DataDefinition>\n"
109 "\t\t<IncidentDataStructure>TensorTree3</IncidentDataStructure>\n"
110 "\t</DataDefinition>\n",
111
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 */
118 static int
119 copy_and_close(int fd)
120 {
121 int ok = 1;
122 char buf[8192];
123 int n;
124
125 if (fd < 0)
126 return 0;
127 while ((n = read(fd, buf, sizeof(buf))) > 0)
128 if (write(fileno(stdout), buf, n) != n) {
129 ok = 0;
130 break;
131 }
132 ok &= (n == 0);
133 close(fd);
134 return ok;
135 }
136
137 /* Allocate and assign string from file or stream */
138 static char *
139 input2str(const char *inpspec)
140 {
141 FILE *fp = NULL;
142 char *str;
143 int len, pos, n;
144
145 if (inpspec == NULL || !*inpspec)
146 return "";
147 if (inpspec == stdin_name) { /* read from stdin */
148 fp = stdin;
149 } else if (inpspec[0] == '!') { /* read from command */
150 fp = popen(inpspec+1, "r");
151 if (fp == NULL) {
152 fprintf(stderr, "Cannot start process '%s'\n",
153 inpspec);
154 return "";
155 }
156 } else { /* else load file */
157 int fd = open(inpspec, O_RDONLY);
158 if (fd < 0) {
159 fprintf(stderr, "%s: cannot open\n", inpspec);
160 return "";
161 }
162 len = lseek(fd, 0L, SEEK_END);
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 str;
179 }
180 fp = fdopen(fd, "r"); /* not a regular file */
181 }
182 /* reading from stream */
183 str = (char *)malloc((len=8192)+1);
184 if (str == NULL)
185 goto memerr;
186 pos = 0;
187 while ((n = read(fileno(fp), str+pos, len-pos)) > 0)
188 if ((pos += n) >= len) { /* need more space? */
189 str = (char *)realloc(str, (len += len>>2) + 1);
190 if (str == NULL)
191 goto memerr;
192 }
193 if (n < 0) {
194 fprintf(stderr, "%s: read error\n", inpspec);
195 free(str);
196 str = "";
197 } else { /* tidy up result */
198 str[pos] = '\0';
199 str = (char *)realloc(str, (len=pos)+1);
200 if (str == NULL)
201 goto memerr;
202 }
203 if (inpspec[0] != '!')
204 fclose(fp);
205 else if (pclose(fp))
206 fprintf(stderr, "Error running command '%s'\n", inpspec);
207 return str;
208 memerr:
209 fprintf(stderr, "%s: error allocating memory\n", inpspec);
210 if (fp != NULL)
211 (inpspec[0] == '!') ? pclose(fp) : fclose(fp);
212 return "";
213 }
214
215 /* Make material assignments in field_assignment to XML fields */
216 static int
217 mat_assignments(const char *caller, const char *fn, ezxml_t wtl)
218 {
219 int i;
220
221 wtl = ezxml_child(wtl, "Material");
222 if (wtl == NULL) {
223 fprintf(stderr, "%s: missing <Material> tag\n", fn);
224 return 0;
225 }
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;
233
234 while (isspace(*fnext))
235 ++fnext;
236 if (!*fnext)
237 break;
238 for (j = 0; *fnext != '=' && !isspace(*fnext); ) {
239 if (!*fnext | (*fnext == FASEP) |
240 (j >= sizeof(sbuf)-1)) {
241 fprintf(stderr,
242 "%s: bad tag name in assignment '%s'\n",
243 caller, field_assignment[i]);
244 return 0;
245 }
246 sbuf[j++] = *fnext++;
247 }
248 sbuf[j] = '\0'; /* check known field */
249 for (j = 0; XMLfieldID[j].nickName[0]; j++)
250 if (!strcasecmp(sbuf, XMLfieldID[j].nickName) ||
251 !strcasecmp(sbuf, XMLfieldID[j].fullName)) {
252 strcpy(sbuf, XMLfieldID[j].fullName);
253 break;
254 }
255 /* check if tag exists */
256 fld = ezxml_child(wtl, sbuf);
257 if (fld == NULL) { /* otherwise, create one */
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++ != '=') {
272 fprintf(stderr,
273 "%s: missing '=' in assignment '%s'\n",
274 caller, field_assignment[i]);
275 return 0;
276 }
277 for (j = 0; *fnext && *fnext != FASEP; ) {
278 if (j >= sizeof(sbuf)-1) {
279 fprintf(stderr,
280 "%s: field too long in '%s'\n",
281 caller, field_assignment[i]);
282 return 0;
283 }
284 sbuf[j++] = *fnext++;
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: warning - missing '%s' assignment for WINDOW <%s>\n",
300 caller, XMLfieldID[i].nickName,
301 XMLfieldID[i].fullName);
302 return 1;
303 }
304
305 /* Complete angle basis specification */
306 static int
307 finish_angle_basis(ezxml_t ab)
308 {
309 const char *bn = ezxml_txt(ezxml_child(ab, "AngleBasisName"));
310 int i, n = nabases;
311 char buf[32];
312
313 if (!*bn) {
314 fputs("Internal error - missing <AngleBasisName>!\n", stderr);
315 return 0;
316 }
317 while (n-- > 0)
318 if (!strcasecmp(bn, abase_list[n].name))
319 break;
320 if (n < 0) {
321 fprintf(stderr, "Internal error - unknown angle basis '%s'", bn);
322 return 0;
323 }
324 for (i = 0; abase_list[n].lat[i].nphis; i++) {
325 ezxml_t tb, abb = ezxml_add_child(ab, "AngleBasisBlock",
326 strlen(ab->txt));
327 sprintf(buf, "%g", i ?
328 .5*(abase_list[n].lat[i].tmin + abase_list[n].lat[i+1].tmin) :
329 .0);
330 ezxml_add_txt(abb, "\n\t\t\t\t");
331 ezxml_set_txt_d(ezxml_add_child(abb,"Theta",strlen(abb->txt)), buf);
332 sprintf(buf, "%d", abase_list[n].lat[i].nphis);
333 ezxml_add_txt(abb, "\n\t\t\t\t");
334 ezxml_set_txt_d(ezxml_add_child(abb,"nPhis",strlen(abb->txt)), buf);
335 ezxml_add_txt(abb, "\n\t\t\t\t");
336 tb = ezxml_add_child(abb, "ThetaBounds", strlen(abb->txt));
337 ezxml_add_txt(tb, "\n\t\t\t\t\t");
338 sprintf(buf, "%g", abase_list[n].lat[i].tmin);
339 ezxml_set_txt_d(ezxml_add_child(tb,"LowerTheta",strlen(tb->txt)), buf);
340 ezxml_add_txt(tb, "\n\t\t\t\t\t");
341 sprintf(buf, "%g", abase_list[n].lat[i+1].tmin);
342 ezxml_set_txt_d(ezxml_add_child(tb,"UpperTheta",strlen(tb->txt)), buf);
343 ezxml_add_txt(tb, "\n\t\t\t\t");
344 ezxml_add_txt(abb, "\n\t\t\t");
345 ezxml_add_txt(ab, "\n\t\t\t");
346 }
347 return 1;
348 }
349
350 /* Determine our angle basis from current tags */
351 static int
352 determine_angle_basis(const char *fn, ezxml_t wtl)
353 {
354 const char *ids;
355 int i;
356
357 wtl = ezxml_child(wtl, "DataDefinition");
358 if (wtl == NULL)
359 return -1;
360 ids = ezxml_txt(ezxml_child(wtl, "IncidentDataStructure"));
361 if (!ids[0])
362 return -1;
363 for (i = 0; i < ABend; i++) {
364 ezxml_t parsed = ezxml_parse_str(basis_definition[i],
365 strlen(basis_definition[i]));
366 int match = 0;
367 if (!strcmp(ids, ezxml_txt(ezxml_child(parsed,
368 "IncidentDataStructure")))) {
369 const char *abn0 = ezxml_txt(
370 ezxml_child(ezxml_child(wtl,
371 "AngleBasis"), "AngleBasisName"));
372 const char *abn1 = ezxml_txt(
373 ezxml_child(ezxml_child(parsed,
374 "AngleBasis"), "AngleBasisName"));
375 match = !strcmp(abn0, abn1);
376 }
377 ezxml_free(parsed);
378 if (match)
379 return i;
380 }
381 return -1;
382 }
383
384 /* Filter Klems angle basis, factoring out incident projected solid angle */
385 static int
386 filter_klems_matrix(FILE *fp)
387 {
388 #define MAX_COLUMNS 145
389 const char *bn = klems_basis_name[angle_basis];
390 float col_corr[MAX_COLUMNS];
391 int i, j, n = nabases;
392 /* get angle basis */
393 while (n-- > 0)
394 if (!strcasecmp(bn, abase_list[n].name))
395 break;
396 if (n < 0)
397 return 0;
398 if (abase_list[n].nangles > MAX_COLUMNS) {
399 fputs("Internal error - too many Klems columns!\n", stderr);
400 return 0;
401 }
402 /* get correction factors */
403 for (j = abase_list[n].nangles; j--; )
404 col_corr[j] = 1.f / io_getohm(j, &abase_list[n]);
405 /* read/correct/write matrix */
406 for (i = 0; i < abase_list[n].nangles; i++) {
407 for (j = 0; j < abase_list[n].nangles; j++) {
408 double d;
409 if (fscanf(fp, "%lf", &d) != 1)
410 return 0;
411 if (d < -1e-3) {
412 fputs("Negative BSDF data!\n", stderr);
413 return 0;
414 }
415 printf(" %.3e", d*col_corr[j]*(d > 0));
416 }
417 fputc('\n', stdout);
418 }
419 while ((i = getc(fp)) != EOF)
420 if (!isspace(i)) {
421 fputs("Unexpected data past EOF\n", stderr);
422 return 0;
423 }
424 return 1; /* all is good */
425 #undef MAX_COLUMNS
426 }
427
428 /* Write out BSDF data block with surrounding tags */
429 static int
430 writeBSDFblock(const char *caller, struct s_dfile *df)
431 {
432 int correct_klems = correct_solid_angle;
433 char *cp;
434
435 puts("\t<WavelengthData>");
436 puts("\t\t<LayerNumber>System</LayerNumber>");
437 switch (df->spectrum) {
438 case DSvisible:
439 puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
440 puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
441 puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
442 break;
443 case DSxbar31:
444 puts("\t\t<Wavelength unit=\"Integral\">CIE-X</Wavelength>");
445 puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
446 puts("\t\t<DetectorSpectrum>ASTM E308 1931 X.dsp</DetectorSpectrum>");
447 break;
448 case DSzbar31:
449 puts("\t\t<Wavelength unit=\"Integral\">CIE-Z</Wavelength>");
450 puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
451 puts("\t\t<DetectorSpectrum>ASTM E308 1931 Z.dsp</DetectorSpectrum>");
452 break;
453 case DSsolar:
454 puts("\t\t<Wavelength unit=\"Integral\">Solar</Wavelength>");
455 puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
456 puts("\t\t<DetectorSpectrum>None</DetectorSpectrum>");
457 break;
458 case DSnir:
459 puts("\t\t<Wavelength unit=\"Integral\">NIR</Wavelength>");
460 puts("\t\tSourceSpectrum>PLACE_HOLDER</SourceSpectrum>");
461 puts("\t\t<DetectorSpectrum>PLACE_HOLDER</DetectorSpectrum>");
462 break;
463 default:
464 cp = strrchr(spectr_file[df->spectrum], '.');
465 if (cp != NULL)
466 *cp = '\0';
467 printf("\t\t<Wavelength unit=\"Integral\">%s</Wavelength>\n",
468 spectr_file[df->spectrum]);
469 if (cp != NULL)
470 *cp = '.';
471 puts("\t\tSourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
472 printf("\t\t<DetectorSpectrum>%s</DetectorSpectrum>\n",
473 spectr_file[df->spectrum]);
474 break;
475 }
476 puts("\t\t<WavelengthDataBlock>");
477 fputs("\t\t\t<WavelengthDataDirection>", stdout);
478 switch (df->type) {
479 case DTtransForward:
480 fputs("Transmission Front", stdout);
481 break;
482 case DTtransBackward:
483 fputs("Transmission Back", stdout);
484 break;
485 case DTreflForward:
486 fputs("Reflection Front", stdout);
487 break;
488 case DTreflBackward:
489 fputs("Reflection Back", stdout);
490 break;
491 default:
492 fprintf(stderr, "%s: internal - bad BSDF type (%d)\n", caller, df->type);
493 return 0;
494 }
495 puts("</WavelengthDataDirection>");
496 switch (angle_basis) {
497 case ABklemsFull:
498 case ABklemsHalf:
499 case ABklemsQuarter:
500 fputs("\t\t\t<ColumnAngleBasis>", stdout);
501 fputs(klems_basis_name[angle_basis], stdout);
502 puts("</ColumnAngleBasis>");
503 fputs("\t\t\t<RowAngleBasis>", stdout);
504 fputs(klems_basis_name[angle_basis], stdout);
505 puts("</RowAngleBasis>");
506 break;
507 case ABtensorTree3:
508 case ABtensorTree4:
509 puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
510 correct_klems = 0;
511 break;
512 default:
513 fprintf(stderr, "%s: bad angle basis (%d)\n", caller, angle_basis);
514 return 0;
515 }
516 puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
517 puts("\t\t\t<ScatteringData>");
518 fflush(stdout);
519 if (correct_klems) { /* correct Klems matrix data */
520 FILE *fp = stdin;
521 if (df->fname[0] == '!')
522 fp = popen(df->fname+1, "r");
523 else if (df->fname != stdin_name)
524 fp = fopen(df->fname, "r");
525 if (fp == NULL) {
526 fprintf(stderr, "%s: cannot open '%s'\n",
527 caller, df->fname);
528 return 0;
529 }
530 if (!filter_klems_matrix(fp)) {
531 fprintf(stderr, "%s: Klems data error from '%s'\n",
532 caller, df->fname);
533 return 0;
534 }
535 if (df->fname[0] != '!') {
536 fclose(fp);
537 } else if (pclose(fp)) {
538 fprintf(stderr, "%s: error running '%s'\n",
539 caller, df->fname);
540 return 0;
541 }
542 } else if (df->fname == stdin_name) {
543 copy_and_close(fileno(stdin));
544 } else if (df->fname[0] != '!') {
545 if (!copy_and_close(open(df->fname, O_RDONLY))) {
546 fprintf(stderr, "%s: error reading from '%s'\n",
547 caller, df->fname);
548 return 0;
549 }
550 } else if (system(df->fname+1)) {
551 fprintf(stderr, "%s: error running '%s'\n", caller, df->fname);
552 return 0;
553 }
554 puts("\t\t\t</ScatteringData>");
555 puts("\t\t</WavelengthDataBlock>");
556 puts("\t</WavelengthData>");
557 return 1;
558 }
559
560 /* Write out XML, interpolating BSDF data block(s) */
561 static int
562 writeBSDF(const char *caller, ezxml_t fl)
563 {
564 char *xml = ezxml_toxml(fl); /* store XML in string */
565 int ei, i;
566 /* locate trailer */
567 for (ei = strlen(xml)-strlen("</Layer></Optical></WindowElement>");
568 ei >= 0; ei--)
569 if (!strncmp(xml+ei, "</Layer>", 8))
570 break;
571 if (ei < 0) {
572 fprintf(stderr, "%s: internal - cannot find trailer\n",
573 caller);
574 free(xml);
575 return 0;
576 }
577 fflush(stdout); /* write previous XML info. */
578 if (write(fileno(stdout), xml, ei) != ei) {
579 free(xml);
580 return 0;
581 }
582 for (i = 0; i < ndataf; i++) /* interpolate new data */
583 if (!writeBSDFblock(caller, &data_file[i])) {
584 free(xml);
585 return 0;
586 }
587 fputs(xml+ei, stdout); /* write trailer */
588 free(xml); /* free string */
589 fputc('\n', stdout);
590 return (fflush(stdout) == 0);
591 }
592
593 /* Insert BSDF data into XML wrapper */
594 static int
595 wrapBSDF(const char *caller)
596 {
597 const char *xml_path = xml_input;
598 ezxml_t fl, wtl;
599 /* load previous XML/template */
600 if (xml_input == stdin_name) {
601 fl = ezxml_parse_fp(stdin);
602 } else if (xml_input[0] == '!') {
603 FILE *pfp = popen(xml_input+1, "r");
604 if (pfp == NULL) {
605 fprintf(stderr, "%s: cannot start process '%s'\n",
606 caller, xml_input);
607 return 0;
608 }
609 fl = ezxml_parse_fp(pfp);
610 if (pclose(pfp)) {
611 fprintf(stderr, "%s: error running '%s'\n",
612 caller, xml_input);
613 return 0;
614 }
615 } else {
616 xml_path = getpath((char *)xml_input, getrlibpath(), R_OK);
617 if (xml_path == NULL) {
618 fprintf(stderr, "%s: cannot find XML file named '%s'\n",
619 caller, xml_input==NULL ? "NULL" : xml_input);
620 return 0;
621 }
622 fl = ezxml_parse_file(xml_path);
623 }
624 if (fl == NULL) {
625 fprintf(stderr, "%s: cannot load XML '%s'\n", caller, xml_path);
626 return 0;
627 }
628 if (ezxml_error(fl)[0]) {
629 fprintf(stderr, "%s: error in XML '%s': %s\n", caller, xml_path,
630 ezxml_error(fl));
631 goto failure;
632 }
633 if (strcmp(ezxml_name(fl), top_level_name)) {
634 fprintf(stderr, "%s: top level in XML '%s' not '%s'\n",
635 caller, xml_path, top_level_name);
636 goto failure;
637 }
638 wtl = ezxml_child(fl, "FileType");
639 if (wtl != NULL && strcmp(ezxml_txt(wtl), "BSDF")) {
640 fprintf(stderr, "%s: wrong FileType in XML '%s' (must be 'BSDF')",
641 caller, xml_path);
642 goto failure;
643 }
644 wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer");
645 if (wtl == NULL) {
646 fprintf(stderr, "%s: no optical layers in XML '%s'",
647 caller, xml_path);
648 goto failure;
649 }
650 /* make material assignments */
651 if (!mat_assignments(caller, xml_path, wtl))
652 goto failure;
653 if (mgf_geometry != NULL) { /* add geometry if specified */
654 ezxml_t geom = ezxml_child(wtl, "Geometry");
655 if (geom == NULL)
656 geom = ezxml_add_child(wtl, "Geometry", strlen(wtl->txt));
657 ezxml_set_attr(geom, "format", "MGF");
658 geom = ezxml_child(geom, "MGFblock");
659 if (geom == NULL) {
660 geom = ezxml_child(wtl, "Geometry");
661 geom = ezxml_add_child(geom, "MGFblock", 0);
662 }
663 ezxml_set_attr(geom, "unit", attr_unit);
664 ezxml_set_txt(geom, input2str(mgf_geometry));
665 if (geom->txt[0])
666 ezxml_set_flag(geom, EZXML_TXTM);
667 }
668 /* check basis */
669 if (angle_basis != ABdefault) {
670 size_t offset = 0;
671 ezxml_t ab, dd = ezxml_child(wtl, "DataDefinition");
672 if (dd != NULL) {
673 offset = dd->off;
674 if (dd->child != NULL)
675 fprintf(stderr,
676 "%s: warning - replacing existing <DataDefinition> in '%s'\n",
677 caller, xml_path);
678 ezxml_remove(dd);
679 } else
680 offset = strlen(wtl->txt);
681 dd = ezxml_insert(ezxml_parse_str(basis_definition[angle_basis],
682 strlen(basis_definition[angle_basis])),
683 wtl, offset);
684 if ((ab = ezxml_child(dd, "AngleBasis")) != NULL &&
685 !finish_angle_basis(ab))
686 goto failure;
687 } else if ((angle_basis = determine_angle_basis(xml_path, wtl)) < 0) {
688 fprintf(stderr, "%s: need -a option to set angle basis\n",
689 caller);
690 goto failure;
691 }
692 /* write & add BSDF data blocks */
693 if (!writeBSDF(caller, fl))
694 goto failure;
695 ezxml_free(fl); /* all done */
696 return 1;
697 failure:
698 ezxml_free(fl);
699 return 0;
700 }
701
702 /* Report usage and exit */
703 static void
704 UsageExit(const char *pname)
705 {
706 fputs("Usage: ", stderr);
707 fputs(pname, stderr);
708 fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr);
709 fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr);
710 fputs(" [input.xml]\n", stderr);
711 exit(1);
712 }
713
714 /* Load XML file and use to wrap BSDF data (or modify fields) */
715 int
716 main(int argc, char *argv[])
717 {
718 int cur_spectrum = DSvisible;
719 int ncust_spec = 0;
720 int used_stdin = 0;
721 int units_set = 0;
722 int i;
723 /* get/check arguments */
724 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
725 switch (argv[i][1]) {
726 case 'W': /* customize for WINDOW 6 output */
727 xml_input = win6_template;
728 continue;
729 case 'f': /* field assignment(s) */
730 if (++i >= argc)
731 UsageExit(argv[0]);
732 if (nfield_assign >= MAXASSIGN) {
733 fprintf(stderr, "%s: too many -f options",
734 argv[0]);
735 return 1;
736 }
737 field_assignment[nfield_assign++] = argv[i];
738 continue;
739 case 'u': /* unit */
740 if (++i >= argc)
741 UsageExit(argv[0]);
742 if (units_set++) {
743 fprintf(stderr, "%s: only one -u option allowed\n",
744 argv[0]);
745 return 1;
746 }
747 if (strstr(legal_units, argv[i]) == NULL) {
748 fprintf(stderr, "%s: -u unit must be one of (%s)\n",
749 argv[0], legal_units);
750 return 1;
751 }
752 attr_unit = argv[i];
753 continue;
754 case 'a': /* angle basis */
755 if (++i >= argc)
756 UsageExit(argv[0]);
757 if (angle_basis != ABdefault) {
758 fprintf(stderr, "%s: only one -a option allowed\n",
759 argv[0]);
760 return 1;
761 }
762 if (!strcasecmp(argv[i], "kf"))
763 angle_basis = ABklemsFull;
764 else if (!strcasecmp(argv[i], "kh"))
765 angle_basis = ABklemsHalf;
766 else if (!strcasecmp(argv[i], "kq"))
767 angle_basis = ABklemsQuarter;
768 else if (!strcasecmp(argv[i], "t3"))
769 angle_basis = ABtensorTree3;
770 else if (!strcasecmp(argv[i], "t4"))
771 angle_basis = ABtensorTree4;
772 else
773 UsageExit(argv[0]);
774 continue;
775 case 'c': /* correct solid angle */
776 correct_solid_angle = 1;
777 continue;
778 case 't': /* transmission */
779 if (i >= argc-1)
780 UsageExit(argv[0]);
781 if (ndataf >= MAXFILES) {
782 fprintf(stderr, "%s: too many data files\n",
783 argv[0]);
784 return 1;
785 }
786 if (!strcmp(argv[i], "-tf"))
787 data_file[ndataf].type = DTtransForward;
788 else if (!strcmp(argv[i], "-tb"))
789 data_file[ndataf].type = DTtransBackward;
790 else
791 UsageExit(argv[0]);
792 if (!strcmp(argv[++i], "-")) {
793 if (used_stdin++) UsageExit(argv[i]);
794 argv[i] = (char *)stdin_name;
795 }
796 data_file[ndataf].fname = argv[i];
797 data_file[ndataf].spectrum = cur_spectrum;
798 ndataf++;
799 continue;
800 case 'r': /* reflection */
801 if (i >= argc-1)
802 UsageExit(argv[0]);
803 if (ndataf >= MAXFILES) {
804 fprintf(stderr, "%s: too many data files\n",
805 argv[0]);
806 return 1;
807 }
808 if (!strcmp(argv[i], "-rf"))
809 data_file[ndataf].type = DTreflForward;
810 else if (!strcmp(argv[i], "-rb"))
811 data_file[ndataf].type = DTreflBackward;
812 else
813 UsageExit(argv[0]);
814 if (!strcmp(argv[++i], "-")) {
815 if (used_stdin++) UsageExit(argv[i]);
816 argv[i] = (char *)stdin_name;
817 }
818 data_file[ndataf].fname = argv[i];
819 data_file[ndataf].spectrum = cur_spectrum;
820 ndataf++;
821 continue;
822 case 's': /* spectrum name or input file */
823 if (++i >= argc)
824 UsageExit(argv[0]);
825 if (!strcasecmp(argv[i], "Solar"))
826 cur_spectrum = DSsolar;
827 else if (!strcasecmp(argv[i], "Visible") ||
828 !strcasecmp(argv[i], "CIE-Y"))
829 cur_spectrum = DSvisible;
830 else if (!strcasecmp(argv[i], "CIE-X"))
831 cur_spectrum = DSxbar31;
832 else if (!strcasecmp(argv[i], "CIE-Z"))
833 cur_spectrum = DSzbar31;
834 else if (!strcasecmp(argv[i], "NIR"))
835 cur_spectrum = DSnir;
836 else {
837 if (!strcmp(argv[i], "-")) {
838 fprintf(stderr,
839 "%s: cannot read spectra from stdin",
840 argv[0]);
841 return 1;
842 }
843 cur_spectrum = ncust_spec;
844 spectr_file[ncust_spec++] = argv[i];
845 }
846 continue;
847 case 'g': /* MGF geometry file */
848 if (i >= argc-1)
849 UsageExit(argv[0]);
850 if (mgf_geometry != NULL) {
851 fprintf(stderr, "%s: only one -g option allowed\n",
852 argv[0]);
853 return 1;
854 }
855 if (!strcmp(argv[++i], "-")) {
856 if (used_stdin++) UsageExit(argv[i]);
857 argv[i] = (char *)stdin_name;
858 }
859 mgf_geometry = argv[i];
860 continue;
861 case '\0': /* input XML from stdin */
862 break;
863 default:
864 UsageExit(argv[0]);
865 break;
866 }
867 break;
868 }
869 doneOptions: /* get XML input */
870 if (i >= argc) {
871 if (xml_input == NULL)
872 xml_input = def_template;
873 } else if ((i < argc-1) | (xml_input != NULL)) {
874 fprintf(stderr, "%s: only one XML input allowed\n", argv[0]);
875 UsageExit(argv[0]);
876 } else if (!strcmp(argv[i], "-")) {
877 if (used_stdin++) UsageExit(argv[0]);
878 xml_input = stdin_name;
879 } else {
880 xml_input = argv[i];
881 }
882 /* wrap it! */
883 return !wrapBSDF(argv[0]);
884 }