1 #include <cpPluginsIO/MeshReader.h>
2 #include <cpPlugins/Mesh.h>
6 #include <vtkPolyDataReader.h>
7 #include <vtkOBJReader.h>
8 #include <vtkSTLReader.h>
10 // -------------------------------------------------------------------------
11 cpPluginsIO::MeshReader::
15 this->_AddOutput< cpPlugins::Mesh >( "Output" );
16 this->m_Parameters.Clear( );
17 this->m_Parameters.ConfigureAsOpenFileName( "FileName" );
18 this->m_Parameters.ConfigureAsUint( "Dimension" );
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" );
26 this->m_Parameters.SetUint( "Dimension", 3 );
27 this->m_Parameters.SetAcceptedFileExtensions(
29 "Mesh files (*.obj *.stl *.vtk)"
33 // -------------------------------------------------------------------------
34 cpPluginsIO::MeshReader::
39 // -------------------------------------------------------------------------
40 std::string cpPluginsIO::MeshReader::
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." );
49 // -------------------------------------------------------------------------
50 template< unsigned int _Dim >
51 std::string cpPluginsIO::MeshReader::
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." );
60 // -------------------------------------------------------------------------
61 template< class _TScalar, unsigned int _Dim >
62 std::string cpPluginsIO::MeshReader::
65 auto fname = this->m_Parameters.GetOpenFileName( "FileName" );
68 std::istringstream fname_str( fname );
69 std::string token, ext;
70 while( std::getline( fname_str, token, '.' ) )
72 std::transform( ext.begin( ), ext.end( ), ext.begin( ), tolower );
77 vtkSTLReader* stlr = this->_CreateVTK< vtkSTLReader >( );
78 stlr->SetFileName( fname.c_str( ) );
81 this->GetOutputData( "Output" )->SetVTK( stlr->GetOutput( ) );
84 else if( ext == "obj" )
86 vtkOBJReader* objr = this->_CreateVTK< vtkOBJReader >( );
87 objr->SetFileName( fname.c_str( ) );
90 this->GetOutputData( "Output" )->SetVTK( objr->GetOutput( ) );
93 else if( ext == "vtk" )
95 vtkPolyDataReader* pdr = this->_CreateVTK< vtkPolyDataReader >( );
96 pdr->SetFileName( fname.c_str( ) );
99 this->GetOutputData( "Output" )->SetVTK( pdr->GetOutput( ) );
103 return( "MeshReader: Input file format not recognized." );