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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines