ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2ttree.c
Revision: 2.44
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.43: +1 -2 lines
Log Message:
Removed redundant include files

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.43 2018/05/04 23:56:49 greg Exp $";
3 #endif
4 /*
5 * Load measured BSDF interpolant and write out as XML file with tensor tree.
6 *
7 * G. Ward
8 */
9
10 #define _USE_MATH_DEFINES
11 #include <stdlib.h>
12 #include <math.h>
13 #include "random.h"
14 #include "platform.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 static double pctcull = 90.;
25 /* sampling order */
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 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
78 /* Draw progress bar of the appropriate length */
79 static void
80 prog_show(double frac)
81 {
82 static unsigned call_cnt = 0;
83 static char lastc[] = "-\\|/";
84 char pbar[256];
85 int nchars;
86
87 if (do_prog <= 1) return;
88 if (do_prog > sizeof(pbar)-2)
89 do_prog = sizeof(pbar)-2;
90 if (frac < 0) frac = 0;
91 else if (frac >= 1) frac = .9999;
92 nchars = do_prog*frac;
93 pbar[0] = '\r';
94 memset(pbar+1, '*', nchars);
95 pbar[nchars+1] = lastc[call_cnt++ & 3];
96 memset(pbar+2+nchars, '-', do_prog-nchars-1);
97 pbar[do_prog+1] = '\0';
98 fputs(pbar, stderr);
99 }
100
101 /* Finish progress bar */
102 static void
103 prog_done(void)
104 {
105 int n = do_prog;
106
107 if (n <= 1) return;
108 fputc('\r', stderr);
109 while (n--)
110 fputc(' ', stderr);
111 fputc('\r', stderr);
112 }
113
114 /* Compute absolute relative difference */
115 static double
116 abs_diff(double v1, double v0)
117 {
118 if ((v0 < 0) | (v1 < 0))
119 return(.0);
120 v1 = (v1-v0)*2./(v0+v1+.0001);
121 if (v1 < 0)
122 return(-v1);
123 return(v1);
124 }
125
126 /* Interpolate and output isotropic BSDF data */
127 static void
128 eval_isotropic(char *funame)
129 {
130 const int sqres = 1<<samp_order;
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, uv[2];
138
139 if (pctcull >= 0) {
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",
145 progname);
146 exit(1);
147 }
148 SET_FILE_BINARY(ofp);
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++) {
194 RBFNODE *rbf = NULL;
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)
199 rbf = advect_rbf(iovec, lobe_lim);
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)*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 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]);
248 varset("Dz", '=', -iovec[5]);
249 ++eclock;
250 }
251 bsdf = funvalue(funame, 6, iovec);
252 #if (NSSAMP > 0)
253 if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
254 int ssi;
255 double ssa[3], ssvec[6], sum = 0;
256 /* super-sample voxel */
257 for (ssi = NSSAMP; ssi--; ) {
258 SDmultiSamp(ssa, 3, (ssi+frandom()) *
259 (1./NSSAMP));
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])*sqfact,
265 (oy+ssa[2])*sqfact);
266 ssvec[5] = output_orient *
267 sqrt(1. - ssvec[3]*ssvec[3] -
268 ssvec[4]*ssvec[4]);
269 if (assignD) {
270 varset("Dx", '=', -ssvec[3]);
271 varset("Dy", '=', -ssvec[4]);
272 varset("Dz", '=', -ssvec[5]);
273 ++eclock;
274 }
275 sum += funvalue(funame, 6, ssvec);
276 }
277 bsdf = sum * (1./NSSAMP);
278 }
279 #endif
280 }
281 if (pctcull >= 0)
282 putbinary(&bsdf, sizeof(bsdf), 1, ofp);
283 else
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.*sqfact));
301 }
302 prog_done();
303 if (pctcull >= 0) { /* finish output */
304 if (pclose(ofp)) {
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", 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 }
338 }
339
340 /* Interpolate and output anisotropic BSDF data */
341 static void
342 eval_anisotropic(char *funame)
343 {
344 const int sqres = 1<<samp_order;
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, uv[2];
352
353 if (pctcull >= 0) {
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",
362 progname);
363 exit(1);
364 }
365 SET_FILE_BINARY(ofp);
366 #ifdef getc_unlocked /* avoid lock/unlock overhead */
367 flockfile(ofp);
368 #endif
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)*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)
419 rbf = advect_rbf(iovec, lobe_lim);
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)*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 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]);
468 varset("Dz", '=', -iovec[5]);
469 ++eclock;
470 }
471 bsdf = funvalue(funame, 6, iovec);
472 #if (NSSAMP > 0)
473 if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
474 int ssi;
475 double ssa[4], ssvec[6], sum = 0;
476 /* super-sample voxel */
477 for (ssi = NSSAMP; ssi--; ) {
478 SDmultiSamp(ssa, 4, (ssi+frandom()) *
479 (1./NSSAMP));
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])*sqfact,
486 (oy+ssa[3])*sqfact);
487 ssvec[5] = output_orient *
488 sqrt(1. - ssvec[3]*ssvec[3] -
489 ssvec[4]*ssvec[4]);
490 if (assignD) {
491 varset("Dx", '=', -ssvec[3]);
492 varset("Dy", '=', -ssvec[4]);
493 varset("Dz", '=', -ssvec[5]);
494 ++eclock;
495 }
496 sum += funvalue(funame, 6, ssvec);
497 }
498 bsdf = sum * (1./NSSAMP);
499 }
500 #endif
501 }
502 if (pctcull >= 0)
503 putbinary(&bsdf, sizeof(bsdf), 1, ofp);
504 else
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 }
519 if (rbf != NULL)
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 rttree_reduce on Y\n",
527 progname);
528 exit(1);
529 }
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];
621 esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
622 esupport &= ~(E_INCHAN|E_OUTCHAN);
623 scompile("PI:3.14159265358979323846", NULL, 0);
624 biggerlib();
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 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;
656 case 't':
657 switch (argv[i][2]) {
658 case '3':
659 single_plane_incident = 1;
660 break;
661 case '4':
662 single_plane_incident = 0;
663 break;
664 case '\0':
665 pctcull = atof(argv[++i]);
666 break;
667 default:
668 goto userr;
669 }
670 break;
671 case 'g':
672 samp_order = atoi(argv[++i]);
673 break;
674 case 'l':
675 lobe_lim = atoi(argv[++i]);
676 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;
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",
701 progname);
702 fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
703 goto userr;
704 }
705 ++eclock;
706 add_wbsdf("-a", 1);
707 add_wbsdf(tfmt[single_plane_incident], 1);
708 if (dofwd) {
709 input_orient = -1;
710 output_orient = -1;
711 prog_start("Evaluating outside reflectance");
712 (*evf)(argv[i]);
713 output_orient = 1;
714 prog_start("Evaluating outside->inside transmission");
715 (*evf)(argv[i]);
716 }
717 if (dobwd) {
718 input_orient = 1;
719 output_orient = 1;
720 prog_start("Evaluating inside reflectance");
721 (*evf)(argv[i]);
722 output_orient = -1;
723 prog_start("Evaluating inside->outside transmission");
724 (*evf)(argv[i]);
725 }
726 return(wrap_up());
727 }
728 if (i < argc) { /* open input files if given */
729 int nbsdf = 0;
730 for ( ; i < argc; i++) { /* interpolate each component */
731 char pbuf[256];
732 FILE *fpin = fopen(argv[i], "rb");
733 if (fpin == NULL) {
734 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
735 progname, argv[i]);
736 return(1);
737 }
738 if (!load_bsdf_rep(fpin))
739 return(1);
740 fclose(fpin);
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 return(wrap_up());
753 }
754 SET_FILE_BINARY(stdin); /* load from stdin */
755 if (!load_bsdf_rep(stdin))
756 return(1);
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
765 return(wrap_up());
766 userr:
767 fprintf(stderr,
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} [{+|-}a][-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
772 progname);
773 return(1);
774 }