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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines