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