Back to index

radiance  4R0+20100331
genbox.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: genbox.c,v 2.7 2003/11/16 10:29:38 schorsch Exp $";
00003 #endif
00004 /*
00005  *  genbox.c - generate a parallelepiped.
00006  *
00007  *     1/8/86
00008  */
00009 
00010 #include  <stdio.h>
00011 
00012 #include <stdlib.h>
00013 
00014 #include  <math.h>
00015 
00016 
00017 char  let[]="0123456789._ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
00018 
00019 char  *cmtype;              /* ppd material type */
00020 
00021 char  *cname;        /* ppd name */
00022 
00023 double  size[3];     /* ppd size */
00024 
00025 double  bevel = 0.0; /* bevel amount */
00026 
00027 int  rounde = 0;            /* boolean true for rounde edges */
00028 
00029 int  reverse = 0;    /* boolean true for reversed normals */
00030 
00031 
00032 
00033 static void
00034 vertex(v)
00035 register int  v;
00036 {
00037        register int  i;
00038 
00039        for (i = 0; i < 3; i++) {
00040               if (v & 010)
00041                      printf("\t%18.12g", v & 01 ? size[i]-bevel : bevel);
00042               else
00043                      printf("\t%18.12g", v & 01 ? size[i] : 0.0);
00044               v >>= 1;
00045        }
00046        printf("\n");
00047 }
00048 
00049 
00050 static void
00051 side(a, b, c, d)            /* generate a rectangular face */
00052 int  a, b, c, d;
00053 {
00054        printf("\n%s polygon %s.%c%c%c%c\n", cmtype, cname,
00055                      let[a], let[b], let[c], let[d]);
00056        printf("0\n0\n12\n");
00057        if (reverse) {
00058               vertex(d);
00059               vertex(c);
00060               vertex(b);
00061               vertex(a);
00062        } else {
00063               vertex(a);
00064               vertex(b);
00065               vertex(c);
00066               vertex(d);
00067        }
00068 }
00069 
00070 
00071 static void
00072 corner(a, b, c)                    /* generate a triangular face */
00073 int  a, b, c;
00074 {
00075        printf("\n%s polygon %s.%c%c%c\n", cmtype, cname,
00076                      let[a], let[b], let[c]);
00077        printf("0\n0\n9\n");
00078        if (reverse) {
00079               vertex(c);
00080               vertex(b);
00081               vertex(a);
00082        } else {
00083               vertex(a);
00084               vertex(b);
00085               vertex(c);
00086        }
00087 }
00088 
00089 
00090 static void
00091 cylinder(v0, v1)            /* generate a cylinder */
00092 int  v0, v1;
00093 {
00094        printf("\n%s cylinder %s.%c%c\n", cmtype, cname, v0+'0', v1+'0');
00095        printf("0\n0\n7\n");
00096        vertex(v0);
00097        vertex(v1);
00098        printf("\t%18.12g\n", bevel);
00099 }
00100 
00101 
00102 static void
00103 sphere(v0)                  /* generate a sphere */
00104 int  v0;
00105 {
00106        printf("\n%s sphere %s.%c\n", cmtype, cname, v0+'0');
00107        printf("0\n0\n4\n");
00108        vertex(v0);
00109        printf("\t%18.12g\n", bevel);
00110 }
00111 
00112 
00113 static void
00114 printhead(ac, av)           /* print command header */
00115 register int  ac;
00116 register char  **av;
00117 {
00118        putchar('#');
00119        while (ac--) {
00120               putchar(' ');
00121               fputs(*av++, stdout);
00122        }
00123        putchar('\n');
00124 }
00125 
00126 
00127 int
00128 main(argc, argv)
00129 int  argc;
00130 char  **argv;
00131 {
00132        int  i;
00133        
00134        if (argc < 6)
00135               goto userr;
00136 
00137        cmtype = argv[1];
00138        cname = argv[2];
00139        size[0] = atof(argv[3]);
00140        size[1] = atof(argv[4]);
00141        size[2] = atof(argv[5]);
00142 
00143        for (i = 6; i < argc; i++) {
00144               if (argv[i][0] != '-')
00145                      goto userr;
00146               switch (argv[i][1]) {
00147               case 'r':
00148                      rounde = 1;
00149                      /* fall through */
00150               case 'b':
00151                      bevel = atof(argv[++i]);
00152                      break;
00153               case 'i':
00154                      reverse = 1;
00155                      break;
00156               default:
00157                      goto userr;
00158               }
00159        }
00160 
00161        printhead(argc, argv);
00162 
00163        if (bevel > 0.0) {
00164                                    /* minor faces */
00165               side(051, 055, 054, 050);
00166               side(064, 066, 062, 060);
00167               side(032, 033, 031, 030);
00168               side(053, 052, 056, 057);
00169               side(065, 061, 063, 067);
00170               side(036, 034, 035, 037);
00171        }
00172        if (bevel > 0.0 && !rounde) {
00173                                    /* bevel faces */
00174               side(031, 051, 050, 030);
00175               side(060, 062, 032, 030);
00176               side(050, 054, 064, 060);
00177               side(034, 036, 066, 064);
00178               side(037, 057, 056, 036);
00179               side(052, 062, 066, 056);
00180               side(052, 053, 033, 032);
00181               side(057, 067, 063, 053);
00182               side(061, 031, 033, 063);
00183               side(065, 067, 037, 035);
00184               side(055, 051, 061, 065);
00185               side(034, 054, 055, 035);
00186                                    /* bevel corners */
00187               corner(030, 050, 060);
00188               corner(051, 031, 061);
00189               corner(032, 062, 052);
00190               corner(064, 054, 034);
00191               corner(036, 056, 066);
00192               corner(065, 035, 055);
00193               corner(053, 063, 033);
00194               corner(037, 067, 057);
00195        }
00196        if (bevel > 0.0 && rounde) {
00197                                    /* rounde edges */
00198               cylinder(070, 071);
00199               cylinder(070, 074);
00200               cylinder(070, 072);
00201               cylinder(073, 071);
00202               cylinder(073, 072);
00203               cylinder(073, 077);
00204               cylinder(075, 071);
00205               cylinder(075, 074);
00206               cylinder(075, 077);
00207               cylinder(076, 072);
00208               cylinder(076, 074);
00209               cylinder(076, 077);
00210                                    /* rounde corners */
00211               sphere(070);
00212               sphere(071);
00213               sphere(072);
00214               sphere(073);
00215               sphere(074);
00216               sphere(075);
00217               sphere(076);
00218               sphere(077);
00219        }
00220        if (bevel == 0.0 ) {
00221                                    /* only need major faces */
00222               side(1, 5, 4, 0);
00223               side(4, 6, 2, 0);
00224               side(2, 3, 1, 0);
00225               side(3, 2, 6, 7);
00226               side(5, 1, 3, 7);
00227               side(6, 4, 5, 7);
00228        }
00229        exit(0);
00230 userr:
00231        fprintf(stderr, "Usage: %s ", argv[0]);
00232        fprintf(stderr, "material name xsize ysize zsize ");
00233        fprintf(stderr, "[-i] [-b bevel | -r rounde]\n");
00234        exit(1);
00235 }
00236