]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/IO/MinimumSpanningTreeWriter.hxx
...
[FrontAlgorithms.git] / lib / fpa / IO / MinimumSpanningTreeWriter.hxx
index f9b8533b264691b3808dd598c7e848ea169513b2..d4b1f120b47c2d1d397988af75f946f49273e5d5 100644 (file)
@@ -2,40 +2,45 @@
 #define __FPA__IO__MINIMUMSPANNINGTREEWRITER__HXX__
 
 #include <fstream>
+#include <sstream>
 #include <itkMacro.h>
+#include <itkOffset.h>
+#include <itkImageRegionConstIteratorWithIndex.h>
 
 // -------------------------------------------------------------------------
-template< class T >
-void fpa::IO::MinimumSpanningTreeWriter< T >::
-SetInput( const T* input )
+template< class _TTree >
+void fpa::IO::MinimumSpanningTreeWriter< _TTree >::
+SetInput( const _TTree* input )
 {
-  this->itk::ProcessObject::SetNthInput( 0, const_cast< T* >( input ) );
+  this->itk::ProcessObject::SetNthInput( 0, const_cast< _TTree* >( input ) );
 }
 
 // -------------------------------------------------------------------------
-template< class T >
-T* fpa::IO::MinimumSpanningTreeWriter< T >::
+template< class _TTree >
+_TTree* fpa::IO::MinimumSpanningTreeWriter< _TTree >::
 GetInput( )
 {
-  return( dynamic_cast< T* >( this->itk::ProcessObject::GetInput( 0 ) ) );
+  return(
+    dynamic_cast< _TTree* >( this->itk::ProcessObject::GetInput( 0 ) )
+    );
 }
 
 // -------------------------------------------------------------------------
-template< class T >
-const T* fpa::IO::MinimumSpanningTreeWriter< T >::
+template< class _TTree >
+const _TTree* fpa::IO::MinimumSpanningTreeWriter< _TTree >::
 GetInput( ) const
 {
   return(
-    dynamic_cast< const T* >( this->itk::ProcessObject::GetInput( 0 ) )
+    dynamic_cast< const _TTree* >( this->itk::ProcessObject::GetInput( 0 ) )
     );
 }
 
 // -------------------------------------------------------------------------
-template< class T >
-void fpa::IO::MinimumSpanningTreeWriter< T >::
+template< class _TTree >
+void fpa::IO::MinimumSpanningTreeWriter< _TTree >::
 Update( )
 {
-  T* input = this->GetInput( );
+  _TTree* input = this->GetInput( );
   if( input != NULL )
   {
     input->UpdateOutputInformation( );
@@ -47,8 +52,8 @@ Update( )
 }
 
 // -------------------------------------------------------------------------
-template< class T >
-fpa::IO::MinimumSpanningTreeWriter< T >::
+template< class _TTree >
+fpa::IO::MinimumSpanningTreeWriter< _TTree >::
 MinimumSpanningTreeWriter( )
   : Superclass( ),
     m_FileName( "" )
@@ -57,60 +62,120 @@ MinimumSpanningTreeWriter( )
 }
 
 // -------------------------------------------------------------------------
-template< class T >
-fpa::IO::MinimumSpanningTreeWriter< T >::
+template< class _TTree >
+fpa::IO::MinimumSpanningTreeWriter< _TTree >::
 ~MinimumSpanningTreeWriter( )
 {
 }
 
 // -------------------------------------------------------------------------
-template< class T >
-void fpa::IO::MinimumSpanningTreeWriter< T >::
+template< class _TTree >
+void fpa::IO::MinimumSpanningTreeWriter< _TTree >::
 GenerateData( )
 {
-  const T* input = this->GetInput( );
-
-  std::ofstream out( this->m_FileName.c_str( ) );
-  if( !out )
+  typedef itk::Offset< _TTree::ImageDimension > _TOffset;
+  _TOffset zero_off;
+  zero_off.Fill( 0 );
+  const _TTree* input = this->GetInput( );
+
+  // Create buffer
+  std::stringstream out;
+
+  // Get tree properties
+  unsigned int dim = _TTree::ImageDimension;
+  auto lr = input->GetLargestPossibleRegion( );
+  auto rr = input->GetRequestedRegion( );
+  auto br = input->GetBufferedRegion( );
+  auto ori = input->GetOrigin( );
+  auto spc = input->GetSpacing( );
+  auto dir = input->GetDirection( );
+
+  // Write tree dimension
+  out << dim << std::endl;
+  out << ( ( input->GetFillNodeQueue( ) )? 1: 0 ) << std::endl;
+
+  // Write tree regions
+  out << lr.GetIndex( )[ 0 ] << " " << lr.GetSize( )[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    out << " " << lr.GetIndex( )[ d ] << " " << lr.GetSize( )[ d ];
+  out << std::endl;
+  out << rr.GetIndex( )[ 0 ] << " " << rr.GetSize( )[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    out << " " << rr.GetIndex( )[ d ] << " " << rr.GetSize( )[ d ];
+  out << std::endl;
+  out << br.GetIndex( )[ 0 ] << " " << br.GetSize( )[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    out << " " << br.GetIndex( )[ d ] << " " << br.GetSize( )[ d ];
+  out << std::endl;
+
+  // Write spatial properties
+  out << ori[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    out << " " << ori[ d ];
+  out << std::endl;
+  out << spc[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    out << " " << spc[ d ];
+  out << std::endl;
+  out << dir[ 0 ][ 0 ];
+  for( unsigned int d = 0; d < dim; ++d )
+    for( unsigned int e = 0; e < dim; ++e )
+      if( d != 0 || e != 0 )
+        out << " " << dir[ d ][ e ];
+  out << std::endl;
+
+  // Write collisions
+  auto& coll = input->GetCollisions( );
+  out << coll.size( ) << std::endl;
+  for( unsigned long i = 0; i < coll.size( ); ++i )
   {
-    itkExceptionMacro(
-      << "Error opening file to write a minimum spanning tree: \""
-      << this->m_FileName
-      << "\""
-      );
-    return;
+    out << coll[ i ].size( );
+    for( unsigned long j = 0; j < coll[ i ].size( ); ++j )
+    {
+      for( unsigned int d = 0; d < dim; ++d )
+        out << " " << coll[ i ][ j ].first[ d ];
+      out << " " << coll[ i ][ j ].second << std::endl;
 
-  } // fi
+    } // rof
 
-  out << T::TVertex::Dimension << std::endl;
+  } // rof
 
-  const typename T::TCollisions& collisions = input->GetCollisions( );
-  unsigned long nSeeds = collisions.size( );
-  out << nSeeds << std::endl;
-  for( unsigned long i = 0; i < nSeeds; ++i )
+  // Write vertices
+  itk::ImageRegionConstIteratorWithIndex< _TTree > vIt( input, rr );
+  for( vIt.GoToBegin( ); !vIt.IsAtEnd( ); ++vIt )
   {
-    for( unsigned long j = 0; j < nSeeds; ++j )
-      out << collisions[ i ][ j ].second << " ";
-    out << std::endl;
+    if(
+      vIt.Get( ).Parent != zero_off ||
+      vIt.Get( ).GlobalCost > double( 0 )
+      )
+    {
+      out << vIt.Get( ).FrontId << " " << vIt.Get( ).GlobalCost;
+      for( unsigned int d = 0; d < dim; ++d )
+        out << " " << vIt.GetIndex( )[ d ];
+      for( unsigned int d = 0; d < dim; ++d )
+        out << " " << vIt.Get( ).Parent[ d ];
+      out << std::endl;
+
+    } // fi
 
   } // rof
 
-  const typename T::TDecorated& real_input = input->Get( );
-  out << real_input.size( ) << std::endl;
-  for(
-    typename T::TDecorated::const_iterator iIt = real_input.begin( );
-    iIt != real_input.end( );
-    ++iIt
-    )
+  // Real file write
+  std::ofstream file(
+    this->m_FileName.c_str( ), std::ios::binary | std::ios::trunc
+    );
+  if( !file )
   {
-    for( unsigned int d = 0; d < T::TVertex::Dimension; ++d )
-      out << iIt->first[ d ] << " ";
-    for( unsigned int d = 0; d < T::TVertex::Dimension; ++d )
-      out << iIt->second.first[ d ] << " ";
-    out << iIt->second.second << std::endl;
+    itkExceptionMacro(
+      << "Error opening file to write a minimum spanning tree: \""
+      << this->m_FileName
+      << "\""
+      );
+    return;
 
-  } // rof
-  out.close( );
+  } // fi
+  file.write( out.str( ).c_str( ), out.str( ).size( ) );
+  file.close( );
 }
 
 #endif // __FPA__IO__MINIMUMSPANNINGTREEWRITER__HXX__