ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/glaresrc.c
(Generate patch)

Comparing ray/src/util/glaresrc.c (file contents):
Revision 1.13 by greg, Fri Apr 19 17:41:03 1991 UTC vs.
Revision 2.5 by schorsch, Fri Jan 2 12:51:54 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Gather samples and compute glare sources.
6   */
7  
8   #include "glare.h"
9 + #include "linregr.h"
10  
11   #define vcont(vd,vu)    ((vu)-(vd)<=SEPS)
12   #define hcont(s1,s2)    ((s1)->r-(s2)->l>=-SEPS&&(s2)->r-(s1)->l>=-SEPS)
# Line 16 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14   struct source   *curlist = NULL;        /* current source list */
15   struct source   *donelist = NULL;       /* finished sources */
16  
17 + static struct srcspan * newspan(int     l, int  r, int  v, float        *sb);
18 + static struct srcspan * newspan(int     l, int  r, int  v, float        *sb);
19 + static void addindirect(int     h, int  v, double       br);
20 + static void addsrcspan(struct srcspan   *nss);
21 + static void mergesource(struct source   *sp, struct source      *ap);
22 + static void close_sources(int   v);
23 + static void close_allsrcs(void);
24 + static struct srcspan * splitspan(register struct srcspan       *sso, double    h, double       v, double       m);
25 + static struct source * splitsource(struct source        *so);
26 + static void donesource(register struct source   *sp);
27 + static struct source * findbuddy(register struct source *s, register struct source      *l);
28 + static void absorb(register struct source       *s);
29 + static void freespans(struct source     *sp);
30  
31 < struct srcspan *
32 < newspan(l, r, v, sb)            /* allocate a new source span */
33 < int     l, r, v;
34 < float   *sb;
31 >
32 > static struct srcspan *
33 > newspan(                /* allocate a new source span */
34 >        int     l,
35 >        int     r,
36 >        int     v,
37 >        float   *sb
38 > )
39   {
40          register struct srcspan *ss;
41          register int    i;
# Line 38 | Line 53 | float  *sb;
53   }
54  
55  
56 < analyze()                       /* analyze our scene */
56 > extern void
57 > analyze(void)                   /* analyze our scene */
58   {
59          int     h, v;
60          int     left;
# Line 50 | Line 66 | analyze()                      /* analyze our scene */
66          for (v = vsize; v >= -vsize; v--) {
67                  close_sources(v);
68   #ifndef DEBUG
69 <                if (verbose)
69 >                if (verbose) {
70                          fprintf(stderr, "%s: analyzing... %3ld%%\r",
71                                  progname, 100L*(vsize-v)/(2*vsize));
72 +                        fflush(stderr);
73 +                }
74   #endif
75                  getviewspan(v, spanbr);
76                  left = hsize + 1;
# Line 78 | Line 96 | analyze()                      /* analyze our scene */
96                  if (left < h)
97                          addsrcspan(newspan(left,h,v,spanbr));
98          }
99 <        free((char *)spanbr);
99 >        free((void *)spanbr);
100          close_allsrcs();
101   }
102  
103  
104 < addindirect(h, v, br)           /* add brightness to indirect illuminances */
105 < int     h, v;
106 < double  br;
104 > static void
105 > addindirect(            /* add brightness to indirect illuminances */
106 >        int     h,
107 >        int     v,
108 >        double  br
109 > )
110   {
111          double  tanb, d;
112          int     hl;
# Line 131 | Line 152 | double br;
152   }
153  
154  
155 < comp_thresh()                   /* compute glare threshold */
155 > extern void
156 > comp_thresh(void)                       /* compute glare threshold */
157   {
158          int     h, v;
159          int     nsamps;
# Line 170 | Line 192 | comp_thresh()                  /* compute glare threshold */
192   }
193  
194  
195 < addsrcspan(nss)                 /* add new source span to our list */
196 < struct srcspan  *nss;
195 > static void
196 > addsrcspan(                     /* add new source span to our list */
197 >        struct srcspan  *nss
198 > )
199   {
200          struct source   *last, *cs, *this;
201          register struct srcspan *ss;
# Line 208 | Line 232 | struct srcspan *nss;
232   }
233  
234  
235 < mergesource(sp, ap)             /* merge source ap into source sp */
236 < struct source   *sp, *ap;
235 > static void
236 > mergesource(            /* merge source ap into source sp */
237 >        struct source   *sp,
238 >        struct source   *ap
239 > )
240   {
241          struct srcspan  head;
242          register struct srcspan *alp, *prev, *tp;
# Line 238 | Line 265 | struct source  *sp, *ap;
265                  sp->brt = (sp->brt*sp->dom + ap->brt*ap->dom)
266                                  / (sp->dom + ap->dom);
267          }
268 <        free((char *)ap);
268 >        free((void *)ap);
269   }
270  
271  
272 < close_sources(v)                /* close sources above v */
273 < int     v;
272 > static void
273 > close_sources(          /* close sources above v */
274 >        int     v
275 > )
276   {
277          struct source   head;
278          register struct source  *last, *this;
# Line 261 | Line 290 | int    v;
290   }
291  
292  
293 < close_allsrcs()                 /* done with everything */
293 > static void
294 > close_allsrcs(void)                     /* done with everything */
295   {
296          register struct source  *this, *next;
297  
# Line 275 | Line 305 | close_allsrcs()                        /* done with everything */
305   }
306  
307  
308 < donesource(sp)                  /* finished with this source */
309 < register struct source  *sp;
308 > static struct srcspan *
309 > splitspan(              /* divide source span at point */
310 >        register struct srcspan *sso,
311 >        double  h,
312 >        double  v,
313 >        double  m
314 > )
315   {
316 <        FVECT   dthis, dright;
316 >        register struct srcspan *ssn;
317 >        double  d;
318 >        int     hs;
319 >
320 >        d = h - m*(sso->v - v);
321 >        hs = d < 0. ? d-.5 : d+.5;
322 >        if (sso->l >= hs)
323 >                return(NULL);
324 >        if (sso->r <= hs)
325 >                return(sso);
326 >                                /* need to split it */
327 >        ssn = (struct srcspan *)malloc(sizeof(struct srcspan));
328 >        if (ssn == NULL)
329 >                memerr("source spans in splitspan");
330 >        ssn->brsum = (double)(hs - sso->l)/(sso->r - sso->l) * sso->brsum;
331 >        sso->brsum -= ssn->brsum;
332 >        ssn->v = sso->v;
333 >        ssn->l = sso->l;
334 >        ssn->r = sso->l = hs;
335 >        return(ssn);
336 > }
337 >
338 >
339 > static struct source *
340 > splitsource(                    /* divide source in two if it's big and long */
341 >        struct source   *so
342 > )
343 > {
344 >        LRSUM   lr;
345 >        LRLIN   fit;
346 >        register struct srcspan *ss, *ssn;
347 >        struct srcspan  *ssl, *ssnl, head;
348 >        int     h;
349 >        double  mh, mv;
350 >        struct source   *sn;
351 >
352 >        lrclear(&lr);
353 >        for (ss = so->first; ss != NULL; ss = ss->next)
354 >                for (h = ss->l; h < ss->r; h++)
355 >                        lrpoint(h, ss->v, &lr);
356 >        if ((double)lr.n/(sampdens*sampdens) < SABIG)
357 >                return(NULL);                   /* too small */
358 >        if (lrfit(&fit, &lr) < 0)
359 >                return(NULL);                   /* can't fit a line */
360 >        if (fit.correlation < LCORR && fit.correlation > -LCORR)
361 >                return(NULL);
362 >        if (verbose)
363 >                fprintf(stderr, "%s: splitting large source\n", progname);
364 >        mh = lrxavg(&lr);
365 >        mv = lryavg(&lr);
366 >        sn = (struct source *)malloc(sizeof(struct source));
367 >        if (sn == NULL)
368 >                memerr("source records in splitsource");
369 >        sn->dom = 0.0;
370 >        sn->first = NULL;
371 >        ssnl = NULL;
372 >        head.next = so->first;
373 >        ssl = &head;
374 >        for (ss = so->first; ss != NULL; ssl = ss, ss = ss->next)
375 >                if ((ssn = splitspan(ss, mh, mv, fit.slope)) != NULL) {
376 >                        if (ssn == ss) {        /* remove from old */
377 >                                ssl->next = ss->next;
378 >                                ss = ssl;
379 >                        }
380 >                        if (ssnl == NULL)       /* add to new */
381 >                                sn->first = ssn;
382 >                        else
383 >                                ssnl->next = ssn;
384 >                        ssn->next = NULL;
385 >                        ssnl = ssn;
386 >                }
387 >        so->first = head.next;
388 >        return(sn);
389 > }
390 >
391 >
392 > static void
393 > donesource(                     /* finished with this source */
394 >        register struct source  *sp
395 > )
396 > {
397 >        struct source   *newsrc;
398          register struct srcspan *ss;
399          int     h, n;
400          double  hsum, vsum, d;
401  
402 +        while ((newsrc = splitsource(sp)) != NULL)      /* split it? */
403 +                donesource(newsrc);
404          sp->dom = 0.0;
405          hsum = vsum = 0.0;
406          sp->brt = 0.0;
# Line 290 | Line 408 | register struct source *sp;
408          for (ss = sp->first; ss != NULL; ss = ss->next) {
409                  sp->brt += ss->brsum;
410                  n += ss->r - ss->l;
411 <                for (h = ss->r-1; h >= ss->l; h--) {
411 >                for (h = ss->l; h < ss->r; h++) {
412                          d = pixsize(h, ss->v);
413                          hsum += d*h;
414                          vsum += d*ss->v;
415                          sp->dom += d;
416                  }
299                free((char *)ss);
417          }
418 <        sp->first = NULL;
418 >        freespans(sp);
419 >        if (sp->dom <= FTINY) {         /* must be right at edge of image */
420 >                free((void *)sp);
421 >                return;
422 >        }
423          sp->brt /= (double)n;
424          compdir(sp->dir, (int)(hsum/sp->dom), (int)(vsum/sp->dom));
425          sp->next = donelist;
# Line 311 | Line 432 | register struct source *sp;
432   }
433  
434  
435 < struct source *
436 < findbuddy(s, l)                 /* find close enough source to s in l*/
437 < register struct source  *s, *l;
435 > static struct source *
436 > findbuddy(                      /* find close enough source to s in l*/
437 >        register struct source  *s,
438 >        register struct source  *l
439 > )
440   {
441          struct source   *bestbuddy = NULL;
442          double  d, r, mindist = MAXBUDDY;
# Line 330 | Line 453 | register struct source *s, *l;
453   }
454  
455  
456 < absorb_specks()                 /* eliminate too-small sources */
456 > extern void
457 > absorb_specks(void)                     /* eliminate too-small sources */
458   {
459          struct source   head, *buddy;
460          register struct source  *last, *this;
# Line 354 | Line 478 | absorb_specks()                        /* eliminate too-small sources */
478   }
479  
480  
481 < absorb(s)                       /* absorb a source into indirect */
482 < register struct source  *s;
481 > static void
482 > absorb(                 /* absorb a source into indirect */
483 >        register struct source  *s
484 > )
485   {
486          FVECT   dir;
487          double  d;
# Line 369 | Line 495 | register struct source *s;
495                  indirect[i].sum += d * s->brt;
496                  indirect[i].n += d;
497          }
498 <        for ( ; s->first != NULL; s->first = s->first->next)
499 <                free((char *)s->first);
500 <        free((char *)s);
498 >        freespans(s);
499 >        free((void *)s);
500 > }
501 >
502 >
503 > static void
504 > freespans(                      /* free spans associated with source */
505 >        struct source   *sp
506 > )
507 > {
508 >        register struct srcspan *ss;
509 >
510 >        while ((ss = sp->first) != NULL) {
511 >                sp->first = ss->next;
512 >                free((void *)ss);
513 >        }
514   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines