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.3 by greg, Tue Oct 23 05:10:42 2012 UTC vs.
Revision 2.19 by greg, Thu Oct 3 17:01:02 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;
19                                  /* percentage to cull (<0 to turn off) */
20 < int                     pctcull = 90;
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 < interp_isotropic()
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 <        FVECT           ivec, ovec;
121 <        double          bsdf;
122 < #if DEBUG
123 <        fprintf(stderr, "Writing isotropic order %d ", samp_order);
124 <        if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull);
125 <        else fputs("raw data\n", stderr);
126 < #endif
38 <        if (pctcull >= 0) {                     /* begin output */
39 <                sprintf(cmd, "rttree_reduce -h -a -fd -r 3 -t %d -g %d",
120 >        double          iovec[6];
121 >        float           bsdf;
122 >
123 >        data_prologue();                        /* begin output */
124 >        if (pctcull >= 0) {
125 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d",
126 >                                (input_orient>0 ^ output_orient>0) ? "" : " -a",
127                                  pctcull, samp_order);
128                  fflush(stdout);
129                  ofp = popen(cmd, "w");
# Line 48 | Line 135 | interp_isotropic()
135                  SET_FILE_BINARY(ofp);
136          } else
137                  fputs("{\n", stdout);
138 +                                                /* need to assign Dx, Dy, Dz? */
139 +        if (funame != NULL)
140 +                assignD = (fundefined(funame) < 6);
141                                                  /* run through directions */
142          for (ix = 0; ix < sqres/2; ix++) {
143 <                RBFNODE *rbf;
144 <                SDsquare2disk(ivec, (ix+.5)/sqres, .5);
145 <                ivec[2] = input_orient *
146 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
147 <                rbf = advect_rbf(ivec);
148 <                for (ox = 0; ox < sqres; ox++)
143 >                RBFNODE *rbf = NULL;
144 >                iovec[0] = 2.*(ix+.5)/sqres - 1.;
145 >                iovec[1] = .0;
146 >                iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
147 >                if (funame == NULL)
148 >                        rbf = advect_rbf(iovec, lobe_lim);
149 >                for (ox = 0; ox < sqres; ox++) {
150 >                    float       last_bsdf = -1;
151                      for (oy = 0; oy < sqres; oy++) {
152 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
153 <                        ovec[2] = output_orient *
154 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
155 <                        bsdf = eval_rbfrep(rbf, ovec) / fabs(ovec[2]);
152 >                        SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
153 >                        iovec[5] = output_orient *
154 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
155 >                        if (funame == NULL)
156 >                            bsdf = eval_rbfrep(rbf, iovec+3) *
157 >                                                output_orient/iovec[5];
158 >                        else {
159 >                            double      ssa[3], ssvec[6], sum;
160 >                            int         ssi;
161 >                            if (assignD) {
162 >                                varset("Dx", '=', -iovec[3]);
163 >                                varset("Dy", '=', -iovec[4]);
164 >                                varset("Dz", '=', -iovec[5]);
165 >                                ++eclock;
166 >                            }
167 >                            bsdf = funvalue(funame, 6, iovec);
168 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
169 >                                sum = 0;        /* super-sample voxel */
170 >                                for (ssi = nssamp; ssi--; ) {
171 >                                    SDmultiSamp(ssa, 3, (ssi+drand48())/nssamp);
172 >                                    ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.;
173 >                                    ssvec[1] = .0;
174 >                                    ssvec[2] = input_orient *
175 >                                                sqrt(1. - ssvec[0]*ssvec[0]);
176 >                                    SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres,
177 >                                                (oy+ssa[2])/sqres);
178 >                                    ssvec[5] = output_orient *
179 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
180 >                                                        ssvec[4]*ssvec[4]);
181 >                                    if (assignD) {
182 >                                        varset("Dx", '=', -iovec[3]);
183 >                                        varset("Dy", '=', -iovec[4]);
184 >                                        varset("Dz", '=', -iovec[5]);
185 >                                        ++eclock;
186 >                                    }
187 >                                    sum += funvalue(funame, 6, ssvec);
188 >                                }
189 >                                bsdf = sum/nssamp;
190 >                            }
191 >                        }
192                          if (pctcull >= 0)
193                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
194                          else
195                                  printf("\t%.3e\n", bsdf);
196 +                        last_bsdf = bsdf;
197                      }
198 +                }
199                  if (rbf != NULL)
200                          free(rbf);
201          }
# Line 80 | Line 210 | interp_isotropic()
210                          fputs("\t0\n", stdout);
211                  fputs("}\n", stdout);
212          }
213 +        data_epilogue();
214   }
215  
216   /* Interpolate and output anisotropic BSDF data */
217   static void
218 < interp_anisotropic()
218 > eval_anisotropic(char *funame)
219   {
220          const int       sqres = 1<<samp_order;
221          FILE            *ofp = NULL;
222 +        int             assignD = 0;
223          char            cmd[128];
224          int             ix, iy, ox, oy;
225 <        FVECT           ivec, ovec;
226 <        double          bsdf;
227 < #if DEBUG
228 <        fprintf(stderr, "Writing anisotropic order %d ", samp_order);
229 <        if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull);
230 <        else fputs("raw data\n", stderr);
231 < #endif
100 <        if (pctcull >= 0) {                     /* begin output */
101 <                sprintf(cmd, "rttree_reduce -h -a -fd -r 4 -t %d -g %d",
225 >        double          iovec[6];
226 >        float           bsdf;
227 >
228 >        data_prologue();                        /* begin output */
229 >        if (pctcull >= 0) {
230 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d",
231 >                                (input_orient>0 ^ output_orient>0) ? "" : " -a",
232                                  pctcull, samp_order);
233                  fflush(stdout);
234                  ofp = popen(cmd, "w");
# Line 109 | Line 239 | interp_anisotropic()
239                  }
240          } else
241                  fputs("{\n", stdout);
242 +                                                /* need to assign Dx, Dy, Dz? */
243 +        if (funame != NULL)
244 +                assignD = (fundefined(funame) < 6);
245                                                  /* run through directions */
246          for (ix = 0; ix < sqres; ix++)
247              for (iy = 0; iy < sqres; iy++) {
248 <                RBFNODE *rbf;
249 <                SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres);
250 <                ivec[2] = input_orient *
251 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
252 <                rbf = advect_rbf(ivec);
253 <                for (ox = 0; ox < sqres; ox++)
248 >                RBFNODE *rbf = NULL;            /* Klems reversal */
249 >                SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres);
250 >                iovec[2] = input_orient *
251 >                                sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
252 >                if (funame == NULL)
253 >                        rbf = advect_rbf(iovec, lobe_lim);
254 >                for (ox = 0; ox < sqres; ox++) {
255 >                    float       last_bsdf = -1;
256                      for (oy = 0; oy < sqres; oy++) {
257 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
258 <                        ovec[2] = output_orient *
259 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
260 <                        bsdf = eval_rbfrep(rbf, ovec) / fabs(ovec[2]);
257 >                        SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
258 >                        iovec[5] = output_orient *
259 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
260 >                        if (funame == NULL)
261 >                            bsdf = eval_rbfrep(rbf, iovec+3) *
262 >                                                output_orient/iovec[5];
263 >                        else {
264 >                            double      ssa[4], ssvec[6], sum;
265 >                            int         ssi;
266 >                            if (assignD) {
267 >                                varset("Dx", '=', -iovec[3]);
268 >                                varset("Dy", '=', -iovec[4]);
269 >                                varset("Dz", '=', -iovec[5]);
270 >                                ++eclock;
271 >                            }
272 >                            bsdf = funvalue(funame, 6, iovec);
273 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
274 >                                sum = 0;        /* super-sample voxel */
275 >                                for (ssi = nssamp; ssi--; ) {
276 >                                    SDmultiSamp(ssa, 4, (ssi+drand48())/nssamp);
277 >                                    SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres,
278 >                                                1.-(iy+ssa[1])/sqres);
279 >                                    ssvec[2] = output_orient *
280 >                                                sqrt(1. - ssvec[0]*ssvec[0] -
281 >                                                        ssvec[1]*ssvec[1]);
282 >                                    SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres,
283 >                                                (oy+ssa[3])/sqres);
284 >                                    ssvec[5] = output_orient *
285 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
286 >                                                        ssvec[4]*ssvec[4]);
287 >                                    if (assignD) {
288 >                                        varset("Dx", '=', -iovec[3]);
289 >                                        varset("Dy", '=', -iovec[4]);
290 >                                        varset("Dz", '=', -iovec[5]);
291 >                                        ++eclock;
292 >                                    }
293 >                                    sum += funvalue(funame, 6, ssvec);
294 >                                }
295 >                                bsdf = sum/nssamp;
296 >                            }
297 >                        }
298                          if (pctcull >= 0)
299                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
300                          else
301                                  printf("\t%.3e\n", bsdf);
302 +                        last_bsdf = bsdf;
303                      }
304 +                }
305                  if (rbf != NULL)
306                          free(rbf);
307              }
# Line 139 | Line 313 | interp_anisotropic()
313                  }
314          } else
315                  fputs("}\n", stdout);
316 +        data_epilogue();
317   }
318  
319   /* Read in BSDF and interpolate as tensor tree representation */
320   int
321   main(int argc, char *argv[])
322   {
323 <        FILE    *fpin = stdin;
324 <        int     i;
323 >        int     dofwd = 0, dobwd = 1;
324 >        int     i, na;
325  
326 <        progname = argv[0];                     /* get options */
327 <        while (argc > 2 && argv[1][0] == '-') {
328 <                switch (argv[1][1]) {
326 >        progname = argv[0];
327 >        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
328 >        esupport &= ~(E_INCHAN|E_OUTCHAN);
329 >        scompile("PI:3.14159265358979323846", NULL, 0);
330 >        biggerlib();
331 >        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
332 >                switch (argv[i][1]) {           /* get options */
333 >                case 'e':
334 >                        scompile(argv[++i], NULL, 0);
335 >                        break;
336 >                case 'f':
337 >                        if (!argv[i][2])
338 >                                fcompile(argv[++i]);
339 >                        else
340 >                                dofwd = (argv[i][0] == '+');
341 >                        break;
342 >                case 'b':
343 >                        dobwd = (argv[i][0] == '+');
344 >                        break;
345                  case 't':
346 <                        pctcull = atoi(argv[2]);
346 >                        switch (argv[i][2]) {
347 >                        case '3':
348 >                                single_plane_incident = 1;
349 >                                break;
350 >                        case '4':
351 >                                single_plane_incident = 0;
352 >                                break;
353 >                        case '\0':
354 >                                pctcull = atof(argv[++i]);
355 >                                break;
356 >                        default:
357 >                                goto userr;
358 >                        }
359                          break;
360                  case 'g':
361 <                        samp_order = atoi(argv[2]);
361 >                        samp_order = atoi(argv[++i]);
362                          break;
363 +                case 'l':
364 +                        lobe_lim = atoi(argv[++i]);
365 +                        break;
366                  default:
367                          goto userr;
368                  }
369 <                argv += 2; argc -= 2;
369 >        if (single_plane_incident >= 0) {       /* function-based BSDF? */
370 >                void    (*evf)(char *s) = single_plane_incident ?
371 >                                &eval_isotropic : &eval_anisotropic;
372 >                if (i != argc-1 || fundefined(argv[i]) < 3) {
373 >                        fprintf(stderr,
374 >        "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
375 >                                        progname);
376 >                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n",
377 >                                        progname);
378 >                        goto userr;
379 >                }
380 >                ++eclock;
381 >                xml_prologue(argc, argv);       /* start XML output */
382 >                if (dofwd) {
383 >                        input_orient = -1;
384 >                        output_orient = -1;
385 >                        (*evf)(argv[i]);        /* outside reflectance */
386 >                        output_orient = 1;
387 >                        (*evf)(argv[i]);        /* outside -> inside */
388 >                }
389 >                if (dobwd) {
390 >                        input_orient = 1;
391 >                        output_orient = 1;
392 >                        (*evf)(argv[i]);        /* inside reflectance */
393 >                        output_orient = -1;
394 >                        (*evf)(argv[i]);        /* inside -> outside */
395 >                }
396 >                xml_epilogue();                 /* finish XML output & exit */
397 >                return(0);
398          }
399 <        if (argc == 2) {                        /* open input if given */
400 <                fpin = fopen(argv[1], "r");
401 <                if (fpin == NULL) {
402 <                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
403 <                                        progname, argv[1]);
404 <                        return(1);
399 >        if (i < argc) {                         /* open input files if given */
400 >                int     nbsdf = 0;
401 >                for ( ; i < argc; i++) {        /* interpolate each component */
402 >                        FILE    *fpin = fopen(argv[i], "rb");
403 >                        if (fpin == NULL) {
404 >                                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
405 >                                                progname, argv[i]);
406 >                                return(1);
407 >                        }
408 >                        if (!load_bsdf_rep(fpin))
409 >                                return(1);
410 >                        fclose(fpin);
411 >                        if (!nbsdf++)           /* start XML on first dist. */
412 >                                xml_prologue(argc, argv);
413 >                        if (single_plane_incident)
414 >                                eval_isotropic(NULL);
415 >                        else
416 >                                eval_anisotropic(NULL);
417                  }
418 <        } else if (argc != 1)
419 <                goto userr;
420 <        SET_FILE_BINARY(fpin);                  /* load BSDF interpolant */
421 <        if (!load_bsdf_rep(fpin))
418 >                xml_epilogue();                 /* finish XML output & exit */
419 >                return(0);
420 >        }
421 >        SET_FILE_BINARY(stdin);                 /* load from stdin */
422 >        if (!load_bsdf_rep(stdin))
423                  return(1);
424 <        /* xml_prologue();                              /* start XML output */
424 >        xml_prologue(argc, argv);               /* start XML output */
425          if (single_plane_incident)              /* resample dist. */
426 <                interp_isotropic();
426 >                eval_isotropic(NULL);
427          else
428 <                interp_anisotropic();
429 <        /* xml_epilogue();                              /* finish XML output */
428 >                eval_anisotropic(NULL);
429 >        xml_epilogue();                         /* finish XML output & exit */
430          return(0);
431   userr:
432          fprintf(stderr,
433 <        "Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n",
433 >        "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n",
434 >                                progname);
435 >        fprintf(stderr,
436 >        "   or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
437                                  progname);
438          return(1);
439   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines