1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.21 2013/11/09 05:47:49 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Load measured BSDF interpolant and write out as XML file with tensor tree. |
6 |
* |
7 |
* G. Ward |
8 |
*/ |
9 |
|
10 |
#define _USE_MATH_DEFINES |
11 |
#include <stdio.h> |
12 |
#include <stdlib.h> |
13 |
#include <math.h> |
14 |
#include "platform.h" |
15 |
#include "rtprocess.h" |
16 |
#include "calcomp.h" |
17 |
#include "bsdfrep.h" |
18 |
/* global argv[0] */ |
19 |
char *progname; |
20 |
/* percentage to cull (<0 to turn off) */ |
21 |
double pctcull = 90.; |
22 |
/* sampling order */ |
23 |
int samp_order = 6; |
24 |
/* super-sampling threshold */ |
25 |
const double ssamp_thresh = 0.35; |
26 |
/* number of super-samples */ |
27 |
const int nssamp = 100; |
28 |
/* limit on number of RBF lobes */ |
29 |
static int lobe_lim = 15000; |
30 |
|
31 |
/* Output XML prologue to stdout */ |
32 |
static void |
33 |
xml_prologue(int ac, char *av[]) |
34 |
{ |
35 |
puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
36 |
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\">"); |
37 |
fputs("<!-- File produced by:", stdout); |
38 |
while (ac-- > 0) { |
39 |
fputc(' ', stdout); |
40 |
fputs(*av++, stdout); |
41 |
} |
42 |
puts(" -->"); |
43 |
puts("<WindowElementType>System</WindowElementType>"); |
44 |
puts("<FileType>BSDF</FileType>"); |
45 |
puts("<Optical>"); |
46 |
puts("<Layer>"); |
47 |
puts("\t<Material>"); |
48 |
printf("\t\t<Name>%s</Name>\n", bsdf_name[0] ? bsdf_name : "Unknown"); |
49 |
printf("\t\t<Manufacturer>%s</Manufacturer>\n", |
50 |
bsdf_manuf[0] ? bsdf_manuf : "Unknown"); |
51 |
puts("\t\t<DeviceType>Other</DeviceType>"); |
52 |
puts("\t</Material>"); |
53 |
puts("\t<DataDefinition>"); |
54 |
printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n", |
55 |
single_plane_incident ? '3' : '4'); |
56 |
puts("\t</DataDefinition>"); |
57 |
} |
58 |
|
59 |
/* Output XML data prologue to stdout */ |
60 |
static void |
61 |
data_prologue() |
62 |
{ |
63 |
static const char *bsdf_type[4] = { |
64 |
"Reflection Front", |
65 |
"Transmission Front", |
66 |
"Transmission Back", |
67 |
"Reflection Back" |
68 |
}; |
69 |
|
70 |
puts("\t<WavelengthData>"); |
71 |
puts("\t\t<LayerNumber>System</LayerNumber>"); |
72 |
puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>"); |
73 |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
74 |
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>"); |
75 |
puts("\t\t<WavelengthDataBlock>"); |
76 |
printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n", |
77 |
bsdf_type[(input_orient>0)<<1 | (output_orient>0)]); |
78 |
puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>"); |
79 |
puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>"); |
80 |
puts("\t\t\t<ScatteringData>"); |
81 |
} |
82 |
|
83 |
/* Output XML data epilogue to stdout */ |
84 |
static void |
85 |
data_epilogue(void) |
86 |
{ |
87 |
puts("\t\t\t</ScatteringData>"); |
88 |
puts("\t\t</WavelengthDataBlock>"); |
89 |
puts("\t</WavelengthData>"); |
90 |
} |
91 |
|
92 |
/* Output XML epilogue to stdout */ |
93 |
static void |
94 |
xml_epilogue(void) |
95 |
{ |
96 |
puts("</Layer>"); |
97 |
puts("</Optical>"); |
98 |
puts("</WindowElement>"); |
99 |
} |
100 |
|
101 |
/* Compute absolute relative difference */ |
102 |
static double |
103 |
abs_diff(double v1, double v0) |
104 |
{ |
105 |
if ((v0 < 0) | (v1 < 0)) |
106 |
return(.0); |
107 |
v1 = (v1-v0)*2./(v0+v1+.0001); |
108 |
if (v1 < 0) |
109 |
return(-v1); |
110 |
return(v1); |
111 |
} |
112 |
|
113 |
/* Interpolate and output isotropic BSDF data */ |
114 |
static void |
115 |
eval_isotropic(char *funame) |
116 |
{ |
117 |
const int sqres = 1<<samp_order; |
118 |
FILE *ofp = NULL; |
119 |
int assignD = 0; |
120 |
char cmd[128]; |
121 |
int ix, ox, oy; |
122 |
double iovec[6]; |
123 |
float bsdf; |
124 |
|
125 |
data_prologue(); /* begin output */ |
126 |
if (pctcull >= 0) { |
127 |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d", |
128 |
pctcull, samp_order); |
129 |
fflush(stdout); |
130 |
ofp = popen(cmd, "w"); |
131 |
if (ofp == NULL) { |
132 |
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
133 |
progname); |
134 |
exit(1); |
135 |
} |
136 |
SET_FILE_BINARY(ofp); |
137 |
} else |
138 |
fputs("{\n", stdout); |
139 |
/* need to assign Dx, Dy, Dz? */ |
140 |
if (funame != NULL) |
141 |
assignD = (fundefined(funame) < 6); |
142 |
/* run through directions */ |
143 |
for (ix = 0; ix < sqres/2; ix++) { |
144 |
RBFNODE *rbf = NULL; |
145 |
iovec[0] = 2.*(ix+.5)/sqres - 1.; |
146 |
iovec[1] = .0; |
147 |
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
148 |
if (funame == NULL) |
149 |
rbf = advect_rbf(iovec, lobe_lim); |
150 |
for (ox = 0; ox < sqres; ox++) { |
151 |
float last_bsdf = -1; |
152 |
for (oy = 0; oy < sqres; oy++) { |
153 |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
154 |
iovec[5] = output_orient * |
155 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
156 |
if (funame == NULL) |
157 |
bsdf = eval_rbfrep(rbf, iovec+3) * |
158 |
output_orient/iovec[5]; |
159 |
else { |
160 |
double ssa[3], ssvec[6], sum; |
161 |
int ssi; |
162 |
if (assignD) { |
163 |
varset("Dx", '=', -iovec[3]); |
164 |
varset("Dy", '=', -iovec[4]); |
165 |
varset("Dz", '=', -iovec[5]); |
166 |
++eclock; |
167 |
} |
168 |
bsdf = funvalue(funame, 6, iovec); |
169 |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
170 |
sum = 0; /* super-sample voxel */ |
171 |
for (ssi = nssamp; ssi--; ) { |
172 |
SDmultiSamp(ssa, 3, (ssi+drand48())/nssamp); |
173 |
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
174 |
ssvec[1] = .0; |
175 |
ssvec[2] = input_orient * |
176 |
sqrt(1. - ssvec[0]*ssvec[0]); |
177 |
SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres, |
178 |
(oy+ssa[2])/sqres); |
179 |
ssvec[5] = output_orient * |
180 |
sqrt(1. - ssvec[3]*ssvec[3] - |
181 |
ssvec[4]*ssvec[4]); |
182 |
if (assignD) { |
183 |
varset("Dx", '=', -iovec[3]); |
184 |
varset("Dy", '=', -iovec[4]); |
185 |
varset("Dz", '=', -iovec[5]); |
186 |
++eclock; |
187 |
} |
188 |
sum += funvalue(funame, 6, ssvec); |
189 |
} |
190 |
bsdf = sum/nssamp; |
191 |
} |
192 |
} |
193 |
if (pctcull >= 0) |
194 |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
195 |
else |
196 |
printf("\t%.3e\n", bsdf); |
197 |
last_bsdf = bsdf; |
198 |
} |
199 |
} |
200 |
if (rbf != NULL) |
201 |
free(rbf); |
202 |
} |
203 |
if (pctcull >= 0) { /* finish output */ |
204 |
if (pclose(ofp)) { |
205 |
fprintf(stderr, "%s: error running '%s'\n", |
206 |
progname, cmd); |
207 |
exit(1); |
208 |
} |
209 |
} else { |
210 |
for (ix = sqres*sqres*sqres/2; ix--; ) |
211 |
fputs("\t0\n", stdout); |
212 |
fputs("}\n", stdout); |
213 |
} |
214 |
data_epilogue(); |
215 |
} |
216 |
|
217 |
/* Interpolate and output anisotropic BSDF data */ |
218 |
static void |
219 |
eval_anisotropic(char *funame) |
220 |
{ |
221 |
const int sqres = 1<<samp_order; |
222 |
FILE *ofp = NULL; |
223 |
int assignD = 0; |
224 |
char cmd[128]; |
225 |
int ix, iy, ox, oy; |
226 |
double iovec[6]; |
227 |
float bsdf; |
228 |
|
229 |
data_prologue(); /* begin output */ |
230 |
if (pctcull >= 0) { |
231 |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d", |
232 |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
233 |
pctcull, samp_order); |
234 |
fflush(stdout); |
235 |
ofp = popen(cmd, "w"); |
236 |
if (ofp == NULL) { |
237 |
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
238 |
progname); |
239 |
exit(1); |
240 |
} |
241 |
} else |
242 |
fputs("{\n", stdout); |
243 |
/* need to assign Dx, Dy, Dz? */ |
244 |
if (funame != NULL) |
245 |
assignD = (fundefined(funame) < 6); |
246 |
/* run through directions */ |
247 |
for (ix = 0; ix < sqres; ix++) |
248 |
for (iy = 0; iy < sqres; iy++) { |
249 |
RBFNODE *rbf = NULL; /* Klems reversal */ |
250 |
SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres); |
251 |
iovec[2] = input_orient * |
252 |
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
253 |
if (funame == NULL) |
254 |
rbf = advect_rbf(iovec, lobe_lim); |
255 |
for (ox = 0; ox < sqres; ox++) { |
256 |
float last_bsdf = -1; |
257 |
for (oy = 0; oy < sqres; oy++) { |
258 |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
259 |
iovec[5] = output_orient * |
260 |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
261 |
if (funame == NULL) |
262 |
bsdf = eval_rbfrep(rbf, iovec+3) * |
263 |
output_orient/iovec[5]; |
264 |
else { |
265 |
double ssa[4], ssvec[6], sum; |
266 |
int ssi; |
267 |
if (assignD) { |
268 |
varset("Dx", '=', -iovec[3]); |
269 |
varset("Dy", '=', -iovec[4]); |
270 |
varset("Dz", '=', -iovec[5]); |
271 |
++eclock; |
272 |
} |
273 |
bsdf = funvalue(funame, 6, iovec); |
274 |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
275 |
sum = 0; /* super-sample voxel */ |
276 |
for (ssi = nssamp; ssi--; ) { |
277 |
SDmultiSamp(ssa, 4, (ssi+drand48())/nssamp); |
278 |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
279 |
1.-(iy+ssa[1])/sqres); |
280 |
ssvec[2] = output_orient * |
281 |
sqrt(1. - ssvec[0]*ssvec[0] - |
282 |
ssvec[1]*ssvec[1]); |
283 |
SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, |
284 |
(oy+ssa[3])/sqres); |
285 |
ssvec[5] = output_orient * |
286 |
sqrt(1. - ssvec[3]*ssvec[3] - |
287 |
ssvec[4]*ssvec[4]); |
288 |
if (assignD) { |
289 |
varset("Dx", '=', -iovec[3]); |
290 |
varset("Dy", '=', -iovec[4]); |
291 |
varset("Dz", '=', -iovec[5]); |
292 |
++eclock; |
293 |
} |
294 |
sum += funvalue(funame, 6, ssvec); |
295 |
} |
296 |
bsdf = sum/nssamp; |
297 |
} |
298 |
} |
299 |
if (pctcull >= 0) |
300 |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
301 |
else |
302 |
printf("\t%.3e\n", bsdf); |
303 |
last_bsdf = bsdf; |
304 |
} |
305 |
} |
306 |
if (rbf != NULL) |
307 |
free(rbf); |
308 |
} |
309 |
if (pctcull >= 0) { /* finish output */ |
310 |
if (pclose(ofp)) { |
311 |
fprintf(stderr, "%s: error running '%s'\n", |
312 |
progname, cmd); |
313 |
exit(1); |
314 |
} |
315 |
} else |
316 |
fputs("}\n", stdout); |
317 |
data_epilogue(); |
318 |
} |
319 |
|
320 |
/* Read in BSDF and interpolate as tensor tree representation */ |
321 |
int |
322 |
main(int argc, char *argv[]) |
323 |
{ |
324 |
int dofwd = 0, dobwd = 1; |
325 |
int i, na; |
326 |
|
327 |
progname = argv[0]; |
328 |
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
329 |
esupport &= ~(E_INCHAN|E_OUTCHAN); |
330 |
scompile("PI:3.14159265358979323846", NULL, 0); |
331 |
biggerlib(); |
332 |
for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
333 |
switch (argv[i][1]) { /* get options */ |
334 |
case 'e': |
335 |
scompile(argv[++i], NULL, 0); |
336 |
break; |
337 |
case 'f': |
338 |
if (!argv[i][2]) |
339 |
fcompile(argv[++i]); |
340 |
else |
341 |
dofwd = (argv[i][0] == '+'); |
342 |
break; |
343 |
case 'b': |
344 |
dobwd = (argv[i][0] == '+'); |
345 |
break; |
346 |
case 't': |
347 |
switch (argv[i][2]) { |
348 |
case '3': |
349 |
single_plane_incident = 1; |
350 |
break; |
351 |
case '4': |
352 |
single_plane_incident = 0; |
353 |
break; |
354 |
case '\0': |
355 |
pctcull = atof(argv[++i]); |
356 |
break; |
357 |
default: |
358 |
goto userr; |
359 |
} |
360 |
break; |
361 |
case 'g': |
362 |
samp_order = atoi(argv[++i]); |
363 |
break; |
364 |
case 'l': |
365 |
lobe_lim = atoi(argv[++i]); |
366 |
break; |
367 |
default: |
368 |
goto userr; |
369 |
} |
370 |
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
371 |
void (*evf)(char *s) = single_plane_incident ? |
372 |
&eval_isotropic : &eval_anisotropic; |
373 |
if (i != argc-1 || fundefined(argv[i]) < 3) { |
374 |
fprintf(stderr, |
375 |
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
376 |
progname); |
377 |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n", |
378 |
progname); |
379 |
goto userr; |
380 |
} |
381 |
++eclock; |
382 |
xml_prologue(argc, argv); /* start XML output */ |
383 |
if (dofwd) { |
384 |
input_orient = -1; |
385 |
output_orient = -1; |
386 |
(*evf)(argv[i]); /* outside reflectance */ |
387 |
output_orient = 1; |
388 |
(*evf)(argv[i]); /* outside -> inside */ |
389 |
} |
390 |
if (dobwd) { |
391 |
input_orient = 1; |
392 |
output_orient = 1; |
393 |
(*evf)(argv[i]); /* inside reflectance */ |
394 |
output_orient = -1; |
395 |
(*evf)(argv[i]); /* inside -> outside */ |
396 |
} |
397 |
xml_epilogue(); /* finish XML output & exit */ |
398 |
return(0); |
399 |
} |
400 |
if (i < argc) { /* open input files if given */ |
401 |
int nbsdf = 0; |
402 |
for ( ; i < argc; i++) { /* interpolate each component */ |
403 |
FILE *fpin = fopen(argv[i], "rb"); |
404 |
if (fpin == NULL) { |
405 |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
406 |
progname, argv[i]); |
407 |
return(1); |
408 |
} |
409 |
if (!load_bsdf_rep(fpin)) |
410 |
return(1); |
411 |
fclose(fpin); |
412 |
if (!nbsdf++) /* start XML on first dist. */ |
413 |
xml_prologue(argc, argv); |
414 |
if (single_plane_incident) |
415 |
eval_isotropic(NULL); |
416 |
else |
417 |
eval_anisotropic(NULL); |
418 |
} |
419 |
xml_epilogue(); /* finish XML output & exit */ |
420 |
return(0); |
421 |
} |
422 |
SET_FILE_BINARY(stdin); /* load from stdin */ |
423 |
if (!load_bsdf_rep(stdin)) |
424 |
return(1); |
425 |
xml_prologue(argc, argv); /* start XML output */ |
426 |
if (single_plane_incident) /* resample dist. */ |
427 |
eval_isotropic(NULL); |
428 |
else |
429 |
eval_anisotropic(NULL); |
430 |
xml_epilogue(); /* finish XML output & exit */ |
431 |
return(0); |
432 |
userr: |
433 |
fprintf(stderr, |
434 |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
435 |
progname); |
436 |
fprintf(stderr, |
437 |
" or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
438 |
progname); |
439 |
return(1); |
440 |
} |