ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfquery.c
Revision: 2.5
Committed: Wed Jan 20 19:43:34 2016 UTC (8 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +3 -5 lines
Log Message:
Fix for SMLFLT

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfquery.c,v 2.4 2014/08/21 10:33:48 greg Exp $";
3 #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 * It is wise to sort the input directions to keep identical ones together
9 * when using a scattering interpolant representation.
10 */
11
12 #define _USE_MATH_DEFINES
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "rtmath.h"
17 #include "bsdfrep.h"
18
19 char *progname;
20
21 /* Read in a vector pair */
22 static int
23 readIOdir(FVECT idir, FVECT odir, FILE *fp, int fmt)
24 {
25 double dvec[6];
26 float fvec[6];
27
28 switch (fmt) {
29 case 'a':
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 break;
34 case 'd':
35 if (fread(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)
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);
50 }
51 return(1);
52 }
53
54 /* Get BSDF values for the input and output directions given on stdin */
55 int
56 main(int argc, char *argv[])
57 {
58 int inpXML = -1;
59 int inpfmt = 'a';
60 int outfmt = 'a';
61 RBFNODE *rbf = NULL;
62 int32 prevInpDir = 0;
63 SDData myBSDF;
64 FVECT idir, odir;
65 int n;
66 /* check arguments */
67 progname = argv[0];
68 if (argc > 2 && argv[1][0] == '-' && argv[1][1] == 'f' &&
69 argv[1][2] && strchr("afd", argv[1][2]) != NULL) {
70 inpfmt = outfmt = argv[1][2];
71 if (argv[1][3] && strchr("afd", argv[1][3]) != NULL)
72 outfmt = argv[1][3];
73 ++argv; --argc;
74 }
75 if (argc > 1 && (n = strlen(argv[1])-4) > 0) {
76 if (!strcasecmp(argv[1]+n, ".xml"))
77 inpXML = 1;
78 else if (!strcasecmp(argv[1]+n, ".sir"))
79 inpXML = 0;
80 }
81 if ((argc != 2) | (inpXML < 0)) {
82 fprintf(stderr, "Usage: %s [-fio] bsdf.{sir|xml}\n", progname);
83 return(1);
84 }
85 /* load BSDF representation */
86 if (inpXML) {
87 SDclearBSDF(&myBSDF, argv[1]);
88 if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
89 return(1);
90 } else {
91 FILE *fp = fopen(argv[1], "rb");
92 if (fp == NULL) {
93 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
94 progname, argv[1]);
95 return(1);
96 }
97 if (!load_bsdf_rep(fp))
98 return(1);
99 fclose(fp);
100 }
101 /* query BSDF values */
102 while (readIOdir(idir, odir, stdin, inpfmt)) {
103 double bsdf;
104 float fval;
105 if (inpXML) {
106 SDValue sval;
107 if (SDreportError(SDevalBSDF(&sval, odir,
108 idir, &myBSDF), stderr))
109 return(1);
110 bsdf = sval.cieY;
111 } else {
112 int32 inpDir = encodedir(idir);
113 if (inpDir != prevInpDir) {
114 if (idir[2] > 0 ^ input_orient > 0) {
115 fprintf(stderr, "%s: input hemisphere error\n",
116 progname);
117 return(1);
118 }
119 if (rbf != NULL) free(rbf);
120 rbf = advect_rbf(idir, 15000);
121 prevInpDir = inpDir;
122 }
123 if (odir[2] > 0 ^ output_orient > 0) {
124 fprintf(stderr, "%s: output hemisphere error\n",
125 progname);
126 return(1);
127 }
128 bsdf = eval_rbfrep(rbf, odir);
129 }
130 switch (outfmt) { /* write to stdout */
131 case 'a':
132 printf("%.6e\n", bsdf);
133 break;
134 case 'd':
135 fwrite(&bsdf, sizeof(double), 1, stdout);
136 break;
137 case 'f':
138 fval = bsdf;
139 fwrite(&fval, sizeof(float), 1, stdout);
140 break;
141 }
142 }
143 return(0);
144 }