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