ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2ttree.c
Revision: 2.18
Committed: Thu Sep 26 17:05:00 2013 UTC (10 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.17: +9 -4 lines
Log Message:
Added -l option to limit maximum number of RBF lobes for interpolation

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.18 static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.17 2013/08/02 20:56:19 greg Exp $";
3 greg 2.1 #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 greg 2.11 #include "calcomp.h"
16 greg 2.1 #include "bsdfrep.h"
17     /* global argv[0] */
18     char *progname;
19     /* percentage to cull (<0 to turn off) */
20 greg 2.7 double pctcull = 90.;
21 greg 2.1 /* sampling order */
22     int samp_order = 6;
23 greg 2.15 /* super-sampling threshold */
24     const double ssamp_thresh = 0.35;
25     /* number of super-samples */
26     const int nssamp = 100;
27 greg 2.18 /* limit on number of RBF lobes */
28     static int lobe_lim = 15000;
29 greg 2.1
30 greg 2.11 /* 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 greg 2.15 /* 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 greg 2.1 /* Interpolate and output isotropic BSDF data */
112     static void
113 greg 2.11 eval_isotropic(char *funame)
114 greg 2.1 {
115     const int sqres = 1<<samp_order;
116     FILE *ofp = NULL;
117 greg 2.17 int assignD = 0;
118 greg 2.1 char cmd[128];
119     int ix, ox, oy;
120 greg 2.11 double iovec[6];
121 greg 2.4 float bsdf;
122 greg 2.15
123 greg 2.11 data_prologue(); /* begin output */
124     if (pctcull >= 0) {
125 greg 2.7 sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d",
126 greg 2.1 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 greg 2.17 /* need to assign Dx, Dy, Dz? */
138     if (funame != NULL)
139     assignD = (fundefined(funame) < 6);
140 greg 2.1 /* run through directions */
141     for (ix = 0; ix < sqres/2; ix++) {
142 greg 2.11 RBFNODE *rbf = NULL;
143 greg 2.14 iovec[0] = 2.*(ix+.5)/sqres - 1.;
144 greg 2.11 iovec[1] = .0;
145     iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
146     if (funame == NULL)
147 greg 2.18 rbf = advect_rbf(iovec, lobe_lim);
148 greg 2.15 for (ox = 0; ox < sqres; ox++) {
149     float last_bsdf = -1;
150 greg 2.1 for (oy = 0; oy < sqres; oy++) {
151 greg 2.11 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 greg 2.15 bsdf = eval_rbfrep(rbf, iovec+3) *
156 greg 2.11 output_orient/iovec[5];
157 greg 2.15 else {
158 greg 2.16 double ssa[3], ssvec[6], sum;
159 greg 2.15 int ssi;
160 greg 2.17 if (assignD) {
161     varset("Dx", '=', -iovec[3]);
162     varset("Dy", '=', -iovec[4]);
163     varset("Dz", '=', -iovec[5]);
164     ++eclock;
165     }
166 greg 2.15 bsdf = funvalue(funame, 6, iovec);
167     if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
168 greg 2.16 sum = 0; /* super-sample voxel */
169 greg 2.15 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 greg 2.17 if (assignD) {
181     varset("Dx", '=', -iovec[3]);
182     varset("Dy", '=', -iovec[4]);
183     varset("Dz", '=', -iovec[5]);
184     ++eclock;
185     }
186 greg 2.16 sum += funvalue(funame, 6, ssvec);
187 greg 2.15 }
188 greg 2.16 bsdf = sum/nssamp;
189 greg 2.15 }
190     }
191 greg 2.1 if (pctcull >= 0)
192     fwrite(&bsdf, sizeof(bsdf), 1, ofp);
193     else
194     printf("\t%.3e\n", bsdf);
195 greg 2.15 last_bsdf = bsdf;
196 greg 2.1 }
197 greg 2.15 }
198 greg 2.3 if (rbf != NULL)
199     free(rbf);
200 greg 2.1 }
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 greg 2.11 data_epilogue();
213 greg 2.1 }
214    
215     /* Interpolate and output anisotropic BSDF data */
216     static void
217 greg 2.11 eval_anisotropic(char *funame)
218 greg 2.1 {
219     const int sqres = 1<<samp_order;
220     FILE *ofp = NULL;
221 greg 2.17 int assignD = 0;
222 greg 2.1 char cmd[128];
223     int ix, iy, ox, oy;
224 greg 2.11 double iovec[6];
225 greg 2.4 float bsdf;
226 greg 2.15
227 greg 2.11 data_prologue(); /* begin output */
228     if (pctcull >= 0) {
229 greg 2.7 sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d",
230 greg 2.1 pctcull, samp_order);
231     fflush(stdout);
232     ofp = popen(cmd, "w");
233     if (ofp == NULL) {
234     fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n",
235     progname);
236     exit(1);
237     }
238     } else
239     fputs("{\n", stdout);
240 greg 2.17 /* need to assign Dx, Dy, Dz? */
241     if (funame != NULL)
242     assignD = (fundefined(funame) < 6);
243 greg 2.1 /* run through directions */
244     for (ix = 0; ix < sqres; ix++)
245     for (iy = 0; iy < sqres; iy++) {
246 greg 2.11 RBFNODE *rbf = NULL; /* Klems reversal */
247 greg 2.15 SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres);
248 greg 2.11 iovec[2] = input_orient *
249     sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
250     if (funame == NULL)
251 greg 2.18 rbf = advect_rbf(iovec, lobe_lim);
252 greg 2.15 for (ox = 0; ox < sqres; ox++) {
253     float last_bsdf = -1;
254 greg 2.1 for (oy = 0; oy < sqres; oy++) {
255 greg 2.11 SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
256     iovec[5] = output_orient *
257     sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
258     if (funame == NULL)
259 greg 2.15 bsdf = eval_rbfrep(rbf, iovec+3) *
260 greg 2.11 output_orient/iovec[5];
261 greg 2.15 else {
262 greg 2.16 double ssa[4], ssvec[6], sum;
263 greg 2.15 int ssi;
264 greg 2.17 if (assignD) {
265     varset("Dx", '=', -iovec[3]);
266     varset("Dy", '=', -iovec[4]);
267     varset("Dz", '=', -iovec[5]);
268     ++eclock;
269     }
270 greg 2.15 bsdf = funvalue(funame, 6, iovec);
271     if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
272 greg 2.16 sum = 0; /* super-sample voxel */
273 greg 2.15 for (ssi = nssamp; ssi--; ) {
274     SDmultiSamp(ssa, 4, (ssi+drand48())/nssamp);
275     SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres,
276     1.-(iy+ssa[1])/sqres);
277     ssvec[2] = output_orient *
278     sqrt(1. - ssvec[0]*ssvec[0] -
279     ssvec[1]*ssvec[1]);
280     SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres,
281     (oy+ssa[3])/sqres);
282     ssvec[5] = output_orient *
283     sqrt(1. - ssvec[3]*ssvec[3] -
284     ssvec[4]*ssvec[4]);
285 greg 2.17 if (assignD) {
286     varset("Dx", '=', -iovec[3]);
287     varset("Dy", '=', -iovec[4]);
288     varset("Dz", '=', -iovec[5]);
289     ++eclock;
290     }
291 greg 2.16 sum += funvalue(funame, 6, ssvec);
292 greg 2.15 }
293 greg 2.16 bsdf = sum/nssamp;
294 greg 2.15 }
295     }
296 greg 2.1 if (pctcull >= 0)
297     fwrite(&bsdf, sizeof(bsdf), 1, ofp);
298     else
299     printf("\t%.3e\n", bsdf);
300 greg 2.15 last_bsdf = bsdf;
301 greg 2.1 }
302 greg 2.15 }
303 greg 2.3 if (rbf != NULL)
304     free(rbf);
305 greg 2.1 }
306     if (pctcull >= 0) { /* finish output */
307     if (pclose(ofp)) {
308     fprintf(stderr, "%s: error running '%s'\n",
309     progname, cmd);
310     exit(1);
311     }
312     } else
313     fputs("}\n", stdout);
314 greg 2.11 data_epilogue();
315 greg 2.5 }
316    
317 greg 2.1 /* Read in BSDF and interpolate as tensor tree representation */
318     int
319     main(int argc, char *argv[])
320     {
321 greg 2.11 int dofwd = 0, dobwd = 1;
322     int i, na;
323 greg 2.1
324 greg 2.5 progname = argv[0];
325 greg 2.11 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
326     esupport &= ~(E_INCHAN|E_OUTCHAN);
327     scompile("PI:3.14159265358979323846", NULL, 0);
328     biggerlib();
329     for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
330     switch (argv[i][1]) { /* get options */
331     case 'e':
332     scompile(argv[++i], NULL, 0);
333     break;
334     case 'f':
335     if (!argv[i][2])
336     fcompile(argv[++i]);
337     else
338     dofwd = (argv[i][0] == '+');
339     break;
340     case 'b':
341     dobwd = (argv[i][0] == '+');
342     break;
343 greg 2.1 case 't':
344 greg 2.11 switch (argv[i][2]) {
345     case '3':
346 greg 2.13 single_plane_incident = 1;
347 greg 2.11 break;
348     case '4':
349 greg 2.13 single_plane_incident = 0;
350 greg 2.11 break;
351     case '\0':
352     pctcull = atof(argv[++i]);
353     break;
354     default:
355     goto userr;
356     }
357 greg 2.1 break;
358     case 'g':
359 greg 2.5 samp_order = atoi(argv[++i]);
360 greg 2.1 break;
361 greg 2.18 case 'l':
362     lobe_lim = atoi(argv[++i]);
363     break;
364 greg 2.1 default:
365     goto userr;
366     }
367 greg 2.11 if (single_plane_incident >= 0) { /* function-based BSDF? */
368     void (*evf)(char *s) = single_plane_incident ?
369     &eval_isotropic : &eval_anisotropic;
370 greg 2.17 if (i != argc-1 || fundefined(argv[i]) < 3) {
371 greg 2.12 fprintf(stderr,
372     "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
373     progname);
374 greg 2.17 fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n",
375     progname);
376 greg 2.11 goto userr;
377 greg 2.12 }
378 greg 2.17 ++eclock;
379 greg 2.11 xml_prologue(argc, argv); /* start XML output */
380     if (dofwd) {
381     input_orient = -1;
382     output_orient = -1;
383     (*evf)(argv[i]); /* outside reflectance */
384     output_orient = 1;
385     (*evf)(argv[i]); /* outside -> inside */
386     }
387     if (dobwd) {
388     input_orient = 1;
389     output_orient = 1;
390     (*evf)(argv[i]); /* inside reflectance */
391     output_orient = -1;
392     (*evf)(argv[i]); /* inside -> outside */
393     }
394     xml_epilogue(); /* finish XML output & exit */
395     return(0);
396     }
397     if (i < argc) { /* open input files if given */
398 greg 2.12 int nbsdf = 0;
399 greg 2.11 for ( ; i < argc; i++) { /* interpolate each component */
400     FILE *fpin = fopen(argv[i], "rb");
401     if (fpin == NULL) {
402     fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
403     progname, argv[i]);
404     return(1);
405     }
406     if (!load_bsdf_rep(fpin))
407     return(1);
408     fclose(fpin);
409 greg 2.12 if (!nbsdf++) /* start XML on first dist. */
410     xml_prologue(argc, argv);
411 greg 2.11 if (single_plane_incident)
412     eval_isotropic(NULL);
413     else
414     eval_anisotropic(NULL);
415     }
416     xml_epilogue(); /* finish XML output & exit */
417     return(0);
418     }
419     SET_FILE_BINARY(stdin); /* load from stdin */
420     if (!load_bsdf_rep(stdin))
421 greg 2.1 return(1);
422 greg 2.5 xml_prologue(argc, argv); /* start XML output */
423 greg 2.1 if (single_plane_incident) /* resample dist. */
424 greg 2.11 eval_isotropic(NULL);
425 greg 2.1 else
426 greg 2.11 eval_anisotropic(NULL);
427     xml_epilogue(); /* finish XML output & exit */
428 greg 2.1 return(0);
429     userr:
430     fprintf(stderr,
431 greg 2.18 "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n",
432 greg 2.11 progname);
433     fprintf(stderr,
434     " or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
435 greg 2.1 progname);
436     return(1);
437     }