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

Comparing ray/src/rt/virtuals.c (file contents):
Revision 1.6 by greg, Fri Jun 21 13:25:42 1991 UTC vs.
Revision 1.20 by greg, Fri Aug 2 10:30:27 1991 UTC

# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11  
12   #include  "ray.h"
13  
14 + #include  "octree.h"
15 +
16   #include  "otypes.h"
17  
18   #include  "source.h"
19  
20 + #include  "random.h"
21  
22 < double  intercircle(), getdisk();
22 > #define  MINSAMPLES     5               /* minimum number of pretest samples */
23 > #define  STESTMAX       30              /* maximum seeks per sample */
24  
25 +
26 + double  getdisk();
27 +
28   static OBJECT  *vobject;                /* virtual source objects */
29   static int  nvobjects = 0;              /* number of virtual source objects */
30  
# Line 55 | Line 62 | markvirtuals()                 /* find and mark virtual sources */
62   #endif
63                                          /* append virtual sources */
64          for (i = nsources; i-- > 0; )
65 <                if (!(source[i].sflags & SSKIP))
59 <                        addvirtuals(i, directrelay);
65 >                addvirtuals(i, directrelay);
66                                          /* done with our object list */
67          free((char *)vobject);
68          nvobjects = 0;
# Line 71 | Line 77 | int  nr;
77                                  /* check relay limit first */
78          if (nr <= 0)
79                  return;
80 +        if (source[sn].sflags & SSKIP)
81 +                return;
82                                  /* check each virtual object for projection */
83          for (i = 0; i < nvobjects; i++)
84                                          /* vproject() calls us recursively */
# Line 96 | Line 104 | int  n;
104          for (i = 0; i < vsmat->nproj; i++)
105                  if ((*vsmat->vproj)(proj, o, &source[sn], i))
106                          if ((ns = makevsrc(o, sn, proj)) >= 0) {
107 +                                source[ns].sa.sv.pn = i;
108   #ifdef DEBUG
109                                  virtverb(ns, stderr);
110   #endif
# Line 110 | Line 119 | OBJREC  *op;
119   register int  sn;
120   MAT4  pm;
121   {
122 <        FVECT  nsloc, nsnorm, ocent;
123 <        double  maxrad2;
122 >        FVECT  nsloc, nsnorm, ocent, v;
123 >        double  maxrad2, d;
124          int  nsflags;
116        double  d1;
125          SPOT  theirspot, ourspot;
126          register int  i;
127  
# Line 127 | Line 135 | MAT4  pm;
135                  if (source[sn].sflags & SPROX)
136                          return(-1);             /* should never get here! */
137                  multv3(nsloc, source[sn].sloc, pm);
138 +                normalize(nsloc);
139                  VCOPY(ourspot.aim, ocent);
140                  ourspot.siz = PI*maxrad2;
141                  ourspot.flen = 0.;
142                  if (source[sn].sflags & SSPOT) {
134                        copystruct(&theirspot, source[sn].sl.s);
143                          multp3(theirspot.aim, source[sn].sl.s->aim, pm);
144 +                        d = sqrt(dist2(ourspot.aim, theirspot.aim));
145 +                        d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].ss;
146 +                        theirspot.siz = PI*d*d;
147 +                        ourspot.flen = theirspot.flen = source[sn].sl.s->flen;
148 +                        d = ourspot.siz;
149                          if (!commonbeam(&ourspot, &theirspot, nsloc))
150 <                                return(-1);             /* no overlap */
150 >                                return(-1);     /* no overlap */
151 >                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
152 >                                d = beamdisk(v, op, &ourspot, nsloc);
153 >                                if (d <= FTINY)
154 >                                        return(-1);
155 >                                if (d < maxrad2) {
156 >                                        maxrad2 = d;
157 >                                        VCOPY(ocent, v);
158 >                                }
159 >                        }
160                  }
161          } else {                                /* local source */
162                  multp3(nsloc, source[sn].sloc, pm);
163                  for (i = 0; i < 3; i++)
164                          ourspot.aim[i] = ocent[i] - nsloc[i];
165 <                if ((d1 = normalize(ourspot.aim)) == 0.)
165 >                if ((d = normalize(ourspot.aim)) == 0.)
166                          return(-1);             /* at source!! */
167 <                if (source[sn].sflags & SPROX && d1 > source[sn].sl.prox)
167 >                if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
168                          return(-1);             /* too far away */
147                ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
169                  ourspot.flen = 0.;
170 +                d = (sqrt(maxrad2) + source[sn].ss) / d;
171 +                if (d < 1.-FTINY)
172 +                        ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d));
173 +                else
174 +                        nsflags &= ~SSPOT;
175                  if (source[sn].sflags & SSPOT) {
176                          copystruct(&theirspot, source[sn].sl.s);
177                          multv3(theirspot.aim, source[sn].sl.s->aim, pm);
178 <                        if (!commonspot(&ourspot, &theirspot, nsloc))
179 <                                return(-1);     /* no overlap */
180 <                        ourspot.flen = theirspot.flen;
178 >                        normalize(theirspot.aim);
179 >                        if (nsflags & SSPOT) {
180 >                                ourspot.flen = theirspot.flen;
181 >                                d = ourspot.siz;
182 >                                if (!commonspot(&ourspot, &theirspot, nsloc))
183 >                                        return(-1);     /* no overlap */
184 >                        } else {
185 >                                nsflags |= SSPOT;
186 >                                copystruct(&ourspot, &theirspot);
187 >                                d = 2.*ourspot.siz;
188 >                        }
189 >                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
190 >                                d = spotdisk(v, op, &ourspot, nsloc);
191 >                                if (d <= FTINY)
192 >                                        return(-1);
193 >                                if (d < maxrad2) {
194 >                                        maxrad2 = d;
195 >                                        VCOPY(ocent, v);
196 >                                }
197 >                        }
198                  }
199                  if (source[sn].sflags & SFLAT) {        /* behind source? */
200                          multv3(nsnorm, source[sn].snorm, pm);
201 <                        if (checkspot(&ourspot, nsnorm) < 0)
201 >                        normalize(nsnorm);
202 >                        if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm))
203                                  return(-1);
204                  }
205          }
206 <                                        /* everything is OK, make source */
206 >                                        /* pretest visibility */
207 >        nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn);
208 >        if (nsflags & SSKIP)
209 >                return(-1);     /* obstructed */
210 >                                        /* it all checks out, so make it */
211          if ((i = newsource()) < 0)
212                  goto memerr;
213          source[i].sflags = nsflags;
# Line 167 | Line 215 | MAT4  pm;
215          if (nsflags & SFLAT)
216                  VCOPY(source[i].snorm, nsnorm);
217          source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
218 <        if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
219 <                goto memerr;
220 <        copystruct(source[i].sl.s, &ourspot);
218 >        if (nsflags & SSPOT) {
219 >                if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
220 >                        goto memerr;
221 >                copystruct(source[i].sl.s, &ourspot);
222 >        }
223          if (nsflags & SPROX)
224                  source[i].sl.prox = source[sn].sl.prox;
225 <        source[i].sa.svnext = sn;
225 >        source[i].sa.sv.sn = sn;
226          source[i].so = op;
227          return(i);
228   memerr:
# Line 189 | Line 239 | register int  sn;
239          double  rad2, roffs, offs, d, rd, rdoto;
240          FVECT  rnrm, nrm;
241                                  /* first, use object getdisk function */
242 <        rad2 = (*sfun[op->otype].of->getdisk)(oc, op);
242 >        rad2 = getmaxdisk(oc, op);
243          if (!(source[sn].sflags & SVIRTUAL))
244                  return(rad2);           /* all done for normal source */
245                                  /* check for correct side of relay surface */
246 <        roffs = (*sfun[source[sn].so->otype].of->getpleq)(rnrm, source[sn].so);
246 >        roffs = getplaneq(rnrm, source[sn].so);
247          rd = DOT(rnrm, source[sn].sloc);        /* source projection */
248          if (!(source[sn].sflags & SDISTANT))
249                  rd -= roffs;
# Line 201 | Line 251 | register int  sn;
251          if ((d > 0.) ^ (rd > 0.))
252                  return(rad2);           /* OK if opposite sides */
253          if (d*d >= rad2)
254 <                return(.0);             /* no relay is possible */
254 >                return(0.);             /* no relay is possible */
255                                  /* we need a closer look */
256 <        offs = (*sfun[op->otype].of->getpleq)(nrm, op);
256 >        offs = getplaneq(nrm, op);
257          rdoto = DOT(rnrm, nrm);
258          if (d*d >= rad2*(1.-rdoto*rdoto))
259                  return(0.);             /* disk entirely on projection side */
# Line 212 | Line 262 | register int  sn;
262   }
263  
264  
265 < commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
266 < register SPOT  *sp1, *sp2;
267 < FVECT  org;
265 > int
266 > vstestvis(f, o, oc, or2, sn)            /* pretest source visibility */
267 > int  f;                 /* virtual source flags */
268 > OBJREC  *o;             /* relay object */
269 > FVECT  oc;              /* relay object center */
270 > double  or2;            /* relay object radius squared */
271 > register int  sn;       /* target source number */
272   {
273 <        FVECT  cent;
274 <        double  rad2, cos1, cos2;
275 <
276 <        cos1 = 1. - sp1->siz/(2.*PI);
277 <        cos2 = 1. - sp2->siz/(2.*PI);
278 <        if (sp2->siz >= 2.*PI-FTINY)            /* BIG, just check overlap */
279 <                return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
280 <                                        sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
281 <                                /* compute and check disks */
282 <        rad2 = intercircle(cent, sp1->aim, sp2->aim,
283 <                        1./(cos1*cos1) - 1.,  1./(cos2*cos2) - 1.);
284 <        if (rad2 <= FTINY || normalize(cent) == 0.)
285 <                return(0);
286 <        VCOPY(sp1->aim, cent);
287 <        sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
288 <        return(1);
289 < }
290 <
237 <
238 < commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
239 < register SPOT  *sp1, *sp2;
240 < FVECT  dir;
241 < {
242 <        FVECT  cent, c1, c2;
243 <        double  rad2, d;
244 <        register int  i;
245 <                                        /* move centers to common plane */
246 <        d = DOT(sp1->aim, dir);
247 <        for (i = 0; i < 3; i++)
248 <                c1[i] = sp1->aim[i] - d*dir[i];
249 <        d = DOT(sp2->aim, dir);
250 <        for (i = 0; i < 3; i++)
251 <                c2[i] = sp2->aim[i] - d*dir[i];
252 <                                        /* compute overlap */
253 <        rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
254 <        if (rad2 <= FTINY)
255 <                return(0);
256 <        VCOPY(sp1->aim, cent);
257 <        sp1->siz = PI*rad2;
258 <        return(1);
259 < }
260 <
261 <
262 < checkspot(sp, nrm)              /* check spotlight for behind source */
263 < register SPOT  *sp;
264 < FVECT  nrm;
265 < {
266 <        double  d, d1;
267 <
268 <        d = DOT(sp->aim, nrm);
269 <        if (d > FTINY)                  /* center in front? */
270 <                return(0);
271 <                                        /* else check horizon */
272 <        d1 = 1. - sp->siz/(2.*PI);
273 <        return(1.-FTINY-d*d > d1*d1);
274 < }
275 <
276 <
277 < double
278 < intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
279 < FVECT  cc;                      /* midpoint (return value) */
280 < FVECT  c1, c2;                  /* circle centers */
281 < double  r1s, r2s;               /* radii squared */
282 < {
283 <        double  a2, d2, l;
284 <        FVECT  disp;
285 <        register int  i;
286 <
287 <        for (i = 0; i < 3; i++)
288 <                disp[i] = c2[i] - c1[i];
289 <        d2 = DOT(disp,disp);
290 <                                        /* circle within overlap? */
291 <        if (r1s < r2s) {
292 <                if (r2s >= r1s + d2) {
293 <                        VCOPY(cc, c1);
294 <                        return(r1s);
295 <                }
273 >        RAY  sr;
274 >        FVECT  onorm;
275 >        FVECT  offsdir;
276 >        double  or, d;
277 >        int  infront;
278 >        int  stestlim, ssn;
279 >        int  nhit, nok;
280 >        register int  i, n;
281 >                                /* return if pretesting disabled */
282 >        if (vspretest <= 0)
283 >                return(f);
284 >                                /* get surface normal */
285 >        getplaneq(onorm, o);
286 >                                /* set number of rays to sample */
287 >        if (source[sn].sflags & SDISTANT) {
288 >                n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
289 >                                vspretest + .5;
290 >                infront = DOT(onorm, source[sn].sloc) > 0.;
291          } else {
292 <                if (r1s >= r2s + d2) {
293 <                        VCOPY(cc, c2);
294 <                        return(r2s);
292 >                for (i = 0; i < 3; i++)
293 >                        offsdir[i] = source[sn].sloc[i] - oc[i];
294 >                d = DOT(offsdir,offsdir);
295 >                if (d <= FTINY)
296 >                        n = 2.*PI * vspretest + .5;
297 >                else
298 >                        n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5;
299 >                infront = DOT(onorm, offsdir) > 0.;
300 >        }
301 >        if (n < MINSAMPLES) n = MINSAMPLES;
302 > #ifdef DEBUG
303 >        fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
304 >                        sn, o->oname, n);
305 > #endif
306 >                                /* sample */
307 >        or = sqrt(or2);
308 >        stestlim = n*STESTMAX;
309 >        ssn = 0;
310 >        nhit = nok = 0;
311 >        while (n-- > 0) {
312 >                                        /* get sample point */
313 >                do {
314 >                        if (ssn >= stestlim) {
315 > #ifdef DEBUG
316 >                                fprintf(stderr, "\ttoo hard to hit\n");
317 > #endif
318 >                                return(f);      /* too small a target! */
319 >                        }
320 >                        for (i = 0; i < 3; i++)
321 >                                offsdir[i] = or*(1. -
322 >                                        2.*urand(urind(931*i+5827,ssn)));
323 >                        ssn++;
324 >                        for (i = 0; i < 3; i++)
325 >                                sr.rorg[i] = oc[i] + offsdir[i];
326 >                        d = DOT(offsdir,onorm);
327 >                        if (infront)
328 >                                for (i = 0; i < 3; i++) {
329 >                                        sr.rorg[i] -= (d-.0001)*onorm[i];
330 >                                        sr.rdir[i] = -onorm[i];
331 >                                }
332 >                        else
333 >                                for (i = 0; i < 3; i++) {
334 >                                        sr.rorg[i] -= (d+.0001)*onorm[i];
335 >                                        sr.rdir[i] = onorm[i];
336 >                                }
337 >                        rayorigin(&sr, NULL, PRIMARY, 1.0);
338 >                } while (!(*ofun[o->otype].funp)(o, &sr));
339 >                                        /* check against source */
340 >                samplendx++;
341 >                if (srcray(&sr, NULL, sn) == 0.)
342 >                        continue;
343 >                sr.revf = srcvalue;
344 >                rayvalue(&sr);
345 >                if (bright(sr.rcol) <= FTINY)
346 >                        continue;
347 >                nok++;
348 >                                        /* check against obstructions */
349 >                rayclear(&sr);
350 >                sr.revf = raytrace;
351 >                rayvalue(&sr);
352 >                if (bright(sr.rcol) > FTINY)
353 >                        nhit++;
354 >                if (nhit > 0 && nhit < nok) {
355 > #ifdef DEBUG
356 >                        fprintf(stderr, "\tpartially occluded\n");
357 > #endif
358 >                        return(f);              /* need to shadow test */
359                  }
360          }
361 <        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
362 <                                        /* no overlap? */
363 <        if (a2 <= 0.)
364 <                return(0.);
365 <                                        /* overlap, compute center */
366 <        l = sqrt((r1s - a2)/d2);
367 <        for (i = 0; i < 3; i++)
368 <                cc[i] = c1[i] + l*disp[i];
369 <        return(a2);
361 >        if (nhit == 0) {
362 > #ifdef DEBUG
363 >                fprintf(stderr, "\t0%% hit rate\n");
364 > #endif
365 >                return(f | SSKIP);      /* 0% hit rate:  totally occluded */
366 >        }
367 > #ifdef DEBUG
368 >        fprintf(stderr, "\t100%% hit rate\n");
369 > #endif
370 >        return(f & ~SFOLLOW);           /* 100% hit rate:  no occlusion */
371   }
372 +        
373  
313
374   #ifdef DEBUG
375   virtverb(sn, fp)        /* print verbose description of virtual source */
376   register int  sn;
# Line 325 | Line 385 | FILE  *fp;
385          fprintf(fp, "\tat (%f,%f,%f)\n",
386                  source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
387          fprintf(fp, "\tlinked to source %d (%s)\n",
388 <                source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
388 >                source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname);
389          if (source[sn].sflags & SFOLLOW)
390                  fprintf(fp, "\talways followed\n");
391          else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines