ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pabopto2xml.c
Revision: 2.2
Committed: Fri Aug 24 20:55:28 2012 UTC (11 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +64 -63 lines
Log Message:
Getting closer on initial estimate -- need to add optimization

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pabopto2xml.c,v 2.1 2012/08/24 15:20:18 greg Exp $";
3 #endif
4 /*
5 * Convert PAB-Opto measurements to XML format using tensor tree representation
6 * Employs Bonneel et al. Earth Mover's Distance interpolant.
7 *
8 * G.Ward
9 */
10
11 #define _USE_MATH_DEFINES
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <math.h>
17 #include "bsdf.h"
18
19 #ifndef GRIDRES
20 #define GRIDRES 200 /* max. grid resolution per side */
21 #endif
22
23 #define RSCA 3. /* radius scaling factor (empirical) */
24 #define MSCA .2 /* magnitude scaling (empirical) */
25
26 #define R2ANG(c) (((c)+.5)*(M_PI/(1<<16)))
27 #define ANG2R(r) (int)((r)*((1<<16)/M_PI))
28
29 typedef struct {
30 float vsum; /* BSDF sum */
31 unsigned short nval; /* number of values in sum */
32 unsigned short crad; /* radius (coded angle) */
33 } GRIDVAL; /* grid value */
34
35 typedef struct {
36 float bsdf; /* lobe value at peak */
37 unsigned short crad; /* radius (coded angle) */
38 unsigned char gx, gy; /* grid position */
39 } RBFVAL; /* radial basis function value */
40
41 typedef struct s_rbflist {
42 struct s_rbflist *next; /* next in our RBF list */
43 FVECT invec; /* incident vector direction */
44 int nrbf; /* number of RBFs */
45 RBFVAL rbfa[1]; /* RBF array (extends struct) */
46 } RBFLIST; /* RBF representation of BSDF @ 1 incidence */
47
48 /* our loaded grid for this incident angle */
49 static double theta_in_deg, phi_in_deg;
50 static GRIDVAL bsdf_grid[GRIDRES][GRIDRES];
51
52 /* processed incident BSDF measurements */
53 static RBFLIST *bsdf_list = NULL;
54
55 /* Count up non-empty nodes and build RBF representation from current grid */
56 static RBFLIST *
57 make_rbfrep(void)
58 {
59 int nn = 0;
60 RBFLIST *newnode;
61 int i, j;
62 /* count non-empty bins */
63 for (i = 0; i < GRIDRES; i++)
64 for (j = 0; j < GRIDRES; j++)
65 nn += (bsdf_grid[i][j].nval > 0);
66 /* allocate RBF array */
67 newnode = (RBFLIST *)malloc(sizeof(RBFLIST) + sizeof(RBFVAL)*(nn-1));
68 if (newnode == NULL) {
69 fputs("Out of memory in make_rbfrep\n", stderr);
70 exit(1);
71 }
72 newnode->invec[2] = sin(M_PI/180.*theta_in_deg);
73 newnode->invec[0] = cos(M_PI/180.*phi_in_deg)*newnode->invec[2];
74 newnode->invec[1] = sin(M_PI/180.*phi_in_deg)*newnode->invec[2];
75 newnode->invec[2] = sqrt(1. - newnode->invec[2]*newnode->invec[2]);
76 newnode->nrbf = nn;
77 nn = 0; /* fill RBF array */
78 for (i = 0; i < GRIDRES; i++)
79 for (j = 0; j < GRIDRES; j++)
80 if (bsdf_grid[i][j].nval) {
81 newnode->rbfa[nn].bsdf = MSCA*bsdf_grid[i][j].vsum /
82 (double)bsdf_grid[i][j].nval;
83 newnode->rbfa[nn].crad = RSCA*bsdf_grid[i][j].crad + .5;
84 newnode->rbfa[nn].gx = i;
85 newnode->rbfa[nn].gy = j;
86 ++nn;
87 }
88 newnode->next = bsdf_list;
89 return(bsdf_list = newnode);
90 }
91
92 /* Compute grid position from normalized outgoing vector */
93 static void
94 pos_from_vec(int pos[2], const FVECT vec)
95 {
96 double sq[2]; /* uniform hemispherical projection */
97 double norm = 1./sqrt(1. + vec[2]);
98
99 SDdisk2square(sq, vec[0]*norm, vec[1]*norm);
100
101 pos[0] = (int)(sq[0]*GRIDRES);
102 pos[1] = (int)(sq[1]*GRIDRES);
103 }
104
105 /* Compute outgoing vector from grid position */
106 static void
107 vec_from_pos(FVECT vec, int xpos, int ypos)
108 {
109 double uv[2];
110 double r2;
111
112 SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5));
113 /* uniform hemispherical projection */
114 r2 = uv[0]*uv[0] + uv[1]*uv[1];
115 vec[0] = vec[1] = sqrt(2. - r2);
116 vec[0] *= uv[0];
117 vec[1] *= uv[1];
118 vec[2] = 1. - r2;
119 }
120
121 /* Evaluate RBF for BSDF at the given normalized outgoing direction */
122 static double
123 eval_rbfrep(const RBFLIST *rp, const FVECT outvec)
124 {
125 double res = .0;
126 const RBFVAL *rbfp;
127 FVECT odir;
128 double sig2;
129 int n;
130
131 rbfp = rp->rbfa;
132 for (n = rp->nrbf; n--; rbfp++) {
133 vec_from_pos(odir, rbfp->gx, rbfp->gy);
134 sig2 = R2ANG(rbfp->crad);
135 sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2);
136 if (sig2 > -19.)
137 res += rbfp->bsdf * exp(sig2);
138 }
139 return(res);
140 }
141
142 /* Load a set of measurements corresponding to a particular incident angle */
143 static int
144 load_bsdf_meas(const char *fname)
145 {
146 FILE *fp = fopen(fname, "r");
147 int inp_is_DSF = -1;
148 double theta_out, phi_out, val;
149 char buf[2048];
150 int n, c;
151
152 if (fp == NULL) {
153 fputs(fname, stderr);
154 fputs(": cannot open\n", stderr);
155 return(0);
156 }
157 memset(bsdf_grid, 0, sizeof(bsdf_grid));
158 /* read header information */
159 while ((c = getc(fp)) == '#' || c == EOF) {
160 if (fgets(buf, sizeof(buf), fp) == NULL) {
161 fputs(fname, stderr);
162 fputs(": unexpected EOF\n", stderr);
163 fclose(fp);
164 return(0);
165 }
166 if (!strcmp(buf, "format: theta phi DSF\n")) {
167 inp_is_DSF = 1;
168 continue;
169 }
170 if (!strcmp(buf, "format: theta phi BSDF\n")) {
171 inp_is_DSF = 0;
172 continue;
173 }
174 if (sscanf(buf, "intheta %lf", &theta_in_deg) == 1)
175 continue;
176 if (sscanf(buf, "inphi %lf", &phi_in_deg) == 1)
177 continue;
178 if (sscanf(buf, "incident_angle %lf %lf",
179 &theta_in_deg, &phi_in_deg) == 2)
180 continue;
181 }
182 if (inp_is_DSF < 0) {
183 fputs(fname, stderr);
184 fputs(": unknown format\n", stderr);
185 fclose(fp);
186 return(0);
187 }
188 ungetc(c, fp); /* read actual data */
189 while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) {
190 FVECT ovec;
191 int pos[2];
192
193 ovec[2] = sin(M_PI/180.*theta_out);
194 ovec[0] = cos(M_PI/180.*phi_out) * ovec[2];
195 ovec[1] = sin(M_PI/180.*phi_out) * ovec[2];
196 ovec[2] = sqrt(1. - ovec[2]*ovec[2]);
197
198 if (inp_is_DSF)
199 val /= ovec[2]; /* convert from DSF to BSDF */
200
201 pos_from_vec(pos, ovec);
202
203 bsdf_grid[pos[0]][pos[1]].vsum += val;
204 bsdf_grid[pos[0]][pos[1]].nval++;
205 }
206 n = 0;
207 while ((c = getc(fp)) != EOF)
208 n += !isspace(c);
209 if (n)
210 fprintf(stderr,
211 "%s: warning: %d unexpected characters past EOD\n",
212 fname, n);
213 fclose(fp);
214 return(1);
215 }
216
217 /* Compute radii for non-empty bins */
218 /* (distance to furthest empty bin for which non-empty bin is the closest) */
219 static void
220 compute_radii(void)
221 {
222 unsigned short fill_grid[GRIDRES][GRIDRES];
223 FVECT ovec0, ovec1;
224 double ang2, lastang2;
225 int r2, lastr2;
226 int r, i, j, jn, ii, jj, inear, jnear;
227
228 r = GRIDRES/2; /* proceed in zig-zag */
229 for (i = 0; i < GRIDRES; i++)
230 for (jn = 0; jn < GRIDRES; jn++) {
231 j = (i&1) ? jn : GRIDRES-1-jn;
232 if (bsdf_grid[i][j].nval) /* find empty grid pos. */
233 continue;
234 vec_from_pos(ovec0, i, j);
235 inear = jnear = -1; /* find nearest non-empty */
236 lastang2 = M_PI*M_PI;
237 for (ii = i-r; ii <= i+r; ii++) {
238 if (ii < 0) continue;
239 if (ii >= GRIDRES) break;
240 for (jj = j-r; jj <= j+r; jj++) {
241 if (jj < 0) continue;
242 if (jj >= GRIDRES) break;
243 if (!bsdf_grid[ii][jj].nval)
244 continue;
245 vec_from_pos(ovec1, ii, jj);
246 ang2 = 2. - 2.*DOT(ovec0,ovec1);
247 if (ang2 >= lastang2)
248 continue;
249 lastang2 = ang2;
250 inear = ii; jnear = jj;
251 }
252 }
253 if (inear < 0) {
254 fputs("Could not find non-empty neighbor!\n", stderr);
255 exit(1);
256 }
257 ang2 = sqrt(lastang2);
258 r = ANG2R(ang2); /* record if > previous */
259 if (r > bsdf_grid[inear][jnear].crad)
260 bsdf_grid[inear][jnear].crad = r;
261 /* next search radius */
262 r = ang2*(2.*GRIDRES/M_PI) + 1;
263 }
264 /* fill in neighbors */
265 memset(fill_grid, 0, sizeof(fill_grid));
266 for (i = 0; i < GRIDRES; i++)
267 for (j = 0; j < GRIDRES; j++) {
268 if (!bsdf_grid[i][j].nval)
269 continue; /* no value -- skip */
270 if (bsdf_grid[i][j].crad)
271 continue; /* has distance already */
272 r = GRIDRES/20;
273 lastr2 = 2*r*r + 1;
274 for (ii = i-r; ii <= i+r; ii++) {
275 if (ii < 0) continue;
276 if (ii >= GRIDRES) break;
277 for (jj = j-r; jj <= j+r; jj++) {
278 if (jj < 0) continue;
279 if (jj >= GRIDRES) break;
280 if (!bsdf_grid[ii][jj].crad)
281 continue;
282 /* OK to use approx. closest */
283 r2 = (ii-i)*(ii-i) + (jj-j)*(jj-j);
284 if (r2 >= lastr2)
285 continue;
286 fill_grid[i][j] = bsdf_grid[ii][jj].crad;
287 lastr2 = r2;
288 }
289 }
290 }
291 /* copy back filled entries */
292 for (i = 0; i < GRIDRES; i++)
293 for (j = 0; j < GRIDRES; j++)
294 if (fill_grid[i][j])
295 bsdf_grid[i][j].crad = fill_grid[i][j];
296 }
297
298 /* Cull points for more uniform distribution */
299 static void
300 cull_values(void)
301 {
302 FVECT ovec0, ovec1;
303 double maxang, maxang2;
304 int i, j, ii, jj, r;
305 /* simple greedy algorithm */
306 for (i = 0; i < GRIDRES; i++)
307 for (j = 0; j < GRIDRES; j++) {
308 if (!bsdf_grid[i][j].nval)
309 continue;
310 if (!bsdf_grid[i][j].crad)
311 continue; /* shouldn't happen */
312 vec_from_pos(ovec0, i, j);
313 maxang = 2.*R2ANG(bsdf_grid[i][j].crad);
314 if (maxang > ovec0[2]) /* clamp near horizon */
315 maxang = ovec0[2];
316 r = maxang*(2.*GRIDRES/M_PI) + 1;
317 maxang2 = maxang*maxang;
318 for (ii = i-r; ii <= i+r; ii++) {
319 if (ii < 0) continue;
320 if (ii >= GRIDRES) break;
321 for (jj = j-r; jj <= j+r; jj++) {
322 if (jj < 0) continue;
323 if (jj >= GRIDRES) break;
324 if (!bsdf_grid[ii][jj].nval)
325 continue;
326 if ((ii == i) & (jj == j))
327 continue; /* don't get self-absorbed */
328 vec_from_pos(ovec1, ii, jj);
329 if (2. - 2.*DOT(ovec0,ovec1) >= maxang2)
330 continue;
331 /* absorb sum */
332 bsdf_grid[i][j].vsum += bsdf_grid[ii][jj].vsum;
333 bsdf_grid[i][j].nval += bsdf_grid[ii][jj].nval;
334 /* keep value, though */
335 bsdf_grid[ii][jj].vsum /= (double)bsdf_grid[ii][jj].nval;
336 bsdf_grid[ii][jj].nval = 0;
337 }
338 }
339 }
340 }
341
342
343 #if 1
344 /* Test main produces a Radiance model from the given input file */
345 int
346 main(int argc, char *argv[])
347 {
348 char buf[128];
349 FILE *pfp;
350 double bsdf;
351 FVECT dir;
352 int i, j, n;
353
354 if (argc != 2) {
355 fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]);
356 return(1);
357 }
358 if (!load_bsdf_meas(argv[1]))
359 return(1);
360 /* produce spheres at meas. */
361 puts("void plastic orange\n0\n0\n5 .6 .4 .01 .04 .08\n");
362 n = 0;
363 for (i = 0; i < GRIDRES; i++)
364 for (j = 0; j < GRIDRES; j++)
365 if (bsdf_grid[i][j].nval) {
366 double bsdf = bsdf_grid[i][j].vsum /
367 (double)bsdf_grid[i][j].nval;
368 FVECT dir;
369
370 vec_from_pos(dir, i, j);
371 printf("orange sphere s%04d\n0\n0\n", ++n);
372 printf("4 %.6g %.6g %.6g .0015\n\n",
373 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
374 }
375 compute_radii();
376 cull_values();
377 /* highlight chosen values */
378 puts("void plastic pink\n0\n0\n5 .5 .05 .9 .04 .08\n");
379 n = 0;
380 for (i = 0; i < GRIDRES; i++)
381 for (j = 0; j < GRIDRES; j++)
382 if (bsdf_grid[i][j].nval) {
383 bsdf = bsdf_grid[i][j].vsum /
384 (double)bsdf_grid[i][j].nval;
385 vec_from_pos(dir, i, j);
386 printf("pink cone c%04d\n0\n0\n8\n", ++n);
387 printf("\t%.6g %.6g %.6g\n",
388 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
389 printf("\t%.6g %.6g %.6g\n",
390 dir[0]*(bsdf+.005), dir[1]*(bsdf+.005),
391 dir[2]*(bsdf+.005));
392 puts("\t.003\t0\n");
393 }
394 /* output continuous surface */
395 make_rbfrep();
396 puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 .9\n");
397 fflush(stdout);
398 sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES, GRIDRES);
399 pfp = popen(buf, "w");
400 if (pfp == NULL) {
401 fputs(buf, stderr);
402 fputs(": cannot start command\n", stderr);
403 return(1);
404 }
405 for (i = 0; i < GRIDRES; i++)
406 for (j = 0; j < GRIDRES; j++) {
407 vec_from_pos(dir, i, j);
408 bsdf = eval_rbfrep(bsdf_list, dir);
409 fprintf(pfp, "%.8e %.8e %.8e\n",
410 dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
411 }
412 return(pclose(pfp)==0 ? 0 : 1);
413 }
414 #endif