home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iritsm3s.zip / iritfltr / dat2irit.c < prev    next >
C/C++ Source or Header  |  1992-01-12  |  15KB  |  492 lines

  1. /*****************************************************************************
  2. * Filter to convert IRIT data files back to IRIT .irt files.             *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 1.0, Sep 1991    *
  5. *****************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <dos.h>
  9. #include <alloc.h>
  10. #endif /* __MSDOS__ */
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include "irit_sm.h"
  17. #include "iritprsr.h"
  18. #include "getarg.h"
  19. #include "genmat.h"
  20.  
  21. #ifdef __MSDOS__
  22. extern unsigned int _stklen = 32766;         /* Increase default stack size. */
  23. #endif /* __MSDOS__ */
  24.  
  25. #ifdef NO_CONCAT_STR
  26. static char *VersionStr =
  27.     "Dat2Irit        Version 3.0,        Gershon Elber,\n\
  28.      (C) Copyright 1989/90/91 Gershon Elber, Non commercial use only.";
  29. #else
  30. static char *VersionStr = "Dat2Irit    " VERSION ",    Gershon Elber,    "
  31.     __DATE__ ",   " __TIME__ "\n"
  32.     "(C) Copyright 1989/90/91 Gershon Elber, Non commercial use only.";
  33. #endif /* NO_CONCAT_STR */
  34.  
  35. static char
  36.     *CtrlStr = "dat2irit z%- DFiles!*s";
  37.  
  38. static IPObjectStruct *MainGetDataFiles(char **DataFileNames,
  39.                     int NumOfDataFiles);
  40. static void DumpDataForIrit(IPObjectStruct *PObjects);
  41. static void DumpOneObject(FILE *f, IPObjectStruct *PObject);
  42. static void DumpOnePolygon(FILE *f, IPPolygonStruct *PPolygon, int PolyNum);
  43. static void DumpOneSurface(FILE *f, char *Name, CagdSrfStruct *Srf);
  44. static void DumpOneCurve(FILE *f, char *Name, CagdCrvStruct *Crv);
  45. static void DumpCtlPt(FILE *f, CagdPointType PType, RealType **Points,
  46.                                 int Index);
  47. static void DumpKnotVector(FILE *f, RealType *KnotVector, int Length);
  48.  
  49. #ifdef __DEBUG_MALLOC__
  50. static void AllocError(const char *Msg, VoidPtr *p);
  51. #endif /* __DEBUG_MALLOC__ */
  52. VoidPtr MyMalloc(unsigned size);
  53. void MyFree(VoidPtr p);
  54. void CagdFatalError(CagdFatalErrorType ErrID);
  55. void MyExit(int ExitCode);
  56.  
  57. /*****************************************************************************
  58. * Main routine - Read Parameter    line and do what you need...             *
  59. *****************************************************************************/
  60. void main(int argc, char **argv)
  61. {
  62.     int Error,
  63.     VerFlag = FALSE,
  64.     NumFiles = 0;
  65.     char
  66.     **FileNames = NULL;
  67.     IPObjectStruct *PObjects;
  68.  
  69. #ifdef __MSDOS__
  70.     ctrlbrk((int (*)()) MyExit);              /* Kill process if ^C. */
  71. #endif /* __MSDOS__ */
  72.  
  73.     if ((Error = GAGetArgs (argc, argv, CtrlStr,
  74.                 &VerFlag, &NumFiles, &FileNames)) != 0) {
  75.     GAPrintErrMsg(Error);
  76.     GAPrintHowTo(CtrlStr);
  77.     MyExit(1);
  78.     }
  79.  
  80.     if (VerFlag) {
  81.     fprintf(stderr, "\n%s\n\n", VersionStr);
  82.     GAPrintHowTo(CtrlStr);
  83.     MyExit(0);
  84.     }
  85.  
  86.     if (!NumFiles) {
  87.     fprintf(stderr, "No data file names where given, exit.\n");
  88.     GAPrintHowTo(CtrlStr);
  89.     MyExit(1);
  90.     }
  91.  
  92.     /* Get the data files: */
  93.     IritPrsrPolyListCirc = FALSE;
  94.     PObjects = MainGetDataFiles(FileNames, NumFiles);
  95.  
  96.     DumpDataForIrit(PObjects);
  97.  
  98.     MyExit(0);
  99. }
  100.  
  101. /*****************************************************************************
  102. * Main routine to read the data    description files:                 *
  103. * Returns pointer to pointers on FileDescription structures (one per file).  *
  104. *****************************************************************************/
  105. static IPObjectStruct *MainGetDataFiles(char **DataFileNames,
  106.                     int NumOfDataFiles)
  107. {
  108.     int    i;
  109.     FILE *f;
  110.     IPObjectStruct *PObj, *PObjTail,
  111.     *PObjHead = NULL;
  112.  
  113.     for    (i = 0; i < NumOfDataFiles; i++) {
  114. #ifdef __MSDOS__
  115.     if ((f = fopen(*DataFileNames, "rt")) == NULL) {   /* Open the file. */
  116. #else
  117.     if ((f = fopen(*DataFileNames, "r")) == NULL) {    /* Open the file. */
  118. #endif /* __MSDOS__ */
  119.         fprintf(stderr, "Can't open data file %s\n", *DataFileNames);
  120.         MyExit(1);
  121.     }
  122.  
  123.     if ((PObj = IritPrsrGetObjects(f)) != NULL) {  /* Get the data file. */
  124.         PObjTail = PObj;
  125.         while (PObjTail -> Pnext) PObjTail = PObjTail -> Pnext;
  126.         PObjTail -> Pnext = PObjHead;
  127.         PObjHead = PObj;
  128.     }
  129.  
  130.     fclose(f);                      /* Close the file. */
  131.  
  132.     DataFileNames++;              /* Skip to next file name. */
  133.     }
  134.  
  135.     if (PObjHead == NULL) {
  136.     fprintf(stderr, "No data found.\n");
  137.     MyExit(1);
  138.     }
  139.  
  140.     return PObjHead;
  141. }
  142.  
  143. /*****************************************************************************
  144. * Routine to handle all surfaces/curves. Simple chain them into a linear     *
  145. * list and return it.                                 *
  146. *****************************************************************************/
  147. IPObjectStruct *IritPrsrProcessFreeForm(IPObjectStruct *CrvObjs,
  148.                     IPObjectStruct *SrfObjs)
  149. {
  150.     if (CrvObjs == NULL)
  151.     return SrfObjs;
  152.     else if (SrfObjs == NULL)
  153.     return CrvObjs;
  154.     else {
  155.     IPObjectStruct *PTmp = CrvObjs;
  156.  
  157.     while (PTmp -> Pnext) PTmp = PTmp -> Pnext;
  158.     PTmp -> Pnext = SrfObjs;
  159.     return CrvObjs;
  160.     }
  161. }
  162.  
  163. /*****************************************************************************
  164. * Dumps the data for ray shade into stdout.                     *
  165. *****************************************************************************/
  166. static void DumpDataForIrit(IPObjectStruct *PObjects)
  167. {
  168.     int NameCount = 1;
  169.     IPObjectStruct *PObj,
  170.     *PObjHead = NULL;
  171.  
  172.     /* Reverse object list since it was loaded in reverse by iritprsr module.*/
  173.     while (PObjects != NULL) {
  174.     PObj = PObjects;
  175.     PObjects = PObjects -> Pnext;
  176.     PObj -> Pnext = PObjHead;
  177.     PObjHead = PObj;
  178.     }
  179.  
  180.     for (PObjects = PObjHead; PObjects != NULL; PObjects = PObjects -> Pnext)
  181.     DumpOneObject(stdout, PObjects);
  182.  
  183.     printf("\n\ndat2irit = list(\n");
  184.     for (PObjects = PObjHead; PObjects != NULL; PObjects = PObjects -> Pnext)
  185.     if (PObjects -> Name == NULL || strlen(PObjects -> Name) == 0)
  186.     printf("\tNoName%d%s\n",
  187.            NameCount++, PObjects -> Pnext == NULL ? "" : ",");
  188.     else
  189.     printf("\t%s%s\n",
  190.            PObjects -> Name, PObjects -> Pnext == NULL ? "" : ",");
  191.     printf(");\n");
  192.  
  193. }
  194.  
  195. /*****************************************************************************
  196. * Routine to dump one object PObject.                         *
  197. *****************************************************************************/
  198. static void DumpOneObject(FILE *f, IPObjectStruct *PObject)
  199. {
  200.     static int
  201.     NameCount = 1;
  202.     int i, j;
  203.     char Name[LINE_LEN_LONG];
  204.     CagdSrfStruct *Srf;
  205.     CagdCrvStruct *Crv;
  206.     IPPolygonStruct *Pl;
  207.  
  208.     if (PObject -> Name == NULL || strlen(PObject -> Name) == 0)
  209.     sprintf(Name, "NoName%d", NameCount++);
  210.     else
  211.     strcpy(Name, PObject -> Name);
  212.  
  213.     switch (PObject -> Type) {
  214.     case IP_OBJ_POLY:
  215.         for (Pl = PObject -> U.PPolygon, i = 1;
  216.          Pl != NULL;
  217.          Pl = Pl -> Pnext, i++)
  218.         DumpOnePolygon(f, Pl, i);
  219.         fprintf(f, "%s = mergePoly( list( ", Name);
  220.         for (j = 1; j < i; j++) {
  221.         if (j % 7 == 0) fprintf(f, "\n\t\t");
  222.         fprintf(f, "POLY%d%s ", j, j == i - 1 ? "" : ",");
  223.         }
  224.         printf(") );\n\n");
  225.         break;
  226.     case IP_OBJ_SURFACE:
  227.         for (Srf = PObject -> U.PSrfs; Srf != NULL; Srf = Srf -> Pnext)
  228.         DumpOneSurface(stdout, Name, Srf);
  229.         break;
  230.     case IP_OBJ_CURVE:
  231.         for (Crv = PObject -> U.PCrvs; Crv != NULL; Crv = Crv -> Pnext)
  232.         DumpOneCurve(stdout, Name, Crv);
  233.         break;
  234.     }
  235. }
  236.  
  237. /*****************************************************************************
  238. * Routine to dump one polygon.                             *
  239. *****************************************************************************/
  240. static void DumpOnePolygon(FILE *f, IPPolygonStruct *PPolygon, int PolyNum)
  241. {
  242.     int i,
  243.     VCount = 1;
  244.     IPVertexStruct
  245.     *V = PPolygon -> PVertex,
  246.     *VFirst = V;
  247.  
  248.     do {
  249.     fprintf(f, "VRTX%d = vector( %lg, %lg, %lg );\n", VCount++,
  250.         V -> Coord[0], V -> Coord[1], V -> Coord[2]);
  251.     V = V -> Pnext;
  252.     }
  253.     while (V != VFirst && V != NULL);
  254.  
  255.     fprintf(f, "POLY%d = poly( list( ", PolyNum );
  256.     for (i = 1; i < VCount; i++) {
  257.     if (VCount % 9 == 0) fprintf(f, "\n\t\t");
  258.     fprintf(f, "VRTX%d%s ", i, i == VCount - 1 ? "" : "," );
  259.     }
  260.     fprintf(f, ") );\n\n");
  261. }
  262.  
  263. /*****************************************************************************
  264. * Routine to dump one surface.                             *
  265. *****************************************************************************/
  266. static void DumpOneSurface(FILE *f, char *Name, CagdSrfStruct *Srf)
  267. {
  268.     int i, j, k;
  269.  
  270.     switch (Srf -> GType) {
  271.     case CAGD_SBEZIER_TYPE:
  272.         fprintf(f, "%s = sbezier(\n", Name);
  273.         break;
  274.     case CAGD_SBSPLINE_TYPE:
  275.         fprintf(f, "%s = sbspline( %d, %d, \n", Name,
  276.             Srf -> UOrder, Srf -> VOrder);
  277.         break;
  278.     default:
  279.         fprintf(stderr, "Unsupported surface type ignored.\n");
  280.         return;
  281.     }
  282.  
  283.     /* Print the control mesh: */
  284.     for (i = k = 0; i < Srf -> VLength; i++) {
  285.     for (j = 0; j < Srf -> ULength; j++) {
  286.         fprintf(f, "%s", i == 0 && j == 0 ? "\tlist( " : "\t      ");
  287.         fprintf(f, "%s", j == 0 ? "list( " : "      ");
  288.         DumpCtlPt(f, Srf -> PType, Srf -> Points, k++);
  289.         fprintf(f, "%s", j == Srf -> ULength - 1 ? " )" : ",\n" );
  290.     }
  291.     if (i < Srf -> VLength - 1) fprintf(f, ",\n");
  292.     }
  293.  
  294.     switch (Srf -> GType) {
  295.     case CAGD_SBEZIER_TYPE:
  296.         fprintf(f, " ) );\n");
  297.         break;
  298.     case CAGD_SBSPLINE_TYPE:
  299.         fprintf(f, " ),\n\tlist( ");
  300.         DumpKnotVector(f, Srf -> UKnotVector,
  301.                Srf -> UOrder + Srf -> ULength);
  302.         fprintf(f, ",\n\t      ");
  303.         DumpKnotVector(f, Srf -> VKnotVector,
  304.                Srf -> VOrder + Srf -> VLength);
  305.         fprintf(f, " ) );\n");
  306.         break;
  307.     }
  308. }
  309.  
  310. /*****************************************************************************
  311. * Routine to dump one curve.                             *
  312. *****************************************************************************/
  313. static void DumpOneCurve(FILE *f, char *Name, CagdCrvStruct *Crv)
  314. {
  315.     int i;
  316.  
  317.     switch (Crv -> GType) {
  318.     case CAGD_CBEZIER_TYPE:
  319.         fprintf(f, "%s = cbezier(\n", Name);
  320.         break;
  321.     case CAGD_CBSPLINE_TYPE:
  322.         fprintf(f, "%s = cbspline( %d,\n", Name, Crv -> Order);
  323.         break;
  324.     default:
  325.         fprintf(stderr, "Unsupported curve type ignored.\n");
  326.         return;
  327.     }
  328.  
  329.     /* Print the control polygon: */
  330.     fprintf(f, "\tlist( ");
  331.     for (i = 0; i < Crv -> Length; i++) {
  332.     DumpCtlPt(f, Crv -> PType, Crv -> Points, i);
  333.     if (i < Crv -> Length - 1) fprintf(f, ",\n\t      ");
  334.     }
  335.  
  336.     switch (Crv -> GType) {
  337.     case CAGD_CBEZIER_TYPE:
  338.         fprintf(f, " ) );\n");
  339.         break;
  340.     case CAGD_CBSPLINE_TYPE:
  341.         fprintf(f, " ),\n\t ");
  342.         DumpKnotVector(f, Crv -> KnotVector, Crv -> Order + Crv -> Length);
  343.         fprintf(f, " );\n");
  344.         break;
  345.     }
  346. }
  347.  
  348. /*****************************************************************************
  349. * Routine to dump one control point.                         *
  350. *****************************************************************************/
  351. static void DumpCtlPt(FILE *f, CagdPointType PType, RealType **Points,
  352.                                 int Index)
  353. {
  354.     fprintf(f, "ctlpt( ");
  355.     switch (PType) {
  356.     case CAGD_PT_E2_TYPE:
  357.         fprintf(f, "E2, %lg, %lg",
  358.             Points[1][Index],
  359.             Points[2][Index]);
  360.         break;
  361.     case CAGD_PT_P2_TYPE:
  362.         fprintf(f, "P2, %lg, %lg, %lg",
  363.             Points[0][Index],
  364.             Points[1][Index],
  365.             Points[2][Index]);
  366.         break;
  367.     case CAGD_PT_E3_TYPE:
  368.         fprintf(f, "E3, %lg, %lg, %lg",
  369.             Points[1][Index],
  370.             Points[2][Index],
  371.             Points[3][Index]);
  372.         break;
  373.     case CAGD_PT_P3_TYPE:
  374.         fprintf(f, "P3, %lg, %lg, %lg, %lg",
  375.             Points[0][Index],
  376.             Points[1][Index],
  377.             Points[2][Index],
  378.             Points[3][Index]);
  379.         break;
  380.     default:
  381.         fprintf(stderr, "Unsupported control point type.\n");
  382.         MyExit(1);
  383.     }
  384.     fprintf(f, " )");
  385. }
  386.  
  387. /*****************************************************************************
  388. * Routine to dump one knot vector.                         *
  389. *****************************************************************************/
  390. static void DumpKnotVector(FILE *f, RealType *KnotVector, int Length)
  391. {
  392.     int i;
  393.  
  394.     fprintf(f, "list( ");
  395.     for (i = 0; i < Length; i++)
  396.     fprintf(f, "%lg%s ", KnotVector[i], i < Length -1 ? "," : "");
  397.     fprintf(f, ")");
  398. }
  399.  
  400. #ifdef __DEBUG_MALLOC__
  401. /*****************************************************************************
  402. * My Routine to    allocate dynamic memory. All program requests must call this *
  403. * routine (no direct call to malloc). Dies if no memory.             *
  404. *****************************************************************************/
  405. static void AllocError(const char *Msg, VoidPtr *p)
  406. {
  407.     fprintf(stderr, "%s, Ptr = %p\n", Msg, p);
  408.     MyExit(3);
  409. }
  410. #endif /* __DEBUG_MALLOC__ */
  411.  
  412. /*****************************************************************************
  413. * My Routine to    allocate dynamic memory. All program requests must call this *
  414. * routine (no direct call to malloc). Dies if no memory.             *
  415. *****************************************************************************/
  416. VoidPtr MyMalloc(unsigned size)
  417. {
  418.     static int Count = 0;
  419.     VoidPtr p;
  420.  
  421. #ifdef __MSDOS__
  422.     if (Count++ == 50) {
  423.     Count = 0;
  424.     fprintf(stderr, "Core left: %ldk   \r", coreleft() / 1024);
  425.     }
  426. #endif /* __MSDOS__ */
  427.  
  428.     if ((p = malloc(size)) != NULL) return p;
  429.  
  430.     fprintf(stderr, "Not enough memory, exit.\n");
  431.     MyExit(2);
  432.  
  433.     return NULL;                    /* Make warnings silent. */
  434. }
  435.  
  436. /*****************************************************************************
  437. * My Routine to    free dynamic memory. All program requests must call this     *
  438. * routine (no direct call to free).                         *
  439. *****************************************************************************/
  440. void MyFree(VoidPtr p)
  441. {
  442. #ifdef __DEBUG_MALLOC__
  443.     switch (heapchecknode(p)) {
  444.     case _HEAPCORRUPT:
  445.         AllocError("Heap is corrupted", p);
  446.         break;
  447.     case _BADNODE:
  448.         AllocError("Attempt to free a bogus pointer", p);
  449.         break;
  450.     case _FREEENTRY:
  451.         AllocError("Attempt to free an already freed pointer", p);
  452.         break;
  453.     case _USEDENTRY:
  454.         break;
  455.     default:
  456.         AllocError("Allocation error", p);
  457.         break;
  458.  
  459.     }
  460. #endif /* __DEBUG_MALLOC__ */
  461.  
  462.     free(p);
  463. }
  464.  
  465. /*****************************************************************************
  466. * Trap Cagd_lib errors right here.                         *
  467. *****************************************************************************/
  468. void CagdFatalError(CagdFatalErrorType ErrID)
  469. {
  470.     char
  471.     *ErrorMsg = CagdDescribeError(ErrID);
  472.  
  473.     fprintf(stderr, "CAGD_LIB: %s", ErrorMsg);
  474.  
  475.     exit(-1);
  476. }
  477.  
  478. /*****************************************************************************
  479. * MyExit routine. Note it might call to CloseGraph without calling         *
  480. * InitGraph(), or call MouseClose() without MouseInit() etc. and it is the   *
  481. * responsibility of the individual modules to do nothing in these cases.     *
  482. *****************************************************************************/
  483. void MyExit(int ExitCode)
  484. {
  485. #ifdef __MSDOS__
  486.     fprintf(stderr,
  487.         "\nIrit2Ray: Core left %ldk.\n", coreleft() / 1024);
  488. #endif /* __MSDOS__ */
  489.  
  490.     exit(ExitCode);
  491. }
  492.