home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / csrpoly2.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  7KB  |  252 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    csrpoly2.c (Cursor Polygon Points)
  6.  * Purpose:    Maintain the vertex lists for drawing a polygon cursor
  7.  * Subroutine:    start_polygon()                returns: void
  8.  * Subroutine:    collapse_polygon()            returns: void
  9.  * Subroutine:    add_polygon_vertex()            returns: void
  10.  * Subroutine:    delete_polygon_vertex()            returns: void
  11.  * Subroutine:    set_polygon_hashmarks()            returns: void
  12.  * Subroutine:    set_active_polygon_hashmark()        returns: void
  13.  * Subroutine:    copy_polygon_region_to_cursor()        returns: void
  14.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  15.  *        You may do anything you like with this file except remove
  16.  *        this copyright.  The Smithsonian Astrophysical Observatory
  17.  *        makes no representations about the suitability of this
  18.  *        software for any purpose.  It is provided "as is" without
  19.  *        express or implied warranty.
  20.  * Modified:    {0} Michael VanHilst    initial version          4 June 1989
  21.  *        {n} <who> -- <does what> -- <when>
  22.  */
  23.  
  24. #include <stdio.h>        /* stderr, NULL, etc. */
  25. #include <X11/Xlib.h>        /* X window stuff */
  26. #include <X11/Xutil.h>        /* X window manager stuff */
  27. #include "hfiles/color.h"    /* cursor colors needed by Cursor.h */
  28. #include "hfiles/constant.h"    /* define codes */
  29. #include "hfiles/coord.h"    /* coord structs */
  30. #include "hfiles/cursor.h"    /* define cursor parameter structures */
  31.  
  32. /* define parameters for hash marks (size, number of vertices available */
  33. #define HASH_RAY 2
  34. #define HASH_SIDE 4
  35.  
  36. static PolyPoint *poly;
  37. static XRectangle *rectangles;
  38. static polysz = 0;
  39.  
  40. /*
  41.  * Subroutine:    collapse_polygon
  42.  * Purpose:    Reset polygon to the last manipulated point
  43.  */
  44. void collapse_polygon ( cursor )
  45.      struct cursorRec *cursor;
  46. {
  47.   int active;
  48.   active = cursor->ctrl.active_vertex;
  49.   bcopy((char *)&cursor->poly[active], (char *)cursor->poly,
  50.     sizeof(PolyPoint));
  51.   cursor->win.X = cursor->poly[0].winX;
  52.   cursor->win.Y = cursor->poly[0].winY;
  53.   cursor->win.x = (int)cursor->win.X;
  54.   cursor->win.x = (int)cursor->win.X;
  55.   cursor->file.X = cursor->poly[0].fileX;
  56.   cursor->file.Y = cursor->poly[0].fileY;
  57.   cursor->points[0].x = cursor->points[active].x;
  58.   cursor->points[0].y = cursor->points[active].y;
  59.   cursor->points[1].x = cursor->points[0].x;
  60.   cursor->points[1].y = cursor->points[0].y;
  61.   cursor->rectangles[0].x = cursor->rectangles[active].x;
  62.   cursor->rectangles[0].y = cursor->rectangles[active].y;
  63.   /* set the counters */
  64.   cursor->point_cnt = 2;
  65.   cursor->poly_cnt = 1;
  66.   cursor->rectangle_cnt = 1;
  67.   cursor->ctrl.active_vertex = 0;
  68. }
  69.  
  70. /*
  71.  * Subroutine:    start_polygon
  72.  * Purpose:    Install polygon drawing stuff for the active cursor
  73.  */
  74. void start_polygon ( cursor, coord )
  75.      struct cursorRec *cursor;
  76.      struct coordRec *coord;
  77. {
  78.   void set_polygon_from_file_coords();
  79.   static void init_polygon();
  80.  
  81.   if( polysz == 0 )
  82.     init_polygon();
  83.   cursor->poly_cnt = 1;
  84.   cursor->rectangle_cnt = 1;
  85.   cursor->point_cnt = 2;
  86.   /* install the local fields */
  87.   cursor->poly = poly;
  88.   cursor->rectangles = rectangles;
  89.   /* update to cursor coords */
  90.   poly[0].fileX = cursor->file.X;
  91.   poly[0].fileY = cursor->file.Y;
  92.   set_polygon_from_file_coords (cursor, &coord->filetodisp, 1);
  93.   cursor->ctrl.active_vertex = 0;
  94. }
  95. static void init_polygon ( )
  96. {
  97.   int i;
  98.   char *calloc_errchk();
  99.   polysz = CURSOR_MAX;
  100.   poly = (PolyPoint *)
  101.     calloc_errchk(polysz, sizeof(PolyPoint), "poloygon records");
  102.   rectangles = (XRectangle *)
  103.     calloc_errchk(polysz, sizeof(XRectangle), "polygon hash marks");
  104.   for( i=0; i<polysz; i++ ) {
  105.     rectangles[i].width = HASH_SIDE;
  106.     rectangles[i].height = HASH_SIDE;
  107.   }
  108. }
  109.  
  110. /*
  111.  * Subroutine:    add_polygon_vertex
  112.  */
  113. void add_polygon_vertex ( cursor, index, x, y )
  114.      struct cursorRec *cursor;
  115.      int index;
  116.      int x, y;
  117. {
  118.   register XPoint *points;
  119.   register int i, j;
  120.  
  121.   if( (cursor->poly_cnt + 1) >= polysz ) {
  122.     (void)fprintf(stderr, "WARNING: vertex buffer full\n");
  123.     return;
  124.   }
  125.   /* shift the succeeding points up */
  126.   points = cursor->points;
  127.   i = cursor->poly_cnt;
  128.   /* line list has first point repeated at top */
  129.   points[i+1].x = points[i].x;
  130.   points[i+1].y = points[i].y;
  131.   for( j=i-1; j>=index; j-- ) {
  132.     points[i].x = points[j].x;
  133.     points[i].y = points[j].y;
  134.     rectangles[i].x = rectangles[j].x;
  135.     rectangles[i].y = rectangles[j].y;
  136.     if( !(poly[i].unset = poly[j].unset) ) {
  137.       poly[i].winX = poly[j].winX;
  138.       poly[i].winY = poly[j].winY;
  139.       poly[i].fileX = poly[j].fileX;
  140.       poly[i].fileY = poly[j].fileY;
  141.     }
  142.     i = j;
  143.   }
  144.   /* install new values (we never add base point) */
  145.   points[index].x = x;
  146.   points[index].y = y;
  147.   rectangles[index].x = x - HASH_RAY;
  148.   rectangles[index].y = y - HASH_RAY;
  149.   poly[index].unset = 1;
  150.   /* updates the counts */
  151.   cursor->point_cnt++;
  152.   cursor->poly_cnt++;
  153.   cursor->rectangle_cnt++;
  154.   cursor->ctrl.active_vertex = index;
  155. }
  156.  
  157. /*
  158.  * Subroutine:    delete_polygon_vertex
  159.  */
  160. void delete_polygon_vertex ( cursor, index )
  161.      struct cursorRec *cursor;
  162.      int index;
  163. {
  164.   register XPoint *points;
  165.   register int i, j;
  166.  
  167.   if( cursor->poly_cnt <= 1 )
  168.     return;
  169.   points = cursor->points;
  170.   i = index;
  171.   for( j=i+1; j<cursor->poly_cnt; j++ ) {
  172.     points[i].x = points[j].x;
  173.     points[i].y = points[j].y;
  174.     rectangles[i].x = rectangles[j].x;
  175.     rectangles[i].y = rectangles[j].y;
  176.     if( !(poly[i].unset = poly[j].unset) ) {
  177.       poly[i].winX = poly[j].winX;
  178.       poly[i].winY = poly[j].winY;
  179.       poly[i].fileX = poly[j].fileX;
  180.       poly[i].fileY = poly[j].fileY;
  181.     }
  182.     i = j;
  183.   }
  184.   /* the first point is repeated at top for drawing */
  185.   points[i].x = points->x;
  186.   points[i].y = points->y;
  187.   /* updates the counts */
  188.   cursor->point_cnt--;
  189.   cursor->poly_cnt--;
  190.   cursor->rectangle_cnt--;
  191.   /* active stays with its vertex, or if deleted, the preivous vertex */
  192.   if( cursor->ctrl.active_vertex >= index ) {
  193.     if( --(cursor->ctrl.active_vertex) < 0 )
  194.       cursor->ctrl.active_vertex = cursor->poly_cnt - 1;
  195.   }
  196. }
  197.  
  198. /*
  199.  * Subroutine:    set_polygon_hashmarks
  200.  */
  201. void set_polygon_hashmarks ( cursor )
  202.      struct cursorRec *cursor;
  203. {
  204.   XPoint *points;
  205.   int i;
  206.  
  207.   points = cursor->points;
  208.   for( i=0; i<cursor->poly_cnt; i++ ) {
  209.     rectangles[i].x = points[i].x - HASH_RAY;
  210.     rectangles[i].y = points[i].y - HASH_RAY;
  211.   }
  212. }
  213.  
  214. /*
  215.  * Subroutine:    set_active_polygon_hashmark
  216.  */
  217. void set_active_polygon_hashmark ( cursor )
  218.      struct cursorRec *cursor;
  219. {
  220.   int active;
  221.  
  222.   active = cursor->ctrl.active_vertex;
  223.   rectangles[active].x = cursor->points[active].x - HASH_RAY;
  224.   rectangles[active].y = cursor->points[active].y - HASH_RAY;
  225. }
  226.  
  227. /*
  228.  * Subroutine:    copy_polygon_region_to_cursor
  229.  * Purpose:    Install stuff to making a polygon copy the active cursor
  230.  * Note:    Must be called by copy_cursor()
  231.  */
  232. void copy_polygon_region_to_cursor ( region, cursor )
  233.      struct cursorRec *region;
  234.      struct cursorRec *cursor;
  235. {
  236.   XPoint *points;
  237.   int i;
  238.  
  239.   points = cursor->points;
  240.   cursor->rectangles = rectangles;
  241.   cursor->poly = poly;
  242.   cursor->rectangle_cnt = cursor->poly_cnt;
  243.   bcopy((char *)region->points, (char *)points,
  244.     region->point_cnt * sizeof(XPoint));
  245.   bcopy((char *)region->poly, (char *)poly,
  246.     region->poly_cnt * sizeof(PolyPoint));
  247.   for( i=0; i<cursor->poly_cnt; i++ ) {
  248.     rectangles[i].x = points[i].x - HASH_RAY;
  249.     rectangles[i].y = points[i].y - HASH_RAY;
  250.   }
  251. }
  252.