| 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 |
|
|
| 102 |
|
fputs(": unknown incident angle\n", stderr); |
| 103 |
|
return(0); |
| 104 |
|
} |
| 105 |
< |
while (inpfile[i].phi < 0) /* normalize phi direction */ |
| 106 |
< |
inpfile[i].phi += 360.; |
| 105 |
> |
/* convert angle to grid position */ |
| 106 |
> |
dv[2] = sin(M_PI/180.*inpfile[i].theta); |
| 107 |
> |
dv[0] = cos(M_PI/180.*inpfile[i].phi)*dv[2]; |
| 108 |
> |
dv[1] = sin(M_PI/180.*inpfile[i].phi)*dv[2]; |
| 109 |
> |
dv[2] = sqrt(1. - dv[2]*dv[2]); |
| 110 |
> |
pos_from_vec(inpfile[i].igp, dv); |
| 111 |
|
return(1); |
| 112 |
|
} |
| 113 |
|
|
| 115 |
|
static int |
| 116 |
|
add_pabopto_inp(const int i) |
| 117 |
|
{ |
| 118 |
< |
FILE *fp = fopen(inpfile[i].fname, "r"); |
| 119 |
< |
double theta_out, phi_out, val; |
| 120 |
< |
int n, c; |
| 118 |
> |
FILE *fp = fopen(inpfile[i].fname, "r"); |
| 119 |
> |
double theta_out, phi_out, val; |
| 120 |
> |
int n, c; |
| 121 |
|
|
| 122 |
|
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
| 123 |
|
fputs(inpfile[i].fname, stderr); |
| 125 |
|
return(0); |
| 126 |
|
} |
| 127 |
|
/* prepare input grid */ |
| 128 |
< |
if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) { |
| 129 |
< |
if (i) /* need to process previous incidence */ |
| 128 |
> |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) { |
| 129 |
> |
if (i) /* process previous incidence */ |
| 130 |
|
make_rbfrep(); |
| 131 |
|
#ifdef DEBUG |
| 132 |
|
fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n", |
| 184 |
|
for (i = 0; i < ninpfiles; i++) |
| 185 |
|
if (!init_pabopto_inp(i, argv[i+1])) |
| 186 |
|
return(1); |
| 187 |
< |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang); |
| 187 |
> |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_indir); |
| 188 |
|
/* compile measurements */ |
| 189 |
|
for (i = 0; i < ninpfiles; i++) |
| 190 |
|
if (!add_pabopto_inp(i)) |
| 210 |
|
FVECT dir; |
| 211 |
|
int i, j, n; |
| 212 |
|
|
| 213 |
+ |
progname = argv[0]; |
| 214 |
|
if (argc != 2) { |
| 215 |
< |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]); |
| 215 |
> |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname); |
| 216 |
|
return(1); |
| 217 |
|
} |
| 218 |
|
ninpfiles = 1; |
| 220 |
|
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
| 221 |
|
return(1); |
| 222 |
|
/* reduce data set */ |
| 223 |
< |
make_rbfrep(); |
| 223 |
> |
if (make_rbfrep() == NULL) { |
| 224 |
> |
fprintf(stderr, "%s: nothing to plot!\n", progname); |
| 225 |
> |
exit(1); |
| 226 |
> |
} |
| 227 |
|
#ifdef DEBUG |
| 228 |
|
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
| 229 |
|
#endif |
| 233 |
|
n = 0; |
| 234 |
|
for (i = 0; i < GRIDRES; i++) |
| 235 |
|
for (j = 0; j < GRIDRES; j++) |
| 236 |
< |
if (dsf_grid[i][j].nval > 0) { |
| 236 |
> |
if (dsf_grid[i][j].sum.n > 0) { |
| 237 |
|
ovec_from_pos(dir, i, j); |
| 238 |
< |
bsdf = dsf_grid[i][j].vsum / |
| 239 |
< |
(dsf_grid[i][j].nval*output_orient*dir[2]); |
| 238 |
> |
bsdf = dsf_grid[i][j].sum.v / |
| 239 |
> |
(dsf_grid[i][j].sum.n*output_orient*dir[2]); |
| 240 |
|
if (bsdf <= bsdf_min*.6) |
| 241 |
|
continue; |
| 242 |
|
bsdf = log(bsdf + 1e-5) - min_log; |
| 266 |
|
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
| 267 |
|
pfp = popen(buf, "w"); |
| 268 |
|
if (pfp == NULL) { |
| 269 |
< |
fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf); |
| 269 |
> |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf); |
| 270 |
|
return(1); |
| 271 |
|
} |
| 272 |
|
for (i = 0; i < GRIDRES; i++) |