ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2klems.c
Revision: 2.16
Committed: Wed Oct 1 23:32:42 2014 UTC (9 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2
Changes since 2.15: +1 -2 lines
Log Message:
Eliminated unnecessary calculation (remnant from previous change)

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdf2klems.c,v 2.15 2014/08/21 10:33:48 greg Exp $";
3 #endif
4 /*
5 * Load measured BSDF interpolant and write out as XML file with Klems matrix.
6 *
7 * G. Ward
8 */
9
10 #define _USE_MATH_DEFINES
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15 #include "random.h"
16 #include "platform.h"
17 #include "calcomp.h"
18 #include "bsdfrep.h"
19 #include "bsdf_m.h"
20 /* assumed maximum # Klems patches */
21 #define MAXPATCHES 145
22 /* global argv[0] */
23 char *progname;
24 /* selected basis function name */
25 static const char *kbasis = "LBNL/Klems Full";
26 /* number of BSDF samples per patch */
27 static int npsamps = 256;
28 /* limit on number of RBF lobes */
29 static int lobe_lim = 15000;
30 /* progress bar length */
31 static int do_prog = 79;
32
33
34 /* Start new progress bar */
35 #define prog_start(s) if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else
36
37 /* Draw progress bar of the appropriate length */
38 static void
39 prog_show(double frac)
40 {
41 char pbar[256];
42 int nchars;
43
44 if (do_prog <= 1) return;
45 if (do_prog > sizeof(pbar)-2)
46 do_prog = sizeof(pbar)-2;
47 if (frac < 0) frac = 0;
48 else if (frac > 1) frac = 1;
49 nchars = do_prog*frac + .5;
50 pbar[0] = '\r';
51 memset(pbar+1, '*', nchars);
52 memset(pbar+1+nchars, '-', do_prog-nchars);
53 pbar[do_prog+1] = '\0';
54 fputs(pbar, stderr);
55 }
56
57 /* Finish progress bar */
58 static void
59 prog_done(void)
60 {
61 int n = do_prog;
62
63 if (n <= 1) return;
64 fputc('\r', stderr);
65 while (n--)
66 fputc(' ', stderr);
67 fputc('\r', stderr);
68 }
69
70 /* Return angle basis corresponding to the given name */
71 static ANGLE_BASIS *
72 get_basis(const char *bn)
73 {
74 int n = nabases;
75
76 while (n-- > 0)
77 if (!strcasecmp(bn, abase_list[n].name))
78 return &abase_list[n];
79 return NULL;
80 }
81
82 /* Output XML header to stdout */
83 static void
84 xml_header(int ac, char *av[])
85 {
86 puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
87 puts("<WindowElement xmlns=\"http://windows.lbl.gov\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://windows.lbl.gov/BSDF-v1.4.xsd\">");
88 fputs("<!-- File produced by:", stdout);
89 while (ac-- > 0) {
90 fputc(' ', stdout);
91 fputs(*av++, stdout);
92 }
93 puts(" -->");
94 }
95
96 /* Output XML prologue to stdout */
97 static void
98 xml_prologue(const SDData *sd)
99 {
100 const char *matn = (sd && sd->matn[0]) ? sd->matn :
101 bsdf_name[0] ? bsdf_name : "Unknown";
102 const char *makr = (sd && sd->makr[0]) ? sd->makr :
103 bsdf_manuf[0] ? bsdf_manuf : "Unknown";
104 ANGLE_BASIS *abp = get_basis(kbasis);
105 int i;
106
107 if (abp == NULL) {
108 fprintf(stderr, "%s: unknown angle basis '%s'\n", progname, kbasis);
109 exit(1);
110 }
111 puts("<WindowElementType>System</WindowElementType>");
112 puts("<FileType>BSDF</FileType>");
113 puts("<Optical>");
114 puts("<Layer>");
115 puts("\t<Material>");
116 printf("\t\t<Name>%s</Name>\n", matn);
117 printf("\t\t<Manufacturer>%s</Manufacturer>\n", makr);
118 if (sd && sd->dim[2] > .001) {
119 printf("\t\t<Thickness unit=\"meter\">%.3f</Thickness>\n", sd->dim[2]);
120 printf("\t\t<Width unit=\"meter\">%.3f</Width>\n", sd->dim[0]);
121 printf("\t\t<Height unit=\"meter\">%.3f</Height>\n", sd->dim[1]);
122 }
123 puts("\t\t<DeviceType>Other</DeviceType>");
124 puts("\t</Material>");
125 if (sd && sd->mgf != NULL) {
126 puts("\t<Geometry format=\"MGF\">");
127 puts("\t\t<MGFblock unit=\"meter\">");
128 fputs(sd->mgf, stdout);
129 puts("</MGFblock>");
130 puts("\t</Geometry>");
131 }
132 puts("\t<DataDefinition>");
133 puts("\t\t<IncidentDataStructure>Columns</IncidentDataStructure>");
134 puts("\t\t<AngleBasis>");
135 printf("\t\t\t<AngleBasisName>%s</AngleBasisName>\n", kbasis);
136 for (i = 0; abp->lat[i].nphis; i++) {
137 puts("\t\t\t<AngleBasisBlock>");
138 printf("\t\t\t<Theta>%g</Theta>\n", i ?
139 .5*(abp->lat[i].tmin + abp->lat[i+1].tmin) :
140 .0 );
141 printf("\t\t\t<nPhis>%d</nPhis>\n", abp->lat[i].nphis);
142 puts("\t\t\t<ThetaBounds>");
143 printf("\t\t\t\t<LowerTheta>%g</LowerTheta>\n", abp->lat[i].tmin);
144 printf("\t\t\t\t<UpperTheta>%g</UpperTheta>\n", abp->lat[i+1].tmin);
145 puts("\t\t\t</ThetaBounds>");
146 puts("\t\t\t</AngleBasisBlock>");
147 }
148 puts("\t\t</AngleBasis>");
149 puts("\t</DataDefinition>");
150 }
151
152 /* Output XML data prologue to stdout */
153 static void
154 data_prologue()
155 {
156 static const char *bsdf_type[4] = {
157 "Reflection Front",
158 "Transmission Front",
159 "Transmission Back",
160 "Reflection Back"
161 };
162
163 puts("\t<WavelengthData>");
164 puts("\t\t<LayerNumber>System</LayerNumber>");
165 puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
166 puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
167 puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
168 puts("\t\t<WavelengthDataBlock>");
169 printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n",
170 bsdf_type[(input_orient>0)<<1 | (output_orient>0)]);
171 printf("\t\t\t<ColumnAngleBasis>%s</ColumnAngleBasis>\n", kbasis);
172 printf("\t\t\t<RowAngleBasis>%s</RowAngleBasis>\n", kbasis);
173 puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
174 puts("\t\t\t<ScatteringData>");
175 }
176
177 /* Output XML data epilogue to stdout */
178 static void
179 data_epilogue(void)
180 {
181 puts("\t\t\t</ScatteringData>");
182 puts("\t\t</WavelengthDataBlock>");
183 puts("\t</WavelengthData>");
184 }
185
186 /* Output XML epilogue to stdout */
187 static void
188 xml_epilogue(void)
189 {
190 puts("</Layer>");
191 puts("</Optical>");
192 puts("</WindowElement>");
193 }
194
195 /* Load and resample XML BSDF description using Klems basis */
196 static void
197 eval_bsdf(const char *fname)
198 {
199 ANGLE_BASIS *abp = get_basis(kbasis);
200 SDData bsd;
201 SDError ec;
202 FVECT vin, vout;
203 SDValue sv;
204 double sum;
205 int i, j, n;
206
207 SDclearBSDF(&bsd, fname); /* load BSDF file */
208 if ((ec = SDloadFile(&bsd, fname)) != SDEnone)
209 goto err;
210 xml_prologue(&bsd); /* pass geometry */
211 /* front reflection */
212 if (bsd.rf != NULL || bsd.rLambFront.cieY > .002) {
213 input_orient = 1; output_orient = 1;
214 data_prologue();
215 for (j = 0; j < abp->nangles; j++) {
216 for (i = 0; i < abp->nangles; i++) {
217 sum = 0; /* average over patches */
218 for (n = npsamps; n-- > 0; ) {
219 fo_getvec(vout, j+(n+frandom())/npsamps, abp);
220 fi_getvec(vin, i+urand(n), abp);
221 ec = SDevalBSDF(&sv, vout, vin, &bsd);
222 if (ec != SDEnone)
223 goto err;
224 sum += sv.cieY;
225 }
226 printf("\t%.3e\n", sum/npsamps);
227 }
228 putchar('\n'); /* extra space between rows */
229 }
230 data_epilogue();
231 }
232 /* back reflection */
233 if (bsd.rb != NULL || bsd.rLambBack.cieY > .002) {
234 input_orient = -1; output_orient = -1;
235 data_prologue();
236 for (j = 0; j < abp->nangles; j++) {
237 for (i = 0; i < abp->nangles; i++) {
238 sum = 0; /* average over patches */
239 for (n = npsamps; n-- > 0; ) {
240 bo_getvec(vout, j+(n+frandom())/npsamps, abp);
241 bi_getvec(vin, i+urand(n), abp);
242 ec = SDevalBSDF(&sv, vout, vin, &bsd);
243 if (ec != SDEnone)
244 goto err;
245 sum += sv.cieY;
246 }
247 printf("\t%.3e\n", sum/npsamps);
248 }
249 putchar('\n'); /* extra space between rows */
250 }
251 data_epilogue();
252 }
253 /* front transmission */
254 if (bsd.tf != NULL || bsd.tLamb.cieY > .002) {
255 input_orient = 1; output_orient = -1;
256 data_prologue();
257 for (j = 0; j < abp->nangles; j++) {
258 for (i = 0; i < abp->nangles; i++) {
259 sum = 0; /* average over patches */
260 for (n = npsamps; n-- > 0; ) {
261 bo_getvec(vout, j+(n+frandom())/npsamps, abp);
262 fi_getvec(vin, i+urand(n), abp);
263 ec = SDevalBSDF(&sv, vout, vin, &bsd);
264 if (ec != SDEnone)
265 goto err;
266 sum += sv.cieY;
267 }
268 printf("\t%.3e\n", sum/npsamps);
269 }
270 putchar('\n'); /* extra space between rows */
271 }
272 data_epilogue();
273 }
274 /* back transmission */
275 if ((bsd.tb != NULL) | (bsd.tf != NULL)) {
276 input_orient = -1; output_orient = 1;
277 data_prologue();
278 for (j = 0; j < abp->nangles; j++) {
279 for (i = 0; i < abp->nangles; i++) {
280 sum = 0; /* average over patches */
281 for (n = npsamps; n-- > 0; ) {
282 fo_getvec(vout, j+(n+frandom())/npsamps, abp);
283 bi_getvec(vin, i+urand(n), abp);
284 ec = SDevalBSDF(&sv, vout, vin, &bsd);
285 if (ec != SDEnone)
286 goto err;
287 sum += sv.cieY;
288 }
289 printf("\t%.3e\n", sum/npsamps);
290 }
291 putchar('\n'); /* extra space between rows */
292 }
293 data_epilogue();
294 }
295 SDfreeBSDF(&bsd); /* all done */
296 return;
297 err:
298 SDreportError(ec, stderr);
299 exit(1);
300 }
301
302 /* Interpolate and output a BSDF function using Klems basis */
303 static void
304 eval_function(char *funame)
305 {
306 ANGLE_BASIS *abp = get_basis(kbasis);
307 int assignD = (fundefined(funame) < 6);
308 double iovec[6];
309 double sum;
310 int i, j, n;
311
312 initurand(npsamps);
313 data_prologue(); /* begin output */
314 for (j = 0; j < abp->nangles; j++) { /* run through directions */
315 for (i = 0; i < abp->nangles; i++) {
316 sum = 0;
317 for (n = npsamps; n--; ) { /* average over patches */
318 if (output_orient > 0)
319 fo_getvec(iovec+3, j+(n+frandom())/npsamps, abp);
320 else
321 bo_getvec(iovec+3, j+(n+frandom())/npsamps, abp);
322
323 if (input_orient > 0)
324 fi_getvec(iovec, i+urand(n), abp);
325 else
326 bi_getvec(iovec, i+urand(n), abp);
327
328 if (assignD) {
329 varset("Dx", '=', -iovec[3]);
330 varset("Dy", '=', -iovec[4]);
331 varset("Dz", '=', -iovec[5]);
332 ++eclock;
333 }
334 sum += funvalue(funame, 6, iovec);
335 }
336 printf("\t%.3e\n", sum/npsamps);
337 }
338 putchar('\n');
339 prog_show((j+1.)/abp->nangles);
340 }
341 data_epilogue(); /* finish output */
342 prog_done();
343 }
344
345 /* Interpolate and output a radial basis function BSDF representation */
346 static void
347 eval_rbf(void)
348 {
349 ANGLE_BASIS *abp = get_basis(kbasis);
350 float bsdfarr[MAXPATCHES*MAXPATCHES];
351 FVECT vin, vout;
352 RBFNODE *rbf;
353 double sum;
354 int i, j, n;
355 /* sanity check */
356 if (abp->nangles > MAXPATCHES) {
357 fprintf(stderr, "%s: too many patches!\n", progname);
358 exit(1);
359 }
360 data_prologue(); /* begin output */
361 for (i = 0; i < abp->nangles; i++) {
362 if (input_orient > 0) /* use incident patch center */
363 fi_getvec(vin, i+.5*(i>0), abp);
364 else
365 bi_getvec(vin, i+.5*(i>0), abp);
366
367 rbf = advect_rbf(vin, lobe_lim); /* compute radial basis func */
368
369 for (j = 0; j < abp->nangles; j++) {
370 sum = 0; /* sample over exiting patch */
371 for (n = npsamps; n--; ) {
372 if (output_orient > 0)
373 fo_getvec(vout, j+(n+frandom())/npsamps, abp);
374 else
375 bo_getvec(vout, j+(n+frandom())/npsamps, abp);
376
377 sum += eval_rbfrep(rbf, vout);
378 }
379 bsdfarr[j*abp->nangles + i] = sum / (double)npsamps;
380 }
381 if (rbf != NULL)
382 free(rbf);
383 prog_show((i+1.)/abp->nangles);
384 }
385 n = 0; /* write out our matrix */
386 for (j = 0; j < abp->nangles; j++) {
387 for (i = 0; i < abp->nangles; i++)
388 printf("\t%.3e\n", bsdfarr[n++]);
389 putchar('\n');
390 }
391 data_epilogue(); /* finish output */
392 prog_done();
393 }
394
395 /* Read in BSDF and interpolate as Klems matrix representation */
396 int
397 main(int argc, char *argv[])
398 {
399 int dofwd = 0, dobwd = 1;
400 char *cp;
401 int i, na;
402
403 progname = argv[0];
404 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
405 esupport &= ~(E_INCHAN|E_OUTCHAN);
406 scompile("PI:3.14159265358979323846", NULL, 0);
407 biggerlib();
408 for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
409 switch (argv[i][1]) { /* get options */
410 case 'n':
411 npsamps = atoi(argv[++i]);
412 if (npsamps <= 0)
413 goto userr;
414 break;
415 case 'e':
416 scompile(argv[++i], NULL, 0);
417 single_plane_incident = 0;
418 break;
419 case 'f':
420 if (!argv[i][2]) {
421 fcompile(argv[++i]);
422 single_plane_incident = 0;
423 } else
424 dofwd = (argv[i][0] == '+');
425 break;
426 case 'b':
427 dobwd = (argv[i][0] == '+');
428 break;
429 case 'h':
430 kbasis = "LBNL/Klems Half";
431 break;
432 case 'q':
433 kbasis = "LBNL/Klems Quarter";
434 break;
435 case 'l':
436 lobe_lim = atoi(argv[++i]);
437 break;
438 case 'p':
439 do_prog = atoi(argv[i]+2);
440 break;
441 default:
442 goto userr;
443 }
444 if (single_plane_incident >= 0) { /* function-based BSDF? */
445 if (i != argc-1 || fundefined(argv[i]) != 6) {
446 fprintf(stderr,
447 "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
448 progname);
449 fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
450 goto userr;
451 }
452 ++eclock;
453 xml_header(argc, argv); /* start XML output */
454 xml_prologue(NULL);
455 if (dofwd) {
456 input_orient = -1;
457 output_orient = -1;
458 prog_start("Evaluating outside reflectance");
459 eval_function(argv[i]);
460 output_orient = 1;
461 prog_start("Evaluating outside->inside transmission");
462 eval_function(argv[i]);
463 }
464 if (dobwd) {
465 input_orient = 1;
466 output_orient = 1;
467 prog_start("Evaluating inside reflectance");
468 eval_function(argv[i]);
469 output_orient = -1;
470 prog_start("Evaluating inside->outside transmission");
471 eval_function(argv[i]);
472 }
473 xml_epilogue(); /* finish XML output & exit */
474 return(0);
475 }
476 /* XML input? */
477 if (i == argc-1 && (cp = argv[i]+strlen(argv[i])-4) > argv[i] &&
478 !strcasecmp(cp, ".xml")) {
479 xml_header(argc, argv); /* start XML output */
480 eval_bsdf(argv[i]); /* load & resample BSDF */
481 xml_epilogue(); /* finish XML output & exit */
482 return(0);
483 }
484 if (i < argc) { /* open input files if given */
485 int nbsdf = 0;
486 for ( ; i < argc; i++) { /* interpolate each component */
487 char pbuf[256];
488 FILE *fpin = fopen(argv[i], "rb");
489 if (fpin == NULL) {
490 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
491 progname, argv[i]);
492 return(1);
493 }
494 if (!load_bsdf_rep(fpin))
495 return(1);
496 fclose(fpin);
497 if (!nbsdf++) { /* start XML on first dist. */
498 xml_header(argc, argv);
499 xml_prologue(NULL);
500 }
501 sprintf(pbuf, "Interpolating component '%s'", argv[i]);
502 prog_start(pbuf);
503 eval_rbf();
504 }
505 xml_epilogue(); /* finish XML output & exit */
506 return(0);
507 }
508 SET_FILE_BINARY(stdin); /* load from stdin */
509 if (!load_bsdf_rep(stdin))
510 return(1);
511 xml_header(argc, argv); /* start XML output */
512 xml_prologue(NULL);
513 prog_start("Interpolating from standard input");
514 eval_rbf(); /* resample dist. */
515 xml_epilogue(); /* finish XML output & exit */
516 return(0);
517 userr:
518 fprintf(stderr,
519 "Usage: %s [-n spp][-h|-q][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", progname);
520 fprintf(stderr,
521 " or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n", progname);
522 fprintf(stderr,
523 " or: %s [-n spp][-h|-q][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
524 progname);
525 return(1);
526 }