Back to index

radiance  4R0+20100331
ambio.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: ambio.c,v 2.5 2003/02/25 02:47:22 greg Exp $";
00003 #endif
00004 /*
00005  * Read and write portable ambient values
00006  *
00007  *  Declarations of external symbols in ambient.h
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include "ray.h"
00013 
00014 #include "ambient.h"
00015 
00016 
00017 #define  putvec(v,fp)       putflt((v)[0],fp);putflt((v)[1],fp);putflt((v)[2],fp)
00018 
00019 #define  getvec(v,fp)       (v)[0]=getflt(fp);(v)[1]=getflt(fp);(v)[2]=getflt(fp)
00020 
00021 #define  badflt(x)   ((x) < -FHUGE || (x) > FHUGE)
00022 
00023 #define  badvec(v)   (badflt((v)[0]) || badflt((v)[1]) || badflt((v)[2]))
00024 
00025 
00026 
00027 void
00028 putambmagic(fp)                    /* write out ambient value magic number */
00029 FILE  *fp;
00030 {
00031        putint((long)AMBMAGIC, 2, fp);
00032 }
00033 
00034 
00035 int
00036 hasambmagic(fp)                    /* read in and check validity of magic # */
00037 FILE  *fp;
00038 {
00039        register int  magic;
00040 
00041        magic = getint(2, fp);
00042        if (feof(fp))
00043               return(0);
00044        return(magic == AMBMAGIC);
00045 }
00046 
00047 
00048 int
00049 writambval(av, fp)          /* write ambient value to stream */
00050 register AMBVAL  *av;
00051 FILE  *fp;
00052 {
00053        COLR  col;
00054 
00055        putint((long)av->lvl, 1, fp);
00056        putflt(av->weight, fp);
00057        putvec(av->pos, fp);
00058        putvec(av->dir, fp);
00059        setcolr(col, colval(av->val,RED),
00060                      colval(av->val,GRN), colval(av->val,BLU));
00061        fwrite((char *)col, sizeof(col), 1, fp);
00062        putflt(av->rad, fp);
00063        putvec(av->gpos, fp);
00064        putvec(av->gdir, fp);
00065        return(ferror(fp) ? -1 : 0);
00066 }
00067 
00068 
00069 int
00070 ambvalOK(av)                /* check consistency of ambient value */
00071 register AMBVAL  *av;
00072 {
00073        double d;
00074 
00075        if (badvec(av->pos)) return(0);
00076        if (badvec(av->dir)) return(0);
00077        d = DOT(av->dir,av->dir);
00078        if (d < 0.9999 || d > 1.0001) return(0);
00079        if (av->lvl < 0 || av->lvl > 100) return(0);
00080        if (av->weight <= 0. || av->weight > 1.) return(0);
00081        if (av->rad <= 0. || av->rad >= FHUGE) return(0);
00082        if (colval(av->val,RED) < 0. ||
00083                      colval(av->val,RED) > FHUGE ||
00084                      colval(av->val,GRN) < 0. ||
00085                      colval(av->val,GRN) > FHUGE ||
00086                      colval(av->val,BLU) < 0. ||
00087                      colval(av->val,BLU) > FHUGE) return(0);
00088        if (badvec(av->gpos)) return(0);
00089        if (badvec(av->gdir)) return(0);
00090        return(1);
00091 }
00092 
00093 
00094 int
00095 readambval(av, fp)          /* read ambient value from stream */
00096 register AMBVAL  *av;
00097 FILE  *fp;
00098 {
00099        COLR  col;
00100 
00101        av->lvl = getint(1, fp);
00102        if (feof(fp))
00103               return(0);
00104        av->weight = getflt(fp);
00105        getvec(av->pos, fp);
00106        getvec(av->dir, fp);
00107        if (fread((char *)col, sizeof(col), 1, fp) != 1)
00108               return(0);
00109        colr_color(av->val, col);
00110        av->rad = getflt(fp);
00111        getvec(av->gpos, fp);
00112        getvec(av->gdir, fp);
00113        return(feof(fp) ? 0 : ambvalOK(av));
00114 }