ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
Revision: 2.22
Committed: Fri Mar 21 23:11:44 2014 UTC (10 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +2 -2 lines
Log Message:
Fixed format on debug statement

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.21 2014/03/21 01:04:42 greg Exp $";
3 #endif
4 /*
5 * Load measured BSDF data in PAB-Opto format.
6 *
7 * G. Ward
8 */
9
10 #define _USE_MATH_DEFINES
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <math.h>
16 #include "platform.h"
17 #include "bsdfrep.h"
18 #include "resolu.h"
19 /* global argv[0] */
20 char *progname;
21
22 typedef struct {
23 const char *fname; /* input file path */
24 double theta, phi; /* input angles */
25 int igp[2]; /* input grid position */
26 int isDSF; /* data is DSF (rather than BSDF)? */
27 long dstart; /* data start offset in file */
28 } PGINPUT;
29
30 PGINPUT *inpfile; /* input files sorted by incidence */
31 int ninpfiles; /* number of input files */
32
33 /* Compare incident angles */
34 static int
35 cmp_indir(const void *p1, const void *p2)
36 {
37 const PGINPUT *inp1 = (const PGINPUT *)p1;
38 const PGINPUT *inp2 = (const PGINPUT *)p2;
39 int ydif = inp1->igp[1] - inp2->igp[1];
40
41 if (ydif)
42 return(ydif);
43
44 return(inp1->igp[0] - inp2->igp[0]);
45 }
46
47 /* Prepare a PAB-Opto input file by reading its header */
48 static int
49 init_pabopto_inp(const int i, const char *fname)
50 {
51 FILE *fp = fopen(fname, "r");
52 FVECT dv;
53 char buf[2048];
54 int c;
55
56 if (fp == NULL) {
57 fputs(fname, stderr);
58 fputs(": cannot open\n", stderr);
59 return(0);
60 }
61 inpfile[i].fname = fname;
62 inpfile[i].isDSF = -1;
63 inpfile[i].theta = inpfile[i].phi = -10001.;
64 /* read header information */
65 while ((c = getc(fp)) == '#' || c == EOF) {
66 char typ[32];
67 if (fgets(buf, sizeof(buf), fp) == NULL) {
68 fputs(fname, stderr);
69 fputs(": unexpected EOF\n", stderr);
70 fclose(fp);
71 return(0);
72 }
73 if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1)
74 continue;
75 if (sscanf(buf, "format: theta phi %s", typ) == 1) {
76 if (!strcasecmp(typ, "DSF")) {
77 inpfile[i].isDSF = 1;
78 continue;
79 }
80 if (!strcasecmp(typ, "BSDF")) {
81 inpfile[i].isDSF = 0;
82 continue;
83 }
84 }
85 if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
86 continue;
87 if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
88 continue;
89 if (sscanf(buf, "incident_angle %lf %lf",
90 &inpfile[i].theta, &inpfile[i].phi) == 2)
91 continue;
92 }
93 inpfile[i].dstart = ftell(fp) - 1;
94 fclose(fp);
95 if (inpfile[i].isDSF < 0) {
96 fputs(fname, stderr);
97 fputs(": unknown format\n", stderr);
98 return(0);
99 }
100 if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
101 fputs(fname, stderr);
102 fputs(": unknown incident angle\n", stderr);
103 return(0);
104 }
105 /* convert angle to grid position */
106 dv[2] = sin(M_PI/180.*inpfile[i].theta);
107 dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2];
108 dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2];
109 dv[2] = sqrt(1. - dv[2]*dv[2]);
110 pos_from_vec(inpfile[i].igp, dv);
111 return(1);
112 }
113
114 /* Load a set of measurements corresponding to a particular incident angle */
115 static int
116 add_pabopto_inp(const int i)
117 {
118 FILE *fp = fopen(inpfile[i].fname, "r");
119 double theta_out, phi_out, val;
120 int n, c;
121
122 if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
123 fputs(inpfile[i].fname, stderr);
124 fputs(": cannot open\n", stderr);
125 return(0);
126 }
127 /* prepare input grid */
128 if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) {
129 if (i) /* process previous incidence */
130 make_rbfrep();
131 #ifdef DEBUG
132 fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n",
133 inpfile[i].theta, inpfile[i].phi);
134 #endif
135 new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
136 }
137 #ifdef DEBUG
138 fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
139 #endif
140 /* read scattering data */
141 while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
142 add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
143 n = 0;
144 while ((c = getc(fp)) != EOF)
145 n += !isspace(c);
146 if (n)
147 fprintf(stderr,
148 "%s: warning: %d unexpected characters past EOD\n",
149 inpfile[i].fname, n);
150 fclose(fp);
151 return(1);
152 }
153
154 #ifndef TEST_MAIN
155 /* Read in PAB-Opto BSDF files and output RBF interpolant */
156 int
157 main(int argc, char *argv[])
158 {
159 extern int nprocs;
160 int i;
161 /* start header */
162 SET_FILE_BINARY(stdout);
163 newheader("RADIANCE", stdout);
164 printargs(argc, argv, stdout);
165 fputnow(stdout);
166 progname = argv[0]; /* get options */
167 while (argc > 2 && argv[1][0] == '-') {
168 switch (argv[1][1]) {
169 case 'n':
170 nprocs = atoi(argv[2]);
171 break;
172 default:
173 goto userr;
174 }
175 argv += 2; argc -= 2;
176 }
177 /* initialize & sort inputs */
178 ninpfiles = argc - 1;
179 if (ninpfiles < 2)
180 goto userr;
181 inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles);
182 if (inpfile == NULL)
183 return(1);
184 for (i = 0; i < ninpfiles; i++)
185 if (!init_pabopto_inp(i, argv[i+1]))
186 return(1);
187 qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_indir);
188 /* compile measurements */
189 for (i = 0; i < ninpfiles; i++)
190 if (!add_pabopto_inp(i))
191 return(1);
192 make_rbfrep(); /* process last data set */
193 build_mesh(); /* create interpolation */
194 save_bsdf_rep(stdout); /* write it out */
195 return(0);
196 userr:
197 fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
198 progname);
199 return(1);
200 }
201 #else
202 /* Test main produces a Radiance model from the given input file */
203 int
204 main(int argc, char *argv[])
205 {
206 PGINPUT pginp;
207 char buf[128];
208 FILE *pfp;
209 double bsdf, min_log;
210 FVECT dir;
211 int i, j, n;
212
213 progname = argv[0];
214 if (argc != 2) {
215 fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname);
216 return(1);
217 }
218 ninpfiles = 1;
219 inpfile = &pginp;
220 if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
221 return(1);
222 /* reduce data set */
223 if (make_rbfrep() == NULL) {
224 fprintf(stderr, "%s: nothing to plot!\n", progname);
225 exit(1);
226 }
227 #ifdef DEBUG
228 fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
229 #endif
230 min_log = log(bsdf_min*.5 + 1e-5);
231 #if 1 /* produce spheres at meas. */
232 puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
233 n = 0;
234 for (i = 0; i < GRIDRES; i++)
235 for (j = 0; j < GRIDRES; j++)
236 if (dsf_grid[i][j].sum.n > 0) {
237 ovec_from_pos(dir, i, j);
238 bsdf = dsf_grid[i][j].sum.v /
239 (dsf_grid[i][j].sum.n*output_orient*dir[2]);
240 if (bsdf <= bsdf_min*.6)
241 continue;
242 bsdf = log(bsdf + 1e-5) - min_log;
243 ovec_from_pos(dir, i, j);
244 printf("yellow sphere s%04d\n0\n0\n", ++n);
245 printf("4 %.6g %.6g %.6g %.6g\n\n",
246 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
247 .007*bsdf);
248 }
249 #endif
250 #if 1 /* spheres at RBF peaks */
251 puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
252 for (n = 0; n < dsf_list->nrbf; n++) {
253 RBFVAL *rbf = &dsf_list->rbfa[n];
254 ovec_from_pos(dir, rbf->gx, rbf->gy);
255 bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
256 bsdf = log(bsdf + 1e-5) - min_log;
257 printf("red sphere p%04d\n0\n0\n", ++n);
258 printf("4 %.6g %.6g %.6g %.6g\n\n",
259 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
260 .011*bsdf);
261 }
262 #endif
263 #if 1 /* output continuous surface */
264 puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
265 fflush(stdout);
266 sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
267 pfp = popen(buf, "w");
268 if (pfp == NULL) {
269 fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
270 return(1);
271 }
272 for (i = 0; i < GRIDRES; i++)
273 for (j = 0; j < GRIDRES; j++) {
274 ovec_from_pos(dir, i, j);
275 bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
276 bsdf = log(bsdf + 1e-5) - min_log;
277 fprintf(pfp, "%.8e %.8e %.8e\n",
278 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
279 }
280 if (pclose(pfp) != 0)
281 return(1);
282 #endif
283 return(0);
284 }
285 #endif