]> Creatis software - FrontAlgorithms.git/blobdiff - appli/examples/example_Image_Dijkstra_EndPointDetection.cxx
I/O classes added
[FrontAlgorithms.git] / appli / examples / example_Image_Dijkstra_EndPointDetection.cxx
index b2d3e7c6c8c21529126619648a152a2f99e88176..a334de3ae881a372578e7e999cc01d9315695d6f 100644 (file)
 #include <fpa/VTK/ImageMPR.h>
 #include <fpa/VTK/Image3DObserver.h>
 
+#include <fpa/IO/MinimumSpanningTreeWriter.h>
+#include <fpa/IO/UniqueValuesContainerWriter.h>
+#include <fpa/IO/MatrixValuesContainerWriter.h>
+
 #include <vtkCellArray.h>
+#include <vtkFloatArray.h>
 #include <vtkImageMarchingCubes.h>
 #include <vtkPoints.h>
 #include <vtkPolyData.h>
@@ -50,11 +55,18 @@ void DistanceMap(
 // -------------------------------------------------------------------------
 int main( int argc, char* argv[] )
 {
-  if( argc < 5 )
+  if( argc < 9 )
   {
     std::cerr
-      << "Usage: " << argv[ 0 ]
-      << " input_image distancemap output_costmap output_labels"
+      << "Usage: " << argv[ 0 ] << std::endl
+      << " input_image" << std::endl
+      << " output_distancemap" << std::endl
+      << " output_costmap" << std::endl
+      << " output_labels" << std::endl
+      << " output_minimum_spanning_tree" << std::endl
+      << " output_endpoints" << std::endl
+      << " output_bifurcations" << std::endl
+      << " output_branches" << std::endl
       << std::endl;
     return( 1 );
 
@@ -63,6 +75,10 @@ int main( int argc, char* argv[] )
   std::string distancemap_fn = argv[ 2 ];
   std::string output_costmap_fn = argv[ 3 ];
   std::string output_labels_fn = argv[ 4 ];
+  std::string mst_output_fn = argv[ 5 ];
+  std::string endpoints_output_fn = argv[ 6 ];
+  std::string bifurcations_output_fn = argv[ 7 ];
+  std::string branches_output_fn = argv[ 8 ];
 
   // Read image
   TImage::Pointer input_image;
@@ -147,142 +163,183 @@ int main( int argc, char* argv[] )
     << std::difftime( end, start )
     << " s." << std::endl;
 
-  // Minimum spanning tree
-  const TFilter::TMinimumSpanningTree* mst =
-    filter->GetMinimumSpanningTree( );
+  // Outputs
+  const TFilter::TOutputImage* accumulated_costs = filter->GetOutput( );
+  const TFilter::TLabelImage* labeled_image = filter->GetLabelImage( );
+  const TFilter::TMinimumSpanningTree* mst = filter->GetMinimumSpanningTree( );
+  const TFilter::TUniqueVertices* endpoints = filter->GetEndPoints( );
+  const TFilter::TUniqueVertices* bifurcations = filter->GetBifurcations( );
+  const TFilter::TBranches* branches = filter->GetBranches( );
+  unsigned long nBranches = filter->GetNumberOfBranches( );
 
-  // Build branches from endpoints to seed
+  // Save outputs
+  SaveImage( dmap.GetPointer( ), distancemap_fn );
+  SaveImage( accumulated_costs, output_costmap_fn );
+  SaveImage( labeled_image, output_labels_fn );
+
+  fpa::IO::MinimumSpanningTreeWriter< TFilter::TMinimumSpanningTree >::Pointer
+    mst_writer =
+    fpa::IO::MinimumSpanningTreeWriter< TFilter::TMinimumSpanningTree >::New( );
+  mst_writer->SetInput( mst );
+  mst_writer->SetFileName( mst_output_fn );
+  mst_writer->Update( );
+
+  fpa::IO::UniqueValuesContainerWriter< TFilter::TUniqueVertices >::Pointer
+    endpoints_writer =
+    fpa::IO::UniqueValuesContainerWriter< TFilter::TUniqueVertices >::New( );
+  endpoints_writer->SetInput( endpoints );
+  endpoints_writer->SetFileName( endpoints_output_fn );
+  endpoints_writer->Update( );
+
+  fpa::IO::UniqueValuesContainerWriter< TFilter::TUniqueVertices >::Pointer
+    bifurcations_writer =
+    fpa::IO::UniqueValuesContainerWriter< TFilter::TUniqueVertices >::New( );
+  bifurcations_writer->SetInput( bifurcations );
+  bifurcations_writer->SetFileName( bifurcations_output_fn );
+  bifurcations_writer->Update( );
+
+  fpa::IO::MatrixValuesContainerWriter< TFilter::TBranches >::Pointer
+    branches_writer =
+    fpa::IO::MatrixValuesContainerWriter< TFilter::TBranches >::New( );
+  branches_writer->SetInput( branches );
+  branches_writer->SetFileName( branches_output_fn );
+  branches_writer->Update( );
+
+  // Show endpoints
   vtkSmartPointer< vtkPoints > endpoints_points =
     vtkSmartPointer< vtkPoints >::New( );
   vtkSmartPointer< vtkCellArray > endpoints_cells =
     vtkSmartPointer< vtkCellArray >::New( );
-  vtkSmartPointer< vtkCellArray > endpoints_vertices =
-    vtkSmartPointer< vtkCellArray >::New( );
-
-  const TFilter::TUniqueVertices& endpoints = filter->GetEndPoints( );
-  TFilter::TUniqueVertices::const_iterator eIt = endpoints.begin( );
-  for( ; eIt != endpoints.end( ); ++eIt )
+  for(
+    TFilter::TUniqueVertices::ConstIterator epIt = endpoints->Begin( );
+    epIt != endpoints->End( );
+    ++epIt
+    )
   {
-    TFilter::TVertices path;
-    mst->GetPath( path, *eIt, filter->GetSeed( 0 ) );
-
-    std::cout << *eIt << std::endl;
-    TFilter::TVertices::const_iterator pIt = path.begin( );
-    for( ; pIt != path.end( ); ++pIt )
-    {
-      TImage::PointType pnt;
-      input_image->TransformIndexToPhysicalPoint( *pIt, pnt );
-      endpoints_points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
-
-      if( pIt != path.begin( ) )
-      {
-        endpoints_cells->InsertNextCell( 2 );
-        endpoints_cells->InsertCellPoint( endpoints_points->GetNumberOfPoints( ) - 2 );
-        endpoints_cells->InsertCellPoint( endpoints_points->GetNumberOfPoints( ) - 1 );
-      }
-      else
-      {
-        std::cout << "ok" << std::endl;
-        endpoints_vertices->InsertNextCell( 1 );
-        endpoints_vertices->InsertCellPoint( endpoints_points->GetNumberOfPoints( ) - 1 );
-
-      } // fi
-
-
-
-
-    } // rof
+    TImage::PointType pnt;
+    input_image->TransformIndexToPhysicalPoint( *epIt, pnt );
+    endpoints_points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
+    endpoints_cells->InsertNextCell( 1 );
+    endpoints_cells->
+      InsertCellPoint( endpoints_points->GetNumberOfPoints( ) - 1 );
 
   } // rof
-  std::cout << endpoints.size( ) << std::endl;
-      
   vtkSmartPointer< vtkPolyData > endpoints_polydata =
     vtkSmartPointer< vtkPolyData >::New( );
   endpoints_polydata->SetPoints( endpoints_points );
-  endpoints_polydata->SetLines( endpoints_cells );
-  endpoints_polydata->SetVerts( endpoints_vertices );
-
-
-
-
-
-
-
-
+  endpoints_polydata->SetVerts( endpoints_cells );
+  view.AddPolyData( endpoints_polydata, 0, 0, 1, 1 );
 
-
-
-  // Bifurcations
+  // Show bifurcations
   vtkSmartPointer< vtkPoints > bifurcations_points =
     vtkSmartPointer< vtkPoints >::New( );
-  vtkSmartPointer< vtkCellArray > bifurcations_vertices =
+  vtkSmartPointer< vtkCellArray > bifurcations_cells =
     vtkSmartPointer< vtkCellArray >::New( );
-
-  const TFilter::TUniqueVertices& bifurcations = filter->GetBifurcationPoints( );
-  TFilter::TUniqueVertices::const_iterator bIt = bifurcations.begin( );
-  for( ; bIt != bifurcations.end( ); ++bIt )
+  for(
+    TFilter::TUniqueVertices::ConstIterator bfIt = bifurcations->Begin( );
+    bfIt != bifurcations->End( );
+    ++bfIt
+    )
   {
-    std::cout << *bIt << std::endl;
-
     TImage::PointType pnt;
-    input_image->TransformIndexToPhysicalPoint( *bIt, pnt );
+    input_image->TransformIndexToPhysicalPoint( *bfIt, pnt );
     bifurcations_points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
-    bifurcations_vertices->InsertNextCell( 1 );
-    bifurcations_vertices->InsertCellPoint( bifurcations_points->GetNumberOfPoints( ) - 1 );
+    bifurcations_cells->InsertNextCell( 1 );
+    bifurcations_cells->
+      InsertCellPoint( bifurcations_points->GetNumberOfPoints( ) - 1 );
 
   } // rof
-
   vtkSmartPointer< vtkPolyData > bifurcations_polydata =
     vtkSmartPointer< vtkPolyData >::New( );
   bifurcations_polydata->SetPoints( bifurcations_points );
-  bifurcations_polydata->SetVerts( bifurcations_vertices );
-
-
-
+  bifurcations_polydata->SetVerts( bifurcations_cells );
+  view.AddPolyData( bifurcations_polydata, 0, 1, 0, 1 );
 
+  // Show branches (simple and detailed)
+  vtkSmartPointer< vtkPoints > simple_branches_points =
+    vtkSmartPointer< vtkPoints >::New( );
+  vtkSmartPointer< vtkCellArray > simple_branches_cells =
+    vtkSmartPointer< vtkCellArray >::New( );
 
-  // Build branches from endpoints to seed
-  vtkSmartPointer< vtkPoints > branches_points =
+  vtkSmartPointer< vtkPoints > detailed_branches_points =
     vtkSmartPointer< vtkPoints >::New( );
-  vtkSmartPointer< vtkCellArray > branches_cells =
+  vtkSmartPointer< vtkCellArray > detailed_branches_cells =
     vtkSmartPointer< vtkCellArray >::New( );
+  vtkSmartPointer< vtkFloatArray > detailed_branches_scalars =
+    vtkSmartPointer< vtkFloatArray >::New( );
 
-  const TFilter::TBranches& branches = filter->GetBranches( );
-  TFilter::TBranches::const_iterator brIt = branches.begin( );
-  for( ; brIt != branches.end( ); ++brIt )
+  TFilter::TBranches::ConstIterator brIt = branches->Begin( );
+  for( ; brIt != branches->End( ); ++brIt )
   {
-    TFilter::TBranch::const_iterator brsIt = brIt->second.begin( );
-    for( ; brsIt != brIt->second.end( ); ++brsIt )
+    // Branch's first point
+    TImage::PointType first_point;
+    input_image->TransformIndexToPhysicalPoint( brIt->first, first_point );
+    unsigned long first_id = simple_branches_points->GetNumberOfPoints( );
+    simple_branches_points->InsertNextPoint(
+      first_point[ 0 ], first_point[ 1 ], first_point[ 2 ]
+      );
+
+    TFilter::TBranches::ConstRowIterator brRowIt = branches->Begin( brIt );
+    for( ; brRowIt != branches->End( brIt ); ++brRowIt )
     {
-      TImage::PointType pnt0, pnt1;
-      input_image->TransformIndexToPhysicalPoint( brIt->first, pnt0 );
-      input_image->TransformIndexToPhysicalPoint( brsIt->first, pnt1 );
-      branches_points->InsertNextPoint( pnt0[ 0 ], pnt0[ 1 ], pnt0[ 2 ] );
-      branches_points->InsertNextPoint( pnt1[ 0 ], pnt1[ 1 ], pnt1[ 2 ] );
-
-      branches_cells->InsertNextCell( 2 );
-      branches_cells->InsertCellPoint( branches_points->GetNumberOfPoints( ) - 2 );
-      branches_cells->InsertCellPoint( branches_points->GetNumberOfPoints( ) - 1 );
+      // Branch's second point
+      TImage::PointType second_point;
+      input_image->
+        TransformIndexToPhysicalPoint( brRowIt->first, second_point );
+      unsigned long second_id = simple_branches_points->GetNumberOfPoints( );
+      simple_branches_points->InsertNextPoint(
+        second_point[ 0 ], second_point[ 1 ], second_point[ 2 ]
+        );
+      simple_branches_cells->InsertNextCell( 2 );
+      simple_branches_cells->InsertCellPoint( first_id );
+      simple_branches_cells->InsertCellPoint( second_id );
+
+      // Detailed path
+      double pathId = double( brRowIt->second - 1 ) / double( nBranches - 1 );
+      TFilter::TVertices path;
+      mst->GetPath( path, brIt->first, brRowIt->first );
+      TFilter::TVertices::const_iterator pIt = path.begin( );
+      for( ; pIt != path.end( ); ++pIt )
+      {
+        TImage::PointType path_point;
+        input_image->TransformIndexToPhysicalPoint( *pIt, path_point );
+        detailed_branches_points->InsertNextPoint(
+          path_point[ 0 ], path_point[ 1 ], path_point[ 2 ]
+          );
+        detailed_branches_scalars->InsertNextTuple1( pathId );
+        if( pIt != path.begin( ) )
+        {
+          unsigned long nPoints =
+            detailed_branches_points->GetNumberOfPoints( );
+          detailed_branches_cells->InsertNextCell( 2 );
+          detailed_branches_cells->InsertCellPoint( nPoints - 2 );
+          detailed_branches_cells->InsertCellPoint( nPoints - 1 );
+
+        } // fi
+
+      } // rof
 
     } // rof
 
   } // rof
+  vtkSmartPointer< vtkPolyData > simple_branches_polydata =
+    vtkSmartPointer< vtkPolyData >::New( );
+  simple_branches_polydata->SetPoints( simple_branches_points );
+  simple_branches_polydata->SetLines( simple_branches_cells );
+  view.AddPolyData( simple_branches_polydata, 1, 0, 1, 1 );
 
-  vtkSmartPointer< vtkPolyData > branches_polydata =
+  vtkSmartPointer< vtkPolyData > detailed_branches_polydata =
     vtkSmartPointer< vtkPolyData >::New( );
-  branches_polydata->SetPoints( branches_points );
-  branches_polydata->SetLines( branches_cells );
+  detailed_branches_polydata->SetPoints( detailed_branches_points );
+  detailed_branches_polydata->SetLines( detailed_branches_cells );
+  detailed_branches_polydata->
+    GetPointData( )->SetScalars( detailed_branches_scalars );
+  view.AddPolyData( detailed_branches_polydata, 1 );
 
-  view.AddPolyData( endpoints_polydata, 1, 0, 0, 1 );
-  view.AddPolyData( bifurcations_polydata, 0, 0, 1, 1 );
-  view.AddPolyData( branches_polydata, 0, 1, 0, 1 );
+  // Let some more interaction
   view.Render( );
   view.Start( );
 
-  // Save final total cost map
-  SaveImage( filter->GetOutput( ), output_costmap_fn );
-  SaveImage( dmap.GetPointer( ), distancemap_fn );
-  SaveImage( filter->GetLabelImage( ), output_labels_fn );
   return( 0 );
 }
 
@@ -362,13 +419,3 @@ void DistanceMap(
 }
 
 // eof - $RCSfile$
-
-
-
-
-
-
-
-
-
-