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.7 by greg, Sat Nov 10 19:47:42 2012 UTC vs.
Revision 2.40 by greg, Fri Feb 17 22:31:49 2017 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          64
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 %f%% 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 >        if (funame != NULL)                     /* need to assign Dx, Dy, Dz? */
189 >                assignD = (fundefined(funame) < 6);
190                                                  /* run through directions */
191          for (ix = 0; ix < sqres/2; ix++) {
192 <                RBFNODE *rbf;
193 <                SDsquare2disk(ivec, (ix+.5)/sqres, .5);
194 <                ivec[2] = input_orient *
195 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
196 <                rbf = advect_rbf(ivec);
197 <                for (ox = 0; ox < sqres; ox++)
192 >                RBFNODE *rbf = NULL;
193 >                iovec[0] = 2.*(ix+.5)/sqres - 1.;
194 >                iovec[1] = .0;
195 >                iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
196 >                if (funame == NULL)
197 >                        rbf = advect_rbf(iovec, lobe_lim);
198 >                for (ox = 0; ox < sqres; ox++) {
199 >                    float       last_bsdf = -1;
200                      for (oy = 0; oy < sqres; oy++) {
201 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
202 <                        ovec[2] = output_orient *
203 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
204 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
201 >                        SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
202 >                        iovec[5] = output_orient *
203 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
204 >                        if (funame == NULL) {
205 >                            SDValue     sdv;
206 >                            eval_rbfcol(&sdv, rbf, iovec+3);
207 >                            bsdf = sdv.cieY;
208 >                            if (rbf_colorimetry == RBCtristimulus) {
209 >                                uv[0] = uv[1] = 1. /
210 >                                    (-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.);
211 >                                uv[0] *= 4.*sdv.spec.cx;
212 >                                uv[1] *= 9.*sdv.spec.cy;
213 >                            }
214 >                        } else {
215 >                            if (assignD) {
216 >                                varset("Dx", '=', -iovec[3]);
217 >                                varset("Dy", '=', -iovec[4]);
218 >                                varset("Dz", '=', -iovec[5]);
219 >                                ++eclock;
220 >                            }
221 >                            bsdf = funvalue(funame, 6, iovec);
222 > #if (NSSAMP > 0)
223 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
224 >                                int     ssi;
225 >                                double  ssa[3], ssvec[6], sum = 0;
226 >                                                /* super-sample voxel */
227 >                                for (ssi = NSSAMP; ssi--; ) {
228 >                                    SDmultiSamp(ssa, 3, (ssi+frandom()) *
229 >                                                        (1./NSSAMP));
230 >                                    ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.;
231 >                                    ssvec[1] = .0;
232 >                                    ssvec[2] = input_orient *
233 >                                                sqrt(1. - ssvec[0]*ssvec[0]);
234 >                                    SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres,
235 >                                                (oy+ssa[2])/sqres);
236 >                                    ssvec[5] = output_orient *
237 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
238 >                                                        ssvec[4]*ssvec[4]);
239 >                                    if (assignD) {
240 >                                        varset("Dx", '=', -ssvec[3]);
241 >                                        varset("Dy", '=', -ssvec[4]);
242 >                                        varset("Dz", '=', -ssvec[5]);
243 >                                        ++eclock;
244 >                                    }
245 >                                    sum += funvalue(funame, 6, ssvec);
246 >                                }
247 >                                bsdf = sum/NSSAMP;
248 >                            }
249 > #endif
250 >                        }
251                          if (pctcull >= 0)
252 <                                fwrite(&bsdf, sizeof(bsdf), 1, ofp);
252 >                                putbinary(&bsdf, sizeof(bsdf), 1, ofp);
253                          else
254 <                                printf("\t%.3e\n", bsdf);
254 >                                fprintf(ofp, "\t%.3e\n", bsdf);
255 >
256 >                        if (rbf_colorimetry == RBCtristimulus) {
257 >                                if (pctcull >= 0) {
258 >                                        putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]);
259 >                                        putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]);
260 >                                } else {
261 >                                        fprintf(uvfp[0], "\t%.3e\n", uv[0]);
262 >                                        fprintf(uvfp[1], "\t%.3e\n", uv[1]);
263 >                                }
264 >                        }
265 >                        last_bsdf = bsdf;
266                      }
267 +                }
268                  if (rbf != NULL)
269                          free(rbf);
270 +                prog_show((ix+1.)*(2./sqres));
271          }
272 +        prog_done();
273          if (pctcull >= 0) {                     /* finish output */
274                  if (pclose(ofp)) {
275 <                        fprintf(stderr, "%s: error running '%s'\n",
276 <                                        progname, cmd);
275 >                        fprintf(stderr, "%s: error running rttree_reduce on Y\n",
276 >                                        progname);
277                          exit(1);
278                  }
279 +                if (rbf_colorimetry == RBCtristimulus &&
280 +                                (pclose(uvfp[0]) || pclose(uvfp[1]))) {
281 +                        fprintf(stderr, "%s: error running rttree_reduce on uv\n",
282 +                                        progname);
283 +                        exit(1);
284 +                }
285          } else {
286                  for (ix = sqres*sqres*sqres/2; ix--; )
287 <                        fputs("\t0\n", stdout);
288 <                fputs("}\n", stdout);
287 >                        fputs("\t0\n", ofp);
288 >                fputs("}\n", ofp);
289 >                if (fclose(ofp)) {
290 >                        fprintf(stderr, "%s: error writing Y file\n",
291 >                                        progname);
292 >                        exit(1);
293 >                }
294 >                if (rbf_colorimetry == RBCtristimulus) {
295 >                        for (ix = sqres*sqres*sqres/2; ix--; ) {
296 >                                fputs("\t0\n", uvfp[0]);
297 >                                fputs("\t0\n", uvfp[1]);
298 >                        }
299 >                        fputs("}\n", uvfp[0]);
300 >                        fputs("}\n", uvfp[1]);
301 >                        if (fclose(uvfp[0]) || fclose(uvfp[1])) {
302 >                                fprintf(stderr, "%s: error writing uv file(s)\n",
303 >                                                progname);
304 >                                exit(1);
305 >                        }
306 >                }
307          }
308   }
309  
310   /* Interpolate and output anisotropic BSDF data */
311   static void
312 < interp_anisotropic()
312 > eval_anisotropic(char *funame)
313   {
314          const int       sqres = 1<<samp_order;
315 <        FILE            *ofp = NULL;
315 >        FILE            *ofp, *uvfp[2];
316 >        int             assignD = 0;
317          char            cmd[128];
318          int             ix, iy, ox, oy;
319 <        FVECT           ivec, ovec;
320 <        float           bsdf;
321 < #if DEBUG
322 <        fprintf(stderr, "Writing anisotropic order %d ", samp_order);
323 <        if (pctcull >= 0) fprintf(stderr, "data with %f%% culling\n", pctcull);
324 <        else fputs("raw data\n", stderr);
325 < #endif
326 <        if (pctcull >= 0) {                     /* begin output */
327 <                sprintf(cmd, "rttree_reduce -h -a -ff -r 4 -t %f -g %d",
102 <                                pctcull, samp_order);
103 <                fflush(stdout);
319 >        double          iovec[6];
320 >        float           bsdf, uv[2];
321 >
322 >        if (pctcull >= 0) {
323 >                const char      *avgopt = (input_orient>0 ^ output_orient>0)
324 >                                                ? "" : " -a";
325 >                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d > %s",
326 >                                avgopt, pctcull, samp_order,
327 >                                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 >                                        avgopt, uvcull, samp_order,
342 >                                        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 >                                        avgopt, uvcull, samp_order,
346 >                                        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 >        if (funame != NULL)                     /* need to assign Dx, Dy, Dz? */
379 >                assignD = (fundefined(funame) < 6);
380                                                  /* run through directions */
381          for (ix = 0; ix < sqres; ix++)
382              for (iy = 0; iy < sqres; iy++) {
383 <                RBFNODE *rbf;
384 <                SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres);
385 <                ivec[2] = input_orient *
386 <                                sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]);
387 <                rbf = advect_rbf(ivec);
388 <                for (ox = 0; ox < sqres; ox++)
383 >                RBFNODE *rbf = NULL;            /* Klems reversal */
384 >                SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres);
385 >                iovec[2] = input_orient *
386 >                                sqrt(1. - iovec[0]*iovec[0] - iovec[1]*iovec[1]);
387 >                if (funame == NULL)
388 >                        rbf = advect_rbf(iovec, lobe_lim);
389 >                for (ox = 0; ox < sqres; ox++) {
390 >                    float       last_bsdf = -1;
391                      for (oy = 0; oy < sqres; oy++) {
392 <                        SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres);
393 <                        ovec[2] = output_orient *
394 <                                sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]);
395 <                        bsdf = eval_rbfrep(rbf, ovec) * output_orient/ovec[2];
392 >                        SDsquare2disk(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
393 >                        iovec[5] = output_orient *
394 >                                sqrt(1. - iovec[3]*iovec[3] - iovec[4]*iovec[4]);
395 >                        if (funame == NULL) {
396 >                            SDValue     sdv;
397 >                            eval_rbfcol(&sdv, rbf, iovec+3);
398 >                            bsdf = sdv.cieY;
399 >                            if (rbf_colorimetry == RBCtristimulus) {
400 >                                uv[0] = uv[1] = 1. /
401 >                                    (-2.*sdv.spec.cx + 12.*sdv.spec.cy + 3.);
402 >                                uv[0] *= 4.*sdv.spec.cx;
403 >                                uv[1] *= 9.*sdv.spec.cy;
404 >                            }
405 >                        } else {
406 >                            if (assignD) {
407 >                                varset("Dx", '=', -iovec[3]);
408 >                                varset("Dy", '=', -iovec[4]);
409 >                                varset("Dz", '=', -iovec[5]);
410 >                                ++eclock;
411 >                            }
412 >                            bsdf = funvalue(funame, 6, iovec);
413 > #if (NSSAMP > 0)
414 >                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
415 >                                int     ssi;
416 >                                double  ssa[4], ssvec[6], sum = 0;
417 >                                                /* super-sample voxel */
418 >                                for (ssi = NSSAMP; ssi--; ) {
419 >                                    SDmultiSamp(ssa, 4, (ssi+frandom()) *
420 >                                                        (1./NSSAMP));
421 >                                    SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres,
422 >                                                1.-(iy+ssa[1])/sqres);
423 >                                    ssvec[2] = input_orient *
424 >                                                sqrt(1. - ssvec[0]*ssvec[0] -
425 >                                                        ssvec[1]*ssvec[1]);
426 >                                    SDsquare2disk(ssvec+3, (ox+ssa[2])/sqres,
427 >                                                (oy+ssa[3])/sqres);
428 >                                    ssvec[5] = output_orient *
429 >                                                sqrt(1. - ssvec[3]*ssvec[3] -
430 >                                                        ssvec[4]*ssvec[4]);
431 >                                    if (assignD) {
432 >                                        varset("Dx", '=', -ssvec[3]);
433 >                                        varset("Dy", '=', -ssvec[4]);
434 >                                        varset("Dz", '=', -ssvec[5]);
435 >                                        ++eclock;
436 >                                    }
437 >                                    sum += funvalue(funame, 6, ssvec);
438 >                                }
439 >                                bsdf = sum/NSSAMP;
440 >                            }
441 > #endif
442 >                        }
443                          if (pctcull >= 0)
444 <                                fwrite(&bsdf, sizeof(bsdf), 1, ofp);
444 >                                putbinary(&bsdf, sizeof(bsdf), 1, ofp);
445                          else
446 <                                printf("\t%.3e\n", bsdf);
446 >                                fprintf(ofp, "\t%.3e\n", bsdf);
447 >
448 >                        if (rbf_colorimetry == RBCtristimulus) {
449 >                                if (pctcull >= 0) {
450 >                                        putbinary(&uv[0], sizeof(*uv), 1, uvfp[0]);
451 >                                        putbinary(&uv[1], sizeof(*uv), 1, uvfp[1]);
452 >                                } else {
453 >                                        fprintf(uvfp[0], "\t%.3e\n", uv[0]);
454 >                                        fprintf(uvfp[1], "\t%.3e\n", uv[1]);
455 >                                }
456 >                        }
457 >                        last_bsdf = bsdf;
458                      }
459 +                }
460                  if (rbf != NULL)
461                          free(rbf);
462 +                prog_show((ix*sqres+iy+1.)/(sqres*sqres));
463              }
464 +        prog_done();
465          if (pctcull >= 0) {                     /* finish output */
466                  if (pclose(ofp)) {
467 <                        fprintf(stderr, "%s: error running '%s'\n",
468 <                                        progname, cmd);
467 >                        fprintf(stderr, "%s: error running rttree_reduce on Y\n",
468 >                                        progname);
469                          exit(1);
470                  }
471 <        } else
472 <                fputs("}\n", stdout);
471 >                if (rbf_colorimetry == RBCtristimulus &&
472 >                                (pclose(uvfp[0]) || pclose(uvfp[1]))) {
473 >                        fprintf(stderr, "%s: error running rttree_reduce on uv\n",
474 >                                        progname);
475 >                        exit(1);
476 >                }
477 >        } else {
478 >                fputs("}\n", ofp);
479 >                if (fclose(ofp)) {
480 >                        fprintf(stderr, "%s: error writing Y file\n",
481 >                                        progname);
482 >                        exit(1);
483 >                }
484 >                if (rbf_colorimetry == RBCtristimulus) {
485 >                        fputs("}\n", uvfp[0]);
486 >                        fputs("}\n", uvfp[1]);
487 >                        if (fclose(uvfp[0]) || fclose(uvfp[1])) {
488 >                                fprintf(stderr, "%s: error writing uv file(s)\n",
489 >                                                progname);
490 >                                exit(1);
491 >                        }
492 >                }
493 >        }
494   }
495  
496 < /* Output XML prologue to stdout */
497 < static void
498 < xml_prologue(int ac, char *av[])
496 > #if defined(_WIN32) || defined(_WIN64)
497 > /* Execute wrapBSDF command (may never return) */
498 > static int
499 > wrap_up(void)
500   {
501 <        static const char       *bsdf_type[4] = {
149 <                                        "Reflection Front",
150 <                                        "Transmission Front",
151 <                                        "Transmission Back",
152 <                                        "Reflection Back"
153 <                                };
501 >        char    cmd[8192];
502  
503 <        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
504 <        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\">");
505 <        fputs("<!-- File produced by:", stdout);
506 <        while (ac-- > 0) {
507 <                fputc(' ', stdout);
160 <                fputs(*av++, stdout);
503 >        if (bsdf_manuf[0]) {
504 >                add_wbsdf("-f", 1);
505 >                strcpy(cmd, "m=");
506 >                strcpy(cmd+2, bsdf_manuf);
507 >                add_wbsdf(cmd, 0);
508          }
509 <        puts(" -->");
510 <        puts("<WindowElementType>System</WindowElementType>");
511 <        puts("<FileType>BSDF</FileType>");
512 <        puts("<Optical>");
513 <        puts("<Layer>");
514 <        puts("\t<Material>");
515 <        puts("\t\t<Name>Name</Name>");
516 <        puts("\t\t<Manufacturer>Manufacturer</Manufacturer>");
517 <        puts("\t\t<DeviceType>Other</DeviceType>");
518 <        puts("\t</Material>");
519 <        puts("\t<DataDefinition>");
520 <        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>");
509 >        if (bsdf_name[0]) {
510 >                add_wbsdf("-f", 1);
511 >                strcpy(cmd, "n=");
512 >                strcpy(cmd+2, bsdf_name);
513 >                add_wbsdf(cmd, 0);
514 >        }
515 >        if (!convert_commandline(cmd, sizeof(cmd), wrapBSDF)) {
516 >                fputs(progname, stderr);
517 >                fputs(": command line too long in wrap_up()\n", stderr);
518 >                return(1);
519 >        }
520 >        return(system(cmd));
521   }
522 <
523 < /* Output XML epilogue to stdout */
524 < static void
525 < xml_epilogue(void)
522 > #else
523 > /* Execute wrapBSDF command (may never return) */
524 > static int
525 > wrap_up(void)
526   {
527 <        puts("\t\t\t</ScatteringData>");
528 <        puts("\t\t</WavelengthDataBlock>");
529 <        puts("\t</WavelengthData>");
530 <        puts("</Layer>");
531 <        puts("</Optical>");
532 <        puts("</WindowElement>");
527 >        char    buf[256];
528 >        char    *compath = getpath((char *)wrapBSDF[0], getenv("PATH"), X_OK);
529 >
530 >        if (compath == NULL) {
531 >                fprintf(stderr, "%s: cannot locate %s\n", progname, wrapBSDF[0]);
532 >                return(1);
533 >        }
534 >        if (bsdf_manuf[0]) {
535 >                add_wbsdf("-f", 1);
536 >                strcpy(buf, "m=");
537 >                strcpy(buf+2, bsdf_manuf);
538 >                add_wbsdf(buf, 0);
539 >        }
540 >        if (bsdf_name[0]) {
541 >                add_wbsdf("-f", 1);
542 >                strcpy(buf, "n=");
543 >                strcpy(buf+2, bsdf_name);
544 >                add_wbsdf(buf, 0);
545 >        }
546 >        execv(compath, wrapBSDF);       /* successful call never returns */
547 >        perror(compath);
548 >        return(1);
549   }
550 + #endif
551  
552   /* Read in BSDF and interpolate as tensor tree representation */
553   int
554   main(int argc, char *argv[])
555   {
556 <        FILE    *fpin = stdin;
557 <        int     i;
556 >        static char     tfmt[2][4] = {"t4", "t3"};
557 >        int     dofwd = 0, dobwd = 1;
558 >        char    buf[2048];
559 >        int     i, na;
560  
561          progname = argv[0];
562 <        for (i = 1; i < argc-1 && argv[i][0] == '-'; i++)
563 <                switch (argv[i][1]) {           /* get option */
562 >        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
563 >        esupport &= ~(E_INCHAN|E_OUTCHAN);
564 >        scompile("PI:3.14159265358979323846", NULL, 0);
565 >        biggerlib();
566 >        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
567 >                switch (argv[i][1]) {           /* get options */
568 >                case 'e':
569 >                        scompile(argv[++i], NULL, 0);
570 >                        break;
571 >                case 'f':
572 >                        if (!argv[i][2]) {
573 >                                if (strchr(argv[++i], '=') != NULL) {
574 >                                        add_wbsdf("-f", 1);
575 >                                        add_wbsdf(argv[i], 1);
576 >                                } else
577 >                                        fcompile(argv[i]);
578 >                        } else
579 >                                dofwd = (argv[i][0] == '+');
580 >                        break;
581 >                case 'b':
582 >                        dobwd = (argv[i][0] == '+');
583 >                        break;
584                  case 't':
585 <                        pctcull = atof(argv[++i]);
585 >                        switch (argv[i][2]) {
586 >                        case '3':
587 >                                single_plane_incident = 1;
588 >                                break;
589 >                        case '4':
590 >                                single_plane_incident = 0;
591 >                                break;
592 >                        case '\0':
593 >                                pctcull = atof(argv[++i]);
594 >                                break;
595 >                        default:
596 >                                goto userr;
597 >                        }
598                          break;
599                  case 'g':
600                          samp_order = atoi(argv[++i]);
601                          break;
602 +                case 'l':
603 +                        lobe_lim = atoi(argv[++i]);
604 +                        break;
605 +                case 'p':
606 +                        do_prog = atoi(argv[i]+2);
607 +                        break;
608 +                case 'W':
609 +                        add_wbsdf(argv[i], 1);
610 +                        break;
611 +                case 'u':
612 +                case 'C':
613 +                        add_wbsdf(argv[i], 1);
614 +                        add_wbsdf(argv[++i], 1);
615 +                        break;
616                  default:
617                          goto userr;
618                  }
619 <
620 <        if (i == argc-1) {                      /* open input if given */
621 <                fpin = fopen(argv[i], "r");
622 <                if (fpin == NULL) {
623 <                        fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
624 <                                        progname, argv[1]);
625 <                        return(1);
619 >        strcpy(buf, "File produced by: ");
620 >        if (convert_commandline(buf+18, sizeof(buf)-18, argv) != NULL) {
621 >                add_wbsdf("-C", 1); add_wbsdf(buf, 0);
622 >        }
623 >        if (single_plane_incident >= 0) {       /* function-based BSDF? */
624 >                void    (*evf)(char *s) = single_plane_incident ?
625 >                                eval_isotropic : eval_anisotropic;
626 >                if (i != argc-1 || fundefined(argv[i]) < 3) {
627 >                        fprintf(stderr,
628 >        "%s: need single function with 6 arguments: bsdf(ix,iy,iz,ox,oy,oz)\n",
629 >                                        progname);
630 >                        fprintf(stderr, "\tor 3 arguments using Dx,Dy,Dz: bsdf(ix,iy,iz)\n");
631 >                        goto userr;
632                  }
633 <        } else if (i < argc-1)
634 <                goto userr;
635 <        SET_FILE_BINARY(fpin);                  /* load BSDF interpolant */
636 <        if (!load_bsdf_rep(fpin))
633 >                ++eclock;
634 >                add_wbsdf("-a", 1);
635 >                add_wbsdf(tfmt[single_plane_incident], 1);
636 >                if (dofwd) {
637 >                        input_orient = -1;
638 >                        output_orient = -1;
639 >                        prog_start("Evaluating outside reflectance");
640 >                        (*evf)(argv[i]);
641 >                        output_orient = 1;
642 >                        prog_start("Evaluating outside->inside transmission");
643 >                        (*evf)(argv[i]);
644 >                }
645 >                if (dobwd) {
646 >                        input_orient = 1;
647 >                        output_orient = 1;
648 >                        prog_start("Evaluating inside reflectance");
649 >                        (*evf)(argv[i]);
650 >                        output_orient = -1;
651 >                        prog_start("Evaluating inside->outside transmission");
652 >                        (*evf)(argv[i]);
653 >                }
654 >                return(wrap_up());
655 >        }
656 >        if (i < argc) {                         /* open input files if given */
657 >                int     nbsdf = 0;
658 >                for ( ; i < argc; i++) {        /* interpolate each component */
659 >                        char    pbuf[256];
660 >                        FILE    *fpin = fopen(argv[i], "rb");
661 >                        if (fpin == NULL) {
662 >                                fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
663 >                                                progname, argv[i]);
664 >                                return(1);
665 >                        }
666 >                        if (!load_bsdf_rep(fpin))
667 >                                return(1);
668 >                        fclose(fpin);
669 >                        sprintf(pbuf, "Interpolating component '%s'", argv[i]);
670 >                        prog_start(pbuf);
671 >                        if (!nbsdf++) {
672 >                                add_wbsdf("-a", 1);
673 >                                add_wbsdf(tfmt[single_plane_incident], 1);
674 >                        }
675 >                        if (single_plane_incident)
676 >                                eval_isotropic(NULL);
677 >                        else
678 >                                eval_anisotropic(NULL);
679 >                }
680 >                return(wrap_up());
681 >        }
682 >        SET_FILE_BINARY(stdin);                 /* load from stdin */
683 >        if (!load_bsdf_rep(stdin))
684                  return(1);
685 <        fclose(fpin);
686 <        xml_prologue(argc, argv);               /* start XML output */
685 >        prog_start("Interpolating from standard input");
686 >        add_wbsdf("-a", 1);
687 >        add_wbsdf(tfmt[single_plane_incident], 1);
688          if (single_plane_incident)              /* resample dist. */
689 <                interp_isotropic();
689 >                eval_isotropic(NULL);
690          else
691 <                interp_anisotropic();
692 <        xml_epilogue();                         /* finish XML output */
693 <        return(0);
691 >                eval_anisotropic(NULL);
692 >
693 >        return(wrap_up());
694   userr:
695          fprintf(stderr,
696 <        "Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n",
696 >        "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [bsdf.sir ..] > bsdf.xml\n",
697 >                                progname);
698 >        fprintf(stderr,
699 >        "   or: %s -t{3|4} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
700                                  progname);
701          return(1);
702   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines