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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines