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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines