ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhdisp2.c
Revision: 3.37
Committed: Thu Jan 1 11:21:55 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad5R1, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1
Changes since 3.36: +61 -38 lines
Log Message:
Ansification and prototypes.

File Contents

# User Rev Content
1 gregl 3.1 #ifndef lint
2 schorsch 3.37 static const char RCSid[] = "$Id: rhdisp2.c,v 3.36 2003/07/27 22:12:02 schorsch Exp $";
3 gregl 3.1 #endif
4     /*
5 gregl 3.2 * Holodeck beam tracking for display process
6 gregl 3.1 */
7    
8     #include "rholo.h"
9     #include "rhdisp.h"
10     #include "rhdriver.h"
11    
12 gwlarson 3.26 #ifndef MEYERNG
13 gwlarson 3.30 #define MEYERNG 0.2 /* target mean eye range (rel. to grid) */
14 gwlarson 3.26 #endif
15 gwlarson 3.20
16 gregl 3.1 #define CBEAMBLK 1024 /* cbeam allocation block size */
17    
18 gwlarson 3.32 static PACKHEAD *cbeam = NULL; /* current beam list */
19 gregl 3.1 static int ncbeams = 0; /* number of sorted beams in cbeam */
20     static int xcbeams = 0; /* extra (unregistered) beams past ncbeams */
21     static int maxcbeam = 0; /* size of cbeam array */
22    
23 gwlarson 3.32 VIEWPOINT cureye; /* current eye position */
24 gregl 3.1
25 schorsch 3.37 static int newcbeam(void);
26     static int cbeamcmp(const void *cb1, const void *cb2);
27     static int cbeamcmp2(const void *cb1, const void *cb2);
28     static int findcbeam(int hd, int bi);
29     static int getcbeam(int hd, int bi);
30     static void cbeamsort(int adopt);
31 gwlarson 3.32
32 schorsch 3.37
33     static int
34     newcbeam(void) /* allocate new entry at end of cbeam array */
35 gregl 3.1 {
36     int i;
37    
38     if ((i = ncbeams + xcbeams++) >= maxcbeam) { /* grow array */
39     maxcbeam += CBEAMBLK;
40     if (cbeam == NULL)
41 gwlarson 3.32 cbeam = (PACKHEAD *)malloc(
42     maxcbeam*sizeof(PACKHEAD) );
43 gregl 3.1 else
44 greg 3.34 cbeam = (PACKHEAD *)realloc((void *)cbeam,
45 gwlarson 3.32 maxcbeam*sizeof(PACKHEAD) );
46 gregl 3.1 if (cbeam == NULL)
47     error(SYSTEM, "out of memory in newcbeam");
48     }
49     return(i);
50     }
51    
52    
53 schorsch 3.37 static int
54     cbeamcmp( /* compare two cbeam entries for sort: keep orphans */
55     const void *cb1,
56     const void *cb2
57     )
58 gregl 3.1 {
59     register int c;
60    
61 schorsch 3.37 if ((c = ((PACKHEAD*)cb1)->bi - ((PACKHEAD*)cb2)->bi)) /* sort on beam index first */
62 gregl 3.1 return(c);
63 schorsch 3.37 return(((PACKHEAD*)cb1)->hd - ((PACKHEAD*)cb2)->hd); /* use hd to resolve matches */
64 gregl 3.1 }
65    
66    
67 schorsch 3.37 static int
68     cbeamcmp2( /* compare two cbeam entries for sort: no orphans */
69     const void *cb1,
70     const void *cb2
71     )
72 gregl 3.1 {
73     register int c;
74    
75 schorsch 3.37 if (!((PACKHEAD*)cb1)->nr) /* put orphans at the end, unsorted */
76     return(((PACKHEAD*)cb2)->nr);
77     if (!((PACKHEAD*)cb2)->nr)
78 gregl 3.15 return(-1);
79 schorsch 3.37 if ((c = ((PACKHEAD*)cb1)->bi - ((PACKHEAD*)cb2)->bi)) /* sort on beam index first */
80 gregl 3.1 return(c);
81 schorsch 3.37 return(((PACKHEAD*)cb1)->hd - ((PACKHEAD*)cb2)->hd); /* use hd to resolve matches */
82 gregl 3.1 }
83    
84    
85 schorsch 3.37 static int
86     findcbeam( /* find the specified beam in our sorted list */
87     int hd,
88     int bi
89     )
90 gregl 3.1 {
91 gwlarson 3.32 PACKHEAD cb;
92     register PACKHEAD *p;
93 gregl 3.1
94     if (ncbeams <= 0)
95     return(-1);
96 gwlarson 3.32 cb.hd = hd; cb.bi = bi; cb.nr = cb.nc = 0;
97     p = (PACKHEAD *)bsearch((char *)&cb, (char *)cbeam, ncbeams,
98     sizeof(PACKHEAD), cbeamcmp);
99 gregl 3.1 if (p == NULL)
100     return(-1);
101     return(p - cbeam);
102     }
103    
104    
105 schorsch 3.37 static int
106     getcbeam( /* get the specified beam, allocating as necessary */
107     register int hd,
108     int bi
109     )
110 gregl 3.1 {
111     register int n;
112     /* first, look in sorted list */
113     if ((n = findcbeam(hd, bi)) >= 0)
114     return(n);
115     /* linear search through xcbeams to be sure */
116     for (n = ncbeams+xcbeams; n-- > ncbeams; )
117     if (cbeam[n].bi == bi && cbeam[n].hd == hd)
118     return(n);
119     /* check legality */
120 schorsch 3.36 if ((hd < 0) | (hd >= HDMAX) || hdlist[hd] == NULL)
121 gregl 3.1 error(INTERNAL, "illegal holodeck number in getcbeam");
122 schorsch 3.36 if ((bi < 1) | (bi > nbeams(hdlist[hd])))
123 gregl 3.1 error(INTERNAL, "illegal beam index in getcbeam");
124     n = newcbeam(); /* allocate and assign */
125 gwlarson 3.32 cbeam[n].hd = hd; cbeam[n].bi = bi; cbeam[n].nr = cbeam[n].nc = 0;
126 gregl 3.1 return(n);
127     }
128    
129    
130 schorsch 3.37 static void
131     cbeamsort( /* sort our beam list, possibly turning out orphans */
132     int adopt
133     )
134 gregl 3.1 {
135     register int i;
136    
137     if (!(ncbeams += xcbeams))
138     return;
139     xcbeams = 0;
140 gwlarson 3.32 qsort((char *)cbeam, ncbeams, sizeof(PACKHEAD),
141 gregl 3.1 adopt ? cbeamcmp : cbeamcmp2);
142     if (adopt)
143     return;
144     for (i = ncbeams; i--; ) /* identify orphans */
145 gwlarson 3.22 if (cbeam[i].nr)
146 gregl 3.1 break;
147     xcbeams = ncbeams - ++i; /* put orphans after ncbeams */
148     ncbeams = i;
149     }
150    
151    
152 schorsch 3.37 extern void
153     beam_init( /* clear beam list for new view(s) */
154     int fresh
155     )
156 gregl 3.1 {
157 gwlarson 3.21 register int i;
158 gwlarson 3.24
159     if (fresh) /* discard old beams? */
160     ncbeams = xcbeams = 0;
161     else /* else clear sample requests */
162     for (i = ncbeams+xcbeams; i--; )
163     cbeam[i].nr = 0;
164 gwlarson 3.26 cureye.rng = 0.;
165 gregl 3.1 }
166    
167    
168 schorsch 3.37 extern int16 *
169     beam_view( /* add beam view (if advisable) */
170     VIEW *vn,
171     int hr,
172     int vr
173     )
174 gregl 3.4 {
175 greg 3.35 int16 *slist;
176 gwlarson 3.32 BEAMLIST blist;
177     double eravg, d;
178 gwlarson 3.26 register HOLO *hp;
179 gwlarson 3.32 register int i, n;
180     /* compute beams for view */
181     slist = viewbeams(vn, hr, vr, &blist);
182     if (*slist < 0) {
183     error(COMMAND, "no sections visible from this view");
184 gwlarson 3.29 return(NULL);
185 gwlarson 3.32 }
186 gwlarson 3.30 /* sort current beam list */
187 gwlarson 3.21 cbeamsort(1);
188 gwlarson 3.32 /* add new beams to list */
189     for (i = blist.nb; i--; ) {
190     n = getcbeam(blist.bl[i].hd, blist.bl[i].bi);
191     if (blist.bl[i].nr > cbeam[n].nr)
192     cbeam[n].nr = blist.bl[i].nr;
193     }
194 greg 3.33 free((void *)blist.bl); /* free list */
195 gwlarson 3.32 if (MEYERNG <= FTINY)
196     return(slist);
197     /* compute average eye range */
198 gwlarson 3.30 eravg = 0.;
199 gwlarson 3.32 for (n = 0; slist[n] >= 0; n++) {
200     hp = hdlist[slist[n]];
201     eravg += MEYERNG/3. / VLEN(hp->wg[0]) +
202 gwlarson 3.30 MEYERNG/3. / VLEN(hp->wg[1]) +
203     MEYERNG/3. / VLEN(hp->wg[2]) ;
204     }
205     eravg /= (double)n;
206 gwlarson 3.26 /* add to current eye position */
207     if (cureye.rng <= FTINY) {
208     VCOPY(cureye.vpt, vn->vp);
209 gwlarson 3.30 cureye.rng = eravg;
210     } else if ((d = sqrt(dist2(vn->vp,cureye.vpt))) + eravg > cureye.rng) {
211 gwlarson 3.26 for (i = 3; i--; )
212     cureye.vpt[i] = 0.5*(cureye.vpt[i] + vn->vp[i]);
213 gwlarson 3.30 cureye.rng = 0.5*(cureye.rng + eravg + d);
214 gwlarson 3.26 }
215 gwlarson 3.32 return(slist);
216 gregl 3.4 }
217    
218    
219 schorsch 3.37 extern int
220     beam_sync( /* update beam list on server */
221     int all
222     )
223 gregl 3.1 {
224 gwlarson 3.26 /* set new eye position */
225     serv_request(DR_VIEWPOINT, sizeof(VIEWPOINT), (char *)&cureye);
226 gwlarson 3.24 /* sort list (put orphans at end) */
227     cbeamsort(all < 0);
228 gwlarson 3.26 /* send beam request */
229 gwlarson 3.32 if (all) {
230     if (ncbeams > 0)
231     serv_request(DR_NEWSET,
232 schorsch 3.37 ncbeams*sizeof(PACKHEAD), (char *)cbeam);
233 gwlarson 3.32 } else {
234     if (ncbeams+xcbeams > 0)
235     serv_request(DR_ADJSET,
236 schorsch 3.37 (ncbeams+xcbeams)*sizeof(PACKHEAD), (char *)cbeam);
237 gwlarson 3.32 }
238 gregl 3.1 xcbeams = 0; /* truncate our list */
239 gwlarson 3.21 return(ncbeams);
240 gwlarson 3.32 }
241    
242    
243 schorsch 3.37 extern void
244     gridlines( /* run through holodeck section grid lines */
245     void (*f)(FVECT wp[2])
246     )
247 gwlarson 3.32 {
248     register int hd, w, i;
249     int g0, g1;
250     FVECT wp[2], mov;
251     double d;
252     /* do each wall on each section */
253     for (hd = 0; hdlist[hd] != NULL; hd++)
254     for (w = 0; w < 6; w++) {
255     g0 = hdwg0[w];
256     g1 = hdwg1[w];
257     d = 1.0/hdlist[hd]->grid[g0];
258     mov[0] = d * hdlist[hd]->xv[g0][0];
259     mov[1] = d * hdlist[hd]->xv[g0][1];
260     mov[2] = d * hdlist[hd]->xv[g0][2];
261     if (w & 1) {
262     VSUM(wp[0], hdlist[hd]->orig,
263     hdlist[hd]->xv[w>>1], 1.);
264     VSUM(wp[0], wp[0], mov, 1.);
265     } else
266     VCOPY(wp[0], hdlist[hd]->orig);
267     VSUM(wp[1], wp[0], hdlist[hd]->xv[g1], 1.);
268     for (i = hdlist[hd]->grid[g0]; ; ) { /* g0 lines */
269     (*f)(wp);
270     if (!--i) break;
271     wp[0][0] += mov[0]; wp[0][1] += mov[1];
272     wp[0][2] += mov[2]; wp[1][0] += mov[0];
273     wp[1][1] += mov[1]; wp[1][2] += mov[2];
274     }
275     d = 1.0/hdlist[hd]->grid[g1];
276     mov[0] = d * hdlist[hd]->xv[g1][0];
277     mov[1] = d * hdlist[hd]->xv[g1][1];
278     mov[2] = d * hdlist[hd]->xv[g1][2];
279     if (w & 1)
280     VSUM(wp[0], hdlist[hd]->orig,
281     hdlist[hd]->xv[w>>1], 1.);
282     else
283     VSUM(wp[0], hdlist[hd]->orig, mov, 1.);
284     VSUM(wp[1], wp[0], hdlist[hd]->xv[g0], 1.);
285     for (i = hdlist[hd]->grid[g1]; ; ) { /* g1 lines */
286     (*f)(wp);
287     if (!--i) break;
288     wp[0][0] += mov[0]; wp[0][1] += mov[1];
289     wp[0][2] += mov[2]; wp[1][0] += mov[0];
290     wp[1][1] += mov[1]; wp[1][2] += mov[2];
291     }
292     }
293 gregl 3.1 }