/* A simpler version of nnbathy that allocates the whole output grid in memory */ int main(int argc, char* argv[]) { specs* s = specs_create(); int nin = 0; point* pin = NULL; minell* me = NULL; int nout = 0; point* pout = NULL; double k = NaN; parse_commandline(argc, argv, s); if (s->fin == NULL) quit("no input data\n"); if (!s->generate_points && s->fout == NULL && !s->nointerp) quit("no output grid specified\n"); points_read(s->fin, 3, &nin, &pin); if (nin < 3) return 0; if (s->thin == 1) points_thingrid(&nin, &pin, s->nxd, s->nyd); else if (s->thin == 2) points_thinlin(&nin, &pin, s->rmax); if (s->nointerp) { points_write(nin, pin); specs_destroy(s); free(pin); return 0; } if (s->generate_points) { /* * points_getrange() only writes the proper values to those arguments * which do not point to NaNs */ points_getrange(nin, pin, s->zoom, &s->xmin, &s->xmax, &s->ymin, &s->ymax); points_generate(s->xmin, s->xmax, s->ymin, s->ymax, s->nx, s->ny, &nout, &pout); } else points_read(s->fout, 2, &nout, &pout); if (s->invariant) { me = minell_build(nin, pin); minell_scalepoints(me, nin, pin); minell_scalepoints(me, nout, pout); } else if (s->square) { k = points_scaletosquare(nin, pin); points_scale(nout, pout, k); } if (s->linear) lpi_interpolate_points(nin, pin, nout, pout); else nnpi_interpolate_points(nin, pin, s->wmin, nout, pout); if (s->invariant) minell_rescalepoints(me, nout, pout); else if (s->square) points_scale(nout, pout, 1.0 / k); points_write(nout, pout); if (me != NULL) minell_destroy(me); specs_destroy(s); free(pin); free(pout); return 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; }