| 1 |
greg |
2.15 |
/* Copyright (c) 1993 Regents of the University of California */ |
| 2 |
greg |
1.1 |
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include "ray.h" |
| 12 |
|
|
|
| 13 |
|
|
#include "octree.h" |
| 14 |
|
|
|
| 15 |
greg |
1.8 |
#include "otypes.h" |
| 16 |
|
|
|
| 17 |
greg |
1.14 |
#include "ambient.h" |
| 18 |
|
|
|
| 19 |
greg |
1.1 |
#include "random.h" |
| 20 |
|
|
|
| 21 |
greg |
2.12 |
#define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */ |
| 22 |
greg |
1.1 |
|
| 23 |
greg |
1.14 |
typedef struct ambtree { |
| 24 |
greg |
2.12 |
AMBVAL *alist; /* ambient value list */ |
| 25 |
|
|
struct ambtree *kid; /* 8 child nodes */ |
| 26 |
greg |
1.14 |
} AMBTREE; /* ambient octree */ |
| 27 |
|
|
|
| 28 |
greg |
1.1 |
extern CUBE thescene; /* contains space boundaries */ |
| 29 |
|
|
|
| 30 |
greg |
2.12 |
#define MAXASET 511 /* maximum number of elements in ambient set */ |
| 31 |
|
|
OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */ |
| 32 |
greg |
1.1 |
|
| 33 |
greg |
2.12 |
double maxarad; /* maximum ambient radius */ |
| 34 |
|
|
double minarad; /* minimum ambient radius */ |
| 35 |
greg |
1.1 |
|
| 36 |
greg |
2.12 |
static AMBTREE atrunk; /* our ambient trunk node */ |
| 37 |
greg |
1.1 |
|
| 38 |
|
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
| 39 |
greg |
2.16 |
static int nunflshed = 0; /* number of unflushed ambient values */ |
| 40 |
greg |
1.1 |
|
| 41 |
greg |
2.12 |
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
| 42 |
greg |
2.6 |
|
| 43 |
greg |
2.12 |
#define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL)) |
| 44 |
greg |
1.1 |
|
| 45 |
greg |
2.12 |
#define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE)) |
| 46 |
greg |
2.20 |
#define freeambtree(t) free((char *)(t)) |
| 47 |
greg |
1.1 |
|
| 48 |
greg |
2.7 |
extern long ftell(), lseek(); |
| 49 |
greg |
2.21 |
static int initambfile(), avsave(), avinsert(), loadatree(); |
| 50 |
greg |
2.20 |
static AMBVAL *avstore(); |
| 51 |
greg |
2.19 |
#ifdef F_SETLKW |
| 52 |
|
|
static aflock(); |
| 53 |
|
|
#endif |
| 54 |
greg |
1.1 |
|
| 55 |
greg |
2.7 |
|
| 56 |
greg |
2.3 |
setambres(ar) /* set ambient resolution */ |
| 57 |
|
|
int ar; |
| 58 |
|
|
{ |
| 59 |
greg |
2.21 |
ambres = ar < 0 ? 0 : ar; /* may be done already */ |
| 60 |
greg |
2.3 |
/* set min & max radii */ |
| 61 |
|
|
if (ar <= 0) { |
| 62 |
|
|
minarad = 0.0; |
| 63 |
|
|
maxarad = thescene.cusize / 2.0; |
| 64 |
|
|
} else { |
| 65 |
|
|
minarad = thescene.cusize / ar; |
| 66 |
|
|
maxarad = 16.0 * minarad; /* heuristic */ |
| 67 |
|
|
if (maxarad > thescene.cusize / 2.0) |
| 68 |
|
|
maxarad = thescene.cusize / 2.0; |
| 69 |
|
|
} |
| 70 |
greg |
2.4 |
if (maxarad <= FTINY) |
| 71 |
|
|
maxarad = .001; |
| 72 |
greg |
2.3 |
} |
| 73 |
|
|
|
| 74 |
|
|
|
| 75 |
greg |
2.21 |
setambacc(newa) /* set ambient accuracy */ |
| 76 |
greg |
2.20 |
double newa; |
| 77 |
|
|
{ |
| 78 |
greg |
2.21 |
static double oldambacc = -1.0; |
| 79 |
greg |
2.20 |
AMBTREE oldatrunk; |
| 80 |
|
|
|
| 81 |
greg |
2.21 |
ambacc = newa < 0.0 ? 0.0 : newa; /* may be done already */ |
| 82 |
|
|
if (oldambacc < -FTINY) |
| 83 |
|
|
oldambacc = ambacc; /* do nothing first call */ |
| 84 |
|
|
if (fabs(newa - oldambacc) < 0.01) |
| 85 |
greg |
2.20 |
return; /* insignificant -- don't bother */ |
| 86 |
|
|
if (ambacc <= FTINY) |
| 87 |
|
|
return; /* cannot build new tree */ |
| 88 |
|
|
/* else need to rebuild tree */ |
| 89 |
|
|
copystruct(&oldatrunk, &atrunk); |
| 90 |
|
|
atrunk.alist = NULL; |
| 91 |
|
|
atrunk.kid = NULL; |
| 92 |
greg |
2.21 |
loadatree(&oldatrunk); |
| 93 |
|
|
oldambacc = ambacc; /* remeber setting for next call */ |
| 94 |
greg |
2.20 |
} |
| 95 |
|
|
|
| 96 |
|
|
|
| 97 |
greg |
1.1 |
setambient(afile) /* initialize calculation */ |
| 98 |
|
|
char *afile; |
| 99 |
|
|
{ |
| 100 |
greg |
2.9 |
long headlen; |
| 101 |
greg |
2.12 |
AMBVAL amb; |
| 102 |
greg |
2.3 |
/* init ambient limits */ |
| 103 |
|
|
setambres(ambres); |
| 104 |
greg |
2.21 |
setambacc(ambacc); |
| 105 |
greg |
2.19 |
if (afile == NULL) |
| 106 |
|
|
return; |
| 107 |
greg |
2.20 |
if (ambacc <= FTINY) { |
| 108 |
greg |
2.21 |
sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", |
| 109 |
greg |
2.20 |
afile); |
| 110 |
|
|
error(WARNING, errmsg); |
| 111 |
|
|
return; |
| 112 |
|
|
} |
| 113 |
greg |
2.3 |
/* open ambient file */ |
| 114 |
greg |
2.19 |
if ((ambfp = fopen(afile, "r+")) != NULL) { |
| 115 |
|
|
initambfile(0); |
| 116 |
|
|
headlen = ftell(ambfp); |
| 117 |
|
|
while (readambval(&amb, ambfp)) |
| 118 |
greg |
2.21 |
avinsert(avstore(&amb)); |
| 119 |
greg |
2.19 |
/* align */ |
| 120 |
|
|
fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1); |
| 121 |
|
|
} else if ((ambfp = fopen(afile, "w+")) != NULL) |
| 122 |
|
|
initambfile(1); |
| 123 |
|
|
else { |
| 124 |
|
|
sprintf(errmsg, "cannot open ambient file \"%s\"", afile); |
| 125 |
|
|
error(SYSTEM, errmsg); |
| 126 |
greg |
2.16 |
} |
| 127 |
greg |
2.19 |
nunflshed++; /* lie */ |
| 128 |
|
|
ambsync(); |
| 129 |
greg |
1.8 |
} |
| 130 |
|
|
|
| 131 |
|
|
|
| 132 |
|
|
ambnotify(obj) /* record new modifier */ |
| 133 |
greg |
2.12 |
OBJECT obj; |
| 134 |
greg |
1.8 |
{ |
| 135 |
greg |
1.11 |
static int hitlimit = 0; |
| 136 |
greg |
2.12 |
register OBJREC *o = objptr(obj); |
| 137 |
greg |
1.8 |
register char **amblp; |
| 138 |
|
|
|
| 139 |
greg |
1.11 |
if (hitlimit || !ismodifier(o->otype)) |
| 140 |
greg |
1.8 |
return; |
| 141 |
|
|
for (amblp = amblist; *amblp != NULL; amblp++) |
| 142 |
|
|
if (!strcmp(o->oname, *amblp)) { |
| 143 |
greg |
1.11 |
if (ambset[0] >= MAXASET) { |
| 144 |
|
|
error(WARNING, "too many modifiers in ambient list"); |
| 145 |
|
|
hitlimit++; |
| 146 |
|
|
return; /* should this be fatal? */ |
| 147 |
|
|
} |
| 148 |
greg |
1.8 |
insertelem(ambset, obj); |
| 149 |
|
|
return; |
| 150 |
greg |
1.1 |
} |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
|
|
| 154 |
|
|
ambient(acol, r) /* compute ambient component for ray */ |
| 155 |
|
|
COLOR acol; |
| 156 |
|
|
register RAY *r; |
| 157 |
|
|
{ |
| 158 |
|
|
static int rdepth = 0; /* ambient recursion */ |
| 159 |
greg |
2.12 |
double d; |
| 160 |
greg |
1.1 |
|
| 161 |
|
|
if (ambdiv <= 0) /* no ambient calculation */ |
| 162 |
|
|
goto dumbamb; |
| 163 |
|
|
/* check number of bounces */ |
| 164 |
greg |
1.16 |
if (rdepth >= ambounce) |
| 165 |
greg |
1.1 |
goto dumbamb; |
| 166 |
|
|
/* check ambient list */ |
| 167 |
|
|
if (ambincl != -1 && r->ro != NULL && |
| 168 |
|
|
ambincl != inset(ambset, r->ro->omod)) |
| 169 |
|
|
goto dumbamb; |
| 170 |
|
|
|
| 171 |
|
|
if (ambacc <= FTINY) { /* no ambient storage */ |
| 172 |
greg |
1.16 |
rdepth++; |
| 173 |
|
|
d = doambient(acol, r, r->rweight, NULL, NULL); |
| 174 |
|
|
rdepth--; |
| 175 |
|
|
if (d == 0.0) |
| 176 |
greg |
1.1 |
goto dumbamb; |
| 177 |
greg |
1.16 |
return; |
| 178 |
greg |
1.1 |
} |
| 179 |
|
|
/* get ambient value */ |
| 180 |
|
|
setcolor(acol, 0.0, 0.0, 0.0); |
| 181 |
greg |
1.16 |
d = sumambient(acol, r, rdepth, |
| 182 |
|
|
&atrunk, thescene.cuorg, thescene.cusize); |
| 183 |
|
|
if (d > FTINY) |
| 184 |
|
|
scalecolor(acol, 1.0/d); |
| 185 |
|
|
else { |
| 186 |
|
|
d = makeambient(acol, r, rdepth++); |
| 187 |
|
|
rdepth--; |
| 188 |
|
|
} |
| 189 |
|
|
if (d > FTINY) |
| 190 |
|
|
return; |
| 191 |
greg |
1.1 |
dumbamb: /* return global value */ |
| 192 |
|
|
copycolor(acol, ambval); |
| 193 |
|
|
} |
| 194 |
|
|
|
| 195 |
|
|
|
| 196 |
|
|
double |
| 197 |
greg |
1.16 |
sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */ |
| 198 |
greg |
1.1 |
COLOR acol; |
| 199 |
|
|
register RAY *r; |
| 200 |
greg |
1.16 |
int al; |
| 201 |
greg |
2.12 |
AMBTREE *at; |
| 202 |
greg |
1.1 |
FVECT c0; |
| 203 |
greg |
2.12 |
double s; |
| 204 |
greg |
1.1 |
{ |
| 205 |
greg |
2.12 |
double d, e1, e2, wt, wsum; |
| 206 |
greg |
1.1 |
COLOR ct; |
| 207 |
|
|
FVECT ck0; |
| 208 |
|
|
int i; |
| 209 |
|
|
register int j; |
| 210 |
greg |
2.12 |
register AMBVAL *av; |
| 211 |
greg |
1.7 |
/* do this node */ |
| 212 |
greg |
1.1 |
wsum = 0.0; |
| 213 |
|
|
for (av = at->alist; av != NULL; av = av->next) { |
| 214 |
|
|
/* |
| 215 |
greg |
1.16 |
* Ambient level test. |
| 216 |
greg |
1.1 |
*/ |
| 217 |
greg |
1.16 |
if (av->lvl > al || av->weight < r->rweight-FTINY) |
| 218 |
greg |
1.1 |
continue; |
| 219 |
|
|
/* |
| 220 |
|
|
* Ambient radius test. |
| 221 |
|
|
*/ |
| 222 |
|
|
e1 = 0.0; |
| 223 |
|
|
for (j = 0; j < 3; j++) { |
| 224 |
|
|
d = av->pos[j] - r->rop[j]; |
| 225 |
|
|
e1 += d * d; |
| 226 |
|
|
} |
| 227 |
|
|
e1 /= av->rad * av->rad; |
| 228 |
|
|
if (e1 > ambacc*ambacc*1.21) |
| 229 |
|
|
continue; |
| 230 |
|
|
/* |
| 231 |
|
|
* Normal direction test. |
| 232 |
|
|
*/ |
| 233 |
|
|
e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight; |
| 234 |
|
|
if (e2 < 0.0) e2 = 0.0; |
| 235 |
|
|
if (e1 + e2 > ambacc*ambacc*1.21) |
| 236 |
|
|
continue; |
| 237 |
|
|
/* |
| 238 |
|
|
* Ray behind test. |
| 239 |
|
|
*/ |
| 240 |
|
|
d = 0.0; |
| 241 |
|
|
for (j = 0; j < 3; j++) |
| 242 |
|
|
d += (r->rop[j] - av->pos[j]) * |
| 243 |
|
|
(av->dir[j] + r->ron[j]); |
| 244 |
greg |
1.18 |
if (d*0.5 < -minarad*ambacc-.001) |
| 245 |
greg |
1.1 |
continue; |
| 246 |
|
|
/* |
| 247 |
|
|
* Jittering final test reduces image artifacts. |
| 248 |
|
|
*/ |
| 249 |
|
|
wt = sqrt(e1) + sqrt(e2); |
| 250 |
greg |
2.2 |
wt *= .9 + .2*urand(9015+samplendx); |
| 251 |
greg |
1.6 |
if (wt > ambacc) |
| 252 |
greg |
1.1 |
continue; |
| 253 |
|
|
if (wt <= 1e-3) |
| 254 |
|
|
wt = 1e3; |
| 255 |
|
|
else |
| 256 |
|
|
wt = 1.0 / wt; |
| 257 |
|
|
wsum += wt; |
| 258 |
greg |
1.15 |
extambient(ct, av, r->rop, r->ron); |
| 259 |
greg |
1.1 |
scalecolor(ct, wt); |
| 260 |
|
|
addcolor(acol, ct); |
| 261 |
greg |
1.7 |
} |
| 262 |
|
|
if (at->kid == NULL) |
| 263 |
|
|
return(wsum); |
| 264 |
|
|
/* do children */ |
| 265 |
|
|
s *= 0.5; |
| 266 |
|
|
for (i = 0; i < 8; i++) { |
| 267 |
|
|
for (j = 0; j < 3; j++) { |
| 268 |
|
|
ck0[j] = c0[j]; |
| 269 |
|
|
if (1<<j & i) |
| 270 |
|
|
ck0[j] += s; |
| 271 |
|
|
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
| 272 |
|
|
break; |
| 273 |
|
|
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
| 274 |
|
|
break; |
| 275 |
|
|
} |
| 276 |
|
|
if (j == 3) |
| 277 |
greg |
1.16 |
wsum += sumambient(acol, r, al, at->kid+i, ck0, s); |
| 278 |
greg |
1.1 |
} |
| 279 |
|
|
return(wsum); |
| 280 |
|
|
} |
| 281 |
|
|
|
| 282 |
|
|
|
| 283 |
|
|
double |
| 284 |
greg |
1.16 |
makeambient(acol, r, al) /* make a new ambient value */ |
| 285 |
greg |
1.1 |
COLOR acol; |
| 286 |
|
|
register RAY *r; |
| 287 |
greg |
1.16 |
int al; |
| 288 |
greg |
1.1 |
{ |
| 289 |
greg |
2.12 |
AMBVAL amb; |
| 290 |
greg |
1.14 |
FVECT gp, gd; |
| 291 |
greg |
1.16 |
/* compute weight */ |
| 292 |
|
|
amb.weight = pow(AVGREFL, (double)al); |
| 293 |
greg |
1.17 |
if (r->rweight < 0.2*amb.weight) /* heuristic */ |
| 294 |
greg |
1.16 |
amb.weight = r->rweight; |
| 295 |
|
|
/* compute ambient */ |
| 296 |
|
|
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
| 297 |
greg |
1.1 |
if (amb.rad == 0.0) |
| 298 |
|
|
return(0.0); |
| 299 |
|
|
/* store it */ |
| 300 |
|
|
VCOPY(amb.pos, r->rop); |
| 301 |
|
|
VCOPY(amb.dir, r->ron); |
| 302 |
greg |
1.16 |
amb.lvl = al; |
| 303 |
greg |
1.1 |
copycolor(amb.val, acol); |
| 304 |
greg |
1.14 |
VCOPY(amb.gpos, gp); |
| 305 |
|
|
VCOPY(amb.gdir, gd); |
| 306 |
greg |
1.1 |
/* insert into tree */ |
| 307 |
greg |
2.7 |
avsave(&amb); /* and save to file */ |
| 308 |
greg |
1.1 |
return(amb.rad); |
| 309 |
greg |
1.15 |
} |
| 310 |
|
|
|
| 311 |
|
|
|
| 312 |
|
|
extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */ |
| 313 |
|
|
COLOR cr; |
| 314 |
greg |
2.12 |
register AMBVAL *ap; |
| 315 |
greg |
1.15 |
FVECT pv, nv; |
| 316 |
|
|
{ |
| 317 |
|
|
FVECT v1, v2; |
| 318 |
|
|
register int i; |
| 319 |
greg |
2.12 |
double d; |
| 320 |
greg |
1.15 |
|
| 321 |
|
|
d = 1.0; /* zeroeth order */ |
| 322 |
|
|
/* gradient due to translation */ |
| 323 |
|
|
for (i = 0; i < 3; i++) |
| 324 |
|
|
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
| 325 |
|
|
/* gradient due to rotation */ |
| 326 |
|
|
VCOPY(v1, ap->dir); |
| 327 |
|
|
fcross(v2, v1, nv); |
| 328 |
|
|
d += DOT(ap->gdir, v2); |
| 329 |
|
|
if (d <= 0.0) { |
| 330 |
|
|
setcolor(cr, 0.0, 0.0, 0.0); |
| 331 |
|
|
return; |
| 332 |
|
|
} |
| 333 |
|
|
copycolor(cr, ap->val); |
| 334 |
|
|
scalecolor(cr, d); |
| 335 |
greg |
1.1 |
} |
| 336 |
|
|
|
| 337 |
|
|
|
| 338 |
|
|
static |
| 339 |
greg |
2.9 |
initambfile(creat) /* initialize ambient file */ |
| 340 |
|
|
int creat; |
| 341 |
|
|
{ |
| 342 |
|
|
extern char *progname, *octname, VersionID[]; |
| 343 |
|
|
|
| 344 |
greg |
2.19 |
#ifdef F_SETLKW |
| 345 |
|
|
aflock(creat ? F_WRLCK : F_RDLCK); |
| 346 |
|
|
#endif |
| 347 |
greg |
2.12 |
#ifdef MSDOS |
| 348 |
|
|
setmode(fileno(ambfp), O_BINARY); |
| 349 |
|
|
#endif |
| 350 |
greg |
2.21 |
setbuf(ambfp, bmalloc(BUFSIZ+8)); |
| 351 |
greg |
2.9 |
if (creat) { /* new file */ |
| 352 |
|
|
fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ", |
| 353 |
|
|
progname, colval(ambval,RED), |
| 354 |
|
|
colval(ambval,GRN), colval(ambval,BLU), |
| 355 |
|
|
ambounce, ambacc); |
| 356 |
|
|
fprintf(ambfp, "-ad %d -as %d -ar %d %s\n", |
| 357 |
|
|
ambdiv, ambssamp, ambres, |
| 358 |
|
|
octname==NULL ? "" : octname); |
| 359 |
|
|
fprintf(ambfp, "SOFTWARE= %s\n", VersionID); |
| 360 |
|
|
fputformat(AMBFMT, ambfp); |
| 361 |
|
|
putc('\n', ambfp); |
| 362 |
|
|
putambmagic(ambfp); |
| 363 |
greg |
2.17 |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
| 364 |
|
|
error(USER, "bad ambient file"); |
| 365 |
greg |
2.9 |
} |
| 366 |
|
|
|
| 367 |
|
|
|
| 368 |
|
|
static |
| 369 |
greg |
2.7 |
avsave(av) /* insert and save an ambient value */ |
| 370 |
greg |
2.12 |
AMBVAL *av; |
| 371 |
greg |
1.1 |
{ |
| 372 |
greg |
2.21 |
avinsert(avstore(av)); |
| 373 |
greg |
1.1 |
if (ambfp == NULL) |
| 374 |
|
|
return; |
| 375 |
greg |
2.5 |
if (writambval(av, ambfp) < 0) |
| 376 |
greg |
1.1 |
goto writerr; |
| 377 |
greg |
2.16 |
if (++nunflshed >= AMBFLUSH) |
| 378 |
greg |
2.7 |
if (ambsync() == EOF) |
| 379 |
greg |
1.1 |
goto writerr; |
| 380 |
|
|
return; |
| 381 |
|
|
writerr: |
| 382 |
greg |
2.17 |
error(SYSTEM, "error writing ambient file"); |
| 383 |
greg |
1.1 |
} |
| 384 |
|
|
|
| 385 |
|
|
|
| 386 |
greg |
2.20 |
static AMBVAL * |
| 387 |
|
|
avstore(aval) /* allocate memory and store aval */ |
| 388 |
|
|
register AMBVAL *aval; |
| 389 |
|
|
{ |
| 390 |
|
|
register AMBVAL *av; |
| 391 |
|
|
|
| 392 |
|
|
if ((av = newambval()) == NULL) |
| 393 |
|
|
error(SYSTEM, "out of memory in avstore"); |
| 394 |
|
|
copystruct(av, aval); |
| 395 |
|
|
return(av); |
| 396 |
|
|
} |
| 397 |
|
|
|
| 398 |
|
|
|
| 399 |
greg |
1.1 |
static |
| 400 |
greg |
2.21 |
avinsert(av) /* insert ambient value in our tree */ |
| 401 |
greg |
2.20 |
register AMBVAL *av; |
| 402 |
greg |
1.1 |
{ |
| 403 |
greg |
2.21 |
register AMBTREE *at; |
| 404 |
greg |
1.1 |
FVECT ck0; |
| 405 |
greg |
2.21 |
double s; |
| 406 |
greg |
1.1 |
int branch; |
| 407 |
|
|
register int i; |
| 408 |
|
|
|
| 409 |
greg |
2.20 |
if (av->rad <= FTINY) |
| 410 |
|
|
error(CONSISTENCY, "zero ambient radius in avinsert"); |
| 411 |
greg |
2.21 |
at = &atrunk; |
| 412 |
|
|
VCOPY(ck0, thescene.cuorg); |
| 413 |
|
|
s = thescene.cusize; |
| 414 |
greg |
1.1 |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
| 415 |
|
|
if (at->kid == NULL) |
| 416 |
|
|
if ((at->kid = newambtree()) == NULL) |
| 417 |
greg |
2.20 |
error(SYSTEM, "out of memory in avinsert"); |
| 418 |
greg |
1.1 |
s *= 0.5; |
| 419 |
|
|
branch = 0; |
| 420 |
|
|
for (i = 0; i < 3; i++) |
| 421 |
|
|
if (av->pos[i] > ck0[i] + s) { |
| 422 |
|
|
ck0[i] += s; |
| 423 |
|
|
branch |= 1 << i; |
| 424 |
|
|
} |
| 425 |
|
|
at = at->kid + branch; |
| 426 |
|
|
} |
| 427 |
|
|
av->next = at->alist; |
| 428 |
|
|
at->alist = av; |
| 429 |
greg |
2.7 |
} |
| 430 |
|
|
|
| 431 |
|
|
|
| 432 |
greg |
2.20 |
static |
| 433 |
greg |
2.21 |
loadatree(at) /* move tree to main store */ |
| 434 |
greg |
2.20 |
register AMBTREE *at; |
| 435 |
|
|
{ |
| 436 |
|
|
register AMBVAL *av; |
| 437 |
|
|
register int i; |
| 438 |
|
|
/* transfer values at this node */ |
| 439 |
|
|
for (av = at->alist; av != NULL; av = at->alist) { |
| 440 |
|
|
at->alist = av->next; |
| 441 |
greg |
2.21 |
avinsert(av); |
| 442 |
greg |
2.20 |
} |
| 443 |
greg |
2.21 |
if (at->kid == NULL) |
| 444 |
|
|
return; |
| 445 |
greg |
2.20 |
for (i = 0; i < 8; i++) /* transfer and free children */ |
| 446 |
greg |
2.21 |
loadatree(at->kid+i); |
| 447 |
greg |
2.20 |
freeambtree(at->kid); |
| 448 |
|
|
} |
| 449 |
|
|
|
| 450 |
|
|
|
| 451 |
greg |
2.18 |
#ifdef F_SETLKW |
| 452 |
greg |
2.10 |
|
| 453 |
greg |
2.19 |
static |
| 454 |
|
|
aflock(typ) /* lock/unlock ambient file */ |
| 455 |
|
|
int typ; |
| 456 |
|
|
{ |
| 457 |
|
|
static struct flock fls; /* static so initialized to zeroes */ |
| 458 |
|
|
|
| 459 |
|
|
fls.l_type = typ; |
| 460 |
|
|
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
| 461 |
|
|
error(SYSTEM, "cannot (un)lock ambient file"); |
| 462 |
|
|
} |
| 463 |
|
|
|
| 464 |
|
|
|
| 465 |
greg |
2.16 |
int |
| 466 |
greg |
2.7 |
ambsync() /* synchronize ambient file */ |
| 467 |
|
|
{ |
| 468 |
|
|
static FILE *ambinp = NULL; |
| 469 |
greg |
2.15 |
static long lastpos = -1; |
| 470 |
|
|
long flen; |
| 471 |
greg |
2.12 |
AMBVAL avs; |
| 472 |
greg |
2.7 |
register int n; |
| 473 |
greg |
2.16 |
|
| 474 |
|
|
if (nunflshed == 0) |
| 475 |
|
|
return(0); |
| 476 |
greg |
2.19 |
if (lastpos < 0) /* initializing (locked in initambfile) */ |
| 477 |
|
|
goto syncend; |
| 478 |
greg |
2.7 |
/* gain exclusive access */ |
| 479 |
greg |
2.19 |
aflock(F_WRLCK); |
| 480 |
greg |
2.7 |
/* see if file has grown */ |
| 481 |
greg |
2.15 |
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
| 482 |
greg |
2.21 |
goto seekerr; |
| 483 |
greg |
2.15 |
if (n = flen - lastpos) { /* file has grown */ |
| 484 |
greg |
2.17 |
if (ambinp == NULL) { /* use duplicate filedes */ |
| 485 |
|
|
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
| 486 |
greg |
2.14 |
if (ambinp == NULL) |
| 487 |
greg |
2.17 |
error(SYSTEM, "fdopen failed in ambsync"); |
| 488 |
greg |
2.14 |
} |
| 489 |
greg |
2.15 |
if (fseek(ambinp, lastpos, 0) < 0) |
| 490 |
greg |
2.21 |
goto seekerr; |
| 491 |
greg |
2.15 |
while (n >= AMBVALSIZ) { /* load contributed values */ |
| 492 |
greg |
2.7 |
readambval(&avs, ambinp); |
| 493 |
greg |
2.21 |
avinsert(avstore(&avs)); |
| 494 |
greg |
2.15 |
n -= AMBVALSIZ; |
| 495 |
|
|
} |
| 496 |
|
|
if (n) /* alignment */ |
| 497 |
greg |
2.21 |
if (lseek(fileno(ambfp), flen-n, 0) < 0) |
| 498 |
|
|
goto seekerr; |
| 499 |
greg |
2.7 |
} |
| 500 |
greg |
2.21 |
#ifdef DEBUG |
| 501 |
|
|
if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { |
| 502 |
|
|
sprintf(errmsg, "ambient file buffer at %d rather than %d", |
| 503 |
|
|
ambfp->_ptr - ambfp->_base, |
| 504 |
|
|
nunflshed*AMBVALSIZ); |
| 505 |
|
|
error(CONSISTENCY, errmsg); |
| 506 |
|
|
} |
| 507 |
|
|
#endif |
| 508 |
greg |
2.15 |
syncend: |
| 509 |
greg |
2.7 |
n = fflush(ambfp); /* calls write() at last */ |
| 510 |
greg |
2.21 |
if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0) |
| 511 |
|
|
goto seekerr; |
| 512 |
greg |
2.19 |
aflock(F_UNLCK); /* release file */ |
| 513 |
greg |
2.16 |
nunflshed = 0; |
| 514 |
greg |
2.7 |
return(n); |
| 515 |
greg |
2.21 |
seekerr: |
| 516 |
|
|
error(SYSTEM, "seek failed in ambsync"); |
| 517 |
greg |
2.18 |
} |
| 518 |
|
|
|
| 519 |
|
|
#else |
| 520 |
|
|
|
| 521 |
|
|
int |
| 522 |
|
|
ambsync() /* flush ambient file */ |
| 523 |
|
|
{ |
| 524 |
|
|
if (nunflshed == 0) |
| 525 |
|
|
return(0); |
| 526 |
|
|
nunflshed = 0; |
| 527 |
|
|
return(fflush(ambfp)); |
| 528 |
greg |
1.1 |
} |
| 529 |
greg |
2.10 |
|
| 530 |
|
|
#endif |