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.20 by greg, Fri Aug 2 10:30:27 1991 UTC vs.
Revision 2.25 by greg, Fri Feb 12 00:41:19 2021 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for simulating virtual light sources
6   *      Thus far, we only support planar mirrors.
7 + *
8 + *  External symbols declared in source.h
9   */
10  
11 < #include  "ray.h"
11 > #include "copyright.h"
12  
13 < #include  "octree.h"
15 <
13 > #include  "ray.h"
14   #include  "otypes.h"
15 <
15 > #include  "otspecial.h"
16   #include  "source.h"
19
17   #include  "random.h"
18  
19 < #define  MINSAMPLES     5               /* minimum number of pretest samples */
20 < #define  STESTMAX       30              /* maximum seeks per sample */
19 > #define  MINSAMPLES     16              /* minimum number of pretest samples */
20 > #define  STESTMAX       32              /* maximum seeks per sample */
21  
22  
26 double  getdisk();
27
23   static OBJECT  *vobject;                /* virtual source objects */
24   static int  nvobjects = 0;              /* number of virtual source objects */
25  
26  
27 < markvirtuals()                  /* find and mark virtual sources */
27 > static int
28 > isident4(MAT4 m)
29   {
30 <        register OBJREC  *o;
31 <        register int  i;
30 >        int     i, j;
31 >
32 >        for (i = 4; i--; )
33 >                for (j = 4; j--; )
34 >                        if (!FABSEQ(m[i][j], i==j))
35 >                                return(0);
36 >        return(1);
37 > }
38 >
39 >
40 > void
41 > markvirtuals(void)                      /* find and mark virtual sources */
42 > {
43 >        OBJREC  *o;
44 >        int  i;
45                                          /* check number of direct relays */
46          if (directrelay <= 0)
47                  return;
48                                          /* find virtual source objects */
49 <        for (i = 0; i < nobjects; i++) {
49 >        for (i = 0; i < nsceneobjs; i++) {
50                  o = objptr(i);
51                  if (!issurface(o->otype) || o->omod == OVOID)
52                          continue;
53 <                if (!isvlight(objptr(o->omod)->otype))
53 >                if (!isvlight(vsmaterial(o)->otype))
54                          continue;
55                  if (sfun[o->otype].of == NULL ||
56 <                                sfun[o->otype].of->getpleq == NULL)
57 <                        objerror(o, USER, "illegal material");
56 >                                sfun[o->otype].of->getpleq == NULL) {
57 >                        objerror(o,WARNING,"secondary sources not supported");
58 >                        continue;
59 >                }
60                  if (nvobjects == 0)
61                          vobject = (OBJECT *)malloc(sizeof(OBJECT));
62                  else
63 <                        vobject = (OBJECT *)realloc((char *)vobject,
63 >                        vobject = (OBJECT *)realloc((void *)vobject,
64                                  (unsigned)(nvobjects+1)*sizeof(OBJECT));
65                  if (vobject == NULL)
66                          error(SYSTEM, "out of memory in addvirtuals");
# Line 64 | Line 75 | markvirtuals()                 /* find and mark virtual sources */
75          for (i = nsources; i-- > 0; )
76                  addvirtuals(i, directrelay);
77                                          /* done with our object list */
78 <        free((char *)vobject);
78 >        free((void *)vobject);
79          nvobjects = 0;
80   }
81  
82  
83 < addvirtuals(sn, nr)             /* add virtuals associated with source */
84 < int  sn;
85 < int  nr;
83 > void
84 > addvirtuals(            /* add virtuals associated with source */
85 >        int  sn,
86 >        int  nr
87 > )
88   {
89 <        register int  i;
89 >        int  i;
90                                  /* check relay limit first */
91          if (nr <= 0)
92                  return;
# Line 86 | Line 99 | int  nr;
99   }
100  
101  
102 < vproject(o, sn, n)              /* create projected source(s) if they exist */
103 < OBJREC  *o;
104 < int  sn;
105 < int  n;
102 > void
103 > vproject(               /* create projected source(s) if they exist */
104 >        OBJREC  *o,
105 >        int  sn,
106 >        int  n
107 > )
108   {
109 <        register int  i;
110 <        register VSMATERIAL  *vsmat;
109 >        int  i;
110 >        VSMATERIAL  *vsmat;
111          MAT4  proj;
112          int  ns;
113  
114          if (o == source[sn].so) /* objects cannot project themselves */
115                  return;
116                                  /* get virtual source material */
117 <        vsmat = sfun[objptr(o->omod)->otype].mf;
117 >        vsmat = sfun[vsmaterial(o)->otype].mf;
118                                  /* project virtual sources */
119          for (i = 0; i < vsmat->nproj; i++)
120                  if ((*vsmat->vproj)(proj, o, &source[sn], i))
# Line 113 | Line 128 | int  n;
128   }
129  
130  
131 + OBJREC *
132 + vsmaterial(                     /* get virtual source material pointer */
133 +        OBJREC  *o
134 + )
135 + {
136 +        int  i;
137 +        OBJREC  *m;
138 +
139 +        i = o->omod;
140 +        m = findmaterial(o);
141 +        if (m == NULL)
142 +                return(objptr(i));
143 +        if (m->otype != MAT_ILLUM || m->oargs.nsargs < 1 ||
144 +                        !strcmp(m->oargs.sarg[0], VOIDID) ||
145 +                        (i = lastmod(objndx(m), m->oargs.sarg[0])) == OVOID)
146 +                return(m);              /* direct modifier */
147 +        return(objptr(i));              /* illum alternate */
148 + }
149 +
150 +
151   int
152 < makevsrc(op, sn, pm)            /* make virtual source if reasonable */
153 < OBJREC  *op;
154 < register int  sn;
155 < MAT4  pm;
152 > makevsrc(               /* make virtual source if reasonable */
153 >        OBJREC  *op,
154 >        int  sn,
155 >        MAT4  pm
156 > )
157   {
158          FVECT  nsloc, nsnorm, ocent, v;
159          double  maxrad2, d;
160          int  nsflags;
161          SPOT  theirspot, ourspot;
162 <        register int  i;
163 <
162 >        int  i;
163 >                                        /* check for no-op */
164 >        if (isident4(pm))
165 >                return(0);
166          nsflags = source[sn].sflags | (SVIRTUAL|SSPOT|SFOLLOW);
167                                          /* get object center and max. radius */
168          maxrad2 = getdisk(ocent, op, sn);
# Line 138 | Line 176 | MAT4  pm;
176                  normalize(nsloc);
177                  VCOPY(ourspot.aim, ocent);
178                  ourspot.siz = PI*maxrad2;
179 <                ourspot.flen = 0.;
179 >                ourspot.flen = -1.;
180                  if (source[sn].sflags & SSPOT) {
181                          multp3(theirspot.aim, source[sn].sl.s->aim, pm);
182 +                                                /* adjust for source size */
183                          d = sqrt(dist2(ourspot.aim, theirspot.aim));
184 <                        d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].ss;
184 >                        d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].srad;
185                          theirspot.siz = PI*d*d;
186                          ourspot.flen = theirspot.flen = source[sn].sl.s->flen;
187                          d = ourspot.siz;
# Line 167 | Line 206 | MAT4  pm;
206                  if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
207                          return(-1);             /* too far away */
208                  ourspot.flen = 0.;
209 <                d = (sqrt(maxrad2) + source[sn].ss) / d;
209 >                                                /* adjust for source size */
210 >                d = (sqrt(maxrad2) + source[sn].srad) / d;
211                  if (d < 1.-FTINY)
212                          ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d));
213                  else
214                          nsflags &= ~SSPOT;
215                  if (source[sn].sflags & SSPOT) {
216 <                        copystruct(&theirspot, source[sn].sl.s);
216 >                        theirspot = *(source[sn].sl.s);
217                          multv3(theirspot.aim, source[sn].sl.s->aim, pm);
218                          normalize(theirspot.aim);
219                          if (nsflags & SSPOT) {
# Line 183 | Line 223 | MAT4  pm;
223                                          return(-1);     /* no overlap */
224                          } else {
225                                  nsflags |= SSPOT;
226 <                                copystruct(&ourspot, &theirspot);
226 >                                ourspot = theirspot;
227                                  d = 2.*ourspot.siz;
228                          }
229                          if (ourspot.siz < d-FTINY) {    /* it shrunk */
# Line 212 | Line 252 | MAT4  pm;
252                  goto memerr;
253          source[i].sflags = nsflags;
254          VCOPY(source[i].sloc, nsloc);
255 +        multv3(source[i].ss[SU], source[sn].ss[SU], pm);
256 +        multv3(source[i].ss[SV], source[sn].ss[SV], pm);
257          if (nsflags & SFLAT)
258                  VCOPY(source[i].snorm, nsnorm);
259 <        source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
259 >        else
260 >                multv3(source[i].ss[SW], source[sn].ss[SW], pm);
261 >        source[i].srad = source[sn].srad;
262 >        source[i].ss2 = source[sn].ss2;
263          if (nsflags & SSPOT) {
264                  if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
265                          goto memerr;
266 <                copystruct(source[i].sl.s, &ourspot);
266 >                *(source[i].sl.s) = ourspot;
267          }
268          if (nsflags & SPROX)
269                  source[i].sl.prox = source[sn].sl.prox;
# Line 227 | Line 272 | MAT4  pm;
272          return(i);
273   memerr:
274          error(SYSTEM, "out of memory in makevsrc");
275 +        return -1; /* pro forma return */
276   }
277  
278  
279   double
280 < getdisk(oc, op, sn)             /* get visible object disk */
281 < FVECT  oc;
282 < OBJREC  *op;
283 < register int  sn;
280 > getdisk(                /* get visible object disk */
281 >        FVECT  oc,
282 >        OBJREC  *op,
283 >        int  sn
284 > )
285   {
286          double  rad2, roffs, offs, d, rd, rdoto;
287          FVECT  rnrm, nrm;
# Line 263 | Line 310 | register int  sn;
310  
311  
312   int
313 < vstestvis(f, o, oc, or2, sn)            /* pretest source visibility */
314 < int  f;                 /* virtual source flags */
315 < OBJREC  *o;             /* relay object */
316 < FVECT  oc;              /* relay object center */
317 < double  or2;            /* relay object radius squared */
318 < register int  sn;       /* target source number */
313 > vstestvis(              /* pretest source visibility */
314 >        int  f,                 /* virtual source flags */
315 >        OBJREC  *o,             /* relay object */
316 >        FVECT  oc,              /* relay object center */
317 >        double  or2,            /* relay object radius squared */
318 >        int  sn /* target source number */
319 > )
320   {
321          RAY  sr;
322          FVECT  onorm;
323 <        FVECT  offsdir;
324 <        double  or, d;
325 <        int  infront;
323 >        double  offsdir[3];
324 >        SRCINDEX  si;
325 >        double  or, d, d1;
326          int  stestlim, ssn;
327          int  nhit, nok;
328 <        register int  i, n;
328 >        int  i, n;
329                                  /* return if pretesting disabled */
330          if (vspretest <= 0)
331                  return(f);
# Line 285 | Line 333 | register int  sn;      /* target source number */
333          getplaneq(onorm, o);
334                                  /* set number of rays to sample */
335          if (source[sn].sflags & SDISTANT) {
336 <                n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
337 <                                vspretest + .5;
290 <                infront = DOT(onorm, source[sn].sloc) > 0.;
336 >                                        /* 32. == heuristic constant */
337 >                n = 32.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5;
338          } else {
339 <                for (i = 0; i < 3; i++)
293 <                        offsdir[i] = source[sn].sloc[i] - oc[i];
339 >                VSUB(offsdir, source[sn].sloc, oc);
340                  d = DOT(offsdir,offsdir);
341                  if (d <= FTINY)
342                          n = 2.*PI * vspretest + .5;
343                  else
344                          n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5;
299                infront = DOT(onorm, offsdir) > 0.;
345          }
346          if (n < MINSAMPLES) n = MINSAMPLES;
347   #ifdef DEBUG
# Line 308 | Line 353 | register int  sn;      /* target source number */
353          stestlim = n*STESTMAX;
354          ssn = 0;
355          nhit = nok = 0;
356 +        initsrcindex(&si);
357          while (n-- > 0) {
358                                          /* get sample point */
359                  do {
# Line 317 | Line 363 | register int  sn;      /* target source number */
363   #endif
364                                  return(f);      /* too small a target! */
365                          }
366 +                        multisamp(offsdir, 3, urand(sn*931+5827+ssn));
367                          for (i = 0; i < 3; i++)
368 <                                offsdir[i] = or*(1. -
322 <                                        2.*urand(urind(931*i+5827,ssn)));
368 >                                offsdir[i] = or*(1. - 2.*offsdir[i]);
369                          ssn++;
370 <                        for (i = 0; i < 3; i++)
371 <                                sr.rorg[i] = oc[i] + offsdir[i];
372 <                        d = DOT(offsdir,onorm);
373 <                        if (infront)
374 <                                for (i = 0; i < 3; i++) {
375 <                                        sr.rorg[i] -= (d-.0001)*onorm[i];
376 <                                        sr.rdir[i] = -onorm[i];
331 <                                }
332 <                        else
333 <                                for (i = 0; i < 3; i++) {
334 <                                        sr.rorg[i] -= (d+.0001)*onorm[i];
335 <                                        sr.rdir[i] = onorm[i];
336 <                                }
337 <                        rayorigin(&sr, NULL, PRIMARY, 1.0);
370 >                        d = 1. - DOT(offsdir, onorm);
371 >                        for (i = 0; i < 3; i++) {
372 >                                sr.rorg[i] = oc[i] + offsdir[i] + d*onorm[i];
373 >                                sr.rdir[i] = -onorm[i];
374 >                        }
375 >                        sr.rmax = 0.0;
376 >                        rayorigin(&sr, PRIMARY, NULL, NULL);
377                  } while (!(*ofun[o->otype].funp)(o, &sr));
378                                          /* check against source */
379 +                VCOPY(sr.rorg, sr.rop); /* starting from intersection */
380                  samplendx++;
381 <                if (srcray(&sr, NULL, sn) == 0.)
382 <                        continue;
381 >                if (si.sp >= si.np-1 ||
382 >                                !srcray(&sr, NULL, &si) || sr.rsrc != sn) {
383 >                        si.sn = sn-1;           /* reset index to our source */
384 >                        si.np = 0;
385 >                        if (!srcray(&sr, NULL, &si) || sr.rsrc != sn)
386 >                                continue;       /* can't get there from here */
387 >                }
388                  sr.revf = srcvalue;
389 <                rayvalue(&sr);
390 <                if (bright(sr.rcol) <= FTINY)
389 >                rayvalue(&sr);                  /* check sample validity */
390 >                if ((d = bright(sr.rcol)) <= FTINY)
391                          continue;
392 <                nok++;
348 <                                        /* check against obstructions */
392 >                nok++;                  /* got sample; check obstructions */
393                  rayclear(&sr);
394                  sr.revf = raytrace;
395                  rayvalue(&sr);
396 <                if (bright(sr.rcol) > FTINY)
396 >                if ((d1 = bright(sr.rcol)) > FTINY) {
397 >                        if (d - d1 > FTINY) {
398 > #ifdef DEBUG
399 >                                fprintf(stderr, "\tpartially shadowed\n");
400 > #endif
401 >                                return(f);      /* intervening transmitter */
402 >                        }
403                          nhit++;
404 +                }
405                  if (nhit > 0 && nhit < nok) {
406   #ifdef DEBUG
407                          fprintf(stderr, "\tpartially occluded\n");
# Line 372 | Line 423 | register int  sn;      /* target source number */
423          
424  
425   #ifdef DEBUG
426 < virtverb(sn, fp)        /* print verbose description of virtual source */
427 < register int  sn;
428 < FILE  *fp;
426 > void
427 > virtverb(       /* print verbose description of virtual source */
428 >        int  sn,
429 >        FILE  *fp
430 > )
431   {
379        register int  i;
380
432          fprintf(fp, "%s virtual source %d in %s %s\n",
433                          source[sn].sflags & SDISTANT ? "distant" : "local",
434                          sn, ofun[source[sn].so->otype].funame,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines