]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Plugins/IO/MeshReader.cxx
...
[cpPlugins.git] / lib / cpPlugins / Plugins / IO / MeshReader.cxx
index 67069046163d768395fc55d2e23c5b8a871f6497..943d3ac6badb87b07d77833a75dc30ac72274212 100644 (file)
@@ -1,36 +1,37 @@
 #include "MeshReader.h"
 #include <cpPlugins/Interface/Mesh.h>
 
+#include <cstring>
+
 #include <vtkPolyData.h>
 #include <vtkPolyDataReader.h>
+#include <vtkSTLReader.h>
 
 // -------------------------------------------------------------------------
 cpPlugins::IO::MeshReader::
 MeshReader( )
-  : Superclass( ),
-    m_Reader( NULL )
+  : Superclass( )
 {
-  this->m_ClassName = "cpPlugins::IO::MeshReader";
-  this->m_ClassCategory = "MeshReader";
+  this->_AddOutput< cpPlugins::Interface::Mesh >( "Output" );
 
-  this->SetNumberOfOutputs( 1 );
-  this->_MakeOutput< cpPlugins::Interface::Mesh >( 0 );
+  std::vector< TParameters::TString > valid_types;
+  valid_types.push_back( "float" );
+  valid_types.push_back( "double" );
+  this->m_Parameters->ConfigureAsOpenFileName( "FileName" );
+  this->m_Parameters->ConfigureAsChoices( "PixelType", valid_types );
+  this->m_Parameters->ConfigureAsUint( "Dimension" );
 
-  using namespace cpPlugins::Interface;
-  this->m_DefaultParameters.Configure( Parameters::String, "FileName" );
-  this->m_DefaultParameters.Configure( Parameters::String, "PixelType" );
-  this->m_DefaultParameters.Configure( Parameters::Uint, "Dimension" );
-  this->m_DefaultParameters.SetValueAsString( "PixelType", "float" );
-  this->m_DefaultParameters.SetValueAsUint( "Dimension", 3 );
-  this->m_Parameters = this->m_DefaultParameters;
+  this->m_Parameters->SetUint( "Dimension", 3 );
+  this->m_Parameters->SetAcceptedFileExtensions(
+    "FileName",
+    "Mesh files (*.vtk *.stl *.obj)"
+    );
 }
 
 // -------------------------------------------------------------------------
 cpPlugins::IO::MeshReader::
 ~MeshReader( )
 {
-  if( this->m_Reader != NULL )
-    this->m_Reader = NULL;
 }
 
 // -------------------------------------------------------------------------
@@ -38,7 +39,7 @@ std::string cpPlugins::IO::MeshReader::
 _GenerateData( )
 {
   using namespace cpPlugins::Interface;
-  Parameters::TUint dim = this->m_Parameters.GetValueAsUint( "Dimension" );
+  Parameters::TUint dim = this->m_Parameters->GetUint( "Dimension" );
   std::string r = "MeshReader: Mesh dimension not supported.";
   if( dim == 2 )
     r = this->_GD0< 2 >( );
@@ -53,7 +54,8 @@ std::string cpPlugins::IO::MeshReader::
 _GD0( )
 {
   using namespace cpPlugins::Interface;
-  Parameters::TString pt = this->m_Parameters.GetValueAsString( "PixelType" );
+  Parameters::TString pt =
+    this->m_Parameters->GetSelectedChoice( "PixelType" );
   std::string r = "MeshReader: Mesh pixel type not supported";
   if( pt == "float" )       r = this->_GD1< float, D >( );
   else if( pt == "double" ) r = this->_GD1< double, D >( );
@@ -66,25 +68,41 @@ std::string cpPlugins::IO::MeshReader::
 _GD1( )
 {
   // Get filename
-  using namespace cpPlugins::Interface;
-  Parameters::TString fname =
-    this->m_Parameters.GetValueAsString( "FileName" );
+  std::string fname = this->m_Parameters->GetOpenFileName( "FileName" );
+
+  // Get file extension
+  std::istringstream fname_str( fname );
+  std::string token, ext;
+  while( std::getline( fname_str, token, '.' ) )
+    ext = token;
+  std::transform( ext.begin( ), ext.end( ), ext.begin( ), tolower );
+
+  // Real read
+  if( ext == "stl" )
+  {
+    vtkSTLReader* stlr = this->_CreateVTK< vtkSTLReader >( );
+    stlr->SetFileName( fname.c_str( ) );
+    stlr->Update( );
 
-  if( this->m_Reader != NULL )
-    this->m_Reader->Delete( );
+    auto out = this->GetOutputData< cpPlugins::Interface::Mesh >( "Output" );
+    out->SetVTK( stlr->GetOutput( ) );
+    return( "" );
+  }
+  else if( ext == "obj" )
+  {
+    // TODO
+  }
+  else if( ext == "vtk" )
+  {
+    vtkPolyDataReader* pdr = this->_CreateVTK< vtkPolyDataReader >( );
+    pdr->SetFileName( fname.c_str( ) );
+    pdr->Update( );
 
-  vtkPolyDataReader* pdr = vtkPolyDataReader::New( );
-  this->m_Reader = pdr;
-  pdr->SetFileName( fname.c_str( ) );
-  pdr->Update( );
+    auto out = this->GetOutputData< cpPlugins::Interface::Mesh >( "Output" );
+    out->SetVTK( pdr->GetOutput( ) );
+    return( "" );
 
-  cpPlugins::Interface::Mesh* out =
-    this->GetOutput< cpPlugins::Interface::Mesh >( 0 );
-  if( out != NULL )
-    out->SetVTKMesh( pdr->GetOutput( ) );
-  else
-    return( "MeshReader: output not correctly created." );
-  return( "" );
+  } // fi
 }
 
 // eof - $RCSfile$