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.1 by greg, Tue Oct 22 04:29:27 2013 UTC vs.
Revision 2.6 by greg, Mon Feb 17 21:56:22 2014 UTC

# Line 2 | Line 2
2   static const char RCSid[] = "$Id$";
3   #endif
4   /*
5 < *  Plot 3-D BSDF output based on scattering interpolant representation
5 > *  Plot 3-D BSDF output based on scattering interpolant or XML representation
6   */
7  
8   #define _USE_MATH_DEFINES
9   #include <stdio.h>
10 + #include <string.h>
11   #include <stdlib.h>
12   #include <math.h>
13 + #include "rtprocess.h"
14   #include "bsdfrep.h"
15  
16   const float     colarr[6][3] = {
# Line 26 | Line 28 | char   *progname;
28   int
29   main(int argc, char *argv[])
30   {
31 <        char    buf[128];
31 >        int     showPeaks = 0;
32 >        int     doTrans = 0;
33 >        int     inpXML = -1;
34 >        RBFNODE *rbf = NULL;
35          FILE    *fp;
36 <        RBFNODE *rbf;
36 >        char    buf[128];
37 >        SDData  myBSDF;
38          double  bsdf, min_log;
39 <        FVECT   dir;
39 >        FVECT   idir, odir;
40          int     i, j, n;
41 <
41 >                                                /* check arguments */
42          progname = argv[0];
43 <        if (argc < 4) {
44 <                fprintf(stderr, "Usage: %s bsdf.sir theta1 phi1 .. > output.rad\n", argv[0]);
43 >        if (argc > 1 && !strcmp(argv[1], "-p")) {
44 >                ++showPeaks;
45 >                ++argv; --argc;
46 >        }
47 >        if (argc > 1 && !strcmp(argv[1], "-t")) {
48 >                ++doTrans;
49 >                ++argv; --argc;
50 >        }
51 >        if (argc >= 4 && (n = strlen(argv[1])-4) > 0) {
52 >                if (!strcasecmp(argv[1]+n, ".xml"))
53 >                        inpXML = 1;
54 >                else if (!strcasecmp(argv[1]+n, ".sir"))
55 >                        inpXML = 0;
56 >        }
57 >        if (inpXML < 0) {
58 >                fprintf(stderr, "Usage: %s [-p] bsdf.sir theta1 phi1 .. > output.rad\n", progname);
59 >                fprintf(stderr, "   Or: %s [-t] bsdf.xml theta1 phi1 .. > output.rad\n", progname);
60                  return(1);
61          }
62                                                  /* load input */
63 <        if ((fp = fopen(argv[1], "rb")) == NULL) {
64 <                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
65 <                                argv[0], argv[1]);
66 <                return(1);
63 >        if (inpXML) {
64 >                SDclearBSDF(&myBSDF, argv[1]);
65 >                if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
66 >                        return(1);
67 >                bsdf_min = 1./M_PI;
68 >                if (myBSDF.rf != NULL && myBSDF.rLambFront.cieY < bsdf_min*M_PI)
69 >                        bsdf_min = myBSDF.rLambFront.cieY/M_PI;
70 >                if (myBSDF.rb != NULL && myBSDF.rLambBack.cieY < bsdf_min*M_PI)
71 >                        bsdf_min = myBSDF.rLambBack.cieY/M_PI;
72 >                if ((myBSDF.tf != NULL) | (myBSDF.tb != NULL) &&
73 >                                myBSDF.tLamb.cieY < bsdf_min*M_PI)
74 >                        bsdf_min = myBSDF.tLamb.cieY/M_PI;
75 >                if (doTrans && (myBSDF.tf == NULL) & (myBSDF.tb == NULL)) {
76 >                        fprintf(stderr, "%s: no transmitted component in '%s'\n",
77 >                                        progname, argv[1]);
78 >                        return(1);
79 >                }
80 >        } else {
81 >                fp = fopen(argv[1], "rb");
82 >                if (fp == NULL) {
83 >                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
84 >                                        progname, argv[1]);
85 >                        return(1);
86 >                }
87 >                if (!load_bsdf_rep(fp))
88 >                        return(1);
89 >                fclose(fp);
90          }
91 <        if (!load_bsdf_rep(fp))
92 <                return(1);
93 <        fclose(fp);
94 <        min_log = log(bsdf_min*.5);
95 <                                                /* output surface(s) */
91 > #ifdef DEBUG
92 >        fprintf(stderr, "Minimum BSDF set to %.4f\n", bsdf_min);
93 > #endif
94 >        min_log = log(bsdf_min*.5 + 1e-5);
95 >                                                /* output BSDF rep. */
96          for (n = 0; (n < 6) & (2*n+3 < argc); n++) {
97 +                double  theta = atof(argv[2*n+2]);
98 +                if (inpXML) {
99 +                        input_orient = (theta <= 90.) ? 1 : -1;
100 +                        output_orient = doTrans ? -input_orient : input_orient;
101 +                }
102 +                idir[2] = sin((M_PI/180.)*theta);
103 +                idir[0] = idir[2] * cos((M_PI/180.)*atof(argv[2*n+3]));
104 +                idir[1] = idir[2] * sin((M_PI/180.)*atof(argv[2*n+3]));
105 +                idir[2] = input_orient * sqrt(1. - idir[2]*idir[2]);
106 + #ifdef DEBUG
107 +                fprintf(stderr, "Computing BSDF for incident direction (%.1f,%.1f)\n",
108 +                                get_theta180(idir), get_phi360(idir));
109 + #endif
110 +                if (!inpXML)
111 +                        rbf = advect_rbf(idir, 15000);
112 + #ifdef DEBUG
113 +                if (inpXML)
114 +                        fprintf(stderr, "Hemispherical %s: %.3f\n",
115 +                                (output_orient > 0 ? "reflection" : "transmission"),
116 +                                SDdirectHemi(idir, SDsampSp|SDsampDf |
117 +                                                (output_orient > 0 ?
118 +                                                 SDsampR : SDsampT), &myBSDF));
119 +                else if (rbf == NULL)
120 +                        fputs("Empty RBF\n", stderr);
121 +                else
122 +                        fprintf(stderr, "Hemispherical %s: %.3f\n",
123 +                                (output_orient > 0 ? "reflection" : "transmission"),
124 +                                rbf->vtotal);
125 + #endif
126                  printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n",
127                                  colarr[n][0], colarr[n][1], colarr[n][2]);
128 +                if (showPeaks && rbf != NULL) {
129 +                        printf("void plastic pmat\n0\n0\n5 %f %f %f .04 .08\n",
130 +                                1.-colarr[n][0], 1.-colarr[n][1], 1.-colarr[n][2]);
131 +                        for (i = 0; i < rbf->nrbf; i++) {
132 +                                ovec_from_pos(odir, rbf->rbfa[i].gx, rbf->rbfa[i].gy);
133 +                                bsdf = eval_rbfrep(rbf, odir) / (output_orient*odir[2]);
134 +                                bsdf = log(bsdf) - min_log;
135 +                                printf("pmat sphere p%d\n0\n0\n4 %f %f %f %f\n",
136 +                                        i+1, odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf,
137 +                                                .007*bsdf);
138 +                        }
139 +                }
140                  fflush(stdout);
141                  sprintf(buf, "gensurf tmat bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
142                  fp = popen(buf, "w");
143                  if (fp == NULL) {
144 <                        fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf);
144 >                        fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
145                          return(1);
146                  }
62                dir[2] = sin((M_PI/180.)*atof(argv[2*n+2]));
63                dir[0] = dir[2] * cos((M_PI/180.)*atof(argv[2*n+3]));
64                dir[1] = dir[2] * sin((M_PI/180.)*atof(argv[2*n+3]));
65                dir[2] = input_orient * sqrt(1. - dir[2]*dir[2]);
66                fprintf(stderr, "Computing DSF for incident direction (%.1f,%.1f)\n",
67                                get_theta180(dir), get_phi360(dir));
68                rbf = advect_rbf(dir, 15000);
69                if (rbf == NULL)
70                        fputs("NULL RBF\n", stderr);
71                else
72                        fprintf(stderr, "Hemispherical reflectance: %.3f\n", rbf->vtotal);
147                  for (i = 0; i < GRIDRES; i++)
148                      for (j = 0; j < GRIDRES; j++) {
149 <                        ovec_from_pos(dir, i, j);
150 <                        bsdf = eval_rbfrep(rbf, dir) / (output_orient*dir[2]);
149 >                        ovec_from_pos(odir, i, j);
150 >                        if (inpXML) {
151 >                                SDValue sval;
152 >                                if (SDreportError(SDevalBSDF(&sval, odir,
153 >                                                        idir, &myBSDF), stderr))
154 >                                        return(1);
155 >                                bsdf = sval.cieY;
156 >                        } else
157 >                                bsdf = eval_rbfrep(rbf, odir) /
158 >                                                (output_orient*odir[2]);
159                          bsdf = log(bsdf) - min_log;
160                          fprintf(fp, "%.8e %.8e %.8e\n",
161 <                                        dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
161 >                                        odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf);
162                      }
163                  if (rbf != NULL)
164                          free(rbf);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines