ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
Revision: 2.5
Committed: Thu Sep 12 16:09:31 2013 UTC (10 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +11 -8 lines
Log Message:
Fixed brittle parser that was not recognizing format from different systems

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.4 2013/06/30 14:46:29 greg Exp $";
3 #endif
4 /*
5 * Load measured BSDF data in PAB-Opto format.
6 *
7 * G. Ward
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "platform.h"
15 #include "bsdfrep.h"
16 /* global argv[0] */
17 char *progname;
18
19 typedef struct {
20 const char *fname; /* input file path */
21 double theta, phi; /* incident angles (in degrees) */
22 int isDSF; /* data is DSF (rather than BSDF)? */
23 long dstart; /* data start offset in file */
24 } PGINPUT;
25
26 PGINPUT *inpfile; /* input files sorted by incidence */
27 int ninpfiles; /* number of input files */
28
29 /* Compare incident angles */
30 static int
31 cmp_inang(const void *p1, const void *p2)
32 {
33 const PGINPUT *inp1 = (const PGINPUT *)p1;
34 const PGINPUT *inp2 = (const PGINPUT *)p2;
35
36 if (inp1->theta > inp2->theta+FTINY)
37 return(1);
38 if (inp1->theta < inp2->theta-FTINY)
39 return(-1);
40 if (inp1->phi > inp2->phi+FTINY)
41 return(1);
42 if (inp1->phi < inp2->phi-FTINY)
43 return(-1);
44 return(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 char buf[2048];
53 int c;
54
55 if (fp == NULL) {
56 fputs(fname, stderr);
57 fputs(": cannot open\n", stderr);
58 return(0);
59 }
60 inpfile[i].fname = fname;
61 inpfile[i].isDSF = -1;
62 inpfile[i].theta = inpfile[i].phi = -10001.;
63 /* read header information */
64 while ((c = getc(fp)) == '#' || c == EOF) {
65 char typ[32];
66 if (fgets(buf, sizeof(buf), fp) == NULL) {
67 fputs(fname, stderr);
68 fputs(": unexpected EOF\n", stderr);
69 fclose(fp);
70 return(0);
71 }
72 if (sscanf(buf, "format: theta phi %s", typ) == 1) {
73 if (!strcasecmp(typ, "DSF")) {
74 inpfile[i].isDSF = 1;
75 continue;
76 }
77 if (!strcasecmp(typ, "BSDF")) {
78 inpfile[i].isDSF = 0;
79 continue;
80 }
81 }
82 if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
83 continue;
84 if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
85 continue;
86 if (sscanf(buf, "incident_angle %lf %lf",
87 &inpfile[i].theta, &inpfile[i].phi) == 2)
88 continue;
89 }
90 inpfile[i].dstart = ftell(fp) - 1;
91 fclose(fp);
92 if (inpfile[i].isDSF < 0) {
93 fputs(fname, stderr);
94 fputs(": unknown format\n", stderr);
95 return(0);
96 }
97 if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
98 fputs(fname, stderr);
99 fputs(": unknown incident angle\n", stderr);
100 return(0);
101 }
102 while (inpfile[i].phi < 0) /* normalize phi direction */
103 inpfile[i].phi += 360.;
104 return(1);
105 }
106
107 /* Load a set of measurements corresponding to a particular incident angle */
108 static int
109 add_pabopto_inp(const int i)
110 {
111 FILE *fp = fopen(inpfile[i].fname, "r");
112 double theta_out, phi_out, val;
113 int n, c;
114
115 if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
116 fputs(inpfile[i].fname, stderr);
117 fputs(": cannot open\n", stderr);
118 return(0);
119 }
120 /* prepare input grid */
121 if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) {
122 if (i) /* need to process previous incidence */
123 make_rbfrep();
124 #ifdef DEBUG
125 fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n",
126 inpfile[i].theta, inpfile[i].phi);
127 #endif
128 new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
129 }
130 #ifdef DEBUG
131 fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
132 #endif
133 /* read scattering data */
134 while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
135 add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
136 n = 0;
137 while ((c = getc(fp)) != EOF)
138 n += !isspace(c);
139 if (n)
140 fprintf(stderr,
141 "%s: warning: %d unexpected characters past EOD\n",
142 inpfile[i].fname, n);
143 fclose(fp);
144 return(1);
145 }
146
147 /* Read in PAB-Opto BSDF files and output RBF interpolant */
148 int
149 main(int argc, char *argv[])
150 {
151 extern int nprocs;
152 int i;
153 /* start header */
154 SET_FILE_BINARY(stdout);
155 newheader("RADIANCE", stdout);
156 printargs(argc, argv, stdout);
157 fputnow(stdout);
158 progname = argv[0]; /* get options */
159 while (argc > 2 && argv[1][0] == '-') {
160 switch (argv[1][1]) {
161 case 'n':
162 nprocs = atoi(argv[2]);
163 break;
164 default:
165 goto userr;
166 }
167 argv += 2; argc -= 2;
168 }
169 /* initialize & sort inputs */
170 ninpfiles = argc - 1;
171 if (ninpfiles < 2)
172 goto userr;
173 inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles);
174 if (inpfile == NULL)
175 return(1);
176 for (i = 0; i < ninpfiles; i++)
177 if (!init_pabopto_inp(i, argv[i+1]))
178 return(1);
179 qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang);
180 /* compile measurements */
181 for (i = 0; i < ninpfiles; i++)
182 if (!add_pabopto_inp(i))
183 return(1);
184 make_rbfrep(); /* process last data set */
185 build_mesh(); /* create interpolation */
186 save_bsdf_rep(stdout); /* write it out */
187 return(0);
188 userr:
189 fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
190 progname);
191 return(1);
192 }