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