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.13 by greg, Wed Jun 26 08:55:10 1991 UTC vs.
Revision 2.23 by greg, Thu Nov 8 00:54:07 2018 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     3               /* 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))
123                          if ((ns = makevsrc(o, sn, proj)) >= 0) {
124 +                                source[ns].sa.sv.pn = i;
125   #ifdef DEBUG
126                                  virtverb(ns, stderr);
127   #endif
# Line 112 | 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(objptr(i));
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 134 | Line 175 | MAT4  pm;
175                  if (source[sn].sflags & SPROX)
176                          return(-1);             /* should never get here! */
177                  multv3(nsloc, source[sn].sloc, 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) {
141                        copystruct(&theirspot, source[sn].sl.s);
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].srad;
187 +                        theirspot.siz = PI*d*d;
188 +                        ourspot.flen = theirspot.flen = source[sn].sl.s->flen;
189                          d = ourspot.siz;
190                          if (!commonbeam(&ourspot, &theirspot, nsloc))
191                                  return(-1);     /* no overlap */
# Line 161 | Line 207 | MAT4  pm;
207                          return(-1);             /* at source!! */
208                  if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
209                          return(-1);             /* too far away */
164                ourspot.siz = 2.*PI*(1. - d/sqrt(d*d+maxrad2));
210                  ourspot.flen = 0.;
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 <                        d = ourspot.siz;
221 <                        if (!commonspot(&ourspot, &theirspot, nsloc))
222 <                                return(-1);     /* no overlap */
220 >                        normalize(theirspot.aim);
221 >                        if (nsflags & SSPOT) {
222 >                                ourspot.flen = theirspot.flen;
223 >                                d = ourspot.siz;
224 >                                if (!commonspot(&ourspot, &theirspot, nsloc))
225 >                                        return(-1);     /* no overlap */
226 >                        } else {
227 >                                nsflags |= SSPOT;
228 >                                ourspot = theirspot;
229 >                                d = 2.*ourspot.siz;
230 >                        }
231                          if (ourspot.siz < d-FTINY) {    /* it shrunk */
232                                  d = spotdisk(v, op, &ourspot, nsloc);
233                                  if (d <= FTINY)
# Line 178 | Line 237 | MAT4  pm;
237                                          VCOPY(ocent, v);
238                                  }
239                          }
181                        ourspot.flen = theirspot.flen;
240                  }
241                  if (source[sn].sflags & SFLAT) {        /* behind source? */
242                          multv3(nsnorm, source[sn].snorm, pm);
243 <                        if (!checkspot(&ourspot, nsnorm))
243 >                        normalize(nsnorm);
244 >                        if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm))
245                                  return(-1);
246                  }
247          }
# Line 195 | 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;
262 <        if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
263 <                goto memerr;
264 <        copystruct(source[i].sl.s, &ourspot);
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 >                *(source[i].sl.s) = ourspot;
269 >        }
270          if (nsflags & SPROX)
271                  source[i].sl.prox = source[sn].sl.prox;
272 <        source[i].sa.svnext = sn;
272 >        source[i].sa.sv.sn = sn;
273          source[i].so = op;
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 244 | 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;
328 <        int  ssn;
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 266 | 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;
271 <                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++)
342 <                        offsdir[i] = source[sn].sloc[i] - oc[i];
343 <                n = or2/DOT(offsdir,offsdir)*vspretest + .5;
344 <                infront = DOT(onorm, offsdir) > 0.;
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;
347          }
348          if (n < MINSAMPLES) n = MINSAMPLES;
349   #ifdef DEBUG
# Line 282 | Line 352 | register int  sn;      /* target source number */
352   #endif
353                                  /* sample */
354          or = sqrt(or2);
355 <        ssn = STESTMAX*n;
355 >        stestlim = n*STESTMAX;
356 >        ssn = 0;
357          nhit = nok = 0;
358 +        initsrcindex(&si);
359          while (n-- > 0) {
360                                          /* get sample point */
361                  do {
362 <                        if (--ssn < 0) {
362 >                        if (ssn >= stestlim) {
363   #ifdef DEBUG
364                                  fprintf(stderr, "\ttoo hard to hit\n");
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. -
371 <                                                2.*urand(931*i+5827+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];
306 <                                }
307 <                        else
308 <                                for (i = 0; i < 3; i++) {
309 <                                        sr.rorg[i] -= (d+.0001)*onorm[i];
310 <                                        sr.rdir[i] = onorm[i];
311 <                                }
312 <                        rayorigin(&sr, NULL, PRIMARY, 1.0);
370 >                                offsdir[i] = or*(1. - 2.*offsdir[i]);
371 >                        ssn++;
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++;
395 <                                        /* check against obstructions */
396 <                srcray(&sr, NULL, sn);
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 346 | 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   {
353        register int  i;
354
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,
# Line 359 | Line 438 | FILE  *fp;
438          fprintf(fp, "\tat (%f,%f,%f)\n",
439                  source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
440          fprintf(fp, "\tlinked to source %d (%s)\n",
441 <                source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
441 >                source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname);
442          if (source[sn].sflags & SFOLLOW)
443                  fprintf(fp, "\talways followed\n");
444          else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines