ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2klems.c
Revision: 2.9
Committed: Sun Aug 11 14:32:34 2013 UTC (10 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +12 -39 lines
Log Message:
Eliminated matrix transpose, which wasn't working anyway

File Contents

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