Back to index

radiance  4R0+20100331
genrhgrid.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: genrhgrid.c,v 3.8 2004/01/01 11:21:55 schorsch Exp $";
00003 #endif
00004 /*
00005  * Generate renderable grids from a holodeck file
00006  */
00007 
00008 #include <stdio.h>
00009 
00010 #include "platform.h"
00011 #include "resolu.h"
00012 #include "holo.h"
00013 
00014 char   *progname;           /* global argv[0] */
00015 
00016 char   *mat, *name;         /* material and object id */
00017 double rad;                 /* grid line radius */
00018 
00019 static void gridsect(char *fname, int sect);
00020 static void putgrid(HOLO *hp);
00021 static void putline(FVECT wp[2]);
00022 
00023 
00024 int
00025 main(
00026        int    argc,
00027        char   *argv[]
00028 )
00029 {
00030        int    sect;
00031 
00032        progname = argv[0];
00033        if ((argc < 5) | (argc > 6))
00034               goto userr;
00035        mat = argv[1];
00036        name = argv[2];
00037        rad = atof(argv[3]);
00038        sect = argc==5 ? -1 : atoi(argv[5]);
00039        fputs("# ", stdout);
00040        printargs(argc, argv, stdout);
00041        gridsect(argv[4], sect);
00042        quit(0);
00043 userr:
00044        fprintf(stderr, "Usage: %s mat name rad input.hdk [section]\n",
00045                      progname);
00046        exit(1);
00047 }
00048 
00049 
00050 void
00051 gridsect(            /* get specified section(s) and print grids */
00052        char   *fname,
00053        int    sect
00054 )
00055 {
00056        FILE   *fp;
00057        HOLO   hdsect;
00058        int    fd;
00059        int32  nextloc;
00060        int    n;
00061                                    /* open holodeck file */
00062        if ((fp = fopen(fname, "r")) == NULL) {
00063               sprintf(errmsg, "cannot open \"%s\"", fname);
00064               error(SYSTEM, errmsg);
00065        }
00066                                    /* check header and magic number */
00067        if (checkheader(fp, HOLOFMT, NULL) < 0 || getw(fp) != HOLOMAGIC) {
00068               sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
00069               error(USER, errmsg);
00070        }
00071        fd = dup(fileno(fp));                     /* dup file handle */
00072        nextloc = ftell(fp);               /* get stdio position */
00073        fclose(fp);                        /* done with stdio */
00074        for (n = 0; nextloc > 0L; n++) {   /* get the section(s) */
00075               lseek(fd, (off_t)nextloc, SEEK_SET);
00076               read(fd, (char *)&nextloc, sizeof(nextloc));
00077               if ((sect < 0) | (n == sect)) {
00078                      read(fd, (char *)&hdsect, sizeof(HDGRID));
00079                      hdcompgrid(&hdsect);
00080                      putgrid(&hdsect);    /* print grid */
00081               }
00082        }
00083 }
00084 
00085 
00086 void
00087 putgrid(                    /* run through holodeck section grid lines */
00088        register HOLO *hp
00089 )
00090 {
00091        register int  w, i;
00092        int    g0, g1;
00093        FVECT  wp[2], mov;
00094        double d;
00095                                    /* do each wall on this section */
00096        for (w = 0; w < 6; w++) {
00097               g0 = hdwg0[w];
00098               g1 = hdwg1[w];
00099               d = 1.0/hp->grid[g0];
00100               mov[0] = d * hp->xv[g0][0];
00101               mov[1] = d * hp->xv[g0][1];
00102               mov[2] = d * hp->xv[g0][2];
00103               if (w & 1) {
00104                      VSUM(wp[0], hp->orig, hp->xv[w>>1], 1.);
00105                      VSUM(wp[0], wp[0], mov, 1.);
00106               } else
00107                      VCOPY(wp[0], hp->orig);
00108               VSUM(wp[1], wp[0], hp->xv[g1], 1.);
00109               for (i = hp->grid[g0]; ; ) {       /* g0 lines */
00110                      putline(wp);
00111                      if (!--i) break;
00112                      wp[0][0] += mov[0]; wp[0][1] += mov[1];
00113                      wp[0][2] += mov[2]; wp[1][0] += mov[0];
00114                      wp[1][1] += mov[1]; wp[1][2] += mov[2];
00115               }
00116               d = 1.0/hp->grid[g1];
00117               mov[0] = d * hp->xv[g1][0];
00118               mov[1] = d * hp->xv[g1][1];
00119               mov[2] = d * hp->xv[g1][2];
00120               if (w & 1)
00121                      VSUM(wp[0], hp->orig, hp->xv[w>>1], 1.);
00122               else
00123                      VSUM(wp[0], hp->orig, mov, 1.);
00124               VSUM(wp[1], wp[0], hp->xv[g0], 1.);
00125               for (i = hp->grid[g1]; ; ) {       /* g1 lines */
00126                      putline(wp);
00127                      if (!--i) break;
00128                      wp[0][0] += mov[0]; wp[0][1] += mov[1];
00129                      wp[0][2] += mov[2]; wp[1][0] += mov[0];
00130                      wp[1][1] += mov[1]; wp[1][2] += mov[2];
00131               }
00132        }
00133 }
00134 
00135 
00136 void
00137 putline(             /* put out a line */
00138        register FVECT       wp[2]
00139 )
00140 {
00141        static int    cnt = 0;
00142 
00143        printf("\n%s cylinder %s.%d\n0\n0\n7\n", mat, name, ++cnt);
00144        printf("\t%.4e %.4e %.4e\n", wp[0][0], wp[0][1], wp[0][2]);
00145        printf("\t%.4e %.4e %.4e\n", wp[1][0], wp[1][1], wp[1][2]);
00146        printf("\t%.4e\n", rad);
00147 }