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.17 by greg, Fri Aug 2 20:56:19 2013 UTC vs.
Revision 2.30 by greg, Tue May 5 22:16:49 2015 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] */
# Line 23 | Line 25 | 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;
28 > #ifndef NSSAMP
29 > #define NSSAMP          100
30 > #endif
31 >                                /* limit on number of RBF lobes */
32 > static int              lobe_lim = 15000;
33 >                                /* progress bar length */
34 > static int              do_prog = 79;
35  
36 +
37 + /* Start new progress bar */
38 + #define prog_start(s)   if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else
39 +
40 + /* Draw progress bar of the appropriate length */
41 + static void
42 + prog_show(double frac)
43 + {
44 +        static unsigned call_cnt = 0;
45 +        static char     lastc[] = "-\\|/";
46 +        char            pbar[256];
47 +        int             nchars;
48 +
49 +        if (do_prog <= 1) return;
50 +        if (do_prog > sizeof(pbar)-2)
51 +                do_prog = sizeof(pbar)-2;
52 +        if (frac < 0) frac = 0;
53 +        else if (frac >= 1) frac = .9999;
54 +        nchars = do_prog*frac;
55 +        pbar[0] = '\r';
56 +        memset(pbar+1, '*', nchars);
57 +        pbar[nchars+1] = lastc[call_cnt++ & 3];
58 +        memset(pbar+2+nchars, '-', do_prog-nchars-1);
59 +        pbar[do_prog+1] = '\0';
60 +        fputs(pbar, stderr);
61 + }
62 +
63 + /* Finish progress bar */
64 + static void
65 + prog_done(void)
66 + {
67 +        int     n = do_prog;
68 +
69 +        if (n <= 1) return;
70 +        fputc('\r', stderr);
71 +        while (n--)
72 +                fputc(' ', stderr);
73 +        fputc('\r', stderr);
74 + }
75 +
76   /* Output XML prologue to stdout */
77   static void
78   xml_prologue(int ac, char *av[])
# Line 42 | Line 90 | xml_prologue(int ac, char *av[])
90          puts("<Optical>");
91          puts("<Layer>");
92          puts("\t<Material>");
93 <        puts("\t\t<Name>Name</Name>");
94 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
93 >        printf("\t\t<Name>%s</Name>\n", bsdf_name[0] ? bsdf_name : "Unknown");
94 >        printf("\t\t<Manufacturer>%s</Manufacturer>\n",
95 >                        bsdf_manuf[0] ? bsdf_manuf : "Unknown");
96          puts("\t\t<DeviceType>Other</DeviceType>");
97          puts("\t</Material>");
98          puts("\t<DataDefinition>");
# Line 120 | Line 169 | eval_isotropic(char *funame)
169  
170          data_prologue();                        /* begin output */
171          if (pctcull >= 0) {
172 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d",
172 >                sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d",
173                                  pctcull, samp_order);
174                  fflush(stdout);
175                  ofp = popen(cmd, "w");
# Line 130 | Line 179 | eval_isotropic(char *funame)
179                          exit(1);
180                  }
181                  SET_FILE_BINARY(ofp);
182 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
183 +                flockfile(ofp);
184 + #endif
185          } else
186                  fputs("{\n", stdout);
187                                                  /* need to assign Dx, Dy, Dz? */
# Line 142 | Line 194 | eval_isotropic(char *funame)
194                  iovec[1] = .0;
195                  iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
196                  if (funame == NULL)
197 <                        rbf = advect_rbf(iovec);
197 >                        rbf = advect_rbf(iovec, lobe_lim);
198                  for (ox = 0; ox < sqres; ox++) {
199                      float       last_bsdf = -1;
200                      for (oy = 0; oy < sqres; oy++) {
# Line 150 | Line 202 | eval_isotropic(char *funame)
202                          iovec[5] = output_orient *
203                                  sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
204                          if (funame == NULL)
205 <                            bsdf = eval_rbfrep(rbf, iovec+3) *
154 <                                                output_orient/iovec[5];
205 >                            bsdf = eval_rbfrep(rbf, iovec+3);
206                          else {
156                            double      ssa[3], ssvec[6], sum;
157                            int         ssi;
207                              if (assignD) {
208                                  varset("Dx", '=', -iovec[3]);
209                                  varset("Dy", '=', -iovec[4]);
# Line 162 | Line 211 | eval_isotropic(char *funame)
211                                  ++eclock;
212                              }
213                              bsdf = funvalue(funame, 6, iovec);
214 + #if (NSSAMP > 0)
215                              if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
216 <                                sum = 0;        /* super-sample voxel */
217 <                                for (ssi = nssamp; ssi--; ) {
218 <                                    SDmultiSamp(ssa, 3, (ssi+drand48())/nssamp);
216 >                                int     ssi;
217 >                                double  ssa[3], ssvec[6], sum = 0;
218 >                                                /* super-sample voxel */
219 >                                for (ssi = NSSAMP; ssi--; ) {
220 >                                    SDmultiSamp(ssa, 3, (ssi+frandom()) *
221 >                                                        (1./NSSAMP));
222                                      ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.;
223                                      ssvec[1] = .0;
224                                      ssvec[2] = input_orient *
# Line 176 | Line 229 | eval_isotropic(char *funame)
229                                                  sqrt(1. - ssvec[3]*ssvec[3] -
230                                                          ssvec[4]*ssvec[4]);
231                                      if (assignD) {
232 <                                        varset("Dx", '=', -iovec[3]);
233 <                                        varset("Dy", '=', -iovec[4]);
234 <                                        varset("Dz", '=', -iovec[5]);
232 >                                        varset("Dx", '=', -ssvec[3]);
233 >                                        varset("Dy", '=', -ssvec[4]);
234 >                                        varset("Dz", '=', -ssvec[5]);
235                                          ++eclock;
236                                      }
237                                      sum += funvalue(funame, 6, ssvec);
238                                  }
239 <                                bsdf = sum/nssamp;
239 >                                bsdf = sum/NSSAMP;
240                              }
241 + #endif
242                          }
243                          if (pctcull >= 0)
244                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
# Line 195 | Line 249 | eval_isotropic(char *funame)
249                  }
250                  if (rbf != NULL)
251                          free(rbf);
252 +                prog_show((ix+1.)*(2./sqres));
253          }
254          if (pctcull >= 0) {                     /* finish output */
255                  if (pclose(ofp)) {
# Line 208 | Line 263 | eval_isotropic(char *funame)
263                  fputs("}\n", stdout);
264          }
265          data_epilogue();
266 +        prog_done();
267   }
268  
269   /* Interpolate and output anisotropic BSDF data */
# Line 224 | Line 280 | eval_anisotropic(char *funame)
280  
281          data_prologue();                        /* begin output */
282          if (pctcull >= 0) {
283 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d",
283 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d",
284 >                                (input_orient>0 ^ output_orient>0) ? "" : " -a",
285                                  pctcull, samp_order);
286                  fflush(stdout);
287                  ofp = popen(cmd, "w");
# Line 233 | Line 290 | eval_anisotropic(char *funame)
290                                          progname);
291                          exit(1);
292                  }
293 +                SET_FILE_BINARY(ofp);
294 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
295 +                flockfile(ofp);
296 + #endif
297          } else
298                  fputs("{\n", stdout);
299                                                  /* need to assign Dx, Dy, Dz? */
# Line 246 | Line 307 | eval_anisotropic(char *funame)
307                  iovec[2] = input_orient *
308                                  sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
309                  if (funame == NULL)
310 <                        rbf = advect_rbf(iovec);
310 >                        rbf = advect_rbf(iovec, lobe_lim);
311                  for (ox = 0; ox < sqres; ox++) {
312                      float       last_bsdf = -1;
313                      for (oy = 0; oy < sqres; oy++) {
# Line 254 | Line 315 | eval_anisotropic(char *funame)
315                          iovec[5] = output_orient *
316                                  sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
317                          if (funame == NULL)
318 <                            bsdf = eval_rbfrep(rbf, iovec+3) *
258 <                                                output_orient/iovec[5];
318 >                            bsdf = eval_rbfrep(rbf, iovec+3);
319                          else {
260                            double      ssa[4], ssvec[6], sum;
261                            int         ssi;
320                              if (assignD) {
321                                  varset("Dx", '=', -iovec[3]);
322                                  varset("Dy", '=', -iovec[4]);
# Line 266 | Line 324 | eval_anisotropic(char *funame)
324                                  ++eclock;
325                              }
326                              bsdf = funvalue(funame, 6, iovec);
327 + #if (NSSAMP > 0)
328                              if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
329 <                                sum = 0;        /* super-sample voxel */
330 <                                for (ssi = nssamp; ssi--; ) {
331 <                                    SDmultiSamp(ssa, 4, (ssi+drand48())/nssamp);
329 >                                int     ssi;
330 >                                double  ssa[4], ssvec[6], sum = 0;
331 >                                                /* super-sample voxel */
332 >                                for (ssi = NSSAMP; ssi--; ) {
333 >                                    SDmultiSamp(ssa, 4, (ssi+frandom()) *
334 >                                                        (1./NSSAMP));
335                                      SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres,
336                                                  1.-(iy+ssa[1])/sqres);
337 <                                    ssvec[2] = output_orient *
337 >                                    ssvec[2] = input_orient *
338                                                  sqrt(1. - ssvec[0]*ssvec[0] -
339                                                          ssvec[1]*ssvec[1]);
340                                      SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres,
# Line 281 | Line 343 | eval_anisotropic(char *funame)
343                                                  sqrt(1. - ssvec[3]*ssvec[3] -
344                                                          ssvec[4]*ssvec[4]);
345                                      if (assignD) {
346 <                                        varset("Dx", '=', -iovec[3]);
347 <                                        varset("Dy", '=', -iovec[4]);
348 <                                        varset("Dz", '=', -iovec[5]);
346 >                                        varset("Dx", '=', -ssvec[3]);
347 >                                        varset("Dy", '=', -ssvec[4]);
348 >                                        varset("Dz", '=', -ssvec[5]);
349                                          ++eclock;
350                                      }
351                                      sum += funvalue(funame, 6, ssvec);
352                                  }
353 <                                bsdf = sum/nssamp;
353 >                                bsdf = sum/NSSAMP;
354                              }
355 + #endif
356                          }
357                          if (pctcull >= 0)
358                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
# Line 300 | Line 363 | eval_anisotropic(char *funame)
363                  }
364                  if (rbf != NULL)
365                          free(rbf);
366 +                prog_show((ix*sqres+iy+1.)/(sqres*sqres));
367              }
368          if (pctcull >= 0) {                     /* finish output */
369                  if (pclose(ofp)) {
# Line 310 | Line 374 | eval_anisotropic(char *funame)
374          } else
375                  fputs("}\n", stdout);
376          data_epilogue();
377 +        prog_done();
378   }
379  
380   /* Read in BSDF and interpolate as tensor tree representation */
# Line 356 | Line 421 | main(int argc, char *argv[])
421                  case 'g':
422                          samp_order = atoi(argv[++i]);
423                          break;
424 +                case 'l':
425 +                        lobe_lim = atoi(argv[++i]);
426 +                        break;
427 +                case 'p':
428 +                        do_prog = atoi(argv[i]+2);
429 +                        break;
430                  default:
431                          goto userr;
432                  }
# Line 366 | Line 437 | main(int argc, char *argv[])
437                          fprintf(stderr,
438          "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
439                                          progname);
440 <                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n",
370 <                                        progname);
440 >                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
441                          goto userr;
442                  }
443                  ++eclock;
# Line 375 | Line 445 | main(int argc, char *argv[])
445                  if (dofwd) {
446                          input_orient = -1;
447                          output_orient = -1;
448 <                        (*evf)(argv[i]);        /* outside reflectance */
448 >                        prog_start("Evaluating outside reflectance");
449 >                        (*evf)(argv[i]);
450                          output_orient = 1;
451 <                        (*evf)(argv[i]);        /* outside -> inside */
451 >                        prog_start("Evaluating outside->inside transmission");
452 >                        (*evf)(argv[i]);
453                  }
454                  if (dobwd) {
455                          input_orient = 1;
456                          output_orient = 1;
457 <                        (*evf)(argv[i]);        /* inside reflectance */
457 >                        prog_start("Evaluating inside reflectance");
458 >                        (*evf)(argv[i]);
459                          output_orient = -1;
460 <                        (*evf)(argv[i]);        /* inside -> outside */
460 >                        prog_start("Evaluating inside->outside transmission");
461 >                        (*evf)(argv[i]);
462                  }
463                  xml_epilogue();                 /* finish XML output & exit */
464                  return(0);
# Line 392 | Line 466 | main(int argc, char *argv[])
466          if (i < argc) {                         /* open input files if given */
467                  int     nbsdf = 0;
468                  for ( ; i < argc; i++) {        /* interpolate each component */
469 +                        char    pbuf[256];
470                          FILE    *fpin = fopen(argv[i], "rb");
471                          if (fpin == NULL) {
472                                  fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
# Line 403 | Line 478 | main(int argc, char *argv[])
478                          fclose(fpin);
479                          if (!nbsdf++)           /* start XML on first dist. */
480                                  xml_prologue(argc, argv);
481 +                        sprintf(pbuf, "Interpolating component '%s'", argv[i]);
482 +                        prog_start(pbuf);
483                          if (single_plane_incident)
484                                  eval_isotropic(NULL);
485                          else
# Line 415 | Line 492 | main(int argc, char *argv[])
492          if (!load_bsdf_rep(stdin))
493                  return(1);
494          xml_prologue(argc, argv);               /* start XML output */
495 +        prog_start("Interpolating from standard input");
496          if (single_plane_incident)              /* resample dist. */
497                  eval_isotropic(NULL);
498          else
# Line 423 | Line 501 | main(int argc, char *argv[])
501          return(0);
502   userr:
503          fprintf(stderr,
504 <        "Usage: %s [-g Nlog2][-t pctcull] [bsdf.sir ..] > bsdf.xml\n",
504 >        "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n",
505                                  progname);
506          fprintf(stderr,
507          "   or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines