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.7 by greg, Mon Jun 24 16:10:59 1991 UTC vs.
Revision 1.21 by greg, Mon Aug 5 09:02:42 1991 UTC

# Line 19 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19  
20   #include  "random.h"
21  
22 + #define  MINSAMPLES     5               /* minimum number of pretest samples */
23 + #define  STESTMAX       30              /* maximum seeks per sample */
24  
23 #define  DISKTFRAC      0.25            /* disk area pretest fraction */
25  
26   double  getdisk();
27  
# Line 43 | 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 103 | 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 117 | 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;
123        double  d1;
127          SPOT  theirspot, ourspot;
128          register int  i;
129  
# Line 134 | 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) {
141                        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 */
154                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          }
# Line 178 | 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 200 | 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 212 | 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 235 | Line 276 | register int  sn;      /* target source number */
276          FVECT  onorm;
277          FVECT  offsdir;
278          double  or, d;
279 <        int  nok, nhit;
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 <        (*sfun[o->otype].of->getpleq)(onorm, o);
287 >        getplaneq(onorm, o);
288                                  /* set number of rays to sample */
289 <        if (source[sn].sflags & SDISTANT)
289 >        if (source[sn].sflags & SDISTANT) {
290                  n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
291                                  vspretest + .5;
292 <        else
293 <                n = or2/dist2(oc,source[sn].sloc)*vspretest + .5;
294 <        if (n < 1) n = 1;
295 <                                /* limit tests to central region */
296 <        or = DISKTFRAC*sqrt(or2);
292 >                infront = DOT(onorm, source[sn].sloc) > 0.;
293 >        } else {
294 >                for (i = 0; i < 3; i++)
295 >                        offsdir[i] = source[sn].sloc[i] - oc[i];
296 >                d = DOT(offsdir,offsdir);
297 >                if (d <= FTINY)
298 >                        n = 2.*PI * vspretest + .5;
299 >                else
300 >                        n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5;
301 >                infront = DOT(onorm, offsdir) > 0.;
302 >        }
303 >        if (n < MINSAMPLES) n = MINSAMPLES;
304 > #ifdef DEBUG
305 >        fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
306 >                        sn, o->oname, n);
307 > #endif
308                                  /* sample */
309 +        or = sqrt(or2);
310 +        stestlim = n*STESTMAX;
311 +        ssn = 0;
312          nhit = nok = 0;
313          while (n-- > 0) {
314 <                samplendx++;
315 <                /*
316 <                 * We're being real sloppy with our sample locations here.
317 <                 */
318 <                for (i = 0; i < 3; i++)
319 <                        offsdir[i] = or*(1. - 2.*urand(931*i+5821+n));
320 <                d = DOT(offsdir,onorm);
321 <                for (i = 0; i < 3; i++)
322 <                        sr.rorg[i] = oc[i] + (1.-d)*offsdir[i];
314 >                                        /* get sample point */
315 >                do {
316 >                        if (ssn >= stestlim) {
317 > #ifdef DEBUG
318 >                                fprintf(stderr, "\ttoo hard to hit\n");
319 > #endif
320 >                                return(f);      /* too small a target! */
321 >                        }
322 >                        for (i = 0; i < 3; i++)
323 >                                offsdir[i] = or*(1. -
324 >                                        2.*urand(urind(931*i+5827,ssn)));
325 >                        ssn++;
326 >                        for (i = 0; i < 3; i++)
327 >                                sr.rorg[i] = oc[i] + offsdir[i];
328 >                        d = DOT(offsdir,onorm);
329 >                        if (infront)
330 >                                for (i = 0; i < 3; i++) {
331 >                                        sr.rorg[i] -= (d-.0001)*onorm[i];
332 >                                        sr.rdir[i] = -onorm[i];
333 >                                }
334 >                        else
335 >                                for (i = 0; i < 3; i++) {
336 >                                        sr.rorg[i] -= (d+.0001)*onorm[i];
337 >                                        sr.rdir[i] = onorm[i];
338 >                                }
339 >                        rayorigin(&sr, NULL, PRIMARY, 1.0);
340 >                } while (!(*ofun[o->otype].funp)(o, &sr));
341                                          /* check against source */
342 <                if (srcray(&sr, NULL, sn) == 0.0)
342 >                samplendx++;
343 >                if (srcray(&sr, NULL, sn) == 0.)
344                          continue;
345                  sr.revf = srcvalue;
346                  rayvalue(&sr);
# Line 272 | Line 348 | register int  sn;      /* target source number */
348                          continue;
349                  nok++;
350                                          /* check against obstructions */
351 <                srcray(&sr, NULL, sn);
351 >                rayclear(&sr);
352 >                sr.revf = raytrace;
353                  rayvalue(&sr);
354 <                if (bright(sr.rcol) <= FTINY)
355 <                        continue;
356 <                nhit++;
354 >                if (bright(sr.rcol) > FTINY)
355 >                        nhit++;
356 >                if (nhit > 0 && nhit < nok) {
357 > #ifdef DEBUG
358 >                        fprintf(stderr, "\tpartially occluded\n");
359 > #endif
360 >                        return(f);              /* need to shadow test */
361 >                }
362          }
363 <                                /* interpret results */
364 <        if (nhit == 0)
363 >        if (nhit == 0) {
364 > #ifdef DEBUG
365 >                fprintf(stderr, "\t0%% hit rate\n");
366 > #endif
367                  return(f | SSKIP);      /* 0% hit rate:  totally occluded */
368 <        if (nhit == nok)
369 <                return(f & ~SFOLLOW);   /* 100% hit rate:  no occlusion */
370 <        return(f);              /* no comment */
368 >        }
369 > #ifdef DEBUG
370 >        fprintf(stderr, "\t100%% hit rate\n");
371 > #endif
372 >        return(f & ~SFOLLOW);           /* 100% hit rate:  no occlusion */
373   }
374          
375  
# Line 301 | Line 387 | FILE  *fp;
387          fprintf(fp, "\tat (%f,%f,%f)\n",
388                  source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
389          fprintf(fp, "\tlinked to source %d (%s)\n",
390 <                source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
390 >                source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname);
391          if (source[sn].sflags & SFOLLOW)
392                  fprintf(fp, "\talways followed\n");
393          else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines