ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2ttree.c
Revision: 2.25
Committed: Tue Mar 11 19:37:45 2014 UTC (10 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.24: +8 -1 lines
Log Message:
Added flockfile calls to reduce i/o overhead

File Contents

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