ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/dcglare.c
Revision: 2.9
Committed: Fri Jun 6 19:11:21 2025 UTC (13 days, 9 hours ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +3 -4 lines
Log Message:
refactor: Making use of printargs() more consistent with fixargv0()

File Contents

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