1 |
< |
/* Copyright (c) 1995 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
50 |
|
#endif |
51 |
|
#endif |
52 |
|
#ifndef SORT_INTVL |
53 |
< |
#define SORT_INTVL (SORT_THRESH*256) |
53 |
> |
#define SORT_INTVL (SORT_THRESH<<4) |
54 |
|
#endif |
55 |
|
#ifndef MAX_SORT_INTVL |
56 |
< |
#define MAX_SORT_INTVL (SORT_INTVL<<4) |
56 |
> |
#define MAX_SORT_INTVL (SORT_INTVL<<8) |
57 |
|
#endif |
58 |
|
|
59 |
|
static COLOR avsum = BLKCOLOR; /* computed ambient value sum */ |
77 |
|
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
78 |
|
|
79 |
|
extern long ftell(), lseek(); |
80 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
80 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); |
81 |
|
static AMBVAL *avstore(); |
82 |
|
#ifdef F_SETLKW |
83 |
|
static aflock(); |
121 |
|
setambient(afile) /* initialize calculation */ |
122 |
|
char *afile; |
123 |
|
{ |
124 |
< |
long headlen; |
124 |
> |
long pos, flen; |
125 |
|
AMBVAL amb; |
126 |
|
/* init ambient limits */ |
127 |
|
setambres(ambres); |
137 |
|
/* open ambient file */ |
138 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
139 |
|
initambfile(0); |
140 |
< |
headlen = ftell(ambfp); |
140 |
> |
pos = ftell(ambfp); |
141 |
|
while (readambval(&amb, ambfp)) |
142 |
|
avinsert(avstore(&amb)); |
143 |
|
/* align */ |
144 |
< |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
144 |
> |
pos += (long)nambvals*AMBVALSIZ; |
145 |
> |
flen = lseek(fileno(ambfp), 0L, 2); |
146 |
> |
if (flen != pos) { |
147 |
> |
error(WARNING, |
148 |
> |
"ignoring last %ld values in ambient file (corrupted)", |
149 |
> |
(flen - pos)/AMBVALSIZ); |
150 |
> |
fseek(ambfp, pos, 0); |
151 |
> |
ftruncate(fileno(ambfp), pos); |
152 |
> |
} |
153 |
|
nambshare = nambvals; |
154 |
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
155 |
|
initambfile(1); |
210 |
|
goto dumbamb; |
211 |
|
return; |
212 |
|
} |
213 |
< |
/* resort memory? */ |
214 |
< |
sortambvals(0); |
213 |
> |
|
214 |
> |
if (tracktime) /* sort to minimize thrashing */ |
215 |
> |
sortambvals(0); |
216 |
|
/* get ambient value */ |
217 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
218 |
|
d = sumambient(acol, r, nrm, rdepth, |
258 |
|
/* do this node */ |
259 |
|
for (av = at->alist; av != NULL; av = av->next) { |
260 |
|
if (tracktime) |
261 |
< |
av->latick = ambclock++; |
261 |
> |
av->latick = ambclock; |
262 |
|
/* |
263 |
|
* Ambient level test. |
264 |
|
*/ |
552 |
|
} |
553 |
|
|
554 |
|
|
555 |
< |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
555 |
> |
static struct avl { |
556 |
> |
AMBVAL *p; |
557 |
> |
unsigned long t; |
558 |
> |
} *avlist1; /* ambient value list with ticks */ |
559 |
> |
static AMBVAL **avlist2; /* memory positions for sorting */ |
560 |
|
static int i_avlist; /* index for lists */ |
561 |
|
|
562 |
|
|
563 |
|
static |
564 |
|
av2list(av) |
565 |
< |
AMBVAL *av; |
565 |
> |
register AMBVAL *av; |
566 |
|
{ |
567 |
|
#ifdef DEBUG |
568 |
|
if (i_avlist >= nambvals) |
569 |
|
error(CONSISTENCY, "too many ambient values in av2list1"); |
570 |
|
#endif |
571 |
< |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
572 |
< |
i_avlist++; |
571 |
> |
avlist1[i_avlist].p = avlist2[i_avlist] = av; |
572 |
> |
avlist1[i_avlist++].t = av->latick; |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
|
static int |
577 |
< |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
578 |
< |
AMBVAL **avp1, **avp2; |
577 |
> |
alatcmp(av1, av2) /* compare ambient values for MRA */ |
578 |
> |
struct avl *av1, *av2; |
579 |
|
{ |
580 |
< |
return((**avp2).latick - (**avp1).latick); |
580 |
> |
register long lc = av2->t - av1->t; |
581 |
> |
return(lc<0 ? -1 : lc>0 ? 1 : 0); |
582 |
|
} |
583 |
|
|
584 |
|
|
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
< |
#ifdef DEBUG |
593 |
> |
#if 1 |
594 |
|
static int |
595 |
|
avlmemi(avaddr) /* find list position from address */ |
596 |
|
AMBVAL *avaddr; |
617 |
|
AMBVAL tav, *tap, *pnext; |
618 |
|
register int i, j; |
619 |
|
/* see if it's time yet */ |
620 |
< |
if (!always && (ambclock < lastsort+sortintvl || |
620 |
> |
if (!always && (ambclock++ < lastsort+sortintvl || |
621 |
|
nambvals < SORT_THRESH)) |
622 |
|
return; |
623 |
|
/* |
636 |
|
* tree will be rebuilt with the new accuracy parameter. |
637 |
|
*/ |
638 |
|
if (tracktime) { /* allocate pointer arrays to sort */ |
625 |
– |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
639 |
|
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
640 |
< |
} else |
641 |
< |
avlist1 = avlist2 = NULL; |
642 |
< |
if (avlist2 == NULL) { /* no time tracking -- rebuild tree? */ |
643 |
< |
if (avlist1 != NULL) |
644 |
< |
free((char *)avlist1); |
640 |
> |
avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); |
641 |
> |
} else { |
642 |
> |
avlist2 = NULL; |
643 |
> |
avlist1 = NULL; |
644 |
> |
} |
645 |
> |
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
646 |
> |
if (avlist2 != NULL) |
647 |
> |
free((char *)avlist2); |
648 |
|
if (always) { /* rebuild without sorting */ |
649 |
|
copystruct(&oldatrunk, &atrunk); |
650 |
|
atrunk.alist = NULL; |
671 |
|
if (i_avlist < nambvals) |
672 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
673 |
|
#endif |
674 |
< |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
674 |
> |
qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); |
675 |
|
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
676 |
|
for (i = 0; i < nambvals; i++) { |
677 |
< |
if (avlist1[i] == NULL) |
677 |
> |
if (avlist1[i].p == NULL) |
678 |
|
continue; |
679 |
|
tap = avlist2[i]; |
680 |
|
copystruct(&tav, tap); |
681 |
< |
for (j = i; (pnext = avlist1[j]) != tap; |
681 |
> |
for (j = i; (pnext = avlist1[j].p) != tap; |
682 |
|
j = avlmemi(pnext)) { |
683 |
|
copystruct(avlist2[j], pnext); |
684 |
|
avinsert(avlist2[j]); |
685 |
< |
avlist1[j] = NULL; |
685 |
> |
avlist1[j].p = NULL; |
686 |
|
} |
687 |
|
copystruct(avlist2[j], &tav); |
688 |
|
avinsert(avlist2[j]); |
689 |
< |
avlist1[j] = NULL; |
689 |
> |
avlist1[j].p = NULL; |
690 |
|
} |
691 |
|
free((char *)avlist1); |
692 |
|
free((char *)avlist2); |
747 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
748 |
|
goto seekerr; |
749 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
750 |
< |
readambval(&avs, ambinp); |
750 |
> |
if (!readambval(&avs, ambinp)) { |
751 |
> |
sprintf(errmsg, |
752 |
> |
"ambient file corrupted near character %ld", |
753 |
> |
flen - n); |
754 |
> |
error(WARNING, errmsg); |
755 |
> |
break; |
756 |
> |
} |
757 |
|
avinsert(avstore(&avs)); |
758 |
|
n -= AMBVALSIZ; |
759 |
|
} |