X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2Ffpa%2FBase%2FMinimumSpanningTree.hxx;h=c5838f319be7784e4d949796aee058ac1d442a75;hb=5c42def42121f822c3f6e482d0b4f193a95a8855;hp=c53a071dd2dba934907f5826c88a0afef23ce94e;hpb=8fafb83c41ab35dfc25eb637170882a612924433;p=FrontAlgorithms.git diff --git a/lib/fpa/Base/MinimumSpanningTree.hxx b/lib/fpa/Base/MinimumSpanningTree.hxx index c53a071..c5838f3 100644 --- a/lib/fpa/Base/MinimumSpanningTree.hxx +++ b/lib/fpa/Base/MinimumSpanningTree.hxx @@ -1,27 +1,39 @@ -#ifndef __FPA__BASE__MINIMUMSPANNINGTREE__HXX__ -#define __FPA__BASE__MINIMUMSPANNINGTREE__HXX__ +// ========================================================================= +// @author Leonardo Florez Valencia +// @email florez-l@javeriana.edu.co +// ========================================================================= -#include +#ifndef __fpa__Base__MinimumSpanningTree__hxx__ +#define __fpa__Base__MinimumSpanningTree__hxx__ // ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -const unsigned long fpa::Base:: -MinimumSpanningTree< _TSuperclass, _TVertex >::INF_VALUE = - std::numeric_limits< unsigned long >::max( ); - +template< class _TVertex, class _Superclass > +const typename fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +TCollisions& fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +GetCollisions( ) const +{ + return( this->m_Collisions ); +} + // ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -void fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: +template< class _TVertex, class _Superclass > +void fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: SetCollisions( const TCollisions& collisions ) { - // Prepare a front graph + static const unsigned long _inf = + std::numeric_limits< unsigned long >::max( ); + if( this->m_Collisions == collisions ) + return; + this->m_Collisions = collisions; - unsigned long nSeeds = this->m_Collisions.size( ); - _TMatrix dist( nSeeds, _TRow( nSeeds, Self::INF_VALUE ) ); + + // Prepare a front graph + unsigned long N = this->m_Collisions.size( ); + _TMatrix dist( N, _TRow( N, _inf ) ); this->m_FrontPaths = dist; - for( unsigned long i = 0; i < nSeeds; ++i ) + for( unsigned long i = 0; i < N; ++i ) { - for( unsigned long j = 0; j < nSeeds; ++j ) + for( unsigned long j = 0; j < N; ++j ) { if( this->m_Collisions[ i ][ j ].second ) { @@ -38,16 +50,24 @@ SetCollisions( const TCollisions& collisions ) } // rof - // Use Floyd-Warshall to compute all possible paths - for( unsigned long k = 0; k < nSeeds; ++k ) + // Use Floyd-Warshall to compute all possible paths between fronts + for( unsigned long k = 0; k < N; ++k ) { - for( unsigned long i = 0; i < nSeeds; ++i ) + for( unsigned long i = 0; i < N; ++i ) { - for( unsigned long j = 0; j < nSeeds; ++j ) + for( unsigned long j = 0; j < N; ++j ) { - if( ( dist[ i ][ k ] + dist[ k ][ j ] ) < dist[ i ][ j ] ) + // WARNING: you don't want a numeric overflow!!! + unsigned long dik = dist[ i ][ k ]; + unsigned long dkj = dist[ k ][ j ]; + unsigned long sum = _inf; + if( dik < _inf && dkj < _inf ) + sum = dik + dkj; + + // Ok, continue Floyd-Warshall + if( sum < dist[ i ][ j ] ) { - dist[ i ][ j ] = dist[ i ][ k ] + dist[ k ][ j ]; + dist[ i ][ j ] = sum; this->m_FrontPaths[ i ][ j ] = this->m_FrontPaths[ i ][ k ]; } // fi @@ -61,72 +81,79 @@ SetCollisions( const TCollisions& collisions ) } // ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -typename fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -TVertices fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -GetPath( const TVertex& a ) const +template< class _TVertex, class _Superclass > +void fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +ClearSeeds( ) +{ + this->m_Seeds.clear( ); + this->Modified( ); +} + +// ------------------------------------------------------------------------- +template< class _TVertex, class _Superclass > +void fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +AddSeed( const _TVertex& seed ) { - TVertices path; - if( this->_HasVertex( a ) ) - this->_Path( path, a ); - return( path ); + this->m_Seeds.push_back( seed ); + this->Modified( ); } // ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -typename fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -TVertices fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -GetPath( const TVertex& a, const TVertex& b ) const +template< class _TVertex, class _Superclass > +typename fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +TVertices fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +GetPath( const _TVertex& a ) const { - TVertices path; + TVertices vertices; + _TVertex it = a; + _TVertex p = this->GetParent( it ); + while( it != p ) + { + vertices.push_back( it ); + it = p; + p = this->GetParent( it ); + + } // elihw + vertices.push_back( it ); + return( vertices ); +} - // Check existence - if( !this->_HasVertex( a ) || !this->_HasVertex( b ) ) - return( path ); - - // Get front ids - short fa = this->_FrontId( a ); - short fb = this->_FrontId( b ); +// ------------------------------------------------------------------------- +template< class _TVertex, class _Superclass > +typename fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +TVertices fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: +GetPath( const _TVertex& a, const _TVertex& b ) const +{ + static const unsigned long _inf = + std::numeric_limits< unsigned long >::max( ); - if( fa == fb ) + TVertices vertices; + TVertices pa = this->GetPath( a ); + TVertices pb = this->GetPath( b ); + if( pa.size( ) > 0 && pb.size( ) > 0 ) { - // Get paths - TVertices ap, bp; - this->_Path( ap, a ); - this->_Path( bp, b ); - - // Ignore common part: find common ancestor - auto raIt = ap.rbegin( ); - auto rbIt = bp.rbegin( ); - while( *raIt == *rbIt && raIt != ap.rend( ) && rbIt != bp.rend( ) ) + // Find front identifiers + unsigned long ia = _inf, ib = _inf; + unsigned long N = this->m_Seeds.size( ); + for( unsigned long i = 0; i < N; ++i ) { - ++raIt; - ++rbIt; - - } // elihw - if( raIt != ap.rbegin( ) ) --raIt; - if( rbIt != bp.rbegin( ) ) --rbIt; - - // Add part from a - for( auto iaIt = ap.begin( ); iaIt != ap.end( ) && *iaIt != *raIt; ++iaIt ) - path.push_back( *iaIt ); - - // Add part from b - for( ; rbIt != bp.rend( ); ++rbIt ) - path.push_back( *rbIt ); - } - else - { - // Use this->m_FrontPaths from Floyd-Warshall - if( this->m_FrontPaths[ fa ][ fb ] < Self::INF_VALUE ) + if( this->m_Seeds[ i ] == pa[ pa.size( ) - 1 ] ) + ia = i; + if( this->m_Seeds[ i ] == pb[ pb.size( ) - 1 ] ) + ib = i; + + } // rof + + // Check if there is a front-jump between given seeds + if( ia != ib ) { // Compute front path std::vector< long > fpath; - fpath.push_back( fa ); - while( fa != fb ) + fpath.push_back( ia ); + while( ia != ib ) { - fa = this->m_FrontPaths[ fa ][ fb ]; - fpath.push_back( fa ); + ia = this->m_FrontPaths[ ia ][ ib ]; + fpath.push_back( ia ); } // elihw @@ -135,7 +162,7 @@ GetPath( const TVertex& a, const TVertex& b ) const if( N > 0 ) { // First path: from start vertex to first collision - path = this->GetPath( + vertices = this->GetPath( a, this->m_Collisions[ fpath[ 0 ] ][ fpath[ 1 ] ].first ); @@ -147,7 +174,8 @@ GetPath( const TVertex& a, const TVertex& b ) const this->m_Collisions[ fpath[ i ] ][ fpath[ i - 1 ] ].first, this->m_Collisions[ fpath[ i ] ][ fpath[ i + 1 ] ].first ); - path.insert( path.end( ), ipath.begin( ), ipath.end( ) ); + for( long id = 0; id < ipath.size( ); ++id ) + vertices.push_back( ipath[ id ] ); } // rof @@ -156,81 +184,54 @@ GetPath( const TVertex& a, const TVertex& b ) const this->GetPath( this->m_Collisions[ fpath[ N - 1 ] ][ fpath[ N - 2 ] ].first, b ); - path.insert( path.end( ), lpath.begin( ), lpath.end( ) ); + for( long id = 0; id < lpath.size( ); ++id ) + vertices.push_back( lpath[ id ] ); } // fi + } + else + { + // Ignore common part: find common ancestor + long aIt = pa.size( ) - 1; + long bIt = pb.size( ) - 1; + bool cont = true; + while( aIt >= 0 && bIt >= 0 && cont ) + { + cont = ( pa[ aIt ] == pb[ bIt ] ); + aIt--; + bIt--; - } // fi - - } // fi - return( path ); -} - -// ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -typename fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -TPoints fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -GetEuclideanPath( const TVertex& a ) const -{ - TPoints points; - return( points ); -} - -// ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -typename fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -TPoints fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -GetEuclideanPath( const TVertex& a, const TVertex& b ) const -{ - TPoints points; - return( points ); -} + } // elihw + aIt++; + bIt++; -// ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -bool fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -IsDefinedInEuclideanSpace( ) const -{ - return( false ); -} + // Glue both parts + for( long cIt = 0; cIt <= aIt; ++cIt ) + vertices.push_back( pa[ cIt ] ); + for( ; bIt >= 0; --bIt ) + vertices.push_back( pb[ bIt ] ); -// ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -void fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -SetNode( - const TVertex& v, const TVertex& p, - const short& fid, const double& cost - ) -{ - typedef typename TNodeQueue::value_type _TNodeQueueValue; - if( this->m_FillNodeQueue ) - this->m_NodeQueue.insert( _TNodeQueueValue( cost, v ) ); -} + } // fi -// ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -void fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: -Clear( ) -{ - this->m_NodeQueue.clear( ); + } // fi + return( vertices ); } // ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: +template< class _TVertex, class _Superclass > +fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: MinimumSpanningTree( ) - : Superclass( ), - m_FillNodeQueue( false ) + : Superclass( ) { } // ------------------------------------------------------------------------- -template< class _TSuperclass, class _TVertex > -fpa::Base::MinimumSpanningTree< _TSuperclass, _TVertex >:: +template< class _TVertex, class _Superclass > +fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: ~MinimumSpanningTree( ) { } -#endif // __FPA__BASE__MINIMUMSPANNINGTREE__HXX__ +#endif // __fpa__Base__MinimumSpanningTree__hxx__ // eof - $RCSfile$