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