~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

Viewing changes to src/modules/data/io/WReaderVTK.h

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <string>
 
30
#include <vector>
 
31
 
 
32
#include <boost/shared_ptr.hpp>
 
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
 
 
40
#include "core/dataHandler/io/WReader.h"
 
41
 
 
42
/**
 
43
 * Reads simple VTK files. For VTK just see http://www.vtk.org.
 
44
 * Currently only a subset of the legacy format is supported.
 
45
 *
 
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
 
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
    friend class WReaderVTKTest;
 
59
    public:
 
60
    /**
 
61
     * Constructs and makes a new VTK reader for separate thread start.
 
62
     *
 
63
     * \param fname File name where to read data from
 
64
     * \throws WDHNoSuchFile
 
65
     */
 
66
    explicit WReaderVTK( std::string fname );
 
67
 
 
68
    /**
 
69
     * Destroys this instance and closes the file.
 
70
     */
 
71
    virtual ~WReaderVTK() throw();
 
72
 
 
73
    /**
 
74
     * Reads the fiber file and creates a dataset out of it.
 
75
     *
 
76
     * \return Reference to the dataset.
 
77
     */
 
78
    virtual boost::shared_ptr< WDataSet > read();
 
79
 
 
80
    protected:
 
81
    /**
 
82
     * Read VTK header from file.
 
83
     *
 
84
     * \return The offset where header ends, so we may skip this next operation.
 
85
     * \throws WDHIOFailure, WDHParseError
 
86
     */
 
87
    void readHeader();
 
88
 
 
89
    /**
 
90
     * Read VTK POINT_DATA field from input stream.
 
91
     */
 
92
    void readPointData();
 
93
 
 
94
    /**
 
95
     * Read VTK STRUCTURED_POINTS field
 
96
     */
 
97
    void readStructuredPoints();
 
98
 
 
99
    /**
 
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
 
103
     * \param nbScalars
 
104
     *      the number of scalars to read
 
105
     * \param name
 
106
     *      the name of the data set that may be overwritten by information in the scalars line
 
107
     */
 
108
    void readScalars( size_t nbScalars, const std::string & name );
 
109
 
 
110
    /**
 
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
 
114
     * \param nbVectors
 
115
     *      the number of vectors to read
 
116
     * \param name
 
117
     *      the name of the data set that may be overwritten by information in the vectors line
 
118
      */
 
119
    void readVectors( size_t nbVectors, const std::string & name );
 
120
 
 
121
    /**
 
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
 
125
     * \param nbTensors
 
126
     *      the number of tensors to read
 
127
     * \param name
 
128
     *      the name of the data set that may be overwritten by information in the tensors line
 
129
      */
 
130
    void readTensors( size_t nbTensors, const std::string & name );
 
131
 
 
132
 
 
133
    std::vector< std::string > m_header; //!< VTK header of the read file
 
134
 
 
135
    /**
 
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
 
138
     * 3 elements.
 
139
     */
 
140
    boost::shared_ptr< std::vector< float > > m_points;
 
141
 
 
142
    //boost::shared_ptr< std::vector< size_t > > m_fiberStartIndices; //!< Stores the start indices (in the point array) for every fiber
 
143
 
 
144
    //boost::shared_ptr< std::vector< size_t > > m_fiberLengths; //!< Stores the length of every fiber
 
145
 
 
146
    /**
 
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.
 
150
     */
 
151
    //boost::shared_ptr< std::vector< size_t > > m_pointFiberMapping;
 
152
 
 
153
    boost::shared_ptr< std::ifstream > m_ifs; //!< Pointer to the input file stream reader.
 
154
 
 
155
    private:
 
156
    /**
 
157
     * Reads the next line from current position in stream of the fiber VTK file.
 
158
     *
 
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.
 
163
     */
 
164
    std::string getLine( const std::string& desc );
 
165
 
 
166
    /**
 
167
     * Try to cast from the given string to the template value T. If the cast fails a
 
168
     * WDHParseError is thrown.
 
169
     *
 
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.
 
174
     */
 
175
    template< typename T > T getLexicalCast( std::string stringValue, const std::string& errMsg ) const;
 
176
 
 
177
 
 
178
 
 
179
    /**
 
180
     * reference to the currently loading data set
 
181
     */
 
182
    boost::shared_ptr< WDataSet > newDataSet;
 
183
 
 
184
    /**
 
185
     * reference to the currently loading grid
 
186
     */
 
187
    boost::shared_ptr< WGridRegular3D > newGrid;
 
188
 
 
189
    /**
 
190
     * internal flag whether we read ascii or binary
 
191
     */
 
192
    bool isAscii;
 
193
};
 
194
 
 
195
template< typename T > inline T WReaderVTK::getLexicalCast( std::string stringValue, const std::string& errMsg ) const
 
196
{
 
197
    T result;
 
198
    try
 
199
    {
 
200
        result = string_utils::fromString< T >( stringValue );
 
201
    }
 
202
    catch( const std::exception &e )
 
203
    {
 
204
        throw WDHParseError( std::string( "Cast error in VTK file: " + m_fname + ": " + errMsg + ": " + stringValue ) );
 
205
    }
 
206
 
 
207
    return result;
 
208
}
 
209
#endif  // WREADERVTK_H