Back to index

radiance  4R0+20100331
rhdisp3.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: rhdisp3.c,v 3.16 2004/06/08 19:48:30 greg Exp $";
00003 #endif
00004 /*
00005  * Holodeck beam support for display process
00006  */
00007 
00008 #include "rholo.h"
00009 #include "rhdisp.h"
00010 
00011 struct cellist {
00012        GCOORD *cl;
00013        int    n;
00014 };
00015 
00016 
00017 int
00018 npixels(vp, hr, vr, hp, bi) /* compute appropriate nrays to evaluate */
00019 register VIEW *vp;
00020 int    hr, vr;
00021 HOLO   *hp;
00022 int    bi;
00023 {
00024        VIEW   vrev;
00025        GCOORD gc[2];
00026        FVECT  cp[4], ip[4], pf, pb;
00027        double af, ab, sf2, sb2, dfb2, df2, db2, penalty;
00028        register int  i;
00029                                    /* special case */
00030        if (hr <= 0 | vr <= 0)
00031               return(0);
00032                                    /* compute cell corners in image */
00033        if (!hdbcoord(gc, hp, bi))
00034               error(CONSISTENCY, "bad beam index in npixels");
00035        hdcell(cp, hp, gc+1);              /* find cell on front image */
00036        for (i = 3; i--; )          /* compute front center */
00037               pf[i] = 0.5*(cp[0][i] + cp[2][i]);
00038        sf2 = 0.25*dist2(cp[0], cp[2]);    /* compute half diagonal length */
00039        for (i = 0; i < 4; i++) {   /* compute visible quad */
00040               viewloc(ip[i], vp, cp[i]);
00041               if (ip[i][2] < 0.) {
00042                      af = 0;
00043                      goto getback;
00044               }
00045               ip[i][0] *= (double)hr;     /* scale by resolution */
00046               ip[i][1] *= (double)vr;
00047        }
00048                                    /* compute front area */
00049        af = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) -
00050               (ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]);
00051        af += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) -
00052               (ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]);
00053        af *= af >= 0 ? 0.5 : -0.5;
00054 getback:
00055        vrev = *vp;          /* compute reverse view */
00056        for (i = 0; i < 3; i++) {
00057               vrev.vdir[i] = -vp->vdir[i];
00058               vrev.vup[i] = -vp->vup[i];
00059               vrev.hvec[i] = -vp->hvec[i];
00060               vrev.vvec[i] = -vp->vvec[i];
00061        }
00062        hdcell(cp, hp, gc);         /* find cell on back image */
00063        for (i = 3; i--; )          /* compute rear center */
00064               pb[i] = 0.5*(cp[0][i] + cp[2][i]);
00065        sb2 = 0.25*dist2(cp[0], cp[2]);    /* compute half diagonal length */
00066        for (i = 0; i < 4; i++) {   /* compute visible quad */
00067               viewloc(ip[i], &vrev, cp[i]);
00068               if (ip[i][2] < 0.) {
00069                      ab = 0;
00070                      goto finish;
00071               }
00072               ip[i][0] *= (double)hr;     /* scale by resolution */
00073               ip[i][1] *= (double)vr;
00074        }
00075                                    /* compute back area */
00076        ab = (ip[1][0]-ip[0][0])*(ip[2][1]-ip[0][1]) -
00077               (ip[2][0]-ip[0][0])*(ip[1][1]-ip[0][1]);
00078        ab += (ip[2][0]-ip[3][0])*(ip[1][1]-ip[3][1]) -
00079               (ip[1][0]-ip[3][0])*(ip[2][1]-ip[3][1]);
00080        ab *= ab >= 0 ? 0.5 : -0.5;
00081 finish:              /* compute penalty based on dist. sightline - viewpoint */
00082        df2 = dist2(vp->vp, pf);
00083        db2 = dist2(vp->vp, pb);
00084        dfb2 = dist2(pf, pb);
00085        penalty = dfb2 + df2 - db2;
00086        penalty = df2 - 0.25*penalty*penalty/dfb2;
00087        if (df2 > db2)       penalty /= df2 <= dfb2 ? sb2 : sb2*df2/dfb2;
00088        else          penalty /= db2 <= dfb2 ? sf2 : sf2*db2/dfb2;
00089        if (penalty < 1.) penalty = 1.;
00090                                    /* round off smaller non-zero area */
00091        if (ab <= FTINY || (af > FTINY && af <= ab))
00092               return((int)(af/penalty + 0.5));
00093        return((int)(ab/penalty + 0.5));
00094 }
00095 
00096 
00097 /*
00098  * The ray directions that define the pyramid in visit_cells() needn't
00099  * be normalized, but they must be given in clockwise order as seen
00100  * from the pyramid's apex (origin).
00101  * If no cell centers fall within the domain, the closest cell is visited.
00102  */
00103 int
00104 visit_cells(orig, pyrd, hp, vf, dp)       /* visit cells within a pyramid */
00105 FVECT  orig, pyrd[4];              /* pyramid ray directions in clockwise order */
00106 register HOLO *hp;
00107 int    (*vf)();
00108 char   *dp;
00109 {
00110        int    ncalls = 0, n = 0;
00111        int    inflags = 0;
00112        FVECT  gp, pn[4], lo, ld;
00113        double po[4], lbeg, lend, d, t;
00114        GCOORD gc, gc2[2];
00115        register int  i;
00116                                    /* figure out whose side we're on */
00117        hdgrid(gp, hp, orig);
00118        for (i = 0; i < 3; i++) {
00119               inflags |= (gp[i] > FTINY) << (i<<1);
00120               inflags |= (gp[i] < hp->grid[i]-FTINY) << (i<<1 | 1);
00121        }
00122                                    /* compute pyramid planes */
00123        for (i = 0; i < 4; i++) {
00124               fcross(pn[i], pyrd[i], pyrd[(i+1)&03]);
00125               po[i] = DOT(pn[i], orig);
00126        }
00127                                    /* traverse each wall */
00128        for (gc.w = 0; gc.w < 6; gc.w++) {
00129               if (!(inflags & 1<<gc.w))   /* origin on wrong side */
00130                      continue;
00131                                    /* scanline algorithm */
00132               for (gc.i[1] = hp->grid[hdwg1[gc.w]]; gc.i[1]--; ) {
00133                                           /* compute scanline */
00134                      gp[gc.w>>1] = gc.w&1 ? hp->grid[gc.w>>1] : 0;
00135                      gp[hdwg0[gc.w]] = 0;
00136                      gp[hdwg1[gc.w]] = gc.i[1] + 0.5;
00137                      hdworld(lo, hp, gp);
00138                      gp[hdwg0[gc.w]] = 1;
00139                      hdworld(ld, hp, gp);
00140                      ld[0] -= lo[0]; ld[1] -= lo[1]; ld[2] -= lo[2];
00141                                           /* find scanline limits */
00142                      lbeg = 0; lend = hp->grid[hdwg0[gc.w]];
00143                      for (i = 0; i < 4; i++) {
00144                             t = DOT(pn[i], lo) - po[i];
00145                             d = -DOT(pn[i], ld);
00146                             if (d > FTINY) {            /* <- plane */
00147                                    if ((t /= d) < lend)
00148                                           lend = t;
00149                             } else if (d < -FTINY) {    /* plane -> */
00150                                    if ((t /= d) > lbeg)
00151                                           lbeg = t;
00152                             } else if (t < 0) {         /* outside */
00153                                    lend = -1;
00154                                    break;
00155                             }
00156                      }
00157                      if (lbeg >= lend)
00158                             continue;
00159                      i = lend + .5;              /* visit cells on this scan */
00160                      for (gc.i[0] = lbeg + .5; gc.i[0] < i; gc.i[0]++) {
00161                             n += (*vf)(&gc, dp);
00162                             ncalls++;
00163                      }
00164               }
00165        }
00166        if (ncalls)                 /* got one at least */
00167               return(n);
00168                                    /* else find closest cell */
00169        VSUM(ld, pyrd[0], pyrd[1], 1.);
00170        VSUM(ld, ld, pyrd[2], 1.);
00171        VSUM(ld, ld, pyrd[3], 1.);
00172 #if 0
00173        if (normalize(ld) == 0.0)   /* technically not necessary */
00174               return(0);
00175 #endif
00176        d = hdinter(gc2, NULL, &t, hp, orig, ld);
00177        if (d >= FHUGE || t <= 0.)
00178               return(0);
00179        return((*vf)(gc2+1, dp));   /* visit it */
00180 }
00181 
00182 
00183 sect_behind(hp, vp)         /* check if section is "behind" viewpoint */
00184 register HOLO *hp;
00185 register VIEW *vp;
00186 {
00187        FVECT  hcent;
00188                                    /* compute holodeck section center */
00189        VSUM(hcent, hp->orig, hp->xv[0], 0.5);
00190        VSUM(hcent, hcent, hp->xv[1], 0.5);
00191        VSUM(hcent, hcent, hp->xv[2], 0.5);
00192                                    /* behind if center is behind */
00193        return(DOT(vp->vdir,hcent) < DOT(vp->vdir,vp->vp));
00194 }
00195 
00196 
00197 viewpyramid(org, dir, hp, vp)      /* compute view pyramid */
00198 FVECT  org, dir[4];
00199 HOLO   *hp;
00200 VIEW   *vp;
00201 {
00202        register int  i;
00203                                    /* check view type */
00204        if (vp->type == VT_PAR)
00205               return(0);
00206                                    /* in front or behind? */
00207        if (!sect_behind(hp, vp)) {
00208               if (viewray(org, dir[0], vp, 0., 0.) < -FTINY)
00209                      return(0);
00210               if (viewray(org, dir[1], vp, 0., 1.) < -FTINY)
00211                      return(0);
00212               if (viewray(org, dir[2], vp, 1., 1.) < -FTINY)
00213                      return(0);
00214               if (viewray(org, dir[3], vp, 1., 0.) < -FTINY)
00215                      return(0);
00216               return(1);
00217        }                           /* reverse pyramid */
00218        if (viewray(org, dir[3], vp, 0., 0.) < -FTINY)
00219               return(0);
00220        if (viewray(org, dir[2], vp, 0., 1.) < -FTINY)
00221               return(0);
00222        if (viewray(org, dir[1], vp, 1., 1.) < -FTINY)
00223               return(0);
00224        if (viewray(org, dir[0], vp, 1., 0.) < -FTINY)
00225               return(0);
00226        for (i = 0; i < 3; i++) {
00227               dir[0][i] = -dir[0][i];
00228               dir[1][i] = -dir[1][i];
00229               dir[2][i] = -dir[2][i];
00230               dir[3][i] = -dir[3][i];
00231        }
00232        return(-1);
00233 }
00234 
00235 
00236 int
00237 addcell(gcp, cl)            /* add a cell to a list */
00238 GCOORD *gcp;
00239 register struct cellist     *cl;
00240 {
00241        *(cl->cl+cl->n) = *gcp;
00242        cl->n++;
00243        return(1);
00244 }
00245 
00246 
00247 int
00248 cellcmp(gcp1, gcp2)         /* visit_cells() cell ordering */
00249 register GCOORD      *gcp1, *gcp2;
00250 {
00251        register int  c;
00252 
00253        if ((c = gcp1->w - gcp2->w))
00254               return(c);
00255        if ((c = gcp2->i[1] - gcp1->i[1])) /* wg1 is reverse-ordered */
00256               return(c);
00257        return(gcp1->i[0] - gcp2->i[0]);
00258 }
00259 
00260 
00261 GCOORD *
00262 getviewcells(np, hp, vp)    /* get ordered cell list for section view */
00263 int    *np;          /* returned number of cells (negative if reversed) */
00264 register HOLO *hp;
00265 VIEW   *vp;
00266 {
00267        FVECT  org, dir[4];
00268        int    orient;
00269        struct cellist       cl;
00270                                    /* compute view pyramid */
00271        *np = 0;
00272        orient = viewpyramid(org, dir, hp, vp);
00273        if (!orient)
00274               return(NULL);
00275                                    /* allocate enough list space */
00276        cl.n = 2*(    hp->grid[0]*hp->grid[1] +
00277                      hp->grid[0]*hp->grid[2] +
00278                      hp->grid[1]*hp->grid[2]     );
00279        cl.cl = (GCOORD *)malloc(cl.n*sizeof(GCOORD));
00280        if (cl.cl == NULL)
00281               goto memerr;
00282        cl.n = 0;                   /* add cells within pyramid */
00283        visit_cells(org, dir, hp, addcell, (char *)&cl);
00284        if (!cl.n) {
00285               free((void *)cl.cl);
00286               return(NULL);
00287        }
00288        *np = cl.n * orient;
00289 #if 0
00290        /* We're just going to free this memory in a moment, and list is
00291         * sorted automatically by visit_cells(), so we don't need this.
00292         */
00293                                    /* optimize memory use */
00294        cl.cl = (GCOORD *)realloc((void *)cl.cl, cl.n*sizeof(GCOORD));
00295        if (cl.cl == NULL)
00296               goto memerr;
00297                                    /* sort the list */
00298        qsort((char *)cl.cl, cl.n, sizeof(GCOORD), cellcmp);
00299 #endif
00300        return(cl.cl);
00301 memerr:
00302        error(SYSTEM, "out of memory in getviewcells");
00303 }
00304 
00305 
00306 extern void
00307 gridlines(                  /* run through holodeck section grid lines */
00308        void   (*f)(FVECT wp[2])
00309 )
00310 {
00311        register int  hd, w, i;
00312        int    g0, g1;
00313        FVECT  wp[2], mov;
00314        double d;
00315                                    /* do each wall on each section */
00316        for (hd = 0; hdlist[hd] != NULL; hd++)
00317               for (w = 0; w < 6; w++) {
00318                      g0 = hdwg0[w];
00319                      g1 = hdwg1[w];
00320                      d = 1.0/hdlist[hd]->grid[g0];
00321                      mov[0] = d * hdlist[hd]->xv[g0][0];
00322                      mov[1] = d * hdlist[hd]->xv[g0][1];
00323                      mov[2] = d * hdlist[hd]->xv[g0][2];
00324                      if (w & 1) {
00325                             VSUM(wp[0], hdlist[hd]->orig,
00326                                           hdlist[hd]->xv[w>>1], 1.);
00327                             VSUM(wp[0], wp[0], mov, 1.);
00328                      } else
00329                             VCOPY(wp[0], hdlist[hd]->orig);
00330                      VSUM(wp[1], wp[0], hdlist[hd]->xv[g1], 1.);
00331                      for (i = hdlist[hd]->grid[g0]; ; ) {      /* g0 lines */
00332                             (*f)(wp);
00333                             if (!--i) break;
00334                             wp[0][0] += mov[0]; wp[0][1] += mov[1];
00335                             wp[0][2] += mov[2]; wp[1][0] += mov[0];
00336                             wp[1][1] += mov[1]; wp[1][2] += mov[2];
00337                      }
00338                      d = 1.0/hdlist[hd]->grid[g1];
00339                      mov[0] = d * hdlist[hd]->xv[g1][0];
00340                      mov[1] = d * hdlist[hd]->xv[g1][1];
00341                      mov[2] = d * hdlist[hd]->xv[g1][2];
00342                      if (w & 1)
00343                             VSUM(wp[0], hdlist[hd]->orig,
00344                                           hdlist[hd]->xv[w>>1], 1.);
00345                      else
00346                             VSUM(wp[0], hdlist[hd]->orig, mov, 1.);
00347                      VSUM(wp[1], wp[0], hdlist[hd]->xv[g0], 1.);
00348                      for (i = hdlist[hd]->grid[g1]; ; ) {      /* g1 lines */
00349                             (*f)(wp);
00350                             if (!--i) break;
00351                             wp[0][0] += mov[0]; wp[0][1] += mov[1];
00352                             wp[0][2] += mov[2]; wp[1][0] += mov[0];
00353                             wp[1][1] += mov[1]; wp[1][2] += mov[2];
00354                      }
00355               }
00356 }