1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
6 |
|
|
7 |
|
/* |
8 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
9 |
– |
* |
10 |
– |
* The macro AMBFLUSH (if defined) is the number of ambient values |
11 |
– |
* to wait before flushing to the ambient file. |
12 |
– |
* |
13 |
– |
* 5/9/86 |
9 |
|
*/ |
10 |
|
|
11 |
|
#include "ray.h" |
14 |
|
|
15 |
|
#include "otypes.h" |
16 |
|
|
17 |
+ |
#include "ambient.h" |
18 |
+ |
|
19 |
|
#include "random.h" |
20 |
|
|
21 |
< |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
21 |
> |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
22 |
|
|
23 |
+ |
typedef struct ambtree { |
24 |
+ |
AMBVAL *alist; /* ambient value list */ |
25 |
+ |
struct ambtree *kid; /* 8 child nodes */ |
26 |
+ |
} AMBTREE; /* ambient octree */ |
27 |
+ |
|
28 |
|
extern CUBE thescene; /* contains space boundaries */ |
29 |
|
|
30 |
< |
extern COLOR ambval; /* global ambient component */ |
29 |
< |
extern double ambacc; /* ambient accuracy */ |
30 |
< |
extern int ambres; /* ambient resolution */ |
31 |
< |
extern int ambdiv; /* number of divisions for calculation */ |
32 |
< |
extern int ambssamp; /* number of super-samples */ |
33 |
< |
extern int ambounce; /* number of ambient bounces */ |
34 |
< |
extern char *amblist[]; /* ambient include/exclude list */ |
35 |
< |
extern int ambincl; /* include == 1, exclude == 0 */ |
30 |
> |
extern char *shm_boundary; /* shared memory boundary */ |
31 |
|
|
32 |
< |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
33 |
< |
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
32 |
> |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
33 |
> |
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
34 |
|
|
35 |
< |
double maxarad; /* maximum ambient radius */ |
36 |
< |
double minarad; /* minimum ambient radius */ |
35 |
> |
double maxarad; /* maximum ambient radius */ |
36 |
> |
double minarad; /* minimum ambient radius */ |
37 |
|
|
38 |
< |
typedef struct ambval { |
44 |
< |
FVECT pos; /* position in space */ |
45 |
< |
FVECT dir; /* normal direction */ |
46 |
< |
int lvl; /* recursion level of parent ray */ |
47 |
< |
float weight; /* weight of parent ray */ |
48 |
< |
COLOR val; /* computed ambient value */ |
49 |
< |
float rad; /* validity radius */ |
50 |
< |
struct ambval *next; /* next in list */ |
51 |
< |
} AMBVAL; /* ambient value */ |
38 |
> |
static AMBTREE atrunk; /* our ambient trunk node */ |
39 |
|
|
40 |
< |
typedef struct ambtree { |
41 |
< |
AMBVAL *alist; /* ambient value list */ |
55 |
< |
struct ambtree *kid; /* 8 child nodes */ |
56 |
< |
} AMBTREE; /* ambient octree */ |
40 |
> |
static FILE *ambfp = NULL; /* ambient file pointer */ |
41 |
> |
static int nunflshed = 0; /* number of unflushed ambient values */ |
42 |
|
|
43 |
< |
typedef struct { |
44 |
< |
float k; /* error contribution per sample */ |
45 |
< |
COLOR v; /* ray sum */ |
46 |
< |
int n; /* number of samples */ |
47 |
< |
short t, p; /* theta, phi indices */ |
48 |
< |
} AMBSAMP; /* ambient sample */ |
43 |
> |
#ifndef SORT_THRESH |
44 |
> |
#ifdef BIGMEM |
45 |
> |
#define SORT_THRESH (6*(1L<<20)/sizeof(AMBVAL)) |
46 |
> |
#else |
47 |
> |
#define SORT_THRESH (2*(1L<<20)/sizeof(AMBVAL)) |
48 |
> |
#endif |
49 |
> |
#endif |
50 |
> |
#ifndef SORT_INTVL |
51 |
> |
#define SORT_INTVL (SORT_THRESH*2) |
52 |
> |
#endif |
53 |
|
|
54 |
< |
static AMBTREE atrunk; /* our ambient trunk node */ |
54 |
> |
static long ambclock = 0; /* ambient access clock */ |
55 |
> |
static int nambvals = 0; /* number of stored ambient values */ |
56 |
> |
static long lastsort = 0; /* time of last value sort */ |
57 |
> |
static long sortintvl = SORT_INTVL; /* time until next sort */ |
58 |
|
|
59 |
< |
static FILE *ambfp = NULL; /* ambient file pointer */ |
59 |
> |
#define MAXACLOCK (1L<<30) /* clock turnover value */ |
60 |
> |
#define tracktime (shm_boundary == NULL || ambfp == NULL) |
61 |
> |
#define need2sort (ambclock > lastsort+sortintvl && \ |
62 |
> |
nambvals > SORT_THRESH) |
63 |
|
|
64 |
< |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
64 |
> |
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
65 |
|
|
66 |
< |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
66 |
> |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
67 |
|
|
68 |
< |
double sumambient(), doambient(), makeambient(); |
68 |
> |
extern long ftell(), lseek(); |
69 |
> |
static int initambfile(), avsave(), avinsert(), sortambvals(); |
70 |
> |
static AMBVAL *avstore(); |
71 |
> |
#ifdef F_SETLKW |
72 |
> |
static aflock(); |
73 |
> |
#endif |
74 |
|
|
75 |
|
|
76 |
< |
setambient(afile) /* initialize calculation */ |
77 |
< |
char *afile; |
76 |
> |
setambres(ar) /* set ambient resolution */ |
77 |
> |
int ar; |
78 |
|
{ |
79 |
< |
long ftell(); |
80 |
< |
AMBVAL amb; |
79 |
> |
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
80 |
> |
/* set min & max radii */ |
81 |
> |
if (ar <= 0) { |
82 |
> |
minarad = 0; |
83 |
> |
maxarad = thescene.cusize / 2.0; |
84 |
> |
} else { |
85 |
> |
minarad = thescene.cusize / ar; |
86 |
> |
maxarad = 16 * minarad; /* heuristic */ |
87 |
> |
if (maxarad > thescene.cusize / 2.0) |
88 |
> |
maxarad = thescene.cusize / 2.0; |
89 |
> |
} |
90 |
> |
if (minarad <= FTINY) |
91 |
> |
minarad = 10*FTINY; |
92 |
> |
if (maxarad <= minarad) |
93 |
> |
maxarad = 64 * minarad; |
94 |
> |
} |
95 |
|
|
82 |
– |
maxarad = thescene.cusize / 2.0; /* maximum radius */ |
83 |
– |
/* minimum radius */ |
84 |
– |
minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; |
96 |
|
|
97 |
< |
/* open ambient file */ |
98 |
< |
if (afile != NULL) |
99 |
< |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
100 |
< |
while (fread((char *)&amb,sizeof(AMBVAL),1,ambfp) == 1) |
101 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, |
102 |
< |
thescene.cusize); |
103 |
< |
/* align */ |
104 |
< |
fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1); |
105 |
< |
} else if ((ambfp = fopen(afile, "w")) == NULL) { |
106 |
< |
sprintf(errmsg, "cannot open ambient file \"%s\"", |
107 |
< |
afile); |
108 |
< |
error(SYSTEM, errmsg); |
109 |
< |
} |
97 |
> |
setambacc(newa) /* set ambient accuracy */ |
98 |
> |
double newa; |
99 |
> |
{ |
100 |
> |
static double oldambacc = -1.0; |
101 |
> |
|
102 |
> |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
103 |
> |
if (oldambacc < -FTINY) |
104 |
> |
oldambacc = ambacc; /* do nothing first call */ |
105 |
> |
if (fabs(newa - oldambacc) < 0.01) |
106 |
> |
return; /* insignificant -- don't bother */ |
107 |
> |
if (ambacc <= FTINY) |
108 |
> |
return; /* cannot build new tree */ |
109 |
> |
/* else need to rebuild tree */ |
110 |
> |
sortambvals(1); |
111 |
> |
oldambacc = ambacc; /* remeber setting for next call */ |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
+ |
setambient(afile) /* initialize calculation */ |
116 |
+ |
char *afile; |
117 |
+ |
{ |
118 |
+ |
long headlen; |
119 |
+ |
AMBVAL amb; |
120 |
+ |
/* init ambient limits */ |
121 |
+ |
setambres(ambres); |
122 |
+ |
setambacc(ambacc); |
123 |
+ |
if (afile == NULL) |
124 |
+ |
return; |
125 |
+ |
if (ambacc <= FTINY) { |
126 |
+ |
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
127 |
+ |
afile); |
128 |
+ |
error(WARNING, errmsg); |
129 |
+ |
return; |
130 |
+ |
} |
131 |
+ |
/* open ambient file */ |
132 |
+ |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
133 |
+ |
initambfile(0); |
134 |
+ |
headlen = ftell(ambfp); |
135 |
+ |
while (readambval(&amb, ambfp)) |
136 |
+ |
avinsert(avstore(&amb)); |
137 |
+ |
/* align */ |
138 |
+ |
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
139 |
+ |
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
140 |
+ |
initambfile(1); |
141 |
+ |
else { |
142 |
+ |
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
143 |
+ |
error(SYSTEM, errmsg); |
144 |
+ |
} |
145 |
+ |
nunflshed++; /* lie */ |
146 |
+ |
ambsync(); |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
|
150 |
|
ambnotify(obj) /* record new modifier */ |
151 |
< |
OBJECT obj; |
151 |
> |
OBJECT obj; |
152 |
|
{ |
153 |
|
static int hitlimit = 0; |
154 |
< |
register OBJREC *o = objptr(obj); |
154 |
> |
register OBJREC *o = objptr(obj); |
155 |
|
register char **amblp; |
156 |
|
|
157 |
|
if (hitlimit || !ismodifier(o->otype)) |
174 |
|
register RAY *r; |
175 |
|
{ |
176 |
|
static int rdepth = 0; /* ambient recursion */ |
177 |
< |
double wsum; |
177 |
> |
double d; |
178 |
|
|
131 |
– |
rdepth++; /* increment level */ |
132 |
– |
|
179 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
180 |
|
goto dumbamb; |
181 |
|
/* check number of bounces */ |
182 |
< |
if (rdepth > ambounce) |
182 |
> |
if (rdepth >= ambounce) |
183 |
|
goto dumbamb; |
184 |
|
/* check ambient list */ |
185 |
|
if (ambincl != -1 && r->ro != NULL && |
187 |
|
goto dumbamb; |
188 |
|
|
189 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
190 |
< |
if (doambient(acol, r) == 0.0) |
190 |
> |
rdepth++; |
191 |
> |
d = doambient(acol, r, r->rweight, NULL, NULL); |
192 |
> |
rdepth--; |
193 |
> |
if (d == 0.0) |
194 |
|
goto dumbamb; |
195 |
< |
goto done; |
195 |
> |
return; |
196 |
|
} |
197 |
|
/* get ambient value */ |
198 |
+ |
if (need2sort) |
199 |
+ |
sortambvals(0); |
200 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
201 |
< |
wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize); |
202 |
< |
if (wsum > FTINY) |
203 |
< |
scalecolor(acol, 1.0/wsum); |
204 |
< |
else if (makeambient(acol, r) == 0.0) |
205 |
< |
goto dumbamb; |
206 |
< |
goto done; |
207 |
< |
|
201 |
> |
d = sumambient(acol, r, rdepth, |
202 |
> |
&atrunk, thescene.cuorg, thescene.cusize); |
203 |
> |
if (d > FTINY) |
204 |
> |
scalecolor(acol, 1.0/d); |
205 |
> |
else { |
206 |
> |
d = makeambient(acol, r, rdepth++); |
207 |
> |
rdepth--; |
208 |
> |
} |
209 |
> |
if (d > FTINY) |
210 |
> |
return; |
211 |
|
dumbamb: /* return global value */ |
212 |
|
copycolor(acol, ambval); |
159 |
– |
done: /* must finish here! */ |
160 |
– |
rdepth--; |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
double |
217 |
< |
sumambient(acol, r, at, c0, s) /* get interpolated ambient value */ |
217 |
> |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
218 |
|
COLOR acol; |
219 |
|
register RAY *r; |
220 |
< |
AMBTREE *at; |
220 |
> |
int al; |
221 |
> |
AMBTREE *at; |
222 |
|
FVECT c0; |
223 |
< |
double s; |
223 |
> |
double s; |
224 |
|
{ |
225 |
< |
extern double sqrt(); |
173 |
< |
double d, e1, e2, wt, wsum; |
225 |
> |
double d, e1, e2, wt, wsum; |
226 |
|
COLOR ct; |
227 |
|
FVECT ck0; |
228 |
|
int i; |
229 |
|
register int j; |
230 |
< |
register AMBVAL *av; |
230 |
> |
register AMBVAL *av; |
231 |
|
/* do this node */ |
232 |
|
wsum = 0.0; |
233 |
|
for (av = at->alist; av != NULL; av = av->next) { |
234 |
+ |
if (tracktime) |
235 |
+ |
av->latick = ambclock++; |
236 |
|
/* |
237 |
< |
* Ray strength test. |
237 |
> |
* Ambient level test. |
238 |
|
*/ |
239 |
< |
if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY) |
239 |
> |
if (av->lvl > al) /* list sorted, so this works */ |
240 |
> |
break; |
241 |
> |
if (av->weight < r->rweight-FTINY) |
242 |
|
continue; |
243 |
|
/* |
244 |
|
* Ambient radius test. |
265 |
|
for (j = 0; j < 3; j++) |
266 |
|
d += (r->rop[j] - av->pos[j]) * |
267 |
|
(av->dir[j] + r->ron[j]); |
268 |
< |
if (d < -minarad) |
268 |
> |
if (d*0.5 < -minarad*ambacc-.001) |
269 |
|
continue; |
270 |
|
/* |
271 |
|
* Jittering final test reduces image artifacts. |
272 |
|
*/ |
273 |
|
wt = sqrt(e1) + sqrt(e2); |
274 |
< |
wt *= .9 + .2*frandom(); |
274 |
> |
wt *= .9 + .2*urand(9015+samplendx); |
275 |
|
if (wt > ambacc) |
276 |
|
continue; |
277 |
|
if (wt <= 1e-3) |
279 |
|
else |
280 |
|
wt = 1.0 / wt; |
281 |
|
wsum += wt; |
282 |
< |
copycolor(ct, av->val); |
282 |
> |
extambient(ct, av, r->rop, r->ron); |
283 |
|
scalecolor(ct, wt); |
284 |
|
addcolor(acol, ct); |
285 |
|
} |
298 |
|
break; |
299 |
|
} |
300 |
|
if (j == 3) |
301 |
< |
wsum += sumambient(acol, r, at->kid+i, ck0, s); |
301 |
> |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
302 |
|
} |
303 |
|
return(wsum); |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
|
double |
308 |
< |
makeambient(acol, r) /* make a new ambient value */ |
308 |
> |
makeambient(acol, r, al) /* make a new ambient value */ |
309 |
|
COLOR acol; |
310 |
|
register RAY *r; |
311 |
+ |
int al; |
312 |
|
{ |
313 |
< |
AMBVAL amb; |
314 |
< |
|
315 |
< |
amb.rad = doambient(acol, r); /* compute ambient */ |
313 |
> |
AMBVAL amb; |
314 |
> |
FVECT gp, gd; |
315 |
> |
/* compute weight */ |
316 |
> |
amb.weight = pow(AVGREFL, (double)al); |
317 |
> |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
318 |
> |
amb.weight = r->rweight; |
319 |
> |
/* compute ambient */ |
320 |
> |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
321 |
|
if (amb.rad == 0.0) |
322 |
|
return(0.0); |
323 |
|
/* store it */ |
324 |
|
VCOPY(amb.pos, r->rop); |
325 |
|
VCOPY(amb.dir, r->ron); |
326 |
< |
amb.lvl = r->rlvl; |
265 |
< |
amb.weight = r->rweight; |
326 |
> |
amb.lvl = al; |
327 |
|
copycolor(amb.val, acol); |
328 |
+ |
VCOPY(amb.gpos, gp); |
329 |
+ |
VCOPY(amb.gdir, gd); |
330 |
|
/* insert into tree */ |
331 |
< |
avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize); |
269 |
< |
avsave(&amb); /* write to file */ |
331 |
> |
avsave(&amb); /* and save to file */ |
332 |
|
return(amb.rad); |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
< |
double |
337 |
< |
doambient(acol, r) /* compute ambient component */ |
338 |
< |
COLOR acol; |
339 |
< |
register RAY *r; |
336 |
> |
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
337 |
> |
COLOR cr; |
338 |
> |
register AMBVAL *ap; |
339 |
> |
FVECT pv, nv; |
340 |
|
{ |
341 |
< |
extern int ambcmp(); |
342 |
< |
extern double sin(), cos(), sqrt(); |
343 |
< |
double phi, xd, yd, zd; |
282 |
< |
double b, b2; |
283 |
< |
register AMBSAMP *div; |
284 |
< |
AMBSAMP dnew; |
285 |
< |
RAY ar; |
286 |
< |
FVECT ux, uy; |
287 |
< |
double arad; |
288 |
< |
int ndivs, nt, np, ns, ne, i, j; |
289 |
< |
register int k; |
341 |
> |
FVECT v1, v2; |
342 |
> |
register int i; |
343 |
> |
double d; |
344 |
|
|
345 |
< |
setcolor(acol, 0.0, 0.0, 0.0); |
346 |
< |
/* set number of divisions */ |
347 |
< |
nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5; |
348 |
< |
np = 2 * nt; |
349 |
< |
ndivs = nt * np; |
350 |
< |
/* check first */ |
351 |
< |
if (ndivs == 0 || rayorigin(&ar, r, AMBIENT, 0.5) < 0) |
352 |
< |
return(0.0); |
353 |
< |
/* set number of super-samples */ |
354 |
< |
ns = ambssamp * r->rweight + 0.5; |
355 |
< |
if (ns > 0) { |
302 |
< |
div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP)); |
303 |
< |
if (div == NULL) |
304 |
< |
error(SYSTEM, "out of memory in doambient"); |
345 |
> |
d = 1.0; /* zeroeth order */ |
346 |
> |
/* gradient due to translation */ |
347 |
> |
for (i = 0; i < 3; i++) |
348 |
> |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
349 |
> |
/* gradient due to rotation */ |
350 |
> |
VCOPY(v1, ap->dir); |
351 |
> |
fcross(v2, v1, nv); |
352 |
> |
d += DOT(ap->gdir, v2); |
353 |
> |
if (d <= 0.0) { |
354 |
> |
setcolor(cr, 0.0, 0.0, 0.0); |
355 |
> |
return; |
356 |
|
} |
357 |
< |
/* make axes */ |
358 |
< |
uy[0] = uy[1] = uy[2] = 0.0; |
308 |
< |
for (k = 0; k < 3; k++) |
309 |
< |
if (r->ron[k] < 0.6 && r->ron[k] > -0.6) |
310 |
< |
break; |
311 |
< |
uy[k] = 1.0; |
312 |
< |
fcross(ux, r->ron, uy); |
313 |
< |
normalize(ux); |
314 |
< |
fcross(uy, ux, r->ron); |
315 |
< |
/* sample divisions */ |
316 |
< |
arad = 0.0; |
317 |
< |
ne = 0; |
318 |
< |
for (i = 0; i < nt; i++) |
319 |
< |
for (j = 0; j < np; j++) { |
320 |
< |
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
321 |
< |
zd = sqrt((i+frandom())/nt); |
322 |
< |
phi = 2.0*PI * (j+frandom())/np; |
323 |
< |
xd = cos(phi) * zd; |
324 |
< |
yd = sin(phi) * zd; |
325 |
< |
zd = sqrt(1.0 - zd*zd); |
326 |
< |
for (k = 0; k < 3; k++) |
327 |
< |
ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k]; |
328 |
< |
rayvalue(&ar); |
329 |
< |
if (ar.rot < FHUGE) |
330 |
< |
arad += 1.0 / ar.rot; |
331 |
< |
if (ns > 0) { /* save division */ |
332 |
< |
div[ne].k = 0.0; |
333 |
< |
copycolor(div[ne].v, ar.rcol); |
334 |
< |
div[ne].n = 0; |
335 |
< |
div[ne].t = i; div[ne].p = j; |
336 |
< |
/* sum errors */ |
337 |
< |
b = bright(ar.rcol); |
338 |
< |
if (i > 0) { /* from above */ |
339 |
< |
b2 = bright(div[ne-np].v) - b; |
340 |
< |
b2 *= b2 * 0.25; |
341 |
< |
div[ne].k += b2; |
342 |
< |
div[ne].n++; |
343 |
< |
div[ne-np].k += b2; |
344 |
< |
div[ne-np].n++; |
345 |
< |
} |
346 |
< |
if (j > 0) { /* from behind */ |
347 |
< |
b2 = bright(div[ne-1].v) - b; |
348 |
< |
b2 *= b2 * 0.25; |
349 |
< |
div[ne].k += b2; |
350 |
< |
div[ne].n++; |
351 |
< |
div[ne-1].k += b2; |
352 |
< |
div[ne-1].n++; |
353 |
< |
} |
354 |
< |
if (j == np-1) { /* around */ |
355 |
< |
b2 = bright(div[ne-(np-1)].v) - b; |
356 |
< |
b2 *= b2 * 0.25; |
357 |
< |
div[ne].k += b2; |
358 |
< |
div[ne].n++; |
359 |
< |
div[ne-(np-1)].k += b2; |
360 |
< |
div[ne-(np-1)].n++; |
361 |
< |
} |
362 |
< |
ne++; |
363 |
< |
} else |
364 |
< |
addcolor(acol, ar.rcol); |
365 |
< |
} |
366 |
< |
for (k = 0; k < ne; k++) { /* compute errors */ |
367 |
< |
if (div[k].n > 1) |
368 |
< |
div[k].k /= div[k].n; |
369 |
< |
div[k].n = 1; |
370 |
< |
} |
371 |
< |
/* sort the divisions */ |
372 |
< |
qsort(div, ne, sizeof(AMBSAMP), ambcmp); |
373 |
< |
/* skim excess */ |
374 |
< |
while (ne > ns) { |
375 |
< |
ne--; |
376 |
< |
addcolor(acol, div[ne].v); |
377 |
< |
} |
378 |
< |
/* super-sample */ |
379 |
< |
for (i = ns; i > 0; i--) { |
380 |
< |
rayorigin(&ar, r, AMBIENT, 0.5); /* pretested */ |
381 |
< |
zd = sqrt((div[0].t+frandom())/nt); |
382 |
< |
phi = 2.0*PI * (div[0].p+frandom())/np; |
383 |
< |
xd = cos(phi) * zd; |
384 |
< |
yd = sin(phi) * zd; |
385 |
< |
zd = sqrt(1.0 - zd*zd); |
386 |
< |
for (k = 0; k < 3; k++) |
387 |
< |
ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k]; |
388 |
< |
rayvalue(&ar); |
389 |
< |
if (ar.rot < FHUGE) |
390 |
< |
arad += 1.0 / ar.rot; |
391 |
< |
/* recompute error */ |
392 |
< |
copycolor(dnew.v, div[0].v); |
393 |
< |
addcolor(dnew.v, ar.rcol); |
394 |
< |
dnew.n = div[0].n + 1; |
395 |
< |
dnew.t = div[0].t; dnew.p = div[0].p; |
396 |
< |
b2 = bright(dnew.v)/dnew.n - bright(ar.rcol); |
397 |
< |
b2 = b2*b2 + div[0].k*(div[0].n*div[0].n); |
398 |
< |
dnew.k = b2/(dnew.n*dnew.n); |
399 |
< |
/* reinsert */ |
400 |
< |
for (k = 0; k < ne-1 && dnew.k < div[k+1].k; k++) |
401 |
< |
copystruct(&div[k], &div[k+1]); |
402 |
< |
copystruct(&div[k], &dnew); |
403 |
< |
|
404 |
< |
if (ne >= i) { /* extract darkest division */ |
405 |
< |
ne--; |
406 |
< |
if (div[ne].n > 1) |
407 |
< |
scalecolor(div[ne].v, 1.0/div[ne].n); |
408 |
< |
addcolor(acol, div[ne].v); |
409 |
< |
} |
410 |
< |
} |
411 |
< |
scalecolor(acol, 1.0/ndivs); |
412 |
< |
if (arad <= FTINY) |
413 |
< |
arad = FHUGE; |
414 |
< |
else |
415 |
< |
arad = (ndivs+ns) / arad / sqrt(r->rweight); |
416 |
< |
if (arad > maxarad) |
417 |
< |
arad = maxarad; |
418 |
< |
else if (arad < minarad) |
419 |
< |
arad = minarad; |
420 |
< |
if (ns > 0) |
421 |
< |
free((char *)div); |
422 |
< |
return(arad); |
357 |
> |
copycolor(cr, ap->val); |
358 |
> |
scalecolor(cr, d); |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
< |
static int |
363 |
< |
ambcmp(d1, d2) /* decreasing order */ |
364 |
< |
AMBSAMP *d1, *d2; |
362 |
> |
static |
363 |
> |
initambfile(creat) /* initialize ambient file */ |
364 |
> |
int creat; |
365 |
|
{ |
366 |
< |
if (d1->k < d2->k) |
367 |
< |
return(1); |
368 |
< |
if (d1->k > d2->k) |
369 |
< |
return(-1); |
370 |
< |
return(0); |
366 |
> |
extern char *progname, *octname, VersionID[]; |
367 |
> |
|
368 |
> |
#ifdef F_SETLKW |
369 |
> |
aflock(creat ? F_WRLCK : F_RDLCK); |
370 |
> |
#endif |
371 |
> |
#ifdef MSDOS |
372 |
> |
setmode(fileno(ambfp), O_BINARY); |
373 |
> |
#endif |
374 |
> |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
375 |
> |
if (creat) { /* new file */ |
376 |
> |
newheader("RADIANCE", ambfp); |
377 |
> |
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
378 |
> |
progname, colval(ambval,RED), |
379 |
> |
colval(ambval,GRN), colval(ambval,BLU), |
380 |
> |
ambounce, ambacc); |
381 |
> |
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
382 |
> |
ambdiv, ambssamp, ambres, |
383 |
> |
octname==NULL ? "" : octname); |
384 |
> |
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
385 |
> |
fputformat(AMBFMT, ambfp); |
386 |
> |
putc('\n', ambfp); |
387 |
> |
putambmagic(ambfp); |
388 |
> |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
389 |
> |
error(USER, "bad ambient file"); |
390 |
|
} |
391 |
|
|
392 |
|
|
393 |
|
static |
394 |
< |
avsave(av) /* save an ambient value */ |
395 |
< |
AMBVAL *av; |
394 |
> |
avsave(av) /* insert and save an ambient value */ |
395 |
> |
AMBVAL *av; |
396 |
|
{ |
397 |
< |
#ifdef AMBFLUSH |
443 |
< |
static int nunflshed = 0; |
444 |
< |
#endif |
397 |
> |
avinsert(avstore(av)); |
398 |
|
if (ambfp == NULL) |
399 |
|
return; |
400 |
< |
if (fwrite((char *)av, sizeof(AMBVAL), 1, ambfp) != 1) |
400 |
> |
if (writambval(av, ambfp) < 0) |
401 |
|
goto writerr; |
402 |
< |
#ifdef AMBFLUSH |
403 |
< |
if (++nunflshed >= AMBFLUSH) { |
451 |
< |
if (fflush(ambfp) == EOF) |
402 |
> |
if (++nunflshed >= AMBFLUSH) |
403 |
> |
if (ambsync() == EOF) |
404 |
|
goto writerr; |
453 |
– |
nunflshed = 0; |
454 |
– |
} |
455 |
– |
#endif |
405 |
|
return; |
406 |
|
writerr: |
407 |
|
error(SYSTEM, "error writing ambient file"); |
408 |
|
} |
409 |
|
|
410 |
|
|
411 |
+ |
static AMBVAL * |
412 |
+ |
avstore(aval) /* allocate memory and store aval */ |
413 |
+ |
register AMBVAL *aval; |
414 |
+ |
{ |
415 |
+ |
register AMBVAL *av; |
416 |
+ |
|
417 |
+ |
if ((av = newambval()) == NULL) |
418 |
+ |
error(SYSTEM, "out of memory in avstore"); |
419 |
+ |
copystruct(av, aval); |
420 |
+ |
av->latick = ambclock; |
421 |
+ |
av->next = NULL; |
422 |
+ |
nambvals++; |
423 |
+ |
return(av); |
424 |
+ |
} |
425 |
+ |
|
426 |
+ |
|
427 |
+ |
#define ATALLOCSZ 512 /* #/8 trees to allocate at once */ |
428 |
+ |
|
429 |
+ |
static AMBTREE *atfreelist = NULL; /* free ambient tree structures */ |
430 |
+ |
|
431 |
+ |
|
432 |
|
static |
433 |
< |
avinsert(aval, at, c0, s) /* insert ambient value in a tree */ |
434 |
< |
AMBVAL *aval; |
465 |
< |
register AMBTREE *at; |
466 |
< |
FVECT c0; |
467 |
< |
double s; |
433 |
> |
AMBTREE * |
434 |
> |
newambtree() /* allocate 8 ambient tree structs */ |
435 |
|
{ |
436 |
+ |
register AMBTREE *atp, *upperlim; |
437 |
+ |
|
438 |
+ |
if (atfreelist == NULL) { /* get more nodes */ |
439 |
+ |
atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE)); |
440 |
+ |
if (atfreelist == NULL) |
441 |
+ |
return(NULL); |
442 |
+ |
/* link new free list */ |
443 |
+ |
upperlim = atfreelist + 8*(ATALLOCSZ-1); |
444 |
+ |
for (atp = atfreelist; atp < upperlim; atp += 8) |
445 |
+ |
atp->kid = atp + 8; |
446 |
+ |
atp->kid = NULL; |
447 |
+ |
} |
448 |
+ |
atp = atfreelist; |
449 |
+ |
atfreelist = atp->kid; |
450 |
+ |
bzero((char *)atp, 8*sizeof(AMBTREE)); |
451 |
+ |
return(atp); |
452 |
+ |
} |
453 |
+ |
|
454 |
+ |
|
455 |
+ |
static |
456 |
+ |
freeambtree(atp) /* free 8 ambient tree structs */ |
457 |
+ |
AMBTREE *atp; |
458 |
+ |
{ |
459 |
+ |
atp->kid = atfreelist; |
460 |
+ |
atfreelist = atp; |
461 |
+ |
} |
462 |
+ |
|
463 |
+ |
|
464 |
+ |
static |
465 |
+ |
avinsert(av) /* insert ambient value in our tree */ |
466 |
+ |
register AMBVAL *av; |
467 |
+ |
{ |
468 |
+ |
register AMBTREE *at; |
469 |
+ |
register AMBVAL *ap; |
470 |
+ |
AMBVAL avh; |
471 |
|
FVECT ck0; |
472 |
+ |
double s; |
473 |
|
int branch; |
471 |
– |
register AMBVAL *av; |
474 |
|
register int i; |
475 |
|
|
476 |
< |
if ((av = newambval()) == NULL) |
477 |
< |
goto memerr; |
478 |
< |
copystruct(av, aval); |
479 |
< |
VCOPY(ck0, c0); |
476 |
> |
if (av->rad <= FTINY) |
477 |
> |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
478 |
> |
at = &atrunk; |
479 |
> |
VCOPY(ck0, thescene.cuorg); |
480 |
> |
s = thescene.cusize; |
481 |
|
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
482 |
|
if (at->kid == NULL) |
483 |
|
if ((at->kid = newambtree()) == NULL) |
484 |
< |
goto memerr; |
484 |
> |
error(SYSTEM, "out of memory in avinsert"); |
485 |
|
s *= 0.5; |
486 |
|
branch = 0; |
487 |
|
for (i = 0; i < 3; i++) |
491 |
|
} |
492 |
|
at = at->kid + branch; |
493 |
|
} |
494 |
< |
av->next = at->alist; |
495 |
< |
at->alist = av; |
496 |
< |
return; |
497 |
< |
memerr: |
498 |
< |
error(SYSTEM, "out of memory in avinsert"); |
494 |
> |
avh.next = at->alist; /* order by increasing level */ |
495 |
> |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
496 |
> |
if (ap->next->lvl >= av->lvl) |
497 |
> |
break; |
498 |
> |
av->next = ap->next; |
499 |
> |
ap->next = av; |
500 |
> |
at->alist = avh.next; |
501 |
|
} |
502 |
+ |
|
503 |
+ |
|
504 |
+ |
static |
505 |
+ |
unloadatree(at, f) /* unload an ambient value tree */ |
506 |
+ |
register AMBTREE *at; |
507 |
+ |
int (*f)(); |
508 |
+ |
{ |
509 |
+ |
register AMBVAL *av; |
510 |
+ |
register int i; |
511 |
+ |
/* transfer values at this node */ |
512 |
+ |
for (av = at->alist; av != NULL; av = at->alist) { |
513 |
+ |
at->alist = av->next; |
514 |
+ |
(*f)(av); |
515 |
+ |
} |
516 |
+ |
if (at->kid == NULL) |
517 |
+ |
return; |
518 |
+ |
for (i = 0; i < 8; i++) /* transfer and free children */ |
519 |
+ |
unloadatree(at->kid+i, f); |
520 |
+ |
freeambtree(at->kid); |
521 |
+ |
at->kid = NULL; |
522 |
+ |
} |
523 |
+ |
|
524 |
+ |
|
525 |
+ |
static AMBVAL **avlist1, **avlist2; /* ambient value lists for sorting */ |
526 |
+ |
static int i_avlist; /* index for lists */ |
527 |
+ |
|
528 |
+ |
|
529 |
+ |
static |
530 |
+ |
av2list(av) |
531 |
+ |
AMBVAL *av; |
532 |
+ |
{ |
533 |
+ |
if (i_avlist >= nambvals) |
534 |
+ |
error(CONSISTENCY, "too many ambient values in av2list1"); |
535 |
+ |
avlist1[i_avlist] = avlist2[i_avlist] = av; |
536 |
+ |
i_avlist++; |
537 |
+ |
} |
538 |
+ |
|
539 |
+ |
|
540 |
+ |
static int |
541 |
+ |
alatcmp(avp1, avp2) /* compare ambient values for MRA */ |
542 |
+ |
AMBVAL **avp1, **avp2; |
543 |
+ |
{ |
544 |
+ |
return((**avp2).latick - (**avp1).latick); |
545 |
+ |
} |
546 |
+ |
|
547 |
+ |
|
548 |
+ |
static int |
549 |
+ |
aposcmp(avp1, avp2) /* compare ambient value positions */ |
550 |
+ |
AMBVAL **avp1, **avp2; |
551 |
+ |
{ |
552 |
+ |
return(*avp1 - *avp2); |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
|
556 |
+ |
static |
557 |
+ |
sortambvals(always) /* resort ambient values */ |
558 |
+ |
int always; |
559 |
+ |
{ |
560 |
+ |
AMBTREE oldatrunk; |
561 |
+ |
AMBVAL tav, *tap, *pnext; |
562 |
+ |
register int i, j; |
563 |
+ |
/* |
564 |
+ |
* The idea here is to minimize memory thrashing |
565 |
+ |
* in VM systems by improving reference locality. |
566 |
+ |
* We do this by periodically sorting our stored ambient |
567 |
+ |
* values in memory in order of most recently to least |
568 |
+ |
* recently accessed. This ordering was chosen so that new |
569 |
+ |
* ambient values (which tend to be less important) go into |
570 |
+ |
* higher memory with the infrequently accessed values. |
571 |
+ |
* Since we expect our values to need sorting less |
572 |
+ |
* frequently as the process continues, we double our |
573 |
+ |
* waiting interval after each call. |
574 |
+ |
* This routine is also called by setambacc() with |
575 |
+ |
* the "always" parameter set to 1 so that the ambient |
576 |
+ |
* tree will be rebuilt with the new accuracy parameter. |
577 |
+ |
*/ |
578 |
+ |
if (tracktime) { |
579 |
+ |
avlist1 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
580 |
+ |
avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); |
581 |
+ |
} else |
582 |
+ |
avlist1 = avlist2 = NULL; |
583 |
+ |
if (avlist2 == NULL) { /* rebuild tree? */ |
584 |
+ |
if (avlist1 != NULL) |
585 |
+ |
free((char *)avlist1); |
586 |
+ |
if (!always) |
587 |
+ |
return; |
588 |
+ |
copystruct(&oldatrunk, &atrunk); |
589 |
+ |
atrunk.alist = NULL; |
590 |
+ |
atrunk.kid = NULL; |
591 |
+ |
unloadatree(&oldatrunk, avinsert); |
592 |
+ |
} else { /* sort memory by last access time */ |
593 |
+ |
i_avlist = 0; |
594 |
+ |
unloadatree(&atrunk, av2list); /* empty current tree */ |
595 |
+ |
/* |
596 |
+ |
* Sorting memory is tricky because it isn't contiguous. |
597 |
+ |
* We have to sort an array of pointers by MRA and also |
598 |
+ |
* by memory position. We then copy values in "loops" |
599 |
+ |
* to minimize memory hits. Nevertheless, we will visit |
600 |
+ |
* everyone at least once, and this is an expensive process |
601 |
+ |
* when we're thrashing, which is when we need to do it. |
602 |
+ |
*/ |
603 |
+ |
qsort((char *)avlist1, nambvals, sizeof(AMBVAL *), alatcmp); |
604 |
+ |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
605 |
+ |
for (i = 0; i < nambvals; i++) { |
606 |
+ |
if (avlist1[i] == NULL || avlist1[i] == avlist2[i]) |
607 |
+ |
continue; |
608 |
+ |
tap = avlist2[i]; |
609 |
+ |
copystruct(&tav, tap); |
610 |
+ |
for (j = i; (pnext = avlist1[j]) != tap; |
611 |
+ |
j = (AMBVAL **)bsearch((char *)&pnext, |
612 |
+ |
(char *)(avlist2+i),nambvals-i, |
613 |
+ |
sizeof(AMBVAL *),aposcmp) - |
614 |
+ |
avlist2) { |
615 |
+ |
copystruct(avlist2[j], pnext); |
616 |
+ |
avinsert(avlist2[j]); |
617 |
+ |
avlist1[j] = NULL; |
618 |
+ |
} |
619 |
+ |
copystruct(avlist2[j], &tav); |
620 |
+ |
avinsert(avlist2[j]); |
621 |
+ |
avlist1[j] = NULL; |
622 |
+ |
} |
623 |
+ |
free((char *)avlist1); |
624 |
+ |
free((char *)avlist2); |
625 |
+ |
if (sortintvl < MAXACLOCK/4) |
626 |
+ |
sortintvl <<= 1; /* wait twice as long next */ |
627 |
+ |
} |
628 |
+ |
if (ambclock >= MAXACLOCK) |
629 |
+ |
ambclock = MAXACLOCK/2; |
630 |
+ |
lastsort = ambclock; |
631 |
+ |
} |
632 |
+ |
|
633 |
+ |
|
634 |
+ |
#ifdef F_SETLKW |
635 |
+ |
|
636 |
+ |
static |
637 |
+ |
aflock(typ) /* lock/unlock ambient file */ |
638 |
+ |
int typ; |
639 |
+ |
{ |
640 |
+ |
static struct flock fls; /* static so initialized to zeroes */ |
641 |
+ |
|
642 |
+ |
fls.l_type = typ; |
643 |
+ |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
644 |
+ |
error(SYSTEM, "cannot (un)lock ambient file"); |
645 |
+ |
} |
646 |
+ |
|
647 |
+ |
|
648 |
+ |
int |
649 |
+ |
ambsync() /* synchronize ambient file */ |
650 |
+ |
{ |
651 |
+ |
static FILE *ambinp = NULL; |
652 |
+ |
static long lastpos = -1; |
653 |
+ |
long flen; |
654 |
+ |
AMBVAL avs; |
655 |
+ |
register int n; |
656 |
+ |
|
657 |
+ |
if (nunflshed == 0) |
658 |
+ |
return(0); |
659 |
+ |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
660 |
+ |
goto syncend; |
661 |
+ |
/* gain exclusive access */ |
662 |
+ |
aflock(F_WRLCK); |
663 |
+ |
/* see if file has grown */ |
664 |
+ |
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
665 |
+ |
goto seekerr; |
666 |
+ |
if (n = flen - lastpos) { /* file has grown */ |
667 |
+ |
if (ambinp == NULL) { /* use duplicate filedes */ |
668 |
+ |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
669 |
+ |
if (ambinp == NULL) |
670 |
+ |
error(SYSTEM, "fdopen failed in ambsync"); |
671 |
+ |
} |
672 |
+ |
if (fseek(ambinp, lastpos, 0) < 0) |
673 |
+ |
goto seekerr; |
674 |
+ |
while (n >= AMBVALSIZ) { /* load contributed values */ |
675 |
+ |
readambval(&avs, ambinp); |
676 |
+ |
avinsert(avstore(&avs)); |
677 |
+ |
n -= AMBVALSIZ; |
678 |
+ |
} |
679 |
+ |
/*** seek always as safety measure |
680 |
+ |
if (n) ***/ /* alignment */ |
681 |
+ |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
682 |
+ |
goto seekerr; |
683 |
+ |
} |
684 |
+ |
#ifdef DEBUG |
685 |
+ |
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
686 |
+ |
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
687 |
+ |
ambfp->_ptr - ambfp->_base, |
688 |
+ |
nunflshed*AMBVALSIZ); |
689 |
+ |
error(CONSISTENCY, errmsg); |
690 |
+ |
} |
691 |
+ |
#endif |
692 |
+ |
syncend: |
693 |
+ |
n = fflush(ambfp); /* calls write() at last */ |
694 |
+ |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
695 |
+ |
goto seekerr; |
696 |
+ |
aflock(F_UNLCK); /* release file */ |
697 |
+ |
nunflshed = 0; |
698 |
+ |
return(n); |
699 |
+ |
seekerr: |
700 |
+ |
error(SYSTEM, "seek failed in ambsync"); |
701 |
+ |
} |
702 |
+ |
|
703 |
+ |
#else |
704 |
+ |
|
705 |
+ |
int |
706 |
+ |
ambsync() /* flush ambient file */ |
707 |
+ |
{ |
708 |
+ |
if (nunflshed == 0) |
709 |
+ |
return(0); |
710 |
+ |
nunflshed = 0; |
711 |
+ |
return(fflush(ambfp)); |
712 |
+ |
} |
713 |
+ |
|
714 |
+ |
#endif |