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

Comparing ray/src/cv/bsdf2klems.c (file contents):
Revision 2.1 by greg, Sun Apr 21 23:01:14 2013 UTC vs.
Revision 2.8 by greg, Fri Aug 2 20:56:19 2013 UTC

# Line 17 | Line 17 | static const char RCSid[] = "$Id$";
17   #include "calcomp.h"
18   #include "bsdfrep.h"
19   #include "bsdf_m.h"
20 +                                /* assumed maximum # Klems patches */
21 + #define MAXPATCHES      145
22                                  /* global argv[0] */
23   char                    *progname;
24                                  /* selected basis function name */
# Line 36 | Line 38 | get_basis(const char *bn)
38          return NULL;
39   }
40  
41 < /* Output XML prologue to stdout */
41 > /* Output XML header to stdout */
42   static void
43 < xml_prologue(int ac, char *av[])
43 > xml_header(int ac, char *av[])
44   {
43        ANGLE_BASIS     *abp = get_basis(kbasis);
44        int             i;
45
46        if (abp == NULL) {
47                fprintf(stderr, "%s: unknown angle basis '%s'\n", progname, kbasis);
48                exit(1);
49        }
45          puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
46          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\">");
47          fputs("<!-- File produced by:", stdout);
# Line 55 | Line 50 | xml_prologue(int ac, char *av[])
50                  fputs(*av++, stdout);
51          }
52          puts(" -->");
53 + }
54 +
55 + /* Output XML prologue to stdout */
56 + static void
57 + xml_prologue(const SDData *sd)
58 + {
59 +        const char      *matn = (sd && sd->matn[0]) ? sd->matn : "Name";
60 +        const char      *makr = (sd && sd->makr[0]) ? sd->makr : "Manufacturer";
61 +        ANGLE_BASIS     *abp = get_basis(kbasis);
62 +        int             i;
63 +
64 +        if (abp == NULL) {
65 +                fprintf(stderr, "%s: unknown angle basis '%s'\n", progname, kbasis);
66 +                exit(1);
67 +        }
68          puts("<WindowElementType>System</WindowElementType>");
69          puts("<FileType>BSDF</FileType>");
70          puts("<Optical>");
71          puts("<Layer>");
72          puts("\t<Material>");
73 <        puts("\t\t<Name>Name</Name>");
74 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
73 >        printf("\t\t<Name>%s</Name>\n", matn);
74 >        printf("\t\t<Manufacturer>%s</Manufacturer>\n", makr);
75 >        if (sd && sd->dim[2] > .001) {
76 >                printf("\t\t<Thickness unit=\"meter\">%.3f</Thickness>\n", sd->dim[2]);
77 >                printf("\t\t<Width unit=\"meter\">%.3f</Width>\n", sd->dim[0]);
78 >                printf("\t\t<Height unit=\"meter\">%.3f</Height>\n", sd->dim[1]);
79 >        }
80          puts("\t\t<DeviceType>Other</DeviceType>");
81          puts("\t</Material>");
82 +        if (sd && sd->mgf != NULL) {
83 +                puts("\t<Geometry format=\"MGF\">");
84 +                puts("\t\t<MGFblock unit=\"meter\">");
85 +                fputs(sd->mgf, stdout);
86 +                puts("</MGFblock>");
87 +                puts("\t</Geometry>");
88 +        }
89          puts("\t<DataDefinition>");
90          puts("\t\t<IncidentDataStructure>Columns</IncidentDataStructure>");
91          puts("\t\t<AngleBasis>");
# Line 73 | Line 95 | xml_prologue(int ac, char *av[])
95                  printf("\t\t\t<Theta>%g</Theta>\n", i ?
96                                  .5*(abp->lat[i].tmin + abp->lat[i+1].tmin) :
97                                  .0 );
98 <                printf("\t\t\t<nPhis>%d</nPhis>", abp->lat[i].nphis);
98 >                printf("\t\t\t<nPhis>%d</nPhis>\n", abp->lat[i].nphis);
99                  puts("\t\t\t<ThetaBounds>");
100                  printf("\t\t\t\t<LowerTheta>%g</LowerTheta>\n", abp->lat[i].tmin);
101                  printf("\t\t\t\t<UpperTheta>%g</UpperTheta>\n", abp->lat[i+1].tmin);
# Line 127 | Line 149 | xml_epilogue(void)
149          puts("</WindowElement>");
150   }
151  
152 < /* Load and resample XML BSDF description */
152 > /* Load and resample XML BSDF description using Klems basis */
153   static void
154   eval_bsdf(const char *fname)
155   {
156          ANGLE_BASIS     *abp = get_basis(kbasis);
157 +        float           *trans_mtx = NULL;
158          SDData          bsd;
159          SDError         ec;
160          FVECT           vin, vout;
# Line 142 | Line 165 | eval_bsdf(const char *fname)
165          SDclearBSDF(&bsd, fname);               /* load BSDF file */
166          if ((ec = SDloadFile(&bsd, fname)) != SDEnone)
167                  goto err;
168 +        xml_prologue(&bsd);                     /* pass geometry */
169                                                  /* front reflection */
170          if (bsd.rf != NULL || bsd.rLambFront.cieY > .002) {
171              input_orient = 1; output_orient = 1;
# Line 151 | Line 175 | eval_bsdf(const char *fname)
175                      sum = 0;                    /* average over patches */
176                      for (n = npsamps; n-- > 0; ) {
177                          fo_getvec(vout, j+(n+frandom())/npsamps, abp);
178 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
178 >                        fi_getvec(vin, i+urand(n), abp);
179                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
180                          if (ec != SDEnone)
181                                  goto err;
# Line 172 | Line 196 | eval_bsdf(const char *fname)
196                      sum = 0;                    /* average over patches */
197                      for (n = npsamps; n-- > 0; ) {
198                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
199 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
199 >                        bi_getvec(vin, i+urand(n), abp);
200                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
201                          if (ec != SDEnone)
202                                  goto err;
# Line 186 | Line 210 | eval_bsdf(const char *fname)
210          }
211                                                  /* front transmission */
212          if (bsd.tf != NULL || bsd.tLamb.cieY > .002) {
213 +            if (bsd.tb == NULL)
214 +                trans_mtx = (float *)malloc(sizeof(float) *
215 +                                                abp->nangles*abp->nangles);
216              input_orient = 1; output_orient = -1;
217              data_prologue();
218              for (j = 0; j < abp->nangles; j++) {
# Line 193 | Line 220 | eval_bsdf(const char *fname)
220                      sum = 0;                    /* average over patches */
221                      for (n = npsamps; n-- > 0; ) {
222                          bo_getvec(vout, j+(n+frandom())/npsamps, abp);
223 <                        fi_getvec(vin, i+(n+frandom())/npsamps, abp);
223 >                        fi_getvec(vin, i+urand(n), abp);
224                          ec = SDevalBSDF(&sv, vout, vin, &bsd);
225                          if (ec != SDEnone)
226                                  goto err;
227                          sum += sv.cieY;
228                      }
229                      printf("\t%.3e\n", sum/npsamps);
230 +                    if (trans_mtx != NULL)
231 +                        trans_mtx[j*abp->nangles + i] = sum/npsamps;
232                  }
233                  putchar('\n');                  /* extra space between rows */
234              }
235              data_epilogue();
236          }
237                                                  /* back transmission */
238 <        if (bsd.tb != NULL) {
238 >        if (bsd.tb != NULL || trans_mtx != NULL) {
239 >            if (bsd.tf == NULL)
240 >                trans_mtx = (float *)malloc(sizeof(float) *
241 >                                                abp->nangles*abp->nangles);
242              input_orient = -1; output_orient = 1;
243              data_prologue();
244              for (j = 0; j < abp->nangles; j++) {
245 <                for (i = 0; i < abp->nangles; i++) {
246 <                    sum = 0;                    /* average over patches */
247 <                    for (n = npsamps; n-- > 0; ) {
248 <                        fo_getvec(vout, j+(n+frandom())/npsamps, abp);
249 <                        bi_getvec(vin, i+(n+frandom())/npsamps, abp);
250 <                        ec = SDevalBSDF(&sv, vout, vin, &bsd);
251 <                        if (ec != SDEnone)
245 >                for (i = 0; i < abp->nangles; i++)
246 >                    if (bsd.tb != NULL) {       /* use tb if we have it */
247 >                        sum = 0;                /* average over patches */
248 >                        for (n = npsamps; n-- > 0; ) {
249 >                            fo_getvec(vout, j+(n+frandom())/npsamps, abp);
250 >                            bi_getvec(vin, i+urand(n), abp);
251 >                            ec = SDevalBSDF(&sv, vout, vin, &bsd);
252 >                            if (ec != SDEnone)
253                                  goto err;
254 <                        sum += sv.cieY;
254 >                            sum += sv.cieY;
255 >                        }
256 >                        printf("\t%.3e\n", sum/npsamps);
257 >                        if (trans_mtx != NULL)
258 >                            trans_mtx[i*abp->nangles + j] = sum/npsamps;
259 >                    } else {                    /* else transpose tf */
260 >                        printf("\t%.3e\n", trans_mtx[i*abp->nangles + j]);
261                      }
223                    printf("\t%.3e\n", sum/npsamps);
224                }
262                  putchar('\n');                  /* extra space between rows */
263              }
264              data_epilogue();
265          }
266 +                                                /* derived front transmission */
267 +        if (bsd.tf == NULL && trans_mtx != NULL) {
268 +            input_orient = 1; output_orient = -1;
269 +            data_prologue();
270 +            for (j = 0; j < abp->nangles; j++) {
271 +                for (i = 0; i < abp->nangles; i++)
272 +                    printf("\t%.3e\n", trans_mtx[j*abp->nangles + i]);
273 +                putchar('\n');                  /* extra space between rows */
274 +            }
275 +            data_epilogue();
276 +        }
277          SDfreeBSDF(&bsd);                       /* all done */
278 +        if (trans_mtx != NULL)
279 +                free(trans_mtx);
280          return;
281   err:
282          SDreportError(ec, stderr);
283          exit(1);
284   }
285  
286 < /* Interpolate and output a BSDF function */
286 > /* Interpolate and output a BSDF function using Klems basis */
287   static void
288   eval_function(char *funame)
289   {
290          ANGLE_BASIS     *abp = get_basis(kbasis);
291 +        int             assignD = (fundefined(funame) < 6);
292          double          iovec[6];
293          double          sum;
294          int             i, j, n;
295  
296 +        initurand(npsamps);
297          data_prologue();                        /* begin output */
298          for (j = 0; j < abp->nangles; j++) {    /* run through directions */
299              for (i = 0; i < abp->nangles; i++) {
# Line 253 | Line 305 | eval_function(char *funame)
305                          bo_getvec(iovec+3, j+(n+frandom())/npsamps, abp);
306  
307                      if (input_orient > 0)
308 <                        fi_getvec(iovec, j+(n+frandom())/npsamps, abp);
308 >                        fi_getvec(iovec, i+urand(n), abp);
309                      else
310 <                        bi_getvec(iovec, j+(n+frandom())/npsamps, abp);
310 >                        bi_getvec(iovec, i+urand(n), abp);
311  
312 +                    if (assignD) {
313 +                        varset("Dx", '=', -iovec[3]);
314 +                        varset("Dy", '=', -iovec[4]);
315 +                        varset("Dz", '=', -iovec[5]);
316 +                        ++eclock;
317 +                    }
318                      sum += funvalue(funame, 6, iovec);
319                  }
320                  printf("\t%.3e\n", sum/npsamps);
# Line 271 | Line 329 | static void
329   eval_rbf(void)
330   {
331          ANGLE_BASIS     *abp = get_basis(kbasis);
332 <        double          iovec[6];
332 >        float           bsdfarr[MAXPATCHES*MAXPATCHES];
333 >        FVECT           vin, vout;
334 >        RBFNODE         *rbf;
335          double          sum;
336          int             i, j, n;
337 +                                                /* sanity check */
338 +        if (abp->nangles > MAXPATCHES) {
339 +                fprintf(stderr, "%s: too many patches!\n", progname);
340 +                exit(1);
341 +        }
342 +        data_prologue();                        /* begin output */
343 +        for (i = 0; i < abp->nangles; i++) {
344 +            if (input_orient > 0)               /* use incident patch center */
345 +                fi_getvec(vin, i+.5*(i>0), abp);
346 +            else
347 +                bi_getvec(vin, i+.5*(i>0), abp);
348  
349 < fprintf(stder, "%s: RBF evaluation currently unimplemented\n", progname);
350 < exit(1);
349 >            rbf = advect_rbf(vin);              /* compute radial basis func */
350 >
351 >            for (j = 0; j < abp->nangles; j++) {
352 >                sum = 0;                        /* sample over exiting patch */
353 >                for (n = npsamps; n--; ) {
354 >                    if (output_orient > 0)
355 >                        fo_getvec(vout, j+(n+frandom())/npsamps, abp);
356 >                    else
357 >                        bo_getvec(vout, j+(n+frandom())/npsamps, abp);
358 >
359 >                    sum += eval_rbfrep(rbf, vout) / vout[2];
360 >                }
361 >                bsdfarr[j*abp->nangles + i] = sum*output_orient/npsamps;
362 >            }
363 >            if (rbf != NULL)
364 >                free(rbf);
365 >        }
366 >        n = 0;                                  /* write out our matrix */
367 >        for (j = 0; j < abp->nangles; j++) {
368 >            for (i = 0; i < abp->nangles; i++)
369 >                printf("\t%.3e\n", bsdfarr[n++]);
370 >            putchar('\n');
371 >        }
372 >        data_epilogue();                        /* finish output */
373   }
374  
375   /* Read in BSDF and interpolate as Klems matrix representation */
# Line 292 | Line 385 | main(int argc, char *argv[])
385          esupport &= ~(E_INCHAN|E_OUTCHAN);
386          scompile("PI:3.14159265358979323846", NULL, 0);
387          biggerlib();
388 <        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
388 >        for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
389                  switch (argv[i][1]) {           /* get options */
390                  case 'n':
391                          npsamps = atoi(argv[++i]);
# Line 327 | Line 420 | main(int argc, char *argv[])
420                          fprintf(stderr,
421          "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
422                                          progname);
423 +                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n",
424 +                                        progname);
425                          goto userr;
426                  }
427 <                xml_prologue(argc, argv);       /* start XML output */
427 >                ++eclock;
428 >                xml_header(argc, argv);                 /* start XML output */
429 >                xml_prologue(NULL);
430                  if (dofwd) {
431                          input_orient = -1;
432                          output_orient = -1;
# Line 347 | Line 444 | main(int argc, char *argv[])
444                  xml_epilogue();                 /* finish XML output & exit */
445                  return(0);
446          }
447 <        if (i == argc-1 && (cp = strstr(argv[i], ".xml")) != NULL &&
448 <                        strlen(cp) == 4) {      /* XML input? */
449 <                xml_prologue(argc, argv);       /* start XML output */
447 >                                                /* XML input? */
448 >        if (i == argc-1 && (cp = argv[i]+strlen(argv[i])-4) > argv[i] &&
449 >                                !strcasecmp(cp, ".xml")) {
450 >                xml_header(argc, argv);         /* start XML output */
451                  eval_bsdf(argv[i]);             /* load & resample BSDF */
452                  xml_epilogue();                 /* finish XML output & exit */
453                  return(0);
# Line 366 | Line 464 | main(int argc, char *argv[])
464                          if (!load_bsdf_rep(fpin))
465                                  return(1);
466                          fclose(fpin);
467 <                        if (!nbsdf++)           /* start XML on first dist. */
468 <                                xml_prologue(argc, argv);
467 >                        if (!nbsdf++) {         /* start XML on first dist. */
468 >                                xml_header(argc, argv);
469 >                                xml_prologue(NULL);
470 >                        }
471                          eval_rbf();
472                  }
473                  xml_epilogue();                 /* finish XML output & exit */
# Line 376 | Line 476 | main(int argc, char *argv[])
476          SET_FILE_BINARY(stdin);                 /* load from stdin */
477          if (!load_bsdf_rep(stdin))
478                  return(1);
479 <        xml_prologue(argc, argv);               /* start XML output */
479 >        xml_header(argc, argv);                 /* start XML output */
480 >        xml_prologue(NULL);
481          eval_rbf();                             /* resample dist. */
482          xml_epilogue();                         /* finish XML output & exit */
483          return(0);
484   userr:
485          fprintf(stderr,
486 <        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n",
386 <                                progname);
486 >        "Usage: %s [-n spp][-h|-q][bsdf.sir ..] > bsdf.xml\n", progname);
487          fprintf(stderr,
488 <        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n",
389 <                                progname);
488 >        "   or: %s [-n spp][-h|-q] bsdf_in.xml > bsdf_out.xml\n", progname);
489          fprintf(stderr,
490          "   or: %s [-n spp][-h|-q][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
491                                  progname);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines