1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
10
// OpenWalnut is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
15
// OpenWalnut is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public License
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
23
//---------------------------------------------------------------------------
32
#include <boost/shared_ptr.hpp>
34
#include "core/common/WStringUtils.h"
35
#include "core/dataHandler/WDataSet.h"
36
#include "core/dataHandler/exceptions/WDHIOFailure.h"
37
#include "core/dataHandler/exceptions/WDHNoSuchFile.h"
38
#include "core/dataHandler/exceptions/WDHParseError.h"
40
#include "core/dataHandler/io/WReader.h"
43
* Reads simple VTK files. For VTK just see http://www.vtk.org.
44
* Currently only a subset of the legacy format is supported.
46
* Formats read: Original ASCII and BINARY. The xml format is not supported.
47
* Grids read are: STRUCTURED_POINTS
48
* Data types read: SCALARS, VECTORS, TENSORS
50
* Endianess is kept as on the platform, so for intel hosts, most files,
51
* which are typically stored in little endian, are not readable if they
54
* \ingroup dataHandler
56
class WReaderVTK : public WReader // NOLINT
58
friend class WReaderVTKTest;
61
* Constructs and makes a new VTK reader for separate thread start.
63
* \param fname File name where to read data from
64
* \throws WDHNoSuchFile
66
explicit WReaderVTK( std::string fname );
69
* Destroys this instance and closes the file.
71
virtual ~WReaderVTK() throw();
74
* Reads the fiber file and creates a dataset out of it.
76
* \return Reference to the dataset.
78
virtual boost::shared_ptr< WDataSet > read();
82
* Read VTK header from file.
84
* \return The offset where header ends, so we may skip this next operation.
85
* \throws WDHIOFailure, WDHParseError
90
* Read VTK POINT_DATA field from input stream.
95
* Read VTK STRUCTURED_POINTS field
97
void readStructuredPoints();
100
* Read VTK SCALARS field
101
* \post the data set pointer is set to the data set constructed of the current grid
102
* and the read scalar values
104
* the number of scalars to read
106
* the name of the data set that may be overwritten by information in the scalars line
108
void readScalars( size_t nbScalars, const std::string & name );
111
* Read VTK SCALARS field
112
* \post the data set pointer is set to the data set constructed of the current grid
113
* and the read vector values
115
* the number of vectors to read
117
* the name of the data set that may be overwritten by information in the vectors line
119
void readVectors( size_t nbVectors, const std::string & name );
122
* Read VTK TENSORS field
123
* \post the data set pointer is set to the data set constructed of the current grid
124
* and the read tensor values
126
* the number of tensors to read
128
* the name of the data set that may be overwritten by information in the tensors line
130
void readTensors( size_t nbTensors, const std::string & name );
133
std::vector< std::string > m_header; //!< VTK header of the read file
136
* Stores for every point its x,y and z float value successivley.
137
* \note The i'th point starts at the 3*i index since every point consumes
140
boost::shared_ptr< std::vector< float > > m_points;
142
//boost::shared_ptr< std::vector< size_t > > m_fiberStartIndices; //!< Stores the start indices (in the point array) for every fiber
144
//boost::shared_ptr< std::vector< size_t > > m_fiberLengths; //!< Stores the length of every fiber
147
* Stores for every point the fiber where it belongs to.
148
* \note This vector has as many components as there are points, hence its
149
* length is just a third of the length of the points vector.
151
//boost::shared_ptr< std::vector< size_t > > m_pointFiberMapping;
153
boost::shared_ptr< std::ifstream > m_ifs; //!< Pointer to the input file stream reader.
157
* Reads the next line from current position in stream of the fiber VTK file.
159
* \param desc In case of trouble while reading, this gives information in
160
* the error message about what was tried to read
161
* \throws WDHIOFailure, WDHParseError
162
* \return Next line as string.
164
std::string getLine( const std::string& desc );
167
* Try to cast from the given string to the template value T. If the cast fails a
168
* WDHParseError is thrown.
170
* \throws WDHParseError
171
* \param stringValue The string to cast from
172
* \param errMsg Error message incase the cast fails
173
* \return The casted value from the given string.
175
template< typename T > T getLexicalCast( std::string stringValue, const std::string& errMsg ) const;
180
* reference to the currently loading data set
182
boost::shared_ptr< WDataSet > newDataSet;
185
* reference to the currently loading grid
187
boost::shared_ptr< WGridRegular3D > newGrid;
190
* internal flag whether we read ascii or binary
195
template< typename T > inline T WReaderVTK::getLexicalCast( std::string stringValue, const std::string& errMsg ) const
200
result = string_utils::fromString< T >( stringValue );
202
catch( const std::exception &e )
204
throw WDHParseError( std::string( "Cast error in VTK file: " + m_fname + ": " + errMsg + ": " + stringValue ) );
209
#endif // WREADERVTK_H