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

Comparing ray/src/hd/rholo3.c (file contents):
Revision 3.13 by gregl, Mon Dec 15 20:44:28 1997 UTC vs.
Revision 3.22 by gwlarson, Wed Jul 8 17:59:58 1998 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
1   /*
2   * Routines for tracking beam compuatations
3   */
# Line 27 | Line 21 | register PACKHEAD      *b0, *b1;
21   }
22  
23  
24 + int
25 + dispbeam(b, hp, bi)                     /* display a holodeck beam */
26 + register BEAM   *b;
27 + HOLO    *hp;
28 + int     bi;
29 + {
30 +        static int      n = 0;
31 +        static PACKHEAD *p = NULL;
32 +
33 +        if (b == NULL)
34 +                return;
35 +        if (b->nrm > n) {               /* (re)allocate packet holder */
36 +                n = b->nrm;
37 +                if (p == NULL) p = (PACKHEAD *)malloc(packsiz(n));
38 +                else p = (PACKHEAD *)realloc((char *)p, packsiz(n));
39 +                if (p == NULL)
40 +                        error(SYSTEM, "out of memory in dispbeam");
41 +        }
42 +                                        /* assign packet fields */
43 +        bcopy((char *)hdbray(b), (char *)packra(p), b->nrm*sizeof(RAYVAL));
44 +        p->nr = p->nc = b->nrm;
45 +        for (p->hd = 0; hdlist[p->hd] != hp; p->hd++)
46 +                if (hdlist[p->hd] == NULL)
47 +                        error(CONSISTENCY, "unregistered holodeck in dispbeam");
48 +        p->bi = bi;
49 +        disp_packet(p);                 /* display it */
50 + }
51 +
52 +
53   bundle_set(op, clist, nents)    /* bundle set operation */
54   int     op;
55 < PACKHEAD        *clist;
55 > register PACKHEAD       *clist;
56   int     nents;
57   {
58 <        BEAM    *b;
59 <        PACKHEAD        *p;
58 >        int     oldnr;
59 >        register HDBEAMI        *hb;
60          register int    i, n;
61 <
61 >                                /* look for common members */
62 >        for (n = 0; n < nents; n++) {
63 >                for (i = 0; i < complen; i++)
64 >                        if (clist[n].bi == complist[i].bi &&
65 >                                        clist[n].hd == complist[i].hd) {
66 >                                oldnr = complist[i].nr;
67 >                                clist[n].nc = complist[i].nc;
68 >                                switch (op) {
69 >                                case BS_ADD:            /* add to count */
70 >                                        complist[i].nr += clist[n].nr;
71 >                                        clist[n].nr = 0;
72 >                                        break;
73 >                                case BS_ADJ:            /* reset count */
74 >                                        complist[i].nr = clist[n].nr;
75 >                                        clist[n].nr = 0;
76 >                                        break;
77 >                                case BS_DEL:            /* delete count */
78 >                                        if (clist[n].nr == 0 ||
79 >                                                clist[n].nr >= complist[i].nr)
80 >                                                complist[i].nr = 0;
81 >                                        else
82 >                                                complist[i].nr -= clist[n].nr;
83 >                                        break;
84 >                                }
85 >                                if (complist[i].nr != oldnr)
86 >                                        lastin = -1;    /* flag sort */
87 >                                break;
88 >                        }
89 >                if (i >= complen)
90 >                        clist[n].nc = bnrays(hdlist[clist[n].hd], clist[n].bi);
91 >        }
92 >                                /* complete list operations */
93          switch (op) {
94          case BS_NEW:                    /* new computation set */
95 <                if (complen)
95 >                listpos = 0; lastin = -1;
96 >                if (complen)            /* free old list */
97                          free((char *)complist);
98 <                if (nents <= 0) {
99 <                        complist = NULL;
45 <                        listpos = complen = 0;
46 <                        lastin = -1;
98 >                complist = NULL;
99 >                if (!(complen = nents))
100                          return;
48                }
101                  complist = (PACKHEAD *)malloc(nents*sizeof(PACKHEAD));
102                  if (complist == NULL)
103                          goto memerr;
104                  bcopy((char *)clist, (char *)complist, nents*sizeof(PACKHEAD));
53                complen = nents;        /* finish initialization below */
105                  break;
106          case BS_ADD:                    /* add to computation set */
107          case BS_ADJ:                    /* adjust set quantities */
108                  if (nents <= 0)
109                          return;
110 <                                        /* merge any common members */
60 <                for (n = 0; n < nents; n++) {
61 <                        for (i = 0; i < complen; i++)
62 <                                if (clist[n].bi == complist[i].bi &&
63 <                                                clist[n].hd == complist[i].hd) {
64 <                                        int     oldnr = complist[i].nr;
65 <                                        if (op == BS_ADD)
66 <                                                complist[i].nr += clist[n].nr;
67 <                                        else /* op == BS_ADJ */
68 <                                                complist[i].nr = clist[n].nr;
69 <                                        clist[n].nr = 0;
70 <                                        clist[n].nc = complist[i].nc;
71 <                                        if (complist[i].nr != oldnr)
72 <                                                lastin = -1;    /* flag sort */
73 <                                        break;
74 <                                }
75 <                        if (i >= complen)
76 <                                clist[n].nc = bnrays(hdlist[clist[n].hd],
77 <                                                clist[n].bi);
78 <                }
79 <                                        /* sort updated list */
80 <                sortcomplist();
81 <                                        /* sort new entries */
110 >                sortcomplist();         /* sort updated list & new entries */
111                  qsort((char *)clist, nents, sizeof(PACKHEAD), beamcmp);
112                                          /* what can't we satisfy? */
113                  for (n = 0; n < nents && clist[n].nr > clist[n].nc; n++)
114                          ;
115 <                if (op == BS_ADJ)
116 <                        nents = n;
115 >                if (op == BS_ADJ) {     /* don't regenerate adjusted beams */
116 >                        for (i = n; i < nents && clist[i].nr > 0; i++)
117 >                                ;
118 >                        nents = i;
119 >                }
120                  if (n) {                /* allocate space for merged list */
121                          PACKHEAD        *newlist;
122                          newlist = (PACKHEAD *)malloc(
# Line 102 | Line 134 | int    nents;
134                  lastin = complen-1;     /* list is now sorted */
135                  break;
136          case BS_DEL:                    /* delete from computation set */
137 <                if (nents <= 0)
106 <                        return;
107 <                                        /* find each member */
108 <                for (i = 0; i < complen; i++)
109 <                        for (n = 0; n < nents; n++)
110 <                                if (clist[n].bi == complist[i].bi &&
111 <                                                clist[n].hd == complist[i].hd) {
112 <                                        if (clist[n].nr == 0 ||
113 <                                                clist[n].nr >= complist[i].nr)
114 <                                                complist[i].nr = 0;
115 <                                        else
116 <                                                complist[i].nr -= clist[n].nr;
117 <                                        lastin = -1;    /* flag full sort */
118 <                                        break;
119 <                                }
120 <                return;                 /* no display */
137 >                return;                 /* already done */
138          default:
139                  error(CONSISTENCY, "bundle_set called with unknown operation");
140          }
141 <        if (outdev == NULL)
141 >        if (outdev == NULL)             /* nothing to display? */
142                  return;
143 <        n = 32*RPACKSIZ;                /* allocate packet holder */
144 <        p = (PACKHEAD *)malloc(packsiz(n));
145 <        if (p == NULL)
146 <                goto memerr;
147 <                                        /* display what we have */
131 <        for (i = 0; i < nents; i++)
132 <                if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) {
133 <                        if (b->nrm > n) {
134 <                                n = b->nrm;
135 <                                p = (PACKHEAD *)realloc((char *)p, packsiz(n));
136 <                                if (p == NULL)
137 <                                        goto memerr;
138 <                        }
139 <                        bcopy((char *)hdbray(b), (char *)packra(p),
140 <                                        b->nrm*sizeof(RAYVAL));
141 <                        p->hd = clist[i].hd;
142 <                        p->bi = clist[i].bi;
143 <                        p->nr = p->nc = b->nrm;
144 <                        disp_packet(p);
145 <                }
146 <        free((char *)p);                /* clean up */
147 <        if (op == BS_NEW) {
148 <                done_packets(flush_queue());    /* empty queue, so we can... */
149 <                for (i = 0; i < complen; i++)   /* ...get number computed */
150 <                        complist[i].nc = bnrays(hdlist[complist[i].hd],
151 <                                                complist[i].bi);
152 <                listpos = 0;
153 <                lastin = -1;            /* flag for initial sort */
143 >                                        /* load and display beams we have */
144 >        hb = (HDBEAMI *)malloc(nents*sizeof(HDBEAMI));
145 >        for (i = 0; i < nents; i++) {
146 >                hb[i].h = hdlist[clist[i].hd];
147 >                hb[i].b = clist[i].bi;
148          }
149 +        hdloadbeams(hb, nents, dispbeam);
150 +        free((char *)hb);
151          return;
152   memerr:
153          error(SYSTEM, "out of memory in bundle_set");
# Line 176 | Line 172 | int    bi;
172                  VSUM(edgeA, cp[1], cp[0], -1.0);
173                  VSUM(edgeB, cp[3], cp[1], -1.0);
174                  fcross(crossp[i], edgeA, edgeB);
179                VSUM(edgeA, cp[2], cp[3], -1.0);
180                VSUM(edgeB, cp[0], cp[2], -1.0);
181                fcross(v, edgeA, edgeB);
182                VSUM(crossp[i], crossp[i], v, 1.0);
175                                          /* compute center */
176                  cent[i][0] = 0.5*(cp[0][0] + cp[2][0]);
177                  cent[i][1] = 0.5*(cp[0][1] + cp[2][1]);
# Line 188 | Line 180 | int    bi;
180                                          /* compute difference vector */
181          VSUM(diffv, cent[1], cent[0], -1.0);
182          for (i = 0; i < 2; i++) {       /* compute volume contributions */
183 <                vol[i] = 0.25*DOT(crossp[i], diffv);
183 >                vol[i] = 0.5*DOT(crossp[i], diffv);
184                  if (vol[i] < 0.) vol[i] = -vol[i];
185          }
186          return(vol[0] + vol[1]);        /* return total volume */
# Line 201 | Line 193 | init_global()                  /* initialize global ray computation *
193          double  frac;
194          int     i;
195          register int    j, k;
196 <                                        /* free old list */
197 <        if (complen > 0)
196 >                                        /* free old list and empty queue */
197 >        if (complen > 0) {
198                  free((char *)complist);
199 +                done_packets(flush_queue());
200 +        }
201                                          /* allocate beam list */
202          complen = 0;
203          for (j = 0; hdlist[j] != NULL; j++)
# Line 214 | Line 208 | init_global()                  /* initialize global ray computation *
208                                          /* compute beam weights */
209          k = 0;
210          for (j = 0; hdlist[j] != NULL; j++) {
211 <                frac = 512. * hdlist[j]->wg[0] *
212 <                                hdlist[j]->wg[1] * hdlist[j]->wg[2];
211 >                frac = 512. * VLEN(hdlist[j]->wg[0]) *
212 >                                VLEN(hdlist[j]->wg[1]) *
213 >                                VLEN(hdlist[j]->wg[2]);
214                  for (i = nbeams(hdlist[j]); i > 0; i--) {
215                          complist[k].hd = j;
216                          complist[k].bi = i;
217                          complist[k].nr = frac*beamvolume(hdlist[j], i) + 0.5;
218 +                        complist[k].nc = bnrays(hdlist[j], i);
219                          wtotal += complist[k++].nr;
220                  }
221          }
# Line 230 | Line 226 | init_global()                  /* initialize global ray computation *
226                  frac = 1024.*1024.*16384. / (wtotal*sizeof(RAYVAL));
227          while (k--)
228                  complist[k].nr = frac * complist[k].nr;
229 <        listpos = 0; lastin = -1;       /* flag initial sort */
229 >        listpos = 0; lastin = -1;       /* perform initial sort */
230 >        sortcomplist();
231   }
232  
233  
234   mergeclists(cdest, cl1, n1, cl2, n2)    /* merge two sorted lists */
235 < PACKHEAD        *cdest;
236 < PACKHEAD        *cl1, *cl2;
235 > register PACKHEAD       *cdest;
236 > register PACKHEAD       *cl1, *cl2;
237   int     n1, n2;
238   {
239 <        int     cmp;
239 >        register int    cmp;
240  
241          while (n1 | n2) {
242                  if (!n1) cmp = 1;
# Line 304 | Line 301 | sortcomplist()                 /* fix our list order */
301   * list and start again from the beginning.  Since
302   * a merge sort is used, the sorting costs are minimal.
303   */
304 < next_packet(p)                  /* prepare packet for computation */
304 > next_packet(p, n)               /* prepare packet for computation */
305   register PACKET *p;
306 + int     n;
307   {
308          register int    i;
309  
# Line 319 | Line 317 | register PACKET        *p;
317          p->nr = complist[listpos].nr - p->nc;
318          if (p->nr <= 0)
319                  return(0);
320 <        if (p->nr > RPACKSIZ)
321 <                p->nr = RPACKSIZ;
320 > #ifdef DEBUG
321 >        if (n < 1 | n > RPACKSIZ)
322 >                error(CONSISTENCY, "next_packet called with bad n value");
323 > #endif
324 >        if (p->nr > n)
325 >                p->nr = n;
326          complist[listpos].nc += p->nr;  /* find where this one would go */
327          while (lastin > listpos &&
328                          beamcmp(complist+lastin, complist+listpos) > 0)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines