]> Creatis software - FrontAlgorithms.git/commitdiff
Even more tests
authorLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Tue, 17 Mar 2015 23:59:50 +0000 (18:59 -0500)
committerLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Tue, 17 Mar 2015 23:59:50 +0000 (18:59 -0500)
appli/examples/CMakeLists.txt
appli/examples/example_ImageAlgorithmDijkstra_03.cxx
lib/fpa/Image/DijkstraWithSphereBacktracking.h
lib/fpa/Image/DijkstraWithSphereBacktracking.hxx
lib/fpa/VTK/ImageMPR.cxx
lib/fpa/VTK/ImageMPR.h

index 2d0ebe63e8ac5ba7e7296c7299c37a22fbc9a71c..93d3c1dd04be86b0644f8eaa2dcc790f0536b615 100644 (file)
@@ -27,7 +27,11 @@ IF(BUILD_EXAMPLES)
 
     FOREACH(APP ${vtk_APPLIS})
       ADD_EXECUTABLE(${APP} ${APP}.cxx)
-      TARGET_LINK_LIBRARIES(${APP} FrontAlgorithms)
+      TARGET_LINK_LIBRARIES(
+        ${APP}
+        FrontAlgorithms
+        ${VTK_LIBRARIES} vtkIOLegacy
+        )
     ENDFOREACH(APP)
   ENDIF(USE_VTK)
 ENDIF(BUILD_EXAMPLES)
index 0de81262791453d8f802238ce15b6ebbb3e37385..613378b2c1e8db3a6aa130b99334e24a9ce9fce9 100644 (file)
 #include <vtkCellArray.h>
 #include <vtkFloatArray.h>
 #include <vtkPolyData.h>
+#include <vtkPolyDataWriter.h>
 #include <vtkSmartPointer.h>
 #include <vtkImageMarchingCubes.h>
+#include <vtkLookupTable.h>
 
 #include <fpa/Image/DijkstraWithSphereBacktracking.h>
 #include <fpa/VTK/ImageMPR.h>
@@ -138,19 +140,22 @@ int main( int argc, char* argv[] )
   vtkSmartPointer< vtkFloatArray > scalars =
     vtkSmartPointer< vtkFloatArray >::New( );
 
+  const TDijkstra::TMarkImage* marks = paths->GetOutputMarkImage( );
+  TDijkstra::TMark max_mark = paths->GetNumberOfBranches( );
   const TDijkstra::TVertices& endpoints = paths->GetEndPoints( );
   const TDijkstra::TTree& tree = paths->GetFinalTree( );
   TDijkstra::TVertices::const_iterator epIt = endpoints.begin( );
   for( unsigned int epId = 0; epIt != endpoints.end( ); ++epIt, ++epId )
   {
-    double pd = double( epId ) / double( endpoints.size( ) - 1 );
-
     TDijkstra::TVertex idx = *epIt;
     do
     {
       TImage::PointType pnt;
       input_image->TransformIndexToPhysicalPoint( idx, pnt );
 
+      TDijkstra::TMark mark = marks->GetPixel( idx );
+      double pd = double( mark );
+
       points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
       scalars->InsertNextTuple1( pd );
       if( idx != *epIt )
@@ -172,10 +177,28 @@ int main( int argc, char* argv[] )
   vtk_tree->SetLines( cells );
   vtk_tree->GetPointData( )->SetScalars( scalars );
 
-  view.AddPolyData( vtk_tree );
+  vtkSmartPointer<vtkLookupTable> lut =
+    vtkSmartPointer<vtkLookupTable>::New( );
+  lut->SetNumberOfTableValues( max_mark + 1 );
+  lut->SetTableRange( 0, max_mark );
+  lut->Build( );
+
+  view.AddPolyData( vtk_tree, lut );
   view.Render( );
   view.Start( );
 
+  vtkSmartPointer< vtkPolyDataWriter > writer =
+    vtkSmartPointer< vtkPolyDataWriter >::New( );
+  writer->SetInputData( vtk_tree );
+  writer->SetFileName( output_image_fn.c_str( ) );
+  writer->Update( );
+
+  itk::ImageFileWriter< TDijkstra::TMarkImage >::Pointer marks_writer =
+    itk::ImageFileWriter< TDijkstra::TMarkImage >::New( );
+  marks_writer->SetInput( marks );
+  marks_writer->SetFileName( "marks.mhd" );
+  marks_writer->Update( );
+
   /* TODO
      TImageWriter::Pointer dijkstra_writer =
      TImageWriter::New( );
index 070a01a2706fb8f85189274307baa7f769a45e6b..febfcc9f134d7e82eea42dbf2dabd92c01c819b5 100644 (file)
@@ -31,6 +31,10 @@ namespace fpa
       typedef typename Superclass::TTraits::TVertexCmp TVertexCmp;
       typedef std::map< TVertex, TVertex, TVertexCmp > TTree;
 
+      typedef unsigned short                         TMark;
+      typedef itk::Image< TMark, I::ImageDimension > TMarkImage;
+
+
       typedef typename Superclass::TEndEvent             TEndEvent;
       typedef typename Superclass::TBacktrackingEvent    TBacktrackingEvent;
       typedef typename Superclass::TEndBacktrackingEvent TEndBacktrackingEvent;
@@ -51,6 +55,11 @@ namespace fpa
       itkGetConstMacro( FinalTree, TTree );
       itkGetConstMacro( EndPoints, TVertices );
       itkGetConstMacro( BifurcationPoints, TVertices );
+      itkGetConstMacro( NumberOfBranches, TMark );
+
+    public:
+      TMarkImage* GetOutputMarkImage( );
+      const TMarkImage* GetOutputMarkImage( ) const;
 
     protected:
       DijkstraWithSphereBacktracking( );
@@ -72,6 +81,7 @@ namespace fpa
       TTree        m_FinalTree;
       TVertices    m_BifurcationPoints;
       TVertices    m_EndPoints;
+      TMark        m_NumberOfBranches;
     };
 
   } // ecapseman
index 95f8dcb0ac90901ac9026dba88cc7d37e28d5087..346103ba9df5e722649fdef634e497c8e48908e2 100644 (file)
@@ -5,12 +5,42 @@
 #include <itkImageRegionConstIteratorWithIndex.h>
 #include <itkImageRegionIteratorWithIndex.h>
 
+// -------------------------------------------------------------------------
+template< class I, class C >
+typename fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+TMarkImage* fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+GetOutputMarkImage( )
+{
+  return(
+    dynamic_cast< TMarkImage* >(
+      this->itk::ProcessObject::GetOutput( 1 )
+      )
+    );
+}
+
+// -------------------------------------------------------------------------
+template< class I, class C >
+const typename fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+TMarkImage* fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+GetOutputMarkImage( ) const
+{
+  return(
+    dynamic_cast< const TMarkImage* >(
+      this->itk::ProcessObject::GetOutput( 1 )
+      )
+    );
+}
+
 // -------------------------------------------------------------------------
 template< class I, class C >
 fpa::Image::DijkstraWithSphereBacktracking< I, C >::
 DijkstraWithSphereBacktracking( )
-  : Superclass( )
+  : Superclass( ),
+    m_NumberOfBranches( 0 )
 {
+  this->SetNumberOfRequiredOutputs( 2 );
+  this->SetNthOutput( 0, I::New( ) );
+  this->SetNthOutput( 1, TMarkImage::New( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -67,8 +97,6 @@ template< class I, class C >
 void fpa::Image::DijkstraWithSphereBacktracking< I, C >::
 _AfterMainLoop( )
 {
-  typedef itk::Image< bool, I::ImageDimension > _TMarkImage;
-
   // Finish base algorithm
   this->Superclass::_AfterMainLoop( );
   this->m_FinalTree.clear( );
@@ -88,25 +116,19 @@ _AfterMainLoop( )
   max_spac *= double( 3 );
 
   // Prepare an object to hold marks
-  typename _TMarkImage::Pointer marks = _TMarkImage::New( );
-  marks->SetLargestPossibleRegion( input->GetLargestPossibleRegion( ) );
-  marks->SetRequestedRegion( input->GetRequestedRegion( ) );
-  marks->SetBufferedRegion( input->GetBufferedRegion( ) );
-  marks->SetOrigin( input->GetOrigin( ) );
-  marks->SetSpacing( input->GetSpacing( ) );
-  marks->SetDirection( input->GetDirection( ) );
-  marks->Allocate( );
-  marks->FillBuffer( false );
+  typename TMarkImage::Pointer marks = this->GetOutputMarkImage( );
+  marks->FillBuffer( 0 );
 
   // Iterate over the candidates, starting from the candidates that
   // are near thin branches
   typename _TCandidates::const_reverse_iterator cIt =
     this->m_Candidates.rbegin( );
-  for( int backId = 0; cIt != this->m_Candidates.rend( ); ++cIt )
+  this->m_NumberOfBranches = 0;
+  for( ; cIt != this->m_Candidates.rend( ); ++cIt )
   {
     // If pixel hasn't been visited, continue
     TVertex v = cIt->second;
-    if( marks->GetPixel( v ) )
+    if( marks->GetPixel( v ) != 0 )
       continue;
 
     // Compute nearest start candidate
@@ -128,46 +150,81 @@ _AfterMainLoop( )
     } // rof
 
     // Keep endpoint
-    if( marks->GetPixel( max_vertex ) )
+    if( marks->GetPixel( max_vertex ) != 0 )
       continue;
-    backId++;
+    this->m_NumberOfBranches++;
     this->m_EndPoints.push_back( max_vertex );
 
     // Construct path (at least the part that hasn't been iterated)
-    bool terminate = false;
     do
     {
-      if( this->m_FinalTree.find( max_vertex ) == this->m_FinalTree.end( ) )
+      if(
+        std::find(
+          this->m_BifurcationPoints.begin( ),
+          this->m_BifurcationPoints.end( ),
+          max_vertex
+          ) == this->m_BifurcationPoints.end( )
+        )
       {
         // Mark a sphere around current point as visited
         double dist = std::sqrt( double( input->GetPixel( max_vertex ) ) );
-        region = this->_Region( max_vertex, dist * double( 1.25 ) );
-        itk::ImageRegionIteratorWithIndex< _TMarkImage >
+        region = this->_Region( max_vertex, dist * double( 1.1 ) );
+        itk::ImageRegionIteratorWithIndex< TMarkImage >
           mIt( marks, region );
         for( mIt.GoToBegin( ); !mIt.IsAtEnd( ); ++mIt )
-          mIt.Set( true );
+          mIt.Set( this->m_NumberOfBranches );
 
         // Next vertex in current path
-        this->InvokeEvent( TBacktrackingEvent( max_vertex, backId ) );
+        this->InvokeEvent( TBacktrackingEvent( max_vertex, this->m_NumberOfBranches ) );
         this->m_FinalTree[ max_vertex ] = this->_Parent( max_vertex );
       }
       else
       {
         // A bifurcation point has been reached!
         this->m_BifurcationPoints.push_back( max_vertex );
-        terminate = true;
+        this->m_NumberOfBranches++;
 
       } // fi
       max_vertex = this->_Parent( max_vertex );
 
-    } while( max_vertex != this->_Parent( max_vertex ) && !terminate );
-
-    if( !terminate )
-    {
-      this->m_FinalTree[ max_vertex ] = max_vertex;
-      this->InvokeEvent( TEndBacktrackingEvent( backId ) );
-
-    } // fi
+    } while( max_vertex != this->_Parent( max_vertex ) );
+
+    /* TODO
+       bool terminate = false;
+       do
+       {
+       if( this->m_FinalTree.find( max_vertex ) == this->m_FinalTree.end( ) )
+       {
+       // Mark a sphere around current point as visited
+       double dist = std::sqrt( double( input->GetPixel( max_vertex ) ) );
+       region = this->_Region( max_vertex, dist * double( 1.25 ) );
+       itk::ImageRegionIteratorWithIndex< TMarkImage >
+       mIt( marks, region );
+       for( mIt.GoToBegin( ); !mIt.IsAtEnd( ); ++mIt )
+       mIt.Set( true );
+
+       // Next vertex in current path
+       this->InvokeEvent( TBacktrackingEvent( max_vertex, this->m_NumberOfBranches ) );
+       this->m_FinalTree[ max_vertex ] = this->_Parent( max_vertex );
+       }
+       else
+       {
+       // A bifurcation point has been reached!
+       this->m_BifurcationPoints.push_back( max_vertex );
+       terminate = true;
+
+       } // fi
+       max_vertex = this->_Parent( max_vertex );
+
+       } while( max_vertex != this->_Parent( max_vertex ) && !terminate );
+
+       if( !terminate )
+       {
+       this->m_FinalTree[ max_vertex ] = max_vertex;
+       this->InvokeEvent( TEndBacktrackingEvent( this->m_NumberOfBranches ) );
+
+       } // fi
+    */
 
   } // rof
 }
index 8484a03cad504b948687ca9e6d4b5d610d8e241a..9acfa12c02cd16d0e901b997e90bb56d14abdc30 100644 (file)
@@ -255,6 +255,24 @@ AddPolyData( vtkPolyData* pd, double r, double g, double b, double opacity )
   this->m_Renderer->AddActor( this->m_Actors[ i ] );
 }
 
+// -------------------------------------------------------------------------
+void fpa::VTK::ImageMPR::
+AddPolyData( vtkPolyData* pd, vtkLookupTable* lut, double opacity )
+{
+  unsigned int i = this->m_PolyDatas.size( );
+
+  this->m_PolyDatas.push_back( pd );
+  this->m_Mappers.push_back( vtkSmartPointer< vtkPolyDataMapper >::New( ) );
+  this->m_Actors.push_back( vtkSmartPointer< vtkActor >::New( ) );
+
+  this->m_Mappers[ i ]->SetInputData( pd );
+  this->m_Mappers[ i ]->SetLookupTable( lut );
+  this->m_Actors[ i ]->SetMapper( this->m_Mappers[ i ] );
+  this->m_Actors[ i ]->GetProperty( )->SetOpacity( opacity );
+  this->m_Renderer->AddActor( this->m_Actors[ i ] );
+}
+
+
 // -------------------------------------------------------------------------
 unsigned int fpa::VTK::ImageMPR::
 GetNumberOfSeeds( ) const
index 21a1c5cd0238effb16b5b1656f0eeb60eb9c30ca..e64ff8aa799637111d64e98796f685af26d2af21 100644 (file)
@@ -20,6 +20,7 @@
 #include <vtkSeedWidget.h>
 #include <vtkSmartPointer.h>
 #include <vtkSphereSource.h>
+#include <vtkLookupTable.h>
 
 namespace fpa
 {
@@ -42,6 +43,11 @@ namespace fpa
         vtkPolyData* pd,
         double r, double g, double b, double opacity = double( 1 )
         );
+      void AddPolyData(
+        vtkPolyData* pd,
+        vtkLookupTable* lut,
+        double opacity = double( 1 )
+        );
 
       unsigned int GetNumberOfSeeds( ) const;
       void GetSeed( int n, double* s ) const;