11 |
|
#include <stdio.h> |
12 |
|
#include <stdlib.h> |
13 |
|
#include <math.h> |
14 |
+ |
#include "random.h" |
15 |
|
#include "platform.h" |
16 |
+ |
#include "rtprocess.h" |
17 |
|
#include "calcomp.h" |
18 |
|
#include "bsdfrep.h" |
19 |
|
/* global argv[0] */ |
22 |
|
double pctcull = 90.; |
23 |
|
/* sampling order */ |
24 |
|
int samp_order = 6; |
25 |
+ |
/* super-sampling threshold */ |
26 |
+ |
const double ssamp_thresh = 0.35; |
27 |
+ |
/* number of super-samples */ |
28 |
+ |
const int nssamp = 100; |
29 |
+ |
/* limit on number of RBF lobes */ |
30 |
+ |
static int lobe_lim = 15000; |
31 |
|
|
32 |
|
/* Output XML prologue to stdout */ |
33 |
|
static void |
46 |
|
puts("<Optical>"); |
47 |
|
puts("<Layer>"); |
48 |
|
puts("\t<Material>"); |
49 |
< |
puts("\t\t<Name>Name</Name>"); |
50 |
< |
puts("\t\t<Manufacturer>Manufacturer</Manufacturer>"); |
49 |
> |
printf("\t\t<Name>%s</Name>\n", bsdf_name[0] ? bsdf_name : "Unknown"); |
50 |
> |
printf("\t\t<Manufacturer>%s</Manufacturer>\n", |
51 |
> |
bsdf_manuf[0] ? bsdf_manuf : "Unknown"); |
52 |
|
puts("\t\t<DeviceType>Other</DeviceType>"); |
53 |
|
puts("\t</Material>"); |
54 |
|
puts("\t<DataDefinition>"); |
99 |
|
puts("</WindowElement>"); |
100 |
|
} |
101 |
|
|
102 |
+ |
/* Compute absolute relative difference */ |
103 |
+ |
static double |
104 |
+ |
abs_diff(double v1, double v0) |
105 |
+ |
{ |
106 |
+ |
if ((v0 < 0) | (v1 < 0)) |
107 |
+ |
return(.0); |
108 |
+ |
v1 = (v1-v0)*2./(v0+v1+.0001); |
109 |
+ |
if (v1 < 0) |
110 |
+ |
return(-v1); |
111 |
+ |
return(v1); |
112 |
+ |
} |
113 |
+ |
|
114 |
|
/* Interpolate and output isotropic BSDF data */ |
115 |
|
static void |
116 |
|
eval_isotropic(char *funame) |
117 |
|
{ |
118 |
|
const int sqres = 1<<samp_order; |
119 |
|
FILE *ofp = NULL; |
120 |
+ |
int assignD = 0; |
121 |
|
char cmd[128]; |
122 |
|
int ix, ox, oy; |
123 |
|
double iovec[6]; |
124 |
|
float bsdf; |
125 |
< |
#if DEBUG |
104 |
< |
fprintf(stderr, "Writing isotropic order %d ", samp_order); |
105 |
< |
if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull); |
106 |
< |
else fputs("raw data\n", stderr); |
107 |
< |
#endif |
125 |
> |
|
126 |
|
data_prologue(); /* begin output */ |
127 |
|
if (pctcull >= 0) { |
128 |
< |
sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d", |
128 |
> |
sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d", |
129 |
|
pctcull, samp_order); |
130 |
|
fflush(stdout); |
131 |
|
ofp = popen(cmd, "w"); |
137 |
|
SET_FILE_BINARY(ofp); |
138 |
|
} else |
139 |
|
fputs("{\n", stdout); |
140 |
+ |
/* need to assign Dx, Dy, Dz? */ |
141 |
+ |
if (funame != NULL) |
142 |
+ |
assignD = (fundefined(funame) < 6); |
143 |
|
/* run through directions */ |
144 |
|
for (ix = 0; ix < sqres/2; ix++) { |
145 |
|
RBFNODE *rbf = NULL; |
146 |
< |
iovec[0] = (ix+.5)/sqres - 1.; |
146 |
> |
iovec[0] = 2.*(ix+.5)/sqres - 1.; |
147 |
|
iovec[1] = .0; |
148 |
|
iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]); |
149 |
|
if (funame == NULL) |
150 |
< |
rbf = advect_rbf(iovec); |
151 |
< |
for (ox = 0; ox < sqres; ox++) |
150 |
> |
rbf = advect_rbf(iovec, lobe_lim); |
151 |
> |
for (ox = 0; ox < sqres; ox++) { |
152 |
> |
float last_bsdf = -1; |
153 |
|
for (oy = 0; oy < sqres; oy++) { |
154 |
|
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
155 |
|
iovec[5] = output_orient * |
156 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
157 |
|
if (funame == NULL) |
158 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
158 |
> |
bsdf = eval_rbfrep(rbf, iovec+3) * |
159 |
|
output_orient/iovec[5]; |
160 |
< |
else |
161 |
< |
bsdf = funvalue(funame, 6, iovec); |
160 |
> |
else { |
161 |
> |
double ssa[3], ssvec[6], sum; |
162 |
> |
int ssi; |
163 |
> |
if (assignD) { |
164 |
> |
varset("Dx", '=', -iovec[3]); |
165 |
> |
varset("Dy", '=', -iovec[4]); |
166 |
> |
varset("Dz", '=', -iovec[5]); |
167 |
> |
++eclock; |
168 |
> |
} |
169 |
> |
bsdf = funvalue(funame, 6, iovec); |
170 |
> |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
171 |
> |
sum = 0; /* super-sample voxel */ |
172 |
> |
for (ssi = nssamp; ssi--; ) { |
173 |
> |
SDmultiSamp(ssa, 3, (ssi+frandom())/nssamp); |
174 |
> |
ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.; |
175 |
> |
ssvec[1] = .0; |
176 |
> |
ssvec[2] = input_orient * |
177 |
> |
sqrt(1. - ssvec[0]*ssvec[0]); |
178 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres, |
179 |
> |
(oy+ssa[2])/sqres); |
180 |
> |
ssvec[5] = output_orient * |
181 |
> |
sqrt(1. - ssvec[3]*ssvec[3] - |
182 |
> |
ssvec[4]*ssvec[4]); |
183 |
> |
if (assignD) { |
184 |
> |
varset("Dx", '=', -iovec[3]); |
185 |
> |
varset("Dy", '=', -iovec[4]); |
186 |
> |
varset("Dz", '=', -iovec[5]); |
187 |
> |
++eclock; |
188 |
> |
} |
189 |
> |
sum += funvalue(funame, 6, ssvec); |
190 |
> |
} |
191 |
> |
bsdf = sum/nssamp; |
192 |
> |
} |
193 |
> |
} |
194 |
|
if (pctcull >= 0) |
195 |
|
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
196 |
|
else |
197 |
|
printf("\t%.3e\n", bsdf); |
198 |
+ |
last_bsdf = bsdf; |
199 |
|
} |
200 |
+ |
} |
201 |
|
if (rbf != NULL) |
202 |
|
free(rbf); |
203 |
|
} |
221 |
|
{ |
222 |
|
const int sqres = 1<<samp_order; |
223 |
|
FILE *ofp = NULL; |
224 |
+ |
int assignD = 0; |
225 |
|
char cmd[128]; |
226 |
|
int ix, iy, ox, oy; |
227 |
|
double iovec[6]; |
228 |
|
float bsdf; |
229 |
< |
#if DEBUG |
173 |
< |
fprintf(stderr, "Writing anisotropic order %d ", samp_order); |
174 |
< |
if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull); |
175 |
< |
else fputs("raw data\n", stderr); |
176 |
< |
#endif |
229 |
> |
|
230 |
|
data_prologue(); /* begin output */ |
231 |
|
if (pctcull >= 0) { |
232 |
< |
sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d", |
232 |
> |
sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d", |
233 |
> |
(input_orient>0 ^ output_orient>0) ? "" : " -a", |
234 |
|
pctcull, samp_order); |
235 |
|
fflush(stdout); |
236 |
|
ofp = popen(cmd, "w"); |
241 |
|
} |
242 |
|
} else |
243 |
|
fputs("{\n", stdout); |
244 |
+ |
/* need to assign Dx, Dy, Dz? */ |
245 |
+ |
if (funame != NULL) |
246 |
+ |
assignD = (fundefined(funame) < 6); |
247 |
|
/* run through directions */ |
248 |
|
for (ix = 0; ix < sqres; ix++) |
249 |
|
for (iy = 0; iy < sqres; iy++) { |
250 |
|
RBFNODE *rbf = NULL; /* Klems reversal */ |
251 |
< |
SDsquare2disk(iovec, (ix+.5)/sqres, (iy+.5)/sqres); |
195 |
< |
iovec[0] = -iovec[0]; iovec[1] = -iovec[1]; |
251 |
> |
SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres); |
252 |
|
iovec[2] = input_orient * |
253 |
|
sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]); |
254 |
|
if (funame == NULL) |
255 |
< |
rbf = advect_rbf(iovec); |
256 |
< |
for (ox = 0; ox < sqres; ox++) |
255 |
> |
rbf = advect_rbf(iovec, lobe_lim); |
256 |
> |
for (ox = 0; ox < sqres; ox++) { |
257 |
> |
float last_bsdf = -1; |
258 |
|
for (oy = 0; oy < sqres; oy++) { |
259 |
|
SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres); |
260 |
|
iovec[5] = output_orient * |
261 |
|
sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]); |
262 |
|
if (funame == NULL) |
263 |
< |
bsdf = eval_rbfrep(rbf, iovec+3) * |
263 |
> |
bsdf = eval_rbfrep(rbf, iovec+3) * |
264 |
|
output_orient/iovec[5]; |
265 |
< |
else |
266 |
< |
bsdf = funvalue(funame, 6, iovec); |
265 |
> |
else { |
266 |
> |
double ssa[4], ssvec[6], sum; |
267 |
> |
int ssi; |
268 |
> |
if (assignD) { |
269 |
> |
varset("Dx", '=', -iovec[3]); |
270 |
> |
varset("Dy", '=', -iovec[4]); |
271 |
> |
varset("Dz", '=', -iovec[5]); |
272 |
> |
++eclock; |
273 |
> |
} |
274 |
> |
bsdf = funvalue(funame, 6, iovec); |
275 |
> |
if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) { |
276 |
> |
sum = 0; /* super-sample voxel */ |
277 |
> |
for (ssi = nssamp; ssi--; ) { |
278 |
> |
SDmultiSamp(ssa, 4, (ssi+frandom())/nssamp); |
279 |
> |
SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres, |
280 |
> |
1.-(iy+ssa[1])/sqres); |
281 |
> |
ssvec[2] = output_orient * |
282 |
> |
sqrt(1. - ssvec[0]*ssvec[0] - |
283 |
> |
ssvec[1]*ssvec[1]); |
284 |
> |
SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres, |
285 |
> |
(oy+ssa[3])/sqres); |
286 |
> |
ssvec[5] = output_orient * |
287 |
> |
sqrt(1. - ssvec[3]*ssvec[3] - |
288 |
> |
ssvec[4]*ssvec[4]); |
289 |
> |
if (assignD) { |
290 |
> |
varset("Dx", '=', -iovec[3]); |
291 |
> |
varset("Dy", '=', -iovec[4]); |
292 |
> |
varset("Dz", '=', -iovec[5]); |
293 |
> |
++eclock; |
294 |
> |
} |
295 |
> |
sum += funvalue(funame, 6, ssvec); |
296 |
> |
} |
297 |
> |
bsdf = sum/nssamp; |
298 |
> |
} |
299 |
> |
} |
300 |
|
if (pctcull >= 0) |
301 |
|
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
302 |
|
else |
303 |
|
printf("\t%.3e\n", bsdf); |
304 |
+ |
last_bsdf = bsdf; |
305 |
|
} |
306 |
+ |
} |
307 |
|
if (rbf != NULL) |
308 |
|
free(rbf); |
309 |
|
} |
362 |
|
case 'g': |
363 |
|
samp_order = atoi(argv[++i]); |
364 |
|
break; |
365 |
+ |
case 'l': |
366 |
+ |
lobe_lim = atoi(argv[++i]); |
367 |
+ |
break; |
368 |
|
default: |
369 |
|
goto userr; |
370 |
|
} |
371 |
|
if (single_plane_incident >= 0) { /* function-based BSDF? */ |
372 |
|
void (*evf)(char *s) = single_plane_incident ? |
373 |
|
&eval_isotropic : &eval_anisotropic; |
374 |
< |
if (i != argc-1 || fundefined(argv[i]) != 6) { |
374 |
> |
if (i != argc-1 || fundefined(argv[i]) < 3) { |
375 |
|
fprintf(stderr, |
376 |
|
"%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n", |
377 |
|
progname); |
378 |
+ |
fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n", |
379 |
+ |
progname); |
380 |
|
goto userr; |
381 |
|
} |
382 |
+ |
++eclock; |
383 |
|
xml_prologue(argc, argv); /* start XML output */ |
384 |
|
if (dofwd) { |
385 |
|
input_orient = -1; |
432 |
|
return(0); |
433 |
|
userr: |
434 |
|
fprintf(stderr, |
435 |
< |
"Usage: %s [-g Nlog2][-t pctcull] [bsdf.sir ..] > bsdf.xml\n", |
435 |
> |
"Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n", |
436 |
|
progname); |
437 |
|
fprintf(stderr, |
438 |
|
" or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n", |