]> Creatis software - cpPlugins.git/blob - plugins/cpPluginsIO/MeshReader.cxx
eead3dae5e4141760e5859407370d1c7cfa9f4d1
[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 void cpPluginsIO::MeshReader::
41 _GenerateData( )
42 {
43   auto d = this->m_Parameters.GetUint( "Dimension" );
44   if     ( d == 2 ) this->_GD0< 2 >( );
45   else if( d == 3 ) this->_GD0< 3 >( );
46   else              this->_Error( "Mesh dimension not supported." );
47 }
48
49 // -------------------------------------------------------------------------
50 template< unsigned int _Dim >
51 void cpPluginsIO::MeshReader::
52 _GD0( )
53 {
54   auto s = this->m_Parameters.GetSelectedChoice( "ScalarType" );
55   if     ( s == "float" )  this->_GD1< float, _Dim >( );
56   else if( s == "double" ) this->_GD1< double, _Dim >( );
57   else                     this->_Error( "Mesh type not supported." );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TScalar, unsigned int _Dim >
62 void 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   }
83   else if( ext == "obj" )
84   {
85     vtkOBJReader* objr = this->_CreateVTK< vtkOBJReader >( );
86     objr->SetFileName( fname.c_str( ) );
87     objr->Update( );
88
89     this->GetOutputData( "Output" )->SetVTK( objr->GetOutput( ) );
90   }
91   else if( ext == "vtk" )
92   {
93     vtkPolyDataReader* pdr = this->_CreateVTK< vtkPolyDataReader >( );
94     pdr->SetFileName( fname.c_str( ) );
95     pdr->Update( );
96
97     this->GetOutputData( "Output" )->SetVTK( pdr->GetOutput( ) );
98   }
99   else
100     this->_Error( "Input file format not recognized." );
101 }
102
103 // eof - $RCSfile$