home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / gmt_os2.zip / src / psclip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-13  |  8.3 KB  |  267 lines

  1. /*--------------------------------------------------------------------
  2.  *    The GMT-system:    @(#)psclip.c    2.20  3/13/95
  3.  *
  4.  *    Copyright (c) 1991-1995 by P. Wessel and W. H. F. Smith
  5.  *    See README file for copying and redistribution conditions.
  6.  *--------------------------------------------------------------------*/
  7. /*
  8.  * psclip reads one or many xy-files and draws polygons just like psxy
  9.  * with the exception that these polygons are set up to act as clip
  10.  * paths for subsequent plotting.  psclip uses the even-odd winding
  11.  * rule to decide what's inside and outside the path.
  12.  *
  13.  * Author:    Paul Wessel
  14.  * Date:    6-FEB-1991-1995
  15.  * Version:    2.0
  16.  * Limitation:    Total clip path cannot exceed PostScript max path length
  17.  * (~1350 on Laserwriters, memory dependent on SUNs).
  18.  *
  19.  */
  20.  
  21. #include "gmt.h"
  22.  
  23. double *xx, *yy;    /* Arrays holding the contour xy values */
  24.  
  25. main (argc, argv)
  26. int argc;
  27. char **argv; {
  28.     int i, n, fno, ix, iy, n_alloc, n_args, n_files = 0;
  29.     
  30.     BOOLEAN error = FALSE, nofile = TRUE, end_of_clip = FALSE, multi_segments = FALSE;
  31.     BOOLEAN invert = FALSE, done, first;
  32.     
  33.     char line[512], EOL_flag = '>', *more;
  34.     
  35.     double west, east, north, south, x0, y0, xy[2], new_z_level = 0.0;
  36.     
  37.     FILE *fp = NULL;
  38.     
  39.     west = east = north = south = 0.0;
  40.  
  41.     argc = gmt_begin (argc, argv);
  42.  
  43.     for (i = 1; i < argc; i++) {
  44.         if (argv[i][0] == '-') {
  45.             switch (argv[i][1]) {
  46.         
  47.                 /* Common parameters */
  48.             
  49.                 case 'B':
  50.                 case 'H':
  51.                 case 'J':
  52.                 case 'K':
  53.                 case 'O':
  54.                 case 'P':
  55.                 case 'R':
  56.                 case 'U':
  57.                 case 'V':
  58.                 case 'X':
  59.                 case 'x':
  60.                 case 'Y':
  61.                 case 'y':
  62.                 case 'c':
  63.                 case ':':
  64.                 case '\0':
  65.                     error += get_common_args (argv[i], &west, &east, &south, &north);
  66.                     break;
  67.                 
  68.                 /* Supplemental parameters */
  69.             
  70.                 case 'E':
  71.                     sscanf (&argv[i][2], "%lf/%lf",&z_project.view_azimuth, &z_project.view_elevation);
  72.                     break;
  73.                 case 'F':
  74.                     if (gmt_getrgb (&argv[i][2], &gmtdefs.basemap_frame_rgb[0], &gmtdefs.basemap_frame_rgb[1], &gmtdefs.basemap_frame_rgb[2])) {
  75.                         gmt_pen_syntax ('F');
  76.                         error++;
  77.                     }
  78.                     break;
  79.                 case 'N':
  80.                     invert = TRUE;
  81.                     break;
  82.                 case 'M':               /* Multiple line segments */
  83.                     multi_segments = TRUE;
  84.                     if (argv[i][2]) EOL_flag = argv[i][2];
  85.                     break;
  86.                 case 'S':
  87.                     end_of_clip = TRUE;
  88.                     break;
  89.                 case 'Z':
  90.                     new_z_level = atof (&argv[i][2]);
  91.                     break;
  92.                 default:
  93.                     error = TRUE;
  94.                     gmt_default_error (argv[i][1]);
  95.                     break;
  96.             }
  97.         }
  98.         else
  99.             n_files++;
  100.     }
  101.     
  102.     if (argc == 1 || gmt_quick) {
  103.         fprintf (stderr,"psclip %s - To set up polygonal clip paths\n\n", GMT_VERSION);
  104.         fprintf (stderr, "usage: psclip -S  OR\n");
  105.         fprintf (stderr, "       psclip <xy-files> -J<params> -R<west/east/south/north>\n");
  106.         fprintf (stderr, "    [-B<tickinfo>] [-Eaz/el] [-F<r/g/b] [-H] [-K] [-M<flag>] [-N] [-O] [-P]\n");
  107.         fprintf (stderr, "    [-U[label] [-V] [-X<x_shift>] [-Y<y_shift>] [-c<ncopies>] [-:]\n\n");
  108.         
  109.         if (gmt_quick) exit(-1);
  110.         
  111.         fprintf (stderr, "    <xy-files> is one or more polygon files.  If none, standard input is read\n");
  112.         explain_option ('j');
  113.         explain_option ('R');
  114.         fprintf (stderr, "\n\tOPTIONS:\n");
  115.         explain_option ('b');
  116.         fprintf (stderr, "      -E set azimuth and elevation of viewpoint for 3-D perspecive [180/90]\n");
  117.         fprintf (stderr, "      -F Set color used for Frame and anotation [%d/%d/%d]\n",
  118.             gmtdefs.basemap_frame_rgb[0], gmtdefs.basemap_frame_rgb[1], gmtdefs.basemap_frame_rgb[2]);
  119.         explain_option ('H');
  120.         explain_option ('K');
  121.         fprintf (stderr, "      -M Input files each consist of multiple segments separated by one record\n");
  122.         fprintf (stderr, "         whose first character is <flag> [>]\n");
  123.         fprintf (stderr, "    -N uses the outside of the polygons as clip paths\n");
  124.         explain_option ('O');
  125.         explain_option ('P');
  126.         fprintf (stderr, "    -S means stop existing clip-path.  No other options required\n");
  127.         explain_option ('U');
  128.         explain_option ('V');
  129.         explain_option ('X');
  130.         explain_option ('c');
  131.         explain_option (':');
  132.         explain_option ('.');
  133.         exit(-1);
  134.     }
  135.     
  136.     if (!end_of_clip) {
  137.         if (!project_info.region_supplied) {
  138.             fprintf (stderr, "%s: GMT SYNTAX ERROR:  Must specify -R option\n", gmt_program);
  139.             error++;
  140.         }
  141.         if (z_project.view_azimuth > 360.0 || z_project.view_elevation <= 0.0 || z_project.view_elevation > 90.0) {
  142.             fprintf (stderr, "%s: GMT SYNTAX ERROR -E option:  Enter azimuth in 0-360 range, elevation in 0-90 range\n", gmt_program);
  143.             error++;
  144.         }
  145.     }
  146.     
  147.     if (error) exit (-1);
  148.  
  149.     if (end_of_clip) gmtdefs.overlay = TRUE;    /* Force overlay mode */
  150.     
  151.     if (!project_info.x_off_supplied && gmtdefs.overlay) gmtdefs.x_origin = 0.0;
  152.     if (!project_info.y_off_supplied && gmtdefs.overlay) gmtdefs.y_origin = 0.0;
  153.  
  154.     ps_plotinit (CNULL, gmtdefs.overlay, gmtdefs.page_orientation, gmtdefs.x_origin, gmtdefs.y_origin,
  155.         gmtdefs.global_x_scale, gmtdefs.global_y_scale, gmtdefs.n_copies,
  156.         gmtdefs.dpi, gmtdefs.measure_unit, gmtdefs.paper_width, gmtdefs.page_rgb, gmt_epsinfo (argv[0]));
  157.     echo_command (argc, argv);
  158.     if (gmtdefs.unix_time) timestamp (argc, argv);
  159.  
  160.     if (end_of_clip && !frame_info.plot) {    /* Just undo previous clip-path, no basemap needed */
  161.         ps_clipoff ();
  162.         ps_plotend (gmtdefs.last_page);
  163.     
  164.         if (gmtdefs.verbose) fprintf (stderr, "psclip: Done!\n");
  165.     
  166.         gmt_end (argc, argv);
  167.     }
  168.  
  169.     map_setup (west, east, south, north);
  170.         
  171.     if (project_info.three_D) ps_transrotate (-z_project.xmin, -z_project.ymin, 0.0);
  172.  
  173.     if (new_z_level != 0.0) project_info.z_level = new_z_level;
  174.  
  175.     ix = (gmtdefs.xy_toggle);    iy = 1 - ix;
  176.  
  177.     if (!end_of_clip) {    /* Start new clip_path */
  178.     
  179.         if (frame_info.plot) {
  180.             ps_setpaint (gmtdefs.basemap_frame_rgb[0], gmtdefs.basemap_frame_rgb[1], gmtdefs.basemap_frame_rgb[2]);
  181.             map_basemap ();
  182.         }
  183.         
  184.         xx = (double *) memory (CNULL, GMT_CHUNK, sizeof (double), "psclip");
  185.         yy = (double *) memory (CNULL, GMT_CHUNK, sizeof (double), "psclip");
  186.         n_alloc = GMT_CHUNK;
  187.         
  188.         if (invert) map_clip_on (-1, -1, -1, 1);    /* Must clip map */
  189.         first = (invert) ? FALSE : TRUE;
  190.         done = FALSE;
  191.         n_args = (argc > 1) ? argc : 2;
  192.         if (n_files > 0) nofile = FALSE;
  193.         
  194.         for (fno = 1; !done && fno < n_args; fno++) {    /* Loop over all input files */
  195.             if (!nofile && argv[fno][0] == '-') continue;
  196.             if (nofile) {
  197.                 fp = stdin;
  198.                 done = TRUE;
  199.             }
  200.             else if ((fp = fopen (argv[fno], "r")) == NULL) {
  201.                 fprintf (stderr, "psclip: Cannot open file %s\n", argv[fno]);
  202.                 continue;
  203.             }
  204.  
  205.             if (!nofile && gmtdefs.verbose) {
  206.                 fprintf (stderr, "psclip: Working on file %s\n", argv[fno]);
  207.                 sprintf (line, "File: %s\0", argv[fno]);
  208.                 ps_comment (line);
  209.             }
  210.             if (gmtdefs.io_header) for (i = 0; i < gmtdefs.n_header_recs; i++) fgets (line, 512, fp);
  211.             if (multi_segments) {
  212.                 fgets (line, 512, fp);
  213.                 if (gmtdefs.verbose) ps_comment (line);
  214.             }
  215.             more = fgets (line, 512, fp);
  216.             n = 0;
  217.             while (more) {
  218.                 while ((more && !multi_segments) || (more && multi_segments && line[0] != EOL_flag)) {
  219.                     sscanf (line, "%lf %lf", &xy[ix], &xy[iy]);
  220.                     xx[n] = xy[0];    yy[n] = xy[1];
  221.                     n++;
  222.                     if (n == n_alloc) {
  223.                         n_alloc += GMT_CHUNK;
  224.                         xx = (double *) memory ((char *)xx, n_alloc, sizeof (double), "psclip");
  225.                         yy = (double *) memory ((char *)yy, n_alloc, sizeof (double), "psclip");
  226.                     }
  227.                     more = fgets (line, 512, fp);
  228.                 }
  229.         
  230.                 xx = (double *) memory ((char *)xx, n, sizeof (double), "psclip");
  231.                 yy = (double *) memory ((char *)yy, n, sizeof (double), "psclip");
  232.                 n_alloc = n;
  233.                 
  234.                 for (i = 0; i < n; i++) {
  235.                     geo_to_xy (xx[i], yy[i], &x0, &y0);
  236.                     xx[i] = x0; yy[i] = y0;
  237.                 }
  238.                 
  239.                 if (project_info.three_D) two_d_to_three_d (xx, yy, n);
  240.                 
  241.                 ps_clipon (xx, yy, n, -1, -1, -1, first);
  242.                 first = FALSE;
  243.                 
  244.                 if (more) more = fgets (line, 512, fp);
  245.             }
  246.         }
  247.         ps_clipon (xx, yy, 0, -1, -1, -1, 2);
  248.         if (fp != stdin) fclose (fp);
  249.         free ((char *)xx);
  250.         free ((char *)yy);
  251.     }
  252.  
  253.     else {    /* Undo previous clip-path and draw basemap */
  254.         ps_clipoff ();
  255.         ps_setpaint (gmtdefs.basemap_frame_rgb[0], gmtdefs.basemap_frame_rgb[1], gmtdefs.basemap_frame_rgb[2]);
  256.         map_basemap ();
  257.     }
  258.     
  259.     if (project_info.three_D) ps_rotatetrans (z_project.xmin, z_project.ymin, 0.0);
  260.  
  261.     ps_plotend (gmtdefs.last_page);
  262.     
  263.     if (gmtdefs.verbose) fprintf (stderr, "psclip: Done!\n");
  264.     
  265.     gmt_end (argc, argv);
  266. }
  267.