rtree: Updating to use functional

(cherry picked from commit 2cf38f68fe)
This commit is contained in:
Seth Hillbrand 2018-07-06 10:20:26 -07:00
parent c7e511c919
commit 4f2f8e7f67
1 changed files with 19 additions and 33 deletions

View File

@ -30,13 +30,10 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <algorithm>
#include <functional>
#define ASSERT assert // RTree uses ASSERT( condition ) #define ASSERT assert // RTree uses ASSERT( condition )
#ifndef rMin
#define rMin std::min
#endif // rMin
#ifndef rMax
#define rMax std::max
#endif // rMax
// //
// RTree.h // RTree.h
@ -124,13 +121,11 @@ public:
/// Find all within search rectangle /// Find all within search rectangle
/// \param a_min Min of search bounding rect /// \param a_min Min of search bounding rect
/// \param a_max Max of search bounding rect /// \param a_max Max of search bounding rect
/// \param a_resultCallback Callback function to return result. Callback should return 'true' to continue searching /// \param a_callback Callback function to return result. Callback should return 'true' to continue searching
/// \param a_context User context to pass as parameter to a_resultCallback
/// \return Returns the number of entries found /// \return Returns the number of entries found
int Search( const ELEMTYPE a_min[NUMDIMS], int Search( const ELEMTYPE a_min[NUMDIMS],
const ELEMTYPE a_max[NUMDIMS], const ELEMTYPE a_max[NUMDIMS],
bool a_resultCallback( DATATYPE a_data, void* a_context ), std::function<bool (const DATATYPE&)> a_callback ) const;
void* a_context );
template <class VISITOR> template <class VISITOR>
int Search( const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], VISITOR& a_visitor ) int Search( const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], VISITOR& a_visitor )
@ -465,14 +460,13 @@ protected:
ListNode** a_listNode ); ListNode** a_listNode );
ListNode* AllocListNode(); ListNode* AllocListNode();
void FreeListNode( ListNode* a_listNode ); void FreeListNode( ListNode* a_listNode );
bool Overlap( Rect* a_rectA, Rect* a_rectB ); bool Overlap( Rect* a_rectA, Rect* a_rectB ) const;
void ReInsert( Node* a_node, ListNode** a_listNode ); void ReInsert( Node* a_node, ListNode** a_listNode );
ELEMTYPE MinDist( const ELEMTYPE a_point[NUMDIMS], Rect* a_rect ); ELEMTYPE MinDist( const ELEMTYPE a_point[NUMDIMS], Rect* a_rect );
void InsertNNListSorted( std::vector<NNNode*>* nodeList, NNNode* newNode ); void InsertNNListSorted( std::vector<NNNode*>* nodeList, NNNode* newNode );
bool Search( Node * a_node, Rect * a_rect, int& a_foundCount, bool a_resultCallback( bool Search( Node * a_node, Rect * a_rect, int& a_foundCount,
DATATYPE a_data, std::function<bool (const DATATYPE&)> a_callback ) const;
void* a_context ), void* a_context );
template <class VISITOR> template <class VISITOR>
bool Search( Node* a_node, Rect* a_rect, VISITOR& a_visitor, int& a_foundCount ) bool Search( Node* a_node, Rect* a_rect, VISITOR& a_visitor, int& a_foundCount )
@ -695,8 +689,7 @@ bool RTREE_QUAL::Remove( const ELEMTYPE a_min[NUMDIMS],
RTREE_TEMPLATE RTREE_TEMPLATE
int RTREE_QUAL::Search( const ELEMTYPE a_min[NUMDIMS], int RTREE_QUAL::Search( const ELEMTYPE a_min[NUMDIMS],
const ELEMTYPE a_max[NUMDIMS], const ELEMTYPE a_max[NUMDIMS],
bool a_resultCallback( DATATYPE a_data, void* a_context ), std::function<bool (const DATATYPE&)> a_callback ) const
void* a_context )
{ {
#ifdef _DEBUG #ifdef _DEBUG
@ -718,7 +711,7 @@ int RTREE_QUAL::Search( const ELEMTYPE a_min[NUMDIMS],
// NOTE: May want to return search result another way, perhaps returning the number of found elements here. // NOTE: May want to return search result another way, perhaps returning the number of found elements here.
int foundCount = 0; int foundCount = 0;
Search( m_root, &rect, foundCount, a_resultCallback, a_context ); Search( m_root, &rect, foundCount, a_callback );
return foundCount; return foundCount;
} }
@ -1325,8 +1318,8 @@ typename RTREE_QUAL::Rect RTREE_QUAL::CombineRect( Rect* a_rectA, Rect* a_rectB
for( int index = 0; index < NUMDIMS; ++index ) for( int index = 0; index < NUMDIMS; ++index )
{ {
newRect.m_min[index] = rMin( a_rectA->m_min[index], a_rectB->m_min[index] ); newRect.m_min[index] = std::min( a_rectA->m_min[index], a_rectB->m_min[index] );
newRect.m_max[index] = rMax( a_rectA->m_max[index], a_rectB->m_max[index] ); newRect.m_max[index] = std::max( a_rectA->m_max[index], a_rectB->m_max[index] );
} }
return newRect; return newRect;
@ -1774,7 +1767,7 @@ bool RTREE_QUAL::RemoveRectRec( Rect* a_rect,
// Decide whether two rectangles overlap. // Decide whether two rectangles overlap.
RTREE_TEMPLATE RTREE_TEMPLATE
bool RTREE_QUAL::Overlap( Rect* a_rectA, Rect* a_rectB ) bool RTREE_QUAL::Overlap( Rect* a_rectA, Rect* a_rectB ) const
{ {
ASSERT( a_rectA && a_rectB ); ASSERT( a_rectA && a_rectB );
@ -1807,9 +1800,8 @@ void RTREE_QUAL::ReInsert( Node* a_node, ListNode** a_listNode )
// Search in an index tree or subtree for all data retangles that overlap the argument rectangle. // Search in an index tree or subtree for all data retangles that overlap the argument rectangle.
RTREE_TEMPLATE RTREE_TEMPLATE
bool RTREE_QUAL::Search( Node* a_node, Rect* a_rect, int& a_foundCount, bool a_resultCallback( bool RTREE_QUAL::Search( Node* a_node, Rect* a_rect, int& a_foundCount,
DATATYPE a_data, std::function<bool (const DATATYPE&)> a_callback ) const
void* a_context ), void* a_context )
{ {
ASSERT( a_node ); ASSERT( a_node );
ASSERT( a_node->m_level >= 0 ); ASSERT( a_node->m_level >= 0 );
@ -1821,8 +1813,7 @@ bool RTREE_QUAL::Search( Node* a_node, Rect* a_rect, int& a_foundCount, bool a_r
{ {
if( Overlap( a_rect, &a_node->m_branch[index].m_rect ) ) if( Overlap( a_rect, &a_node->m_branch[index].m_rect ) )
{ {
if( !Search( a_node->m_branch[index].m_child, a_rect, a_foundCount, if( !Search( a_node->m_branch[index].m_child, a_rect, a_foundCount, a_callback ) )
a_resultCallback, a_context ) )
{ {
return false; // Don't continue searching return false; // Don't continue searching
} }
@ -1836,20 +1827,15 @@ bool RTREE_QUAL::Search( Node* a_node, Rect* a_rect, int& a_foundCount, bool a_r
if( Overlap( a_rect, &a_node->m_branch[index].m_rect ) ) if( Overlap( a_rect, &a_node->m_branch[index].m_rect ) )
{ {
DATATYPE& id = a_node->m_branch[index].m_data; DATATYPE& id = a_node->m_branch[index].m_data;
// NOTE: There are different ways to return results. Here's where to modify
if( &a_resultCallback )
{
++a_foundCount; ++a_foundCount;
if( !a_resultCallback( id, a_context ) ) if( a_callback && !a_callback( id ) )
{ {
return false; // Don't continue searching return false; // Don't continue searching
} }
} }
} }
} }
}
return true; // Continue searching return true; // Continue searching
} }