| 1 | greg | 1.1 | #ifndef lint | 
| 2 | greg | 2.65 | static const char       RCSid[] = "$Id: ambient.c,v 2.64 2007/09/14 00:50:31 greg Exp $"; | 
| 3 | greg | 1.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | *  ambient.c - routines dealing with ambient (inter-reflected) component. | 
| 6 | greg | 2.47 | * | 
| 7 |  |  | *  Declarations of external symbols in ambient.h | 
| 8 |  |  | */ | 
| 9 |  |  |  | 
| 10 | greg | 2.48 | #include "copyright.h" | 
| 11 | greg | 1.1 |  | 
| 12 | schorsch | 2.52 | #include <string.h> | 
| 13 |  |  |  | 
| 14 | schorsch | 2.50 | #include  "platform.h" | 
| 15 | greg | 1.1 | #include  "ray.h" | 
| 16 | greg | 1.8 | #include  "otypes.h" | 
| 17 | schorsch | 2.56 | #include  "resolu.h" | 
| 18 | greg | 1.14 | #include  "ambient.h" | 
| 19 | greg | 1.1 | #include  "random.h" | 
| 20 |  |  |  | 
| 21 | greg | 2.32 | #ifndef  OCTSCALE | 
| 22 | greg | 2.29 | #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */ | 
| 23 | greg | 2.32 | #endif | 
| 24 | greg | 1.1 |  | 
| 25 | greg | 2.27 | extern char  *shm_boundary;     /* memory sharing boundary */ | 
| 26 | greg | 2.26 |  | 
| 27 | greg | 2.57 | #ifndef  MAXASET | 
| 28 |  |  | #define  MAXASET        2047    /* maximum number of elements in ambient set */ | 
| 29 |  |  | #endif | 
| 30 | greg | 2.12 | OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */ | 
| 31 | greg | 1.1 |  | 
| 32 | greg | 2.12 | double  maxarad;                /* maximum ambient radius */ | 
| 33 |  |  | double  minarad;                /* minimum ambient radius */ | 
| 34 | greg | 1.1 |  | 
| 35 | greg | 2.12 | static AMBTREE  atrunk;         /* our ambient trunk node */ | 
| 36 | greg | 1.1 |  | 
| 37 |  |  | static FILE  *ambfp = NULL;     /* ambient file pointer */ | 
| 38 | greg | 2.16 | static int  nunflshed = 0;      /* number of unflushed ambient values */ | 
| 39 | greg | 1.1 |  | 
| 40 | greg | 2.26 | #ifndef SORT_THRESH | 
| 41 | greg | 2.49 | #ifdef SMLMEM | 
| 42 |  |  | #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL)) | 
| 43 |  |  | #else | 
| 44 | greg | 2.27 | #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL)) | 
| 45 | greg | 2.26 | #endif | 
| 46 |  |  | #endif | 
| 47 |  |  | #ifndef SORT_INTVL | 
| 48 | greg | 2.41 | #define SORT_INTVL      (SORT_THRESH<<1) | 
| 49 | greg | 2.26 | #endif | 
| 50 | greg | 2.28 | #ifndef MAX_SORT_INTVL | 
| 51 | greg | 2.41 | #define MAX_SORT_INTVL  (SORT_INTVL<<6) | 
| 52 | greg | 2.28 | #endif | 
| 53 | greg | 2.26 |  | 
| 54 | gregl | 2.43 | static double  avsum = 0.;              /* computed ambient value sum (log) */ | 
| 55 |  |  | static unsigned int  navsum = 0;        /* number of values in avsum */ | 
| 56 | greg | 2.35 | static unsigned int  nambvals = 0;      /* total number of indirect values */ | 
| 57 |  |  | static unsigned int  nambshare = 0;     /* number of values from file */ | 
| 58 | greg | 2.27 | static unsigned long  ambclock = 0;     /* ambient access clock */ | 
| 59 |  |  | static unsigned long  lastsort = 0;     /* time of last value sort */ | 
| 60 | greg | 2.26 | static long  sortintvl = SORT_INTVL;    /* time until next sort */ | 
| 61 | greg | 2.47 | static FILE  *ambinp = NULL;            /* auxiliary file for input */ | 
| 62 |  |  | static long  lastpos = -1;              /* last flush position */ | 
| 63 | greg | 2.26 |  | 
| 64 |  |  | #define MAXACLOCK       (1L<<30)        /* clock turnover value */ | 
| 65 | greg | 2.27 | /* | 
| 66 |  |  | * Track access times unless we are sharing ambient values | 
| 67 |  |  | * through memory on a multiprocessor, when we want to avoid | 
| 68 | greg | 2.35 | * claiming our own memory (copy on write).  Go ahead anyway | 
| 69 |  |  | * if more than two thirds of our values are unshared. | 
| 70 | gregl | 2.43 | * Compile with -Dtracktime=0 to turn this code off. | 
| 71 | greg | 2.27 | */ | 
| 72 | gregl | 2.43 | #ifndef tracktime | 
| 73 | greg | 2.35 | #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare) | 
| 74 | gregl | 2.43 | #endif | 
| 75 | greg | 2.26 |  | 
| 76 | greg | 2.12 | #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ) | 
| 77 | greg | 2.6 |  | 
| 78 | greg | 2.47 | #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL)) | 
| 79 |  |  | #define  freeav(av)     free((void *)av); | 
| 80 | greg | 1.1 |  | 
| 81 | schorsch | 2.56 | static void initambfile(int creat); | 
| 82 |  |  | static void avsave(AMBVAL *av); | 
| 83 |  |  | static AMBVAL *avstore(AMBVAL  *aval); | 
| 84 |  |  | static AMBTREE *newambtree(void); | 
| 85 |  |  | static void freeambtree(AMBTREE  *atp); | 
| 86 |  |  |  | 
| 87 |  |  | typedef void unloadtf_t(void *); | 
| 88 |  |  | static unloadtf_t avinsert; | 
| 89 |  |  | static unloadtf_t av2list; | 
| 90 |  |  | static void unloadatree(AMBTREE  *at, unloadtf_t *f); | 
| 91 |  |  |  | 
| 92 |  |  | static int aposcmp(const void *avp1, const void *avp2); | 
| 93 |  |  | static int avlmemi(AMBVAL *avaddr); | 
| 94 |  |  | static void sortambvals(int always); | 
| 95 |  |  |  | 
| 96 | greg | 2.19 | #ifdef  F_SETLKW | 
| 97 | schorsch | 2.56 | static void aflock(int  typ); | 
| 98 | greg | 2.19 | #endif | 
| 99 | greg | 1.1 |  | 
| 100 | greg | 2.7 |  | 
| 101 | schorsch | 2.56 | extern void | 
| 102 |  |  | setambres(                              /* set ambient resolution */ | 
| 103 |  |  | int  ar | 
| 104 |  |  | ) | 
| 105 | greg | 2.3 | { | 
| 106 | greg | 2.21 | ambres = ar < 0 ? 0 : ar;               /* may be done already */ | 
| 107 | greg | 2.3 | /* set min & max radii */ | 
| 108 |  |  | if (ar <= 0) { | 
| 109 | greg | 2.25 | minarad = 0; | 
| 110 | greg | 2.3 | maxarad = thescene.cusize / 2.0; | 
| 111 |  |  | } else { | 
| 112 |  |  | minarad = thescene.cusize / ar; | 
| 113 | greg | 2.30 | maxarad = 64 * minarad;                 /* heuristic */ | 
| 114 | greg | 2.3 | if (maxarad > thescene.cusize / 2.0) | 
| 115 |  |  | maxarad = thescene.cusize / 2.0; | 
| 116 |  |  | } | 
| 117 | greg | 2.25 | if (minarad <= FTINY) | 
| 118 |  |  | minarad = 10*FTINY; | 
| 119 |  |  | if (maxarad <= minarad) | 
| 120 |  |  | maxarad = 64 * minarad; | 
| 121 | greg | 2.3 | } | 
| 122 |  |  |  | 
| 123 |  |  |  | 
| 124 | schorsch | 2.56 | extern void | 
| 125 |  |  | setambacc(                              /* set ambient accuracy */ | 
| 126 |  |  | double  newa | 
| 127 |  |  | ) | 
| 128 | greg | 2.20 | { | 
| 129 | greg | 2.33 | double  ambdiff; | 
| 130 | greg | 2.20 |  | 
| 131 | greg | 2.33 | if (newa < 0.0) | 
| 132 |  |  | newa = 0.0; | 
| 133 |  |  | ambdiff = fabs(newa - ambacc); | 
| 134 |  |  | if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) | 
| 135 |  |  | sortambvals(1);                 /* rebuild tree */ | 
| 136 | greg | 2.20 | } | 
| 137 |  |  |  | 
| 138 |  |  |  | 
| 139 | schorsch | 2.56 | extern void | 
| 140 |  |  | setambient(void)                                /* initialize calculation */ | 
| 141 | greg | 1.1 | { | 
| 142 | gwlarson | 2.46 | int     readonly = 0; | 
| 143 | greg | 2.37 | long  pos, flen; | 
| 144 | greg | 2.12 | AMBVAL  amb; | 
| 145 | greg | 2.47 | /* make sure we're fresh */ | 
| 146 |  |  | ambdone(); | 
| 147 | greg | 2.3 | /* init ambient limits */ | 
| 148 |  |  | setambres(ambres); | 
| 149 | greg | 2.21 | setambacc(ambacc); | 
| 150 | greg | 2.47 | if (ambfile == NULL || !ambfile[0]) | 
| 151 | greg | 2.19 | return; | 
| 152 | greg | 2.20 | if (ambacc <= FTINY) { | 
| 153 | greg | 2.21 | sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened", | 
| 154 | greg | 2.47 | ambfile); | 
| 155 | greg | 2.20 | error(WARNING, errmsg); | 
| 156 |  |  | return; | 
| 157 |  |  | } | 
| 158 | greg | 2.3 | /* open ambient file */ | 
| 159 | greg | 2.47 | if ((ambfp = fopen(ambfile, "r+")) == NULL) | 
| 160 |  |  | readonly = (ambfp = fopen(ambfile, "r")) != NULL; | 
| 161 | gwlarson | 2.46 | if (ambfp != NULL) { | 
| 162 |  |  | initambfile(0);                 /* file exists */ | 
| 163 | greg | 2.37 | pos = ftell(ambfp); | 
| 164 | greg | 2.19 | while (readambval(&amb, ambfp)) | 
| 165 | greg | 2.21 | avinsert(avstore(&amb)); | 
| 166 | gwlarson | 2.46 | nambshare = nambvals;           /* share loaded values */ | 
| 167 |  |  | if (readonly) { | 
| 168 |  |  | sprintf(errmsg, | 
| 169 |  |  | "loaded %u values from read-only ambient file", | 
| 170 |  |  | nambvals); | 
| 171 |  |  | error(WARNING, errmsg); | 
| 172 |  |  | fclose(ambfp);          /* close file so no writes */ | 
| 173 |  |  | ambfp = NULL; | 
| 174 |  |  | return;                 /* avoid ambsync() */ | 
| 175 |  |  | } | 
| 176 |  |  | /* align file pointer */ | 
| 177 | greg | 2.37 | pos += (long)nambvals*AMBVALSIZ; | 
| 178 | greg | 2.55 | flen = lseek(fileno(ambfp), (off_t)0, SEEK_END); | 
| 179 | greg | 2.37 | if (flen != pos) { | 
| 180 | greg | 2.42 | sprintf(errmsg, | 
| 181 | greg | 2.37 | "ignoring last %ld values in ambient file (corrupted)", | 
| 182 |  |  | (flen - pos)/AMBVALSIZ); | 
| 183 | greg | 2.42 | error(WARNING, errmsg); | 
| 184 | greg | 2.37 | fseek(ambfp, pos, 0); | 
| 185 | schorsch | 2.51 | #ifndef _WIN32 /* XXX we need a replacement for that one */ | 
| 186 | greg | 2.47 | ftruncate(fileno(ambfp), (off_t)pos); | 
| 187 | schorsch | 2.51 | #endif | 
| 188 | greg | 2.37 | } | 
| 189 | greg | 2.47 | } else if ((ambfp = fopen(ambfile, "w+")) != NULL) { | 
| 190 | gwlarson | 2.46 | initambfile(1);                 /* else create new file */ | 
| 191 |  |  | } else { | 
| 192 | greg | 2.47 | sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); | 
| 193 | greg | 2.19 | error(SYSTEM, errmsg); | 
| 194 | greg | 2.16 | } | 
| 195 | greg | 2.63 | ambsync();                      /* load previous values */ | 
| 196 | greg | 1.8 | } | 
| 197 |  |  |  | 
| 198 |  |  |  | 
| 199 | schorsch | 2.56 | extern void | 
| 200 |  |  | ambdone(void)                   /* close ambient file and free memory */ | 
| 201 | greg | 2.47 | { | 
| 202 |  |  | if (ambfp != NULL) {            /* close ambient file */ | 
| 203 |  |  | ambsync(); | 
| 204 |  |  | fclose(ambfp); | 
| 205 |  |  | ambfp = NULL; | 
| 206 |  |  | if (ambinp != NULL) { | 
| 207 |  |  | fclose(ambinp); | 
| 208 |  |  | ambinp = NULL; | 
| 209 |  |  | } | 
| 210 |  |  | lastpos = -1; | 
| 211 |  |  | } | 
| 212 |  |  | /* free ambient tree */ | 
| 213 |  |  | unloadatree(&atrunk, free); | 
| 214 |  |  | /* reset state variables */ | 
| 215 |  |  | avsum = 0.; | 
| 216 |  |  | navsum = 0; | 
| 217 |  |  | nambvals = 0; | 
| 218 |  |  | nambshare = 0; | 
| 219 |  |  | ambclock = 0; | 
| 220 |  |  | lastsort = 0; | 
| 221 |  |  | sortintvl = SORT_INTVL; | 
| 222 |  |  | } | 
| 223 |  |  |  | 
| 224 |  |  |  | 
| 225 | schorsch | 2.56 | extern void | 
| 226 |  |  | ambnotify(                      /* record new modifier */ | 
| 227 |  |  | OBJECT  obj | 
| 228 |  |  | ) | 
| 229 | greg | 1.8 | { | 
| 230 | greg | 1.11 | static int  hitlimit = 0; | 
| 231 | greg | 2.47 | register OBJREC  *o; | 
| 232 | greg | 1.8 | register char  **amblp; | 
| 233 |  |  |  | 
| 234 | greg | 2.47 | if (obj == OVOID) {             /* starting over */ | 
| 235 |  |  | ambset[0] = 0; | 
| 236 |  |  | hitlimit = 0; | 
| 237 |  |  | return; | 
| 238 |  |  | } | 
| 239 |  |  | o = objptr(obj); | 
| 240 | greg | 1.11 | if (hitlimit || !ismodifier(o->otype)) | 
| 241 | greg | 1.8 | return; | 
| 242 |  |  | for (amblp = amblist; *amblp != NULL; amblp++) | 
| 243 |  |  | if (!strcmp(o->oname, *amblp)) { | 
| 244 | greg | 1.11 | if (ambset[0] >= MAXASET) { | 
| 245 |  |  | error(WARNING, "too many modifiers in ambient list"); | 
| 246 |  |  | hitlimit++; | 
| 247 |  |  | return;         /* should this be fatal? */ | 
| 248 |  |  | } | 
| 249 | greg | 1.8 | insertelem(ambset, obj); | 
| 250 |  |  | return; | 
| 251 | greg | 1.1 | } | 
| 252 |  |  | } | 
| 253 |  |  |  | 
| 254 |  |  |  | 
| 255 | schorsch | 2.56 | extern void | 
| 256 | greg | 2.58 | multambient(            /* compute ambient component & multiply by coef. */ | 
| 257 |  |  | COLOR  aval, | 
| 258 | schorsch | 2.56 | register RAY  *r, | 
| 259 |  |  | FVECT  nrm | 
| 260 |  |  | ) | 
| 261 | greg | 1.1 | { | 
| 262 |  |  | static int  rdepth = 0;                 /* ambient recursion */ | 
| 263 | greg | 2.58 | COLOR   acol; | 
| 264 | gregl | 2.43 | double  d, l; | 
| 265 | greg | 1.1 |  | 
| 266 |  |  | if (ambdiv <= 0)                        /* no ambient calculation */ | 
| 267 |  |  | goto dumbamb; | 
| 268 |  |  | /* check number of bounces */ | 
| 269 | greg | 1.16 | if (rdepth >= ambounce) | 
| 270 | greg | 1.1 | goto dumbamb; | 
| 271 |  |  | /* check ambient list */ | 
| 272 |  |  | if (ambincl != -1 && r->ro != NULL && | 
| 273 |  |  | ambincl != inset(ambset, r->ro->omod)) | 
| 274 |  |  | goto dumbamb; | 
| 275 |  |  |  | 
| 276 |  |  | if (ambacc <= FTINY) {                  /* no ambient storage */ | 
| 277 | greg | 2.61 | copycolor(acol, aval); | 
| 278 | greg | 1.16 | rdepth++; | 
| 279 | greg | 2.61 | d = doambient(acol, r, r->rweight, NULL, NULL); | 
| 280 | greg | 1.16 | rdepth--; | 
| 281 | greg | 2.32 | if (d <= FTINY) | 
| 282 | greg | 1.1 | goto dumbamb; | 
| 283 | greg | 2.61 | copycolor(aval, acol); | 
| 284 | greg | 1.16 | return; | 
| 285 | greg | 1.1 | } | 
| 286 | greg | 2.40 |  | 
| 287 |  |  | if (tracktime)                          /* sort to minimize thrashing */ | 
| 288 |  |  | sortambvals(0); | 
| 289 | greg | 2.61 | /* interpolate ambient value */ | 
| 290 | greg | 1.1 | setcolor(acol, 0.0, 0.0, 0.0); | 
| 291 | greg | 2.61 | d = sumambient(acol, r, nrm, rdepth, | 
| 292 | greg | 1.16 | &atrunk, thescene.cuorg, thescene.cusize); | 
| 293 | greg | 2.32 | if (d > FTINY) { | 
| 294 | greg | 2.61 | d = 1.0/d; | 
| 295 |  |  | scalecolor(acol, d); | 
| 296 | greg | 2.58 | multcolor(aval, acol); | 
| 297 | greg | 2.32 | return; | 
| 298 | greg | 1.16 | } | 
| 299 | greg | 2.33 | rdepth++;                               /* need to cache new value */ | 
| 300 | greg | 2.61 | d = makeambient(acol, r, nrm, rdepth-1); | 
| 301 | greg | 2.32 | rdepth--; | 
| 302 | greg | 2.58 | if (d > FTINY) { | 
| 303 |  |  | multcolor(aval, acol);          /* got new value */ | 
| 304 | greg | 1.16 | return; | 
| 305 | greg | 2.58 | } | 
| 306 | greg | 1.1 | dumbamb:                                        /* return global value */ | 
| 307 | greg | 2.58 | if ((ambvwt <= 0) | (navsum == 0)) { | 
| 308 |  |  | multcolor(aval, ambval); | 
| 309 | greg | 2.32 | return; | 
| 310 | greg | 2.58 | } | 
| 311 | gregl | 2.43 | l = bright(ambval);                     /* average in computations */ | 
| 312 |  |  | if (l > FTINY) { | 
| 313 |  |  | d = (log(l)*(double)ambvwt + avsum) / | 
| 314 |  |  | (double)(ambvwt + navsum); | 
| 315 |  |  | d = exp(d) / l; | 
| 316 | greg | 2.58 | scalecolor(aval, d); | 
| 317 |  |  | multcolor(aval, ambval);        /* apply color of ambval */ | 
| 318 | gregl | 2.43 | } else { | 
| 319 |  |  | d = exp( avsum / (double)navsum ); | 
| 320 | greg | 2.58 | scalecolor(aval, d);            /* neutral color */ | 
| 321 | gregl | 2.43 | } | 
| 322 | greg | 1.1 | } | 
| 323 |  |  |  | 
| 324 |  |  |  | 
| 325 | schorsch | 2.56 | extern double | 
| 326 |  |  | sumambient(     /* get interpolated ambient value */ | 
| 327 |  |  | COLOR  acol, | 
| 328 |  |  | register RAY  *r, | 
| 329 |  |  | FVECT  rn, | 
| 330 |  |  | int  al, | 
| 331 |  |  | AMBTREE  *at, | 
| 332 |  |  | FVECT  c0, | 
| 333 |  |  | double  s | 
| 334 |  |  | ) | 
| 335 | greg | 1.1 | { | 
| 336 | greg | 2.12 | double  d, e1, e2, wt, wsum; | 
| 337 | greg | 1.1 | COLOR  ct; | 
| 338 |  |  | FVECT  ck0; | 
| 339 |  |  | int  i; | 
| 340 |  |  | register int  j; | 
| 341 | greg | 2.12 | register AMBVAL  *av; | 
| 342 | greg | 2.29 |  | 
| 343 |  |  | wsum = 0.0; | 
| 344 | greg | 1.7 | /* do this node */ | 
| 345 | greg | 1.1 | for (av = at->alist; av != NULL; av = av->next) { | 
| 346 | greg | 2.47 | double  rn_dot = -2.0; | 
| 347 | greg | 2.26 | if (tracktime) | 
| 348 | greg | 2.40 | av->latick = ambclock; | 
| 349 | greg | 1.1 | /* | 
| 350 | greg | 1.16 | *  Ambient level test. | 
| 351 | greg | 1.1 | */ | 
| 352 | greg | 2.23 | if (av->lvl > al)       /* list sorted, so this works */ | 
| 353 |  |  | break; | 
| 354 | greg | 2.61 | if (av->weight < 0.9*r->rweight) | 
| 355 | greg | 1.1 | continue; | 
| 356 |  |  | /* | 
| 357 |  |  | *  Ambient radius test. | 
| 358 |  |  | */ | 
| 359 | greg | 2.29 | d = av->pos[0] - r->rop[0]; | 
| 360 |  |  | e1 = d * d; | 
| 361 |  |  | d = av->pos[1] - r->rop[1]; | 
| 362 |  |  | e1 += d * d; | 
| 363 |  |  | d = av->pos[2] - r->rop[2]; | 
| 364 |  |  | e1 += d * d; | 
| 365 | greg | 1.1 | e1 /= av->rad * av->rad; | 
| 366 |  |  | if (e1 > ambacc*ambacc*1.21) | 
| 367 |  |  | continue; | 
| 368 |  |  | /* | 
| 369 | greg | 2.47 | *  Direction test using closest normal. | 
| 370 | greg | 1.1 | */ | 
| 371 | greg | 2.47 | d = DOT(av->dir, r->ron); | 
| 372 |  |  | if (rn != r->ron) { | 
| 373 |  |  | rn_dot = DOT(av->dir, rn); | 
| 374 |  |  | if (rn_dot > 1.0-FTINY) | 
| 375 |  |  | rn_dot = 1.0-FTINY; | 
| 376 |  |  | if (rn_dot >= d-FTINY) { | 
| 377 |  |  | d = rn_dot; | 
| 378 |  |  | rn_dot = -2.0; | 
| 379 |  |  | } | 
| 380 |  |  | } | 
| 381 |  |  | e2 = (1.0 - d) * r->rweight; | 
| 382 | greg | 1.1 | if (e2 < 0.0) e2 = 0.0; | 
| 383 |  |  | if (e1 + e2 > ambacc*ambacc*1.21) | 
| 384 |  |  | continue; | 
| 385 |  |  | /* | 
| 386 |  |  | *  Ray behind test. | 
| 387 |  |  | */ | 
| 388 |  |  | d = 0.0; | 
| 389 |  |  | for (j = 0; j < 3; j++) | 
| 390 |  |  | d += (r->rop[j] - av->pos[j]) * | 
| 391 |  |  | (av->dir[j] + r->ron[j]); | 
| 392 | greg | 1.18 | if (d*0.5 < -minarad*ambacc-.001) | 
| 393 | greg | 1.1 | continue; | 
| 394 |  |  | /* | 
| 395 |  |  | *  Jittering final test reduces image artifacts. | 
| 396 |  |  | */ | 
| 397 | greg | 2.47 | e1 = sqrt(e1); | 
| 398 |  |  | e2 = sqrt(e2); | 
| 399 |  |  | wt = e1 + e2; | 
| 400 | greg | 2.31 | if (wt > ambacc*(.9+.2*urand(9015+samplendx))) | 
| 401 | greg | 1.1 | continue; | 
| 402 | greg | 2.47 | /* | 
| 403 |  |  | *  Recompute directional error using perturbed normal | 
| 404 |  |  | */ | 
| 405 |  |  | if (rn_dot > 0.0) { | 
| 406 |  |  | e2 = sqrt((1.0 - rn_dot)*r->rweight); | 
| 407 |  |  | wt = e1 + e2; | 
| 408 |  |  | } | 
| 409 | greg | 1.1 | if (wt <= 1e-3) | 
| 410 |  |  | wt = 1e3; | 
| 411 |  |  | else | 
| 412 |  |  | wt = 1.0 / wt; | 
| 413 |  |  | wsum += wt; | 
| 414 | greg | 2.34 | extambient(ct, av, r->rop, rn); | 
| 415 | greg | 1.1 | scalecolor(ct, wt); | 
| 416 |  |  | addcolor(acol, ct); | 
| 417 | greg | 1.7 | } | 
| 418 |  |  | if (at->kid == NULL) | 
| 419 |  |  | return(wsum); | 
| 420 |  |  | /* do children */ | 
| 421 |  |  | s *= 0.5; | 
| 422 |  |  | for (i = 0; i < 8; i++) { | 
| 423 |  |  | for (j = 0; j < 3; j++) { | 
| 424 |  |  | ck0[j] = c0[j]; | 
| 425 |  |  | if (1<<j & i) | 
| 426 |  |  | ck0[j] += s; | 
| 427 |  |  | if (r->rop[j] < ck0[j] - OCTSCALE*s) | 
| 428 |  |  | break; | 
| 429 |  |  | if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) | 
| 430 |  |  | break; | 
| 431 |  |  | } | 
| 432 |  |  | if (j == 3) | 
| 433 | greg | 2.61 | wsum += sumambient(acol, r, rn, al, | 
| 434 | greg | 2.59 | at->kid+i, ck0, s); | 
| 435 | greg | 1.1 | } | 
| 436 |  |  | return(wsum); | 
| 437 |  |  | } | 
| 438 |  |  |  | 
| 439 |  |  |  | 
| 440 | schorsch | 2.56 | extern double | 
| 441 | greg | 2.61 | makeambient(            /* make a new ambient value for storage */ | 
| 442 | schorsch | 2.56 | COLOR  acol, | 
| 443 | greg | 2.58 | RAY  *r, | 
| 444 | schorsch | 2.56 | FVECT  rn, | 
| 445 |  |  | int  al | 
| 446 |  |  | ) | 
| 447 | greg | 1.1 | { | 
| 448 | greg | 2.12 | AMBVAL  amb; | 
| 449 | greg | 1.14 | FVECT   gp, gd; | 
| 450 | greg | 2.60 | int     i; | 
| 451 |  |  |  | 
| 452 | greg | 2.61 | amb.weight = 1.0;                       /* compute weight */ | 
| 453 |  |  | for (i = al; i-- > 0; ) | 
| 454 | greg | 2.60 | amb.weight *= AVGREFL; | 
| 455 | greg | 2.61 | if (r->rweight < 0.1*amb.weight)        /* heuristic override */ | 
| 456 | greg | 2.62 | amb.weight = 1.25*r->rweight; | 
| 457 | greg | 2.61 | setcolor(acol, AVGREFL, AVGREFL, AVGREFL); | 
| 458 | greg | 1.16 | /* compute ambient */ | 
| 459 | greg | 2.61 | amb.rad = doambient(acol, r, amb.weight, gp, gd); | 
| 460 |  |  | if (amb.rad <= FTINY) { | 
| 461 |  |  | setcolor(acol, 0.0, 0.0, 0.0); | 
| 462 | greg | 1.1 | return(0.0); | 
| 463 | greg | 2.61 | } | 
| 464 |  |  | scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */ | 
| 465 |  |  | /* store value */ | 
| 466 | greg | 1.1 | VCOPY(amb.pos, r->rop); | 
| 467 |  |  | VCOPY(amb.dir, r->ron); | 
| 468 | greg | 1.16 | amb.lvl = al; | 
| 469 | greg | 1.1 | copycolor(amb.val, acol); | 
| 470 | greg | 1.14 | VCOPY(amb.gpos, gp); | 
| 471 |  |  | VCOPY(amb.gdir, gd); | 
| 472 | greg | 1.1 | /* insert into tree */ | 
| 473 | greg | 2.7 | avsave(&amb);                           /* and save to file */ | 
| 474 | greg | 2.34 | if (rn != r->ron) | 
| 475 |  |  | extambient(acol, &amb, r->rop, rn);     /* texture */ | 
| 476 | greg | 1.1 | return(amb.rad); | 
| 477 | greg | 1.15 | } | 
| 478 |  |  |  | 
| 479 |  |  |  | 
| 480 | schorsch | 2.56 | extern void | 
| 481 |  |  | extambient(             /* extrapolate value at pv, nv */ | 
| 482 |  |  | COLOR  cr, | 
| 483 |  |  | register AMBVAL  *ap, | 
| 484 |  |  | FVECT  pv, | 
| 485 |  |  | FVECT  nv | 
| 486 |  |  | ) | 
| 487 | greg | 1.15 | { | 
| 488 | gwlarson | 2.45 | FVECT  v1; | 
| 489 | greg | 1.15 | register int  i; | 
| 490 | greg | 2.12 | double  d; | 
| 491 | greg | 1.15 |  | 
| 492 |  |  | d = 1.0;                        /* zeroeth order */ | 
| 493 |  |  | /* gradient due to translation */ | 
| 494 |  |  | for (i = 0; i < 3; i++) | 
| 495 |  |  | d += ap->gpos[i]*(pv[i]-ap->pos[i]); | 
| 496 |  |  | /* gradient due to rotation */ | 
| 497 | gwlarson | 2.45 | VCROSS(v1, ap->dir, nv); | 
| 498 |  |  | d += DOT(ap->gdir, v1); | 
| 499 | greg | 1.15 | if (d <= 0.0) { | 
| 500 |  |  | setcolor(cr, 0.0, 0.0, 0.0); | 
| 501 |  |  | return; | 
| 502 |  |  | } | 
| 503 |  |  | copycolor(cr, ap->val); | 
| 504 |  |  | scalecolor(cr, d); | 
| 505 | greg | 1.1 | } | 
| 506 |  |  |  | 
| 507 |  |  |  | 
| 508 | greg | 2.47 | static void | 
| 509 | schorsch | 2.56 | initambfile(            /* initialize ambient file */ | 
| 510 |  |  | int  creat | 
| 511 |  |  | ) | 
| 512 | greg | 2.9 | { | 
| 513 | greg | 2.47 | extern char  *progname, *octname; | 
| 514 |  |  | static char  *mybuf = NULL; | 
| 515 | greg | 2.9 |  | 
| 516 | greg | 2.19 | #ifdef  F_SETLKW | 
| 517 |  |  | aflock(creat ? F_WRLCK : F_RDLCK); | 
| 518 |  |  | #endif | 
| 519 | schorsch | 2.50 | SET_FILE_BINARY(ambfp); | 
| 520 | greg | 2.47 | if (mybuf == NULL) | 
| 521 |  |  | mybuf = (char *)bmalloc(BUFSIZ+8); | 
| 522 |  |  | setbuf(ambfp, mybuf); | 
| 523 | greg | 2.9 | if (creat) {                    /* new file */ | 
| 524 | greg | 2.24 | newheader("RADIANCE", ambfp); | 
| 525 | greg | 2.41 | fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ", | 
| 526 | greg | 2.9 | progname, colval(ambval,RED), | 
| 527 |  |  | colval(ambval,GRN), colval(ambval,BLU), | 
| 528 | greg | 2.41 | ambvwt, ambounce, ambacc); | 
| 529 | greg | 2.47 | fprintf(ambfp, "-ad %d -as %d -ar %d ", | 
| 530 |  |  | ambdiv, ambssamp, ambres); | 
| 531 |  |  | if (octname != NULL) | 
| 532 |  |  | printargs(1, &octname, ambfp); | 
| 533 |  |  | else | 
| 534 |  |  | fputc('\n', ambfp); | 
| 535 | greg | 2.9 | fprintf(ambfp, "SOFTWARE= %s\n", VersionID); | 
| 536 | greg | 2.47 | fputnow(ambfp); | 
| 537 | greg | 2.9 | fputformat(AMBFMT, ambfp); | 
| 538 |  |  | putc('\n', ambfp); | 
| 539 |  |  | putambmagic(ambfp); | 
| 540 | greg | 2.17 | } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) | 
| 541 |  |  | error(USER, "bad ambient file"); | 
| 542 | greg | 2.9 | } | 
| 543 |  |  |  | 
| 544 |  |  |  | 
| 545 | greg | 2.47 | static void | 
| 546 | schorsch | 2.56 | avsave(                         /* insert and save an ambient value */ | 
| 547 |  |  | AMBVAL  *av | 
| 548 |  |  | ) | 
| 549 | greg | 1.1 | { | 
| 550 | greg | 2.21 | avinsert(avstore(av)); | 
| 551 | greg | 1.1 | if (ambfp == NULL) | 
| 552 |  |  | return; | 
| 553 | greg | 2.5 | if (writambval(av, ambfp) < 0) | 
| 554 | greg | 1.1 | goto writerr; | 
| 555 | greg | 2.16 | if (++nunflshed >= AMBFLUSH) | 
| 556 | greg | 2.7 | if (ambsync() == EOF) | 
| 557 | greg | 1.1 | goto writerr; | 
| 558 |  |  | return; | 
| 559 |  |  | writerr: | 
| 560 | greg | 2.47 | error(SYSTEM, "error writing to ambient file"); | 
| 561 | greg | 1.1 | } | 
| 562 |  |  |  | 
| 563 |  |  |  | 
| 564 | greg | 2.20 | static AMBVAL * | 
| 565 | schorsch | 2.56 | avstore(                                /* allocate memory and store aval */ | 
| 566 |  |  | register AMBVAL  *aval | 
| 567 |  |  | ) | 
| 568 | greg | 2.20 | { | 
| 569 |  |  | register AMBVAL  *av; | 
| 570 | gregl | 2.43 | double  d; | 
| 571 | greg | 2.20 |  | 
| 572 |  |  | if ((av = newambval()) == NULL) | 
| 573 |  |  | error(SYSTEM, "out of memory in avstore"); | 
| 574 | schorsch | 2.53 | *av = *aval; | 
| 575 | greg | 2.26 | av->latick = ambclock; | 
| 576 |  |  | av->next = NULL; | 
| 577 |  |  | nambvals++; | 
| 578 | gregl | 2.43 | d = bright(av->val); | 
| 579 |  |  | if (d > FTINY) {                /* add to log sum for averaging */ | 
| 580 |  |  | avsum += log(d); | 
| 581 |  |  | navsum++; | 
| 582 |  |  | } | 
| 583 | greg | 2.20 | return(av); | 
| 584 |  |  | } | 
| 585 |  |  |  | 
| 586 |  |  |  | 
| 587 | greg | 2.26 | #define ATALLOCSZ       512             /* #/8 trees to allocate at once */ | 
| 588 |  |  |  | 
| 589 |  |  | static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */ | 
| 590 |  |  |  | 
| 591 |  |  |  | 
| 592 | greg | 2.47 | static AMBTREE * | 
| 593 | schorsch | 2.56 | newambtree(void)                                /* allocate 8 ambient tree structs */ | 
| 594 | greg | 2.26 | { | 
| 595 |  |  | register AMBTREE  *atp, *upperlim; | 
| 596 |  |  |  | 
| 597 |  |  | if (atfreelist == NULL) {       /* get more nodes */ | 
| 598 | greg | 2.47 | atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE)); | 
| 599 | greg | 2.26 | if (atfreelist == NULL) | 
| 600 |  |  | return(NULL); | 
| 601 |  |  | /* link new free list */ | 
| 602 |  |  | upperlim = atfreelist + 8*(ATALLOCSZ-1); | 
| 603 |  |  | for (atp = atfreelist; atp < upperlim; atp += 8) | 
| 604 |  |  | atp->kid = atp + 8; | 
| 605 |  |  | atp->kid = NULL; | 
| 606 |  |  | } | 
| 607 |  |  | atp = atfreelist; | 
| 608 |  |  | atfreelist = atp->kid; | 
| 609 | schorsch | 2.52 | memset((char *)atp, '\0', 8*sizeof(AMBTREE)); | 
| 610 | greg | 2.26 | return(atp); | 
| 611 |  |  | } | 
| 612 |  |  |  | 
| 613 |  |  |  | 
| 614 | greg | 2.47 | static void | 
| 615 | schorsch | 2.56 | freeambtree(                    /* free 8 ambient tree structs */ | 
| 616 |  |  | AMBTREE  *atp | 
| 617 |  |  | ) | 
| 618 | greg | 2.26 | { | 
| 619 |  |  | atp->kid = atfreelist; | 
| 620 |  |  | atfreelist = atp; | 
| 621 |  |  | } | 
| 622 |  |  |  | 
| 623 |  |  |  | 
| 624 | greg | 2.47 | static void | 
| 625 | schorsch | 2.56 | avinsert(                               /* insert ambient value in our tree */ | 
| 626 |  |  | void *av | 
| 627 |  |  | ) | 
| 628 | greg | 1.1 | { | 
| 629 | greg | 2.21 | register AMBTREE  *at; | 
| 630 | greg | 2.23 | register AMBVAL  *ap; | 
| 631 |  |  | AMBVAL  avh; | 
| 632 | greg | 1.1 | FVECT  ck0; | 
| 633 | greg | 2.21 | double  s; | 
| 634 | greg | 1.1 | int  branch; | 
| 635 |  |  | register int  i; | 
| 636 |  |  |  | 
| 637 | schorsch | 2.56 | if (((AMBVAL*)av)->rad <= FTINY) | 
| 638 | greg | 2.20 | error(CONSISTENCY, "zero ambient radius in avinsert"); | 
| 639 | greg | 2.21 | at = &atrunk; | 
| 640 |  |  | VCOPY(ck0, thescene.cuorg); | 
| 641 |  |  | s = thescene.cusize; | 
| 642 | schorsch | 2.56 | while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) { | 
| 643 | greg | 1.1 | if (at->kid == NULL) | 
| 644 |  |  | if ((at->kid = newambtree()) == NULL) | 
| 645 | greg | 2.20 | error(SYSTEM, "out of memory in avinsert"); | 
| 646 | greg | 1.1 | s *= 0.5; | 
| 647 |  |  | branch = 0; | 
| 648 |  |  | for (i = 0; i < 3; i++) | 
| 649 | schorsch | 2.56 | if (((AMBVAL*)av)->pos[i] > ck0[i] + s) { | 
| 650 | greg | 1.1 | ck0[i] += s; | 
| 651 |  |  | branch |= 1 << i; | 
| 652 |  |  | } | 
| 653 |  |  | at = at->kid + branch; | 
| 654 |  |  | } | 
| 655 | greg | 2.23 | avh.next = at->alist;           /* order by increasing level */ | 
| 656 |  |  | for (ap = &avh; ap->next != NULL; ap = ap->next) | 
| 657 | schorsch | 2.56 | if (ap->next->lvl >= ((AMBVAL*)av)->lvl) | 
| 658 | greg | 2.23 | break; | 
| 659 | schorsch | 2.56 | ((AMBVAL*)av)->next = ap->next; | 
| 660 |  |  | ap->next = (AMBVAL*)av; | 
| 661 | greg | 2.23 | at->alist = avh.next; | 
| 662 | greg | 2.7 | } | 
| 663 |  |  |  | 
| 664 |  |  |  | 
| 665 | greg | 2.47 | static void | 
| 666 | schorsch | 2.56 | unloadatree(                    /* unload an ambient value tree */ | 
| 667 |  |  | register AMBTREE  *at, | 
| 668 |  |  | unloadtf_t *f | 
| 669 |  |  | ) | 
| 670 | greg | 2.20 | { | 
| 671 |  |  | register AMBVAL  *av; | 
| 672 |  |  | register int  i; | 
| 673 |  |  | /* transfer values at this node */ | 
| 674 |  |  | for (av = at->alist; av != NULL; av = at->alist) { | 
| 675 |  |  | at->alist = av->next; | 
| 676 | greg | 2.26 | (*f)(av); | 
| 677 | greg | 2.20 | } | 
| 678 | greg | 2.21 | if (at->kid == NULL) | 
| 679 |  |  | return; | 
| 680 | greg | 2.20 | for (i = 0; i < 8; i++)         /* transfer and free children */ | 
| 681 | greg | 2.26 | unloadatree(at->kid+i, f); | 
| 682 | greg | 2.20 | freeambtree(at->kid); | 
| 683 | greg | 2.26 | at->kid = NULL; | 
| 684 |  |  | } | 
| 685 |  |  |  | 
| 686 |  |  |  | 
| 687 | greg | 2.39 | static struct avl { | 
| 688 |  |  | AMBVAL  *p; | 
| 689 |  |  | unsigned long   t; | 
| 690 |  |  | }       *avlist1;                       /* ambient value list with ticks */ | 
| 691 |  |  | static AMBVAL   **avlist2;              /* memory positions for sorting */ | 
| 692 | greg | 2.26 | static int      i_avlist;               /* index for lists */ | 
| 693 |  |  |  | 
| 694 | schorsch | 2.56 | static int alatcmp(const void *av1, const void *av2); | 
| 695 | greg | 2.26 |  | 
| 696 | schorsch | 2.56 | static void | 
| 697 |  |  | av2list( | 
| 698 |  |  | void *av | 
| 699 |  |  | ) | 
| 700 | greg | 2.26 | { | 
| 701 | greg | 2.27 | #ifdef DEBUG | 
| 702 | greg | 2.26 | if (i_avlist >= nambvals) | 
| 703 |  |  | error(CONSISTENCY, "too many ambient values in av2list1"); | 
| 704 | greg | 2.27 | #endif | 
| 705 | schorsch | 2.56 | avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av; | 
| 706 |  |  | avlist1[i_avlist++].t = ((AMBVAL*)av)->latick; | 
| 707 | greg | 2.26 | } | 
| 708 |  |  |  | 
| 709 |  |  |  | 
| 710 |  |  | static int | 
| 711 | schorsch | 2.56 | alatcmp(                        /* compare ambient values for MRA */ | 
| 712 |  |  | const void *av1, | 
| 713 |  |  | const void *av2 | 
| 714 |  |  | ) | 
| 715 | greg | 2.26 | { | 
| 716 | schorsch | 2.56 | register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t; | 
| 717 | greg | 2.38 | return(lc<0 ? -1 : lc>0 ? 1 : 0); | 
| 718 | greg | 2.26 | } | 
| 719 |  |  |  | 
| 720 |  |  |  | 
| 721 | greg | 2.47 | /* GW NOTE 2002/10/3: | 
| 722 |  |  | * I used to compare AMBVAL pointers, but found that this was the | 
| 723 |  |  | * cause of a serious consistency error with gcc, since the optimizer | 
| 724 |  |  | * uses some dangerous trick in pointer subtraction that | 
| 725 |  |  | * assumes pointers differ by exact struct size increments. | 
| 726 |  |  | */ | 
| 727 | greg | 2.26 | static int | 
| 728 | schorsch | 2.56 | aposcmp(                        /* compare ambient value positions */ | 
| 729 |  |  | const void      *avp1, | 
| 730 |  |  | const void      *avp2 | 
| 731 |  |  | ) | 
| 732 | greg | 2.26 | { | 
| 733 | greg | 2.47 | register long   diff = *(char * const *)avp1 - *(char * const *)avp2; | 
| 734 |  |  | if (diff < 0) | 
| 735 |  |  | return(-1); | 
| 736 |  |  | return(diff > 0); | 
| 737 | greg | 2.26 | } | 
| 738 |  |  |  | 
| 739 | greg | 2.39 | #if 1 | 
| 740 | greg | 2.27 | static int | 
| 741 | schorsch | 2.56 | avlmemi(                                /* find list position from address */ | 
| 742 |  |  | AMBVAL  *avaddr | 
| 743 |  |  | ) | 
| 744 | greg | 2.27 | { | 
| 745 |  |  | register AMBVAL  **avlpp; | 
| 746 |  |  |  | 
| 747 |  |  | avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, | 
| 748 |  |  | nambvals, sizeof(AMBVAL *), aposcmp); | 
| 749 |  |  | if (avlpp == NULL) | 
| 750 |  |  | error(CONSISTENCY, "address not found in avlmemi"); | 
| 751 |  |  | return(avlpp - avlist2); | 
| 752 |  |  | } | 
| 753 |  |  | #else | 
| 754 |  |  | #define avlmemi(avaddr) ((AMBVAL **)bsearch((char *)&avaddr,(char *)avlist2, \ | 
| 755 |  |  | nambvals,sizeof(AMBVAL *),aposcmp) - avlist2) | 
| 756 |  |  | #endif | 
| 757 |  |  |  | 
| 758 |  |  |  | 
| 759 | greg | 2.47 | static void | 
| 760 | schorsch | 2.56 | sortambvals(                    /* resort ambient values */ | 
| 761 |  |  | int     always | 
| 762 |  |  | ) | 
| 763 | greg | 2.26 | { | 
| 764 |  |  | AMBTREE  oldatrunk; | 
| 765 |  |  | AMBVAL  tav, *tap, *pnext; | 
| 766 |  |  | register int    i, j; | 
| 767 | greg | 2.28 | /* see if it's time yet */ | 
| 768 | greg | 2.40 | if (!always && (ambclock++ < lastsort+sortintvl || | 
| 769 | greg | 2.28 | nambvals < SORT_THRESH)) | 
| 770 |  |  | return; | 
| 771 | greg | 2.26 | /* | 
| 772 |  |  | * The idea here is to minimize memory thrashing | 
| 773 |  |  | * in VM systems by improving reference locality. | 
| 774 |  |  | * We do this by periodically sorting our stored ambient | 
| 775 |  |  | * values in memory in order of most recently to least | 
| 776 |  |  | * recently accessed.  This ordering was chosen so that new | 
| 777 |  |  | * ambient values (which tend to be less important) go into | 
| 778 |  |  | * higher memory with the infrequently accessed values. | 
| 779 |  |  | *      Since we expect our values to need sorting less | 
| 780 |  |  | * frequently as the process continues, we double our | 
| 781 |  |  | * waiting interval after each call. | 
| 782 |  |  | *      This routine is also called by setambacc() with | 
| 783 |  |  | * the "always" parameter set to 1 so that the ambient | 
| 784 |  |  | * tree will be rebuilt with the new accuracy parameter. | 
| 785 |  |  | */ | 
| 786 | greg | 2.27 | if (tracktime) {                /* allocate pointer arrays to sort */ | 
| 787 | greg | 2.26 | avlist2 = (AMBVAL **)malloc(nambvals*sizeof(AMBVAL *)); | 
| 788 | greg | 2.39 | avlist1 = (struct avl *)malloc(nambvals*sizeof(struct avl)); | 
| 789 |  |  | } else { | 
| 790 |  |  | avlist2 = NULL; | 
| 791 |  |  | avlist1 = NULL; | 
| 792 |  |  | } | 
| 793 |  |  | if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */ | 
| 794 |  |  | if (avlist2 != NULL) | 
| 795 | greg | 2.47 | free((void *)avlist2); | 
| 796 | greg | 2.27 | if (always) {           /* rebuild without sorting */ | 
| 797 | schorsch | 2.53 | oldatrunk = atrunk; | 
| 798 | greg | 2.27 | atrunk.alist = NULL; | 
| 799 |  |  | atrunk.kid = NULL; | 
| 800 |  |  | unloadatree(&oldatrunk, avinsert); | 
| 801 |  |  | } | 
| 802 | greg | 2.26 | } else {                        /* sort memory by last access time */ | 
| 803 |  |  | /* | 
| 804 |  |  | * Sorting memory is tricky because it isn't contiguous. | 
| 805 |  |  | * We have to sort an array of pointers by MRA and also | 
| 806 |  |  | * by memory position.  We then copy values in "loops" | 
| 807 |  |  | * to minimize memory hits.  Nevertheless, we will visit | 
| 808 | greg | 2.27 | * everyone at least twice, and this is an expensive process | 
| 809 | greg | 2.26 | * when we're thrashing, which is when we need to do it. | 
| 810 |  |  | */ | 
| 811 | greg | 2.27 | #ifdef DEBUG | 
| 812 | greg | 2.29 | sprintf(errmsg, "sorting %u ambient values at ambclock=%lu...", | 
| 813 |  |  | nambvals, ambclock); | 
| 814 | greg | 2.27 | eputs(errmsg); | 
| 815 |  |  | #endif | 
| 816 |  |  | i_avlist = 0; | 
| 817 |  |  | unloadatree(&atrunk, av2list);  /* empty current tree */ | 
| 818 |  |  | #ifdef DEBUG | 
| 819 |  |  | if (i_avlist < nambvals) | 
| 820 |  |  | error(CONSISTENCY, "missing ambient values in sortambvals"); | 
| 821 |  |  | #endif | 
| 822 | greg | 2.39 | qsort((char *)avlist1, nambvals, sizeof(struct avl), alatcmp); | 
| 823 | greg | 2.26 | qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), aposcmp); | 
| 824 |  |  | for (i = 0; i < nambvals; i++) { | 
| 825 | greg | 2.39 | if (avlist1[i].p == NULL) | 
| 826 | greg | 2.26 | continue; | 
| 827 |  |  | tap = avlist2[i]; | 
| 828 | schorsch | 2.53 | tav = *tap; | 
| 829 | greg | 2.39 | for (j = i; (pnext = avlist1[j].p) != tap; | 
| 830 | greg | 2.27 | j = avlmemi(pnext)) { | 
| 831 | schorsch | 2.53 | *(avlist2[j]) = *pnext; | 
| 832 | greg | 2.26 | avinsert(avlist2[j]); | 
| 833 | greg | 2.39 | avlist1[j].p = NULL; | 
| 834 | greg | 2.26 | } | 
| 835 | schorsch | 2.53 | *(avlist2[j]) = tav; | 
| 836 | greg | 2.26 | avinsert(avlist2[j]); | 
| 837 | greg | 2.39 | avlist1[j].p = NULL; | 
| 838 | greg | 2.26 | } | 
| 839 | greg | 2.47 | free((void *)avlist1); | 
| 840 |  |  | free((void *)avlist2); | 
| 841 | greg | 2.28 | /* compute new sort interval */ | 
| 842 |  |  | sortintvl = ambclock - lastsort; | 
| 843 | greg | 2.32 | if (sortintvl >= MAX_SORT_INTVL/2) | 
| 844 | greg | 2.28 | sortintvl = MAX_SORT_INTVL; | 
| 845 |  |  | else | 
| 846 | greg | 2.26 | sortintvl <<= 1;        /* wait twice as long next */ | 
| 847 | greg | 2.27 | #ifdef DEBUG | 
| 848 |  |  | eputs("done\n"); | 
| 849 |  |  | #endif | 
| 850 | greg | 2.26 | } | 
| 851 |  |  | if (ambclock >= MAXACLOCK) | 
| 852 |  |  | ambclock = MAXACLOCK/2; | 
| 853 |  |  | lastsort = ambclock; | 
| 854 | greg | 2.20 | } | 
| 855 |  |  |  | 
| 856 |  |  |  | 
| 857 | greg | 2.18 | #ifdef  F_SETLKW | 
| 858 | greg | 2.10 |  | 
| 859 | greg | 2.47 | static void | 
| 860 | schorsch | 2.56 | aflock(                 /* lock/unlock ambient file */ | 
| 861 |  |  | int  typ | 
| 862 |  |  | ) | 
| 863 | greg | 2.19 | { | 
| 864 |  |  | static struct flock  fls;       /* static so initialized to zeroes */ | 
| 865 |  |  |  | 
| 866 |  |  | fls.l_type = typ; | 
| 867 |  |  | if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) | 
| 868 |  |  | error(SYSTEM, "cannot (un)lock ambient file"); | 
| 869 |  |  | } | 
| 870 |  |  |  | 
| 871 |  |  |  | 
| 872 | schorsch | 2.56 | extern int | 
| 873 |  |  | ambsync(void)                   /* synchronize ambient file */ | 
| 874 | greg | 2.7 | { | 
| 875 | greg | 2.15 | long  flen; | 
| 876 | greg | 2.12 | AMBVAL  avs; | 
| 877 | greg | 2.7 | register int  n; | 
| 878 | greg | 2.16 |  | 
| 879 | greg | 2.65 | if (ambfp == NULL)      /* no ambient file? */ | 
| 880 |  |  | return(0); | 
| 881 | greg | 2.19 | if (lastpos < 0)        /* initializing (locked in initambfile) */ | 
| 882 |  |  | goto syncend; | 
| 883 | greg | 2.63 | /* gain appropriate access */ | 
| 884 |  |  | aflock(nunflshed ? F_WRLCK : F_RDLCK); | 
| 885 | greg | 2.7 | /* see if file has grown */ | 
| 886 | greg | 2.55 | if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) | 
| 887 | greg | 2.21 | goto seekerr; | 
| 888 | schorsch | 2.54 | if ( (n = flen - lastpos) ) {           /* file has grown */ | 
| 889 | greg | 2.17 | if (ambinp == NULL) {           /* use duplicate filedes */ | 
| 890 |  |  | ambinp = fdopen(dup(fileno(ambfp)), "r"); | 
| 891 | greg | 2.14 | if (ambinp == NULL) | 
| 892 | greg | 2.17 | error(SYSTEM, "fdopen failed in ambsync"); | 
| 893 | greg | 2.14 | } | 
| 894 | greg | 2.15 | if (fseek(ambinp, lastpos, 0) < 0) | 
| 895 | greg | 2.21 | goto seekerr; | 
| 896 | greg | 2.15 | while (n >= AMBVALSIZ) {        /* load contributed values */ | 
| 897 | greg | 2.37 | if (!readambval(&avs, ambinp)) { | 
| 898 |  |  | sprintf(errmsg, | 
| 899 | greg | 2.47 | "ambient file \"%s\" corrupted near character %ld", | 
| 900 |  |  | ambfile, flen - n); | 
| 901 | greg | 2.37 | error(WARNING, errmsg); | 
| 902 |  |  | break; | 
| 903 |  |  | } | 
| 904 | greg | 2.21 | avinsert(avstore(&avs)); | 
| 905 | greg | 2.15 | n -= AMBVALSIZ; | 
| 906 |  |  | } | 
| 907 | greg | 2.64 | lastpos = flen - n; | 
| 908 | greg | 2.22 | /*** seek always as safety measure | 
| 909 |  |  | if (n) ***/                     /* alignment */ | 
| 910 | greg | 2.64 | if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) | 
| 911 | greg | 2.21 | goto seekerr; | 
| 912 | greg | 2.7 | } | 
| 913 | greg | 2.21 | #ifdef  DEBUG | 
| 914 |  |  | if (ambfp->_ptr - ambfp->_base != nunflshed*AMBVALSIZ) { | 
| 915 |  |  | sprintf(errmsg, "ambient file buffer at %d rather than %d", | 
| 916 |  |  | ambfp->_ptr - ambfp->_base, | 
| 917 |  |  | nunflshed*AMBVALSIZ); | 
| 918 |  |  | error(CONSISTENCY, errmsg); | 
| 919 |  |  | } | 
| 920 |  |  | #endif | 
| 921 | greg | 2.15 | syncend: | 
| 922 | greg | 2.7 | n = fflush(ambfp);                      /* calls write() at last */ | 
| 923 | greg | 2.65 | if ((n == EOF) | (lastpos < 0)) { | 
| 924 | greg | 2.64 | if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) | 
| 925 |  |  | goto seekerr; | 
| 926 |  |  | } else | 
| 927 |  |  | lastpos += (long)nunflshed*AMBVALSIZ; | 
| 928 | greg | 2.19 | aflock(F_UNLCK);                        /* release file */ | 
| 929 | greg | 2.16 | nunflshed = 0; | 
| 930 | greg | 2.7 | return(n); | 
| 931 | greg | 2.21 | seekerr: | 
| 932 |  |  | error(SYSTEM, "seek failed in ambsync"); | 
| 933 | schorsch | 2.56 | return -1; /* pro forma return */ | 
| 934 | greg | 2.18 | } | 
| 935 |  |  |  | 
| 936 |  |  | #else | 
| 937 |  |  |  | 
| 938 | schorsch | 2.56 | extern int | 
| 939 |  |  | ambsync(void)                   /* flush ambient file */ | 
| 940 | greg | 2.18 | { | 
| 941 | greg | 2.65 | if (ambfp == NULL) | 
| 942 |  |  | return(0); | 
| 943 | greg | 2.18 | nunflshed = 0; | 
| 944 |  |  | return(fflush(ambfp)); | 
| 945 | greg | 1.1 | } | 
| 946 | greg | 2.10 |  | 
| 947 |  |  | #endif |