+#include "MinimumSpanningTreeToMesh.h"
+
+#include <cpPlugins/Interface/Image.h>
+#include <cpPlugins/Interface/Mesh.h>
+#include <cpPlugins/Interface/PointList.h>
+#include <fpaPlugins/MinimumSpanningTree.h>
+#include <fpa/Base/MinimumSpanningTree.h>
+
+#include <vtkCellArray.h>
+#include <vtkPoints.h>
+#include <vtkPolyData.h>
+
+// -------------------------------------------------------------------------
+fpaPlugins::MinimumSpanningTreeToMesh::
+MinimumSpanningTreeToMesh( )
+ : Superclass( )
+{
+ this->_AddInput( "Input" );
+ this->_AddInput( "Seeds" );
+ this->_AddInput( "ReferenceImage" );
+ this->_AddOutput< cpPlugins::Interface::Mesh >( "Output" );
+
+ this->m_Parameters->ConfigureAsUint( "Kernel" );
+ this->m_Parameters->SetUint( "Kernel", 0 );
+}
+
+// -------------------------------------------------------------------------
+fpaPlugins::MinimumSpanningTreeToMesh::
+~MinimumSpanningTreeToMesh( )
+{
+}
+
+// -------------------------------------------------------------------------
+std::string fpaPlugins::MinimumSpanningTreeToMesh::
+_GenerateData( )
+{
+ typedef itk::ImageBase< 2 > _2D;
+ typedef itk::ImageBase< 3 > _3D;
+
+ cpPlugins::Interface::Image* input =
+ this->GetInput< cpPlugins::Interface::Image >( "ReferenceImage" );
+
+ if( input == NULL )
+ return( "fpaPlugins::MinimumSpanningTreeToMesh: No reference image." );
+
+ _2D* im2d = input->GetITK< _2D >( );
+ _3D* im3d = input->GetITK< _3D >( );
+ if( im2d != NULL )
+ return( this->_GD0( im2d ) );
+ else if( im3d != NULL )
+ return( this->_GD0( im3d ) );
+ else
+ return( "fpaPlugins::MinimumSpanningTreeToMesh: Reference image type not supported." );
+}
+
+// -------------------------------------------------------------------------
+template< class I >
+std::string fpaPlugins::MinimumSpanningTreeToMesh::
+_GD0( I* image )
+{
+ typedef typename I::IndexType _V;
+ typedef typename I::PointType _P;
+ typedef itk::Functor::IndexLexicographicCompare< I::ImageDimension > _VC;
+ typedef fpa::Base::MinimumSpanningTree< _V, _VC > _MST;
+
+ // Get inputs
+ fpaPlugins::MinimumSpanningTree* mst_wrapper =
+ this->GetInput< fpaPlugins::MinimumSpanningTree >( "Input" );
+ if( mst_wrapper == NULL )
+ return( "fpaPlugins::MinimumSpanningTreeToMesh: No input MST" );
+ _MST* mst = mst_wrapper->GetITK< _MST >( );
+ if( mst == NULL )
+ return( "fpaPlugins::MinimumSpanningTreeToMesh: Input MST type not supported." );
+ cpPlugins::Interface::PointList* seeds =
+ this->GetInput< cpPlugins::Interface::PointList >( "Seeds" );
+ if( seeds == NULL )
+ return( "fpaPlugins::MinimumSpanningTreeToMesh: No given seeds." );
+ if( seeds->GetNumberOfPoints( ) < 2 )
+ return( "fpaPlugins::MinimumSpanningTreeToMesh: Not enough seeds (<2)." );
+
+ // Get output
+ cpPlugins::Interface::Mesh* out =
+ this->GetOutput< cpPlugins::Interface::Mesh >( "Output" );
+ vtkSmartPointer< vtkPolyData > pd = out->GetVTK< vtkPolyData >( );
+ if( pd.GetPointer( ) == NULL )
+ {
+ pd = vtkPolyData::New( );
+ out->SetVTK( pd );
+
+ } // fi
+
+ _P pa = seeds->GetPoint< _P >( 0 );
+ _P pb = seeds->GetPoint< _P >( 1 );
+ _V va, vb;
+ image->TransformPhysicalPointToIndex( pa, va );
+ image->TransformPhysicalPointToIndex( pb, vb );
+
+ std::vector< _P > path =
+ mst->GetPathFromImage(
+ va, vb, image,
+ this->m_Parameters->GetUint( "Kernel" )
+ );
+
+ vtkSmartPointer< vtkPoints > points =
+ vtkSmartPointer< vtkPoints >::New( );
+ vtkSmartPointer< vtkCellArray > array =
+ vtkSmartPointer< vtkCellArray >::New( );
+ for( unsigned int i = 0; i < path.size( ); ++i )
+ {
+ if( I::ImageDimension == 2 )
+ points->InsertNextPoint( path[ i ][ 0 ], path[ i ][ 1 ], 0 );
+ else if( I::ImageDimension == 3 )
+ points->InsertNextPoint(
+ path[ i ][ 0 ], path[ i ][ 1 ], path[ i ][ 2 ]
+ );
+ else
+ points->InsertNextPoint( 0, 0, 0 );
+ if( i > 0 )
+ {
+ array->InsertNextCell( 2 );
+ array->InsertCellPoint( i - 1 );
+ array->InsertCellPoint( i );
+
+ } // fi
+
+ } // rof
+ pd->SetPoints( points );
+ pd->SetLines( array );
+
+ return( "" );
+}
+
+// eof - $RCSfile$