home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / POLY3DHS.ZIP / POLY3D-H.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  9.5 KB  |  275 lines

  1. /*****************************************************************************
  2. *   Program to draw 3D object as wireframe after removing the hidden lines.  *
  3. * This porgram works in object space, and if redirect stdout to a file, dump *
  4. * the visible polylines into it instead of drawing them on current device.   *
  5. * This may be used to display the results on any device (a plotter !?) later.*
  6. *                                         *
  7. * Options:                                     *
  8. * 1. -b : Delete back facing polygons.                         *
  9. * 2. -e #Edges : If the edges are order is specific way (like in DrawFn3D)   *
  10. *         and only the k first edges of each polygons are to be displayed    *
  11. *      use this option as -e k.                         *
  12. * 3. -i : Internal edges. IRIT solid modeller may generate edges, which one  *
  13. *      may not want to see (default). -i will draw all of them.         *
  14. * 4. -m : More flag, to print more imformation on input/errors.             *
  15. * 5. -o Objects : List of objects to be displayed from the input data files. *
  16. *      This option should not be last and Object list terminates with     *
  17. *      next option (- as first char). ALL objects are displayed otherwise.*
  18. * 6. -v ViewFile : Optional view to start with.                     *
  19. * 7. -z : Print current version, and some helpfull data.             *
  20. *                                         *
  21. * Note some of those options may be permanently set to a different default   *
  22. * using the configuration file "Poly3D-H.cfg"                     *
  23. *                                         *
  24. * Usage: Poly3D-H [-b] [-i] [m] [-e #Edges] [-o Objects] [-v ViewFile] [-z]  *
  25. *                                       Files *
  26. *                                         *
  27. * Written by:  Gershon Elber                Ver 2.0, Sep. 1989   *
  28. *****************************************************************************/
  29.  
  30. #ifdef __MSDOS__
  31. #include <stdlib.h>
  32. #include <conio.h>
  33. #include <io.h>
  34. #include <dos.h>
  35. #include <alloc.h>
  36. #endif /* __MSDOS__ */
  37.  
  38. #include <stdio.h>
  39. #include <math.h>
  40. #include <time.h>
  41. #include "program.h"
  42. #include "getarg.h"
  43. #include "genmat.h"
  44. #include "parser.h"
  45. #include "config.h"
  46. #include "ctrl-brk.h"
  47.  
  48. #ifdef __MSDOS__
  49. /* This huge stack is mainly from second phase - the segment intersections   */
  50. /* which may cause recursive calls - a lot...                     */
  51. extern unsigned int _stklen = 32766;
  52. #endif /* __MSDOS__ */
  53.  
  54. #ifdef SYSV
  55. static char *VersionStr =
  56.     "Poly3D-H    version 2.0,    Gershon Elber,\n\
  57.     (C) Copyright 1989 Gershon Elber, Non commercial use only.";
  58. #else
  59. static char *VersionStr = "Poly3D-H    version 2.0,    Gershon Elber,    "
  60.     __DATE__ ",  " __TIME__ "\n"
  61.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.";
  62. #endif /* SYSV */
  63.  
  64. static char *CtrlStr =
  65.     "poly3d-h b%- m%- i%- e%-#Edges!d o%-Objects!*s v%-ViewFile!s z%- DFiles!*s";
  66.  
  67. int NumOfPolygons = 0;              /* Total number of polygons to handle. */
  68. MatrixType GlblViewMat;                  /* Current view of object. */
  69.  
  70. /* Data structures used by the hidden line modules: */
  71. int EdgeCount = 0;
  72. struct EdgeStruct *EdgeHashTable[EDGE_HASH_TABLE_SIZE];
  73. struct PolygonStruct *PolyHashTable[POLY_HASH_TABLE_SIZE];
  74.  
  75. static long SaveTotalTime;
  76.  
  77. /* The following are setable variables (via configuration file poly3d-h.cfg).*/
  78. int MoreFlag = FALSE, NumEdges = 0, ViewFlag = FALSE, BackFacingFlag = FALSE,
  79.     InternalFlag;
  80.  
  81. static ConfigStruct SetUp[] =
  82. { { "Internal",        (VoidPtr) &InternalFlag,    SU_BOOLEAN_TYPE },
  83.   { "More",        (VoidPtr) &MoreFlag,        SU_BOOLEAN_TYPE },
  84.   { "NumOfEdges",    (VoidPtr) &NumEdges,        SU_INTEGER_TYPE } };
  85.  
  86. #define NUM_SET_UP    (sizeof(SetUp) / sizeof(ConfigStruct))
  87.  
  88. static void MainGetViewFile(char *ViewFileName);
  89. static FileDescription **MainGetDataFiles(char **DataFileNames,
  90.                             int NumOfDataFiles);
  91. static void FreeBinTree(struct BinTree *Pbin);
  92.  
  93. /*****************************************************************************
  94. * Main routine - Read Parameter    line and do what you need...             *
  95. *****************************************************************************/
  96. void main(int argc, char **argv)
  97. {
  98.     int EdgesFlag = FALSE, ObjFlag = FALSE, NumObjs = 0, VerFlag = FALSE,
  99.     NumFiles = FALSE, Error;
  100.     char **Objects = NULL, *ViewName = NULL, **FileNames = NULL;
  101.     FileDescription **FD;
  102.  
  103.     SaveTotalTime = time(NULL);                  /* Save starting time. */
  104. #ifdef __MSDOS__
  105.     ctrlbrk((int (*)()) MyExit);           /* Kill this program if ^C... */
  106. #endif /* __MSDOS_ */
  107.     SetUpHardErr();        /* Set up hardware error trap routine (int 24h). */
  108.  
  109.     Config("poly3d-h", SetUp, NUM_SET_UP);   /* Read config. file if exists. */
  110.  
  111.     if ((Error = GAGetArgs (argc, argv, CtrlStr,
  112.         &BackFacingFlag, &MoreFlag, &InternalFlag, &EdgesFlag,
  113.         &NumEdges, &ObjFlag, &NumObjs, &Objects,
  114.         &ViewFlag, &ViewName, &VerFlag,    &NumFiles, &FileNames)) != 0) {
  115.     GAPrintErrMsg(Error);
  116.     GAPrintHowTo(CtrlStr);
  117.     MyExit(1);
  118.     }
  119.  
  120.     if (VerFlag) {
  121.     fprintf(stderr, "\n%s\n\n", VersionStr);
  122.     GAPrintHowTo(CtrlStr);
  123.     ConfigPrint(SetUp, NUM_SET_UP);
  124.     MyExit(0);
  125.     }
  126.  
  127.     if (!NumFiles) {
  128.     fprintf(stderr, "No data file names were given, exit\n");
  129.     GAPrintHowTo(CtrlStr);
  130.     MyExit(1);
  131.     }
  132.  
  133.     MainGetViewFile(ViewName);
  134.  
  135.     /* Get the data files: */
  136.     FD = MainGetDataFiles(FileNames, NumFiles);
  137.  
  138.     /* Prepare data structures to be able to decide on visibility: */
  139.     PrepareViewData(FD, NumObjs, Objects);
  140.  
  141.     OutVisibleEdges();               /* Scan all sub-edges output visible. */
  142.  
  143.     MyExit(0);
  144. }
  145.  
  146. /*****************************************************************************
  147. * Main routine to read the view    description file, into GlblViewMat:         *
  148. *****************************************************************************/
  149. static void MainGetViewFile(char *ViewFileName)
  150. {
  151.     FILE *f;
  152.  
  153.     if (ViewFlag && (f = fopen(ViewFileName, "rt")) == NULL) {
  154.     fprintf(stderr, "Can't open view file %s\n", ViewFileName);
  155.     MyExit(1);
  156.     }
  157.  
  158.     /* Get view data if ViewFlag, otherwise set default view (isometric): */
  159.     GetViewFile(f, ViewFlag);
  160.  
  161.     if (ViewFlag) fclose(f);                  /* Close the file. */
  162. }
  163.  
  164. /*****************************************************************************
  165. * Main routine to read the data    description files:                 *
  166. * Returns pointer to pointers on FileDescription structures (one per file).  *
  167. *****************************************************************************/
  168. static FileDescription **MainGetDataFiles(char **DataFileNames,
  169.                             int NumOfDataFiles)
  170. {
  171.     int i;
  172.     long SaveTime = time(NULL);
  173.     FILE *f;
  174.     FileDescription **FDbase, **FDinc;  /* Used to save memory pointers loc. */
  175.  
  176.     NumOfPolygons = 0;
  177.     fprintf(stderr, "\nPass 1, Polys =      ");
  178.  
  179.     FDbase = FDinc = (FileDescription **)    /* Allocate the pointers block. */
  180.      MyMalloc((unsigned) sizeof(FileDescription *) * (NumOfDataFiles + 1));
  181.  
  182.     for    (i=0; i<NumOfDataFiles;    i++) {
  183.     if (MoreFlag) {
  184.         fprintf(stderr, "\rReading data file %s", *DataFileNames);
  185.         fprintf(stderr, "\nPass 1, Polys =      ");
  186.     }
  187.     f = fopen(*DataFileNames, "rt");           /* Open the file. */
  188.     if (!f)
  189.     {
  190.         fprintf(stderr, "Can't open data file %s\n", *DataFileNames);
  191.         MyExit(1);
  192.     }
  193.  
  194.     *FDinc = GetDataFile(f);               /* Get the data file. */
  195.     FreeBinTree((*FDinc) ->    VertexPointer); /* We dont need these trees. */
  196.     FreeBinTree((*FDinc) ->    PolygonPointer);/* - objects are all loaded. */
  197.     FreeBinTree((*FDinc) ->    PolylinePointer);
  198.     fprintf(stderr, " \b");          /* Erase the FreeBinTree marker... */
  199.     (*FDinc) -> VertexPointer =
  200.     (*FDinc) -> PolygonPointer =
  201.     (*FDinc) -> PolylinePointer = NULL;
  202.     *FDinc++;
  203.  
  204.     fclose(f);                      /* Close the file. */
  205.  
  206.     DataFileNames++;              /* Skip to next file name. */
  207.     }
  208.  
  209.     *FDinc = NULL;
  210.  
  211.     fprintf(stderr, ",  %ld seconds.", time(NULL) - SaveTime);
  212.  
  213.     return FDbase;
  214. }
  215.  
  216. /*****************************************************************************
  217. * My Routine to    allocate dynamic memory. All program requests must call         *
  218. * this routine (no direct call to malloc). Dies if no memory.             *
  219. *****************************************************************************/
  220. char *MyMalloc(unsigned size)
  221. {
  222.     char *p;
  223.  
  224.     if ((p = malloc(size)) != NULL) return p;
  225.  
  226.     fprintf(stderr, "\nNot enough memory, exit\n");
  227.     MyExit(2);
  228.  
  229.     return NULL;                    /* Makes warning silent. */
  230. }
  231.  
  232. /*****************************************************************************
  233. * Routine to free a binary tree, which is not needed any more:             *
  234. *****************************************************************************/
  235. static void FreeBinTree(BinTree *Pbin)
  236. {
  237.     static Flip = FALSE;
  238.  
  239.     if (Flip) {
  240.     Flip = FALSE;
  241.     fprintf(stderr, "m\b");
  242.     }
  243.     else {
  244.     Flip = TRUE;
  245.     fprintf(stderr, "M\b");
  246.     }
  247.  
  248.     if (Pbin) {
  249.     FreeBinTree(Pbin -> right);
  250.     FreeBinTree(Pbin -> left);
  251.     free((char *) Pbin);
  252.     }
  253. }
  254.  
  255. /*****************************************************************************
  256. * MyExit routine. Note it might call to CloseGraph without calling         *
  257. * InitGraph(), or call MouseClose() without MouseInit(), or call         *
  258. * RestoreCtrlBrk() without SetUpCtrlBrk() and it is the responsibility         *
  259. * of the individual modules to do nothing in these cases.             *
  260. *****************************************************************************/
  261. void MyExit(int ExitCode)
  262. {
  263. #ifdef __MSDOS__
  264.     fprintf(stderr,
  265.         "\nPoly3D-H: Total RealTime %ld seconds, Core left %ldk.\n",
  266.         time(NULL) - SaveTotalTime, coreleft() / 1024);
  267. #else
  268.     fprintf(stderr,
  269.         "\nPoly3D-H: Total RealTime %ld seconds.\n",
  270.         time(NULL) - SaveTotalTime);
  271. #endif /* __MSDOS__ */
  272.  
  273.     exit(ExitCode);
  274. }
  275.