ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dctimestep.c
Revision: 2.29
Committed: Mon Jan 20 22:18:29 2014 UTC (8 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +4 -9 lines
Log Message:
Fixed improper handling of diffuse transmission matrix

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.29 static const char RCSid[] = "$Id: dctimestep.c,v 2.28 2014/01/20 21:29:04 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Compute time-step result using Daylight Coefficient method.
6     *
7     * G. Ward
8     */
9    
10     #include <ctype.h>
11     #include "standard.h"
12 greg 2.28 #include "cmatrix.h"
13 greg 2.1 #include "platform.h"
14     #include "resolu.h"
15    
16     char *progname; /* global argv[0] */
17    
18 greg 2.24 /* Sum together a set of images and write result to fout */
19 greg 2.1 static int
20 greg 2.13 sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
21 greg 2.1 {
22     int myDT = DTfromHeader;
23 greg 2.24 COLOR *scanline = NULL;
24     CMATRIX *pmat = NULL;
25     int myXR=0, myYR=0;
26 greg 2.1 int i, y;
27    
28     if (cv->ncols != 1)
29     error(INTERNAL, "expected vector in sum_images()");
30     for (i = 0; i < cv->nrows; i++) {
31     const COLORV *scv = cv_lval(cv,i);
32     char fname[1024];
33     FILE *fp;
34     int dt, xr, yr;
35     COLORV *psp;
36 greg 2.14 /* check for zero */
37 greg 2.19 if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
38     (myDT != DTfromHeader) | (i < cv->nrows-1))
39 greg 2.14 continue;
40 greg 2.1 /* open next picture */
41     sprintf(fname, fspec, i);
42     if ((fp = fopen(fname, "r")) == NULL) {
43     sprintf(errmsg, "cannot open picture '%s'", fname);
44     error(SYSTEM, errmsg);
45     }
46     SET_FILE_BINARY(fp);
47     dt = getDTfromHeader(fp);
48     if ((dt != DTrgbe) & (dt != DTxyze) ||
49     !fscnresolu(&xr, &yr, fp)) {
50     sprintf(errmsg, "file '%s' not a picture", fname);
51     error(USER, errmsg);
52     }
53     if (myDT == DTfromHeader) { /* on first one */
54     myDT = dt;
55     myXR = xr; myYR = yr;
56     scanline = (COLOR *)malloc(sizeof(COLOR)*myXR);
57     if (scanline == NULL)
58     error(SYSTEM, "out of memory in sum_images()");
59     pmat = cm_alloc(myYR, myXR);
60     memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
61 greg 2.2 /* finish header */
62 greg 2.13 fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fout);
63     fputc('\n', fout);
64     fprtresolu(myXR, myYR, fout);
65     fflush(fout);
66 greg 2.1 } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
67     sprintf(errmsg, "picture '%s' format/size mismatch",
68     fname);
69     error(USER, errmsg);
70     }
71     psp = pmat->cmem;
72     for (y = 0; y < yr; y++) { /* read it in */
73     int x;
74     if (freadscan(scanline, xr, fp) < 0) {
75     sprintf(errmsg, "error reading picture '%s'",
76     fname);
77     error(SYSTEM, errmsg);
78     }
79     /* sum in scanline */
80     for (x = 0; x < xr; x++, psp += 3) {
81     multcolor(scanline[x], scv);
82     addcolor(psp, scanline[x]);
83     }
84     }
85     fclose(fp); /* done this picture */
86     }
87     free(scanline);
88     /* write scanlines */
89     for (y = 0; y < myYR; y++)
90 greg 2.13 if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fout) < 0)
91 greg 2.1 return(0);
92     cm_free(pmat); /* all done */
93 greg 2.13 return(fflush(fout) == 0);
94 greg 2.1 }
95    
96 greg 2.11 /* check to see if a string contains a %d or %o specification */
97     static int
98     hasNumberFormat(const char *s)
99 greg 2.1 {
100 greg 2.24 if (s == NULL)
101     return(0);
102    
103 greg 2.18 while (*s) {
104     while (*s != '%')
105     if (!*s++)
106     return(0);
107     if (*++s == '%') { /* ignore "%%" */
108     ++s;
109     continue;
110     }
111     while (isdigit(*s)) /* field length */
112     ++s;
113     /* field we'll use? */
114     if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
115     (*s == 'x') | (*s == 'X'))
116     return(1);
117     }
118     return(0); /* didn't find one */
119 greg 2.1 }
120    
121     int
122     main(int argc, char *argv[])
123     {
124 greg 2.25 int skyfmt = DTascii;
125 greg 2.24 int nsteps = 1;
126     char *ofspec = NULL;
127     FILE *ofp = stdout;
128     CMATRIX *cmtx; /* component vector/matrix result */
129     char fnbuf[256];
130     int a, i;
131 greg 2.1
132     progname = argv[0];
133 greg 2.24 /* get options */
134 greg 2.25 for (a = 1; a < argc && argv[a][0] == '-'; a++)
135     switch (argv[a][1]) {
136 greg 2.24 case 'n':
137     nsteps = atoi(argv[++a]);
138     if (nsteps <= 0)
139     goto userr;
140     break;
141     case 'o':
142     ofspec = argv[++a];
143     break;
144 greg 2.25 case 'i':
145     switch (argv[a][2]) {
146     case 'f':
147     skyfmt = DTfloat;
148     break;
149     case 'd':
150     skyfmt = DTdouble;
151     break;
152     case 'a':
153     skyfmt = DTascii;
154     break;
155     default:
156     goto userr;
157     }
158     break;
159 greg 2.24 default:
160     goto userr;
161     }
162     if ((argc-a < 1) | (argc-a > 4))
163     goto userr;
164 greg 2.1
165 greg 2.24 if (argc-a > 2) { /* VTDs expression */
166     CMATRIX *smtx, *Dmat, *Tmat, *imtx;
167     /* get sky vector/matrix */
168 greg 2.25 smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
169 greg 2.12 /* load BSDF */
170 greg 2.29 Tmat = cm_loadBTDF(argv[a+1]);
171 greg 2.5 /* load Daylight matrix */
172 greg 2.24 Dmat = cm_load(argv[a+2], Tmat==NULL ? 0 : Tmat->ncols,
173     smtx->nrows, DTfromHeader);
174 greg 2.1 /* multiply vector through */
175 greg 2.24 imtx = cm_multiply(Dmat, smtx);
176     cm_free(Dmat); cm_free(smtx);
177 greg 2.29 cmtx = cm_multiply(Tmat, imtx);
178     cm_free(Tmat);
179 greg 2.24 cm_free(imtx);
180     } else { /* sky vector/matrix only */
181 greg 2.25 cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
182 greg 2.24 }
183     /* prepare output stream */
184     if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
185     sprintf(fnbuf, ofspec, 1);
186     ofspec = fnbuf;
187     }
188     if (ofspec != NULL && !hasNumberFormat(ofspec)) {
189     if ((ofp = fopen(ofspec, "w")) == NULL) {
190     fprintf(stderr, "%s: cannot open '%s' for output\n",
191     progname, ofspec);
192     return(1);
193     }
194     ofspec = NULL; /* only need to open once */
195 greg 2.12 }
196 greg 2.24 if (hasNumberFormat(argv[a])) { /* generating image(s) */
197     if (ofspec == NULL) {
198     SET_FILE_BINARY(ofp);
199     newheader("RADIANCE", ofp);
200     printargs(argc, argv, ofp);
201     fputnow(ofp);
202     }
203     if (nsteps > 1) /* multiple output frames? */
204     for (i = 0; i < nsteps; i++) {
205     CMATRIX *cvec = cm_column(cmtx, i);
206     if (ofspec != NULL) {
207     sprintf(fnbuf, ofspec, i+1);
208     if ((ofp = fopen(fnbuf, "wb")) == NULL) {
209     fprintf(stderr,
210     "%s: cannot open '%s' for output\n",
211     progname, fnbuf);
212     return(1);
213     }
214     newheader("RADIANCE", ofp);
215     printargs(argc, argv, ofp);
216     fputnow(ofp);
217     }
218     fprintf(ofp, "FRAME=%d\n", i+1);
219     if (!sum_images(argv[a], cvec, ofp))
220     return(1);
221     if (ofspec != NULL) {
222     if (fclose(ofp) == EOF) {
223     fprintf(stderr,
224     "%s: error writing to '%s'\n",
225     progname, fnbuf);
226     return(1);
227     }
228     ofp = stdout;
229     }
230     cm_free(cvec);
231     }
232     else if (!sum_images(argv[a], cmtx, ofp))
233 greg 2.1 return(1);
234 greg 2.24 } else { /* generating vector/matrix */
235     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
236     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
237 greg 2.1 cm_free(Vmat);
238 greg 2.24 if (ofspec != NULL) /* multiple vector files? */
239     for (i = 0; i < nsteps; i++) {
240     CMATRIX *rvec = cm_column(rmtx, i);
241     sprintf(fnbuf, ofspec, i+1);
242     if ((ofp = fopen(fnbuf, "w")) == NULL) {
243     fprintf(stderr,
244     "%s: cannot open '%s' for output\n",
245     progname, fnbuf);
246     return(1);
247     }
248     cm_print(rvec, ofp);
249     if (fclose(ofp) == EOF) {
250     fprintf(stderr,
251     "%s: error writing to '%s'\n",
252     progname, fnbuf);
253     return(1);
254     }
255     ofp = stdout;
256     cm_free(rvec);
257     }
258     else
259     cm_print(rmtx, ofp);
260     cm_free(rmtx);
261     }
262     if (fflush(ofp) == EOF) { /* final clean-up */
263     fprintf(stderr, "%s: write error on output\n", progname);
264     return(1);
265 greg 2.1 }
266 greg 2.24 cm_free(cmtx);
267 greg 2.1 return(0);
268 greg 2.24 userr:
269 greg 2.25 fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d}] DCspec [skyf]\n",
270 greg 2.24 progname);
271 greg 2.25 fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d}] Vspec Tbsdf.xml Dmat.dat [skyf]\n",
272 greg 2.24 progname);
273     return(1);
274 greg 2.1 }