OpenWalnut  1.5.0dev
WReaderVTK.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
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.
14 //
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.
19 //
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/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WREADERVTK_H
26 #define WREADERVTK_H
27 
28 #include <fstream>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 
33 
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"
39 #include "core/dataHandler/io/WReader.h"
40 
41 /**
42  * Reads simple VTK files. For VTK just see http://www.vtk.org.
43  * Currently only a subset of the legacy format is supported.
44  *
45  * Formats read: Original ASCII and BINARY. The xml format is not supported.
46  * Grids read are: STRUCTURED_POINTS, RECTILINEAR_GRID is interpreted as a grid with
47  * equal spacing per coordinate axis
48  * Data types read: SCALARS, VECTORS, TENSORS
49  *
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
52  * are binary files.
53  *
54  * \ingroup dataHandler
55  */
56 class WReaderVTK : public WReader // NOLINT
57 {
58  //! allow member access by the test class
59  friend class WReaderVTKTest;
60 
61 public:
62  /**
63  * Constructs and makes a new VTK reader for separate thread start.
64  *
65  * \param fname File name where to read data from
66  * \throws WDHNoSuchFile
67  */
68  explicit WReaderVTK( std::string fname );
69 
70  /**
71  * Destroys this instance and closes the file.
72  */
73  virtual ~WReaderVTK() throw();
74 
75  /**
76  * Reads the data file and creates a dataset out of it.
77  *
78  * \return Reference to the dataset.
79  */
80  virtual std::shared_ptr< WDataSet > read();
81 
82 protected:
83  /**
84  * Read VTK header from file. Sets the m_isASCII member accordingly.
85  *
86  * \return true, if the header was valid
87  */
88  bool readHeader();
89 
90  /**
91  * Read VTK Domain specification and create a matching grid.
92  *
93  * \return The constructed grid or an invalid pointer if any problem occurred.
94  */
95  std::shared_ptr< WGridRegular3D > readStructuredPoints();
96 
97  /**
98  * Read VTK Domain specification and create a matching grid.
99  *
100  * \return The constructed grid or an invalid pointer if any problem occurred.
101  */
102  std::shared_ptr< WGridRegular3D > readRectilinearGrid();
103 
104  /**
105  * Read domain information for structured points.
106  *
107  * \param grid The grid constructed from the domain information.
108  *
109  * \return The value set containing the values read from the file or an invalid pointer if anything went wrong.
110  */
111  std::shared_ptr< WValueSetBase > readData( std::shared_ptr< WGridRegular3D > const& grid );
112 
113  /**
114  * Read VTK SCALARS field
115  * \post the data set pointer is set to the data set constructed of the current grid
116  * and the read scalar values
117  * \param nbScalars
118  * the number of scalars to read
119  * \param name
120  * the name of the data set that may be overwritten by information in the scalars line
121  *
122  * \return The resulting value set.
123  */
124  std::shared_ptr< WValueSetBase > readScalars( size_t nbScalars, const std::string & name );
125 
126  /**
127  * Read VTK SCALARS field
128  * \post the data set pointer is set to the data set constructed of the current grid
129  * and the read vector values
130  * \param nbVectors
131  * the number of vectors to read
132  * \param name
133  * the name of the data set that may be overwritten by information in the vectors line
134  *
135  * \return The resulting value set.
136  */
137  std::shared_ptr< WValueSetBase > readVectors( size_t nbVectors, const std::string & name );
138 
139  /**
140  * Read HARDI data from the file.
141  *
142  * \param nbPoints The number of voxels.
143  * \param nbGradients The number of gradients for this HARDI dataset.
144  *
145  * \return The data read as a value set of floats.
146  */
147  std::shared_ptr< WValueSetBase > readHARDI( std::size_t nbPoints, std::size_t nbGradients, const std::string& /*name*/ );
148 
149  /**
150  * Read VTK TENSORS field
151  * \post the data set pointer is set to the data set constructed of the current grid
152  * and the read tensor values
153  * \param nbTensors
154  * the number of tensors to read
155  * \param name
156  * the name of the data set that may be overwritten by information in the tensors line
157  *
158  * \return The resulting value set.
159  */
160  std::shared_ptr< WValueSetBase > readTensors( size_t nbTensors, const std::string & name );
161 
162  std::vector< std::string > m_header; //!< VTK header of the read file
163 
164  std::shared_ptr< std::ifstream > m_ifs; //!< Pointer to the input file stream reader.
165 
166 private:
167  //! The types of domains supported so far.
169  {
170  //! structured points
172 
173  //! rectilinear grid
175 
176  //! anything else
178  };
179 
180  //! The types of attributes supported so far.
182  {
183  //! scalar data
185 
186  //! vector data
188 
189  //! tensor data
191 
192  //! array data
194 
195  //! anything else
197  };
198 
199  /**
200  * Reads the next line from current position in stream of the fiber VTK file.
201  *
202  * \param desc In case of trouble while reading, this gives information in
203  * the error message about what was tried to read
204  * \throws WDHIOFailure, WDHParseError
205  * \return Next line as string.
206  */
207  std::string getLine( const std::string& desc );
208 
209  /**
210  * Read the coordinates of the dataset's domain.
211  *
212  * \param name The name to look for in the first line.
213  * \param dim The number of coordinates.
214  * \param coords The resulting vector of coordinates.
215  */
216  void readCoords( std::string const& name, std::size_t dim, std::vector< float >& coords );
217 
218  /**
219  * Read values from the file. The m_isASCII flag must be initialized accordingly.
220  *
221  * \param values The values read from the file.
222  * \param numValues The number of values to read.
223  */
224  void readValuesFromFile( std::vector< float >& values, std::size_t numValues ); // NOLINT: is non-const ref since we write results to this vector
225 
226  /**
227  * Read gradients from a gradients file which has the same basename as the data file and ends in .bvec.
228  *
229  * \return A pointer to a vector of gradients.
230  */
231  std::shared_ptr< std::vector< WVector3d > > readGradients();
232 
233  /**
234  * Try to cast from the given string to the template value T. If the cast fails a
235  * WDHParseError is thrown.
236  *
237  * \throws WDHParseError
238  * \param stringValue The string to cast from
239  * \param errMsg Error message incase the cast fails
240  * \return The casted value from the given string.
241  */
242  template< typename T > T getLexicalCast( std::string stringValue, const std::string& errMsg ) const;
243 
244  /**
245  * reference to the currently loading data set
246  */
247  std::shared_ptr< WDataSet > m_newDataSet;
248 
249  /**
250  * reference to the currently loading grid
251  */
252  std::shared_ptr< WGridRegular3D > m_newGrid;
253 
254  /**
255  * internal flag whether we read ascii or binary
256  */
257  bool m_isASCII;
258 
259  //! The type of domain specified in the file.
261 
262  //! The type of the attributes first read from the file.
264 };
265 
266 template< typename T > inline T WReaderVTK::getLexicalCast( std::string stringValue, const std::string& errMsg ) const
267 {
268  T result;
269  try
270  {
271  result = string_utils::fromString< T >( stringValue );
272  }
273  catch( const std::exception &e )
274  {
275  throw WDHParseError( std::string( "Cast error in VTK file: " + m_fname + ": " + errMsg + ": " + stringValue ) );
276  }
277 
278  return result;
279 }
280 
281 #endif // WREADERVTK_H
Use this for IO error handling.
Definition: WDHParseError.h:38
Base class for all data set types.
Definition: WDataSet.h:50
A grid that has parallelepiped cells which all have the same proportion.
Reads simple VTK files.
Definition: WReaderVTK.h:57
std::shared_ptr< WValueSetBase > readVectors(size_t nbVectors, const std::string &name)
Read VTK SCALARS field.
Definition: WReaderVTK.cpp:388
AttributeType
The types of attributes supported so far.
Definition: WReaderVTK.h:182
@ VECTORS
vector data
Definition: WReaderVTK.h:187
@ TENSORS
tensor data
Definition: WReaderVTK.h:190
@ UNSUPPORTED_ATTRIBUTE
anything else
Definition: WReaderVTK.h:196
@ SCALARS
scalar data
Definition: WReaderVTK.h:184
@ ARRAYS
array data
Definition: WReaderVTK.h:193
std::shared_ptr< std::ifstream > m_ifs
Pointer to the input file stream reader.
Definition: WReaderVTK.h:164
WReaderVTK(std::string fname)
Constructs and makes a new VTK reader for separate thread start.
Definition: WReaderVTK.cpp:49
void readCoords(std::string const &name, std::size_t dim, std::vector< float > &coords)
Read the coordinates of the dataset's domain.
Definition: WReaderVTK.cpp:316
std::shared_ptr< WGridRegular3D > readStructuredPoints()
Read VTK Domain specification and create a matching grid.
Definition: WReaderVTK.cpp:245
std::shared_ptr< WDataSet > m_newDataSet
reference to the currently loading data set
Definition: WReaderVTK.h:247
bool readHeader()
Read VTK header from file.
Definition: WReaderVTK.cpp:186
std::shared_ptr< WValueSetBase > readTensors(size_t nbTensors, const std::string &name)
Read VTK TENSORS field.
Definition: WReaderVTK.cpp:418
DomainType
The types of domains supported so far.
Definition: WReaderVTK.h:169
@ RECTILINEAR_GRID
rectilinear grid
Definition: WReaderVTK.h:174
@ STRUCTURED_POINTS
structured points
Definition: WReaderVTK.h:171
@ UNSUPPORTED_DOMAIN
anything else
Definition: WReaderVTK.h:177
std::shared_ptr< WGridRegular3D > readRectilinearGrid()
Read VTK Domain specification and create a matching grid.
Definition: WReaderVTK.cpp:289
T getLexicalCast(std::string stringValue, const std::string &errMsg) const
Try to cast from the given string to the template value T.
Definition: WReaderVTK.h:266
void readValuesFromFile(std::vector< float > &values, std::size_t numValues)
Read values from the file.
Definition: WReaderVTK.cpp:328
AttributeType m_attributeType
The type of the attributes first read from the file.
Definition: WReaderVTK.h:263
bool m_isASCII
internal flag whether we read ascii or binary
Definition: WReaderVTK.h:257
std::shared_ptr< WValueSetBase > readHARDI(std::size_t nbPoints, std::size_t nbGradients, const std::string &)
Read HARDI data from the file.
Definition: WReaderVTK.cpp:463
std::shared_ptr< std::vector< WVector3d > > readGradients()
Read gradients from a gradients file which has the same basename as the data file and ends in ....
Definition: WReaderVTK.cpp:134
std::shared_ptr< WValueSetBase > readScalars(size_t nbScalars, const std::string &name)
Read VTK SCALARS field.
Definition: WReaderVTK.cpp:368
DomainType m_domainType
The type of domain specified in the file.
Definition: WReaderVTK.h:260
virtual std::shared_ptr< WDataSet > read()
Reads the data file and creates a dataset out of it.
Definition: WReaderVTK.cpp:58
virtual ~WReaderVTK()
Destroys this instance and closes the file.
Definition: WReaderVTK.cpp:54
std::shared_ptr< WValueSetBase > readData(std::shared_ptr< WGridRegular3D > const &grid)
Read domain information for structured points.
Definition: WReaderVTK.cpp:493
std::shared_ptr< WGridRegular3D > m_newGrid
reference to the currently loading grid
Definition: WReaderVTK.h:252
std::string getLine(const std::string &desc)
Reads the next line from current position in stream of the fiber VTK file.
Definition: WReaderVTK.cpp:545
friend class WReaderVTKTest
allow member access by the test class
Definition: WReaderVTK.h:59
std::vector< std::string > m_header
VTK header of the read file.
Definition: WReaderVTK.h:162
Read some data from a given file.
Definition: WReader.h:40
std::string m_fname
Absolute path of the file to read from.
Definition: WReader.h:68
Abstract base class to all WValueSets.
Definition: WValueSetBase.h:60