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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines