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