ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/bsdf2rad.c
Revision: 2.4
Committed: Thu Nov 21 20:09:26 2013 UTC (10 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +90 -30 lines
Log Message:
Added ability to create models from XML files

File Contents

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