ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/virtuals.c
Revision: 1.8
Committed: Tue Jun 25 08:52:04 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +34 -15 lines
Log Message:
improved presampling

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