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.2 by greg, Sat Oct 20 17:01:26 2012 UTC vs.
Revision 2.24 by greg, Sat Mar 8 01:05:00 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines