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

Comparing ray/src/rt/virtuals.c (file contents):
Revision 1.6 by greg, Fri Jun 21 13:25:42 1991 UTC vs.
Revision 1.24 by greg, Fri Aug 16 09:23:15 1991 UTC

# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11  
12   #include  "ray.h"
13  
14 + #include  "octree.h"
15 +
16   #include  "otypes.h"
17  
18   #include  "source.h"
19  
20 + #include  "random.h"
21  
22 < double  intercircle(), getdisk();
22 > #define  MINSAMPLES     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(ns, stderr);
112   #endif
# Line 110 | Line 121 | OBJREC  *op;
121   register int  sn;
122   MAT4  pm;
123   {
124 <        FVECT  nsloc, nsnorm, ocent;
125 <        double  maxrad2;
124 >        FVECT  nsloc, nsnorm, ocent, v;
125 >        double  maxrad2, d;
126          int  nsflags;
116        double  d1;
127          SPOT  theirspot, ourspot;
128          register int  i;
129  
# Line 127 | Line 137 | MAT4  pm;
137                  if (source[sn].sflags & SPROX)
138                          return(-1);             /* should never get here! */
139                  multv3(nsloc, source[sn].sloc, pm);
140 +                normalize(nsloc);
141                  VCOPY(ourspot.aim, ocent);
142                  ourspot.siz = PI*maxrad2;
143                  ourspot.flen = 0.;
144                  if (source[sn].sflags & SSPOT) {
134                        copystruct(&theirspot, source[sn].sl.s);
145                          multp3(theirspot.aim, source[sn].sl.s->aim, pm);
146 +                        d = sqrt(dist2(ourspot.aim, theirspot.aim));
147 +                        d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].ss;
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 */
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                  for (i = 0; i < 3; i++)
166                          ourspot.aim[i] = ocent[i] - nsloc[i];
167 <                if ((d1 = normalize(ourspot.aim)) == 0.)
167 >                if ((d = normalize(ourspot.aim)) == 0.)
168                          return(-1);             /* at source!! */
169 <                if (source[sn].sflags & SPROX && d1 > source[sn].sl.prox)
169 >                if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
170                          return(-1);             /* too far away */
147                ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
171                  ourspot.flen = 0.;
172 +                d = (sqrt(maxrad2) + source[sn].ss) / 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 <                        if (!commonspot(&ourspot, &theirspot, nsloc))
181 <                                return(-1);     /* no overlap */
182 <                        ourspot.flen = theirspot.flen;
180 >                        normalize(theirspot.aim);
181 >                        if (nsflags & SSPOT) {
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 (checkspot(&ourspot, nsnorm) < 0)
203 >                        normalize(nsnorm);
204 >                        if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm))
205                                  return(-1);
206                  }
207          }
208 <                                        /* everything is OK, make source */
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[i].sflags = nsflags;
# Line 167 | Line 217 | MAT4  pm;
217          if (nsflags & SFLAT)
218                  VCOPY(source[i].snorm, nsnorm);
219          source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
220 <        if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
221 <                goto memerr;
222 <        copystruct(source[i].sl.s, &ourspot);
220 >        if (nsflags & SSPOT) {
221 >                if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
222 >                        goto memerr;
223 >                copystruct(source[i].sl.s, &ourspot);
224 >        }
225          if (nsflags & SPROX)
226                  source[i].sl.prox = source[sn].sl.prox;
227 <        source[i].sa.svnext = sn;
227 >        source[i].sa.sv.sn = sn;
228          source[i].so = op;
229          return(i);
230   memerr:
# Line 189 | Line 241 | register int  sn;
241          double  rad2, roffs, offs, d, rd, rdoto;
242          FVECT  rnrm, nrm;
243                                  /* first, use object getdisk function */
244 <        rad2 = (*sfun[op->otype].of->getdisk)(oc, op);
244 >        rad2 = getmaxdisk(oc, op);
245          if (!(source[sn].sflags & SVIRTUAL))
246                  return(rad2);           /* all done for normal source */
247                                  /* check for correct side of relay surface */
248 <        roffs = (*sfun[source[sn].so->otype].of->getpleq)(rnrm, source[sn].so);
248 >        roffs = getplaneq(rnrm, source[sn].so);
249          rd = DOT(rnrm, source[sn].sloc);        /* source projection */
250          if (!(source[sn].sflags & SDISTANT))
251                  rd -= roffs;
# Line 201 | Line 253 | register int  sn;
253          if ((d > 0.) ^ (rd > 0.))
254                  return(rad2);           /* OK if opposite sides */
255          if (d*d >= rad2)
256 <                return(.0);             /* no relay is possible */
256 >                return(0.);             /* no relay is possible */
257                                  /* we need a closer look */
258 <        offs = (*sfun[op->otype].of->getpleq)(nrm, op);
258 >        offs = getplaneq(nrm, op);
259          rdoto = DOT(rnrm, nrm);
260          if (d*d >= rad2*(1.-rdoto*rdoto))
261                  return(0.);             /* disk entirely on projection side */
# Line 212 | Line 264 | register int  sn;
264   }
265  
266  
267 < commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
268 < register SPOT  *sp1, *sp2;
269 < FVECT  org;
267 > int
268 > vstestvis(f, o, oc, or2, sn)            /* pretest source visibility */
269 > int  f;                 /* virtual source flags */
270 > OBJREC  *o;             /* relay object */
271 > FVECT  oc;              /* relay object center */
272 > double  or2;            /* relay object radius squared */
273 > register int  sn;       /* target source number */
274   {
275 <        FVECT  cent;
276 <        double  rad2, cos1, cos2;
277 <
278 <        cos1 = 1. - sp1->siz/(2.*PI);
279 <        cos2 = 1. - sp2->siz/(2.*PI);
280 <        if (sp2->siz >= 2.*PI-FTINY)            /* BIG, just check overlap */
281 <                return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
282 <                                        sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
283 <                                /* compute and check disks */
284 <        rad2 = intercircle(cent, sp1->aim, sp2->aim,
285 <                        1./(cos1*cos1) - 1.,  1./(cos2*cos2) - 1.);
286 <        if (rad2 <= FTINY || normalize(cent) == 0.)
287 <                return(0);
288 <        VCOPY(sp1->aim, cent);
289 <        sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
290 <        return(1);
291 < }
236 <
237 <
238 < commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
239 < register SPOT  *sp1, *sp2;
240 < FVECT  dir;
241 < {
242 <        FVECT  cent, c1, c2;
243 <        double  rad2, d;
244 <        register int  i;
245 <                                        /* move centers to common plane */
246 <        d = DOT(sp1->aim, dir);
247 <        for (i = 0; i < 3; i++)
248 <                c1[i] = sp1->aim[i] - d*dir[i];
249 <        d = DOT(sp2->aim, dir);
250 <        for (i = 0; i < 3; i++)
251 <                c2[i] = sp2->aim[i] - d*dir[i];
252 <                                        /* compute overlap */
253 <        rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
254 <        if (rad2 <= FTINY)
255 <                return(0);
256 <        VCOPY(sp1->aim, cent);
257 <        sp1->siz = PI*rad2;
258 <        return(1);
259 < }
260 <
261 <
262 < checkspot(sp, nrm)              /* check spotlight for behind source */
263 < register SPOT  *sp;
264 < FVECT  nrm;
265 < {
266 <        double  d, d1;
267 <
268 <        d = DOT(sp->aim, nrm);
269 <        if (d > FTINY)                  /* center in front? */
270 <                return(0);
271 <                                        /* else check horizon */
272 <        d1 = 1. - sp->siz/(2.*PI);
273 <        return(1.-FTINY-d*d > d1*d1);
274 < }
275 <
276 <
277 < double
278 < intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
279 < FVECT  cc;                      /* midpoint (return value) */
280 < FVECT  c1, c2;                  /* circle centers */
281 < double  r1s, r2s;               /* radii squared */
282 < {
283 <        double  a2, d2, l;
284 <        FVECT  disp;
285 <        register int  i;
286 <
287 <        for (i = 0; i < 3; i++)
288 <                disp[i] = c2[i] - c1[i];
289 <        d2 = DOT(disp,disp);
290 <                                        /* circle within overlap? */
291 <        if (r1s < r2s) {
292 <                if (r2s >= r1s + d2) {
293 <                        VCOPY(cc, c1);
294 <                        return(r1s);
295 <                }
275 >        RAY  sr;
276 >        FVECT  onorm;
277 >        FVECT  offsdir;
278 >        double  or, d;
279 >        int  infront;
280 >        int  stestlim, ssn;
281 >        int  nhit, nok;
282 >        register int  i, n;
283 >                                /* return if pretesting disabled */
284 >        if (vspretest <= 0)
285 >                return(f);
286 >                                /* get surface normal */
287 >        getplaneq(onorm, o);
288 >                                /* set number of rays to sample */
289 >        if (source[sn].sflags & SDISTANT) {
290 >                n = 4.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5;
291 >                infront = DOT(onorm, source[sn].sloc) > 0.;
292          } else {
293 <                if (r1s >= r2s + d2) {
294 <                        VCOPY(cc, c2);
295 <                        return(r2s);
293 >                for (i = 0; i < 3; i++)
294 >                        offsdir[i] = source[sn].sloc[i] - oc[i];
295 >                d = DOT(offsdir,offsdir);
296 >                if (d <= FTINY)
297 >                        n = 2.*PI * vspretest + .5;
298 >                else
299 >                        n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5;
300 >                infront = DOT(onorm, offsdir) > 0.;
301 >        }
302 >        if (n < MINSAMPLES) n = MINSAMPLES;
303 > #ifdef DEBUG
304 >        fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
305 >                        sn, o->oname, n);
306 > #endif
307 >                                /* sample */
308 >        or = sqrt(or2);
309 >        stestlim = n*STESTMAX;
310 >        ssn = 0;
311 >        nhit = nok = 0;
312 >        while (n-- > 0) {
313 >                                        /* get sample point */
314 >                do {
315 >                        if (ssn >= stestlim) {
316 > #ifdef DEBUG
317 >                                fprintf(stderr, "\ttoo hard to hit\n");
318 > #endif
319 >                                return(f);      /* too small a target! */
320 >                        }
321 >                        peano(offsdir, 3, urand(sn*931+5827+ssn), .005);
322 >                        for (i = 0; i < 3; i++)
323 >                                offsdir[i] = or*(1. - 2.*offsdir[i]);
324 >                        ssn++;
325 >                        for (i = 0; i < 3; i++)
326 >                                sr.rorg[i] = oc[i] + offsdir[i];
327 >                        d = DOT(offsdir,onorm);
328 >                        if (infront)
329 >                                for (i = 0; i < 3; i++) {
330 >                                        sr.rorg[i] -= (d-.0001)*onorm[i];
331 >                                        sr.rdir[i] = -onorm[i];
332 >                                }
333 >                        else
334 >                                for (i = 0; i < 3; i++) {
335 >                                        sr.rorg[i] -= (d+.0001)*onorm[i];
336 >                                        sr.rdir[i] = onorm[i];
337 >                                }
338 >                        rayorigin(&sr, NULL, PRIMARY, 1.0);
339 >                } while (!(*ofun[o->otype].funp)(o, &sr));
340 >                                        /* check against source */
341 >                samplendx++;
342 >                if (srcray(&sr, NULL, sn) == 0.)
343 >                        continue;
344 >                sr.revf = srcvalue;
345 >                rayvalue(&sr);
346 >                if (bright(sr.rcol) <= FTINY)
347 >                        continue;
348 >                nok++;
349 >                                        /* check against obstructions */
350 >                rayclear(&sr);
351 >                if (!localhit(&sr, &thescene) || sr.ro == source[sn].so)
352 >                        nhit++;
353 >                if (nhit > 0 && nhit < nok) {
354 > #ifdef DEBUG
355 >                        fprintf(stderr, "\tpartially occluded\n");
356 > #endif
357 >                        return(f);              /* need to shadow test */
358                  }
359          }
360 <        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
361 <                                        /* no overlap? */
362 <        if (a2 <= 0.)
363 <                return(0.);
364 <                                        /* overlap, compute center */
365 <        l = sqrt((r1s - a2)/d2);
366 <        for (i = 0; i < 3; i++)
367 <                cc[i] = c1[i] + l*disp[i];
368 <        return(a2);
360 >        if (nhit == 0) {
361 > #ifdef DEBUG
362 >                fprintf(stderr, "\t0%% hit rate\n");
363 > #endif
364 >                return(f | SSKIP);      /* 0% hit rate:  totally occluded */
365 >        }
366 > #ifdef DEBUG
367 >        fprintf(stderr, "\t100%% hit rate\n");
368 > #endif
369 >        return(f & ~SFOLLOW);           /* 100% hit rate:  no occlusion */
370   }
371 +        
372  
313
373   #ifdef DEBUG
374   virtverb(sn, fp)        /* print verbose description of virtual source */
375   register int  sn;
# Line 325 | Line 384 | FILE  *fp;
384          fprintf(fp, "\tat (%f,%f,%f)\n",
385                  source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
386          fprintf(fp, "\tlinked to source %d (%s)\n",
387 <                source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
387 >                source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname);
388          if (source[sn].sflags & SFOLLOW)
389                  fprintf(fp, "\talways followed\n");
390          else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines