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.24 by greg, Thu Aug 4 22:43:46 2022 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          32              /* realloc increment for array */
23  
24   SRCREC  *source = NULL;                 /* our list of sources */
25   int  nsources = 0;                      /* the number of sources */
# Line 25 | 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;
34 <        extern int  fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc();
35 <        extern double  fgetplaneq(), rgetplaneq();
36 <        extern double  fgetmaxdisk(), rgetmaxdisk();
37 <        static SOBJECT  fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk};
38 <        static SOBJECT  ssobj = {ssetsrc};
36 <        static SOBJECT  sphsobj = {sphsetsrc};
37 <        static SOBJECT  rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk};
33 >        extern VSMATERIAL  mirror_vs, direct1_vs, direct2_vs;
34 >        static SOBJECT  fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk};
35 >        static SOBJECT  ssobj = {ssetsrc, nopart};
36 >        static SOBJECT  sphsobj = {sphsetsrc, nopart};
37 >        static SOBJECT  cylsobj = {cylsetsrc, cylpart};
38 >        static SOBJECT  rsobj = {rsetsrc, flatpart, rgetplaneq, rgetmaxdisk};
39  
40          sfun[MAT_MIRROR].mf = &mirror_vs;
41 +        sfun[MAT_DIRECT1].mf = &direct1_vs;
42 +        sfun[MAT_DIRECT2].mf = &direct2_vs;
43          sfun[OBJ_FACE].of = &fsobj;
44          sfun[OBJ_SOURCE].of = &ssobj;
45          sfun[OBJ_SPHERE].of = &sphsobj;
46 +        sfun[OBJ_CYLINDER].of = &cylsobj;
47          sfun[OBJ_RING].of = &rsobj;
48   }
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(sizeof(SRCREC));
56 <        else
57 <                source = (SRCREC *)realloc((char *)source,
58 <                                (unsigned)(nsources+1)*sizeof(SRCREC));
55 >                source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC));
56 >        else if (nsources%SRCINC == 0)
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 < fsetsrc(src, so)                        /* set a face as a source */
72 < register SRCREC  *src;
73 < OBJREC  *so;
71 > void
72 > setflatss(                              /* set sampling for a flat source */
73 >        SRCREC  *src
74 > )
75   {
76 <        register FACE  *f;
77 <        register int  i, j;
76 >        double  mult;
77 >        int  i;
78 >
79 >        getperpendicular(src->ss[SU], src->snorm, rand_samp);
80 >        mult = .5 * sqrt( src->ss2 );
81 >        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 > void
88 > fsetsrc(                        /* set a face as a source */
89 >        SRCREC  *src,
90 >        OBJREC  *so
91 > )
92 > {
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 80 | 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);
86        src->ss = sqrt(f->area / PI);
114          src->ss2 = f->area;
115 +                                                /* find maximum radius */
116 +        src->srad = 0.;
117 +        for (i = 0; i < f->nv; i++) {
118 +                d = dist2(VERTEX(f,i), src->sloc);
119 +                if (d > src->srad)
120 +                        src->srad = d;
121 +        }
122 +        src->srad = sqrt(src->srad);
123 +                                                /* compute size vectors */
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 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 98 | 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");
170          theta = PI/180.0/2.0 * so->oargs.farg[3];
171          if (theta <= FTINY)
172                  objerror(so, USER, "zero size");
108        src->ss = theta >= PI/4 ? 1.0 : tan(theta);
173          src->ss2 = 2.0*PI * (1.0 - cos(theta));
174 +                                        /* the following is approximate */
175 +        src->srad = sqrt(src->ss2/PI);
176 +        VCOPY(src->snorm, src->sloc);
177 +        setflatss(src);                 /* hey, whatever works */
178   }
179  
180  
181 < sphsetsrc(src, so)                      /* set a sphere as a source */
182 < register SRCREC  *src;
183 < register OBJREC  *so;
181 > void
182 > sphsetsrc(                      /* set a sphere as a source */
183 >        SRCREC  *src,
184 >        OBJREC  *so
185 > )
186   {
187 +        int  i;
188 +
189          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
190          src->so = so;
191          if (so->oargs.nfargs != 4)
192                  objerror(so, USER, "bad # arguments");
193          if (so->oargs.farg[3] <= FTINY)
194 <                objerror(so, USER, "illegal radius");
194 >                objerror(so, USER, "illegal source radius");
195 >        src->sflags |= SCIR;
196          VCOPY(src->sloc, so->oargs.farg);
197 <        src->ss = so->oargs.farg[3];
198 <        src->ss2 = PI * src->ss * src->ss;
197 >        src->srad = so->oargs.farg[3];
198 >        src->ss2 = PI * src->srad * src->srad;
199 >        memset(src->ss, 0, sizeof(src->ss));
200 >        for (i = 0; i < 3; i++)
201 >                src->ss[i][i] = 0.7236 * so->oargs.farg[3];
202   }
203  
204  
205 < rsetsrc(src, so)                        /* set a ring (disk) as a source */
206 < register SRCREC  *src;
207 < OBJREC  *so;
205 > void
206 > rsetsrc(                        /* set a ring (disk) as a source */
207 >        SRCREC  *src,
208 >        OBJREC  *so
209 > )
210   {
211 <        register CONE  *co;
211 >        CONE  *co;
212          
213          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
214          src->so = so;
215                                                  /* get the ring */
216          co = getcone(so, 0);
217 +        if (co == NULL)
218 +                objerror(so, USER, "illegal source");
219 +        if (CO_R1(co) <= FTINY)
220 +                objerror(so, USER, "illegal source radius");
221          VCOPY(src->sloc, CO_P0(co));
222          if (CO_R0(co) > 0.0)
223 <                objerror(so, USER, "cannot hit center");
224 <        src->sflags |= SFLAT;
223 >                objerror(so, USER, "cannot hit source center");
224 >        src->sflags |= (SFLAT|SCIR);
225          VCOPY(src->snorm, co->ad);
226 <        src->ss = CO_R1(co);
227 <        src->ss2 = PI * src->ss * src->ss;
226 >        src->srad = CO_R1(co);
227 >        src->ss2 = PI * src->srad * src->srad;
228 >        setflatss(src);
229   }
230  
231  
232 + void
233 + cylsetsrc(                      /* set a cylinder as a source */
234 +        SRCREC  *src,
235 +        OBJREC  *so
236 + )
237 + {
238 +        CONE  *co;
239 +        int  i;
240 +        
241 +        src->sa.success = 4*AIMREQT-1;          /* bitch on fourth failure */
242 +        src->so = so;
243 +                                                /* get the cylinder */
244 +        co = getcone(so, 0);
245 +        if (co == NULL)
246 +                objerror(so, USER, "illegal source");
247 +        if (CO_R0(co) <= FTINY)
248 +                objerror(so, USER, "illegal source radius");
249 +        if (CO_R0(co) > .2*co->al)              /* heuristic constraint */
250 +                objerror(so, WARNING, "source aspect too small");
251 +        src->sflags |= SCYL;
252 +        for (i = 0; i < 3; i++)
253 +                src->sloc[i] = .5 * (CO_P1(co)[i] + CO_P0(co)[i]);
254 +        src->srad = .5*co->al;
255 +        src->ss2 = 2.*CO_R0(co)*co->al;
256 +                                                /* set sampling vectors */
257 +        for (i = 0; i < 3; i++)
258 +                src->ss[SU][i] = .5 * co->al * co->ad[i];
259 +        getperpendicular(src->ss[SW], co->ad, rand_samp);
260 +        for (i = 0; i < 3; i++)
261 +                src->ss[SW][i] *= .8559 * CO_R0(co);
262 +        fcross(src->ss[SV], src->ss[SW], co->ad);
263 + }
264 +
265 +
266   SPOT *
267 < makespot(m)                     /* make a spotlight */
268 < register OBJREC  *m;
267 > makespot(                       /* make a spotlight */
268 >        OBJREC  *m
269 > )
270   {
271 <        extern double  cos();
154 <        register SPOT  *ns;
271 >        SPOT  *ns;
272  
273 +        if ((ns = (SPOT *)m->os) != NULL)
274 +                return(ns);
275          if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
276                  return(NULL);
277 +        if (m->oargs.farg[3] <= FTINY)
278 +                objerror(m, USER, "zero angle");
279          ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
280          VCOPY(ns->aim, m->oargs.farg+4);
281          if ((ns->flen = normalize(ns->aim)) == 0.0)
282                  objerror(m, USER, "zero focus vector");
283 +        m->os = (char *)ns;
284          return(ns);
285   }
286  
287  
288 + int
289 + spotout(                        /* check if we're outside spot region */
290 +        RAY  *r,
291 +        SPOT  *s
292 + )
293 + {
294 +        double  d;
295 +        FVECT  vd;
296 +        
297 +        if (s == NULL)
298 +                return(0);
299 +        if (s->flen < -FTINY) {         /* distant source */
300 +                vd[0] = s->aim[0] - r->rorg[0];
301 +                vd[1] = s->aim[1] - r->rorg[1];
302 +                vd[2] = s->aim[2] - r->rorg[2];
303 +                d = DOT(r->rdir,vd);
304 +                /*                      wrong side?
305 +                if (d <= FTINY)
306 +                        return(1);      */
307 +                d = DOT(vd,vd) - d*d;
308 +                if (PI*d > s->siz)
309 +                        return(1);      /* out */
310 +                return(0);      /* OK */
311 +        }
312 +                                        /* local source */
313 +        if (s->siz < 2.0*PI * (1.0 + DOT(s->aim,r->rdir)))
314 +                return(1);      /* out */
315 +        return(0);      /* OK */
316 + }
317 +
318 +
319   double
320 < fgetmaxdisk(ocent, op)          /* get center and squared radius of face */
321 < FVECT  ocent;
322 < OBJREC  *op;
320 > fgetmaxdisk(            /* get center and squared radius of face */
321 >        FVECT  ocent,
322 >        OBJREC  *op
323 > )
324   {
325          double  maxrad2;
326 <        double  d2;
327 <        register int  i, j;
328 <        register FACE  *f;
326 >        double  d;
327 >        int  i, j;
328 >        FACE  *f;
329          
330          f = getface(op);
331 +        if (f->area == 0.)
332 +                return(0.);
333          for (i = 0; i < 3; i++) {
334                  ocent[i] = 0.;
335                  for (j = 0; j < f->nv; j++)
336                          ocent[i] += VERTEX(f,j)[i];
337                  ocent[i] /= (double)f->nv;
338          }
339 <        if (f->area == 0.)
340 <                return(0.);
339 >        d = DOT(ocent,f->norm);
340 >        for (i = 0; i < 3; i++)
341 >                ocent[i] += (f->offset - d)*f->norm[i];
342          maxrad2 = 0.;
343          for (j = 0; j < f->nv; j++) {
344 <                d2 = dist2(VERTEX(f,j), ocent);
345 <                if (d2 > maxrad2)
346 <                        maxrad2 = d2;
344 >                d = dist2(VERTEX(f,j), ocent);
345 >                if (d > maxrad2)
346 >                        maxrad2 = d;
347          }
348          return(maxrad2);
349   }
350  
351  
352   double
353 < rgetmaxdisk(ocent, op)          /* get center and squared radius of ring */
354 < FVECT  ocent;
355 < OBJREC  *op;
353 > rgetmaxdisk(            /* get center and squared radius of ring */
354 >        FVECT  ocent,
355 >        OBJREC  *op
356 > )
357   {
358 <        register CONE  *co;
358 >        CONE  *co;
359          
360          co = getcone(op, 0);
361 +        if (co == NULL)
362 +                return(0.);
363          VCOPY(ocent, CO_P0(co));
364          return(CO_R1(co)*CO_R1(co));
365   }
366  
367  
368   double
369 < fgetplaneq(nvec, op)                    /* get plane equation for face */
370 < FVECT  nvec;
371 < OBJREC  *op;
369 > fgetplaneq(                     /* get plane equation for face */
370 >        FVECT  nvec,
371 >        OBJREC  *op
372 > )
373   {
374 <        register FACE  *fo;
374 >        FACE  *fo;
375  
376          fo = getface(op);
377          VCOPY(nvec, fo->norm);
# Line 219 | Line 380 | OBJREC  *op;
380  
381  
382   double
383 < rgetplaneq(nvec, op)                    /* get plane equation for ring */
384 < FVECT  nvec;
385 < OBJREC  *op;
383 > rgetplaneq(                     /* get plane equation for ring */
384 >        FVECT  nvec,
385 >        OBJREC  *op
386 > )
387   {
388 <        register CONE  *co;
388 >        CONE  *co;
389  
390          co = getcone(op, 0);
391 +        if (co == NULL) {
392 +                memset(nvec, 0, sizeof(FVECT));
393 +                return(0.);
394 +        }
395          VCOPY(nvec, co->ad);
396          return(DOT(nvec, CO_P0(co)));
397   }
398  
399  
400 < sourcehit(r)                    /* check to see if ray hit distant source */
401 < register RAY  *r;
400 > int
401 > commonspot(             /* set sp1 to intersection of sp1 and sp2 */
402 >        SPOT  *sp1,
403 >        SPOT  *sp2,
404 >        FVECT  org
405 > )
406   {
407 <        int  first, last;
408 <        register int  i;
407 >        FVECT  cent;
408 >        double  rad2, cos1, cos2;
409  
410 <        if (r->rsrc >= 0) {             /* check only one if aimed */
411 <                first = last = r->rsrc;
412 <        } else {                        /* otherwise check all */
413 <                first = 0; last = nsources-1;
414 <        }
415 <        for (i = first; i <= last; i++)
416 <                if (source[i].sflags & SDISTANT)
417 <                        /*
418 <                         * Check to see if ray is within
419 <                         * solid angle of source.
420 <                         */
421 <                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
422 <                                        <= source[i].ss2) {
423 <                                r->ro = source[i].so;
254 <                                if (!(source[i].sflags & SSKIP))
255 <                                        break;
256 <                        }
410 >        cos1 = 1. - sp1->siz/(2.*PI);
411 >        cos2 = 1. - sp2->siz/(2.*PI);
412 >        if (sp2->siz >= 2.*PI-FTINY)            /* BIG, just check overlap */
413 >                return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
414 >                                        sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
415 >                                /* compute and check disks */
416 >        rad2 = intercircle(cent, sp1->aim, sp2->aim,
417 >                        1./(cos1*cos1) - 1.,  1./(cos2*cos2) - 1.);
418 >        if (rad2 <= FTINY || normalize(cent) == 0.)
419 >                return(0);
420 >        VCOPY(sp1->aim, cent);
421 >        sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
422 >        return(1);
423 > }
424  
425 <        if (r->ro != NULL) {
426 <                for (i = 0; i < 3; i++)
427 <                        r->ron[i] = -r->rdir[i];
428 <                r->rod = 1.0;
429 <                r->rox = NULL;
430 <                return(1);
431 <        }
432 <        return(0);
425 >
426 > int
427 > commonbeam(             /* set sp1 to intersection of sp1 and sp2 */
428 >        SPOT  *sp1,
429 >        SPOT  *sp2,
430 >        FVECT  dir
431 > )
432 > {
433 >        FVECT  cent, c1, c2;
434 >        double  rad2, d;
435 >                                        /* move centers to common plane */
436 >        d = DOT(sp1->aim, dir);
437 >        VSUM(c1, sp1->aim, dir, -d);
438 >        d = DOT(sp2->aim, dir);
439 >        VSUM(c2, sp2->aim, dir, -d);
440 >                                        /* compute overlap */
441 >        rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
442 >        if (rad2 <= FTINY)
443 >                return(0);
444 >        VCOPY(sp1->aim, cent);
445 >        sp1->siz = PI*rad2;
446 >        return(1);
447   }
448  
449  
450 < #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
451 <                                r->rsrc>=0 && \
452 <                                source[r->rsrc].so!=r->ro)
450 > int
451 > checkspot(                      /* check spotlight for behind source */
452 >        SPOT  *sp,      /* spotlight */
453 >        FVECT  nrm      /* source surface normal */
454 > )
455 > {
456 >        double  d, d1;
457  
458 < #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
459 <                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
458 >        d = DOT(sp->aim, nrm);
459 >        if (d > FTINY)                  /* center in front? */
460 >                return(1);
461 >                                        /* else check horizon */
462 >        d1 = 1. - sp->siz/(2.*PI);
463 >        return(1.-FTINY-d*d < d1*d1);
464 > }
465  
276 #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
277                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
466  
467 + double
468 + spotdisk(               /* intersect spot with object op */
469 +        FVECT  oc,
470 +        OBJREC  *op,
471 +        SPOT  *sp,
472 +        FVECT  pos
473 + )
474 + {
475 +        FVECT  onorm;
476 +        double  offs, d, dist;
477  
478 < m_light(m, r)                   /* ray hit a light source */
479 < register OBJREC  *m;
480 < register RAY  *r;
478 >        offs = getplaneq(onorm, op);
479 >        d = -DOT(onorm, sp->aim);
480 >        if (d >= -FTINY && d <= FTINY)
481 >                return(0.);
482 >        dist = (DOT(pos, onorm) - offs)/d;
483 >        if (dist < 0.)
484 >                return(0.);
485 >        VSUM(oc, pos, sp->aim, dist);
486 >        return(sp->siz*dist*dist/PI/(d*d));
487 > }
488 >
489 >
490 > double
491 > beamdisk(               /* intersect beam with object op */
492 >        FVECT  oc,
493 >        OBJREC  *op,
494 >        SPOT  *sp,
495 >        FVECT  dir
496 > )
497   {
498 <                                                /* check for over-counting */
499 <        if (wrongsource(m, r) || badambient(m, r))
286 <                return;
287 <                                                /* check for passed illum */
288 <        if (passillum(m, r)) {
498 >        FVECT  onorm;
499 >        double  offs, d, dist;
500  
501 <                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
502 <                        raytrans(r);
503 <                else
504 <                        rayshade(r, modifier(m->oargs.sarg[0]));
501 >        offs = getplaneq(onorm, op);
502 >        d = -DOT(onorm, dir);
503 >        if (d >= -FTINY && d <= FTINY)
504 >                return(0.);
505 >        dist = (DOT(sp->aim, onorm) - offs)/d;
506 >        VSUM(oc, sp->aim, dir, dist);
507 >        return(sp->siz/PI/(d*d));
508 > }
509  
510 <                                                /* otherwise treat as source */
510 >
511 > double
512 > intercircle(                            /* intersect two circles */
513 >        FVECT  cc,              /* midpoint (return value) */
514 >        FVECT  c1,              /* circle centers */
515 >        FVECT  c2,
516 >        double  r1s,            /* radii squared */
517 >        double  r2s
518 > )
519 > {
520 >        double  a2, d2, l;
521 >        FVECT  disp;
522 >
523 >        VSUB(disp, c2, c1);
524 >        d2 = DOT(disp,disp);
525 >                                        /* circle within overlap? */
526 >        if (r1s < r2s) {
527 >                if (r2s >= r1s + d2) {
528 >                        VCOPY(cc, c1);
529 >                        return(r1s);
530 >                }
531          } else {
532 <                                                /* check for behind */
533 <                if (r->rod < 0.0)
534 <                        return;
535 <                                                /* 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);
532 >                if (r1s >= r2s + d2) {
533 >                        VCOPY(cc, c2);
534 >                        return(r2s);
535 >                }
536          }
537 +        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
538 +                                        /* no overlap? */
539 +        if (a2 <= 0.)
540 +                return(0.);
541 +                                        /* overlap, compute center */
542 +        l = sqrt((r1s - a2)/d2);
543 +        VSUM(cc, c1, disp, l);
544 +        return(a2);
545   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines