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 "random.h" |
19 |
+ |
#include "pmapamb.h" |
20 |
|
|
21 |
|
#ifndef OCTSCALE |
22 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
25 |
|
extern char *shm_boundary; /* memory sharing boundary */ |
26 |
|
|
27 |
|
#ifndef MAXASET |
28 |
< |
#define MAXASET 2047 /* maximum number of elements in ambient set */ |
28 |
> |
#define MAXASET 4095 /* maximum number of elements in ambient set */ |
29 |
|
#endif |
30 |
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
31 |
|
|
39 |
|
|
40 |
|
#ifndef SORT_THRESH |
41 |
|
#ifdef SMLMEM |
42 |
< |
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
42 |
> |
#define SORT_THRESH ((16L<<20)/sizeof(AMBVAL)) |
43 |
|
#else |
44 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
44 |
> |
#define SORT_THRESH ((64L<<20)/sizeof(AMBVAL)) |
45 |
|
#endif |
46 |
|
#endif |
47 |
|
#ifndef SORT_INTVL |
51 |
|
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
52 |
|
#endif |
53 |
|
|
54 |
+ |
|
55 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
56 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
57 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
77 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
78 |
|
|
79 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
79 |
– |
#define freeav(av) free((void *)av); |
80 |
|
|
81 |
+ |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
82 |
+ |
|
83 |
|
static void initambfile(int creat); |
84 |
|
static void avsave(AMBVAL *av); |
85 |
|
static AMBVAL *avstore(AMBVAL *aval); |
86 |
|
static AMBTREE *newambtree(void); |
87 |
|
static void freeambtree(AMBTREE *atp); |
88 |
|
|
89 |
< |
typedef void unloadtf_t(void *); |
89 |
> |
typedef void unloadtf_t(AMBVAL *); |
90 |
|
static unloadtf_t avinsert; |
91 |
|
static unloadtf_t av2list; |
92 |
+ |
static unloadtf_t avfree; |
93 |
|
static void unloadatree(AMBTREE *at, unloadtf_t *f); |
94 |
|
|
95 |
|
static int aposcmp(const void *avp1, const void *avp2); |
96 |
|
static int avlmemi(AMBVAL *avaddr); |
97 |
|
static void sortambvals(int always); |
98 |
|
|
99 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
100 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
101 |
+ |
AMBTREE *at, FVECT c0, double s); |
102 |
+ |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
103 |
+ |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
104 |
+ |
FVECT uvw[3]); |
105 |
+ |
|
106 |
|
#ifdef F_SETLKW |
107 |
|
static void aflock(int typ); |
108 |
|
#endif |
109 |
|
|
110 |
|
|
111 |
< |
extern void |
111 |
> |
void |
112 |
|
setambres( /* set ambient resolution */ |
113 |
|
int ar |
114 |
|
) |
117 |
|
/* set min & max radii */ |
118 |
|
if (ar <= 0) { |
119 |
|
minarad = 0; |
120 |
< |
maxarad = thescene.cusize / 2.0; |
120 |
> |
maxarad = thescene.cusize*0.2; |
121 |
|
} else { |
122 |
|
minarad = thescene.cusize / ar; |
123 |
< |
maxarad = 64 * minarad; /* heuristic */ |
124 |
< |
if (maxarad > thescene.cusize / 2.0) |
125 |
< |
maxarad = thescene.cusize / 2.0; |
123 |
> |
maxarad = 64.0 * minarad; /* heuristic */ |
124 |
> |
if (maxarad > thescene.cusize*0.2) |
125 |
> |
maxarad = thescene.cusize*0.2; |
126 |
|
} |
127 |
|
if (minarad <= FTINY) |
128 |
< |
minarad = 10*FTINY; |
128 |
> |
minarad = 10.0*FTINY; |
129 |
|
if (maxarad <= minarad) |
130 |
< |
maxarad = 64 * minarad; |
130 |
> |
maxarad = 64.0 * minarad; |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
< |
extern void |
134 |
> |
void |
135 |
|
setambacc( /* set ambient accuracy */ |
136 |
|
double newa |
137 |
|
) |
138 |
|
{ |
139 |
< |
double ambdiff; |
140 |
< |
|
141 |
< |
if (newa < 0.0) |
142 |
< |
newa = 0.0; |
143 |
< |
ambdiff = fabs(newa - ambacc); |
144 |
< |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
145 |
< |
sortambvals(1); /* rebuild tree */ |
139 |
> |
static double olda; /* remember previous setting here */ |
140 |
> |
|
141 |
> |
newa *= (newa > 0); |
142 |
> |
if (fabs(newa - olda) >= .05*(newa + olda)) { |
143 |
> |
ambacc = newa; |
144 |
> |
if (ambacc > FTINY && nambvals > 0) |
145 |
> |
sortambvals(1); /* rebuild tree */ |
146 |
> |
} |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
< |
extern void |
150 |
> |
void |
151 |
|
setambient(void) /* initialize calculation */ |
152 |
|
{ |
153 |
|
int readonly = 0; |
154 |
< |
long pos, flen; |
154 |
> |
long flen; |
155 |
|
AMBVAL amb; |
156 |
|
/* make sure we're fresh */ |
157 |
|
ambdone(); |
171 |
|
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
172 |
|
if (ambfp != NULL) { |
173 |
|
initambfile(0); /* file exists */ |
174 |
< |
pos = ftell(ambfp); |
174 |
> |
lastpos = ftell(ambfp); |
175 |
|
while (readambval(&amb, ambfp)) |
176 |
< |
avinsert(avstore(&amb)); |
176 |
> |
avstore(&amb); |
177 |
|
nambshare = nambvals; /* share loaded values */ |
178 |
|
if (readonly) { |
179 |
|
sprintf(errmsg, |
185 |
|
return; /* avoid ambsync() */ |
186 |
|
} |
187 |
|
/* align file pointer */ |
188 |
< |
pos += (long)nambvals*AMBVALSIZ; |
188 |
> |
lastpos += (long)nambvals*AMBVALSIZ; |
189 |
|
flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); |
190 |
< |
if (flen != pos) { |
190 |
> |
if (flen != lastpos) { |
191 |
|
sprintf(errmsg, |
192 |
|
"ignoring last %ld values in ambient file (corrupted)", |
193 |
< |
(flen - pos)/AMBVALSIZ); |
193 |
> |
(flen - lastpos)/AMBVALSIZ); |
194 |
|
error(WARNING, errmsg); |
195 |
< |
fseek(ambfp, pos, 0); |
196 |
< |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
186 |
< |
ftruncate(fileno(ambfp), (off_t)pos); |
187 |
< |
#endif |
195 |
> |
fseek(ambfp, lastpos, SEEK_SET); |
196 |
> |
ftruncate(fileno(ambfp), (off_t)lastpos); |
197 |
|
} |
198 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
199 |
|
initambfile(1); /* else create new file */ |
200 |
+ |
fflush(ambfp); |
201 |
+ |
lastpos = ftell(ambfp); |
202 |
|
} else { |
203 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
204 |
|
error(SYSTEM, errmsg); |
205 |
|
} |
206 |
< |
nunflshed++; /* lie */ |
207 |
< |
ambsync(); |
206 |
> |
#ifdef F_SETLKW |
207 |
> |
aflock(F_UNLCK); /* release file */ |
208 |
> |
#endif |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
< |
extern void |
212 |
> |
void |
213 |
|
ambdone(void) /* close ambient file and free memory */ |
214 |
|
{ |
215 |
|
if (ambfp != NULL) { /* close ambient file */ |
223 |
|
lastpos = -1; |
224 |
|
} |
225 |
|
/* free ambient tree */ |
226 |
< |
unloadatree(&atrunk, free); |
226 |
> |
unloadatree(&atrunk, avfree); |
227 |
|
/* reset state variables */ |
228 |
|
avsum = 0.; |
229 |
|
navsum = 0; |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
< |
extern void |
238 |
> |
void |
239 |
|
ambnotify( /* record new modifier */ |
240 |
|
OBJECT obj |
241 |
|
) |
242 |
|
{ |
243 |
|
static int hitlimit = 0; |
244 |
< |
register OBJREC *o; |
245 |
< |
register char **amblp; |
244 |
> |
OBJREC *o; |
245 |
> |
char **amblp; |
246 |
|
|
247 |
|
if (obj == OVOID) { /* starting over */ |
248 |
|
ambset[0] = 0; |
265 |
|
} |
266 |
|
|
267 |
|
|
268 |
< |
extern void |
268 |
> |
void |
269 |
|
multambient( /* compute ambient component & multiply by coef. */ |
270 |
|
COLOR aval, |
271 |
< |
register RAY *r, |
271 |
> |
RAY *r, |
272 |
|
FVECT nrm |
273 |
|
) |
274 |
|
{ |
275 |
|
static int rdepth = 0; /* ambient recursion */ |
276 |
< |
COLOR acol; |
276 |
> |
COLOR acol, caustic; |
277 |
> |
int i, ok; |
278 |
|
double d, l; |
279 |
|
|
280 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
281 |
+ |
* ambient. Return as all ambient components accounted for, else |
282 |
+ |
* continue. */ |
283 |
+ |
if (ambPmap(aval, r, rdepth)) |
284 |
+ |
return; |
285 |
+ |
|
286 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
287 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
288 |
+ |
* with RADIANCE ambient calculation */ |
289 |
+ |
copycolor(caustic, aval); |
290 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
291 |
+ |
|
292 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
293 |
|
goto dumbamb; |
294 |
|
/* check number of bounces */ |
300 |
|
goto dumbamb; |
301 |
|
|
302 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
303 |
+ |
FVECT uvd[2]; |
304 |
+ |
float dgrad[2], *dgp = NULL; |
305 |
+ |
|
306 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
307 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
308 |
+ |
copycolor(acol, aval); |
309 |
|
rdepth++; |
310 |
< |
d = doambient(acol, r, aval, intens(aval)*r->rweight, |
311 |
< |
NULL, NULL); |
310 |
> |
ok = doambient(acol, r, r->rweight, |
311 |
> |
uvd, NULL, NULL, dgp, NULL); |
312 |
|
rdepth--; |
313 |
< |
if (d <= FTINY) |
313 |
> |
if (!ok) |
314 |
|
goto dumbamb; |
315 |
< |
multcolor(aval, acol); |
315 |
> |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
316 |
> |
FVECT v1; |
317 |
> |
VCROSS(v1, r->ron, nrm); |
318 |
> |
d = 1.0; |
319 |
> |
for (i = 3; i--; ) |
320 |
> |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
321 |
> |
if (d >= 0.05) |
322 |
> |
scalecolor(acol, d); |
323 |
> |
} |
324 |
> |
copycolor(aval, acol); |
325 |
> |
|
326 |
> |
/* PMAP: add in caustic */ |
327 |
> |
addcolor(aval, caustic); |
328 |
|
return; |
329 |
|
} |
330 |
|
|
331 |
|
if (tracktime) /* sort to minimize thrashing */ |
332 |
|
sortambvals(0); |
333 |
< |
/* get ambient value */ |
333 |
> |
/* interpolate ambient value */ |
334 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
335 |
|
d = sumambient(acol, r, nrm, rdepth, |
336 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
337 |
+ |
|
338 |
|
if (d > FTINY) { |
339 |
< |
scalecolor(acol, 1.0/d); |
339 |
> |
d = 1.0/d; |
340 |
> |
scalecolor(acol, d); |
341 |
|
multcolor(aval, acol); |
342 |
+ |
|
343 |
+ |
/* PMAP: add in caustic */ |
344 |
+ |
addcolor(aval, caustic); |
345 |
|
return; |
346 |
|
} |
347 |
+ |
|
348 |
|
rdepth++; /* need to cache new value */ |
349 |
< |
d = makeambient(acol, r, aval, nrm, rdepth-1); |
349 |
> |
ok = makeambient(acol, r, nrm, rdepth-1); |
350 |
|
rdepth--; |
351 |
< |
if (d > FTINY) { |
352 |
< |
multcolor(aval, acol); /* got new value */ |
351 |
> |
|
352 |
> |
if (ok) { |
353 |
> |
multcolor(aval, acol); /* computed new value */ |
354 |
> |
|
355 |
> |
/* PMAP: add in caustic */ |
356 |
> |
addcolor(aval, caustic); |
357 |
|
return; |
358 |
|
} |
359 |
+ |
|
360 |
|
dumbamb: /* return global value */ |
361 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
362 |
|
multcolor(aval, ambval); |
363 |
+ |
|
364 |
+ |
/* PMAP: add in caustic */ |
365 |
+ |
addcolor(aval, caustic); |
366 |
|
return; |
367 |
|
} |
368 |
< |
l = bright(ambval); /* average in computations */ |
368 |
> |
|
369 |
> |
l = bright(ambval); /* average in computations */ |
370 |
|
if (l > FTINY) { |
371 |
|
d = (log(l)*(double)ambvwt + avsum) / |
372 |
|
(double)(ambvwt + navsum); |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
< |
extern double |
384 |
< |
sumambient( /* get interpolated ambient value */ |
383 |
> |
/* Plug a potential leak where ambient cache value is occluded */ |
384 |
> |
static int |
385 |
> |
plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang) |
386 |
> |
{ |
387 |
> |
const double cost70sq = 0.1169778; /* cos(70deg)^2 */ |
388 |
> |
RAY rtst; |
389 |
> |
FVECT vdif; |
390 |
> |
double normdot, ndotd, nadotd; |
391 |
> |
double a, b, c, t[2]; |
392 |
> |
|
393 |
> |
ang += 2.*PI*(ang < 0); /* check direction flags */ |
394 |
> |
if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) ) |
395 |
> |
return(0); |
396 |
> |
/* |
397 |
> |
* Generate test ray, targeting 20 degrees above sample point plane |
398 |
> |
* along surface normal from cache position. This should be high |
399 |
> |
* enough to miss local geometry we don't really care about. |
400 |
> |
*/ |
401 |
> |
VSUB(vdif, ap->pos, r->rop); |
402 |
> |
normdot = DOT(anorm, r->ron); |
403 |
> |
ndotd = DOT(vdif, r->ron); |
404 |
> |
nadotd = DOT(vdif, anorm); |
405 |
> |
a = normdot*normdot - cost70sq; |
406 |
> |
b = 2.0*(normdot*ndotd - nadotd*cost70sq); |
407 |
> |
c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq; |
408 |
> |
if (quadratic(t, a, b, c) != 2) |
409 |
> |
return(1); /* should rarely happen */ |
410 |
> |
if (t[1] <= FTINY) |
411 |
> |
return(0); /* should fail behind test */ |
412 |
> |
rayorigin(&rtst, SHADOW, r, NULL); |
413 |
> |
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
414 |
> |
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
415 |
> |
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
416 |
> |
OBJREC *m = findmaterial(rtst.ro); |
417 |
> |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
418 |
> |
(rtst.clipset == NULL || |
419 |
> |
!inset(rtst.clipset, rtst.ro->omod))) |
420 |
> |
return(1); /* plug light leak */ |
421 |
> |
VCOPY(rtst.rorg, rtst.rop); /* skip invisible surface */ |
422 |
> |
rtst.rmax -= rtst.rot; |
423 |
> |
rayclear(&rtst); |
424 |
> |
} |
425 |
> |
return(0); /* seems we're OK */ |
426 |
> |
} |
427 |
> |
|
428 |
> |
|
429 |
> |
static double |
430 |
> |
sumambient( /* get interpolated ambient value */ |
431 |
|
COLOR acol, |
432 |
< |
register RAY *r, |
432 |
> |
RAY *r, |
433 |
|
FVECT rn, |
434 |
|
int al, |
435 |
|
AMBTREE *at, |
436 |
|
FVECT c0, |
437 |
|
double s |
438 |
|
) |
439 |
< |
{ |
440 |
< |
double d, e1, e2, wt, wsum; |
441 |
< |
COLOR ct; |
442 |
< |
FVECT ck0; |
443 |
< |
int i; |
444 |
< |
register int j; |
445 |
< |
register AMBVAL *av; |
439 |
> |
{ /* initial limit is 10 degrees plus ambacc radians */ |
440 |
> |
const double minangle = 10.0 * PI/180.; |
441 |
> |
double maxangle = minangle + ambacc; |
442 |
> |
double wsum = 0.0; |
443 |
> |
FVECT ck0; |
444 |
> |
int i, j; |
445 |
> |
AMBVAL *av; |
446 |
|
|
447 |
< |
wsum = 0.0; |
448 |
< |
/* do this node */ |
447 |
> |
if (at->kid != NULL) { /* sum children first */ |
448 |
> |
s *= 0.5; |
449 |
> |
for (i = 0; i < 8; i++) { |
450 |
> |
for (j = 0; j < 3; j++) { |
451 |
> |
ck0[j] = c0[j]; |
452 |
> |
if (1<<j & i) |
453 |
> |
ck0[j] += s; |
454 |
> |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
455 |
> |
break; |
456 |
> |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
457 |
> |
break; |
458 |
> |
} |
459 |
> |
if (j == 3) |
460 |
> |
wsum += sumambient(acol, r, rn, al, |
461 |
> |
at->kid+i, ck0, s); |
462 |
> |
} |
463 |
> |
/* good enough? */ |
464 |
> |
if (wsum >= 0.05 && s > minarad*10.0) |
465 |
> |
return(wsum); |
466 |
> |
} |
467 |
> |
/* adjust maximum angle */ |
468 |
> |
if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) |
469 |
> |
maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; |
470 |
> |
/* sum this node */ |
471 |
|
for (av = at->alist; av != NULL; av = av->next) { |
472 |
< |
double rn_dot = -2.0; |
472 |
> |
double u, v, d, delta_r2, delta_t2; |
473 |
> |
COLOR ct; |
474 |
> |
FVECT uvw[3]; |
475 |
> |
/* record access */ |
476 |
|
if (tracktime) |
477 |
|
av->latick = ambclock; |
478 |
|
/* |
479 |
< |
* Ambient level test. |
479 |
> |
* Ambient level test |
480 |
|
*/ |
481 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
481 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
482 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
483 |
|
break; |
354 |
– |
if (av->weight < r->rweight-FTINY) |
355 |
– |
continue; |
484 |
|
/* |
485 |
< |
* Ambient radius test. |
485 |
> |
* Direction test using unperturbed normal |
486 |
|
*/ |
487 |
< |
d = av->pos[0] - r->rop[0]; |
488 |
< |
e1 = d * d; |
489 |
< |
d = av->pos[1] - r->rop[1]; |
362 |
< |
e1 += d * d; |
363 |
< |
d = av->pos[2] - r->rop[2]; |
364 |
< |
e1 += d * d; |
365 |
< |
e1 /= av->rad * av->rad; |
366 |
< |
if (e1 > ambacc*ambacc*1.21) |
487 |
> |
decodedir(uvw[2], av->ndir); |
488 |
> |
d = DOT(uvw[2], r->ron); |
489 |
> |
if (d <= 0.0) /* >= 90 degrees */ |
490 |
|
continue; |
491 |
+ |
delta_r2 = 2.0 - 2.0*d; /* approx. radians^2 */ |
492 |
+ |
if (delta_r2 >= maxangle*maxangle) |
493 |
+ |
continue; |
494 |
|
/* |
495 |
< |
* Direction test using closest normal. |
495 |
> |
* Modified ray behind test |
496 |
|
*/ |
497 |
< |
d = DOT(av->dir, r->ron); |
498 |
< |
if (rn != r->ron) { |
499 |
< |
rn_dot = DOT(av->dir, rn); |
374 |
< |
if (rn_dot > 1.0-FTINY) |
375 |
< |
rn_dot = 1.0-FTINY; |
376 |
< |
if (rn_dot >= d-FTINY) { |
377 |
< |
d = rn_dot; |
378 |
< |
rn_dot = -2.0; |
379 |
< |
} |
380 |
< |
} |
381 |
< |
e2 = (1.0 - d) * r->rweight; |
382 |
< |
if (e2 < 0.0) e2 = 0.0; |
383 |
< |
if (e1 + e2 > ambacc*ambacc*1.21) |
497 |
> |
VSUB(ck0, r->rop, av->pos); |
498 |
> |
d = DOT(ck0, uvw[2]); |
499 |
> |
if (d < -minarad*ambacc-.001) |
500 |
|
continue; |
501 |
+ |
d /= av->rad[0]; |
502 |
+ |
delta_t2 = d*d; |
503 |
+ |
if (delta_t2 >= ambacc*ambacc) |
504 |
+ |
continue; |
505 |
|
/* |
506 |
< |
* Ray behind test. |
506 |
> |
* Elliptical radii test based on Hessian |
507 |
|
*/ |
508 |
< |
d = 0.0; |
509 |
< |
for (j = 0; j < 3; j++) |
510 |
< |
d += (r->rop[j] - av->pos[j]) * |
511 |
< |
(av->dir[j] + r->ron[j]); |
512 |
< |
if (d*0.5 < -minarad*ambacc-.001) |
508 |
> |
decodedir(uvw[0], av->udir); |
509 |
> |
VCROSS(uvw[1], uvw[2], uvw[0]); |
510 |
> |
d = (u = DOT(ck0, uvw[0])) / av->rad[0]; |
511 |
> |
delta_t2 += d*d; |
512 |
> |
d = (v = DOT(ck0, uvw[1])) / av->rad[1]; |
513 |
> |
delta_t2 += d*d; |
514 |
> |
if (delta_t2 >= ambacc*ambacc) |
515 |
|
continue; |
516 |
|
/* |
517 |
< |
* Jittering final test reduces image artifacts. |
517 |
> |
* Test for potential light leak |
518 |
|
*/ |
519 |
< |
e1 = sqrt(e1); |
398 |
< |
e2 = sqrt(e2); |
399 |
< |
wt = e1 + e2; |
400 |
< |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
519 |
> |
if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u))) |
520 |
|
continue; |
521 |
|
/* |
522 |
< |
* Recompute directional error using perturbed normal |
522 |
> |
* Extrapolate value and compute final weight (hat function) |
523 |
|
*/ |
524 |
< |
if (rn_dot > 0.0) { |
525 |
< |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
526 |
< |
wt = e1 + e2; |
527 |
< |
} |
528 |
< |
if (wt <= 1e-3) |
410 |
< |
wt = 1e3; |
411 |
< |
else |
412 |
< |
wt = 1.0 / wt; |
413 |
< |
wsum += wt; |
414 |
< |
extambient(ct, av, r->rop, rn); |
415 |
< |
scalecolor(ct, wt); |
524 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
525 |
> |
continue; |
526 |
> |
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
527 |
> |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
528 |
> |
scalecolor(ct, d); |
529 |
|
addcolor(acol, ct); |
530 |
+ |
wsum += d; |
531 |
|
} |
418 |
– |
if (at->kid == NULL) |
419 |
– |
return(wsum); |
420 |
– |
/* do children */ |
421 |
– |
s *= 0.5; |
422 |
– |
for (i = 0; i < 8; i++) { |
423 |
– |
for (j = 0; j < 3; j++) { |
424 |
– |
ck0[j] = c0[j]; |
425 |
– |
if (1<<j & i) |
426 |
– |
ck0[j] += s; |
427 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
428 |
– |
break; |
429 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
430 |
– |
break; |
431 |
– |
} |
432 |
– |
if (j == 3) |
433 |
– |
wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s); |
434 |
– |
} |
532 |
|
return(wsum); |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
< |
extern double |
537 |
< |
makeambient( /* make a new ambient value */ |
536 |
> |
static int |
537 |
> |
makeambient( /* make a new ambient value for storage */ |
538 |
|
COLOR acol, |
539 |
|
RAY *r, |
443 |
– |
COLOR ac, |
540 |
|
FVECT rn, |
541 |
|
int al |
542 |
|
) |
543 |
|
{ |
544 |
|
AMBVAL amb; |
545 |
< |
double coef; |
546 |
< |
FVECT gp, gd; |
547 |
< |
/* compute weight */ |
548 |
< |
amb.weight = pow(AVGREFL, (double)al); |
549 |
< |
coef = intens(ac)*r->rweight; |
550 |
< |
if (coef < 0.1*amb.weight) /* heuristic */ |
551 |
< |
amb.weight = coef; |
545 |
> |
FVECT uvw[3]; |
546 |
> |
int i; |
547 |
> |
|
548 |
> |
amb.weight = 1.0; /* compute weight */ |
549 |
> |
for (i = al; i-- > 0; ) |
550 |
> |
amb.weight *= AVGREFL; |
551 |
> |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
552 |
> |
amb.weight = 1.25*r->rweight; |
553 |
> |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
554 |
|
/* compute ambient */ |
555 |
< |
amb.rad = doambient(acol, r, ac, amb.weight, gp, gd); |
556 |
< |
if (amb.rad <= FTINY) |
557 |
< |
return(0.0); |
558 |
< |
/* store it */ |
555 |
> |
i = doambient(acol, r, amb.weight, |
556 |
> |
uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral); |
557 |
> |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
558 |
> |
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
559 |
> |
return(i); |
560 |
> |
/* store value */ |
561 |
|
VCOPY(amb.pos, r->rop); |
562 |
< |
VCOPY(amb.dir, r->ron); |
562 |
> |
amb.ndir = encodedir(r->ron); |
563 |
> |
amb.udir = encodedir(uvw[0]); |
564 |
|
amb.lvl = al; |
565 |
|
copycolor(amb.val, acol); |
465 |
– |
VCOPY(amb.gpos, gp); |
466 |
– |
VCOPY(amb.gdir, gd); |
566 |
|
/* insert into tree */ |
567 |
|
avsave(&amb); /* and save to file */ |
568 |
< |
if (rn != r->ron) |
569 |
< |
extambient(acol, &amb, r->rop, rn); /* texture */ |
570 |
< |
return(amb.rad); |
568 |
> |
if (rn != r->ron) { /* texture */ |
569 |
> |
VCOPY(uvw[2], r->ron); |
570 |
> |
extambient(acol, &amb, r->rop, rn, uvw); |
571 |
> |
} |
572 |
> |
return(1); |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
< |
extern void |
576 |
> |
static int |
577 |
|
extambient( /* extrapolate value at pv, nv */ |
578 |
|
COLOR cr, |
579 |
< |
register AMBVAL *ap, |
579 |
> |
AMBVAL *ap, |
580 |
|
FVECT pv, |
581 |
< |
FVECT nv |
581 |
> |
FVECT nv, |
582 |
> |
FVECT uvw[3] |
583 |
|
) |
584 |
|
{ |
585 |
< |
FVECT v1; |
586 |
< |
register int i; |
587 |
< |
double d; |
585 |
> |
const double min_d = 0.05; |
586 |
> |
static FVECT my_uvw[3]; |
587 |
> |
FVECT v1; |
588 |
> |
int i; |
589 |
> |
double d = 1.0; /* zeroeth order */ |
590 |
|
|
591 |
< |
d = 1.0; /* zeroeth order */ |
592 |
< |
/* gradient due to translation */ |
593 |
< |
for (i = 0; i < 3; i++) |
594 |
< |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
595 |
< |
/* gradient due to rotation */ |
492 |
< |
VCROSS(v1, ap->dir, nv); |
493 |
< |
d += DOT(ap->gdir, v1); |
494 |
< |
if (d <= 0.0) { |
495 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
496 |
< |
return; |
591 |
> |
if (uvw == NULL) { /* need local coordinates? */ |
592 |
> |
decodedir(my_uvw[2], ap->ndir); |
593 |
> |
decodedir(my_uvw[0], ap->udir); |
594 |
> |
VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); |
595 |
> |
uvw = my_uvw; |
596 |
|
} |
597 |
+ |
for (i = 3; i--; ) /* gradient due to translation */ |
598 |
+ |
d += (pv[i] - ap->pos[i]) * |
599 |
+ |
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
600 |
+ |
|
601 |
+ |
VCROSS(v1, uvw[2], nv); /* gradient due to rotation */ |
602 |
+ |
for (i = 3; i--; ) |
603 |
+ |
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
604 |
+ |
|
605 |
+ |
if (d < min_d) /* should not use if we can avoid it */ |
606 |
+ |
d = min_d; |
607 |
|
copycolor(cr, ap->val); |
608 |
|
scalecolor(cr, d); |
609 |
+ |
return(d > min_d); |
610 |
|
} |
611 |
|
|
612 |
|
|
613 |
|
static void |
614 |
+ |
avinsert( /* insert ambient value in our tree */ |
615 |
+ |
AMBVAL *av |
616 |
+ |
) |
617 |
+ |
{ |
618 |
+ |
AMBTREE *at; |
619 |
+ |
AMBVAL *ap; |
620 |
+ |
AMBVAL avh; |
621 |
+ |
FVECT ck0; |
622 |
+ |
double s; |
623 |
+ |
int branch; |
624 |
+ |
int i; |
625 |
+ |
|
626 |
+ |
if (av->rad[0] <= FTINY) |
627 |
+ |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
628 |
+ |
at = &atrunk; |
629 |
+ |
VCOPY(ck0, thescene.cuorg); |
630 |
+ |
s = thescene.cusize; |
631 |
+ |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
632 |
+ |
if (at->kid == NULL) |
633 |
+ |
if ((at->kid = newambtree()) == NULL) |
634 |
+ |
error(SYSTEM, "out of memory in avinsert"); |
635 |
+ |
s *= 0.5; |
636 |
+ |
branch = 0; |
637 |
+ |
for (i = 0; i < 3; i++) |
638 |
+ |
if (av->pos[i] > ck0[i] + s) { |
639 |
+ |
ck0[i] += s; |
640 |
+ |
branch |= 1 << i; |
641 |
+ |
} |
642 |
+ |
at = at->kid + branch; |
643 |
+ |
} |
644 |
+ |
avh.next = at->alist; /* order by increasing level */ |
645 |
+ |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
646 |
+ |
if ( ap->next->lvl > av->lvl || |
647 |
+ |
(ap->next->lvl == av->lvl) & |
648 |
+ |
(ap->next->weight <= av->weight) ) |
649 |
+ |
break; |
650 |
+ |
av->next = ap->next; |
651 |
+ |
ap->next = (AMBVAL*)av; |
652 |
+ |
at->alist = avh.next; |
653 |
+ |
} |
654 |
+ |
|
655 |
+ |
|
656 |
+ |
static void |
657 |
|
initambfile( /* initialize ambient file */ |
658 |
< |
int creat |
658 |
> |
int cre8 |
659 |
|
) |
660 |
|
{ |
661 |
|
extern char *progname, *octname; |
662 |
|
static char *mybuf = NULL; |
663 |
|
|
664 |
|
#ifdef F_SETLKW |
665 |
< |
aflock(creat ? F_WRLCK : F_RDLCK); |
665 |
> |
aflock(cre8 ? F_WRLCK : F_RDLCK); |
666 |
|
#endif |
667 |
|
SET_FILE_BINARY(ambfp); |
668 |
|
if (mybuf == NULL) |
669 |
|
mybuf = (char *)bmalloc(BUFSIZ+8); |
670 |
|
setbuf(ambfp, mybuf); |
671 |
< |
if (creat) { /* new file */ |
671 |
> |
if (cre8) { /* new file */ |
672 |
|
newheader("RADIANCE", ambfp); |
673 |
|
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
674 |
|
progname, colval(ambval,RED), |
677 |
|
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
678 |
|
ambdiv, ambssamp, ambres); |
679 |
|
if (octname != NULL) |
680 |
< |
printargs(1, &octname, ambfp); |
681 |
< |
else |
529 |
< |
fputc('\n', ambfp); |
680 |
> |
fputs(octname, ambfp); |
681 |
> |
fputc('\n', ambfp); |
682 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
683 |
|
fputnow(ambfp); |
684 |
|
fputformat(AMBFMT, ambfp); |
685 |
< |
putc('\n', ambfp); |
685 |
> |
fputc('\n', ambfp); |
686 |
|
putambmagic(ambfp); |
687 |
|
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
688 |
|
error(USER, "bad ambient file"); |
694 |
|
AMBVAL *av |
695 |
|
) |
696 |
|
{ |
697 |
< |
avinsert(avstore(av)); |
697 |
> |
avstore(av); |
698 |
|
if (ambfp == NULL) |
699 |
|
return; |
700 |
|
if (writambval(av, ambfp) < 0) |
709 |
|
|
710 |
|
|
711 |
|
static AMBVAL * |
712 |
< |
avstore( /* allocate memory and store aval */ |
713 |
< |
register AMBVAL *aval |
712 |
> |
avstore( /* allocate memory and save aval */ |
713 |
> |
AMBVAL *aval |
714 |
|
) |
715 |
|
{ |
716 |
< |
register AMBVAL *av; |
716 |
> |
AMBVAL *av; |
717 |
|
double d; |
718 |
|
|
719 |
|
if ((av = newambval()) == NULL) |
727 |
|
avsum += log(d); |
728 |
|
navsum++; |
729 |
|
} |
730 |
+ |
avinsert(av); /* insert in our cache tree */ |
731 |
|
return(av); |
732 |
|
} |
733 |
|
|
740 |
|
static AMBTREE * |
741 |
|
newambtree(void) /* allocate 8 ambient tree structs */ |
742 |
|
{ |
743 |
< |
register AMBTREE *atp, *upperlim; |
743 |
> |
AMBTREE *atp, *upperlim; |
744 |
|
|
745 |
|
if (atfreelist == NULL) { /* get more nodes */ |
746 |
|
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
754 |
|
} |
755 |
|
atp = atfreelist; |
756 |
|
atfreelist = atp->kid; |
757 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
757 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
758 |
|
return(atp); |
759 |
|
} |
760 |
|
|
770 |
|
|
771 |
|
|
772 |
|
static void |
620 |
– |
avinsert( /* insert ambient value in our tree */ |
621 |
– |
void *av |
622 |
– |
) |
623 |
– |
{ |
624 |
– |
register AMBTREE *at; |
625 |
– |
register AMBVAL *ap; |
626 |
– |
AMBVAL avh; |
627 |
– |
FVECT ck0; |
628 |
– |
double s; |
629 |
– |
int branch; |
630 |
– |
register int i; |
631 |
– |
|
632 |
– |
if (((AMBVAL*)av)->rad <= FTINY) |
633 |
– |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
634 |
– |
at = &atrunk; |
635 |
– |
VCOPY(ck0, thescene.cuorg); |
636 |
– |
s = thescene.cusize; |
637 |
– |
while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { |
638 |
– |
if (at->kid == NULL) |
639 |
– |
if ((at->kid = newambtree()) == NULL) |
640 |
– |
error(SYSTEM, "out of memory in avinsert"); |
641 |
– |
s *= 0.5; |
642 |
– |
branch = 0; |
643 |
– |
for (i = 0; i < 3; i++) |
644 |
– |
if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { |
645 |
– |
ck0[i] += s; |
646 |
– |
branch |= 1 << i; |
647 |
– |
} |
648 |
– |
at = at->kid + branch; |
649 |
– |
} |
650 |
– |
avh.next = at->alist; /* order by increasing level */ |
651 |
– |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
652 |
– |
if (ap->next->lvl >= ((AMBVAL*)av)->lvl) |
653 |
– |
break; |
654 |
– |
((AMBVAL*)av)->next = ap->next; |
655 |
– |
ap->next = (AMBVAL*)av; |
656 |
– |
at->alist = avh.next; |
657 |
– |
} |
658 |
– |
|
659 |
– |
|
660 |
– |
static void |
773 |
|
unloadatree( /* unload an ambient value tree */ |
774 |
< |
register AMBTREE *at, |
774 |
> |
AMBTREE *at, |
775 |
|
unloadtf_t *f |
776 |
|
) |
777 |
|
{ |
778 |
< |
register AMBVAL *av; |
779 |
< |
register int i; |
778 |
> |
AMBVAL *av; |
779 |
> |
int i; |
780 |
|
/* transfer values at this node */ |
781 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
782 |
|
at->alist = av->next; |
783 |
+ |
av->next = NULL; |
784 |
|
(*f)(av); |
785 |
|
} |
786 |
|
if (at->kid == NULL) |
802 |
|
static int alatcmp(const void *av1, const void *av2); |
803 |
|
|
804 |
|
static void |
805 |
+ |
avfree(AMBVAL *av) |
806 |
+ |
{ |
807 |
+ |
free(av); |
808 |
+ |
} |
809 |
+ |
|
810 |
+ |
static void |
811 |
|
av2list( |
812 |
< |
void *av |
812 |
> |
AMBVAL *av |
813 |
|
) |
814 |
|
{ |
815 |
|
#ifdef DEBUG |
817 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
818 |
|
#endif |
819 |
|
avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; |
820 |
< |
avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; |
820 |
> |
avlist1[i_avlist++].t = av->latick; |
821 |
|
} |
822 |
|
|
823 |
|
|
827 |
|
const void *av2 |
828 |
|
) |
829 |
|
{ |
830 |
< |
register long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
830 |
> |
long lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; |
831 |
|
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
832 |
|
} |
833 |
|
|
844 |
|
const void *avp2 |
845 |
|
) |
846 |
|
{ |
847 |
< |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
847 |
> |
long diff = *(char * const *)avp1 - *(char * const *)avp2; |
848 |
|
if (diff < 0) |
849 |
|
return(-1); |
850 |
|
return(diff > 0); |
851 |
|
} |
852 |
|
|
853 |
< |
#if 1 |
853 |
> |
|
854 |
|
static int |
855 |
|
avlmemi( /* find list position from address */ |
856 |
|
AMBVAL *avaddr |
857 |
|
) |
858 |
|
{ |
859 |
< |
register AMBVAL **avlpp; |
859 |
> |
AMBVAL **avlpp; |
860 |
|
|
861 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
861 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
862 |
|
nambvals, sizeof(AMBVAL *), aposcmp); |
863 |
|
if (avlpp == NULL) |
864 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
865 |
|
return(avlpp - avlist2); |
866 |
|
} |
748 |
– |
#else |
749 |
– |
#define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ |
750 |
– |
nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) |
751 |
– |
#endif |
867 |
|
|
868 |
|
|
869 |
|
static void |
873 |
|
{ |
874 |
|
AMBTREE oldatrunk; |
875 |
|
AMBVAL tav, *tap, *pnext; |
876 |
< |
register int i, j; |
876 |
> |
int i, j; |
877 |
|
/* see if it's time yet */ |
878 |
|
if (!always && (ambclock++ < lastsort+sortintvl || |
879 |
|
nambvals < SORT_THRESH)) |
902 |
|
} |
903 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
904 |
|
if (avlist2 != NULL) |
905 |
< |
free((void *)avlist2); |
905 |
> |
free(avlist2); |
906 |
|
if (always) { /* rebuild without sorting */ |
907 |
|
oldatrunk = atrunk; |
908 |
|
atrunk.alist = NULL; |
929 |
|
if (i_avlist < nambvals) |
930 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
931 |
|
#endif |
932 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
933 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
932 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
933 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
934 |
|
for (i = 0; i < nambvals; i++) { |
935 |
|
if (avlist1[i].p == NULL) |
936 |
|
continue; |
946 |
|
avinsert(avlist2[j]); |
947 |
|
avlist1[j].p = NULL; |
948 |
|
} |
949 |
< |
free((void *)avlist1); |
950 |
< |
free((void *)avlist2); |
949 |
> |
free(avlist1); |
950 |
> |
free(avlist2); |
951 |
|
/* compute new sort interval */ |
952 |
|
sortintvl = ambclock - lastsort; |
953 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
973 |
|
{ |
974 |
|
static struct flock fls; /* static so initialized to zeroes */ |
975 |
|
|
976 |
+ |
if (typ == fls.l_type) /* already called? */ |
977 |
+ |
return; |
978 |
|
fls.l_type = typ; |
979 |
|
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
980 |
|
error(SYSTEM, "cannot (un)lock ambient file"); |
981 |
|
} |
982 |
|
|
983 |
|
|
984 |
< |
extern int |
984 |
> |
int |
985 |
|
ambsync(void) /* synchronize ambient file */ |
986 |
|
{ |
987 |
|
long flen; |
988 |
|
AMBVAL avs; |
989 |
< |
register int n; |
989 |
> |
int n; |
990 |
|
|
991 |
< |
if (nunflshed == 0) |
991 |
> |
if (ambfp == NULL) /* no ambient file? */ |
992 |
|
return(0); |
993 |
< |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
994 |
< |
goto syncend; |
878 |
< |
/* gain exclusive access */ |
879 |
< |
aflock(F_WRLCK); |
993 |
> |
/* gain appropriate access */ |
994 |
> |
aflock(nunflshed ? F_WRLCK : F_RDLCK); |
995 |
|
/* see if file has grown */ |
996 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
997 |
|
goto seekerr; |
998 |
< |
if ( (n = flen - lastpos) ) { /* file has grown */ |
999 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1000 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
998 |
> |
if ((n = flen - lastpos) > 0) { /* file has grown */ |
999 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1000 |
> |
ambinp = fopen(ambfile, "rb"); |
1001 |
|
if (ambinp == NULL) |
1002 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1002 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1003 |
|
} |
1004 |
< |
if (fseek(ambinp, lastpos, 0) < 0) |
1004 |
> |
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1005 |
|
goto seekerr; |
1006 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
1007 |
|
if (!readambval(&avs, ambinp)) { |
1011 |
|
error(WARNING, errmsg); |
1012 |
|
break; |
1013 |
|
} |
1014 |
< |
avinsert(avstore(&avs)); |
1014 |
> |
avstore(&avs); |
1015 |
|
n -= AMBVALSIZ; |
1016 |
|
} |
1017 |
< |
/*** seek always as safety measure |
1018 |
< |
if (n) ***/ /* alignment */ |
1019 |
< |
if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0) |
905 |
< |
goto seekerr; |
1017 |
> |
lastpos = flen - n; /* check alignment */ |
1018 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1019 |
> |
goto seekerr; |
1020 |
|
} |
907 |
– |
#ifdef DEBUG |
908 |
– |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
909 |
– |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
910 |
– |
ambfp->_ptr - ambfp->_base, |
911 |
– |
nunflshed*AMBVALSIZ); |
912 |
– |
error(CONSISTENCY, errmsg); |
913 |
– |
} |
914 |
– |
#endif |
915 |
– |
syncend: |
1021 |
|
n = fflush(ambfp); /* calls write() at last */ |
1022 |
< |
if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
918 |
< |
goto seekerr; |
1022 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1023 |
|
aflock(F_UNLCK); /* release file */ |
1024 |
|
nunflshed = 0; |
1025 |
|
return(n); |
1026 |
|
seekerr: |
1027 |
|
error(SYSTEM, "seek failed in ambsync"); |
1028 |
< |
return -1; /* pro forma return */ |
1028 |
> |
return(EOF); /* pro forma return */ |
1029 |
|
} |
1030 |
|
|
1031 |
< |
#else |
1031 |
> |
#else /* ! F_SETLKW */ |
1032 |
|
|
1033 |
< |
extern int |
1033 |
> |
int |
1034 |
|
ambsync(void) /* flush ambient file */ |
1035 |
|
{ |
1036 |
< |
if (nunflshed == 0) |
1036 |
> |
if (ambfp == NULL) |
1037 |
|
return(0); |
1038 |
|
nunflshed = 0; |
1039 |
|
return(fflush(ambfp)); |
1040 |
|
} |
1041 |
|
|
1042 |
< |
#endif |
1042 |
> |
#endif /* ! F_SETLKW */ |