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.14 by greg, Mon Oct 21 12:58:16 1991 UTC vs.
Revision 2.20 by greg, Sun Jun 22 18:05:28 2014 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   *  Support routines for source objects and materials
6 + *
7 + *  External symbols declared in source.h
8   */
9  
10 + #include "copyright.h"
11 +
12   #include  "ray.h"
13  
14   #include  "otypes.h"
# Line 18 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19  
20   #include  "face.h"
21  
22 < #define SRCINC          4               /* realloc increment for array */
22 > #define SRCINC          32              /* realloc increment for array */
23  
24   SRCREC  *source = NULL;                 /* our list of sources */
25   int  nsources = 0;                      /* the number of sources */
# Line 26 | Line 27 | int  nsources = 0;                     /* the number of sources */
27   SRCFUNC  sfun[NUMOTYPE];                /* source dispatch table */
28  
29  
30 < initstypes()                    /* initialize source dispatch table */
30 > void
31 > initstypes(void)                        /* initialize source dispatch table */
32   {
33          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();
34          static SOBJECT  fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk};
35          static SOBJECT  ssobj = {ssetsrc, nopart};
36          static SOBJECT  sphsobj = {sphsetsrc, nopart};
# Line 51 | Line 49 | initstypes()                   /* initialize source dispatch table */
49  
50  
51   int
52 < newsource()                     /* allocate new source in our array */
52 > newsource(void)                 /* allocate new source in our array */
53   {
54          if (nsources == 0)
55                  source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC));
56          else if (nsources%SRCINC == 0)
57 <                source = (SRCREC *)realloc((char *)source,
57 >                source = (SRCREC *)realloc((void *)source,
58                                  (unsigned)(nsources+SRCINC)*sizeof(SRCREC));
59          if (source == NULL)
60                  return(-1);
61          source[nsources].sflags = 0;
62          source[nsources].nhits = 1;
63 <        source[nsources].ntests = 2;    /* initial hit probability = 1/2 */
63 >        source[nsources].ntests = 2;    /* initial hit probability = 50% */
64 > #if SHADCACHE
65 >        source[nsources].obscache = NULL;
66 > #endif
67          return(nsources++);
68   }
69  
70  
71 < setflatss(src)                          /* set sampling for a flat source */
72 < register SRCREC  *src;
71 > void
72 > setflatss(                              /* set sampling for a flat source */
73 >        SRCREC  *src
74 > )
75   {
76          double  mult;
77 <        register int  i;
77 >        int  i;
78  
79          src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
80          for (i = 0; i < 3; i++)
# Line 86 | Line 89 | register SRCREC  *src;
89   }
90  
91  
92 < fsetsrc(src, so)                        /* set a face as a source */
93 < register SRCREC  *src;
94 < OBJREC  *so;
92 > void
93 > fsetsrc(                        /* set a face as a source */
94 >        SRCREC  *src,
95 >        OBJREC  *so
96 > )
97   {
98 <        register FACE  *f;
99 <        register int  i, j;
98 >        FACE  *f;
99 >        int  i, j;
100          double  d;
101          
102          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
103          src->so = so;
104                                                  /* get the face */
105          f = getface(so);
106 +        if (f->area == 0.0)
107 +                objerror(so, USER, "zero source area");
108                                                  /* find the center */
109          for (j = 0; j < 3; j++) {
110                  src->sloc[j] = 0.0;
# Line 106 | Line 113 | OBJREC  *so;
113                  src->sloc[j] /= (double)f->nv;
114          }
115          if (!inface(src->sloc, f))
116 <                objerror(so, USER, "cannot hit center");
116 >                objerror(so, USER, "cannot hit source center");
117          src->sflags |= SFLAT;
118          VCOPY(src->snorm, f->norm);
119          src->ss2 = f->area;
# Line 119 | Line 126 | OBJREC  *so;
126          }
127          src->srad = sqrt(src->srad);
128                                                  /* compute size vectors */
129 <        if (f->nv == 4 || (f->nv == 5 &&        /* parallelogram case */
123 <                        dist2(VERTEX(f,0),VERTEX(f,4)) <= FTINY*FTINY))
129 >        if (f->nv == 4)                         /* parallelogram case */
130                  for (j = 0; j < 3; j++) {
131                          src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]);
132                          src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]);
# Line 130 | Line 136 | OBJREC  *so;
136   }
137  
138  
139 < ssetsrc(src, so)                        /* set a source as a source */
140 < register SRCREC  *src;
141 < register OBJREC  *so;
139 > void
140 > ssetsrc(                        /* set a source as a source */
141 >        SRCREC  *src,
142 >        OBJREC  *so
143 > )
144   {
145          double  theta;
146          
# Line 140 | Line 148 | register OBJREC  *so;
148          src->so = so;
149          if (so->oargs.nfargs != 4)
150                  objerror(so, USER, "bad arguments");
151 <        src->sflags |= SDISTANT;
151 >        src->sflags |= (SDISTANT|SCIR);
152          VCOPY(src->sloc, so->oargs.farg);
153          if (normalize(src->sloc) == 0.0)
154                  objerror(so, USER, "zero direction");
# Line 156 | Line 164 | register OBJREC  *so;
164   }
165  
166  
167 < sphsetsrc(src, so)                      /* set a sphere as a source */
168 < register SRCREC  *src;
169 < register OBJREC  *so;
167 > void
168 > sphsetsrc(                      /* set a sphere as a source */
169 >        SRCREC  *src,
170 >        OBJREC  *so
171 > )
172   {
173 <        register int  i;
173 >        int  i;
174  
175          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
176          src->so = so;
177          if (so->oargs.nfargs != 4)
178                  objerror(so, USER, "bad # arguments");
179          if (so->oargs.farg[3] <= FTINY)
180 <                objerror(so, USER, "illegal radius");
180 >                objerror(so, USER, "illegal source radius");
181 >        src->sflags |= SCIR;
182          VCOPY(src->sloc, so->oargs.farg);
183          src->srad = so->oargs.farg[3];
184          src->ss2 = PI * src->srad * src->srad;
185          for (i = 0; i < 3; i++)
186                  src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0;
187          for (i = 0; i < 3; i++)
188 <                src->ss[i][i] = .886227 * so->oargs.farg[3];
188 >                src->ss[i][i] = 0.7236 * so->oargs.farg[3];
189   }
190  
191  
192 < rsetsrc(src, so)                        /* set a ring (disk) as a source */
193 < register SRCREC  *src;
194 < OBJREC  *so;
192 > void
193 > rsetsrc(                        /* set a ring (disk) as a source */
194 >        SRCREC  *src,
195 >        OBJREC  *so
196 > )
197   {
198 <        register CONE  *co;
198 >        CONE  *co;
199          
200          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
201          src->so = so;
202                                                  /* get the ring */
203          co = getcone(so, 0);
204 +        if (CO_R1(co) <= FTINY)
205 +                objerror(so, USER, "illegal source radius");
206          VCOPY(src->sloc, CO_P0(co));
207          if (CO_R0(co) > 0.0)
208 <                objerror(so, USER, "cannot hit center");
209 <        src->sflags |= SFLAT;
208 >                objerror(so, USER, "cannot hit source center");
209 >        src->sflags |= (SFLAT|SCIR);
210          VCOPY(src->snorm, co->ad);
211          src->srad = CO_R1(co);
212          src->ss2 = PI * src->srad * src->srad;
# Line 199 | Line 214 | OBJREC  *so;
214   }
215  
216  
217 < cylsetsrc(src, so)                      /* set a cylinder as a source */
218 < register SRCREC  *src;
219 < OBJREC  *so;
217 > void
218 > cylsetsrc(                      /* set a cylinder as a source */
219 >        SRCREC  *src,
220 >        OBJREC  *so
221 > )
222   {
223 <        register CONE  *co;
224 <        register int  i;
223 >        CONE  *co;
224 >        int  i;
225          
226          src->sa.success = 4*AIMREQT-1;          /* bitch on fourth failure */
227          src->so = so;
228                                                  /* get the cylinder */
229          co = getcone(so, 0);
230 +        if (CO_R0(co) <= FTINY)
231 +                objerror(so, USER, "illegal source radius");
232          if (CO_R0(co) > .2*co->al)              /* heuristic constraint */
233                  objerror(so, WARNING, "source aspect too small");
234 +        src->sflags |= SCYL;
235          for (i = 0; i < 3; i++)
236                  src->sloc[i] = .5 * (CO_P1(co)[i] + CO_P0(co)[i]);
237 <        src->srad = co->al;
237 >        src->srad = .5*co->al;
238          src->ss2 = 2.*CO_R0(co)*co->al;
239                                                  /* set sampling vectors */
240          for (i = 0; i < 3; i++)
# Line 227 | Line 247 | OBJREC  *so;
247          fcross(src->ss[SW], src->ss[SV], co->ad);
248          normalize(src->ss[SW]);
249          for (i = 0; i < 3; i++)
250 <                src->ss[SW][i] *= .886227 * CO_R0(co);
250 >                src->ss[SW][i] *= .8559 * CO_R0(co);
251          fcross(src->ss[SV], src->ss[SW], co->ad);
252   }
253  
254  
255   SPOT *
256 < makespot(m)                     /* make a spotlight */
257 < register OBJREC  *m;
256 > makespot(                       /* make a spotlight */
257 >        OBJREC  *m
258 > )
259   {
260 <        register SPOT  *ns;
260 >        SPOT  *ns;
261  
262 +        if ((ns = (SPOT *)m->os) != NULL)
263 +                return(ns);
264          if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
265                  return(NULL);
266 +        if (m->oargs.farg[3] <= FTINY)
267 +                objerror(m, USER, "zero angle");
268          ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
269          VCOPY(ns->aim, m->oargs.farg+4);
270          if ((ns->flen = normalize(ns->aim)) == 0.0)
271                  objerror(m, USER, "zero focus vector");
272 +        m->os = (char *)ns;
273          return(ns);
274   }
275  
276  
277 + int
278 + spotout(                        /* check if we're outside spot region */
279 +        RAY  *r,
280 +        SPOT  *s
281 + )
282 + {
283 +        double  d;
284 +        FVECT  vd;
285 +        
286 +        if (s == NULL)
287 +                return(0);
288 +        if (s->flen < -FTINY) {         /* distant source */
289 +                vd[0] = s->aim[0] - r->rorg[0];
290 +                vd[1] = s->aim[1] - r->rorg[1];
291 +                vd[2] = s->aim[2] - r->rorg[2];
292 +                d = DOT(r->rdir,vd);
293 +                /*                      wrong side?
294 +                if (d <= FTINY)
295 +                        return(1);      */
296 +                d = DOT(vd,vd) - d*d;
297 +                if (PI*d > s->siz)
298 +                        return(1);      /* out */
299 +                return(0);      /* OK */
300 +        }
301 +                                        /* local source */
302 +        if (s->siz < 2.0*PI * (1.0 + DOT(s->aim,r->rdir)))
303 +                return(1);      /* out */
304 +        return(0);      /* OK */
305 + }
306 +
307 +
308   double
309 < fgetmaxdisk(ocent, op)          /* get center and squared radius of face */
310 < FVECT  ocent;
311 < OBJREC  *op;
309 > fgetmaxdisk(            /* get center and squared radius of face */
310 >        FVECT  ocent,
311 >        OBJREC  *op
312 > )
313   {
314          double  maxrad2;
315          double  d;
316 <        register int  i, j;
317 <        register FACE  *f;
316 >        int  i, j;
317 >        FACE  *f;
318          
319          f = getface(op);
320          if (f->area == 0.)
# Line 281 | Line 339 | OBJREC  *op;
339  
340  
341   double
342 < rgetmaxdisk(ocent, op)          /* get center and squared radius of ring */
343 < FVECT  ocent;
344 < OBJREC  *op;
342 > rgetmaxdisk(            /* get center and squared radius of ring */
343 >        FVECT  ocent,
344 >        OBJREC  *op
345 > )
346   {
347 <        register CONE  *co;
347 >        CONE  *co;
348          
349          co = getcone(op, 0);
350          VCOPY(ocent, CO_P0(co));
# Line 294 | Line 353 | OBJREC  *op;
353  
354  
355   double
356 < fgetplaneq(nvec, op)                    /* get plane equation for face */
357 < FVECT  nvec;
358 < OBJREC  *op;
356 > fgetplaneq(                     /* get plane equation for face */
357 >        FVECT  nvec,
358 >        OBJREC  *op
359 > )
360   {
361 <        register FACE  *fo;
361 >        FACE  *fo;
362  
363          fo = getface(op);
364          VCOPY(nvec, fo->norm);
# Line 307 | Line 367 | OBJREC  *op;
367  
368  
369   double
370 < rgetplaneq(nvec, op)                    /* get plane equation for ring */
371 < FVECT  nvec;
372 < OBJREC  *op;
370 > rgetplaneq(                     /* get plane equation for ring */
371 >        FVECT  nvec,
372 >        OBJREC  *op
373 > )
374   {
375 <        register CONE  *co;
375 >        CONE  *co;
376  
377          co = getcone(op, 0);
378          VCOPY(nvec, co->ad);
# Line 319 | Line 380 | OBJREC  *op;
380   }
381  
382  
383 < commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
384 < register SPOT  *sp1, *sp2;
385 < FVECT  org;
383 > int
384 > commonspot(             /* set sp1 to intersection of sp1 and sp2 */
385 >        SPOT  *sp1,
386 >        SPOT  *sp2,
387 >        FVECT  org
388 > )
389   {
390          FVECT  cent;
391          double  rad2, cos1, cos2;
# Line 342 | Line 406 | FVECT  org;
406   }
407  
408  
409 < commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
410 < register SPOT  *sp1, *sp2;
411 < FVECT  dir;
409 > int
410 > commonbeam(             /* set sp1 to intersection of sp1 and sp2 */
411 >        SPOT  *sp1,
412 >        SPOT  *sp2,
413 >        FVECT  dir
414 > )
415   {
416          FVECT  cent, c1, c2;
417          double  rad2, d;
351        register int  i;
418                                          /* move centers to common plane */
419          d = DOT(sp1->aim, dir);
420 <        for (i = 0; i < 3; i++)
355 <                c1[i] = sp1->aim[i] - d*dir[i];
420 >        VSUM(c1, sp1->aim, dir, -d);
421          d = DOT(sp2->aim, dir);
422 <        for (i = 0; i < 3; i++)
358 <                c2[i] = sp2->aim[i] - d*dir[i];
422 >        VSUM(c2, sp2->aim, dir, -d);
423                                          /* compute overlap */
424          rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
425          if (rad2 <= FTINY)
# Line 366 | Line 430 | FVECT  dir;
430   }
431  
432  
433 < checkspot(sp, nrm)              /* check spotlight for behind source */
434 < register SPOT  *sp;     /* spotlight */
435 < FVECT  nrm;             /* source surface normal */
433 > int
434 > checkspot(                      /* check spotlight for behind source */
435 >        SPOT  *sp,      /* spotlight */
436 >        FVECT  nrm      /* source surface normal */
437 > )
438   {
439          double  d, d1;
440  
# Line 382 | Line 448 | FVECT  nrm;            /* source surface normal */
448  
449  
450   double
451 < spotdisk(oc, op, sp, pos)       /* intersect spot with object op */
452 < FVECT  oc;
453 < OBJREC  *op;
454 < register SPOT  *sp;
455 < FVECT  pos;
451 > spotdisk(               /* intersect spot with object op */
452 >        FVECT  oc,
453 >        OBJREC  *op,
454 >        SPOT  *sp,
455 >        FVECT  pos
456 > )
457   {
458          FVECT  onorm;
459          double  offs, d, dist;
393        register int  i;
460  
461          offs = getplaneq(onorm, op);
462          d = -DOT(onorm, sp->aim);
# Line 399 | Line 465 | FVECT  pos;
465          dist = (DOT(pos, onorm) - offs)/d;
466          if (dist < 0.)
467                  return(0.);
468 <        for (i = 0; i < 3; i++)
403 <                oc[i] = pos[i] + dist*sp->aim[i];
468 >        VSUM(oc, pos, sp->aim, dist);
469          return(sp->siz*dist*dist/PI/(d*d));
470   }
471  
472  
473   double
474 < beamdisk(oc, op, sp, dir)       /* intersect beam with object op */
475 < FVECT  oc;
476 < OBJREC  *op;
477 < register SPOT  *sp;
478 < FVECT  dir;
474 > beamdisk(               /* intersect beam with object op */
475 >        FVECT  oc,
476 >        OBJREC  *op,
477 >        SPOT  *sp,
478 >        FVECT  dir
479 > )
480   {
481          FVECT  onorm;
482          double  offs, d, dist;
417        register int  i;
483  
484          offs = getplaneq(onorm, op);
485          d = -DOT(onorm, dir);
486          if (d >= -FTINY && d <= FTINY)
487                  return(0.);
488          dist = (DOT(sp->aim, onorm) - offs)/d;
489 <        for (i = 0; i < 3; i++)
425 <                oc[i] = sp->aim[i] + dist*dir[i];
489 >        VSUM(oc, sp->aim, dir, dist);
490          return(sp->siz/PI/(d*d));
491   }
492  
493  
494   double
495 < intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
496 < FVECT  cc;                      /* midpoint (return value) */
497 < FVECT  c1, c2;                  /* circle centers */
498 < double  r1s, r2s;               /* radii squared */
495 > intercircle(                            /* intersect two circles */
496 >        FVECT  cc,              /* midpoint (return value) */
497 >        FVECT  c1,              /* circle centers */
498 >        FVECT  c2,
499 >        double  r1s,            /* radii squared */
500 >        double  r2s
501 > )
502   {
503          double  a2, d2, l;
504          FVECT  disp;
438        register int  i;
505  
506 <        for (i = 0; i < 3; i++)
441 <                disp[i] = c2[i] - c1[i];
506 >        VSUB(disp, c2, c1);
507          d2 = DOT(disp,disp);
508                                          /* circle within overlap? */
509          if (r1s < r2s) {
# Line 458 | Line 523 | double  r1s, r2s;              /* radii squared */
523                  return(0.);
524                                          /* overlap, compute center */
525          l = sqrt((r1s - a2)/d2);
526 <        for (i = 0; i < 3; i++)
462 <                cc[i] = c1[i] + l*disp[i];
526 >        VSUM(cc, c1, disp, l);
527          return(a2);
464 }
465
466
467 sourcehit(r)                    /* check to see if ray hit distant source */
468 register RAY  *r;
469 {
470        int  first, last;
471        register int  i;
472
473        if (r->rsrc >= 0) {             /* check only one if aimed */
474                first = last = r->rsrc;
475        } else {                        /* otherwise check all */
476                first = 0; last = nsources-1;
477        }
478        for (i = first; i <= last; i++)
479                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
480                        /*
481                         * Check to see if ray is within
482                         * solid angle of source.
483                         */
484                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
485                                        <= source[i].ss2) {
486                                r->ro = source[i].so;
487                                if (!(source[i].sflags & SSKIP))
488                                        break;
489                        }
490
491        if (r->ro != NULL) {
492                for (i = 0; i < 3; i++)
493                        r->ron[i] = -r->rdir[i];
494                r->rod = 1.0;
495                r->rox = NULL;
496                return(1);
497        }
498        return(0);
499 }
500
501
502 #define  wrongsource(m, r)      (r->rsrc>=0 && \
503                                source[r->rsrc].so!=r->ro && \
504                                (m->otype!=MAT_ILLUM || \
505                        objptr(source[r->rsrc].so->omod)->otype==MAT_ILLUM))
506
507 #define  distglow(m, r)         (m->otype==MAT_GLOW && \
508                                r->rot > m->oargs.farg[3])
509
510 #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
511                                !distglow(m, r))
512
513 #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
514                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
515
516 #define  srcignore(m, r)        (directinvis && !(r->crtype&SHADOW) && \
517                                !distglow(m, r))
518
519
520 m_light(m, r)                   /* ray hit a light source */
521 register OBJREC  *m;
522 register RAY  *r;
523 {
524                                                /* check for over-counting */
525        if (wrongsource(m, r) || badambient(m, r))
526                return;
527                                                /* check for passed illum */
528        if (passillum(m, r)) {
529                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
530                        raytrans(r);
531                else
532                        rayshade(r, modifier(m->oargs.sarg[0]));
533                return;
534        }
535                                        /* otherwise treat as source */
536                                                /* check for behind */
537        if (r->rod < 0.0)
538                return;
539                                                /* check for invisibility */
540        if (srcignore(m, r))
541                return;
542                                                /* get distribution pattern */
543        raytexture(r, m->omod);
544                                                /* get source color */
545        setcolor(r->rcol, m->oargs.farg[0],
546                          m->oargs.farg[1],
547                          m->oargs.farg[2]);
548                                                /* modify value */
549        multcolor(r->rcol, r->pcol);
528   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines