ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcdraw.c
Revision: 2.14
Committed: Wed Oct 20 18:19:22 2004 UTC (19 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6
Changes since 2.13: +3 -7 lines
Log Message:
Changed source drawing routines so edges of illum's are not drawn

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.14 static const char RCSid[] = "$Id: srcdraw.c,v 2.13 2004/09/02 16:02:56 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Draw small sources into image in case we missed them.
6 greg 2.5 *
7     * External symbols declared in ray.h
8     */
9    
10 greg 2.6 #include "copyright.h"
11 greg 2.1
12     #include "ray.h"
13     #include "view.h"
14     #include "source.h"
15    
16    
17     #define CLIP_ABOVE 1
18     #define CLIP_BELOW 2
19     #define CLIP_RIGHT 3
20     #define CLIP_LEFT 4
21    
22     #define MAXVERT 10
23    
24 greg 2.3 typedef struct splist {
25     struct splist *next; /* next source in list */
26     int sn; /* source number */
27     short nv; /* number of vertices */
28 schorsch 2.7 RREAL vl[3][2]; /* vertex array (last) */
29 greg 2.3 } SPLIST; /* source polygon list */
30 greg 2.1
31 greg 2.3 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 schorsch 2.11 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 greg 2.3
49 greg 2.1 static int
50 schorsch 2.11 inregion( /* check if vertex is in region */
51     RREAL p[2],
52     double cv,
53     int crit
54     )
55 greg 2.1 {
56     switch (crit) {
57     case CLIP_ABOVE:
58     return(p[1] < cv);
59     case CLIP_BELOW:
60     return(p[1] >= cv);
61     case CLIP_RIGHT:
62     return(p[0] < cv);
63     case CLIP_LEFT:
64     return(p[0] >= cv);
65     }
66     return(-1);
67     }
68    
69    
70 schorsch 2.11 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 greg 2.1 {
79     switch (crit) {
80     case CLIP_ABOVE:
81     case CLIP_BELOW:
82     r[1] = cv;
83     r[0] = a[0] + (cv-a[1])/(b[1]-a[1])*(b[0]-a[0]);
84     return;
85     case CLIP_RIGHT:
86     case CLIP_LEFT:
87     r[0] = cv;
88     r[1] = a[1] + (cv-a[0])/(b[0]-a[0])*(b[1]-a[1]);
89     return;
90     }
91     }
92    
93    
94     static int
95 schorsch 2.11 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 greg 2.1 {
103 schorsch 2.7 RREAL *s, *p;
104 greg 2.1 register int j, nvo;
105    
106     s = vl[nv-1];
107     nvo = 0;
108     for (j = 0; j < nv; j++) {
109     p = vl[j];
110     if (inregion(p, cv, crit)) {
111     if (!inregion(s, cv, crit))
112     clipregion(s, p, cv, crit, vlo[nvo++]);
113     vlo[nvo][0] = p[0]; vlo[nvo++][1] = p[1];
114     } else if (inregion(s, cv, crit))
115     clipregion(s, p, cv, crit, vlo[nvo++]);
116     s = p;
117     }
118     return(nvo);
119     }
120    
121    
122     static int
123 schorsch 2.11 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 greg 2.1 {
133 schorsch 2.7 RREAL vlt[MAXVERT][2];
134 greg 2.1 int nvt, nvo;
135    
136     nvt = hp_clip_poly(vl, nv, yb, CLIP_BELOW, vlt);
137     nvo = hp_clip_poly(vlt, nvt, ya, CLIP_ABOVE, vlo);
138     nvt = hp_clip_poly(vlo, nvo, xl, CLIP_LEFT, vlt);
139     nvo = hp_clip_poly(vlt, nvt, xr, CLIP_RIGHT, vlo);
140    
141     return(nvo);
142     }
143    
144    
145     static double
146 schorsch 2.11 minw2( /* compute square of minimum width */
147     RREAL vl[][2],
148     int nv,
149     double ar2
150     )
151 greg 2.1 {
152     double d2, w2, w2min, w2max;
153 schorsch 2.7 register RREAL *p0, *p1, *p2;
154 greg 2.1 int i, j;
155     /* find minimum for all widths */
156     w2min = FHUGE;
157     p0 = vl[nv-1];
158     for (i = 0; i < nv; i++) { /* for each edge */
159     p1 = vl[i];
160     d2 = (p1[0]-p0[0])*(p1[0]-p0[0]) +
161     (p1[1]-p0[1])*(p1[1]-p0[1])*ar2;
162     w2max = 0.; /* find maximum for this side */
163     for (j = 1; j < nv-1; j++) {
164     p2 = vl[(i+j)%nv];
165     w2 = (p1[0]-p0[0])*(p2[1]-p0[1]) -
166     (p1[1]-p0[1])*(p2[0]-p0[0]);
167     w2 = w2*w2*ar2/d2; /* triangle height squared */
168     if (w2 > w2max)
169     w2max = w2;
170     }
171     if (w2max < w2min) /* global min. based on local max.'s */
172     w2min = w2max;
173     p0 = p1;
174     }
175     return(w2min);
176     }
177    
178    
179 schorsch 2.11 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 greg 2.1 {
186     register int i;
187     /* simple average (suboptimal) */
188     cv[0] = cv[1] = 0.;
189     for (i = 0; i < nv; i++) {
190     cv[0] += vl[i][0];
191     cv[1] += vl[i][1];
192     }
193     cv[0] /= (double)nv;
194     cv[1] /= (double)nv;
195     }
196    
197    
198     static double
199 schorsch 2.11 poly_area( /* compute area of polygon */
200     register RREAL vl[][2],
201     int nv
202     )
203 greg 2.1 {
204     double a;
205 schorsch 2.7 RREAL v0[2], v1[2];
206 greg 2.1 register int i;
207    
208     a = 0.;
209     v0[0] = vl[1][0] - vl[0][0];
210     v0[1] = vl[1][1] - vl[0][1];
211     for (i = 2; i < nv; i++) {
212     v1[0] = vl[i][0] - vl[0][0];
213     v1[1] = vl[i][1] - vl[0][1];
214     a += v0[0]*v1[1] - v0[1]*v1[0];
215     v0[0] = v1[0]; v0[1] = v1[1];
216     }
217     return(a * (a >= 0. ? .5 : -.5));
218     }
219    
220    
221     static int
222 schorsch 2.11 convex_hull( /* compute polygon's convex hull */
223     RREAL vl[][2],
224     int nv,
225     RREAL vlo[][2] /* return value */
226     )
227 greg 2.1 {
228     int nvo, nvt;
229 schorsch 2.7 RREAL vlt[MAXVERT][2];
230 greg 2.1 double voa, vta;
231     register int i, j;
232     /* start with original polygon */
233     for (i = nvo = nv; i--; ) {
234     vlo[i][0] = vl[i][0]; vlo[i][1] = vl[i][1];
235     }
236     voa = poly_area(vlo, nvo); /* compute its area */
237     for (i = 0; i < nvo; i++) { /* for each output vertex */
238     for (j = 0; j < i; j++) {
239     vlt[j][0] = vlo[j][0]; vlt[j][1] = vlo[j][1];
240     }
241     nvt = nvo - 1; /* make poly w/o vertex */
242     for (j = i; j < nvt; j++) {
243     vlt[j][0] = vlo[j+1][0]; vlt[j][1] = vlo[j+1][1];
244     }
245     vta = poly_area(vlt, nvt);
246     if (vta >= voa) { /* is simpler poly bigger? */
247     voa = vta; /* then use it */
248     for (j = nvo = nvt; j--; ) {
249     vlo[j][0] = vlt[j][0]; vlo[j][1] = vlt[j][1];
250     }
251     i--; /* next adjust */
252     }
253     }
254     return(nvo);
255     }
256    
257    
258 schorsch 2.11 static void
259     spinsert( /* insert new source polygon */
260     int sn,
261     RREAL vl[][2],
262     int nv
263     )
264 greg 2.3 {
265     register SPLIST *spn;
266     register int i;
267    
268     if (nv < 3)
269     return;
270     if (nv > 3)
271 schorsch 2.7 spn = (SPLIST *)malloc(sizeof(SPLIST)+sizeof(RREAL)*2*(nv-3));
272 greg 2.3 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 schorsch 2.11 static int
286     sourcepoly( /* compute image polygon for source */
287     int sn,
288     RREAL sp[MAXVERT][2]
289     )
290 greg 2.1 {
291 greg 2.9 static short cubeord[8][6] = {{1,3,2,6,4,5},{0,4,5,7,3,2},
292 greg 2.1 {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 schorsch 2.7 RREAL pt[6][2];
298 greg 2.1 int dir;
299     register int i, j;
300    
301     if (s->sflags & (SDISTANT|SFLAT)) {
302 greg 2.12 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 greg 2.1 if (s->sflags & SFLAT) {
309     for (i = 0; i < 3; i++)
310 greg 2.3 ap[i] = s->sloc[i] - ourview.vp[i];
311 greg 2.1 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 schorsch 2.8 if ((j==1)|(j==2)) ap[i] += s->ss[SU][i];
318 greg 2.1 else ap[i] -= s->ss[SU][i];
319 schorsch 2.8 if ((j==2)|(j==3)) ap[i] += s->ss[SV][i];
320 greg 2.1 else ap[i] -= s->ss[SV][i];
321     if (s->sflags & SDISTANT) {
322 greg 2.3 ap[i] *= 1. + ourview.vfore;
323     ap[i] += ourview.vp[i];
324 greg 2.1 }
325     }
326 greg 2.3 viewloc(ip, &ourview, ap); /* find image point */
327 greg 2.1 if (ip[2] <= 0.)
328     return(0); /* in front of view */
329     sp[j][0] = ip[0]; sp[j][1] = ip[1];
330     }
331     return(4);
332     }
333     /* identify furthest corner */
334     for (i = 0; i < 3; i++)
335 greg 2.3 ap[i] = s->sloc[i] - ourview.vp[i];
336 greg 2.1 dir = (DOT(ap,s->ss[SU])>0.) |
337     (DOT(ap,s->ss[SV])>0.)<<1 |
338     (DOT(ap,s->ss[SW])>0.)<<2 ;
339     /* order vertices based on this */
340     for (j = 0; j < 6; j++) {
341     for (i = 0; i < 3; i++) {
342     ap[i] = s->sloc[i];
343     if (cubeord[dir][j] & 1) ap[i] += s->ss[SU][i];
344     else ap[i] -= s->ss[SU][i];
345     if (cubeord[dir][j] & 2) ap[i] += s->ss[SV][i];
346     else ap[i] -= s->ss[SV][i];
347     if (cubeord[dir][j] & 4) ap[i] += s->ss[SW][i];
348     else ap[i] -= s->ss[SW][i];
349     }
350 greg 2.3 viewloc(ip, &ourview, ap); /* find image point */
351 greg 2.1 if (ip[2] <= 0.)
352     return(0); /* in front of view */
353     pt[j][0] = ip[0]; pt[j][1] = ip[1];
354     }
355     return(convex_hull(pt, 6, sp)); /* make sure it's convex */
356     }
357    
358    
359 greg 2.3 /* initialize by finding sources smaller than rad */
360 schorsch 2.11 extern void
361     init_drawsources(
362     int rad /* source sample size */
363     )
364 greg 2.3 {
365 schorsch 2.7 RREAL spoly[MAXVERT][2];
366 greg 2.3 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 greg 2.5 free((void *)sp);
373 greg 2.3 }
374     /* loop through all sources */
375     for (i = nsources; i--; ) {
376     /* compute image polygon for source */
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 schorsch 2.11 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 greg 2.1 {
401 schorsch 2.7 RREAL spoly[MAXVERT][2], ppoly[MAXVERT][2];
402 greg 2.1 int nsv, npv;
403 greg 2.3 int xmin, xmax, ymin, ymax, x, y;
404 schorsch 2.7 RREAL cxy[2];
405 gregl 2.4 double w;
406 greg 2.1 RAY sr;
407 greg 2.3 register SPLIST *sp;
408     register int i;
409     /* check each source in our list */
410     for (sp = sphead; sp != NULL; sp = sp->next) {
411 greg 2.1 /* clip source poly to subimage */
412 greg 2.3 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 greg 2.1 if (!nsv)
416     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 greg 2.3 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 greg 2.1 }
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 greg 2.3 (double)x/hres, (x+1.)/hres,
436     (double)y/vres, (y+1.)/vres,
437     ppoly);
438 greg 2.1 if (!npv)
439     continue; /* no overlap */
440     convex_center(ppoly, npv, cxy);
441 greg 2.3 if ((sr.rmax = viewray(sr.rorg,sr.rdir,&ourview,
442     cxy[0],cxy[1])) < -FTINY)
443 greg 2.1 continue; /* not in view */
444 greg 2.3 if (source[sp->sn].sflags & SSPOT &&
445     spotout(&sr, source[sp->sn].sl.s))
446 greg 2.1 continue; /* outside spot */
447 greg 2.14 rayorigin(&sr, NULL, PRIMARY, 1.0);
448 greg 2.1 rayvalue(&sr); /* compute value */
449     if (bright(sr.rcol) <= FTINY)
450     continue; /* missed/blocked */
451     /* modify pixel */
452 greg 2.14 w = poly_area(ppoly, npv) * hres * vres;
453 greg 2.1 if (zbf[y-y0] != NULL &&
454 greg 2.13 sr.rt < 0.99*zbf[y-y0][x-x0]) {
455 greg 2.1 zbf[y-y0][x-x0] = sr.rt;
456 greg 2.13 } else if (!bigdiff(sr.rcol, pic[y-y0][x-x0],
457     0.01)) { /* source sample */
458 greg 2.12 scalecolor(pic[y-y0][x-x0], w);
459 greg 2.13 continue;
460 greg 2.12 }
461 greg 2.13 scalecolor(sr.rcol, w);
462     scalecolor(pic[y-y0][x-x0], 1.-w);
463     addcolor(pic[y-y0][x-x0], sr.rcol);
464 greg 2.1 }
465     }
466     }