ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
(Generate patch)

Comparing ray/src/rt/ambient.c (file contents):
Revision 2.42 by greg, Thu Jan 2 09:37:13 1997 UTC vs.
Revision 2.63 by greg, Thu Sep 13 20:13:16 2007 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  ambient.c - routines dealing with ambient (inter-reflected) component.
6 + *
7 + *  Declarations of external symbols in ambient.h
8   */
9  
10 < #include  "ray.h"
10 > #include "copyright.h"
11  
12 < #include  "octree.h"
12 > #include <string.h>
13  
14 + #include  "platform.h"
15 + #include  "ray.h"
16   #include  "otypes.h"
17 <
17 > #include  "resolu.h"
18   #include  "ambient.h"
18
19   #include  "random.h"
20  
21   #ifndef  OCTSCALE
22   #define  OCTSCALE       1.0     /* ceil((valid rad.)/(cube size)) */
23   #endif
24  
25 typedef struct ambtree {
26        AMBVAL  *alist;         /* ambient value list */
27        struct ambtree  *kid;   /* 8 child nodes */
28 }  AMBTREE;                     /* ambient octree */
29
30 extern CUBE  thescene;          /* contains space boundaries */
31
25   extern char  *shm_boundary;     /* memory sharing boundary */
26  
27 < #define  MAXASET        511     /* maximum number of elements in ambient set */
27 > #ifndef  MAXASET
28 > #define  MAXASET        2047    /* maximum number of elements in ambient set */
29 > #endif
30   OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
31  
32   double  maxarad;                /* maximum ambient radius */
# Line 43 | Line 38 | static FILE  *ambfp = NULL;    /* ambient file pointer */
38   static int  nunflshed = 0;      /* number of unflushed ambient values */
39  
40   #ifndef SORT_THRESH
41 < #ifdef BIGMEM
47 < #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
48 < #else
41 > #ifdef SMLMEM
42   #define SORT_THRESH     ((3L<<20)/sizeof(AMBVAL))
43 + #else
44 + #define SORT_THRESH     ((9L<<20)/sizeof(AMBVAL))
45   #endif
46   #endif
47   #ifndef SORT_INTVL
# Line 56 | Line 51 | static int  nunflshed = 0;     /* number of unflushed ambi
51   #define MAX_SORT_INTVL  (SORT_INTVL<<6)
52   #endif
53  
54 < static COLOR  avsum = BLKCOLOR;         /* computed ambient value sum */
54 > static double  avsum = 0.;              /* computed ambient value sum (log) */
55 > static unsigned int  navsum = 0;        /* number of values in avsum */
56   static unsigned int  nambvals = 0;      /* total number of indirect values */
57   static unsigned int  nambshare = 0;     /* number of values from file */
58   static unsigned long  ambclock = 0;     /* ambient access clock */
59   static unsigned long  lastsort = 0;     /* time of last value sort */
60   static long  sortintvl = SORT_INTVL;    /* time until next sort */
61 + static FILE  *ambinp = NULL;            /* auxiliary file for input */
62 + static long  lastpos = -1;              /* last flush position */
63  
64   #define MAXACLOCK       (1L<<30)        /* clock turnover value */
65          /*
# Line 69 | Line 67 | static long  sortintvl = SORT_INTVL;   /* time until nex
67           * through memory on a multiprocessor, when we want to avoid
68           * claiming our own memory (copy on write).  Go ahead anyway
69           * if more than two thirds of our values are unshared.
70 +         * Compile with -Dtracktime=0 to turn this code off.
71           */
72 + #ifndef tracktime
73   #define tracktime       (shm_boundary == NULL || nambvals > 3*nambshare)
74 + #endif
75  
76   #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
77  
78 < #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
78 > #define  newambval()    (AMBVAL *)malloc(sizeof(AMBVAL))
79 > #define  freeav(av)     free((void *)av);
80  
81 < extern long  ftell(), lseek();
82 < static int  initambfile(), avsave(), avinsert(), sortambvals(), avlmemi();
83 < static AMBVAL  *avstore();
81 > 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   #ifdef  F_SETLKW
97 < static  aflock();
97 > static void aflock(int  typ);
98   #endif
99  
100  
101 < setambres(ar)                           /* set ambient resolution */
102 < int  ar;
101 > extern void
102 > setambres(                              /* set ambient resolution */
103 >        int  ar
104 > )
105   {
106          ambres = ar < 0 ? 0 : ar;               /* may be done already */
107                                                  /* set min & max radii */
# Line 105 | Line 121 | int  ar;
121   }
122  
123  
124 < setambacc(newa)                         /* set ambient accuracy */
125 < double  newa;
124 > extern void
125 > setambacc(                              /* set ambient accuracy */
126 >        double  newa
127 > )
128   {
129          double  ambdiff;
130  
# Line 118 | Line 136 | double  newa;
136   }
137  
138  
139 < setambient(afile)                       /* initialize calculation */
140 < char  *afile;
139 > extern void
140 > setambient(void)                                /* initialize calculation */
141   {
142 +        int     readonly = 0;
143          long  pos, flen;
144          AMBVAL  amb;
145 +                                                /* make sure we're fresh */
146 +        ambdone();
147                                                  /* init ambient limits */
148          setambres(ambres);
149          setambacc(ambacc);
150 <        if (afile == NULL)
150 >        if (ambfile == NULL || !ambfile[0])
151                  return;
152          if (ambacc <= FTINY) {
153                  sprintf(errmsg, "zero ambient accuracy so \"%s\" not opened",
154 <                                afile);
154 >                                ambfile);
155                  error(WARNING, errmsg);
156                  return;
157          }
158                                                  /* open ambient file */
159 <        if ((ambfp = fopen(afile, "r+")) != NULL) {
160 <                initambfile(0);
159 >        if ((ambfp = fopen(ambfile, "r+")) == NULL)
160 >                readonly = (ambfp = fopen(ambfile, "r")) != NULL;
161 >        if (ambfp != NULL) {
162 >                initambfile(0);                 /* file exists */
163                  pos = ftell(ambfp);
164                  while (readambval(&amb, ambfp))
165                          avinsert(avstore(&amb));
166 <                                                /* align */
166 >                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                  pos += (long)nambvals*AMBVALSIZ;
178 <                flen = lseek(fileno(ambfp), 0L, 2);
178 >                flen = lseek(fileno(ambfp), (off_t)0, SEEK_END);
179                  if (flen != pos) {
180                          sprintf(errmsg,
181                          "ignoring last %ld values in ambient file (corrupted)",
182                                          (flen - pos)/AMBVALSIZ);
183                          error(WARNING, errmsg);
184                          fseek(ambfp, pos, 0);
185 <                        ftruncate(fileno(ambfp), pos);
185 > #ifndef _WIN32 /* XXX we need a replacement for that one */
186 >                        ftruncate(fileno(ambfp), (off_t)pos);
187 > #endif
188                  }
189 <                nambshare = nambvals;
190 <        } else if ((ambfp = fopen(afile, "w+")) != NULL)
191 <                initambfile(1);
192 <        else {
158 <                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
189 >        } else if ((ambfp = fopen(ambfile, "w+")) != NULL) {
190 >                initambfile(1);                 /* else create new file */
191 >        } else {
192 >                sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile);
193                  error(SYSTEM, errmsg);
194          }
195 <        nunflshed++;    /* lie */
162 <        ambsync();
195 >        ambsync();                      /* load previous values */
196   }
197  
198  
199 < ambnotify(obj)                  /* record new modifier */
200 < OBJECT  obj;
199 > extern void
200 > ambdone(void)                   /* close ambient file and free memory */
201   {
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 + extern void
226 + ambnotify(                      /* record new modifier */
227 +        OBJECT  obj
228 + )
229 + {
230          static int  hitlimit = 0;
231 <        register OBJREC  *o = objptr(obj);
231 >        register OBJREC  *o;
232          register char  **amblp;
233  
234 +        if (obj == OVOID) {             /* starting over */
235 +                ambset[0] = 0;
236 +                hitlimit = 0;
237 +                return;
238 +        }
239 +        o = objptr(obj);
240          if (hitlimit || !ismodifier(o->otype))
241                  return;
242          for (amblp = amblist; *amblp != NULL; amblp++)
# Line 185 | Line 252 | OBJECT obj;
252   }
253  
254  
255 < ambient(acol, r, nrm)           /* compute ambient component for ray */
256 < COLOR  acol;
257 < register RAY  *r;
258 < FVECT  nrm;
255 > extern void
256 > multambient(            /* compute ambient component & multiply by coef. */
257 >        COLOR  aval,
258 >        register RAY  *r,
259 >        FVECT  nrm
260 > )
261   {
262          static int  rdepth = 0;                 /* ambient recursion */
263 <        double  d;
263 >        COLOR   acol;
264 >        double  d, l;
265  
266          if (ambdiv <= 0)                        /* no ambient calculation */
267                  goto dumbamb;
# Line 204 | Line 274 | FVECT  nrm;
274                  goto dumbamb;
275  
276          if (ambacc <= FTINY) {                  /* no ambient storage */
277 +                copycolor(acol, aval);
278                  rdepth++;
279                  d = doambient(acol, r, r->rweight, NULL, NULL);
280                  rdepth--;
281                  if (d <= FTINY)
282                          goto dumbamb;
283 +                copycolor(aval, acol);
284                  return;
285          }
286  
287          if (tracktime)                          /* sort to minimize thrashing */
288                  sortambvals(0);
289 <                                                /* get ambient value */
289 >                                                /* interpolate ambient value */
290          setcolor(acol, 0.0, 0.0, 0.0);
291          d = sumambient(acol, r, nrm, rdepth,
292                          &atrunk, thescene.cuorg, thescene.cusize);
293          if (d > FTINY) {
294 <                scalecolor(acol, 1.0/d);
294 >                d = 1.0/d;
295 >                scalecolor(acol, d);
296 >                multcolor(aval, acol);
297                  return;
298          }
299          rdepth++;                               /* need to cache new value */
300          d = makeambient(acol, r, nrm, rdepth-1);
301          rdepth--;
302 <        if (d > FTINY)
302 >        if (d > FTINY) {
303 >                multcolor(aval, acol);          /* got new value */
304                  return;
305 +        }
306   dumbamb:                                        /* return global value */
307 <        copycolor(acol, ambval);
308 <        if (ambvwt <= 0 | nambvals == 0)
307 >        if ((ambvwt <= 0) | (navsum == 0)) {
308 >                multcolor(aval, ambval);
309                  return;
310 <        scalecolor(acol, (double)ambvwt);
311 <        addcolor(acol, avsum);                  /* average in computations */
312 <        d = 1.0/(ambvwt+nambvals);
313 <        scalecolor(acol, d);
310 >        }
311 >        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 >                scalecolor(aval, d);
317 >                multcolor(aval, ambval);        /* apply color of ambval */
318 >        } else {
319 >                d = exp( avsum / (double)navsum );
320 >                scalecolor(aval, d);            /* neutral color */
321 >        }
322   }
323  
324  
325 < double
326 < sumambient(acol, r, rn, al, at, c0, s)  /* 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;
325 > 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   {
336          double  d, e1, e2, wt, wsum;
337          COLOR  ct;
# Line 258 | Line 343 | double s;
343          wsum = 0.0;
344                                          /* do this node */
345          for (av = at->alist; av != NULL; av = av->next) {
346 +                double  rn_dot = -2.0;
347                  if (tracktime)
348                          av->latick = ambclock;
349                  /*
# Line 265 | Line 351 | double s;
351                   */
352                  if (av->lvl > al)       /* list sorted, so this works */
353                          break;
354 <                if (av->weight < r->rweight-FTINY)
354 >                if (av->weight < 0.9*r->rweight)
355                          continue;
356                  /*
357                   *  Ambient radius test.
# Line 280 | Line 366 | double s;
366                  if (e1 > ambacc*ambacc*1.21)
367                          continue;
368                  /*
369 <                 *  Normal direction test.
369 >                 *  Direction test using closest normal.
370                   */
371 <                e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
371 >                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                  if (e2 < 0.0) e2 = 0.0;
383                  if (e1 + e2 > ambacc*ambacc*1.21)
384                          continue;
# Line 298 | Line 394 | double s;
394                  /*
395                   *  Jittering final test reduces image artifacts.
396                   */
397 <                wt = sqrt(e1) + sqrt(e2);
397 >                e1 = sqrt(e1);
398 >                e2 = sqrt(e2);
399 >                wt = e1 + e2;
400                  if (wt > ambacc*(.9+.2*urand(9015+samplendx)))
401                          continue;
402 +                /*
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                  if (wt <= 1e-3)
410                          wt = 1e3;
411                  else
# Line 325 | Line 430 | double s;
430                                  break;
431                  }
432                  if (j == 3)
433 <                        wsum += sumambient(acol, r, rn, al, at->kid+i, ck0, s);
433 >                        wsum += sumambient(acol, r, rn, al,
434 >                                                at->kid+i, ck0, s);
435          }
436          return(wsum);
437   }
438  
439  
440 < double
441 < makeambient(acol, r, rn, al)    /* make a new ambient value */
442 < COLOR  acol;
443 < register RAY  *r;
444 < FVECT  rn;
445 < int  al;
440 > extern double
441 > makeambient(            /* make a new ambient value for storage */
442 >        COLOR  acol,
443 >        RAY  *r,
444 >        FVECT  rn,
445 >        int  al
446 > )
447   {
448          AMBVAL  amb;
449          FVECT   gp, gd;
450 <                                                /* compute weight */
451 <        amb.weight = pow(AVGREFL, (double)al);
452 <        if (r->rweight < 0.1*amb.weight)        /* heuristic */
453 <                amb.weight = r->rweight;
450 >        int     i;
451 >
452 >        amb.weight = 1.0;                       /* compute weight */
453 >        for (i = al; i-- > 0; )
454 >                amb.weight *= AVGREFL;
455 >        if (r->rweight < 0.1*amb.weight)        /* heuristic override */
456 >                amb.weight = 1.25*r->rweight;
457 >        setcolor(acol, AVGREFL, AVGREFL, AVGREFL);
458                                                  /* compute ambient */
459          amb.rad = doambient(acol, r, amb.weight, gp, gd);
460 <        if (amb.rad <= FTINY)
460 >        if (amb.rad <= FTINY) {
461 >                setcolor(acol, 0.0, 0.0, 0.0);
462                  return(0.0);
463 <                                                /* store it */
463 >        }
464 >        scalecolor(acol, 1./AVGREFL);           /* undo assumed reflectance */
465 >                                                /* store value */
466          VCOPY(amb.pos, r->rop);
467          VCOPY(amb.dir, r->ron);
468          amb.lvl = al;
# Line 363 | Line 477 | int  al;
477   }
478  
479  
480 < extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
481 < COLOR  cr;
482 < register AMBVAL  *ap;
483 < FVECT  pv, nv;
480 > extern void
481 > extambient(             /* extrapolate value at pv, nv */
482 >        COLOR  cr,
483 >        register AMBVAL  *ap,
484 >        FVECT  pv,
485 >        FVECT  nv
486 > )
487   {
488 <        FVECT  v1, v2;
488 >        FVECT  v1;
489          register int  i;
490          double  d;
491  
# Line 377 | Line 494 | FVECT  pv, nv;
494          for (i = 0; i < 3; i++)
495                  d += ap->gpos[i]*(pv[i]-ap->pos[i]);
496                                          /* gradient due to rotation */
497 <        VCOPY(v1, ap->dir);
498 <        fcross(v2, v1, nv);
382 <        d += DOT(ap->gdir, v2);
497 >        VCROSS(v1, ap->dir, nv);
498 >        d += DOT(ap->gdir, v1);
499          if (d <= 0.0) {
500                  setcolor(cr, 0.0, 0.0, 0.0);
501                  return;
# Line 389 | Line 505 | FVECT  pv, nv;
505   }
506  
507  
508 < static
509 < initambfile(creat)              /* initialize ambient file */
510 < int  creat;
508 > static void
509 > initambfile(            /* initialize ambient file */
510 >        int  creat
511 > )
512   {
513 <        extern char  *progname, *octname, VersionID[];
513 >        extern char  *progname, *octname;
514 >        static char  *mybuf = NULL;
515  
516   #ifdef  F_SETLKW
517          aflock(creat ? F_WRLCK : F_RDLCK);
518   #endif
519 < #ifdef MSDOS
520 <        setmode(fileno(ambfp), O_BINARY);
521 < #endif
522 <        setbuf(ambfp, bmalloc(BUFSIZ+8));
519 >        SET_FILE_BINARY(ambfp);
520 >        if (mybuf == NULL)
521 >                mybuf = (char *)bmalloc(BUFSIZ+8);
522 >        setbuf(ambfp, mybuf);
523          if (creat) {                    /* new file */
524                  newheader("RADIANCE", ambfp);
525                  fprintf(ambfp, "%s -av %g %g %g -aw %d -ab %d -aa %g ",
526                                  progname, colval(ambval,RED),
527                                  colval(ambval,GRN), colval(ambval,BLU),
528                                  ambvwt, ambounce, ambacc);
529 <                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
530 <                                ambdiv, ambssamp, ambres,
531 <                                octname==NULL ? "" : octname);
529 >                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                  fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
536 +                fputnow(ambfp);
537                  fputformat(AMBFMT, ambfp);
538                  putc('\n', ambfp);
539                  putambmagic(ambfp);
# Line 420 | Line 542 | int  creat;
542   }
543  
544  
545 < static
546 < avsave(av)                              /* insert and save an ambient value */
547 < AMBVAL  *av;
545 > static void
546 > avsave(                         /* insert and save an ambient value */
547 >        AMBVAL  *av
548 > )
549   {
550          avinsert(avstore(av));
551          if (ambfp == NULL)
# Line 434 | Line 557 | AMBVAL *av;
557                          goto writerr;
558          return;
559   writerr:
560 <        error(SYSTEM, "error writing ambient file");
560 >        error(SYSTEM, "error writing to ambient file");
561   }
562  
563  
564   static AMBVAL *
565 < avstore(aval)                           /* allocate memory and store aval */
566 < register AMBVAL  *aval;
565 > avstore(                                /* allocate memory and store aval */
566 >        register AMBVAL  *aval
567 > )
568   {
569          register AMBVAL  *av;
570 +        double  d;
571  
572          if ((av = newambval()) == NULL)
573                  error(SYSTEM, "out of memory in avstore");
574 <        copystruct(av, aval);
574 >        *av = *aval;
575          av->latick = ambclock;
576          av->next = NULL;
452        addcolor(avsum, av->val);       /* add to sum for averaging */
577          nambvals++;
578 +        d = bright(av->val);
579 +        if (d > FTINY) {                /* add to log sum for averaging */
580 +                avsum += log(d);
581 +                navsum++;
582 +        }
583          return(av);
584   }
585  
# Line 460 | Line 589 | register AMBVAL  *aval;
589   static AMBTREE  *atfreelist = NULL;     /* free ambient tree structures */
590  
591  
592 < static
593 < AMBTREE *
465 < newambtree()                            /* allocate 8 ambient tree structs */
592 > static AMBTREE *
593 > newambtree(void)                                /* allocate 8 ambient tree structs */
594   {
595          register AMBTREE  *atp, *upperlim;
596  
597          if (atfreelist == NULL) {       /* get more nodes */
598 <                atfreelist = (AMBTREE *)bmalloc(ATALLOCSZ*8*sizeof(AMBTREE));
598 >                atfreelist = (AMBTREE *)malloc(ATALLOCSZ*8*sizeof(AMBTREE));
599                  if (atfreelist == NULL)
600                          return(NULL);
601                                          /* link new free list */
# Line 478 | Line 606 | newambtree()                           /* allocate 8 ambient tree structs */
606          }
607          atp = atfreelist;
608          atfreelist = atp->kid;
609 <        bzero((char *)atp, 8*sizeof(AMBTREE));
609 >        memset((char *)atp, '\0', 8*sizeof(AMBTREE));
610          return(atp);
611   }
612  
613  
614 < static
615 < freeambtree(atp)                        /* free 8 ambient tree structs */
616 < AMBTREE  *atp;
614 > static void
615 > freeambtree(                    /* free 8 ambient tree structs */
616 >        AMBTREE  *atp
617 > )
618   {
619          atp->kid = atfreelist;
620          atfreelist = atp;
621   }
622  
623  
624 < static
625 < avinsert(av)                            /* insert ambient value in our tree */
626 < register AMBVAL  *av;
624 > static void
625 > avinsert(                               /* insert ambient value in our tree */
626 >        void *av
627 > )
628   {
629          register AMBTREE  *at;
630          register AMBVAL  *ap;
# Line 504 | Line 634 | register AMBVAL         *av;
634          int  branch;
635          register int  i;
636  
637 <        if (av->rad <= FTINY)
637 >        if (((AMBVAL*)av)->rad <= FTINY)
638                  error(CONSISTENCY, "zero ambient radius in avinsert");
639          at = &atrunk;
640          VCOPY(ck0, thescene.cuorg);
641          s = thescene.cusize;
642 <        while (s*(OCTSCALE/2) > av->rad*ambacc) {
642 >        while (s*(OCTSCALE/2) > ((AMBVAL*)av)->rad*ambacc) {
643                  if (at->kid == NULL)
644                          if ((at->kid = newambtree()) == NULL)
645                                  error(SYSTEM, "out of memory in avinsert");
646                  s *= 0.5;
647                  branch = 0;
648                  for (i = 0; i < 3; i++)
649 <                        if (av->pos[i] > ck0[i] + s) {
649 >                        if (((AMBVAL*)av)->pos[i] > ck0[i] + s) {
650                                  ck0[i] += s;
651                                  branch |= 1 << i;
652                          }
# Line 524 | Line 654 | register AMBVAL         *av;
654          }
655          avh.next = at->alist;           /* order by increasing level */
656          for (ap = &avh; ap->next != NULL; ap = ap->next)
657 <                if (ap->next->lvl >= av->lvl)
657 >                if (ap->next->lvl >= ((AMBVAL*)av)->lvl)
658                          break;
659 <        av->next = ap->next;
660 <        ap->next = av;
659 >        ((AMBVAL*)av)->next = ap->next;
660 >        ap->next = (AMBVAL*)av;
661          at->alist = avh.next;
662   }
663  
664  
665 < static
666 < unloadatree(at, f)                      /* unload an ambient value tree */
667 < register AMBTREE  *at;
668 < int     (*f)();
665 > static void
666 > unloadatree(                    /* unload an ambient value tree */
667 >        register AMBTREE  *at,
668 >        unloadtf_t *f
669 > )
670   {
671          register AMBVAL  *av;
672          register int  i;
# Line 560 | Line 691 | static struct avl {
691   static AMBVAL   **avlist2;              /* memory positions for sorting */
692   static int      i_avlist;               /* index for lists */
693  
694 + static int alatcmp(const void *av1, const void *av2);
695  
696 < static
697 < av2list(av)
698 < register AMBVAL *av;
696 > static void
697 > av2list(
698 >        void *av
699 > )
700   {
701   #ifdef DEBUG
702          if (i_avlist >= nambvals)
703                  error(CONSISTENCY, "too many ambient values in av2list1");
704   #endif
705 <        avlist1[i_avlist].p = avlist2[i_avlist] = av;
706 <        avlist1[i_avlist++].t = av->latick;
705 >        avlist1[i_avlist].p = avlist2[i_avlist] = (AMBVAL*)av;
706 >        avlist1[i_avlist++].t = ((AMBVAL*)av)->latick;
707   }
708  
709  
710   static int
711 < alatcmp(av1, av2)                       /* compare ambient values for MRA */
712 < struct avl      *av1, *av2;
711 > alatcmp(                        /* compare ambient values for MRA */
712 >        const void *av1,
713 >        const void *av2
714 > )
715   {
716 <        register long  lc = av2->t - av1->t;
716 >        register long  lc = ((struct avl *)av2)->t - ((struct avl *)av1)->t;
717          return(lc<0 ? -1 : lc>0 ? 1 : 0);
718   }
719  
720  
721 + /* 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   static int
728 < aposcmp(avp1, avp2)                     /* compare ambient value positions */
729 < AMBVAL  **avp1, **avp2;
728 > aposcmp(                        /* compare ambient value positions */
729 >        const void      *avp1,
730 >        const void      *avp2
731 > )
732   {
733 <        return(*avp1 - *avp2);
733 >        register long   diff = *(char * const *)avp1 - *(char * const *)avp2;
734 >        if (diff < 0)
735 >                return(-1);
736 >        return(diff > 0);
737   }
738  
593
739   #if 1
740   static int
741 < avlmemi(avaddr)                         /* find list position from address */
742 < AMBVAL  *avaddr;
741 > avlmemi(                                /* find list position from address */
742 >        AMBVAL  *avaddr
743 > )
744   {
745          register AMBVAL  **avlpp;
746  
# Line 610 | Line 756 | AMBVAL *avaddr;
756   #endif
757  
758  
759 < static
760 < sortambvals(always)                     /* resort ambient values */
761 < int     always;
759 > static void
760 > sortambvals(                    /* resort ambient values */
761 >        int     always
762 > )
763   {
764          AMBTREE  oldatrunk;
765          AMBVAL  tav, *tap, *pnext;
# Line 645 | Line 792 | int    always;
792          }
793          if (avlist1 == NULL) {          /* no time tracking -- rebuild tree? */
794                  if (avlist2 != NULL)
795 <                        free((char *)avlist2);
795 >                        free((void *)avlist2);
796                  if (always) {           /* rebuild without sorting */
797 <                        copystruct(&oldatrunk, &atrunk);
797 >                        oldatrunk = atrunk;
798                          atrunk.alist = NULL;
799                          atrunk.kid = NULL;
800                          unloadatree(&oldatrunk, avinsert);
# Line 678 | Line 825 | int    always;
825                          if (avlist1[i].p == NULL)
826                                  continue;
827                          tap = avlist2[i];
828 <                        copystruct(&tav, tap);
828 >                        tav = *tap;
829                          for (j = i; (pnext = avlist1[j].p) != tap;
830                                          j = avlmemi(pnext)) {
831 <                                copystruct(avlist2[j], pnext);
831 >                                *(avlist2[j]) = *pnext;
832                                  avinsert(avlist2[j]);
833                                  avlist1[j].p = NULL;
834                          }
835 <                        copystruct(avlist2[j], &tav);
835 >                        *(avlist2[j]) = tav;
836                          avinsert(avlist2[j]);
837                          avlist1[j].p = NULL;
838                  }
839 <                free((char *)avlist1);
840 <                free((char *)avlist2);
839 >                free((void *)avlist1);
840 >                free((void *)avlist2);
841                                                  /* compute new sort interval */
842                  sortintvl = ambclock - lastsort;
843                  if (sortintvl >= MAX_SORT_INTVL/2)
# Line 709 | Line 856 | int    always;
856  
857   #ifdef  F_SETLKW
858  
859 < static
860 < aflock(typ)                     /* lock/unlock ambient file */
861 < int  typ;
859 > static void
860 > aflock(                 /* lock/unlock ambient file */
861 >        int  typ
862 > )
863   {
864          static struct flock  fls;       /* static so initialized to zeroes */
865  
# Line 721 | Line 869 | int  typ;
869   }
870  
871  
872 < int
873 < ambsync()                       /* synchronize ambient file */
872 > extern int
873 > ambsync(void)                   /* synchronize ambient file */
874   {
727        static FILE  *ambinp = NULL;
728        static long  lastpos = -1;
875          long  flen;
876          AMBVAL  avs;
877          register int  n;
878  
733        if (nunflshed == 0)
734                return(0);
879          if (lastpos < 0)        /* initializing (locked in initambfile) */
880                  goto syncend;
881 <                                /* gain exclusive access */
882 <        aflock(F_WRLCK);
881 >                                /* gain appropriate access */
882 >        aflock(nunflshed ? F_WRLCK : F_RDLCK);
883                                  /* see if file has grown */
884 <        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
884 >        if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0)
885                  goto seekerr;
886 <        if (n = flen - lastpos) {               /* file has grown */
886 >        if ( (n = flen - lastpos) ) {           /* file has grown */
887                  if (ambinp == NULL) {           /* use duplicate filedes */
888                          ambinp = fdopen(dup(fileno(ambfp)), "r");
889                          if (ambinp == NULL)
# Line 750 | Line 894 | ambsync()                      /* synchronize ambient file */
894                  while (n >= AMBVALSIZ) {        /* load contributed values */
895                          if (!readambval(&avs, ambinp)) {
896                                  sprintf(errmsg,
897 <                                "ambient file corrupted near character %ld",
898 <                                                flen - n);
897 >                        "ambient file \"%s\" corrupted near character %ld",
898 >                                                ambfile, flen - n);
899                                  error(WARNING, errmsg);
900                                  break;
901                          }
# Line 760 | Line 904 | ambsync()                      /* synchronize ambient file */
904                  }
905                  /*** seek always as safety measure
906                  if (n) ***/                     /* alignment */
907 <                        if (lseek(fileno(ambfp), flen-n, 0) < 0)
907 >                        if (lseek(fileno(ambfp), (off_t)(flen-n), SEEK_SET) < 0)
908                                  goto seekerr;
909          }
910   #ifdef  DEBUG
# Line 773 | Line 917 | ambsync()                      /* synchronize ambient file */
917   #endif
918   syncend:
919          n = fflush(ambfp);                      /* calls write() at last */
920 <        if ((lastpos = lseek(fileno(ambfp), 0L, 1)) < 0)
920 >        if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0)
921                  goto seekerr;
922          aflock(F_UNLCK);                        /* release file */
923          nunflshed = 0;
924          return(n);
925   seekerr:
926          error(SYSTEM, "seek failed in ambsync");
927 +        return -1; /* pro forma return */
928   }
929  
930   #else
931  
932 < int
933 < ambsync()                       /* flush ambient file */
932 > extern int
933 > ambsync(void)                   /* flush ambient file */
934   {
790        if (nunflshed == 0)
791                return(0);
935          nunflshed = 0;
936          return(fflush(ambfp));
937   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines