32 #include "../../common/WAssert.h"
33 #include "../../common/WIOTools.h"
34 #include "../../common/WLogger.h"
35 #include "../../common/WStringUtils.h"
36 #include "../exceptions/WDHException.h"
37 #include "../exceptions/WDHIOFailure.h"
39 #include "WReaderMatrixSymVTK.h"
48 WAssert( table->size() == 0,
"Non-zero size indicates an error, since the vector will be filled IN HERE." );
53 ifs.open(
m_fname.c_str(), std::ifstream::in | std::ifstream::binary );
54 WAssert( ifs && !ifs.bad(),
"" );
56 std::vector< std::string > header;
60 for(
int i = 0; i < 4; ++i )
62 std::getline( ifs, line );
67 header.push_back( line );
70 catch(
const std::ios_base::failure &e )
72 throw WDHIOFailure( std::string(
"Reading first 4 lines of '" +
m_fname +
"': " + e.what() ) );
74 if( header[0] !=
"# vtk DataFile Version 3.0" )
76 wlog::warn(
"WReaderMatrixSymVTK" ) <<
"Wrong version string in file header found, expected: "
77 "\"# vtk DataFile Version 3.0\" but got: " << header[0];
79 if( header[2] !=
"BINARY" )
81 wlog::error(
"WReaderMatrixSymVTK" ) <<
"Wrong data format: BINARY expected but got: " << header[2];
84 if( header[3] !=
"FIELD WMatrixSym 1" )
86 wlog::error(
"WReaderMatrixSymVTK" ) <<
"Wrong field desc in file header found: " << header[3] <<
" but expected: \"FIELD WMatrixSym 1\"";
92 std::getline( ifs, line );
94 catch(
const std::ios_base::failure &e )
96 throw WDHIOFailure( std::string(
"Error reading ELEMENTS field '" +
m_fname +
"': " + e.what() ) );
99 size_t numDistances = 0;
100 std::vector< std::string > tokens = su::tokenize( line );
101 if( tokens.size() != 4 || su::toLower( tokens.at( 3 ) ) !=
"float" )
103 throw WDHException( std::string(
"Invalid ELEMENTS declaration: " + line ) );
107 numDistances = string_utils::fromString< size_t >( tokens.at( 1 ) );
109 catch(
const std::exception &e )
111 throw WDHException( std::string(
"Invalid number of elements: " + tokens.at( 1 ) ) );
114 float *data =
new float[ numDistances ];
117 ifs.read(
reinterpret_cast< char*
>( data ),
sizeof(
float ) * numDistances );
119 catch(
const std::ios_base::failure &e )
121 throw WDHIOFailure( std::string(
"Error reading elements in VTK ELEMENTS field '" +
m_fname +
"': " + e.what() ) );
125 switchByteOrderOfArray( data, numDistances );
127 for(
size_t i = 0; i < numDistances; ++i )
129 table->push_back(
static_cast< double >( data[ i ] ) );
General purpose exception and therefore base class for all DataHandler related exceptions.
Use this for IO error handling.
void readTable(std::shared_ptr< std::vector< double > > table) const
Perform reading from the file.
WReaderMatrixSymVTK(std::string fname)
Creates a reader object for look up tables.
Read some data from a given file.
std::string m_fname
Absolute path of the file to read from.
Some utilities for string manipulation and output operations.
WStreamedLogger warn(const std::string &source)
Logging a warning message.
WStreamedLogger error(const std::string &source)
Logging an error message.