kdtree.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
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 //#include "spacetree.h"
00051 //#include "bounds.h"
00052 
00053 #include "fastlib/col/arraylist.h"
00054 #include "fastlib/fx/fx.h"
00055 
00056 #include "fastlib/tree/kdtree_impl.h"
00057 //#include "kdtree_impl.h"
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       //if (fx_param_exists("do_pca")) {}
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
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3