ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dcglare.c
Revision: 2.8
Committed: Wed Mar 5 02:59:58 2025 UTC (8 weeks, 3 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.7: +3 -0 lines
Log Message:
chore: Added missing RCSid

File Contents

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