Cortex  10.0.0-a4
BoundedKDTree.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_BOUNDEDKDTREE_H
36 #define IE_CORE_BOUNDEDKDTREE_H
37 
38 #include <vector>
39 
40 #include "OpenEXR/ImathBox.h"
41 
42 #include "IECore/BoxTraits.h"
43 
44 namespace IECore
45 {
46 
49 template<class BoundIterator>
51 {
52  public:
53 
54  typedef BoundIterator Iterator;
55  typedef typename std::iterator_traits<BoundIterator>::value_type Bound;
56  typedef typename BoxTraits<Bound>::BaseType BaseType;
57  class Node;
58  typedef std::vector<Node> NodeVector;
59  typedef typename NodeVector::size_type NodeIndex;
60 
63  BoundedKDTree();
64 
69  BoundedKDTree( BoundIterator first, BoundIterator last, int maxLeafSize=4 );
70 
76  void init( BoundIterator first, BoundIterator last, int maxLeafSize=4 );
77 
81  template<typename S>
82  unsigned int intersectingBounds( const S &b, std::vector<BoundIterator> &bounds ) const;
83 
85  inline NodeIndex numNodes() const;
86 
88  const Node& node( NodeIndex idx ) const;
89 
91  NodeIndex rootIndex() const;
92 
94  static NodeIndex lowChildIndex( NodeIndex index );
95 
97  static NodeIndex highChildIndex( NodeIndex index );
98 
99  private:
100 
101  typedef std::vector<BoundIterator> Permutation;
102  typedef typename Permutation::iterator PermutationIterator;
103  typedef typename Permutation::const_iterator PermutationConstIterator;
104 
105  class AxisSort;
106 
107  unsigned char majorAxis( PermutationConstIterator permFirst, PermutationConstIterator permLast );
108  void build( NodeIndex nodeIndex, PermutationIterator permFirst, PermutationIterator permLast );
109  void bound( NodeIndex nodeIndex );
110 
111  template<typename S>
112  void intersectingBoundsWalk( NodeIndex nodeIndex, const S &p, std::vector<BoundIterator> &bounds ) const;
113 
114  Permutation m_perm;
115  NodeVector m_nodes;
116  int m_maxLeafSize;
117  BoundIterator m_lastBound;
118 };
119 
120 template<class BoundIterator>
121 class BoundedKDTree<BoundIterator>::Node
122 {
123  public :
124 
126  Node();
127 
128  inline bool isLeaf() const;
129 
130  inline BoundIterator *permFirst() const;
131 
132  inline BoundIterator *permLast() const;
133 
134  inline bool isBranch() const;
135 
136  inline unsigned char cutAxis() const;
137 
138  inline const Bound &bound() const;
139 
140  private :
141 
142  friend class BoundedKDTree<BoundIterator>;
143 
144  inline void makeLeaf( PermutationIterator permFirst, PermutationIterator permLast );
145  inline void makeBranch( unsigned char cutAxis );
146 
147  inline Bound &bound();
148 
149  unsigned char m_cutAxisAndLeaf;
150 
151  Bound m_bound;
152 
153  struct
154  {
156  BoundIterator *first;
159  BoundIterator *last;
160  } m_perm;
161 };
162 
163 typedef BoundedKDTree<std::vector<Imath::Box2f>::const_iterator> Box2fTree;
164 typedef BoundedKDTree<std::vector<Imath::Box2d>::const_iterator> Box2dTree;
165 typedef BoundedKDTree<std::vector<Imath::Box3f>::const_iterator> Box3fTree;
166 typedef BoundedKDTree<std::vector<Imath::Box3d>::const_iterator> Box3dTree;
167 
168 }
169 
170 #include "BoundedKDTree.inl"
171 
172 #endif // IE_CORE_BOUNDEDKDTREE_H
Definition: BoundedKDTree.h:50
static NodeIndex highChildIndex(NodeIndex index)
Retrieve the index of the "high" child node.
NodeIndex rootIndex() const
Returns the index for the root node.
const Node & node(NodeIndex idx) const
Retrieve the node associated with a given index.
unsigned int intersectingBounds(const S &b, std::vector< BoundIterator > &bounds) const
void init(BoundIterator first, BoundIterator last, int maxLeafSize=4)
NodeIndex numNodes() const
Returns the number of nodes in the tree.
static NodeIndex lowChildIndex(NodeIndex index)
Retrieve the index of the "low" child node.
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43