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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines