home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / ASCENDER / ascendMar8.tar / UMass / BuildingFinder / Staging / list.c < prev    next >
C/C++ Source or Header  |  1996-02-29  |  4KB  |  215 lines

  1. #include "../polygons.h"
  2.  
  3. cList *read_corners_from_file(char *file)
  4. {
  5.  
  6.     FILE *fp, *fopen();
  7.     cList *list;
  8.  
  9.     int x,y;
  10.     int value;
  11.     double leg1Dx, leg1Dy, leg2Dx, leg2Dy;
  12.     double Dx1, Dy1, Dx2, Dy2;
  13.     double height;
  14.     
  15.     
  16.     if ( (fp = fopen(file,"r")) == NULL) {
  17.         fprintf(stderr,"Cannot open Corner Data file.\n");
  18.         fprintf(stderr,"[%s]\n",file);
  19.         return(NULL);
  20.     }
  21.  
  22.     list = NULL;
  23.  
  24.     while (fscanf(fp,"%d %d %lf %d %lf %lf %lf %lf",
  25.         &x,&y,&height,&value,&Dx1,&Dy1,&Dx2,&Dy2) != EOF) {
  26.     
  27.            /** As a convention, Axis2 points primarally in the Y
  28.            direction, while AXIS1 points in the X.
  29.     
  30.            This helps to decide how to assign the corner vectors
  31.             to an axis.  It is a cludge only needed because of
  32.            the dfs_visit implementation **/
  33.  
  34.            if (fabs(Dx1) > fabs(Dy1)) {
  35.             leg1Dx = Dx1;  leg1Dy = Dy1;
  36.             leg2Dx = Dx2;  leg2Dy = Dy2;
  37.            }  else {
  38.             leg1Dx = Dx2;  leg1Dy = Dy2;
  39.             leg2Dx = Dx1;  leg2Dy = Dy1;
  40.            }
  41.  
  42.  
  43.            if (list == NULL) {
  44.               list = create_list(x,y,height,value,
  45.                 leg1Dx,leg1Dy,leg2Dx,leg2Dy);
  46.  
  47.  
  48.            } else {
  49.               list = insert_corner(list,x,y,height,value,
  50.                     leg1Dx,leg1Dy,leg2Dx,leg2Dy);
  51.            }
  52.  
  53.         }
  54.     fclose(fp);
  55.  
  56.     return(list);
  57. }
  58.  
  59. cList *create_list(int x, int y, double height, int mag,
  60.         double leg1Dx, double leg1Dy, double leg2Dx, double leg2Dy)
  61. {
  62.     cList *list;
  63.  
  64.     list = (cList *) malloc(sizeof(cList));
  65.     list->x = x; list->y = y;
  66.     list->height = height;
  67.     list->magnitude = mag;
  68.     list->leg1Dx = leg1Dx;
  69.     list->leg1Dy = leg1Dy;
  70.     list->leg2Dx = leg2Dx;
  71.     list->leg2Dy = leg2Dy;
  72.     list->P = 0;
  73.     list->last = NULL;
  74.     list->next = NULL;
  75.     list->eList = NULL;
  76.  
  77.     return(list);
  78. }
  79.  
  80. cList *insert_corner(cList *list,int x,int y,double height, int mag,
  81.          double leg1Dx, double leg1Dy, double leg2Dx, double leg2Dy)
  82. {
  83.  
  84.     cList *ptr;
  85.     int   done=FALSE;
  86.     cList *newList;
  87.  
  88.     newList = list;
  89.     
  90.     ptr = list;
  91.     while (!done) {
  92.        if (mag >= ptr->magnitude) {
  93.         newList = insert(list,ptr,x,y,height,mag,
  94.                 leg1Dx,leg1Dy,leg2Dx,leg2Dy);
  95.         done = TRUE;
  96.        } else
  97.           if (ptr->next == NULL) {
  98.         insert_end(ptr,x,y,height,mag,leg1Dx,leg1Dy,leg2Dx,
  99.                leg2Dy);
  100.         done = TRUE;
  101.           }
  102.        ptr = ptr->next;
  103.     }
  104.  
  105.     return(newList);
  106. }
  107.  
  108. cList *insert(cList *list, cList *ptr, int x, int y, double height, int mag,
  109.          double leg1Dx, double leg1Dy, double leg2Dx,
  110.          double leg2Dy)
  111. {
  112.  
  113.     cList *newNode;
  114.  
  115.     newNode = (cList *) malloc(sizeof(cList));
  116.     newNode->x = x;
  117.     newNode->y = y;
  118.     newNode->height = height;
  119.     newNode->leg1Dx = leg1Dx;
  120.     newNode->leg1Dy = leg1Dy;
  121.     newNode->leg2Dx = leg2Dx;
  122.     newNode->leg2Dy = leg2Dy;
  123.     newNode->P = 0;
  124.     newNode->magnitude = mag;
  125.     newNode->next = ptr;
  126.     newNode->last = ptr->last;
  127.     newNode->eList = NULL;
  128.  
  129.  
  130.     ptr->last = newNode;
  131.     if (newNode->last != NULL) {
  132.         newNode->last->next = newNode;
  133.         return(list);
  134.     }
  135.     else {
  136.         return(newNode);
  137.     }
  138. }
  139.  
  140. void insert_end(cList *ptr, int x, int y, double height, int mag,
  141.          double leg1Dx, double leg1Dy, double leg2Dx, double leg2Dy)
  142. {
  143.     cList *newNode;
  144.  
  145.     newNode = (cList *) malloc(sizeof(cList));
  146.     newNode->x = x;
  147.     newNode->y = y;
  148.     newNode->height = height;
  149.     newNode->P = 0;
  150.     newNode->leg1Dx = leg1Dx;
  151.     newNode->leg1Dy = leg1Dy;
  152.     newNode->leg2Dx = leg2Dx;
  153.     newNode->leg2Dy = leg2Dy;
  154.     newNode->magnitude = mag;
  155.     newNode->next = NULL;
  156.     newNode->last = ptr;
  157.     newNode->eList = NULL;
  158.     ptr->next = newNode;
  159. }
  160.  
  161.  
  162. void print_corner_list(cList *ptr)
  163. {
  164.  
  165.     cList *temp;
  166.     
  167.     fprintf(stderr,"    CORNER DATA \n\n");
  168.     
  169.     temp = ptr;
  170.     while (temp != NULL) {
  171.         fprintf(stderr,"(%d %d  %f)      %d\n",temp->x,temp->y,
  172.                             temp->height,
  173.                             temp->magnitude);
  174.         draw_fat_point(in_pane,(double)temp->x,(double)temp->y,3.0); 
  175.         temp=temp->next;
  176.     }
  177.     
  178. }
  179.  
  180.  
  181. void write_corner_list_to_file(char *fname, cList *ptr)
  182. {
  183.  
  184.     FILE *fopen(), *fp;
  185.     cList *temp;
  186.     
  187.     if ( (fp = fopen(fname,"w")) == NULL) {
  188.         fprintf(stderr,"Cannot open Corner Data File.\n");
  189.         exit(1);
  190.     }
  191.     
  192.     temp = ptr;
  193.     while(temp != NULL) {
  194.         fprintf(fp,"%d %d %d\n",temp->x,temp->y,temp->magnitude);
  195.         temp = temp->next;
  196.     }
  197.  
  198.     fclose(fp);
  199. }
  200.         
  201.  
  202.  
  203.         
  204. void clear_list(cList *list)
  205. {
  206.  
  207.     cList *p, *q;
  208.  
  209.     for (p = list; p != NULL; p = q) {
  210.         q = p->next;
  211.         free(p);
  212.     }
  213. }
  214.  
  215.