Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwinding of stack, inclusion of a rectangular bounds search, optimization for large numbers of equal points. #14

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ CFLAGS = -std=c89 -pedantic -Wall -g -I..
LDFLAGS = $(kdlib) -lm

.PHONY: all
all: test test2
all: test test2 test3

test: test.c $(kdlib)
test2: test2.c $(kdlib)
test3: test3.c $(kdlib)

.PHONY: clean
clean:
Expand Down
4 changes: 2 additions & 2 deletions examples/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ unsigned int get_msec(void)

int main(int argc, char **argv)
{
int i, vcount = 10;
int i, vcount = 10000;
void *kd, *set;
unsigned int msec, start;

Expand Down Expand Up @@ -52,7 +52,7 @@ int main(int argc, char **argv)
msec = get_msec() - start;
printf("range query returned %d items in %.5f sec\n", kd_res_size(set), (float)msec / 1000.0);
kd_res_free(set);

kd_clear(kd);
kd_free(kd);
return 0;
}
2 changes: 1 addition & 1 deletion examples/test2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <time.h>
#include "kdtree.h"

#define DEF_NUM_PTS 10
#define DEF_NUM_PTS 10000

/* returns the distance squared between two dims-dimensional double arrays */
static double dist_sq( double *a1, double *a2, int dims );
Expand Down
94 changes: 94 additions & 0 deletions examples/test3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*! gcc -std=c89 -pedantic -Wall -g -o test2 test2.c libkdtree.a -lm */
/* Extended test program, contributed by David Underhill */
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include "kdtree.h"

#define DEF_NUM_PTS 10000
#define DIM 3

/* returns the distance squared between two dims-dimensional double arrays */
static double dist_sq( double *a1, double *a2, int dims );

/* get a random double between -10 and 10 */
static double rd( void );

int main(int argc, char **argv) {
int i, num_pts = DEF_NUM_PTS;
void *ptree;
char *data, *pch;
struct kdres *presults;
double pos[3], dist, minpos[3], maxpos[3];
double pt[3] = { 0, 0, 1 };
double radius = 10;

if(argc > 1 && isdigit(argv[1][0])) {
num_pts = atoi(argv[1]);
}

if(!(data = malloc(num_pts))) {
perror("malloc failed");
return 1;
}

srand( time(0) );

/* create a k-d tree for 3-dimensional points */
ptree = kd_create( DIM );

/* add some random nodes to the tree (assert nodes are successfully inserted) */
for( i=0; i<num_pts; i++ ) {
data[i] = 'a' + i;
assert( 0 == kd_insert3( ptree, rd(), rd(), rd(), &data[i] ) );
}

for ( i = 0; i < DIM; i++)
{ minpos[i] = rd();
maxpos[i] = rd();
}

/* find points closest to the origin and within distance radius */
presults = kd_in_bounds( ptree, minpos, maxpos, 1);

/* print out all the points found in results */
printf( "found %d results:\n", kd_res_size(presults) );

while( !kd_res_end( presults ) ) {
/* get the data and position of the current result item */
pch = (char*)kd_res_item( presults, pos );

/* compute the distance of the current result from the pt */
dist = sqrt( dist_sq( pt, pos, 3 ) );

/* print out the retrieved data */
printf( "node at (%.3f, %.3f, %.3f) is %.3f away and has data=%c\n",
pos[0], pos[1], pos[2], dist, *pch );

/* go to the next entry */
kd_res_next( presults );
}

/* free our tree, results set, and other allocated memory */
free( data );
kd_res_free( presults );
kd_free( ptree );

return 0;
}

static double dist_sq( double *a1, double *a2, int dims ) {
double dist_sq = 0, diff;
while( --dims >= 0 ) {
diff = (a1[dims] - a2[dims]);
dist_sq += diff*diff;
}
return dist_sq;
}

static double rd( void ) {
return (double)rand()/RAND_MAX * 20.0 - 10.0;
}
Loading