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.49 by greg, Fri May 8 22:34:21 2020 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines