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.13 by greg, Sat Mar 23 02:21:14 2013 UTC vs.
Revision 2.25 by greg, Tue Mar 11 19:37:45 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] */
# Line 20 | Line 22 | char                   *progname;
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
# Line 38 | Line 46 | xml_prologue(int ac, char *av[])
46          puts("<Optical>");
47          puts("<Layer>");
48          puts("\t<Material>");
49 <        puts("\t\t<Name>Name</Name>");
50 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
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>");
# Line 90 | Line 99 | xml_epilogue(void)
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   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          double          iovec[6];
124          float           bsdf;
125 < #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
125 >
126          data_prologue();                        /* begin output */
127          if (pctcull >= 0) {
128 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d",
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 117 | Line 135 | eval_isotropic(char *funame)
135                          exit(1);
136                  }
137                  SET_FILE_BINARY(ofp);
138 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
139 +                flockfile(ofp);
140 + #endif
141          } else
142                  fputs("{\n", stdout);
143 +                                                /* need to assign Dx, Dy, Dz? */
144 +        if (funame != NULL)
145 +                assignD = (fundefined(funame) < 6);
146                                                  /* run through directions */
147          for (ix = 0; ix < sqres/2; ix++) {
148                  RBFNODE *rbf = NULL;
149 <                iovec[0] = (ix+.5)/sqres - 1.;
149 >                iovec[0] = 2.*(ix+.5)/sqres - 1.;
150                  iovec[1] = .0;
151                  iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
152                  if (funame == NULL)
153 <                        rbf = advect_rbf(iovec);
154 <                for (ox = 0; ox < sqres; ox++)
153 >                        rbf = advect_rbf(iovec, lobe_lim);
154 >                for (ox = 0; ox < sqres; ox++) {
155 >                    float       last_bsdf = -1;
156                      for (oy = 0; oy < sqres; oy++) {
157                          SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
158                          iovec[5] = output_orient *
159                                  sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
160                          if (funame == NULL)
161 <                                bsdf = eval_rbfrep(rbf, iovec+3) *
161 >                            bsdf = eval_rbfrep(rbf, iovec+3) *
162                                                  output_orient/iovec[5];
163 <                        else
164 <                                bsdf = funvalue(funame, 6, iovec);
163 >                        else {
164 >                            double      ssa[3], ssvec[6], sum;
165 >                            int         ssi;
166 >                            if (assignD) {
167 >                                varset("Dx", '=', -iovec[3]);
168 >                                varset("Dy", '=', -iovec[4]);
169 >                                varset("Dz", '=', -iovec[5]);
170 >                                ++eclock;
171 >                            }
172 >                            bsdf = funvalue(funame, 6, iovec);
173 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
174 >                                sum = 0;        /* super-sample voxel */
175 >                                for (ssi = nssamp; ssi--; ) {
176 >                                    SDmultiSamp(ssa, 3, (ssi+frandom())/nssamp);
177 >                                    ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.;
178 >                                    ssvec[1] = .0;
179 >                                    ssvec[2] = input_orient *
180 >                                                sqrt(1. - ssvec[0]*ssvec[0]);
181 >                                    SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres,
182 >                                                (oy+ssa[2])/sqres);
183 >                                    ssvec[5] = output_orient *
184 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
185 >                                                        ssvec[4]*ssvec[4]);
186 >                                    if (assignD) {
187 >                                        varset("Dx", '=', -iovec[3]);
188 >                                        varset("Dy", '=', -iovec[4]);
189 >                                        varset("Dz", '=', -iovec[5]);
190 >                                        ++eclock;
191 >                                    }
192 >                                    sum += funvalue(funame, 6, ssvec);
193 >                                }
194 >                                bsdf = sum/nssamp;
195 >                            }
196 >                        }
197                          if (pctcull >= 0)
198                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
199                          else
200                                  printf("\t%.3e\n", bsdf);
201 +                        last_bsdf = bsdf;
202                      }
203 +                }
204                  if (rbf != NULL)
205                          free(rbf);
206          }
# Line 165 | Line 224 | eval_anisotropic(char *funame)
224   {
225          const int       sqres = 1<<samp_order;
226          FILE            *ofp = NULL;
227 +        int             assignD = 0;
228          char            cmd[128];
229          int             ix, iy, ox, oy;
230          double          iovec[6];
231          float           bsdf;
232 < #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
232 >
233          data_prologue();                        /* begin output */
234          if (pctcull >= 0) {
235 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d",
235 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d",
236 >                                (input_orient>0 ^ output_orient>0) ? "" : " -a",
237                                  pctcull, samp_order);
238                  fflush(stdout);
239                  ofp = popen(cmd, "w");
# Line 185 | Line 242 | eval_anisotropic(char *funame)
242                                          progname);
243                          exit(1);
244                  }
245 +                SET_FILE_BINARY(ofp);
246 + #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
247 +                flockfile(ofp);
248 + #endif
249          } else
250                  fputs("{\n", stdout);
251 +                                                /* need to assign Dx, Dy, Dz? */
252 +        if (funame != NULL)
253 +                assignD = (fundefined(funame) < 6);
254                                                  /* run through directions */
255          for (ix = 0; ix < sqres; ix++)
256              for (iy = 0; iy < sqres; iy++) {
257                  RBFNODE *rbf = NULL;            /* Klems reversal */
258 <                SDsquare2disk(iovec, (ix+.5)/sqres, (iy+.5)/sqres);
195 <                iovec[0] = -iovec[0]; iovec[1] = -iovec[1];
258 >                SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres);
259                  iovec[2] = input_orient *
260                                  sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
261                  if (funame == NULL)
262 <                        rbf = advect_rbf(iovec);
263 <                for (ox = 0; ox < sqres; ox++)
262 >                        rbf = advect_rbf(iovec, lobe_lim);
263 >                for (ox = 0; ox < sqres; ox++) {
264 >                    float       last_bsdf = -1;
265                      for (oy = 0; oy < sqres; oy++) {
266                          SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
267                          iovec[5] = output_orient *
268                                  sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
269                          if (funame == NULL)
270 <                                bsdf = eval_rbfrep(rbf, iovec+3) *
270 >                            bsdf = eval_rbfrep(rbf, iovec+3) *
271                                                  output_orient/iovec[5];
272 <                        else
273 <                                bsdf = funvalue(funame, 6, iovec);
272 >                        else {
273 >                            double      ssa[4], ssvec[6], sum;
274 >                            int         ssi;
275 >                            if (assignD) {
276 >                                varset("Dx", '=', -iovec[3]);
277 >                                varset("Dy", '=', -iovec[4]);
278 >                                varset("Dz", '=', -iovec[5]);
279 >                                ++eclock;
280 >                            }
281 >                            bsdf = funvalue(funame, 6, iovec);
282 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
283 >                                sum = 0;        /* super-sample voxel */
284 >                                for (ssi = nssamp; ssi--; ) {
285 >                                    SDmultiSamp(ssa, 4, (ssi+frandom())/nssamp);
286 >                                    SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres,
287 >                                                1.-(iy+ssa[1])/sqres);
288 >                                    ssvec[2] = output_orient *
289 >                                                sqrt(1. - ssvec[0]*ssvec[0] -
290 >                                                        ssvec[1]*ssvec[1]);
291 >                                    SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres,
292 >                                                (oy+ssa[3])/sqres);
293 >                                    ssvec[5] = output_orient *
294 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
295 >                                                        ssvec[4]*ssvec[4]);
296 >                                    if (assignD) {
297 >                                        varset("Dx", '=', -iovec[3]);
298 >                                        varset("Dy", '=', -iovec[4]);
299 >                                        varset("Dz", '=', -iovec[5]);
300 >                                        ++eclock;
301 >                                    }
302 >                                    sum += funvalue(funame, 6, ssvec);
303 >                                }
304 >                                bsdf = sum/nssamp;
305 >                            }
306 >                        }
307                          if (pctcull >= 0)
308                                  fwrite(&bsdf, sizeof(bsdf), 1, ofp);
309                          else
310                                  printf("\t%.3e\n", bsdf);
311 +                        last_bsdf = bsdf;
312                      }
313 +                }
314                  if (rbf != NULL)
315                          free(rbf);
316              }
# Line 270 | Line 369 | main(int argc, char *argv[])
369                  case 'g':
370                          samp_order = atoi(argv[++i]);
371                          break;
372 +                case 'l':
373 +                        lobe_lim = atoi(argv[++i]);
374 +                        break;
375                  default:
376                          goto userr;
377                  }
378          if (single_plane_incident >= 0) {       /* function-based BSDF? */
379                  void    (*evf)(char *s) = single_plane_incident ?
380                                  &eval_isotropic : &eval_anisotropic;
381 <                if (i != argc-1 || fundefined(argv[i]) != 6) {
381 >                if (i != argc-1 || fundefined(argv[i]) < 3) {
382                          fprintf(stderr,
383          "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
384                                          progname);
385 +                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
386                          goto userr;
387                  }
388 +                ++eclock;
389                  xml_prologue(argc, argv);       /* start XML output */
390                  if (dofwd) {
391                          input_orient = -1;
# Line 334 | Line 438 | main(int argc, char *argv[])
438          return(0);
439   userr:
440          fprintf(stderr,
441 <        "Usage: %s [-g Nlog2][-t pctcull] [bsdf.sir ..] > bsdf.xml\n",
441 >        "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n",
442                                  progname);
443          fprintf(stderr,
444          "   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