From c46a49b7dda110fa7dbd81329dc6bd9c41d69f4f Mon Sep 17 00:00:00 2001 From: jwaters6 Date: Fri, 23 May 2008 19:57:54 +0000 Subject: [PATCH] Changed kdtree_impl functions to pass by reference (when applicable) --- fastlib2/fastlib/tree/bounds.h | 27 ------------- fastlib2/fastlib/tree/kdtree.h | 6 +-- fastlib2/fastlib/tree/kdtree_impl.h | 63 +++++++++++++++-------------- 3 files changed, 35 insertions(+), 61 deletions(-) diff --git a/fastlib2/fastlib/tree/bounds.h b/fastlib2/fastlib/tree/bounds.h index c230e185e6..f82057f9cc 100644 --- a/fastlib2/fastlib/tree/bounds.h +++ b/fastlib2/fastlib/tree/bounds.h @@ -198,33 +198,6 @@ class DHrectBound { } - - /** - * Calcualtes minimum bound-to-bound squared distance, with - * an offset between their respective coordinate systems. - */ - double MinDistanceSq(const DHrectBound& other, const Vector& offset) const { - double sum = 0; - const DRange *a = this->bounds_; - const DRange *b = other.bounds_; - index_t mdim = dim_; - - DEBUG_SAME_SIZE(dim_, other.dim_); - //Add Debug for offset vector - - for (index_t d = 0; d < mdim; d++) { - double v1 = b[d].lo + offset[d] - a[d].hi; - double v2 = a[d].lo - offset[d] - b[d].lo; - - double v = (v1 + fabs(v1)) + (v2 + fabs(v2)); - - sum += math::Pow(v); - } - - return math::Pow<2, t_pow>(sum) / 4; - } - - /** * Calculates maximum bound-to-point squared distance. */ diff --git a/fastlib2/fastlib/tree/kdtree.h b/fastlib2/fastlib/tree/kdtree.h index 470e64d889..99d646875b 100644 --- a/fastlib2/fastlib/tree/kdtree.h +++ b/fastlib2/fastlib/tree/kdtree.h @@ -48,7 +48,7 @@ namespace tree { template TKdTree *MakeKdTreeMidpointSelective(GenMatrix& matrix, - Vector* split_dimensions, + const Vector& split_dimensions, index_t leaf_size, ArrayList *old_from_new = NULL, ArrayList *new_from_old = NULL) { @@ -68,7 +68,7 @@ namespace tree { } node->Init(0, matrix.n_cols()); - node->bound().Init(split_dimensions->length()); + node->bound().Init(split_dimensions.length()); tree_kdtree_private::SelectFindBoundFromMatrix(matrix, split_dimensions, 0, matrix.n_cols(), &node->bound()); @@ -95,7 +95,7 @@ namespace tree { split_dimensions[i] = i; } TKdTree *result; - result = MakeKdTreeMidpointSelective(matrix, &split_dimensions, + result = MakeKdTreeMidpointSelective(matrix, split_dimensions, leaf_size, old_from_new, new_from_old); return result; } diff --git a/fastlib2/fastlib/tree/kdtree_impl.h b/fastlib2/fastlib/tree/kdtree_impl.h index 1d51ae1e52..4ffad98056 100644 --- a/fastlib2/fastlib/tree/kdtree_impl.h +++ b/fastlib2/fastlib/tree/kdtree_impl.h @@ -2,28 +2,29 @@ namespace tree_kdtree_private { template - void MakeBoundVector(GenVector* point, GenVector* bound_dimensions, - GenVector* bound_vector){ + void MakeBoundVector(const GenVector& point, + const Vector& bound_dimensions, GenVector* bound_vector){ int i; - for (i = 0; i < bound_dimensions->length(); i++){ - (*bound_vector)[i] = (*point)[(int)(*bound_dimensions)[i]]; + for (i = 0; i < bound_dimensions.length(); i++){ + (*bound_vector)[i] = point[(int)bound_dimensions[i]]; } } template void SelectFindBoundFromMatrix(const GenMatrix& matrix, - Vector* split_dimensions, index_t first, index_t count, TBound *bounds){ + const Vector& split_dimensions, index_t first, index_t count, + TBound *bounds){ index_t end = first + count; for (index_t i = first; i < end; i++) { GenVector col; matrix.MakeColumnVector(i, &col); - if (split_dimensions->length() == matrix.n_rows()){ + if (split_dimensions.length() == matrix.n_rows()){ *bounds |= col; } else { GenVector sub_col; - sub_col.Init(split_dimensions->length()); - MakeBoundVector(&col, split_dimensions, &sub_col); + sub_col.Init(split_dimensions.length()); + MakeBoundVector(col, split_dimensions, &sub_col); *bounds |= sub_col; } } @@ -38,7 +39,7 @@ namespace tree_kdtree_private { for (i = 0; i < matrix.n_rows(); i++){ split_dimensions[i] = i; } - SelectFindBoundFromMatrix(matrix, &split_dimensions, first, count, bounds); + SelectFindBoundFromMatrix(matrix, split_dimensions, first, count, bounds); } template @@ -59,11 +60,11 @@ namespace tree_kdtree_private { } template - index_t SelectMatrixPartition(GenMatrix& matrix, Vector* split_dimensions, - index_t dim, double splitvalue, - index_t first, index_t count, - TBound* left_bound, TBound* right_bound, - index_t *old_from_new) { + index_t SelectMatrixPartition(GenMatrix& matrix, + const Vector& split_dimensions, index_t dim, double splitvalue, + index_t first, index_t count, TBound* left_bound, TBound* right_bound, + index_t *old_from_new) { + index_t left = first; index_t right = first + count - 1; @@ -76,11 +77,11 @@ namespace tree_kdtree_private { while (matrix.get(dim, left) < splitvalue && likely(left <= right)) { GenVector left_vector; matrix.MakeColumnVector(left, &left_vector); - if (split_dimensions->length() == matrix.n_rows()){ + if (split_dimensions.length() == matrix.n_rows()){ *left_bound |= left_vector; } else { GenVector sub_left_vector; - MakeBoundVector(&left_vector, split_dimensions, &sub_left_vector); + MakeBoundVector(left_vector, split_dimensions, &sub_left_vector); *left_bound |= sub_left_vector; } left++; @@ -89,11 +90,11 @@ namespace tree_kdtree_private { while (matrix.get(dim, right) >= splitvalue && likely(left <= right)) { GenVector right_vector; matrix.MakeColumnVector(right, &right_vector); - if (split_dimensions->length() == matrix.n_rows()){ + if (split_dimensions.length() == matrix.n_rows()){ *right_bound |= right_vector; } else { GenVector sub_right_vector; - MakeBoundVector(&right_vector, split_dimensions, &sub_right_vector); + MakeBoundVector(right_vector, split_dimensions, &sub_right_vector); *right_bound |= sub_right_vector; } right--; @@ -112,19 +113,19 @@ namespace tree_kdtree_private { left_vector.SwapValues(&right_vector); - if (split_dimensions->length() == matrix.n_rows()){ + if (split_dimensions.length() == matrix.n_rows()){ *left_bound |= left_vector; } else { GenVector sub_left_vector; - MakeBoundVector(&left_vector, split_dimensions, &sub_left_vector); + MakeBoundVector(left_vector, split_dimensions, &sub_left_vector); *left_bound |= sub_left_vector; } - if (split_dimensions->length() == matrix.n_rows()){ + if (split_dimensions.length() == matrix.n_rows()){ *right_bound |= right_vector; } else { GenVector sub_right_vector; - MakeBoundVector(&right_vector, split_dimensions, &sub_right_vector); + MakeBoundVector(right_vector, split_dimensions, &sub_right_vector); *right_bound |= sub_right_vector; } @@ -159,15 +160,15 @@ namespace tree_kdtree_private { for (i = 0; i < matrix.n_rows(); i++){ split_dimensions[i] = i; } - SelectSplitKdTreeMidpoint(matrix, &split_dimensions, node, + SelectSplitKdTreeMidpoint(matrix, split_dimensions, node, leaf_size, old_from_new); } template - void SelectSplitKdTreeMidpoint(GenMatrix& matrix, - Vector* split_dimensions, - TKdTree *node, index_t leaf_size, index_t *old_from_new) { + void SelectSplitKdTreeMidpoint(GenMatrix& matrix, + const Vector& split_dimensions, TKdTree *node, index_t leaf_size, + index_t *old_from_new) { TKdTree *left = NULL; TKdTree *right = NULL; @@ -179,7 +180,7 @@ namespace tree_kdtree_private { index_t split_dim = BIG_BAD_NUMBER; double max_width = -1; - for (index_t d = 0; d < split_dimensions->length(); d++) { + for (index_t d = 0; d < split_dimensions.length(); d++) { double w = node->bound().get(d).width(); if (w > max_width) { @@ -195,20 +196,20 @@ namespace tree_kdtree_private { // same. We have to give up. } else { left = new TKdTree(); - left->bound().Init(split_dimensions->length()); + left->bound().Init(split_dimensions.length()); right = new TKdTree(); - right->bound().Init(split_dimensions->length()); + right->bound().Init(split_dimensions.length()); index_t split_col = SelectMatrixPartition(matrix, split_dimensions, - (int)(*split_dimensions)[split_dim], split_val, + (int)split_dimensions[split_dim], split_val, node->begin(), node->count(), &left->bound(), &right->bound(), old_from_new); VERBOSE_MSG(3.0,"split (%d,[%d],%d) dim %d on %f (between %f, %f)", node->begin(), split_col, - node->begin() + node->count(), (int)(*split_dimensions)[split_dim], + node->begin() + node->count(), (int)split_dimensions[split_dim], split_val, node->bound().get(split_dim).lo, node->bound().get(split_dim).hi);