1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
6 |
+ |
* |
7 |
+ |
* Declarations of external symbols in ambient.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include "ray.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "octree.h" |
13 |
< |
|
12 |
> |
#include "platform.h" |
13 |
> |
#include "ray.h" |
14 |
|
#include "otypes.h" |
16 |
– |
|
15 |
|
#include "ambient.h" |
18 |
– |
|
16 |
|
#include "random.h" |
17 |
|
|
18 |
|
#ifndef OCTSCALE |
19 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
20 |
|
#endif |
21 |
|
|
25 |
– |
typedef struct ambtree { |
26 |
– |
AMBVAL *alist; /* ambient value list */ |
27 |
– |
struct ambtree *kid; /* 8 child nodes */ |
28 |
– |
} AMBTREE; /* ambient octree */ |
29 |
– |
|
30 |
– |
extern CUBE thescene; /* contains space boundaries */ |
31 |
– |
|
22 |
|
extern char *shm_boundary; /* memory sharing boundary */ |
23 |
|
|
24 |
|
#define MAXASET 511 /* maximum number of elements in ambient set */ |
33 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
34 |
|
|
35 |
|
#ifndef SORT_THRESH |
36 |
< |
#ifdef BIGMEM |
47 |
< |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
48 |
< |
#else |
36 |
> |
#ifdef SMLMEM |
37 |
|
#define SORT_THRESH ((3L<<20)/sizeof(AMBVAL)) |
38 |
+ |
#else |
39 |
+ |
#define SORT_THRESH ((9L<<20)/sizeof(AMBVAL)) |
40 |
|
#endif |
41 |
|
#endif |
42 |
|
#ifndef SORT_INTVL |
53 |
|
static unsigned long ambclock = 0; /* ambient access clock */ |
54 |
|
static unsigned long lastsort = 0; /* time of last value sort */ |
55 |
|
static long sortintvl = SORT_INTVL; /* time until next sort */ |
56 |
+ |
static FILE *ambinp = NULL; /* auxiliary file for input */ |
57 |
+ |
static long lastpos = -1; /* last flush position */ |
58 |
|
|
59 |
|
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
60 |
|
/* |
70 |
|
|
71 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
72 |
|
|
73 |
< |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
73 |
> |
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
74 |
> |
#define freeav(av) free((void *)av); |
75 |
|
|
76 |
< |
static int initambfile(), avsave(), avinsert(), sortambvals(), avlmemi(); |
76 |
> |
static void initambfile(), avsave(), avinsert(), sortambvals(), unloadatree(); |
77 |
> |
static int avlmemi(); |
78 |
|
static AMBVAL *avstore(); |
79 |
|
#ifdef F_SETLKW |
80 |
< |
static aflock(); |
80 |
> |
static void aflock(); |
81 |
|
#endif |
82 |
|
|
83 |
|
|
84 |
+ |
void |
85 |
|
setambres(ar) /* set ambient resolution */ |
86 |
|
int ar; |
87 |
|
{ |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
+ |
void |
107 |
|
setambacc(newa) /* set ambient accuracy */ |
108 |
|
double newa; |
109 |
|
{ |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
< |
setambient(afile) /* initialize calculation */ |
121 |
< |
char *afile; |
120 |
> |
void |
121 |
> |
setambient() /* initialize calculation */ |
122 |
|
{ |
123 |
|
int readonly = 0; |
124 |
|
long pos, flen; |
125 |
|
AMBVAL amb; |
126 |
+ |
/* make sure we're fresh */ |
127 |
+ |
ambdone(); |
128 |
|
/* init ambient limits */ |
129 |
|
setambres(ambres); |
130 |
|
setambacc(ambacc); |
131 |
< |
if (afile == NULL) |
131 |
> |
if (ambfile == NULL || !ambfile[0]) |
132 |
|
return; |
133 |
|
if (ambacc <= FTINY) { |
134 |
|
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
135 |
< |
afile); |
135 |
> |
ambfile); |
136 |
|
error(WARNING, errmsg); |
137 |
|
return; |
138 |
|
} |
139 |
|
/* open ambient file */ |
140 |
< |
if ((ambfp = fopen(afile, "r+")) == NULL) |
141 |
< |
readonly = (ambfp = fopen(afile, "r")) != NULL; |
140 |
> |
if ((ambfp = fopen(ambfile, "r+")) == NULL) |
141 |
> |
readonly = (ambfp = fopen(ambfile, "r")) != NULL; |
142 |
|
if (ambfp != NULL) { |
143 |
|
initambfile(0); /* file exists */ |
144 |
|
pos = ftell(ambfp); |
156 |
|
} |
157 |
|
/* align file pointer */ |
158 |
|
pos += (long)nambvals*AMBVALSIZ; |
159 |
< |
flen = lseek(fileno(ambfp), 0L, 2); |
159 |
> |
flen = lseek(fileno(ambfp), (off_t)0L, 2); |
160 |
|
if (flen != pos) { |
161 |
|
sprintf(errmsg, |
162 |
|
"ignoring last %ld values in ambient file (corrupted)", |
163 |
|
(flen - pos)/AMBVALSIZ); |
164 |
|
error(WARNING, errmsg); |
165 |
|
fseek(ambfp, pos, 0); |
166 |
< |
ftruncate(fileno(ambfp), pos); |
166 |
> |
ftruncate(fileno(ambfp), (off_t)pos); |
167 |
|
} |
168 |
< |
} else if ((ambfp = fopen(afile, "w+")) != NULL) { |
168 |
> |
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
169 |
|
initambfile(1); /* else create new file */ |
170 |
|
} else { |
171 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
171 |
> |
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
172 |
|
error(SYSTEM, errmsg); |
173 |
|
} |
174 |
|
nunflshed++; /* lie */ |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
+ |
void |
180 |
+ |
ambdone() /* close ambient file and free memory */ |
181 |
+ |
{ |
182 |
+ |
if (ambfp != NULL) { /* close ambient file */ |
183 |
+ |
ambsync(); |
184 |
+ |
fclose(ambfp); |
185 |
+ |
ambfp = NULL; |
186 |
+ |
if (ambinp != NULL) { |
187 |
+ |
fclose(ambinp); |
188 |
+ |
ambinp = NULL; |
189 |
+ |
} |
190 |
+ |
lastpos = -1; |
191 |
+ |
} |
192 |
+ |
/* free ambient tree */ |
193 |
+ |
unloadatree(&atrunk, free); |
194 |
+ |
/* reset state variables */ |
195 |
+ |
avsum = 0.; |
196 |
+ |
navsum = 0; |
197 |
+ |
nambvals = 0; |
198 |
+ |
nambshare = 0; |
199 |
+ |
ambclock = 0; |
200 |
+ |
lastsort = 0; |
201 |
+ |
sortintvl = SORT_INTVL; |
202 |
+ |
} |
203 |
+ |
|
204 |
+ |
|
205 |
+ |
void |
206 |
|
ambnotify(obj) /* record new modifier */ |
207 |
|
OBJECT obj; |
208 |
|
{ |
209 |
|
static int hitlimit = 0; |
210 |
< |
register OBJREC *o = objptr(obj); |
210 |
> |
register OBJREC *o; |
211 |
|
register char **amblp; |
212 |
|
|
213 |
+ |
if (obj == OVOID) { /* starting over */ |
214 |
+ |
ambset[0] = 0; |
215 |
+ |
hitlimit = 0; |
216 |
+ |
return; |
217 |
+ |
} |
218 |
+ |
o = objptr(obj); |
219 |
|
if (hitlimit || !ismodifier(o->otype)) |
220 |
|
return; |
221 |
|
for (amblp = amblist; *amblp != NULL; amblp++) |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
+ |
void |
235 |
|
ambient(acol, r, nrm) /* compute ambient component for ray */ |
236 |
|
COLOR acol; |
237 |
|
register RAY *r; |
311 |
|
wsum = 0.0; |
312 |
|
/* do this node */ |
313 |
|
for (av = at->alist; av != NULL; av = av->next) { |
314 |
+ |
double rn_dot = -2.0; |
315 |
|
if (tracktime) |
316 |
|
av->latick = ambclock; |
317 |
|
/* |
334 |
|
if (e1 > ambacc*ambacc*1.21) |
335 |
|
continue; |
336 |
|
/* |
337 |
< |
* Normal direction test. |
337 |
> |
* Direction test using closest normal. |
338 |
|
*/ |
339 |
< |
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
339 |
> |
d = DOT(av->dir, r->ron); |
340 |
> |
if (rn != r->ron) { |
341 |
> |
rn_dot = DOT(av->dir, rn); |
342 |
> |
if (rn_dot > 1.0-FTINY) |
343 |
> |
rn_dot = 1.0-FTINY; |
344 |
> |
if (rn_dot >= d-FTINY) { |
345 |
> |
d = rn_dot; |
346 |
> |
rn_dot = -2.0; |
347 |
> |
} |
348 |
> |
} |
349 |
> |
e2 = (1.0 - d) * r->rweight; |
350 |
|
if (e2 < 0.0) e2 = 0.0; |
351 |
|
if (e1 + e2 > ambacc*ambacc*1.21) |
352 |
|
continue; |
362 |
|
/* |
363 |
|
* Jittering final test reduces image artifacts. |
364 |
|
*/ |
365 |
< |
wt = sqrt(e1) + sqrt(e2); |
365 |
> |
e1 = sqrt(e1); |
366 |
> |
e2 = sqrt(e2); |
367 |
> |
wt = e1 + e2; |
368 |
|
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
369 |
|
continue; |
370 |
+ |
/* |
371 |
+ |
* Recompute directional error using perturbed normal |
372 |
+ |
*/ |
373 |
+ |
if (rn_dot > 0.0) { |
374 |
+ |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
375 |
+ |
wt = e1 + e2; |
376 |
+ |
} |
377 |
|
if (wt <= 1e-3) |
378 |
|
wt = 1e3; |
379 |
|
else |
436 |
|
} |
437 |
|
|
438 |
|
|
439 |
+ |
void |
440 |
|
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
441 |
|
COLOR cr; |
442 |
|
register AMBVAL *ap; |
462 |
|
} |
463 |
|
|
464 |
|
|
465 |
< |
static |
465 |
> |
static void |
466 |
|
initambfile(creat) /* initialize ambient file */ |
467 |
|
int creat; |
468 |
|
{ |
469 |
< |
extern char *progname, *octname, VersionID[]; |
469 |
> |
extern char *progname, *octname; |
470 |
> |
static char *mybuf = NULL; |
471 |
|
|
472 |
|
#ifdef F_SETLKW |
473 |
|
aflock(creat ? F_WRLCK : F_RDLCK); |
474 |
|
#endif |
475 |
< |
#ifdef MSDOS |
476 |
< |
setmode(fileno(ambfp), O_BINARY); |
477 |
< |
#endif |
478 |
< |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
475 |
> |
SET_FILE_BINARY(ambfp); |
476 |
> |
if (mybuf == NULL) |
477 |
> |
mybuf = (char *)bmalloc(BUFSIZ+8); |
478 |
> |
setbuf(ambfp, mybuf); |
479 |
|
if (creat) { /* new file */ |
480 |
|
newheader("RADIANCE", ambfp); |
481 |
|
fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", |
482 |
|
progname, colval(ambval,RED), |
483 |
|
colval(ambval,GRN), colval(ambval,BLU), |
484 |
|
ambvwt, ambounce, ambacc); |
485 |
< |
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
486 |
< |
ambdiv, ambssamp, ambres, |
487 |
< |
octname==NULL ? "" : octname); |
485 |
> |
fprintf(ambfp, "-ad %d -as %d -ar %d ", |
486 |
> |
ambdiv, ambssamp, ambres); |
487 |
> |
if (octname != NULL) |
488 |
> |
printargs(1, &octname, ambfp); |
489 |
> |
else |
490 |
> |
fputc('\n', ambfp); |
491 |
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
492 |
+ |
fputnow(ambfp); |
493 |
|
fputformat(AMBFMT, ambfp); |
494 |
|
putc('\n', ambfp); |
495 |
|
putambmagic(ambfp); |
498 |
|
} |
499 |
|
|
500 |
|
|
501 |
< |
static |
501 |
> |
static void |
502 |
|
avsave(av) /* insert and save an ambient value */ |
503 |
|
AMBVAL *av; |
504 |
|
{ |
512 |
|
goto writerr; |
513 |
|
return; |
514 |
|
writerr: |
515 |
< |
error(SYSTEM, "error writing ambient file"); |
515 |
> |
error(SYSTEM, "error writing to ambient file"); |
516 |
|
} |
517 |
|
|
518 |
|
|
543 |
|
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
544 |
|
|
545 |
|
|
546 |
< |
static |
489 |
< |
AMBTREE * |
546 |
> |
static AMBTREE * |
547 |
|
newambtree() /* allocate 8 ambient tree structs */ |
548 |
|
{ |
549 |
|
register AMBTREE *atp, *upperlim; |
550 |
|
|
551 |
|
if (atfreelist == NULL) { /* get more nodes */ |
552 |
< |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
552 |
> |
atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
553 |
|
if (atfreelist == NULL) |
554 |
|
return(NULL); |
555 |
|
/* link new free list */ |
565 |
|
} |
566 |
|
|
567 |
|
|
568 |
< |
static |
568 |
> |
static void |
569 |
|
freeambtree(atp) /* free 8 ambient tree structs */ |
570 |
|
AMBTREE *atp; |
571 |
|
{ |
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
< |
static |
577 |
> |
static void |
578 |
|
avinsert(av) /* insert ambient value in our tree */ |
579 |
|
register AMBVAL *av; |
580 |
|
{ |
614 |
|
} |
615 |
|
|
616 |
|
|
617 |
< |
static |
617 |
> |
static void |
618 |
|
unloadatree(at, f) /* unload an ambient value tree */ |
619 |
|
register AMBTREE *at; |
620 |
< |
int (*f)(); |
620 |
> |
void (*f)(); |
621 |
|
{ |
622 |
|
register AMBVAL *av; |
623 |
|
register int i; |
643 |
|
static int i_avlist; /* index for lists */ |
644 |
|
|
645 |
|
|
646 |
< |
static |
646 |
> |
static int |
647 |
|
av2list(av) |
648 |
|
register AMBVAL *av; |
649 |
|
{ |
665 |
|
} |
666 |
|
|
667 |
|
|
668 |
+ |
/* GW NOTE 2002/10/3: |
669 |
+ |
* I used to compare AMBVAL pointers, but found that this was the |
670 |
+ |
* cause of a serious consistency error with gcc, since the optimizer |
671 |
+ |
* uses some dangerous trick in pointer subtraction that |
672 |
+ |
* assumes pointers differ by exact struct size increments. |
673 |
+ |
*/ |
674 |
|
static int |
675 |
|
aposcmp(avp1, avp2) /* compare ambient value positions */ |
676 |
< |
AMBVAL **avp1, **avp2; |
676 |
> |
const void *avp1, *avp2; |
677 |
|
{ |
678 |
< |
return(*avp1 - *avp2); |
678 |
> |
register long diff = *(char * const *)avp1 - *(char * const *)avp2; |
679 |
> |
if (diff < 0) |
680 |
> |
return(-1); |
681 |
> |
return(diff > 0); |
682 |
|
} |
683 |
|
|
618 |
– |
|
684 |
|
#if 1 |
685 |
|
static int |
686 |
|
avlmemi(avaddr) /* find list position from address */ |
700 |
|
#endif |
701 |
|
|
702 |
|
|
703 |
< |
static |
703 |
> |
static void |
704 |
|
sortambvals(always) /* resort ambient values */ |
705 |
|
int always; |
706 |
|
{ |
735 |
|
} |
736 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
737 |
|
if (avlist2 != NULL) |
738 |
< |
free((char *)avlist2); |
738 |
> |
free((void *)avlist2); |
739 |
|
if (always) { /* rebuild without sorting */ |
740 |
|
copystruct(&oldatrunk, &atrunk); |
741 |
|
atrunk.alist = NULL; |
779 |
|
avinsert(avlist2[j]); |
780 |
|
avlist1[j].p = NULL; |
781 |
|
} |
782 |
< |
free((char *)avlist1); |
783 |
< |
free((char *)avlist2); |
782 |
> |
free((void *)avlist1); |
783 |
> |
free((void *)avlist2); |
784 |
|
/* compute new sort interval */ |
785 |
|
sortintvl = ambclock - lastsort; |
786 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
799 |
|
|
800 |
|
#ifdef F_SETLKW |
801 |
|
|
802 |
< |
static |
802 |
> |
static void |
803 |
|
aflock(typ) /* lock/unlock ambient file */ |
804 |
|
int typ; |
805 |
|
{ |
814 |
|
int |
815 |
|
ambsync() /* synchronize ambient file */ |
816 |
|
{ |
752 |
– |
static FILE *ambinp = NULL; |
753 |
– |
static long lastpos = -1; |
817 |
|
long flen; |
818 |
|
AMBVAL avs; |
819 |
|
register int n; |
825 |
|
/* gain exclusive access */ |
826 |
|
aflock(F_WRLCK); |
827 |
|
/* see if file has grown */ |
828 |
< |
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
828 |
> |
if ((flen = lseek(fileno(ambfp), (off_t)0L, 2)) < 0) |
829 |
|
goto seekerr; |
830 |
|
if (n = flen - lastpos) { /* file has grown */ |
831 |
|
if (ambinp == NULL) { /* use duplicate filedes */ |
838 |
|
while (n >= AMBVALSIZ) { /* load contributed values */ |
839 |
|
if (!readambval(&avs, ambinp)) { |
840 |
|
sprintf(errmsg, |
841 |
< |
"ambient file corrupted near character %ld", |
842 |
< |
flen - n); |
841 |
> |
"ambient file \"%s\" corrupted near character %ld", |
842 |
> |
ambfile, flen - n); |
843 |
|
error(WARNING, errmsg); |
844 |
|
break; |
845 |
|
} |
848 |
|
} |
849 |
|
/*** seek always as safety measure |
850 |
|
if (n) ***/ /* alignment */ |
851 |
< |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
851 |
> |
if (lseek(fileno(ambfp), (off_t)(flen-n), 0) < 0) |
852 |
|
goto seekerr; |
853 |
|
} |
854 |
|
#ifdef DEBUG |
861 |
|
#endif |
862 |
|
syncend: |
863 |
|
n = fflush(ambfp); /* calls write() at last */ |
864 |
< |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
864 |
> |
if ((lastpos = lseek(fileno(ambfp), (off_t)0L, 1)) < 0) |
865 |
|
goto seekerr; |
866 |
|
aflock(F_UNLCK); /* release file */ |
867 |
|
nunflshed = 0; |