X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2Ffpa%2FBase%2FMinimumSpanningTree.hxx;h=6004aaa183efea0514cc2839fea585a009fadfa6;hb=479d85a44365c8aa01976ffb9c7d67d3e9e52b63;hp=8746005f8a776b11f8021a65a55d86341b65eea1;hpb=ea46079b5aef76c1782648ed23e70ea944649635;p=FrontAlgorithms.git diff --git a/lib/fpa/Base/MinimumSpanningTree.hxx b/lib/fpa/Base/MinimumSpanningTree.hxx index 8746005..6004aaa 100644 --- a/lib/fpa/Base/MinimumSpanningTree.hxx +++ b/lib/fpa/Base/MinimumSpanningTree.hxx @@ -1,20 +1,23 @@ +// ========================================================================= +// @author Leonardo Florez Valencia +// @email florez-l@javeriana.edu.co +// ========================================================================= + #ifndef __fpa__Base__MinimumSpanningTree__hxx__ #define __fpa__Base__MinimumSpanningTree__hxx__ -#include - // ------------------------------------------------------------------------- -template< class _TVertex, class _TSuperclass > -const typename fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: -TCollisions& fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +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 _TVertex, class _TSuperclass > -void fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +template< class _TVertex, class _Superclass > +void fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: SetCollisions( const TCollisions& collisions ) { static const unsigned long _inf = @@ -78,8 +81,8 @@ SetCollisions( const TCollisions& collisions ) } // ------------------------------------------------------------------------- -template< class _TVertex, class _TSuperclass > -void fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +template< class _TVertex, class _Superclass > +void fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: ClearSeeds( ) { this->m_Seeds.clear( ); @@ -87,8 +90,8 @@ ClearSeeds( ) } // ------------------------------------------------------------------------- -template< class _TVertex, class _TSuperclass > -void fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +template< class _TVertex, class _Superclass > +void fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: AddSeed( const _TVertex& seed ) { this->m_Seeds.push_back( seed ); @@ -96,35 +99,35 @@ AddSeed( const _TVertex& seed ) } // ------------------------------------------------------------------------- -template< class _TVertex, class _TSuperclass > -typename fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: -TVertices fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +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 ) { - path.push_front( it ); + vertices.push_back( it ); it = p; p = this->GetParent( it ); } // elihw - path.push_front( it ); - return( path ); + vertices.push_back( it ); + return( vertices ); } // ------------------------------------------------------------------------- -template< class _TVertex, class _TSuperclass > -typename fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: -TVertices fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +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( ); - TVertices path; + TVertices vertices; TVertices pa = this->GetPath( a ); TVertices pb = this->GetPath( b ); if( pa.size( ) > 0 && pb.size( ) > 0 ) @@ -134,95 +137,97 @@ GetPath( const _TVertex& a, const _TVertex& b ) const unsigned long N = this->m_Seeds.size( ); for( unsigned long i = 0; i < N; ++i ) { - if( this->m_Seeds[ i ] == pa.front( ) ) + if( this->m_Seeds[ i ] == pa[ pa.size( ) - 1 ] ) ia = i; - if( this->m_Seeds[ i ] == pb.front( ) ) + 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 ) { - // Use this->m_FrontPaths from Floyd-Warshall - if( this->m_FrontPaths[ ia ][ ib ] < _inf ) + // Compute front path + std::vector< long > fpath; + fpath.push_back( ia ); + while( ia != ib ) { - // Compute front path - std::vector< long > fpath; + ia = this->m_FrontPaths[ ia ][ ib ]; fpath.push_back( ia ); - while( ia != ib ) - { - ia = this->m_FrontPaths[ ia ][ ib ]; - fpath.push_back( ia ); - - } // elihw - - // Continue only if both fronts are connected - unsigned int N = fpath.size( ); - if( N > 0 ) - { - // First path: from start vertex to first collision - path = this->GetPath( - a, this->m_Collisions[ fpath[ 0 ] ][ fpath[ 1 ] ].first - ); - // Intermediary paths - for( unsigned int i = 1; i < N - 1; ++i ) - { - TVertices ipath = - this->GetPath( - 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( ) ); + } // elihw - } // rof + // Continue only if both fronts are connected + unsigned int N = fpath.size( ); + if( N > 0 ) + { + // First path: from start vertex to first collision + vertices = this->GetPath( + a, this->m_Collisions[ fpath[ 1 ] ][ fpath[ 0 ] ].first + ); - // Final path: from last collision to end point - TVertices lpath = + // Intermediary paths + for( unsigned int i = 1; i < N - 1; ++i ) + { + TVertices ipath = this->GetPath( - this->m_Collisions[ fpath[ N - 1 ] ][ fpath[ N - 2 ] ].first, b + this->m_Collisions[ fpath[ i ] ][ fpath[ i - 1 ] ].first, + this->m_Collisions[ fpath[ i ] ][ fpath[ i + 1 ] ].first ); - path.insert( path.end( ), lpath.begin( ), lpath.end( ) ); + for( long id = 0; id < ipath.size( ); ++id ) + vertices.push_back( ipath[ id ] ); - } // fi + } // rof + + // Final path: from last collision to end point + TVertices lpath = + this->GetPath( + this->m_Collisions[ fpath[ N - 2 ] ][ fpath[ N - 1 ] ].first, b + ); + for( long id = 0; id < lpath.size( ); ++id ) + vertices.push_back( lpath[ id ] ); } // fi } else { // Ignore common part: find common ancestor - auto aIt = pa.begin( ); - auto bIt = pb.begin( ); - while( *aIt == *bIt && aIt != pa.end( ) && bIt != pb.end( ) ) + long aIt = pa.size( ) - 1; + long bIt = pb.size( ) - 1; + bool cont = true; + while( aIt >= 0 && bIt >= 0 && cont ) { - ++aIt; - ++bIt; + cont = ( pa[ aIt ] == pb[ bIt ] ); + aIt--; + bIt--; } // elihw + aIt++; + bIt++; // Glue both parts - for( --aIt; aIt != pa.end( ); ++aIt ) - path.push_front( *aIt ); - for( ; bIt != pb.end( ); ++bIt ) - path.push_back( *bIt ); + for( long cIt = 0; cIt <= aIt; ++cIt ) + vertices.push_back( pa[ cIt ] ); + for( ; bIt >= 0; --bIt ) + vertices.push_back( pb[ bIt ] ); } // fi } // fi - return( path ); + return( vertices ); } // ------------------------------------------------------------------------- -template< class _TVertex, class _TSuperclass > -fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +template< class _TVertex, class _Superclass > +fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: MinimumSpanningTree( ) : Superclass( ) { } // ------------------------------------------------------------------------- -template< class _TVertex, class _TSuperclass > -fpa::Base::MinimumSpanningTree< _TVertex, _TSuperclass >:: +template< class _TVertex, class _Superclass > +fpa::Base::MinimumSpanningTree< _TVertex, _Superclass >:: ~MinimumSpanningTree( ) { }