ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/virtuals.c
Revision: 1.14
Committed: Wed Jun 26 11:54:43 1991 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +19 -8 lines
Log Message:
fixed bug in solid angle determination for local sources

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1991 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Routines for simulating virtual light sources
9     * Thus far, we only support planar mirrors.
10     */
11    
12     #include "ray.h"
13    
14 greg 1.7 #include "octree.h"
15    
16 greg 1.3 #include "otypes.h"
17    
18 greg 1.1 #include "source.h"
19    
20 greg 1.7 #include "random.h"
21 greg 1.1
22 greg 1.13 #define MINSAMPLES 3 /* minimum number of pretest samples */
23     #define STESTMAX 30 /* maximum seeks per sample */
24 greg 1.1
25 greg 1.13
26 greg 1.7 double getdisk();
27    
28 greg 1.1 static OBJECT *vobject; /* virtual source objects */
29     static int nvobjects = 0; /* number of virtual source objects */
30    
31    
32     markvirtuals() /* find and mark virtual sources */
33     {
34     register OBJREC *o;
35     register int i;
36     /* check number of direct relays */
37     if (directrelay <= 0)
38     return;
39     /* find virtual source objects */
40     for (i = 0; i < nobjects; i++) {
41     o = objptr(i);
42 greg 1.3 if (!issurface(o->otype) || o->omod == OVOID)
43 greg 1.1 continue;
44     if (!isvlight(objptr(o->omod)->otype))
45     continue;
46 greg 1.3 if (sfun[o->otype].of == NULL ||
47     sfun[o->otype].of->getpleq == NULL)
48     objerror(o, USER, "illegal material");
49 greg 1.1 if (nvobjects == 0)
50     vobject = (OBJECT *)malloc(sizeof(OBJECT));
51     else
52     vobject = (OBJECT *)realloc((char *)vobject,
53     (unsigned)(nvobjects+1)*sizeof(OBJECT));
54     if (vobject == NULL)
55     error(SYSTEM, "out of memory in addvirtuals");
56     vobject[nvobjects++] = i;
57     }
58     if (nvobjects == 0)
59     return;
60 greg 1.4 #ifdef DEBUG
61     fprintf(stderr, "found %d virtual source objects\n", nvobjects);
62     #endif
63 greg 1.1 /* append virtual sources */
64     for (i = nsources; i-- > 0; )
65 greg 1.7 addvirtuals(i, directrelay);
66 greg 1.1 /* done with our object list */
67     free((char *)vobject);
68     nvobjects = 0;
69     }
70    
71    
72 greg 1.4 addvirtuals(sn, nr) /* add virtuals associated with source */
73     int sn;
74 greg 1.1 int nr;
75     {
76     register int i;
77     /* check relay limit first */
78     if (nr <= 0)
79     return;
80 greg 1.7 if (source[sn].sflags & SSKIP)
81     return;
82 greg 1.1 /* check each virtual object for projection */
83     for (i = 0; i < nvobjects; i++)
84 greg 1.3 /* vproject() calls us recursively */
85 greg 1.4 vproject(objptr(vobject[i]), sn, nr-1);
86 greg 1.1 }
87    
88    
89 greg 1.4 vproject(o, sn, n) /* create projected source(s) if they exist */
90 greg 1.3 OBJREC *o;
91 greg 1.4 int sn;
92 greg 1.3 int n;
93     {
94     register int i;
95     register VSMATERIAL *vsmat;
96     MAT4 proj;
97 greg 1.4 int ns;
98    
99     if (o == source[sn].so) /* objects cannot project themselves */
100     return;
101 greg 1.3 /* get virtual source material */
102     vsmat = sfun[objptr(o->omod)->otype].mf;
103     /* project virtual sources */
104     for (i = 0; i < vsmat->nproj; i++)
105 greg 1.4 if ((*vsmat->vproj)(proj, o, &source[sn], i))
106     if ((ns = makevsrc(o, sn, proj)) >= 0) {
107     #ifdef DEBUG
108 greg 1.6 virtverb(ns, stderr);
109 greg 1.4 #endif
110 greg 1.3 addvirtuals(ns, n);
111 greg 1.4 }
112 greg 1.3 }
113    
114    
115 greg 1.4 int
116     makevsrc(op, sn, pm) /* make virtual source if reasonable */
117 greg 1.1 OBJREC *op;
118 greg 1.4 register int sn;
119 greg 1.1 MAT4 pm;
120     {
121 greg 1.9 FVECT nsloc, nsnorm, ocent, v;
122     double maxrad2, d;
123 greg 1.3 int nsflags;
124 greg 1.1 SPOT theirspot, ourspot;
125     register int i;
126 greg 1.3
127 greg 1.6 nsflags = source[sn].sflags | (SVIRTUAL|SSPOT|SFOLLOW);
128 greg 1.1 /* get object center and max. radius */
129 greg 1.6 maxrad2 = getdisk(ocent, op, sn);
130     if (maxrad2 <= FTINY) /* too small? */
131     return(-1);
132 greg 1.1 /* get location and spot */
133 greg 1.4 if (source[sn].sflags & SDISTANT) { /* distant source */
134     if (source[sn].sflags & SPROX)
135 greg 1.5 return(-1); /* should never get here! */
136 greg 1.4 multv3(nsloc, source[sn].sloc, pm);
137 greg 1.6 VCOPY(ourspot.aim, ocent);
138     ourspot.siz = PI*maxrad2;
139     ourspot.flen = 0.;
140 greg 1.4 if (source[sn].sflags & SSPOT) {
141     copystruct(&theirspot, source[sn].sl.s);
142     multp3(theirspot.aim, source[sn].sl.s->aim, pm);
143 greg 1.9 d = ourspot.siz;
144 greg 1.6 if (!commonbeam(&ourspot, &theirspot, nsloc))
145 greg 1.9 return(-1); /* no overlap */
146     if (ourspot.siz < d-FTINY) { /* it shrunk */
147     d = beamdisk(v, op, &ourspot, nsloc);
148     if (d <= FTINY)
149     return(-1);
150     if (d < maxrad2) {
151     maxrad2 = d;
152     VCOPY(ocent, v);
153     }
154     }
155 greg 1.1 }
156     } else { /* local source */
157 greg 1.4 multp3(nsloc, source[sn].sloc, pm);
158 greg 1.6 for (i = 0; i < 3; i++)
159     ourspot.aim[i] = ocent[i] - nsloc[i];
160 greg 1.9 if ((d = normalize(ourspot.aim)) == 0.)
161 greg 1.6 return(-1); /* at source!! */
162 greg 1.9 if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
163 greg 1.6 return(-1); /* too far away */
164     ourspot.flen = 0.;
165 greg 1.14 if (d*d > maxrad2)
166     ourspot.siz = 2.*PI*(1. - sqrt(1.-maxrad2/(d*d)));
167     else
168     nsflags &= ~SSPOT;
169 greg 1.4 if (source[sn].sflags & SSPOT) {
170     copystruct(&theirspot, source[sn].sl.s);
171     multv3(theirspot.aim, source[sn].sl.s->aim, pm);
172 greg 1.14 if (nsflags & SSPOT) {
173     ourspot.flen = theirspot.flen;
174     d = ourspot.siz;
175     if (!commonspot(&ourspot, &theirspot, nsloc))
176     return(-1); /* no overlap */
177     } else {
178     nsflags |= SSPOT;
179     copystruct(&ourspot, &theirspot);
180     d = 2.*ourspot.siz;
181     }
182 greg 1.9 if (ourspot.siz < d-FTINY) { /* it shrunk */
183     d = spotdisk(v, op, &ourspot, nsloc);
184     if (d <= FTINY)
185     return(-1);
186     if (d < maxrad2) {
187     maxrad2 = d;
188     VCOPY(ocent, v);
189     }
190     }
191 greg 1.1 }
192 greg 1.4 if (source[sn].sflags & SFLAT) { /* behind source? */
193     multv3(nsnorm, source[sn].snorm, pm);
194 greg 1.12 if (!checkspot(&ourspot, nsnorm))
195 greg 1.5 return(-1);
196 greg 1.1 }
197     }
198 greg 1.7 /* pretest visibility */
199     nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn);
200     if (nsflags & SSKIP)
201     return(-1); /* obstructed */
202     /* it all checks out, so make it */
203 greg 1.6 if ((i = newsource()) < 0)
204 greg 1.1 goto memerr;
205 greg 1.6 source[i].sflags = nsflags;
206     VCOPY(source[i].sloc, nsloc);
207 greg 1.3 if (nsflags & SFLAT)
208 greg 1.6 VCOPY(source[i].snorm, nsnorm);
209     source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
210 greg 1.14 if (nsflags & SSPOT) {
211     if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
212     goto memerr;
213     copystruct(source[i].sl.s, &ourspot);
214     }
215 greg 1.3 if (nsflags & SPROX)
216 greg 1.6 source[i].sl.prox = source[sn].sl.prox;
217     source[i].sa.svnext = sn;
218     source[i].so = op;
219     return(i);
220 greg 1.1 memerr:
221     error(SYSTEM, "out of memory in makevsrc");
222     }
223    
224    
225 greg 1.6 double
226     getdisk(oc, op, sn) /* get visible object disk */
227     FVECT oc;
228     OBJREC *op;
229     register int sn;
230     {
231     double rad2, roffs, offs, d, rd, rdoto;
232     FVECT rnrm, nrm;
233     /* first, use object getdisk function */
234 greg 1.9 rad2 = getmaxdisk(oc, op);
235 greg 1.6 if (!(source[sn].sflags & SVIRTUAL))
236     return(rad2); /* all done for normal source */
237     /* check for correct side of relay surface */
238 greg 1.9 roffs = getplaneq(rnrm, source[sn].so);
239 greg 1.6 rd = DOT(rnrm, source[sn].sloc); /* source projection */
240     if (!(source[sn].sflags & SDISTANT))
241     rd -= roffs;
242     d = DOT(rnrm, oc) - roffs; /* disk distance to relay plane */
243     if ((d > 0.) ^ (rd > 0.))
244     return(rad2); /* OK if opposite sides */
245     if (d*d >= rad2)
246 greg 1.9 return(0.); /* no relay is possible */
247 greg 1.6 /* we need a closer look */
248 greg 1.9 offs = getplaneq(nrm, op);
249 greg 1.6 rdoto = DOT(rnrm, nrm);
250     if (d*d >= rad2*(1.-rdoto*rdoto))
251     return(0.); /* disk entirely on projection side */
252     /* should shrink disk but I'm lazy */
253     return(rad2);
254     }
255    
256    
257 greg 1.7 int
258     vstestvis(f, o, oc, or2, sn) /* pretest source visibility */
259     int f; /* virtual source flags */
260     OBJREC *o; /* relay object */
261     FVECT oc; /* relay object center */
262     double or2; /* relay object radius squared */
263     register int sn; /* target source number */
264 greg 1.1 {
265 greg 1.7 RAY sr;
266     FVECT onorm;
267     FVECT offsdir;
268     double or, d;
269 greg 1.8 int infront;
270     int ssn;
271 greg 1.11 int nhit, nok;
272 greg 1.7 register int i, n;
273     /* return if pretesting disabled */
274     if (vspretest <= 0)
275     return(f);
276     /* get surface normal */
277 greg 1.9 getplaneq(onorm, o);
278 greg 1.7 /* set number of rays to sample */
279 greg 1.8 if (source[sn].sflags & SDISTANT) {
280 greg 1.7 n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
281     vspretest + .5;
282 greg 1.8 infront = DOT(onorm, source[sn].sloc) > 0.;
283     } else {
284     for (i = 0; i < 3; i++)
285     offsdir[i] = source[sn].sloc[i] - oc[i];
286 greg 1.9 n = or2/DOT(offsdir,offsdir)*vspretest + .5;
287 greg 1.8 infront = DOT(onorm, offsdir) > 0.;
288     }
289 greg 1.13 if (n < MINSAMPLES) n = MINSAMPLES;
290 greg 1.9 #ifdef DEBUG
291     fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
292     sn, o->oname, n);
293     #endif
294 greg 1.7 /* sample */
295 greg 1.8 or = sqrt(or2);
296 greg 1.13 ssn = STESTMAX*n;
297 greg 1.11 nhit = nok = 0;
298 greg 1.7 while (n-- > 0) {
299 greg 1.8 /* get sample point */
300     do {
301 greg 1.9 if (--ssn < 0) {
302     #ifdef DEBUG
303     fprintf(stderr, "\ttoo hard to hit\n");
304     #endif
305 greg 1.8 return(f); /* too small a target! */
306 greg 1.9 }
307 greg 1.8 for (i = 0; i < 3; i++)
308     offsdir[i] = or*(1. -
309     2.*urand(931*i+5827+ssn));
310     for (i = 0; i < 3; i++)
311     sr.rorg[i] = oc[i] + offsdir[i];
312     d = DOT(offsdir,onorm);
313     if (infront)
314     for (i = 0; i < 3; i++) {
315     sr.rorg[i] -= (d-.0001)*onorm[i];
316     sr.rdir[i] = -onorm[i];
317     }
318     else
319     for (i = 0; i < 3; i++) {
320     sr.rorg[i] -= (d+.0001)*onorm[i];
321     sr.rdir[i] = onorm[i];
322     }
323     rayorigin(&sr, NULL, PRIMARY, 1.0);
324     } while (!(*ofun[o->otype].funp)(o, &sr));
325     /* check against source */
326 greg 1.7 samplendx++;
327 greg 1.9 if (srcray(&sr, NULL, sn) == 0.)
328 greg 1.7 continue;
329     sr.revf = srcvalue;
330     rayvalue(&sr);
331     if (bright(sr.rcol) <= FTINY)
332     continue;
333 greg 1.11 nok++;
334 greg 1.7 /* check against obstructions */
335     srcray(&sr, NULL, sn);
336     rayvalue(&sr);
337 greg 1.11 if (bright(sr.rcol) > FTINY)
338     nhit++;
339     if (nhit > 0 && nhit < nok) {
340 greg 1.9 #ifdef DEBUG
341 greg 1.11 fprintf(stderr, "\tpartially occluded\n");
342 greg 1.9 #endif
343 greg 1.11 return(f); /* need to shadow test */
344     }
345 greg 1.1 }
346 greg 1.9 if (nhit == 0) {
347     #ifdef DEBUG
348     fprintf(stderr, "\t0%% hit rate\n");
349     #endif
350 greg 1.7 return(f | SSKIP); /* 0% hit rate: totally occluded */
351 greg 1.9 }
352     #ifdef DEBUG
353     fprintf(stderr, "\t100%% hit rate\n");
354     #endif
355     return(f & ~SFOLLOW); /* 100% hit rate: no occlusion */
356 greg 1.1 }
357 greg 1.7
358 greg 1.4
359     #ifdef DEBUG
360 greg 1.6 virtverb(sn, fp) /* print verbose description of virtual source */
361     register int sn;
362 greg 1.4 FILE *fp;
363     {
364     register int i;
365    
366     fprintf(fp, "%s virtual source %d in %s %s\n",
367 greg 1.6 source[sn].sflags & SDISTANT ? "distant" : "local",
368     sn, ofun[source[sn].so->otype].funame,
369     source[sn].so->oname);
370 greg 1.4 fprintf(fp, "\tat (%f,%f,%f)\n",
371 greg 1.6 source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
372 greg 1.4 fprintf(fp, "\tlinked to source %d (%s)\n",
373 greg 1.6 source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
374     if (source[sn].sflags & SFOLLOW)
375 greg 1.4 fprintf(fp, "\talways followed\n");
376     else
377     fprintf(fp, "\tnever followed\n");
378 greg 1.6 if (!(source[sn].sflags & SSPOT))
379 greg 1.4 return;
380     fprintf(fp, "\twith spot aim (%f,%f,%f) and size %f\n",
381 greg 1.6 source[sn].sl.s->aim[0], source[sn].sl.s->aim[1],
382     source[sn].sl.s->aim[2], source[sn].sl.s->siz);
383 greg 1.4 }
384     #endif