kdtree备份
这个很好用。
例子:
/*! gcc -Wall -g -o test test.c libkdtree.a */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <assert.h> #include <time.h> #include "kdtree.h" int main(int argc, char **argv) { long i, vcount = 5000; void *kd, *set; unsigned int msec, start; if (argc > 1 && isdigit(argv[1][0])) { vcount = atoi(argv[1]); } printf("inserting %d random vectors... ", vcount); fflush(stdout); kd = kd_create(3); start = clock(); for (i = 0; i<vcount; i++) { float x, y, z; x = ((float)rand() / RAND_MAX) * 200.0 - 100.0; y = ((float)rand() / RAND_MAX) * 200.0 - 100.0; z = ((float)rand() / RAND_MAX) * 200.0 - 100.0; assert(kd_insert3((kdtree*)kd, x, y, z, 0) == 0); } msec = clock() - start; printf("%.3f sec\n", (float)msec); start = clock(); //set = kd_nearest_range3((kdtree*)kd, 100, 100, 100, 30); set=kd_nearest3((kdtree*)kd, 100,100,100); msec = clock() - start; printf("range query returned %d items in %.5f sec\n", kd_res_size((kdres*)set), (float)msec); double pos[3]; while (!kd_res_end((kdres*)set)) { /* get the data and position of the current result item */ kd_res_item((kdres*)set, pos); /* print out the retrieved data */ printf("node at (%.3f, %.3f, %.3f)\n", pos[0], pos[1], pos[2]); /* go to the next entry */ kd_res_next((kdres*)set); } kd_res_free((kdres*)set); kd_free((kdtree*)kd); getchar(); return 0; }