ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rado.c
Revision: 2.6
Committed: Thu Jun 5 19:10:31 2025 UTC (25 hours, 13 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.5: +2 -4 lines
Log Message:
refactor: Made progname declarations more consistent

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: bsdf2rado.c,v 2.5 2021/12/07 23:55:02 greg Exp $";
3 #endif
4 /*
5 * Plot 3-D BSDF output based on scattering interpolant or XML representation
6 */
7
8 #define _USE_MATH_DEFINES
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <math.h>
13 #include "rtprocess.h"
14 #include "bsdfrep.h"
15
16 const float colarr[6][3] = {
17 .7, 1., .7,
18 1., .7, .7,
19 .7, .7, 1.,
20 1., .5, 1.,
21 1., 1., .5,
22 .5, 1., 1.
23 };
24
25 #if defined(_WIN32) || defined(_WIN64)
26 char validf[] = "-e \"valid(s,t)=X`SYS(s,t)^2+Y`SYS(s,t)^2+Z`SYS(s,t)^2-1e-7\"";
27 #else
28 char validf[] = "-e 'valid(s,t)=X`SYS(s,t)^2+Y`SYS(s,t)^2+Z`SYS(s,t)^2-1e-7'";
29 #endif
30
31 /* Produce a Radiance model plotting the indicated incident direction(s) */
32 int
33 main(int argc, char *argv[])
34 {
35 int showPeaks = 0;
36 int doTrans = 0;
37 int inpXML = -1;
38 RBFNODE *rbf = NULL;
39 FILE *fp;
40 char buf[128];
41 SDData myBSDF;
42 double bsdf, min_log;
43 FVECT idir, odir;
44 int i, j, n;
45 /* check arguments */
46 fixargv0(argv[0]);
47 if (argc > 1 && !strcmp(argv[1], "-p")) {
48 ++showPeaks;
49 ++argv; --argc;
50 }
51 if (argc > 1 && !strcmp(argv[1], "-t")) {
52 ++doTrans;
53 ++argv; --argc;
54 }
55 if (argc >= 4 && (n = strlen(argv[1])-4) > 0) {
56 if (!strcasecmp(argv[1]+n, ".xml"))
57 inpXML = 1;
58 else if (!strcasecmp(argv[1]+n, ".sir"))
59 inpXML = 0;
60 }
61 if (inpXML < 0) {
62 fprintf(stderr, "Usage: %s [-p] bsdf.sir theta1 phi1 .. > output.rad\n", progname);
63 fprintf(stderr, " Or: %s [-t] bsdf.xml theta1 phi1 .. > output.rad\n", progname);
64 return(1);
65 }
66 /* load input */
67 if (inpXML) {
68 SDclearBSDF(&myBSDF, argv[1]);
69 if (SDreportError(SDloadFile(&myBSDF, argv[1]), stderr))
70 return(1);
71 bsdf_min = 1./M_PI;
72 if (myBSDF.rf != NULL && myBSDF.rLambFront.cieY < bsdf_min*M_PI)
73 bsdf_min = myBSDF.rLambFront.cieY/M_PI;
74 if (myBSDF.rb != NULL && myBSDF.rLambBack.cieY < bsdf_min*M_PI)
75 bsdf_min = myBSDF.rLambBack.cieY/M_PI;
76 if (myBSDF.tf != NULL && myBSDF.tLambFront.cieY < bsdf_min*M_PI)
77 bsdf_min = myBSDF.tLambFront.cieY/M_PI;
78 if (myBSDF.tb != NULL && myBSDF.tLambBack.cieY < bsdf_min*M_PI)
79 bsdf_min = myBSDF.tLambBack.cieY/M_PI;
80 if (doTrans && (myBSDF.tf == NULL) & (myBSDF.tb == NULL)) {
81 fprintf(stderr, "%s: no transmitted component in '%s'\n",
82 progname, argv[1]);
83 return(1);
84 }
85 } else {
86 fp = fopen(argv[1], "rb");
87 if (fp == NULL) {
88 fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n",
89 progname, argv[1]);
90 return(1);
91 }
92 if (!load_bsdf_rep(fp))
93 return(1);
94 fclose(fp);
95 }
96 #ifdef DEBUG
97 fprintf(stderr, "Minimum BSDF set to %.4g\n", bsdf_min);
98 #endif
99 min_log = log(bsdf_min*.5 + 1e-6);
100 /* output BSDF rep. */
101 for (n = 0; (n < 6) & (2*n+3 < argc); n++) {
102 double theta = (M_PI/180.)*atof(argv[2*n+2]);
103 double phi = (M_PI/180.)*atof(argv[2*n+3]);
104 if (theta < -FTINY) {
105 fprintf(stderr, "%s: theta values must be positive\n",
106 progname);
107 return(1);
108 }
109 if (inpXML) {
110 input_orient = (theta <= M_PI/2.) ? 1 : -1;
111 output_orient = doTrans ? -input_orient : input_orient;
112 }
113 idir[2] = sin(theta);
114 idir[0] = idir[2] * cos(phi);
115 idir[1] = idir[2] * sin(phi);
116 idir[2] = input_orient * sqrt(1. - idir[2]*idir[2]);
117 #ifdef DEBUG
118 fprintf(stderr, "Computing BSDF for incident direction (%.2f,%.2f)\n",
119 get_theta180(idir), get_phi360(idir));
120 #endif
121 if (!inpXML)
122 rbf = advect_rbf(idir, 15000);
123 #ifdef DEBUG
124 if (inpXML)
125 fprintf(stderr, "Hemispherical %s: %.4f\n",
126 (output_orient > 0 ^ input_orient > 0 ?
127 "transmission" : "reflection"),
128 SDdirectHemi(idir, SDsampSp|SDsampDf |
129 (output_orient > 0 ^ input_orient > 0 ?
130 SDsampT : SDsampR), &myBSDF));
131 else if (rbf == NULL)
132 fputs("Empty RBF\n", stderr);
133 else
134 fprintf(stderr, "Hemispherical %s: %.4f\n",
135 (output_orient > 0 ^ input_orient > 0 ?
136 "transmission" : "reflection"),
137 rbf->vtotal);
138 #endif
139 printf("# Incident direction (theta,phi) = (%.2f,%.2f) deg.\n\n",
140 (180./M_PI)*theta, (180./M_PI)*phi);
141 printf("void trans tmat\n0\n0\n7 %f %f %f .04 .04 .9 1\n",
142 colarr[n][0], colarr[n][1], colarr[n][2]);
143 if (showPeaks && rbf != NULL) {
144 printf("void plastic pmat\n0\n0\n5 %f %f %f .04 .08\n",
145 1.-colarr[n][0], 1.-colarr[n][1], 1.-colarr[n][2]);
146 for (i = 0; i < rbf->nrbf; i++) {
147 ovec_from_pos(odir, rbf->rbfa[i].gx, rbf->rbfa[i].gy);
148 bsdf = eval_rbfrep(rbf, odir);
149 bsdf = log(bsdf + 1e-5) - min_log;
150 printf("pmat sphere p%d\n0\n0\n4 %f %f %f %f\n",
151 i+1, odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf,
152 .007*bsdf);
153 }
154 }
155 fflush(stdout);
156 sprintf(buf, "gensurf tmat bsdf%d - - - %d %d %s", n+1,
157 grid_res-1, grid_res-1, validf);
158 fp = popen(buf, "w");
159 if (fp == NULL) {
160 fprintf(stderr, "%s: cannot open '| %s'\n", progname, buf);
161 return(1);
162 }
163 for (i = 0; i < grid_res; i++)
164 for (j = 0; j < grid_res; j++) {
165 ovec_from_pos(odir, i, j);
166 if (inpXML) {
167 SDValue sval;
168 if (SDreportError(SDevalBSDF(&sval, idir,
169 odir, &myBSDF), stderr))
170 return(1);
171 bsdf = sval.cieY;
172 } else
173 bsdf = eval_rbfrep(rbf, odir);
174 bsdf = log(bsdf + 1e-6) - min_log;
175 fprintf(fp, "%.8e %.8e %.8e\n",
176 odir[0]*bsdf, odir[1]*bsdf, odir[2]*bsdf);
177 }
178 if (rbf != NULL)
179 free(rbf);
180 if (pclose(fp))
181 return(1);
182 }
183 return(0);
184 }