Back to index

radiance  4R0+20100331
srcdraw.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: srcdraw.c,v 2.16 2005/04/19 01:15:07 greg Exp $";
00003 #endif
00004 /*
00005  * Draw small sources into image in case we missed them.
00006  *
00007  *  External symbols declared in ray.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include  "ray.h"
00013 #include  "view.h"
00014 #include  "otypes.h"
00015 #include  "source.h"
00016 
00017 
00018 #define       CLIP_ABOVE    1
00019 #define CLIP_BELOW   2
00020 #define CLIP_RIGHT   3
00021 #define CLIP_LEFT    4
00022 
00023 #define MAXVERT             10
00024 
00025 typedef struct splist {
00026        struct splist *next;               /* next source in list */
00027        int    sn;                         /* source number */
00028        short  nv;                         /* number of vertices */
00029        RREAL  vl[3][2];                   /* vertex array (last) */
00030 } SPLIST;                          /* source polygon list */
00031 
00032 extern VIEW   ourview;             /* our view parameters */
00033 extern int    hres, vres;          /* our image resolution */
00034 static SPLIST *sphead = NULL;             /* our list of source polys */
00035 
00036 static int inregion(RREAL p[2], double cv, int crit);
00037 static void clipregion(RREAL a[2], RREAL b[2], double cv, int crit, RREAL r[2]);
00038 static int hp_clip_poly(RREAL vl[][2], int nv, double cv, int crit,
00039               RREAL vlo[][2]);
00040 static int box_clip_poly(RREAL vl[MAXVERT][2], int nv,
00041               double xl, double xr, double yb, double ya, RREAL vlo[MAXVERT][2]);
00042 static double minw2(RREAL vl[][2], int nv, double ar2);
00043 static void convex_center(RREAL vl[][2], int nv, RREAL cv[2]);
00044 static double poly_area(RREAL vl[][2], int nv);
00045 static int convex_hull(RREAL vl[][2], int nv, RREAL vlo[][2]);
00046 static void spinsert(int sn, RREAL vl[][2], int nv);
00047 static int sourcepoly(int sn, RREAL sp[MAXVERT][2]);
00048 
00049 
00050 static int
00051 inregion(                   /* check if vertex is in region */
00052        RREAL  p[2],
00053        double cv,
00054        int    crit
00055 )
00056 {
00057        switch (crit) {
00058        case CLIP_ABOVE:
00059               return(p[1] < cv);
00060        case CLIP_BELOW:
00061               return(p[1] >= cv);
00062        case CLIP_RIGHT:
00063               return(p[0] < cv);
00064        case CLIP_LEFT:
00065               return(p[0] >= cv);
00066        }
00067        return(-1);
00068 }
00069 
00070 
00071 static void
00072 clipregion(          /* find intersection with boundary */
00073        register RREAL       a[2],
00074        register RREAL       b[2],
00075        double cv,
00076        int    crit,
00077        RREAL  r[2]   /* return value */
00078 )
00079 {
00080        switch (crit) {
00081        case CLIP_ABOVE:
00082        case CLIP_BELOW:
00083               r[1] = cv;
00084               r[0] = a[0] + (cv-a[1])/(b[1]-a[1])*(b[0]-a[0]);
00085               return;
00086        case CLIP_RIGHT:
00087        case CLIP_LEFT:
00088               r[0] = cv;
00089               r[1] = a[1] + (cv-a[0])/(b[0]-a[0])*(b[1]-a[1]);
00090               return;
00091        }
00092 }
00093 
00094 
00095 static int
00096 hp_clip_poly( /* clip polygon to half-plane */
00097        RREAL  vl[][2],
00098        int    nv,
00099        double cv,
00100        int    crit,
00101        RREAL  vlo[][2]      /* return value */
00102 )
00103 {
00104        RREAL  *s, *p;
00105        register int  j, nvo;
00106 
00107        s = vl[nv-1];
00108        nvo = 0;
00109        for (j = 0; j < nv; j++) {
00110               p = vl[j];
00111               if (inregion(p, cv, crit)) {
00112                      if (!inregion(s, cv, crit))
00113                             clipregion(s, p, cv, crit, vlo[nvo++]);
00114                      vlo[nvo][0] = p[0]; vlo[nvo++][1] = p[1];
00115               } else if (inregion(s, cv, crit))
00116                      clipregion(s, p, cv, crit, vlo[nvo++]);
00117               s = p;
00118        }
00119        return(nvo);
00120 }
00121 
00122 
00123 static int
00124 box_clip_poly(       /* clip polygon to box */
00125        RREAL  vl[MAXVERT][2],
00126        int    nv,
00127        double xl,
00128        double xr,
00129        double yb,
00130        double ya,
00131        RREAL  vlo[MAXVERT][2]      /* return value */
00132 )
00133 {
00134        RREAL  vlt[MAXVERT][2];
00135        int    nvt, nvo;
00136 
00137        nvt = hp_clip_poly(vl, nv, yb, CLIP_BELOW, vlt);
00138        nvo = hp_clip_poly(vlt, nvt, ya, CLIP_ABOVE, vlo);
00139        nvt = hp_clip_poly(vlo, nvo, xl, CLIP_LEFT, vlt);
00140        nvo = hp_clip_poly(vlt, nvt, xr, CLIP_RIGHT, vlo);
00141 
00142        return(nvo);
00143 }
00144 
00145 
00146 static double
00147 minw2(               /* compute square of minimum width */
00148        RREAL  vl[][2],
00149        int    nv,
00150        double ar2
00151 )
00152 {
00153        double d2, w2, w2min, w2max;
00154        register RREAL       *p0, *p1, *p2;
00155        int    i, j;
00156                             /* find minimum for all widths */
00157        w2min = FHUGE;
00158        p0 = vl[nv-1];
00159        for (i = 0; i < nv; i++) {  /* for each edge */
00160               p1 = vl[i];
00161               d2 = (p1[0]-p0[0])*(p1[0]-p0[0]) +
00162                             (p1[1]-p0[1])*(p1[1]-p0[1])*ar2;
00163               w2max = 0.;          /* find maximum for this side */
00164               for (j = 1; j < nv-1; j++) {
00165                      p2 = vl[(i+j)%nv];
00166                      w2 = (p1[0]-p0[0])*(p2[1]-p0[1]) -
00167                                    (p1[1]-p0[1])*(p2[0]-p0[0]);
00168                      w2 = w2*w2*ar2/d2;   /* triangle height squared */
00169                      if (w2 > w2max)
00170                             w2max = w2;
00171               }
00172               if (w2max < w2min)   /* global min. based on local max.'s */
00173                      w2min = w2max;
00174               p0 = p1;
00175        }
00176        return(w2min);
00177 }
00178 
00179 
00180 static void
00181 convex_center(              /* compute center of convex polygon */
00182        register RREAL       vl[][2],
00183        int    nv,
00184        RREAL  cv[2]         /* return value */
00185 )
00186 {
00187        register int  i;
00188                                    /* simple average (suboptimal) */
00189        cv[0] = cv[1] = 0.;
00190        for (i = 0; i < nv; i++) {
00191               cv[0] += vl[i][0];
00192               cv[1] += vl[i][1];
00193        }
00194        cv[0] /= (double)nv;
00195        cv[1] /= (double)nv;
00196 }
00197 
00198 
00199 static double
00200 poly_area(                  /* compute area of polygon */
00201        register RREAL       vl[][2],
00202        int    nv
00203 )
00204 {
00205        double a;
00206        RREAL  v0[2], v1[2];
00207        register int  i;
00208 
00209        a = 0.;
00210        v0[0] = vl[1][0] - vl[0][0];
00211        v0[1] = vl[1][1] - vl[0][1];
00212        for (i = 2; i < nv; i++) {
00213               v1[0] = vl[i][0] - vl[0][0];
00214               v1[1] = vl[i][1] - vl[0][1];
00215               a += v0[0]*v1[1] - v0[1]*v1[0];
00216               v0[0] = v1[0]; v0[1] = v1[1];
00217        }
00218        return(a * (a >= 0. ? .5 : -.5));
00219 }
00220 
00221 
00222 static int
00223 convex_hull(         /* compute polygon's convex hull */
00224        RREAL  vl[][2],
00225        int    nv,
00226        RREAL  vlo[][2]      /* return value */
00227 )
00228 {
00229        int    nvo, nvt;
00230        RREAL  vlt[MAXVERT][2];
00231        double voa, vta;
00232        register int  i, j;
00233                                    /* start with original polygon */
00234        for (i = nvo = nv; i--; ) {
00235               vlo[i][0] = vl[i][0]; vlo[i][1] = vl[i][1];
00236        }
00237        voa = poly_area(vlo, nvo);  /* compute its area */
00238        for (i = 0; i < nvo; i++) {        /* for each output vertex */
00239               for (j = 0; j < i; j++) {
00240                      vlt[j][0] = vlo[j][0]; vlt[j][1] = vlo[j][1];
00241               }
00242               nvt = nvo - 1;                     /* make poly w/o vertex */
00243               for (j = i; j < nvt; j++) {
00244                      vlt[j][0] = vlo[j+1][0]; vlt[j][1] = vlo[j+1][1];
00245               }
00246               vta = poly_area(vlt, nvt);
00247               if (vta >= voa) {           /* is simpler poly bigger? */
00248                      voa = vta;                  /* then use it */
00249                      for (j = nvo = nvt; j--; ) {
00250                             vlo[j][0] = vlt[j][0]; vlo[j][1] = vlt[j][1];
00251                      }
00252                      i--;                        /* next adjust */
00253               }
00254        }
00255        return(nvo);
00256 }
00257 
00258 
00259 static void
00260 spinsert(                   /* insert new source polygon */
00261        int    sn,
00262        RREAL  vl[][2],
00263        int    nv
00264 )
00265 {
00266        register SPLIST      *spn;
00267        register int  i;
00268 
00269        if (nv < 3)
00270               return;
00271        if (nv > 3)
00272               spn = (SPLIST *)malloc(sizeof(SPLIST)+sizeof(RREAL)*2*(nv-3));
00273        else
00274               spn = (SPLIST *)malloc(sizeof(SPLIST));
00275        if (spn == NULL)
00276               error(SYSTEM, "out of memory in spinsert");
00277        spn->sn = sn;
00278        for (i = spn->nv = nv; i--; ) {
00279               spn->vl[i][0] = vl[i][0]; spn->vl[i][1] = vl[i][1];
00280        }
00281        spn->next = sphead;         /* push onto global list */
00282        sphead = spn;
00283 }
00284 
00285 
00286 static int
00287 sourcepoly(                 /* compute image polygon for source */
00288        int    sn,
00289        RREAL  sp[MAXVERT][2]
00290 )
00291 {
00292        static short  cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2},
00293                                     {0,1,3,7,6,4},{0,1,5,7,6,2},
00294                                     {0,2,6,7,5,1},{0,4,6,7,3,1},
00295                                     {0,2,3,7,5,4},{1,5,4,6,2,3}};
00296        register SRCREC      *s = source + sn;
00297        FVECT  ap, ip;
00298        RREAL  pt[6][2];
00299        int    dir;
00300        register int  i, j;
00301 
00302        if (s->sflags & (SDISTANT|SFLAT)) {
00303               if (s->sflags & SDISTANT) {
00304                      if (ourview.type == VT_PAR)
00305                             return(0);    /* all or nothing case */
00306                      if (s->srad >= 0.05)
00307                             return(0);    /* should never be a problem */
00308               }
00309               if (s->sflags & SFLAT) {
00310                      for (i = 0; i < 3; i++)
00311                             ap[i] = s->sloc[i] - ourview.vp[i];
00312                      if (DOT(ap, s->snorm) >= 0.)
00313                             return(0);    /* source faces away */
00314               }
00315               for (j = 0; j < 4; j++) {   /* four corners */
00316                      for (i = 0; i < 3; i++) {
00317                             ap[i] = s->sloc[i];
00318                             if ((j==1)|(j==2)) ap[i] += s->ss[SU][i];
00319                             else ap[i] -= s->ss[SU][i];
00320                             if ((j==2)|(j==3)) ap[i] += s->ss[SV][i];
00321                             else ap[i] -= s->ss[SV][i];
00322                             if (s->sflags & SDISTANT) {
00323                                    ap[i] *= 1. + ourview.vfore;
00324                                    ap[i] += ourview.vp[i];
00325                             }
00326                      }
00327                      viewloc(ip, &ourview, ap);  /* find image point */
00328                      if (ip[2] <= 0.)
00329                             return(0);           /* in front of view */
00330                      sp[j][0] = ip[0]; sp[j][1] = ip[1];
00331               }
00332               return(4);
00333        }
00334                                    /* identify furthest corner */
00335        for (i = 0; i < 3; i++)
00336               ap[i] = s->sloc[i] - ourview.vp[i];
00337        dir =  (DOT(ap,s->ss[SU])>0.) |
00338               (DOT(ap,s->ss[SV])>0.)<<1 |
00339               (DOT(ap,s->ss[SW])>0.)<<2 ;
00340                                    /* order vertices based on this */
00341        for (j = 0; j < 6; j++) {
00342               for (i = 0; i < 3; i++) {
00343                      ap[i] = s->sloc[i];
00344                      if (cubeord[dir][j] & 1) ap[i] += s->ss[SU][i];
00345                      else ap[i] -= s->ss[SU][i];
00346                      if (cubeord[dir][j] & 2) ap[i] += s->ss[SV][i];
00347                      else ap[i] -= s->ss[SV][i];
00348                      if (cubeord[dir][j] & 4) ap[i] += s->ss[SW][i];
00349                      else ap[i] -= s->ss[SW][i];
00350               }
00351               viewloc(ip, &ourview, ap);  /* find image point */
00352               if (ip[2] <= 0.)
00353                      return(0);           /* in front of view */
00354               pt[j][0] = ip[0]; pt[j][1] = ip[1];
00355        }
00356        return(convex_hull(pt, 6, sp));           /* make sure it's convex */
00357 }
00358 
00359 
00360                      /* initialize by finding sources smaller than rad */
00361 extern void
00362 init_drawsources(
00363        int    rad                         /* source sample size */
00364 )
00365 {
00366        RREAL  spoly[MAXVERT][2];
00367        int    nsv;
00368        register SPLIST      *sp;
00369        register int  i;
00370                                    /* free old source list if one */
00371        for (sp = sphead; sp != NULL; sp = sphead) {
00372               sphead = sp->next;
00373               free((void *)sp);
00374        }
00375                                    /* loop through all sources */
00376        for (i = nsources; i--; ) {
00377                                    /* skip illum's */
00378               if (findmaterial(source[i].so)->otype == MAT_ILLUM)
00379                      continue;
00380                                    /* compute image polygon for source */
00381               if (!(nsv = sourcepoly(i, spoly)))
00382                      continue;
00383                                    /* clip to image boundaries */
00384               if (!(nsv = box_clip_poly(spoly, nsv, 0., 1., 0., 1., spoly)))
00385                      continue;
00386                                    /* big enough for standard sampling? */
00387               if (minw2(spoly, nsv, ourview.vn2/ourview.hn2) >
00388                             (double)rad*rad/hres/hres)
00389                      continue;
00390                                    /* OK, add to our list */
00391               spinsert(i, spoly, nsv);
00392        }
00393 }
00394 
00395 extern void                 /* add sources smaller than rad to computed subimage */
00396 drawsources(
00397        COLOR  *pic[],                            /* subimage pixel value array */
00398        float  *zbf[],                            /* subimage distance array (opt.) */
00399        int    x0,                         /* origin and size of subimage */
00400        int    xsiz,
00401        int    y0,
00402        int    ysiz
00403 )
00404 {
00405        RREAL  spoly[MAXVERT][2], ppoly[MAXVERT][2];
00406        int    nsv, npv;
00407        int    xmin, xmax, ymin, ymax, x, y;
00408        RREAL  cxy[2];
00409        double w;
00410        RAY    sr;
00411        register SPLIST      *sp;
00412        register int  i;
00413                                    /* check each source in our list */
00414        for (sp = sphead; sp != NULL; sp = sp->next) {
00415                                    /* clip source poly to subimage */
00416               nsv = box_clip_poly(sp->vl, sp->nv,
00417                             (double)x0/hres, (double)(x0+xsiz)/hres,
00418                             (double)y0/vres, (double)(y0+ysiz)/vres, spoly);
00419               if (!nsv)
00420                      continue;
00421                                    /* find common subimage (BBox) */
00422               xmin = x0 + xsiz; xmax = x0;
00423               ymin = y0 + ysiz; ymax = y0;
00424               for (i = 0; i < nsv; i++) {
00425                      if ((double)xmin/hres > spoly[i][0])
00426                             xmin = spoly[i][0]*hres + FTINY;
00427                      if ((double)xmax/hres < spoly[i][0])
00428                             xmax = spoly[i][0]*hres - FTINY;
00429                      if ((double)ymin/vres > spoly[i][1])
00430                             ymin = spoly[i][1]*vres + FTINY;
00431                      if ((double)ymax/vres < spoly[i][1])
00432                             ymax = spoly[i][1]*vres - FTINY;
00433               }
00434                                    /* evaluate each pixel in BBox */
00435               for (y = ymin; y <= ymax; y++)
00436                      for (x = xmin; x <= xmax; x++) {
00437                                                  /* subarea for pixel */
00438                             npv = box_clip_poly(spoly, nsv,
00439                                           (double)x/hres, (x+1.)/hres,
00440                                           (double)y/vres, (y+1.)/vres,
00441                                           ppoly);
00442                             if (!npv)
00443                                    continue;     /* no overlap */
00444                             convex_center(ppoly, npv, cxy);
00445                             if ((sr.rmax = viewray(sr.rorg,sr.rdir,&ourview,
00446                                           cxy[0],cxy[1])) < -FTINY)
00447                                    continue;     /* not in view */
00448                             if (source[sp->sn].sflags & SSPOT &&
00449                                           spotout(&sr, source[sp->sn].sl.s))
00450                                    continue;     /* outside spot */
00451                             rayorigin(&sr, SHADOW, NULL, NULL);
00452                             sr.rsrc = sp->sn;
00453                             rayvalue(&sr);              /* compute value */
00454                             if (bright(sr.rcol) <= FTINY)
00455                                    continue;     /* missed/blocked */
00456                                                  /* modify pixel */
00457                             w = poly_area(ppoly, npv) * hres * vres;
00458                             if (zbf[y-y0] != NULL &&
00459                                           sr.rt < 0.99*zbf[y-y0][x-x0]) {
00460                                    zbf[y-y0][x-x0] = sr.rt;
00461                             } else if (!bigdiff(sr.rcol, pic[y-y0][x-x0],
00462                                           0.01)) { /* source sample */
00463                                    scalecolor(pic[y-y0][x-x0], w);
00464                                    continue;
00465                             }
00466                             scalecolor(sr.rcol, w);
00467                             scalecolor(pic[y-y0][x-x0], 1.-w);
00468                             addcolor(pic[y-y0][x-x0], sr.rcol);
00469                      }
00470        }
00471 }