ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.9
Committed: Mon Mar 24 06:07:46 2014 UTC (10 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +12 -6 lines
Log Message:
Minor fixes

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdf2rad.c,v 2.8 2014/03/24 05:36:40 greg Exp $";
3 #endif
4 /*
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] = {
17 .7, 1., .7,
18 1., .7, .7,
19 .7, .7, 1.,
20 1., .5, 1.,
21 1., 1., .5,
22 .5, 1., 1.
23 };
24
25 char *progname;
26
27 /* Produce a Radiance model plotting the indicated incident direction(s) */
28 int
29 main(int argc, char *argv[])
30 {
31 int showPeaks = 0;
32 int doTrans = 0;
33 int inpXML = -1;
34 RBFNODE *rbf = NULL;
35 FILE *fp;
36 char buf[128];
37 SDData myBSDF;
38 double bsdf, min_log;
39 FVECT idir, odir;
40 int i, j, n;
41 /* check arguments */
42 progname = 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 (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 #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 = (M_PI/180.)*atof(argv[2*n+2]);
98 double phi = (M_PI/180.)*atof(argv[2*n+3]);
99 if (theta < -FTINY) {
100 fprintf(stderr, "%s: theta values must be positive\n",
101 progname);
102 return(1);
103 }
104 if (inpXML) {
105 input_orient = (theta <= M_PI/2.) ? 1 : -1;
106 output_orient = doTrans ? -input_orient : input_orient;
107 }
108 idir[2] = sin(theta);
109 idir[0] = idir[2] * cos(phi);
110 idir[1] = idir[2] * sin(phi);
111 idir[2] = input_orient * sqrt(1. - idir[2]*idir[2]);
112 #ifdef DEBUG
113 fprintf(stderr, "Computing BSDF for incident direction (%.1f,%.1f)\n",
114 get_theta180(idir), get_phi360(idir));
115 #endif
116 if (!inpXML)
117 rbf = advect_rbf(idir, 15000);
118 #ifdef DEBUG
119 if (inpXML)
120 fprintf(stderr, "Hemispherical %s: %.3f\n",
121 (output_orient > 0 ? "reflection" : "transmission"),
122 SDdirectHemi(idir, SDsampSp|SDsampDf |
123 (output_orient > 0 ?
124 SDsampR : SDsampT), &myBSDF));
125 else if (rbf == NULL)
126 fputs("Empty RBF\n", stderr);
127 else
128 fprintf(stderr, "Hemispherical %s: %.3f\n",
129 (output_orient > 0 ? "reflection" : "transmission"),
130 rbf->vtotal);
131 #endif
132 printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n",
133 colarr[n][0], colarr[n][1], colarr[n][2]);
134 if (showPeaks && rbf != NULL) {
135 printf("void plastic pmat\n0\n0\n5 %f %f %f .04 .08\n",
136 1.-colarr[n][0], 1.-colarr[n][1], 1.-colarr[n][2]);
137 for (i = 0; i < rbf->nrbf; i++) {
138 ovec_from_pos(odir, rbf->rbfa[i].gx, rbf->rbfa[i].gy);
139 bsdf = eval_rbfrep(rbf, odir) / (output_orient*odir[2]);
140 bsdf = log(bsdf + 1e-5) - min_log;
141 printf("pmat sphere p%d\n0\n0\n4 %f %f %f %f\n",
142 i+1, odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf,
143 .007*bsdf);
144 }
145 }
146 fflush(stdout);
147 sprintf(buf, "gensurf tmat bsdf%d - - - %d %d", n+1,
148 GRIDRES-1, GRIDRES-1);
149 fp = popen(buf, "w");
150 if (fp == NULL) {
151 fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
152 return(1);
153 }
154 for (i = 0; i < GRIDRES; i++)
155 for (j = 0; j < GRIDRES; j++) {
156 ovec_from_pos(odir, i, j);
157 if (inpXML) {
158 SDValue sval;
159 if (SDreportError(SDevalBSDF(&sval, odir,
160 idir, &myBSDF), stderr))
161 return(1);
162 bsdf = sval.cieY;
163 } else
164 bsdf = eval_rbfrep(rbf, odir) /
165 (output_orient*odir[2]);
166 bsdf = log(bsdf + 1e-5) - min_log;
167 fprintf(fp, "%.8e %.8e %.8e\n",
168 odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf);
169 }
170 if (rbf != NULL)
171 free(rbf);
172 if (pclose(fp))
173 return(1);
174 }
175 return(0);
176 }