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 1.8 by greg, Fri Jan 12 11:30:59 1990 UTC vs.
Revision 2.19 by greg, Wed Aug 4 14:22:11 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 6 | Line 6 | static char SCCSid[] = "$SunId$ LBL";
6  
7   /*
8   *  ambient.c - routines dealing with ambient (inter-reflected) component.
9 *
10 *  The macro AMBFLUSH (if defined) is the number of ambient values
11 *      to wait before flushing to the ambient file.
12 *
13 *     5/9/86
9   */
10  
11   #include  "ray.h"
# Line 19 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "otypes.h"
16  
17 + #include  "ambient.h"
18 +
19   #include  "random.h"
20  
21 < #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
21 > #define  OCTSCALE       0.5     /* ceil((valid rad.)/(cube size)) */
22  
23 + typedef struct ambtree {
24 +        AMBVAL  *alist;         /* ambient value list */
25 +        struct ambtree  *kid;   /* 8 child nodes */
26 + }  AMBTREE;                     /* ambient octree */
27 +
28   extern CUBE  thescene;          /* contains space boundaries */
29  
30 < extern COLOR  ambval;           /* global ambient component */
31 < extern double  ambacc;          /* ambient accuracy */
30 < extern int  ambres;             /* ambient resolution */
31 < extern int  ambdiv;             /* number of divisions for calculation */
32 < extern int  ambssamp;           /* number of super-samples */
33 < extern int  ambounce;           /* number of ambient bounces */
34 < extern char  *amblist[];        /* ambient include/exclude list */
35 < extern int  ambincl;            /* include == 1, exclude == 0 */
30 > #define  MAXASET        511     /* maximum number of elements in ambient set */
31 > OBJECT  ambset[MAXASET+1]={0};  /* ambient include/exclude set */
32  
33 < OBJECT  ambset[256]={0};        /* ambient include/exclude set */
33 > double  maxarad;                /* maximum ambient radius */
34 > double  minarad;                /* minimum ambient radius */
35  
36 < double  maxarad;                /* maximum ambient radius */
40 < double  minarad;                /* minimum ambient radius */
36 > static AMBTREE  atrunk;         /* our ambient trunk node */
37  
38 < typedef struct ambval {
39 <        FVECT  pos;             /* position in space */
44 <        FVECT  dir;             /* normal direction */
45 <        int  lvl;               /* recursion level of parent ray */
46 <        float  weight;          /* weight of parent ray */
47 <        COLOR  val;             /* computed ambient value */
48 <        float  rad;             /* validity radius */
49 <        struct ambval  *next;   /* next in list */
50 < }  AMBVAL;                      /* ambient value */
38 > static FILE  *ambfp = NULL;     /* ambient file pointer */
39 > static int  nunflshed = 0;      /* number of unflushed ambient values */
40  
41 < typedef struct ambtree {
53 <        AMBVAL  *alist;         /* ambient value list */
54 <        struct ambtree  *kid;   /* 8 child nodes */
55 < }  AMBTREE;                     /* ambient octree */
41 > #define  AMBFLUSH       (BUFSIZ/AMBVALSIZ)
42  
43 < typedef struct {
58 <        float  k;               /* error contribution per sample */
59 <        COLOR  v;               /* ray sum */
60 <        int  n;                 /* number of samples */
61 <        short  t, p;            /* theta, phi indices */
62 < }  AMBSAMP;                     /* ambient sample */
43 > #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
44  
45 < static AMBTREE  atrunk;         /* our ambient trunk node */
45 > #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
46  
47 < static FILE  *ambfp = NULL;     /* ambient file pointer */
47 > extern long  ftell(), lseek();
48 > static int  initambfile(), avsave(), avinsert();
49 > #ifdef  F_SETLKW
50 > static  aflock();
51 > #endif
52  
68 #define  newambval()    (AMBVAL *)bmalloc(sizeof(AMBVAL))
53  
54 < #define  newambtree()   (AMBTREE *)calloc(8, sizeof(AMBTREE))
54 > setambres(ar)                           /* set ambient resolution */
55 > int  ar;
56 > {
57 >                                                /* set min & max radii */
58 >        if (ar <= 0) {
59 >                minarad = 0.0;
60 >                maxarad = thescene.cusize / 2.0;
61 >        } else {
62 >                minarad = thescene.cusize / ar;
63 >                maxarad = 16.0 * minarad;               /* heuristic */
64 >                if (maxarad > thescene.cusize / 2.0)
65 >                        maxarad = thescene.cusize / 2.0;
66 >        }
67 >        if (maxarad <= FTINY)
68 >                maxarad = .001;
69 > }
70  
72 double  sumambient(), doambient(), makeambient();
71  
74
72   setambient(afile)                       /* initialize calculation */
73   char  *afile;
74   {
75 <        long  ftell();
76 <        OBJECT  obj;
77 <        AMBVAL  amb;
78 <
79 <        maxarad = thescene.cusize / 2.0;                /* maximum radius */
80 <                                                        /* minimum radius */
81 <        minarad = ambres > 0 ? thescene.cusize/ambres : 0.0;
82 <
83 <                                        /* open ambient file */
84 <        if (afile != NULL)
85 <                if ((ambfp = fopen(afile, "r+")) != NULL) {
86 <                        while (fread(&amb, sizeof(AMBVAL), 1, ambfp) == 1)
87 <                                avinsert(&amb, &atrunk, thescene.cuorg,
88 <                                                thescene.cusize);
89 <                                                        /* align */
90 <                        fseek(ambfp, -(ftell(ambfp)%sizeof(AMBVAL)), 1);
91 <                } else if ((ambfp = fopen(afile, "w")) == NULL) {
92 <                        sprintf(errmsg, "cannot open ambient file \"%s\"",
93 <                                        afile);
94 <                        error(SYSTEM, errmsg);
95 <                }
75 >        long  headlen;
76 >        AMBVAL  amb;
77 >                                                /* init ambient limits */
78 >        setambres(ambres);
79 >        if (afile == NULL)
80 >                return;
81 >                                                /* open ambient file */
82 >        if ((ambfp = fopen(afile, "r+")) != NULL) {
83 >                initambfile(0);
84 >                headlen = ftell(ambfp);
85 >                while (readambval(&amb, ambfp))
86 >                        avinsert(&amb, &atrunk, thescene.cuorg,
87 >                                        thescene.cusize);
88 >                                                /* align */
89 >                fseek(ambfp, -((ftell(ambfp)-headlen)%AMBVALSIZ), 1);
90 >        } else if ((ambfp = fopen(afile, "w+")) != NULL)
91 >                initambfile(1);
92 >        else {
93 >                sprintf(errmsg, "cannot open ambient file \"%s\"", afile);
94 >                error(SYSTEM, errmsg);
95 >        }
96 >        nunflshed++;    /* lie */
97 >        ambsync();
98   }
99  
100  
101   ambnotify(obj)                  /* record new modifier */
102 < OBJECT  obj;
102 > OBJECT  obj;
103   {
104 <        register OBJREC  *o = objptr(obj);
104 >        static int  hitlimit = 0;
105 >        register OBJREC  *o = objptr(obj);
106          register char  **amblp;
107  
108 <        if (!ismodifier(o->otype))
108 >        if (hitlimit || !ismodifier(o->otype))
109                  return;
110          for (amblp = amblist; *amblp != NULL; amblp++)
111                  if (!strcmp(o->oname, *amblp)) {
112 +                        if (ambset[0] >= MAXASET) {
113 +                                error(WARNING, "too many modifiers in ambient list");
114 +                                hitlimit++;
115 +                                return;         /* should this be fatal? */
116 +                        }
117                          insertelem(ambset, obj);
118                          return;
119                  }
# Line 120 | Line 125 | COLOR  acol;
125   register RAY  *r;
126   {
127          static int  rdepth = 0;                 /* ambient recursion */
128 <        double  wsum;
128 >        double  d;
129  
125        rdepth++;                               /* increment level */
126
130          if (ambdiv <= 0)                        /* no ambient calculation */
131                  goto dumbamb;
132                                                  /* check number of bounces */
133 <        if (rdepth > ambounce)
133 >        if (rdepth >= ambounce)
134                  goto dumbamb;
135                                                  /* check ambient list */
136          if (ambincl != -1 && r->ro != NULL &&
# Line 135 | Line 138 | register RAY  *r;
138                  goto dumbamb;
139  
140          if (ambacc <= FTINY) {                  /* no ambient storage */
141 <                if (doambient(acol, r) == 0.0)
141 >                rdepth++;
142 >                d = doambient(acol, r, r->rweight, NULL, NULL);
143 >                rdepth--;
144 >                if (d == 0.0)
145                          goto dumbamb;
146 <                goto done;
146 >                return;
147          }
148                                                  /* get ambient value */
149          setcolor(acol, 0.0, 0.0, 0.0);
150 <        wsum = sumambient(acol, r, &atrunk, thescene.cuorg, thescene.cusize);
151 <        if (wsum > FTINY)
152 <                scalecolor(acol, 1.0/wsum);
153 <        else if (makeambient(acol, r) == 0.0)
154 <                goto dumbamb;
155 <        goto done;
156 <
150 >        d = sumambient(acol, r, rdepth,
151 >                        &atrunk, thescene.cuorg, thescene.cusize);
152 >        if (d > FTINY)
153 >                scalecolor(acol, 1.0/d);
154 >        else {
155 >                d = makeambient(acol, r, rdepth++);
156 >                rdepth--;
157 >        }
158 >        if (d > FTINY)
159 >                return;
160   dumbamb:                                        /* return global value */
161          copycolor(acol, ambval);
153 done:                                           /* must finish here! */
154        rdepth--;
162   }
163  
164  
165   double
166 < sumambient(acol, r, at, c0, s)          /* get interpolated ambient value */
166 > sumambient(acol, r, al, at, c0, s)      /* get interpolated ambient value */
167   COLOR  acol;
168   register RAY  *r;
169 < AMBTREE  *at;
169 > int  al;
170 > AMBTREE  *at;
171   FVECT  c0;
172 < double  s;
172 > double  s;
173   {
174 <        extern double  sqrt();
167 <        double  d, e1, e2, wt, wsum;
174 >        double  d, e1, e2, wt, wsum;
175          COLOR  ct;
176          FVECT  ck0;
177          int  i;
178          register int  j;
179 <        register AMBVAL  *av;
179 >        register AMBVAL  *av;
180                                          /* do this node */
181          wsum = 0.0;
182          for (av = at->alist; av != NULL; av = av->next) {
183                  /*
184 <                 *  Ray strength test.
184 >                 *  Ambient level test.
185                   */
186 <                if (av->lvl > r->rlvl || av->weight < r->rweight-FTINY)
186 >                if (av->lvl > al || av->weight < r->rweight-FTINY)
187                          continue;
188                  /*
189                   *  Ambient radius test.
# Line 203 | Line 210 | double  s;
210                  for (j = 0; j < 3; j++)
211                          d += (r->rop[j] - av->pos[j]) *
212                                          (av->dir[j] + r->ron[j]);
213 <                if (d < -minarad)
213 >                if (d*0.5 < -minarad*ambacc-.001)
214                          continue;
215                  /*
216                   *  Jittering final test reduces image artifacts.
217                   */
218                  wt = sqrt(e1) + sqrt(e2);
219 <                wt *= .9 + .2*frandom();
219 >                wt *= .9 + .2*urand(9015+samplendx);
220                  if (wt > ambacc)
221                          continue;
222                  if (wt <= 1e-3)
# Line 217 | Line 224 | double  s;
224                  else
225                          wt = 1.0 / wt;
226                  wsum += wt;
227 <                copycolor(ct, av->val);
227 >                extambient(ct, av, r->rop, r->ron);
228                  scalecolor(ct, wt);
229                  addcolor(acol, ct);
230          }
# Line 236 | Line 243 | double  s;
243                                  break;
244                  }
245                  if (j == 3)
246 <                        wsum += sumambient(acol, r, at->kid+i, ck0, s);
246 >                        wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
247          }
248          return(wsum);
249   }
250  
251  
252   double
253 < makeambient(acol, r)            /* make a new ambient value */
253 > makeambient(acol, r, al)        /* make a new ambient value */
254   COLOR  acol;
255   register RAY  *r;
256 + int  al;
257   {
258 <        AMBVAL  amb;
259 <
260 <        amb.rad = doambient(acol, r);           /* compute ambient */
258 >        AMBVAL  amb;
259 >        FVECT   gp, gd;
260 >                                                /* compute weight */
261 >        amb.weight = pow(AVGREFL, (double)al);
262 >        if (r->rweight < 0.2*amb.weight)        /* heuristic */
263 >                amb.weight = r->rweight;
264 >                                                /* compute ambient */
265 >        amb.rad = doambient(acol, r, amb.weight, gp, gd);
266          if (amb.rad == 0.0)
267                  return(0.0);
268                                                  /* store it */
269          VCOPY(amb.pos, r->rop);
270          VCOPY(amb.dir, r->ron);
271 <        amb.lvl = r->rlvl;
259 <        amb.weight = r->rweight;
271 >        amb.lvl = al;
272          copycolor(amb.val, acol);
273 +        VCOPY(amb.gpos, gp);
274 +        VCOPY(amb.gdir, gd);
275                                                  /* insert into tree */
276 <        avinsert(&amb, &atrunk, thescene.cuorg, thescene.cusize);
263 <        avsave(&amb);                           /* write to file */
276 >        avsave(&amb);                           /* and save to file */
277          return(amb.rad);
278   }
279  
280  
281 < double
282 < doambient(acol, r)                      /* compute ambient component */
283 < COLOR  acol;
284 < register RAY  *r;
281 > extambient(cr, ap, pv, nv)              /* extrapolate value at pv, nv */
282 > COLOR  cr;
283 > register AMBVAL  *ap;
284 > FVECT  pv, nv;
285   {
286 <        extern int  ambcmp();
287 <        extern double  sin(), cos(), sqrt();
288 <        double  phi, xd, yd, zd;
276 <        double  b, b2;
277 <        register AMBSAMP  *div;
278 <        AMBSAMP  dnew;
279 <        RAY  ar;
280 <        FVECT  ux, uy;
281 <        double  arad;
282 <        int  ndivs, nt, np, ns, ne, i, j;
283 <        register int  k;
286 >        FVECT  v1, v2;
287 >        register int  i;
288 >        double  d;
289  
290 <        setcolor(acol, 0.0, 0.0, 0.0);
291 <                                        /* set number of divisions */
292 <        nt = sqrt(ambdiv * r->rweight * 0.5) + 0.5;
293 <        np = 2 * nt;
294 <        ndivs = nt * np;
295 <                                        /* check first */
296 <        if (ndivs == 0 || rayorigin(&ar, r, AMBIENT, 0.5) < 0)
297 <                return(0.0);
298 <                                        /* set number of super-samples */
299 <        ns = ambssamp * r->rweight + 0.5;
300 <        if (ns > 0) {
296 <                div = (AMBSAMP *)malloc(ndivs*sizeof(AMBSAMP));
297 <                if (div == NULL)
298 <                        error(SYSTEM, "out of memory in doambient");
290 >        d = 1.0;                        /* zeroeth order */
291 >                                        /* gradient due to translation */
292 >        for (i = 0; i < 3; i++)
293 >                d += ap->gpos[i]*(pv[i]-ap->pos[i]);
294 >                                        /* gradient due to rotation */
295 >        VCOPY(v1, ap->dir);
296 >        fcross(v2, v1, nv);
297 >        d += DOT(ap->gdir, v2);
298 >        if (d <= 0.0) {
299 >                setcolor(cr, 0.0, 0.0, 0.0);
300 >                return;
301          }
302 <                                        /* make axes */
303 <        uy[0] = uy[1] = uy[2] = 0.0;
302 <        for (k = 0; k < 3; k++)
303 <                if (r->ron[k] < 0.6 && r->ron[k] > -0.6)
304 <                        break;
305 <        uy[k] = 1.0;
306 <        fcross(ux, r->ron, uy);
307 <        normalize(ux);
308 <        fcross(uy, ux, r->ron);
309 <                                                /* sample divisions */
310 <        arad = 0.0;
311 <        ne = 0;
312 <        for (i = 0; i < nt; i++)
313 <                for (j = 0; j < np; j++) {
314 <                        rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
315 <                        zd = sqrt((i+frandom())/nt);
316 <                        phi = 2.0*PI * (j+frandom())/np;
317 <                        xd = cos(phi) * zd;
318 <                        yd = sin(phi) * zd;
319 <                        zd = sqrt(1.0 - zd*zd);
320 <                        for (k = 0; k < 3; k++)
321 <                                ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
322 <                        rayvalue(&ar);
323 <                        if (ar.rot < FHUGE)
324 <                                arad += 1.0 / ar.rot;
325 <                        if (ns > 0) {                   /* save division */
326 <                                div[ne].k = 0.0;
327 <                                copycolor(div[ne].v, ar.rcol);
328 <                                div[ne].n = 0;
329 <                                div[ne].t = i; div[ne].p = j;
330 <                                                        /* sum errors */
331 <                                b = bright(ar.rcol);
332 <                                if (i > 0) {            /* from above */
333 <                                        b2 = bright(div[ne-np].v) - b;
334 <                                        b2 *= b2 * 0.25;
335 <                                        div[ne].k += b2;
336 <                                        div[ne].n++;
337 <                                        div[ne-np].k += b2;
338 <                                        div[ne-np].n++;
339 <                                }
340 <                                if (j > 0) {            /* from behind */
341 <                                        b2 = bright(div[ne-1].v) - b;
342 <                                        b2 *= b2 * 0.25;
343 <                                        div[ne].k += b2;
344 <                                        div[ne].n++;
345 <                                        div[ne-1].k += b2;
346 <                                        div[ne-1].n++;
347 <                                }
348 <                                if (j == np-1) {        /* around */
349 <                                        b2 = bright(div[ne-(np-1)].v) - b;
350 <                                        b2 *= b2 * 0.25;
351 <                                        div[ne].k += b2;
352 <                                        div[ne].n++;
353 <                                        div[ne-(np-1)].k += b2;
354 <                                        div[ne-(np-1)].n++;
355 <                                }
356 <                                ne++;
357 <                        } else
358 <                                addcolor(acol, ar.rcol);
359 <                }
360 <        for (k = 0; k < ne; k++) {              /* compute errors */
361 <                if (div[k].n > 1)
362 <                        div[k].k /= div[k].n;
363 <                div[k].n = 1;
364 <        }
365 <                                                /* sort the divisions */
366 <        qsort(div, ne, sizeof(AMBSAMP), ambcmp);
367 <                                                /* skim excess */
368 <        while (ne > ns) {
369 <                ne--;
370 <                addcolor(acol, div[ne].v);
371 <        }
372 <                                                /* super-sample */
373 <        for (i = ns; i > 0; i--) {
374 <                rayorigin(&ar, r, AMBIENT, 0.5);        /* pretested */
375 <                zd = sqrt((div[0].t+frandom())/nt);
376 <                phi = 2.0*PI * (div[0].p+frandom())/np;
377 <                xd = cos(phi) * zd;
378 <                yd = sin(phi) * zd;
379 <                zd = sqrt(1.0 - zd*zd);
380 <                for (k = 0; k < 3; k++)
381 <                        ar.rdir[k] = xd*ux[k]+yd*uy[k]+zd*r->ron[k];
382 <                rayvalue(&ar);
383 <                if (ar.rot < FHUGE)
384 <                        arad += 1.0 / ar.rot;
385 <                                                /* recompute error */
386 <                copycolor(dnew.v, div[0].v);
387 <                addcolor(dnew.v, ar.rcol);
388 <                dnew.n = div[0].n + 1;
389 <                dnew.t = div[0].t; dnew.p = div[0].p;
390 <                b2 = bright(dnew.v)/dnew.n - bright(ar.rcol);
391 <                b2 = b2*b2 + div[0].k*(div[0].n*div[0].n);
392 <                dnew.k = b2/(dnew.n*dnew.n);
393 <                                                /* reinsert */
394 <                for (k = 0; k < ne-1 && dnew.k < div[k+1].k; k++)
395 <                        bcopy(&div[k+1], &div[k], sizeof(AMBSAMP));
396 <                bcopy(&dnew, &div[k], sizeof(AMBSAMP));
397 <
398 <                if (ne >= i) {          /* extract darkest division */
399 <                        ne--;
400 <                        if (div[ne].n > 1)
401 <                                scalecolor(div[ne].v, 1.0/div[ne].n);
402 <                        addcolor(acol, div[ne].v);
403 <                }
404 <        }
405 <        scalecolor(acol, 1.0/ndivs);
406 <        if (arad <= FTINY)
407 <                arad = FHUGE;
408 <        else
409 <                arad = (ndivs+ns) / arad / sqrt(r->rweight);
410 <        if (arad > maxarad)
411 <                arad = maxarad;
412 <        else if (arad < minarad)
413 <                arad = minarad;
414 <        if (ns > 0)
415 <                free((char *)div);
416 <        return(arad);
302 >        copycolor(cr, ap->val);
303 >        scalecolor(cr, d);
304   }
305  
306  
307 < static int
308 < ambcmp(d1, d2)                          /* decreasing order */
309 < AMBSAMP  *d1, *d2;
307 > static
308 > initambfile(creat)              /* initialize ambient file */
309 > int  creat;
310   {
311 <        if (d1->k < d2->k)
312 <                return(1);
313 <        if (d1->k > d2->k)
314 <                return(-1);
315 <        return(0);
311 >        extern char  *progname, *octname, VersionID[];
312 >
313 > #ifdef  F_SETLKW
314 >        aflock(creat ? F_WRLCK : F_RDLCK);
315 > #endif
316 > #ifdef MSDOS
317 >        setmode(fileno(ambfp), O_BINARY);
318 > #endif
319 >        setbuf(ambfp, bmalloc(BUFSIZ));
320 >        if (creat) {                    /* new file */
321 >                fprintf(ambfp, "%s -av %g %g %g -ab %d -aa %g ",
322 >                                progname, colval(ambval,RED),
323 >                                colval(ambval,GRN), colval(ambval,BLU),
324 >                                ambounce, ambacc);
325 >                fprintf(ambfp, "-ad %d -as %d -ar %d %s\n",
326 >                                ambdiv, ambssamp, ambres,
327 >                                octname==NULL ? "" : octname);
328 >                fprintf(ambfp, "SOFTWARE= %s\n", VersionID);
329 >                fputformat(AMBFMT, ambfp);
330 >                putc('\n', ambfp);
331 >                putambmagic(ambfp);
332 >        } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
333 >                error(USER, "bad ambient file");
334   }
335  
336  
337   static
338 < avsave(av)                              /* save an ambient value */
339 < AMBVAL  *av;
338 > avsave(av)                              /* insert and save an ambient value */
339 > AMBVAL  *av;
340   {
341 < #ifdef  AMBFLUSH
437 <        static int  nunflshed = 0;
438 < #endif
341 >        avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
342          if (ambfp == NULL)
343                  return;
344 <        if (fwrite(av, sizeof(AMBVAL), 1, ambfp) != 1)
344 >        if (writambval(av, ambfp) < 0)
345                  goto writerr;
346 < #ifdef  AMBFLUSH
347 <        if (++nunflshed >= AMBFLUSH) {
445 <                if (fflush(ambfp) == EOF)
346 >        if (++nunflshed >= AMBFLUSH)
347 >                if (ambsync() == EOF)
348                          goto writerr;
447                nunflshed = 0;
448        }
449 #endif
349          return;
350   writerr:
351          error(SYSTEM, "error writing ambient file");
# Line 455 | Line 354 | writerr:
354  
355   static
356   avinsert(aval, at, c0, s)               /* insert ambient value in a tree */
357 < AMBVAL  *aval;
357 > AMBVAL  *aval;
358   register AMBTREE  *at;
359   FVECT  c0;
360 < double  s;
360 > double  s;
361   {
362          FVECT  ck0;
363          int  branch;
364 <        register AMBVAL  *av;
364 >        register AMBVAL  *av;
365          register int  i;
366  
367          if ((av = newambval()) == NULL)
368                  goto memerr;
369 <        bcopy(aval, av, sizeof(AMBVAL));
369 >        copystruct(av, aval);
370          VCOPY(ck0, c0);
371          while (s*(OCTSCALE/2) > av->rad*ambacc) {
372                  if (at->kid == NULL)
# Line 488 | Line 387 | double  s;
387   memerr:
388          error(SYSTEM, "out of memory in avinsert");
389   }
390 +
391 +
392 + #ifdef  F_SETLKW
393 +
394 + static
395 + aflock(typ)                     /* lock/unlock ambient file */
396 + int  typ;
397 + {
398 +        static struct flock  fls;       /* static so initialized to zeroes */
399 +
400 +        fls.l_type = typ;
401 +        if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0)
402 +                error(SYSTEM, "cannot (un)lock ambient file");
403 + }
404 +
405 +
406 + int
407 + ambsync()                       /* synchronize ambient file */
408 + {
409 +        static FILE  *ambinp = NULL;
410 +        static long  lastpos = -1;
411 +        long  flen;
412 +        AMBVAL  avs;
413 +        register int  n;
414 +
415 +        if (nunflshed == 0)
416 +                return(0);
417 +        if (lastpos < 0)        /* initializing (locked in initambfile) */
418 +                goto syncend;
419 +                                /* gain exclusive access */
420 +        aflock(F_WRLCK);
421 +                                /* see if file has grown */
422 +        if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0)
423 +                error(SYSTEM, "cannot seek on ambient file");
424 +        if (n = flen - lastpos) {               /* file has grown */
425 +                if (ambinp == NULL) {           /* use duplicate filedes */
426 +                        ambinp = fdopen(dup(fileno(ambfp)), "r");
427 +                        if (ambinp == NULL)
428 +                                error(SYSTEM, "fdopen failed in ambsync");
429 +                }
430 +                if (fseek(ambinp, lastpos, 0) < 0)
431 +                        error(SYSTEM, "fseek failed in ambsync");
432 +                while (n >= AMBVALSIZ) {        /* load contributed values */
433 +                        readambval(&avs, ambinp);
434 +                        avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
435 +                        n -= AMBVALSIZ;
436 +                }
437 +                if (n)                          /* alignment */
438 +                        lseek(fileno(ambfp), flen-n, 0);
439 +        }
440 + syncend:
441 +        n = fflush(ambfp);                      /* calls write() at last */
442 +        lastpos = lseek(fileno(ambfp), 0L, 1);
443 +        aflock(F_UNLCK);                        /* release file */
444 +        nunflshed = 0;
445 +        return(n);
446 + }
447 +
448 + #else
449 +
450 + int
451 + ambsync()                       /* flush ambient file */
452 + {
453 +        if (nunflshed == 0)
454 +                return(0);
455 +        nunflshed = 0;
456 +        return(fflush(ambfp));
457 + }
458 +
459 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines