| 11 |
|
#include <stdio.h> |
| 12 |
|
#include <stdlib.h> |
| 13 |
|
#include <math.h> |
| 14 |
+ |
#include "random.h" |
| 15 |
|
#include "platform.h" |
| 16 |
+ |
#include "paths.h" |
| 17 |
+ |
#include "rtio.h" |
| 18 |
|
#include "calcomp.h" |
| 19 |
|
#include "bsdfrep.h" |
| 20 |
|
/* global argv[0] */ |
| 21 |
|
char *progname; |
| 22 |
+ |
/* reciprocity averaging option */ |
| 23 |
+ |
static const char *recip = " -a"; |
| 24 |
|
/* percentage to cull (<0 to turn off) */ |
| 25 |
< |
double pctcull = 90.; |
| 25 |
> |
static double pctcull = 90.; |
| 26 |
|
/* sampling order */ |
| 27 |
< |
int samp_order = 6; |
| 27 |
> |
static int samp_order = 6; |
| 28 |
|
/* super-sampling threshold */ |
| 29 |
|
const double ssamp_thresh = 0.35; |
| 30 |
|
/* number of super-samples */ |
| 31 |
< |
const int nssamp = 100; |
| 31 |
> |
#ifndef NSSAMP |
| 32 |
> |
#define NSSAMP 64 |
| 33 |
> |
#endif |
| 34 |
|
/* limit on number of RBF lobes */ |
| 35 |
|
static int lobe_lim = 15000; |
| 36 |
+ |
/* progress bar length */ |
| 37 |
+ |
static int do_prog = 79; |
| 38 |
|
|
| 39 |
< |
/* Output XML prologue to stdout */ |
| 39 |
> |
#define MAXCARG 512 /* wrapBSDF command */ |
| 40 |
> |
static char *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"}; |
| 41 |
> |
static int wbsdfac = 2; |
| 42 |
> |
|
| 43 |
> |
/* Add argument to wrapBSDF, allocating space if !isstatic */ |
| 44 |
|
static void |
| 45 |
< |
xml_prologue(int ac, char *av[]) |
| 45 |
> |
add_wbsdf(const char *arg, int isstatic) |
| 46 |
|
{ |
| 47 |
< |
puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 48 |
< |
puts("<WindowElement xmlns=\"http://windows.lbl.gov\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://windows.lbl.gov/BSDF-v1.4.xsd\">"); |
| 49 |
< |
fputs("<!-- File produced by:", stdout); |
| 50 |
< |
while (ac-- > 0) { |
| 51 |
< |
fputc(' ', stdout); |
| 52 |
< |
fputs(*av++, stdout); |
| 47 |
> |
if (arg == NULL) |
| 48 |
> |
return; |
| 49 |
> |
if (wbsdfac >= MAXCARG-1) { |
| 50 |
> |
fputs(progname, stderr); |
| 51 |
> |
fputs(": too many command arguments to wrapBSDF\n", stderr); |
| 52 |
> |
exit(1); |
| 53 |
|
} |
| 54 |
< |
puts(" -->"); |
| 55 |
< |
puts("<WindowElementType>System</WindowElementType>"); |
| 56 |
< |
puts("<FileType>BSDF</FileType>"); |
| 57 |
< |
puts("<Optical>"); |
| 58 |
< |
puts("<Layer>"); |
| 59 |
< |
puts("\t<Material>"); |
| 47 |
< |
puts("\t\t<Name>Name</Name>"); |
| 48 |
< |
puts("\t\t<Manufacturer>Manufacturer</Manufacturer>"); |
| 49 |
< |
puts("\t\t<DeviceType>Other</DeviceType>"); |
| 50 |
< |
puts("\t</Material>"); |
| 51 |
< |
puts("\t<DataDefinition>"); |
| 52 |
< |
printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n", |
| 53 |
< |
single_plane_incident ? '3' : '4'); |
| 54 |
< |
puts("\t</DataDefinition>"); |
| 54 |
> |
if (!*arg) |
| 55 |
> |
arg = ""; |
| 56 |
> |
else if (!isstatic) |
| 57 |
> |
arg = savqstr((char *)arg); |
| 58 |
> |
|
| 59 |
> |
wrapBSDF[wbsdfac++] = (char *)arg; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
< |
/* Output XML data prologue to stdout */ |
| 63 |
< |
static void |
| 64 |
< |
data_prologue() |
| 62 |
> |
/* Create Yuv component file and add appropriate arguments */ |
| 63 |
> |
static char * |
| 64 |
> |
create_component_file(int c) |
| 65 |
|
{ |
| 66 |
< |
static const char *bsdf_type[4] = { |
| 67 |
< |
"Reflection Front", |
| 68 |
< |
"Transmission Front", |
| 64 |
< |
"Transmission Back", |
| 65 |
< |
"Reflection Back" |
| 66 |
< |
}; |
| 66 |
> |
static const char sname[3][6] = {"CIE-Y", "CIE-u", "CIE-v"}; |
| 67 |
> |
static const char cname[4][4] = {"-rf", "-tf", "-tb", "-rb"}; |
| 68 |
> |
char *tfname = mktemp(savqstr(TEMPLATE)); |
| 69 |
|
|
| 70 |
< |
puts("\t<WavelengthData>"); |
| 71 |
< |
puts("\t\t<LayerNumber>System</LayerNumber>"); |
| 72 |
< |
puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>"); |
| 73 |
< |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
| 72 |
< |
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>"); |
| 73 |
< |
puts("\t\t<WavelengthDataBlock>"); |
| 74 |
< |
printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n", |
| 75 |
< |
bsdf_type[(input_orient>0)<<1 | (output_orient>0)]); |
| 76 |
< |
puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>"); |
| 77 |
< |
puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>"); |
| 78 |
< |
puts("\t\t\t<ScatteringData>"); |
| 70 |
> |
add_wbsdf("-s", 1); add_wbsdf(sname[c], 1); |
| 71 |
> |
add_wbsdf(cname[(input_orient>0)<<1 | (output_orient>0)], 1); |
| 72 |
> |
add_wbsdf(tfname, 1); |
| 73 |
> |
return(tfname); |
| 74 |
|
} |
| 75 |
|
|
| 76 |
< |
/* Output XML data epilogue to stdout */ |
| 76 |
> |
/* Start new progress bar */ |
| 77 |
> |
#define prog_start(s) if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else |
| 78 |
> |
|
| 79 |
> |
/* Draw progress bar of the appropriate length */ |
| 80 |
|
static void |
| 81 |
< |
data_epilogue(void) |
| 81 |
> |
prog_show(double frac) |
| 82 |
|
{ |
| 83 |
< |
puts("\t\t\t</ScatteringData>"); |
| 84 |
< |
puts("\t\t</WavelengthDataBlock>"); |
| 85 |
< |
puts("\t</WavelengthData>"); |
| 83 |
> |
static unsigned call_cnt = 0; |
| 84 |
> |
static char lastc[] = "-\\|/"; |
| 85 |
> |
char pbar[256]; |
| 86 |
> |
int nchars; |
| 87 |
> |
|
| 88 |
> |
if (do_prog <= 1) return; |
| 89 |
> |
if (do_prog > sizeof(pbar)-2) |
| 90 |
> |
do_prog = sizeof(pbar)-2; |
| 91 |
> |
if (frac < 0) frac = 0; |
| 92 |
> |
else if (frac >= 1) frac = .9999; |
| 93 |
> |
nchars = do_prog*frac; |
| 94 |
> |
pbar[0] = '\r'; |
| 95 |
> |
memset(pbar+1, '*', nchars); |
| 96 |
> |
pbar[nchars+1] = lastc[call_cnt++ & 3]; |
| 97 |
> |
memset(pbar+2+nchars, '-', do_prog-nchars-1); |
| 98 |
> |
pbar[do_prog+1] = '\0'; |
| 99 |
> |
fputs(pbar, stderr); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
< |
/* Output XML epilogue to stdout */ |
| 102 |
> |
/* Finish progress bar */ |
| 103 |
|
static void |
| 104 |
< |
xml_epilogue(void) |
| 104 |
> |
prog_done(void) |
| 105 |
|
{ |
| 106 |
< |
puts("</Layer>"); |
| 107 |
< |
puts("</Optical>"); |
| 108 |
< |
puts("</WindowElement>"); |
| 106 |
> |
int n = do_prog; |
| 107 |
> |
|
| 108 |
> |
if (n <= 1) return; |
| 109 |
> |
fputc('\r', stderr); |
| 110 |
> |
while (n--) |
| 111 |
> |
fputc(' ', stderr); |
| 112 |
> |
fputc('\r', stderr); |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
/* Compute absolute relative difference */ |
| 129 |
|
eval_isotropic(char *funame) |
| 130 |
|
{ |
| 131 |
|
const int sqres = 1<<samp_order; |
| 132 |
< |
FILE *ofp = NULL; |
| 132 |
> |
FILE *ofp, *uvfp[2]; |
| 133 |
|
int assignD = 0; |
| 134 |
|
char cmd[128]; |
| 135 |
|
int ix, ox, oy; |
| 136 |
|
double iovec[6]; |
| 137 |
< |
float bsdf; |
| 137 |
> |
float bsdf, uv[2]; |
| 138 |
|
|
| 123 |
– |
data_prologue(); /* begin output */ |
| 139 |
|
if (pctcull >= 0) { |
| 140 |
< |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d", |
| 141 |
< |
pctcull, samp_order); |
| 127 |
< |
fflush(stdout); |
| 140 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
| 141 |
> |
recip, pctcull, samp_order, create_component_file(0)); |
| 142 |
|
ofp = popen(cmd, "w"); |
| 143 |
|
if (ofp == NULL) { |
| 144 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
| 146 |
|
exit(1); |
| 147 |
|
} |
| 148 |
|
SET_FILE_BINARY(ofp); |
| 149 |
< |
} else |
| 150 |
< |
fputs("{\n", stdout); |
| 151 |
< |
/* need to assign Dx, Dy, Dz? */ |
| 152 |
< |
if (funame != NULL) |
| 149 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 150 |
> |
flockfile(ofp); |
| 151 |
> |
#endif |
| 152 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 153 |
> |
double uvcull = 100. - (100.-pctcull)*.25; |
| 154 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
| 155 |
> |
recip, uvcull, samp_order, create_component_file(1)); |
| 156 |
> |
uvfp[0] = popen(cmd, "w"); |
| 157 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 3 -t %f -g %d > %s", |
| 158 |
> |
recip, uvcull, samp_order, create_component_file(2)); |
| 159 |
> |
uvfp[1] = popen(cmd, "w"); |
| 160 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
| 161 |
> |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
| 162 |
> |
progname); |
| 163 |
> |
exit(1); |
| 164 |
> |
} |
| 165 |
> |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
| 166 |
> |
#ifdef getc_unlocked |
| 167 |
> |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
| 168 |
> |
#endif |
| 169 |
> |
} |
| 170 |
> |
} else { |
| 171 |
> |
ofp = fopen(create_component_file(0), "w"); |
| 172 |
> |
if (ofp == NULL) { |
| 173 |
> |
fprintf(stderr, "%s: cannot create Y output file\n", |
| 174 |
> |
progname); |
| 175 |
> |
exit(1); |
| 176 |
> |
} |
| 177 |
> |
fputs("{\n", ofp); |
| 178 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 179 |
> |
uvfp[0] = fopen(create_component_file(1), "w"); |
| 180 |
> |
uvfp[1] = fopen(create_component_file(2), "w"); |
| 181 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
| 182 |
> |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
| 183 |
> |
progname); |
| 184 |
> |
exit(1); |
| 185 |
> |
} |
| 186 |
> |
fputs("{\n", uvfp[0]); |
| 187 |
> |
fputs("{\n", uvfp[1]); |
| 188 |
> |
} |
| 189 |
> |
} |
| 190 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
| 191 |
|
assignD = (fundefined(funame) < 6); |
| 192 |
|
/* run through directions */ |
| 193 |
|
for (ix = 0; ix < sqres/2; ix++) { |
| 203 |
|
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
| 204 |
|
iovec[5] = output_orient * |
| 205 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 206 |
< |
if (funame == NULL) |
| 207 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
| 208 |
< |
output_orient/iovec[5]; |
| 209 |
< |
else { |
| 210 |
< |
double ssa[3], ssvec[6], sum; |
| 211 |
< |
int ssi; |
| 206 |
> |
if (funame == NULL) { |
| 207 |
> |
SDValue sdv; |
| 208 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
| 209 |
> |
bsdf = sdv.cieY; |
| 210 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 211 |
> |
uv[0] = uv[1] = 1. / |
| 212 |
> |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 213 |
> |
uv[0] *= 4.*sdv.spec.cx; |
| 214 |
> |
uv[1] *= 9.*sdv.spec.cy; |
| 215 |
> |
} |
| 216 |
> |
} else { |
| 217 |
|
if (assignD) { |
| 218 |
|
varset("Dx", '=', -iovec[3]); |
| 219 |
|
varset("Dy", '=', -iovec[4]); |
| 221 |
|
++eclock; |
| 222 |
|
} |
| 223 |
|
bsdf = funvalue(funame, 6, iovec); |
| 224 |
+ |
#if (NSSAMP > 0) |
| 225 |
|
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
| 226 |
< |
sum = 0; /* super-sample voxel */ |
| 227 |
< |
for (ssi = nssamp; ssi--; ) { |
| 228 |
< |
SDmultiSamp(ssa, 3, (ssi+drand48())/nssamp); |
| 226 |
> |
int ssi; |
| 227 |
> |
double ssa[3], ssvec[6], sum = 0; |
| 228 |
> |
/* super-sample voxel */ |
| 229 |
> |
for (ssi = NSSAMP; ssi--; ) { |
| 230 |
> |
SDmultiSamp(ssa, 3, (ssi+frandom()) * |
| 231 |
> |
(1./NSSAMP)); |
| 232 |
|
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
| 233 |
|
ssvec[1] = .0; |
| 234 |
|
ssvec[2] = input_orient * |
| 239 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 240 |
|
ssvec[4]*ssvec[4]); |
| 241 |
|
if (assignD) { |
| 242 |
< |
varset("Dx", '=', -iovec[3]); |
| 243 |
< |
varset("Dy", '=', -iovec[4]); |
| 244 |
< |
varset("Dz", '=', -iovec[5]); |
| 242 |
> |
varset("Dx", '=', -ssvec[3]); |
| 243 |
> |
varset("Dy", '=', -ssvec[4]); |
| 244 |
> |
varset("Dz", '=', -ssvec[5]); |
| 245 |
|
++eclock; |
| 246 |
|
} |
| 247 |
|
sum += funvalue(funame, 6, ssvec); |
| 248 |
|
} |
| 249 |
< |
bsdf = sum/nssamp; |
| 249 |
> |
bsdf = sum/NSSAMP; |
| 250 |
|
} |
| 251 |
+ |
#endif |
| 252 |
|
} |
| 253 |
|
if (pctcull >= 0) |
| 254 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 254 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
| 255 |
|
else |
| 256 |
< |
printf("\t%.3e\n", bsdf); |
| 256 |
> |
fprintf(ofp, "\t%.3e\n", bsdf); |
| 257 |
> |
|
| 258 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 259 |
> |
if (pctcull >= 0) { |
| 260 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 261 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 262 |
> |
} else { |
| 263 |
> |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
| 264 |
> |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
| 265 |
> |
} |
| 266 |
> |
} |
| 267 |
|
last_bsdf = bsdf; |
| 268 |
|
} |
| 269 |
|
} |
| 270 |
|
if (rbf != NULL) |
| 271 |
|
free(rbf); |
| 272 |
+ |
prog_show((ix+1.)*(2./sqres)); |
| 273 |
|
} |
| 274 |
+ |
prog_done(); |
| 275 |
|
if (pctcull >= 0) { /* finish output */ |
| 276 |
|
if (pclose(ofp)) { |
| 277 |
< |
fprintf(stderr, "%s: error running '%s'\n", |
| 278 |
< |
progname, cmd); |
| 277 |
> |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
| 278 |
> |
progname); |
| 279 |
|
exit(1); |
| 280 |
|
} |
| 281 |
+ |
if (rbf_colorimetry == RBCtristimulus && |
| 282 |
+ |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
| 283 |
+ |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
| 284 |
+ |
progname); |
| 285 |
+ |
exit(1); |
| 286 |
+ |
} |
| 287 |
|
} else { |
| 288 |
|
for (ix = sqres*sqres*sqres/2; ix--; ) |
| 289 |
< |
fputs("\t0\n", stdout); |
| 290 |
< |
fputs("}\n", stdout); |
| 289 |
> |
fputs("\t0\n", ofp); |
| 290 |
> |
fputs("}\n", ofp); |
| 291 |
> |
if (fclose(ofp)) { |
| 292 |
> |
fprintf(stderr, "%s: error writing Y file\n", |
| 293 |
> |
progname); |
| 294 |
> |
exit(1); |
| 295 |
> |
} |
| 296 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 297 |
> |
for (ix = sqres*sqres*sqres/2; ix--; ) { |
| 298 |
> |
fputs("\t0\n", uvfp[0]); |
| 299 |
> |
fputs("\t0\n", uvfp[1]); |
| 300 |
> |
} |
| 301 |
> |
fputs("}\n", uvfp[0]); |
| 302 |
> |
fputs("}\n", uvfp[1]); |
| 303 |
> |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
| 304 |
> |
fprintf(stderr, "%s: error writing uv file(s)\n", |
| 305 |
> |
progname); |
| 306 |
> |
exit(1); |
| 307 |
> |
} |
| 308 |
> |
} |
| 309 |
|
} |
| 212 |
– |
data_epilogue(); |
| 310 |
|
} |
| 311 |
|
|
| 312 |
|
/* Interpolate and output anisotropic BSDF data */ |
| 314 |
|
eval_anisotropic(char *funame) |
| 315 |
|
{ |
| 316 |
|
const int sqres = 1<<samp_order; |
| 317 |
< |
FILE *ofp = NULL; |
| 317 |
> |
FILE *ofp, *uvfp[2]; |
| 318 |
|
int assignD = 0; |
| 319 |
|
char cmd[128]; |
| 320 |
|
int ix, iy, ox, oy; |
| 321 |
|
double iovec[6]; |
| 322 |
< |
float bsdf; |
| 322 |
> |
float bsdf, uv[2]; |
| 323 |
|
|
| 227 |
– |
data_prologue(); /* begin output */ |
| 324 |
|
if (pctcull >= 0) { |
| 325 |
< |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d", |
| 326 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
| 327 |
< |
pctcull, samp_order); |
| 328 |
< |
fflush(stdout); |
| 325 |
> |
const char *avgopt = (input_orient>0 ^ output_orient>0) |
| 326 |
> |
? "" : recip; |
| 327 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
| 328 |
> |
avgopt, pctcull, samp_order, |
| 329 |
> |
create_component_file(0)); |
| 330 |
|
ofp = popen(cmd, "w"); |
| 331 |
|
if (ofp == NULL) { |
| 332 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
| 333 |
|
progname); |
| 334 |
|
exit(1); |
| 335 |
|
} |
| 336 |
< |
} else |
| 337 |
< |
fputs("{\n", stdout); |
| 338 |
< |
/* need to assign Dx, Dy, Dz? */ |
| 339 |
< |
if (funame != NULL) |
| 336 |
> |
SET_FILE_BINARY(ofp); |
| 337 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
| 338 |
> |
flockfile(ofp); |
| 339 |
> |
#endif |
| 340 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 341 |
> |
double uvcull = 100. - (100.-pctcull)*.25; |
| 342 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
| 343 |
> |
avgopt, uvcull, samp_order, |
| 344 |
> |
create_component_file(1)); |
| 345 |
> |
uvfp[0] = popen(cmd, "w"); |
| 346 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
| 347 |
> |
avgopt, uvcull, samp_order, |
| 348 |
> |
create_component_file(2)); |
| 349 |
> |
uvfp[1] = popen(cmd, "w"); |
| 350 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
| 351 |
> |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
| 352 |
> |
progname); |
| 353 |
> |
exit(1); |
| 354 |
> |
} |
| 355 |
> |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
| 356 |
> |
#ifdef getc_unlocked |
| 357 |
> |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
| 358 |
> |
#endif |
| 359 |
> |
} |
| 360 |
> |
} else { |
| 361 |
> |
ofp = fopen(create_component_file(0), "w"); |
| 362 |
> |
if (ofp == NULL) { |
| 363 |
> |
fprintf(stderr, "%s: cannot create Y output file\n", |
| 364 |
> |
progname); |
| 365 |
> |
exit(1); |
| 366 |
> |
} |
| 367 |
> |
fputs("{\n", ofp); |
| 368 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 369 |
> |
uvfp[0] = fopen(create_component_file(1), "w"); |
| 370 |
> |
uvfp[1] = fopen(create_component_file(2), "w"); |
| 371 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
| 372 |
> |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
| 373 |
> |
progname); |
| 374 |
> |
exit(1); |
| 375 |
> |
} |
| 376 |
> |
fputs("{\n", uvfp[0]); |
| 377 |
> |
fputs("{\n", uvfp[1]); |
| 378 |
> |
} |
| 379 |
> |
} |
| 380 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
| 381 |
|
assignD = (fundefined(funame) < 6); |
| 382 |
|
/* run through directions */ |
| 383 |
|
for (ix = 0; ix < sqres; ix++) |
| 394 |
|
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
| 395 |
|
iovec[5] = output_orient * |
| 396 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
| 397 |
< |
if (funame == NULL) |
| 398 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
| 399 |
< |
output_orient/iovec[5]; |
| 400 |
< |
else { |
| 401 |
< |
double ssa[4], ssvec[6], sum; |
| 402 |
< |
int ssi; |
| 397 |
> |
if (funame == NULL) { |
| 398 |
> |
SDValue sdv; |
| 399 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
| 400 |
> |
bsdf = sdv.cieY; |
| 401 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 402 |
> |
uv[0] = uv[1] = 1. / |
| 403 |
> |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
| 404 |
> |
uv[0] *= 4.*sdv.spec.cx; |
| 405 |
> |
uv[1] *= 9.*sdv.spec.cy; |
| 406 |
> |
} |
| 407 |
> |
} else { |
| 408 |
|
if (assignD) { |
| 409 |
|
varset("Dx", '=', -iovec[3]); |
| 410 |
|
varset("Dy", '=', -iovec[4]); |
| 412 |
|
++eclock; |
| 413 |
|
} |
| 414 |
|
bsdf = funvalue(funame, 6, iovec); |
| 415 |
+ |
#if (NSSAMP > 0) |
| 416 |
|
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
| 417 |
< |
sum = 0; /* super-sample voxel */ |
| 418 |
< |
for (ssi = nssamp; ssi--; ) { |
| 419 |
< |
SDmultiSamp(ssa, 4, (ssi+drand48())/nssamp); |
| 417 |
> |
int ssi; |
| 418 |
> |
double ssa[4], ssvec[6], sum = 0; |
| 419 |
> |
/* super-sample voxel */ |
| 420 |
> |
for (ssi = NSSAMP; ssi--; ) { |
| 421 |
> |
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
| 422 |
> |
(1./NSSAMP)); |
| 423 |
|
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
| 424 |
|
1.-(iy+ssa[1])/sqres); |
| 425 |
< |
ssvec[2] = output_orient * |
| 425 |
> |
ssvec[2] = input_orient * |
| 426 |
|
sqrt(1. - ssvec[0]*ssvec[0] - |
| 427 |
|
ssvec[1]*ssvec[1]); |
| 428 |
|
SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, |
| 431 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
| 432 |
|
ssvec[4]*ssvec[4]); |
| 433 |
|
if (assignD) { |
| 434 |
< |
varset("Dx", '=', -iovec[3]); |
| 435 |
< |
varset("Dy", '=', -iovec[4]); |
| 436 |
< |
varset("Dz", '=', -iovec[5]); |
| 434 |
> |
varset("Dx", '=', -ssvec[3]); |
| 435 |
> |
varset("Dy", '=', -ssvec[4]); |
| 436 |
> |
varset("Dz", '=', -ssvec[5]); |
| 437 |
|
++eclock; |
| 438 |
|
} |
| 439 |
|
sum += funvalue(funame, 6, ssvec); |
| 440 |
|
} |
| 441 |
< |
bsdf = sum/nssamp; |
| 441 |
> |
bsdf = sum/NSSAMP; |
| 442 |
|
} |
| 443 |
+ |
#endif |
| 444 |
|
} |
| 445 |
|
if (pctcull >= 0) |
| 446 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
| 446 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
| 447 |
|
else |
| 448 |
< |
printf("\t%.3e\n", bsdf); |
| 448 |
> |
fprintf(ofp, "\t%.3e\n", bsdf); |
| 449 |
> |
|
| 450 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 451 |
> |
if (pctcull >= 0) { |
| 452 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
| 453 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
| 454 |
> |
} else { |
| 455 |
> |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
| 456 |
> |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
| 457 |
> |
} |
| 458 |
> |
} |
| 459 |
|
last_bsdf = bsdf; |
| 460 |
|
} |
| 461 |
|
} |
| 462 |
|
if (rbf != NULL) |
| 463 |
|
free(rbf); |
| 464 |
+ |
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
| 465 |
|
} |
| 466 |
+ |
prog_done(); |
| 467 |
|
if (pctcull >= 0) { /* finish output */ |
| 468 |
|
if (pclose(ofp)) { |
| 469 |
< |
fprintf(stderr, "%s: error running '%s'\n", |
| 470 |
< |
progname, cmd); |
| 469 |
> |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
| 470 |
> |
progname); |
| 471 |
|
exit(1); |
| 472 |
|
} |
| 473 |
< |
} else |
| 474 |
< |
fputs("}\n", stdout); |
| 475 |
< |
data_epilogue(); |
| 473 |
> |
if (rbf_colorimetry == RBCtristimulus && |
| 474 |
> |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
| 475 |
> |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
| 476 |
> |
progname); |
| 477 |
> |
exit(1); |
| 478 |
> |
} |
| 479 |
> |
} else { |
| 480 |
> |
fputs("}\n", ofp); |
| 481 |
> |
if (fclose(ofp)) { |
| 482 |
> |
fprintf(stderr, "%s: error writing Y file\n", |
| 483 |
> |
progname); |
| 484 |
> |
exit(1); |
| 485 |
> |
} |
| 486 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
| 487 |
> |
fputs("}\n", uvfp[0]); |
| 488 |
> |
fputs("}\n", uvfp[1]); |
| 489 |
> |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
| 490 |
> |
fprintf(stderr, "%s: error writing uv file(s)\n", |
| 491 |
> |
progname); |
| 492 |
> |
exit(1); |
| 493 |
> |
} |
| 494 |
> |
} |
| 495 |
> |
} |
| 496 |
|
} |
| 497 |
|
|
| 498 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
| 499 |
+ |
/* Execute wrapBSDF command (may never return) */ |
| 500 |
+ |
static int |
| 501 |
+ |
wrap_up(void) |
| 502 |
+ |
{ |
| 503 |
+ |
char cmd[8192]; |
| 504 |
+ |
|
| 505 |
+ |
if (bsdf_manuf[0]) { |
| 506 |
+ |
add_wbsdf("-f", 1); |
| 507 |
+ |
strcpy(cmd, "m="); |
| 508 |
+ |
strcpy(cmd+2, bsdf_manuf); |
| 509 |
+ |
add_wbsdf(cmd, 0); |
| 510 |
+ |
} |
| 511 |
+ |
if (bsdf_name[0]) { |
| 512 |
+ |
add_wbsdf("-f", 1); |
| 513 |
+ |
strcpy(cmd, "n="); |
| 514 |
+ |
strcpy(cmd+2, bsdf_name); |
| 515 |
+ |
add_wbsdf(cmd, 0); |
| 516 |
+ |
} |
| 517 |
+ |
if (!convert_commandline(cmd, sizeof(cmd), wrapBSDF)) { |
| 518 |
+ |
fputs(progname, stderr); |
| 519 |
+ |
fputs(": command line too long in wrap_up()\n", stderr); |
| 520 |
+ |
return(1); |
| 521 |
+ |
} |
| 522 |
+ |
return(system(cmd)); |
| 523 |
+ |
} |
| 524 |
+ |
#else |
| 525 |
+ |
/* Execute wrapBSDF command (may never return) */ |
| 526 |
+ |
static int |
| 527 |
+ |
wrap_up(void) |
| 528 |
+ |
{ |
| 529 |
+ |
char buf[256]; |
| 530 |
+ |
char *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK); |
| 531 |
+ |
|
| 532 |
+ |
if (compath == NULL) { |
| 533 |
+ |
fprintf(stderr, "%s: cannot locate %s\n", progname, wrapBSDF[0]); |
| 534 |
+ |
return(1); |
| 535 |
+ |
} |
| 536 |
+ |
if (bsdf_manuf[0]) { |
| 537 |
+ |
add_wbsdf("-f", 1); |
| 538 |
+ |
strcpy(buf, "m="); |
| 539 |
+ |
strcpy(buf+2, bsdf_manuf); |
| 540 |
+ |
add_wbsdf(buf, 0); |
| 541 |
+ |
} |
| 542 |
+ |
if (bsdf_name[0]) { |
| 543 |
+ |
add_wbsdf("-f", 1); |
| 544 |
+ |
strcpy(buf, "n="); |
| 545 |
+ |
strcpy(buf+2, bsdf_name); |
| 546 |
+ |
add_wbsdf(buf, 0); |
| 547 |
+ |
} |
| 548 |
+ |
execv(compath, wrapBSDF); /* successful call never returns */ |
| 549 |
+ |
perror(compath); |
| 550 |
+ |
return(1); |
| 551 |
+ |
} |
| 552 |
+ |
#endif |
| 553 |
+ |
|
| 554 |
|
/* Read in BSDF and interpolate as tensor tree representation */ |
| 555 |
|
int |
| 556 |
|
main(int argc, char *argv[]) |
| 557 |
|
{ |
| 558 |
+ |
static char tfmt[2][4] = {"t4", "t3"}; |
| 559 |
|
int dofwd = 0, dobwd = 1; |
| 560 |
+ |
char buf[2048]; |
| 561 |
|
int i, na; |
| 562 |
|
|
| 563 |
|
progname = argv[0]; |
| 565 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
| 566 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
| 567 |
|
biggerlib(); |
| 568 |
< |
for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
| 568 |
> |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
| 569 |
|
switch (argv[i][1]) { /* get options */ |
| 570 |
|
case 'e': |
| 571 |
|
scompile(argv[++i], NULL, 0); |
| 572 |
|
break; |
| 573 |
|
case 'f': |
| 574 |
< |
if (!argv[i][2]) |
| 575 |
< |
fcompile(argv[++i]); |
| 576 |
< |
else |
| 574 |
> |
if (!argv[i][2]) { |
| 575 |
> |
if (strchr(argv[++i], '=') != NULL) { |
| 576 |
> |
add_wbsdf("-f", 1); |
| 577 |
> |
add_wbsdf(argv[i], 1); |
| 578 |
> |
} else |
| 579 |
> |
fcompile(argv[i]); |
| 580 |
> |
} else |
| 581 |
|
dofwd = (argv[i][0] == '+'); |
| 582 |
|
break; |
| 583 |
+ |
case 'a': |
| 584 |
+ |
recip = (argv[i][0] == '+') ? " -a" : ""; |
| 585 |
+ |
break; |
| 586 |
|
case 'b': |
| 587 |
|
dobwd = (argv[i][0] == '+'); |
| 588 |
|
break; |
| 607 |
|
case 'l': |
| 608 |
|
lobe_lim = atoi(argv[++i]); |
| 609 |
|
break; |
| 610 |
+ |
case 'p': |
| 611 |
+ |
do_prog = atoi(argv[i]+2); |
| 612 |
+ |
break; |
| 613 |
+ |
case 'W': |
| 614 |
+ |
add_wbsdf(argv[i], 1); |
| 615 |
+ |
break; |
| 616 |
+ |
case 'u': |
| 617 |
+ |
case 'C': |
| 618 |
+ |
add_wbsdf(argv[i], 1); |
| 619 |
+ |
add_wbsdf(argv[++i], 1); |
| 620 |
+ |
break; |
| 621 |
|
default: |
| 622 |
|
goto userr; |
| 623 |
|
} |
| 624 |
+ |
strcpy(buf, "File produced by: "); |
| 625 |
+ |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
| 626 |
+ |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
| 627 |
+ |
} |
| 628 |
|
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
| 629 |
|
void (*evf)(char *s) = single_plane_incident ? |
| 630 |
< |
&eval_isotropic : &eval_anisotropic; |
| 630 |
> |
eval_isotropic : eval_anisotropic; |
| 631 |
|
if (i != argc-1 || fundefined(argv[i]) < 3) { |
| 632 |
|
fprintf(stderr, |
| 633 |
|
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
| 634 |
|
progname); |
| 635 |
< |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n", |
| 376 |
< |
progname); |
| 635 |
> |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n"); |
| 636 |
|
goto userr; |
| 637 |
|
} |
| 638 |
|
++eclock; |
| 639 |
< |
xml_prologue(argc, argv); /* start XML output */ |
| 639 |
> |
add_wbsdf("-a", 1); |
| 640 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
| 641 |
|
if (dofwd) { |
| 642 |
|
input_orient = -1; |
| 643 |
|
output_orient = -1; |
| 644 |
< |
(*evf)(argv[i]); /* outside reflectance */ |
| 644 |
> |
prog_start("Evaluating outside reflectance"); |
| 645 |
> |
(*evf)(argv[i]); |
| 646 |
|
output_orient = 1; |
| 647 |
< |
(*evf)(argv[i]); /* outside -> inside */ |
| 647 |
> |
prog_start("Evaluating outside->inside transmission"); |
| 648 |
> |
(*evf)(argv[i]); |
| 649 |
|
} |
| 650 |
|
if (dobwd) { |
| 651 |
|
input_orient = 1; |
| 652 |
|
output_orient = 1; |
| 653 |
< |
(*evf)(argv[i]); /* inside reflectance */ |
| 653 |
> |
prog_start("Evaluating inside reflectance"); |
| 654 |
> |
(*evf)(argv[i]); |
| 655 |
|
output_orient = -1; |
| 656 |
< |
(*evf)(argv[i]); /* inside -> outside */ |
| 656 |
> |
prog_start("Evaluating inside->outside transmission"); |
| 657 |
> |
(*evf)(argv[i]); |
| 658 |
|
} |
| 659 |
< |
xml_epilogue(); /* finish XML output & exit */ |
| 396 |
< |
return(0); |
| 659 |
> |
return(wrap_up()); |
| 660 |
|
} |
| 661 |
|
if (i < argc) { /* open input files if given */ |
| 662 |
|
int nbsdf = 0; |
| 663 |
|
for ( ; i < argc; i++) { /* interpolate each component */ |
| 664 |
+ |
char pbuf[256]; |
| 665 |
|
FILE *fpin = fopen(argv[i], "rb"); |
| 666 |
|
if (fpin == NULL) { |
| 667 |
|
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
| 671 |
|
if (!load_bsdf_rep(fpin)) |
| 672 |
|
return(1); |
| 673 |
|
fclose(fpin); |
| 674 |
< |
if (!nbsdf++) /* start XML on first dist. */ |
| 675 |
< |
xml_prologue(argc, argv); |
| 674 |
> |
sprintf(pbuf, "Interpolating component '%s'", argv[i]); |
| 675 |
> |
prog_start(pbuf); |
| 676 |
> |
if (!nbsdf++) { |
| 677 |
> |
add_wbsdf("-a", 1); |
| 678 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
| 679 |
> |
} |
| 680 |
|
if (single_plane_incident) |
| 681 |
|
eval_isotropic(NULL); |
| 682 |
|
else |
| 683 |
|
eval_anisotropic(NULL); |
| 684 |
|
} |
| 685 |
< |
xml_epilogue(); /* finish XML output & exit */ |
| 418 |
< |
return(0); |
| 685 |
> |
return(wrap_up()); |
| 686 |
|
} |
| 687 |
|
SET_FILE_BINARY(stdin); /* load from stdin */ |
| 688 |
|
if (!load_bsdf_rep(stdin)) |
| 689 |
|
return(1); |
| 690 |
< |
xml_prologue(argc, argv); /* start XML output */ |
| 690 |
> |
prog_start("Interpolating from standard input"); |
| 691 |
> |
add_wbsdf("-a", 1); |
| 692 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
| 693 |
|
if (single_plane_incident) /* resample dist. */ |
| 694 |
|
eval_isotropic(NULL); |
| 695 |
|
else |
| 696 |
|
eval_anisotropic(NULL); |
| 697 |
< |
xml_epilogue(); /* finish XML output & exit */ |
| 698 |
< |
return(0); |
| 697 |
> |
|
| 698 |
> |
return(wrap_up()); |
| 699 |
|
userr: |
| 700 |
|
fprintf(stderr, |
| 701 |
< |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
| 701 |
> |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
| 702 |
|
progname); |
| 703 |
|
fprintf(stderr, |
| 704 |
< |
" or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
| 704 |
> |
" or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
| 705 |
|
progname); |
| 706 |
|
return(1); |
| 707 |
|
} |