ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2ttree.c
Revision: 2.11
Committed: Fri Mar 22 02:15:06 2013 UTC (11 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.10: +195 -105 lines
Log Message:
Added ability to generate tensor tree directly from function

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.11 static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.10 2012/12/12 04:49:59 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    
24 greg 2.11 /* Output XML prologue to stdout */
25     static void
26     xml_prologue(int ac, char *av[])
27     {
28     puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
29     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\">");
30     fputs("<!-- File produced by:", stdout);
31     while (ac-- > 0) {
32     fputc(' ', stdout);
33     fputs(*av++, stdout);
34     }
35     puts(" -->");
36     puts("<WindowElementType>System</WindowElementType>");
37     puts("<FileType>BSDF</FileType>");
38     puts("<Optical>");
39     puts("<Layer>");
40     puts("\t<Material>");
41     puts("\t\t<Name>Name</Name>");
42     puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
43     puts("\t\t<DeviceType>Other</DeviceType>");
44     puts("\t</Material>");
45     puts("\t<DataDefinition>");
46     printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n",
47     single_plane_incident ? '3' : '4');
48     puts("\t</DataDefinition>");
49     }
50    
51     /* Output XML data prologue to stdout */
52     static void
53     data_prologue()
54     {
55     static const char *bsdf_type[4] = {
56     "Reflection Front",
57     "Transmission Front",
58     "Transmission Back",
59     "Reflection Back"
60     };
61    
62     puts("\t<WavelengthData>");
63     puts("\t\t<LayerNumber>System</LayerNumber>");
64     puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
65     puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
66     puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
67     puts("\t\t<WavelengthDataBlock>");
68     printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n",
69     bsdf_type[(input_orient>0)<<1 | (output_orient>0)]);
70     puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
71     puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
72     puts("\t\t\t<ScatteringData>");
73     }
74    
75     /* Output XML data epilogue to stdout */
76     static void
77     data_epilogue(void)
78     {
79     puts("\t\t\t</ScatteringData>");
80     puts("\t\t</WavelengthDataBlock>");
81     puts("\t</WavelengthData>");
82     }
83    
84     /* Output XML epilogue to stdout */
85     static void
86     xml_epilogue(void)
87     {
88     puts("</Layer>");
89     puts("</Optical>");
90     puts("</WindowElement>");
91     }
92    
93 greg 2.1 /* Interpolate and output isotropic BSDF data */
94     static void
95 greg 2.11 eval_isotropic(char *funame)
96 greg 2.1 {
97     const int sqres = 1<<samp_order;
98     FILE *ofp = NULL;
99     char cmd[128];
100     int ix, ox, oy;
101 greg 2.11 double iovec[6];
102 greg 2.4 float bsdf;
103 greg 2.1 #if DEBUG
104     fprintf(stderr, "Writing isotropic order %d ", samp_order);
105 greg 2.8 if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull);
106 greg 2.1 else fputs("raw data\n", stderr);
107     #endif
108 greg 2.11 data_prologue(); /* begin output */
109     if (pctcull >= 0) {
110 greg 2.7 sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d",
111 greg 2.1 pctcull, samp_order);
112     fflush(stdout);
113     ofp = popen(cmd, "w");
114     if (ofp == NULL) {
115     fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n",
116     progname);
117     exit(1);
118     }
119     SET_FILE_BINARY(ofp);
120     } else
121     fputs("{\n", stdout);
122     /* run through directions */
123     for (ix = 0; ix < sqres/2; ix++) {
124 greg 2.11 RBFNODE *rbf = NULL;
125     iovec[0] = (ix+.5)/sqres - 1.;
126     iovec[1] = .0;
127     iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
128     if (funame == NULL)
129     rbf = advect_rbf(iovec);
130 greg 2.1 for (ox = 0; ox < sqres; ox++)
131     for (oy = 0; oy < sqres; oy++) {
132 greg 2.11 SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
133     iovec[5] = output_orient *
134     sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
135     if (funame == NULL)
136     bsdf = eval_rbfrep(rbf, iovec+3) *
137     output_orient/iovec[5];
138     else
139     bsdf = funvalue(funame, 6, iovec);
140 greg 2.1 if (pctcull >= 0)
141     fwrite(&bsdf, sizeof(bsdf), 1, ofp);
142     else
143     printf("\t%.3e\n", bsdf);
144     }
145 greg 2.3 if (rbf != NULL)
146     free(rbf);
147 greg 2.1 }
148     if (pctcull >= 0) { /* finish output */
149     if (pclose(ofp)) {
150     fprintf(stderr, "%s: error running '%s'\n",
151     progname, cmd);
152     exit(1);
153     }
154     } else {
155     for (ix = sqres*sqres*sqres/2; ix--; )
156     fputs("\t0\n", stdout);
157     fputs("}\n", stdout);
158     }
159 greg 2.11 data_epilogue();
160 greg 2.1 }
161    
162     /* Interpolate and output anisotropic BSDF data */
163     static void
164 greg 2.11 eval_anisotropic(char *funame)
165 greg 2.1 {
166     const int sqres = 1<<samp_order;
167     FILE *ofp = NULL;
168     char cmd[128];
169     int ix, iy, ox, oy;
170 greg 2.11 double iovec[6];
171 greg 2.4 float bsdf;
172 greg 2.1 #if DEBUG
173     fprintf(stderr, "Writing anisotropic order %d ", samp_order);
174 greg 2.8 if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull);
175 greg 2.1 else fputs("raw data\n", stderr);
176     #endif
177 greg 2.11 data_prologue(); /* begin output */
178     if (pctcull >= 0) {
179 greg 2.7 sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d",
180 greg 2.1 pctcull, samp_order);
181     fflush(stdout);
182     ofp = popen(cmd, "w");
183     if (ofp == NULL) {
184     fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n",
185     progname);
186     exit(1);
187     }
188     } else
189     fputs("{\n", stdout);
190     /* run through directions */
191     for (ix = 0; ix < sqres; ix++)
192     for (iy = 0; iy < sqres; iy++) {
193 greg 2.11 RBFNODE *rbf = NULL; /* Klems reversal */
194     SDsquare2disk(iovec, (ix+.5)/sqres, (iy+.5)/sqres);
195     iovec[0] = -iovec[0]; iovec[1] = -iovec[1];
196     iovec[2] = input_orient *
197     sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
198     if (funame == NULL)
199     rbf = advect_rbf(iovec);
200 greg 2.1 for (ox = 0; ox < sqres; ox++)
201     for (oy = 0; oy < sqres; oy++) {
202 greg 2.11 SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
203     iovec[5] = output_orient *
204     sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
205     if (funame == NULL)
206     bsdf = eval_rbfrep(rbf, iovec+3) *
207     output_orient/iovec[5];
208     else
209     bsdf = funvalue(funame, 6, iovec);
210 greg 2.1 if (pctcull >= 0)
211     fwrite(&bsdf, sizeof(bsdf), 1, ofp);
212     else
213     printf("\t%.3e\n", bsdf);
214     }
215 greg 2.3 if (rbf != NULL)
216     free(rbf);
217 greg 2.1 }
218     if (pctcull >= 0) { /* finish output */
219     if (pclose(ofp)) {
220     fprintf(stderr, "%s: error running '%s'\n",
221     progname, cmd);
222     exit(1);
223     }
224     } else
225     fputs("}\n", stdout);
226 greg 2.11 data_epilogue();
227 greg 2.5 }
228    
229 greg 2.1 /* Read in BSDF and interpolate as tensor tree representation */
230     int
231     main(int argc, char *argv[])
232     {
233 greg 2.11 int dofwd = 0, dobwd = 1;
234     int i, na;
235 greg 2.1
236 greg 2.5 progname = argv[0];
237 greg 2.11 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
238     esupport &= ~(E_INCHAN|E_OUTCHAN);
239     scompile("PI:3.14159265358979323846", NULL, 0);
240     biggerlib();
241     for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
242     switch (argv[i][1]) { /* get options */
243     case 'e':
244     scompile(argv[++i], NULL, 0);
245     break;
246     case 'f':
247     if (!argv[i][2])
248     fcompile(argv[++i]);
249     else
250     dofwd = (argv[i][0] == '+');
251     break;
252     case 'b':
253     dobwd = (argv[i][0] == '+');
254     break;
255 greg 2.1 case 't':
256 greg 2.11 switch (argv[i][2]) {
257     case '3':
258     single_plane_incident = 0;
259     break;
260     case '4':
261     single_plane_incident = 1;
262     break;
263     case '\0':
264     pctcull = atof(argv[++i]);
265     break;
266     default:
267     goto userr;
268     }
269 greg 2.1 break;
270     case 'g':
271 greg 2.5 samp_order = atoi(argv[++i]);
272 greg 2.1 break;
273     default:
274     goto userr;
275     }
276 greg 2.11 if (single_plane_incident >= 0) { /* function-based BSDF? */
277     void (*evf)(char *s) = single_plane_incident ?
278     &eval_isotropic : &eval_anisotropic;
279     if (i != argc-1 || fundefined(argv[i]) != 6)
280     goto userr;
281     xml_prologue(argc, argv); /* start XML output */
282     if (dofwd) {
283     input_orient = -1;
284     output_orient = -1;
285     (*evf)(argv[i]); /* outside reflectance */
286     output_orient = 1;
287     (*evf)(argv[i]); /* outside -> inside */
288     }
289     if (dobwd) {
290     input_orient = 1;
291     output_orient = 1;
292     (*evf)(argv[i]); /* inside reflectance */
293     output_orient = -1;
294     (*evf)(argv[i]); /* inside -> outside */
295     }
296     xml_epilogue(); /* finish XML output & exit */
297     return(0);
298     }
299     if (i < argc) { /* open input files if given */
300     xml_prologue(argc, argv); /* start XML output */
301     for ( ; i < argc; i++) { /* interpolate each component */
302     FILE *fpin = fopen(argv[i], "rb");
303     if (fpin == NULL) {
304     fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
305     progname, argv[i]);
306     return(1);
307     }
308     if (!load_bsdf_rep(fpin))
309     return(1);
310     fclose(fpin);
311     if (single_plane_incident)
312     eval_isotropic(NULL);
313     else
314     eval_anisotropic(NULL);
315     }
316     xml_epilogue(); /* finish XML output & exit */
317     return(0);
318     }
319     SET_FILE_BINARY(stdin); /* load from stdin */
320     if (!load_bsdf_rep(stdin))
321 greg 2.1 return(1);
322 greg 2.5 xml_prologue(argc, argv); /* start XML output */
323 greg 2.1 if (single_plane_incident) /* resample dist. */
324 greg 2.11 eval_isotropic(NULL);
325 greg 2.1 else
326 greg 2.11 eval_anisotropic(NULL);
327     xml_epilogue(); /* finish XML output & exit */
328 greg 2.1 return(0);
329     userr:
330     fprintf(stderr,
331 greg 2.11 "Usage: %s [-g Nlog2][-t pctcull] [bsdf.sir ..] > bsdf.xml\n",
332     progname);
333     fprintf(stderr,
334     " or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
335 greg 2.1 progname);
336     return(1);
337     }