]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsIO/MeshReader.cxx
49b76a2406840e98f5a5c62ec323b2a3ad110cd4
[cpPlugins.git] / plugins / cpPluginsIO / MeshReader.cxx
1 #include <cpPluginsIO/MeshReader.h>
2 #include <cpPlugins/Mesh.h>
3
4 #include <cstring>
5
6 #include <vtkPolyDataReader.h>
7 #include <vtkOBJReader.h>
8 #include <vtkSTLReader.h>
9
10 // -------------------------------------------------------------------------
11 cpPluginsIO::MeshReader::
12 MeshReader( )
13   : Superclass( )
14 {
15   this->_AddOutput< cpPlugins::Mesh >( "Output" );
16   this->m_Parameters.Clear( );
17   this->m_Parameters.ConfigureAsOpenFileName( "FileName" );
18   this->m_Parameters.ConfigureAsUint( "Dimension" );
19
20   std::vector< std::string > valid_types;
21   valid_types.push_back( "float" );
22   valid_types.push_back( "double" );
23   this->m_Parameters.ConfigureAsChoices( "ScalarType", valid_types );
24   this->m_Parameters.SetSelectedChoice( "ScalarType", "float" );
25
26   this->m_Parameters.SetUint( "Dimension", 3 );
27   this->m_Parameters.SetAcceptedFileExtensions(
28     "FileName",
29     "Mesh files (*.obj *.stl *.vtk)"
30     );
31 }
32
33 // -------------------------------------------------------------------------
34 cpPluginsIO::MeshReader::
35 ~MeshReader( )
36 {
37 }
38
39 // -------------------------------------------------------------------------
40 std::string cpPluginsIO::MeshReader::
41 _GenerateData( )
42 {
43   auto dim = this->m_Parameters.GetUint( "Dimension" );
44   if     ( dim == 2 ) return( this->_GD0< 2 >( ) );
45   else if( dim == 3 ) return( this->_GD0< 3 >( ) );
46   else                return( "MeshReader: Mesh dimension not supported." );
47 }
48
49 // -------------------------------------------------------------------------
50 template< unsigned int _Dim >
51 std::string cpPluginsIO::MeshReader::
52 _GD0( )
53 {
54   auto st = this->m_Parameters.GetSelectedChoice( "ScalarType" );
55   if     ( st == "float" )  return( this->_GD1< float, _Dim >( ) );
56   else if( st == "double" ) return( this->_GD1< double, _Dim >( ) );
57   else                      return( "MeshReader: Mesh type not supported." );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TScalar, unsigned int _Dim >
62 std::string cpPluginsIO::MeshReader::
63 _GD1( )
64 {
65   auto fname = this->m_Parameters.GetOpenFileName( "FileName" );
66
67   // Get file extension
68   std::istringstream fname_str( fname );
69   std::string token, ext;
70   while( std::getline( fname_str, token, '.' ) )
71     ext = token;
72   std::transform( ext.begin( ), ext.end( ), ext.begin( ), tolower );
73
74   // Real read
75   if( ext == "stl" )
76   {
77     vtkSTLReader* stlr = this->_CreateVTK< vtkSTLReader >( );
78     stlr->SetFileName( fname.c_str( ) );
79     stlr->Update( );
80
81     this->GetOutputData( "Output" )->SetVTK( stlr->GetOutput( ) );
82     return( "" );
83   }
84   else if( ext == "obj" )
85   {
86     vtkOBJReader* objr = this->_CreateVTK< vtkOBJReader >( );
87     objr->SetFileName( fname.c_str( ) );
88     objr->Update( );
89
90     this->GetOutputData( "Output" )->SetVTK( objr->GetOutput( ) );
91     return( "" );
92   }
93   else if( ext == "vtk" )
94   {
95     vtkPolyDataReader* pdr = this->_CreateVTK< vtkPolyDataReader >( );
96     pdr->SetFileName( fname.c_str( ) );
97     pdr->Update( );
98
99     this->GetOutputData( "Output" )->SetVTK( pdr->GetOutput( ) );
100     return( "" );
101   }
102   else
103     return( "MeshReader: Input file format not recognized." );
104 }
105
106 // eof - $RCSfile$