ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2ttree.c
Revision: 2.20
Committed: Thu Oct 3 17:27:41 2013 UTC (10 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +2 -3 lines
Log Message:
Re-allowed rttree_reduce -a option for isotropic transmission

File Contents

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