ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/ambio.c
Revision: 2.3
Committed: Tue May 28 13:42:46 1996 UTC (27 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +32 -1 lines
Log Message:
added check for ambient value validity, ambvalOK(), called by readambval()

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Read and write portable ambient values
9 */
10
11 #include <stdio.h>
12
13 #include "color.h"
14
15 #include "fvect.h"
16
17 #include "ambient.h"
18
19
20 #define putvec(v,fp) putflt((v)[0],fp);putflt((v)[1],fp);putflt((v)[2],fp)
21
22 #define getvec(v,fp) (v)[0]=getflt(fp);(v)[1]=getflt(fp);(v)[2]=getflt(fp)
23
24 #define badflt(x) ((x) < -FHUGE || (x) > FHUGE)
25
26 #define badvec(v) (badflt((v)[0]) || badflt((v)[1]) || badflt((v)[2]))
27
28
29 extern double getflt();
30 extern long getint();
31
32
33 putambmagic(fp) /* write out ambient value magic number */
34 FILE *fp;
35 {
36 putint((long)AMBMAGIC, 2, fp);
37 }
38
39
40 hasambmagic(fp) /* read in and check validity of magic # */
41 FILE *fp;
42 {
43 register int magic;
44
45 magic = getint(2, fp);
46 if (feof(fp))
47 return(0);
48 return(magic == AMBMAGIC);
49 }
50
51
52 writambval(av, fp) /* write ambient value to stream */
53 register AMBVAL *av;
54 FILE *fp;
55 {
56 COLR col;
57
58 putint((long)av->lvl, 1, fp);
59 putflt(av->weight, fp);
60 putvec(av->pos, fp);
61 putvec(av->dir, fp);
62 setcolr(col, colval(av->val,RED),
63 colval(av->val,GRN), colval(av->val,BLU));
64 fwrite((char *)col, sizeof(col), 1, fp);
65 putflt(av->rad, fp);
66 putvec(av->gpos, fp);
67 putvec(av->gdir, fp);
68 return(ferror(fp) ? -1 : 0);
69 }
70
71
72 int
73 ambvalOK(av) /* check consistency of ambient value */
74 register AMBVAL *av;
75 {
76 double d;
77
78 if (badvec(av->pos)) return(0);
79 if (badvec(av->dir)) return(0);
80 d = DOT(av->dir,av->dir);
81 if (d < 0.9999 || d > 1.0001) return(0);
82 if (av->lvl < 0 || av->lvl > 100) return(0);
83 if (av->weight <= 0. || av->weight > 1.) return(0);
84 if (av->rad <= 0. || av->rad >= FHUGE) return(0);
85 if (colval(av->val,RED) < 0. ||
86 colval(av->val,RED) > FHUGE ||
87 colval(av->val,GRN) < 0. ||
88 colval(av->val,GRN) > FHUGE ||
89 colval(av->val,BLU) < 0. ||
90 colval(av->val,BLU) > FHUGE) return(0);
91 if (badvec(av->gpos)) return(0);
92 if (badvec(av->gdir)) return(0);
93 return(1);
94 }
95
96
97 readambval(av, fp) /* read ambient value from stream */
98 register AMBVAL *av;
99 FILE *fp;
100 {
101 COLR col;
102
103 av->lvl = getint(1, fp);
104 if (feof(fp))
105 return(0);
106 av->weight = getflt(fp);
107 getvec(av->pos, fp);
108 getvec(av->dir, fp);
109 if (fread((char *)col, sizeof(col), 1, fp) != 1)
110 return(0);
111 colr_color(av->val, col);
112 av->rad = getflt(fp);
113 getvec(av->gpos, fp);
114 getvec(av->gdir, fp);
115 return(feof(fp) ? 0 : ambvalOK(av));
116 }