33 #include "WReaderFiberVTK.h"
34 #include "core/common/WAssert.h"
35 #include "core/common/WIOTools.h"
36 #include "core/common/WLimits.h"
37 #include "core/common/WLogger.h"
38 #include "core/common/WStringUtils.h"
39 #include "core/dataHandler/WDataSetFibers.h"
40 #include "core/dataHandler/exceptions/WDHIOFailure.h"
41 #include "core/dataHandler/exceptions/WDHNoSuchFile.h"
42 #include "core/dataHandler/exceptions/WDHParseError.h"
55 m_ifs = std::shared_ptr< std::ifstream >(
new std::ifstream() );
56 m_ifs->open(
m_fname.c_str(), std::ifstream::in | std::ifstream::binary );
59 throw WDHIOFailure( std::string(
"internal error while opening file" ) );
77 for(
int i = 0; i < 4; ++i )
83 if(
m_header.at( 0 ) !=
"# vtk DataFile Version 3.0" )
85 wlog::error(
"WReaderFiberVTK" ) <<
"Unsupported format version string in VTK fiber file: "
92 wlog::warn(
"WReaderFiberVTK" ) <<
"Invalid header size of VTK fiber file: "
94 <<
", max. 256 but got: "
98 if( su::toUpper( su::trim(
m_header.at( 2 ) ) ) !=
"BINARY" )
100 wlog::error(
"WReaderFiberVTK" ) <<
"VTK files in '" <<
m_header.at( 2 ) <<
"' format are not yet supported";
102 if( su::tokenize(
m_header.at( 3 ) ).size() < 2 || su::toUpper( su::tokenize(
m_header.at( 3 ) )[1] ) !=
"POLYDATA" )
104 wlog::error(
"WReaderFiberVTK" ) <<
"Invalid fiber VTK DATASET type: " << su::tokenize(
m_header.back() )[1];
110 std::string line =
getLine(
"reading POINTS declaration" );
115 throw WDHParseError( std::string(
"Invalid POINTS declaration: " + line +
", expected float." ) );
118 size_t numPoints = getLexicalCast< size_t >( tokens.at( 1 ),
"Invalid number of points" );
120 float *pointData =
new float[ 3 * numPoints ];
121 m_ifs->read(
reinterpret_cast< char*
>( pointData ), 3 *
sizeof(
float ) * numPoints );
123 switchByteOrderOfArray( pointData, 3 * numPoints );
125 m_points = std::shared_ptr< std::vector< float > >(
new std::vector< float >( pointData, pointData + 3 * numPoints ) );
127 WAssert(
m_points->size() % 3 == 0,
"Number of floats for coordinates not dividable by three." );
131 m_pointFiberMapping = std::shared_ptr< std::vector< size_t > >(
new std::vector< size_t > );
134 line =
getLine(
"also eat the remaining newline after points declaration" );
135 WAssert( std::string(
"" ) == line,
"Found characters in file where nothing was expected." );
140 std::string line =
getLine(
"LINES declaration" );
145 throw WDHException( std::string(
"Invalid VTK LINES declaration: " + line ) );
147 size_t numLines = getLexicalCast< size_t >( tokens.at( 1 ),
"Invalid number of lines in LINES delclaration" );
148 size_t linesSize = getLexicalCast< size_t >( tokens.at( 2 ),
"Invalid size of lines in LINES delclaration" );
150 uint32_t *lineData =
new uint32_t[ linesSize ];
151 m_ifs->read(
reinterpret_cast<char*
>( lineData ), linesSize *
sizeof( uint32_t ) );
153 switchByteOrderOfArray( lineData, linesSize );
155 m_fiberStartIndices = std::shared_ptr< std::vector< size_t > >(
new std::vector< size_t > );
156 m_fiberLengths = std::shared_ptr< std::vector< size_t > >(
new std::vector< size_t > );
161 size_t linesSoFar = 0;
163 size_t posInVerts = 0;
164 while( linesSoFar < numLines )
167 size_t fiberLength = lineData[pos];
170 for(
size_t i = 0; i < fiberLength; ++i, ++pos, ++posInVerts )
179 line =
getLine(
"also eat the remaining newline after lines declaration" );
180 WAssert( std::string(
"" ) == line,
"Found characters in file where nothing was expected." );
189 std::string line =
getLine(
"reading VALUES declaration" );
198 size_t numValues = getLexicalCast< size_t >( tokens.at( 1 ),
"Invalid number of values" );
200 wlog::debug(
"ReaderFiberVTK" ) <<
"Found " << numValues <<
" values.";
202 double *valueData =
new double[ numValues ];
203 m_ifs->read(
reinterpret_cast< char*
>( valueData ),
sizeof(
double ) * numValues );
205 switchByteOrderOfArray( valueData, numValues );
209 for(
size_t i = 0; i < numValues; ++i )
216 line =
getLine(
"also eat the remaining newline after values declaration" );
217 WAssert( std::string(
"" ) == line,
"Found characters in file where nothing was expected." );
227 std::getline( *
m_ifs, line,
'\n' );
229 catch(
const std::ios_base::failure &e )
231 throw WDHIOFailure( std::string(
"IO error while " + desc +
" of VTK fiber file: " +
m_fname +
", " + e.what() ) );
General purpose exception and therefore base class for all DataHandler related exceptions.
Use this for IO error handling.
Use this for IO error handling.
Represents a simple set of WFibers.
std::shared_ptr< std::vector< double > > VertexParemeterArray
Parameter storage for each vertex.
std::string getLine(const std::string &desc)
Reads the next line from current position in stream of the fiber VTK file.
std::vector< std::string > m_header
VTK header of the read file.
virtual ~WReaderFiberVTK()
Destroys this instance and closes the file.
WDataSetFibers::VertexParemeterArray m_fiberParameters
additional colors if found in file.
void readValues()
Read VTK VALUES field from input stream.
std::shared_ptr< std::ifstream > m_ifs
Pointer to the input file stream reader.
std::shared_ptr< std::vector< float > > m_points
Stores for every point its x,y and z float value successivley.
std::shared_ptr< std::vector< size_t > > m_fiberStartIndices
Stores the start indices (in the point array) for every fiber.
std::shared_ptr< std::vector< size_t > > m_fiberLengths
Stores the length of every fiber.
void readPoints()
Read VTK POINTS field from input stream.
WReaderFiberVTK(std::string fname)
Constructs and makes a new VTK reader for separate thread start.
virtual std::shared_ptr< WDataSetFibers > read()
Reads the fiber file and creates a dataset out of it.
void readHeader()
Read VTK header from file.
std::shared_ptr< std::vector< size_t > > m_pointFiberMapping
Stores for every point the fiber where it belongs to.
void readLines()
Read VTK LINES field from input stream.
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.
std::string toLower(const std::string &source)
Transforms all characters in the given string into lower case characters.
std::string toUpper(const std::string &source)
Transforms all characters in the given string into upper case characters.
std::vector< std::string > tokenize(const std::string &source, const std::string &delim=WHITESPACE, bool compress=true)
Splits the given string into a vector of strings (so called tokens).
std::string toString(const T &value)
Convert a given value to a string.
WStreamedLogger debug(const std::string &source)
Logging a debug message.
WStreamedLogger warn(const std::string &source)
Logging a warning message.
WStreamedLogger error(const std::string &source)
Logging an error message.