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.26 by greg, Wed Mar 12 21:15:31 2014 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 "rtprocess.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 < const double            ssamp_thresh = 0.35;
28 > static double           ssamp_thresh = 0.35;
29                                  /* number of super-samples */
30 < const int               nssamp = 100;
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  
# Line 39 | Line 79 | static int             do_prog = 79;
79   static void
80   prog_show(double frac)
81   {
82 <        char    pbar[256];
83 <        int     nchars;
82 >        static unsigned call_cnt = 0;
83 >        static char     lastc[] = "-\\|/";
84 >        char            pbar[256];
85 >        int             nchars;
86  
87 <        if (do_prog <= 0) return;
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 = 1;
92 <        nchars = do_prog*frac + .5;
91 >        else if (frac >= 1) frac = .9999;
92 >        nchars = do_prog*frac;
93          pbar[0] = '\r';
94          memset(pbar+1, '*', nchars);
95 <        memset(pbar+1+nchars, '-', do_prog-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 */
59 #define prog_done()     if (do_prog) fputc('\n',stderr); else
60
61 /* Output XML prologue to stdout */
102   static void
103 < xml_prologue(int ac, char *av[])
103 > prog_done(void)
104   {
105 <        puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
66 <        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\">");
67 <        fputs("<!-- File produced by:", stdout);
68 <        while (ac-- > 0) {
69 <                fputc(' ', stdout);
70 <                fputs(*av++, stdout);
71 <        }
72 <        puts(" -->");
73 <        puts("<WindowElementType>System</WindowElementType>");
74 <        puts("<FileType>BSDF</FileType>");
75 <        puts("<Optical>");
76 <        puts("<Layer>");
77 <        puts("\t<Material>");
78 <        printf("\t\t<Name>%s</Name>\n", bsdf_name[0] ? bsdf_name : "Unknown");
79 <        printf("\t\t<Manufacturer>%s</Manufacturer>\n",
80 <                        bsdf_manuf[0] ? bsdf_manuf : "Unknown");
81 <        puts("\t\t<DeviceType>Other</DeviceType>");
82 <        puts("\t</Material>");
83 <        puts("\t<DataDefinition>");
84 <        printf("\t\t<IncidentDataStructure>TensorTree%c</IncidentDataStructure>\n",
85 <                        single_plane_incident ? '3' : '4');
86 <        puts("\t</DataDefinition>");
87 < }
105 >        int     n = do_prog;
106  
107 < /* Output XML data prologue to stdout */
108 < static void
109 < data_prologue()
110 < {
111 <        static const char       *bsdf_type[4] = {
94 <                                        "Reflection Front",
95 <                                        "Transmission Front",
96 <                                        "Transmission Back",
97 <                                        "Reflection Back"
98 <                                };
99 <
100 <        puts("\t<WavelengthData>");
101 <        puts("\t\t<LayerNumber>System</LayerNumber>");
102 <        puts("\t\t<Wavelength unit=\"Integral\">Visible</Wavelength>");
103 <        puts("\t\t<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>");
104 <        puts("\t\t<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>");
105 <        puts("\t\t<WavelengthDataBlock>");
106 <        printf("\t\t\t<WavelengthDataDirection>%s</WavelengthDataDirection>\n",
107 <                        bsdf_type[(input_orient>0)<<1 | (output_orient>0)]);
108 <        puts("\t\t\t<AngleBasis>LBNL/Shirley-Chiu</AngleBasis>");
109 <        puts("\t\t\t<ScatteringDataType>BTDF</ScatteringDataType>");
110 <        puts("\t\t\t<ScatteringData>");
107 >        if (n <= 1) return;
108 >        fputc('\r', stderr);
109 >        while (n--)
110 >                fputc(' ', stderr);
111 >        fputc('\r', stderr);
112   }
113  
113 /* Output XML data epilogue to stdout */
114 static void
115 data_epilogue(void)
116 {
117        puts("\t\t\t</ScatteringData>");
118        puts("\t\t</WavelengthDataBlock>");
119        puts("\t</WavelengthData>");
120 }
121
122 /* Output XML epilogue to stdout */
123 static void
124 xml_epilogue(void)
125 {
126        puts("</Layer>");
127        puts("</Optical>");
128        puts("</WindowElement>");
129 }
130
114   /* Compute absolute relative difference */
115   static double
116   abs_diff(double v1, double v0)
# Line 145 | Line 128 | static void
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          double          iovec[6];
139 <        float           bsdf;
139 >        float           bsdf, uv[2];
140  
155        data_prologue();                        /* begin output */
141          if (pctcull >= 0) {
142 <                sprintf(cmd, "rttree_reduce -a -h -ff -r 3 -t %f -g %d",
143 <                                pctcull, samp_order);
159 <                fflush(stdout);
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 167 | Line 151 | eval_isotropic(char *funame)
151   #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
152                  flockfile(ofp);
153   #endif
154 <        } else
155 <                fputs("{\n", stdout);
156 <                                                /* need to assign Dx, Dy, Dz? */
157 <        if (funame != NULL)
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 = NULL;
201 <                iovec[0] = 2.*(ix+.5)/sqres - 1.;
202 <                iovec[1] = .0;
203 <                iovec[2] = input_orient * sqrt(1. - iovec[0]*iovec[0]);
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 <                    float       last_bsdf = -1;
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(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
234 <                        iovec[5] = output_orient *
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 <                        if (funame == NULL)
240 <                            bsdf = eval_rbfrep(rbf, iovec+3) *
241 <                                                output_orient/iovec[5];
242 <                        else {
243 <                            double      ssa[3], ssvec[6], sum;
244 <                            int         ssi;
245 <                            if (assignD) {
246 <                                varset("Dx", '=', -iovec[3]);
247 <                                varset("Dy", '=', -iovec[4]);
248 <                                varset("Dz", '=', -iovec[5]);
249 <                                ++eclock;
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 <                            bsdf = funvalue(funame, 6, iovec);
282 <                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
283 <                                sum = 0;        /* super-sample voxel */
284 <                                for (ssi = nssamp; ssi--; ) {
285 <                                    SDmultiSamp(ssa, 3, (ssi+frandom())/nssamp);
286 <                                    ssvec[0] = 2.*(ix+ssa[0])/sqres - 1.;
287 <                                    ssvec[1] = .0;
288 <                                    ssvec[2] = input_orient *
289 <                                                sqrt(1. - ssvec[0]*ssvec[0]);
290 <                                    SDsquare2disk(ssvec+3, (ox+ssa[1])/sqres,
291 <                                                (oy+ssa[2])/sqres);
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", '=', -iovec[3]);
318 <                                        varset("Dy", '=', -iovec[4]);
319 <                                        varset("Dz", '=', -iovec[5]);
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/nssamp;
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);
332 <                        last_bsdf = 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./sqres));
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          }
248        data_epilogue();
249        prog_done();
392   }
393  
394   /* Interpolate and output anisotropic BSDF data */
# Line 254 | Line 396 | static void
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          double          iovec[6];
407 <        float           bsdf;
407 >        float           bsdf, uv[2];
408  
264        data_prologue();                        /* begin output */
409          if (pctcull >= 0) {
410 <                sprintf(cmd, "rttree_reduce%s -h -ff -r 4 -t %f -g %d",
411 <                                (input_orient>0 ^ output_orient>0) ? "" : " -a",
412 <                                pctcull, samp_order);
413 <                fflush(stdout);
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",
# Line 277 | Line 422 | eval_anisotropic(char *funame)
422   #ifdef getc_unlocked                            /* avoid lock/unlock overhead */
423                  flockfile(ofp);
424   #endif
425 <        } else
426 <                fputs("{\n", stdout);
427 <                                                /* need to assign Dx, Dy, Dz? */
428 <        if (funame != NULL)
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 = NULL;            /* Klems reversal */
475 <                SDsquare2disk(iovec, 1.-(ix+.5)/sqres, 1.-(iy+.5)/sqres);
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 <                    float       last_bsdf = -1;
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(iovec+3, (ox+.5)/sqres, (oy+.5)/sqres);
506 <                        iovec[5] = output_orient *
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 <                        if (funame == NULL)
512 <                            bsdf = eval_rbfrep(rbf, iovec+3) *
513 <                                                output_orient/iovec[5];
514 <                        else {
515 <                            double      ssa[4], ssvec[6], sum;
516 <                            int         ssi;
517 <                            if (assignD) {
518 <                                varset("Dx", '=', -iovec[3]);
519 <                                varset("Dy", '=', -iovec[4]);
520 <                                varset("Dz", '=', -iovec[5]);
521 <                                ++eclock;
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 <                            bsdf = funvalue(funame, 6, iovec);
554 <                            if (abs_diff(bsdf, last_bsdf) > ssamp_thresh) {
555 <                                sum = 0;        /* super-sample voxel */
556 <                                for (ssi = nssamp; ssi--; ) {
557 <                                    SDmultiSamp(ssa, 4, (ssi+frandom())/nssamp);
558 <                                    SDsquare2disk(ssvec, 1.-(ix+ssa[0])/sqres,
559 <                                                1.-(iy+ssa[1])/sqres);
560 <                                    ssvec[2] = output_orient *
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])/sqres,
580 <                                                (oy+ssa[3])/sqres);
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", '=', -iovec[3]);
586 <                                        varset("Dy", '=', -iovec[4]);
587 <                                        varset("Dz", '=', -iovec[5]);
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/nssamp;
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);
600 <                        last_bsdf = 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                      }
344                }
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);
633 <        data_epilogue();
634 <        prog_done();
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 + #if defined(_WIN32) || defined(_WIN64)
657 + /* Execute wrapBSDF command (may never return) */
658 + static int
659 + wrap_up(void)
660 + {
661 +        char    cmd[8192];
662 +
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 +        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 + #else
683 + /* Execute wrapBSDF command (may never return) */
684 + static int
685 + wrap_up(void)
686 + {
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 +        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];
# Line 370 | Line 723 | main(int argc, char *argv[])
723          esupport &= ~(E_INCHAN|E_OUTCHAN);
724          scompile("PI:3.14159265358979323846", NULL, 0);
725          biggerlib();
726 <        for (i = 1; i < argc-1 && (argv[i][0] == '-') | (argv[i][0] == '+'); i++)
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 <                                fcompile(argv[++i]);
736 <                        else
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;
# Line 408 | Line 781 | main(int argc, char *argv[])
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 +        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;
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",
# Line 422 | Line 807 | main(int argc, char *argv[])
807                          goto userr;
808                  }
809                  ++eclock;
810 <                xml_prologue(argc, argv);       /* start XML output */
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;
# Line 441 | Line 828 | main(int argc, char *argv[])
828                          prog_start("Evaluating inside->outside transmission");
829                          (*evf)(argv[i]);
830                  }
831 <                xml_epilogue();                 /* finish XML output & exit */
445 <                return(0);
831 >                return(wrap_up());
832          }
833          if (i < argc) {                         /* open input files if given */
834                  int     nbsdf = 0;
# Line 457 | Line 843 | main(int argc, char *argv[])
843                          if (!load_bsdf_rep(fpin))
844                                  return(1);
845                          fclose(fpin);
460                        if (!nbsdf++)           /* start XML on first dist. */
461                                xml_prologue(argc, argv);
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 <                xml_epilogue();                 /* finish XML output & exit */
470 <                return(0);
857 >                return(wrap_up());
858          }
859          SET_FILE_BINARY(stdin);                 /* load from stdin */
860          if (!load_bsdf_rep(stdin))
861                  return(1);
475        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                  eval_isotropic(NULL);
867          else
868                  eval_anisotropic(NULL);
869 <        xml_epilogue();                         /* finish XML output & exit */
870 <        return(0);
869 >
870 >        return(wrap_up());
871   userr:
872          fprintf(stderr,
873 <        "Usage: %s [-g Nlog2][-t pctcull][-l maxlobes] [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} [-g Nlog2][-t pctcull][{+|-}for[ward]][{+|-}b[ackward]][-e expr][-f file] bsdf_func > bsdf.xml\n",
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