]> Creatis software - cpPlugins.git/blobdiff - plugins/cpPluginsIO/MeshReader.cxx
...
[cpPlugins.git] / plugins / cpPluginsIO / MeshReader.cxx
diff --git a/plugins/cpPluginsIO/MeshReader.cxx b/plugins/cpPluginsIO/MeshReader.cxx
new file mode 100644 (file)
index 0000000..49b76a2
--- /dev/null
@@ -0,0 +1,106 @@
+#include <cpPluginsIO/MeshReader.h>
+#include <cpPlugins/Mesh.h>
+
+#include <cstring>
+
+#include <vtkPolyDataReader.h>
+#include <vtkOBJReader.h>
+#include <vtkSTLReader.h>
+
+// -------------------------------------------------------------------------
+cpPluginsIO::MeshReader::
+MeshReader( )
+  : Superclass( )
+{
+  this->_AddOutput< cpPlugins::Mesh >( "Output" );
+  this->m_Parameters.Clear( );
+  this->m_Parameters.ConfigureAsOpenFileName( "FileName" );
+  this->m_Parameters.ConfigureAsUint( "Dimension" );
+
+  std::vector< std::string > valid_types;
+  valid_types.push_back( "float" );
+  valid_types.push_back( "double" );
+  this->m_Parameters.ConfigureAsChoices( "ScalarType", valid_types );
+  this->m_Parameters.SetSelectedChoice( "ScalarType", "float" );
+
+  this->m_Parameters.SetUint( "Dimension", 3 );
+  this->m_Parameters.SetAcceptedFileExtensions(
+    "FileName",
+    "Mesh files (*.obj *.stl *.vtk)"
+    );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsIO::MeshReader::
+~MeshReader( )
+{
+}
+
+// -------------------------------------------------------------------------
+std::string cpPluginsIO::MeshReader::
+_GenerateData( )
+{
+  auto dim = this->m_Parameters.GetUint( "Dimension" );
+  if     ( dim == 2 ) return( this->_GD0< 2 >( ) );
+  else if( dim == 3 ) return( this->_GD0< 3 >( ) );
+  else                return( "MeshReader: Mesh dimension not supported." );
+}
+
+// -------------------------------------------------------------------------
+template< unsigned int _Dim >
+std::string cpPluginsIO::MeshReader::
+_GD0( )
+{
+  auto st = this->m_Parameters.GetSelectedChoice( "ScalarType" );
+  if     ( st == "float" )  return( this->_GD1< float, _Dim >( ) );
+  else if( st == "double" ) return( this->_GD1< double, _Dim >( ) );
+  else                      return( "MeshReader: Mesh type not supported." );
+}
+
+// -------------------------------------------------------------------------
+template< class _TScalar, unsigned int _Dim >
+std::string cpPluginsIO::MeshReader::
+_GD1( )
+{
+  auto 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( );
+
+    this->GetOutputData( "Output" )->SetVTK( stlr->GetOutput( ) );
+    return( "" );
+  }
+  else if( ext == "obj" )
+  {
+    vtkOBJReader* objr = this->_CreateVTK< vtkOBJReader >( );
+    objr->SetFileName( fname.c_str( ) );
+    objr->Update( );
+
+    this->GetOutputData( "Output" )->SetVTK( objr->GetOutput( ) );
+    return( "" );
+  }
+  else if( ext == "vtk" )
+  {
+    vtkPolyDataReader* pdr = this->_CreateVTK< vtkPolyDataReader >( );
+    pdr->SetFileName( fname.c_str( ) );
+    pdr->Update( );
+
+    this->GetOutputData( "Output" )->SetVTK( pdr->GetOutput( ) );
+    return( "" );
+  }
+  else
+    return( "MeshReader: Input file format not recognized." );
+}
+
+// eof - $RCSfile$