00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00043 #ifndef TREE_KDTREE_H
00044 #define TREE_KDTREE_H
00045
00046 #include "fastlib/base/base.h"
00047
00048 #include "fastlib/tree/spacetree.h"
00049 #include "fastlib/tree/bounds.h"
00050
00051
00052
00053 #include "fastlib/col/arraylist.h"
00054 #include "fastlib/fx/fx.h"
00055
00056 #include "fastlib/tree/kdtree_impl.h"
00057
00058
00062 namespace tree {
00081 template<typename TKdTree, typename T>
00082 TKdTree *MakeKdTreeMidpointSelective(GenMatrix<T>& matrix,
00083 const Vector& split_dimensions,
00084 index_t leaf_size,
00085 ArrayList<index_t> *old_from_new = NULL,
00086 ArrayList<index_t> *new_from_old = NULL) {
00087 TKdTree *node = new TKdTree();
00088 index_t *old_from_new_ptr;
00089
00090 if (old_from_new) {
00091 old_from_new->Init(matrix.n_cols());
00092
00093 for (index_t i = 0; i < matrix.n_cols(); i++) {
00094 (*old_from_new)[i] = i;
00095 }
00096
00097 old_from_new_ptr = old_from_new->begin();
00098 } else {
00099 old_from_new_ptr = NULL;
00100 }
00101
00102 node->Init(0, matrix.n_cols());
00103 node->bound().Init(split_dimensions.length());
00104 tree_kdtree_private::SelectFindBoundFromMatrix(matrix, split_dimensions,
00105 0, matrix.n_cols(), &node->bound());
00106
00107 tree_kdtree_private::SelectSplitKdTreeMidpoint(matrix, split_dimensions,
00108 node, leaf_size, old_from_new_ptr);
00109
00110 if (new_from_old) {
00111 new_from_old->Init(matrix.n_cols());
00112 for (index_t i = 0; i < matrix.n_cols(); i++) {
00113 (*new_from_old)[(*old_from_new)[i]] = i;
00114 }
00115 }
00116 return node;
00117 }
00118
00119 template<typename TKdTree, typename T>
00120 TKdTree *MakeKdTreeMidpointSelective(GenMatrix<T>& matrix,
00121 const Vector& split_dimensions,
00122 index_t leaf_size,
00123 GenVector<index_t> *old_from_new = NULL,
00124 GenVector<index_t> *new_from_old = NULL) {
00125 TKdTree *node = new TKdTree();
00126 index_t *old_from_new_ptr;
00127
00128 if (old_from_new) {
00129 old_from_new->Init(matrix.n_cols());
00130
00131 for (index_t i = 0; i < matrix.n_cols(); i++) {
00132 (*old_from_new)[i] = i;
00133 }
00134
00135 old_from_new_ptr = old_from_new->ptr();
00136 } else {
00137 old_from_new_ptr = NULL;
00138 }
00139
00140 node->Init(0, matrix.n_cols());
00141 node->bound().Init(split_dimensions.length());
00142 tree_kdtree_private::SelectFindBoundFromMatrix(matrix, split_dimensions,
00143 0, matrix.n_cols(), &node->bound());
00144
00145 tree_kdtree_private::SelectSplitKdTreeMidpoint(matrix, split_dimensions,
00146 node, leaf_size, old_from_new_ptr);
00147
00148 if (new_from_old) {
00149 new_from_old->Init(matrix.n_cols());
00150 for (index_t i = 0; i < matrix.n_cols(); i++) {
00151 (*new_from_old)[(*old_from_new)[i]] = i;
00152 }
00153 }
00154 return node;
00155 }
00156
00157
00158 template<typename TKdTree, typename T>
00159 TKdTree *MakeKdTreeMidpoint(GenMatrix<T>& matrix, index_t leaf_size,
00160 ArrayList<index_t> *old_from_new = NULL,
00161 ArrayList<index_t> *new_from_old = NULL) {
00162 Vector split_dimensions;
00163 split_dimensions.Init(matrix.n_rows());
00164 int i;
00165 for (i = 0; i < matrix.n_rows(); i++){
00166 split_dimensions[i] = i;
00167 }
00168 TKdTree *result;
00169 result = MakeKdTreeMidpointSelective<TKdTree>(matrix, split_dimensions,
00170 leaf_size, old_from_new, new_from_old);
00171 return result;
00172 }
00173
00174 template<typename TKdTree, typename T>
00175 TKdTree *MakeKdTreeMidpoint(GenMatrix<T>& matrix,
00176 index_t leaf_size,
00177 GenVector<index_t> *old_from_new = NULL,
00178 GenVector<index_t> *new_from_old = NULL) {
00179 Vector split_dimensions;
00180 split_dimensions.Init(matrix.n_rows());
00181 int i;
00182 for (i = 0; i < matrix.n_rows(); i++){
00183 split_dimensions[i] = i;
00184 }
00185 TKdTree *result;
00186 result = MakeKdTreeMidpointSelective<TKdTree>(matrix,
00187 split_dimensions,
00188 leaf_size, old_from_new, new_from_old);
00189 return result;
00190 }
00191
00230 template<typename TKdTree, typename T>
00231 success_t LoadKdTree(datanode *module,
00232 GenMatrix<T> *matrix, TKdTree **tree_pp,
00233 ArrayList<index_t> *old_from_new) {
00234 const char *type = fx_param_str(module, "type", "text");
00235 const char *fname = fx_param_str(module, "", NULL);
00236 success_t success = SUCCESS_PASS;
00237
00238 fx_timer_start(module, "load");
00239 if (strcmp(type, "text") == 0) {
00240 int leaflen = fx_param_int(module, "leaflen", 20);
00241
00242 fx_timer_start(module, "load_matrix");
00243 success = data::Load(fname, matrix);
00244 fx_timer_stop(module, "load_matrix");
00245
00246
00247
00248 fx_timer_start(module, "make_tree");
00249 *tree_pp = MakeKdTreeMidpoint<TKdTree>(
00250 *matrix, leaflen, old_from_new);
00251 fx_timer_stop(module, "make_tree");
00252 }
00253 fx_timer_stop(module, "load");
00254
00255 return success;
00256 }
00257 }
00258
00260 typedef BinarySpaceTree<DHrectBound<2>, Matrix> BasicKdTree;
00261
00262 #endif