Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
construct_kdtree.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/kdtree/util.cc
11 /// @brief
12 /// @author James Thompson
13 
15 #include <numeric/kdtree/util.hh>
18 #include <numeric/kdtree/KDNode.hh>
21 
24 
25 #include <algorithm>
26 
27 namespace numeric {
28 namespace kdtree {
29 
32 
33  numeric::Size depth,
34  KDTree & tree
35 ) {
36  using numeric::Real;
37  using numeric::Size;
38  using utility::vector1;
39  if ( points.size() == 0 ) {
40  return NULL;
41  }
42 
43  numeric::Size width = points.front()->size();
44  numeric::Size const axis( ( depth % width ) + 1 );
45 
46  // sort points by this axis, which is the index into the points for
47  // comparison. This is a big place where we can get a speedup in tree
48  // construction. Optimizations for the future could be:
49  // - a linear-time median finding function (smart and hard to
50  // implement).
51  // - finding a median based on a subset of points and grabbing a pivot
52  // point near it (stupid but easy to implement).
53  std::sort( points.begin(), points.end(), CompareKDPoints(axis) );
54 
55  //std::cout << "axis = " << axis << std::endl;
56  //std::cout << "constructing tree from points: " << std::endl;
57  //for ( Size ii = 1; ii <= points.size(); ++ii ) {
58  // print_point( std::cout, points[ii]->location() );
59  // std::cout << std::endl;
60  //}
61  //std::cout << std::endl << std::endl;
62 
63  // location is the median of points along the split axis
64  Size const median_idx( static_cast< Size > ( points.size() / 2 ) + 1 );
65 
66  KDNodeOP current( new KDNode(tree) );
67  //tree.extend_bounds( points[median_idx]->location() );
68  //std::cout << "median_idx = " << median_idx << std::endl;
69  //std::cout << "current point = ";
70  //print_point( std::cout, points[median_idx]->location() );
71  //std::cout << std::endl;
72  current->point( points[median_idx] );
73  current->split_axis( axis );
74 
75  // if we have enough points, split the points into two halves:
76  // - the set of points less than this location (left_child)
77  // - the set of points greater than this location (right_child)
78  // Since the points are already sorted by axis.
79  if ( points.size() > 1 ) {
80  vector1< KDPointOP > left_points(
81  points.begin(), points.begin() + median_idx - 1
82  );
83  current->left_child( construct_kd_tree( left_points, depth + 1, tree ) );
84  current->left_child()->parent( current );
85  }
86  if ( points.size() > 2 ) {
87  vector1< KDPointOP > right_points(
88  points.begin() + median_idx, points.end()
89  );
90  current->right_child( construct_kd_tree( right_points, depth + 1, tree ) );
91  current->right_child()->parent( current );
92  }
93 
94  return current;
95 } // construct_kd_tree
96 
97 } // kdtree
98 } // numeric
constants used for kd-tree implementation
Implementation of a node in a kd-tree. See numeric/kdtree/kdtree.hh for more information.
utility functions for kd-tree. See kdtree.hh for more information.
platform::Size Size
Definition: types.hh:42
ReferenceCount base class – dispatch class.
utility::pointer::shared_ptr< KDNode > KDNodeOP
Definition: KDNode.fwd.hh:22
double Real
Definition: types.hh:39
utility::pointer::ReferenceCount forward declarations
KDNodeOP construct_kd_tree(utility::vector1< KDPointOP > &points, numeric::Size depth, KDTree &tree)
Function for constructing a KDTree. Returns a KDNodeOP that represents the root of the tree...
point axis