Commit 6aa41f2d authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

kd_check_dist: changed type of q to size_t.

parent 332ce346
......@@ -11,6 +11,7 @@
changed *max to max[KD_MAX_DIM]
_compPoints: compare index if points[axis] are equal
20171102: renamed kd_buildArg() to kd_initArg(), changed interface and memory handling
changed data pointer to size_t
*/
#ifndef KDTREE_H_
#define KDTREE_H_
......@@ -152,7 +153,6 @@ void kd_printNode(struct kdNode *node);
void kd_printTree(struct kdNode *node);
/* Functions for building and destroying trees */
void kd_freeNode(kdNode * node);
struct kdNode *kd_allocNode(struct kd_point *points, size_t pivot,
kdata_t *min, kdata_t *max, int dim, int axis);
void kd_destroyTree(struct kdNode *node);
......
......@@ -325,73 +325,75 @@ kd_qnearest(struct kdNode *node, kdata_t *p,
* return 1 if okay, zero in case of problems
*/
// Uwe Schulzweida: extract kd_check_dist() from kd_doQnearest()
static int
static bool
kd_check_dist(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, unsigned int q, struct pqueue *res)
kdata_t *max_dist_sq, size_t q, struct pqueue *res)
{
kdata_t dist_sq = kd_dist_sq(node->location, p);
if ( dist_sq < *max_dist_sq && kd_isleaf(node) ) {
struct resItem *point = (struct resItem *) kd_malloc(sizeof(struct resItem), "kd_doQnearest: ");
if ( point == NULL) return 0;
point->node = node;
point->dist_sq = dist_sq;
pqinsert(res, point);
kdata_t dist_sq = kd_dist_sq(node->location, p);
if ( dist_sq < *max_dist_sq && kd_isleaf(node) )
{
struct resItem *point = (struct resItem *) kd_malloc(sizeof(struct resItem), "kd_doQnearest: ");
if ( point == NULL) return false;
point->node = node;
point->dist_sq = dist_sq;
pqinsert(res, point);
}
if ( res->size > q ) {
struct resItem *item;
pqremove_max(res, &item);
free(item);
if ( res->size > 1 ) {
/*
* Only inspect the queue if there are items left
*/
pqpeek_max(res, &item);
*max_dist_sq = item->dist_sq;
} else {
/*
* Nothing was found within the max search radius
*/
*max_dist_sq = 0;
if ( res->size > q )
{
struct resItem *item;
pqremove_max(res, &item);
free(item);
if ( res->size > 1 )
{
// Only inspect the queue if there are items left
pqpeek_max(res, &item);
*max_dist_sq = item->dist_sq;
}
else
{
// Nothing was found within the max search radius
*max_dist_sq = 0;
}
}
return 1;
return true;
}
int
kd_doQnearest(struct kdNode *node, kdata_t *p,
kdata_t *max_dist_sq, size_t q, int dim, struct pqueue *res)
{
if ( !node ) return 1;
if ( !node ) return 1;
if ( !kd_check_dist(node, p, max_dist_sq, q, res) ) return 0;
if ( !kd_check_dist(node, p, max_dist_sq, q, res) ) return 0;
struct kdNode *nearer, *further;
if ( p[node->split] < node->location[node->split] ) {
nearer = node->left;
further = node->right;
} else {
nearer = node->right;
further = node->left;
}
if ( !kd_doQnearest(nearer, p, max_dist_sq, q, dim, res) ) return 0;
struct kdNode *nearer, *further;
if ( p[node->split] < node->location[node->split] ) {
nearer = node->left;
further = node->right;
} else {
nearer = node->right;
further = node->left;
}
if ( !kd_doQnearest(nearer, p, max_dist_sq, q, dim, res) ) return 0;
if ( !further ) return 1;
if ( !further ) return 1;
kdata_t dx = kd_min(KDATA_ABS(p[node->split] - further->min[node->split]),
KDATA_ABS(p[node->split] - further->max[node->split]));
kdata_t dx = kd_min(KDATA_ABS(p[node->split] - further->min[node->split]),
KDATA_ABS(p[node->split] - further->max[node->split]));
if ( *max_dist_sq > kd_sqr(dx) ) {
/*
* some part of the further hyper-rectangle is in the search
* radius, search the further node
*/
if (!kd_doQnearest(further, p, max_dist_sq, q, dim, res)) return 0;
if ( *max_dist_sq > kd_sqr(dx) ) {
/*
* some part of the further hyper-rectangle is in the search
* radius, search the further node
*/
if (!kd_doQnearest(further, p, max_dist_sq, q, dim, res)) return 0;
if (!kd_check_dist(node, p, max_dist_sq, q, res)) return 0;
}
return 1;
if (!kd_check_dist(node, p, max_dist_sq, q, res)) return 0;
}
return 1;
}
......
......@@ -178,12 +178,10 @@ void *kd_doBuildTree(void *threadarg)
return (void *) node;
}
void
kd_freeNode(kdNode *node)
static
void kd_freeNode(kdNode *node)
{
if ( node ) free(node);
return;
}
......@@ -191,6 +189,7 @@ kd_freeNode(kdNode *node)
#ifdef TEST_BIGMEM
size_t num_nodes = 0;
struct kdNode *mem_pool = NULL;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
struct kdNode *
......@@ -200,8 +199,11 @@ kd_allocNode(struct kd_point *points, size_t pivot,
struct kdNode *node;
#ifdef TEST_BIGMEM
if ( num_nodes > 2*25920000 ) fprintf(stderr, "number of nodes exceeded!\n");
if ( mem_pool == NULL ) mem_pool = (kdNode *) malloc(2*25920000*sizeof(kdNode)); // 2*gridsize
pthread_mutex_lock(&mutex);
node = &mem_pool[num_nodes++];
pthread_mutex_unlock(&mutex);
#else
if ((node = (kdNode *)kd_malloc(sizeof(kdNode), "kd_allocNode (node): ")) == NULL)
return NULL;
......@@ -234,8 +236,8 @@ kd_destroyTree(struct kdNode *node)
#ifdef TEST_BIGMEM
if ( mem_pool )
{
free(mem_pool);
num_nodes = 0;
// free(mem_pool);
// num_nodes = 0;
}
#else
kd_destroyTree(node->left);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment