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.4 by greg, Thu Jun 20 16:36:48 1991 UTC vs.
Revision 1.14 by greg, Wed Jun 26 11:54:43 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();
22 > #define  MINSAMPLES     3               /* 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 97 | Line 105 | int  n;
105                  if ((*vsmat->vproj)(proj, o, &source[sn], i))
106                          if ((ns = makevsrc(o, sn, proj)) >= 0) {
107   #ifdef DEBUG
108 <                                virtverb(&source[sn], stderr);
108 >                                virtverb(ns, stderr);
109   #endif
110                                  addvirtuals(ns, n);
111                          }
# Line 110 | Line 118 | OBJREC  *op;
118   register int  sn;
119   MAT4  pm;
120   {
121 <        register int  nsn;
122 <        FVECT  nsloc, ocent, nsnorm;
121 >        FVECT  nsloc, nsnorm, ocent, v;
122 >        double  maxrad2, d;
123          int  nsflags;
116        double  maxrad2;
117        double  d1;
124          SPOT  theirspot, ourspot;
125          register int  i;
126  
127 <        nsflags = (source[sn].sflags|(SVIRTUAL|SFOLLOW)) & ~SSPOT;
127 >        nsflags = source[sn].sflags | (SVIRTUAL|SSPOT|SFOLLOW);
128                                          /* get object center and max. radius */
129 <        if (sfun[op->otype].of->getdisk != NULL) {
130 <                maxrad2 = (*sfun[op->otype].of->getdisk)(ocent, op);
131 <                if (maxrad2 <= FTINY)                   /* too small? */
126 <                        return(NULL);
127 <                nsflags |= SSPOT;
128 <        }
129 >        maxrad2 = getdisk(ocent, op, sn);
130 >        if (maxrad2 <= FTINY)                   /* too small? */
131 >                return(-1);
132                                          /* get location and spot */
133          if (source[sn].sflags & SDISTANT) {             /* distant source */
134                  if (source[sn].sflags & SPROX)
135 <                        return(NULL);           /* should never get here! */
135 >                        return(-1);             /* should never get here! */
136                  multv3(nsloc, source[sn].sloc, pm);
137 <                if (nsflags & SSPOT) {
138 <                        VCOPY(ourspot.aim, ocent);
139 <                        ourspot.siz = PI*maxrad2;
137 <                        ourspot.flen = 0.;
138 <                }
137 >                VCOPY(ourspot.aim, ocent);
138 >                ourspot.siz = PI*maxrad2;
139 >                ourspot.flen = 0.;
140                  if (source[sn].sflags & SSPOT) {
141                          copystruct(&theirspot, source[sn].sl.s);
142                          multp3(theirspot.aim, source[sn].sl.s->aim, pm);
143 <                        if (nsflags & SSPOT &&
144 <                                !commonbeam(&ourspot, &theirspot, nsloc))
145 <                                return(NULL);           /* no overlap */
143 >                        d = ourspot.siz;
144 >                        if (!commonbeam(&ourspot, &theirspot, nsloc))
145 >                                return(-1);     /* no overlap */
146 >                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
147 >                                d = beamdisk(v, op, &ourspot, nsloc);
148 >                                if (d <= FTINY)
149 >                                        return(-1);
150 >                                if (d < maxrad2) {
151 >                                        maxrad2 = d;
152 >                                        VCOPY(ocent, v);
153 >                                }
154 >                        }
155                  }
156          } else {                                /* local source */
157                  multp3(nsloc, source[sn].sloc, pm);
158 <                if (nsflags & SSPOT) {
159 <                        for (i = 0; i < 3; i++)
160 <                                ourspot.aim[i] = ocent[i] - nsloc[i];
161 <                        if ((d1 = normalize(ourspot.aim)) == 0.)
162 <                                return(NULL);           /* at source!! */
163 <                        if (source[sn].sflags & SPROX &&
164 <                                        d1 > source[sn].sl.prox)
165 <                                return(NULL);           /* too far away */
166 <                        ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
167 <                        ourspot.flen = 0.;
168 <                } else if (source[sn].sflags & SPROX) {
159 <                        FVECT  norm;
160 <                        double  offs;
161 <                                                /* use distance from plane */
162 <                        offs = (*sfun[op->otype].of->getpleq)(norm, op);
163 <                        d1 = DOT(norm, nsloc) - offs;
164 <                        if (d1 < 0.) d1 = -d1;
165 <                        if (d1 > source[sn].sl.prox)
166 <                                return(NULL);           /* too far away */
167 <                }
158 >                for (i = 0; i < 3; i++)
159 >                        ourspot.aim[i] = ocent[i] - nsloc[i];
160 >                if ((d = normalize(ourspot.aim)) == 0.)
161 >                        return(-1);             /* at source!! */
162 >                if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
163 >                        return(-1);             /* too far away */
164 >                ourspot.flen = 0.;
165 >                if (d*d > maxrad2)
166 >                        ourspot.siz = 2.*PI*(1. - sqrt(1.-maxrad2/(d*d)));
167 >                else
168 >                        nsflags &= ~SSPOT;
169                  if (source[sn].sflags & SSPOT) {
170                          copystruct(&theirspot, source[sn].sl.s);
171                          multv3(theirspot.aim, source[sn].sl.s->aim, pm);
172                          if (nsflags & SSPOT) {
172                                if (!commonspot(&ourspot, &theirspot, nsloc))
173                                        return(NULL);   /* no overlap */
173                                  ourspot.flen = theirspot.flen;
174 +                                d = ourspot.siz;
175 +                                if (!commonspot(&ourspot, &theirspot, nsloc))
176 +                                        return(-1);     /* no overlap */
177 +                        } else {
178 +                                nsflags |= SSPOT;
179 +                                copystruct(&ourspot, &theirspot);
180 +                                d = 2.*ourspot.siz;
181                          }
182 +                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
183 +                                d = spotdisk(v, op, &ourspot, nsloc);
184 +                                if (d <= FTINY)
185 +                                        return(-1);
186 +                                if (d < maxrad2) {
187 +                                        maxrad2 = d;
188 +                                        VCOPY(ocent, v);
189 +                                }
190 +                        }
191                  }
192                  if (source[sn].sflags & SFLAT) {        /* behind source? */
193                          multv3(nsnorm, source[sn].snorm, pm);
194 <                        if (nsflags & SSPOT && checkspot(&ourspot, nsnorm) < 0)
195 <                                return(NULL);
194 >                        if (!checkspot(&ourspot, nsnorm))
195 >                                return(-1);
196                  }
197          }
198 <                                        /* everything is OK, make source */
199 <        if ((nsn = newsource()) < 0)
198 >                                        /* pretest visibility */
199 >        nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn);
200 >        if (nsflags & SSKIP)
201 >                return(-1);     /* obstructed */
202 >                                        /* it all checks out, so make it */
203 >        if ((i = newsource()) < 0)
204                  goto memerr;
205 <        source[nsn].sflags = nsflags;
206 <        VCOPY(source[nsn].sloc, nsloc);
205 >        source[i].sflags = nsflags;
206 >        VCOPY(source[i].sloc, nsloc);
207          if (nsflags & SFLAT)
208 <                VCOPY(source[nsn].snorm, nsnorm);
209 <        source[nsn].ss = source[sn].ss; source[nsn].ss2 = source[sn].ss2;
210 <        if ((nsflags | source[sn].sflags) & SSPOT) {
211 <                if ((source[nsn].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
208 >                VCOPY(source[i].snorm, nsnorm);
209 >        source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
210 >        if (nsflags & SSPOT) {
211 >                if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
212                          goto memerr;
213 <                if (nsflags & SSPOT)
195 <                        copystruct(source[nsn].sl.s, &ourspot);
196 <                else
197 <                        copystruct(source[nsn].sl.s, &theirspot);
198 <                source[nsn].sflags |= SSPOT;
213 >                copystruct(source[i].sl.s, &ourspot);
214          }
215          if (nsflags & SPROX)
216 <                source[nsn].sl.prox = source[sn].sl.prox;
217 <        source[nsn].sa.svnext = sn;
218 <        source[nsn].so = op;
219 <        return(nsn);
216 >                source[i].sl.prox = source[sn].sl.prox;
217 >        source[i].sa.svnext = sn;
218 >        source[i].so = op;
219 >        return(i);
220   memerr:
221          error(SYSTEM, "out of memory in makevsrc");
222   }
223  
224  
225 < commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
226 < register SPOT  *sp1, *sp2;
227 < FVECT  org;
225 > double
226 > getdisk(oc, op, sn)             /* get visible object disk */
227 > FVECT  oc;
228 > OBJREC  *op;
229 > register int  sn;
230   {
231 <        FVECT  cent;
232 <        double  rad2, cos1, cos2;
233 <
234 <        cos1 = 1. - sp1->siz/(2.*PI);
235 <        cos2 = 1. - sp2->siz/(2.*PI);
236 <        if (sp2->siz >= 2.*PI-FTINY)            /* BIG, just check overlap */
237 <                return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
238 <                                        sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
239 <                                /* compute and check disks */
240 <        rad2 = intercircle(cent, sp1->aim, sp2->aim,
241 <                        1./(cos1*cos1) - 1.,  1./(cos2*cos2) - 1.);
242 <        if (rad2 <= FTINY || normalize(cent) == 0.)
243 <                return(0);
244 <        VCOPY(sp1->aim, cent);
245 <        sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
246 <        return(1);
231 >        double  rad2, roffs, offs, d, rd, rdoto;
232 >        FVECT  rnrm, nrm;
233 >                                /* first, use object getdisk function */
234 >        rad2 = getmaxdisk(oc, op);
235 >        if (!(source[sn].sflags & SVIRTUAL))
236 >                return(rad2);           /* all done for normal source */
237 >                                /* check for correct side of relay surface */
238 >        roffs = getplaneq(rnrm, source[sn].so);
239 >        rd = DOT(rnrm, source[sn].sloc);        /* source projection */
240 >        if (!(source[sn].sflags & SDISTANT))
241 >                rd -= roffs;
242 >        d = DOT(rnrm, oc) - roffs;      /* disk distance to relay plane */
243 >        if ((d > 0.) ^ (rd > 0.))
244 >                return(rad2);           /* OK if opposite sides */
245 >        if (d*d >= rad2)
246 >                return(0.);             /* no relay is possible */
247 >                                /* we need a closer look */
248 >        offs = getplaneq(nrm, op);
249 >        rdoto = DOT(rnrm, nrm);
250 >        if (d*d >= rad2*(1.-rdoto*rdoto))
251 >                return(0.);             /* disk entirely on projection side */
252 >                                /* should shrink disk but I'm lazy */
253 >        return(rad2);
254   }
255  
256  
257 < commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
258 < register SPOT  *sp1, *sp2;
259 < FVECT  dir;
257 > int
258 > vstestvis(f, o, oc, or2, sn)            /* pretest source visibility */
259 > int  f;                 /* virtual source flags */
260 > OBJREC  *o;             /* relay object */
261 > FVECT  oc;              /* relay object center */
262 > double  or2;            /* relay object radius squared */
263 > register int  sn;       /* target source number */
264   {
265 <        FVECT  cent, c1, c2;
266 <        double  rad2, d;
267 <        register int  i;
268 <                                        /* move centers to common plane */
269 <        d = DOT(sp1->aim, dir);
270 <        for (i = 0; i < 3; i++)
271 <                c1[i] = sp1->aim[i] - d*dir[i];
272 <        d = DOT(sp2->aim, dir);
273 <        for (i = 0; i < 3; i++)
274 <                c2[i] = sp2->aim[i] - d*dir[i];
275 <                                        /* compute overlap */
276 <        rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
277 <        if (rad2 <= FTINY)
278 <                return(0);
279 <        VCOPY(sp1->aim, cent);
280 <        sp1->siz = PI*rad2;
281 <        return(1);
282 < }
255 <
256 <
257 < checkspot(sp, nrm)              /* check spotlight for behind source */
258 < register SPOT  *sp;
259 < FVECT  nrm;
260 < {
261 <        double  d, d1;
262 <
263 <        d = DOT(sp->aim, nrm);
264 <        if (d > FTINY)                  /* center in front? */
265 <                return(0);
266 <                                        /* else check horizon */
267 <        d1 = 1. - sp->siz/(2.*PI);
268 <        return(1.-FTINY-d*d > d1*d1);
269 < }
270 <
271 <
272 < double
273 < intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
274 < FVECT  cc;                      /* midpoint (return value) */
275 < FVECT  c1, c2;                  /* circle centers */
276 < double  r1s, r2s;               /* radii squared */
277 < {
278 <        double  a2, d2, l;
279 <        FVECT  disp;
280 <        register int  i;
281 <
282 <        for (i = 0; i < 3; i++)
283 <                disp[i] = c2[i] - c1[i];
284 <        d2 = DOT(disp,disp);
285 <                                        /* circle within overlap? */
286 <        if (r1s < r2s) {
287 <                if (r2s >= r1s + d2) {
288 <                        VCOPY(cc, c1);
289 <                        return(r1s);
290 <                }
265 >        RAY  sr;
266 >        FVECT  onorm;
267 >        FVECT  offsdir;
268 >        double  or, d;
269 >        int  infront;
270 >        int  ssn;
271 >        int  nhit, nok;
272 >        register int  i, n;
273 >                                /* return if pretesting disabled */
274 >        if (vspretest <= 0)
275 >                return(f);
276 >                                /* get surface normal */
277 >        getplaneq(onorm, o);
278 >                                /* set number of rays to sample */
279 >        if (source[sn].sflags & SDISTANT) {
280 >                n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
281 >                                vspretest + .5;
282 >                infront = DOT(onorm, source[sn].sloc) > 0.;
283          } else {
284 <                if (r1s >= r2s + d2) {
285 <                        VCOPY(cc, c2);
286 <                        return(r2s);
284 >                for (i = 0; i < 3; i++)
285 >                        offsdir[i] = source[sn].sloc[i] - oc[i];
286 >                n = or2/DOT(offsdir,offsdir)*vspretest + .5;
287 >                infront = DOT(onorm, offsdir) > 0.;
288 >        }
289 >        if (n < MINSAMPLES) n = MINSAMPLES;
290 > #ifdef DEBUG
291 >        fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
292 >                        sn, o->oname, n);
293 > #endif
294 >                                /* sample */
295 >        or = sqrt(or2);
296 >        ssn = STESTMAX*n;
297 >        nhit = nok = 0;
298 >        while (n-- > 0) {
299 >                                        /* get sample point */
300 >                do {
301 >                        if (--ssn < 0) {
302 > #ifdef DEBUG
303 >                                fprintf(stderr, "\ttoo hard to hit\n");
304 > #endif
305 >                                return(f);      /* too small a target! */
306 >                        }
307 >                        for (i = 0; i < 3; i++)
308 >                                offsdir[i] = or*(1. -
309 >                                                2.*urand(931*i+5827+ssn));
310 >                        for (i = 0; i < 3; i++)
311 >                                sr.rorg[i] = oc[i] + offsdir[i];
312 >                        d = DOT(offsdir,onorm);
313 >                        if (infront)
314 >                                for (i = 0; i < 3; i++) {
315 >                                        sr.rorg[i] -= (d-.0001)*onorm[i];
316 >                                        sr.rdir[i] = -onorm[i];
317 >                                }
318 >                        else
319 >                                for (i = 0; i < 3; i++) {
320 >                                        sr.rorg[i] -= (d+.0001)*onorm[i];
321 >                                        sr.rdir[i] = onorm[i];
322 >                                }
323 >                        rayorigin(&sr, NULL, PRIMARY, 1.0);
324 >                } while (!(*ofun[o->otype].funp)(o, &sr));
325 >                                        /* check against source */
326 >                samplendx++;
327 >                if (srcray(&sr, NULL, sn) == 0.)
328 >                        continue;
329 >                sr.revf = srcvalue;
330 >                rayvalue(&sr);
331 >                if (bright(sr.rcol) <= FTINY)
332 >                        continue;
333 >                nok++;
334 >                                        /* check against obstructions */
335 >                srcray(&sr, NULL, sn);
336 >                rayvalue(&sr);
337 >                if (bright(sr.rcol) > FTINY)
338 >                        nhit++;
339 >                if (nhit > 0 && nhit < nok) {
340 > #ifdef DEBUG
341 >                        fprintf(stderr, "\tpartially occluded\n");
342 > #endif
343 >                        return(f);              /* need to shadow test */
344                  }
345          }
346 <        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
347 <                                        /* no overlap? */
348 <        if (a2 <= 0.)
349 <                return(0.);
350 <                                        /* overlap, compute center */
351 <        l = sqrt((r1s - a2)/d2);
352 <        for (i = 0; i < 3; i++)
353 <                cc[i] = c1[i] + l*disp[i];
354 <        return(a2);
346 >        if (nhit == 0) {
347 > #ifdef DEBUG
348 >                fprintf(stderr, "\t0%% hit rate\n");
349 > #endif
350 >                return(f | SSKIP);      /* 0% hit rate:  totally occluded */
351 >        }
352 > #ifdef DEBUG
353 >        fprintf(stderr, "\t100%% hit rate\n");
354 > #endif
355 >        return(f & ~SFOLLOW);           /* 100% hit rate:  no occlusion */
356   }
357 +        
358  
308
359   #ifdef DEBUG
360 < virtverb(vs, fp)        /* print verbose description of virtual source */
361 < register SRCREC  *vs;
360 > virtverb(sn, fp)        /* print verbose description of virtual source */
361 > register int  sn;
362   FILE  *fp;
363   {
364          register int  i;
365  
366          fprintf(fp, "%s virtual source %d in %s %s\n",
367 <                        vs->sflags & SDISTANT ? "distant" : "local",
368 <                        vs-source, ofun[vs->so->otype].funame, vs->so->oname);
367 >                        source[sn].sflags & SDISTANT ? "distant" : "local",
368 >                        sn, ofun[source[sn].so->otype].funame,
369 >                        source[sn].so->oname);
370          fprintf(fp, "\tat (%f,%f,%f)\n",
371 <                        vs->sloc[0], vs->sloc[1], vs->sloc[2]);
371 >                source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
372          fprintf(fp, "\tlinked to source %d (%s)\n",
373 <                        vs->sa.svnext, source[vs->sa.svnext].so->oname);
374 <        if (vs->sflags & SFOLLOW)
373 >                source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
374 >        if (source[sn].sflags & SFOLLOW)
375                  fprintf(fp, "\talways followed\n");
376          else
377                  fprintf(fp, "\tnever followed\n");
378 <        if (!(vs->sflags & SSPOT))
378 >        if (!(source[sn].sflags & SSPOT))
379                  return;
380          fprintf(fp, "\twith spot aim (%f,%f,%f) and size %f\n",
381 <                        vs->sl.s->aim[0], vs->sl.s->aim[1], vs->sl.s->aim[2],
382 <                        vs->sl.s->siz);
381 >                        source[sn].sl.s->aim[0], source[sn].sl.s->aim[1],
382 >                        source[sn].sl.s->aim[2], source[sn].sl.s->siz);
383   }
384   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines