ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
Revision: 2.27
Committed: Fri Oct 3 21:57:06 2014 UTC (9 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0
Changes since 2.26: +10 -3 lines
Log Message:
Added default 90-degree rotation to align PAB's up vector to BSDF standard

File Contents

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