| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: pabopto2bsdf.c,v 2.43 2025/06/03 21:31:51 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Load measured BSDF data in PAB-Opto format.
|
| 6 |
* Assumes that surface-normal (Z-axis) faces into room unless -t option given.
|
| 7 |
*
|
| 8 |
* G. Ward
|
| 9 |
*/
|
| 10 |
|
| 11 |
#define _USE_MATH_DEFINES
|
| 12 |
#include <stdlib.h>
|
| 13 |
#include <ctype.h>
|
| 14 |
#include <math.h>
|
| 15 |
#include "rtio.h"
|
| 16 |
#include "bsdfrep.h"
|
| 17 |
|
| 18 |
typedef struct {
|
| 19 |
const char *fname; /* input file path */
|
| 20 |
double theta, phi; /* input angles (degrees) */
|
| 21 |
double up_phi; /* azimuth for "up" direction */
|
| 22 |
int igp[2]; /* input grid position */
|
| 23 |
int isDSF; /* data is DSF (rather than BSDF)? */
|
| 24 |
int nspec; /* number of spectral samples */
|
| 25 |
long dstart; /* data start offset in file */
|
| 26 |
} PGINPUT;
|
| 27 |
|
| 28 |
PGINPUT *inpfile; /* input files sorted by incidence */
|
| 29 |
|
| 30 |
int rev_orient = 0; /* shall we reverse surface orientation? */
|
| 31 |
|
| 32 |
double lim_graze = 0; /* limit scattering near grazing (deg above) */
|
| 33 |
|
| 34 |
/* Compare incident angles */
|
| 35 |
static int
|
| 36 |
cmp_indir(const void *p1, const void *p2)
|
| 37 |
{
|
| 38 |
const PGINPUT *inp1 = (const PGINPUT *)p1;
|
| 39 |
const PGINPUT *inp2 = (const PGINPUT *)p2;
|
| 40 |
int ydif = inp1->igp[1] - inp2->igp[1];
|
| 41 |
|
| 42 |
if (ydif)
|
| 43 |
return(ydif);
|
| 44 |
|
| 45 |
return(inp1->igp[0] - inp2->igp[0]);
|
| 46 |
}
|
| 47 |
|
| 48 |
/* Assign grid position from theta and phi */
|
| 49 |
static void
|
| 50 |
set_grid_pos(PGINPUT *pip)
|
| 51 |
{
|
| 52 |
FVECT dv;
|
| 53 |
|
| 54 |
if (pip->theta <= FTINY) {
|
| 55 |
pip->igp[0] = pip->igp[1] = grid_res/2 - 1;
|
| 56 |
return;
|
| 57 |
}
|
| 58 |
dv[2] = sin(M_PI/180.*pip->theta);
|
| 59 |
dv[0] = cos(M_PI/180.*pip->phi)*dv[2];
|
| 60 |
dv[1] = sin(M_PI/180.*pip->phi)*dv[2];
|
| 61 |
dv[2] = sqrt(1. - dv[2]*dv[2]);
|
| 62 |
pos_from_vec(pip->igp, dv);
|
| 63 |
}
|
| 64 |
|
| 65 |
/* Prepare a PAB-Opto input file by reading its header */
|
| 66 |
static int
|
| 67 |
init_pabopto_inp(const int i, const char *fname)
|
| 68 |
{
|
| 69 |
FILE *fp = fopen(fname, "r");
|
| 70 |
char buf[2048];
|
| 71 |
int c;
|
| 72 |
|
| 73 |
if (fp == NULL) {
|
| 74 |
fputs(fname, stderr);
|
| 75 |
fputs(": cannot open\n", stderr);
|
| 76 |
return(0);
|
| 77 |
}
|
| 78 |
inpfile[i].fname = fname;
|
| 79 |
inpfile[i].isDSF = -1;
|
| 80 |
inpfile[i].nspec = 0;
|
| 81 |
inpfile[i].up_phi = 0;
|
| 82 |
inpfile[i].theta = inpfile[i].phi = -10001.;
|
| 83 |
/* read header information */
|
| 84 |
while ((c = getc(fp)) == '#' || c == EOF) {
|
| 85 |
char typ[64];
|
| 86 |
if (fgets(buf, sizeof(buf), fp) == NULL) {
|
| 87 |
fputs(fname, stderr);
|
| 88 |
fputs(": unexpected EOF\n", stderr);
|
| 89 |
fclose(fp);
|
| 90 |
return(0);
|
| 91 |
}
|
| 92 |
if (sscanf(buf, "sample_name \"%[^\"]\"", bsdf_name) == 1)
|
| 93 |
continue;
|
| 94 |
if (sscanf(buf, "colorimetry: %s", typ) == 1) {
|
| 95 |
if (!strcasecmp(typ, "CIE-XYZ"))
|
| 96 |
inpfile[i].nspec = 3;
|
| 97 |
else if (!strcasecmp(typ, "CIE-Y"))
|
| 98 |
inpfile[i].nspec = 1;
|
| 99 |
continue;
|
| 100 |
}
|
| 101 |
if (sscanf(buf, "format: theta phi %s", typ) == 1) {
|
| 102 |
if (!strcasecmp(typ, "DSF"))
|
| 103 |
inpfile[i].isDSF = 1;
|
| 104 |
else if (!strcasecmp(typ, "BSDF") ||
|
| 105 |
!strcasecmp(typ, "BRDF") ||
|
| 106 |
!strcasecmp(typ, "BTDF"))
|
| 107 |
inpfile[i].isDSF = 0;
|
| 108 |
continue;
|
| 109 |
}
|
| 110 |
if (sscanf(buf, "upphi %lf", &inpfile[i].up_phi) == 1)
|
| 111 |
continue;
|
| 112 |
if (sscanf(buf, "intheta %lf", &inpfile[i].theta) == 1)
|
| 113 |
continue;
|
| 114 |
if (sscanf(buf, "inphi %lf", &inpfile[i].phi) == 1)
|
| 115 |
continue;
|
| 116 |
if (sscanf(buf, "incident_angle %lf %lf",
|
| 117 |
&inpfile[i].theta, &inpfile[i].phi) == 2)
|
| 118 |
continue;
|
| 119 |
}
|
| 120 |
inpfile[i].dstart = ftell(fp) - 1;
|
| 121 |
fclose(fp);
|
| 122 |
if (inpfile[i].isDSF < 0) {
|
| 123 |
fputs(fname, stderr);
|
| 124 |
fputs(": unknown format\n", stderr);
|
| 125 |
return(0);
|
| 126 |
}
|
| 127 |
if ((inpfile[i].theta < -10000.) | (inpfile[i].phi < -10000.)) {
|
| 128 |
fputs(fname, stderr);
|
| 129 |
fputs(": unknown incident angle\n", stderr);
|
| 130 |
return(0);
|
| 131 |
}
|
| 132 |
if (rev_orient) { /* reverse Z-axis to face outside */
|
| 133 |
inpfile[i].theta = 180. - inpfile[i].theta;
|
| 134 |
inpfile[i].phi = 360. - inpfile[i].phi;
|
| 135 |
}
|
| 136 |
/* convert to Y-up orientation */
|
| 137 |
inpfile[i].phi += 90.-inpfile[i].up_phi;
|
| 138 |
/* convert angle to grid position */
|
| 139 |
set_grid_pos(&inpfile[i]);
|
| 140 |
return(1);
|
| 141 |
}
|
| 142 |
|
| 143 |
/* Load a set of measurements corresponding to a particular incident angle */
|
| 144 |
static int
|
| 145 |
add_pabopto_inp(const int i)
|
| 146 |
{
|
| 147 |
FILE *fp = fopen(inpfile[i].fname, "r");
|
| 148 |
double theta_out, phi_out, val[3];
|
| 149 |
int n, c;
|
| 150 |
|
| 151 |
if (fp == NULL || fseek(fp, inpfile[i].dstart, 0) == EOF) {
|
| 152 |
fputs(inpfile[i].fname, stderr);
|
| 153 |
fputs(": cannot open\n", stderr);
|
| 154 |
return(0);
|
| 155 |
}
|
| 156 |
/* prepare input grid */
|
| 157 |
if (!i || cmp_indir(&inpfile[i-1], &inpfile[i])) {
|
| 158 |
if (i) /* process previous incidence */
|
| 159 |
make_rbfrep();
|
| 160 |
#ifdef DEBUG
|
| 161 |
fprintf(stderr, "New incident (theta,phi)=(%.1f,%.1f)\n",
|
| 162 |
inpfile[i].theta, inpfile[i].phi);
|
| 163 |
#endif
|
| 164 |
if (inpfile[i].nspec)
|
| 165 |
set_spectral_samples(inpfile[i].nspec);
|
| 166 |
new_bsdf_data(inpfile[i].theta, inpfile[i].phi);
|
| 167 |
}
|
| 168 |
#ifdef DEBUG
|
| 169 |
fprintf(stderr, "Loading measurements from '%s'...\n", inpfile[i].fname);
|
| 170 |
#endif
|
| 171 |
/* read scattering data */
|
| 172 |
while (fscanf(fp, "%lf %lf %lf", &theta_out, &phi_out, val) == 3) {
|
| 173 |
for (n = 1; n < inpfile[i].nspec; n++)
|
| 174 |
if (fscanf(fp, "%lf", val+n) != 1) {
|
| 175 |
fprintf(stderr, "%s: warning: unexpected EOF\n",
|
| 176 |
inpfile[i].fname);
|
| 177 |
fclose(fp);
|
| 178 |
return(1);
|
| 179 |
}
|
| 180 |
/* check if scatter angle is too low */
|
| 181 |
if (fabs(theta_out - 90.) < lim_graze-FTINY)
|
| 182 |
continue;
|
| 183 |
if (rev_orient) { /* reverse Z-axis to face outside? */
|
| 184 |
theta_out = 180. - theta_out;
|
| 185 |
phi_out = 360. - phi_out;
|
| 186 |
}
|
| 187 |
phi_out += 90.-inpfile[i].up_phi;
|
| 188 |
add_bsdf_data(theta_out, phi_out, val, inpfile[i].isDSF);
|
| 189 |
}
|
| 190 |
n = 0;
|
| 191 |
while ((c = getc(fp)) != EOF)
|
| 192 |
n += !isspace(c);
|
| 193 |
if (n)
|
| 194 |
fprintf(stderr,
|
| 195 |
"%s: warning: %d unexpected characters past EOD\n",
|
| 196 |
inpfile[i].fname, n);
|
| 197 |
fclose(fp);
|
| 198 |
return(1);
|
| 199 |
}
|
| 200 |
|
| 201 |
#ifndef TEST_MAIN
|
| 202 |
|
| 203 |
#define SYM_ILL '?' /* illegal symmetry value */
|
| 204 |
#define SYM_ISO 'I' /* isotropic */
|
| 205 |
#define SYM_QUAD 'Q' /* quadrilateral symmetry */
|
| 206 |
#define SYM_BILAT 'B' /* bilateral symmetry */
|
| 207 |
#define SYM_ANISO 'A' /* anisotropic */
|
| 208 |
#define SYM_UP 'U' /* "up-down" (180°) symmetry */
|
| 209 |
|
| 210 |
static const char quadrant_rep[16][16] = {
|
| 211 |
"in-plane","0-90","90-180","0-180",
|
| 212 |
"180-270","0-90+180-270","90-270",
|
| 213 |
"0-270","270-360","270-90",
|
| 214 |
"90-180+270-360","270-180","180-360",
|
| 215 |
"180-90","90-360","0-360"
|
| 216 |
};
|
| 217 |
static const char quadrant_sym[16] = {
|
| 218 |
SYM_ISO, SYM_QUAD, SYM_QUAD, SYM_BILAT,
|
| 219 |
SYM_QUAD, SYM_ILL, SYM_BILAT, SYM_ILL,
|
| 220 |
SYM_QUAD, SYM_BILAT, SYM_ILL, SYM_ILL,
|
| 221 |
SYM_BILAT, SYM_ILL, SYM_ILL, SYM_ANISO
|
| 222 |
};
|
| 223 |
|
| 224 |
/* Read in PAB-Opto BSDF files and output RBF interpolant */
|
| 225 |
int
|
| 226 |
main(int argc, char *argv[])
|
| 227 |
{
|
| 228 |
extern int nprocs;
|
| 229 |
static char gval_buf[16];
|
| 230 |
char * auto_grazing = NULL;
|
| 231 |
const char *symmetry = "0";
|
| 232 |
int ninpfiles, totinc;
|
| 233 |
int a, i;
|
| 234 |
/* set global progname */
|
| 235 |
fixargv0(argv[0]);
|
| 236 |
/* get options */
|
| 237 |
for (a = 1; a < argc && argv[a][0] == '-'; a++)
|
| 238 |
switch (argv[a][1]) {
|
| 239 |
case 't':
|
| 240 |
rev_orient = !rev_orient;
|
| 241 |
break;
|
| 242 |
case 'n':
|
| 243 |
nprocs = atoi(argv[++a]);
|
| 244 |
break;
|
| 245 |
case 's':
|
| 246 |
symmetry = argv[++a];
|
| 247 |
break;
|
| 248 |
case 'g':
|
| 249 |
if (toupper(argv[a+1][0]) == 'A')
|
| 250 |
auto_grazing = argv[a+1] = gval_buf;
|
| 251 |
else
|
| 252 |
lim_graze = atof(argv[a+1]);
|
| 253 |
++a;
|
| 254 |
break;
|
| 255 |
default:
|
| 256 |
goto userr;
|
| 257 |
}
|
| 258 |
totinc = ninpfiles = argc - a; /* initialize & sort inputs */
|
| 259 |
if (ninpfiles < 2)
|
| 260 |
goto userr;
|
| 261 |
if (toupper(symmetry[0]) == SYM_UP) /* special case for "up" symmetry */
|
| 262 |
totinc += ninpfiles;
|
| 263 |
inpfile = (PGINPUT *)malloc(sizeof(PGINPUT)*totinc);
|
| 264 |
if (inpfile == NULL)
|
| 265 |
return(1);
|
| 266 |
if (auto_grazing)
|
| 267 |
lim_graze = 90.;
|
| 268 |
for (i = 0; i < ninpfiles; i++) {
|
| 269 |
if (!init_pabopto_inp(i, argv[a+i]))
|
| 270 |
return(1);
|
| 271 |
if (auto_grazing && fabs(inpfile[i].theta - 90.) < lim_graze)
|
| 272 |
lim_graze = fabs(inpfile[i].theta - 90.);
|
| 273 |
}
|
| 274 |
if (auto_grazing)
|
| 275 |
sprintf(auto_grazing, "%.2f", lim_graze);
|
| 276 |
for (i = ninpfiles; i < totinc; i++) { /* copy for "up" symmetry */
|
| 277 |
inpfile[i] = inpfile[i-ninpfiles];
|
| 278 |
inpfile[i].phi += 180.; /* invert duplicate data */
|
| 279 |
inpfile[i].up_phi -= 180.;
|
| 280 |
set_grid_pos(&inpfile[i]); /* grid location for sorting */
|
| 281 |
}
|
| 282 |
qsort(inpfile, totinc, sizeof(PGINPUT), cmp_indir);
|
| 283 |
/* compile measurements */
|
| 284 |
for (i = 0; i < totinc; i++)
|
| 285 |
if (!add_pabopto_inp(i))
|
| 286 |
return(1);
|
| 287 |
make_rbfrep(); /* process last data set */
|
| 288 |
/* check input symmetry */
|
| 289 |
switch (toupper(symmetry[0])) {
|
| 290 |
case '0': /* unspecified symmetry */
|
| 291 |
if (quadrant_sym[inp_coverage] != SYM_ILL)
|
| 292 |
break; /* anything legal goes */
|
| 293 |
fprintf(stderr, "%s: unsupported phi coverage (%s)\n",
|
| 294 |
progname, quadrant_rep[inp_coverage]);
|
| 295 |
return(1);
|
| 296 |
case SYM_UP: /* faux "up" symmetry */
|
| 297 |
if (quadrant_sym[inp_coverage] == SYM_ANISO)
|
| 298 |
break;
|
| 299 |
/* fall through */
|
| 300 |
case SYM_ISO: /* usual symmetry types */
|
| 301 |
case SYM_QUAD:
|
| 302 |
case SYM_BILAT:
|
| 303 |
case SYM_ANISO:
|
| 304 |
if (quadrant_sym[inp_coverage] == toupper(symmetry[0]))
|
| 305 |
break; /* matches spec */
|
| 306 |
fprintf(stderr,
|
| 307 |
"%s: phi coverage (%s) does not match requested '%s' symmetry\n",
|
| 308 |
progname, quadrant_rep[inp_coverage], symmetry);
|
| 309 |
return(1);
|
| 310 |
default:
|
| 311 |
fprintf(stderr,
|
| 312 |
"%s: -s option must be Isotropic, Quadrilateral, Bilateral, Up, or Anisotropic\n",
|
| 313 |
progname);
|
| 314 |
return(1);
|
| 315 |
}
|
| 316 |
#ifdef DEBUG
|
| 317 |
fprintf(stderr, "Input phi coverage (%s) has '%c' symmetry\n",
|
| 318 |
quadrant_rep[inp_coverage],
|
| 319 |
quadrant_sym[inp_coverage]);
|
| 320 |
#endif
|
| 321 |
build_mesh(); /* create interpolation */
|
| 322 |
SET_FILE_BINARY(stdout); /* start header */
|
| 323 |
newheader("RADIANCE", stdout);
|
| 324 |
printargs(argc, argv, stdout);
|
| 325 |
fputnow(stdout);
|
| 326 |
save_bsdf_rep(stdout); /* complete header + data */
|
| 327 |
return(0);
|
| 328 |
userr:
|
| 329 |
fprintf(stderr, "Usage: %s [-t][-n nproc][-s symmetry][-g angle|'A'] meas1.dat meas2.dat .. > bsdf.sir\n",
|
| 330 |
progname);
|
| 331 |
return(1);
|
| 332 |
}
|
| 333 |
|
| 334 |
#else /* TEST_MAIN */
|
| 335 |
|
| 336 |
/* Test main produces a Radiance model from the given input file */
|
| 337 |
int
|
| 338 |
main(int argc, char *argv[])
|
| 339 |
{
|
| 340 |
PGINPUT pginp;
|
| 341 |
char buf[128];
|
| 342 |
FILE *pfp;
|
| 343 |
double bsdf, min_log;
|
| 344 |
FVECT dir;
|
| 345 |
int i, j, n;
|
| 346 |
|
| 347 |
progname = argv[0];
|
| 348 |
if (argc != 2) {
|
| 349 |
fprintf(stderr, "Usage: %s input.dat > output.rad\n", progname);
|
| 350 |
return(1);
|
| 351 |
}
|
| 352 |
inpfile = &pginp;
|
| 353 |
if (!init_pabopto_inp(0, argv[1]) || !add_pabopto_inp(0))
|
| 354 |
return(1);
|
| 355 |
/* reduce data set */
|
| 356 |
if (make_rbfrep() == NULL) {
|
| 357 |
fprintf(stderr, "%s: nothing to plot!\n", progname);
|
| 358 |
exit(1);
|
| 359 |
}
|
| 360 |
#ifdef DEBUG
|
| 361 |
fprintf(stderr, "Minimum BSDF = %.4f\n", bsdf_min);
|
| 362 |
#endif
|
| 363 |
min_log = log(bsdf_min*.5 + 1e-5);
|
| 364 |
#if 1 /* produce spheres at meas. */
|
| 365 |
puts("void plastic yellow\n0\n0\n5 .6 .4 .01 .04 .08\n");
|
| 366 |
n = 0;
|
| 367 |
for (i = 0; i < grid_res; i++)
|
| 368 |
for (j = 0; j < grid_res; j++)
|
| 369 |
if (dsf_grid[i][j].sum.n > 0) {
|
| 370 |
ovec_from_pos(dir, i, j);
|
| 371 |
bsdf = dsf_grid[i][j].sum.v /
|
| 372 |
((double)dsf_grid[i][j].sum.n*output_orient*dir[2]);
|
| 373 |
if (bsdf <= bsdf_min*.6)
|
| 374 |
continue;
|
| 375 |
bsdf = log(bsdf + 1e-5) - min_log;
|
| 376 |
ovec_from_pos(dir, i, j);
|
| 377 |
printf("yellow sphere s%04d\n0\n0\n", ++n);
|
| 378 |
printf("4 %.6g %.6g %.6g %.6g\n\n",
|
| 379 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
|
| 380 |
.007*bsdf);
|
| 381 |
}
|
| 382 |
#endif
|
| 383 |
#if 1 /* spheres at RBF peaks */
|
| 384 |
puts("void plastic red\n0\n0\n5 .8 .01 .01 .04 .08\n");
|
| 385 |
for (n = 0; n < dsf_list->nrbf; n++) {
|
| 386 |
RBFVAL *rbf = &dsf_list->rbfa[n];
|
| 387 |
ovec_from_pos(dir, rbf->gx, rbf->gy);
|
| 388 |
bsdf = eval_rbfrep(dsf_list, dir);
|
| 389 |
bsdf = log(bsdf + 1e-5) - min_log;
|
| 390 |
printf("red sphere p%04d\n0\n0\n", ++n);
|
| 391 |
printf("4 %.6g %.6g %.6g %.6g\n\n",
|
| 392 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf,
|
| 393 |
.011*bsdf);
|
| 394 |
}
|
| 395 |
#endif
|
| 396 |
#if 1 /* output continuous surface */
|
| 397 |
puts("void trans tgreen\n0\n0\n7 .7 1 .7 .04 .04 .9 1\n");
|
| 398 |
fflush(stdout);
|
| 399 |
sprintf(buf, "gensurf tgreen bsdf - - - %d %d", grid_res-1, grid_res-1);
|
| 400 |
pfp = popen(buf, "w");
|
| 401 |
if (pfp == NULL) {
|
| 402 |
fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
|
| 403 |
return(1);
|
| 404 |
}
|
| 405 |
for (i = 0; i < grid_res; i++)
|
| 406 |
for (j = 0; j < grid_res; j++) {
|
| 407 |
ovec_from_pos(dir, i, j);
|
| 408 |
bsdf = eval_rbfrep(dsf_list, dir);
|
| 409 |
bsdf = log(bsdf + 1e-5) - min_log;
|
| 410 |
fprintf(pfp, "%.8e %.8e %.8e\n",
|
| 411 |
dir[0]*bsdf, dir[1]*bsdf, dir[2]*bsdf);
|
| 412 |
}
|
| 413 |
if (pclose(pfp) != 0)
|
| 414 |
return(1);
|
| 415 |
#endif
|
| 416 |
return(0);
|
| 417 |
}
|
| 418 |
|
| 419 |
#endif /* TEST_MAIN */
|