ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.99
Committed: Thu Mar 3 22:44:10 2016 UTC (8 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.98: +13 -5 lines
Log Message:
Made freeing of ambient tree more thorough, freeing nodes as well

File Contents

# Content
1 static const char RCSid[] = "$Id: ambient.c,v 2.98 2015/08/23 00:17:12 greg Exp $";
2 /*
3 * ambient.c - routines dealing with ambient (inter-reflected) component.
4 *
5 * Declarations of external symbols in ambient.h
6 */
7
8 #include "copyright.h"
9
10 #include <string.h>
11
12 #include "platform.h"
13 #include "ray.h"
14 #include "otypes.h"
15 #include "resolu.h"
16 #include "ambient.h"
17 #include "random.h"
18 #include "pmapamb.h"
19
20 #ifndef OCTSCALE
21 #define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */
22 #endif
23
24 extern char *shm_boundary; /* memory sharing boundary */
25
26 #ifndef MAXASET
27 #define MAXASET 4095 /* maximum number of elements in ambient set */
28 #endif
29 OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
30
31 double maxarad; /* maximum ambient radius */
32 double minarad; /* minimum ambient radius */
33
34 static AMBTREE atrunk; /* our ambient trunk node */
35
36 static FILE *ambfp = NULL; /* ambient file pointer */
37 static int nunflshed = 0; /* number of unflushed ambient values */
38
39 #ifndef SORT_THRESH
40 #ifdef SMLMEM
41 #define SORT_THRESH ((16L<<20)/sizeof(AMBVAL))
42 #else
43 #define SORT_THRESH ((64L<<20)/sizeof(AMBVAL))
44 #endif
45 #endif
46 #ifndef SORT_INTVL
47 #define SORT_INTVL (SORT_THRESH<<1)
48 #endif
49 #ifndef MAX_SORT_INTVL
50 #define MAX_SORT_INTVL (SORT_INTVL<<6)
51 #endif
52
53
54 static double avsum = 0.; /* computed ambient value sum (log) */
55 static unsigned int navsum = 0; /* number of values in avsum */
56 static unsigned int nambvals = 0; /* total number of indirect values */
57 static unsigned int nambshare = 0; /* number of values from file */
58 static unsigned long ambclock = 0; /* ambient access clock */
59 static unsigned long lastsort = 0; /* time of last value sort */
60 static long sortintvl = SORT_INTVL; /* time until next sort */
61 static FILE *ambinp = NULL; /* auxiliary file for input */
62 static long lastpos = -1; /* last flush position */
63
64 #define MAXACLOCK (1L<<30) /* clock turnover value */
65 /*
66 * Track access times unless we are sharing ambient values
67 * through memory on a multiprocessor, when we want to avoid
68 * claiming our own memory (copy on write). Go ahead anyway
69 * if more than two thirds of our values are unshared.
70 * Compile with -Dtracktime=0 to turn this code off.
71 */
72 #ifndef tracktime
73 #define tracktime (shm_boundary == NULL || nambvals > 3*nambshare)
74 #endif
75
76 #define AMBFLUSH (BUFSIZ/AMBVALSIZ)
77
78 #define newambval() (AMBVAL *)malloc(sizeof(AMBVAL))
79 #define freeav(av) free((void *)av);
80
81 static void initambfile(int creat);
82 static void avsave(AMBVAL *av);
83 static AMBVAL *avstore(AMBVAL *aval);
84 static AMBTREE *newambtree(void);
85 static void freeambtree(AMBTREE *atp);
86
87 typedef void unloadtf_t(AMBVAL *);
88 static unloadtf_t avinsert;
89 static unloadtf_t av2list;
90 static unloadtf_t avfree;
91 static void unloadatree(AMBTREE *at, unloadtf_t *f);
92
93 static int aposcmp(const void *avp1, const void *avp2);
94 static int avlmemi(AMBVAL *avaddr);
95 static void sortambvals(int always);
96
97 #ifdef F_SETLKW
98 static void aflock(int typ);
99 #endif
100
101
102 void
103 setambres( /* set ambient resolution */
104 int ar
105 )
106 {
107 ambres = ar < 0 ? 0 : ar; /* may be done already */
108 /* set min & max radii */
109 if (ar <= 0) {
110 minarad = 0;
111 maxarad = thescene.cusize*0.2;
112 } else {
113 minarad = thescene.cusize / ar;
114 maxarad = 64.0 * minarad; /* heuristic */
115 if (maxarad > thescene.cusize*0.2)
116 maxarad = thescene.cusize*0.2;
117 }
118 if (minarad <= FTINY)
119 minarad = 10.0*FTINY;
120 if (maxarad <= minarad)
121 maxarad = 64.0 * minarad;
122 }
123
124
125 void
126 setambacc( /* set ambient accuracy */
127 double newa
128 )
129 {
130 static double olda; /* remember previous setting here */
131
132 newa *= (newa > 0);
133 if (fabs(newa - olda) >= .05*(newa + olda)) {
134 ambacc = newa;
135 if (nambvals > 0)
136 sortambvals(1); /* rebuild tree */
137 }
138 }
139
140
141 void
142 setambient(void) /* initialize calculation */
143 {
144 int readonly = 0;
145 long flen;
146 AMBVAL amb;
147 /* make sure we're fresh */
148 ambdone();
149 /* init ambient limits */
150 setambres(ambres);
151 setambacc(ambacc);
152 if (ambfile == NULL || !ambfile[0])
153 return;
154 if (ambacc <= FTINY) {
155 sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
156 ambfile);
157 error(WARNING, errmsg);
158 return;
159 }
160 /* open ambient file */
161 if ((ambfp = fopen(ambfile, "r+")) == NULL)
162 readonly = (ambfp = fopen(ambfile, "r")) != NULL;
163 if (ambfp != NULL) {
164 initambfile(0); /* file exists */
165 lastpos = ftell(ambfp);
166 while (readambval(&amb, ambfp))
167 avstore(&amb);
168 nambshare = nambvals; /* share loaded values */
169 if (readonly) {
170 sprintf(errmsg,
171 "loaded %u values from read-only ambient file",
172 nambvals);
173 error(WARNING, errmsg);
174 fclose(ambfp); /* close file so no writes */
175 ambfp = NULL;
176 return; /* avoid ambsync() */
177 }
178 /* align file pointer */
179 lastpos += (long)nambvals*AMBVALSIZ;
180 flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
181 if (flen != lastpos) {
182 sprintf(errmsg,
183 "ignoring last %ld values in ambient file (corrupted)",
184 (flen - lastpos)/AMBVALSIZ);
185 error(WARNING, errmsg);
186 fseek(ambfp, lastpos, SEEK_SET);
187 #ifndef _WIN32 /* XXX we need a replacement for that one */
188 ftruncate(fileno(ambfp), (off_t)lastpos);
189 #endif
190 }
191 } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
192 initambfile(1); /* else create new file */
193 fflush(ambfp);
194 lastpos = ftell(ambfp);
195 } else {
196 sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
197 error(SYSTEM, errmsg);
198 }
199 #ifdef F_SETLKW
200 aflock(F_UNLCK); /* release file */
201 #endif
202 }
203
204
205 void
206 ambdone(void) /* close ambient file and free memory */
207 {
208 if (ambfp != NULL) { /* close ambient file */
209 ambsync();
210 fclose(ambfp);
211 ambfp = NULL;
212 if (ambinp != NULL) {
213 fclose(ambinp);
214 ambinp = NULL;
215 }
216 lastpos = -1;
217 }
218 /* free ambient tree */
219 unloadatree(&atrunk, avfree);
220 freeambtree(NULL);
221 /* reset state variables */
222 avsum = 0.;
223 navsum = 0;
224 nambvals = 0;
225 nambshare = 0;
226 ambclock = 0;
227 lastsort = 0;
228 sortintvl = SORT_INTVL;
229 }
230
231
232 void
233 ambnotify( /* record new modifier */
234 OBJECT obj
235 )
236 {
237 static int hitlimit = 0;
238 OBJREC *o;
239 char **amblp;
240
241 if (obj == OVOID) { /* starting over */
242 ambset[0] = 0;
243 hitlimit = 0;
244 return;
245 }
246 o = objptr(obj);
247 if (hitlimit || !ismodifier(o->otype))
248 return;
249 for (amblp = amblist; *amblp != NULL; amblp++)
250 if (!strcmp(o->oname, *amblp)) {
251 if (ambset[0] >= MAXASET) {
252 error(WARNING, "too many modifiers in ambient list");
253 hitlimit++;
254 return; /* should this be fatal? */
255 }
256 insertelem(ambset, obj);
257 return;
258 }
259 }
260
261 /************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/
262
263 #ifndef OLDAMB
264
265 #define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr)))
266
267 static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang);
268 static double sumambient(COLOR acol, RAY *r, FVECT rn, int al,
269 AMBTREE *at, FVECT c0, double s);
270 static int makeambient(COLOR acol, RAY *r, FVECT rn, int al);
271 static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv,
272 FVECT uvw[3]);
273
274 void
275 multambient( /* compute ambient component & multiply by coef. */
276 COLOR aval,
277 RAY *r,
278 FVECT nrm
279 )
280 {
281 static int rdepth = 0; /* ambient recursion */
282 COLOR acol, caustic;
283 int ok;
284 double d, l;
285
286 /* PMAP: Factor in ambient from photon map, if enabled and ray is
287 * ambient. Return as all ambient components accounted for, else
288 * continue. */
289 if (ambPmap(aval, r, rdepth))
290 return;
291
292 /* PMAP: Factor in specular-diffuse ambient (caustics) from photon
293 * map, if enabled and ray is primary, else caustic is zero. Continue
294 * with RADIANCE ambient calculation */
295 copycolor(caustic, aval);
296 ambPmapCaustic(caustic, r, rdepth);
297
298 if (ambdiv <= 0) /* no ambient calculation */
299 goto dumbamb;
300 /* check number of bounces */
301 if (rdepth >= ambounce)
302 goto dumbamb;
303 /* check ambient list */
304 if (ambincl != -1 && r->ro != NULL &&
305 ambincl != inset(ambset, r->ro->omod))
306 goto dumbamb;
307
308 if (ambacc <= FTINY) { /* no ambient storage */
309 copycolor(acol, aval);
310 rdepth++;
311 ok = doambient(acol, r, r->rweight,
312 NULL, NULL, NULL, NULL, NULL);
313 rdepth--;
314 if (!ok)
315 goto dumbamb;
316 copycolor(aval, acol);
317
318 /* PMAP: add in caustic */
319 addcolor(aval, caustic);
320 return;
321 }
322
323 if (tracktime) /* sort to minimize thrashing */
324 sortambvals(0);
325 /* interpolate ambient value */
326 setcolor(acol, 0.0, 0.0, 0.0);
327 d = sumambient(acol, r, nrm, rdepth,
328 &atrunk, thescene.cuorg, thescene.cusize);
329
330 if (d > FTINY) {
331 d = 1.0/d;
332 scalecolor(acol, d);
333 multcolor(aval, acol);
334
335 /* PMAP: add in caustic */
336 addcolor(aval, caustic);
337 return;
338 }
339
340 rdepth++; /* need to cache new value */
341 ok = makeambient(acol, r, nrm, rdepth-1);
342 rdepth--;
343
344 if (ok) {
345 multcolor(aval, acol); /* computed new value */
346
347 /* PMAP: add in caustic */
348 addcolor(aval, caustic);
349 return;
350 }
351
352 dumbamb: /* return global value */
353 if ((ambvwt <= 0) | (navsum == 0)) {
354 multcolor(aval, ambval);
355
356 /* PMAP: add in caustic */
357 addcolor(aval, caustic);
358 return;
359 }
360
361 l = bright(ambval); /* average in computations */
362 if (l > FTINY) {
363 d = (log(l)*(double)ambvwt + avsum) /
364 (double)(ambvwt + navsum);
365 d = exp(d) / l;
366 scalecolor(aval, d);
367 multcolor(aval, ambval); /* apply color of ambval */
368 } else {
369 d = exp( avsum / (double)navsum );
370 scalecolor(aval, d); /* neutral color */
371 }
372 }
373
374
375 /* Plug a potential leak where ambient cache value is occluded */
376 static int
377 plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang)
378 {
379 const double cost70sq = 0.1169778; /* cos(70deg)^2 */
380 RAY rtst;
381 FVECT vdif;
382 double normdot, ndotd, nadotd;
383 double a, b, c, t[2];
384
385 ang += 2.*PI*(ang < 0); /* check direction flags */
386 if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) )
387 return(0);
388 /*
389 * Generate test ray, targeting 20 degrees above sample point plane
390 * along surface normal from cache position. This should be high
391 * enough to miss local geometry we don't really care about.
392 */
393 VSUB(vdif, ap->pos, r->rop);
394 normdot = DOT(anorm, r->ron);
395 ndotd = DOT(vdif, r->ron);
396 nadotd = DOT(vdif, anorm);
397 a = normdot*normdot - cost70sq;
398 b = 2.0*(normdot*ndotd - nadotd*cost70sq);
399 c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq;
400 if (quadratic(t, a, b, c) != 2)
401 return(1); /* should rarely happen */
402 if (t[1] <= FTINY)
403 return(0); /* should fail behind test */
404 rayorigin(&rtst, SHADOW, r, NULL);
405 VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */
406 rtst.rmax = normalize(rtst.rdir); /* short ray test */
407 while (localhit(&rtst, &thescene)) { /* check for occluder */
408 if (rtst.ro->omod != OVOID &&
409 (rtst.clipset == NULL ||
410 !inset(rtst.clipset, rtst.ro->omod)))
411 return(1); /* plug light leak */
412 VCOPY(rtst.rorg, rtst.rop); /* skip invisible surface */
413 rtst.rmax -= rtst.rot;
414 rayclear(&rtst);
415 }
416 return(0); /* seems we're OK */
417 }
418
419
420 static double
421 sumambient( /* get interpolated ambient value */
422 COLOR acol,
423 RAY *r,
424 FVECT rn,
425 int al,
426 AMBTREE *at,
427 FVECT c0,
428 double s
429 )
430 { /* initial limit is 10 degrees plus ambacc radians */
431 const double minangle = 10.0 * PI/180.;
432 double maxangle = minangle + ambacc;
433 double wsum = 0.0;
434 FVECT ck0;
435 int i, j;
436 AMBVAL *av;
437
438 if (at->kid != NULL) { /* sum children first */
439 s *= 0.5;
440 for (i = 0; i < 8; i++) {
441 for (j = 0; j < 3; j++) {
442 ck0[j] = c0[j];
443 if (1<<j & i)
444 ck0[j] += s;
445 if (r->rop[j] < ck0[j] - OCTSCALE*s)
446 break;
447 if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
448 break;
449 }
450 if (j == 3)
451 wsum += sumambient(acol, r, rn, al,
452 at->kid+i, ck0, s);
453 }
454 /* good enough? */
455 if (wsum >= 0.05 && s > minarad*10.0)
456 return(wsum);
457 }
458 /* adjust maximum angle */
459 if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6))
460 maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.;
461 /* sum this node */
462 for (av = at->alist; av != NULL; av = av->next) {
463 double u, v, d, delta_r2, delta_t2;
464 COLOR ct;
465 FVECT uvw[3];
466 /* record access */
467 if (tracktime)
468 av->latick = ambclock;
469 /*
470 * Ambient level test
471 */
472 if (av->lvl > al || /* list sorted, so this works */
473 (av->lvl == al) & (av->weight < 0.9*r->rweight))
474 break;
475 /*
476 * Direction test using unperturbed normal
477 */
478 decodedir(uvw[2], av->ndir);
479 d = DOT(uvw[2], r->ron);
480 if (d <= 0.0) /* >= 90 degrees */
481 continue;
482 delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */
483 if (delta_r2 >= maxangle*maxangle)
484 continue;
485 /*
486 * Modified ray behind test
487 */
488 VSUB(ck0, r->rop, av->pos);
489 d = DOT(ck0, uvw[2]);
490 if (d < -minarad*ambacc-.001)
491 continue;
492 d /= av->rad[0];
493 delta_t2 = d*d;
494 if (delta_t2 >= ambacc*ambacc)
495 continue;
496 /*
497 * Elliptical radii test based on Hessian
498 */
499 decodedir(uvw[0], av->udir);
500 VCROSS(uvw[1], uvw[2], uvw[0]);
501 d = (u = DOT(ck0, uvw[0])) / av->rad[0];
502 delta_t2 += d*d;
503 d = (v = DOT(ck0, uvw[1])) / av->rad[1];
504 delta_t2 += d*d;
505 if (delta_t2 >= ambacc*ambacc)
506 continue;
507 /*
508 * Test for potential light leak
509 */
510 if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u)))
511 continue;
512 /*
513 * Extrapolate value and compute final weight (hat function)
514 */
515 if (!extambient(ct, av, r->rop, rn, uvw))
516 continue;
517 d = tfunc(maxangle, sqrt(delta_r2), 0.0) *
518 tfunc(ambacc, sqrt(delta_t2), 0.0);
519 scalecolor(ct, d);
520 addcolor(acol, ct);
521 wsum += d;
522 }
523 return(wsum);
524 }
525
526
527 static int
528 makeambient( /* make a new ambient value for storage */
529 COLOR acol,
530 RAY *r,
531 FVECT rn,
532 int al
533 )
534 {
535 AMBVAL amb;
536 FVECT uvw[3];
537 int i;
538
539 amb.weight = 1.0; /* compute weight */
540 for (i = al; i-- > 0; )
541 amb.weight *= AVGREFL;
542 if (r->rweight < 0.1*amb.weight) /* heuristic override */
543 amb.weight = 1.25*r->rweight;
544 setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
545 /* compute ambient */
546 i = doambient(acol, r, amb.weight,
547 uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral);
548 scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */
549 if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */
550 return(i);
551 /* store value */
552 VCOPY(amb.pos, r->rop);
553 amb.ndir = encodedir(r->ron);
554 amb.udir = encodedir(uvw[0]);
555 amb.lvl = al;
556 copycolor(amb.val, acol);
557 /* insert into tree */
558 avsave(&amb); /* and save to file */
559 if (rn != r->ron) { /* texture */
560 VCOPY(uvw[2], r->ron);
561 extambient(acol, &amb, r->rop, rn, uvw);
562 }
563 return(1);
564 }
565
566
567 static int
568 extambient( /* extrapolate value at pv, nv */
569 COLOR cr,
570 AMBVAL *ap,
571 FVECT pv,
572 FVECT nv,
573 FVECT uvw[3]
574 )
575 {
576 const double min_d = 0.05;
577 static FVECT my_uvw[3];
578 FVECT v1;
579 int i;
580 double d = 1.0; /* zeroeth order */
581
582 if (uvw == NULL) { /* need local coordinates? */
583 decodedir(my_uvw[2], ap->ndir);
584 decodedir(my_uvw[0], ap->udir);
585 VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]);
586 uvw = my_uvw;
587 }
588 for (i = 3; i--; ) /* gradient due to translation */
589 d += (pv[i] - ap->pos[i]) *
590 (ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]);
591
592 VCROSS(v1, uvw[2], nv); /* gradient due to rotation */
593 for (i = 3; i--; )
594 d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]);
595
596 if (d < min_d) /* should not use if we can avoid it */
597 d = min_d;
598 copycolor(cr, ap->val);
599 scalecolor(cr, d);
600 return(d > min_d);
601 }
602
603
604 static void
605 avinsert( /* insert ambient value in our tree */
606 AMBVAL *av
607 )
608 {
609 AMBTREE *at;
610 AMBVAL *ap;
611 AMBVAL avh;
612 FVECT ck0;
613 double s;
614 int branch;
615 int i;
616
617 if (av->rad[0] <= FTINY)
618 error(CONSISTENCY, "zero ambient radius in avinsert");
619 at = &atrunk;
620 VCOPY(ck0, thescene.cuorg);
621 s = thescene.cusize;
622 while (s*(OCTSCALE/2) > av->rad[1]*ambacc) {
623 if (at->kid == NULL)
624 if ((at->kid = newambtree()) == NULL)
625 error(SYSTEM, "out of memory in avinsert");
626 s *= 0.5;
627 branch = 0;
628 for (i = 0; i < 3; i++)
629 if (av->pos[i] > ck0[i] + s) {
630 ck0[i] += s;
631 branch |= 1 << i;
632 }
633 at = at->kid + branch;
634 }
635 avh.next = at->alist; /* order by increasing level */
636 for (ap = &avh; ap->next != NULL; ap = ap->next)
637 if ( ap->next->lvl > av->lvl ||
638 (ap->next->lvl == av->lvl) &
639 (ap->next->weight <= av->weight) )
640 break;
641 av->next = ap->next;
642 ap->next = (AMBVAL*)av;
643 at->alist = avh.next;
644 }
645
646
647 #else /* ! NEWAMB */
648
649 static double sumambient(COLOR acol, RAY *r, FVECT rn, int al,
650 AMBTREE *at, FVECT c0, double s);
651 static double makeambient(COLOR acol, RAY *r, FVECT rn, int al);
652 static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv);
653
654
655 void
656 multambient( /* compute ambient component & multiply by coef. */
657 COLOR aval,
658 RAY *r,
659 FVECT nrm
660 )
661 {
662 static int rdepth = 0; /* ambient recursion */
663 COLOR acol, caustic;
664 double d, l;
665
666 /* PMAP: Factor in ambient from global photon map (if enabled) and return
667 * as all ambient components accounted for */
668 if (ambPmap(aval, r, rdepth))
669 return;
670
671 /* PMAP: Otherwise factor in ambient from caustic photon map
672 * (ambPmapCaustic() returns zero if caustic photons disabled) and
673 * continue with RADIANCE ambient calculation */
674 copycolor(caustic, aval);
675 ambPmapCaustic(caustic, r, rdepth);
676
677 if (ambdiv <= 0) /* no ambient calculation */
678 goto dumbamb;
679 /* check number of bounces */
680 if (rdepth >= ambounce)
681 goto dumbamb;
682 /* check ambient list */
683 if (ambincl != -1 && r->ro != NULL &&
684 ambincl != inset(ambset, r->ro->omod))
685 goto dumbamb;
686
687 if (ambacc <= FTINY) { /* no ambient storage */
688 copycolor(acol, aval);
689 rdepth++;
690 d = doambient(acol, r, r->rweight, NULL, NULL);
691 rdepth--;
692 if (d <= FTINY)
693 goto dumbamb;
694 copycolor(aval, acol);
695
696 /* PMAP: add in caustic */
697 addcolor(aval, caustic);
698 return;
699 }
700
701 if (tracktime) /* sort to minimize thrashing */
702 sortambvals(0);
703 /* interpolate ambient value */
704 setcolor(acol, 0.0, 0.0, 0.0);
705 d = sumambient(acol, r, nrm, rdepth,
706 &atrunk, thescene.cuorg, thescene.cusize);
707
708 if (d > FTINY) {
709 d = 1.0/d;
710 scalecolor(acol, d);
711 multcolor(aval, acol);
712
713 /* PMAP: add in caustic */
714 addcolor(aval, caustic);
715 return;
716 }
717
718 rdepth++; /* need to cache new value */
719 d = makeambient(acol, r, nrm, rdepth-1);
720 rdepth--;
721
722 if (d > FTINY) {
723 multcolor(aval, acol); /* got new value */
724
725 /* PMAP: add in caustic */
726 addcolor(aval, caustic);
727 return;
728 }
729
730 dumbamb: /* return global value */
731 if ((ambvwt <= 0) | (navsum == 0)) {
732 multcolor(aval, ambval);
733
734 /* PMAP: add in caustic */
735 addcolor(aval, caustic);
736 return;
737 }
738
739 l = bright(ambval); /* average in computations */
740 if (l > FTINY) {
741 d = (log(l)*(double)ambvwt + avsum) /
742 (double)(ambvwt + navsum);
743 d = exp(d) / l;
744 scalecolor(aval, d);
745 multcolor(aval, ambval); /* apply color of ambval */
746 } else {
747 d = exp( avsum / (double)navsum );
748 scalecolor(aval, d); /* neutral color */
749 }
750 }
751
752
753 static double
754 sumambient( /* get interpolated ambient value */
755 COLOR acol,
756 RAY *r,
757 FVECT rn,
758 int al,
759 AMBTREE *at,
760 FVECT c0,
761 double s
762 )
763 {
764 double d, e1, e2, wt, wsum;
765 COLOR ct;
766 FVECT ck0;
767 int i;
768 int j;
769 AMBVAL *av;
770
771 wsum = 0.0;
772 /* do this node */
773 for (av = at->alist; av != NULL; av = av->next) {
774 double rn_dot = -2.0;
775 if (tracktime)
776 av->latick = ambclock;
777 /*
778 * Ambient level test.
779 */
780 if (av->lvl > al || /* list sorted, so this works */
781 (av->lvl == al) & (av->weight < 0.9*r->rweight))
782 break;
783 /*
784 * Ambient radius test.
785 */
786 VSUB(ck0, av->pos, r->rop);
787 e1 = DOT(ck0, ck0) / (av->rad * av->rad);
788 if (e1 > ambacc*ambacc*1.21)
789 continue;
790 /*
791 * Direction test using closest normal.
792 */
793 d = DOT(av->dir, r->ron);
794 if (rn != r->ron) {
795 rn_dot = DOT(av->dir, rn);
796 if (rn_dot > 1.0-FTINY)
797 rn_dot = 1.0-FTINY;
798 if (rn_dot >= d-FTINY) {
799 d = rn_dot;
800 rn_dot = -2.0;
801 }
802 }
803 e2 = (1.0 - d) * r->rweight;
804 if (e2 < 0.0)
805 e2 = 0.0;
806 else if (e1 + e2 > ambacc*ambacc*1.21)
807 continue;
808 /*
809 * Ray behind test.
810 */
811 d = 0.0;
812 for (j = 0; j < 3; j++)
813 d += (r->rop[j] - av->pos[j]) *
814 (av->dir[j] + r->ron[j]);
815 if (d*0.5 < -minarad*ambacc-.001)
816 continue;
817 /*
818 * Jittering final test reduces image artifacts.
819 */
820 e1 = sqrt(e1);
821 e2 = sqrt(e2);
822 wt = e1 + e2;
823 if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
824 continue;
825 /*
826 * Recompute directional error using perturbed normal
827 */
828 if (rn_dot > 0.0) {
829 e2 = sqrt((1.0 - rn_dot)*r->rweight);
830 wt = e1 + e2;
831 }
832 if (wt <= 1e-3)
833 wt = 1e3;
834 else
835 wt = 1.0 / wt;
836 wsum += wt;
837 extambient(ct, av, r->rop, rn);
838 scalecolor(ct, wt);
839 addcolor(acol, ct);
840 }
841 if (at->kid == NULL)
842 return(wsum);
843 /* do children */
844 s *= 0.5;
845 for (i = 0; i < 8; i++) {
846 for (j = 0; j < 3; j++) {
847 ck0[j] = c0[j];
848 if (1<<j & i)
849 ck0[j] += s;
850 if (r->rop[j] < ck0[j] - OCTSCALE*s)
851 break;
852 if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
853 break;
854 }
855 if (j == 3)
856 wsum += sumambient(acol, r, rn, al,
857 at->kid+i, ck0, s);
858 }
859 return(wsum);
860 }
861
862
863 static double
864 makeambient( /* make a new ambient value for storage */
865 COLOR acol,
866 RAY *r,
867 FVECT rn,
868 int al
869 )
870 {
871 AMBVAL amb;
872 FVECT gp, gd;
873 int i;
874
875 amb.weight = 1.0; /* compute weight */
876 for (i = al; i-- > 0; )
877 amb.weight *= AVGREFL;
878 if (r->rweight < 0.1*amb.weight) /* heuristic override */
879 amb.weight = 1.25*r->rweight;
880 setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
881 /* compute ambient */
882 amb.rad = doambient(acol, r, amb.weight, gp, gd);
883 if (amb.rad <= FTINY) {
884 setcolor(acol, 0.0, 0.0, 0.0);
885 return(0.0);
886 }
887 scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */
888 /* store value */
889 VCOPY(amb.pos, r->rop);
890 VCOPY(amb.dir, r->ron);
891 amb.lvl = al;
892 copycolor(amb.val, acol);
893 VCOPY(amb.gpos, gp);
894 VCOPY(amb.gdir, gd);
895 /* insert into tree */
896 avsave(&amb); /* and save to file */
897 if (rn != r->ron)
898 extambient(acol, &amb, r->rop, rn); /* texture */
899 return(amb.rad);
900 }
901
902
903 static void
904 extambient( /* extrapolate value at pv, nv */
905 COLOR cr,
906 AMBVAL *ap,
907 FVECT pv,
908 FVECT nv
909 )
910 {
911 FVECT v1;
912 int i;
913 double d;
914
915 d = 1.0; /* zeroeth order */
916 /* gradient due to translation */
917 for (i = 0; i < 3; i++)
918 d += ap->gpos[i]*(pv[i]-ap->pos[i]);
919 /* gradient due to rotation */
920 VCROSS(v1, ap->dir, nv);
921 d += DOT(ap->gdir, v1);
922 if (d <= 0.0) {
923 setcolor(cr, 0.0, 0.0, 0.0);
924 return;
925 }
926 copycolor(cr, ap->val);
927 scalecolor(cr, d);
928 }
929
930
931 static void
932 avinsert( /* insert ambient value in our tree */
933 AMBVAL *av
934 )
935 {
936 AMBTREE *at;
937 AMBVAL *ap;
938 AMBVAL avh;
939 FVECT ck0;
940 double s;
941 int branch;
942 int i;
943
944 if (av->rad <= FTINY)
945 error(CONSISTENCY, "zero ambient radius in avinsert");
946 at = &atrunk;
947 VCOPY(ck0, thescene.cuorg);
948 s = thescene.cusize;
949 while (s*(OCTSCALE/2) > av->rad*ambacc) {
950 if (at->kid == NULL)
951 if ((at->kid = newambtree()) == NULL)
952 error(SYSTEM, "out of memory in avinsert");
953 s *= 0.5;
954 branch = 0;
955 for (i = 0; i < 3; i++)
956 if (av->pos[i] > ck0[i] + s) {
957 ck0[i] += s;
958 branch |= 1 << i;
959 }
960 at = at->kid + branch;
961 }
962 avh.next = at->alist; /* order by increasing level */
963 for (ap = &avh; ap->next != NULL; ap = ap->next)
964 if ( ap->next->lvl > av->lvl ||
965 (ap->next->lvl == av->lvl) &
966 (ap->next->weight <= av->weight) )
967 break;
968 av->next = ap->next;
969 ap->next = (AMBVAL*)av;
970 at->alist = avh.next;
971 }
972
973 #endif /* ! NEWAMB */
974
975 /************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/
976
977 static void
978 initambfile( /* initialize ambient file */
979 int cre8
980 )
981 {
982 extern char *progname, *octname;
983 static char *mybuf = NULL;
984
985 #ifdef F_SETLKW
986 aflock(cre8 ? F_WRLCK : F_RDLCK);
987 #endif
988 SET_FILE_BINARY(ambfp);
989 if (mybuf == NULL)
990 mybuf = (char *)bmalloc(BUFSIZ+8);
991 setbuf(ambfp, mybuf);
992 if (cre8) { /* new file */
993 newheader("RADIANCE", ambfp);
994 fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
995 progname, colval(ambval,RED),
996 colval(ambval,GRN), colval(ambval,BLU),
997 ambvwt, ambounce, ambacc);
998 fprintf(ambfp, "-ad %d -as %d -ar %d ",
999 ambdiv, ambssamp, ambres);
1000 if (octname != NULL)
1001 fputs(octname, ambfp);
1002 fputc('\n', ambfp);
1003 fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
1004 fputnow(ambfp);
1005 fputformat(AMBFMT, ambfp);
1006 fputc('\n', ambfp);
1007 putambmagic(ambfp);
1008 } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
1009 error(USER, "bad ambient file");
1010 }
1011
1012
1013 static void
1014 avsave( /* insert and save an ambient value */
1015 AMBVAL *av
1016 )
1017 {
1018 avstore(av);
1019 if (ambfp == NULL)
1020 return;
1021 if (writambval(av, ambfp) < 0)
1022 goto writerr;
1023 if (++nunflshed >= AMBFLUSH)
1024 if (ambsync() == EOF)
1025 goto writerr;
1026 return;
1027 writerr:
1028 error(SYSTEM, "error writing to ambient file");
1029 }
1030
1031
1032 static AMBVAL *
1033 avstore( /* allocate memory and save aval */
1034 AMBVAL *aval
1035 )
1036 {
1037 AMBVAL *av;
1038 double d;
1039
1040 if ((av = newambval()) == NULL)
1041 error(SYSTEM, "out of memory in avstore");
1042 *av = *aval;
1043 av->latick = ambclock;
1044 av->next = NULL;
1045 nambvals++;
1046 d = bright(av->val);
1047 if (d > FTINY) { /* add to log sum for averaging */
1048 avsum += log(d);
1049 navsum++;
1050 }
1051 avinsert(av); /* insert in our cache tree */
1052 return(av);
1053 }
1054
1055
1056 #define ATALLOCSZ 512 /* #/8 trees to allocate at once */
1057
1058 static AMBTREE *atfreelist = NULL; /* free ambient tree structures */
1059
1060
1061 static AMBTREE *
1062 newambtree(void) /* allocate 8 ambient tree structs */
1063 {
1064 AMBTREE *atp, *upperlim;
1065
1066 if (atfreelist == NULL) { /* get more nodes */
1067 atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
1068 if (atfreelist == NULL)
1069 return(NULL);
1070 /* link new free list */
1071 upperlim = atfreelist + 8*(ATALLOCSZ-1);
1072 for (atp = atfreelist; atp < upperlim; atp += 8)
1073 atp->kid = atp + 8;
1074 atp->kid = NULL;
1075 }
1076 atp = atfreelist;
1077 atfreelist = atp->kid;
1078 memset((char *)atp, '\0', 8*sizeof(AMBTREE));
1079 return(atp);
1080 }
1081
1082
1083 static void
1084 freeambtree( /* free 8 ambient tree structs */
1085 AMBTREE *atp
1086 )
1087 {
1088 if (atp == NULL) { /* freeing free list? */
1089 while ((atp = atfreelist) != NULL) {
1090 atfreelist = atp->kid;
1091 free(atp);
1092 }
1093 return;
1094 }
1095 atp->kid = atfreelist; /* else push node onto free list */
1096 atfreelist = atp;
1097 }
1098
1099
1100 static void
1101 unloadatree( /* unload an ambient value tree */
1102 AMBTREE *at,
1103 unloadtf_t *f
1104 )
1105 {
1106 AMBVAL *av;
1107 int i;
1108 /* transfer values at this node */
1109 for (av = at->alist; av != NULL; av = at->alist) {
1110 at->alist = av->next;
1111 (*f)(av);
1112 }
1113 if (at->kid == NULL)
1114 return;
1115 for (i = 0; i < 8; i++) /* transfer and free children */
1116 unloadatree(at->kid+i, f);
1117 freeambtree(at->kid);
1118 at->kid = NULL;
1119 }
1120
1121
1122 static struct avl {
1123 AMBVAL *p;
1124 unsigned long t;
1125 } *avlist1; /* ambient value list with ticks */
1126 static AMBVAL **avlist2; /* memory positions for sorting */
1127 static int i_avlist; /* index for lists */
1128
1129 static int alatcmp(const void *av1, const void *av2);
1130
1131 static void
1132 avfree(AMBVAL *av)
1133 {
1134 free(av);
1135 }
1136
1137 static void
1138 av2list(
1139 AMBVAL *av
1140 )
1141 {
1142 #ifdef DEBUG
1143 if (i_avlist >= nambvals)
1144 error(CONSISTENCY, "too many ambient values in av2list1");
1145 #endif
1146 avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
1147 avlist1[i_avlist++].t = av->latick;
1148 }
1149
1150
1151 static int
1152 alatcmp( /* compare ambient values for MRA */
1153 const void *av1,
1154 const void *av2
1155 )
1156 {
1157 long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
1158 return(lc<0 ? -1 : lc>0 ? 1 : 0);
1159 }
1160
1161
1162 /* GW NOTE 2002/10/3:
1163 * I used to compare AMBVAL pointers, but found that this was the
1164 * cause of a serious consistency error with gcc, since the optimizer
1165 * uses some dangerous trick in pointer subtraction that
1166 * assumes pointers differ by exact struct size increments.
1167 */
1168 static int
1169 aposcmp( /* compare ambient value positions */
1170 const void *avp1,
1171 const void *avp2
1172 )
1173 {
1174 long diff = *(char * const *)avp1 - *(char * const *)avp2;
1175 if (diff < 0)
1176 return(-1);
1177 return(diff > 0);
1178 }
1179
1180
1181 static int
1182 avlmemi( /* find list position from address */
1183 AMBVAL *avaddr
1184 )
1185 {
1186 AMBVAL **avlpp;
1187
1188 avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2,
1189 nambvals, sizeof(AMBVAL *), &aposcmp);
1190 if (avlpp == NULL)
1191 error(CONSISTENCY, "address not found in avlmemi");
1192 return(avlpp - avlist2);
1193 }
1194
1195
1196 static void
1197 sortambvals( /* resort ambient values */
1198 int always
1199 )
1200 {
1201 AMBTREE oldatrunk;
1202 AMBVAL tav, *tap, *pnext;
1203 int i, j;
1204 /* see if it's time yet */
1205 if (!always && (ambclock++ < lastsort+sortintvl ||
1206 nambvals < SORT_THRESH))
1207 return;
1208 /*
1209 * The idea here is to minimize memory thrashing
1210 * in VM systems by improving reference locality.
1211 * We do this by periodically sorting our stored ambient
1212 * values in memory in order of most recently to least
1213 * recently accessed. This ordering was chosen so that new
1214 * ambient values (which tend to be less important) go into
1215 * higher memory with the infrequently accessed values.
1216 * Since we expect our values to need sorting less
1217 * frequently as the process continues, we double our
1218 * waiting interval after each call.
1219 * This routine is also called by setambacc() with
1220 * the "always" parameter set to 1 so that the ambient
1221 * tree will be rebuilt with the new accuracy parameter.
1222 */
1223 if (tracktime) { /* allocate pointer arrays to sort */
1224 avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *));
1225 avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl));
1226 } else {
1227 avlist2 = NULL;
1228 avlist1 = NULL;
1229 }
1230 if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */
1231 if (avlist2 != NULL)
1232 free((void *)avlist2);
1233 if (always) { /* rebuild without sorting */
1234 oldatrunk = atrunk;
1235 atrunk.alist = NULL;
1236 atrunk.kid = NULL;
1237 unloadatree(&oldatrunk, avinsert);
1238 }
1239 } else { /* sort memory by last access time */
1240 /*
1241 * Sorting memory is tricky because it isn't contiguous.
1242 * We have to sort an array of pointers by MRA and also
1243 * by memory position. We then copy values in "loops"
1244 * to minimize memory hits. Nevertheless, we will visit
1245 * everyone at least twice, and this is an expensive process
1246 * when we're thrashing, which is when we need to do it.
1247 */
1248 #ifdef DEBUG
1249 sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...",
1250 nambvals, ambclock);
1251 eputs(errmsg);
1252 #endif
1253 i_avlist = 0;
1254 unloadatree(&atrunk, av2list); /* empty current tree */
1255 #ifdef DEBUG
1256 if (i_avlist < nambvals)
1257 error(CONSISTENCY, "missing ambient values in sortambvals");
1258 #endif
1259 qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp);
1260 qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp);
1261 for (i = 0; i < nambvals; i++) {
1262 if (avlist1[i].p == NULL)
1263 continue;
1264 tap = avlist2[i];
1265 tav = *tap;
1266 for (j = i; (pnext = avlist1[j].p) != tap;
1267 j = avlmemi(pnext)) {
1268 *(avlist2[j]) = *pnext;
1269 avinsert(avlist2[j]);
1270 avlist1[j].p = NULL;
1271 }
1272 *(avlist2[j]) = tav;
1273 avinsert(avlist2[j]);
1274 avlist1[j].p = NULL;
1275 }
1276 free((void *)avlist1);
1277 free((void *)avlist2);
1278 /* compute new sort interval */
1279 sortintvl = ambclock - lastsort;
1280 if (sortintvl >= MAX_SORT_INTVL/2)
1281 sortintvl = MAX_SORT_INTVL;
1282 else
1283 sortintvl <<= 1; /* wait twice as long next */
1284 #ifdef DEBUG
1285 eputs("done\n");
1286 #endif
1287 }
1288 if (ambclock >= MAXACLOCK)
1289 ambclock = MAXACLOCK/2;
1290 lastsort = ambclock;
1291 }
1292
1293
1294 #ifdef F_SETLKW
1295
1296 static void
1297 aflock( /* lock/unlock ambient file */
1298 int typ
1299 )
1300 {
1301 static struct flock fls; /* static so initialized to zeroes */
1302
1303 if (typ == fls.l_type) /* already called? */
1304 return;
1305 fls.l_type = typ;
1306 if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
1307 error(SYSTEM, "cannot (un)lock ambient file");
1308 }
1309
1310
1311 int
1312 ambsync(void) /* synchronize ambient file */
1313 {
1314 long flen;
1315 AMBVAL avs;
1316 int n;
1317
1318 if (ambfp == NULL) /* no ambient file? */
1319 return(0);
1320 /* gain appropriate access */
1321 aflock(nunflshed ? F_WRLCK : F_RDLCK);
1322 /* see if file has grown */
1323 if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
1324 goto seekerr;
1325 if ((n = flen - lastpos) > 0) { /* file has grown */
1326 if (ambinp == NULL) { /* use duplicate filedes */
1327 ambinp = fdopen(dup(fileno(ambfp)), "r");
1328 if (ambinp == NULL)
1329 error(SYSTEM, "fdopen failed in ambsync");
1330 }
1331 if (fseek(ambinp, lastpos, SEEK_SET) < 0)
1332 goto seekerr;
1333 while (n >= AMBVALSIZ) { /* load contributed values */
1334 if (!readambval(&avs, ambinp)) {
1335 sprintf(errmsg,
1336 "ambient file \"%s\" corrupted near character %ld",
1337 ambfile, flen - n);
1338 error(WARNING, errmsg);
1339 break;
1340 }
1341 avstore(&avs);
1342 n -= AMBVALSIZ;
1343 }
1344 lastpos = flen - n;
1345 /*** seek always as safety measure
1346 if (n) ***/ /* alignment */
1347 if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0)
1348 goto seekerr;
1349 }
1350 n = fflush(ambfp); /* calls write() at last */
1351 if (n != EOF)
1352 lastpos += (long)nunflshed*AMBVALSIZ;
1353 else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
1354 goto seekerr;
1355
1356 aflock(F_UNLCK); /* release file */
1357 nunflshed = 0;
1358 return(n);
1359 seekerr:
1360 error(SYSTEM, "seek failed in ambsync");
1361 return -1; /* pro forma return */
1362 }
1363
1364 #else /* ! F_SETLKW */
1365
1366 int
1367 ambsync(void) /* flush ambient file */
1368 {
1369 if (ambfp == NULL)
1370 return(0);
1371 nunflshed = 0;
1372 return(fflush(ambfp));
1373 }
1374
1375 #endif /* ! F_SETLKW */