ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
Revision: 2.20
Committed: Thu Mar 20 16:54:23 2014 UTC (10 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.19: +20 -23 lines
Log Message:
Hopefully final change to angle collusion method

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.19 2014/03/19 19:48:57 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)=(%f,%f)\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 if (argc != 2) {
214 fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]);
215 return(1);
216 }
217 ninpfiles = 1;
218 inpfile = &pginp;
219 if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
220 return(1);
221 /* reduce data set */
222 make_rbfrep();
223 #ifdef DEBUG
224 fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
225 #endif
226 min_log = log(bsdf_min*.5 + 1e-5);
227 #if 1 /* produce spheres at meas. */
228 puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
229 n = 0;
230 for (i = 0; i < GRIDRES; i++)
231 for (j = 0; j < GRIDRES; j++)
232 if (dsf_grid[i][j].sum.n > 0) {
233 ovec_from_pos(dir, i, j);
234 bsdf = dsf_grid[i][j].sum.v /
235 (dsf_grid[i][j].sum.n*output_orient*dir[2]);
236 if (bsdf <= bsdf_min*.6)
237 continue;
238 bsdf = log(bsdf + 1e-5) - min_log;
239 ovec_from_pos(dir, i, j);
240 printf("yellow sphere s%04d\n0\n0\n", ++n);
241 printf("4 %.6g %.6g %.6g %.6g\n\n",
242 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
243 .007*bsdf);
244 }
245 #endif
246 #if 1 /* spheres at RBF peaks */
247 puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
248 for (n = 0; n < dsf_list->nrbf; n++) {
249 RBFVAL *rbf = &dsf_list->rbfa[n];
250 ovec_from_pos(dir, rbf->gx, rbf->gy);
251 bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
252 bsdf = log(bsdf + 1e-5) - min_log;
253 printf("red sphere p%04d\n0\n0\n", ++n);
254 printf("4 %.6g %.6g %.6g %.6g\n\n",
255 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
256 .011*bsdf);
257 }
258 #endif
259 #if 1 /* output continuous surface */
260 puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
261 fflush(stdout);
262 sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1);
263 pfp = popen(buf, "w");
264 if (pfp == NULL) {
265 fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf);
266 return(1);
267 }
268 for (i = 0; i < GRIDRES; i++)
269 for (j = 0; j < GRIDRES; j++) {
270 ovec_from_pos(dir, i, j);
271 bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]);
272 bsdf = log(bsdf + 1e-5) - min_log;
273 fprintf(pfp, "%.8e %.8e %.8e\n",
274 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
275 }
276 if (pclose(pfp) != 0)
277 return(1);
278 #endif
279 return(0);
280 }
281 #endif