home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / linkedit / linkedit.lha / link-edit / LinkEdit / Link / link_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-13  |  5.6 KB  |  199 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <X11/Xlib.h>
  4. #include "link_types.h"
  5. #include "link_global.h"
  6.  
  7.  
  8. LinkPrintLink(gnrc,fp,lnk)
  9. LinkStatus *gnrc; FILE *fp; LinkList *lnk;
  10. /* Prints out coordinate data for strand */
  11. /* Routine does not print initial Strand id, but does print End */
  12. {
  13.   LinkPointList *pnt;
  14.  
  15.   fprintf(fp,"  closed %d\n",lnk->closed);
  16.   fprintf(fp,"  data %d\n",lnk->num);
  17.   pnt = lnk->point.next;
  18.   while(pnt != NULL) {
  19.      fprintf(fp,"    %.4lf  %.4lf  %.4lf\n",pnt->x,pnt->y,pnt->z);
  20.      pnt = pnt->next;
  21.     }
  22.   fprintf(fp," End\n\n");
  23.   return(0);
  24. }
  25.  
  26. LinkPrintStrandPS(gnrc,fp,lnk)
  27. LinkStatus *gnrc; FILE *fp; LinkList *lnk;
  28. /* Prints out PS file for the lnk, using millimeters as fundamental unit */
  29. {
  30.   LinkPointList *pnt,*nxt;
  31.   pnt = lnk->point.next;
  32.   if(pnt == NULL) return(0);
  33.  
  34.   while(pnt != NULL) {
  35.      LinkPrintEdgePS(gnrc,fp,lnk,pnt);
  36.      pnt = pnt->next;
  37.     }
  38. }
  39.  
  40. LinkPrintEdgePS(gnrc,fp,lnk,pnt)
  41. LinkStatus *gnrc; FILE *fp; LinkList *lnk; LinkPointList *pnt;
  42. /* Prints out PS file for the edge, using millimeters as fundamental unit */ 
  43. {
  44.   LinkPointList *nxt;
  45.   LinkCrossingList *crssng;
  46.   double x,y,param,gamma;
  47.   double tmp_x,tmp_y,delta_x,delta_y;
  48.  
  49.   if(pnt == NULL) return(0);
  50.   if(pnt->next == NULL && lnk->closed == 0) return(0);
  51.   nxt = pnt->next;
  52.   if(nxt == NULL) /* Closed link, use first element */
  53.   nxt = lnk->point.next;
  54.  
  55.   crssng = pnt->crossing.next;
  56.   if(crssng == NULL) {  /* just draw line */
  57.      fprintf(fp,"%.4lf %.4lf  %.4lf %.4lf vec\n",(double) (pnt->dcx)/xppmm,
  58.                           (double) (pnt->dcy)/yppmm,
  59.                           (double) (nxt->dcx)/xppmm,
  60.                           (double) (nxt->dcy)/yppmm);
  61.     }
  62.   else {  /* Draw crossing gaps */
  63.           /* Compute crossing gap vector */
  64.      tmp_x = (double) (nxt->dcx - pnt->dcx)/xppmm;
  65.      tmp_y = (double) (nxt->dcy - pnt->dcy)/yppmm;
  66.  
  67.      /* gamma = half fraction of total length devoted to each gap */
  68.      gamma = 0.5*LINK_GAP/sqrt(tmp_x*tmp_x + tmp_y*tmp_y);
  69.      delta_x = gamma * tmp_x; 
  70.      delta_y = gamma * tmp_y; 
  71.  
  72.      x = (double) (pnt->dcx)/xppmm; 
  73.      y = (double) (pnt->dcy)/yppmm;
  74.      param = 0;
  75.  
  76.      while(crssng != NULL) {
  77.         if(crssng->z < 0) {
  78.            if(param < crssng->param - gamma) {
  79.               fprintf(fp,"%.4lf %.4lf  %.4lf %.4lf vec\n",x,y,
  80.                       (double) (crssng->dcx)/xppmm - delta_x,
  81.                       (double) (crssng->dcy)/yppmm - delta_y);
  82.              }
  83.            if((param = crssng->param + gamma) > 1.0) {
  84.               param = 1.0;
  85.               x = (double) (nxt->dcx)/xppmm;
  86.               y = (double) (nxt->dcy)/yppmm;
  87.              }
  88.            else {
  89.               x = (double) (crssng->dcx)/xppmm  + delta_x;
  90.               y = (double) (crssng->dcy)/yppmm  + delta_y;
  91.              }
  92.           }
  93.         crssng = crssng->next;
  94.        }
  95.      /* Draw final piece */
  96.      if(param < 1.0) {
  97.         fprintf(fp,"%.4lf %.4lf  %.4lf %.4lf vec\n",x,y,
  98.                             (double) (nxt->dcx)/xppmm,
  99.                             (double) (nxt->dcy)/yppmm);
  100.        }
  101.     }
  102. }
  103.  
  104. LinkPrintStrand3D(gnrc,fp,lnk)
  105. LinkStatus *gnrc; FILE *fp; LinkList *lnk;
  106. /* Prints out coordinate data for lnk, incorporating in */
  107. /* the crossings as part of the data.  The result is a */
  108. /* tubable strand.  The header is printed by the caller */
  109. /* The format is the geometry group LINK format */
  110. /* If a strand is not closed a warning message is printed */
  111. {
  112.   
  113.   LinkPointList *pnt;
  114.   LinkCrossingList *crssng;
  115.  
  116.   if(!lnk->closed) {
  117.      fprintf(stderr,"Warning! Strand not closed! Printing data anyway...\n");
  118.     }
  119.   pnt = lnk->point.next;
  120.   while(pnt != NULL) {
  121.      fprintf(fp,"    %.4lf  %.4lf  %.4lf\n",pnt->x,pnt->y,pnt->z);
  122.      crssng = pnt->crossing.next;
  123.      while(crssng != NULL) {
  124.         fprintf(fp,"    %.4lf  %.4lf  %.4lf\n",crssng->x,crssng->y,crssng->z);
  125.         crssng = crssng->next;
  126.        }
  127.      pnt = pnt->next;
  128.     }
  129.   return(0);
  130. }
  131.  
  132. LinkReadStrand(gnrc,fp,lnk)
  133. LinkStatus *gnrc; FILE *fp; LinkList *lnk;
  134. /* Reads strand data into lnk. */
  135. /* Reads AFTER initial strand id to End */
  136. /* returns 0 if successful */
  137. {
  138.   int i,done,ch;
  139.   char strng[40];
  140.   LinkPointList *pnt;
  141.   double x,y,z;
  142.  
  143.   done = 0;
  144.   while(!done) {
  145.      if(fscanf(fp,"%s",strng) != 1) {
  146.         LinkPrintMessage(gnrc,"Error reading link file.");
  147.         return(-1);
  148.        }
  149.      if(strcmp(strng,"closed") == 0) {
  150.         fscanf(fp,"%d",&(lnk->closed));
  151.         continue;
  152.        }
  153.  
  154.      if(strcmp(strng,"data") == 0) {
  155.         fscanf(fp,"%d",&(lnk->num));
  156.         pnt = &(lnk->point);
  157.         for(i=0;i<lnk->num;++i) {
  158.            if(fscanf(fp,"%lf %lf %lf",&x,&y,&z) != 3) {
  159.               LinkPrintMessage(gnrc,"Error reading link file.");
  160.               return(-1);
  161.              }
  162.            pnt->next = (LinkPointList *) malloc(sizeof(LinkPointList));
  163.            if(pnt->next == NULL) {
  164.               LinkPrintMessage(gnrc,"Out of memory reading link file.");
  165.               return(-1);
  166.              }
  167.            pnt->next->previous = pnt;
  168.            pnt = pnt->next;
  169.            pnt->next = NULL;
  170.            pnt->crossing.next = NULL;
  171.            pnt->point_id = i;
  172.            pnt->x = x; pnt->y = y; pnt->z = z; 
  173.            LinkComputePointDeviceCoords(gnrc,pnt);
  174.           }
  175.         continue;
  176.        }
  177.  
  178.      if(strng[0] == '#') { /* Read till end of line */
  179.         ch = 'a';
  180.         while(ch != '\n' && ch != '\r') {
  181.            ch = fgetc(fp);
  182.            if(ch == EOF) {
  183.               LinkPrintMessage(gnrc,"Error reading link file comment.");
  184.               return(-1);
  185.              }
  186.           }
  187.        }
  188.  
  189.      if(strcmp(strng,"End") ==  0) {
  190.        done = 1;
  191.        continue;
  192.       }
  193.     }
  194.   return(0);
  195. }
  196.  
  197.  
  198.  
  199.