ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambient.c
Revision: 2.19
Committed: Wed Aug 4 14:22:11 1993 UTC (30 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.18: +39 -29 lines
Log Message:
fixed bug due to NFS sloppiness on writing files w/o lock manager

File Contents

# User Rev Content
1 greg 2.15 /* Copyright (c) 1993 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * ambient.c - routines dealing with ambient (inter-reflected) component.
9     */
10    
11     #include "ray.h"
12    
13     #include "octree.h"
14    
15 greg 1.8 #include "otypes.h"
16    
17 greg 1.14 #include "ambient.h"
18    
19 greg 1.1 #include "random.h"
20    
21 greg 2.12 #define OCTSCALE 0.5 /* ceil((valid rad.)/(cube size)) */
22 greg 1.1
23 greg 1.14 typedef struct ambtree {
24 greg 2.12 AMBVAL *alist; /* ambient value list */
25     struct ambtree *kid; /* 8 child nodes */
26 greg 1.14 } AMBTREE; /* ambient octree */
27    
28 greg 1.1 extern CUBE thescene; /* contains space boundaries */
29    
30 greg 2.12 #define MAXASET 511 /* maximum number of elements in ambient set */
31     OBJECT ambset[MAXASET+1]={0}; /* ambient include/exclude set */
32 greg 1.1
33 greg 2.12 double maxarad; /* maximum ambient radius */
34     double minarad; /* minimum ambient radius */
35 greg 1.1
36 greg 2.12 static AMBTREE atrunk; /* our ambient trunk node */
37 greg 1.1
38     static FILE *ambfp = NULL; /* ambient file pointer */
39 greg 2.16 static int nunflshed = 0; /* number of unflushed ambient values */
40 greg 1.1
41 greg 2.12 #define AMBFLUSH (BUFSIZ/AMBVALSIZ)
42 greg 2.6
43 greg 2.12 #define newambval() (AMBVAL *)bmalloc(sizeof(AMBVAL))
44 greg 1.1
45 greg 2.12 #define newambtree() (AMBTREE *)calloc(8, sizeof(AMBTREE))
46 greg 1.1
47 greg 2.7 extern long ftell(), lseek();
48 greg 2.16 static int initambfile(), avsave(), avinsert();
49 greg 2.19 #ifdef F_SETLKW
50     static aflock();
51     #endif
52 greg 1.1
53 greg 2.7
54 greg 2.3 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 greg 2.4 if (maxarad <= FTINY)
68     maxarad = .001;
69 greg 2.3 }
70    
71    
72 greg 1.1 setambient(afile) /* initialize calculation */
73     char *afile;
74     {
75 greg 2.9 long headlen;
76 greg 2.12 AMBVAL amb;
77 greg 2.3 /* init ambient limits */
78     setambres(ambres);
79 greg 2.19 if (afile == NULL)
80     return;
81 greg 2.3 /* open ambient file */
82 greg 2.19 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 greg 2.16 }
96 greg 2.19 nunflshed++; /* lie */
97     ambsync();
98 greg 1.8 }
99    
100    
101     ambnotify(obj) /* record new modifier */
102 greg 2.12 OBJECT obj;
103 greg 1.8 {
104 greg 1.11 static int hitlimit = 0;
105 greg 2.12 register OBJREC *o = objptr(obj);
106 greg 1.8 register char **amblp;
107    
108 greg 1.11 if (hitlimit || !ismodifier(o->otype))
109 greg 1.8 return;
110     for (amblp = amblist; *amblp != NULL; amblp++)
111     if (!strcmp(o->oname, *amblp)) {
112 greg 1.11 if (ambset[0] >= MAXASET) {
113     error(WARNING, "too many modifiers in ambient list");
114     hitlimit++;
115     return; /* should this be fatal? */
116     }
117 greg 1.8 insertelem(ambset, obj);
118     return;
119 greg 1.1 }
120     }
121    
122    
123     ambient(acol, r) /* compute ambient component for ray */
124     COLOR acol;
125     register RAY *r;
126     {
127     static int rdepth = 0; /* ambient recursion */
128 greg 2.12 double d;
129 greg 1.1
130     if (ambdiv <= 0) /* no ambient calculation */
131     goto dumbamb;
132     /* check number of bounces */
133 greg 1.16 if (rdepth >= ambounce)
134 greg 1.1 goto dumbamb;
135     /* check ambient list */
136     if (ambincl != -1 && r->ro != NULL &&
137     ambincl != inset(ambset, r->ro->omod))
138     goto dumbamb;
139    
140     if (ambacc <= FTINY) { /* no ambient storage */
141 greg 1.16 rdepth++;
142     d = doambient(acol, r, r->rweight, NULL, NULL);
143     rdepth--;
144     if (d == 0.0)
145 greg 1.1 goto dumbamb;
146 greg 1.16 return;
147 greg 1.1 }
148     /* get ambient value */
149     setcolor(acol, 0.0, 0.0, 0.0);
150 greg 1.16 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 greg 1.1 dumbamb: /* return global value */
161     copycolor(acol, ambval);
162     }
163    
164    
165     double
166 greg 1.16 sumambient(acol, r, al, at, c0, s) /* get interpolated ambient value */
167 greg 1.1 COLOR acol;
168     register RAY *r;
169 greg 1.16 int al;
170 greg 2.12 AMBTREE *at;
171 greg 1.1 FVECT c0;
172 greg 2.12 double s;
173 greg 1.1 {
174 greg 2.12 double d, e1, e2, wt, wsum;
175 greg 1.1 COLOR ct;
176     FVECT ck0;
177     int i;
178     register int j;
179 greg 2.12 register AMBVAL *av;
180 greg 1.7 /* do this node */
181 greg 1.1 wsum = 0.0;
182     for (av = at->alist; av != NULL; av = av->next) {
183     /*
184 greg 1.16 * Ambient level test.
185 greg 1.1 */
186 greg 1.16 if (av->lvl > al || av->weight < r->rweight-FTINY)
187 greg 1.1 continue;
188     /*
189     * Ambient radius test.
190     */
191     e1 = 0.0;
192     for (j = 0; j < 3; j++) {
193     d = av->pos[j] - r->rop[j];
194     e1 += d * d;
195     }
196     e1 /= av->rad * av->rad;
197     if (e1 > ambacc*ambacc*1.21)
198     continue;
199     /*
200     * Normal direction test.
201     */
202     e2 = (1.0 - DOT(av->dir, r->ron)) * r->rweight;
203     if (e2 < 0.0) e2 = 0.0;
204     if (e1 + e2 > ambacc*ambacc*1.21)
205     continue;
206     /*
207     * Ray behind test.
208     */
209     d = 0.0;
210     for (j = 0; j < 3; j++)
211     d += (r->rop[j] - av->pos[j]) *
212     (av->dir[j] + r->ron[j]);
213 greg 1.18 if (d*0.5 < -minarad*ambacc-.001)
214 greg 1.1 continue;
215     /*
216     * Jittering final test reduces image artifacts.
217     */
218     wt = sqrt(e1) + sqrt(e2);
219 greg 2.2 wt *= .9 + .2*urand(9015+samplendx);
220 greg 1.6 if (wt > ambacc)
221 greg 1.1 continue;
222     if (wt <= 1e-3)
223     wt = 1e3;
224     else
225     wt = 1.0 / wt;
226     wsum += wt;
227 greg 1.15 extambient(ct, av, r->rop, r->ron);
228 greg 1.1 scalecolor(ct, wt);
229     addcolor(acol, ct);
230 greg 1.7 }
231     if (at->kid == NULL)
232     return(wsum);
233     /* do children */
234     s *= 0.5;
235     for (i = 0; i < 8; i++) {
236     for (j = 0; j < 3; j++) {
237     ck0[j] = c0[j];
238     if (1<<j & i)
239     ck0[j] += s;
240     if (r->rop[j] < ck0[j] - OCTSCALE*s)
241     break;
242     if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s)
243     break;
244     }
245     if (j == 3)
246 greg 1.16 wsum += sumambient(acol, r, al, at->kid+i, ck0, s);
247 greg 1.1 }
248     return(wsum);
249     }
250    
251    
252     double
253 greg 1.16 makeambient(acol, r, al) /* make a new ambient value */
254 greg 1.1 COLOR acol;
255     register RAY *r;
256 greg 1.16 int al;
257 greg 1.1 {
258 greg 2.12 AMBVAL amb;
259 greg 1.14 FVECT gp, gd;
260 greg 1.16 /* compute weight */
261     amb.weight = pow(AVGREFL, (double)al);
262 greg 1.17 if (r->rweight < 0.2*amb.weight) /* heuristic */
263 greg 1.16 amb.weight = r->rweight;
264     /* compute ambient */
265     amb.rad = doambient(acol, r, amb.weight, gp, gd);
266 greg 1.1 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 greg 1.16 amb.lvl = al;
272 greg 1.1 copycolor(amb.val, acol);
273 greg 1.14 VCOPY(amb.gpos, gp);
274     VCOPY(amb.gdir, gd);
275 greg 1.1 /* insert into tree */
276 greg 2.7 avsave(&amb); /* and save to file */
277 greg 1.1 return(amb.rad);
278 greg 1.15 }
279    
280    
281     extambient(cr, ap, pv, nv) /* extrapolate value at pv, nv */
282     COLOR cr;
283 greg 2.12 register AMBVAL *ap;
284 greg 1.15 FVECT pv, nv;
285     {
286     FVECT v1, v2;
287     register int i;
288 greg 2.12 double d;
289 greg 1.15
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     copycolor(cr, ap->val);
303     scalecolor(cr, d);
304 greg 1.1 }
305    
306    
307     static
308 greg 2.9 initambfile(creat) /* initialize ambient file */
309     int creat;
310     {
311     extern char *progname, *octname, VersionID[];
312    
313 greg 2.19 #ifdef F_SETLKW
314     aflock(creat ? F_WRLCK : F_RDLCK);
315     #endif
316 greg 2.12 #ifdef MSDOS
317     setmode(fileno(ambfp), O_BINARY);
318     #endif
319 greg 2.9 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 greg 2.17 } else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp))
333     error(USER, "bad ambient file");
334 greg 2.9 }
335    
336    
337     static
338 greg 2.7 avsave(av) /* insert and save an ambient value */
339 greg 2.12 AMBVAL *av;
340 greg 1.1 {
341 greg 2.7 avinsert(av, &atrunk, thescene.cuorg, thescene.cusize);
342 greg 1.1 if (ambfp == NULL)
343     return;
344 greg 2.5 if (writambval(av, ambfp) < 0)
345 greg 1.1 goto writerr;
346 greg 2.16 if (++nunflshed >= AMBFLUSH)
347 greg 2.7 if (ambsync() == EOF)
348 greg 1.1 goto writerr;
349     return;
350     writerr:
351 greg 2.17 error(SYSTEM, "error writing ambient file");
352 greg 1.1 }
353    
354    
355     static
356     avinsert(aval, at, c0, s) /* insert ambient value in a tree */
357 greg 2.12 AMBVAL *aval;
358 greg 1.1 register AMBTREE *at;
359     FVECT c0;
360 greg 2.12 double s;
361 greg 1.1 {
362     FVECT ck0;
363     int branch;
364 greg 2.12 register AMBVAL *av;
365 greg 1.1 register int i;
366    
367     if ((av = newambval()) == NULL)
368     goto memerr;
369 greg 1.9 copystruct(av, aval);
370 greg 1.1 VCOPY(ck0, c0);
371     while (s*(OCTSCALE/2) > av->rad*ambacc) {
372     if (at->kid == NULL)
373     if ((at->kid = newambtree()) == NULL)
374     goto memerr;
375     s *= 0.5;
376     branch = 0;
377     for (i = 0; i < 3; i++)
378     if (av->pos[i] > ck0[i] + s) {
379     ck0[i] += s;
380     branch |= 1 << i;
381     }
382     at = at->kid + branch;
383     }
384     av->next = at->alist;
385     at->alist = av;
386     return;
387     memerr:
388     error(SYSTEM, "out of memory in avinsert");
389 greg 2.7 }
390    
391    
392 greg 2.18 #ifdef F_SETLKW
393 greg 2.10
394 greg 2.19 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 greg 2.16 int
407 greg 2.7 ambsync() /* synchronize ambient file */
408     {
409     static FILE *ambinp = NULL;
410 greg 2.15 static long lastpos = -1;
411     long flen;
412 greg 2.12 AMBVAL avs;
413 greg 2.7 register int n;
414 greg 2.16
415     if (nunflshed == 0)
416     return(0);
417 greg 2.19 if (lastpos < 0) /* initializing (locked in initambfile) */
418     goto syncend;
419 greg 2.7 /* gain exclusive access */
420 greg 2.19 aflock(F_WRLCK);
421 greg 2.7 /* see if file has grown */
422 greg 2.15 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 greg 2.17 if (ambinp == NULL) { /* use duplicate filedes */
426     ambinp = fdopen(dup(fileno(ambfp)), "r");
427 greg 2.14 if (ambinp == NULL)
428 greg 2.17 error(SYSTEM, "fdopen failed in ambsync");
429 greg 2.14 }
430 greg 2.15 if (fseek(ambinp, lastpos, 0) < 0)
431     error(SYSTEM, "fseek failed in ambsync");
432     while (n >= AMBVALSIZ) { /* load contributed values */
433 greg 2.7 readambval(&avs, ambinp);
434     avinsert(&avs,&atrunk,thescene.cuorg,thescene.cusize);
435 greg 2.15 n -= AMBVALSIZ;
436     }
437     if (n) /* alignment */
438 greg 2.10 lseek(fileno(ambfp), flen-n, 0);
439 greg 2.7 }
440 greg 2.15 syncend:
441 greg 2.7 n = fflush(ambfp); /* calls write() at last */
442 greg 2.15 lastpos = lseek(fileno(ambfp), 0L, 1);
443 greg 2.19 aflock(F_UNLCK); /* release file */
444 greg 2.16 nunflshed = 0;
445 greg 2.7 return(n);
446 greg 2.18 }
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 greg 1.1 }
458 greg 2.10
459     #endif