ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2ttree.c
(Generate patch)

Comparing ray/src/cv/bsdf2ttree.c (file contents):
Revision 2.10 by greg, Wed Dec 12 04:49:59 2012 UTC vs.
Revision 2.11 by greg, Fri Mar 22 02:15:06 2013 UTC

# Line 12 | Line 12 | static const char RCSid[] = "$Id$";
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;
# Line 20 | Line 21 | double                 pctcull = 90.;
21                                  /* sampling order */
22   int                     samp_order = 6;
23  
24 + /* 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   /* Interpolate and output isotropic BSDF data */
94   static void
95 < interp_isotropic()
95 > eval_isotropic(char *funame)
96   {
97          const int       sqres = 1<<samp_order;
98          FILE            *ofp = NULL;
99          char            cmd[128];
100          int             ix, ox, oy;
101 <        FVECT           ivec, ovec;
101 >        double          iovec[6];
102          float           bsdf;
103   #if DEBUG
104          fprintf(stderr, "Writing isotropic order %d ", samp_order);
105          if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull);
106          else fputs("raw data\n", stderr);
107   #endif
108 <        if (pctcull >= 0) {                     /* begin output */
108 >        data_prologue();                        /* begin output */
109 >        if (pctcull >= 0) {
110                  sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d",
111                                  pctcull, samp_order);
112                  fflush(stdout);
# Line 50 | Line 121 | interp_isotropic()
121                  fputs("{\n", stdout);
122                                                  /* run through directions */
123          for (ix = 0; ix < sqres/2; ix++) {
124 <                RBFNODE *rbf;
125 <                SDsquare2disk(ivec, (ix+.5)/sqres, .5);
126 <                ivec[2] = input_orient *
127 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
128 <                rbf = advect_rbf(ivec);
124 >                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                  for (ox = 0; ox < sqres; ox++)
131                      for (oy = 0; oy < sqres; oy++) {
132 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
133 <                        ovec[2] = output_orient *
134 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
135 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
132 >                        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                          if (pctcull >= 0)
141                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
142                          else
# Line 80 | Line 156 | interp_isotropic()
156                          fputs("\t0\n", stdout);
157                  fputs("}\n", stdout);
158          }
159 +        data_epilogue();
160   }
161  
162   /* Interpolate and output anisotropic BSDF data */
163   static void
164 < interp_anisotropic()
164 > eval_anisotropic(char *funame)
165   {
166          const int       sqres = 1<<samp_order;
167          FILE            *ofp = NULL;
168          char            cmd[128];
169          int             ix, iy, ox, oy;
170 <        FVECT           ivec, ovec;
170 >        double          iovec[6];
171          float           bsdf;
172   #if DEBUG
173          fprintf(stderr, "Writing anisotropic order %d ", samp_order);
174          if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull);
175          else fputs("raw data\n", stderr);
176   #endif
177 <        if (pctcull >= 0) {                     /* begin output */
177 >        data_prologue();                        /* begin output */
178 >        if (pctcull >= 0) {
179                  sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d",
180                                  pctcull, samp_order);
181                  fflush(stdout);
# Line 112 | Line 190 | interp_anisotropic()
190                                                  /* run through directions */
191          for (ix = 0; ix < sqres; ix++)
192              for (iy = 0; iy < sqres; iy++) {
193 <                RBFNODE *rbf;                   /* Klems reversal */
194 <                SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres);
195 <                ivec[0] = -ivec[0]; ivec[1] = -ivec[1];
196 <                ivec[2] = input_orient *
197 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
198 <                rbf = advect_rbf(ivec);
193 >                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                  for (ox = 0; ox < sqres; ox++)
201                      for (oy = 0; oy < sqres; oy++) {
202 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
203 <                        ovec[2] = output_orient *
204 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
205 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
202 >                        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                          if (pctcull >= 0)
211                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
212                          else
# Line 140 | Line 223 | interp_anisotropic()
223                  }
224          } else
225                  fputs("}\n", stdout);
226 +        data_epilogue();
227   }
228  
145 /* Output XML prologue to stdout */
146 static void
147 xml_prologue(int ac, char *av[])
148 {
149        static const char       *bsdf_type[4] = {
150                                        "Reflection Front",
151                                        "Transmission Front",
152                                        "Transmission Back",
153                                        "Reflection Back"
154                                };
155
156        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
157        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\">");
158        fputs("<!-- File produced by:", stdout);
159        while (ac-- > 0) {
160                fputc(' ', stdout);
161                fputs(*av++, stdout);
162        }
163        puts(" -->");
164        puts("<WindowElementType>System</WindowElementType>");
165        puts("<FileType>BSDF</FileType>");
166        puts("<Optical>");
167        puts("<Layer>");
168        puts("\t<Material>");
169        puts("\t\t<Name>Name</Name>");
170        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
171        puts("\t\t<DeviceType>Other</DeviceType>");
172        puts("\t</Material>");
173        puts("\t<DataDefinition>");
174        printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n",
175                        single_plane_incident ? '3' : '4');
176        puts("\t</DataDefinition>");
177        puts("\t<WavelengthData>");
178        puts("\t\t<LayerNumber>System</LayerNumber>");
179        puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
180        puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
181        puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
182        puts("\t\t<WavelengthDataBlock>");
183        printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n",
184                        bsdf_type[(input_orient>0)<<1 | (output_orient>0)]);
185        puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
186        puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
187        puts("\t\t\t<ScatteringData>");
188 }
189
190 /* Output XML epilogue to stdout */
191 static void
192 xml_epilogue(void)
193 {
194        puts("\t\t\t</ScatteringData>");
195        puts("\t\t</WavelengthDataBlock>");
196        puts("\t</WavelengthData>");
197        puts("</Layer>");
198        puts("</Optical>");
199        puts("</WindowElement>");
200 }
201
229   /* Read in BSDF and interpolate as tensor tree representation */
230   int
231   main(int argc, char *argv[])
232   {
233 <        FILE    *fpin = stdin;
234 <        int     i;
233 >        int     dofwd = 0, dobwd = 1;
234 >        int     i, na;
235  
236          progname = argv[0];
237 <        for (i = 1; i < argc-1 && argv[i][0] == '-'; i++)
238 <                switch (argv[i][1]) {           /* get option */
237 >        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                  case 't':
256 <                        pctcull = atof(argv[++i]);
256 >                        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                          break;
270                  case 'g':
271                          samp_order = atoi(argv[++i]);
# Line 218 | Line 273 | main(int argc, char *argv[])
273                  default:
274                          goto userr;
275                  }
276 <
277 <        if (i == argc-1) {                      /* open input if given */
278 <                fpin = fopen(argv[i], "r");
279 <                if (fpin == NULL) {
280 <                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
281 <                                        progname, argv[1]);
282 <                        return(1);
276 >        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 <        } else if (i < argc-1)
290 <                goto userr;
291 <        SET_FILE_BINARY(fpin);                  /* load BSDF interpolant */
292 <        if (!load_bsdf_rep(fpin))
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                  return(1);
234        fclose(fpin);
322          xml_prologue(argc, argv);               /* start XML output */
323          if (single_plane_incident)              /* resample dist. */
324 <                interp_isotropic();
324 >                eval_isotropic(NULL);
325          else
326 <                interp_anisotropic();
327 <        xml_epilogue();                         /* finish XML output */
326 >                eval_anisotropic(NULL);
327 >        xml_epilogue();                         /* finish XML output & exit */
328          return(0);
329   userr:
330          fprintf(stderr,
331 <        "Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n",
331 >        "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                                  progname);
336          return(1);
337   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines