Skip to content

Commit

Permalink
Reduce number of new / delete operations for class KDTreeSearch
Browse files Browse the repository at this point in the history
Add also several TODO comments because it is not clear why expensive
FLOAT64 calculations are used instead of cheaper FLOAT32 ones.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed May 19, 2017
1 parent 95bf30d commit 562de89
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions classify/kdtree.cpp
Expand Up @@ -132,21 +132,20 @@ class KDTreeSearch {

KDTREE *tree_;
FLOAT32 *query_point_;
MinK<FLOAT32, void *>* results_;
FLOAT32 *sb_min_; //< search box minimum
FLOAT32 *sb_max_; //< search box maximum
MinK<FLOAT32, void *> results_;
};

KDTreeSearch::KDTreeSearch(KDTREE* tree, FLOAT32 *query_point, int k_closest) :
tree_(tree),
query_point_(query_point) {
results_ = new MinK<FLOAT32, void *>(MAXSEARCH, k_closest);
query_point_(query_point),
results_(MAXSEARCH, k_closest) {
sb_min_ = new FLOAT32[tree->KeySize];
sb_max_ = new FLOAT32[tree->KeySize];
}

KDTreeSearch::~KDTreeSearch() {
delete results_;
delete[] sb_min_;
delete[] sb_max_;
}
Expand All @@ -164,11 +163,12 @@ void KDTreeSearch::Search(int *result_count,
sb_max_[i] = tree_->KeyDesc[i].Max;
}
SearchRec(0, tree_->Root.Left);
int count = results_->elements_count();
int count = results_.elements_count();
*result_count = count;
for (int j = 0; j < count; j++) {
distances[j] = (FLOAT32) sqrt((FLOAT64)results_->elements()[j].key);
results[j] = results_->elements()[j].value;
// TODO: why FLOAT64 here?
distances[j] = (FLOAT32) sqrt((FLOAT64)results_.elements()[j].key);
results[j] = results_.elements()[j].value;
}
}
}
Expand Down Expand Up @@ -405,9 +405,9 @@ void KDTreeSearch::SearchRec(int level, KDNODE *sub_tree) {
if (!BoxIntersectsSearch(sb_min_, sb_max_))
return;

results_->insert(DistanceSquared(tree_->KeySize, tree_->KeyDesc,
results_.insert(DistanceSquared(tree_->KeySize, tree_->KeyDesc,
query_point_, sub_tree->Key),
sub_tree->Data);
sub_tree->Data);

if (query_point_[level] < sub_tree->BranchPoint) {
if (sub_tree->Left != NULL) {
Expand Down Expand Up @@ -479,9 +479,10 @@ FLOAT32 ComputeDistance(int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[]) {
/// one wrap distance away from the query.
bool KDTreeSearch::BoxIntersectsSearch(FLOAT32 *lower, FLOAT32 *upper) {
FLOAT32 *query = query_point_;
// Why FLOAT64?
FLOAT64 total_distance = 0.0;
FLOAT64 radius_squared =
results_->max_insertable_key() * results_->max_insertable_key();
results_.max_insertable_key() * results_.max_insertable_key();
PARAM_DESC *dim = tree_->KeyDesc;

for (int i = tree_->KeySize; i > 0; i--, dim++, query++, lower++, upper++) {
Expand Down

0 comments on commit 562de89

Please sign in to comment.