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