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.5 by greg, Thu Jun 20 17:08:39 1991 UTC vs.
Revision 1.7 by greg, Mon Jun 24 16:10:59 1991 UTC

# Line 11 | Line 11 | static char SCCSid[] = "$SunId$ LBL";
11  
12   #include  "ray.h"
13  
14 + #include  "octree.h"
15 +
16   #include  "otypes.h"
17  
18   #include  "source.h"
19  
20 + #include  "random.h"
21  
19 double  intercircle();
22  
23 + #define  DISKTFRAC      0.25            /* disk area pretest fraction */
24 +
25 + double  getdisk();
26 +
27   static OBJECT  *vobject;                /* virtual source objects */
28   static int  nvobjects = 0;              /* number of virtual source objects */
29  
# Line 55 | Line 61 | markvirtuals()                 /* find and mark virtual sources */
61   #endif
62                                          /* append virtual sources */
63          for (i = nsources; i-- > 0; )
64 <                if (!(source[i].sflags & SSKIP))
59 <                        addvirtuals(i, directrelay);
64 >                addvirtuals(i, directrelay);
65                                          /* done with our object list */
66          free((char *)vobject);
67          nvobjects = 0;
# Line 71 | Line 76 | int  nr;
76                                  /* check relay limit first */
77          if (nr <= 0)
78                  return;
79 +        if (source[sn].sflags & SSKIP)
80 +                return;
81                                  /* check each virtual object for projection */
82          for (i = 0; i < nvobjects; i++)
83                                          /* vproject() calls us recursively */
# Line 97 | Line 104 | int  n;
104                  if ((*vsmat->vproj)(proj, o, &source[sn], i))
105                          if ((ns = makevsrc(o, sn, proj)) >= 0) {
106   #ifdef DEBUG
107 <                                virtverb(&source[ns], stderr);
107 >                                virtverb(ns, stderr);
108   #endif
109                                  addvirtuals(ns, n);
110                          }
# Line 110 | Line 117 | OBJREC  *op;
117   register int  sn;
118   MAT4  pm;
119   {
120 <        register int  nsn;
114 <        FVECT  nsloc, ocent, nsnorm;
115 <        int  nsflags;
120 >        FVECT  nsloc, nsnorm, ocent;
121          double  maxrad2;
122 +        int  nsflags;
123          double  d1;
124          SPOT  theirspot, ourspot;
125          register int  i;
126  
127 <        nsflags = (source[sn].sflags|(SVIRTUAL|SFOLLOW)) & ~SSPOT;
127 >        nsflags = source[sn].sflags | (SVIRTUAL|SSPOT|SFOLLOW);
128                                          /* get object center and max. radius */
129 <        if (sfun[op->otype].of->getdisk != NULL) {
130 <                maxrad2 = (*sfun[op->otype].of->getdisk)(ocent, op);
131 <                if (maxrad2 <= FTINY)                   /* too small? */
126 <                        return(-1);
127 <                nsflags |= SSPOT;
128 <        }
129 >        maxrad2 = getdisk(ocent, op, sn);
130 >        if (maxrad2 <= FTINY)                   /* too small? */
131 >                return(-1);
132                                          /* get location and spot */
133          if (source[sn].sflags & SDISTANT) {             /* distant source */
134                  if (source[sn].sflags & SPROX)
135                          return(-1);             /* should never get here! */
136                  multv3(nsloc, source[sn].sloc, pm);
137 <                if (nsflags & SSPOT) {
138 <                        VCOPY(ourspot.aim, ocent);
139 <                        ourspot.siz = PI*maxrad2;
137 <                        ourspot.flen = 0.;
138 <                }
137 >                VCOPY(ourspot.aim, ocent);
138 >                ourspot.siz = PI*maxrad2;
139 >                ourspot.flen = 0.;
140                  if (source[sn].sflags & SSPOT) {
141                          copystruct(&theirspot, source[sn].sl.s);
142                          multp3(theirspot.aim, source[sn].sl.s->aim, pm);
143 <                        if (nsflags & SSPOT &&
143 <                                !commonbeam(&ourspot, &theirspot, nsloc))
143 >                        if (!commonbeam(&ourspot, &theirspot, nsloc))
144                                  return(-1);             /* no overlap */
145                  }
146          } else {                                /* local source */
147                  multp3(nsloc, source[sn].sloc, pm);
148 <                if (nsflags & SSPOT) {
149 <                        for (i = 0; i < 3; i++)
150 <                                ourspot.aim[i] = ocent[i] - nsloc[i];
151 <                        if ((d1 = normalize(ourspot.aim)) == 0.)
152 <                                return(-1);             /* at source!! */
153 <                        if (source[sn].sflags & SPROX &&
154 <                                        d1 > source[sn].sl.prox)
155 <                                return(-1);             /* too far away */
156 <                        ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
157 <                        ourspot.flen = 0.;
158 <                } else if (source[sn].sflags & SPROX) {
159 <                        FVECT  norm;
160 <                        double  offs;
161 <                                                /* use distance from plane */
162 <                        offs = (*sfun[op->otype].of->getpleq)(norm, op);
163 <                        d1 = DOT(norm, nsloc) - offs;
164 <                        if (d1 < 0.) d1 = -d1;
165 <                        if (d1 > source[sn].sl.prox)
166 <                                return(-1);             /* too far away */
167 <                }
148 >                for (i = 0; i < 3; i++)
149 >                        ourspot.aim[i] = ocent[i] - nsloc[i];
150 >                if ((d1 = normalize(ourspot.aim)) == 0.)
151 >                        return(-1);             /* at source!! */
152 >                if (source[sn].sflags & SPROX && d1 > source[sn].sl.prox)
153 >                        return(-1);             /* too far away */
154 >                ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
155 >                ourspot.flen = 0.;
156                  if (source[sn].sflags & SSPOT) {
157                          copystruct(&theirspot, source[sn].sl.s);
158                          multv3(theirspot.aim, source[sn].sl.s->aim, pm);
159 <                        if (nsflags & SSPOT) {
160 <                                if (!commonspot(&ourspot, &theirspot, nsloc))
161 <                                        return(-1);     /* no overlap */
174 <                                ourspot.flen = theirspot.flen;
175 <                        }
159 >                        if (!commonspot(&ourspot, &theirspot, nsloc))
160 >                                return(-1);     /* no overlap */
161 >                        ourspot.flen = theirspot.flen;
162                  }
163                  if (source[sn].sflags & SFLAT) {        /* behind source? */
164                          multv3(nsnorm, source[sn].snorm, pm);
165 <                        if (nsflags & SSPOT && checkspot(&ourspot, nsnorm) < 0)
165 >                        if (checkspot(&ourspot, nsnorm) < 0)
166                                  return(-1);
167                  }
168          }
169 <                                        /* everything is OK, make source */
170 <        if ((nsn = newsource()) < 0)
169 >                                        /* pretest visibility */
170 >        nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn);
171 >        if (nsflags & SSKIP)
172 >                return(-1);     /* obstructed */
173 >                                        /* it all checks out, so make it */
174 >        if ((i = newsource()) < 0)
175                  goto memerr;
176 <        source[nsn].sflags = nsflags;
177 <        VCOPY(source[nsn].sloc, nsloc);
176 >        source[i].sflags = nsflags;
177 >        VCOPY(source[i].sloc, nsloc);
178          if (nsflags & SFLAT)
179 <                VCOPY(source[nsn].snorm, nsnorm);
180 <        source[nsn].ss = source[sn].ss; source[nsn].ss2 = source[sn].ss2;
181 <        if ((nsflags | source[sn].sflags) & SSPOT) {
182 <                if ((source[nsn].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
183 <                        goto memerr;
194 <                if (nsflags & SSPOT)
195 <                        copystruct(source[nsn].sl.s, &ourspot);
196 <                else
197 <                        copystruct(source[nsn].sl.s, &theirspot);
198 <                source[nsn].sflags |= SSPOT;
199 <        }
179 >                VCOPY(source[i].snorm, nsnorm);
180 >        source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
181 >        if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
182 >                goto memerr;
183 >        copystruct(source[i].sl.s, &ourspot);
184          if (nsflags & SPROX)
185 <                source[nsn].sl.prox = source[sn].sl.prox;
186 <        source[nsn].sa.svnext = sn;
187 <        source[nsn].so = op;
188 <        return(nsn);
185 >                source[i].sl.prox = source[sn].sl.prox;
186 >        source[i].sa.svnext = sn;
187 >        source[i].so = op;
188 >        return(i);
189   memerr:
190          error(SYSTEM, "out of memory in makevsrc");
191   }
192  
193  
194 < commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
195 < register SPOT  *sp1, *sp2;
196 < FVECT  org;
194 > double
195 > getdisk(oc, op, sn)             /* get visible object disk */
196 > FVECT  oc;
197 > OBJREC  *op;
198 > register int  sn;
199   {
200 <        FVECT  cent;
201 <        double  rad2, cos1, cos2;
202 <
203 <        cos1 = 1. - sp1->siz/(2.*PI);
204 <        cos2 = 1. - sp2->siz/(2.*PI);
205 <        if (sp2->siz >= 2.*PI-FTINY)            /* BIG, just check overlap */
206 <                return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
207 <                                        sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
208 <                                /* compute and check disks */
209 <        rad2 = intercircle(cent, sp1->aim, sp2->aim,
210 <                        1./(cos1*cos1) - 1.,  1./(cos2*cos2) - 1.);
211 <        if (rad2 <= FTINY || normalize(cent) == 0.)
212 <                return(0);
213 <        VCOPY(sp1->aim, cent);
214 <        sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
215 <        return(1);
200 >        double  rad2, roffs, offs, d, rd, rdoto;
201 >        FVECT  rnrm, nrm;
202 >                                /* first, use object getdisk function */
203 >        rad2 = (*sfun[op->otype].of->getdisk)(oc, op);
204 >        if (!(source[sn].sflags & SVIRTUAL))
205 >                return(rad2);           /* all done for normal source */
206 >                                /* check for correct side of relay surface */
207 >        roffs = (*sfun[source[sn].so->otype].of->getpleq)(rnrm, source[sn].so);
208 >        rd = DOT(rnrm, source[sn].sloc);        /* source projection */
209 >        if (!(source[sn].sflags & SDISTANT))
210 >                rd -= roffs;
211 >        d = DOT(rnrm, oc) - roffs;      /* disk distance to relay plane */
212 >        if ((d > 0.) ^ (rd > 0.))
213 >                return(rad2);           /* OK if opposite sides */
214 >        if (d*d >= rad2)
215 >                return(.0);             /* no relay is possible */
216 >                                /* we need a closer look */
217 >        offs = (*sfun[op->otype].of->getpleq)(nrm, op);
218 >        rdoto = DOT(rnrm, nrm);
219 >        if (d*d >= rad2*(1.-rdoto*rdoto))
220 >                return(0.);             /* disk entirely on projection side */
221 >                                /* should shrink disk but I'm lazy */
222 >        return(rad2);
223   }
224  
225  
226 < commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
227 < register SPOT  *sp1, *sp2;
228 < FVECT  dir;
226 > int
227 > vstestvis(f, o, oc, or2, sn)            /* pretest source visibility */
228 > int  f;                 /* virtual source flags */
229 > OBJREC  *o;             /* relay object */
230 > FVECT  oc;              /* relay object center */
231 > double  or2;            /* relay object radius squared */
232 > register int  sn;       /* target source number */
233   {
234 <        FVECT  cent, c1, c2;
235 <        double  rad2, d;
236 <        register int  i;
237 <                                        /* move centers to common plane */
238 <        d = DOT(sp1->aim, dir);
239 <        for (i = 0; i < 3; i++)
240 <                c1[i] = sp1->aim[i] - d*dir[i];
241 <        d = DOT(sp2->aim, dir);
242 <        for (i = 0; i < 3; i++)
243 <                c2[i] = sp2->aim[i] - d*dir[i];
244 <                                        /* compute overlap */
245 <        rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
246 <        if (rad2 <= FTINY)
247 <                return(0);
248 <        VCOPY(sp1->aim, cent);
249 <        sp1->siz = PI*rad2;
250 <        return(1);
251 < }
252 <
253 <
254 < checkspot(sp, nrm)              /* check spotlight for behind source */
255 < register SPOT  *sp;
256 < FVECT  nrm;
257 < {
258 <        double  d, d1;
259 <
260 <        d = DOT(sp->aim, nrm);
261 <        if (d > FTINY)                  /* center in front? */
262 <                return(0);
263 <                                        /* else check horizon */
264 <        d1 = 1. - sp->siz/(2.*PI);
265 <        return(1.-FTINY-d*d > d1*d1);
266 < }
267 <
268 <
269 < double
270 < intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
271 < FVECT  cc;                      /* midpoint (return value) */
272 < FVECT  c1, c2;                  /* circle centers */
273 < double  r1s, r2s;               /* radii squared */
274 < {
275 <        double  a2, d2, l;
276 <        FVECT  disp;
277 <        register int  i;
278 <
279 <        for (i = 0; i < 3; i++)
283 <                disp[i] = c2[i] - c1[i];
284 <        d2 = DOT(disp,disp);
285 <                                        /* circle within overlap? */
286 <        if (r1s < r2s) {
287 <                if (r2s >= r1s + d2) {
288 <                        VCOPY(cc, c1);
289 <                        return(r1s);
290 <                }
291 <        } else {
292 <                if (r1s >= r2s + d2) {
293 <                        VCOPY(cc, c2);
294 <                        return(r2s);
295 <                }
234 >        RAY  sr;
235 >        FVECT  onorm;
236 >        FVECT  offsdir;
237 >        double  or, d;
238 >        int  nok, nhit;
239 >        register int  i, n;
240 >                                /* return if pretesting disabled */
241 >        if (vspretest <= 0)
242 >                return(f);
243 >                                /* get surface normal */
244 >        (*sfun[o->otype].of->getpleq)(onorm, o);
245 >                                /* set number of rays to sample */
246 >        if (source[sn].sflags & SDISTANT)
247 >                n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
248 >                                vspretest + .5;
249 >        else
250 >                n = or2/dist2(oc,source[sn].sloc)*vspretest + .5;
251 >        if (n < 1) n = 1;
252 >                                /* limit tests to central region */
253 >        or = DISKTFRAC*sqrt(or2);
254 >                                /* sample */
255 >        nhit = nok = 0;
256 >        while (n-- > 0) {
257 >                samplendx++;
258 >                /*
259 >                 * We're being real sloppy with our sample locations here.
260 >                 */
261 >                for (i = 0; i < 3; i++)
262 >                        offsdir[i] = or*(1. - 2.*urand(931*i+5821+n));
263 >                d = DOT(offsdir,onorm);
264 >                for (i = 0; i < 3; i++)
265 >                        sr.rorg[i] = oc[i] + (1.-d)*offsdir[i];
266 >                                        /* check against source */
267 >                if (srcray(&sr, NULL, sn) == 0.0)
268 >                        continue;
269 >                sr.revf = srcvalue;
270 >                rayvalue(&sr);
271 >                if (bright(sr.rcol) <= FTINY)
272 >                        continue;
273 >                nok++;
274 >                                        /* check against obstructions */
275 >                srcray(&sr, NULL, sn);
276 >                rayvalue(&sr);
277 >                if (bright(sr.rcol) <= FTINY)
278 >                        continue;
279 >                nhit++;
280          }
281 <        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
282 <                                        /* no overlap? */
283 <        if (a2 <= 0.)
284 <                return(0.);
285 <                                        /* overlap, compute center */
286 <        l = sqrt((r1s - a2)/d2);
303 <        for (i = 0; i < 3; i++)
304 <                cc[i] = c1[i] + l*disp[i];
305 <        return(a2);
281 >                                /* interpret results */
282 >        if (nhit == 0)
283 >                return(f | SSKIP);      /* 0% hit rate:  totally occluded */
284 >        if (nhit == nok)
285 >                return(f & ~SFOLLOW);   /* 100% hit rate:  no occlusion */
286 >        return(f);              /* no comment */
287   }
288 +        
289  
308
290   #ifdef DEBUG
291 < virtverb(vs, fp)        /* print verbose description of virtual source */
292 < register SRCREC  *vs;
291 > virtverb(sn, fp)        /* print verbose description of virtual source */
292 > register int  sn;
293   FILE  *fp;
294   {
295          register int  i;
296  
297          fprintf(fp, "%s virtual source %d in %s %s\n",
298 <                        vs->sflags & SDISTANT ? "distant" : "local",
299 <                        vs-source, ofun[vs->so->otype].funame, vs->so->oname);
298 >                        source[sn].sflags & SDISTANT ? "distant" : "local",
299 >                        sn, ofun[source[sn].so->otype].funame,
300 >                        source[sn].so->oname);
301          fprintf(fp, "\tat (%f,%f,%f)\n",
302 <                        vs->sloc[0], vs->sloc[1], vs->sloc[2]);
302 >                source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
303          fprintf(fp, "\tlinked to source %d (%s)\n",
304 <                        vs->sa.svnext, source[vs->sa.svnext].so->oname);
305 <        if (vs->sflags & SFOLLOW)
304 >                source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
305 >        if (source[sn].sflags & SFOLLOW)
306                  fprintf(fp, "\talways followed\n");
307          else
308                  fprintf(fp, "\tnever followed\n");
309 <        if (!(vs->sflags & SSPOT))
309 >        if (!(source[sn].sflags & SSPOT))
310                  return;
311          fprintf(fp, "\twith spot aim (%f,%f,%f) and size %f\n",
312 <                        vs->sl.s->aim[0], vs->sl.s->aim[1], vs->sl.s->aim[2],
313 <                        vs->sl.s->siz);
312 >                        source[sn].sl.s->aim[0], source[sn].sl.s->aim[1],
313 >                        source[sn].sl.s->aim[2], source[sn].sl.s->siz);
314   }
315   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines