Back to index

radiance  4R0+20100331
writeoct.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char RCSid[] = "$Id: writeoct.c,v 2.7 2004/11/09 16:09:20 greg Exp $";
00003 #endif
00004 /*
00005  *  writeoct.c - routines for writing octree information to stdout.
00006  *
00007  *     7/30/85
00008  */
00009 
00010 #include  "standard.h"
00011 
00012 #include  "octree.h"
00013 #include  "object.h"
00014 #include  "oconv.h"
00015 
00016 #ifdef putc_unlocked        /* avoid horrendous overhead of flockfile */
00017 #undef putc
00018 #define putc    putc_unlocked
00019 #endif
00020 
00021 static void oputstr(char *s);
00022 static void putfullnode(OCTREE fn);
00023 static void oputint(long i, int siz);
00024 static void oputflt(double f);
00025 static void puttree(OCTREE ot);
00026 
00027 
00028 void
00029 writeoct(            /* write octree structures to stdout */
00030        int  store,
00031        CUBE  *scene,
00032        char  *ofn[]
00033 )
00034 {
00035        char  sbuf[64];
00036        int  i;
00037                                    /* write format number */
00038        oputint((long)(OCTMAGIC+sizeof(OBJECT)), 2);
00039 
00040        if (!(store & IO_BOUNDS))
00041               return;
00042                                    /* write boundaries */
00043        for (i = 0; i < 3; i++) {
00044               sprintf(sbuf, "%.12g", scene->cuorg[i]);
00045               oputstr(sbuf);
00046        }
00047        sprintf(sbuf, "%.12g", scene->cusize);
00048        oputstr(sbuf);
00049                                    /* write object file names */
00050        if (store & IO_FILES)
00051               for (i = 0; ofn[i] != NULL; i++)
00052                      oputstr(ofn[i]);
00053        oputstr("");
00054                                    /* write number of objects */
00055        oputint((long)nobjects, sizeof(OBJECT));
00056 
00057        if (!(store & IO_TREE))
00058               return;
00059                                    /* write the octree */
00060        puttree(scene->cutree);
00061 
00062        if (store & IO_FILES || !(store & IO_SCENE))
00063               return;
00064                                    /* write the scene */
00065        writescene(0, nobjects, stdout);
00066 }
00067 
00068 
00069 static void
00070 oputstr(                    /* write null-terminated string to stdout */
00071        register char  *s
00072 )
00073 {
00074        putstr(s, stdout);
00075        if (ferror(stdout))
00076               error(SYSTEM, "write error in putstr");
00077 }
00078 
00079 
00080 static void
00081 putfullnode(                /* write out a full node */
00082        OCTREE  fn
00083 )
00084 {
00085        OBJECT  oset[MAXSET+1];
00086        register int  i;
00087 
00088        objset(oset, fn);
00089        for (i = 0; i <= oset[0]; i++)
00090               oputint((long)oset[i], sizeof(OBJECT));
00091 }
00092 
00093 
00094 static void
00095 oputint(                    /* write a siz-byte integer to stdout */
00096        register long  i,
00097        register int  siz
00098 )
00099 {
00100        putint(i, siz, stdout);
00101        if (ferror(stdout))
00102               error(SYSTEM, "write error in putint");
00103 }
00104 
00105 
00106 static void
00107 oputflt(                    /* put out floating point number */
00108        double  f
00109 )
00110 {
00111        putflt(f, stdout);
00112        if (ferror(stdout))
00113               error(SYSTEM, "write error in putflt");
00114 }
00115 
00116 
00117 static void
00118 puttree(                    /* write octree to stdout in pre-order form */
00119        register OCTREE  ot
00120 )
00121 {
00122        register int  i;
00123        
00124        if (istree(ot)) {
00125               putc(OT_TREE, stdout);             /* indicate tree */
00126               for (i = 0; i < 8; i++)            /* write tree */
00127                      puttree(octkid(ot, i));
00128        } else if (isfull(ot)) {
00129               putc(OT_FULL, stdout);             /* indicate fullnode */
00130               putfullnode(ot);            /* write fullnode */
00131        } else
00132               putc(OT_EMPTY, stdout);            /* indicate empty */
00133 }