ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/viewbeams.c
Revision: 3.6
Committed: Mon Jul 21 22:30:18 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.5: +2 -2 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 schorsch 3.6 static const char RCSid[] = "$Id: viewbeams.c,v 3.5 2003/06/30 14:59:12 schorsch Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Convert view to beam list.
6     */
7    
8 schorsch 3.5 #include <string.h>
9    
10 gwlarson 3.1 #include "rholo.h"
11     #include "view.h"
12     #include "random.h"
13    
14     #ifndef MINRES
15     #define MINRES 12 /* section sample resolution */
16     #endif
17     #ifndef NVSAMPS
18     #define NVSAMPS 16384 /* target number of view samples */
19     #endif
20    
21     #define OJITTER 0.25 /* amount to jitter sample origin */
22    
23     #define BALLOCBLK 128 /* beam allocation block size */
24    
25    
26     static BEAMLIST blist;
27    
28    
29     static
30     add2blist(hd, bi, nr) /* add to beam sample list */
31     int hd, bi, nr;
32     {
33     register int i;
34    
35     for (i = blist.nb; i--; )
36     if (blist.bl[i].bi == bi && blist.bl[i].hd == hd) {
37     blist.bl[i].nr += nr; /* found it */
38     return;
39     }
40     i = blist.nb++; /* else add beam to list */
41     if (i % BALLOCBLK == 0) {
42 greg 3.3 blist.bl = (PACKHEAD *)realloc((void *)blist.bl,
43 gwlarson 3.1 (i+BALLOCBLK)*sizeof(PACKHEAD));
44     CHECK(blist.bl==NULL, SYSTEM, "out of memory in add2blist");
45     }
46     blist.bl[i].hd = hd; blist.bl[i].bi = bi;
47     blist.bl[i].nr = nr; blist.bl[i].nc = 0;
48     }
49    
50    
51 greg 3.4 int16 *
52 gwlarson 3.1 viewbeams(vp, hr, vr, blp) /* convert view into sections/beams */
53     VIEW *vp;
54     int hr, vr;
55     BEAMLIST *blp;
56     {
57 greg 3.4 static int16 sectlist[HDMAX+1];
58     int16 sectarr[MINRES+1][MINRES+1];
59 gwlarson 3.1 double d0, d1, mindist;
60     GCOORD gc[2];
61     FVECT rorg, rdir;
62     int shr, svr, sampquant;
63     int v;
64     register int h, hd;
65     /* clear section flags */
66 schorsch 3.5 memset((char *)sectlist, '\0', sizeof(sectlist));
67 gwlarson 3.1 /* identify view sections */
68     for (v = 0; v <= MINRES; v++)
69     for (h = 0; h <= MINRES; h++) {
70     sectarr[v][h] = -1;
71     mindist = 0.99*FHUGE;
72     if (viewray(rorg, rdir, vp, (double)h/MINRES,
73     (double)v/MINRES) < -FTINY)
74     continue;
75     for (hd = 0; hdlist[hd] != NULL; hd++) {
76     d0 = hdinter(gc, NULL, &d1,
77     hdlist[hd], rorg, rdir);
78     if (d0 >= 0.99*FHUGE)
79     continue; /* missed */
80     if (d0 <= 0. && d1 >= 0.) {
81     sectarr[v][h] = hd;
82     break; /* inside */
83     }
84     if (d0 > 0. && d0 < mindist) {
85     sectarr[v][h] = hd;
86     mindist = d0;
87     } else if (d1 < 0. && -d1 < mindist) {
88     sectarr[v][h] = hd;
89     mindist = -d1;
90     }
91     }
92     if ((hd = sectarr[v][h]) >= 0)
93     sectlist[hd]++; /* flag section */
94     }
95     /* convert flags to list */
96     for (h = hd = 0; hdlist[hd] != NULL; h++, hd++) {
97     while (!sectlist[hd])
98     if (hdlist[++hd] == NULL)
99     goto loopexit;
100     sectlist[h] = hd;
101     }
102     loopexit:
103     sectlist[h] = -1; /* list terminator */
104     if (blp == NULL) /* if no beam list... */
105     return(sectlist); /* return early */
106     /* else set up sampling */
107     if (hr|vr && hr*vr <= NVSAMPS) {
108     shr = hr; svr = vr;
109     sampquant = 1;
110     } else {
111     shr = sqrt(NVSAMPS/viewaspect(vp)) + .5;
112     svr = (NVSAMPS + shr/2)/shr;
113     sampquant = (hr*vr + shr*svr/2)/(shr*svr);
114     }
115     blist.bl = NULL; blist.nb = 0; /* sample view rays */
116     for (v = svr; v--; )
117     for (h = shr; h--; ) {
118     hd = random()>>6 & 03; /* get section */
119     hd = sectarr[v*MINRES/svr + (hd&01)]
120     [h*MINRES/shr + (hd>>1)];
121     if (hd < 0)
122     continue;
123     /* get sample ray */
124     if (viewray(rorg, rdir, vp, (h+frandom())/shr,
125     (v+frandom())/svr) < -FTINY)
126     continue;
127     #ifdef OJITTER
128     /* jitter origin */
129     d0 = OJITTER*(frandom() - .5) / hdlist[hd]->grid[0];
130     VSUM(rorg, rorg, hdlist[hd]->xv[0], d0);
131     d0 = OJITTER*(frandom() - .5) / hdlist[hd]->grid[1];
132     VSUM(rorg, rorg, hdlist[hd]->xv[1], d0);
133     d0 = OJITTER*(frandom() - .5) / hdlist[hd]->grid[2];
134     VSUM(rorg, rorg, hdlist[hd]->xv[2], d0);
135     #endif
136     /* intersect section */
137     if (hdinter(gc, NULL, NULL, hdlist[hd], rorg, rdir)
138     >= 0.99*FHUGE)
139     continue;
140     /* add samples */
141     add2blist(hd, hdbindex(hdlist[hd],gc), sampquant);
142     }
143 schorsch 3.6 *blp = blist; /* transfer beam list */
144 gwlarson 3.1 return(sectlist); /* all done! */
145     }
146    
147    
148     int
149     nextview(vp, fp) /* get next view from fp */
150     VIEW *vp;
151     FILE *fp;
152     {
153     char linebuf[256];
154    
155     while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
156     if (isview(linebuf) && sscanview(vp, linebuf) > 0)
157     return(0);
158     return(EOF);
159     }