| 1 |
greg |
2.1 |
#ifndef lint |
| 2 |
greg |
2.10 |
static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.9 2013/11/08 03:43:01 greg Exp $"; |
| 3 |
greg |
2.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Load measured BSDF data in PAB-Opto format. |
| 6 |
|
|
* |
| 7 |
|
|
* G. Ward |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include <stdio.h> |
| 11 |
greg |
2.2 |
#include <stdlib.h> |
| 12 |
greg |
2.1 |
#include <string.h> |
| 13 |
|
|
#include <ctype.h> |
| 14 |
greg |
2.6 |
#include <math.h> |
| 15 |
greg |
2.1 |
#include "platform.h" |
| 16 |
|
|
#include "bsdfrep.h" |
| 17 |
|
|
/* global argv[0] */ |
| 18 |
|
|
char *progname; |
| 19 |
|
|
|
| 20 |
greg |
2.2 |
typedef struct { |
| 21 |
|
|
const char *fname; /* input file path */ |
| 22 |
|
|
double theta, phi; /* incident angles (in degrees) */ |
| 23 |
|
|
int isDSF; /* data is DSF (rather than BSDF)? */ |
| 24 |
|
|
long dstart; /* data start offset in file */ |
| 25 |
|
|
} PGINPUT; |
| 26 |
|
|
|
| 27 |
|
|
PGINPUT *inpfile; /* input files sorted by incidence */ |
| 28 |
|
|
int ninpfiles; /* number of input files */ |
| 29 |
|
|
|
| 30 |
|
|
/* Compare incident angles */ |
| 31 |
|
|
static int |
| 32 |
|
|
cmp_inang(const void *p1, const void *p2) |
| 33 |
|
|
{ |
| 34 |
|
|
const PGINPUT *inp1 = (const PGINPUT *)p1; |
| 35 |
|
|
const PGINPUT *inp2 = (const PGINPUT *)p2; |
| 36 |
|
|
|
| 37 |
|
|
if (inp1->theta > inp2->theta+FTINY) |
| 38 |
|
|
return(1); |
| 39 |
|
|
if (inp1->theta < inp2->theta-FTINY) |
| 40 |
|
|
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); |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
|
|
/* Prepare a PAB-Opto input file by reading its header */ |
| 49 |
greg |
2.1 |
static int |
| 50 |
greg |
2.2 |
init_pabopto_inp(const int i, const char *fname) |
| 51 |
greg |
2.1 |
{ |
| 52 |
|
|
FILE *fp = fopen(fname, "r"); |
| 53 |
|
|
char buf[2048]; |
| 54 |
greg |
2.2 |
int c; |
| 55 |
greg |
2.1 |
|
| 56 |
|
|
if (fp == NULL) { |
| 57 |
|
|
fputs(fname, stderr); |
| 58 |
|
|
fputs(": cannot open\n", stderr); |
| 59 |
|
|
return(0); |
| 60 |
|
|
} |
| 61 |
greg |
2.2 |
inpfile[i].fname = fname; |
| 62 |
|
|
inpfile[i].isDSF = -1; |
| 63 |
|
|
inpfile[i].theta = inpfile[i].phi = -10001.; |
| 64 |
greg |
2.1 |
/* read header information */ |
| 65 |
|
|
while ((c = getc(fp)) == '#' || c == EOF) { |
| 66 |
greg |
2.5 |
char typ[32]; |
| 67 |
greg |
2.1 |
if (fgets(buf, sizeof(buf), fp) == NULL) { |
| 68 |
|
|
fputs(fname, stderr); |
| 69 |
|
|
fputs(": unexpected EOF\n", stderr); |
| 70 |
|
|
fclose(fp); |
| 71 |
|
|
return(0); |
| 72 |
|
|
} |
| 73 |
greg |
2.10 |
if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1) |
| 74 |
|
|
continue; |
| 75 |
greg |
2.5 |
if (sscanf(buf, "format: theta phi %s", typ) == 1) { |
| 76 |
|
|
if (!strcasecmp(typ, "DSF")) { |
| 77 |
|
|
inpfile[i].isDSF = 1; |
| 78 |
|
|
continue; |
| 79 |
|
|
} |
| 80 |
|
|
if (!strcasecmp(typ, "BSDF")) { |
| 81 |
|
|
inpfile[i].isDSF = 0; |
| 82 |
|
|
continue; |
| 83 |
|
|
} |
| 84 |
greg |
2.1 |
} |
| 85 |
greg |
2.2 |
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1) |
| 86 |
greg |
2.1 |
continue; |
| 87 |
greg |
2.2 |
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1) |
| 88 |
greg |
2.1 |
continue; |
| 89 |
|
|
if (sscanf(buf, "incident_angle %lf %lf", |
| 90 |
greg |
2.2 |
&inpfile[i].theta, &inpfile[i].phi) == 2) |
| 91 |
greg |
2.1 |
continue; |
| 92 |
|
|
} |
| 93 |
greg |
2.2 |
inpfile[i].dstart = ftell(fp) - 1; |
| 94 |
|
|
fclose(fp); |
| 95 |
|
|
if (inpfile[i].isDSF < 0) { |
| 96 |
greg |
2.1 |
fputs(fname, stderr); |
| 97 |
|
|
fputs(": unknown format\n", stderr); |
| 98 |
|
|
return(0); |
| 99 |
|
|
} |
| 100 |
greg |
2.2 |
if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) { |
| 101 |
|
|
fputs(fname, stderr); |
| 102 |
|
|
fputs(": unknown incident angle\n", stderr); |
| 103 |
|
|
return(0); |
| 104 |
|
|
} |
| 105 |
greg |
2.4 |
while (inpfile[i].phi < 0) /* normalize phi direction */ |
| 106 |
|
|
inpfile[i].phi += 360.; |
| 107 |
greg |
2.2 |
return(1); |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
/* Load a set of measurements corresponding to a particular incident angle */ |
| 111 |
|
|
static int |
| 112 |
|
|
add_pabopto_inp(const int i) |
| 113 |
|
|
{ |
| 114 |
|
|
FILE *fp = fopen(inpfile[i].fname, "r"); |
| 115 |
|
|
double theta_out, phi_out, val; |
| 116 |
|
|
int n, c; |
| 117 |
|
|
|
| 118 |
|
|
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) { |
| 119 |
|
|
fputs(inpfile[i].fname, stderr); |
| 120 |
|
|
fputs(": cannot open\n", stderr); |
| 121 |
|
|
return(0); |
| 122 |
|
|
} |
| 123 |
greg |
2.1 |
/* prepare input grid */ |
| 124 |
greg |
2.2 |
if (!i || cmp_inang(&inpfile[i-1], &inpfile[i])) { |
| 125 |
|
|
if (i) /* need to process previous incidence */ |
| 126 |
|
|
make_rbfrep(); |
| 127 |
|
|
#ifdef DEBUG |
| 128 |
|
|
fprintf(stderr, "New incident (theta,phi)=(%f,%f)\n", |
| 129 |
|
|
inpfile[i].theta, inpfile[i].phi); |
| 130 |
|
|
#endif |
| 131 |
|
|
new_bsdf_data(inpfile[i].theta, inpfile[i].phi); |
| 132 |
|
|
} |
| 133 |
greg |
2.3 |
#ifdef DEBUG |
| 134 |
|
|
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname); |
| 135 |
|
|
#endif |
| 136 |
greg |
2.2 |
/* read scattering data */ |
| 137 |
greg |
2.1 |
while (fscanf(fp, "%lf %lf %lf\n", &theta_out, &phi_out, &val) == 3) |
| 138 |
greg |
2.2 |
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF); |
| 139 |
greg |
2.1 |
n = 0; |
| 140 |
|
|
while ((c = getc(fp)) != EOF) |
| 141 |
|
|
n += !isspace(c); |
| 142 |
|
|
if (n) |
| 143 |
|
|
fprintf(stderr, |
| 144 |
|
|
"%s: warning: %d unexpected characters past EOD\n", |
| 145 |
greg |
2.2 |
inpfile[i].fname, n); |
| 146 |
greg |
2.1 |
fclose(fp); |
| 147 |
|
|
return(1); |
| 148 |
|
|
} |
| 149 |
|
|
|
| 150 |
greg |
2.8 |
#ifndef TEST_MAIN |
| 151 |
greg |
2.1 |
/* Read in PAB-Opto BSDF files and output RBF interpolant */ |
| 152 |
|
|
int |
| 153 |
|
|
main(int argc, char *argv[]) |
| 154 |
|
|
{ |
| 155 |
|
|
extern int nprocs; |
| 156 |
|
|
int i; |
| 157 |
|
|
/* start header */ |
| 158 |
|
|
SET_FILE_BINARY(stdout); |
| 159 |
|
|
newheader("RADIANCE", stdout); |
| 160 |
|
|
printargs(argc, argv, stdout); |
| 161 |
|
|
fputnow(stdout); |
| 162 |
|
|
progname = argv[0]; /* get options */ |
| 163 |
|
|
while (argc > 2 && argv[1][0] == '-') { |
| 164 |
|
|
switch (argv[1][1]) { |
| 165 |
|
|
case 'n': |
| 166 |
|
|
nprocs = atoi(argv[2]); |
| 167 |
|
|
break; |
| 168 |
|
|
default: |
| 169 |
|
|
goto userr; |
| 170 |
|
|
} |
| 171 |
|
|
argv += 2; argc -= 2; |
| 172 |
|
|
} |
| 173 |
greg |
2.2 |
/* initialize & sort inputs */ |
| 174 |
|
|
ninpfiles = argc - 1; |
| 175 |
|
|
if (ninpfiles < 2) |
| 176 |
greg |
2.1 |
goto userr; |
| 177 |
greg |
2.2 |
inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*ninpfiles); |
| 178 |
|
|
if (inpfile == NULL) |
| 179 |
|
|
return(1); |
| 180 |
|
|
for (i = 0; i < ninpfiles; i++) |
| 181 |
|
|
if (!init_pabopto_inp(i, argv[i+1])) |
| 182 |
greg |
2.1 |
return(1); |
| 183 |
greg |
2.2 |
qsort(inpfile, ninpfiles, sizeof(PGINPUT), &cmp_inang); |
| 184 |
|
|
/* compile measurements */ |
| 185 |
|
|
for (i = 0; i < ninpfiles; i++) |
| 186 |
|
|
if (!add_pabopto_inp(i)) |
| 187 |
|
|
return(1); |
| 188 |
|
|
make_rbfrep(); /* process last data set */ |
| 189 |
greg |
2.1 |
build_mesh(); /* create interpolation */ |
| 190 |
|
|
save_bsdf_rep(stdout); /* write it out */ |
| 191 |
|
|
return(0); |
| 192 |
|
|
userr: |
| 193 |
|
|
fprintf(stderr, "Usage: %s [-n nproc] meas1.dat meas2.dat .. > bsdf.sir\n", |
| 194 |
|
|
progname); |
| 195 |
|
|
return(1); |
| 196 |
|
|
} |
| 197 |
greg |
2.6 |
#else |
| 198 |
|
|
/* Test main produces a Radiance model from the given input file */ |
| 199 |
|
|
int |
| 200 |
|
|
main(int argc, char *argv[]) |
| 201 |
|
|
{ |
| 202 |
|
|
PGINPUT pginp; |
| 203 |
|
|
char buf[128]; |
| 204 |
|
|
FILE *pfp; |
| 205 |
|
|
double bsdf, min_log; |
| 206 |
|
|
FVECT dir; |
| 207 |
|
|
int i, j, n; |
| 208 |
|
|
|
| 209 |
|
|
if (argc != 2) { |
| 210 |
|
|
fprintf(stderr, "Usage: %s input.dat > output.rad\n", argv[0]); |
| 211 |
|
|
return(1); |
| 212 |
|
|
} |
| 213 |
|
|
ninpfiles = 1; |
| 214 |
|
|
inpfile = &pginp; |
| 215 |
|
|
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0)) |
| 216 |
|
|
return(1); |
| 217 |
|
|
/* reduce data set */ |
| 218 |
|
|
make_rbfrep(); |
| 219 |
greg |
2.9 |
#ifdef DEBUG |
| 220 |
|
|
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min); |
| 221 |
|
|
#endif |
| 222 |
greg |
2.6 |
/* produce spheres at meas. */ |
| 223 |
|
|
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n"); |
| 224 |
|
|
min_log = log(bsdf_min*.5); |
| 225 |
|
|
n = 0; |
| 226 |
|
|
for (i = 0; i < GRIDRES; i++) |
| 227 |
|
|
for (j = 0; j < GRIDRES; j++) |
| 228 |
greg |
2.7 |
if (dsf_grid[i][j].nval > 0) { |
| 229 |
greg |
2.6 |
ovec_from_pos(dir, i, j); |
| 230 |
greg |
2.8 |
bsdf = dsf_grid[i][j].vsum / |
| 231 |
|
|
(dsf_grid[i][j].nval*output_orient*dir[2]); |
| 232 |
greg |
2.6 |
if (bsdf <= bsdf_min*.6) |
| 233 |
|
|
continue; |
| 234 |
|
|
bsdf = log(bsdf) - min_log; |
| 235 |
greg |
2.7 |
ovec_from_pos(dir, i, j); |
| 236 |
|
|
printf("yellow sphere s%04d\n0\n0\n", ++n); |
| 237 |
|
|
printf("4 %.6g %.6g %.6g %.6g\n\n", |
| 238 |
greg |
2.6 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf, |
| 239 |
greg |
2.7 |
.007*bsdf); |
| 240 |
greg |
2.6 |
} |
| 241 |
|
|
/* output continuous surface */ |
| 242 |
|
|
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n"); |
| 243 |
|
|
fflush(stdout); |
| 244 |
|
|
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", GRIDRES-1, GRIDRES-1); |
| 245 |
|
|
pfp = popen(buf, "w"); |
| 246 |
|
|
if (pfp == NULL) { |
| 247 |
greg |
2.8 |
fprintf(stderr, "%s: cannot open '| %s'\n", argv[0], buf); |
| 248 |
greg |
2.6 |
return(1); |
| 249 |
|
|
} |
| 250 |
|
|
for (i = 0; i < GRIDRES; i++) |
| 251 |
|
|
for (j = 0; j < GRIDRES; j++) { |
| 252 |
|
|
ovec_from_pos(dir, i, j); |
| 253 |
greg |
2.8 |
bsdf = eval_rbfrep(dsf_list, dir) / (output_orient*dir[2]); |
| 254 |
greg |
2.6 |
bsdf = log(bsdf) - min_log; |
| 255 |
|
|
fprintf(pfp, "%.8e %.8e %.8e\n", |
| 256 |
|
|
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf); |
| 257 |
|
|
} |
| 258 |
|
|
return(pclose(pfp)==0 ? 0 : 1); |
| 259 |
|
|
} |
| 260 |
|
|
#endif |