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.8 by greg, Tue Jun 25 08:52:04 1991 UTC vs.
Revision 2.6 by gwlarson, Mon Aug 10 18:37:06 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
# Line 19 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19  
20   #include  "random.h"
21  
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 */
# Line 38 | Line 41 | markvirtuals()                 /* find and mark virtual sources */
41                  o = objptr(i);
42                  if (!issurface(o->otype) || o->omod == OVOID)
43                          continue;
44 <                if (!isvlight(objptr(o->omod)->otype))
44 >                if (!isvlight(vsmaterial(o)->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 96 | Line 101 | int  n;
101          if (o == source[sn].so) /* objects cannot project themselves */
102                  return;
103                                  /* get virtual source material */
104 <        vsmat = sfun[objptr(o->omod)->otype].mf;
104 >        vsmat = sfun[vsmaterial(o)->otype].mf;
105                                  /* project virtual sources */
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 109 | Line 115 | int  n;
115   }
116  
117  
118 + OBJREC *
119 + vsmaterial(o)                   /* get virtual source material pointer */
120 + OBJREC  *o;
121 + {
122 +        register int  i;
123 +        register OBJREC  *m;
124 +
125 +        i = o->omod;
126 +        m = objptr(i);
127 +        if (m->otype != MAT_ILLUM || m->oargs.nsargs < 1 ||
128 +                        !strcmp(m->oargs.sarg[0], VOIDID) ||
129 +                        (i = lastmod(objndx(m), m->oargs.sarg[0])) == OVOID)
130 +                return(m);              /* direct modifier */
131 +        return(objptr(i));              /* illum alternate */
132 + }
133 +
134 +
135   int
136   makevsrc(op, sn, pm)            /* make virtual source if reasonable */
137   OBJREC  *op;
138   register int  sn;
139   MAT4  pm;
140   {
141 <        FVECT  nsloc, nsnorm, ocent;
142 <        double  maxrad2;
141 >        FVECT  nsloc, nsnorm, ocent, v;
142 >        double  maxrad2, d;
143          int  nsflags;
121        double  d1;
144          SPOT  theirspot, ourspot;
145          register int  i;
146  
# Line 132 | Line 154 | MAT4  pm;
154                  if (source[sn].sflags & SPROX)
155                          return(-1);             /* should never get here! */
156                  multv3(nsloc, source[sn].sloc, pm);
157 +                normalize(nsloc);
158                  VCOPY(ourspot.aim, ocent);
159                  ourspot.siz = PI*maxrad2;
160 <                ourspot.flen = 0.;
160 >                ourspot.flen = -1.;
161                  if (source[sn].sflags & SSPOT) {
139                        copystruct(&theirspot, source[sn].sl.s);
162                          multp3(theirspot.aim, source[sn].sl.s->aim, pm);
163 +                                                /* adjust for source size */
164 +                        d = sqrt(dist2(ourspot.aim, theirspot.aim));
165 +                        d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].srad;
166 +                        theirspot.siz = PI*d*d;
167 +                        ourspot.flen = theirspot.flen = source[sn].sl.s->flen;
168 +                        d = ourspot.siz;
169                          if (!commonbeam(&ourspot, &theirspot, nsloc))
170 <                                return(-1);             /* no overlap */
170 >                                return(-1);     /* no overlap */
171 >                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
172 >                                d = beamdisk(v, op, &ourspot, nsloc);
173 >                                if (d <= FTINY)
174 >                                        return(-1);
175 >                                if (d < maxrad2) {
176 >                                        maxrad2 = d;
177 >                                        VCOPY(ocent, v);
178 >                                }
179 >                        }
180                  }
181          } else {                                /* local source */
182                  multp3(nsloc, source[sn].sloc, pm);
183                  for (i = 0; i < 3; i++)
184                          ourspot.aim[i] = ocent[i] - nsloc[i];
185 <                if ((d1 = normalize(ourspot.aim)) == 0.)
185 >                if ((d = normalize(ourspot.aim)) == 0.)
186                          return(-1);             /* at source!! */
187 <                if (source[sn].sflags & SPROX && d1 > source[sn].sl.prox)
187 >                if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
188                          return(-1);             /* too far away */
152                ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
189                  ourspot.flen = 0.;
190 +                                                /* adjust for source size */
191 +                d = (sqrt(maxrad2) + source[sn].srad) / d;
192 +                if (d < 1.-FTINY)
193 +                        ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d));
194 +                else
195 +                        nsflags &= ~SSPOT;
196                  if (source[sn].sflags & SSPOT) {
197                          copystruct(&theirspot, source[sn].sl.s);
198                          multv3(theirspot.aim, source[sn].sl.s->aim, pm);
199 <                        if (!commonspot(&ourspot, &theirspot, nsloc))
200 <                                return(-1);     /* no overlap */
201 <                        ourspot.flen = theirspot.flen;
199 >                        normalize(theirspot.aim);
200 >                        if (nsflags & SSPOT) {
201 >                                ourspot.flen = theirspot.flen;
202 >                                d = ourspot.siz;
203 >                                if (!commonspot(&ourspot, &theirspot, nsloc))
204 >                                        return(-1);     /* no overlap */
205 >                        } else {
206 >                                nsflags |= SSPOT;
207 >                                copystruct(&ourspot, &theirspot);
208 >                                d = 2.*ourspot.siz;
209 >                        }
210 >                        if (ourspot.siz < d-FTINY) {    /* it shrunk */
211 >                                d = spotdisk(v, op, &ourspot, nsloc);
212 >                                if (d <= FTINY)
213 >                                        return(-1);
214 >                                if (d < maxrad2) {
215 >                                        maxrad2 = d;
216 >                                        VCOPY(ocent, v);
217 >                                }
218 >                        }
219                  }
220                  if (source[sn].sflags & SFLAT) {        /* behind source? */
221                          multv3(nsnorm, source[sn].snorm, pm);
222 <                        if (checkspot(&ourspot, nsnorm) < 0)
222 >                        normalize(nsnorm);
223 >                        if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm))
224                                  return(-1);
225                  }
226          }
# Line 173 | Line 233 | MAT4  pm;
233                  goto memerr;
234          source[i].sflags = nsflags;
235          VCOPY(source[i].sloc, nsloc);
236 +        multv3(source[i].ss[SU], source[sn].ss[SU], pm);
237 +        multv3(source[i].ss[SV], source[sn].ss[SV], pm);
238          if (nsflags & SFLAT)
239                  VCOPY(source[i].snorm, nsnorm);
240 <        source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
241 <        if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
242 <                goto memerr;
243 <        copystruct(source[i].sl.s, &ourspot);
240 >        else
241 >                multv3(source[i].ss[SW], source[sn].ss[SW], pm);
242 >        source[i].srad = source[sn].srad;
243 >        source[i].ss2 = source[sn].ss2;
244 >        if (nsflags & SSPOT) {
245 >                if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
246 >                        goto memerr;
247 >                copystruct(source[i].sl.s, &ourspot);
248 >        }
249          if (nsflags & SPROX)
250                  source[i].sl.prox = source[sn].sl.prox;
251 <        source[i].sa.svnext = sn;
251 >        source[i].sa.sv.sn = sn;
252          source[i].so = op;
253          return(i);
254   memerr:
# Line 198 | Line 265 | register int  sn;
265          double  rad2, roffs, offs, d, rd, rdoto;
266          FVECT  rnrm, nrm;
267                                  /* first, use object getdisk function */
268 <        rad2 = (*sfun[op->otype].of->getdisk)(oc, op);
268 >        rad2 = getmaxdisk(oc, op);
269          if (!(source[sn].sflags & SVIRTUAL))
270                  return(rad2);           /* all done for normal source */
271                                  /* check for correct side of relay surface */
272 <        roffs = (*sfun[source[sn].so->otype].of->getpleq)(rnrm, source[sn].so);
272 >        roffs = getplaneq(rnrm, source[sn].so);
273          rd = DOT(rnrm, source[sn].sloc);        /* source projection */
274          if (!(source[sn].sflags & SDISTANT))
275                  rd -= roffs;
# Line 210 | Line 277 | register int  sn;
277          if ((d > 0.) ^ (rd > 0.))
278                  return(rad2);           /* OK if opposite sides */
279          if (d*d >= rad2)
280 <                return(.0);             /* no relay is possible */
280 >                return(0.);             /* no relay is possible */
281                                  /* we need a closer look */
282 <        offs = (*sfun[op->otype].of->getpleq)(nrm, op);
282 >        offs = getplaneq(nrm, op);
283          rdoto = DOT(rnrm, nrm);
284          if (d*d >= rad2*(1.-rdoto*rdoto))
285                  return(0.);             /* disk entirely on projection side */
# Line 232 | Line 299 | register int  sn;      /* target source number */
299          RAY  sr;
300          FVECT  onorm;
301          FVECT  offsdir;
302 +        SRCINDEX  si;
303          double  or, d;
304 <        int  infront;
305 <        int  ssn;
238 <        int  nok, nhit;
304 >        int  stestlim, ssn;
305 >        int  nhit, nok;
306          register int  i, n;
307                                  /* return if pretesting disabled */
308          if (vspretest <= 0)
309                  return(f);
310                                  /* get surface normal */
311 <        (*sfun[o->otype].of->getpleq)(onorm, o);
311 >        getplaneq(onorm, o);
312                                  /* set number of rays to sample */
313          if (source[sn].sflags & SDISTANT) {
314 <                n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
315 <                                vspretest + .5;
249 <                infront = DOT(onorm, source[sn].sloc) > 0.;
314 >                                        /* 32. == heuristic constant */
315 >                n = 32.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5;
316          } else {
251                n = or2/dist2(oc,source[sn].sloc)*vspretest + .5;
317                  for (i = 0; i < 3; i++)
318                          offsdir[i] = source[sn].sloc[i] - oc[i];
319 <                infront = DOT(onorm, offsdir) > 0.;
319 >                d = DOT(offsdir,offsdir);
320 >                if (d <= FTINY)
321 >                        n = 2.*PI * vspretest + .5;
322 >                else
323 >                        n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5;
324          }
325 <        if (n < 1) n = 1;
325 >        if (n < MINSAMPLES) n = MINSAMPLES;
326 > #ifdef DEBUG
327 >        fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
328 >                        sn, o->oname, n);
329 > #endif
330                                  /* sample */
331          or = sqrt(or2);
332 <        ssn = 7*n;
332 >        stestlim = n*STESTMAX;
333 >        ssn = 0;
334          nhit = nok = 0;
335 +        initsrcindex(&si);
336          while (n-- > 0) {
337                                          /* get sample point */
338                  do {
339 <                        if (--ssn < 0)
339 >                        if (ssn >= stestlim) {
340 > #ifdef DEBUG
341 >                                fprintf(stderr, "\ttoo hard to hit\n");
342 > #endif
343                                  return(f);      /* too small a target! */
344 +                        }
345 +                        multisamp(offsdir, 3, urand(sn*931+5827+ssn));
346                          for (i = 0; i < 3; i++)
347 <                                offsdir[i] = or*(1. -
348 <                                                2.*urand(931*i+5827+ssn));
349 <                        for (i = 0; i < 3; i++)
350 <                                sr.rorg[i] = oc[i] + offsdir[i];
351 <                        d = DOT(offsdir,onorm);
352 <                        if (infront)
353 <                                for (i = 0; i < 3; i++) {
354 <                                        sr.rorg[i] -= (d-.0001)*onorm[i];
275 <                                        sr.rdir[i] = -onorm[i];
276 <                                }
277 <                        else
278 <                                for (i = 0; i < 3; i++) {
279 <                                        sr.rorg[i] -= (d+.0001)*onorm[i];
280 <                                        sr.rdir[i] = onorm[i];
281 <                                }
347 >                                offsdir[i] = or*(1. - 2.*offsdir[i]);
348 >                        ssn++;
349 >                        d = 1. - DOT(offsdir, onorm);
350 >                        for (i = 0; i < 3; i++) {
351 >                                sr.rorg[i] = oc[i] + offsdir[i] + d*onorm[i];
352 >                                sr.rdir[i] = -onorm[i];
353 >                        }
354 >                        sr.rmax = 0.0;
355                          rayorigin(&sr, NULL, PRIMARY, 1.0);
356                  } while (!(*ofun[o->otype].funp)(o, &sr));
357                                          /* check against source */
358 +                VCOPY(sr.rorg, sr.rop); /* starting from intersection */
359                  samplendx++;
360 <                if (srcray(&sr, NULL, sn) == 0.0)
361 <                        continue;
360 >                if (si.sp >= si.np-1 ||
361 >                                !srcray(&sr, NULL, &si) || sr.rsrc != sn) {
362 >                        si.sn = sn-1;           /* reset index to our source */
363 >                        si.np = 0;
364 >                        if (!srcray(&sr, NULL, &si) || sr.rsrc != sn)
365 >                                continue;       /* can't get there from here */
366 >                }
367                  sr.revf = srcvalue;
368 <                rayvalue(&sr);
368 >                rayvalue(&sr);                  /* check sample validity */
369                  if (bright(sr.rcol) <= FTINY)
370                          continue;
371 <                nok++;
372 <                                        /* check against obstructions */
373 <                srcray(&sr, NULL, sn);
371 >                nok++;                  /* got sample; check obstructions */
372 >                rayclear(&sr);
373 >                sr.revf = raytrace;
374                  rayvalue(&sr);
375 <                if (bright(sr.rcol) <= FTINY)
376 <                        continue;
377 <                nhit++;
375 >                if (bright(sr.rcol) > FTINY)
376 >                        nhit++;
377 >                if (nhit > 0 && nhit < nok) {
378 > #ifdef DEBUG
379 >                        fprintf(stderr, "\tpartially occluded\n");
380 > #endif
381 >                        return(f);              /* need to shadow test */
382 >                }
383          }
384 <                                /* interpret results */
385 <        if (nhit == 0)
384 >        if (nhit == 0) {
385 > #ifdef DEBUG
386 >                fprintf(stderr, "\t0%% hit rate\n");
387 > #endif
388                  return(f | SSKIP);      /* 0% hit rate:  totally occluded */
389 <        if (nhit == nok)
390 <                return(f & ~SFOLLOW);   /* 100% hit rate:  no occlusion */
391 <        return(f);              /* no comment */
389 >        }
390 > #ifdef DEBUG
391 >        fprintf(stderr, "\t100%% hit rate\n");
392 > #endif
393 >        return(f & ~SFOLLOW);           /* 100% hit rate:  no occlusion */
394   }
395          
396  
# Line 320 | Line 408 | FILE  *fp;
408          fprintf(fp, "\tat (%f,%f,%f)\n",
409                  source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
410          fprintf(fp, "\tlinked to source %d (%s)\n",
411 <                source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
411 >                source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname);
412          if (source[sn].sflags & SFOLLOW)
413                  fprintf(fp, "\talways followed\n");
414          else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines