WReaderVTK.h 8.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WREADERVTK_H
#define WREADERVTK_H

#include <fstream>
#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>

34
#include "core/common/WStringUtils.h"
35 36 37 38 39 40 41 42 43 44 45 46
#include "core/dataHandler/WDataSet.h"
#include "core/dataHandler/exceptions/WDHIOFailure.h"
#include "core/dataHandler/exceptions/WDHNoSuchFile.h"
#include "core/dataHandler/exceptions/WDHParseError.h"

#include "core/dataHandler/io/WReader.h"

/**
 * Reads simple VTK files. For VTK just see http://www.vtk.org.
 * Currently only a subset of the legacy format is supported.
 *
 * Formats read: Original ASCII and BINARY. The xml format is not supported.
47 48
 * Grids read are: STRUCTURED_POINTS, RECTILINEAR_GRID is interpreted as a grid with
 *                 equal spacing per coordinate axis
49 50 51 52 53 54 55 56 57 58
 * Data types read: SCALARS, VECTORS, TENSORS
 *
 * Endianess is kept as on the platform, so for intel hosts, most files,
 * which are typically stored in little endian, are not readable if they
 * are binary files.
 *
 * \ingroup dataHandler
 */
class WReaderVTK : public WReader // NOLINT
{
59
    //! allow member access by the test class
60
    friend class WReaderVTKTest;
61 62

public:
63 64 65 66 67 68 69 70 71 72 73 74 75 76
    /**
     * Constructs and makes a new VTK reader for separate thread start.
     *
     * \param fname File name where to read data from
     * \throws WDHNoSuchFile
     */
    explicit WReaderVTK( std::string fname );

    /**
     * Destroys this instance and closes the file.
     */
    virtual ~WReaderVTK() throw();

    /**
77
     * Reads the data file and creates a dataset out of it.
78 79 80 81 82
     *
     * \return Reference to the dataset.
     */
    virtual boost::shared_ptr< WDataSet > read();

83
protected:
84
    /**
85
     * Read VTK header from file. Sets the m_isASCII member accordingly.
86
     *
87
     * \return true, if the header was valid
88
     */
89
    bool readHeader();
90 91

    /**
92 93 94
     * Read VTK Domain specification and create a matching grid.
     *
     * \return The constructed grid or an invalid pointer if any problem occurred.
95
     */
96
    boost::shared_ptr< WGridRegular3D > readStructuredPoints();
97 98

    /**
99 100 101
     * Read VTK Domain specification and create a matching grid.
     *
     * \return The constructed grid or an invalid pointer if any problem occurred.
102
     */
103 104 105 106 107 108 109 110 111 112
    boost::shared_ptr< WGridRegular3D > readRectilinearGrid();

    /**
     * Read domain information for structured points.
     *
     * \param grid The grid constructed from the domain information.
     *
     * \return The value set containing the values read from the file or an invalid pointer if anything went wrong.
     */
    boost::shared_ptr< WValueSetBase > readData( boost::shared_ptr< WGridRegular3D > const& grid );
113 114 115 116 117

    /**
     * Read VTK SCALARS field
     * \post the data set pointer is set to the data set constructed of the current grid
     *       and the read scalar values
118 119 120 121
     * \param nbScalars
     *      the number of scalars to read
     * \param name
     *      the name of the data set that may be overwritten by information in the scalars line
122 123
     *
     * \return The resulting value set.
124
     */
125
    boost::shared_ptr< WValueSetBase > readScalars( size_t nbScalars, const std::string & name );
126 127 128 129

    /**
     * Read VTK SCALARS field
     * \post the data set pointer is set to the data set constructed of the current grid
130 131 132 133 134
     *       and the read vector values
     * \param nbVectors
     *      the number of vectors to read
     * \param name
     *      the name of the data set that may be overwritten by information in the vectors line
135 136 137 138
     *
     * \return The resulting value set.
     */
    boost::shared_ptr< WValueSetBase > readVectors( size_t nbVectors, const std::string & name );
139 140 141 142

    /**
     * Read VTK TENSORS field
     * \post the data set pointer is set to the data set constructed of the current grid
143 144 145 146 147
     *       and the read tensor values
     * \param nbTensors
     *      the number of tensors to read
     * \param name
     *      the name of the data set that may be overwritten by information in the tensors line
148 149 150 151
     *
     * \return The resulting value set.
     */
    boost::shared_ptr< WValueSetBase > readTensors( size_t nbTensors, const std::string & name );
152 153 154

    std::vector< std::string > m_header; //!< VTK header of the read file

155
    boost::shared_ptr< std::ifstream > m_ifs; //!< Pointer to the input file stream reader.
156

157 158 159 160 161 162
private:
    //! The types of domains supported so far.
    enum DomainType
    {
        //! structured points
        STRUCTURED_POINTS,
163

164 165
        //! rectilinear grid
        RECTILINEAR_GRID,
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        //! anything else
        UNSUPPORTED_DOMAIN
    };

    //! The types of attributes supported so far.
    enum AttributeType
    {
        //! scalar data
        SCALARS,

        //! vector data
        VECTORS,

        //! tensor data
        TENSORS,

        //! array data
        ARRAYS,

        //! anything else
        UNSUPPORTED_ATTRIBUTE
    };
189 190 191 192 193 194 195 196 197 198 199

    /**
     * Reads the next line from current position in stream of the fiber VTK file.
     *
     * \param desc In case of trouble while reading, this gives information in
     * the error message about what was tried to read
     * \throws WDHIOFailure, WDHParseError
     * \return Next line as string.
     */
    std::string getLine( const std::string& desc );

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    /**
     * Read the coordinates of the dataset's domain.
     *
     * \param name The name to look for in the first line.
     * \param dim The number of coordinates.
     * \param coords The resulting vector of coordinates.
     */
    void readCoords( std::string const& name, std::size_t dim, std::vector< float >& coords );

    /**
     * Read values from the file. The m_isASCII flag must be initialized accordingly.
     *
     * \param values The values read from the file.
     * \param numValues The number of values to read.
     */
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
215
    void readValuesFromFile( std::vector< float >& values, std::size_t numValues ); // NOLINT: is non-const ref since we write results to this vector
216

217 218 219 220 221 222 223 224 225 226 227
    /**
     * Try to cast from the given string to the template value T. If the cast fails a
     * WDHParseError is thrown.
     *
     * \throws WDHParseError
     * \param stringValue The string to cast from
     * \param errMsg Error message incase the cast fails
     * \return The casted value from the given string.
     */
    template< typename T > T getLexicalCast( std::string stringValue, const std::string& errMsg ) const;

228 229 230
    /**
     * reference to the currently loading data set
     */
231
    boost::shared_ptr< WDataSet > m_newDataSet;
232 233 234 235

    /**
     * reference to the currently loading grid
     */
236
    boost::shared_ptr< WGridRegular3D > m_newGrid;
237

238 239 240
    /**
     * internal flag whether we read ascii or binary
     */
241 242 243 244 245 246 247
    bool m_isASCII;

    //! The type of domain specified in the file.
    DomainType m_domainType;

    //! The type of the attributes first read from the file.
    AttributeType m_attributeType;
248 249 250 251 252 253 254
};

template< typename T > inline T WReaderVTK::getLexicalCast( std::string stringValue, const std::string& errMsg ) const
{
    T result;
    try
    {
255
        result = string_utils::fromString< T >( stringValue );
256
    }
257
    catch( const std::exception &e )
258 259 260 261 262 263
    {
        throw WDHParseError( std::string( "Cast error in VTK file: " + m_fname + ": " + errMsg + ": " + stringValue ) );
    }

    return result;
}
264

265
#endif  // WREADERVTK_H