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.3 by greg, Thu Jun 20 13:43:38 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  "cone.h"
20 > #include  "random.h"
21  
20 #include  "face.h"
22  
23 + #define  DISKTFRAC      0.25            /* disk area pretest fraction */
24  
25 < double  intercircle();
24 < SRCREC  *makevsrc();
25 > double  getdisk();
26  
27   static OBJECT  *vobject;                /* virtual source objects */
28   static int  nvobjects = 0;              /* number of virtual source objects */
# Line 55 | Line 56 | markvirtuals()                 /* find and mark virtual sources */
56          }
57          if (nvobjects == 0)
58                  return;
59 + #ifdef DEBUG
60 +        fprintf(stderr, "found %d virtual source objects\n", nvobjects);
61 + #endif
62                                          /* append virtual sources */
63          for (i = nsources; i-- > 0; )
64 <                if (!(source[i].sflags & SSKIP))
61 <                        addvirtuals(&source[i], directrelay);
64 >                addvirtuals(i, directrelay);
65                                          /* done with our object list */
66          free((char *)vobject);
67          nvobjects = 0;
68   }
69  
70  
71 < addvirtuals(sr, nr)             /* add virtual sources associated with sr */
72 < SRCREC  *sr;
71 > addvirtuals(sn, nr)             /* add virtuals associated with source */
72 > int  sn;
73   int  nr;
74   {
75          register int  i;
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 */
84 <                vproject(objptr(i), sr, nr-1);
84 >                vproject(objptr(vobject[i]), sn, nr-1);
85   }
86  
87  
88 < vproject(o, s, n)               /* create projected source(s) if they exist */
88 > vproject(o, sn, n)              /* create projected source(s) if they exist */
89   OBJREC  *o;
90 < SRCREC  *s;
90 > int  sn;
91   int  n;
92   {
93          register int  i;
94          register VSMATERIAL  *vsmat;
95          MAT4  proj;
96 <        SRCREC  *ns;
96 >        int  ns;
97 >
98 >        if (o == source[sn].so) /* objects cannot project themselves */
99 >                return;
100                                  /* get virtual source material */
101          vsmat = sfun[objptr(o->omod)->otype].mf;
102                                  /* project virtual sources */
103          for (i = 0; i < vsmat->nproj; i++)
104 <                if ((*vsmat->vproj)(proj, o, s, i))
105 <                        if ((ns = makevsrc(o, s, proj)) != NULL)
104 >                if ((*vsmat->vproj)(proj, o, &source[sn], i))
105 >                        if ((ns = makevsrc(o, sn, proj)) >= 0) {
106 > #ifdef DEBUG
107 >                                virtverb(ns, stderr);
108 > #endif
109                                  addvirtuals(ns, n);
110 +                        }
111   }
112  
113  
114 < SRCREC *
115 < makevsrc(op, sp, pm)            /* make virtual source if reasonable */
114 > int
115 > makevsrc(op, sn, pm)            /* make virtual source if reasonable */
116   OBJREC  *op;
117 < register SRCREC  *sp;
117 > register int  sn;
118   MAT4  pm;
119   {
120 <        register SRCREC  *newsrc;
109 <        FVECT  nsloc, ocent, nsnorm;
110 <        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 = (sp->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? */
121 <                        return(NULL);
122 <                nsflags |= SSPOT;
123 <        }
129 >        maxrad2 = getdisk(ocent, op, sn);
130 >        if (maxrad2 <= FTINY)                   /* too small? */
131 >                return(-1);
132                                          /* get location and spot */
133 <        if (sp->sflags & SDISTANT) {            /* distant source */
134 <                if (sp->sflags & SPROX)
135 <                        return(NULL);           /* should never get here! */
136 <                multv3(nsloc, sp->sloc, pm);
137 <                if (nsflags & SSPOT) {
138 <                        VCOPY(ourspot.aim, ocent);
139 <                        ourspot.siz = PI*maxrad2;
140 <                        ourspot.flen = 0.;
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 >                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 (!commonbeam(&ourspot, &theirspot, nsloc))
144 >                                return(-1);             /* no overlap */
145                  }
134                if (sp->sflags & SSPOT) {
135                        copystruct(&theirspot, sp->sl.s);
136                        multp3(theirspot.aim, sp->sl.s->aim, pm);
137                        if (nsflags & SSPOT &&
138                                !commonbeam(&ourspot, &theirspot, nsloc))
139                                return(NULL);           /* no overlap */
140                }
146          } else {                                /* local source */
147 <                multp3(nsloc, sp->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(NULL);           /* at source!! */
153 <                        if (sp->sflags & SPROX && d1 > sp->sl.prox)
154 <                                return(NULL);           /* too far away */
155 <                        ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2));
156 <                        ourspot.flen = 0.;
157 <                } else if (sp->sflags & SPROX) {
158 <                        FVECT  norm;
159 <                        double  offs;
160 <                                                /* use distance from plane */
161 <                        offs = (*sfun[op->otype].of->getpleq)(norm, op);
157 <                        d1 = DOT(norm, nsloc) - offs;
158 <                        if (d1 > sp->sl.prox || d1 < -sp->sl.prox)
159 <                                return(NULL);           /* too far away */
147 >                multp3(nsloc, source[sn].sloc, pm);
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 (!commonspot(&ourspot, &theirspot, nsloc))
160 >                                return(-1);     /* no overlap */
161 >                        ourspot.flen = theirspot.flen;
162                  }
163 <                if (sp->sflags & SSPOT) {
164 <                        copystruct(&theirspot, sp->sl.s);
165 <                        multv3(theirspot.aim, sp->sl.s->aim, pm);
166 <                        if (nsflags & SSPOT) {
165 <                                if (!commonspot(&ourspot, &theirspot, nsloc))
166 <                                        return(NULL);   /* no overlap */
167 <                                ourspot.flen = theirspot.flen;
168 <                        }
163 >                if (source[sn].sflags & SFLAT) {        /* behind source? */
164 >                        multv3(nsnorm, source[sn].snorm, pm);
165 >                        if (checkspot(&ourspot, nsnorm) < 0)
166 >                                return(-1);
167                  }
170                if (sp->sflags & SFLAT) {       /* check for behind source */
171                        multv3(nsnorm, sp->snorm, pm);
172                        if (nsflags & SSPOT && checkspot(&ourspot, nsnorm) < 0)
173                                return(NULL);
174                }
168          }
169 <                                        /* everything is OK, make source */
170 <        if ((newsrc = newsource()) == NULL)
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 <        newsrc->sflags = nsflags;
177 <        VCOPY(newsrc->sloc, nsloc);
176 >        source[i].sflags = nsflags;
177 >        VCOPY(source[i].sloc, nsloc);
178          if (nsflags & SFLAT)
179 <                VCOPY(newsrc->snorm, nsnorm);
180 <        newsrc->ss = sp->ss; newsrc->ss2 = sp->ss2;
181 <        if ((nsflags | sp->sflags) & SSPOT) {
182 <                if ((newsrc->sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
183 <                        goto memerr;
187 <                if (nsflags & SSPOT)
188 <                        copystruct(newsrc->sl.s, &ourspot);
189 <                else
190 <                        copystruct(newsrc->sl.s, &theirspot);
191 <                newsrc->sflags |= SSPOT;
192 <        }
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 <                newsrc->sl.prox = sp->sl.prox;
186 <        newsrc->sa.svnext = sp - source;
187 <        return(newsrc);
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);
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 >                                /* 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  
290 <
291 < checkspot(sp, nrm)              /* check spotlight for behind source */
292 < register SPOT  *sp;
293 < FVECT  nrm;
290 > #ifdef DEBUG
291 > virtverb(sn, fp)        /* print verbose description of virtual source */
292 > register int  sn;
293 > FILE  *fp;
294   {
253        double  d, d1;
254
255        d = DOT(sp->aim, nrm);
256        if (d > FTINY)                  /* center in front? */
257                return(0);
258                                        /* else check horizon */
259        d1 = 1. - sp->siz/(2.*PI);
260        return(1.-FTINY-d*d > d1*d1);
261 }
262
263
264 double
265 intercircle(cc, c1, c2, r1s, r2s)       /* intersect two circles */
266 FVECT  cc;                      /* midpoint (return value) */
267 FVECT  c1, c2;                  /* circle centers */
268 double  r1s, r2s;               /* radii squared */
269 {
270        double  a2, d2, l;
271        FVECT  disp;
295          register int  i;
296  
297 <        for (i = 0; i < 3; i++)
298 <                disp[i] = c2[i] - c1[i];
299 <        d2 = DOT(disp,disp);
300 <                                        /* circle within overlap? */
301 <        if (r1s < r2s) {
302 <                if (r2s >= r1s + d2) {
303 <                        VCOPY(cc, c1);
304 <                        return(r1s);
305 <                }
306 <        } else {
307 <                if (r1s >= r2s + d2) {
308 <                        VCOPY(cc, c2);
309 <                        return(r2s);
310 <                }
311 <        }
312 <        a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
313 <                                        /* no overlap? */
291 <        if (a2 <= 0.)
292 <                return(0.);
293 <                                        /* overlap, compute center */
294 <        l = sqrt((r1s - a2)/d2);
295 <        for (i = 0; i < 3; i++)
296 <                cc[i] = c1[i] + l*disp[i];
297 <        return(a2);
297 >        fprintf(fp, "%s virtual source %d in %s %s\n",
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 >                source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
303 >        fprintf(fp, "\tlinked to source %d (%s)\n",
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 (!(source[sn].sflags & SSPOT))
310 >                return;
311 >        fprintf(fp, "\twith spot aim (%f,%f,%f) and size %f\n",
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