1 #ifndef __fpa__Base__MinimumSpanningTree__hxx__
2 #define __fpa__Base__MinimumSpanningTree__hxx__
6 // -------------------------------------------------------------------------
7 template< class _TVertex, class _TPath, class _TSuperclass >
9 fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
11 fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
12 GetCollisions( ) const
14 return( this->m_Collisions );
17 // -------------------------------------------------------------------------
18 template< class _TVertex, class _TPath, class _TSuperclass >
19 void fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
20 SetCollisions( const TCollisions& collisions )
22 static const unsigned long _inf =
23 std::numeric_limits< unsigned long >::max( );
24 if( this->m_Collisions == collisions )
27 this->m_Collisions = collisions;
29 // Prepare a front graph
30 unsigned long N = this->m_Collisions.size( );
31 _TMatrix dist( N, _TRow( N, _inf ) );
32 this->m_FrontPaths = dist;
33 for( unsigned long i = 0; i < N; ++i )
35 for( unsigned long j = 0; j < N; ++j )
37 if( this->m_Collisions[ i ][ j ].second )
41 this->m_FrontPaths[ i ][ j ] = j;
42 this->m_FrontPaths[ j ][ i ] = i;
48 this->m_FrontPaths[ i ][ i ] = i;
52 // Use Floyd-Warshall to compute all possible paths between fronts
53 for( unsigned long k = 0; k < N; ++k )
55 for( unsigned long i = 0; i < N; ++i )
57 for( unsigned long j = 0; j < N; ++j )
59 // WARNING: you don't want a numeric overflow!!!
60 unsigned long dik = dist[ i ][ k ];
61 unsigned long dkj = dist[ k ][ j ];
62 unsigned long sum = _inf;
63 if( dik < _inf && dkj < _inf )
66 // Ok, continue Floyd-Warshall
67 if( sum < dist[ i ][ j ] )
70 this->m_FrontPaths[ i ][ j ] = this->m_FrontPaths[ i ][ k ];
82 // -------------------------------------------------------------------------
83 template< class _TVertex, class _TPath, class _TSuperclass >
84 void fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
87 this->m_Seeds.clear( );
91 // -------------------------------------------------------------------------
92 template< class _TVertex, class _TPath, class _TSuperclass >
93 void fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
94 AddSeed( const _TVertex& seed )
96 this->m_Seeds.push_back( seed );
100 // -------------------------------------------------------------------------
101 template< class _TVertex, class _TPath, class _TSuperclass >
102 void fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
103 GetPath( typename _TPath::Pointer& path, const _TVertex& a ) const
105 std::vector< _TVertex > vertices;
107 _TVertex p = this->GetParent( it );
110 vertices.push_back( it );
112 p = this->GetParent( it );
115 vertices.push_back( it );
117 path = _TPath::New( );
118 for( auto v = vertices.begin( ); v != vertices.end( ); ++v )
119 path->AddVertex( *v );
122 // -------------------------------------------------------------------------
123 template< class _TVertex, class _TPath, class _TSuperclass >
124 void fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
126 typename _TPath::Pointer& path, const _TVertex& a, const _TVertex& b
130 static const unsigned long _inf =
131 std::numeric_limits< unsigned long >::max( );
134 _TPath pa = this->GetPath( a );
135 _TPath pb = this->GetPath( b );
136 if( pa.size( ) > 0 && pb.size( ) > 0 )
138 // Find front identifiers
139 unsigned long ia = _inf, ib = _inf;
140 unsigned long N = this->m_Seeds.size( );
141 for( unsigned long i = 0; i < N; ++i )
143 if( this->m_Seeds[ i ] == pa.front( ) )
145 if( this->m_Seeds[ i ] == pb.front( ) )
152 // Use this->m_FrontPaths from Floyd-Warshall
153 if( this->m_FrontPaths[ ia ][ ib ] < _inf )
155 // Compute front path
156 std::vector< long > fpath;
157 fpath.push_back( ia );
160 ia = this->m_FrontPaths[ ia ][ ib ];
161 fpath.push_back( ia );
165 // Continue only if both fronts are connected
166 unsigned int N = fpath.size( );
169 // First path: from start vertex to first collision
170 path = this->GetPath(
171 a, this->m_Collisions[ fpath[ 0 ] ][ fpath[ 1 ] ].first
174 // Intermediary paths
175 for( unsigned int i = 1; i < N - 1; ++i )
179 this->m_Collisions[ fpath[ i ] ][ fpath[ i - 1 ] ].first,
180 this->m_Collisions[ fpath[ i ] ][ fpath[ i + 1 ] ].first
182 path.insert( path.end( ), ipath.begin( ), ipath.end( ) );
186 // Final path: from last collision to end point
189 this->m_Collisions[ fpath[ N - 1 ] ][ fpath[ N - 2 ] ].first, b
191 path.insert( path.end( ), lpath.begin( ), lpath.end( ) );
199 // Ignore common part: find common ancestor
200 auto aIt = pa.begin( );
201 auto bIt = pb.begin( );
202 while( *aIt == *bIt && aIt != pa.end( ) && bIt != pb.end( ) )
210 for( --aIt; aIt != pa.end( ); ++aIt )
211 path.push_front( *aIt );
212 for( ; bIt != pb.end( ); ++bIt )
213 path.push_back( *bIt );
222 // -------------------------------------------------------------------------
223 template< class _TVertex, class _TPath, class _TSuperclass >
224 fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
225 MinimumSpanningTree( )
230 // -------------------------------------------------------------------------
231 template< class _TVertex, class _TPath, class _TSuperclass >
232 fpa::Base::MinimumSpanningTree< _TVertex, _TPath, _TSuperclass >::
233 ~MinimumSpanningTree( )
237 #endif // __fpa__Base__MinimumSpanningTree__hxx__