1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdf2klems.c,v 2.6 2013/06/29 21:03:25 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 |
float *trans_mtx = NULL; |
158 |
SDData bsd; |
159 |
SDError ec; |
160 |
FVECT vin, vout; |
161 |
SDValue sv; |
162 |
double sum; |
163 |
int i, j, n; |
164 |
|
165 |
SDclearBSDF(&bsd, fname); /* load BSDF file */ |
166 |
if ((ec = SDloadFile(&bsd, fname)) != SDEnone) |
167 |
goto err; |
168 |
xml_prologue(&bsd); /* pass geometry */ |
169 |
/* front reflection */ |
170 |
if (bsd.rf != NULL || bsd.rLambFront.cieY > .002) { |
171 |
input_orient = 1; output_orient = 1; |
172 |
data_prologue(); |
173 |
for (j = 0; j < abp->nangles; j++) { |
174 |
for (i = 0; i < abp->nangles; i++) { |
175 |
sum = 0; /* average over patches */ |
176 |
for (n = npsamps; n-- > 0; ) { |
177 |
fo_getvec(vout, j+(n+frandom())/npsamps, abp); |
178 |
fi_getvec(vin, i+urand(n), abp); |
179 |
ec = SDevalBSDF(&sv, vout, vin, &bsd); |
180 |
if (ec != SDEnone) |
181 |
goto err; |
182 |
sum += sv.cieY; |
183 |
} |
184 |
printf("\t%.3e\n", sum/npsamps); |
185 |
} |
186 |
putchar('\n'); /* extra space between rows */ |
187 |
} |
188 |
data_epilogue(); |
189 |
} |
190 |
/* back reflection */ |
191 |
if (bsd.rb != NULL || bsd.rLambBack.cieY > .002) { |
192 |
input_orient = -1; output_orient = -1; |
193 |
data_prologue(); |
194 |
for (j = 0; j < abp->nangles; j++) { |
195 |
for (i = 0; i < abp->nangles; i++) { |
196 |
sum = 0; /* average over patches */ |
197 |
for (n = npsamps; n-- > 0; ) { |
198 |
bo_getvec(vout, j+(n+frandom())/npsamps, abp); |
199 |
bi_getvec(vin, i+urand(n), abp); |
200 |
ec = SDevalBSDF(&sv, vout, vin, &bsd); |
201 |
if (ec != SDEnone) |
202 |
goto err; |
203 |
sum += sv.cieY; |
204 |
} |
205 |
printf("\t%.3e\n", sum/npsamps); |
206 |
} |
207 |
putchar('\n'); /* extra space between rows */ |
208 |
} |
209 |
data_epilogue(); |
210 |
} |
211 |
/* front transmission */ |
212 |
if (bsd.tf != NULL || bsd.tLamb.cieY > .002) { |
213 |
if (bsd.tb == NULL) |
214 |
trans_mtx = (float *)malloc(sizeof(float) * |
215 |
abp->nangles*abp->nangles); |
216 |
input_orient = 1; output_orient = -1; |
217 |
data_prologue(); |
218 |
for (j = 0; j < abp->nangles; j++) { |
219 |
for (i = 0; i < abp->nangles; i++) { |
220 |
sum = 0; /* average over patches */ |
221 |
for (n = npsamps; n-- > 0; ) { |
222 |
bo_getvec(vout, j+(n+frandom())/npsamps, abp); |
223 |
fi_getvec(vin, i+urand(n), abp); |
224 |
ec = SDevalBSDF(&sv, vout, vin, &bsd); |
225 |
if (ec != SDEnone) |
226 |
goto err; |
227 |
sum += sv.cieY; |
228 |
} |
229 |
printf("\t%.3e\n", sum/npsamps); |
230 |
if (trans_mtx != NULL) |
231 |
trans_mtx[j*abp->nangles + i] = sum/npsamps; |
232 |
} |
233 |
putchar('\n'); /* extra space between rows */ |
234 |
} |
235 |
data_epilogue(); |
236 |
} |
237 |
/* back transmission */ |
238 |
if (bsd.tb != NULL || trans_mtx != NULL) { |
239 |
if (bsd.tf == NULL) |
240 |
trans_mtx = (float *)malloc(sizeof(float) * |
241 |
abp->nangles*abp->nangles); |
242 |
input_orient = -1; output_orient = 1; |
243 |
data_prologue(); |
244 |
for (j = 0; j < abp->nangles; j++) { |
245 |
for (i = 0; i < abp->nangles; i++) |
246 |
if (bsd.tb != NULL) { /* use tb if we have it */ |
247 |
sum = 0; /* average over patches */ |
248 |
for (n = npsamps; n-- > 0; ) { |
249 |
fo_getvec(vout, j+(n+frandom())/npsamps, abp); |
250 |
bi_getvec(vin, i+urand(n), abp); |
251 |
ec = SDevalBSDF(&sv, vout, vin, &bsd); |
252 |
if (ec != SDEnone) |
253 |
goto err; |
254 |
sum += sv.cieY; |
255 |
} |
256 |
printf("\t%.3e\n", sum/npsamps); |
257 |
if (trans_mtx != NULL) |
258 |
trans_mtx[i*abp->nangles + j] = sum/npsamps; |
259 |
} else { /* else transpose tf */ |
260 |
printf("\t%.3e\n", trans_mtx[i*abp->nangles + j]); |
261 |
} |
262 |
putchar('\n'); /* extra space between rows */ |
263 |
} |
264 |
data_epilogue(); |
265 |
} |
266 |
/* derived front transmission */ |
267 |
if (bsd.tf == NULL && trans_mtx != NULL) { |
268 |
input_orient = 1; output_orient = -1; |
269 |
data_prologue(); |
270 |
for (j = 0; j < abp->nangles; j++) { |
271 |
for (i = 0; i < abp->nangles; i++) |
272 |
printf("\t%.3e\n", trans_mtx[j*abp->nangles + i]); |
273 |
putchar('\n'); /* extra space between rows */ |
274 |
} |
275 |
data_epilogue(); |
276 |
} |
277 |
SDfreeBSDF(&bsd); /* all done */ |
278 |
if (trans_mtx != NULL) |
279 |
free(trans_mtx); |
280 |
return; |
281 |
err: |
282 |
SDreportError(ec, stderr); |
283 |
exit(1); |
284 |
} |
285 |
|
286 |
/* Interpolate and output a BSDF function using Klems basis */ |
287 |
static void |
288 |
eval_function(char *funame) |
289 |
{ |
290 |
ANGLE_BASIS *abp = get_basis(kbasis); |
291 |
double iovec[6]; |
292 |
double sum; |
293 |
int i, j, n; |
294 |
|
295 |
initurand(npsamps); |
296 |
data_prologue(); /* begin output */ |
297 |
for (j = 0; j < abp->nangles; j++) { /* run through directions */ |
298 |
for (i = 0; i < abp->nangles; i++) { |
299 |
sum = 0; |
300 |
for (n = npsamps; n--; ) { /* average over patches */ |
301 |
if (output_orient > 0) |
302 |
fo_getvec(iovec+3, j+(n+frandom())/npsamps, abp); |
303 |
else |
304 |
bo_getvec(iovec+3, j+(n+frandom())/npsamps, abp); |
305 |
|
306 |
if (input_orient > 0) |
307 |
fi_getvec(iovec, i+urand(n), abp); |
308 |
else |
309 |
bi_getvec(iovec, i+urand(n), abp); |
310 |
|
311 |
sum += funvalue(funame, 6, iovec); |
312 |
} |
313 |
printf("\t%.3e\n", sum/npsamps); |
314 |
} |
315 |
putchar('\n'); |
316 |
} |
317 |
data_epilogue(); /* finish output */ |
318 |
} |
319 |
|
320 |
/* Interpolate and output a radial basis function BSDF representation */ |
321 |
static void |
322 |
eval_rbf(void) |
323 |
{ |
324 |
ANGLE_BASIS *abp = get_basis(kbasis); |
325 |
float bsdfarr[MAXPATCHES*MAXPATCHES]; |
326 |
FVECT vin, vout; |
327 |
RBFNODE *rbf; |
328 |
double sum; |
329 |
int i, j, n; |
330 |
/* sanity check */ |
331 |
if (abp->nangles > MAXPATCHES) { |
332 |
fprintf(stderr, "%s: too many patches!\n", progname); |
333 |
exit(1); |
334 |
} |
335 |
data_prologue(); /* begin output */ |
336 |
for (i = 0; i < abp->nangles; i++) { |
337 |
if (input_orient > 0) /* use incident patch center */ |
338 |
fi_getvec(vin, i+.5*(i>0), abp); |
339 |
else |
340 |
bi_getvec(vin, i+.5*(i>0), abp); |
341 |
|
342 |
rbf = advect_rbf(vin); /* compute radial basis func */ |
343 |
|
344 |
for (j = 0; j < abp->nangles; j++) { |
345 |
sum = 0; /* sample over exiting patch */ |
346 |
for (n = npsamps; n--; ) { |
347 |
if (output_orient > 0) |
348 |
fo_getvec(vout, j+(n+frandom())/npsamps, abp); |
349 |
else |
350 |
bo_getvec(vout, j+(n+frandom())/npsamps, abp); |
351 |
|
352 |
sum += eval_rbfrep(rbf, vout) / vout[2]; |
353 |
} |
354 |
bsdfarr[j*abp->nangles + i] = sum*output_orient/npsamps; |
355 |
} |
356 |
if (rbf != NULL) |
357 |
free(rbf); |
358 |
} |
359 |
n = 0; /* write out our matrix */ |
360 |
for (j = 0; j < abp->nangles; j++) { |
361 |
for (i = 0; i < abp->nangles; i++) |
362 |
printf("\t%.3e\n", bsdfarr[n++]); |
363 |
putchar('\n'); |
364 |
} |
365 |
data_epilogue(); /* finish output */ |
366 |
} |
367 |
|
368 |
/* Read in BSDF and interpolate as Klems matrix representation */ |
369 |
int |
370 |
main(int argc, char *argv[]) |
371 |
{ |
372 |
int dofwd = 0, dobwd = 1; |
373 |
char *cp; |
374 |
int i, na; |
375 |
|
376 |
progname = argv[0]; |
377 |
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
378 |
esupport &= ~(E_INCHAN|E_OUTCHAN); |
379 |
scompile("PI:3.14159265358979323846", NULL, 0); |
380 |
biggerlib(); |
381 |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
382 |
switch (argv[i][1]) { /* get options */ |
383 |
case 'n': |
384 |
npsamps = atoi(argv[++i]); |
385 |
if (npsamps <= 0) |
386 |
goto userr; |
387 |
break; |
388 |
case 'e': |
389 |
scompile(argv[++i], NULL, 0); |
390 |
single_plane_incident = 0; |
391 |
break; |
392 |
case 'f': |
393 |
if (!argv[i][2]) { |
394 |
fcompile(argv[++i]); |
395 |
single_plane_incident = 0; |
396 |
} else |
397 |
dofwd = (argv[i][0] == '+'); |
398 |
break; |
399 |
case 'b': |
400 |
dobwd = (argv[i][0] == '+'); |
401 |
break; |
402 |
case 'h': |
403 |
kbasis = "LBNL/Klems Half"; |
404 |
break; |
405 |
case 'q': |
406 |
kbasis = "LBNL/Klems Quarter"; |
407 |
break; |
408 |
default: |
409 |
goto userr; |
410 |
} |
411 |
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
412 |
if (i != argc-1 || fundefined(argv[i]) != 6) { |
413 |
fprintf(stderr, |
414 |
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
415 |
progname); |
416 |
goto userr; |
417 |
} |
418 |
xml_header(argc, argv); /* start XML output */ |
419 |
xml_prologue(NULL); |
420 |
if (dofwd) { |
421 |
input_orient = -1; |
422 |
output_orient = -1; |
423 |
eval_function(argv[i]); /* outside reflectance */ |
424 |
output_orient = 1; |
425 |
eval_function(argv[i]); /* outside -> inside */ |
426 |
} |
427 |
if (dobwd) { |
428 |
input_orient = 1; |
429 |
output_orient = 1; |
430 |
eval_function(argv[i]); /* inside reflectance */ |
431 |
output_orient = -1; |
432 |
eval_function(argv[i]); /* inside -> outside */ |
433 |
} |
434 |
xml_epilogue(); /* finish XML output & exit */ |
435 |
return(0); |
436 |
} |
437 |
/* XML input? */ |
438 |
if (i == argc-1 && (cp = argv[i]+strlen(argv[i])-4) > argv[i] && |
439 |
!strcasecmp(cp, ".xml")) { |
440 |
xml_header(argc, argv); /* start XML output */ |
441 |
eval_bsdf(argv[i]); /* load & resample BSDF */ |
442 |
xml_epilogue(); /* finish XML output & exit */ |
443 |
return(0); |
444 |
} |
445 |
if (i < argc) { /* open input files if given */ |
446 |
int nbsdf = 0; |
447 |
for ( ; i < argc; i++) { /* interpolate each component */ |
448 |
FILE *fpin = fopen(argv[i], "rb"); |
449 |
if (fpin == NULL) { |
450 |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
451 |
progname, argv[i]); |
452 |
return(1); |
453 |
} |
454 |
if (!load_bsdf_rep(fpin)) |
455 |
return(1); |
456 |
fclose(fpin); |
457 |
if (!nbsdf++) { /* start XML on first dist. */ |
458 |
xml_header(argc, argv); |
459 |
xml_prologue(NULL); |
460 |
} |
461 |
eval_rbf(); |
462 |
} |
463 |
xml_epilogue(); /* finish XML output & exit */ |
464 |
return(0); |
465 |
} |
466 |
SET_FILE_BINARY(stdin); /* load from stdin */ |
467 |
if (!load_bsdf_rep(stdin)) |
468 |
return(1); |
469 |
xml_header(argc, argv); /* start XML output */ |
470 |
xml_prologue(NULL); |
471 |
eval_rbf(); /* resample dist. */ |
472 |
xml_epilogue(); /* finish XML output & exit */ |
473 |
return(0); |
474 |
userr: |
475 |
fprintf(stderr, |
476 |
"Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n", progname); |
477 |
fprintf(stderr, |
478 |
" or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n", progname); |
479 |
fprintf(stderr, |
480 |
" or: %s [-n spp][-h|-q][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
481 |
progname); |
482 |
return(1); |
483 |
} |