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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines