ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2ttree.c
(Generate patch)

Comparing ray/src/cv/bsdf2ttree.c (file contents):
Revision 2.8 by greg, Tue Nov 13 04:23:38 2012 UTC vs.
Revision 2.34 by greg, Tue Feb 2 22:34:00 2016 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
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                                  /* percentage to cull (<0 to turn off) */
23 < double                  pctcull = 90.;
23 > static double           pctcull = 90.;
24                                  /* sampling order */
25 < int                     samp_order = 6;
25 > static int              samp_order = 6;
26 >                                /* super-sampling threshold */
27 > const double            ssamp_thresh = 0.35;
28 >                                /* number of super-samples */
29 > #ifndef NSSAMP
30 > #define NSSAMP          100
31 > #endif
32 >                                /* limit on number of RBF lobes */
33 > static int              lobe_lim = 15000;
34 >                                /* progress bar length */
35 > static int              do_prog = 79;
36  
37 + #define MAXCARG         512     /* wrapBSDF command */
38 + static char             *wrapBSDF[MAXCARG] = {"wrapBSDF", "-U"};
39 + static int              wbsdfac = 2;
40 +
41 + /* Add argument to wrapBSDF, allocating space if isstatic */
42 + static void
43 + add_wbsdf(const char *arg, int isstatic)
44 + {
45 +        if (arg == NULL)
46 +                return;
47 +        if (wbsdfac >= MAXCARG-1) {
48 +                fputs(progname, stderr);
49 +                fputs(": too many command arguments to wrapBSDF\n", stderr);
50 +                exit(1);
51 +        }
52 +        if (!*arg)
53 +                arg = "";
54 +        else if (!isstatic)
55 +                arg = savqstr((char *)arg);
56 +
57 +        wrapBSDF[wbsdfac++] = (char *)arg;
58 + }
59 +
60 + /* Create Yuv component file and add appropriate arguments */
61 + static char *
62 + create_component_file(int c)
63 + {
64 +        static const char       sname[3][6] = {"CIE-Y", "CIE-u", "CIE-v"};
65 +        static const char       cname[4][4] = {"-rf", "-tf", "-tb", "-rb"};
66 +        char                    *tfname = mktemp(savqstr(TEMPLATE));
67 +
68 +        add_wbsdf("-s", 1); add_wbsdf(sname[c], 1);
69 +        add_wbsdf(cname[(input_orient>0)<<1 | (output_orient>0)], 1);
70 +        add_wbsdf(tfname, 1);
71 +        return(tfname);
72 + }
73 +
74 + /* Start new progress bar */
75 + #define prog_start(s)   if (do_prog) fprintf(stderr, "%s: %s...\n", progname, s); else
76 +
77 + /* Draw progress bar of the appropriate length */
78 + static void
79 + prog_show(double frac)
80 + {
81 +        static unsigned call_cnt = 0;
82 +        static char     lastc[] = "-\\|/";
83 +        char            pbar[256];
84 +        int             nchars;
85 +
86 +        if (do_prog <= 1) return;
87 +        if (do_prog > sizeof(pbar)-2)
88 +                do_prog = sizeof(pbar)-2;
89 +        if (frac < 0) frac = 0;
90 +        else if (frac >= 1) frac = .9999;
91 +        nchars = do_prog*frac;
92 +        pbar[0] = '\r';
93 +        memset(pbar+1, '*', nchars);
94 +        pbar[nchars+1] = lastc[call_cnt++ & 3];
95 +        memset(pbar+2+nchars, '-', do_prog-nchars-1);
96 +        pbar[do_prog+1] = '\0';
97 +        fputs(pbar, stderr);
98 + }
99 +
100 + /* Finish progress bar */
101 + static void
102 + prog_done(void)
103 + {
104 +        int     n = do_prog;
105 +
106 +        if (n <= 1) return;
107 +        fputc('\r', stderr);
108 +        while (n--)
109 +                fputc(' ', stderr);
110 +        fputc('\r', stderr);
111 + }
112 +
113 + /* Compute absolute relative difference */
114 + static double
115 + abs_diff(double v1, double v0)
116 + {
117 +        if ((v0 < 0) | (v1 < 0))
118 +                return(.0);
119 +        v1 = (v1-v0)*2./(v0+v1+.0001);
120 +        if (v1 < 0)
121 +                return(-v1);
122 +        return(v1);
123 + }
124 +
125   /* Interpolate and output isotropic BSDF data */
126   static void
127 < interp_isotropic()
127 > eval_isotropic(char *funame)
128   {
129          const int       sqres = 1<<samp_order;
130 <        FILE            *ofp = NULL;
130 >        FILE            *ofp, *uvfp[2];
131 >        int             assignD = 0;
132          char            cmd[128];
133          int             ix, ox, oy;
134 <        FVECT           ivec, ovec;
135 <        float           bsdf;
136 < #if DEBUG
137 <        fprintf(stderr, "Writing isotropic order %d ", samp_order);
138 <        if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull);
139 <        else fputs("raw data\n", stderr);
37 < #endif
38 <        if (pctcull >= 0) {                     /* begin output */
39 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 3 -t %f -g %d",
40 <                                pctcull, samp_order);
41 <                fflush(stdout);
134 >        double          iovec[6];
135 >        float           bsdf, uv[2];
136 >
137 >        if (pctcull >= 0) {
138 >                sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s",
139 >                                pctcull, samp_order, create_component_file(0));
140                  ofp = popen(cmd, "w");
141                  if (ofp == NULL) {
142                          fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n",
# Line 46 | Line 144 | interp_isotropic()
144                          exit(1);
145                  }
146                  SET_FILE_BINARY(ofp);
147 <        } else
148 <                fputs("{\n", stdout);
147 > #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
148 >                flockfile(ofp);
149 > #endif
150 >                if (rbf_colorimetry == RBCtristimulus) {
151 >                        double  uvcull = 100. - (100.-pctcull)*.25;
152 >                        sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s",
153 >                                        uvcull, samp_order, create_component_file(1));
154 >                        uvfp[0] = popen(cmd, "w");
155 >                        sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d > %s",
156 >                                        uvcull, samp_order, create_component_file(2));
157 >                        uvfp[1] = popen(cmd, "w");
158 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
159 >                                fprintf(stderr, "%s: cannot open pipes to uv output\n",
160 >                                                progname);
161 >                                exit(1);
162 >                        }
163 >                        SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]);
164 > #ifdef getc_unlocked
165 >                        flockfile(uvfp[0]); flockfile(uvfp[1]);
166 > #endif
167 >                }
168 >        } else {
169 >                ofp = fopen(create_component_file(0), "w");
170 >                if (ofp == NULL) {
171 >                        fprintf(stderr, "%s: cannot create Y output file\n",
172 >                                        progname);
173 >                        exit(1);
174 >                }
175 >                fputs("{\n", ofp);
176 >                if (rbf_colorimetry == RBCtristimulus) {
177 >                        uvfp[0] = fopen(create_component_file(1), "w");
178 >                        uvfp[1] = fopen(create_component_file(2), "w");
179 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
180 >                                fprintf(stderr, "%s: cannot create uv output file(s)\n",
181 >                                                progname);
182 >                                exit(1);
183 >                        }
184 >                        fputs("{\n", uvfp[0]);
185 >                        fputs("{\n", uvfp[1]);
186 >                }
187 >        }
188 >                                                /* need to assign Dx, Dy, Dz? */
189 >        if (funame != NULL)
190 >                assignD = (fundefined(funame) < 6);
191                                                  /* run through directions */
192          for (ix = 0; ix < sqres/2; ix++) {
193 <                RBFNODE *rbf;
194 <                SDsquare2disk(ivec, (ix+.5)/sqres, .5);
195 <                ivec[2] = input_orient *
196 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
197 <                rbf = advect_rbf(ivec);
198 <                for (ox = 0; ox < sqres; ox++)
193 >                RBFNODE *rbf = NULL;
194 >                iovec[0] = 2.*(ix+.5)/sqres - 1.;
195 >                iovec[1] = .0;
196 >                iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
197 >                if (funame == NULL)
198 >                        rbf = advect_rbf(iovec, lobe_lim);
199 >                for (ox = 0; ox < sqres; ox++) {
200 >                    float       last_bsdf = -1;
201                      for (oy = 0; oy < sqres; oy++) {
202 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
203 <                        ovec[2] = output_orient *
204 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
205 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
202 >                        SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
203 >                        iovec[5] = output_orient *
204 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
205 >                        if (funame == NULL) {
206 >                            SDValue     sdv;
207 >                            eval_rbfcol(&sdv, rbf, iovec+3);
208 >                            bsdf = sdv.cieY;
209 >                            if (rbf_colorimetry == RBCtristimulus) {
210 >                                c_ccvt(&sdv.spec, C_CSXY);
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]);
220 >                                varset("Dz", '=', -iovec[5]);
221 >                                ++eclock;
222 >                            }
223 >                            bsdf = funvalue(funame, 6, iovec);
224 > #if (NSSAMP > 0)
225 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
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 *
235 >                                                sqrt(1. - ssvec[0]*ssvec[0]);
236 >                                    SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres,
237 >                                                (oy+ssa[2])/sqres);
238 >                                    ssvec[5] = output_orient *
239 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
240 >                                                        ssvec[4]*ssvec[4]);
241 >                                    if (assignD) {
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;
250 >                            }
251 > #endif
252 >                        }
253                          if (pctcull >= 0)
254                                  fwrite(&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 >                                        fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]);
261 >                                        fwrite(&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          if (pctcull >= 0) {                     /* finish output */
275                  if (pclose(ofp)) {
276 <                        fprintf(stderr, "%s: error running '%s'\n",
277 <                                        progname, cmd);
276 >                        fprintf(stderr, "%s: error running rttree_reduce on Y\n",
277 >                                        progname);
278                          exit(1);
279                  }
280 +                if (rbf_colorimetry == RBCtristimulus &&
281 +                                (pclose(uvfp[0]) || pclose(uvfp[1]))) {
282 +                        fprintf(stderr, "%s: error running rttree_reduce on uv\n",
283 +                                        progname);
284 +                        exit(1);
285 +                }
286          } else {
287                  for (ix = sqres*sqres*sqres/2; ix--; )
288 <                        fputs("\t0\n", stdout);
289 <                fputs("}\n", stdout);
288 >                        fputs("\t0\n", ofp);
289 >                fputs("}\n", ofp);
290 >                if (fclose(ofp)) {
291 >                        fprintf(stderr, "%s: error writing Y file\n",
292 >                                        progname);
293 >                        exit(1);
294 >                }
295 >                if (rbf_colorimetry == RBCtristimulus) {
296 >                        for (ix = sqres*sqres*sqres/2; ix--; ) {
297 >                                fputs("\t0\n", uvfp[0]);
298 >                                fputs("\t0\n", uvfp[1]);
299 >                        }
300 >                        fputs("}\n", uvfp[0]);
301 >                        fputs("}\n", uvfp[1]);
302 >                        if (fclose(uvfp[0]) || fclose(uvfp[1])) {
303 >                                fprintf(stderr, "%s: error writing uv file(s)\n",
304 >                                                progname);
305 >                                exit(1);
306 >                        }
307 >                }
308          }
309 +        prog_done();
310   }
311  
312   /* Interpolate and output anisotropic BSDF data */
313   static void
314 < interp_anisotropic()
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 <        FVECT           ivec, ovec;
322 <        float           bsdf;
323 < #if DEBUG
324 <        fprintf(stderr, "Writing anisotropic order %d ", samp_order);
325 <        if (pctcull >= 0) fprintf(stderr, "data with %.1f%% culling\n", pctcull);
326 <        else fputs("raw data\n", stderr);
327 < #endif
100 <        if (pctcull >= 0) {                     /* begin output */
101 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d",
102 <                                pctcull, samp_order);
103 <                fflush(stdout);
321 >        double          iovec[6];
322 >        float           bsdf, uv[2];
323 >
324 >        if (pctcull >= 0) {
325 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s",
326 >                                (input_orient>0 ^ output_orient>0) ? "" : " -a",
327 >                                pctcull, samp_order, create_component_file(0));
328                  ofp = popen(cmd, "w");
329                  if (ofp == NULL) {
330                          fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n",
331                                          progname);
332                          exit(1);
333                  }
334 <        } else
335 <                fputs("{\n", stdout);
334 >                SET_FILE_BINARY(ofp);
335 > #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
336 >                flockfile(ofp);
337 > #endif
338 >                if (rbf_colorimetry == RBCtristimulus) {
339 >                        double  uvcull = 100. - (100.-pctcull)*.25;
340 >                        sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s",
341 >                                        (input_orient>0 ^ output_orient>0) ? "" : " -a",
342 >                                        uvcull, samp_order, create_component_file(1));
343 >                        uvfp[0] = popen(cmd, "w");
344 >                        sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s",
345 >                                        (input_orient>0 ^ output_orient>0) ? "" : " -a",
346 >                                        uvcull, samp_order, create_component_file(2));
347 >                        uvfp[1] = popen(cmd, "w");
348 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
349 >                                fprintf(stderr, "%s: cannot open pipes to uv output\n",
350 >                                                progname);
351 >                                exit(1);
352 >                        }
353 >                        SET_FILE_BINARY(uvfp[0]); SET_FILE_BINARY(uvfp[1]);
354 > #ifdef getc_unlocked
355 >                        flockfile(uvfp[0]); flockfile(uvfp[1]);
356 > #endif
357 >                }
358 >        } else {
359 >                ofp = fopen(create_component_file(0), "w");
360 >                if (ofp == NULL) {
361 >                        fprintf(stderr, "%s: cannot create Y output file\n",
362 >                                        progname);
363 >                        exit(1);
364 >                }
365 >                fputs("{\n", ofp);
366 >                if (rbf_colorimetry == RBCtristimulus) {
367 >                        uvfp[0] = fopen(create_component_file(1), "w");
368 >                        uvfp[1] = fopen(create_component_file(2), "w");
369 >                        if ((uvfp[0] == NULL) | (uvfp[1] == NULL)) {
370 >                                fprintf(stderr, "%s: cannot create uv output file(s)\n",
371 >                                                progname);
372 >                                exit(1);
373 >                        }
374 >                        fputs("{\n", uvfp[0]);
375 >                        fputs("{\n", uvfp[1]);
376 >                }
377 >        }
378 >                                                /* need to assign Dx, Dy, Dz? */
379 >        if (funame != NULL)
380 >                assignD = (fundefined(funame) < 6);
381                                                  /* run through directions */
382          for (ix = 0; ix < sqres; ix++)
383              for (iy = 0; iy < sqres; iy++) {
384 <                RBFNODE *rbf;
385 <                SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres);
386 <                ivec[2] = input_orient *
387 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
388 <                rbf = advect_rbf(ivec);
389 <                for (ox = 0; ox < sqres; ox++)
384 >                RBFNODE *rbf = NULL;            /* Klems reversal */
385 >                SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres);
386 >                iovec[2] = input_orient *
387 >                                sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
388 >                if (funame == NULL)
389 >                        rbf = advect_rbf(iovec, lobe_lim);
390 >                for (ox = 0; ox < sqres; ox++) {
391 >                    float       last_bsdf = -1;
392                      for (oy = 0; oy < sqres; oy++) {
393 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
394 <                        ovec[2] = output_orient *
395 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
396 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
393 >                        SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
394 >                        iovec[5] = output_orient *
395 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
396 >                        if (funame == NULL) {
397 >                            SDValue     sdv;
398 >                            eval_rbfcol(&sdv, rbf, iovec+3);
399 >                            bsdf = sdv.cieY;
400 >                            if (rbf_colorimetry == RBCtristimulus) {
401 >                                c_ccvt(&sdv.spec, C_CSXY);
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]);
411 >                                varset("Dz", '=', -iovec[5]);
412 >                                ++eclock;
413 >                            }
414 >                            bsdf = funvalue(funame, 6, iovec);
415 > #if (NSSAMP > 0)
416 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
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] = input_orient *
426 >                                                sqrt(1. - ssvec[0]*ssvec[0] -
427 >                                                        ssvec[1]*ssvec[1]);
428 >                                    SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres,
429 >                                                (oy+ssa[3])/sqres);
430 >                                    ssvec[5] = output_orient *
431 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
432 >                                                        ssvec[4]*ssvec[4]);
433 >                                    if (assignD) {
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;
442 >                            }
443 > #endif
444 >                        }
445                          if (pctcull >= 0)
446                                  fwrite(&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 >                                        fwrite(&uv[0], sizeof(*uv), 1, uvfp[0]);
453 >                                        fwrite(&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          if (pctcull >= 0) {                     /* finish output */
467                  if (pclose(ofp)) {
468 <                        fprintf(stderr, "%s: error running '%s'\n",
469 <                                        progname, cmd);
468 >                        fprintf(stderr, "%s: error running rttree_reduce on Y\n",
469 >                                        progname);
470                          exit(1);
471                  }
472 <        } else
473 <                fputs("}\n", stdout);
472 >                if (rbf_colorimetry == RBCtristimulus &&
473 >                                (pclose(uvfp[0]) || pclose(uvfp[1]))) {
474 >                        fprintf(stderr, "%s: error running rttree_reduce on uv\n",
475 >                                        progname);
476 >                        exit(1);
477 >                }
478 >        } else {
479 >                fputs("}\n", ofp);
480 >                if (fclose(ofp)) {
481 >                        fprintf(stderr, "%s: error writing Y file\n",
482 >                                        progname);
483 >                        exit(1);
484 >                }
485 >                if (rbf_colorimetry == RBCtristimulus) {
486 >                        fputs("}\n", uvfp[0]);
487 >                        fputs("}\n", uvfp[1]);
488 >                        if (fclose(uvfp[0]) || fclose(uvfp[1])) {
489 >                                fprintf(stderr, "%s: error writing uv file(s)\n",
490 >                                                progname);
491 >                                exit(1);
492 >                        }
493 >                }
494 >        }
495 >        prog_done();
496   }
497  
498 < /* Output XML prologue to stdout */
499 < static void
500 < xml_prologue(int ac, char *av[])
498 > #ifdef _WIN32
499 > /* Execute wrapBSDF command (may never return) */
500 > static int
501 > wrap_up(void)
502   {
503 <        static const char       *bsdf_type[4] = {
149 <                                        "Reflection Front",
150 <                                        "Transmission Front",
151 <                                        "Transmission Back",
152 <                                        "Reflection Back"
153 <                                };
503 >        char    cmd[8192];
504  
505 <        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
506 <        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\">");
507 <        fputs("<!-- File produced by:", stdout);
508 <        while (ac-- > 0) {
509 <                fputc(' ', stdout);
160 <                fputs(*av++, stdout);
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 <        puts(" -->");
512 <        puts("<WindowElementType>System</WindowElementType>");
513 <        puts("<FileType>BSDF</FileType>");
514 <        puts("<Optical>");
515 <        puts("<Layer>");
516 <        puts("\t<Material>");
517 <        puts("\t\t<Name>Name</Name>");
518 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
519 <        puts("\t\t<DeviceType>Other</DeviceType>");
520 <        puts("\t</Material>");
521 <        puts("\t<DataDefinition>");
522 <        printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n",
174 <                        single_plane_incident ? '3' : '4');
175 <        puts("\t</DataDefinition>");
176 <        puts("\t<WavelengthData>");
177 <        puts("\t\t<LayerNumber>System</LayerNumber>");
178 <        puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
179 <        puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
180 <        puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
181 <        puts("\t\t<WavelengthDataBlock>");
182 <        printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n",
183 <                bsdf_type[(input_orient>0)<<1 | (output_orient>0)]);
184 <        puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
185 <        puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
186 <        puts("\t\t\t<ScatteringData>");
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 <
525 < /* Output XML epilogue to stdout */
526 < static void
527 < xml_epilogue(void)
524 > #else
525 > /* Execute wrapBSDF command (may never return) */
526 > static int
527 > wrap_up(void)
528   {
529 <        puts("\t\t\t</ScatteringData>");
530 <        puts("\t\t</WavelengthDataBlock>");
531 <        puts("\t</WavelengthData>");
532 <        puts("</Layer>");
533 <        puts("</Optical>");
534 <        puts("</WindowElement>");
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 <        FILE    *fpin = stdin;
559 <        int     i;
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];
564 <        for (i = 1; i < argc-1 && argv[i][0] == '-'; i++)
565 <                switch (argv[i][1]) {           /* get option */
564 >        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
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++)
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 >                                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 'b':
584 >                        dobwd = (argv[i][0] == '+');
585 >                        break;
586                  case 't':
587 <                        pctcull = atof(argv[++i]);
587 >                        switch (argv[i][2]) {
588 >                        case '3':
589 >                                single_plane_incident = 1;
590 >                                break;
591 >                        case '4':
592 >                                single_plane_incident = 0;
593 >                                break;
594 >                        case '\0':
595 >                                pctcull = atof(argv[++i]);
596 >                                break;
597 >                        default:
598 >                                goto userr;
599 >                        }
600                          break;
601                  case 'g':
602                          samp_order = atoi(argv[++i]);
603                          break;
604 +                case 'l':
605 +                        lobe_lim = atoi(argv[++i]);
606 +                        break;
607 +                case 'p':
608 +                        do_prog = atoi(argv[i]+2);
609 +                        break;
610 +                case 'W':
611 +                        add_wbsdf(argv[i], 1);
612 +                        break;
613 +                case 'u':
614 +                case 'C':
615 +                        add_wbsdf(argv[i], 1);
616 +                        add_wbsdf(argv[++i], 1);
617 +                        break;
618                  default:
619                          goto userr;
620                  }
621 <
622 <        if (i == argc-1) {                      /* open input if given */
623 <                fpin = fopen(argv[i], "r");
624 <                if (fpin == NULL) {
625 <                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
626 <                                        progname, argv[1]);
627 <                        return(1);
621 >        strcpy(buf, "File produced by: ");
622 >        if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) {
623 >                add_wbsdf("-C", 1); add_wbsdf(buf, 0);
624 >        }
625 >        if (single_plane_incident >= 0) {       /* function-based BSDF? */
626 >                void    (*evf)(char *s) = single_plane_incident ?
627 >                                eval_isotropic : eval_anisotropic;
628 >                if (i != argc-1 || fundefined(argv[i]) < 3) {
629 >                        fprintf(stderr,
630 >        "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
631 >                                        progname);
632 >                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
633 >                        goto userr;
634                  }
635 <        } else if (i < argc-1)
636 <                goto userr;
637 <        SET_FILE_BINARY(fpin);                  /* load BSDF interpolant */
638 <        if (!load_bsdf_rep(fpin))
635 >                ++eclock;
636 >                add_wbsdf("-a", 1);
637 >                add_wbsdf(tfmt[single_plane_incident], 1);
638 >                if (dofwd) {
639 >                        input_orient = -1;
640 >                        output_orient = -1;
641 >                        prog_start("Evaluating outside reflectance");
642 >                        (*evf)(argv[i]);
643 >                        output_orient = 1;
644 >                        prog_start("Evaluating outside->inside transmission");
645 >                        (*evf)(argv[i]);
646 >                }
647 >                if (dobwd) {
648 >                        input_orient = 1;
649 >                        output_orient = 1;
650 >                        prog_start("Evaluating inside reflectance");
651 >                        (*evf)(argv[i]);
652 >                        output_orient = -1;
653 >                        prog_start("Evaluating inside->outside transmission");
654 >                        (*evf)(argv[i]);
655 >                }
656 >                return(wrap_up());
657 >        }
658 >        if (i < argc) {                         /* open input files if given */
659 >                int     nbsdf = 0;
660 >                for ( ; i < argc; i++) {        /* interpolate each component */
661 >                        char    pbuf[256];
662 >                        FILE    *fpin = fopen(argv[i], "rb");
663 >                        if (fpin == NULL) {
664 >                                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
665 >                                                progname, argv[i]);
666 >                                return(1);
667 >                        }
668 >                        if (!load_bsdf_rep(fpin))
669 >                                return(1);
670 >                        fclose(fpin);
671 >                        sprintf(pbuf, "Interpolating component '%s'", argv[i]);
672 >                        prog_start(pbuf);
673 >                        if (!nbsdf++) {
674 >                                add_wbsdf("-a", 1);
675 >                                add_wbsdf(tfmt[single_plane_incident], 1);
676 >                        }
677 >                        if (single_plane_incident)
678 >                                eval_isotropic(NULL);
679 >                        else
680 >                                eval_anisotropic(NULL);
681 >                }
682 >                return(wrap_up());
683 >        }
684 >        SET_FILE_BINARY(stdin);                 /* load from stdin */
685 >        if (!load_bsdf_rep(stdin))
686                  return(1);
687 <        fclose(fpin);
688 <        xml_prologue(argc, argv);               /* start XML output */
687 >        prog_start("Interpolating from standard input");
688 >        add_wbsdf("-a", 1);
689 >        add_wbsdf(tfmt[single_plane_incident], 1);
690          if (single_plane_incident)              /* resample dist. */
691 <                interp_isotropic();
691 >                eval_isotropic(NULL);
692          else
693 <                interp_anisotropic();
694 <        xml_epilogue();                         /* finish XML output */
695 <        return(0);
693 >                eval_anisotropic(NULL);
694 >
695 >        return(wrap_up());
696   userr:
697          fprintf(stderr,
698 <        "Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n",
698 >        "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n",
699 >                                progname);
700 >        fprintf(stderr,
701 >        "   or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
702                                  progname);
703          return(1);
704   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines