コード例 #1
0
int main(int argc, char* argv[])
{
    int nin = NPOINTSIN;
    int nx = NX;
    int nout = 0;
    point* pin = NULL;
    delaunay* d = NULL;
    point* pout = NULL;
    nnhpi* nn = NULL;
    int cpi = -1;               /* control point index */
    struct timeval tv0, tv1;
    struct timezone tz;
    int i;

    i = 1;
    while (i < argc) {
        switch (argv[i][1]) {
        case 'a':
            i++;
            nn_rule = NON_SIBSONIAN;
            break;
        case 'n':
            i++;
            if (i >= argc)
                nn_quit("no number of data points found after -n\n");
            nin = atoi(argv[i]);
            i++;
            if (i >= argc)
                nn_quit("no number of ouput points per side found after -i\n");
            nx = atoi(argv[i]);
            i++;
            break;
        case 'v':
            i++;
            nn_verbose = 1;
            break;
        case 'V':
            i++;
            nn_verbose = 2;
            break;
        default:
            usage();
            break;
        }
    }

    if (nin < NMIN)
        nin = NMIN;
    if (nx < NXMIN)
        nx = NXMIN;

    printf("\nTest of Natural Neighbours hashing point interpolator:\n\n");
    printf("  %d data points\n", nin);
    printf("  %d output points\n", nx * nx);

    /*
     * generate data 
     */
    printf("  generating data:\n");
    fflush(stdout);
    pin = malloc(nin * sizeof(point));
    for (i = 0; i < nin; ++i) {
        point* p = &pin[i];

        p->x = (double) random() / RAND_MAX;
        p->y = (double) random() / RAND_MAX;
        p->z = franke(p->x, p->y);
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    /*
     * triangulate
     */
    printf("  triangulating:\n");
    fflush(stdout);
    d = delaunay_build(nin, pin, 0, NULL, 0, NULL);

    /*
     * generate output points 
     */
    points_generate(-0.1, 1.1, -0.1, 1.1, nx, nx, &nout, &pout);
    cpi = (nx / 2) * (nx + 1);

    gettimeofday(&tv0, &tz);

    /*
     * create interpolator 
     */
    printf("  creating interpolator:\n");
    fflush(stdout);
    nn = nnhpi_create(d, nout);

    fflush(stdout);
    gettimeofday(&tv1, &tz);
    {
        long dt = 1000000 * (tv1.tv_sec - tv0.tv_sec) + tv1.tv_usec - tv0.tv_usec;

        printf("    interpolator creation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    /*
     * interpolate 
     */
    printf("  interpolating:\n");
    fflush(stdout);
    gettimeofday(&tv1, &tz);
    for (i = 0; i < nout; ++i) {
        point* p = &pout[i];

        nnhpi_interpolate(nn, p);
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    fflush(stdout);
    gettimeofday(&tv0, &tz);
    {
        long dt = 1000000.0 * (tv0.tv_sec - tv1.tv_sec) + tv0.tv_usec - tv1.tv_usec;

        printf("    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", pout[cpi].x, pout[cpi].y, pout[cpi].z, franke(pout[cpi].x, pout[cpi].y));

    printf("  interpolating one more time:\n");
    fflush(stdout);
    gettimeofday(&tv0, &tz);
    for (i = 0; i < nout; ++i) {
        point* p = &pout[i];

        nnhpi_interpolate(nn, p);
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    fflush(stdout);
    gettimeofday(&tv1, &tz);
    {
        long dt = 1000000.0 * (tv1.tv_sec - tv0.tv_sec) + tv1.tv_usec - tv0.tv_usec;

        printf("    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", pout[cpi].x, pout[cpi].y, pout[cpi].z, franke(pout[cpi].x, pout[cpi].y));

    printf("  entering new data:\n");
    fflush(stdout);
    for (i = 0; i < nin; ++i) {
        point* p = &pin[i];

        p->z = p->x * p->x - p->y * p->y;
        nnhpi_modify_data(nn, p);
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    printf("  interpolating:\n");
    fflush(stdout);
    gettimeofday(&tv1, &tz);
    for (i = 0; i < nout; ++i) {
        point* p = &pout[i];

        nnhpi_interpolate(nn, p);
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    fflush(stdout);
    gettimeofday(&tv0, &tz);
    {
        long dt = 1000000.0 * (tv0.tv_sec - tv1.tv_sec) + tv0.tv_usec - tv1.tv_usec;

        printf("    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", pout[cpi].x, pout[cpi].y, pout[cpi].z, pout[cpi].x * pout[cpi].x - pout[cpi].y * pout[cpi].y);

    printf("  restoring data:\n");
    fflush(stdout);
    for (i = 0; i < nin; ++i) {
        point* p = &pin[i];

        p->z = franke(p->x, p->y);
        nnhpi_modify_data(nn, p);
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    printf("  interpolating:\n");
    fflush(stdout);
    gettimeofday(&tv0, &tz);
    for (i = 0; i < nout; ++i) {
        point* p = &pout[i];

        nnhpi_interpolate(nn, p);
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    fflush(stdout);
    gettimeofday(&tv1, &tz);
    {
        long dt = 1000000.0 * (tv1.tv_sec - tv0.tv_sec) + tv1.tv_usec - tv0.tv_usec;

        printf("    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", pout[cpi].x, pout[cpi].y, pout[cpi].z, franke(pout[cpi].x, pout[cpi].y));

    printf("  hashtable stats:\n");
    fflush(stdout);
    {
        hashtable* ht = nn->ht_data;

        printf("    input points: %d entries, %d table elements, %d filled elements\n", ht_getnentries(ht), ht_getsize(ht), ht_getnfilled(ht));
        ht = nn->ht_weights;
        printf("    weights: %d entries, %d table elements, %d filled elements\n", ht_getnentries(ht), ht_getsize(ht), ht_getnfilled(ht));
    }
    printf("\n");

    nnhpi_destroy(nn);
    free(pout);
    delaunay_destroy(d);
    free(pin);

    return 0;
}
コード例 #2
0
ファイル: rectangle.c プロジェクト: johannesgerer/jburkardt-c
int main ( )

/******************************************************************************/
/*
  Purpose:

    MAIN is the main program for RECTANGLE.

  Discussion:

    This driver computes the interpolation of the Franke function
    on the rectangle R(A,B) = [A1,B1] x [A2,B2] with A=(A1,A2)=(0,0) 
    and B=(B1,B2)=(1,1) (unit square) at the FAMILY = 1 of Padua points. 

    The degree of interpolation is DEG = 60 and the number of target 
    points is NTG = NTG1^2, NTG1 = 100. 

    The maps from the reference square [-1,1]^2 to the rectangle
    are SIGMA1 and SIGMA2 with inverses ISIGM1 and ISIGM2.

  Licensing:

    This code is distributed under the GNU LGPL license.
  
  Modified:

    16 February 2014

  Author:

    Original FORTRAN77 version by Marco Caliari, Stefano De Marchi, 
    Marco Vianello.
    This C version by John Burkardt.

  Reference:

    Marco Caliari, Stefano de Marchi, Marco Vianello,
    Algorithm 886:
    Padua2D: Lagrange Interpolation at Padua Points on Bivariate Domains,
    ACM Transactions on Mathematical Software,
    Volume 35, Number 3, October 2008, Article 21, 11 pages.

  Parameters:

    Local, int DEGMAX, the maximum degree of interpolation.

    Local, int NPDMAX, the maximum number of Padua points
    = (DEGMAX + 1) * (DEGMAX + 2) / 2.

    Local, int NTG1MX, the maximum value of the parameter determining 
    the number of target points.

    Local, int NTGMAX, the maximum number of target points,
    dependent on NTG1MX.

    Local, int DEG, the degree of interpolation,

    Local, int NTG1, the parameter determining the number 
    of target points.

    Local, int FAMILY, specifies the desired family of Padua points.

    Local, int NPD, the number of Padua points = (DEG + 1) * (DEG + 2) / 2.

    Local, int NTG, the number of target points, dependent on NTG1.

    Local, double PD1[NPDMAX], the first coordinates of 
    the Padua points.

    Local, double PD2[NPDMAX], the second coordinates of the 
    Padua points.

    Local, double WPD[NPDMAX], the weights.

    Local, double FPD[NPDMAX], the function at the Padua points.

    Workspace, double RAUX1[(DEGMAX+1)*(DEGMAX+2)].

    Workspace, double RAUX2[(DEGMAX+1)*(DEGMAX+2)].

    Local, double C0[(0:DEGMAX+1)*(0:DEGMAX+1)], the coefficient matrix.

    Local, double TG1[NTGMAX], the first coordinates of the 
    target points.

    Local, double TG2[NTGMAX], the second coordinates of the 
    target points.

    Local, double INTFTG[NTGMAX], the values of the 
    interpolated function.

    Local, double MAXERR, the maximum norm of the error at target 
    points.

    Local, double ESTERR, the estimated error.
*/
{
# define DEGMAX 60
# define NTG1MX 100
# define NPDMAX ( ( DEGMAX + 1 ) * ( DEGMAX + 2 ) / 2 )
# define NTGMAX ( NTG1MX * NTG1MX )

  double a1;
  double a2;
  double b1;
  double b2;
  double c0[(DEGMAX+2)*(DEGMAX+2)];
  int deg;
  int degmax = DEGMAX;
  double esterr;
  int family;
  char filename[255];
  double fmax;
  double fmin;
  double fpd[NPDMAX];
  double fxy;
  int i;
  double intftg[NTGMAX];
  double ixy;
  double maxdev;
  double maxerr;
  double mean;
  int npd;
  int npdmax = NPDMAX;
  int ntg;
  int ntg1;
  int ntg1mx = NTG1MX;
  int ntgmax = NTGMAX;
  FILE *output;
  double pd1[NPDMAX];
  double pd2[NPDMAX];
  double raux1[(DEGMAX+1)*(DEGMAX+2)];
  double raux2[(DEGMAX+1)*(DEGMAX+2)];
  double tg1[NTGMAX];
  double tg2[NTGMAX];
  double wpd[NPDMAX];
  double x;
  double y;

  a1 = 0.0;
  a2 = 0.0;
  b1 = 1.0;
  b2 = 1.0;
  family = 1;
  deg = 60;
  ntg1 = 100;
 
  timestamp ( );
  printf ( "\n" );
  printf ( "RECTANGLE:\n" );
  printf ( "  C version\n" );
  printf ( "  Interpolation of the Franke function\n" );
  printf ( "  on the unit square [0,1] x [0,1]\n" );
  printf ( "  of degree = %d\n", deg );

  if ( degmax < deg )
  {
    fprintf ( stderr, "\n" );
    fprintf ( stderr, "RECTANGLE - Fatal error!\n" );
    fprintf ( stderr, "  DEGMAX < DEG.\n" );
    fprintf ( stderr, "  DEG =    %d\n", deg );
    fprintf ( stderr, "  DEGMAX = %d\n", degmax );
    exit ( 1 );
  }
/*
  Build the first family of Padua points in the square [-1,1]^2
*/     
  pdpts ( deg, pd1, pd2, wpd, &npd );
/*    
  Compute the Franke function at Padua points mapped to the region.
*/
  for ( i = 0; i < npd; i++ )
  {
    x = sigma1 ( pd1[i], pd2[i], a1, a2, b1, b2, family, deg );
    y = sigma2 ( pd1[i], pd2[i], a1, a2, b1, b2, family, deg );
    fpd[i] = franke ( x, y );
  }
/*
  Write X, Y, F(X,Y) to a file.
*/
  strcpy ( filename, "rectangle_fpd.txt" );
  output = fopen ( filename, "wt" );
  for ( i = 0; i < npd; i++ )
  {
    x = sigma1 ( pd1[i], pd2[i], a1, a2, b1, b2, family, deg );
    y = sigma2 ( pd1[i], pd2[i], a1, a2, b1, b2, family, deg );
    fprintf ( output, "%g  %g  %g\n", x, y, fpd[i] );
  }
  fclose ( output );
  printf ( "\n" );
  printf ( "  Wrote F(x,y) at Padua points in '%s'\n", filename ); 
/*     
  Compute the matrix C0 of the coefficients in the bivariate
  orthonormal Chebyshev basis
*/     
  padua2 ( deg, degmax, npd, wpd, fpd, raux1, raux2, c0, &esterr );
/*    
  Evaluate the target points in the region.
*/     
  target ( a1, b1, a2, b2, ntg1, ntgmax, tg1, tg2, &ntg );
/*    
  Evaluate the interpolant at the target points.
*/ 
  for ( i = 0; i < ntg; i++ )
  {
    x = isigm1 ( tg1[i], tg2[i], a1, a2, b1, b2, family, deg );
    y = isigm2 ( tg1[i], tg2[i], a1, a2, b1, b2, family, deg );
    intftg[i] = pd2val ( deg, degmax, c0, x, y );
  }
/*
  Write the function value at target points to a file.
*/
  strcpy ( filename, "rectangle_ftg.txt" );
  output = fopen ( filename, "wt" );
  for ( i = 0; i < ntg; i++ )
  {
    fprintf ( output, "%g  %g  %g\n", 
      tg1[i], tg2[i], franke ( tg1[i], tg2[i] ) );
  }
  fclose ( output );
  printf ( "  Wrote F(x,y) at target points in '%s'\n", filename );
/*
  Write the interpolated function value at target points to a file.
*/
  strcpy ( filename, "ellipse_itg.txt" );
  output = fopen ( filename, "wt" );
  for ( i = 0; i < ntg; i++ )
  {
    fprintf ( output, "%g  %g  %g\n", tg1[i], tg2[i], intftg[i] );
  }
  fclose ( output );
  printf ( "  Wrote I(F)(x,y) at target points in '%s'\n", filename );
/*
  Compute the error relative to the max deviation from the mean.
*/    
  maxerr = 0.0;
  mean = 0.0;
  fmax = - r8_huge ( );
  fmin = + r8_huge ( );

  for ( i = 0; i < ntg; i++ )
  {
    fxy = franke ( tg1[i], tg2[i] );
    ixy = intftg[i];
    maxerr = r8_max ( maxerr, fabs ( fxy - ixy ) );
    mean = mean + fxy;
    fmax = r8_max ( fmax, fxy );
    fmin = r8_min ( fmin, fxy );
  }
 
  if ( fmax == fmin )
  {
    maxdev = 1.0;
  }
  else
  {
    mean = mean / ( double ) ( ntg );
    maxdev = r8_max ( fmax - mean, mean - fmin );
  }
/*
  Print error ratios.
*/
  printf ( "\n" );
  printf ( "  Estimated error:  %g\n", esterr / maxdev );
  printf ( "  Actual error:     %g\n", maxerr / maxdev );
  printf ( "  Expected error:   %g\n", 0.2468E-10 );
/*
  Terminate.
*/
  printf ( "\n" );
  printf ( "RECTANGLE:\n" );
  printf ( "  Normal end of execution.\n" );
  printf ( "\n" );
  timestamp ( );

  return 0;

# undef DEGMAX
# undef NTG1MX
# undef NPDMAX
# undef NTGMAX
}
コード例 #3
0
ファイル: nnai.c プロジェクト: Fooway/SAGA-GIS-git-mirror
int main(int argc, char* argv[])
{
    int nin = NPOINTSIN;
    int nx = NX;
    int nout = 0;
    point* pin = NULL;
    delaunay* d = NULL;
    point* pout = NULL;
    nnai* nn = NULL;
    double* zin = NULL;
    double* xout = NULL;
    double* yout = NULL;
    double* zout = NULL;
    int cpi = -1;               /* control point index */
    struct timeval tv0, tv1, tv2;
    struct timezone tz;
    int i;

    i = 1;
    while (i < argc) {
        switch (argv[i][1]) {
        case 'a':
            i++;
            nn_rule = NON_SIBSONIAN;
            break;
        case 'n':
            i++;
            if (i >= argc)
                nn_quit("no number of data points found after -i\n");
            nin = atoi(argv[i]);
            i++;
            if (i >= argc)
                nn_quit("no number of ouput points per side found after -i\n");
            nx = atoi(argv[i]);
            i++;
            break;
        case 'v':
            i++;
            nn_verbose = 1;
            break;
        case 'V':
            i++;
            nn_verbose = 2;
            break;
        default:
            usage();
            break;
        }
    }

    if (nin < NMIN)
        nin = NMIN;
    if (nx < NXMIN)
        nx = NXMIN;

    printf("\nTest of Natural Neighbours array interpolator:\n\n");
    printf("  %d data points\n", nin);
    printf("  %d output points\n", nx * nx);

    /*
     * generate data 
     */
    printf("  generating data:\n");
    fflush(stdout);
    pin = (point *)malloc(nin * sizeof(point));
    zin = (double *)malloc(nin * sizeof(double));
    for (i = 0; i < nin; ++i) {
        point* p = &pin[i];

        p->x = (double) random() / RAND_MAX;
        p->y = (double) random() / RAND_MAX;
        p->z = franke(p->x, p->y);
        zin[i] = p->z;
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    /*
     * triangulate
     */
    printf("  triangulating:\n");
    fflush(stdout);
    d = delaunay_build(nin, pin, 0, NULL, 0, NULL);

    /*
     * generate output points 
     */
    points_generate2(-0.1, 1.1, -0.1, 1.1, nx, nx, &nout, &pout);
    xout = (double *)malloc(nout * sizeof(double));
    yout = (double *)malloc(nout * sizeof(double));
    zout = (double *)malloc(nout * sizeof(double));
    for (i = 0; i < nout; ++i) {
        point* p = &pout[i];

        xout[i] = p->x;
        yout[i] = p->y;
        zout[i] = NaN;
    }
    cpi = (nx / 2) * (nx + 1);

    gettimeofday(&tv0, &tz);

    /*
     * create interpolator 
     */
    printf("  creating interpolator:\n");
    fflush(stdout);
    nn = nnai_build(d, nout, xout, yout);

    fflush(stdout);
    gettimeofday(&tv1, &tz);
    {
        long dt = 1000000 * (tv1.tv_sec - tv0.tv_sec) + tv1.tv_usec - tv0.tv_usec;

        printf("    interpolator creation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    /*
     * interpolate 
     */
    printf("  interpolating:\n");
    fflush(stdout);
    nnai_interpolate(nn, zin, zout);
    if (nn_verbose)
        for (i = 0; i < nout; ++i)
            printf("    (%f, %f, %f)\n", xout[i], yout[i], zout[i]);

    fflush(stdout);
    gettimeofday(&tv2, &tz);
    {
        long dt = 1000000.0 * (tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec;

        printf("    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke(xout[cpi], yout[cpi]));

    printf("  interpolating one more time:\n");
    fflush(stdout);
    nnai_interpolate(nn, zin, zout);
    if (nn_verbose)
        for (i = 0; i < nout; ++i)
            printf("    (%f, %f, %f)\n", xout[i], yout[i], zout[i]);

    fflush(stdout);
    gettimeofday(&tv0, &tz);
    {
        long dt = 1000000.0 * (tv0.tv_sec - tv2.tv_sec) + tv0.tv_usec - tv2.tv_usec;

        printf("    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout);
    }

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke(xout[cpi], yout[cpi]));

    printf("  entering new data:\n");
    fflush(stdout);
    for (i = 0; i < nin; ++i) {
        point* p = &pin[i];

        p->z = p->x * p->x - p->y * p->y;
        zin[i] = p->z;
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    printf("  interpolating:\n");
    fflush(stdout);
    nnai_interpolate(nn, zin, zout);
    if (nn_verbose)
        for (i = 0; i < nout; ++i)
            printf("    (%f, %f, %f)\n", xout[i], yout[i], zout[i]);

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], xout[cpi] * xout[cpi] - yout[cpi] * yout[cpi]);

    printf("  restoring data:\n");
    fflush(stdout);
    for (i = 0; i < nin; ++i) {
        point* p = &pin[i];

        p->z = franke(p->x, p->y);
        zin[i] = p->z;
        if (nn_verbose)
            printf("    (%f, %f, %f)\n", p->x, p->y, p->z);
    }

    printf("  interpolating:\n");
    fflush(stdout);
    nnai_interpolate(nn, zin, zout);
    if (nn_verbose)
        for (i = 0; i < nout; ++i)
            printf("    (%f, %f, %f)\n", xout[i], yout[i], zout[i]);

    if (!nn_verbose)
        printf("    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke(xout[cpi], yout[cpi]));

    printf("\n");

    nnai_destroy(nn);
    free(zin);
    free(xout);
    free(yout);
    free(zout);
    free(pout);
    delaunay_destroy(d);
    free(pin);

    return 0;
}