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

Comparing ray/src/cv/bsdf2rad.c (file contents):
Revision 2.27 by greg, Wed Apr 12 04:15:08 2017 UTC vs.
Revision 2.42 by greg, Sat Jun 7 05:09:45 2025 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   *  Plot 3-D BSDF output based on scattering interpolant or XML representation
6   */
7  
8 #include <stdio.h>
9 #include <string.h>
8   #include <stdlib.h>
9 < #include "paths.h"
9 > #include "rtio.h"
10   #include "rtmath.h"
13 #include "resolu.h"
11   #include "bsdfrep.h"
12  
13 + #ifndef NINCIDENT
14   #define NINCIDENT       37              /* number of samples/hemisphere */
15 <
15 > #endif
16 > #ifndef GRIDSTEP
17   #define GRIDSTEP        2               /* our grid step size */
18 + #endif
19   #define SAMPRES         (GRIDRES/GRIDSTEP)
20  
21   int     front_comp = 0;                 /* front component flags (SDsamp*) */
# Line 24 | Line 24 | double overall_min = 1./PI;            /* overall minimum BSDF v
24   double  min_log10;                      /* smallest log10 value for plotting */
25   double  overall_max = .0;               /* overall maximum BSDF value */
26  
27 < char    ourTempDir[TEMPLEN] = "";       /* our temporary directory */
27 > char    ourTempDir[TEMPLEN+1] = "";     /* our temporary directory */
28  
29   const char      frpref[] = "rf";
30   const char      ftpref[] = "tf";
# Line 40 | Line 40 | const double   sph_xoffset = 15.;
40   #define bsdf_rad        (sph_rad*.25)
41   #define arrow_rad       (bsdf_rad*.015)
42  
43 < #define FEQ(a,b)        ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
43 > #define set_minlog()    overall_min = (overall_min < 1e-5) ? 1e-5 : overall_min; \
44 >                                min_log10 = log10(overall_min) - .1
45  
45 #define set_minlog()    (min_log10 = log10(overall_min + 1e-5) - .1)
46
47 char    *progname;
48
46   /* Get Fibonacci sphere vector (0 to NINCIDENT-1) */
47   static RREAL *
48   get_ivector(FVECT iv, int i)
# Line 76 | Line 73 | cvt_sposition(FVECT sp, const FVECT iv, int inc_side)
73   static char *
74   tfile_name(const char *prefix, const char *suffix, int i)
75   {
76 <        static char     buf[128];
76 >        static char     buf[256];
77  
78          if (!ourTempDir[0]) {           /* create temporary directory */
79                  mktemp(strcpy(ourTempDir,TEMPLATE));
# Line 144 | Line 141 | plotBSDF(const char *fname, const FVECT ivec, int dfl,
141                  SDValue sval;
142                  double  bsdf;
143                  ovec_from_pos(ovec, i*GRIDSTEP, j*GRIDSTEP);
144 <                if (SDreportError(SDevalBSDF(&sval, ovec,
145 <                                                ivec, sd), stderr))
144 >                if (SDreportError(SDevalBSDF(&sval, ivec,
145 >                                                ovec, sd), stderr))
146                          return(0);
147                  if (sval.cieY > overall_max)
148                          overall_max = sval.cieY;
# Line 274 | Line 271 | put_mirror_arrow(const FVECT origin, const FVECT nrm)
271   {
272          const double    arrow_len = 1.2*bsdf_rad;
273          const double    tip_len = 0.2*bsdf_rad;
274 +        static int      cnt = 1;
275          FVECT           refl;
276          int             i;
277  
# Line 281 | Line 279 | put_mirror_arrow(const FVECT origin, const FVECT nrm)
279          refl[1] = 2.*nrm[2]*nrm[1];
280          refl[2] = 2.*nrm[2]*nrm[2] - 1.;
281  
282 <        printf("\n# Mirror arrow\n");
283 <        printf("\narrow_mat cylinder inc_dir\n0\n0\n7");
282 >        printf("\n# Mirror arrow #%d\n", cnt);
283 >        printf("\nshaft_mat cylinder inc_dir%d\n0\n0\n7", cnt);
284          printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
285                          origin[0], origin[1], origin[2]+arrow_len,
286                          origin[0], origin[1], origin[2],
287                          arrow_rad);
288 <        printf("\narrow_mat cylinder mir_dir\n0\n0\n7");
288 >        printf("\nshaft_mat cylinder mir_dir%d\n0\n0\n7", cnt);
289          printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
290                          origin[0], origin[1], origin[2],
291                          origin[0] + arrow_len*refl[0],
292                          origin[1] + arrow_len*refl[1],
293                          origin[2] + arrow_len*refl[2],
294                          arrow_rad);
295 <        printf("\narrow_mat cone mir_tip\n0\n0\n8");
295 >        printf("\ntip_mat cone mir_tip%d\n0\n0\n8", cnt);
296          printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
297                          origin[0] + (arrow_len-.5*tip_len)*refl[0],
298                          origin[1] + (arrow_len-.5*tip_len)*refl[1],
# Line 303 | Line 301 | put_mirror_arrow(const FVECT origin, const FVECT nrm)
301                          origin[1] + (arrow_len+.5*tip_len)*refl[1],
302                          origin[2] + (arrow_len+.5*tip_len)*refl[2],
303                          2.*arrow_rad);
304 +        ++cnt;
305   }
306  
307   /* Put out transmitted direction arrow for the given incident vector */
# Line 311 | Line 310 | put_trans_arrow(const FVECT origin)
310   {
311          const double    arrow_len = 1.2*bsdf_rad;
312          const double    tip_len = 0.2*bsdf_rad;
313 +        static int      cnt = 1;
314          int             i;
315  
316 <        printf("\n# Transmission arrow\n");
317 <        printf("\narrow_mat cylinder trans_dir\n0\n0\n7");
316 >        printf("\n# Transmission arrow #%d\n", cnt);
317 >        printf("\nshaft_mat cylinder trans_dir%d\n0\n0\n7", cnt);
318          printf("\n\t%f %f %f\n\t%f %f %f\n\t%f\n",
319                          origin[0], origin[1], origin[2],
320                          origin[0], origin[1], origin[2]-arrow_len,
321                          arrow_rad);
322 <        printf("\narrow_mat cone trans_tip\n0\n0\n8");
322 >        printf("\ntip_mat cone trans_tip%d\n0\n0\n8", cnt);
323          printf("\n\t%f %f %f\n\t%f %f %f\n\t%f 0\n",
324                          origin[0], origin[1], origin[2]-arrow_len+.5*tip_len,
325                          origin[0], origin[1], origin[2]-arrow_len-.5*tip_len,
326 <                        2.*arrow_rad);  
326 >                        2.*arrow_rad);
327 >        ++cnt;
328   }
329  
330   /* Compute rotation (x,y,z) => (xp,yp,zp) */
# Line 342 | Line 343 | addrot(char *xf, const FVECT xp, const FVECT yp, const
343                  return(4);
344          }
345          theta = atan2(yp[2], zp[2]);
346 <        if (!FEQ(theta,0.0)) {
346 >        if (!FABSEQ(theta,0.0)) {
347                  sprintf(xf, " -rx %f", theta*(180./PI));
348                  while (*xf) ++xf;
349                  n += 2;
350          }
351          theta = Asin(-xp[2]);
352 <        if (!FEQ(theta,0.0)) {
352 >        if (!FABSEQ(theta,0.0)) {
353                  sprintf(xf, " -ry %f", theta*(180./PI));
354                  while (*xf) ++xf;
355                  n += 2;
356          }
357          theta = atan2(xp[1], xp[0]);
358 <        if (!FEQ(theta,0.0)) {
358 >        if (!FABSEQ(theta,0.0)) {
359                  sprintf(xf, " -rz %f", theta*(180./PI));
360                  /* while (*xf) ++xf; */
361                  n += 2;
# Line 378 | Line 379 | put_BSDFs(void)
379          printf("\n# Gensurf output corresponding to %d incident directions\n",
380                          NINCIDENT);
381  
382 <        printf("\nvoid glow arrow_glow\n0\n0\n4 1 0 1 0\n");
383 <        printf("\nvoid mixfunc arrow_mat\n4 arrow_glow void 0.25 .\n0\n0\n");
382 >        printf("\nvoid glow tip_mat\n0\n0\n4 1 0 1 0\n");
383 >        printf("\nvoid mixfunc shaft_mat\n4 tip_mat void 0.25 .\n0\n0\n");
384  
385          for (i = 0; i < NINCIDENT; i++) {
386                  get_ivector(ivec, i);
# Line 470 | Line 471 | put_matBSDF(const char *XMLfile)
471          }
472          switch (XMLfile[0]) {           /* avoid RAYPATH search */
473          case '.':
474 +        case '~':
475          CASEDIRSEP:
476                  curdir = "";
477                  break;
# Line 511 | Line 513 | put_hemispheres(void)
513          if (front_comp) {
514                  printf(
515   "\n!genrev %s Front \"R*sin(A*t)\" \"R*cos(A*t)\" %d -e \"R:%g;A:%f\" -s | xform -t %g 0 0\n",
516 <                                sph_fmat, nsegs, sph_rad, 0.495*PI, sph_xoffset);
516 >                                sph_fmat, nsegs, sph_rad, 0.5*PI, sph_xoffset);
517                  printf("\nvoid brighttext front_text\n3 helvet.fnt . FRONT\n0\n");
518                  printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
519                                  -.22*sph_rad + sph_xoffset, -1.4*sph_rad,
# Line 527 | Line 529 | put_hemispheres(void)
529          if (back_comp) {
530                  printf(
531   "\n!genrev %s Back \"R*cos(A*t)\" \"R*sin(A*t)\" %d -e \"R:%g;A:%f\" -s | xform -t %g 0 0\n",
532 <                                sph_bmat, nsegs, sph_rad, 0.495*PI, -sph_xoffset);
532 >                                sph_bmat, nsegs, sph_rad, 0.5*PI, -sph_xoffset);
533                  printf("\nvoid brighttext back_text\n3 helvet.fnt . BACK\n0\n");
534                  printf("12\n\t%f %f 0\n\t%f 0 0\n\t0 %f 0\n\t.01 1 -.1\n",
535                                  -.22*sph_rad - sph_xoffset, -1.4*sph_rad,
# Line 633 | Line 635 | convert_mgf(const char *mgfdata)
635          if ((fp = popen(cmdbuf, "r")) == NULL ||
636                          fscanf(fp, "%f %f %f %f %f %f",
637                                  &xmin, &xmax, &ymin, &ymax, &zmin, &zmax) != 6
638 <                        || pclose(fp) < 0) {
638 >                        || pclose(fp) != 0) {
639                  fprintf(stderr, "%s: error reading from command: %s\n",
640                                  progname, cmdbuf);
641                  return;
# Line 667 | Line 669 | convert_mgf(const char *mgfdata)
669   static int
670   rbf_headline(char *s, void *p)
671   {
672 <        char    fmt[64];
672 >        char    fmt[MAXFMTLEN];
673  
674          if (formatval(fmt, s)) {
675                  if (strcmp(fmt, BSDFREP_FMT))
# Line 701 | Line 703 | int
703   main(int argc, char *argv[])
704   {
705          int     inpXML = -1;
706 +        double  myLim[2];
707          SDData  myBSDF;
708 <        int     n;
708 >        int     a, n;
709 >                                                /* set global progname */
710 >        fixargv0(argv[0]);
711                                                  /* check arguments */
712 <        progname = argv[0];
713 <        if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
714 <                if (!strcasecmp(argv[1]+n, ".xml"))
712 >        a = 1;
713 >        myLim[0] = -1; myLim[1] = -2;           /* specified BSDF range? */
714 >        if (argc > a+3 && argv[a][0] == '-' && argv[a][1] == 'r') {
715 >                myLim[0] = atof(argv[++a]);
716 >                myLim[1] = atof(argv[++a]);
717 >                ++a;
718 >        }
719 >        if (argc > a && (n = strlen(argv[a])-4) > 0) {
720 >                if (!strcasecmp(argv[a]+n, ".xml"))
721                          inpXML = 1;
722 <                else if (!strcasecmp(argv[1]+n, ".sir"))
722 >                else if (!strcasecmp(argv[a]+n, ".sir"))
723                          inpXML = 0;
724          }
725 <        if (inpXML < 0 || inpXML & (argc > 2)) {
726 <                fprintf(stderr, "Usage: %s bsdf.xml > output.rad\n", progname);
727 <                fprintf(stderr, "   Or: %s hemi1.sir hemi2.sir .. > output.rad\n", progname);
725 >        if (inpXML < 0 || inpXML & (argc > a+1)) {
726 >                fprintf(stderr, "Usage: %s [-r min max] bsdf.xml > output.rad\n", progname);
727 >                fprintf(stderr, "   Or: %s [-r min max] hemi1.sir hemi2.sir .. > output.rad\n", progname);
728                  return(1);
729          }
730          fputs("# ", stdout);                    /* copy our command */
731          printargs(argc, argv, stdout);
732                                                  /* evaluate BSDF */
733          if (inpXML) {
734 <                SDclearBSDF(&myBSDF, argv[1]);
735 <                if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
734 >                SDclearBSDF(&myBSDF, argv[a]);
735 >                if (SDreportError(SDloadFile(&myBSDF, argv[a]), stderr))
736                          return(1);
737                  if (myBSDF.rf != NULL) front_comp |= SDsampR;
738                  if (myBSDF.tf != NULL) front_comp |= SDsampT;
# Line 729 | Line 740 | main(int argc, char *argv[])
740                  if (myBSDF.tb != NULL) back_comp |= SDsampT;
741                  if (!front_comp & !back_comp) {
742                          fprintf(stderr, "%s: nothing to plot in '%s'\n",
743 <                                        progname, argv[1]);
743 >                                        progname, argv[a]);
744                          return(1);
745                  }
746 <                if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
747 <                        overall_min = myBSDF.rLambFront.cieY/PI;
748 <                if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
749 <                        overall_min = myBSDF.rLambBack.cieY/PI;
750 <                if ((front_comp|back_comp) & SDsampT &&
751 <                                myBSDF.tLamb.cieY < overall_min*PI)
752 <                        overall_min = myBSDF.tLamb.cieY/PI;
746 >                if (myLim[0] >= 0)
747 >                        overall_min = myLim[0];
748 >                else {
749 >                        if (front_comp & SDsampR && myBSDF.rLambFront.cieY < overall_min*PI)
750 >                                overall_min = myBSDF.rLambFront.cieY/PI;
751 >                        if (back_comp & SDsampR && myBSDF.rLambBack.cieY < overall_min*PI)
752 >                                overall_min = myBSDF.rLambBack.cieY/PI;
753 >                        if (front_comp & SDsampT && myBSDF.tLambFront.cieY < overall_min*PI)
754 >                                overall_min = myBSDF.tLambFront.cieY/PI;
755 >                        if (back_comp & SDsampT && myBSDF.tLambBack.cieY < overall_min*PI)
756 >                                overall_min = myBSDF.tLambBack.cieY/PI;
757 >                }
758                  set_minlog();
759                  if (!build_wBSDF(&myBSDF))
760                          return(1);
# Line 747 | Line 763 | main(int argc, char *argv[])
763                  else
764                          strcpy(bsdf_name, myBSDF.name);
765                  strcpy(bsdf_manuf, myBSDF.makr);
766 <                put_matBSDF(argv[1]);
766 >                put_matBSDF(argv[a]);
767          } else {
768                  FILE    *fp[4];
769 <                if (argc > 5) {
770 <                        fprintf(stderr, "%s: too many input files\n", progname);
769 >                if (argc > a+4) {
770 >                        fprintf(stderr, "%s: more than 4 hemispheres!\n", progname);
771                          return(1);
772                  }
773 <                for (n = 1; n < argc; n++) {
774 <                        fp[n-1] = fopen(argv[n], "rb");
775 <                        if (fp[n-1] == NULL) {
773 >                for (n = a; n < argc; n++) {
774 >                        fp[n-a] = fopen(argv[n], "rb");
775 >                        if (fp[n-a] == NULL) {
776                                  fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
777                                                  progname, argv[n]);
778                                  return(1);
779                          }
780 <                        if (getheader(fp[n-1], rbf_headline, NULL) < 0) {
780 >                        if (getheader(fp[n-a], rbf_headline, NULL) < 0) {
781                                  fprintf(stderr, "%s: bad BSDF interpolant '%s'\n",
782                                                  progname, argv[n]);
783                                  return(1);
784                          }
785                  }
786 +                if (myLim[0] >= 0)
787 +                        overall_min = myLim[0];
788                  set_minlog();
789 <                for (n = 1; n < argc; n++) {
790 <                        if (fseek(fp[n-1], 0L, SEEK_SET) < 0) {
789 >                for (n = a; n < argc; n++) {
790 >                        if (fseek(fp[n-a], 0L, SEEK_SET) < 0) {
791                                  fprintf(stderr, "%s: cannot seek on '%s'\n",
792                                                  progname, argv[n]);
793                                  return(1);
794                          }
795 <                        if (!load_bsdf_rep(fp[n-1]))
795 >                        if (!load_bsdf_rep(fp[n-a]))
796                                  return(1);
797 <                        fclose(fp[n-1]);
797 >                        fclose(fp[n-a]);
798                          if (!build_wRBF())
799                                  return(1);
800                  }
801                  put_matBSDF(NULL);
802          }
803 +        if (myLim[1] > myLim[0])        /* override maximum BSDF? */
804 +                overall_max = myLim[1];
805          put_source();                   /* before hemispheres & labels */
806          put_hemispheres();
807          put_scale();
808          if (inpXML && myBSDF.mgf)
809                  convert_mgf(myBSDF.mgf);
810 <        if (!put_BSDFs())
810 >        if (!put_BSDFs())               /* most of the output happens here */
811                  return(1);
812          cleanup_tmp();
813          return(0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines