ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdfpeaks.c
Revision: 2.6
Committed: Sat Jun 7 05:09:45 2025 UTC (8 hours, 17 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.5: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdfpeaks.c,v 2.5 2025/06/03 21:31:51 greg Exp $";
3 #endif
4 /*
5 * Compute minimum FWHM peak for each incident direction in SIR input.
6 * Report FWHM of corresponding peaks in XML representations if provided.
7 */
8
9 #define _USE_MATH_DEFINES
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <math.h>
13 #include "bsdfrep.h"
14
15 typedef struct {
16 float peakv; /* peak BSDF value */
17 float width; /* smallest FWHM (deg) */
18 const RBFNODE *rbs; /* incident system */
19 int ndx; /* peak index for RBFVAL */
20 } FWHM; /* struct to hold peak value */
21
22 typedef double eval_f(const FVECT vin, const FVECT vout, const void *p);
23
24 /* Comparison function to put larger peaks first */
25 int
26 cmpFWHM(const void *p0, const void *p1)
27 {
28 float diff = (*(const FWHM *)p0).peakv -
29 (*(const FWHM *)p1).peakv;
30
31 if (diff > 0) return(-1);
32 if (diff < 0) return(1);
33 return(0);
34 }
35
36 /* BSDF evaluation function for RBF system */
37 double
38 rbf_eval(const FVECT vin, const FVECT vout, const void *p)
39 {
40 /* XXX verify vin == p->invec ? */
41 return(eval_rbfrep((const RBFNODE *)p, vout));
42 }
43
44 /* BSDF evaluation for XML input */
45 double
46 bsdf_eval(const FVECT vin, const FVECT vout, const void *p)
47 {
48 SDValue sv;
49
50 if (SDreportError(
51 SDevalBSDF(&sv, vin, vout, (const SDData *)p),
52 stderr))
53 exit(1);
54
55 return(sv.cieY);
56 }
57
58 /* Find full-width, half-maximum in radians around BSDF direction */
59 double
60 getFWHM(const FVECT vin, const FVECT vc, double rad0, eval_f *ev, const void *p)
61 {
62 const double peakv = (*ev)(vin, vc, p);
63 double rad1 = rad0; /* current radii */
64
65 while (rad0 < M_PI/2.) { /* look for FWHM */
66 FVECT v0, vt;
67 double phi;
68 v0[0] = 1; v0[1] = v0[2] = 0;
69 geodesic(v0, vc, v0, rad0, GEOD_RAD); /* use vc as pivot */
70 for (phi = 0; phi < 2.*M_PI; phi += M_PI/18.) {
71 spinvector(vt, v0, vc, phi);
72 if ((*ev)(vin, vt, p) <= .5*peakv) { /* found one side? */
73 FVECT vt1;
74 while (rad1 < M_PI/2.) { /* bracket peak */
75 geodesic(vt1, vt, vc, rad0+rad1, GEOD_RAD);
76 if ((*ev)(vin, vt1, p) <= .5*peakv)
77 return(rad0+rad1); /* got both! */
78 rad1 *= 1.05; /* else bump rad1 */
79 }
80 }
81 }
82 rad1 = rad0 *= 1.05; /* or expand search */
83 }
84 return(M_PI); /* failure return */
85 }
86
87 /* Get outgoing direction for the given FWHM record */
88 void
89 getOutDir(FVECT vo, FWHM *dp)
90 {
91 const RBFVAL *vp = dp->rbs->rbfa + dp->ndx;
92
93 ovec_from_pos(vo, vp->gx, vp->gy);
94 }
95
96 /* Assign FWHM record for specified RBF system */
97 void
98 assignFWHM(FWHM *dp, const RBFNODE *rbf)
99 {
100 FVECT vo;
101 int j;
102 double rad;
103
104 dp->rbs = rbf;
105 dp->ndx = 0; /* find peak outgoing */
106 for (j = rbf->nrbf; --j; )
107 if (rbf->rbfa[j].peak > rbf->rbfa[dp->ndx].peak)
108 dp->ndx = j;
109 /* record peak */
110 getOutDir(vo, dp);
111 dp->peakv = eval_rbfrep(rbf, vo);
112 /* get FWHM angle in degrees */
113 dp->width = 180./M_PI * getFWHM(rbf->invec, vo,
114 R2ANG(rbf->rbfa[dp->ndx].crad),
115 rbf_eval, rbf);
116 }
117
118 /* Evaluate FWHM for each incident direction recorded in SIR */
119 int
120 main(int argc, char *argv[])
121 {
122 const RBFNODE *rbf;
123 SDData *sdp;
124 FILE *fp;
125 int ndirs;
126 FWHM *peaka;
127 int i;
128 /* set global progname */
129 fixargv0(argv[0]);
130 if (argc < 2)
131 goto userr;
132
133 fp = fopen(argv[1], "rb"); /* load SIR input */
134 if (fp == NULL) {
135 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
136 progname, argv[1]);
137 return(1);
138 }
139 if (!load_bsdf_rep(fp))
140 return(1);
141 fclose(fp);
142 for (i = 2; i < argc; i++) /* check/load any XMLs */
143 if (SDcacheFile(argv[i]) == NULL)
144 return(1);
145 ndirs = 0; /* count input directions */
146 for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
147 if (rbf->nrbf <= 0) {
148 ndirs = 0;
149 break;
150 }
151 ++ndirs;
152 }
153 if (!ndirs) {
154 fprintf(stderr, "%s: missing/bad RBFs in '%s'\n", progname, argv[1]);
155 return(1);
156 }
157 /* print output header */
158 printf("%d incident directions in '%s': %s -> %s\n", ndirs, argv[1],
159 input_orient>0 ? "Front" : "Back",
160 output_orient>0 ? "Front" : "Back");
161 fputs("Incident (theta, phi)\tExiting (theta, phi)\tPeak\tFWHM", stdout);
162 for (i = 2; i < argc; i++)
163 printf("\t'%s'", argv[i]);
164 fputc('\n', stdout);
165 /* find SIR peaks */
166 peaka = (FWHM *)malloc(sizeof(FWHM)*ndirs);
167 if (peaka == NULL) return(1);
168 for (i = 0, rbf = dsf_list; i < ndirs; i++, rbf = rbf->next)
169 assignFWHM(&peaka[i], rbf);
170 /* sort strong to weak */
171 qsort(peaka, ndirs, sizeof(FWHM), cmpFWHM);
172
173 for (i = 0; i < ndirs; i++) { /* report FWHM for each incidence */
174 FVECT vout;
175 int j;
176 getOutDir(vout, &peaka[i]);
177 printf("%.0f %.0f\t%.0f %.0f", get_theta180(peaka[i].rbs->invec),
178 get_phi360(peaka[i].rbs->invec),
179 get_theta180(vout), get_phi360(vout));
180 /* peak and FWHM from SIR */
181 printf("\t%.2e\t%.1f", peaka[i].peakv, peaka[i].width);
182 /* FWHM for each XML */
183 for (j = 2; j < argc; j++) {
184 const SDData *sd = SDcacheFile(argv[j]);
185 double psa;
186 if (SDreportError(
187 SDsizeBSDF(&psa, peaka[i].rbs->invec,
188 NULL, SDqueryMin, sd),
189 stderr))
190 return(1);
191
192 printf("\t%.1f", 180./M_PI * getFWHM(peaka[i].rbs->invec,
193 vout, sqrt(psa/M_PI),
194 bsdf_eval, sd));
195 SDfreeCache(sd);
196 }
197 fputc('\n', stdout);
198 }
199 /* we're exiting, anyway...
200 SDfreeCache(NULL);
201 clear_bsdf_rep();
202 */
203 return(0);
204 userr:
205 fprintf(stderr, "Usage: %s bsdf.sir [bsdfrep1.xml ..]\n", progname);
206 return(1);
207 }