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

Comparing ray/src/rt/srcsupp.c (file contents):
Revision 1.3 by greg, Fri Jun 21 13:23:05 1991 UTC vs.
Revision 2.7 by greg, Thu Aug 24 20:56:08 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "face.h"
20  
21 + #define SRCINC          4               /* realloc increment for array */
22  
23   SRCREC  *source = NULL;                 /* our list of sources */
24   int  nsources = 0;                      /* the number of sources */
# Line 27 | Line 28 | SRCFUNC  sfun[NUMOTYPE];               /* source dispatch table */
28  
29   initstypes()                    /* initialize source dispatch table */
30   {
31 <        extern VSMATERIAL  mirror_vs;
32 <        extern int  fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc();
31 >        extern VSMATERIAL  mirror_vs, direct1_vs, direct2_vs;
32 >        extern int  fsetsrc(), ssetsrc(), sphsetsrc(), cylsetsrc(), rsetsrc();
33 >        extern int  nopart(), flatpart(), cylpart();
34          extern double  fgetplaneq(), rgetplaneq();
35          extern double  fgetmaxdisk(), rgetmaxdisk();
36 <        static SOBJECT  fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk};
37 <        static SOBJECT  ssobj = {ssetsrc};
38 <        static SOBJECT  sphsobj = {sphsetsrc};
39 <        static SOBJECT  rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk};
36 >        static SOBJECT  fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk};
37 >        static SOBJECT  ssobj = {ssetsrc, nopart};
38 >        static SOBJECT  sphsobj = {sphsetsrc, nopart};
39 >        static SOBJECT  cylsobj = {cylsetsrc, cylpart};
40 >        static SOBJECT  rsobj = {rsetsrc, flatpart, rgetplaneq, rgetmaxdisk};
41  
42          sfun[MAT_MIRROR].mf = &mirror_vs;
43 +        sfun[MAT_DIRECT1].mf = &direct1_vs;
44 +        sfun[MAT_DIRECT2].mf = &direct2_vs;
45          sfun[OBJ_FACE].of = &fsobj;
46          sfun[OBJ_SOURCE].of = &ssobj;
47          sfun[OBJ_SPHERE].of = &sphsobj;
48 +        sfun[OBJ_CYLINDER].of = &cylsobj;
49          sfun[OBJ_RING].of = &rsobj;
50   }
51  
# Line 48 | Line 54 | int
54   newsource()                     /* allocate new source in our array */
55   {
56          if (nsources == 0)
57 <                source = (SRCREC *)malloc(sizeof(SRCREC));
58 <        else
57 >                source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC));
58 >        else if (nsources%SRCINC == 0)
59                  source = (SRCREC *)realloc((char *)source,
60 <                                (unsigned)(nsources+1)*sizeof(SRCREC));
60 >                                (unsigned)(nsources+SRCINC)*sizeof(SRCREC));
61          if (source == NULL)
62                  return(-1);
63          source[nsources].sflags = 0;
# Line 61 | Line 67 | newsource()                    /* allocate new source in our array */
67   }
68  
69  
70 + setflatss(src)                          /* set sampling for a flat source */
71 + register SRCREC  *src;
72 + {
73 +        double  mult;
74 +        register int  i;
75 +
76 +        src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
77 +        for (i = 0; i < 3; i++)
78 +                if (src->snorm[i] < 0.6 && src->snorm[i] > -0.6)
79 +                        break;
80 +        src->ss[SV][i] = 1.0;
81 +        fcross(src->ss[SU], src->ss[SV], src->snorm);
82 +        mult = .5 * sqrt( src->ss2 / DOT(src->ss[SU],src->ss[SU]) );
83 +        for (i = 0; i < 3; i++)
84 +                src->ss[SU][i] *= mult;
85 +        fcross(src->ss[SV], src->snorm, src->ss[SU]);
86 + }
87 +
88 +
89   fsetsrc(src, so)                        /* set a face as a source */
90   register SRCREC  *src;
91   OBJREC  *so;
92   {
93          register FACE  *f;
94          register int  i, j;
95 +        double  d;
96          
97          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
98          src->so = so;
# Line 83 | Line 109 | OBJREC  *so;
109                  objerror(so, USER, "cannot hit center");
110          src->sflags |= SFLAT;
111          VCOPY(src->snorm, f->norm);
86        src->ss = sqrt(f->area / PI);
112          src->ss2 = f->area;
113 +                                                /* find maximum radius */
114 +        src->srad = 0.;
115 +        for (i = 0; i < f->nv; i++) {
116 +                d = dist2(VERTEX(f,i), src->sloc);
117 +                if (d > src->srad)
118 +                        src->srad = d;
119 +        }
120 +        src->srad = sqrt(src->srad);
121 +                                                /* compute size vectors */
122 +        if (f->nv == 4)                         /* parallelogram case */
123 +                for (j = 0; j < 3; j++) {
124 +                        src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]);
125 +                        src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]);
126 +                }
127 +        else
128 +                setflatss(src);
129   }
130  
131  
# Line 105 | Line 146 | register OBJREC  *so;
146          theta = PI/180.0/2.0 * so->oargs.farg[3];
147          if (theta <= FTINY)
148                  objerror(so, USER, "zero size");
108        src->ss = theta >= PI/4 ? 1.0 : tan(theta);
149          src->ss2 = 2.0*PI * (1.0 - cos(theta));
150 +                                        /* the following is approximate */
151 +        src->srad = sqrt(src->ss2/PI);
152 +        VCOPY(src->snorm, src->sloc);
153 +        setflatss(src);                 /* hey, whatever works */
154 +        src->ss[SW][0] = src->ss[SW][1] = src->ss[SW][2] = 0.0;
155   }
156  
157  
# Line 114 | Line 159 | sphsetsrc(src, so)                     /* set a sphere as a source */
159   register SRCREC  *src;
160   register OBJREC  *so;
161   {
162 +        register int  i;
163 +
164          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
165          src->so = so;
166          if (so->oargs.nfargs != 4)
# Line 121 | Line 168 | register OBJREC  *so;
168          if (so->oargs.farg[3] <= FTINY)
169                  objerror(so, USER, "illegal radius");
170          VCOPY(src->sloc, so->oargs.farg);
171 <        src->ss = so->oargs.farg[3];
172 <        src->ss2 = PI * src->ss * src->ss;
171 >        src->srad = so->oargs.farg[3];
172 >        src->ss2 = PI * src->srad * src->srad;
173 >        for (i = 0; i < 3; i++)
174 >                src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0;
175 >        for (i = 0; i < 3; i++)
176 >                src->ss[i][i] = .7236 * so->oargs.farg[3];
177   }
178  
179  
# Line 141 | Line 192 | OBJREC  *so;
192                  objerror(so, USER, "cannot hit center");
193          src->sflags |= SFLAT;
194          VCOPY(src->snorm, co->ad);
195 <        src->ss = CO_R1(co);
196 <        src->ss2 = PI * src->ss * src->ss;
195 >        src->srad = CO_R1(co);
196 >        src->ss2 = PI * src->srad * src->srad;
197 >        setflatss(src);
198   }
199  
200  
201 + cylsetsrc(src, so)                      /* set a cylinder as a source */
202 + register SRCREC  *src;
203 + OBJREC  *so;
204 + {
205 +        register CONE  *co;
206 +        register int  i;
207 +        
208 +        src->sa.success = 4*AIMREQT-1;          /* bitch on fourth failure */
209 +        src->so = so;
210 +                                                /* get the cylinder */
211 +        co = getcone(so, 0);
212 +        if (CO_R0(co) > .2*co->al)              /* heuristic constraint */
213 +                objerror(so, WARNING, "source aspect too small");
214 +        src->sflags |= SCYL;
215 +        for (i = 0; i < 3; i++)
216 +                src->sloc[i] = .5 * (CO_P1(co)[i] + CO_P0(co)[i]);
217 +        src->srad = .5*co->al;
218 +        src->ss2 = 2.*CO_R0(co)*co->al;
219 +                                                /* set sampling vectors */
220 +        for (i = 0; i < 3; i++)
221 +                src->ss[SU][i] = .5 * co->al * co->ad[i];
222 +        src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
223 +        for (i = 0; i < 3; i++)
224 +                if (co->ad[i] < 0.6 && co->ad[i] > -0.6)
225 +                        break;
226 +        src->ss[SV][i] = 1.0;
227 +        fcross(src->ss[SW], src->ss[SV], co->ad);
228 +        normalize(src->ss[SW]);
229 +        for (i = 0; i < 3; i++)
230 +                src->ss[SW][i] *= .8559 * CO_R0(co);
231 +        fcross(src->ss[SV], src->ss[SW], co->ad);
232 + }
233 +
234 +
235   SPOT *
236   makespot(m)                     /* make a spotlight */
237   register OBJREC  *m;
238   {
153        extern double  cos();
239          register SPOT  *ns;
240  
241 +        if ((ns = (SPOT *)m->os) != NULL)
242 +                return(ns);
243          if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
244                  return(NULL);
245          ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
246          VCOPY(ns->aim, m->oargs.farg+4);
247          if ((ns->flen = normalize(ns->aim)) == 0.0)
248                  objerror(m, USER, "zero focus vector");
249 +        m->os = (char *)ns;
250          return(ns);
251   }
252  
253  
254 + spotout(r, s, dist)             /* check if we're outside spot region */
255 + register RAY  *r;
256 + register SPOT  *s;
257 + int  dist;
258 + {
259 +        double  d;
260 +        FVECT  vd;
261 +        
262 +        if (s == NULL)
263 +                return(0);
264 +        if (dist) {                     /* distant source */
265 +                vd[0] = s->aim[0] - r->rorg[0];
266 +                vd[1] = s->aim[1] - r->rorg[1];
267 +                vd[2] = s->aim[2] - r->rorg[2];
268 +                d = DOT(r->rdir,vd);
269 +                /*                      wrong side?
270 +                if (d <= FTINY)
271 +                        return(1);      */
272 +                d = DOT(vd,vd) - d*d;
273 +                if (PI*d > s->siz)
274 +                        return(1);      /* out */
275 +                return(0);      /* OK */
276 +        }
277 +                                        /* local source */
278 +        if (s->siz < 2.0*PI * (1.0 + DOT(s->aim,r->rdir)))
279 +                return(1);      /* out */
280 +        return(0);      /* OK */
281 + }
282 +
283 +
284   double
285   fgetmaxdisk(ocent, op)          /* get center and squared radius of face */
286   FVECT  ocent;
287   OBJREC  *op;
288   {
289          double  maxrad2;
290 <        double  d2;
290 >        double  d;
291          register int  i, j;
292          register FACE  *f;
293          
294          f = getface(op);
295 +        if (f->area == 0.)
296 +                return(0.);
297          for (i = 0; i < 3; i++) {
298                  ocent[i] = 0.;
299                  for (j = 0; j < f->nv; j++)
300                          ocent[i] += VERTEX(f,j)[i];
301                  ocent[i] /= (double)f->nv;
302          }
303 <        if (f->area == 0.)
304 <                return(0.);
303 >        d = DOT(ocent,f->norm);
304 >        for (i = 0; i < 3; i++)
305 >                ocent[i] += (f->offset - d)*f->norm[i];
306          maxrad2 = 0.;
307          for (j = 0; j < f->nv; j++) {
308 <                d2 = dist2(VERTEX(f,j), ocent);
309 <                if (d2 > maxrad2)
310 <                        maxrad2 = d2;
308 >                d = dist2(VERTEX(f,j), ocent);
309 >                if (d > maxrad2)
310 >                        maxrad2 = d;
311          }
312          return(maxrad2);
313   }
# Line 231 | Line 352 | OBJREC  *op;
352   }
353  
354  
355 < sourcehit(r)                    /* check to see if ray hit distant source */
356 < register RAY  *r;
355 > commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
356 > register SPOT  *sp1, *sp2;
357 > FVECT  org;
358   {
359 <        int  first, last;
359 >        FVECT  cent;
360 >        double  rad2, cos1, cos2;
361 >
362 >        cos1 = 1. - sp1->siz/(2.*PI);
363 >        cos2 = 1. - sp2->siz/(2.*PI);
364 >        if (sp2->siz >= 2.*PI-FTINY)            /* BIG, just check overlap */
365 >                return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
366 >                                        sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
367 >                                /* compute and check disks */
368 >        rad2 = intercircle(cent, sp1->aim, sp2->aim,
369 >                        1./(cos1*cos1) - 1.,  1./(cos2*cos2) - 1.);
370 >        if (rad2 <= FTINY || normalize(cent) == 0.)
371 >                return(0);
372 >        VCOPY(sp1->aim, cent);
373 >        sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
374 >        return(1);
375 > }
376 >
377 >
378 > commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
379 > register SPOT  *sp1, *sp2;
380 > FVECT  dir;
381 > {
382 >        FVECT  cent, c1, c2;
383 >        double  rad2, d;
384          register int  i;
385 +                                        /* move centers to common plane */
386 +        d = DOT(sp1->aim, dir);
387 +        for (i = 0; i < 3; i++)
388 +                c1[i] = sp1->aim[i] - d*dir[i];
389 +        d = DOT(sp2->aim, dir);
390 +        for (i = 0; i < 3; i++)
391 +                c2[i] = sp2->aim[i] - d*dir[i];
392 +                                        /* compute overlap */
393 +        rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
394 +        if (rad2 <= FTINY)
395 +                return(0);
396 +        VCOPY(sp1->aim, cent);
397 +        sp1->siz = PI*rad2;
398 +        return(1);
399 + }
400  
240        if (r->rsrc >= 0) {             /* check only one if aimed */
241                first = last = r->rsrc;
242        } else {                        /* otherwise check all */
243                first = 0; last = nsources-1;
244        }
245        for (i = first; i <= last; i++)
246                if (source[i].sflags & SDISTANT)
247                        /*
248                         * Check to see if ray is within
249                         * solid angle of source.
250                         */
251                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
252                                        <= source[i].ss2) {
253                                r->ro = source[i].so;
254                                if (!(source[i].sflags & SSKIP))
255                                        break;
256                        }
401  
402 <        if (r->ro != NULL) {
403 <                for (i = 0; i < 3; i++)
404 <                        r->ron[i] = -r->rdir[i];
405 <                r->rod = 1.0;
406 <                r->rox = NULL;
402 > checkspot(sp, nrm)              /* check spotlight for behind source */
403 > register SPOT  *sp;     /* spotlight */
404 > FVECT  nrm;             /* source surface normal */
405 > {
406 >        double  d, d1;
407 >
408 >        d = DOT(sp->aim, nrm);
409 >        if (d > FTINY)                  /* center in front? */
410                  return(1);
411 <        }
412 <        return(0);
411 >                                        /* else check horizon */
412 >        d1 = 1. - sp->siz/(2.*PI);
413 >        return(1.-FTINY-d*d < d1*d1);
414   }
415  
416  
417 < #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
418 <                                r->rsrc>=0 && \
419 <                                source[r->rsrc].so!=r->ro)
417 > double
418 > spotdisk(oc, op, sp, pos)       /* intersect spot with object op */
419 > FVECT  oc;
420 > OBJREC  *op;
421 > register SPOT  *sp;
422 > FVECT  pos;
423 > {
424 >        FVECT  onorm;
425 >        double  offs, d, dist;
426 >        register int  i;
427  
428 < #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
429 <                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
428 >        offs = getplaneq(onorm, op);
429 >        d = -DOT(onorm, sp->aim);
430 >        if (d >= -FTINY && d <= FTINY)
431 >                return(0.);
432 >        dist = (DOT(pos, onorm) - offs)/d;
433 >        if (dist < 0.)
434 >                return(0.);
435 >        for (i = 0; i < 3; i++)
436 >                oc[i] = pos[i] + dist*sp->aim[i];
437 >        return(sp->siz*dist*dist/PI/(d*d));
438 > }
439  
276 #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
277                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
440  
441 <
442 < m_light(m, r)                   /* ray hit a light source */
443 < register OBJREC  *m;
444 < register RAY  *r;
441 > double
442 > beamdisk(oc, op, sp, dir)       /* intersect beam with object op */
443 > FVECT  oc;
444 > OBJREC  *op;
445 > register SPOT  *sp;
446 > FVECT  dir;
447   {
448 <                                                /* check for over-counting */
449 <        if (wrongsource(m, r) || badambient(m, r))
450 <                return;
287 <                                                /* check for passed illum */
288 <        if (passillum(m, r)) {
448 >        FVECT  onorm;
449 >        double  offs, d, dist;
450 >        register int  i;
451  
452 <                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
453 <                        raytrans(r);
454 <                else
455 <                        rayshade(r, modifier(m->oargs.sarg[0]));
452 >        offs = getplaneq(onorm, op);
453 >        d = -DOT(onorm, dir);
454 >        if (d >= -FTINY && d <= FTINY)
455 >                return(0.);
456 >        dist = (DOT(sp->aim, onorm) - offs)/d;
457 >        for (i = 0; i < 3; i++)
458 >                oc[i] = sp->aim[i] + dist*dir[i];
459 >        return(sp->siz/PI/(d*d));
460 > }
461  
462 <                                                /* otherwise treat as source */
462 >
463 > double
464 > intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
465 > FVECT  cc;                      /* midpoint (return value) */
466 > FVECT  c1, c2;                  /* circle centers */
467 > double  r1s, r2s;               /* radii squared */
468 > {
469 >        double  a2, d2, l;
470 >        FVECT  disp;
471 >        register int  i;
472 >
473 >        for (i = 0; i < 3; i++)
474 >                disp[i] = c2[i] - c1[i];
475 >        d2 = DOT(disp,disp);
476 >                                        /* circle within overlap? */
477 >        if (r1s < r2s) {
478 >                if (r2s >= r1s + d2) {
479 >                        VCOPY(cc, c1);
480 >                        return(r1s);
481 >                }
482          } else {
483 <                                                /* check for behind */
484 <                if (r->rod < 0.0)
485 <                        return;
486 <                                                /* get distribution pattern */
301 <                raytexture(r, m->omod);
302 <                                                /* get source color */
303 <                setcolor(r->rcol, m->oargs.farg[0],
304 <                                  m->oargs.farg[1],
305 <                                  m->oargs.farg[2]);
306 <                                                /* modify value */
307 <                multcolor(r->rcol, r->pcol);
483 >                if (r1s >= r2s + d2) {
484 >                        VCOPY(cc, c2);
485 >                        return(r2s);
486 >                }
487          }
488 +        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
489 +                                        /* no overlap? */
490 +        if (a2 <= 0.)
491 +                return(0.);
492 +                                        /* overlap, compute center */
493 +        l = sqrt((r1s - a2)/d2);
494 +        for (i = 0; i < 3; i++)
495 +                cc[i] = c1[i] + l*disp[i];
496 +        return(a2);
497   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines