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.16 by greg, Wed May 15 17:29:30 2013 UTC vs.
Revision 2.40 by greg, Fri Feb 17 22:31:49 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines