43 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
44 |
|
|
45 |
|
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
46 |
+ |
#define freeambtree(t) free((char *)(t)) |
47 |
|
|
48 |
|
extern long ftell(), lseek(); |
49 |
< |
static int initambfile(), avsave(), avinsert(); |
49 |
> |
static int initambfile(), avsave(), avinsert(), loadatree(); |
50 |
> |
static AMBVAL *avstore(); |
51 |
|
#ifdef F_SETLKW |
52 |
|
static aflock(); |
53 |
|
#endif |
56 |
|
setambres(ar) /* set ambient resolution */ |
57 |
|
int ar; |
58 |
|
{ |
59 |
+ |
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
60 |
|
/* set min & max radii */ |
61 |
|
if (ar <= 0) { |
62 |
|
minarad = 0.0; |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
+ |
setambacc(newa) /* set ambient accuracy */ |
76 |
+ |
double newa; |
77 |
+ |
{ |
78 |
+ |
static double oldambacc = -1.0; |
79 |
+ |
AMBTREE oldatrunk; |
80 |
+ |
|
81 |
+ |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
82 |
+ |
if (oldambacc < -FTINY) |
83 |
+ |
oldambacc = ambacc; /* do nothing first call */ |
84 |
+ |
if (fabs(newa - oldambacc) < 0.01) |
85 |
+ |
return; /* insignificant -- don't bother */ |
86 |
+ |
if (ambacc <= FTINY) |
87 |
+ |
return; /* cannot build new tree */ |
88 |
+ |
/* else need to rebuild tree */ |
89 |
+ |
copystruct(&oldatrunk, &atrunk); |
90 |
+ |
atrunk.alist = NULL; |
91 |
+ |
atrunk.kid = NULL; |
92 |
+ |
loadatree(&oldatrunk); |
93 |
+ |
oldambacc = ambacc; /* remeber setting for next call */ |
94 |
+ |
} |
95 |
+ |
|
96 |
+ |
|
97 |
|
setambient(afile) /* initialize calculation */ |
98 |
|
char *afile; |
99 |
|
{ |
101 |
|
AMBVAL amb; |
102 |
|
/* init ambient limits */ |
103 |
|
setambres(ambres); |
104 |
+ |
setambacc(ambacc); |
105 |
|
if (afile == NULL) |
106 |
|
return; |
107 |
+ |
if (ambacc <= FTINY) { |
108 |
+ |
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
109 |
+ |
afile); |
110 |
+ |
error(WARNING, errmsg); |
111 |
+ |
return; |
112 |
+ |
} |
113 |
|
/* open ambient file */ |
114 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
115 |
|
initambfile(0); |
116 |
|
headlen = ftell(ambfp); |
117 |
|
while (readambval(&amb, ambfp)) |
118 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, |
87 |
< |
thescene.cusize); |
118 |
> |
avinsert(avstore(&amb)); |
119 |
|
/* align */ |
120 |
|
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
121 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
214 |
|
/* |
215 |
|
* Ambient level test. |
216 |
|
*/ |
217 |
< |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
217 |
> |
if (av->lvl > al) /* list sorted, so this works */ |
218 |
> |
break; |
219 |
> |
if (av->weight < r->rweight-FTINY) |
220 |
|
continue; |
221 |
|
/* |
222 |
|
* Ambient radius test. |
349 |
|
#ifdef MSDOS |
350 |
|
setmode(fileno(ambfp), O_BINARY); |
351 |
|
#endif |
352 |
< |
setbuf(ambfp, bmalloc(BUFSIZ)); |
352 |
> |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
353 |
|
if (creat) { /* new file */ |
354 |
|
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
355 |
|
progname, colval(ambval,RED), |
371 |
|
avsave(av) /* insert and save an ambient value */ |
372 |
|
AMBVAL *av; |
373 |
|
{ |
374 |
< |
avinsert(av, &atrunk, thescene.cuorg, thescene.cusize); |
374 |
> |
avinsert(avstore(av)); |
375 |
|
if (ambfp == NULL) |
376 |
|
return; |
377 |
|
if (writambval(av, ambfp) < 0) |
385 |
|
} |
386 |
|
|
387 |
|
|
388 |
+ |
static AMBVAL * |
389 |
+ |
avstore(aval) /* allocate memory and store aval */ |
390 |
+ |
register AMBVAL *aval; |
391 |
+ |
{ |
392 |
+ |
register AMBVAL *av; |
393 |
+ |
|
394 |
+ |
if ((av = newambval()) == NULL) |
395 |
+ |
error(SYSTEM, "out of memory in avstore"); |
396 |
+ |
copystruct(av, aval); |
397 |
+ |
return(av); |
398 |
+ |
} |
399 |
+ |
|
400 |
+ |
|
401 |
|
static |
402 |
< |
avinsert(aval, at, c0, s) /* insert ambient value in a tree */ |
403 |
< |
AMBVAL *aval; |
358 |
< |
register AMBTREE *at; |
359 |
< |
FVECT c0; |
360 |
< |
double s; |
402 |
> |
avinsert(av) /* insert ambient value in our tree */ |
403 |
> |
register AMBVAL *av; |
404 |
|
{ |
405 |
+ |
register AMBTREE *at; |
406 |
+ |
register AMBVAL *ap; |
407 |
+ |
AMBVAL avh; |
408 |
|
FVECT ck0; |
409 |
+ |
double s; |
410 |
|
int branch; |
364 |
– |
register AMBVAL *av; |
411 |
|
register int i; |
412 |
|
|
413 |
< |
if ((av = newambval()) == NULL) |
414 |
< |
goto memerr; |
415 |
< |
copystruct(av, aval); |
416 |
< |
VCOPY(ck0, c0); |
413 |
> |
if (av->rad <= FTINY) |
414 |
> |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
415 |
> |
at = &atrunk; |
416 |
> |
VCOPY(ck0, thescene.cuorg); |
417 |
> |
s = thescene.cusize; |
418 |
|
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
419 |
|
if (at->kid == NULL) |
420 |
|
if ((at->kid = newambtree()) == NULL) |
421 |
< |
goto memerr; |
421 |
> |
error(SYSTEM, "out of memory in avinsert"); |
422 |
|
s *= 0.5; |
423 |
|
branch = 0; |
424 |
|
for (i = 0; i < 3; i++) |
428 |
|
} |
429 |
|
at = at->kid + branch; |
430 |
|
} |
431 |
< |
av->next = at->alist; |
432 |
< |
at->alist = av; |
433 |
< |
return; |
434 |
< |
memerr: |
435 |
< |
error(SYSTEM, "out of memory in avinsert"); |
431 |
> |
avh.next = at->alist; /* order by increasing level */ |
432 |
> |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
433 |
> |
if (ap->next->lvl >= av->lvl) |
434 |
> |
break; |
435 |
> |
av->next = ap->next; |
436 |
> |
ap->next = av; |
437 |
> |
at->alist = avh.next; |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
+ |
static |
442 |
+ |
loadatree(at) /* move tree to main store */ |
443 |
+ |
register AMBTREE *at; |
444 |
+ |
{ |
445 |
+ |
register AMBVAL *av; |
446 |
+ |
register int i; |
447 |
+ |
/* transfer values at this node */ |
448 |
+ |
for (av = at->alist; av != NULL; av = at->alist) { |
449 |
+ |
at->alist = av->next; |
450 |
+ |
avinsert(av); |
451 |
+ |
} |
452 |
+ |
if (at->kid == NULL) |
453 |
+ |
return; |
454 |
+ |
for (i = 0; i < 8; i++) /* transfer and free children */ |
455 |
+ |
loadatree(at->kid+i); |
456 |
+ |
freeambtree(at->kid); |
457 |
+ |
} |
458 |
+ |
|
459 |
+ |
|
460 |
|
#ifdef F_SETLKW |
461 |
|
|
462 |
|
static |
488 |
|
aflock(F_WRLCK); |
489 |
|
/* see if file has grown */ |
490 |
|
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
491 |
< |
error(SYSTEM, "cannot seek on ambient file"); |
491 |
> |
goto seekerr; |
492 |
|
if (n = flen - lastpos) { /* file has grown */ |
493 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
494 |
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
496 |
|
error(SYSTEM, "fdopen failed in ambsync"); |
497 |
|
} |
498 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
499 |
< |
error(SYSTEM, "fseek failed in ambsync"); |
499 |
> |
goto seekerr; |
500 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
501 |
|
readambval(&avs, ambinp); |
502 |
< |
avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize); |
502 |
> |
avinsert(avstore(&avs)); |
503 |
|
n -= AMBVALSIZ; |
504 |
|
} |
505 |
< |
if (n) /* alignment */ |
506 |
< |
lseek(fileno(ambfp), flen-n, 0); |
505 |
> |
/*** seek always as safety measure |
506 |
> |
if (n) ***/ /* alignment */ |
507 |
> |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
508 |
> |
goto seekerr; |
509 |
|
} |
510 |
+ |
#ifdef DEBUG |
511 |
+ |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
512 |
+ |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
513 |
+ |
ambfp->_ptr - ambfp->_base, |
514 |
+ |
nunflshed*AMBVALSIZ); |
515 |
+ |
error(CONSISTENCY, errmsg); |
516 |
+ |
} |
517 |
+ |
#endif |
518 |
|
syncend: |
519 |
|
n = fflush(ambfp); /* calls write() at last */ |
520 |
< |
lastpos = lseek(fileno(ambfp), 0L, 1); |
520 |
> |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
521 |
> |
goto seekerr; |
522 |
|
aflock(F_UNLCK); /* release file */ |
523 |
|
nunflshed = 0; |
524 |
|
return(n); |
525 |
+ |
seekerr: |
526 |
+ |
error(SYSTEM, "seek failed in ambsync"); |
527 |
|
} |
528 |
|
|
529 |
|
#else |