30 #include <boost/lexical_cast.hpp>
31 #include <boost/shared_ptr.hpp>
33 #include "../../common/WAssert.h"
34 #include "../../common/WIOTools.h"
35 #include "../../common/WLogger.h"
36 #include "../../common/WStringUtils.h"
37 #include "../exceptions/WDHException.h"
38 #include "../exceptions/WDHIOFailure.h"
40 #include "WReaderMatrixSymVTK.h"
49 WAssert( table->size() == 0,
"Non-zero size indicates an error, since the vector will be filled IN HERE." );
54 ifs.open(
m_fname.c_str(), std::ifstream::in | std::ifstream::binary );
55 WAssert( ifs && !ifs.bad(),
"" );
57 std::vector< std::string > header;
61 for(
int i = 0; i < 4; ++i )
63 std::getline( ifs, line );
68 header.push_back( line );
71 catch(
const std::ios_base::failure &e )
73 throw WDHIOFailure( std::string(
"Reading first 4 lines of '" +
m_fname +
"': " + e.what() ) );
75 if( header[0] !=
"# vtk DataFile Version 3.0" )
77 wlog::warn(
"WReaderMatrixSymVTK" ) <<
"Wrong version string in file header found, expected: "
78 "\"# vtk DataFile Version 3.0\" but got: " << header[0];
80 if( header[2] !=
"BINARY" )
82 wlog::error(
"WReaderMatrixSymVTK" ) <<
"Wrong data format: BINARY expected but got: " << header[2];
85 if( header[3] !=
"FIELD WMatrixSym 1" )
87 wlog::error(
"WReaderMatrixSymVTK" ) <<
"Wrong field desc in file header found: " << header[3] <<
" but expected: \"FIELD WMatrixSym 1\"";
93 std::getline( ifs, line );
95 catch(
const std::ios_base::failure &e )
97 throw WDHIOFailure( std::string(
"Error reading ELEMENTS field '" +
m_fname +
"': " + e.what() ) );
99 namespace su = string_utils;
100 size_t numDistances = 0;
101 std::vector< std::string > tokens = su::tokenize( line );
102 if( tokens.size() != 4 || su::toLower( tokens.at( 3 ) ) !=
"float" )
104 throw WDHException( std::string(
"Invalid ELEMENTS declaration: " + line ) );
108 numDistances = boost::lexical_cast<
size_t >( tokens.at( 1 ) );
110 catch(
const boost::bad_lexical_cast &e )
112 throw WDHException( std::string(
"Invalid number of elements: " + tokens.at( 1 ) ) );
115 float *data =
new float[ numDistances ];
118 ifs.read( reinterpret_cast< char* >( data ),
sizeof(
float ) * numDistances );
120 catch(
const std::ios_base::failure &e )
122 throw WDHIOFailure( std::string(
"Error reading elements in VTK ELEMENTS field '" +
m_fname +
"': " + e.what() ) );
126 switchByteOrderOfArray( data, numDistances );
128 for(
size_t i = 0; i < numDistances; ++i )
130 table->push_back( static_cast< double >( data[ i ] ) );