home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 460.lha / 3DPlot_v2.0 / sources / Data2.c < prev    next >
C/C++ Source or Header  |  1991-01-04  |  13KB  |  542 lines

  1. /*-----  PROGRAM INCLUDE FILES    -----*/
  2.  
  3. #include "common.h"
  4.  
  5. /*--------  DEFINES  ----------*/
  6.  
  7. #define NO_OUTPUT_FILE 1L
  8. #define NO_ICON_FILE   2L
  9. #define NO_INPUT_FILE  3L
  10.  
  11. #define CkErr(expression)  {if (ifferr == IFF_OKAY) ifferr = (expression);}
  12.  
  13. /*--------  GLOBAL STRUCTURES  --------*/
  14.  
  15. extern struct ILBMBase *ILBMBase;
  16. extern struct IFFBase *IFFBase;
  17.  
  18. extern struct Window *setwin;
  19. extern struct Gadget EquationD;
  20. extern struct Gadget RotationX;
  21. extern struct Gadget RotationY;
  22. extern struct Gadget RotationZ;
  23.  
  24.  
  25. /*-------  INTERNAL FUNCTIONS  --------*/
  26.  
  27. AllPlotData *AllocAllPlotData(VOID)
  28. {
  29. return ( AllocMem(sizeof(AllPlotData), MEMF_CLEAR) );
  30. }
  31.  
  32.  
  33. VOID FreeAllPlotData(AllPlotData *apd)
  34. {
  35. FreeMem( (void *)apd, sizeof(AllPlotData) );
  36. }
  37.  
  38.  
  39. VOID Init3DPL(AllPlotData *pd)
  40. {
  41. pd->dd.RotationX    =  45.;
  42. pd->dd.RotationY    = -60.;
  43. pd->dd.RotationZ    = -135.;
  44. pd->dd.OriginX        =  160;
  45. pd->dd.OriginY        =  100;
  46. pd->dd.ProjPlane    =  0.;
  47. pd->dd.ViewDist     =  20.;
  48. pd->dd.Scale        =  30.;
  49. pd->dd.LineSpacingX    =  0.25;
  50. pd->dd.LineSpacingY    =  0.25;
  51. pd->dd.PlotXmin     = -3.;
  52. pd->dd.PlotXmax     =  3.;
  53. pd->dd.PlotYmin     = -3.;
  54. pd->dd.PlotYmax     =  3.;
  55. pd->dd.PlotPrecisionX    =  0.1;
  56. pd->dd.PlotPrecisionY    =  0.1;
  57.  
  58. pd->ad.AxesXmin     = -3.;
  59. pd->ad.AxesXmax     =  3.;
  60. pd->ad.AxesYmin     = -3.;
  61. pd->ad.AxesYmax     =  3.;
  62. pd->ad.AxesZmin     = -1.;
  63. pd->ad.AxesZmax     =  1.;
  64. pd->ad.AxesPrecision    =  0.1;
  65.  
  66. pd->td.TicX        =  1.;
  67. pd->td.TicY        =  1.;
  68. pd->td.TicZ        =  1.;
  69. pd->td.NumTicX        =  1;
  70. pd->td.NumTicY        =  1;
  71. pd->td.NumTicZ        =  1;
  72.  
  73. pd->ei.DependVar    = 'Z';
  74. strcpy(pd->ei.IndependVar,"XY");
  75. strcpy(&(pd->ei.Equation[0]),"SIN(X*Y)");
  76.  
  77. pd->pi.Surface        = XANDY;
  78. pd->pi.AxesType     = AXESTYPENONE;
  79. pd->pi.RotationOrder    = ROTATEZYX;
  80.  
  81. strcpy(pd->nt,"This is a sample note");
  82.  
  83. }  /* Init3DPL */
  84.  
  85.  
  86. IFFP Save3DPL(char *filename, AllPlotData *pd)
  87. {
  88.   LONG         file;
  89.   IFFP         ifferr = IFF_OKAY;
  90.   GroupContext     fileContext, formContext;
  91.  
  92.   if (!(file = Open(filename, MODE_NEWFILE))) {
  93.      printf("Can't open output file\n");
  94.      return (NO_OUTPUT_FILE);
  95.   }
  96.  
  97.   Write(file,"x",1);  /* 1.1 so Seek to beginning works ? */
  98.  
  99. #if DEBUG
  100.   printf("\nSaving...\n");
  101. #endif
  102.  
  103.   CkErr( OpenWIFF(szNotYetKnown, file, &fileContext) );
  104.   CkErr( StartWGroup(FORM,szNotYetKnown,ID_3DPL,&fileContext,&formContext) );
  105.   CkErr( PutEQUN(&formContext, &(pd->ei)) );
  106.   CkErr( PutDRAW(&formContext, &(pd->dd)) );
  107.   CkErr( PutAXES(&formContext, &(pd->ad)) );
  108.   CkErr( PutTICS(&formContext, &(pd->td)) );
  109.   CkErr( PutTYPE(&formContext, &(pd->pi)) );
  110.   CkErr( PutNOTE(&formContext, pd->nt) );
  111.   CkErr( EndWGroup(&formContext) );
  112.   CkErr( CloseWGroup(&fileContext) );
  113.  
  114.   Close(file);
  115.  
  116. #if DEBUG
  117.   if (ifferr == IFF_OKAY) {
  118.      printf("3DPL IFF saved\n");
  119.   }
  120.   else {
  121.      printf("Error on save\n");
  122.   }
  123. #endif
  124.  
  125.   return(ifferr);
  126.  
  127. }  /* Save3DPL */
  128.  
  129.  
  130. /*
  131. IFFP Read3DPL(char *filename, AllPlotData *pd)
  132. {
  133.   LONG         file;
  134.   IFFP         ifferr = IFF_OKAY;
  135.   ID         CkHdr;
  136.   GroupContext     fileContext, formContext;
  137.  
  138.   if (!(file = Open(filename, MODE_OLDFILE))) {
  139.      printf("Can't open input file\n");
  140.      return (NO_INPUT_FILE);
  141.   }
  142.  
  143. #if DEBUG
  144.   printf("\nReading...\n");
  145. #endif
  146.  
  147. CkErr( OpenRIFF(file, &fileContext, NULL) );
  148. CkErr( OpenRGroup(&fileContext,&formContext) );
  149.  
  150. CkHdr = GetFChunkHdr(&formContext);
  151.  
  152. while( (CkHdr != END_MARK) && (CkHdr != BAD_IFF) && (ifferr == IFF_OKAY) ) {
  153.  
  154.    switch(CkHdr) {
  155.  
  156.       case ID_EQUN:
  157.      ifferr = IFFReadBytes(sizeof(EqInfo),&formContext,&(pd->ei));
  158.      break;
  159.  
  160.       case ID_DRAW:
  161.      ifferr = IFFReadBytes(sizeof(DrawData),&formContext,&(pd->dd));
  162.      break;
  163.  
  164.       case ID_AXES:
  165.      ifferr = IFFReadBytes(sizeof(AxesData),&formContext,&(pd->ad));
  166.      break;
  167.  
  168.       case ID_TICS:
  169.      ifferr = IFFReadBytes(sizeof(TicData),&formContext,&(pd->td));
  170.      break;
  171.  
  172.       case ID_TYPE:
  173.      ifferr = IFFReadBytes(sizeof(PlotInfo),&formContext,&(pd->pi));
  174.      break;
  175.  
  176.       case ID_NOTE:
  177.      ifferr = IFFReadBytes(NOTELEN,&formContext,pd->nt);
  178.      break;
  179.  
  180.    } /* switch */
  181.  
  182.    CkHdr = GetChunkHdr(&formContext);
  183.  
  184. } /* while */
  185.  
  186.   CkErr( CloseRGroup(&formContext) );
  187.   CkErr( CloseRGroup(&fileContext) );
  188.  
  189.   Close(file);
  190.  
  191. #if DEBUG
  192.   if (ifferr == IFF_OKAY) {
  193.      printf("3DPL IFF read\n");
  194.   }
  195.   else {
  196.      printf("Error on read\n");
  197.   }
  198. #endif
  199.  
  200.   return(ifferr);
  201.  
  202. }  /* Read3DPL */
  203. */
  204.  
  205. LONG Read3DPL(STRPTR filename, AllPlotData *pd)
  206. {
  207.    APTR ifffile;
  208.    ULONG *chunk;
  209.  
  210. if( !(ifffile = OldOpenIFF(filename)) )  return IffError();
  211.  
  212. if( !(chunk = (ULONG *)FindChunk(ifffile, ID_EQUN)) )  goto endread;
  213. memcpy( &(pd->ei), (chunk+2), sizeof(EqInfo) );
  214.  
  215. if( !(chunk = (ULONG *)FindChunk(ifffile, ID_DRAW)) )  goto endread;
  216. memcpy( &(pd->dd), (chunk+2), sizeof(DrawData) );
  217.  
  218. if( !(chunk = (ULONG *)FindChunk(ifffile, ID_AXES)) )  goto endread;
  219. memcpy( &(pd->ad), (chunk+2), sizeof(AxesData) );
  220.  
  221. if( !(chunk = (ULONG *)FindChunk(ifffile, ID_TICS)) )  goto endread;
  222. memcpy( &(pd->td), (chunk+2), sizeof(TicData) );
  223.  
  224. if( !(chunk = (ULONG *)FindChunk(ifffile, ID_TYPE)) )  goto endread;
  225. memcpy( &(pd->pi), (chunk+2), sizeof(PlotInfo) );
  226.  
  227. if( !(chunk = (ULONG *)FindChunk(ifffile, ID_NOTE)) )  goto endread;
  228. memcpy( &(pd->nt), (chunk+2), NOTELEN );
  229.  
  230.  
  231. endread:
  232.  
  233. CloseIFF(ifffile);
  234. return IffError();
  235.  
  236. }  /* Read3DPL */
  237.  
  238.  
  239. VOID PrintData(AllPlotData *pd)
  240. {
  241. printf("pd->dd.RotationX = %f\n", pd->dd.RotationX);
  242. printf("pd->dd.RotationY = %f\n", pd->dd.RotationY);
  243. printf("pd->dd.RotationZ = %f\n", pd->dd.RotationZ);
  244. printf("pd->dd.OriginX = %d\n", pd->dd.OriginX);
  245. printf("pd->dd.OriginY = %d\n", pd->dd.OriginY);
  246. printf("pd->dd.ProjPlane = %f\n", pd->dd.ProjPlane);
  247. printf("pd->dd.ViewDist = %f\n", pd->dd.ViewDist);
  248. printf("pd->dd.Scale = %f\n", pd->dd.Scale);
  249. printf("pd->dd.LineSpacingX = %f\n", pd->dd.LineSpacingX);
  250. printf("pd->dd.LineSpacingY = %f\n", pd->dd.LineSpacingY);
  251. printf("pd->dd.PlotXmin = %f\n", pd->dd.PlotXmin);
  252. printf("pd->dd.PlotXmax = %f\n", pd->dd.PlotXmax);
  253. printf("pd->dd.PlotYmin = %f\n", pd->dd.PlotYmin);
  254. printf("pd->dd.PlotYmax = %f\n", pd->dd.PlotYmax);
  255. printf("pd->dd.PlotPrecisionX = %f\n", pd->dd.PlotPrecisionX);
  256. printf("pd->dd.PlotPrecisionY = %f\n", pd->dd.PlotPrecisionY);
  257.  
  258. printf("pd->ad.AxesXmin = %f\n", pd->ad.AxesXmin);
  259. printf("pd->ad.AxesXmax = %f\n", pd->ad.AxesXmax);
  260. printf("pd->ad.AxesYmin = %f\n", pd->ad.AxesYmin);
  261. printf("pd->ad.AxesYmax = %f\n", pd->ad.AxesYmax);
  262. printf("pd->ad.AxesZmin = %f\n", pd->ad.AxesZmin);
  263. printf("pd->ad.AxesZmax = %f\n", pd->ad.AxesZmax);
  264. printf("pd->ad.AxesPrecision = %f\n", pd->ad.AxesPrecision);
  265.  
  266. printf("pd->td.TicX = %f\n", pd->td.TicX);
  267. printf("pd->td.TicY = %f\n", pd->td.TicY);
  268. printf("pd->td.TicZ = %f\n", pd->td.TicZ);
  269. printf("pd->td.NumTicX = %d\n", pd->td.NumTicX);
  270. printf("pd->td.NumTicY = %d\n", pd->td.NumTicY);
  271. printf("pd->td.NumTicZ = %d\n", pd->td.NumTicZ);
  272.  
  273. printf("pd->ei.DependVar = %c\n", pd->ei.DependVar);
  274. printf("pd->ei.IndependVar = %s\n", pd->ei.IndependVar);
  275. printf("pd->ei.Equation = %s\n", pd->ei.Equation);
  276.  
  277. printf("pd->pi.Surface = %d\n", pd->pi.Surface);
  278. printf("pd->pi.AxesType = %d\n", pd->pi.AxesType);
  279. printf("pd->pi.RotationOrder = %d\n", pd->pi.RotationOrder);
  280.  
  281. printf("pd->nt = %s\n", pd->nt);
  282.  
  283. } /* PrintData */
  284.  
  285.  
  286. VOID SetGadgets(struct Gadget *gadget, AllPlotData *pd)
  287.    /* gadget is the Gadget to begin with
  288.       pd is the data to insert           */
  289. {
  290.    struct Gadget *fgadget;  /* Copy of first gadget */
  291.    STRPTR gtext;
  292.    STRPTR string;
  293.    LONG *longint;
  294.  
  295.    fgadget = gadget;
  296.  
  297.    while(gadget) {   /* loop until no more gadgets */
  298.  
  299.       string = ((struct StringInfo *)(gadget->SpecialInfo))->Buffer;
  300.       longint = &(((struct StringInfo *)(gadget->SpecialInfo))->LongInt);
  301.  
  302.       switch(gadget->GadgetID) {
  303.  
  304.      case SURFACE:
  305.         gtext = gadget->GadgetText->NextText->IText;
  306.         switch(pd->pi.Surface) {
  307.            case XONLY:
  308.           strcpy(gtext, "X Only ");
  309.           break;
  310.            case YONLY:
  311.           strcpy(gtext, "Y Only ");
  312.           break;
  313.            case XANDY:
  314.           strcpy(gtext, "X and Y");
  315.           break;
  316.            default:
  317.           break;
  318.         } /* switch */
  319.         break;
  320.  
  321.      case AXESTYPE:
  322.         gtext = gadget->GadgetText->NextText->IText;
  323.         switch(pd->pi.AxesType) {
  324.            case AXESTYPENONE:
  325.           strcpy(gtext, "None");
  326.           break;
  327.            case AXESTYPESTAR:
  328.           strcpy(gtext, "Star");
  329.           break;
  330.            case AXESTYPEBOX:
  331.           strcpy(gtext, "Box ");
  332.           break;
  333.            default:
  334.           break;
  335.         } /* switch */
  336.         break;
  337.  
  338.      case SWAPXYZ:
  339.         {               /* begin block */
  340.         WORD tempTopEdge[3];
  341.         struct Gadget *tempGadget;
  342.  
  343.         /* Obtain three top edges for X, Y, and Z gadgets in order */
  344.         tempTopEdge[0] = RotationX.TopEdge;
  345.         tempTopEdge[1] = RotationY.TopEdge;
  346.         tempTopEdge[2] = RotationZ.TopEdge;
  347.         sqsort(&tempTopEdge[0], 3);  /* Sort short integers, ascending */
  348.  
  349.         /* Obtain gadget after X, Y, and Z gadgets */
  350.         tempGadget = RotationX.NextGadget;
  351.         while( (tempGadget == &RotationY) || (tempGadget == &RotationZ) ) {
  352.            tempGadget = tempGadget->NextGadget;
  353.         }
  354.  
  355.         switch(pd->pi.RotationOrder) {
  356.            case ROTATEXYZ:
  357.                  /* New positions */
  358.           RotationX.TopEdge = tempTopEdge[0];
  359.           RotationY.TopEdge = tempTopEdge[1];
  360.           RotationZ.TopEdge = tempTopEdge[2];
  361.                  /* Order gadgets in list */
  362.           EquationD.NextGadget = &RotationX;
  363.           RotationX.NextGadget = &RotationY;
  364.           RotationY.NextGadget = &RotationZ;
  365.           RotationZ.NextGadget = tempGadget;
  366.           break;
  367.            case ROTATEXZY:
  368.                  /* New positions */
  369.           RotationX.TopEdge = tempTopEdge[0];
  370.           RotationZ.TopEdge = tempTopEdge[1];
  371.           RotationY.TopEdge = tempTopEdge[2];
  372.                  /* Order gadgets in list */
  373.           EquationD.NextGadget = &RotationX;
  374.           RotationX.NextGadget = &RotationZ;
  375.           RotationZ.NextGadget = &RotationY;
  376.           RotationY.NextGadget = tempGadget;
  377.           break;
  378.            case ROTATEYXZ:
  379.                  /* New positions */
  380.           RotationY.TopEdge = tempTopEdge[0];
  381.           RotationX.TopEdge = tempTopEdge[1];
  382.           RotationZ.TopEdge = tempTopEdge[2];
  383.                  /* Order gadgets in list */
  384.           EquationD.NextGadget = &RotationY;
  385.           RotationY.NextGadget = &RotationX;
  386.           RotationX.NextGadget = &RotationZ;
  387.           RotationZ.NextGadget = tempGadget;
  388.           break;
  389.            case ROTATEYZX:
  390.                  /* New positions */
  391.           RotationY.TopEdge = tempTopEdge[0];
  392.           RotationZ.TopEdge = tempTopEdge[1];
  393.           RotationX.TopEdge = tempTopEdge[2];
  394.                  /* Order gadgets in list */
  395.           EquationD.NextGadget = &RotationY;
  396.           RotationY.NextGadget = &RotationZ;
  397.           RotationZ.NextGadget = &RotationX;
  398.           RotationX.NextGadget = tempGadget;
  399.           break;
  400.            case ROTATEZXY:
  401.                  /* New positions */
  402.           RotationZ.TopEdge = tempTopEdge[0];
  403.           RotationX.TopEdge = tempTopEdge[1];
  404.           RotationY.TopEdge = tempTopEdge[2];
  405.                  /* Order gadgets in list */
  406.           EquationD.NextGadget = &RotationZ;
  407.           RotationZ.NextGadget = &RotationX;
  408.           RotationX.NextGadget = &RotationY;
  409.           RotationY.NextGadget = tempGadget;
  410.           break;
  411.            case ROTATEZYX:
  412.                  /* New positions */
  413.           RotationZ.TopEdge = tempTopEdge[0];
  414.           RotationY.TopEdge = tempTopEdge[1];
  415.           RotationX.TopEdge = tempTopEdge[2];
  416.                  /* Order gadgets in list */
  417.           EquationD.NextGadget = &RotationZ;
  418.           RotationZ.NextGadget = &RotationY;
  419.           RotationY.NextGadget = &RotationX;
  420.           RotationX.NextGadget = tempGadget;
  421.           break;
  422.            default:
  423.           break;
  424.         } /* switch */
  425.         } /* end block */
  426.         break;
  427.  
  428.      case ROTATIONX:
  429.         *longint = (LONG)(pd->dd.RotationX);
  430.         sprintf(string, "%d", *longint);
  431.         break;
  432.  
  433.      case ROTATIONY:
  434.         *longint = (LONG)(pd->dd.RotationY);
  435.         sprintf(string, "%d", *longint);
  436.         break;
  437.  
  438.      case ROTATIONZ:
  439.         *longint = (LONG)(pd->dd.RotationZ);
  440.         sprintf(string, "%d", *longint);
  441.         break;
  442.  
  443.      case ORIGINX:
  444.         *longint = pd->dd.OriginX;
  445.         sprintf(string, "%d", *longint);
  446.         break;
  447.  
  448.      case ORIGINY:
  449.         *longint = pd->dd.OriginY;
  450.         sprintf(string, "%d", *longint);
  451.         break;
  452.  
  453.      case SIZINGPROJPLANE:
  454.         gcvt(pd->dd.ProjPlane, 10, string);
  455.         break;
  456.  
  457.      case SIZINGVIEWDIST:
  458.         gcvt(pd->dd.ViewDist, 10, string);
  459.         break;
  460.  
  461.      case SIZINGSCALE:
  462.         gcvt(pd->dd.Scale, 10, string);
  463.         break;
  464.  
  465.      case LINESPACINGX:
  466.         gcvt(pd->dd.LineSpacingX, 10, string);
  467.         break;
  468.  
  469.      case LINESPACINGY:
  470.         gcvt(pd->dd.LineSpacingY, 10, string);
  471.         break;
  472.  
  473.      case PLOTSPANXMIN:
  474.         gcvt(pd->dd.PlotXmin, 10, string);
  475.         break;
  476.  
  477.      case PLOTSPANXMAX:
  478.         gcvt(pd->dd.PlotXmax, 10, string);
  479.         break;
  480.  
  481.      case PLOTSPANYMIN:
  482.         gcvt(pd->dd.PlotYmin, 10, string);
  483.         break;
  484.  
  485.      case PLOTSPANYMAX:
  486.         gcvt(pd->dd.PlotYmax, 10, string);
  487.         break;
  488.  
  489.      case PLOTPRECISIONX:
  490.         gcvt(pd->dd.PlotPrecisionX, 10, string);
  491.         break;
  492.  
  493.      case PLOTPRECISIONY:
  494.         gcvt(pd->dd.PlotPrecisionY, 10, string);
  495.         break;
  496.  
  497.      case AXESSPANXMIN:
  498.         gcvt(pd->ad.AxesXmin, 10, string);
  499.         break;
  500.  
  501.      case AXESSPANXMAX:
  502.         gcvt(pd->ad.AxesXmax, 10, string);
  503.         break;
  504.  
  505.      case AXESSPANYMIN:
  506.         gcvt(pd->ad.AxesYmin, 10, string);
  507.         break;
  508.  
  509.      case AXESSPANYMAX:
  510.         gcvt(pd->ad.AxesYmax, 10, string);
  511.         break;
  512.  
  513.      case AXESSPANZMIN:
  514.         gcvt(pd->ad.AxesZmin, 10, string);
  515.         break;
  516.  
  517.      case AXESSPANZMAX:
  518.         gcvt(pd->ad.AxesZmax, 10, string);
  519.         break;
  520.  
  521.      case AXESPRECISION:
  522.         gcvt(pd->ad.AxesPrecision, 10, string);
  523.         break;
  524.  
  525.      case EQUATIONZ:
  526.         strcpy(string, pd->ei.Equation);
  527.         break;
  528.  
  529.      case SETTINGSNOTE:
  530.         strcpy(string, pd->nt);
  531.         break;
  532.  
  533.       } /* switch */
  534.  
  535.       gadget = gadget->NextGadget;
  536.  
  537.    } /* while */
  538.  
  539.    RefreshGadgets(fgadget, setwin, NULL);
  540.  
  541. } /* SetGadgets */
  542.