ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/virtuals.c
Revision: 2.26
Committed: Wed Nov 15 18:02:53 2023 UTC (5 months, 2 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.25: +3 -3 lines
Log Message:
feat(rpict,rtrace,rcontrib,rtpict): Hyperspectral rendering (except photon map)

File Contents

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