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 2.5 by greg, Fri Aug 28 14:07:45 1992 UTC vs.
Revision 2.25 by greg, Wed Aug 7 18:33:33 2024 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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;
79 >        getperpendicular(src->ss[SU], src->snorm, rand_samp);
80 >        mult = .5 * sqrt( src->ss2 );
81          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++)
82                  src->ss[SU][i] *= mult;
83          fcross(src->ss[SV], src->snorm, src->ss[SU]);
84   }
85  
86  
87 < fsetsrc(src, so)                        /* set a face as a source */
88 < register SRCREC  *src;
89 < OBJREC  *so;
87 > void
88 > fsetsrc(                        /* set a face as a source */
89 >        SRCREC  *src,
90 >        OBJREC  *so
91 > )
92   {
93 <        register FACE  *f;
94 <        register int  i, j;
93 >        FACE  *f;
94 >        int  i, j;
95          double  d;
96          
97          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
98          src->so = so;
99                                                  /* get the face */
100          f = getface(so);
101 +        if (f->area == 0.)
102 +                objerror(so, USER, "zero source area");
103                                                  /* find the center */
104          for (j = 0; j < 3; j++) {
105                  src->sloc[j] = 0.0;
# Line 106 | Line 108 | OBJREC  *so;
108                  src->sloc[j] /= (double)f->nv;
109          }
110          if (!inface(src->sloc, f))
111 <                objerror(so, USER, "cannot hit center");
111 >                objerror(so, USER, "cannot hit source center");
112          src->sflags |= SFLAT;
113          VCOPY(src->snorm, f->norm);
114          src->ss2 = f->area;
# Line 119 | Line 121 | OBJREC  *so;
121          }
122          src->srad = sqrt(src->srad);
123                                                  /* compute size vectors */
124 <        if (f->nv == 4 || (f->nv == 5 &&        /* parallelogram case */
123 <                        dist2(VERTEX(f,0),VERTEX(f,4)) <= FTINY*FTINY))
124 >        if (f->nv == 4) {                               /* parallelogram case */
125                  for (j = 0; j < 3; j++) {
126                          src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]);
127                          src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]);
128                  }
129 <        else
130 <                setflatss(src);
129 >        } else if (f->nv == 3) {                        /* triangle case */
130 >                int     near0 = 2;
131 >                double  dmin = dist2line(src->sloc, VERTEX(f,2), VERTEX(f,0));
132 >                for (i = 0; i < 2; i++) {
133 >                        double  d2 = dist2line(src->sloc, VERTEX(f,i), VERTEX(f,i+1));
134 >                        if (d2 >= dmin)
135 >                                continue;
136 >                        near0 = i;
137 >                        dmin = d2;                      /* radius = min distance */
138 >                }
139 >                if (dmin < .08*f->area)
140 >                        objerror(so, WARNING, "triangular source with poor aspect");
141 >                i = (near0 + 1) % 3;
142 >                for (j = 0; j < 3; j++)
143 >                        src->ss[SU][j] = VERTEX(f,i)[j] - VERTEX(f,near0)[j];
144 >                normalize(src->ss[SU]);
145 >                dmin = sqrt(dmin);
146 >                for (j = 0; j < 3; j++)
147 >                        src->ss[SU][j] *= dmin;
148 >                fcross(src->ss[SV], f->norm, src->ss[SU]);
149 >        } else
150 >                setflatss(src);                         /* hope for convex! */
151   }
152  
153  
154 < ssetsrc(src, so)                        /* set a source as a source */
155 < register SRCREC  *src;
156 < register OBJREC  *so;
154 > void
155 > ssetsrc(                        /* set a source as a source */
156 >        SRCREC  *src,
157 >        OBJREC  *so
158 > )
159   {
160          double  theta;
161          
# Line 140 | Line 163 | register OBJREC  *so;
163          src->so = so;
164          if (so->oargs.nfargs != 4)
165                  objerror(so, USER, "bad arguments");
166 <        src->sflags |= SDISTANT;
166 >        src->sflags |= (SDISTANT|SCIR);
167          VCOPY(src->sloc, so->oargs.farg);
168          if (normalize(src->sloc) == 0.0)
169                  objerror(so, USER, "zero direction");
# Line 156 | Line 179 | register OBJREC  *so;
179   }
180  
181  
182 < sphsetsrc(src, so)                      /* set a sphere as a source */
183 < register SRCREC  *src;
184 < register OBJREC  *so;
182 > void
183 > sphsetsrc(                      /* set a sphere as a source */
184 >        SRCREC  *src,
185 >        OBJREC  *so
186 > )
187   {
188 <        register int  i;
188 >        int  i;
189  
190          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
191          src->so = so;
192          if (so->oargs.nfargs != 4)
193                  objerror(so, USER, "bad # arguments");
194          if (so->oargs.farg[3] <= FTINY)
195 <                objerror(so, USER, "illegal radius");
195 >                objerror(so, USER, "illegal source radius");
196 >        src->sflags |= SCIR;
197          VCOPY(src->sloc, so->oargs.farg);
198          src->srad = so->oargs.farg[3];
199          src->ss2 = PI * src->srad * src->srad;
200 +        memset(src->ss, 0, sizeof(src->ss));
201          for (i = 0; i < 3; i++)
202 <                src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0;
176 <        for (i = 0; i < 3; i++)
177 <                src->ss[i][i] = .7236 * so->oargs.farg[3];
202 >                src->ss[i][i] = 0.7236 * so->oargs.farg[3];
203   }
204  
205  
206 < rsetsrc(src, so)                        /* set a ring (disk) as a source */
207 < register SRCREC  *src;
208 < OBJREC  *so;
206 > void
207 > rsetsrc(                        /* set a ring (disk) as a source */
208 >        SRCREC  *src,
209 >        OBJREC  *so
210 > )
211   {
212 <        register CONE  *co;
212 >        CONE  *co;
213          
214          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
215          src->so = so;
216                                                  /* get the ring */
217          co = getcone(so, 0);
218 +        if (co == NULL)
219 +                objerror(so, USER, "illegal source");
220 +        if (CO_R1(co) <= FTINY)
221 +                objerror(so, USER, "illegal source radius");
222          VCOPY(src->sloc, CO_P0(co));
223          if (CO_R0(co) > 0.0)
224 <                objerror(so, USER, "cannot hit center");
225 <        src->sflags |= SFLAT;
224 >                objerror(so, USER, "cannot hit source center");
225 >        src->sflags |= (SFLAT|SCIR);
226          VCOPY(src->snorm, co->ad);
227          src->srad = CO_R1(co);
228          src->ss2 = PI * src->srad * src->srad;
# Line 199 | Line 230 | OBJREC  *so;
230   }
231  
232  
233 < cylsetsrc(src, so)                      /* set a cylinder as a source */
234 < register SRCREC  *src;
235 < OBJREC  *so;
233 > void
234 > cylsetsrc(                      /* set a cylinder as a source */
235 >        SRCREC  *src,
236 >        OBJREC  *so
237 > )
238   {
239 <        register CONE  *co;
240 <        register int  i;
239 >        CONE  *co;
240 >        int  i;
241          
242          src->sa.success = 4*AIMREQT-1;          /* bitch on fourth failure */
243          src->so = so;
244                                                  /* get the cylinder */
245          co = getcone(so, 0);
246 +        if (co == NULL)
247 +                objerror(so, USER, "illegal source");
248 +        if (CO_R0(co) <= FTINY)
249 +                objerror(so, USER, "illegal source radius");
250          if (CO_R0(co) > .2*co->al)              /* heuristic constraint */
251                  objerror(so, WARNING, "source aspect too small");
252          src->sflags |= SCYL;
# Line 220 | Line 257 | OBJREC  *so;
257                                                  /* set sampling vectors */
258          for (i = 0; i < 3; i++)
259                  src->ss[SU][i] = .5 * co->al * co->ad[i];
260 <        src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
260 >        getperpendicular(src->ss[SW], co->ad, rand_samp);
261          for (i = 0; i < 3; i++)
225                if (co->ad[i] < 0.6 && co->ad[i] > -0.6)
226                        break;
227        src->ss[SV][i] = 1.0;
228        fcross(src->ss[SW], src->ss[SV], co->ad);
229        normalize(src->ss[SW]);
230        for (i = 0; i < 3; i++)
262                  src->ss[SW][i] *= .8559 * CO_R0(co);
263          fcross(src->ss[SV], src->ss[SW], co->ad);
264   }
265  
266  
267   SPOT *
268 < makespot(m)                     /* make a spotlight */
269 < register OBJREC  *m;
268 > makespot(                       /* make a spotlight */
269 >        OBJREC  *m
270 > )
271   {
272 <        register SPOT  *ns;
272 >        SPOT  *ns;
273  
274          if ((ns = (SPOT *)m->os) != NULL)
275                  return(ns);
276          if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
277                  return(NULL);
278 +        if (m->oargs.farg[3] <= FTINY)
279 +                objerror(m, USER, "zero angle");
280          ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
281          VCOPY(ns->aim, m->oargs.farg+4);
282          if ((ns->flen = normalize(ns->aim)) == 0.0)
# Line 252 | Line 286 | register OBJREC  *m;
286   }
287  
288  
289 < spotout(r, s, dist)             /* check if we're outside spot region */
290 < register RAY  *r;
291 < register SPOT  *s;
292 < int  dist;
289 > int
290 > spotout(                        /* check if we're outside spot region */
291 >        RAY  *r,
292 >        SPOT  *s
293 > )
294   {
295          double  d;
296          FVECT  vd;
297          
298          if (s == NULL)
299                  return(0);
300 <        if (dist) {                     /* distant source */
300 >        if (s->flen < -FTINY) {         /* distant source */
301                  vd[0] = s->aim[0] - r->rorg[0];
302                  vd[1] = s->aim[1] - r->rorg[1];
303                  vd[2] = s->aim[2] - r->rorg[2];
# Line 283 | Line 318 | int  dist;
318  
319  
320   double
321 < fgetmaxdisk(ocent, op)          /* get center and squared radius of face */
322 < FVECT  ocent;
323 < OBJREC  *op;
321 > fgetmaxdisk(            /* get center and squared radius of face */
322 >        FVECT  ocent,
323 >        OBJREC  *op
324 > )
325   {
326          double  maxrad2;
327          double  d;
328 <        register int  i, j;
329 <        register FACE  *f;
328 >        int  i, j;
329 >        FACE  *f;
330          
331          f = getface(op);
332          if (f->area == 0.)
# Line 315 | Line 351 | OBJREC  *op;
351  
352  
353   double
354 < rgetmaxdisk(ocent, op)          /* get center and squared radius of ring */
355 < FVECT  ocent;
356 < OBJREC  *op;
354 > rgetmaxdisk(            /* get center and squared radius of ring */
355 >        FVECT  ocent,
356 >        OBJREC  *op
357 > )
358   {
359 <        register CONE  *co;
359 >        CONE  *co;
360          
361          co = getcone(op, 0);
362 +        if (co == NULL)
363 +                return(0.);
364          VCOPY(ocent, CO_P0(co));
365          return(CO_R1(co)*CO_R1(co));
366   }
367  
368  
369   double
370 < fgetplaneq(nvec, op)                    /* get plane equation for face */
371 < FVECT  nvec;
372 < OBJREC  *op;
370 > fgetplaneq(                     /* get plane equation for face */
371 >        FVECT  nvec,
372 >        OBJREC  *op
373 > )
374   {
375 <        register FACE  *fo;
375 >        FACE  *fo;
376  
377          fo = getface(op);
378          VCOPY(nvec, fo->norm);
# Line 341 | Line 381 | OBJREC  *op;
381  
382  
383   double
384 < rgetplaneq(nvec, op)                    /* get plane equation for ring */
385 < FVECT  nvec;
386 < OBJREC  *op;
384 > rgetplaneq(                     /* get plane equation for ring */
385 >        FVECT  nvec,
386 >        OBJREC  *op
387 > )
388   {
389 <        register CONE  *co;
389 >        CONE  *co;
390  
391          co = getcone(op, 0);
392 +        if (co == NULL) {
393 +                memset(nvec, 0, sizeof(FVECT));
394 +                return(0.);
395 +        }
396          VCOPY(nvec, co->ad);
397          return(DOT(nvec, CO_P0(co)));
398   }
399  
400  
401 < commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
402 < register SPOT  *sp1, *sp2;
403 < FVECT  org;
401 > int
402 > commonspot(             /* set sp1 to intersection of sp1 and sp2 */
403 >        SPOT  *sp1,
404 >        SPOT  *sp2,
405 >        FVECT  org
406 > )
407   {
408          FVECT  cent;
409          double  rad2, cos1, cos2;
# Line 376 | Line 424 | FVECT  org;
424   }
425  
426  
427 < commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
428 < register SPOT  *sp1, *sp2;
429 < FVECT  dir;
427 > int
428 > commonbeam(             /* set sp1 to intersection of sp1 and sp2 */
429 >        SPOT  *sp1,
430 >        SPOT  *sp2,
431 >        FVECT  dir
432 > )
433   {
434          FVECT  cent, c1, c2;
435          double  rad2, d;
385        register int  i;
436                                          /* move centers to common plane */
437          d = DOT(sp1->aim, dir);
438 <        for (i = 0; i < 3; i++)
389 <                c1[i] = sp1->aim[i] - d*dir[i];
438 >        VSUM(c1, sp1->aim, dir, -d);
439          d = DOT(sp2->aim, dir);
440 <        for (i = 0; i < 3; i++)
392 <                c2[i] = sp2->aim[i] - d*dir[i];
440 >        VSUM(c2, sp2->aim, dir, -d);
441                                          /* compute overlap */
442          rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
443          if (rad2 <= FTINY)
# Line 400 | Line 448 | FVECT  dir;
448   }
449  
450  
451 < checkspot(sp, nrm)              /* check spotlight for behind source */
452 < register SPOT  *sp;     /* spotlight */
453 < FVECT  nrm;             /* source surface normal */
451 > int
452 > checkspot(                      /* check spotlight for behind source */
453 >        SPOT  *sp,      /* spotlight */
454 >        FVECT  nrm      /* source surface normal */
455 > )
456   {
457          double  d, d1;
458  
# Line 416 | Line 466 | FVECT  nrm;            /* source surface normal */
466  
467  
468   double
469 < spotdisk(oc, op, sp, pos)       /* intersect spot with object op */
470 < FVECT  oc;
471 < OBJREC  *op;
472 < register SPOT  *sp;
473 < FVECT  pos;
469 > spotdisk(               /* intersect spot with object op */
470 >        FVECT  oc,
471 >        OBJREC  *op,
472 >        SPOT  *sp,
473 >        FVECT  pos
474 > )
475   {
476          FVECT  onorm;
477          double  offs, d, dist;
427        register int  i;
478  
479          offs = getplaneq(onorm, op);
480          d = -DOT(onorm, sp->aim);
# Line 433 | Line 483 | FVECT  pos;
483          dist = (DOT(pos, onorm) - offs)/d;
484          if (dist < 0.)
485                  return(0.);
486 <        for (i = 0; i < 3; i++)
437 <                oc[i] = pos[i] + dist*sp->aim[i];
486 >        VSUM(oc, pos, sp->aim, dist);
487          return(sp->siz*dist*dist/PI/(d*d));
488   }
489  
490  
491   double
492 < beamdisk(oc, op, sp, dir)       /* intersect beam with object op */
493 < FVECT  oc;
494 < OBJREC  *op;
495 < register SPOT  *sp;
496 < FVECT  dir;
492 > beamdisk(               /* intersect beam with object op */
493 >        FVECT  oc,
494 >        OBJREC  *op,
495 >        SPOT  *sp,
496 >        FVECT  dir
497 > )
498   {
499          FVECT  onorm;
500          double  offs, d, dist;
451        register int  i;
501  
502          offs = getplaneq(onorm, op);
503          d = -DOT(onorm, dir);
504          if (d >= -FTINY && d <= FTINY)
505                  return(0.);
506          dist = (DOT(sp->aim, onorm) - offs)/d;
507 <        for (i = 0; i < 3; i++)
459 <                oc[i] = sp->aim[i] + dist*dir[i];
507 >        VSUM(oc, sp->aim, dir, dist);
508          return(sp->siz/PI/(d*d));
509   }
510  
511  
512   double
513 < intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
514 < FVECT  cc;                      /* midpoint (return value) */
515 < FVECT  c1, c2;                  /* circle centers */
516 < double  r1s, r2s;               /* radii squared */
513 > intercircle(                            /* intersect two circles */
514 >        FVECT  cc,              /* midpoint (return value) */
515 >        FVECT  c1,              /* circle centers */
516 >        FVECT  c2,
517 >        double  r1s,            /* radii squared */
518 >        double  r2s
519 > )
520   {
521          double  a2, d2, l;
522          FVECT  disp;
472        register int  i;
523  
524 <        for (i = 0; i < 3; i++)
475 <                disp[i] = c2[i] - c1[i];
524 >        VSUB(disp, c2, c1);
525          d2 = DOT(disp,disp);
526                                          /* circle within overlap? */
527          if (r1s < r2s) {
# Line 492 | Line 541 | double  r1s, r2s;              /* radii squared */
541                  return(0.);
542                                          /* overlap, compute center */
543          l = sqrt((r1s - a2)/d2);
544 <        for (i = 0; i < 3; i++)
496 <                cc[i] = c1[i] + l*disp[i];
544 >        VSUM(cc, c1, disp, l);
545          return(a2);
498 }
499
500
501 sourcehit(r)                    /* check to see if ray hit distant source */
502 register RAY  *r;
503 {
504        int  first, last;
505        register int  i;
506
507        if (r->rsrc >= 0) {             /* check only one if aimed */
508                first = last = r->rsrc;
509        } else {                        /* otherwise check all */
510                first = 0; last = nsources-1;
511        }
512        for (i = first; i <= last; i++)
513                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
514                        /*
515                         * Check to see if ray is within
516                         * solid angle of source.
517                         */
518                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
519                                        <= source[i].ss2) {
520                                r->ro = source[i].so;
521                                if (!(source[i].sflags & SSKIP))
522                                        break;
523                        }
524
525        if (r->ro != NULL) {
526                for (i = 0; i < 3; i++)
527                        r->ron[i] = -r->rdir[i];
528                r->rod = 1.0;
529                r->rox = NULL;
530                return(1);
531        }
532        return(0);
546   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines