OpenWalnut  1.5.0dev
WReaderFiberVTK.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 WREADERFIBERVTK_H
26 #define WREADERFIBERVTK_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/WDataSetFibers.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 fibers from a VTK file. For VTK just see http://www.vtk.org.
43  * Currently only a subset of the legacy format is supported: MedInria's
44  * Fib-VTK format. The byte order of the files is assumed to be big endian by
45  * default.
46  *
47  * \ingroup dataHandler
48  */
49 class WReaderFiberVTK : public WReader // NOLINT
50 {
51 /**
52 * Only UnitTests may be friends.
53 */
54 friend class WReaderFiberVTKTest;
55 public:
56  /**
57  * Constructs and makes a new VTK reader for separate thread start.
58  *
59  * \param fname File name where to read data from
60  * \throws WDHNoSuchFile
61  */
62  explicit WReaderFiberVTK( std::string fname );
63 
64  /**
65  * Destroys this instance and closes the file.
66  */
67  virtual ~WReaderFiberVTK() throw();
68 
69  /**
70  * Reads the fiber file and creates a dataset out of it.
71  *
72  * \return Reference to the dataset.
73  */
74  virtual std::shared_ptr< WDataSetFibers > read();
75 
76 protected:
77  /**
78  * Read VTK header from file.
79  *
80  * \throws WDHIOFailure, WDHParseError
81  */
82  void readHeader();
83 
84  /**
85  * Read VTK POINTS field from input stream.
86  */
87  void readPoints();
88 
89  /**
90  * Read VTK VALUES field from input stream.
91  */
92  void readValues();
93 
94  /**
95  * Read VTK LINES field from input stream.
96  */
97  void readLines();
98 
99  std::vector< std::string > m_header; //!< VTK header of the read file
100 
101  /**
102  * Stores for every point its x,y and z float value successivley.
103  * \note The i'th point starts at the 3*i index since every point consumes
104  * 3 elements.
105  */
106  std::shared_ptr< std::vector< float > > m_points;
107 
108  std::shared_ptr< std::vector< size_t > > m_fiberStartIndices; //!< Stores the start indices (in the point array) for every fiber
109 
110  std::shared_ptr< std::vector< size_t > > m_fiberLengths; //!< Stores the length of every fiber
111 
112  WDataSetFibers::VertexParemeterArray m_fiberParameters; //!< additional colors if found in file.
113 
114  /**
115  * Stores for every point the fiber where it belongs to.
116  * \note This vector has as many components as there are points, hence its
117  * length is just a third of the length of the points vector.
118  */
119  std::shared_ptr< std::vector< size_t > > m_pointFiberMapping;
120 
121  std::shared_ptr< std::ifstream > m_ifs; //!< Pointer to the input file stream reader.
122 
123 private:
124  /**
125  * Reads the next line from current position in stream of the fiber VTK file.
126  *
127  * \param desc In case of trouble while reading, this gives information in
128  * the error message about what was tried to read
129  * \throws WDHIOFailure, WDHParseError
130  * \return Next line as string.
131  */
132  std::string getLine( const std::string& desc );
133 
134  /**
135  * Try to cast from the given string to the template value T. If the cast fails a
136  * WDHParseError is thrown.
137  *
138  * \throws WDHParseError
139  * \param stringValue The string to cast from
140  * \param errMsg Error message incase the cast fails
141  * \return The casted value from the given string.
142  */
143  template< typename T > T getLexicalCast( std::string stringValue, const std::string& errMsg ) const;
144 };
145 
146 template< typename T > inline T WReaderFiberVTK::getLexicalCast( std::string stringValue, const std::string& errMsg ) const
147 {
148  T result;
149  try
150  {
151  result = string_utils::fromString< T >( stringValue );
152  }
153  catch( const std::exception &e )
154  {
155  throw WDHParseError( std::string( "Cast error in VTK fiber file: " + m_fname + ": " + errMsg + ": " + stringValue ) );
156  }
157 
158  return result;
159 }
160 #endif // WREADERFIBERVTK_H
Use this for IO error handling.
Definition: WDHParseError.h:38
Represents a simple set of WFibers.
Reads fibers from a VTK file.
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.
T getLexicalCast(std::string stringValue, const std::string &errMsg) const
Try to cast from the given string to the template value T.
WDataSetFibers::VertexParemeterArray m_fiberParameters
additional colors if found in file.
friend class WReaderFiberVTKTest
Only UnitTests may be friends.
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.
Definition: WReader.h:40
std::string m_fname
Absolute path of the file to read from.
Definition: WReader.h:68