ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfquery.c
Revision: 2.10
Committed: Tue Jun 11 16:09:40 2019 UTC (4 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.9: +8 -2 lines
Log Message:
Added -u option for unbuffered output

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.10 static const char RCSid[] = "$Id: bsdfquery.c,v 2.9 2017/04/13 00:14:36 greg Exp $";
3 greg 2.1 #endif
4     /*
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 greg 2.9 * A zero length in or out vector is ignored, causing output to be flushed.
9 greg 2.1 * 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
14     #include <stdio.h>
15     #include <string.h>
16     #include <stdlib.h>
17     #include "rtmath.h"
18 greg 2.7 #include "rtio.h"
19 greg 2.1 #include "bsdfrep.h"
20    
21     char *progname;
22    
23     /* Read in a vector pair */
24     static int
25     readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
26     {
27     double dvec[6];
28     float fvec[6];
29 greg 2.9 tryagain:
30 greg 2.1 switch (fmt) {
31     case 'a':
32 greg 2.5 if (fscanf(fp, FVFORMAT, &idir[0], &idir[1], &idir[2]) != 3 ||
33     fscanf(fp, FVFORMAT, &odir[0], &odir[1], &odir[2]) != 3)
34 greg 2.1 return(0);
35     break;
36     case 'd':
37 greg 2.7 if (getbinary(dvec, sizeof(double), 6, fp) != 6)
38 greg 2.1 return(0);
39     VCOPY(idir, dvec);
40     VCOPY(odir, dvec+3);
41     break;
42     case 'f':
43 greg 2.7 if (getbinary(fvec, sizeof(float), 6, fp) != 6)
44 greg 2.1 return(0);
45     VCOPY(idir, fvec);
46     VCOPY(odir, fvec+3);
47     break;
48     }
49     if ((normalize(idir) == 0) | (normalize(odir) == 0)) {
50 greg 2.9 fflush(stdout); /* desired side-effect? */
51     goto tryagain;
52 greg 2.1 }
53     return(1);
54     }
55    
56     /* Get BSDF values for the input and output directions given on stdin */
57     int
58     main(int argc, char *argv[])
59     {
60 greg 2.10 int unbuffered = 0;
61 greg 2.6 int repXYZ = 0;
62 greg 2.1 int inpXML = -1;
63     int inpfmt = 'a';
64     int outfmt = 'a';
65     RBFNODE *rbf = NULL;
66     int32 prevInpDir = 0;
67     SDData myBSDF;
68     FVECT idir, odir;
69     int n;
70     /* check arguments */
71     progname = argv[0];
72 greg 2.6 while (argc > 2 && argv[1][0] == '-') {
73     switch (argv[1][1]) {
74 greg 2.10 case 'u': /* unbuffered output */\
75     unbuffered = 1;
76     break;
77 greg 2.6 case 'c': /* color output */
78     repXYZ = 1;
79     break;
80     case 'f': /* i/o format */
81     if (!argv[1][2] || strchr("afd", argv[1][2]) == NULL)
82     goto userr;
83     inpfmt = outfmt = argv[1][2];
84     if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
85     outfmt = argv[1][3];
86     break;
87     default:
88     goto userr;
89     }
90 greg 2.1 ++argv; --argc;
91     }
92 greg 2.2 if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
93 greg 2.1 if (!strcasecmp(argv[1]+n, ".xml"))
94     inpXML = 1;
95     else if (!strcasecmp(argv[1]+n, ".sir"))
96     inpXML = 0;
97     }
98 greg 2.6 if ((argc != 2) | (inpXML < 0))
99     goto userr;
100 greg 2.1 /* load BSDF representation */
101     if (inpXML) {
102     SDclearBSDF(&myBSDF, argv[1]);
103     if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
104     return(1);
105     } else {
106     FILE *fp = fopen(argv[1], "rb");
107     if (fp == NULL) {
108     fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
109     progname, argv[1]);
110     return(1);
111     }
112     if (!load_bsdf_rep(fp))
113     return(1);
114     fclose(fp);
115     }
116     /* query BSDF values */
117     while (readIOdir(idir, odir, stdin, inpfmt)) {
118 greg 2.6 SDValue sval;
119 greg 2.1 if (inpXML) {
120     if (SDreportError(SDevalBSDF(&sval, odir,
121     idir, &myBSDF), stderr))
122     return(1);
123     } else {
124     int32 inpDir = encodedir(idir);
125     if (inpDir != prevInpDir) {
126     if (idir[2] > 0 ^ input_orient > 0) {
127     fprintf(stderr, "%s: input hemisphere error\n",
128     progname);
129     return(1);
130     }
131     if (rbf != NULL) free(rbf);
132     rbf = advect_rbf(idir, 15000);
133     prevInpDir = inpDir;
134     }
135     if (odir[2] > 0 ^ output_orient > 0) {
136     fprintf(stderr, "%s: output hemisphere error\n",
137     progname);
138     return(1);
139     }
140 greg 2.6 if (SDreportError(eval_rbfcol(&sval, rbf, odir), stderr))
141     return(1);
142 greg 2.1 }
143 greg 2.6
144 greg 2.1 switch (outfmt) { /* write to stdout */
145     case 'a':
146 greg 2.6 if (repXYZ) {
147     double cieX = sval.spec.cx/sval.spec.cy*sval.cieY;
148     double cieZ = (1. - sval.spec.cx - sval.spec.cy) /
149     sval.spec.cy * sval.cieY;
150     printf("%.6e %.6e %.6e\n", cieX, sval.cieY, cieZ);
151     } else
152     printf("%.6e\n", sval.cieY);
153 greg 2.1 break;
154     case 'd':
155 greg 2.6 if (repXYZ) {
156     double cieXYZ[3];
157     cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
158     cieXYZ[1] = sval.cieY;
159     cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
160     sval.spec.cy * sval.cieY;
161 greg 2.7 putbinary(cieXYZ, sizeof(double), 3, stdout);
162 greg 2.6 } else
163 greg 2.7 putbinary(&sval.cieY, sizeof(double), 1, stdout);
164 greg 2.1 break;
165     case 'f':
166 greg 2.6 if (repXYZ) {
167     float cieXYZ[3];
168     cieXYZ[0] = sval.spec.cx/sval.spec.cy*sval.cieY;
169     cieXYZ[1] = sval.cieY;
170     cieXYZ[2] = (1. - sval.spec.cx - sval.spec.cy) /
171     sval.spec.cy * sval.cieY;
172 greg 2.7 putbinary(cieXYZ, sizeof(float), 3, stdout);
173 greg 2.6 } else {
174     float cieY = sval.cieY;
175 greg 2.7 putbinary(&cieY, sizeof(float), 1, stdout);
176 greg 2.6 }
177 greg 2.1 break;
178     }
179 greg 2.10 if (unbuffered)
180     fflush(stdout);
181 greg 2.1 }
182 greg 2.6 /* if (rbf != NULL) free(rbf); */
183 greg 2.1 return(0);
184 greg 2.6 userr:
185 greg 2.10 fprintf(stderr, "Usage: %s [-u][-c][-fio] bsdf.{sir|xml}\n", progname);
186 greg 2.6 return(1);
187 greg 2.1 }