ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/virtuals.c
Revision: 1.11
Committed: Tue Jun 25 15:48:11 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +9 -9 lines
Log Message:
another minor bug in vstestvis()

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.9 FVECT nsloc, nsnorm, ocent, v;
119     double maxrad2, d;
120 greg 1.3 int nsflags;
121 greg 1.1 SPOT theirspot, ourspot;
122     register int i;
123 greg 1.3
124 greg 1.6 nsflags = source[sn].sflags | (SVIRTUAL|SSPOT|SFOLLOW);
125 greg 1.1 /* get object center and max. radius */
126 greg 1.6 maxrad2 = getdisk(ocent, op, sn);
127     if (maxrad2 <= FTINY) /* too small? */
128     return(-1);
129 greg 1.1 /* get location and spot */
130 greg 1.4 if (source[sn].sflags & SDISTANT) { /* distant source */
131     if (source[sn].sflags & SPROX)
132 greg 1.5 return(-1); /* should never get here! */
133 greg 1.4 multv3(nsloc, source[sn].sloc, pm);
134 greg 1.6 VCOPY(ourspot.aim, ocent);
135     ourspot.siz = PI*maxrad2;
136     ourspot.flen = 0.;
137 greg 1.4 if (source[sn].sflags & SSPOT) {
138     copystruct(&theirspot, source[sn].sl.s);
139     multp3(theirspot.aim, source[sn].sl.s->aim, pm);
140 greg 1.9 d = ourspot.siz;
141 greg 1.6 if (!commonbeam(&ourspot, &theirspot, nsloc))
142 greg 1.9 return(-1); /* no overlap */
143     if (ourspot.siz < d-FTINY) { /* it shrunk */
144     d = beamdisk(v, op, &ourspot, nsloc);
145     if (d <= FTINY)
146     return(-1);
147     if (d < maxrad2) {
148     maxrad2 = d;
149     VCOPY(ocent, v);
150     }
151     }
152 greg 1.1 }
153     } else { /* local source */
154 greg 1.4 multp3(nsloc, source[sn].sloc, pm);
155 greg 1.6 for (i = 0; i < 3; i++)
156     ourspot.aim[i] = ocent[i] - nsloc[i];
157 greg 1.9 if ((d = normalize(ourspot.aim)) == 0.)
158 greg 1.6 return(-1); /* at source!! */
159 greg 1.9 if (source[sn].sflags & SPROX && d > source[sn].sl.prox)
160 greg 1.6 return(-1); /* too far away */
161 greg 1.9 ourspot.siz = 2.*PI*(1. - d/sqrt(d*d+maxrad2));
162 greg 1.6 ourspot.flen = 0.;
163 greg 1.4 if (source[sn].sflags & SSPOT) {
164     copystruct(&theirspot, source[sn].sl.s);
165     multv3(theirspot.aim, source[sn].sl.s->aim, pm);
166 greg 1.9 d = ourspot.siz;
167 greg 1.6 if (!commonspot(&ourspot, &theirspot, nsloc))
168     return(-1); /* no overlap */
169 greg 1.9 if (ourspot.siz < d-FTINY) { /* it shrunk */
170     d = spotdisk(v, op, &ourspot, nsloc);
171     if (d <= FTINY)
172     return(-1);
173     if (d < maxrad2) {
174     maxrad2 = d;
175     VCOPY(ocent, v);
176     }
177     }
178 greg 1.6 ourspot.flen = theirspot.flen;
179 greg 1.1 }
180 greg 1.4 if (source[sn].sflags & SFLAT) { /* behind source? */
181     multv3(nsnorm, source[sn].snorm, pm);
182 greg 1.6 if (checkspot(&ourspot, nsnorm) < 0)
183 greg 1.5 return(-1);
184 greg 1.1 }
185     }
186 greg 1.7 /* pretest visibility */
187     nsflags = vstestvis(nsflags, op, ocent, maxrad2, sn);
188     if (nsflags & SSKIP)
189     return(-1); /* obstructed */
190     /* it all checks out, so make it */
191 greg 1.6 if ((i = newsource()) < 0)
192 greg 1.1 goto memerr;
193 greg 1.6 source[i].sflags = nsflags;
194     VCOPY(source[i].sloc, nsloc);
195 greg 1.3 if (nsflags & SFLAT)
196 greg 1.6 VCOPY(source[i].snorm, nsnorm);
197     source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2;
198     if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL)
199     goto memerr;
200     copystruct(source[i].sl.s, &ourspot);
201 greg 1.3 if (nsflags & SPROX)
202 greg 1.6 source[i].sl.prox = source[sn].sl.prox;
203     source[i].sa.svnext = sn;
204     source[i].so = op;
205     return(i);
206 greg 1.1 memerr:
207     error(SYSTEM, "out of memory in makevsrc");
208     }
209    
210    
211 greg 1.6 double
212     getdisk(oc, op, sn) /* get visible object disk */
213     FVECT oc;
214     OBJREC *op;
215     register int sn;
216     {
217     double rad2, roffs, offs, d, rd, rdoto;
218     FVECT rnrm, nrm;
219     /* first, use object getdisk function */
220 greg 1.9 rad2 = getmaxdisk(oc, op);
221 greg 1.6 if (!(source[sn].sflags & SVIRTUAL))
222     return(rad2); /* all done for normal source */
223     /* check for correct side of relay surface */
224 greg 1.9 roffs = getplaneq(rnrm, source[sn].so);
225 greg 1.6 rd = DOT(rnrm, source[sn].sloc); /* source projection */
226     if (!(source[sn].sflags & SDISTANT))
227     rd -= roffs;
228     d = DOT(rnrm, oc) - roffs; /* disk distance to relay plane */
229     if ((d > 0.) ^ (rd > 0.))
230     return(rad2); /* OK if opposite sides */
231     if (d*d >= rad2)
232 greg 1.9 return(0.); /* no relay is possible */
233 greg 1.6 /* we need a closer look */
234 greg 1.9 offs = getplaneq(nrm, op);
235 greg 1.6 rdoto = DOT(rnrm, nrm);
236     if (d*d >= rad2*(1.-rdoto*rdoto))
237     return(0.); /* disk entirely on projection side */
238     /* should shrink disk but I'm lazy */
239     return(rad2);
240     }
241    
242    
243 greg 1.7 int
244     vstestvis(f, o, oc, or2, sn) /* pretest source visibility */
245     int f; /* virtual source flags */
246     OBJREC *o; /* relay object */
247     FVECT oc; /* relay object center */
248     double or2; /* relay object radius squared */
249     register int sn; /* target source number */
250 greg 1.1 {
251 greg 1.7 RAY sr;
252     FVECT onorm;
253     FVECT offsdir;
254     double or, d;
255 greg 1.8 int infront;
256     int ssn;
257 greg 1.11 int nhit, nok;
258 greg 1.7 register int i, n;
259     /* return if pretesting disabled */
260     if (vspretest <= 0)
261     return(f);
262     /* get surface normal */
263 greg 1.9 getplaneq(onorm, o);
264 greg 1.7 /* set number of rays to sample */
265 greg 1.8 if (source[sn].sflags & SDISTANT) {
266 greg 1.7 n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)*
267     vspretest + .5;
268 greg 1.8 infront = DOT(onorm, source[sn].sloc) > 0.;
269     } else {
270     for (i = 0; i < 3; i++)
271     offsdir[i] = source[sn].sloc[i] - oc[i];
272 greg 1.9 n = or2/DOT(offsdir,offsdir)*vspretest + .5;
273 greg 1.8 infront = DOT(onorm, offsdir) > 0.;
274     }
275 greg 1.7 if (n < 1) n = 1;
276 greg 1.9 #ifdef DEBUG
277     fprintf(stderr, "pretesting source %d in object %s with %d rays\n",
278     sn, o->oname, n);
279     #endif
280 greg 1.7 /* sample */
281 greg 1.8 or = sqrt(or2);
282 greg 1.9 ssn = 25*n;
283 greg 1.11 nhit = nok = 0;
284 greg 1.7 while (n-- > 0) {
285 greg 1.8 /* get sample point */
286     do {
287 greg 1.9 if (--ssn < 0) {
288     #ifdef DEBUG
289     fprintf(stderr, "\ttoo hard to hit\n");
290     #endif
291 greg 1.8 return(f); /* too small a target! */
292 greg 1.9 }
293 greg 1.8 for (i = 0; i < 3; i++)
294     offsdir[i] = or*(1. -
295     2.*urand(931*i+5827+ssn));
296     for (i = 0; i < 3; i++)
297     sr.rorg[i] = oc[i] + offsdir[i];
298     d = DOT(offsdir,onorm);
299     if (infront)
300     for (i = 0; i < 3; i++) {
301     sr.rorg[i] -= (d-.0001)*onorm[i];
302     sr.rdir[i] = -onorm[i];
303     }
304     else
305     for (i = 0; i < 3; i++) {
306     sr.rorg[i] -= (d+.0001)*onorm[i];
307     sr.rdir[i] = onorm[i];
308     }
309     rayorigin(&sr, NULL, PRIMARY, 1.0);
310     } while (!(*ofun[o->otype].funp)(o, &sr));
311     /* check against source */
312 greg 1.7 samplendx++;
313 greg 1.9 if (srcray(&sr, NULL, sn) == 0.)
314 greg 1.7 continue;
315     sr.revf = srcvalue;
316     rayvalue(&sr);
317     if (bright(sr.rcol) <= FTINY)
318     continue;
319 greg 1.11 nok++;
320 greg 1.7 /* check against obstructions */
321     srcray(&sr, NULL, sn);
322     rayvalue(&sr);
323 greg 1.11 if (bright(sr.rcol) > FTINY)
324     nhit++;
325     if (nhit > 0 && nhit < nok) {
326 greg 1.9 #ifdef DEBUG
327 greg 1.11 fprintf(stderr, "\tpartially occluded\n");
328 greg 1.9 #endif
329 greg 1.11 return(f); /* need to shadow test */
330     }
331 greg 1.1 }
332 greg 1.9 if (nhit == 0) {
333     #ifdef DEBUG
334     fprintf(stderr, "\t0%% hit rate\n");
335     #endif
336 greg 1.7 return(f | SSKIP); /* 0% hit rate: totally occluded */
337 greg 1.9 }
338     #ifdef DEBUG
339     fprintf(stderr, "\t100%% hit rate\n");
340     #endif
341     return(f & ~SFOLLOW); /* 100% hit rate: no occlusion */
342 greg 1.1 }
343 greg 1.7
344 greg 1.4
345     #ifdef DEBUG
346 greg 1.6 virtverb(sn, fp) /* print verbose description of virtual source */
347     register int sn;
348 greg 1.4 FILE *fp;
349     {
350     register int i;
351    
352     fprintf(fp, "%s virtual source %d in %s %s\n",
353 greg 1.6 source[sn].sflags & SDISTANT ? "distant" : "local",
354     sn, ofun[source[sn].so->otype].funame,
355     source[sn].so->oname);
356 greg 1.4 fprintf(fp, "\tat (%f,%f,%f)\n",
357 greg 1.6 source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]);
358 greg 1.4 fprintf(fp, "\tlinked to source %d (%s)\n",
359 greg 1.6 source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname);
360     if (source[sn].sflags & SFOLLOW)
361 greg 1.4 fprintf(fp, "\talways followed\n");
362     else
363     fprintf(fp, "\tnever followed\n");
364 greg 1.6 if (!(source[sn].sflags & SSPOT))
365 greg 1.4 return;
366     fprintf(fp, "\twith spot aim (%f,%f,%f) and size %f\n",
367 greg 1.6 source[sn].sl.s->aim[0], source[sn].sl.s->aim[1],
368     source[sn].sl.s->aim[2], source[sn].sl.s->siz);
369 greg 1.4 }
370     #endif