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 2.1 by greg, Tue Nov 12 17:10:33 1991 UTC vs.
Revision 2.21 by greg, Tue Jul 15 23:44:53 2014 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 "copyright.h"
12 +
13   #include  "ray.h"
14  
14 #include  "octree.h"
15
15   #include  "otypes.h"
16  
17   #include  "source.h"
# Line 22 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21   #define  MINSAMPLES     16              /* minimum number of pretest samples */
22   #define  STESTMAX       32              /* maximum seeks per sample */
23  
24 + #define FEQ(a,b)        ((a)-(b)+FTINY >= 0 && (b)-(a)+FTINY >= 0)
25  
26 double  getdisk();
26  
27   static OBJECT  *vobject;                /* virtual source objects */
28   static int  nvobjects = 0;              /* number of virtual source objects */
29  
30  
31 < markvirtuals()                  /* find and mark virtual sources */
31 > static int
32 > isident4(MAT4 m)
33   {
34 <        register OBJREC  *o;
35 <        register int  i;
34 >        int     i, j;
35 >
36 >        for (i = 4; i--; )
37 >                for (j = 4; j--; )
38 >                        if (!FEQ(m[i][j], i==j))
39 >                                return(0);
40 >        return(1);
41 > }
42 >
43 >
44 > void
45 > markvirtuals(void)                      /* find and mark virtual sources */
46 > {
47 >        OBJREC  *o;
48 >        int  i;
49                                          /* check number of direct relays */
50          if (directrelay <= 0)
51                  return;
52                                          /* find virtual source objects */
53 <        for (i = 0; i < nobjects; i++) {
53 >        for (i = 0; i < nsceneobjs; i++) {
54                  o = objptr(i);
55                  if (!issurface(o->otype) || o->omod == OVOID)
56                          continue;
# Line 51 | Line 64 | markvirtuals()                 /* find and mark virtual sources */
64                  if (nvobjects == 0)
65                          vobject = (OBJECT *)malloc(sizeof(OBJECT));
66                  else
67 <                        vobject = (OBJECT *)realloc((char *)vobject,
67 >                        vobject = (OBJECT *)realloc((void *)vobject,
68                                  (unsigned)(nvobjects+1)*sizeof(OBJECT));
69                  if (vobject == NULL)
70                          error(SYSTEM, "out of memory in addvirtuals");
# Line 66 | Line 79 | markvirtuals()                 /* find and mark virtual sources */
79          for (i = nsources; i-- > 0; )
80                  addvirtuals(i, directrelay);
81                                          /* done with our object list */
82 <        free((char *)vobject);
82 >        free((void *)vobject);
83          nvobjects = 0;
84   }
85  
86  
87 < addvirtuals(sn, nr)             /* add virtuals associated with source */
88 < int  sn;
89 < int  nr;
87 > void
88 > addvirtuals(            /* add virtuals associated with source */
89 >        int  sn,
90 >        int  nr
91 > )
92   {
93 <        register int  i;
93 >        int  i;
94                                  /* check relay limit first */
95          if (nr <= 0)
96                  return;
# Line 88 | Line 103 | int  nr;
103   }
104  
105  
106 < vproject(o, sn, n)              /* create projected source(s) if they exist */
107 < OBJREC  *o;
108 < int  sn;
109 < int  n;
106 > void
107 > vproject(               /* create projected source(s) if they exist */
108 >        OBJREC  *o,
109 >        int  sn,
110 >        int  n
111 > )
112   {
113 <        register int  i;
114 <        register VSMATERIAL  *vsmat;
113 >        int  i;
114 >        VSMATERIAL  *vsmat;
115          MAT4  proj;
116          int  ns;
117  
# Line 116 | Line 133 | int  n;
133  
134  
135   OBJREC *
136 < vsmaterial(o)                   /* get virtual source material pointer */
137 < OBJREC  *o;
136 > vsmaterial(                     /* get virtual source material pointer */
137 >        OBJREC  *o
138 > )
139   {
140 <        register int  i;
141 <        register OBJREC  *m;
140 >        int  i;
141 >        OBJREC  *m;
142  
143          i = o->omod;
144 <        m = objptr(i);
144 >        m = findmaterial(objptr(i));
145 >        if (m == NULL)
146 >                return(objptr(i));
147          if (m->otype != MAT_ILLUM || m->oargs.nsargs < 1 ||
148                          !strcmp(m->oargs.sarg[0], VOIDID) ||
149 <                        (i = modifier(m->oargs.sarg[0])) < 0)
149 >                        (i = lastmod(objndx(m), m->oargs.sarg[0])) == OVOID)
150                  return(m);              /* direct modifier */
151          return(objptr(i));              /* illum alternate */
152   }
153  
154  
155   int
156 < makevsrc(op, sn, pm)            /* make virtual source if reasonable */
157 < OBJREC  *op;
158 < register int  sn;
159 < MAT4  pm;
156 > makevsrc(               /* make virtual source if reasonable */
157 >        OBJREC  *op,
158 >        int  sn,
159 >        MAT4  pm
160 > )
161   {
162          FVECT  nsloc, nsnorm, ocent, v;
163          double  maxrad2, d;
164          int  nsflags;
165          SPOT  theirspot, ourspot;
166 <        register int  i;
167 <
166 >        int  i;
167 >                                        /* check for no-op */
168 >        if (isident4(pm))
169 >                return(0);
170          nsflags = source[sn].sflags | (SVIRTUAL|SSPOT|SFOLLOW);
171                                          /* get object center and max. radius */
172          maxrad2 = getdisk(ocent, op, sn);
# Line 157 | Line 180 | MAT4  pm;
180                  normalize(nsloc);
181                  VCOPY(ourspot.aim, ocent);
182                  ourspot.siz = PI*maxrad2;
183 <                ourspot.flen = 0.;
183 >                ourspot.flen = -1.;
184                  if (source[sn].sflags & SSPOT) {
185                          multp3(theirspot.aim, source[sn].sl.s->aim, pm);
186                                                  /* adjust for source size */
# Line 194 | Line 217 | MAT4  pm;
217                  else
218                          nsflags &= ~SSPOT;
219                  if (source[sn].sflags & SSPOT) {
220 <                        copystruct(&theirspot, source[sn].sl.s);
220 >                        theirspot = *(source[sn].sl.s);
221                          multv3(theirspot.aim, source[sn].sl.s->aim, pm);
222                          normalize(theirspot.aim);
223                          if (nsflags & SSPOT) {
# Line 204 | Line 227 | MAT4  pm;
227                                          return(-1);     /* no overlap */
228                          } else {
229                                  nsflags |= SSPOT;
230 <                                copystruct(&ourspot, &theirspot);
230 >                                ourspot = theirspot;
231                                  d = 2.*ourspot.siz;
232                          }
233                          if (ourspot.siz < d-FTINY) {    /* it shrunk */
# Line 244 | Line 267 | MAT4  pm;
267          if (nsflags & SSPOT) {
268                  if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
269                          goto memerr;
270 <                copystruct(source[i].sl.s, &ourspot);
270 >                *(source[i].sl.s) = ourspot;
271          }
272          if (nsflags & SPROX)
273                  source[i].sl.prox = source[sn].sl.prox;
# Line 253 | Line 276 | MAT4  pm;
276          return(i);
277   memerr:
278          error(SYSTEM, "out of memory in makevsrc");
279 +        return -1; /* pro forma return */
280   }
281  
282  
283   double
284 < getdisk(oc, op, sn)             /* get visible object disk */
285 < FVECT  oc;
286 < OBJREC  *op;
287 < register int  sn;
284 > getdisk(                /* get visible object disk */
285 >        FVECT  oc,
286 >        OBJREC  *op,
287 >        int  sn
288 > )
289   {
290          double  rad2, roffs, offs, d, rd, rdoto;
291          FVECT  rnrm, nrm;
# Line 289 | Line 314 | register int  sn;
314  
315  
316   int
317 < vstestvis(f, o, oc, or2, sn)            /* pretest source visibility */
318 < int  f;                 /* virtual source flags */
319 < OBJREC  *o;             /* relay object */
320 < FVECT  oc;              /* relay object center */
321 < double  or2;            /* relay object radius squared */
322 < register int  sn;       /* target source number */
317 > vstestvis(              /* pretest source visibility */
318 >        int  f,                 /* virtual source flags */
319 >        OBJREC  *o,             /* relay object */
320 >        FVECT  oc,              /* relay object center */
321 >        double  or2,            /* relay object radius squared */
322 >        int  sn /* target source number */
323 > )
324   {
325          RAY  sr;
326          FVECT  onorm;
327 <        FVECT  offsdir;
327 >        double  offsdir[3];
328          SRCINDEX  si;
329 <        double  or, d;
304 <        int  infront;
329 >        double  or, d, d1;
330          int  stestlim, ssn;
331          int  nhit, nok;
332 <        register int  i, n;
332 >        int  i, n;
333                                  /* return if pretesting disabled */
334          if (vspretest <= 0)
335                  return(f);
# Line 314 | Line 339 | register int  sn;      /* target source number */
339          if (source[sn].sflags & SDISTANT) {
340                                          /* 32. == heuristic constant */
341                  n = 32.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5;
317                infront = DOT(onorm, source[sn].sloc) > 0.;
342          } else {
343 <                for (i = 0; i < 3; i++)
320 <                        offsdir[i] = source[sn].sloc[i] - oc[i];
343 >                VSUB(offsdir, source[sn].sloc, oc);
344                  d = DOT(offsdir,offsdir);
345                  if (d <= FTINY)
346                          n = 2.*PI * vspretest + .5;
347                  else
348                          n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5;
326                infront = DOT(onorm, offsdir) > 0.;
349          }
350          if (n < MINSAMPLES) n = MINSAMPLES;
351   #ifdef DEBUG
# Line 335 | Line 357 | register int  sn;      /* target source number */
357          stestlim = n*STESTMAX;
358          ssn = 0;
359          nhit = nok = 0;
360 +        initsrcindex(&si);
361          while (n-- > 0) {
362                                          /* get sample point */
363                  do {
# Line 348 | Line 371 | register int  sn;      /* target source number */
371                          for (i = 0; i < 3; i++)
372                                  offsdir[i] = or*(1. - 2.*offsdir[i]);
373                          ssn++;
374 <                        for (i = 0; i < 3; i++)
375 <                                sr.rorg[i] = oc[i] + offsdir[i];
376 <                        d = DOT(offsdir,onorm);
377 <                        if (infront)
378 <                                for (i = 0; i < 3; i++) {
379 <                                        sr.rorg[i] -= (d-.0001)*onorm[i];
357 <                                        sr.rdir[i] = -onorm[i];
358 <                                }
359 <                        else
360 <                                for (i = 0; i < 3; i++) {
361 <                                        sr.rorg[i] -= (d+.0001)*onorm[i];
362 <                                        sr.rdir[i] = onorm[i];
363 <                                }
364 <                        rayorigin(&sr, NULL, PRIMARY, 1.0);
374 >                        d = 1. - DOT(offsdir, onorm);
375 >                        for (i = 0; i < 3; i++) {
376 >                                sr.rorg[i] = oc[i] + offsdir[i] + d*onorm[i];
377 >                                sr.rdir[i] = -onorm[i];
378 >                        }
379 >                        rayorigin(&sr, PRIMARY, NULL, NULL);
380                  } while (!(*ofun[o->otype].funp)(o, &sr));
381                                          /* check against source */
382 <                initsrcindex(&si);
368 <                si.sn = sn;
369 <                nopart(&si, &sr);
382 >                VCOPY(sr.rorg, sr.rop); /* starting from intersection */
383                  samplendx++;
384 <                if (!srcray(&sr, NULL, &si) || sr.rsrc != sn)
385 <                        continue;
384 >                if (si.sp >= si.np-1 ||
385 >                                !srcray(&sr, NULL, &si) || sr.rsrc != sn) {
386 >                        si.sn = sn-1;           /* reset index to our source */
387 >                        si.np = 0;
388 >                        if (!srcray(&sr, NULL, &si) || sr.rsrc != sn)
389 >                                continue;       /* can't get there from here */
390 >                }
391                  sr.revf = srcvalue;
392 <                rayvalue(&sr);
393 <                if (bright(sr.rcol) <= FTINY)
392 >                rayvalue(&sr);                  /* check sample validity */
393 >                if ((d = bright(sr.rcol)) <= FTINY)
394                          continue;
395 <                nok++;
378 <                                        /* check against obstructions */
395 >                nok++;                  /* got sample; check obstructions */
396                  rayclear(&sr);
397                  sr.revf = raytrace;
398                  rayvalue(&sr);
399 <                if (bright(sr.rcol) > FTINY)
399 >                if ((d1 = bright(sr.rcol)) > FTINY) {
400 >                        if (d - d1 > FTINY) {
401 > #ifdef DEBUG
402 >                                fprintf(stderr, "\tpartially shadowed\n");
403 > #endif
404 >                                return(f);      /* intervening transmitter */
405 >                        }
406                          nhit++;
407 +                }
408                  if (nhit > 0 && nhit < nok) {
409   #ifdef DEBUG
410                          fprintf(stderr, "\tpartially occluded\n");
# Line 402 | Line 426 | register int  sn;      /* target source number */
426          
427  
428   #ifdef DEBUG
429 < virtverb(sn, fp)        /* print verbose description of virtual source */
430 < register int  sn;
431 < FILE  *fp;
429 > void
430 > virtverb(       /* print verbose description of virtual source */
431 >        int  sn,
432 >        FILE  *fp
433 > )
434   {
409        register int  i;
410
435          fprintf(fp, "%s virtual source %d in %s %s\n",
436                          source[sn].sflags & SDISTANT ? "distant" : "local",
437                          sn, ofun[source[sn].so->otype].funame,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines