1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdfquery.c,v 2.1 2013/11/21 23:57:28 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, "%lf %lf %lf %lf %lf %lf", |
31 |
dvec, dvec+1, dvec+2, dvec+3, dvec+4, dvec+5) != 6) |
32 |
return(0); |
33 |
VCOPY(idir, dvec); |
34 |
VCOPY(odir, dvec+3); |
35 |
break; |
36 |
case 'd': |
37 |
if (fread(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 (fread(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 |
fprintf(stderr, "%s: zero input vector!\n", progname); |
51 |
return(0); |
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 inpXML = -1; |
61 |
int inpfmt = 'a'; |
62 |
int outfmt = 'a'; |
63 |
RBFNODE *rbf = NULL; |
64 |
int32 prevInpDir = 0; |
65 |
SDData myBSDF; |
66 |
FVECT idir, odir; |
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]; |
75 |
++argv; --argc; |
76 |
} |
77 |
if (argc > 1 && (n = strlen(argv[1])-4) > 0) { |
78 |
if (!strcasecmp(argv[1]+n, ".xml")) |
79 |
inpXML = 1; |
80 |
else if (!strcasecmp(argv[1]+n, ".sir")) |
81 |
inpXML = 0; |
82 |
} |
83 |
if ((argc != 2) | (inpXML < 0)) { |
84 |
fprintf(stderr, "Usage: %s [-fio] bsdf.{sir|xml}\n", progname); |
85 |
return(1); |
86 |
} |
87 |
/* load BSDF representation */ |
88 |
if (inpXML) { |
89 |
SDclearBSDF(&myBSDF, argv[1]); |
90 |
if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr)) |
91 |
return(1); |
92 |
} else { |
93 |
FILE *fp = fopen(argv[1], "rb"); |
94 |
if (fp == NULL) { |
95 |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
96 |
progname, argv[1]); |
97 |
return(1); |
98 |
} |
99 |
if (!load_bsdf_rep(fp)) |
100 |
return(1); |
101 |
fclose(fp); |
102 |
} |
103 |
/* query BSDF values */ |
104 |
while (readIOdir(idir, odir, stdin, inpfmt)) { |
105 |
double bsdf; |
106 |
float fval; |
107 |
if (inpXML) { |
108 |
SDValue sval; |
109 |
if (SDreportError(SDevalBSDF(&sval, odir, |
110 |
idir, &myBSDF), stderr)) |
111 |
return(1); |
112 |
bsdf = sval.cieY; |
113 |
} else { |
114 |
int32 inpDir = encodedir(idir); |
115 |
if (inpDir != prevInpDir) { |
116 |
if (idir[2] > 0 ^ input_orient > 0) { |
117 |
fprintf(stderr, "%s: input hemisphere error\n", |
118 |
progname); |
119 |
return(1); |
120 |
} |
121 |
if (rbf != NULL) free(rbf); |
122 |
rbf = advect_rbf(idir, 15000); |
123 |
prevInpDir = inpDir; |
124 |
} |
125 |
if (odir[2] > 0 ^ output_orient > 0) { |
126 |
fprintf(stderr, "%s: output hemisphere error\n", |
127 |
progname); |
128 |
return(1); |
129 |
} |
130 |
bsdf = eval_rbfrep(rbf, odir)/(output_orient*odir[2]); |
131 |
} |
132 |
switch (outfmt) { /* write to stdout */ |
133 |
case 'a': |
134 |
printf("%.6e\n", bsdf); |
135 |
break; |
136 |
case 'd': |
137 |
fwrite(&bsdf, sizeof(double), 1, stdout); |
138 |
break; |
139 |
case 'f': |
140 |
fval = bsdf; |
141 |
fwrite(&fval, sizeof(float), 1, stdout); |
142 |
break; |
143 |
} |
144 |
} |
145 |
return(0); |
146 |
} |