ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2bsdf.c
Revision: 2.4
Committed: Sun Jun 30 14:46:29 2013 UTC (10 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +3 -1 lines
Log Message:
Added normalization step for azimuth angle

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.3 2013/06/29 22:38:25 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 if (fgets(buf, sizeof(buf), fp) == NULL) {
66 fputs(fname, stderr);
67 fputs(": unexpected EOF\n", stderr);
68 fclose(fp);
69 return(0);
70 }
71 if (!strcmp(buf, "format: theta phi DSF\n")) {
72 inpfile[i].isDSF = 1;
73 continue;
74 }
75 if (!strcmp(buf, "format: theta phi BSDF\n")) {
76 inpfile[i].isDSF = 0;
77 continue;
78 }
79 if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
80 continue;
81 if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
82 continue;
83 if (sscanf(buf, "incident_angle %lf %lf",
84 &inpfile[i].theta, &inpfile[i].phi) == 2)
85 continue;
86 }
87 inpfile[i].dstart = ftell(fp) - 1;
88 fclose(fp);
89 if (inpfile[i].isDSF < 0) {
90 fputs(fname, stderr);
91 fputs(": unknown format\n", stderr);
92 return(0);
93 }
94 if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
95 fputs(fname, stderr);
96 fputs(": unknown incident angle\n", stderr);
97 return(0);
98 }
99 while (inpfile[i].phi < 0) /* normalize phi direction */
100 inpfile[i].phi += 360.;
101 return(1);
102 }
103
104 /* Load a set of measurements corresponding to a particular incident angle */
105 static int
106 add_pabopto_inp(const int i)
107 {
108 FILE *fp = fopen(inpfile[i].fname, "r");
109 double theta_out, phi_out, val;
110 int n, c;
111
112 if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
113 fputs(inpfile[i].fname, stderr);
114 fputs(": cannot open\n", stderr);
115 return(0);
116 }
117 /* prepare input grid */
118 if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) {
119 if (i) /* need to process previous incidence */
120 make_rbfrep();
121 #ifdef DEBUG
122 fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n",
123 inpfile[i].theta, inpfile[i].phi);
124 #endif
125 new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
126 }
127 #ifdef DEBUG
128 fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
129 #endif
130 /* read scattering data */
131 while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3)
132 add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
133 n = 0;
134 while ((c = getc(fp)) != EOF)
135 n += !isspace(c);
136 if (n)
137 fprintf(stderr,
138 "%s: warning: %d unexpected characters past EOD\n",
139 inpfile[i].fname, n);
140 fclose(fp);
141 return(1);
142 }
143
144 /* Read in PAB-Opto BSDF files and output RBF interpolant */
145 int
146 main(int argc, char *argv[])
147 {
148 extern int nprocs;
149 int i;
150 /* start header */
151 SET_FILE_BINARY(stdout);
152 newheader("RADIANCE", stdout);
153 printargs(argc, argv, stdout);
154 fputnow(stdout);
155 progname = argv[0]; /* get options */
156 while (argc > 2 && argv[1][0] == '-') {
157 switch (argv[1][1]) {
158 case 'n':
159 nprocs = atoi(argv[2]);
160 break;
161 default:
162 goto userr;
163 }
164 argv += 2; argc -= 2;
165 }
166 /* initialize & sort inputs */
167 ninpfiles = argc - 1;
168 if (ninpfiles < 2)
169 goto userr;
170 inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles);
171 if (inpfile == NULL)
172 return(1);
173 for (i = 0; i < ninpfiles; i++)
174 if (!init_pabopto_inp(i, argv[i+1]))
175 return(1);
176 qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang);
177 /* compile measurements */
178 for (i = 0; i < ninpfiles; i++)
179 if (!add_pabopto_inp(i))
180 return(1);
181 make_rbfrep(); /* process last data set */
182 build_mesh(); /* create interpolation */
183 save_bsdf_rep(stdout); /* write it out */
184 return(0);
185 userr:
186 fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n",
187 progname);
188 return(1);
189 }