ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfquery.c
(Generate patch)

Comparing ray/src/cv/bsdfquery.c (file contents):
Revision 2.3 by greg, Fri Apr 11 20:27:23 2014 UTC vs.
Revision 2.11 by greg, Sat Dec 28 18:05:14 2019 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   *  Query values from the given BSDF (scattering interpolant or XML repres.)
6   *  Input query is incident and exiting vectors directed away from surface.
7   *  We normalize.  Output is a BSDF value for the vector pair.
8 + *  A zero length in or out vector is ignored, causing output to be flushed.
9   *  It is wise to sort the input directions to keep identical ones together
10   *  when using a scattering interpolant representation.
11   */
12  
13   #define _USE_MATH_DEFINES
13 #include <stdio.h>
14 #include <string.h>
14   #include <stdlib.h>
15   #include "rtmath.h"
16 + #include "rtio.h"
17   #include "bsdfrep.h"
18  
19   char    *progname;
# Line 24 | Line 24 | readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
24   {
25          double  dvec[6];
26          float   fvec[6];
27 <
27 > tryagain:
28          switch (fmt) {
29          case 'a':
30 <                if (fscanf(fp, FVFORMAT, dvec, dvec+1, dvec+2) != 3 ||
31 <                                fscanf(fp, FVFORMAT, dvec+3, dvec+4, dvec+5) != 3)
30 >                if (fscanf(fp, FVFORMAT, &idir[0], &idir[1], &idir[2]) != 3 ||
31 >                                fscanf(fp, FVFORMAT, &odir[0], &odir[1], &odir[2]) != 3)
32                          return(0);
33                VCOPY(idir, dvec);
34                VCOPY(odir, dvec+3);
33                  break;
34          case 'd':
35 <                if (fread(dvec, sizeof(double), 6, fp) != 6)
35 >                if (getbinary(dvec, sizeof(double), 6, fp) != 6)
36                          return(0);
37                  VCOPY(idir, dvec);
38                  VCOPY(odir, dvec+3);
39                  break;
40          case 'f':
41 <                if (fread(fvec, sizeof(float), 6, fp) != 6)
41 >                if (getbinary(fvec, sizeof(float), 6, fp) != 6)
42                          return(0);
43                  VCOPY(idir, fvec);
44                  VCOPY(odir, fvec+3);
45                  break;
46          }
47          if ((normalize(idir) == 0) | (normalize(odir) == 0)) {
48 <                fprintf(stderr, "%s: zero input vector!\n", progname);
49 <                return(0);
48 >                fflush(stdout);         /* desired side-effect? */
49 >                goto tryagain;
50          }
51          return(1);
52   }
# Line 57 | Line 55 | readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
55   int
56   main(int argc, char *argv[])
57   {
58 +        int     unbuffered = 0;
59 +        int     repXYZ = 0;
60          int     inpXML = -1;
61          int     inpfmt = 'a';
62          int     outfmt = 'a';
# Line 67 | Line 67 | main(int argc, char *argv[])
67          int     n;
68                                                  /* check arguments */
69          progname = argv[0];
70 <        if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'f' &&
71 <                        argv[1][2] && strchr("afd", argv[1][2]) != NULL) {
72 <                inpfmt = outfmt = argv[1][2];
73 <                if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
74 <                        outfmt = argv[1][3];
70 >        while (argc > 2 && argv[1][0] == '-') {
71 >                switch (argv[1][1]) {
72 >                case 'u':                       /* unbuffered output */\
73 >                        unbuffered = 1;
74 >                        break;
75 >                case 'c':                       /* color output */
76 >                        repXYZ = 1;
77 >                        break;
78 >                case 'f':                       /* i/o format */
79 >                        if (!argv[1][2] || strchr("afd", argv[1][2]) == NULL)
80 >                                goto userr;
81 >                        inpfmt = outfmt = argv[1][2];
82 >                        if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
83 >                                outfmt = argv[1][3];
84 >                        break;
85 >                default:
86 >                        goto userr;
87 >                }
88                  ++argv; --argc;
89          }
90          if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
# Line 80 | Line 93 | main(int argc, char *argv[])
93                  else if (!strcasecmp(argv[1]+n, ".sir"))
94                          inpXML = 0;
95          }
96 <        if ((argc != 2) | (inpXML < 0)) {
97 <                fprintf(stderr, "Usage: %s [-fio] bsdf.{sir|xml}\n", progname);
85 <                return(1);
86 <        }
96 >        if ((argc != 2) | (inpXML < 0))
97 >                goto userr;
98                                                  /* load BSDF representation */
99          if (inpXML) {
100                  SDclearBSDF(&myBSDF, argv[1]);
# Line 102 | Line 113 | main(int argc, char *argv[])
113          }
114                                                  /* query BSDF values */
115          while (readIOdir(idir, odir, stdin, inpfmt)) {
116 <                double  bsdf;
106 <                float   fval;
116 >                SDValue sval;
117                  if (inpXML) {
108                        SDValue sval;
118                          if (SDreportError(SDevalBSDF(&sval, odir,
119                                                  idir, &myBSDF), stderr))
120                                  return(1);
112                        bsdf = sval.cieY;
121                  } else {
122                          int32   inpDir = encodedir(idir);
123                          if (inpDir != prevInpDir) {
# Line 127 | Line 135 | main(int argc, char *argv[])
135                                                  progname);
136                                  return(1);
137                          }
138 <                        bsdf = eval_rbfrep(rbf, odir)/(output_orient*odir[2]);
138 >                        if (SDreportError(eval_rbfcol(&sval, rbf, odir), stderr))
139 >                                return(1);
140                  }
141 +
142                  switch (outfmt) {               /* write to stdout */
143                  case 'a':
144 <                        printf("%.6e\n", bsdf);
144 >                        if (repXYZ) {
145 >                                double  cieX = sval.spec.cx/sval.spec.cy*sval.cieY;
146 >                                double  cieZ = (1. - sval.spec.cx - sval.spec.cy) /
147 >                                                sval.spec.cy * sval.cieY;
148 >                                printf("%.6e %.6e %.6e\n", cieX, sval.cieY, cieZ);
149 >                        } else
150 >                                printf("%.6e\n", sval.cieY);
151                          break;
152                  case 'd':
153 <                        fwrite(&bsdf, sizeof(double), 1, stdout);
153 >                        if (repXYZ) {
154 >                                double  cieXYZ[3];
155 >                                cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
156 >                                cieXYZ[1] = sval.cieY;
157 >                                cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
158 >                                                sval.spec.cy * sval.cieY;
159 >                                putbinary(cieXYZ, sizeof(double), 3, stdout);
160 >                        } else
161 >                                putbinary(&sval.cieY, sizeof(double), 1, stdout);
162                          break;
163                  case 'f':
164 <                        fval = bsdf;
165 <                        fwrite(&fval, sizeof(float), 1, stdout);
164 >                        if (repXYZ) {
165 >                                float   cieXYZ[3];
166 >                                cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
167 >                                cieXYZ[1] = sval.cieY;
168 >                                cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
169 >                                                sval.spec.cy * sval.cieY;
170 >                                putbinary(cieXYZ, sizeof(float), 3, stdout);
171 >                        } else {
172 >                                float   cieY = sval.cieY;
173 >                                putbinary(&cieY, sizeof(float), 1, stdout);
174 >                        }
175                          break;
176                  }
177 +                if (unbuffered)
178 +                        fflush(stdout);
179          }
180 +        /* if (rbf != NULL) free(rbf); */
181          return(0);
182 + userr:
183 +        fprintf(stderr, "Usage: %s [-u][-c][-fio] bsdf.{sir|xml}\n", progname);
184 +        return(1);
185   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines