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