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