ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dcglare.c
Revision: 2.1
Committed: Mon Sep 9 17:19:51 2019 UTC (4 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Added Nathaniel Jones' dcglare utility

File Contents

# User Rev Content
1 greg 2.1 /*
2     * Compute time-step glare using imageless DGP calculation method.
3     *
4     * N. Jones
5     */
6    
7     /*
8     * Copyright (c) 2017-2019 Nathaniel Jones
9     *
10     * Permission is hereby granted, free of charge, to any person obtaining a copy
11     * of this software and associated documentation files (the "Software"), to deal
12     * in the Software without restriction, including without limitation the rights
13     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14     * copies of the Software, and to permit persons to whom the Software is
15     * furnished to do so, subject to the following conditions:
16     *
17     * The above copyright notice and this permission notice shall be included in all
18     * copies or substantial portions of the Software.
19     *
20     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26     * SOFTWARE.
27     */
28    
29     #include <ctype.h>
30     #include "platform.h"
31     #include "standard.h"
32     #include "cmatrix.h"
33     #include "platform.h"
34     #include "resolu.h"
35     #include "cmglare.h"
36    
37     char *progname; /* global argv[0] */
38    
39     /* Sum together a set of images and write result to fout */
40     static int
41     sum_images(const char *fspec, const CMATRIX *cv, FILE *fout)
42     {
43     int myDT = DTfromHeader;
44     COLR *scanline = NULL;
45     CMATRIX *pmat = NULL;
46     int myXR=0, myYR=0;
47     int i, y;
48    
49     if (cv->ncols != 1)
50     error(INTERNAL, "expected vector in sum_images()");
51     for (i = 0; i < cv->nrows; i++) {
52     const COLORV *scv = cv_lval(cv,i);
53     int flat_file = 0;
54     char fname[1024];
55     FILE *fp;
56     long data_start;
57     int dt, xr, yr;
58     COLORV *psp;
59     char *err;
60     /* check for zero */
61     if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) &&
62     (myDT != DTfromHeader) | (i < cv->nrows-1))
63     continue;
64     /* open next picture */
65     sprintf(fname, fspec, i);
66     if ((fp = fopen(fname, "rb")) == NULL) {
67     sprintf(errmsg, "cannot open picture '%s'", fname);
68     error(SYSTEM, errmsg);
69     }
70     dt = DTfromHeader;
71     if ((err = cm_getheader(&dt, NULL, NULL, NULL, fp)) != NULL)
72     error(USER, err);
73     if ((dt != DTrgbe) & (dt != DTxyze) ||
74     !fscnresolu(&xr, &yr, fp)) {
75     sprintf(errmsg, "file '%s' not a picture", fname);
76     error(USER, errmsg);
77     }
78     if (myDT == DTfromHeader) { /* on first one */
79     myDT = dt;
80     myXR = xr; myYR = yr;
81     scanline = (COLR *)malloc(sizeof(COLR)*myXR);
82     if (scanline == NULL)
83     error(SYSTEM, "out of memory in sum_images()");
84     pmat = cm_alloc(myYR, myXR);
85     memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR);
86     /* finish header */
87     fputformat((char *)cm_fmt_id[myDT], fout);
88     fputc('\n', fout);
89     fflush(fout);
90     } else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) {
91     sprintf(errmsg, "picture '%s' format/size mismatch",
92     fname);
93     error(USER, errmsg);
94     }
95     /* flat file check */
96     if ((data_start = ftell(fp)) > 0 && fseek(fp, 0L, SEEK_END) == 0) {
97     flat_file = (ftell(fp) == data_start + sizeof(COLR)*xr*yr);
98     if (fseek(fp, data_start, SEEK_SET) < 0) {
99     sprintf(errmsg, "cannot seek on picture '%s'", fname);
100     error(SYSTEM, errmsg);
101     }
102     }
103     psp = pmat->cmem;
104     for (y = 0; y < yr; y++) { /* read it in */
105     COLOR col;
106     int x;
107     if (flat_file ? getbinary(scanline, sizeof(COLR), xr, fp) != xr :
108     freadcolrs(scanline, xr, fp) < 0) {
109     sprintf(errmsg, "error reading picture '%s'",
110     fname);
111     error(SYSTEM, errmsg);
112     }
113     /* sum in scanline */
114     for (x = 0; x < xr; x++, psp += 3) {
115     if (!scanline[x][EXP])
116     continue; /* skip zeroes */
117     colr_color(col, scanline[x]);
118     multcolor(col, scv);
119     addcolor(psp, col);
120     }
121     }
122     fclose(fp); /* done this picture */
123     }
124     free(scanline);
125     i = cm_write(pmat, myDT, fout); /* write picture */
126     cm_free(pmat); /* free data */
127     return(i);
128     }
129    
130     /* check to see if a string contains a %d or %o specification */
131     static int
132     hasNumberFormat(const char *s)
133     {
134     if (s == NULL)
135     return(0);
136    
137     while (*s) {
138     while (*s != '%')
139     if (!*s++)
140     return(0);
141     if (*++s == '%') { /* ignore "%%" */
142     ++s;
143     continue;
144     }
145     while (isdigit(*s)) /* field length */
146     ++s;
147     /* field we'll use? */
148     if ((*s == 'd') | (*s == 'i') | (*s == 'o') |
149     (*s == 'x') | (*s == 'X'))
150     return(1);
151     }
152     return(0); /* didn't find one */
153     }
154    
155     int
156     main(int argc, char *argv[])
157     {
158     int skyfmt = DTfromHeader;
159     int outfmt = DTascii;
160     int headout = 1;
161     int nsteps = 0;
162     char *ofspec = NULL;
163     FILE *ofp = stdout;
164     CMATRIX *cmtx; /* component vector/matrix result */
165     char fnbuf[256];
166     int a, i;
167     #ifdef DC_GLARE
168     char *direct_path = NULL;
169     char *schedule_path = NULL;
170     int *occupancy = NULL;
171     int start_hour = 0;
172     int end_hour = 24;
173     double dgp_limit = -1;
174     double dgp_threshold = 2000;
175     char *view_path = NULL;
176     FILE *fp;
177     float *dgp_values = NULL;
178     FVECT vdir, vup;
179     FVECT *views = NULL;
180     int viewfmt = DTascii;
181    
182     vdir[0] = vdir[1] = vdir[2] = vup[0] = vup[1] = 0;
183     vup[2] = 1;
184    
185     clock_t timer = clock();
186     #endif /* DC_GLARE */
187    
188     progname = argv[0];
189     /* get options */
190     for (a = 1; a < argc && argv[a][0] == '-'; a++)
191     switch (argv[a][1]) {
192     case 'n':
193     nsteps = atoi(argv[++a]);
194     if (nsteps < 0)
195     goto userr;
196     skyfmt = nsteps ? DTascii : DTfromHeader;
197     break;
198     case 'h':
199     headout = !headout;
200     break;
201     case 'i':
202     switch (argv[a][2]) {
203     case 'f':
204     skyfmt = DTfloat;
205     break;
206     case 'd':
207     skyfmt = DTdouble;
208     break;
209     case 'a':
210     skyfmt = DTascii;
211     break;
212     default:
213     goto userr;
214     }
215     break;
216     case 'o':
217     switch (argv[a][2]) {
218     #ifndef DC_GLARE
219     case '\0': /* output specification (not format) */
220     ofspec = argv[++a];
221     break;
222     #endif /* DC_GLARE */
223     case 'f':
224     outfmt = DTfloat;
225     break;
226     case 'd':
227     outfmt = DTdouble;
228     break;
229     case 'a':
230     outfmt = DTascii;
231     break;
232     default:
233     goto userr;
234     }
235     break;
236     #ifdef DC_GLARE
237     case 's':
238     switch (argv[a][2]) {
239     case 'f': /* occupancy schedule file */
240     schedule_path = argv[++a];
241     break;
242     case 's': /* occupancy start hour */
243     start_hour = atoi(argv[++a]);
244     break;
245     case 'e': /* occupancy end hour */
246     end_hour = atoi(argv[++a]);
247     break;
248     default:
249     goto userr;
250     }
251     break;
252     case 'l': /* perceptible glare threshold */
253     dgp_limit = atof(argv[++a]);
254     break;
255     case 'b': /* luminance threshold */
256     dgp_threshold = atof(argv[++a]);
257     break;
258     case 'v':
259     switch (argv[a][2]) {
260     case 'd': /* forward */
261     //check(3, "fff");
262     vdir[0] = atof(argv[++a]);
263     vdir[1] = atof(argv[++a]);
264     vdir[2] = atof(argv[++a]);
265     if (normalize(vdir) == 0.0) goto userr;
266     break;
267     case 'u': /* up */
268     //check(3, "fff");
269     vup[0] = atof(argv[++a]);
270     vup[1] = atof(argv[++a]);
271     vup[2] = atof(argv[++a]);
272     if (normalize(vup) == 0.0) goto userr;
273     break;
274     case 'f': /* view directions file */
275     //check(2, "s");
276     view_path = argv[++a];
277     break;
278     case 'i':
279     switch (argv[a][3]) {
280     case 'f':
281     viewfmt = DTfloat;
282     break;
283     case 'd':
284     viewfmt = DTdouble;
285     break;
286     case 'a':
287     viewfmt = DTascii;
288     break;
289     default:
290     goto userr;
291     }
292     default:
293     goto userr;
294     }
295     break;
296     #endif /* DC_GLARE */
297     default:
298     goto userr;
299     }
300     #ifdef DC_GLARE
301     if ((argc-a < 2) | (argc-a > 5))
302     goto userr;
303     /* single bounce daylight coefficients file */
304     direct_path = argv[++a];
305     #else
306     if ((argc-a < 1) | (argc-a > 4))
307     goto userr;
308     #endif /* DC_GLARE */
309    
310     if (argc-a > 2) { /* VTDs expression */
311     CMATRIX *smtx, *Dmat, *Tmat, *imtx;
312     const char *ccp;
313     /* get sky vector/matrix */
314     smtx = cm_load(argv[a+3], 0, nsteps, skyfmt);
315     nsteps = smtx->ncols;
316     /* load BSDF */
317     if (argv[a+1][0] != '!' &&
318     (ccp = strrchr(argv[a+1], '.')) != NULL &&
319     !strcasecmp(ccp+1, "XML"))
320     Tmat = cm_loadBTDF(argv[a+1]);
321     else
322     Tmat = cm_load(argv[a+1], 0, 0, DTfromHeader);
323     /* load Daylight matrix */
324     Dmat = cm_load(argv[a+2], Tmat->ncols,
325     smtx->nrows, DTfromHeader);
326     /* multiply vector through */
327     imtx = cm_multiply(Dmat, smtx);
328     cm_free(Dmat); cm_free(smtx);
329     cmtx = cm_multiply(Tmat, imtx);
330     cm_free(Tmat);
331     cm_free(imtx);
332     } else { /* sky vector/matrix only */
333     //TIMER(timer, "read args");
334     cmtx = cm_load(argv[a+1], 0, nsteps, skyfmt);
335     nsteps = cmtx->ncols;
336     //TIMER(timer, "load sky matrix");
337     }
338     /* prepare output stream */
339     if ((ofspec != NULL) & (nsteps == 1) && hasNumberFormat(ofspec)) {
340     sprintf(fnbuf, ofspec, 1);
341     ofspec = fnbuf;
342     }
343     if (ofspec != NULL && !hasNumberFormat(ofspec)) {
344     if ((ofp = fopen(ofspec, "w")) == NULL) {
345     fprintf(stderr, "%s: cannot open '%s' for output\n",
346     progname, ofspec);
347     return(1);
348     }
349     ofspec = NULL; /* only need to open once */
350     }
351     if (hasNumberFormat(argv[a])) { /* generating image(s) */
352     if (ofspec == NULL) {
353     SET_FILE_BINARY(ofp);
354     newheader("RADIANCE", ofp);
355     printargs(argc, argv, ofp);
356     fputnow(ofp);
357     }
358     if (nsteps > 1) /* multiple output frames? */
359     for (i = 0; i < nsteps; i++) {
360     CMATRIX *cvec = cm_column(cmtx, i);
361     if (ofspec != NULL) {
362     sprintf(fnbuf, ofspec, i);
363     if ((ofp = fopen(fnbuf, "wb")) == NULL) {
364     fprintf(stderr,
365     "%s: cannot open '%s' for output\n",
366     progname, fnbuf);
367     return(1);
368     }
369     newheader("RADIANCE", ofp);
370     printargs(argc, argv, ofp);
371     fputnow(ofp);
372     }
373     fprintf(ofp, "FRAME=%d\n", i);
374     if (!sum_images(argv[a], cvec, ofp))
375     return(1);
376     if (ofspec != NULL) {
377     if (fclose(ofp) == EOF) {
378     fprintf(stderr,
379     "%s: error writing to '%s'\n",
380     progname, fnbuf);
381     return(1);
382     }
383     ofp = stdout;
384     }
385     cm_free(cvec);
386     }
387     else if (!sum_images(argv[a], cmtx, ofp))
388     return(1);
389     } else { /* generating vector/matrix */
390     CMATRIX *Vmat = cm_load(argv[a], 0, cmtx->nrows, DTfromHeader);
391     //TIMER(timer, "load view matrix");
392     CMATRIX *rmtx = cm_multiply(Vmat, cmtx);
393     //TIMER(timer, "multiply");
394     cm_free(Vmat);
395     #ifdef DC_GLARE
396     if (direct_path) { /* Do glare autonomy calculation */
397     /* Load occupancy schedule */
398     occupancy = (int*)malloc(nsteps * sizeof(int));
399     if (!occupancy) {
400     fprintf(stderr,
401     "%s: out of memory for schedule\n",
402     progname);
403     return(1);
404     }
405     if (schedule_path) {
406     if ((fp = fopen(schedule_path, "r")) == NULL) {
407     fprintf(stderr,
408     "%s: cannot open input file \"%s\"\n",
409     progname, schedule_path);
410     return(1);
411     }
412     if (cm_load_schedule(nsteps, occupancy, fp) < 0) return(1);
413     }
414     else {
415     for (i = 0; i < nsteps; i++) {
416     /* Assume hourly spacing */
417     int hour = i % 24;
418     occupancy[i] = ((hour >= start_hour) & (hour < end_hour));
419     }
420     }
421     //TIMER(timer, "load occupancy schedule");
422    
423     /* Load view directions */
424     if (view_path == NULL) {
425     if (vdir[0] == 0.0f && vdir[1] == 0.0f && vdir[2] == 0.0f) {
426     fprintf(stderr,
427     "%s: missing view direction\n",
428     progname);
429     return(1);
430     }
431     }
432     else {
433     if ((fp = fopen(view_path, "r")) == NULL) {
434     fprintf(stderr,
435     "%s: cannot open input file \"%s\"\n",
436     progname, view_path);
437     return(1);
438     }
439     if (viewfmt != DTascii)
440     SET_FILE_BINARY(fp);
441     views = cm_load_views(rmtx->nrows, viewfmt, fp);
442     if (!views) return(1);
443     //TIMER(timer, "load views");
444     }
445    
446     /* Calculate glare values */
447     Vmat = cm_load(direct_path, 0, cmtx->nrows, DTfromHeader);
448     //TIMER(timer, "load direct matrix");
449     dgp_values = cm_glare(Vmat, rmtx, cmtx, occupancy, dgp_limit, dgp_threshold, views, vdir, vup);
450     //TIMER(timer, "calculate dgp");
451     free(views);
452     cm_free(Vmat);
453    
454     /* Check successful calculation */
455     if (!dgp_values) return(1);
456     }
457     #endif /* DC_GLARE */
458     if (ofspec != NULL) { /* multiple vector files? */
459     const char *wtype = (outfmt==DTascii) ? "w" : "wb";
460     for (i = 0; i < nsteps; i++) {
461     CMATRIX *rvec = cm_column(rmtx, i);
462     sprintf(fnbuf, ofspec, i);
463     if ((ofp = fopen(fnbuf, wtype)) == NULL) {
464     fprintf(stderr,
465     "%s: cannot open '%s' for output\n",
466     progname, fnbuf);
467     return(1);
468     }
469     #ifdef getc_unlocked
470     flockfile(ofp);
471     #endif
472     if (headout) { /* header output */
473     newheader("RADIANCE", ofp);
474     printargs(argc, argv, ofp);
475     fputnow(ofp);
476     fprintf(ofp, "FRAME=%d\n", i);
477     fprintf(ofp, "NROWS=%d\n", rvec->nrows);
478     fputs("NCOLS=1\nNCOMP=3\n", ofp);
479     if ((outfmt == 'f') | (outfmt == 'd'))
480     fputendian(ofp);
481     fputformat((char *)cm_fmt_id[outfmt], ofp);
482     fputc('\n', ofp);
483     }
484     cm_write(rvec, outfmt, ofp);
485     if (fclose(ofp) == EOF) {
486     fprintf(stderr,
487     "%s: error writing to '%s'\n",
488     progname, fnbuf);
489     return(1);
490     }
491     ofp = stdout;
492     cm_free(rvec);
493     }
494     } else {
495     #ifdef getc_unlocked
496     flockfile(ofp);
497     #endif
498     if (outfmt != DTascii)
499     SET_FILE_BINARY(ofp);
500     if (headout) { /* header output */
501     newheader("RADIANCE", ofp);
502     printargs(argc, argv, ofp);
503     fputnow(ofp);
504     fprintf(ofp, "NROWS=%d\n", rmtx->nrows);
505     #ifdef DC_GLARE
506     fprintf(ofp, "NCOLS=%d\n", (!dgp_values || dgp_limit < 0) ? rmtx->ncols : 1);
507     fprintf(ofp, "NCOMP=%d\n", dgp_values ? 1 : 3);
508     #else
509     fprintf(ofp, "NCOLS=%d\n", rmtx->ncols);
510     fputs("NCOMP=3\n", ofp);
511     #endif /* DC_GLARE */
512     if ((outfmt == 'f') | (outfmt == 'd'))
513     fputendian(ofp);
514     fputformat((char *)cm_fmt_id[outfmt], ofp);
515     fputc('\n', ofp);
516     }
517     #ifdef DC_GLARE
518     if (dgp_values) { /* Write glare autonomy */
519     cm_write_glare(dgp_values, rmtx->nrows, dgp_limit < 0 ? rmtx->ncols : 1, outfmt, ofp);
520     free(dgp_values);
521     //TIMER(timer, "write");
522     }
523     else
524     #endif /* DC_GLARE */
525     cm_write(rmtx, outfmt, ofp);
526     }
527     cm_free(rmtx);
528     }
529     if (fflush(ofp) == EOF) { /* final clean-up */
530     fprintf(stderr, "%s: write error on output\n", progname);
531     return(1);
532     }
533     cm_free(cmtx);
534     return(0);
535     userr:
536     #ifdef DC_GLARE
537     fprintf(stderr, "Usage: %s [-n nsteps][-i{f|d|h}][-o{f|d}][-l limit][-b threshold][{-sf occupancy|-ss start -se end}]{-vf views [-vi{f|d}]|-vd x y z}[-vu x y z] DCdirect DCtotal [skyf]\n",
538     progname);
539     fprintf(stderr, " or: %s [-n nsteps][-i{f|d|h}][-o{f|d}][-l limit][-b threshold][{-sf occupancy|-ss start -se end}]{-vf views [-vi{f|d}]|-vd x y z}[-vu x y z] DCdirect Vspec Tbsdf Dmat.dat [skyf]\n",
540     progname);
541     #else
542     fprintf(stderr, "Usage: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] DCspec [skyf]\n",
543     progname);
544     fprintf(stderr, " or: %s [-n nsteps][-o ospec][-i{f|d|h}][-o{f|d}] Vspec Tbsdf Dmat.dat [skyf]\n",
545     progname);
546     #endif /* DC_GLARE */
547     return(1);
548     }