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

Comparing ray/src/rt/srcdraw.c (file contents):
Revision 2.1 by greg, Fri Mar 15 21:26:42 1996 UTC vs.
Revision 2.14 by greg, Wed Oct 20 18:19:22 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Draw small sources into image in case we missed them.
6 + *
7 + *  External symbols declared in ray.h
8   */
9  
10 < #include  "ray.h"
10 > #include "copyright.h"
11  
12 + #include  "ray.h"
13   #include  "view.h"
14
14   #include  "source.h"
15  
16  
# Line 22 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #define MAXVERT         10
23  
24 + typedef struct splist {
25 +        struct splist   *next;                  /* next source in list */
26 +        int     sn;                             /* source number */
27 +        short   nv;                             /* number of vertices */
28 +        RREAL   vl[3][2];                       /* vertex array (last) */
29 + } SPLIST;                               /* source polygon list */
30  
31 + extern VIEW     ourview;                /* our view parameters */
32 + extern int      hres, vres;             /* our image resolution */
33 + static SPLIST   *sphead = NULL;         /* our list of source polys */
34 +
35 + static int inregion(RREAL p[2], double cv, int crit);
36 + static void clipregion(RREAL a[2], RREAL b[2], double cv, int crit, RREAL r[2]);
37 + static int hp_clip_poly(RREAL vl[][2], int nv, double cv, int crit,
38 +                RREAL vlo[][2]);
39 + static int box_clip_poly(RREAL vl[MAXVERT][2], int nv,
40 +                double xl, double xr, double yb, double ya, RREAL vlo[MAXVERT][2]);
41 + static double minw2(RREAL vl[][2], int nv, double ar2);
42 + static void convex_center(RREAL vl[][2], int nv, RREAL cv[2]);
43 + static double poly_area(RREAL vl[][2], int nv);
44 + static int convex_hull(RREAL vl[][2], int nv, RREAL vlo[][2]);
45 + static void spinsert(int sn, RREAL vl[][2], int nv);
46 + static int sourcepoly(int sn, RREAL sp[MAXVERT][2]);
47 +
48 +
49   static int
50 < inregion(p, cv, crit)                   /* check if vertex is in region */
51 < FLOAT   p[2];
52 < double  cv;
53 < int     crit;
50 > inregion(                       /* check if vertex is in region */
51 >        RREAL   p[2],
52 >        double  cv,
53 >        int     crit
54 > )
55   {
56          switch (crit) {
57          case CLIP_ABOVE:
# Line 43 | Line 67 | int    crit;
67   }
68  
69  
70 < static
71 < clipregion(a, b, cv, crit, r)           /* find intersection with boundary */
72 < register FLOAT  a[2], b[2];
73 < double  cv;
74 < int     crit;
75 < FLOAT   r[2];   /* return value */
70 > static void
71 > clipregion(             /* find intersection with boundary */
72 >        register RREAL  a[2],
73 >        register RREAL  b[2],
74 >        double  cv,
75 >        int     crit,
76 >        RREAL   r[2]    /* return value */
77 > )
78   {
79          switch (crit) {
80          case CLIP_ABOVE:
# Line 66 | Line 92 | FLOAT  r[2];   /* return value */
92  
93  
94   static int
95 < hp_clip_poly(vl, nv, cv, crit, vlo)     /* clip polygon to half-plane */
96 < FLOAT   vl[][2];
97 < int     nv;
98 < double  cv;
99 < int     crit;
100 < FLOAT   vlo[][2];       /* return value */
95 > hp_clip_poly(   /* clip polygon to half-plane */
96 >        RREAL   vl[][2],
97 >        int     nv,
98 >        double  cv,
99 >        int     crit,
100 >        RREAL   vlo[][2]        /* return value */
101 > )
102   {
103 <        FLOAT   *s, *p;
103 >        RREAL   *s, *p;
104          register int    j, nvo;
105  
106          s = vl[nv-1];
# Line 93 | Line 120 | FLOAT  vlo[][2];       /* return value */
120  
121  
122   static int
123 < box_clip_poly(vl, nv, xl, xr, yb, ya, vlo)      /* clip polygon to box */
124 < FLOAT   vl[MAXVERT][2];
125 < int     nv;
126 < double  xl, xr, yb, ya;
127 < FLOAT   vlo[MAXVERT][2];        /* return value */
123 > box_clip_poly(  /* clip polygon to box */
124 >        RREAL   vl[MAXVERT][2],
125 >        int     nv,
126 >        double  xl,
127 >        double  xr,
128 >        double  yb,
129 >        double  ya,
130 >        RREAL   vlo[MAXVERT][2] /* return value */
131 > )
132   {
133 <        FLOAT   vlt[MAXVERT][2];
133 >        RREAL   vlt[MAXVERT][2];
134          int     nvt, nvo;
135  
136          nvt = hp_clip_poly(vl, nv, yb, CLIP_BELOW, vlt);
# Line 112 | Line 143 | FLOAT  vlo[MAXVERT][2];        /* return value */
143  
144  
145   static double
146 < minw2(vl, nv, ar2)                      /* compute square of minimum width */
147 < FLOAT   vl[][2];
148 < int     nv;
149 < double  ar2;
146 > minw2(                  /* compute square of minimum width */
147 >        RREAL   vl[][2],
148 >        int     nv,
149 >        double  ar2
150 > )
151   {
152          double  d2, w2, w2min, w2max;
153 <        register FLOAT  *p0, *p1, *p2;
153 >        register RREAL  *p0, *p1, *p2;
154          int     i, j;
155                                  /* find minimum for all widths */
156          w2min = FHUGE;
# Line 144 | Line 176 | double ar2;
176   }
177  
178  
179 < static
180 < convex_center(vl, nv, cv)               /* compute center of convex polygon */
181 < register FLOAT  vl[][2];
182 < int     nv;
183 < FLOAT   cv[2];          /* return value */
179 > static void
180 > convex_center(          /* compute center of convex polygon */
181 >        register RREAL  vl[][2],
182 >        int     nv,
183 >        RREAL   cv[2]           /* return value */
184 > )
185   {
186          register int    i;
187                                          /* simple average (suboptimal) */
# Line 163 | Line 196 | FLOAT  cv[2];          /* return value */
196  
197  
198   static double
199 < poly_area(vl, nv)                       /* compute area of polygon */
200 < register FLOAT  vl[][2];
201 < int     nv;
199 > poly_area(                      /* compute area of polygon */
200 >        register RREAL  vl[][2],
201 >        int     nv
202 > )
203   {
204          double  a;
205 <        FLOAT   v0[2], v1[2];
205 >        RREAL   v0[2], v1[2];
206          register int    i;
207  
208          a = 0.;
# Line 185 | Line 219 | int    nv;
219  
220  
221   static int
222 < convex_hull(vl, nv, vlo)                /* compute polygon's convex hull */
223 < FLOAT   vl[][2];
224 < int     nv;
225 < FLOAT   vlo[][2];       /* return value */
222 > convex_hull(            /* compute polygon's convex hull */
223 >        RREAL   vl[][2],
224 >        int     nv,
225 >        RREAL   vlo[][2]        /* return value */
226 > )
227   {
228          int     nvo, nvt;
229 <        FLOAT   vlt[MAXVERT][2];
229 >        RREAL   vlt[MAXVERT][2];
230          double  voa, vta;
231          register int    i, j;
232                                          /* start with original polygon */
# Line 220 | Line 255 | FLOAT  vlo[][2];       /* return value */
255   }
256  
257  
258 < int
259 < sourcepoly(vw, sn, sp)                  /* compute image polygon for source */
260 < VIEW    *vw;
261 < int     sn;
262 < FLOAT   sp[MAXVERT][2];
258 > static void
259 > spinsert(                       /* insert new source polygon */
260 >        int     sn,
261 >        RREAL   vl[][2],
262 >        int     nv
263 > )
264   {
265 <        static char     cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2},
265 >        register SPLIST *spn;
266 >        register int    i;
267 >
268 >        if (nv < 3)
269 >                return;
270 >        if (nv > 3)
271 >                spn = (SPLIST *)malloc(sizeof(SPLIST)+sizeof(RREAL)*2*(nv-3));
272 >        else
273 >                spn = (SPLIST *)malloc(sizeof(SPLIST));
274 >        if (spn == NULL)
275 >                error(SYSTEM, "out of memory in spinsert");
276 >        spn->sn = sn;
277 >        for (i = spn->nv = nv; i--; ) {
278 >                spn->vl[i][0] = vl[i][0]; spn->vl[i][1] = vl[i][1];
279 >        }
280 >        spn->next = sphead;             /* push onto global list */
281 >        sphead = spn;
282 > }
283 >
284 >
285 > static int
286 > sourcepoly(                     /* compute image polygon for source */
287 >        int     sn,
288 >        RREAL   sp[MAXVERT][2]
289 > )
290 > {
291 >        static short    cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2},
292                                           {0,1,3,7,6,4},{0,1,5,7,6,2},
293                                           {0,2,6,7,5,1},{0,4,6,7,3,1},
294                                           {0,2,3,7,5,4},{1,5,4,6,2,3}};
295          register SRCREC *s = source + sn;
296          FVECT   ap, ip;
297 <        FLOAT   pt[6][2];
297 >        RREAL   pt[6][2];
298          int     dir;
299          register int    i, j;
300  
301          if (s->sflags & (SDISTANT|SFLAT)) {
302 <                if (s->sflags & SDISTANT && vw->type == VT_PAR)
303 <                        return(0);              /* all or nothing case */
302 >                if (s->sflags & SDISTANT) {
303 >                        if (ourview.type == VT_PAR)
304 >                                return(0);      /* all or nothing case */
305 >                        if (s->srad >= 0.05)
306 >                                return(0);      /* should never be a problem */
307 >                }
308                  if (s->sflags & SFLAT) {
309                          for (i = 0; i < 3; i++)
310 <                                ap[i] = s->sloc[i] - vw->vp[i];
310 >                                ap[i] = s->sloc[i] - ourview.vp[i];
311                          if (DOT(ap, s->snorm) >= 0.)
312                                  return(0);      /* source faces away */
313                  }
314                  for (j = 0; j < 4; j++) {       /* four corners */
315                          for (i = 0; i < 3; i++) {
316                                  ap[i] = s->sloc[i];
317 <                                if (j==1|j==2) ap[i] += s->ss[SU][i];
317 >                                if ((j==1)|(j==2)) ap[i] += s->ss[SU][i];
318                                  else ap[i] -= s->ss[SU][i];
319 <                                if (j==2|j==3) ap[i] += s->ss[SV][i];
319 >                                if ((j==2)|(j==3)) ap[i] += s->ss[SV][i];
320                                  else ap[i] -= s->ss[SV][i];
321                                  if (s->sflags & SDISTANT) {
322 <                                        ap[i] *= 1. + vw->vfore;
323 <                                        ap[i] += vw->vp[i];
322 >                                        ap[i] *= 1. + ourview.vfore;
323 >                                        ap[i] += ourview.vp[i];
324                                  }
325                          }
326 <                        viewloc(ip, vw, ap);            /* find image point */
326 >                        viewloc(ip, &ourview, ap);      /* find image point */
327                          if (ip[2] <= 0.)
328                                  return(0);              /* in front of view */
329                          sp[j][0] = ip[0]; sp[j][1] = ip[1];
# Line 266 | Line 332 | FLOAT  sp[MAXVERT][2];
332          }
333                                          /* identify furthest corner */
334          for (i = 0; i < 3; i++)
335 <                ap[i] = s->sloc[i] - vw->vp[i];
335 >                ap[i] = s->sloc[i] - ourview.vp[i];
336          dir =   (DOT(ap,s->ss[SU])>0.) |
337                  (DOT(ap,s->ss[SV])>0.)<<1 |
338                  (DOT(ap,s->ss[SW])>0.)<<2 ;
# Line 281 | Line 347 | FLOAT  sp[MAXVERT][2];
347                          if (cubeord[dir][j] & 4) ap[i] += s->ss[SW][i];
348                          else ap[i] -= s->ss[SW][i];
349                  }
350 <                viewloc(ip, vw, ap);            /* find image point */
350 >                viewloc(ip, &ourview, ap);      /* find image point */
351                  if (ip[2] <= 0.)
352                          return(0);              /* in front of view */
353                  pt[j][0] = ip[0]; pt[j][1] = ip[1];
# Line 290 | Line 356 | FLOAT  sp[MAXVERT][2];
356   }
357  
358  
359 <                        /* add sources smaller than rad to computed subimage */
360 < drawsources(vw, xr, yr, pic, zbf, x0, xsiz, y0, ysiz, rad)
361 < VIEW    *vw;                            /* full image view */
362 < int     xr, yr;                         /* full image dimensions */
363 < COLOR   *pic[];                         /* subimage pixel value array */
298 < float   *zbf[];                         /* subimage distance array (opt.) */
299 < int     x0, xsiz, y0, ysiz;             /* origin and size of subimage */
300 < int     rad;                            /* source sample size */
359 >                        /* initialize by finding sources smaller than rad */
360 > extern void
361 > init_drawsources(
362 >        int     rad                             /* source sample size */
363 > )
364   {
365 <        int     sn;
366 <        FLOAT   spoly[MAXVERT][2], ppoly[MAXVERT][2];
367 <        int     nsv, npv;
368 <        int     xmin, xmax, ymin, ymax, x, y, i;
369 <        FLOAT   cxy[2];
370 <        double  pa;
371 <        RAY     sr;
365 >        RREAL   spoly[MAXVERT][2];
366 >        int     nsv;
367 >        register SPLIST *sp;
368 >        register int    i;
369 >                                        /* free old source list if one */
370 >        for (sp = sphead; sp != NULL; sp = sphead) {
371 >                sphead = sp->next;
372 >                free((void *)sp);
373 >        }
374                                          /* loop through all sources */
375 <        for (sn = 0; sn < nsources; sn++) {
375 >        for (i = nsources; i--; ) {
376                                          /* compute image polygon for source */
377 <                if (!(nsv = sourcepoly(vw, sn, spoly)))
377 >                if (!(nsv = sourcepoly(i, spoly)))
378                          continue;
379 +                                        /* clip to image boundaries */
380 +                if (!(nsv = box_clip_poly(spoly, nsv, 0., 1., 0., 1., spoly)))
381 +                        continue;
382 +                                        /* big enough for standard sampling? */
383 +                if (minw2(spoly, nsv, ourview.vn2/ourview.hn2) >
384 +                                (double)rad*rad/hres/hres)
385 +                        continue;
386 +                                        /* OK, add to our list */
387 +                spinsert(i, spoly, nsv);
388 +        }
389 + }
390 +
391 + extern void                     /* add sources smaller than rad to computed subimage */
392 + drawsources(
393 +        COLOR   *pic[],                         /* subimage pixel value array */
394 +        float   *zbf[],                         /* subimage distance array (opt.) */
395 +        int     x0,                             /* origin and size of subimage */
396 +        int     xsiz,
397 +        int     y0,
398 +        int     ysiz
399 + )
400 + {
401 +        RREAL   spoly[MAXVERT][2], ppoly[MAXVERT][2];
402 +        int     nsv, npv;
403 +        int     xmin, xmax, ymin, ymax, x, y;
404 +        RREAL   cxy[2];
405 +        double  w;
406 +        RAY     sr;
407 +        register SPLIST *sp;
408 +        register int    i;
409 +                                        /* check each source in our list */
410 +        for (sp = sphead; sp != NULL; sp = sp->next) {
411                                          /* clip source poly to subimage */
412 <                nsv = box_clip_poly(spoly, nsv,
413 <                                (double)x0/xr, (double)(x0+xsiz)/xr,
414 <                                (double)y0/yr, (double)(y0+ysiz)/yr, spoly);
412 >                nsv = box_clip_poly(sp->vl, sp->nv,
413 >                                (double)x0/hres, (double)(x0+xsiz)/hres,
414 >                                (double)y0/vres, (double)(y0+ysiz)/vres, spoly);
415                  if (!nsv)
416                          continue;
320                                        /* is it big so sampling found it? */
321                if (minw2(spoly, nsv, vw->vn2/vw->hn2) > (double)rad*rad/xr/xr)
322                        continue;
417                                          /* find common subimage (BBox) */
418                  xmin = x0 + xsiz; xmax = x0;
419                  ymin = y0 + ysiz; ymax = y0;
420                  for (i = 0; i < nsv; i++) {
421 <                        if ((double)xmin/xr > spoly[i][0])
422 <                                xmin = spoly[i][0]*xr + FTINY;
423 <                        if ((double)xmax/xr < spoly[i][0])
424 <                                xmax = spoly[i][0]*xr - FTINY;
425 <                        if ((double)ymin/yr > spoly[i][1])
426 <                                ymin = spoly[i][1]*yr + FTINY;
427 <                        if ((double)ymax/yr < spoly[i][1])
428 <                                ymax = spoly[i][1]*yr - FTINY;
421 >                        if ((double)xmin/hres > spoly[i][0])
422 >                                xmin = spoly[i][0]*hres + FTINY;
423 >                        if ((double)xmax/hres < spoly[i][0])
424 >                                xmax = spoly[i][0]*hres - FTINY;
425 >                        if ((double)ymin/vres > spoly[i][1])
426 >                                ymin = spoly[i][1]*vres + FTINY;
427 >                        if ((double)ymax/vres < spoly[i][1])
428 >                                ymax = spoly[i][1]*vres - FTINY;
429                  }
430                                          /* evaluate each pixel in BBox */
431                  for (y = ymin; y <= ymax; y++)
432                          for (x = xmin; x <= xmax; x++) {
433                                                          /* subarea for pixel */
434                                  npv = box_clip_poly(spoly, nsv,
435 <                                                (double)x/xr, (x+1.)/xr,
436 <                                                (double)y/yr, (y+1.)/yr, ppoly);
435 >                                                (double)x/hres, (x+1.)/hres,
436 >                                                (double)y/vres, (y+1.)/vres,
437 >                                                ppoly);
438                                  if (!npv)
439                                          continue;       /* no overlap */
440                                  convex_center(ppoly, npv, cxy);
441 <                                if ((sr.rmax = viewray(sr.rorg, sr.rdir, vw,
442 <                                                cxy[0], cxy[1])) < -FTINY)
441 >                                if ((sr.rmax = viewray(sr.rorg,sr.rdir,&ourview,
442 >                                                cxy[0],cxy[1])) < -FTINY)
443                                          continue;       /* not in view */
444 <                                if (source[sn].sflags & SSPOT &&
445 <                                                spotout(sr, source[sn].sl.s))
444 >                                if (source[sp->sn].sflags & SSPOT &&
445 >                                                spotout(&sr, source[sp->sn].sl.s))
446                                          continue;       /* outside spot */
447 <                                rayorigin(&sr, NULL, SHADOW, 1.0);
353 <                                sr.rsrc = sn;
447 >                                rayorigin(&sr, NULL, PRIMARY, 1.0);
448                                  rayvalue(&sr);          /* compute value */
449                                  if (bright(sr.rcol) <= FTINY)
450                                          continue;       /* missed/blocked */
451                                                          /* modify pixel */
452 +                                w = poly_area(ppoly, npv) * hres * vres;
453                                  if (zbf[y-y0] != NULL &&
454 <                                                sr.rt < zbf[y-y0][x-x0])
454 >                                                sr.rt < 0.99*zbf[y-y0][x-x0]) {
455                                          zbf[y-y0][x-x0] = sr.rt;
456 <                                pa = poly_area(ppoly, npv);
457 <                                scalecolor(sr.rcol, pa*xr*yr);
458 <                                scalecolor(pic[y-y0][x-x0], (1.-pa*xr*yr));
456 >                                } else if (!bigdiff(sr.rcol, pic[y-y0][x-x0],
457 >                                                0.01)) { /* source sample */
458 >                                        scalecolor(pic[y-y0][x-x0], w);
459 >                                        continue;
460 >                                }
461 >                                scalecolor(sr.rcol, w);
462 >                                scalecolor(pic[y-y0][x-x0], 1.-w);
463                                  addcolor(pic[y-y0][x-x0], sr.rcol);
464                          }
465          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines