Cortex  10.0.0-a4
KDTree.h
1 //
3 // Copyright (c) 2007-2011, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IE_CORE_KDTREE_H
36 #define IE_CORE_KDTREE_H
37 
38 #include <set>
39 #include <vector>
40 
41 #include "OpenEXR/ImathVec.h"
42 
43 #include "IECore/VectorTraits.h"
44 
45 namespace IECore
46 {
47 
52 template<class PointIterator>
53 class KDTree
54 {
55  public :
56 
57  typedef PointIterator Iterator;
58  typedef typename std::iterator_traits<PointIterator>::value_type Point;
59  typedef typename VectorTraits<Point>::BaseType BaseType;
60  class Node;
61  typedef std::vector<Node> NodeVector;
62  typedef typename NodeVector::size_type NodeIndex;
63 
66  KDTree();
67 
72  KDTree( PointIterator first, PointIterator last, int maxLeafSize=4 );
73 
79  void init( PointIterator first, PointIterator last, int maxLeafSize=4 );
80 
83  PointIterator nearestNeighbour( const Point &p ) const;
91  PointIterator nearestNeighbour( const Point &p, BaseType &distSquared ) const;
92 
97  unsigned int nearestNeighbours( const Point &p, BaseType r, std::vector<PointIterator> &nearNeighbours ) const;
98 
99  class Neighbour;
102  unsigned int nearestNNeighbours( const Point &p, unsigned int numNeighbours, std::vector<Neighbour> &nearNeighbours ) const;
103 
106  template<typename Box, typename OutputIterator>
107  void enclosedPoints( const Box &bound, OutputIterator it ) const;
108 
110  inline NodeIndex numNodes() const;
114  inline const Node &node( NodeIndex index ) const;
116  inline NodeIndex rootIndex() const;
119  inline NodeIndex lowChildIndex( NodeIndex parentIndex ) const;
122  inline NodeIndex highChildIndex( NodeIndex parentIndex ) const;
123 
124  private :
125 
126  typedef std::vector<PointIterator> Permutation;
127  typedef typename Permutation::iterator PermutationIterator;
128  typedef typename Permutation::const_iterator PermutationConstIterator;
129 
130  class AxisSort;
131 
132  unsigned char majorAxis( PermutationConstIterator permFirst, PermutationConstIterator permLast );
133  void build( NodeIndex nodeIndex, PermutationIterator permFirst, PermutationIterator permLast );
134 
135  void nearestNeighbourWalk( NodeIndex nodeIndex, const Point &p, PointIterator &closestPoint, BaseType &distSquared ) const;
136 
137  void nearestNeighboursWalk( NodeIndex nodeIndex, const Point &p, BaseType r2, std::vector<PointIterator> &nearNeighbours ) const;
138 
139  template<typename Box, typename OutputIterator>
140  void enclosedPointsWalk( NodeIndex nodeIndex, const Box &bound, OutputIterator it ) const;
141 
142  void nearestNNeighboursWalk( NodeIndex nodeIndex, const Point &p, unsigned int numNeighbours, std::vector<Neighbour> &nearNeighbours, BaseType &maxDistSquared ) const;
143 
144  Permutation m_perm;
145  NodeVector m_nodes;
146  int m_maxLeafSize;
147  PointIterator m_lastPoint;
148 
149 };
150 
152 template<class PointIterator>
153 class KDTree<PointIterator>::Node
154 {
155  public :
156 
158  inline bool isLeaf() const;
161  inline PointIterator *permFirst() const;
164  inline PointIterator *permLast() const;
166  inline bool isBranch() const;
169  inline unsigned char cutAxis() const;
172  inline BaseType cutValue() const;
173 
174  private :
175 
176  friend class KDTree<PointIterator>;
177 
178  inline void makeLeaf( PermutationIterator permFirst, PermutationIterator permLast );
179  inline void makeBranch( unsigned char cutAxis, BaseType cutValue );
180 
181  unsigned char m_cutAxisAndLeaf;
182  union {
183  BaseType m_cutValue;
184  struct {
185  PointIterator *first;
188  PointIterator *last;
189  } m_perm;
190  };
191 
192 };
193 
195 template<class PointIterator>
196 class KDTree<PointIterator>::Neighbour
197 {
198  public :
199 
200  Neighbour( Iterator p, BaseType d2 )
201  : point( p ), distSquared( d2 )
202  {
203  }
204 
205  Iterator point;
206  BaseType distSquared;
207 
208  bool operator < ( const Neighbour &other ) const
209  {
210  return distSquared < other.distSquared;
211  }
212 
213 };
214 
219 
220 } // namespace IECore
221 
222 #include "KDTree.inl"
223 
224 #endif // IE_CORE_KDTREE_H
The Node class which is used to implement the branching structure in the KDTree.
Definition: KDTree.h:153
NodeIndex rootIndex() const
Returns the index for the root node.
NodeIndex highChildIndex(NodeIndex parentIndex) const
void init(PointIterator first, PointIterator last, int maxLeafSize=4)
void enclosedPoints(const Box &bound, OutputIterator it) const
Definition: KDTree.h:53
unsigned int nearestNeighbours(const Point &p, BaseType r, std::vector< PointIterator > &nearNeighbours) const
The Neighbour class is used to return information from the KDTree::nearestNNeighbours() query...
Definition: KDTree.h:196
unsigned int nearestNNeighbours(const Point &p, unsigned int numNeighbours, std::vector< Neighbour > &nearNeighbours) const
Definition: VectorTraits.h:48
const Node & node(NodeIndex index) const
PointIterator nearestNeighbour(const Point &p) const
NodeIndex lowChildIndex(NodeIndex parentIndex) const
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43
NodeIndex numNodes() const
Returns the number of nodes in the tree.