8 |
|
*/ |
9 |
|
|
10 |
|
#define _USE_MATH_DEFINES |
11 |
– |
#include <stdio.h> |
11 |
|
#include <stdlib.h> |
12 |
|
#include <math.h> |
13 |
|
#include "random.h" |
14 |
|
#include "platform.h" |
15 |
< |
#include "rtprocess.h" |
15 |
> |
#include "paths.h" |
16 |
> |
#include "rtio.h" |
17 |
|
#include "calcomp.h" |
18 |
|
#include "bsdfrep.h" |
19 |
|
/* global argv[0] */ |
20 |
|
char *progname; |
21 |
+ |
/* reciprocity averaging option */ |
22 |
+ |
static const char *recip = " -a"; |
23 |
|
/* percentage to cull (<0 to turn off) */ |
24 |
< |
double pctcull = 90.; |
24 |
> |
static double pctcull = 90.; |
25 |
|
/* sampling order */ |
26 |
< |
int samp_order = 6; |
26 |
> |
static int samp_order = 6; |
27 |
|
/* super-sampling threshold */ |
28 |
|
const double ssamp_thresh = 0.35; |
29 |
|
/* number of super-samples */ |
30 |
|
#ifndef NSSAMP |
31 |
< |
#define NSSAMP 100 |
31 |
> |
#define NSSAMP 64 |
32 |
|
#endif |
33 |
|
/* limit on number of RBF lobes */ |
34 |
|
static int lobe_lim = 15000; |
35 |
|
/* progress bar length */ |
36 |
|
static int do_prog = 79; |
37 |
|
|
38 |
+ |
#define MAXCARG 512 /* wrapBSDF command */ |
39 |
+ |
static char *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"}; |
40 |
+ |
static int wbsdfac = 2; |
41 |
|
|
42 |
+ |
/* Add argument to wrapBSDF, allocating space if !isstatic */ |
43 |
+ |
static void |
44 |
+ |
add_wbsdf(const char *arg, int isstatic) |
45 |
+ |
{ |
46 |
+ |
if (arg == NULL) |
47 |
+ |
return; |
48 |
+ |
if (wbsdfac >= MAXCARG-1) { |
49 |
+ |
fputs(progname, stderr); |
50 |
+ |
fputs(": too many command arguments to wrapBSDF\n", stderr); |
51 |
+ |
exit(1); |
52 |
+ |
} |
53 |
+ |
if (!*arg) |
54 |
+ |
arg = ""; |
55 |
+ |
else if (!isstatic) |
56 |
+ |
arg = savqstr((char *)arg); |
57 |
+ |
|
58 |
+ |
wrapBSDF[wbsdfac++] = (char *)arg; |
59 |
+ |
} |
60 |
+ |
|
61 |
+ |
/* Create Yuv component file and add appropriate arguments */ |
62 |
+ |
static char * |
63 |
+ |
create_component_file(int c) |
64 |
+ |
{ |
65 |
+ |
static const char sname[3][6] = {"CIE-Y", "CIE-u", "CIE-v"}; |
66 |
+ |
static const char cname[4][4] = {"-rf", "-tf", "-tb", "-rb"}; |
67 |
+ |
char *tfname = mktemp(savqstr(TEMPLATE)); |
68 |
+ |
|
69 |
+ |
add_wbsdf("-s", 1); add_wbsdf(sname[c], 1); |
70 |
+ |
add_wbsdf(cname[(input_orient>0)<<1 | (output_orient>0)], 1); |
71 |
+ |
add_wbsdf(tfname, 1); |
72 |
+ |
return(tfname); |
73 |
+ |
} |
74 |
+ |
|
75 |
|
/* Start new progress bar */ |
76 |
|
#define prog_start(s) if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else |
77 |
|
|
111 |
|
fputc('\r', stderr); |
112 |
|
} |
113 |
|
|
76 |
– |
/* Output XML prologue to stdout */ |
77 |
– |
static void |
78 |
– |
xml_prologue(int ac, char *av[]) |
79 |
– |
{ |
80 |
– |
puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
81 |
– |
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\">"); |
82 |
– |
fputs("<!-- File produced by:", stdout); |
83 |
– |
while (ac-- > 0) { |
84 |
– |
fputc(' ', stdout); |
85 |
– |
fputs(*av++, stdout); |
86 |
– |
} |
87 |
– |
puts(" -->"); |
88 |
– |
puts("<WindowElementType>System</WindowElementType>"); |
89 |
– |
puts("<FileType>BSDF</FileType>"); |
90 |
– |
puts("<Optical>"); |
91 |
– |
puts("<Layer>"); |
92 |
– |
puts("\t<Material>"); |
93 |
– |
printf("\t\t<Name>%s</Name>\n", bsdf_name[0] ? bsdf_name : "Unknown"); |
94 |
– |
printf("\t\t<Manufacturer>%s</Manufacturer>\n", |
95 |
– |
bsdf_manuf[0] ? bsdf_manuf : "Unknown"); |
96 |
– |
puts("\t\t<DeviceType>Other</DeviceType>"); |
97 |
– |
puts("\t</Material>"); |
98 |
– |
puts("\t<DataDefinition>"); |
99 |
– |
printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n", |
100 |
– |
single_plane_incident ? '3' : '4'); |
101 |
– |
puts("\t</DataDefinition>"); |
102 |
– |
} |
103 |
– |
|
104 |
– |
/* Output XML data prologue to stdout */ |
105 |
– |
static void |
106 |
– |
data_prologue() |
107 |
– |
{ |
108 |
– |
static const char *bsdf_type[4] = { |
109 |
– |
"Reflection Front", |
110 |
– |
"Transmission Front", |
111 |
– |
"Transmission Back", |
112 |
– |
"Reflection Back" |
113 |
– |
}; |
114 |
– |
|
115 |
– |
puts("\t<WavelengthData>"); |
116 |
– |
puts("\t\t<LayerNumber>System</LayerNumber>"); |
117 |
– |
puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>"); |
118 |
– |
puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>"); |
119 |
– |
puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>"); |
120 |
– |
puts("\t\t<WavelengthDataBlock>"); |
121 |
– |
printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n", |
122 |
– |
bsdf_type[(input_orient>0)<<1 | (output_orient>0)]); |
123 |
– |
puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>"); |
124 |
– |
puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>"); |
125 |
– |
puts("\t\t\t<ScatteringData>"); |
126 |
– |
} |
127 |
– |
|
128 |
– |
/* Output XML data epilogue to stdout */ |
129 |
– |
static void |
130 |
– |
data_epilogue(void) |
131 |
– |
{ |
132 |
– |
puts("\t\t\t</ScatteringData>"); |
133 |
– |
puts("\t\t</WavelengthDataBlock>"); |
134 |
– |
puts("\t</WavelengthData>"); |
135 |
– |
} |
136 |
– |
|
137 |
– |
/* Output XML epilogue to stdout */ |
138 |
– |
static void |
139 |
– |
xml_epilogue(void) |
140 |
– |
{ |
141 |
– |
puts("</Layer>"); |
142 |
– |
puts("</Optical>"); |
143 |
– |
puts("</WindowElement>"); |
144 |
– |
} |
145 |
– |
|
114 |
|
/* Compute absolute relative difference */ |
115 |
|
static double |
116 |
|
abs_diff(double v1, double v0) |
128 |
|
eval_isotropic(char *funame) |
129 |
|
{ |
130 |
|
const int sqres = 1<<samp_order; |
131 |
< |
FILE *ofp = NULL; |
131 |
> |
const double sqfact = 1./(double)sqres; |
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 |
|
|
170 |
– |
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); |
174 |
< |
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", |
149 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
150 |
|
flockfile(ofp); |
151 |
|
#endif |
152 |
< |
} else |
153 |
< |
fputs("{\n", stdout); |
154 |
< |
/* need to assign Dx, Dy, Dz? */ |
155 |
< |
if (funame != NULL) |
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++) { |
194 |
|
RBFNODE *rbf = NULL; |
195 |
< |
iovec[0] = 2.*(ix+.5)/sqres - 1.; |
195 |
> |
iovec[0] = 2.*sqfact*(ix+.5) - 1.; |
196 |
|
iovec[1] = .0; |
197 |
|
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
198 |
|
if (funame == NULL) |
200 |
|
for (ox = 0; ox < sqres; ox++) { |
201 |
|
float last_bsdf = -1; |
202 |
|
for (oy = 0; oy < sqres; oy++) { |
203 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
203 |
> |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
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 |
< |
else { |
206 |
> |
if (funame == NULL) { |
207 |
> |
SDValue sdv; |
208 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
209 |
> |
bsdf = sdv.cieY; |
210 |
> |
#if (NSSAMP > 0) |
211 |
> |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
212 |
> |
int ssi; |
213 |
> |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
214 |
> |
/* super-sample voxel */ |
215 |
> |
for (ssi = NSSAMP; ssi--; ) { |
216 |
> |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
217 |
> |
(1./NSSAMP)); |
218 |
> |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
219 |
> |
(oy+ssa[1])*sqfact); |
220 |
> |
iovec[5] = output_orient * |
221 |
> |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
222 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
223 |
> |
sum += sdv.cieY; |
224 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
225 |
> |
sdv.cieY /= |
226 |
> |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
227 |
> |
usum += 4.*sdv.spec.cx * sdv.cieY; |
228 |
> |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
229 |
> |
} |
230 |
> |
} |
231 |
> |
bsdf = sum * (1./NSSAMP); |
232 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
233 |
> |
uv[0] = usum / (sum+FTINY); |
234 |
> |
uv[1] = vsum / (sum+FTINY); |
235 |
> |
} |
236 |
> |
} else |
237 |
> |
#endif |
238 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
239 |
> |
uv[0] = uv[1] = 1. / |
240 |
> |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
241 |
> |
uv[0] *= 4.*sdv.spec.cx; |
242 |
> |
uv[1] *= 9.*sdv.spec.cy; |
243 |
> |
} |
244 |
> |
} else { |
245 |
|
if (assignD) { |
246 |
|
varset("Dx", '=', -iovec[3]); |
247 |
|
varset("Dy", '=', -iovec[4]); |
257 |
|
for (ssi = NSSAMP; ssi--; ) { |
258 |
|
SDmultiSamp(ssa, 3, (ssi+frandom()) * |
259 |
|
(1./NSSAMP)); |
260 |
< |
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
260 |
> |
ssvec[0] = 2.*sqfact*(ix+ssa[0]) - 1.; |
261 |
|
ssvec[1] = .0; |
262 |
|
ssvec[2] = input_orient * |
263 |
|
sqrt(1. - ssvec[0]*ssvec[0]); |
264 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres, |
265 |
< |
(oy+ssa[2])/sqres); |
264 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[1])*sqfact, |
265 |
> |
(oy+ssa[2])*sqfact); |
266 |
|
ssvec[5] = output_orient * |
267 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
268 |
|
ssvec[4]*ssvec[4]); |
274 |
|
} |
275 |
|
sum += funvalue(funame, 6, ssvec); |
276 |
|
} |
277 |
< |
bsdf = sum/NSSAMP; |
277 |
> |
bsdf = sum * (1./NSSAMP); |
278 |
|
} |
279 |
|
#endif |
280 |
|
} |
281 |
|
if (pctcull >= 0) |
282 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
282 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
283 |
|
else |
284 |
< |
printf("\t%.3e\n", bsdf); |
284 |
> |
fprintf(ofp, "\t%.3e\n", bsdf); |
285 |
> |
|
286 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
287 |
> |
if (pctcull >= 0) { |
288 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
289 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
290 |
> |
} else { |
291 |
> |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
292 |
> |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
293 |
> |
} |
294 |
> |
} |
295 |
|
last_bsdf = bsdf; |
296 |
|
} |
297 |
|
} |
298 |
|
if (rbf != NULL) |
299 |
|
free(rbf); |
300 |
< |
prog_show((ix+1.)*(2./sqres)); |
300 |
> |
prog_show((ix+1.)*(2.*sqfact)); |
301 |
|
} |
302 |
+ |
prog_done(); |
303 |
|
if (pctcull >= 0) { /* finish output */ |
304 |
|
if (pclose(ofp)) { |
305 |
< |
fprintf(stderr, "%s: error running '%s'\n", |
306 |
< |
progname, cmd); |
305 |
> |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
306 |
> |
progname); |
307 |
|
exit(1); |
308 |
|
} |
309 |
+ |
if (rbf_colorimetry == RBCtristimulus && |
310 |
+ |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
311 |
+ |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
312 |
+ |
progname); |
313 |
+ |
exit(1); |
314 |
+ |
} |
315 |
|
} else { |
316 |
|
for (ix = sqres*sqres*sqres/2; ix--; ) |
317 |
< |
fputs("\t0\n", stdout); |
318 |
< |
fputs("}\n", stdout); |
317 |
> |
fputs("\t0\n", ofp); |
318 |
> |
fputs("}\n", ofp); |
319 |
> |
if (fclose(ofp)) { |
320 |
> |
fprintf(stderr, "%s: error writing Y file\n", |
321 |
> |
progname); |
322 |
> |
exit(1); |
323 |
> |
} |
324 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
325 |
> |
for (ix = sqres*sqres*sqres/2; ix--; ) { |
326 |
> |
fputs("\t0\n", uvfp[0]); |
327 |
> |
fputs("\t0\n", uvfp[1]); |
328 |
> |
} |
329 |
> |
fputs("}\n", uvfp[0]); |
330 |
> |
fputs("}\n", uvfp[1]); |
331 |
> |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
332 |
> |
fprintf(stderr, "%s: error writing uv file(s)\n", |
333 |
> |
progname); |
334 |
> |
exit(1); |
335 |
> |
} |
336 |
> |
} |
337 |
|
} |
265 |
– |
data_epilogue(); |
266 |
– |
prog_done(); |
338 |
|
} |
339 |
|
|
340 |
|
/* Interpolate and output anisotropic BSDF data */ |
342 |
|
eval_anisotropic(char *funame) |
343 |
|
{ |
344 |
|
const int sqres = 1<<samp_order; |
345 |
< |
FILE *ofp = NULL; |
345 |
> |
const double sqfact = 1./(double)sqres; |
346 |
> |
FILE *ofp, *uvfp[2]; |
347 |
|
int assignD = 0; |
348 |
|
char cmd[128]; |
349 |
|
int ix, iy, ox, oy; |
350 |
|
double iovec[6]; |
351 |
< |
float bsdf; |
351 |
> |
float bsdf, uv[2]; |
352 |
|
|
281 |
– |
data_prologue(); /* begin output */ |
353 |
|
if (pctcull >= 0) { |
354 |
< |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d", |
355 |
< |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
356 |
< |
pctcull, samp_order); |
357 |
< |
fflush(stdout); |
354 |
> |
const char *avgopt = (input_orient>0 ^ output_orient>0) |
355 |
> |
? "" : recip; |
356 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
357 |
> |
avgopt, pctcull, samp_order, |
358 |
> |
create_component_file(0)); |
359 |
|
ofp = popen(cmd, "w"); |
360 |
|
if (ofp == NULL) { |
361 |
|
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
366 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
367 |
|
flockfile(ofp); |
368 |
|
#endif |
369 |
< |
} else |
370 |
< |
fputs("{\n", stdout); |
371 |
< |
/* need to assign Dx, Dy, Dz? */ |
372 |
< |
if (funame != NULL) |
369 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
370 |
> |
double uvcull = 100. - (100.-pctcull)*.25; |
371 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
372 |
> |
avgopt, uvcull, samp_order, |
373 |
> |
create_component_file(1)); |
374 |
> |
uvfp[0] = popen(cmd, "w"); |
375 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s", |
376 |
> |
avgopt, uvcull, samp_order, |
377 |
> |
create_component_file(2)); |
378 |
> |
uvfp[1] = popen(cmd, "w"); |
379 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
380 |
> |
fprintf(stderr, "%s: cannot open pipes to uv output\n", |
381 |
> |
progname); |
382 |
> |
exit(1); |
383 |
> |
} |
384 |
> |
SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]); |
385 |
> |
#ifdef getc_unlocked |
386 |
> |
flockfile(uvfp[0]); flockfile(uvfp[1]); |
387 |
> |
#endif |
388 |
> |
} |
389 |
> |
} else { |
390 |
> |
ofp = fopen(create_component_file(0), "w"); |
391 |
> |
if (ofp == NULL) { |
392 |
> |
fprintf(stderr, "%s: cannot create Y output file\n", |
393 |
> |
progname); |
394 |
> |
exit(1); |
395 |
> |
} |
396 |
> |
fputs("{\n", ofp); |
397 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
398 |
> |
uvfp[0] = fopen(create_component_file(1), "w"); |
399 |
> |
uvfp[1] = fopen(create_component_file(2), "w"); |
400 |
> |
if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) { |
401 |
> |
fprintf(stderr, "%s: cannot create uv output file(s)\n", |
402 |
> |
progname); |
403 |
> |
exit(1); |
404 |
> |
} |
405 |
> |
fputs("{\n", uvfp[0]); |
406 |
> |
fputs("{\n", uvfp[1]); |
407 |
> |
} |
408 |
> |
} |
409 |
> |
if (funame != NULL) /* need to assign Dx, Dy, Dz? */ |
410 |
|
assignD = (fundefined(funame) < 6); |
411 |
|
/* run through directions */ |
412 |
|
for (ix = 0; ix < sqres; ix++) |
413 |
|
for (iy = 0; iy < sqres; iy++) { |
414 |
|
RBFNODE *rbf = NULL; /* Klems reversal */ |
415 |
< |
SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres); |
415 |
> |
SDsquare2disk(iovec, 1.-(ix+.5)*sqfact, 1.-(iy+.5)*sqfact); |
416 |
|
iovec[2] = input_orient * |
417 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
418 |
|
if (funame == NULL) |
420 |
|
for (ox = 0; ox < sqres; ox++) { |
421 |
|
float last_bsdf = -1; |
422 |
|
for (oy = 0; oy < sqres; oy++) { |
423 |
< |
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
423 |
> |
SDsquare2disk(iovec+3, (ox+.5)*sqfact, (oy+.5)*sqfact); |
424 |
|
iovec[5] = output_orient * |
425 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
426 |
< |
if (funame == NULL) |
427 |
< |
bsdf = eval_rbfrep(rbf, iovec+3); |
428 |
< |
else { |
426 |
> |
if (funame == NULL) { |
427 |
> |
SDValue sdv; |
428 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
429 |
> |
bsdf = sdv.cieY; |
430 |
> |
#if (NSSAMP > 0) |
431 |
> |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
432 |
> |
int ssi; |
433 |
> |
double ssa[2], sum = 0, usum = 0, vsum = 0; |
434 |
> |
/* super-sample voxel */ |
435 |
> |
for (ssi = NSSAMP; ssi--; ) { |
436 |
> |
SDmultiSamp(ssa, 2, (ssi+frandom()) * |
437 |
> |
(1./NSSAMP)); |
438 |
> |
SDsquare2disk(iovec+3, (ox+ssa[0])*sqfact, |
439 |
> |
(oy+ssa[1])*sqfact); |
440 |
> |
iovec[5] = output_orient * |
441 |
> |
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
442 |
> |
eval_rbfcol(&sdv, rbf, iovec+3); |
443 |
> |
sum += sdv.cieY; |
444 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
445 |
> |
sdv.cieY /= |
446 |
> |
-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.; |
447 |
> |
usum += 4.*sdv.spec.cx * sdv.cieY; |
448 |
> |
vsum += 9.*sdv.spec.cy * sdv.cieY; |
449 |
> |
} |
450 |
> |
} |
451 |
> |
bsdf = sum * (1./NSSAMP); |
452 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
453 |
> |
uv[0] = usum / (sum+FTINY); |
454 |
> |
uv[1] = vsum / (sum+FTINY); |
455 |
> |
} |
456 |
> |
} else |
457 |
> |
#endif |
458 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
459 |
> |
uv[0] = uv[1] = 1. / |
460 |
> |
(-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.); |
461 |
> |
uv[0] *= 4.*sdv.spec.cx; |
462 |
> |
uv[1] *= 9.*sdv.spec.cy; |
463 |
> |
} |
464 |
> |
} else { |
465 |
|
if (assignD) { |
466 |
|
varset("Dx", '=', -iovec[3]); |
467 |
|
varset("Dy", '=', -iovec[4]); |
477 |
|
for (ssi = NSSAMP; ssi--; ) { |
478 |
|
SDmultiSamp(ssa, 4, (ssi+frandom()) * |
479 |
|
(1./NSSAMP)); |
480 |
< |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
481 |
< |
1.-(iy+ssa[1])/sqres); |
480 |
> |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])*sqfact, |
481 |
> |
1.-(iy+ssa[1])*sqfact); |
482 |
|
ssvec[2] = input_orient * |
483 |
|
sqrt(1. - ssvec[0]*ssvec[0] - |
484 |
|
ssvec[1]*ssvec[1]); |
485 |
< |
SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, |
486 |
< |
(oy+ssa[3])/sqres); |
485 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[2])*sqfact, |
486 |
> |
(oy+ssa[3])*sqfact); |
487 |
|
ssvec[5] = output_orient * |
488 |
|
sqrt(1. - ssvec[3]*ssvec[3] - |
489 |
|
ssvec[4]*ssvec[4]); |
495 |
|
} |
496 |
|
sum += funvalue(funame, 6, ssvec); |
497 |
|
} |
498 |
< |
bsdf = sum/NSSAMP; |
498 |
> |
bsdf = sum * (1./NSSAMP); |
499 |
|
} |
500 |
|
#endif |
501 |
|
} |
502 |
|
if (pctcull >= 0) |
503 |
< |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
503 |
> |
putbinary(&bsdf, sizeof(bsdf), 1, ofp); |
504 |
|
else |
505 |
< |
printf("\t%.3e\n", bsdf); |
505 |
> |
fprintf(ofp, "\t%.3e\n", bsdf); |
506 |
> |
|
507 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
508 |
> |
if (pctcull >= 0) { |
509 |
> |
putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]); |
510 |
> |
putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]); |
511 |
> |
} else { |
512 |
> |
fprintf(uvfp[0], "\t%.3e\n", uv[0]); |
513 |
> |
fprintf(uvfp[1], "\t%.3e\n", uv[1]); |
514 |
> |
} |
515 |
> |
} |
516 |
|
last_bsdf = bsdf; |
517 |
|
} |
518 |
|
} |
520 |
|
free(rbf); |
521 |
|
prog_show((ix*sqres+iy+1.)/(sqres*sqres)); |
522 |
|
} |
523 |
+ |
prog_done(); |
524 |
|
if (pctcull >= 0) { /* finish output */ |
525 |
|
if (pclose(ofp)) { |
526 |
< |
fprintf(stderr, "%s: error running '%s'\n", |
527 |
< |
progname, cmd); |
526 |
> |
fprintf(stderr, "%s: error running rttree_reduce on Y\n", |
527 |
> |
progname); |
528 |
|
exit(1); |
529 |
|
} |
530 |
< |
} else |
531 |
< |
fputs("}\n", stdout); |
532 |
< |
data_epilogue(); |
533 |
< |
prog_done(); |
530 |
> |
if (rbf_colorimetry == RBCtristimulus && |
531 |
> |
(pclose(uvfp[0]) || pclose(uvfp[1]))) { |
532 |
> |
fprintf(stderr, "%s: error running rttree_reduce on uv\n", |
533 |
> |
progname); |
534 |
> |
exit(1); |
535 |
> |
} |
536 |
> |
} else { |
537 |
> |
fputs("}\n", ofp); |
538 |
> |
if (fclose(ofp)) { |
539 |
> |
fprintf(stderr, "%s: error writing Y file\n", |
540 |
> |
progname); |
541 |
> |
exit(1); |
542 |
> |
} |
543 |
> |
if (rbf_colorimetry == RBCtristimulus) { |
544 |
> |
fputs("}\n", uvfp[0]); |
545 |
> |
fputs("}\n", uvfp[1]); |
546 |
> |
if (fclose(uvfp[0]) || fclose(uvfp[1])) { |
547 |
> |
fprintf(stderr, "%s: error writing uv file(s)\n", |
548 |
> |
progname); |
549 |
> |
exit(1); |
550 |
> |
} |
551 |
> |
} |
552 |
> |
} |
553 |
|
} |
554 |
|
|
555 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
556 |
+ |
/* Execute wrapBSDF command (may never return) */ |
557 |
+ |
static int |
558 |
+ |
wrap_up(void) |
559 |
+ |
{ |
560 |
+ |
char cmd[8192]; |
561 |
+ |
|
562 |
+ |
if (bsdf_manuf[0]) { |
563 |
+ |
add_wbsdf("-f", 1); |
564 |
+ |
strcpy(cmd, "m="); |
565 |
+ |
strcpy(cmd+2, bsdf_manuf); |
566 |
+ |
add_wbsdf(cmd, 0); |
567 |
+ |
} |
568 |
+ |
if (bsdf_name[0]) { |
569 |
+ |
add_wbsdf("-f", 1); |
570 |
+ |
strcpy(cmd, "n="); |
571 |
+ |
strcpy(cmd+2, bsdf_name); |
572 |
+ |
add_wbsdf(cmd, 0); |
573 |
+ |
} |
574 |
+ |
if (!convert_commandline(cmd, sizeof(cmd), wrapBSDF)) { |
575 |
+ |
fputs(progname, stderr); |
576 |
+ |
fputs(": command line too long in wrap_up()\n", stderr); |
577 |
+ |
return(1); |
578 |
+ |
} |
579 |
+ |
return(system(cmd)); |
580 |
+ |
} |
581 |
+ |
#else |
582 |
+ |
/* Execute wrapBSDF command (may never return) */ |
583 |
+ |
static int |
584 |
+ |
wrap_up(void) |
585 |
+ |
{ |
586 |
+ |
char buf[256]; |
587 |
+ |
char *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK); |
588 |
+ |
|
589 |
+ |
if (compath == NULL) { |
590 |
+ |
fprintf(stderr, "%s: cannot locate %s\n", progname, wrapBSDF[0]); |
591 |
+ |
return(1); |
592 |
+ |
} |
593 |
+ |
if (bsdf_manuf[0]) { |
594 |
+ |
add_wbsdf("-f", 1); |
595 |
+ |
strcpy(buf, "m="); |
596 |
+ |
strcpy(buf+2, bsdf_manuf); |
597 |
+ |
add_wbsdf(buf, 0); |
598 |
+ |
} |
599 |
+ |
if (bsdf_name[0]) { |
600 |
+ |
add_wbsdf("-f", 1); |
601 |
+ |
strcpy(buf, "n="); |
602 |
+ |
strcpy(buf+2, bsdf_name); |
603 |
+ |
add_wbsdf(buf, 0); |
604 |
+ |
} |
605 |
+ |
execv(compath, wrapBSDF); /* successful call never returns */ |
606 |
+ |
perror(compath); |
607 |
+ |
return(1); |
608 |
+ |
} |
609 |
+ |
#endif |
610 |
+ |
|
611 |
|
/* Read in BSDF and interpolate as tensor tree representation */ |
612 |
|
int |
613 |
|
main(int argc, char *argv[]) |
614 |
|
{ |
615 |
+ |
static char tfmt[2][4] = {"t4", "t3"}; |
616 |
|
int dofwd = 0, dobwd = 1; |
617 |
+ |
char buf[2048]; |
618 |
|
int i, na; |
619 |
|
|
620 |
|
progname = argv[0]; |
622 |
|
esupport &= ~(E_INCHAN|E_OUTCHAN); |
623 |
|
scompile("PI:3.14159265358979323846", NULL, 0); |
624 |
|
biggerlib(); |
625 |
< |
for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
625 |
> |
for (i = 1; i < argc && (argv[i][0] == '-') | (argv[i][0] == '+'); i++) |
626 |
|
switch (argv[i][1]) { /* get options */ |
627 |
|
case 'e': |
628 |
|
scompile(argv[++i], NULL, 0); |
629 |
|
break; |
630 |
|
case 'f': |
631 |
< |
if (!argv[i][2]) |
632 |
< |
fcompile(argv[++i]); |
633 |
< |
else |
631 |
> |
if (!argv[i][2]) { |
632 |
> |
if (strchr(argv[++i], '=') != NULL) { |
633 |
> |
add_wbsdf("-f", 1); |
634 |
> |
add_wbsdf(argv[i], 1); |
635 |
> |
} else { |
636 |
> |
char *fpath = getpath(argv[i], |
637 |
> |
getrlibpath(), 0); |
638 |
> |
if (fpath == NULL) { |
639 |
> |
fprintf(stderr, |
640 |
> |
"%s: cannot find file '%s'\n", |
641 |
> |
argv[0], argv[i]); |
642 |
> |
return(1); |
643 |
> |
} |
644 |
> |
fcompile(fpath); |
645 |
> |
single_plane_incident = 0; |
646 |
> |
} |
647 |
> |
} else |
648 |
|
dofwd = (argv[i][0] == '+'); |
649 |
|
break; |
650 |
+ |
case 'a': |
651 |
+ |
recip = (argv[i][0] == '+') ? " -a" : ""; |
652 |
+ |
break; |
653 |
|
case 'b': |
654 |
|
dobwd = (argv[i][0] == '+'); |
655 |
|
break; |
677 |
|
case 'p': |
678 |
|
do_prog = atoi(argv[i]+2); |
679 |
|
break; |
680 |
+ |
case 'W': |
681 |
+ |
add_wbsdf(argv[i], 1); |
682 |
+ |
break; |
683 |
+ |
case 'u': |
684 |
+ |
case 'C': |
685 |
+ |
add_wbsdf(argv[i], 1); |
686 |
+ |
add_wbsdf(argv[++i], 1); |
687 |
+ |
break; |
688 |
|
default: |
689 |
|
goto userr; |
690 |
|
} |
691 |
+ |
strcpy(buf, "File produced by: "); |
692 |
+ |
if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) { |
693 |
+ |
add_wbsdf("-C", 1); add_wbsdf(buf, 0); |
694 |
+ |
} |
695 |
|
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
696 |
|
void (*evf)(char *s) = single_plane_incident ? |
697 |
< |
&eval_isotropic : &eval_anisotropic; |
697 |
> |
eval_isotropic : eval_anisotropic; |
698 |
|
if (i != argc-1 || fundefined(argv[i]) < 3) { |
699 |
|
fprintf(stderr, |
700 |
|
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
703 |
|
goto userr; |
704 |
|
} |
705 |
|
++eclock; |
706 |
< |
xml_prologue(argc, argv); /* start XML output */ |
706 |
> |
add_wbsdf("-a", 1); |
707 |
> |
add_wbsdf(tfmt[single_plane_incident], 1); |
708 |
|
if (dofwd) { |
709 |
|
input_orient = -1; |
710 |
|
output_orient = -1; |
723 |
|
prog_start("Evaluating inside->outside transmission"); |
724 |
|
(*evf)(argv[i]); |
725 |
|
} |
726 |
< |
xml_epilogue(); /* finish XML output & exit */ |
464 |
< |
return(0); |
726 |
> |
return(wrap_up()); |
727 |
|
} |
728 |
|
if (i < argc) { /* open input files if given */ |
729 |
|
int nbsdf = 0; |
738 |
|
if (!load_bsdf_rep(fpin)) |
739 |
|
return(1); |
740 |
|
fclose(fpin); |
479 |
– |
if (!nbsdf++) /* start XML on first dist. */ |
480 |
– |
xml_prologue(argc, argv); |
741 |
|
sprintf(pbuf, "Interpolating component '%s'", argv[i]); |
742 |
|
prog_start(pbuf); |
743 |
+ |
if (!nbsdf++) { |
744 |
+ |
add_wbsdf("-a", 1); |
745 |
+ |
add_wbsdf(tfmt[single_plane_incident], 1); |
746 |
+ |
} |
747 |
|
if (single_plane_incident) |
748 |
|
eval_isotropic(NULL); |
749 |
|
else |
750 |
|
eval_anisotropic(NULL); |
751 |
|
} |
752 |
< |
xml_epilogue(); /* finish XML output & exit */ |
489 |
< |
return(0); |
752 |
> |
return(wrap_up()); |
753 |
|
} |
754 |
|
SET_FILE_BINARY(stdin); /* load from stdin */ |
755 |
|
if (!load_bsdf_rep(stdin)) |
756 |
|
return(1); |
494 |
– |
xml_prologue(argc, argv); /* start XML output */ |
757 |
|
prog_start("Interpolating from standard input"); |
758 |
+ |
add_wbsdf("-a", 1); |
759 |
+ |
add_wbsdf(tfmt[single_plane_incident], 1); |
760 |
|
if (single_plane_incident) /* resample dist. */ |
761 |
|
eval_isotropic(NULL); |
762 |
|
else |
763 |
|
eval_anisotropic(NULL); |
764 |
< |
xml_epilogue(); /* finish XML output & exit */ |
765 |
< |
return(0); |
764 |
> |
|
765 |
> |
return(wrap_up()); |
766 |
|
userr: |
767 |
|
fprintf(stderr, |
768 |
< |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
768 |
> |
"Usage: %s [{+|-}a][-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
769 |
|
progname); |
770 |
|
fprintf(stderr, |
771 |
< |
" or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
771 |
> |
" or: %s -t{3|4} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |
772 |
|
progname); |
773 |
|
return(1); |
774 |
|
} |