33 #ifndef __H__LIB_GRID__KD_TREE_IMPL__
34 #define __H__LIB_GRID__KD_TREE_IMPL__
46 template<
class TPositionAttachment,
int numDimensions,
class TVector>
48 KDTreeStatic<TPositionAttachment, numDimensions, TVector>
56 m_pChild[0] = m_pChild[1] = NULL;
62 template<
class TPositionAttachment,
int numDimensions,
class TVector>
71 template<
class TPositionAttachment,
int numDimensions,
class TVector>
72 template <
class TVrtIterator>
76 TPositionAttachment& aPos,
int maxTreeDepth,
80 return create_from_grid(grid, vrtsBegin, vrtsEnd, aaPos, maxTreeDepth,
81 splitThreshold, splitDimension);
84 template<
class TPositionAttachment,
int numDimensions,
class TVector>
85 template <
class TVrtIterator>
90 int maxTreeDepth,
int splitThreshold,
96 m_iSplitThreshold = splitThreshold;
97 m_splitDimension = splitDimension;
98 return create_barycentric(vrtsBegin, vrtsEnd, grid.
num_vertices(), &m_parentNode, 0, maxTreeDepth);
101 template<
class TPositionAttachment,
int numDimensions,
class TVector>
104 get_neighbourhood(std::vector<Vertex*>& vrtsOut,
typename TPositionAttachment::ValueType& pos,
int numClosest)
107 m_numNeighboursFound = 0;
110 neighbourhood(pdList, &m_parentNode, pos, numClosest);
111 for(KDVertexDistanceList::iterator iter = pdList.begin(); iter != pdList.end(); iter++)
112 vrtsOut.push_back((*iter).vertex);
116 template<
class TPositionAttachment,
int numDimensions,
class TVector>
119 get_points_in_box(std::vector<Vertex*>& vrtsOut,
const TVector& boxMin,
const TVector& boxMax)
122 return get_vertices_in_box(vrtsOut, &m_parentNode, boxMin, boxMax);
125 template<
class TPositionAttachment,
int numDimensions,
class TVector>
131 get_leafs_recursive(vLeafsOut, &m_parentNode);
137 template<
class TPositionAttachment,
int numDimensions,
class TVector>
140 get_points_in_box(std::vector<Vertex*>& vrtsOut,
Node* pNode,
const TVector& boxMin,
const TVector& boxMax)
149 for(
int i = 0; i < numDimensions; ++i)
151 if((m_aaPos[*iter].coord(i) < boxMin[i]) || (m_aaPos[*iter].coord(i) > boxMax[i]))
158 vrtsOut.push_back(*iter);
169 bool bSuccess =
true;
172 bSuccess &= get_points_in_box(vrtsOut, pNode->
m_pChild[1], boxMin, boxMax);
174 bSuccess &= get_points_in_box(vrtsOut, pNode->
m_pChild[0], boxMin, boxMax);
181 template<
class TPositionAttachment,
int numDimensions,
class TVector>
194 for(
int i = 0; i < numDimensions; ++i)
196 float t = pos[i] - m_aaPos[*iter].coord(i);
201 if(m_numNeighboursFound == numClosest)
203 if(distSQ < m_maxDistSQ)
211 KDVertexDistanceList::iterator pdIter;
213 for(pdIter = vrtsOut.begin(); pdIter != vrtsOut.end(); pdIter++)
215 if(distSQ < (*pdIter).distSQ)
218 m_numNeighboursFound++;
223 if(pdIter == vrtsOut.end())
226 m_numNeighboursFound++;
228 m_maxDistSQ = vrtsOut.back().distSQ;
230 if(m_numNeighboursFound > numClosest)
233 KDVertexDistanceList::iterator tmpIter = vrtsOut.end();
235 vrtsOut.erase(tmpIter);
236 m_numNeighboursFound--;
238 m_maxDistSQ = vrtsOut.back().distSQ;
256 neighbourhood(vrtsOut, pNode->
m_pChild[bestNodeIndex], pos, numClosest);
260 if(pNode->
m_pChild[1 - bestNodeIndex])
262 if(m_numNeighboursFound < numClosest)
263 neighbourhood(vrtsOut, pNode->
m_pChild[1 - bestNodeIndex], pos, numClosest);
267 if(t*t < m_maxDistSQ)
268 neighbourhood(vrtsOut, pNode->
m_pChild[1 - bestNodeIndex], pos, numClosest);
274 template<
class TPositionAttachment,
int numDimensions,
class TVector>
275 template <
class TVertexIterator>
278 create_barycentric(TVertexIterator vrts_begin, TVertexIterator vrts_end,
int numVertices,
279 Node* pNode,
int actDimension,
int maxTreeDepth)
283 if((maxTreeDepth < 1) || (numVertices <= m_iSplitThreshold))
288 for(TVertexIterator iter = vrts_begin; iter != vrts_end; iter++)
296 float barycentre = 0;
298 for(TVertexIterator iter = vrts_begin; iter != vrts_end; iter++)
299 barycentre += m_aaPos[*iter].coord(actDimension);
300 barycentre /= (float)numVertices;
304 std::list<Vertex*> lstPos;
305 std::list<Vertex*> lstNeg;
309 for(TVertexIterator iter = vrts_begin; iter != vrts_end; iter++, numPos++, numNeg++)
311 if(m_aaPos[*iter].coord(actDimension) >= barycentre)
312 lstPos.push_back(*iter);
314 lstNeg.push_back(*iter);
318 bool bSuccess =
true;
323 for(
int i = 0; i < 2; ++i)
327 bSuccess &= create_barycentric(lstPos.begin(), lstPos.end(), numPos, pNode->
m_pChild[0], get_next_split_dimension(actDimension, lstPos.begin(), lstPos.end()), maxTreeDepth - 1);
330 bSuccess &= create_barycentric(lstNeg.begin(), lstNeg.end(), numNeg, pNode->
m_pChild[1], get_next_split_dimension(actDimension, lstNeg.begin(), lstNeg.end()), maxTreeDepth - 1);
336 template<
class TPositionAttachment,
int numDimensions,
class TVector>
337 template <
class TVertexIterator>
346 TVertexIterator iter = vrts_begin;
349 for(
int i = 0; i < numDimensions; i++)
350 boxMin[i] = boxMax[i] = m_aaPos[*iter].coord(i);
355 for(; iter != vrts_end; iter++)
357 for(
int i = 0; i < numDimensions; ++i)
359 boxMin[i] = min(boxMin[i], m_aaPos[*iter].coord(i));
360 boxMax[i] = max(boxMax[i], m_aaPos[*iter].coord(i));
366 for(
int i = 0; i < numDimensions; ++i)
367 extension[i] = boxMax[i] - boxMin[i];
372 for(
uint i = 1; i < TVector::Size; ++i)
374 if(extension[i] > extension[bCI])
381 template<
class TPositionAttachment,
int numDimensions,
class TVector>
382 template <
class TVertexIterator>
387 switch(m_splitDimension)
390 return get_largest_dimension(vrts_begin, vrts_end);
393 return (actSplitDimension+1) % numDimensions;
398 return (actSplitDimension+1) % numDimensions;
401 template<
class TPositionAttachment,
int numDimensions,
class TVector>
410 vLeafsOut.push_back(pNode);
414 for(
int i = 0; i < 2; ++i)
417 get_leafs_recursive(vLeafsOut, pNode->
m_pChild[i]);
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
size_t num_vertices() const
Definition: grid.h:551
Definition: kd_tree_static.h:90
float m_fSplitValue
Definition: kd_tree_static.h:98
int m_iSplitDimension
Definition: kd_tree_static.h:99
Node * m_pChild[2]
Definition: kd_tree_static.h:97
VertexVec * m_pvVertices
Definition: kd_tree_static.h:100
void clear()
Definition: kd_tree_static_impl.hpp:65
void get_leafs(std::vector< Node * > &vLeafsOut)
Definition: kd_tree_static_impl.hpp:128
void get_leafs_recursive(std::vector< Node * > &vLeafsOut, Node *pNode)
Definition: kd_tree_static_impl.hpp:404
bool create_barycentric(TVertexIterator vrts_begin, TVertexIterator vrts_end, int numVertices, Node *pNode, int actDimension, int maxTreeDepth)
Definition: kd_tree_static_impl.hpp:278
std::vector< Vertex * > VertexVec
Definition: kd_tree_static.h:87
bool get_neighbourhood(std::vector< Vertex * > &vrtsOut, typename TPositionAttachment::ValueType &pos, int numClosest)
Definition: kd_tree_static_impl.hpp:104
bool get_points_in_box(std::vector< Vertex * > &vrtsOut, const TVector &boxMin, const TVector &boxMax)
Definition: kd_tree_static_impl.hpp:119
void neighbourhood(KDVertexDistanceList &vrtsOut, Node *pNode, TVector &pos, int numClosest)
Definition: kd_tree_static_impl.hpp:184
bool create_from_grid(Grid &grid, TVrtIterator vrtsBegin, TVrtIterator vrtsEnd, TPositionAttachment &aPos, int maxTreeDepth, int splitThreshold, KDSplitDimension splitDimension=KDSD_LARGEST)
Definition: kd_tree_static_impl.hpp:75
int get_next_split_dimension(int actSplitDimension, TVertexIterator vrts_begin, TVertexIterator vrts_end)
Definition: kd_tree_static_impl.hpp:385
int get_largest_dimension(TVertexIterator vrts_begin, TVertexIterator vrts_end)
Definition: kd_tree_static_impl.hpp:340
used by KDTreeStatic
Definition: kd_tree_static.h:50
std::list< KDVertexDistance > KDVertexDistanceList
Definition: kd_tree_static.h:67
KDSplitDimension
used by KDTreeStatic
Definition: kd_tree_static.h:62
@ KDSD_LARGEST
Definition: kd_tree_static.h:64
@ KDSD_CIRCULAR
Definition: kd_tree_static.h:63
unsigned int uint
Definition: types.h:114
Definition: smart_pointer.h:814