home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / radsrc22 / src / gen / genprism.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-19  |  3.7 KB  |  193 lines

  1. /* Copyright (c) 1987 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)genprism.c 2.2 12/19/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  genprism.c - generate a prism.
  9.  *        2D vertices in the xy plane are given on the
  10.  *        command line or from a file.  Their order together
  11.  *        with the extrude direction will determine surface
  12.  *        orientation.
  13.  *
  14.  *     8/24/87
  15.  */
  16.  
  17. #include  <stdio.h>
  18.  
  19. #include  <ctype.h>
  20.  
  21. #define  MAXVERT    1024        /* maximum # vertices */
  22.  
  23. #ifndef atof
  24. extern double  atof();
  25. #endif
  26.  
  27. char  *pmtype;        /* material type */
  28. char  *pname;        /* name */
  29.  
  30. double  lvect[3] = {0.0, 0.0, 1.0};
  31.  
  32. double  vert[MAXVERT][2];
  33. int  nverts = 0;
  34.  
  35. int  do_ends = 1;        /* include end caps */
  36. int  iscomplete = 0;        /* polygon is already completed */
  37.  
  38.  
  39. main(argc, argv)
  40. int  argc;
  41. char  **argv;
  42. {
  43.     int  an;
  44.     
  45.     if (argc < 4)
  46.         goto userr;
  47.  
  48.     pmtype = argv[1];
  49.     pname = argv[2];
  50.  
  51.     if (!strcmp(argv[3], "-")) {
  52.         readverts(NULL);
  53.         an = 4;
  54.     } else if (isdigit(argv[3][0])) {
  55.         nverts = atoi(argv[3]);
  56.         if (argc-3 < 2*nverts)
  57.             goto userr;
  58.         for (an = 0; an < nverts; an++) {
  59.             vert[an][0] = atof(argv[2*an+4]);
  60.             vert[an][1] = atof(argv[2*an+5]);
  61.         }
  62.         an = 2*nverts+4;
  63.     } else {
  64.         readverts(argv[3]);
  65.         an = 4;
  66.     }
  67.     if (nverts < 3) {
  68.         fprintf(stderr, "%s: not enough vertices\n", argv[0]);
  69.         exit(1);
  70.     }
  71.  
  72.     for ( ; an < argc; an++) {
  73.         if (argv[an][0] != '-')
  74.             goto userr;
  75.         switch (argv[an][1]) {
  76.         case 'l':                /* length vector */
  77.             lvect[0] = atof(argv[++an]);
  78.             lvect[1] = atof(argv[++an]);
  79.             lvect[2] = atof(argv[++an]);
  80.             break;
  81.         case 'e':                /* ends */
  82.             do_ends = !do_ends;
  83.             break;
  84.         case 'c':                /* complete */
  85.             iscomplete = !iscomplete;
  86.             break;
  87.         default:
  88.             goto userr;
  89.         }
  90.     }
  91.  
  92.     printhead(argc, argv);
  93.  
  94.     if (do_ends)
  95.         printends();
  96.     printsides();
  97.  
  98.     return(0);
  99. userr:
  100.     fprintf(stderr, "Usage: %s material name ", argv[0]);
  101.     fprintf(stderr, "{ - | vfile | N v1 v2 .. vN } ");
  102.     fprintf(stderr, "[-l lvect][-c][-e]\n");
  103.     exit(1);
  104. }
  105.  
  106.  
  107. readverts(fname)        /* read vertices from a file */
  108. char  *fname;
  109. {
  110.     FILE  *fp;
  111.  
  112.     if (fname == NULL)
  113.         fp = stdin;
  114.     else if ((fp = fopen(fname, "r")) == NULL) {
  115.         fprintf(stderr, "%s: cannot open\n", fname);
  116.         exit(1);
  117.     }
  118.     while (fscanf(fp, "%lf %lf", &vert[nverts][0], &vert[nverts][1]) == 2)
  119.         nverts++;
  120.     fclose(fp);
  121. }
  122.  
  123.  
  124. printends()            /* print ends of prism */
  125. {
  126.     register int  i;
  127.  
  128.     printf("\n%s polygon %s.b\n", pmtype, pname);
  129.     printf("0\n0\n%d\n", nverts*3);
  130.     for (i = 0; i < nverts; i++) {
  131.         printf("\t%18.12g\t%18.12g\t%18.12g\n",
  132.                 vert[i][0],
  133.                 vert[i][1],
  134.                 0.0);
  135.     }
  136.     printf("\n%s polygon %s.e\n", pmtype, pname);
  137.     printf("0\n0\n%d\n", nverts*3);
  138.     for (i = nverts-1; i >= 0; i--) {
  139.         printf("\t%18.12g\t%18.12g\t%18.12g\n",
  140.                 vert[i][0]+lvect[0],
  141.                 vert[i][1]+lvect[1],
  142.                 lvect[2]);
  143.     }
  144. }
  145.  
  146.  
  147. printsides()            /* print prism sides */
  148. {
  149.     register int  i;
  150.  
  151.     for (i = 0; i < nverts-1; i++)
  152.         side(i, i+1);
  153.     if (!iscomplete)
  154.         side(nverts-1, 0);
  155. }
  156.  
  157.  
  158. side(n1, n2)            /* print single side */
  159. register int  n1, n2;
  160. {
  161.     printf("\n%s polygon %s.%d\n", pmtype, pname, n1+1);
  162.     printf("0\n0\n12\n");
  163.     printf("\t%18.12g\t%18.12g\t%18.12g\n",
  164.             vert[n1][0],
  165.             vert[n1][1],
  166.             0.0);
  167.     printf("\t%18.12g\t%18.12g\t%18.12g\n",
  168.             vert[n1][0]+lvect[0],
  169.             vert[n1][1]+lvect[1],
  170.             lvect[2]);
  171.     printf("\t%18.12g\t%18.12g\t%18.12g\n",
  172.             vert[n2][0]+lvect[0],
  173.             vert[n2][1]+lvect[1],
  174.             lvect[2]);
  175.     printf("\t%18.12g\t%18.12g\t%18.12g\n",
  176.             vert[n2][0],
  177.             vert[n2][1],
  178.             0.0);
  179. }
  180.  
  181.  
  182. printhead(ac, av)        /* print command header */
  183. register int  ac;
  184. register char  **av;
  185. {
  186.     putchar('#');
  187.     while (ac--) {
  188.         putchar(' ');
  189.         fputs(*av++, stdout);
  190.     }
  191.     putchar('\n');
  192. }
  193.