1 |
– |
#ifndef lint |
1 |
|
static const char RCSid[] = "$Id$"; |
3 |
– |
#endif |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
|
* |
12 |
|
#include "platform.h" |
13 |
|
#include "ray.h" |
14 |
|
#include "otypes.h" |
15 |
+ |
#include "otspecial.h" |
16 |
|
#include "resolu.h" |
17 |
|
#include "ambient.h" |
18 |
+ |
#include "source.h" |
19 |
|
#include "random.h" |
20 |
+ |
#include "pmapamb.h" |
21 |
|
|
22 |
|
#ifndef OCTSCALE |
23 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
53 |
|
#endif |
54 |
|
|
55 |
|
|
55 |
– |
static double qambacc = 0.; /* ambient accuracy to the 1/4 power */ |
56 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
57 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
58 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
78 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
79 |
|
|
80 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
81 |
– |
#define freeav(av) free((void *)av); |
81 |
|
|
82 |
|
static void initambfile(int creat); |
83 |
|
static void avsave(AMBVAL *av); |
109 |
|
/* set min & max radii */ |
110 |
|
if (ar <= 0) { |
111 |
|
minarad = 0; |
112 |
< |
maxarad = thescene.cusize*0.5; |
112 |
> |
maxarad = thescene.cusize*0.2; |
113 |
|
} else { |
114 |
|
minarad = thescene.cusize / ar; |
115 |
|
maxarad = 64.0 * minarad; /* heuristic */ |
116 |
< |
if (maxarad > thescene.cusize*0.5) |
117 |
< |
maxarad = thescene.cusize*0.5; |
116 |
> |
if (maxarad > thescene.cusize*0.2) |
117 |
> |
maxarad = thescene.cusize*0.2; |
118 |
|
} |
119 |
|
if (minarad <= FTINY) |
120 |
|
minarad = 10.0*FTINY; |
128 |
|
double newa |
129 |
|
) |
130 |
|
{ |
131 |
< |
double olda = qambacc*qambacc*qambacc*qambacc; |
131 |
> |
static double olda; /* remember previous setting here */ |
132 |
|
|
133 |
|
newa *= (newa > 0); |
134 |
|
if (fabs(newa - olda) >= .05*(newa + olda)) { |
135 |
< |
qambacc = sqrt(sqrt(ambacc = newa)); |
135 |
> |
ambacc = newa; |
136 |
|
if (nambvals > 0) |
137 |
|
sortambvals(1); /* rebuild tree */ |
138 |
|
} |
165 |
|
initambfile(0); /* file exists */ |
166 |
|
lastpos = ftell(ambfp); |
167 |
|
while (readambval(&amb, ambfp)) |
168 |
< |
avinsert(avstore(&amb)); |
168 |
> |
avstore(&amb); |
169 |
|
nambshare = nambvals; /* share loaded values */ |
170 |
|
if (readonly) { |
171 |
|
sprintf(errmsg, |
185 |
|
(flen - lastpos)/AMBVALSIZ); |
186 |
|
error(WARNING, errmsg); |
187 |
|
fseek(ambfp, lastpos, SEEK_SET); |
189 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
188 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
191 |
– |
#endif |
189 |
|
} |
190 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
191 |
|
initambfile(1); /* else create new file */ |
195 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
196 |
|
error(SYSTEM, errmsg); |
197 |
|
} |
201 |
– |
#ifdef getc_unlocked |
202 |
– |
flockfile(ambfp); /* application-level lock */ |
203 |
– |
#endif |
198 |
|
#ifdef F_SETLKW |
199 |
|
aflock(F_UNLCK); /* release file */ |
200 |
|
#endif |
215 |
|
lastpos = -1; |
216 |
|
} |
217 |
|
/* free ambient tree */ |
218 |
< |
unloadatree(&atrunk, &avfree); |
218 |
> |
unloadatree(&atrunk, avfree); |
219 |
|
/* reset state variables */ |
220 |
|
avsum = 0.; |
221 |
|
navsum = 0; |
258 |
|
|
259 |
|
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
260 |
|
|
261 |
< |
#ifdef NEWAMB |
261 |
> |
#ifndef OLDAMB |
262 |
|
|
263 |
|
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
264 |
|
|
265 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
266 |
|
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
267 |
|
AMBTREE *at, FVECT c0, double s); |
268 |
|
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
269 |
< |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
269 |
> |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
270 |
|
FVECT uvw[3]); |
271 |
|
|
272 |
|
void |
277 |
|
) |
278 |
|
{ |
279 |
|
static int rdepth = 0; /* ambient recursion */ |
280 |
< |
COLOR acol; |
281 |
< |
int ok; |
280 |
> |
COLOR acol, caustic; |
281 |
> |
int i, ok; |
282 |
|
double d, l; |
283 |
|
|
284 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
285 |
+ |
* ambient. Return as all ambient components accounted for, else |
286 |
+ |
* continue. */ |
287 |
+ |
if (ambPmap(aval, r, rdepth)) |
288 |
+ |
return; |
289 |
+ |
|
290 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
291 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
292 |
+ |
* with RADIANCE ambient calculation */ |
293 |
+ |
copycolor(caustic, aval); |
294 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
295 |
+ |
|
296 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
297 |
|
goto dumbamb; |
298 |
|
/* check number of bounces */ |
304 |
|
goto dumbamb; |
305 |
|
|
306 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
307 |
+ |
FVECT uvd[2]; |
308 |
+ |
float dgrad[2], *dgp = NULL; |
309 |
+ |
|
310 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
311 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
312 |
|
copycolor(acol, aval); |
313 |
|
rdepth++; |
314 |
< |
ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL); |
314 |
> |
ok = doambient(acol, r, r->rweight, |
315 |
> |
uvd, NULL, NULL, dgp, NULL); |
316 |
|
rdepth--; |
317 |
|
if (!ok) |
318 |
|
goto dumbamb; |
319 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
320 |
+ |
FVECT v1; |
321 |
+ |
VCROSS(v1, r->ron, nrm); |
322 |
+ |
d = 1.0; |
323 |
+ |
for (i = 3; i--; ) |
324 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
325 |
+ |
if (d >= 0.05) |
326 |
+ |
scalecolor(acol, d); |
327 |
+ |
} |
328 |
|
copycolor(aval, acol); |
329 |
+ |
|
330 |
+ |
/* PMAP: add in caustic */ |
331 |
+ |
addcolor(aval, caustic); |
332 |
|
return; |
333 |
|
} |
334 |
|
|
338 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
339 |
|
d = sumambient(acol, r, nrm, rdepth, |
340 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
341 |
+ |
|
342 |
|
if (d > FTINY) { |
343 |
|
d = 1.0/d; |
344 |
|
scalecolor(acol, d); |
345 |
|
multcolor(aval, acol); |
346 |
+ |
|
347 |
+ |
/* PMAP: add in caustic */ |
348 |
+ |
addcolor(aval, caustic); |
349 |
|
return; |
350 |
|
} |
351 |
+ |
|
352 |
|
rdepth++; /* need to cache new value */ |
353 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
354 |
|
rdepth--; |
355 |
+ |
|
356 |
|
if (ok) { |
357 |
|
multcolor(aval, acol); /* computed new value */ |
358 |
+ |
|
359 |
+ |
/* PMAP: add in caustic */ |
360 |
+ |
addcolor(aval, caustic); |
361 |
|
return; |
362 |
|
} |
363 |
+ |
|
364 |
|
dumbamb: /* return global value */ |
365 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
366 |
|
multcolor(aval, ambval); |
367 |
+ |
|
368 |
+ |
/* PMAP: add in caustic */ |
369 |
+ |
addcolor(aval, caustic); |
370 |
|
return; |
371 |
|
} |
372 |
< |
l = bright(ambval); /* average in computations */ |
372 |
> |
|
373 |
> |
l = bright(ambval); /* average in computations */ |
374 |
|
if (l > FTINY) { |
375 |
|
d = (log(l)*(double)ambvwt + avsum) / |
376 |
|
(double)(ambvwt + navsum); |
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
< |
double |
387 |
> |
/* Plug a potential leak where ambient cache value is occluded */ |
388 |
> |
static int |
389 |
> |
plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang) |
390 |
> |
{ |
391 |
> |
const double cost70sq = 0.1169778; /* cos(70deg)^2 */ |
392 |
> |
RAY rtst; |
393 |
> |
FVECT vdif; |
394 |
> |
double normdot, ndotd, nadotd; |
395 |
> |
double a, b, c, t[2]; |
396 |
> |
|
397 |
> |
ang += 2.*PI*(ang < 0); /* check direction flags */ |
398 |
> |
if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) ) |
399 |
> |
return(0); |
400 |
> |
/* |
401 |
> |
* Generate test ray, targeting 20 degrees above sample point plane |
402 |
> |
* along surface normal from cache position. This should be high |
403 |
> |
* enough to miss local geometry we don't really care about. |
404 |
> |
*/ |
405 |
> |
VSUB(vdif, ap->pos, r->rop); |
406 |
> |
normdot = DOT(anorm, r->ron); |
407 |
> |
ndotd = DOT(vdif, r->ron); |
408 |
> |
nadotd = DOT(vdif, anorm); |
409 |
> |
a = normdot*normdot - cost70sq; |
410 |
> |
b = 2.0*(normdot*ndotd - nadotd*cost70sq); |
411 |
> |
c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq; |
412 |
> |
if (quadratic(t, a, b, c) != 2) |
413 |
> |
return(1); /* should rarely happen */ |
414 |
> |
if (t[1] <= FTINY) |
415 |
> |
return(0); /* should fail behind test */ |
416 |
> |
rayorigin(&rtst, SHADOW, r, NULL); |
417 |
> |
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
418 |
> |
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
419 |
> |
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
420 |
> |
OBJREC *m = findmaterial(rtst.ro); |
421 |
> |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
422 |
> |
(rtst.clipset == NULL || |
423 |
> |
!inset(rtst.clipset, rtst.ro->omod))) |
424 |
> |
return(1); /* plug light leak */ |
425 |
> |
VCOPY(rtst.rorg, rtst.rop); /* skip invisible surface */ |
426 |
> |
rtst.rmax -= rtst.rot; |
427 |
> |
rayclear(&rtst); |
428 |
> |
} |
429 |
> |
return(0); /* seems we're OK */ |
430 |
> |
} |
431 |
> |
|
432 |
> |
|
433 |
> |
static double |
434 |
|
sumambient( /* get interpolated ambient value */ |
435 |
|
COLOR acol, |
436 |
|
RAY *r, |
442 |
|
) |
443 |
|
{ /* initial limit is 10 degrees plus ambacc radians */ |
444 |
|
const double minangle = 10.0 * PI/180.; |
445 |
< |
const double maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13) |
361 |
< |
+ PI/2.; |
445 |
> |
double maxangle = minangle + ambacc; |
446 |
|
double wsum = 0.0; |
447 |
|
FVECT ck0; |
448 |
|
int i, j; |
465 |
|
at->kid+i, ck0, s); |
466 |
|
} |
467 |
|
/* good enough? */ |
468 |
< |
if (wsum > 0.04 && s > (minarad*0.8+maxarad*0.2)) |
468 |
> |
if (wsum >= 0.05 && s > minarad*10.0) |
469 |
|
return(wsum); |
470 |
|
} |
471 |
+ |
/* adjust maximum angle */ |
472 |
+ |
if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) |
473 |
+ |
maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; |
474 |
|
/* sum this node */ |
475 |
|
for (av = at->alist; av != NULL; av = av->next) { |
476 |
< |
double d, delta_r2, delta_t2; |
476 |
> |
double u, v, d, delta_r2, delta_t2; |
477 |
|
COLOR ct; |
478 |
|
FVECT uvw[3]; |
479 |
|
/* record access */ |
482 |
|
/* |
483 |
|
* Ambient level test |
484 |
|
*/ |
485 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
485 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
486 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
487 |
|
break; |
400 |
– |
if (av->weight < 0.9*r->rweight) |
401 |
– |
continue; |
488 |
|
/* |
489 |
|
* Direction test using unperturbed normal |
490 |
|
*/ |
498 |
|
/* |
499 |
|
* Modified ray behind test |
500 |
|
*/ |
501 |
< |
VSUB(ck0, av->pos, r->rop); |
501 |
> |
VSUB(ck0, r->rop, av->pos); |
502 |
|
d = DOT(ck0, uvw[2]); |
503 |
< |
if (d < -minarad*qambacc-.001) |
503 |
> |
if (d < -minarad*ambacc-.001) |
504 |
|
continue; |
505 |
|
d /= av->rad[0]; |
506 |
|
delta_t2 = d*d; |
507 |
< |
if (delta_t2 >= qambacc*qambacc) |
507 |
> |
if (delta_t2 >= ambacc*ambacc) |
508 |
|
continue; |
509 |
|
/* |
510 |
|
* Elliptical radii test based on Hessian |
511 |
|
*/ |
512 |
|
decodedir(uvw[0], av->udir); |
513 |
|
VCROSS(uvw[1], uvw[2], uvw[0]); |
514 |
< |
d = DOT(ck0, uvw[0]) / av->rad[0]; |
514 |
> |
d = (u = DOT(ck0, uvw[0])) / av->rad[0]; |
515 |
|
delta_t2 += d*d; |
516 |
< |
d = DOT(ck0, uvw[1]) / av->rad[1]; |
516 |
> |
d = (v = DOT(ck0, uvw[1])) / av->rad[1]; |
517 |
|
delta_t2 += d*d; |
518 |
< |
if (delta_t2 >= qambacc*qambacc) |
518 |
> |
if (delta_t2 >= ambacc*ambacc) |
519 |
|
continue; |
520 |
|
/* |
521 |
+ |
* Test for potential light leak |
522 |
+ |
*/ |
523 |
+ |
if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u))) |
524 |
+ |
continue; |
525 |
+ |
/* |
526 |
|
* Extrapolate value and compute final weight (hat function) |
527 |
|
*/ |
528 |
< |
extambient(ct, av, r->rop, rn, uvw); |
528 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
529 |
> |
continue; |
530 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
531 |
< |
tfunc(qambacc, sqrt(delta_t2), 0.0); |
531 |
> |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
532 |
|
scalecolor(ct, d); |
533 |
|
addcolor(acol, ct); |
534 |
|
wsum += d; |
537 |
|
} |
538 |
|
|
539 |
|
|
540 |
< |
int |
540 |
> |
static int |
541 |
|
makeambient( /* make a new ambient value for storage */ |
542 |
|
COLOR acol, |
543 |
|
RAY *r, |
556 |
|
amb.weight = 1.25*r->rweight; |
557 |
|
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
558 |
|
/* compute ambient */ |
559 |
< |
i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); |
559 |
> |
i = doambient(acol, r, amb.weight, |
560 |
> |
uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral); |
561 |
|
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
562 |
|
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
563 |
|
return(i); |
577 |
|
} |
578 |
|
|
579 |
|
|
580 |
< |
void |
580 |
> |
static int |
581 |
|
extambient( /* extrapolate value at pv, nv */ |
582 |
|
COLOR cr, |
583 |
|
AMBVAL *ap, |
586 |
|
FVECT uvw[3] |
587 |
|
) |
588 |
|
{ |
589 |
+ |
const double min_d = 0.05; |
590 |
|
static FVECT my_uvw[3]; |
591 |
|
FVECT v1; |
592 |
|
int i; |
606 |
|
for (i = 3; i--; ) |
607 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
608 |
|
|
609 |
< |
if (d <= 0.0) { |
610 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
517 |
< |
return; |
518 |
< |
} |
609 |
> |
if (d < min_d) /* should not use if we can avoid it */ |
610 |
> |
d = min_d; |
611 |
|
copycolor(cr, ap->val); |
612 |
|
scalecolor(cr, d); |
613 |
+ |
return(d > min_d); |
614 |
|
} |
615 |
|
|
616 |
|
|
632 |
|
at = &atrunk; |
633 |
|
VCOPY(ck0, thescene.cuorg); |
634 |
|
s = thescene.cusize; |
635 |
< |
while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { |
635 |
> |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
636 |
|
if (at->kid == NULL) |
637 |
|
if ((at->kid = newambtree()) == NULL) |
638 |
|
error(SYSTEM, "out of memory in avinsert"); |
647 |
|
} |
648 |
|
avh.next = at->alist; /* order by increasing level */ |
649 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
650 |
< |
if (ap->next->lvl >= av->lvl) |
650 |
> |
if ( ap->next->lvl > av->lvl || |
651 |
> |
(ap->next->lvl == av->lvl) & |
652 |
> |
(ap->next->weight <= av->weight) ) |
653 |
|
break; |
654 |
|
av->next = ap->next; |
655 |
|
ap->next = (AMBVAL*)av; |
673 |
|
) |
674 |
|
{ |
675 |
|
static int rdepth = 0; /* ambient recursion */ |
676 |
< |
COLOR acol; |
676 |
> |
COLOR acol, caustic; |
677 |
|
double d, l; |
678 |
|
|
679 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
680 |
+ |
* as all ambient components accounted for */ |
681 |
+ |
if (ambPmap(aval, r, rdepth)) |
682 |
+ |
return; |
683 |
+ |
|
684 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
685 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
686 |
+ |
* continue with RADIANCE ambient calculation */ |
687 |
+ |
copycolor(caustic, aval); |
688 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
689 |
+ |
|
690 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
691 |
|
goto dumbamb; |
692 |
|
/* check number of bounces */ |
704 |
|
rdepth--; |
705 |
|
if (d <= FTINY) |
706 |
|
goto dumbamb; |
707 |
< |
copycolor(aval, acol); |
707 |
> |
copycolor(aval, acol); |
708 |
> |
|
709 |
> |
/* PMAP: add in caustic */ |
710 |
> |
addcolor(aval, caustic); |
711 |
|
return; |
712 |
|
} |
713 |
|
|
717 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
718 |
|
d = sumambient(acol, r, nrm, rdepth, |
719 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
720 |
+ |
|
721 |
|
if (d > FTINY) { |
722 |
|
d = 1.0/d; |
723 |
|
scalecolor(acol, d); |
724 |
|
multcolor(aval, acol); |
725 |
+ |
|
726 |
+ |
/* PMAP: add in caustic */ |
727 |
+ |
addcolor(aval, caustic); |
728 |
|
return; |
729 |
|
} |
730 |
+ |
|
731 |
|
rdepth++; /* need to cache new value */ |
732 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
733 |
|
rdepth--; |
734 |
+ |
|
735 |
|
if (d > FTINY) { |
736 |
|
multcolor(aval, acol); /* got new value */ |
737 |
+ |
|
738 |
+ |
/* PMAP: add in caustic */ |
739 |
+ |
addcolor(aval, caustic); |
740 |
|
return; |
741 |
|
} |
742 |
+ |
|
743 |
|
dumbamb: /* return global value */ |
744 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
745 |
|
multcolor(aval, ambval); |
746 |
+ |
|
747 |
+ |
/* PMAP: add in caustic */ |
748 |
+ |
addcolor(aval, caustic); |
749 |
|
return; |
750 |
|
} |
751 |
+ |
|
752 |
|
l = bright(ambval); /* average in computations */ |
753 |
|
if (l > FTINY) { |
754 |
|
d = (log(l)*(double)ambvwt + avsum) / |
790 |
|
/* |
791 |
|
* Ambient level test. |
792 |
|
*/ |
793 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
793 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
794 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
795 |
|
break; |
672 |
– |
if (av->weight < 0.9*r->rweight) |
673 |
– |
continue; |
796 |
|
/* |
797 |
|
* Ambient radius test. |
798 |
|
*/ |
974 |
|
} |
975 |
|
avh.next = at->alist; /* order by increasing level */ |
976 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
977 |
< |
if (ap->next->lvl >= av->lvl) |
977 |
> |
if ( ap->next->lvl > av->lvl || |
978 |
> |
(ap->next->lvl == av->lvl) & |
979 |
> |
(ap->next->weight <= av->weight) ) |
980 |
|
break; |
981 |
|
av->next = ap->next; |
982 |
|
ap->next = (AMBVAL*)av; |
1028 |
|
AMBVAL *av |
1029 |
|
) |
1030 |
|
{ |
1031 |
< |
avinsert(avstore(av)); |
1031 |
> |
avstore(av); |
1032 |
|
if (ambfp == NULL) |
1033 |
|
return; |
1034 |
|
if (writambval(av, ambfp) < 0) |
1043 |
|
|
1044 |
|
|
1045 |
|
static AMBVAL * |
1046 |
< |
avstore( /* allocate memory and store aval */ |
1046 |
> |
avstore( /* allocate memory and save aval */ |
1047 |
|
AMBVAL *aval |
1048 |
|
) |
1049 |
|
{ |
1061 |
|
avsum += log(d); |
1062 |
|
navsum++; |
1063 |
|
} |
1064 |
+ |
avinsert(av); /* insert in our cache tree */ |
1065 |
|
return(av); |
1066 |
|
} |
1067 |
|
|
1088 |
|
} |
1089 |
|
atp = atfreelist; |
1090 |
|
atfreelist = atp->kid; |
1091 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
1091 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
1092 |
|
return(atp); |
1093 |
|
} |
1094 |
|
|
1114 |
|
/* transfer values at this node */ |
1115 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
1116 |
|
at->alist = av->next; |
1117 |
+ |
av->next = NULL; |
1118 |
|
(*f)(av); |
1119 |
|
} |
1120 |
|
if (at->kid == NULL) |
1192 |
|
{ |
1193 |
|
AMBVAL **avlpp; |
1194 |
|
|
1195 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
1196 |
< |
nambvals, sizeof(AMBVAL *), &aposcmp); |
1195 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
1196 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
1197 |
|
if (avlpp == NULL) |
1198 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
1199 |
|
return(avlpp - avlist2); |
1236 |
|
} |
1237 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
1238 |
|
if (avlist2 != NULL) |
1239 |
< |
free((void *)avlist2); |
1239 |
> |
free(avlist2); |
1240 |
|
if (always) { /* rebuild without sorting */ |
1241 |
|
oldatrunk = atrunk; |
1242 |
|
atrunk.alist = NULL; |
1243 |
|
atrunk.kid = NULL; |
1244 |
< |
unloadatree(&oldatrunk, &avinsert); |
1244 |
> |
unloadatree(&oldatrunk, avinsert); |
1245 |
|
} |
1246 |
|
} else { /* sort memory by last access time */ |
1247 |
|
/* |
1258 |
|
eputs(errmsg); |
1259 |
|
#endif |
1260 |
|
i_avlist = 0; |
1261 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1261 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1262 |
|
#ifdef DEBUG |
1263 |
|
if (i_avlist < nambvals) |
1264 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1265 |
|
#endif |
1266 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); |
1267 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); |
1266 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
1267 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1268 |
|
for (i = 0; i < nambvals; i++) { |
1269 |
|
if (avlist1[i].p == NULL) |
1270 |
|
continue; |
1280 |
|
avinsert(avlist2[j]); |
1281 |
|
avlist1[j].p = NULL; |
1282 |
|
} |
1283 |
< |
free((void *)avlist1); |
1284 |
< |
free((void *)avlist2); |
1283 |
> |
free(avlist1); |
1284 |
> |
free(avlist2); |
1285 |
|
/* compute new sort interval */ |
1286 |
|
sortintvl = ambclock - lastsort; |
1287 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
1330 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
1331 |
|
goto seekerr; |
1332 |
|
if ((n = flen - lastpos) > 0) { /* file has grown */ |
1333 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1334 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
1333 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1334 |
> |
ambinp = fopen(ambfile, "rb"); |
1335 |
|
if (ambinp == NULL) |
1336 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1336 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1337 |
|
} |
1338 |
|
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1339 |
|
goto seekerr; |
1345 |
|
error(WARNING, errmsg); |
1346 |
|
break; |
1347 |
|
} |
1348 |
< |
avinsert(avstore(&avs)); |
1348 |
> |
avstore(&avs); |
1349 |
|
n -= AMBVALSIZ; |
1350 |
|
} |
1351 |
< |
lastpos = flen - n; |
1352 |
< |
/*** seek always as safety measure |
1353 |
< |
if (n) ***/ /* alignment */ |
1228 |
< |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1229 |
< |
goto seekerr; |
1351 |
> |
lastpos = flen - n; /* check alignment */ |
1352 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1353 |
> |
goto seekerr; |
1354 |
|
} |
1355 |
|
n = fflush(ambfp); /* calls write() at last */ |
1356 |
< |
if (n != EOF) |
1233 |
< |
lastpos += (long)nunflshed*AMBVALSIZ; |
1234 |
< |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
1235 |
< |
goto seekerr; |
1236 |
< |
|
1356 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1357 |
|
aflock(F_UNLCK); /* release file */ |
1358 |
|
nunflshed = 0; |
1359 |
|
return(n); |
1360 |
|
seekerr: |
1361 |
|
error(SYSTEM, "seek failed in ambsync"); |
1362 |
< |
return -1; /* pro forma return */ |
1362 |
> |
return(EOF); /* pro forma return */ |
1363 |
|
} |
1364 |
|
|
1365 |
|
#else /* ! F_SETLKW */ |