7 |
|
* G. Ward |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#define _USE_MATH_DEFINES |
11 |
|
#include <stdio.h> |
12 |
|
#include <stdlib.h> |
13 |
|
#include <string.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; /* incident angles (in degrees) */ |
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; |
32 |
|
|
33 |
|
/* Compare incident angles */ |
34 |
|
static int |
35 |
< |
cmp_inang(const void *p1, const void *p2) |
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 (inp1->theta > inp2->theta+FTINY) |
42 |
< |
return(1); |
43 |
< |
if (inp1->theta < inp2->theta-FTINY) |
44 |
< |
return(-1); |
41 |
< |
if (inp1->phi > inp2->phi+FTINY) |
42 |
< |
return(1); |
43 |
< |
if (inp1->phi < inp2->phi-FTINY) |
44 |
< |
return(-1); |
45 |
< |
return(0); |
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 */ |
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 |
|
|
77 |
|
inpfile[i].isDSF = 1; |
78 |
|
continue; |
79 |
|
} |
80 |
< |
if (!strcasecmp(typ, "BSDF")) { |
80 |
> |
if (!strcasecmp(typ, "BSDF") || |
81 |
> |
!strcasecmp(typ, "BRDF") || |
82 |
> |
!strcasecmp(typ, "BTDF")) { |
83 |
|
inpfile[i].isDSF = 0; |
84 |
|
continue; |
85 |
|
} |
104 |
|
fputs(": unknown incident angle\n", stderr); |
105 |
|
return(0); |
106 |
|
} |
107 |
< |
while (inpfile[i].phi < 0) /* normalize phi direction */ |
108 |
< |
inpfile[i].phi += 360.; |
107 |
> |
/* convert angle to grid position */ |
108 |
> |
dv[2] = sin(M_PI/180.*inpfile[i].theta); |
109 |
> |
dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2]; |
110 |
> |
dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2]; |
111 |
> |
dv[2] = sqrt(1. - dv[2]*dv[2]); |
112 |
> |
pos_from_vec(inpfile[i].igp, dv); |
113 |
|
return(1); |
114 |
|
} |
115 |
|
|
117 |
|
static int |
118 |
|
add_pabopto_inp(const int i) |
119 |
|
{ |
120 |
< |
FILE *fp = fopen(inpfile[i].fname, "r"); |
121 |
< |
double theta_out, phi_out, val; |
122 |
< |
int n, c; |
120 |
> |
FILE *fp = fopen(inpfile[i].fname, "r"); |
121 |
> |
double theta_out, phi_out, val; |
122 |
> |
int n, c; |
123 |
|
|
124 |
|
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
125 |
|
fputs(inpfile[i].fname, stderr); |
127 |
|
return(0); |
128 |
|
} |
129 |
|
/* prepare input grid */ |
130 |
< |
if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) { |
131 |
< |
if (i) /* need to process previous incidence */ |
130 |
> |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) { |
131 |
> |
if (i) /* process previous incidence */ |
132 |
|
make_rbfrep(); |
133 |
|
#ifdef DEBUG |
134 |
< |
fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n", |
134 |
> |
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n", |
135 |
|
inpfile[i].theta, inpfile[i].phi); |
136 |
|
#endif |
137 |
|
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
186 |
|
for (i = 0; i < ninpfiles; i++) |
187 |
|
if (!init_pabopto_inp(i, argv[i+1])) |
188 |
|
return(1); |
189 |
< |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang); |
189 |
> |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_indir); |
190 |
|
/* compile measurements */ |
191 |
|
for (i = 0; i < ninpfiles; i++) |
192 |
|
if (!add_pabopto_inp(i)) |
212 |
|
FVECT dir; |
213 |
|
int i, j, n; |
214 |
|
|
215 |
+ |
progname = argv[0]; |
216 |
|
if (argc != 2) { |
217 |
< |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]); |
217 |
> |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
218 |
|
return(1); |
219 |
|
} |
220 |
|
ninpfiles = 1; |
222 |
|
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
223 |
|
return(1); |
224 |
|
/* reduce data set */ |
225 |
< |
make_rbfrep(); |
225 |
> |
if (make_rbfrep() == NULL) { |
226 |
> |
fprintf(stderr, "%s: nothing to plot!\n", progname); |
227 |
> |
exit(1); |
228 |
> |
} |
229 |
|
#ifdef DEBUG |
230 |
|
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
231 |
|
#endif |
235 |
|
n = 0; |
236 |
|
for (i = 0; i < GRIDRES; i++) |
237 |
|
for (j = 0; j < GRIDRES; j++) |
238 |
< |
if (dsf_grid[i][j].nval > 0) { |
238 |
> |
if (dsf_grid[i][j].sum.n > 0) { |
239 |
|
ovec_from_pos(dir, i, j); |
240 |
< |
bsdf = dsf_grid[i][j].vsum / |
241 |
< |
(dsf_grid[i][j].nval*output_orient*dir[2]); |
240 |
> |
bsdf = dsf_grid[i][j].sum.v / |
241 |
> |
(dsf_grid[i][j].sum.n*output_orient*dir[2]); |
242 |
|
if (bsdf <= bsdf_min*.6) |
243 |
|
continue; |
244 |
< |
bsdf = log(bsdf) - min_log; |
244 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
245 |
|
ovec_from_pos(dir, i, j); |
246 |
|
printf("yellow sphere s%04d\n0\n0\n", ++n); |
247 |
|
printf("4 %.6g %.6g %.6g %.6g\n\n", |
255 |
|
RBFVAL *rbf = &dsf_list->rbfa[n]; |
256 |
|
ovec_from_pos(dir, rbf->gx, rbf->gy); |
257 |
|
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
258 |
< |
bsdf = log(bsdf) - min_log; |
258 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
259 |
|
printf("red sphere p%04d\n0\n0\n", ++n); |
260 |
|
printf("4 %.6g %.6g %.6g %.6g\n\n", |
261 |
|
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
268 |
|
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
269 |
|
pfp = popen(buf, "w"); |
270 |
|
if (pfp == NULL) { |
271 |
< |
fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf); |
271 |
> |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
272 |
|
return(1); |
273 |
|
} |
274 |
|
for (i = 0; i < GRIDRES; i++) |
275 |
|
for (j = 0; j < GRIDRES; j++) { |
276 |
|
ovec_from_pos(dir, i, j); |
277 |
|
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
278 |
< |
bsdf = log(bsdf) - min_log; |
278 |
> |
bsdf = log(bsdf + 1e-5) - min_log; |
279 |
|
fprintf(pfp, "%.8e %.8e %.8e\n", |
280 |
|
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
281 |
|
} |