static void kd_destroy(kd_node_t *node) { if (node) { kd_destroy(node->left); kd_destroy(node->right); free(node); } }
void kdf_destroy(kd_forest_t *kdf) { for (unsigned int i = 0; i < kdf->roots_size; ++i) { kd_destroy(kdf->roots[i]); } free(kdf->roots); }
void gridkmap_destroy(gridkmap* gm) { kd_destroy(gm->tree); free(gm); }
int main(int argc, char* argv[]) { char* fname = NULL; int ndim = -1; double coords[NDIMMAX]; double range = NaN; int dosort = 0; int dorandomise = 1; kdtree* tree = NULL; kdset* set = NULL; double dist; size_t id, id_orig; int i; parse_commandline(argc, argv, &fname, &ndim, coords, &range, &dosort, &dorandomise); tree = kd_readfile(fname, ndim, dorandomise); if (kd_getsize(tree) == 0) { printf(" nothing to do, exiting\n"); return 0; } if (isll) { double lon = coords[0] * INVRAD; double lat = coords[1] * INVRAD; coords[0] = REARTH * sin(lon) * cos(lat); coords[1] = REARTH * cos(lon) * cos(lat); coords[2] = REARTH * sin(lat); } /* * first find the nearest node */ printf(" searching for the nearest node:"); fflush(stdout); id = kd_findnearestnode(tree, coords); id_orig = kd_getnodeorigid(tree, id); printf("\n"); printf(" X Y ID DIST\n"); { double* rescoords = kd_getnodecoords(tree, id); double rescoordsout[NDIMMAX]; if (isll) { double lat = asin(rescoords[2] / REARTH); double lon = asin(rescoords[0] / REARTH / cos(lat)); dist = sqrt((rescoords[0] - coords[0]) * (rescoords[0] - coords[0]) + (rescoords[1] - coords[1]) * (rescoords[1] - coords[1]) + (rescoords[2] - coords[2]) * (rescoords[2] - coords[2])); rescoordsout[0] = lon / INVRAD; rescoordsout[1] = lat / INVRAD; } else { for (i = 0; i < ndim; ++i) dist += (rescoords[i] - coords[i]) * (rescoords[i] - coords[i]); for (i = 0; i < ndim; ++i) rescoordsout[i] = rescoords[i]; dist = sqrt(dist); } printf(" "); for (i = 0; i < ndim; ++i) printf("%8.3f ", rescoordsout[i]); printf("%8zu ", id_orig); printf("%8g\n", dist); } fflush(stdout); /* * first nodes within range */ printf(" searching for the nodes within range:"); fflush(stdout); set = kd_findnodeswithinrange(tree, coords, range, dosort); printf("\n"); printf(" %zu points found:\n", kdset_getsize(set)); if (kdset_getsize(set) > 0) printf(" X Y ID DIST\n"); for (; (id = kdset_read(set, &dist)) < SIZE_MAX;) { double* rescoords = kd_getnodecoords(tree, id); id_orig = kd_getnodeorigid(tree, id); if (isll) { double lat = asin(rescoords[2] / REARTH); double lon = asin(rescoords[0] / REARTH / cos(lat)); rescoords[0] = lon / INVRAD; rescoords[1] = lat / INVRAD; } printf(" "); for (i = 0; i < ndim; ++i) printf("%8.3f ", rescoords[i]); printf("%8zu ", id_orig); printf("%8g\n", dist); } kdset_free(set); kd_destroy(tree); return 0; }