R3TEST.C

/****************************************************************************
    (c) 1984 - 2003 by Scientific Endeavors Corporation.
    All rights reserved.

    This program demonstrates the use of randomly spaced data points to
    draw a 3-D surface and a contour map. The points have been generated
    using the random number generator. z values are calculated from:
        z = sin(y) * cos(x - y)
    Values of x and y lie between 0 and PI and 0 and 2*PI respectively.
    The points are in the file CDATA.DAT, which holds a 2000-point set.
****************************************************************************/

#include <graphic.h>                           /* Include all needed files */

#if TCQ                                 /* Set stack for Borland (Turbo) C */
extern unsigned _stklen = 0x3000;
#endif

/****************************************************************************
    Main program
****************************************************************************/
void GPC_MAIN(void)
{
    float x3axis, y3axis, z3axis, xvu, yvu, zvu;
    FILE *file1;
    int i, labint, nx, ny, nxc, nyc;
    float *x = NULL, *y = NULL, *z = NULL;
    float factor, *x_in, *y_in, **z_in;
    float *smoothed_x, *smoothed_y, **smoothed_z;
    float xorig = 0.f, xstep = 1.f, xmax = (float)PI;
    float yorig = 0.f, ystep = 1.f, ymax = (float)TWO_PI;
    float zorig = -1.f, zstep = .5f, zmax = 1.f;

    nx = 61;
    ny = 81;
    x3axis = 1.f;
    y3axis = 2.f;
    z3axis = 1.f;
    xvu = -9.8f;               /* x distance to viewpoint in workbox units */
    yvu = -4.66f;              /* y distance to viewpoint in workbox units */
    zvu = 5.f;                 /* z distance to viewpoint in workbox units */

    x = (float *)gpcalloc((unsigned)300, sizeof(float));/* Allocate arrays */
    y = (float *)gpcalloc((unsigned)300, sizeof(float));
    z = (float *)gpcalloc((unsigned)300, sizeof(float));
    if (x == NULL || y == NULL || z == NULL){
        GPC_PUTS("R3TEST: Could not allocate data arrays");
        goto EndOfApp;
    }

    file1 = gfopen("cdata.dat", "r", 0);                /* Open data file */
    if(file1 == NULL){
        GPC_PUTS("R3TEST: Could not open data file");
        goto EndOfApp;
    }
    for (i = 0; i < 300; i++)                              /* Read in data */
        fscanf(file1, "%f %f %f\n", &x[i], &y[i], &z[i]);
    gfclose(file1);

    bgnplot(1, 'g', "xy_rand.tkf");              /* GraphiC initialization */
    startplot(GREEN);
    metricunits(0);                                 /* Plots are in inches */
    font(3, "simplex.fnt", '\310', "triplex.fnt", '\311',  /* Select fonts */
        "microb.fnt", '\312');

    page(9.0f, 6.884f);                      /* Size of page and plot area */
    area2d(7.6f, 5.5f);

    color(BLACK);                                     /* Black axis titles */
    x3name("\310X-axis");
    y3name("\310Y-axis");
    z3name("\310Z-axis");

    volm3d(x3axis, y3axis, z3axis);                  /* Input workbox size */
    vuabs(xvu, yvu, zvu);                               /* Input viewpoint */

    tickht(-.07f);                          /* 3-D axes with ticks outward */
    graf3d(xorig, xstep, xmax, yorig, ystep, ymax, zorig, zstep, zmax);

    survis("both");                   /* Both top and bottom surfaces seen */
    base(1, 0);                                 /* Will be drawn with base */
    tcolor(RED);                             /* Top of surface will be red */
    bcolor(CYAN);                        /* Bottom of surface will be cyan */
    surmatr(x, y, z, 300, xorig, xmax, nx, yorig, ymax, ny);

    color(YELLOW);                                    /* Yellow plot title */
    fntchg((Uchar)'\311');
    d3head("3-D Test Plot", 't');

    if(endplot()) {                                /* Terminate first plot */
        stopplot();
        goto EndOfApp;
    }
    startplot(YELLOW);                                /* Start second plot */
    rotate(1);                                          /* Plot is rotated */

    file1 = gfopen("cdata.dat", "r", 0);                /* Open data file */
    if(file1 == NULL){
        GPC_PUTS("R3TEST: Could not open data file\n");
        exit(0);
    }
    for (i = 0; i < 300; i++)                              /* Read in data */
        fscanf(file1, "%f %f %f\n", &x[i], &y[i], &z[i]);
    gfclose(file1);

    page(6.884f, 9.0f);                      /* Size of page and plot area */
    area2d(5.5f, 7.3f);

    color(BLACK);                            /* Black axis and plot titles */
    xname("\310x");
    yname("\310y");
    heading("\312sin(y)*cos(x-y)");

    color(RED);                                        /* Red box and axes */
    box();
    xstep = ystep = (float)PI_OVER_TWO;
    graf("%-4.4f", xorig, xstep, xmax, "%-4.4f", yorig, ystep, ymax);

    color(BLUE);                                      /* Blue contour plot */
    labint = 2;                                  /* Contour label interval */
    zstep=.1f;
    nxc = nx / 8;
    nyc = ny / 8;
    z_in = (float **)dim2(nyc, nxc, sizeof(float));
    InterpolateZ(x, y, z, 300, xorig, xmax, nxc,
        yorig, ymax, nyc, z_in);
    x_in = (float *)gpcalloc(nxc, sizeof(float));
    y_in = (float *)gpcalloc(nyc, sizeof(float));
    factor = (xmax - xorig) / (nxc - 1);
    for (i = 0; i < nxc; i++)
        x_in[i] = factor * i + xorig;
    factor = (ymax - yorig) / (nyc - 1);
    for (i = 0; i < nyc; i++)
        y_in[i] = factor * i + yorig;
    smoothed_x = (float *)gpcalloc(nx, sizeof(float));
    smoothed_y = (float *)gpcalloc(ny, sizeof(float));
    smoothed_z = (float **)dim2(ny, nx, sizeof(float));
    spline3_2(x_in, y_in, z_in, nxc, nyc, nx, ny,
        smoothed_x, smoothed_y, smoothed_z);
    conmat2(smoothed_z, smoothed_x, nx, smoothed_y, ny,
        zorig, zstep, zmax, 0L, labint);
    gpcfree((void **)&x_in);
    gpcfree((void **)&y_in);
    free2((void ***)&z_in);
    gpcfree((void **)&smoothed_x);
    gpcfree((void **)&smoothed_y);
    free2((void ***)&smoothed_z);

    endplot();                                           /* Terminate plot */
    stopplot();

EndOfApp:
    gpcfree((void **)&x);                              /* Free data arrays */
    gpcfree((void **)&y);
    gpcfree((void **)&z);
}