ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/findglare.c
Revision: 2.12
Committed: Fri Mar 26 23:34:23 2004 UTC (20 years ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad3R6, rad3R6P1, rad3R8, rad3R9
Changes since 2.11: +2 -2 lines
Log Message:
Continued ANSIfication.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: findglare.c,v 2.11 2004/01/02 12:51:54 schorsch Exp $";
3 #endif
4 /*
5 * Find glare sources in a scene or image.
6 *
7 * Greg Ward March 1991
8 */
9
10 #include "glare.h"
11
12 #define FEQ(a,b) ((a)-(b)<=FTINY&&(b)-(a)<=FTINY)
13 #define VEQ(v1,v2) (FEQ((v1)[0],(v2)[0])&&FEQ((v1)[1],(v2)[1]) \
14 &&FEQ((v1)[2],(v2)[2]))
15
16 char *rtargv[64] = {"rtrace", "-h-", "-ov", "-fff", "-ld-", "-i-", "-I-"};
17 int rtargc = 7;
18
19 VIEW ourview = STDVIEW; /* our view */
20 VIEW pictview = STDVIEW; /* picture view */
21 VIEW leftview, rightview; /* leftmost and rightmost views */
22
23 char *picture = NULL; /* picture file name */
24 char *octree = NULL; /* octree file name */
25
26 int verbose = 0; /* verbose reporting */
27 char *progname; /* global argv[0] */
28
29 double threshold = 0.; /* glare threshold */
30
31 int sampdens = SAMPDENS; /* sample density */
32 ANGLE glarang[180] = {AEND}; /* glare calculation angles */
33 int nglarangs = 0;
34 double maxtheta; /* maximum angle (in radians) */
35 int hsize; /* horizontal size */
36
37 struct illum *indirect; /* array of indirect illuminances */
38
39 long npixinvw; /* number of pixels in view */
40 long npixmiss; /* number of pixels missed */
41
42 static int angcmp(const void *ap1, const void *ap2);
43 static void init(void);
44 static void cleanup(void);
45 static void printsources(void);
46 static void printillum(void);
47
48
49
50 int
51 main(
52 int argc,
53 char *argv[]
54 )
55 {
56 int combine = 1;
57 int gotview = 0;
58 int rval, i;
59 char *err;
60
61 progname = argv[0];
62 /* process options */
63 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
64 /* expand arguments */
65 while ((rval = expandarg(&argc, &argv, i)) > 0)
66 ;
67 if (rval < 0) {
68 fprintf(stderr, "%s: cannot expand '%s'",
69 argv[0], argv[i]);
70 exit(1);
71 }
72 rval = getviewopt(&ourview, argc-i, argv+i);
73 if (rval >= 0) {
74 i += rval;
75 gotview++;
76 continue;
77 }
78 switch (argv[i][1]) {
79 case 't':
80 threshold = atof(argv[++i]);
81 break;
82 case 'r':
83 sampdens = atoi(argv[++i])/2;
84 break;
85 case 'v':
86 if (argv[i][2] == '\0') {
87 verbose++;
88 break;
89 }
90 if (argv[i][2] != 'f')
91 goto userr;
92 rval = viewfile(argv[++i], &ourview, NULL);
93 if (rval < 0) {
94 fprintf(stderr,
95 "%s: cannot open view file \"%s\"\n",
96 progname, argv[i]);
97 exit(1);
98 } else if (rval == 0) {
99 fprintf(stderr,
100 "%s: bad view file \"%s\"\n",
101 progname, argv[i]);
102 exit(1);
103 } else
104 gotview++;
105 break;
106 case 'g':
107 if (argv[i][2] != 'a')
108 goto userr;
109 if (setscan(glarang, argv[++i]) < 0) {
110 fprintf(stderr, "%s: bad angle spec \"%s\"\n",
111 progname, argv[i]);
112 exit(1);
113 }
114 break;
115 case 'p':
116 picture = argv[++i];
117 break;
118 case 'c':
119 combine = !combine;
120 break;
121 case 'd':
122 rtargv[rtargc++] = argv[i];
123 if (argv[i][2] != 'v')
124 rtargv[rtargc++] = argv[++i];
125 break;
126 case 'l':
127 if (argv[i][2] == 'd')
128 break;
129 /* FALL THROUGH */
130 case 's':
131 case 'P':
132 rtargv[rtargc++] = argv[i];
133 rtargv[rtargc++] = argv[++i];
134 break;
135 case 'w':
136 rtargv[rtargc++] = argv[i];
137 break;
138 case 'a':
139 rtargv[rtargc++] = argv[i];
140 if (argv[i][2] == 'v') {
141 rtargv[rtargc++] = argv[++i];
142 rtargv[rtargc++] = argv[++i];
143 }
144 rtargv[rtargc++] = argv[++i];
145 break;
146 default:
147 goto userr;
148 }
149 }
150 /* get octree */
151 if (i < argc-1)
152 goto userr;
153 if (i == argc-1) {
154 rtargv[rtargc++] = octree = argv[i];
155 rtargv[rtargc] = NULL;
156 }
157 /* get view */
158 if (picture != NULL) {
159 rval = viewfile(picture, &pictview, NULL);
160 if (rval < 0) {
161 fprintf(stderr, "%s: cannot open picture file \"%s\"\n",
162 progname, picture);
163 exit(1);
164 } else if (rval == 0) {
165 fprintf(stderr,
166 "%s: cannot get view from picture \"%s\"\n",
167 progname, picture);
168 exit(1);
169 }
170 if (pictview.type == VT_PAR) {
171 fprintf(stderr, "%s: %s: cannot use parallel view\n",
172 progname, picture);
173 exit(1);
174 }
175 if ((err = setview(&pictview)) != NULL) {
176 fprintf(stderr, "%s: %s\n", picture, err);
177 exit(1);
178 }
179 }
180 if (!gotview) {
181 if (picture == NULL) {
182 fprintf(stderr, "%s: must have view or picture\n",
183 progname);
184 exit(1);
185 }
186 ourview = pictview;
187 } else if (picture != NULL && !VEQ(ourview.vp, pictview.vp)) {
188 fprintf(stderr, "%s: picture must have same viewpoint\n",
189 progname);
190 exit(1);
191 }
192 ourview.type = VT_HEM;
193 ourview.horiz = ourview.vert = 180.0;
194 ourview.hoff = ourview.voff = 0.0;
195 fvsum(ourview.vdir, ourview.vdir, ourview.vup,
196 -DOT(ourview.vdir,ourview.vup));
197 if ((err = setview(&ourview)) != NULL) {
198 fprintf(stderr, "%s: %s\n", progname, err);
199 exit(1);
200 }
201 if (octree == NULL && picture == NULL) {
202 fprintf(stderr,
203 "%s: must specify at least one of picture or octree\n",
204 progname);
205 exit(1);
206 }
207 init(); /* initialize program */
208 if (threshold <= FTINY)
209 comp_thresh(); /* compute glare threshold */
210 analyze(); /* analyze view */
211 if (combine)
212 absorb_specks(); /* eliminate tiny sources */
213 cleanup(); /* tidy up */
214 /* print header */
215 newheader("RADIANCE", stdout);
216 printargs(argc, argv, stdout);
217 fputs(VIEWSTR, stdout);
218 fprintview(&ourview, stdout);
219 printf("\n");
220 fputformat("ascii", stdout);
221 printf("\n");
222 printsources(); /* print glare sources */
223 printillum(); /* print illuminances */
224 exit(0);
225 userr:
226 fprintf(stderr,
227 "Usage: %s [view options][-ga angles][-p picture][[rtrace options] octree]\n",
228 progname);
229 exit(1);
230 }
231
232
233 static int
234 angcmp( /* compare two angles */
235 const void *ap1,
236 const void *ap2
237 )
238 {
239 register int a1, a2;
240
241 a1 = *(ANGLE *)ap1;
242 a2 = *(ANGLE *)ap2;
243 if (a1 == a2) {
244 fprintf(stderr, "%s: duplicate glare angle (%d)\n",
245 progname, a1);
246 exit(1);
247 }
248 return(a1-a2);
249 }
250
251
252 static void
253 init(void) /* initialize global variables */
254 {
255 double d;
256 register int i;
257
258 if (verbose)
259 fprintf(stderr, "%s: initializing data structures...\n",
260 progname);
261 /* set direction vectors */
262 for (i = 0; glarang[i] != AEND; i++)
263 ;
264 qsort(glarang, i, sizeof(ANGLE), angcmp);
265 if (i > 0 && (glarang[0] <= 0 || glarang[i-1] > 180)) {
266 fprintf(stderr, "%s: glare angles must be between 1 and 180\n",
267 progname);
268 exit(1);
269 }
270 nglarangs = i;
271 /* nglardirs = 2*nglarangs + 1; */
272 /* vsize = sampdens - 1; */
273 if (nglarangs > 0)
274 maxtheta = (PI/180.)*glarang[nglarangs-1];
275 else
276 maxtheta = 0.0;
277 hsize = hlim(0) + sampdens - 1;
278 if (hsize > (int)(PI*sampdens))
279 hsize = PI*sampdens;
280 indirect = (struct illum *)calloc(nglardirs, sizeof(struct illum));
281 if (indirect == NULL)
282 memerr("indirect illuminances");
283 npixinvw = npixmiss = 0L;
284 leftview = ourview;
285 rightview = ourview;
286 spinvector(leftview.vdir, ourview.vdir, ourview.vup, maxtheta);
287 spinvector(rightview.vdir, ourview.vdir, ourview.vup, -maxtheta);
288 setview(&leftview);
289 setview(&rightview);
290 indirect[nglarangs].lcos =
291 indirect[nglarangs].rcos = cos(maxtheta);
292 indirect[nglarangs].rsin =
293 -(indirect[nglarangs].lsin = sin(maxtheta));
294 indirect[nglarangs].theta = 0.0;
295 for (i = 0; i < nglarangs; i++) {
296 d = (glarang[nglarangs-1] - glarang[i])*(PI/180.);
297 indirect[nglarangs-i-1].lcos =
298 indirect[nglarangs+i+1].rcos = cos(d);
299 indirect[nglarangs+i+1].rsin =
300 -(indirect[nglarangs-i-1].lsin = sin(d));
301 d = (glarang[nglarangs-1] + glarang[i])*(PI/180.);
302 indirect[nglarangs-i-1].rcos =
303 indirect[nglarangs+i+1].lcos = cos(d);
304 indirect[nglarangs-i-1].rsin =
305 -(indirect[nglarangs+i+1].lsin = sin(d));
306 indirect[nglarangs-i-1].theta = (PI/180.)*glarang[i];
307 indirect[nglarangs+i+1].theta = -(PI/180.)*glarang[i];
308 }
309 /* open picture */
310 if (picture != NULL) {
311 if (verbose)
312 fprintf(stderr, "%s: opening picture file \"%s\"...\n",
313 progname, picture);
314 open_pict(picture);
315 }
316 /* start rtrace */
317 if (octree != NULL) {
318 if (verbose) {
319 fprintf(stderr,
320 "%s: starting luminance calculation...\n\t",
321 progname);
322 printargs(rtargc, rtargv, stderr);
323 }
324 fork_rtrace(rtargv);
325 }
326 }
327
328
329 static void
330 cleanup(void) /* close files, wait for children */
331 {
332 if (verbose)
333 fprintf(stderr, "%s: cleaning up... \n", progname);
334 if (picture != NULL)
335 close_pict();
336 if (octree != NULL)
337 done_rtrace();
338 if (npixinvw < 100*npixmiss)
339 fprintf(stderr, "%s: warning -- missing %d%% of samples\n",
340 progname, (int)(100L*npixmiss/npixinvw));
341 }
342
343
344 extern int
345 compdir( /* compute direction for x,y */
346 FVECT vd,
347 int x,
348 int y
349 )
350 {
351 int hl;
352 FVECT org; /* dummy variable */
353
354 hl = hlim(y);
355 if (x <= -hl) { /* left region */
356 if (x <= -hl-sampdens)
357 return(-1);
358 return(viewray(org, vd, &leftview,
359 (double)(x+hl)/(2*sampdens)+.5,
360 (double)y/(2*sampdens)+.5));
361 }
362 if (x >= hl) { /* right region */
363 if (x >= hl+sampdens)
364 return(-1);
365 return(viewray(org, vd, &rightview,
366 (double)(x-hl)/(2*sampdens)+.5,
367 (double)y/(2*sampdens)+.5));
368 }
369 /* central region */
370 if (viewray(org, vd, &ourview, .5, (double)y/(2*sampdens)+.5) < 0)
371 return(-1);
372 spinvector(vd, vd, ourview.vup, h_theta(x,y));
373 return(0);
374 }
375
376
377 extern double
378 pixsize( /* return the solid angle of pixel at (x,y) */
379 int x,
380 int y
381 )
382 {
383 register int hl, xo;
384 double disc;
385
386 hl = hlim(y);
387 if (x < -hl)
388 xo = x+hl;
389 else if (x > hl)
390 xo = x-hl;
391 else
392 xo = 0;
393 disc = 1. - (double)((long)xo*xo + (long)y*y)/((long)sampdens*sampdens);
394 if (disc <= FTINY*FTINY)
395 return(0.);
396 return(1./(sampdens*sampdens*sqrt(disc)));
397 }
398
399
400 extern void
401 memerr( /* malloc failure */
402 char *s
403 )
404 {
405 fprintf(stderr, "%s: out of memory for %s\n", progname, s);
406 exit(1);
407 }
408
409
410 static void
411 printsources(void) /* print out glare sources */
412 {
413 register struct source *sp;
414
415 printf("BEGIN glare source\n");
416 for (sp = donelist; sp != NULL; sp = sp->next)
417 printf("\t%f %f %f\t%f\t%f\n",
418 sp->dir[0], sp->dir[1], sp->dir[2],
419 sp->dom, sp->brt);
420 printf("END glare source\n");
421 }
422
423
424 static void
425 printillum(void) /* print out indirect illuminances */
426 {
427 register int i;
428
429 printf("BEGIN indirect illuminance\n");
430 for (i = 0; i < nglardirs; i++)
431 if (indirect[i].n > FTINY)
432 printf("\t%.0f\t%f\n", (180.0/PI)*indirect[i].theta,
433 PI * indirect[i].sum / indirect[i].n);
434 printf("END indirect illuminance\n");
435 }