ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/checkBSDF.c
Revision: 2.2
Committed: Wed Dec 15 02:13:27 2021 UTC (2 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +114 -30 lines
Log Message:
feat: finished implementation of tensor tree reciprocity calculation

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: checkBSDF.c,v 2.1 2021/12/14 02:33:18 greg Exp $";
3 #endif
4 /*
5 * checkBSDF.c
6 *
7 * Load BSDF XML file and check Helmholtz reciprocity
8 */
9
10 #define _USE_MATH_DEFINES
11 #include <math.h>
12 #include "rtio.h"
13 #include "random.h"
14 #include "bsdf.h"
15 #include "bsdf_m.h"
16 #include "bsdf_t.h"
17
18 #define F_IN_COLOR 0x1
19 #define F_ISOTROPIC 0x2
20 #define F_MATRIX 0x4
21 #define F_TTREE 0x8
22
23 typedef struct {
24 double vmin, vmax; /* extrema */
25 double vsum; /* straight sum */
26 long nvals; /* number of values */
27 } SimpleStats;
28
29 const SimpleStats SSinit = {FHUGE, -FHUGE, .0, 0};
30
31 /* relative difference formula */
32 #define rdiff(a,b) ((a)>(b) ? ((a)-(b))/(a) : ((b)-(a))/(b))
33
34 /* Figure out BSDF type (and optionally determine if in color) */
35 const char *
36 getBSDFtype(const SDData *bsdf, int *flags)
37 {
38 const SDSpectralDF *df = bsdf->tb;
39
40 if (flags) *flags = 0;
41 if (!df) df = bsdf->tf;
42 if (!df) df = bsdf->rf;
43 if (!df) df = bsdf->rb;
44 if (!df) return "Pure_Lambertian";
45 if (df->comp[0].func == &SDhandleMtx) {
46 const SDMat *m = (const SDMat *)df->comp[0].dist;
47 if (flags) {
48 *flags |= F_MATRIX;
49 *flags |= F_IN_COLOR*(m->chroma != NULL);
50 }
51 switch (m->ninc) {
52 case 145:
53 return "Klems_Full";
54 case 73:
55 return "Klems_Half";
56 case 41:
57 return "Klems_Quarter";
58 }
59 return "Unknown_Matrix";
60 }
61 if (df->comp[0].func == &SDhandleTre) {
62 const SDTre *t = (const SDTre *)df->comp[0].dist;
63 if (flags) {
64 *flags |= F_TTREE;
65 *flags |= F_IN_COLOR*(t->stc[1] != NULL);
66 }
67 switch (t->stc[0]->ndim) {
68 case 4:
69 return "Anisotropic_Tensor_Tree";
70 case 3:
71 if (flags) *flags |= F_ISOTROPIC;
72 return "Isotropic_Tensor_Tree";
73 }
74 return "Unknown_Tensor_Tree";
75 }
76 return "Unknown";
77 }
78
79 /* Report details related to one hemisphere distribution */
80 void
81 detailComponent(const char *nm, const SDValue *lamb, const SDSpectralDF *df)
82 {
83 printf("%s\t%4.1f %4.1f %4.1f\t\t", nm,
84 100.*lamb->cieY*lamb->spec.cx/lamb->spec.cy,
85 100.*lamb->cieY,
86 100.*lamb->cieY*(1.f - lamb->spec.cx - lamb->spec.cy)/lamb->spec.cy);
87 if (df)
88 printf("%5.1f%%\t\t%.2f deg\n", 100.*df->maxHemi,
89 sqrt(df->minProjSA/M_PI)*(360./M_PI));
90 else
91 puts("0%\t\t180");
92 }
93
94 /* Add a value to stats */
95 void
96 addStat(SimpleStats *ssp, double v)
97 {
98 if (v < ssp->vmin) ssp->vmin = v;
99 if (v > ssp->vmax) ssp->vmax = v;
100 ssp->vsum += v;
101 ssp->nvals++;
102 }
103
104 /* Sample a BSDF hemisphere with callback (quadtree recursion) */
105 int
106 qtSampBSDF(double xleft, double ytop, double siz,
107 const SDData *bsdf, const int side, const RREAL *v0,
108 int (*cf)(const SDData *b, const FVECT v1, const RREAL *v0, void *p),
109 void *cdata)
110 {
111 if (siz < 0.124) { /* make sure we subdivide, first */
112 FVECT vsmp;
113 double sa;
114 square2disk(vsmp, xleft + frandom()*siz, ytop + frandom()*siz);
115 vsmp[2] = 1. - vsmp[0]*vsmp[0] - vsmp[1]*vsmp[1];
116 if (vsmp[2] <= 0) return 0;
117 vsmp[2] = side * sqrt(vsmp[2]);
118 if (SDreportError( SDsizeBSDF(&sa, vsmp, v0, SDqueryMin, bsdf), stderr))
119 return 0;
120 if (sa >= M_PI*siz*siz - FTINY) /* no further division needed */
121 return (*cf)(bsdf, vsmp, v0, cdata);
122 }
123 siz *= .5; /* 4-branch recursion */
124 return( qtSampBSDF(xleft, ytop, siz, bsdf, side, v0, cf, cdata) &&
125 qtSampBSDF(xleft+siz, ytop, siz, bsdf, side, v0, cf, cdata) &&
126 qtSampBSDF(xleft, ytop+siz, siz, bsdf, side, v0, cf, cdata) &&
127 qtSampBSDF(xleft+siz, ytop+siz, siz, bsdf, side, v0, cf, cdata) );
128 }
129
130 #define sampBSDFhemi(b,s,v0,cf,cd) qtSampBSDF(0,0,1,b,s,v0,cf,cd)
131
132 /* Call-back to compute reciprocity difference */
133 int
134 diffRecip(const SDData *bsdf, const FVECT v1, const RREAL *v0, void *p)
135 {
136 SDValue sdv;
137 double otherY;
138
139 if (SDreportError( SDevalBSDF(&sdv, v0, v1, bsdf), stderr))
140 return 0;
141 otherY = sdv.cieY;
142 if (SDreportError( SDevalBSDF(&sdv, v1, v0, bsdf), stderr))
143 return 0;
144
145 addStat((SimpleStats *)p, rdiff(sdv.cieY, otherY));
146 return 1;
147 }
148
149 /* Call-back to compute reciprocity over reflected hemisphere */
150 int
151 reflHemi(const SDData *bsdf, const FVECT v1, const RREAL *v0, void *p)
152 {
153 return sampBSDFhemi(bsdf, 1 - 2*(v1[2]<0), v1, &diffRecip, p);
154 }
155
156 /* Call-back to compute reciprocity over transmitted hemisphere */
157 int
158 transHemi(const SDData *bsdf, const FVECT v1, const RREAL *v0, void *p)
159 {
160 return sampBSDFhemi(bsdf, 1 - 2*(v1[2]>0), v1, &diffRecip, p);
161 }
162
163 /* Report reciprocity errors for the given directions */
164 void
165 checkReciprocity(const char *nm, const int side1, const int side2,
166 const SDData *bsdf, const int fl)
167 {
168 SimpleStats myStats = SSinit;
169 const SDSpectralDF *df = bsdf->tf;
170
171 if (side1 == side2) {
172 df = (side1 > 0) ? bsdf->rf : bsdf->rb;
173 if (!df) goto nothing2do;
174 } else if (!bsdf->tf | !bsdf->tb)
175 goto nothing2do;
176
177 if (fl & F_MATRIX) { /* special case for matrix BSDF */
178 const SDMat *m = (const SDMat *)df->comp[0].dist;
179 int i = m->ninc;
180 FVECT vin, vout;
181 double fwdY;
182 SDValue rev;
183 while (i--) {
184 int o = m->nout;
185 if (!mBSDF_incvec(vin, m, i+.5))
186 continue;
187 while (o--) {
188 if (!mBSDF_outvec(vout, m, o+.5))
189 continue;
190 fwdY = mBSDF_value(m, o, i);
191 if (fwdY <= 1e-4)
192 continue;
193 if (SDreportError( SDevalBSDF(&rev, vout, vin, bsdf), stderr))
194 return;
195 if (rev.cieY > 1e-4)
196 addStat(&myStats, rdiff(fwdY, rev.cieY));
197 }
198 }
199 } if (fl & F_ISOTROPIC) { /* isotropic case */
200 const double stepSize = sqrt(df->minProjSA/M_PI);
201 FVECT vin;
202 vin[1] = 0;
203 for (vin[0] = 0.5*stepSize; vin[0] < 1; vin[0] += stepSize) {
204 vin[2] = side1*sqrt(1. - vin[0]*vin[0]);
205 if (!sampBSDFhemi(bsdf, side2, vin, &diffRecip, &myStats))
206 return;
207 }
208 } else if (!sampBSDFhemi(bsdf, side1, NULL,
209 (side1==side2) ? &reflHemi : &transHemi, &myStats))
210 return;
211 if (myStats.nvals) {
212 printf("%s\t%5.1f\t%5.1f\t%5.1f\n", nm,
213 100.*myStats.vmin,
214 100.*myStats.vsum/(double)myStats.nvals,
215 100.*myStats.vmax);
216 return;
217 }
218 nothing2do:
219 printf("%s\t0\t0\t0\n", nm);
220 }
221
222 /* Report on the given BSDF XML file */
223 int
224 checkXML(char *fname)
225 {
226 int flags;
227 SDData myBSDF;
228 char *pth;
229
230 puts("=====================================================");
231 printf("File: '%s'\n", fname);
232 SDclearBSDF(&myBSDF, fname);
233 pth = getpath(fname, getrlibpath(), R_OK);
234 if (!pth) {
235 fprintf(stderr, "Cannot find file '%s'\n", fname);
236 return 0;
237 }
238 if (SDreportError( SDloadFile(&myBSDF, pth), stderr))
239 return 0;
240 printf("Manufacturer: '%s'\n", myBSDF.makr);
241 printf("BSDF Name: '%s'\n", myBSDF.matn);
242 printf("Dimensions (W x H x Thickness): %g x %g x %g cm\n", 100.*myBSDF.dim[0],
243 100.*myBSDF.dim[1], 100.*myBSDF.dim[2]);
244 printf("Type: %s\n", getBSDFtype(&myBSDF, &flags));
245 printf("Color: %d\n", (flags & F_IN_COLOR) != 0);
246 printf("Has Geometry: %d\n", (myBSDF.mgf != NULL));
247 puts("Component\tLambertian XYZ %\tMax. Dir\tMin. Angle");
248 detailComponent("Internal Refl", &myBSDF.rLambFront, myBSDF.rf);
249 detailComponent("External Refl", &myBSDF.rLambBack, myBSDF.rb);
250 detailComponent("Int->Ext Trans", &myBSDF.tLambFront, myBSDF.tf);
251 detailComponent("Ext->Int Trans", &myBSDF.tLambBack, myBSDF.tb);
252 puts("Component\tReciprocity Error (min avg max %)");
253 checkReciprocity("Front Refl", 1, 1, &myBSDF, flags);
254 checkReciprocity("Back Refl", -1, -1, &myBSDF, flags);
255 checkReciprocity("Transmission", -1, 1, &myBSDF, flags);
256 SDfreeBSDF(&myBSDF);
257 return 1;
258 }
259
260 int
261 main(int argc, char *argv[])
262 {
263 int i;
264
265 if (argc < 2) {
266 fprintf(stderr, "Usage: %s bsdf.xml ..\n", argv[0]);
267 return 1;
268 }
269 for (i = 1; i < argc; i++)
270 if (!checkXML(argv[i]))
271 return 1;
272 return 0;
273 }