Example showing drawing using GINOSURF

/*      Program SURFEX4 */
/* ********************************************** */
/* Copyright Bradly Associates Limited            */
/*        GINOSURF  Version 6.0                   */
/* ********************************************** */

/*  filled contour map and cross-section          */
/*  using the data files gstd1.dat and rivers.dat */

/* ********************************************** */

#include <stdlib.h>
#include <stdio.h>

#include <gino-c.h>
#include <surf-c.h>

#define NUMX 60
#define NUMY 45
#define NCONT 13

#define MAX(x,y)	((x) > (y) ? (x) : (y))
#define MIN(x,y)	((x) < (y) ? (x) : (y))

/*  Declare variables */

GPOINT3 *data;
GPOINT  *river;
int     *line_style;
GWORKSPACE	work;
GDEVSTATE device_state;
GDIM	  paper;
GLIMIT	  picture2D = {0.0,1000.0,0.0,750.0};
GLIMIT3   picture3D = {0.0,1000.0,0.0,750.0,-5000.0,5000.0};
GLIMIT    viewport = {0.0,1.0,0.0,1.0},
          frame1  = {250.0,750.0,350.0,700.0},
          frame2  = {50.0,450.0,110.0,300.0},
          frame3  = {550.0,950.0,110.0,300.0},
          data_limit = {999.0,-999.0,999.0,-999.0},
          pframe1 = {10.0,990.0,50.0,740.0},
          pframe2 = {15.0,985.0,55.0,735.0};
GPOINT    point1,point2;
int       index1[NCONT+1];
GFILL	  index2[NCONT+1];
GFILL     base[3] = {GSOLID,9,  GSOLID,9,  GSOLID,9};
GSURF3D   state = {GON,GOFF};
GPRJAXI   style = {GAXISANDGRID,GAXISANDGRID,GAXISANDGRID,GAXISANDGRID,GAXISANDGRID,GAXISANDGRID};
GPRJAXI   sup   = {GHIGHEST,GHIGHEST,GHIGHEST,GHIGHEST,GHIGHEST,GNONE};

#if defined(MWIN) || defined(WOGL)
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
			 LPSTR lpszCmdParam, int nCmdShow)
#else
int main ()
#endif
{

      FILE      *fp;
      float     *gridz,
                rad = 0.0,
                the = 20.0,
                phi = 60.0,
                step = 1.0;
      int       i, j, ip, ndc, ndt, col, npts, nr;

/*  Read contour data from GSTD1.DAT */

      fp=fopen("GSTD1.DAT","r");
         fscanf(fp,"%d\n",&npts);
         data = (GPOINT3 *) malloc(npts * sizeof(GPOINT3));
         river = (GPOINT *) malloc(npts * sizeof(GPOINT));
         line_style = (int *) malloc(npts * sizeof(int));
         for (i=0; i<npts; i++) {
            fscanf(fp,"%f%f%f\n",&data[i].x,&data[i].y,&data[i].z);
            data_limit.xmin = MIN(data_limit.xmin,data[i].x);
            data_limit.xmax = MAX(data_limit.xmax,data[i].x);
            data_limit.ymin = MIN(data_limit.ymin,data[i].y);
            data_limit.ymax = MAX(data_limit.ymax,data[i].y);
         }
      fclose(fp);

/*  Add rivers to contour map */

      fp=fopen("RIVERS.DAT","r");
      nr=0;
      line_style[nr]=0;
      while ( fscanf(fp,"%f%f\n",&river[nr].x,&river[nr].y)  != EOF ) {
         if ((int) river[nr].x == 99 && (int) river[nr].y == 99) {
            line_style[nr]=0;
         } else {
            nr=nr+1;
            line_style[nr]=14;
         }
      }
      fclose(fp);

/*  Create array of pointers for random grid */

      gridz = (float *) malloc(NUMX * NUMY * sizeof(float));

/*  Enter GINO-C */

      gOpenGino();
/* Nominate the device */
#if defined(MWIN)
  gMwin(hInstance, hPrevInstance);
#elif defined(WOGL)
  gWogl(hInstance, hPrevInstance);
#elif defined(XWIN)
  gXwinw(1,0,0,0,0,0);
#elif defined(GLX)
  gGlxw(1,0,0,0,0,0);
#elif defined(VGA)
  gVga();
#else
  xxxxx();
#endif

      gEnqDrawingLimits(&paper,&ip);
      viewport.xmax=paper.xpap;
      viewport.ymax=paper.ypap;

/* Check for 3D device */

      gEnqDeviceState(&device_state);
      if(device_state.dddim == 2) 
         gSetViewport2D(&picture2D, &viewport);
      else
         gSetViewport3D(&picture3D, &viewport);

      gNewDrawing();
      gSetWindowMode(GON2D);
      gEnqColourInfo(&ndc,&ndt);
      gEnqLineColour(&col);
      if(col == 10) col=13;
      gSetCharSize(7.5,7.5);

/*  Set up colour table and fill fill_style */

      gDefineRGB(2,0.5,0.8,1.0);
      gDefineRGB(3,0.0,0.7,0.15);
      gDefineRGB(4,0.0,0.9,0.2);
      gDefineRGB(5,0.25,0.95,0.4);
      gDefineRGB(6,0.5,1.0,0.6);
      gDefineRGB(7,0.75,0.95,0.35);
      gDefineRGB(8,1.0,0.9,0.1);
      gDefineRGB(9,0.85,0.75,0.15);
      gDefineRGB(10,0.7,0.6,0.2);
      gDefineRGB(11,0.6,0.5,0.2);
      gDefineRGB(12,0.5,0.4,0.2);
      gDefineRGB(13,1.0,1.0,1.0);
      gDefineRGB(14,0.0,0.0,0.9);
      gDefineRGB(15,0.0,1.0,1.0);

/*  Allocate filling styles */

      for (i=0; i<NCONT+1; i++) {
         index1[i]=i+2;
         index2[i].fill=GSOLID;
         index2[i].line=i+2;
      }

/*  Use gsGenerateRandomGrid to give finer mesh */

      gsOpenSurf();
      if(device_state.dddim == 3) gsSetSurf3DState(&state);
      gsGenerateRandomGrid(npts,data,&data_limit,NUMX,NUMY,gridz,&work);
      gsSetContourLimits(-50.,550.0);
      gsSetContourDrawingSwitch(GON);
      gsSetSurfAxesAnnotation(GXAXIS,3,0,0);
      gsSetSurfAxesAnnotation(GYAXIS,3,0,0);

/*  Draw surface view with rivers */

      gsSetSurfaceFrame(&frame1);
      gsSetContourMapFrameType(GDEFAULT);
      gsSetHeightRatio(0.3);
      gsSetSurfaceAxesStyle(&style,&sup);
      gsSetSurfaceBaseStyle(GLEVELS,base);
      gsSetSurfaceAxesIntervals (step,step,50.0);
      gsAddSurfacePolylineHidden(nr,river,line_style,&work);
      if (ndc <= 1) {
        gsDrawLineStyleSurface(&data_limit, NUMX, NUMY, gridz, &rad, the, phi,
                                 NCONT, index1, GON, &work);
      } else {
        gsFillContourSurface3D(&data_limit, NUMX, NUMY, gridz, &rad, the, phi,
                                 NCONT, index2, GON, &work);
      }
      gsDrawSurfaceAxes(&work);

/* Switch off use of 3D device */

      state.hard=GOFF;
      if(device_state.dddim == 3) gsSetSurf3DState(&state);

/*  Draw contour map with 12 contours ranging from -50 to 550 */
/*  adjust annotation values and force to be integers */

      gsSetContourMapFrame(&frame2);
      gsSetContourMapFrameType(GDEFAULT);
      gsSetContourAnnotation(-1,0.0,2.0,0,0,0,0,0);
      gsSetSurfLabels(GXLIMITSSHOWN,"projection against x axis");
      gsSetContourMapLineStyles(col,12,12,0);
      gsSetContourMapFrameScale (step,step,50.0,GVARIABLE,GROUNDED);
      if (ndc <= 1) {
        gsDrawGriddedContourMap(&data_limit,NUMX,NUMY,gridz,
                                     NCONT,GSTRAIGHT,&work);
      } else {
        gsFillGriddedContourMap(&data_limit,NUMX,NUMY,gridz,
                                     NCONT,GSTRAIGHT,index2,GOFF,&work);
      }

/*  Add rivers to contour map */

      gSetLineColour(14);
      j=0;
      for (i=0; i<nr; i++) {
         if (line_style[i] == 0) {
            gsAddContourPolyline(i-j,&river[j]);
            j=i;
         }
      }
      gsAddContourPolyline(nr-j,&river[j]);

/*  Draw line of cross section on the contour map */

      gsTransformMapCoord(data_limit.xmin, data_limit.ymin, &point1);
      gsTransformMapCoord(data_limit.xmax, data_limit.ymax, &point2);
      gSetLineColour(col);
      gMoveTo2D(point1.x, point1.y);
      gDrawLineTo2D(point2.x, point2.y);

/*  Add grid over contour map */

      gsAddContourMapGrid(&data_limit,2.0*step,2.0*step);

/*  Draw cross section of surface */

      gsSetCrossSectionFrame(&frame3);
      gsDrawGriddedCrossSection(data_limit.xmin, data_limit.ymin,
                                data_limit.xmax, data_limit.ymax,&work);
/*  Annotate cross section */

      gMoveTo2D(550-60.0,110.0);
      gSetStrAngle(90.0);
      gDisplayStr("Metres above sea level");
      gSetStrAngle(0.0);

/*  Draw frame and title */

      gSetLineColour(15);
      gSetCharSize(15.0,15.0);
      gFillRect(GHOLLOW,GCURRENT,&pframe1);
      gFillRect(GHOLLOW,GCURRENT,&pframe2);
      gSetStrJustify(GCENTRE);
      gMoveTo2D(500.0,700.0);
      gDisplayStr("Various Views of GSTD1.DAT");
      gMoveTo2D(500.0,20.0);
      gDisplayStr("GINOSURF Example Program 4");

/*  Free up workspace */

      gsFreeWorkspace(&work);
      free(gridz);
      free(data);
      free(river);

/*  Close down GINOSURF, the device and GINO */

      gsCloseSurf();
      gSuspendDevice();
      gCloseGino();

      return 0;
}